blob: 7d26b4bfb2c6a400f3ccdfb5e435ae695ba8a150 [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>
Chris Mason39279cc2007-06-12 06:35:45 -04008#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>
Nikolay Borisovb1c16ac2019-04-01 11:29:57 +030031#include <linux/sched/mm.h>
David Sterba92d32172018-04-16 21:10:14 +020032#include <asm/unaligned.h>
David Sterba602cbe92019-08-21 18:48:25 +020033#include "misc.h"
Chris Mason39279cc2007-06-12 06:35:45 -040034#include "ctree.h"
35#include "disk-io.h"
36#include "transaction.h"
37#include "btrfs_inode.h"
Chris Mason39279cc2007-06-12 06:35:45 -040038#include "print-tree.h"
Chris Masone6dcd2d2008-07-17 12:53:50 -040039#include "ordered-data.h"
Christoph Hellwig95819c02008-08-28 06:21:17 -040040#include "xattr.h"
Chris Masone02119d2008-09-05 16:13:11 -040041#include "tree-log.h"
Jan Schmidt4a54c8c2011-07-22 15:41:52 +020042#include "volumes.h"
Chris Masonc8b97812008-10-29 14:49:59 -040043#include "compression.h"
Chris Masonb4ce94d2009-02-04 09:25:08 -050044#include "locking.h"
Josef Bacikdc89e982011-01-28 17:05:48 -050045#include "free-space-cache.h"
Li Zefan581bb052011-04-20 10:06:11 +080046#include "inode-map.h"
Filipe David Borba Manana63541922014-01-07 11:47:46 +000047#include "props.h"
Dongsheng Yang31193212014-12-12 16:44:35 +080048#include "qgroup.h"
Josef Bacik86736342019-06-19 15:12:00 -040049#include "delalloc-space.h"
Josef Bacikaac00232019-06-20 15:37:44 -040050#include "block-group.h"
Chris Mason39279cc2007-06-12 06:35:45 -040051
52struct btrfs_iget_args {
Chris Mason90d3e592014-01-09 17:28:00 -080053 struct btrfs_key *location;
Chris Mason39279cc2007-06-12 06:35:45 -040054 struct btrfs_root *root;
55};
56
Filipe Mananaf28a4922015-12-08 19:23:20 +000057struct btrfs_dio_data {
Filipe Mananaf28a4922015-12-08 19:23:20 +000058 u64 reserve;
59 u64 unsubmitted_oe_range_start;
60 u64 unsubmitted_oe_range_end;
Liu Bo4aaedfb2016-12-14 22:36:05 -080061 int overwrite;
Filipe Mananaf28a4922015-12-08 19:23:20 +000062};
63
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070064static const struct inode_operations btrfs_dir_inode_operations;
65static const struct inode_operations btrfs_symlink_inode_operations;
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070066static const struct inode_operations btrfs_special_inode_operations;
67static const struct inode_operations btrfs_file_inode_operations;
Alexey Dobriyan7f094102009-09-21 17:01:10 -070068static const struct address_space_operations btrfs_aops;
Alexey Dobriyan828c0952009-10-01 15:43:56 -070069static const struct file_operations btrfs_dir_file_operations;
David Sterba20e55062015-11-19 11:42:28 +010070static const struct extent_io_ops btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -040071
72static struct kmem_cache *btrfs_inode_cachep;
73struct kmem_cache *btrfs_trans_handle_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040074struct kmem_cache *btrfs_path_cachep;
Josef Bacikdc89e982011-01-28 17:05:48 -050075struct kmem_cache *btrfs_free_space_cachep;
Christophe Leroy3acd4852019-08-21 15:05:55 +000076struct kmem_cache *btrfs_free_space_bitmap_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040077
Eric Sandeen3972f262013-01-12 02:57:22 +000078static int btrfs_setsize(struct inode *inode, struct iattr *attr);
Filipe Manana213e8c52018-02-06 20:40:31 +000079static int btrfs_truncate(struct inode *inode, bool skip_writeback);
Josef Bacik5fd02042012-05-02 14:00:54 -040080static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent);
Chris Mason771ed682008-11-06 22:02:51 -050081static noinline int cow_file_range(struct inode *inode,
82 struct page *locked_page,
Nikolay Borisov74e91942019-07-17 16:18:16 +030083 u64 start, u64 end, int *page_started,
Nikolay Borisov330a5822019-07-17 16:18:17 +030084 unsigned long *nr_written, int unlock);
Liu Bo6f9994d2017-01-31 07:50:22 -080085static struct extent_map *create_io_em(struct inode *inode, u64 start, u64 len,
86 u64 orig_start, u64 block_start,
87 u64 block_len, u64 orig_block_len,
88 u64 ram_bytes, int compress_type,
89 int type);
Josef Bacik7b128762008-07-24 12:17:14 -040090
Qu Wenruo524272602017-03-08 10:25:52 +080091static void __endio_write_update_ordered(struct inode *inode,
92 const u64 offset, const u64 bytes,
93 const bool uptodate);
94
95/*
96 * Cleanup all submitted ordered extents in specified range to handle errors
Andrea Gelmini52042d82018-11-28 12:05:13 +010097 * from the btrfs_run_delalloc_range() callback.
Qu Wenruo524272602017-03-08 10:25:52 +080098 *
99 * NOTE: caller must ensure that when an error happens, it can not call
100 * extent_clear_unlock_delalloc() to clear both the bits EXTENT_DO_ACCOUNTING
101 * and EXTENT_DELALLOC simultaneously, because that causes the reserved metadata
102 * to be released, which we want to happen only when finishing the ordered
Nikolay Borisovd1051d62018-11-21 17:10:52 +0200103 * extent (btrfs_finish_ordered_io()).
Qu Wenruo524272602017-03-08 10:25:52 +0800104 */
105static inline void btrfs_cleanup_ordered_extents(struct inode *inode,
Nikolay Borisovd1051d62018-11-21 17:10:52 +0200106 struct page *locked_page,
107 u64 offset, u64 bytes)
Qu Wenruo524272602017-03-08 10:25:52 +0800108{
Naohiro Aota63d71450c2017-09-01 17:58:47 +0900109 unsigned long index = offset >> PAGE_SHIFT;
110 unsigned long end_index = (offset + bytes - 1) >> PAGE_SHIFT;
Nikolay Borisovd1051d62018-11-21 17:10:52 +0200111 u64 page_start = page_offset(locked_page);
112 u64 page_end = page_start + PAGE_SIZE - 1;
113
Naohiro Aota63d71450c2017-09-01 17:58:47 +0900114 struct page *page;
115
116 while (index <= end_index) {
117 page = find_get_page(inode->i_mapping, index);
118 index++;
119 if (!page)
120 continue;
121 ClearPagePrivate2(page);
122 put_page(page);
123 }
Nikolay Borisovd1051d62018-11-21 17:10:52 +0200124
125 /*
126 * In case this page belongs to the delalloc range being instantiated
127 * then skip it, since the first page of a range is going to be
128 * properly cleaned up by the caller of run_delalloc_range
129 */
130 if (page_start >= offset && page_end <= (offset + bytes - 1)) {
131 offset += PAGE_SIZE;
132 bytes -= PAGE_SIZE;
133 }
134
135 return __endio_write_update_ordered(inode, offset, bytes, false);
Qu Wenruo524272602017-03-08 10:25:52 +0800136}
137
Eric Sandeen48a3b632013-04-25 20:41:01 +0000138static int btrfs_dirty_inode(struct inode *inode);
Chris Mason39279cc2007-06-12 06:35:45 -0400139
Josef Bacik6a3891c2015-03-16 17:38:52 -0400140#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
141void btrfs_test_inode_set_ops(struct inode *inode)
142{
143 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
144}
145#endif
146
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000147static int btrfs_init_inode_security(struct btrfs_trans_handle *trans,
Eric Paris2a7dba32011-02-01 11:05:39 -0500148 struct inode *inode, struct inode *dir,
149 const struct qstr *qstr)
Jim Owens0279b4c2009-02-04 09:29:13 -0500150{
151 int err;
152
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000153 err = btrfs_init_acl(trans, inode, dir);
Jim Owens0279b4c2009-02-04 09:29:13 -0500154 if (!err)
Eric Paris2a7dba32011-02-01 11:05:39 -0500155 err = btrfs_xattr_security_init(trans, inode, dir, qstr);
Jim Owens0279b4c2009-02-04 09:29:13 -0500156 return err;
157}
158
Chris Masond352ac62008-09-29 15:18:18 -0400159/*
Chris Masonc8b97812008-10-29 14:49:59 -0400160 * this does all the hard work for inserting an inline extent into
161 * the btree. The caller should have done a btrfs_drop_extents so that
162 * no overlapping inline items exist in the btree
163 */
Chris Mason40f76582014-05-21 13:35:51 -0700164static int insert_inline_extent(struct btrfs_trans_handle *trans,
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000165 struct btrfs_path *path, int extent_inserted,
Chris Masonc8b97812008-10-29 14:49:59 -0400166 struct btrfs_root *root, struct inode *inode,
167 u64 start, size_t size, size_t compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000168 int compress_type,
Chris Masonc8b97812008-10-29 14:49:59 -0400169 struct page **compressed_pages)
170{
Chris Masonc8b97812008-10-29 14:49:59 -0400171 struct extent_buffer *leaf;
172 struct page *page = NULL;
173 char *kaddr;
174 unsigned long ptr;
175 struct btrfs_file_extent_item *ei;
Chris Masonc8b97812008-10-29 14:49:59 -0400176 int ret;
177 size_t cur_size = size;
Chris Masonc8b97812008-10-29 14:49:59 -0400178 unsigned long offset;
Chris Masonc8b97812008-10-29 14:49:59 -0400179
Jia-Ju Bai982f1f5d2019-07-27 16:51:13 +0800180 ASSERT((compressed_size > 0 && compressed_pages) ||
181 (compressed_size == 0 && !compressed_pages));
182
Li Zefanfe3f5662011-03-28 08:30:38 +0000183 if (compressed_size && compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400184 cur_size = compressed_size;
Chris Masonc8b97812008-10-29 14:49:59 -0400185
Chris Masonc8b97812008-10-29 14:49:59 -0400186 inode_add_bytes(inode, size);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000187
188 if (!extent_inserted) {
189 struct btrfs_key key;
190 size_t datasize;
191
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +0200192 key.objectid = btrfs_ino(BTRFS_I(inode));
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000193 key.offset = start;
David Sterba962a2982014-06-04 18:41:45 +0200194 key.type = BTRFS_EXTENT_DATA_KEY;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000195
196 datasize = btrfs_file_extent_calc_inline_size(cur_size);
197 path->leave_spinning = 1;
198 ret = btrfs_insert_empty_item(trans, root, path, &key,
199 datasize);
David Sterba79b4f4c2017-06-15 19:09:51 +0200200 if (ret)
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000201 goto fail;
Chris Masonc8b97812008-10-29 14:49:59 -0400202 }
203 leaf = path->nodes[0];
204 ei = btrfs_item_ptr(leaf, path->slots[0],
205 struct btrfs_file_extent_item);
206 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
207 btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
208 btrfs_set_file_extent_encryption(leaf, ei, 0);
209 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
210 btrfs_set_file_extent_ram_bytes(leaf, ei, size);
211 ptr = btrfs_file_extent_inline_start(ei);
212
Li Zefan261507a02010-12-17 14:21:50 +0800213 if (compress_type != BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -0400214 struct page *cpage;
215 int i = 0;
Chris Masond3977122009-01-05 21:25:51 -0500216 while (compressed_size > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -0400217 cpage = compressed_pages[i];
Chris Mason5b050f02008-11-11 09:34:41 -0500218 cur_size = min_t(unsigned long, compressed_size,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300219 PAGE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400220
Cong Wang7ac687d2011-11-25 23:14:28 +0800221 kaddr = kmap_atomic(cpage);
Chris Masonc8b97812008-10-29 14:49:59 -0400222 write_extent_buffer(leaf, kaddr, ptr, cur_size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800223 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400224
225 i++;
226 ptr += cur_size;
227 compressed_size -= cur_size;
228 }
229 btrfs_set_file_extent_compression(leaf, ei,
Li Zefan261507a02010-12-17 14:21:50 +0800230 compress_type);
Chris Masonc8b97812008-10-29 14:49:59 -0400231 } else {
232 page = find_get_page(inode->i_mapping,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300233 start >> PAGE_SHIFT);
Chris Masonc8b97812008-10-29 14:49:59 -0400234 btrfs_set_file_extent_compression(leaf, ei, 0);
Cong Wang7ac687d2011-11-25 23:14:28 +0800235 kaddr = kmap_atomic(page);
Johannes Thumshirn70730172018-12-05 15:23:03 +0100236 offset = offset_in_page(start);
Chris Masonc8b97812008-10-29 14:49:59 -0400237 write_extent_buffer(leaf, kaddr + offset, ptr, size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800238 kunmap_atomic(kaddr);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300239 put_page(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400240 }
241 btrfs_mark_buffer_dirty(leaf);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000242 btrfs_release_path(path);
Chris Masonc8b97812008-10-29 14:49:59 -0400243
Yan, Zhengc2167752009-11-12 09:34:21 +0000244 /*
245 * we're an inline extent, so nobody can
246 * extend the file past i_size without locking
247 * a page we already have locked.
248 *
249 * We must do any isize and inode updates
250 * before we unlock the pages. Otherwise we
251 * could end up racing with unlink.
252 */
Chris Masonc8b97812008-10-29 14:49:59 -0400253 BTRFS_I(inode)->disk_i_size = inode->i_size;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100254 ret = btrfs_update_inode(trans, root, inode);
Yan, Zhengc2167752009-11-12 09:34:21 +0000255
Chris Masonc8b97812008-10-29 14:49:59 -0400256fail:
David Sterba79b4f4c2017-06-15 19:09:51 +0200257 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400258}
259
260
261/*
262 * conditionally insert an inline extent into the file. This
263 * does the checks required to make sure the data is small enough
264 * to fit as an inline extent.
265 */
Nikolay Borisovd02c0e22018-03-02 09:43:15 +0200266static noinline int cow_file_range_inline(struct inode *inode, u64 start,
Josef Bacik00361582013-08-14 14:02:47 -0400267 u64 end, size_t compressed_size,
268 int compress_type,
269 struct page **compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400270{
Nikolay Borisovd02c0e22018-03-02 09:43:15 +0200271 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400272 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik00361582013-08-14 14:02:47 -0400273 struct btrfs_trans_handle *trans;
Chris Masonc8b97812008-10-29 14:49:59 -0400274 u64 isize = i_size_read(inode);
275 u64 actual_end = min(end + 1, isize);
276 u64 inline_len = actual_end - start;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400277 u64 aligned_end = ALIGN(end, fs_info->sectorsize);
Chris Masonc8b97812008-10-29 14:49:59 -0400278 u64 data_len = inline_len;
279 int ret;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000280 struct btrfs_path *path;
281 int extent_inserted = 0;
282 u32 extent_item_size;
Chris Masonc8b97812008-10-29 14:49:59 -0400283
284 if (compressed_size)
285 data_len = compressed_size;
286
287 if (start > 0 ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400288 actual_end > fs_info->sectorsize ||
289 data_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info) ||
Chris Masonc8b97812008-10-29 14:49:59 -0400290 (!compressed_size &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400291 (actual_end & (fs_info->sectorsize - 1)) == 0) ||
Chris Masonc8b97812008-10-29 14:49:59 -0400292 end + 1 < isize ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400293 data_len > fs_info->max_inline) {
Chris Masonc8b97812008-10-29 14:49:59 -0400294 return 1;
295 }
296
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000297 path = btrfs_alloc_path();
298 if (!path)
299 return -ENOMEM;
300
Josef Bacik00361582013-08-14 14:02:47 -0400301 trans = btrfs_join_transaction(root);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000302 if (IS_ERR(trans)) {
303 btrfs_free_path(path);
Josef Bacik00361582013-08-14 14:02:47 -0400304 return PTR_ERR(trans);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000305 }
Josef Bacik69fe2d72017-10-19 14:15:57 -0400306 trans->block_rsv = &BTRFS_I(inode)->block_rsv;
Josef Bacik00361582013-08-14 14:02:47 -0400307
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000308 if (compressed_size && compressed_pages)
309 extent_item_size = btrfs_file_extent_calc_inline_size(
310 compressed_size);
311 else
312 extent_item_size = btrfs_file_extent_calc_inline_size(
313 inline_len);
314
315 ret = __btrfs_drop_extents(trans, root, inode, path,
316 start, aligned_end, NULL,
317 1, 1, extent_item_size, &extent_inserted);
Josef Bacik00361582013-08-14 14:02:47 -0400318 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -0400319 btrfs_abort_transaction(trans, ret);
Josef Bacik00361582013-08-14 14:02:47 -0400320 goto out;
321 }
Chris Masonc8b97812008-10-29 14:49:59 -0400322
323 if (isize > actual_end)
324 inline_len = min_t(u64, isize, actual_end);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000325 ret = insert_inline_extent(trans, path, extent_inserted,
326 root, inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -0400327 inline_len, compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000328 compress_type, compressed_pages);
Josef Bacik2adcac12012-05-23 16:10:14 -0400329 if (ret && ret != -ENOSPC) {
Jeff Mahoney66642832016-06-10 18:19:25 -0400330 btrfs_abort_transaction(trans, ret);
Josef Bacik00361582013-08-14 14:02:47 -0400331 goto out;
Josef Bacik2adcac12012-05-23 16:10:14 -0400332 } else if (ret == -ENOSPC) {
Josef Bacik00361582013-08-14 14:02:47 -0400333 ret = 1;
334 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100335 }
Josef Bacik2adcac12012-05-23 16:10:14 -0400336
Josef Bacikbdc20e62013-02-28 13:23:38 -0500337 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +0200338 btrfs_drop_extent_cache(BTRFS_I(inode), start, aligned_end - 1, 0);
Josef Bacik00361582013-08-14 14:02:47 -0400339out:
Qu Wenruo94ed9382015-09-08 17:25:56 +0800340 /*
341 * Don't forget to free the reserved space, as for inlined extent
342 * it won't count as data extent, free them directly here.
343 * And at reserve time, it's always aligned to page size, so
344 * just free one page here.
345 */
Qu Wenruobc42bda2017-02-27 15:10:39 +0800346 btrfs_qgroup_free_data(inode, NULL, 0, PAGE_SIZE);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000347 btrfs_free_path(path);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -0400348 btrfs_end_transaction(trans);
Josef Bacik00361582013-08-14 14:02:47 -0400349 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400350}
351
Chris Mason771ed682008-11-06 22:02:51 -0500352struct async_extent {
353 u64 start;
354 u64 ram_size;
355 u64 compressed_size;
356 struct page **pages;
357 unsigned long nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800358 int compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500359 struct list_head list;
360};
361
Nikolay Borisov97db1202019-03-12 17:20:24 +0200362struct async_chunk {
Chris Mason771ed682008-11-06 22:02:51 -0500363 struct inode *inode;
Chris Mason771ed682008-11-06 22:02:51 -0500364 struct page *locked_page;
365 u64 start;
366 u64 end;
Liu Bof82b7352017-10-23 23:18:16 -0600367 unsigned int write_flags;
Chris Mason771ed682008-11-06 22:02:51 -0500368 struct list_head extents;
Chris Masonec39f762019-07-10 12:28:17 -0700369 struct cgroup_subsys_state *blkcg_css;
Chris Mason771ed682008-11-06 22:02:51 -0500370 struct btrfs_work work;
Nikolay Borisov97db1202019-03-12 17:20:24 +0200371 atomic_t *pending;
Chris Mason771ed682008-11-06 22:02:51 -0500372};
373
Nikolay Borisov97db1202019-03-12 17:20:24 +0200374struct async_cow {
375 /* Number of chunks in flight; must be first in the structure */
376 atomic_t num_chunks;
377 struct async_chunk chunks[];
378};
379
380static noinline int add_async_extent(struct async_chunk *cow,
Chris Mason771ed682008-11-06 22:02:51 -0500381 u64 start, u64 ram_size,
382 u64 compressed_size,
383 struct page **pages,
Li Zefan261507a02010-12-17 14:21:50 +0800384 unsigned long nr_pages,
385 int compress_type)
Chris Mason771ed682008-11-06 22:02:51 -0500386{
387 struct async_extent *async_extent;
388
389 async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100390 BUG_ON(!async_extent); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -0500391 async_extent->start = start;
392 async_extent->ram_size = ram_size;
393 async_extent->compressed_size = compressed_size;
394 async_extent->pages = pages;
395 async_extent->nr_pages = nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800396 async_extent->compress_type = compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500397 list_add_tail(&async_extent->list, &cow->extents);
398 return 0;
399}
400
Qu Wenruo42c16da2019-07-01 05:12:46 +0000401/*
402 * Check if the inode has flags compatible with compression
403 */
404static inline bool inode_can_compress(struct inode *inode)
405{
406 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW ||
407 BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
408 return false;
409 return true;
410}
411
412/*
413 * Check if the inode needs to be submitted to compression, based on mount
414 * options, defragmentation, properties or heuristics.
415 */
Timofey Titovetsc2fcdcd2017-07-17 16:52:58 +0300416static inline int inode_need_compress(struct inode *inode, u64 start, u64 end)
Wang Shilongf79707b2014-07-17 11:44:09 +0800417{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400418 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Wang Shilongf79707b2014-07-17 11:44:09 +0800419
Qu Wenruo42c16da2019-07-01 05:12:46 +0000420 if (!inode_can_compress(inode)) {
421 WARN(IS_ENABLED(CONFIG_BTRFS_DEBUG),
422 KERN_ERR "BTRFS: unexpected compression for ino %llu\n",
423 btrfs_ino(BTRFS_I(inode)));
424 return 0;
425 }
Wang Shilongf79707b2014-07-17 11:44:09 +0800426 /* force compress */
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400427 if (btrfs_test_opt(fs_info, FORCE_COMPRESS))
Wang Shilongf79707b2014-07-17 11:44:09 +0800428 return 1;
David Sterbaeec63c62017-07-17 19:41:31 +0200429 /* defrag ioctl */
430 if (BTRFS_I(inode)->defrag_compress)
431 return 1;
Wang Shilongf79707b2014-07-17 11:44:09 +0800432 /* bad compression ratios */
433 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS)
434 return 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400435 if (btrfs_test_opt(fs_info, COMPRESS) ||
Wang Shilongf79707b2014-07-17 11:44:09 +0800436 BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS ||
David Sterbab52aa8c2017-07-17 19:17:20 +0200437 BTRFS_I(inode)->prop_compress)
Timofey Titovetsc2fcdcd2017-07-17 16:52:58 +0300438 return btrfs_compress_heuristic(inode, start, end);
Wang Shilongf79707b2014-07-17 11:44:09 +0800439 return 0;
440}
441
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200442static inline void inode_should_defrag(struct btrfs_inode *inode,
Anand Jain26d30f82016-12-19 19:09:06 +0800443 u64 start, u64 end, u64 num_bytes, u64 small_write)
444{
445 /* If this is a small write inside eof, kick off a defrag */
446 if (num_bytes < small_write &&
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200447 (start > 0 || end + 1 < inode->disk_i_size))
Anand Jain26d30f82016-12-19 19:09:06 +0800448 btrfs_add_inode_defrag(NULL, inode);
449}
450
Chris Masonc8b97812008-10-29 14:49:59 -0400451/*
Chris Mason771ed682008-11-06 22:02:51 -0500452 * we create compressed extents in two phases. The first
453 * phase compresses a range of pages that have already been
454 * locked (both pages and state bits are locked).
Chris Masonc8b97812008-10-29 14:49:59 -0400455 *
Chris Mason771ed682008-11-06 22:02:51 -0500456 * This is done inside an ordered work queue, and the compression
457 * is spread across many cpus. The actual IO submission is step
458 * two, and the ordered work queue takes care of making sure that
459 * happens in the same order things were put onto the queue by
460 * writepages and friends.
Chris Masonc8b97812008-10-29 14:49:59 -0400461 *
Chris Mason771ed682008-11-06 22:02:51 -0500462 * If this code finds it can't get good compression, it puts an
463 * entry onto the work queue to write the uncompressed bytes. This
464 * makes sure that both compressed inodes and uncompressed inodes
Artem Bityutskiyb2570312012-07-25 18:12:06 +0300465 * are written in the same order that the flusher thread sent them
466 * down.
Chris Masond352ac62008-09-29 15:18:18 -0400467 */
Nikolay Borisovac3e9932019-07-17 14:41:44 +0300468static noinline int compress_file_range(struct async_chunk *async_chunk)
Chris Masonb888db22007-08-27 16:49:44 -0400469{
Nikolay Borisov1368c6d2019-03-12 17:20:27 +0200470 struct inode *inode = async_chunk->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400471 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400472 u64 blocksize = fs_info->sectorsize;
Nikolay Borisov1368c6d2019-03-12 17:20:27 +0200473 u64 start = async_chunk->start;
474 u64 end = async_chunk->end;
Chris Masonc8b97812008-10-29 14:49:59 -0400475 u64 actual_end;
Josef Bacikd98da492019-10-11 09:03:54 -0400476 u64 i_size;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400477 int ret = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400478 struct page **pages = NULL;
479 unsigned long nr_pages;
Chris Masonc8b97812008-10-29 14:49:59 -0400480 unsigned long total_compressed = 0;
481 unsigned long total_in = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400482 int i;
483 int will_compress;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400484 int compress_type = fs_info->compress_type;
Nikolay Borisovac3e9932019-07-17 14:41:44 +0300485 int compressed_extents = 0;
Chris Mason4adaa612013-03-26 13:07:00 -0400486 int redirty = 0;
Chris Masonb888db22007-08-27 16:49:44 -0400487
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200488 inode_should_defrag(BTRFS_I(inode), start, end, end - start + 1,
489 SZ_16K);
Chris Mason4cb53002011-05-24 15:35:30 -0400490
Josef Bacikd98da492019-10-11 09:03:54 -0400491 /*
492 * We need to save i_size before now because it could change in between
493 * us evaluating the size and assigning it. This is because we lock and
494 * unlock the page in truncate and fallocate, and then modify the i_size
495 * later on.
496 *
497 * The barriers are to emulate READ_ONCE, remove that once i_size_read
498 * does that for us.
499 */
500 barrier();
501 i_size = i_size_read(inode);
502 barrier();
503 actual_end = min_t(u64, i_size, end + 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400504again:
505 will_compress = 0;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300506 nr_pages = (end >> PAGE_SHIFT) - (start >> PAGE_SHIFT) + 1;
David Sterba069eac72017-02-14 19:36:54 +0100507 BUILD_BUG_ON((BTRFS_MAX_COMPRESSED % PAGE_SIZE) != 0);
508 nr_pages = min_t(unsigned long, nr_pages,
509 BTRFS_MAX_COMPRESSED / PAGE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400510
Chris Masonf03d9301f2009-02-04 09:31:06 -0500511 /*
512 * we don't want to send crud past the end of i_size through
513 * compression, that's just a waste of CPU time. So, if the
514 * end of the file is before the start of our current
515 * requested range of bytes, we bail out to the uncompressed
516 * cleanup code that can deal with all of this.
517 *
518 * It isn't really the fastest way to fix things, but this is a
519 * very uncommon corner.
520 */
521 if (actual_end <= start)
522 goto cleanup_and_bail_uncompressed;
523
Chris Masonc8b97812008-10-29 14:49:59 -0400524 total_compressed = actual_end - start;
525
Shilong Wang4bcbb332014-10-07 18:44:35 -0400526 /*
527 * skip compression for a small file range(<=blocksize) that
Nicholas D Steeves01327612016-05-19 21:18:45 -0400528 * isn't an inline extent, since it doesn't save disk space at all.
Shilong Wang4bcbb332014-10-07 18:44:35 -0400529 */
530 if (total_compressed <= blocksize &&
531 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
532 goto cleanup_and_bail_uncompressed;
533
David Sterba069eac72017-02-14 19:36:54 +0100534 total_compressed = min_t(unsigned long, total_compressed,
535 BTRFS_MAX_UNCOMPRESSED);
Chris Masonc8b97812008-10-29 14:49:59 -0400536 total_in = 0;
537 ret = 0;
Chris Masondb945352007-10-15 16:15:53 -0400538
Chris Mason771ed682008-11-06 22:02:51 -0500539 /*
540 * we do compression for mount -o compress and when the
541 * inode has not been flagged as nocompress. This flag can
542 * change at any time if we discover bad compression ratios.
Chris Masonc8b97812008-10-29 14:49:59 -0400543 */
Timofey Titovetsc2fcdcd2017-07-17 16:52:58 +0300544 if (inode_need_compress(inode, start, end)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400545 WARN_ON(pages);
David Sterba31e818f2015-02-20 18:00:26 +0100546 pages = kcalloc(nr_pages, sizeof(struct page *), GFP_NOFS);
Li Zefan560f7d72011-09-08 10:22:01 +0800547 if (!pages) {
548 /* just bail out to the uncompressed code */
Filipe Manana3527a012018-10-13 00:37:25 +0100549 nr_pages = 0;
Li Zefan560f7d72011-09-08 10:22:01 +0800550 goto cont;
551 }
Chris Mason179e29e2007-11-01 11:28:41 -0400552
David Sterbaeec63c62017-07-17 19:41:31 +0200553 if (BTRFS_I(inode)->defrag_compress)
554 compress_type = BTRFS_I(inode)->defrag_compress;
555 else if (BTRFS_I(inode)->prop_compress)
David Sterbab52aa8c2017-07-17 19:17:20 +0200556 compress_type = BTRFS_I(inode)->prop_compress;
Li Zefan261507a02010-12-17 14:21:50 +0800557
Chris Mason4adaa612013-03-26 13:07:00 -0400558 /*
559 * we need to call clear_page_dirty_for_io on each
560 * page in the range. Otherwise applications with the file
561 * mmap'd can wander in and change the page contents while
562 * we are compressing them.
563 *
564 * If the compression fails for any reason, we set the pages
565 * dirty again later on.
Timofey Titovetse9679de2017-10-24 01:29:48 +0300566 *
567 * Note that the remaining part is redirtied, the start pointer
568 * has moved, the end is the original one.
Chris Mason4adaa612013-03-26 13:07:00 -0400569 */
Timofey Titovetse9679de2017-10-24 01:29:48 +0300570 if (!redirty) {
571 extent_range_clear_dirty_for_io(inode, start, end);
572 redirty = 1;
573 }
David Sterbaf51d2b52017-09-15 17:36:57 +0200574
575 /* Compression level is applied here and only here */
576 ret = btrfs_compress_pages(
577 compress_type | (fs_info->compress_level << 4),
Li Zefan261507a02010-12-17 14:21:50 +0800578 inode->i_mapping, start,
David Sterba38c31462017-02-14 19:04:07 +0100579 pages,
David Sterba4d3a8002017-02-14 19:04:07 +0100580 &nr_pages,
Li Zefan261507a02010-12-17 14:21:50 +0800581 &total_in,
David Sterbae5d74902017-02-14 19:45:05 +0100582 &total_compressed);
Chris Masonc8b97812008-10-29 14:49:59 -0400583
584 if (!ret) {
Johannes Thumshirn70730172018-12-05 15:23:03 +0100585 unsigned long offset = offset_in_page(total_compressed);
David Sterba4d3a8002017-02-14 19:04:07 +0100586 struct page *page = pages[nr_pages - 1];
Chris Masonc8b97812008-10-29 14:49:59 -0400587 char *kaddr;
588
589 /* zero the tail end of the last page, we might be
590 * sending it down to disk
591 */
592 if (offset) {
Cong Wang7ac687d2011-11-25 23:14:28 +0800593 kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400594 memset(kaddr + offset, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300595 PAGE_SIZE - offset);
Cong Wang7ac687d2011-11-25 23:14:28 +0800596 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400597 }
598 will_compress = 1;
599 }
600 }
Li Zefan560f7d72011-09-08 10:22:01 +0800601cont:
Chris Masonc8b97812008-10-29 14:49:59 -0400602 if (start == 0) {
603 /* lets try to make an inline extent */
Timofey Titovets6018ba02017-09-15 01:57:26 +0300604 if (ret || total_in < actual_end) {
Chris Masonc8b97812008-10-29 14:49:59 -0400605 /* we didn't compress the entire range, try
Chris Mason771ed682008-11-06 22:02:51 -0500606 * to make an uncompressed inline extent.
Chris Masonc8b97812008-10-29 14:49:59 -0400607 */
Nikolay Borisovd02c0e22018-03-02 09:43:15 +0200608 ret = cow_file_range_inline(inode, start, end, 0,
609 BTRFS_COMPRESS_NONE, NULL);
Chris Masonc8b97812008-10-29 14:49:59 -0400610 } else {
Chris Mason771ed682008-11-06 22:02:51 -0500611 /* try making a compressed inline extent */
Nikolay Borisovd02c0e22018-03-02 09:43:15 +0200612 ret = cow_file_range_inline(inode, start, end,
Li Zefanfe3f5662011-03-28 08:30:38 +0000613 total_compressed,
614 compress_type, pages);
Chris Masonc8b97812008-10-29 14:49:59 -0400615 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100616 if (ret <= 0) {
Josef Bacik151a41b2013-07-29 13:22:24 -0400617 unsigned long clear_flags = EXTENT_DELALLOC |
Josef Bacik8b62f872017-10-19 14:15:55 -0400618 EXTENT_DELALLOC_NEW | EXTENT_DEFRAG |
619 EXTENT_DO_ACCOUNTING;
Filipe Mananae6eb4312014-10-10 10:45:12 +0100620 unsigned long page_error_op;
621
Filipe Mananae6eb4312014-10-10 10:45:12 +0100622 page_error_op = ret < 0 ? PAGE_SET_ERROR : 0;
Josef Bacik151a41b2013-07-29 13:22:24 -0400623
Chris Mason771ed682008-11-06 22:02:51 -0500624 /*
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100625 * inline extent creation worked or returned error,
626 * we don't need to create any more async work items.
627 * Unlock and free up our temp pages.
Josef Bacik8b62f872017-10-19 14:15:55 -0400628 *
629 * We use DO_ACCOUNTING here because we need the
630 * delalloc_release_metadata to be done _after_ we drop
631 * our outstanding extent for clearing delalloc for this
632 * range.
Chris Mason771ed682008-11-06 22:02:51 -0500633 */
Nikolay Borisov74e91942019-07-17 16:18:16 +0300634 extent_clear_unlock_delalloc(inode, start, end, NULL,
635 clear_flags,
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800636 PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -0400637 PAGE_CLEAR_DIRTY |
638 PAGE_SET_WRITEBACK |
Filipe Mananae6eb4312014-10-10 10:45:12 +0100639 page_error_op |
Josef Bacikc2790a22013-07-29 11:20:47 -0400640 PAGE_END_WRITEBACK);
Nikolay Borisovcecc8d92019-07-17 14:41:45 +0300641
642 for (i = 0; i < nr_pages; i++) {
643 WARN_ON(pages[i]->mapping);
644 put_page(pages[i]);
645 }
646 kfree(pages);
647
648 return 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400649 }
650 }
651
652 if (will_compress) {
653 /*
654 * we aren't doing an inline extent round the compressed size
655 * up to a block size boundary so the allocator does sane
656 * things
657 */
Qu Wenruofda28322013-02-26 08:10:22 +0000658 total_compressed = ALIGN(total_compressed, blocksize);
Chris Masonc8b97812008-10-29 14:49:59 -0400659
660 /*
661 * one last check to make sure the compression is really a
Timofey Titovets170607e2017-06-06 14:41:15 +0300662 * win, compare the page count read with the blocks on disk,
663 * compression must free at least one sector size
Chris Masonc8b97812008-10-29 14:49:59 -0400664 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300665 total_in = ALIGN(total_in, PAGE_SIZE);
Timofey Titovets170607e2017-06-06 14:41:15 +0300666 if (total_compressed + blocksize <= total_in) {
Nikolay Borisovac3e9932019-07-17 14:41:44 +0300667 compressed_extents++;
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700668
669 /*
670 * The async work queues will take care of doing actual
671 * allocation on disk for these compressed pages, and
672 * will submit them to the elevator.
673 */
Nikolay Borisovb5326272019-03-12 17:20:25 +0200674 add_async_extent(async_chunk, start, total_in,
David Sterba4d3a8002017-02-14 19:04:07 +0100675 total_compressed, pages, nr_pages,
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700676 compress_type);
677
Timofey Titovets11708622017-10-03 18:06:01 +0300678 if (start + total_in < end) {
679 start += total_in;
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700680 pages = NULL;
681 cond_resched();
682 goto again;
683 }
Nikolay Borisovac3e9932019-07-17 14:41:44 +0300684 return compressed_extents;
Chris Masonc8b97812008-10-29 14:49:59 -0400685 }
686 }
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700687 if (pages) {
Chris Masonc8b97812008-10-29 14:49:59 -0400688 /*
689 * the compression code ran but failed to make things smaller,
690 * free any pages it allocated and our page pointer array
691 */
David Sterba4d3a8002017-02-14 19:04:07 +0100692 for (i = 0; i < nr_pages; i++) {
Chris Mason70b99e62008-10-31 12:46:39 -0400693 WARN_ON(pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300694 put_page(pages[i]);
Chris Masonc8b97812008-10-29 14:49:59 -0400695 }
696 kfree(pages);
697 pages = NULL;
698 total_compressed = 0;
David Sterba4d3a8002017-02-14 19:04:07 +0100699 nr_pages = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400700
701 /* flag the file so we don't compress in the future */
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400702 if (!btrfs_test_opt(fs_info, FORCE_COMPRESS) &&
David Sterbab52aa8c2017-07-17 19:17:20 +0200703 !(BTRFS_I(inode)->prop_compress)) {
Chris Masona555f812010-01-28 16:18:15 -0500704 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
Chris Mason1e701a32010-03-11 09:42:04 -0500705 }
Chris Masonc8b97812008-10-29 14:49:59 -0400706 }
Chris Masonf03d9301f2009-02-04 09:31:06 -0500707cleanup_and_bail_uncompressed:
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700708 /*
709 * No compression, but we still need to write the pages in the file
710 * we've been given so far. redirty the locked page if it corresponds
711 * to our extent and set things up for the async work queue to run
712 * cow_file_range to do the normal delalloc dance.
713 */
Chris Mason1d53c9e2019-07-10 12:28:16 -0700714 if (async_chunk->locked_page &&
715 (page_offset(async_chunk->locked_page) >= start &&
716 page_offset(async_chunk->locked_page)) <= end) {
Nikolay Borisov1368c6d2019-03-12 17:20:27 +0200717 __set_page_dirty_nobuffers(async_chunk->locked_page);
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700718 /* unlocked later on in the async handlers */
Chris Mason1d53c9e2019-07-10 12:28:16 -0700719 }
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700720
721 if (redirty)
722 extent_range_redirty_for_io(inode, start, end);
Nikolay Borisovb5326272019-03-12 17:20:25 +0200723 add_async_extent(async_chunk, start, end - start + 1, 0, NULL, 0,
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700724 BTRFS_COMPRESS_NONE);
Nikolay Borisovac3e9932019-07-17 14:41:44 +0300725 compressed_extents++;
Chris Mason771ed682008-11-06 22:02:51 -0500726
Nikolay Borisovac3e9932019-07-17 14:41:44 +0300727 return compressed_extents;
Chris Mason771ed682008-11-06 22:02:51 -0500728}
Chris Mason771ed682008-11-06 22:02:51 -0500729
Filipe Manana40ae8372014-10-06 22:14:24 +0100730static void free_async_extent_pages(struct async_extent *async_extent)
731{
732 int i;
733
734 if (!async_extent->pages)
735 return;
736
737 for (i = 0; i < async_extent->nr_pages; i++) {
738 WARN_ON(async_extent->pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300739 put_page(async_extent->pages[i]);
Filipe Manana40ae8372014-10-06 22:14:24 +0100740 }
741 kfree(async_extent->pages);
742 async_extent->nr_pages = 0;
743 async_extent->pages = NULL;
Chris Mason771ed682008-11-06 22:02:51 -0500744}
745
746/*
747 * phase two of compressed writeback. This is the ordered portion
748 * of the code, which only gets called in the order the work was
749 * queued. We walk all the async extents created by compress_file_range
750 * and send them down to the disk.
751 */
Nikolay Borisovb5326272019-03-12 17:20:25 +0200752static noinline void submit_compressed_extents(struct async_chunk *async_chunk)
Chris Mason771ed682008-11-06 22:02:51 -0500753{
Nikolay Borisovb5326272019-03-12 17:20:25 +0200754 struct inode *inode = async_chunk->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400755 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason771ed682008-11-06 22:02:51 -0500756 struct async_extent *async_extent;
757 u64 alloc_hint = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500758 struct btrfs_key ins;
759 struct extent_map *em;
760 struct btrfs_root *root = BTRFS_I(inode)->root;
Nikolay Borisov43366502019-03-12 17:20:29 +0200761 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500762 int ret = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500763
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500764again:
Nikolay Borisovb5326272019-03-12 17:20:25 +0200765 while (!list_empty(&async_chunk->extents)) {
766 async_extent = list_entry(async_chunk->extents.next,
Chris Mason771ed682008-11-06 22:02:51 -0500767 struct async_extent, list);
768 list_del(&async_extent->list);
769
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500770retry:
Nikolay Borisov74475552019-03-12 17:20:30 +0200771 lock_extent(io_tree, async_extent->start,
772 async_extent->start + async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500773 /* did the compression code fall back to uncompressed IO? */
774 if (!async_extent->pages) {
775 int page_started = 0;
776 unsigned long nr_written = 0;
777
Chris Mason771ed682008-11-06 22:02:51 -0500778 /* allocate blocks */
Nikolay Borisovb5326272019-03-12 17:20:25 +0200779 ret = cow_file_range(inode, async_chunk->locked_page,
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500780 async_extent->start,
781 async_extent->start +
782 async_extent->ram_size - 1,
Nikolay Borisov330a5822019-07-17 16:18:17 +0300783 &page_started, &nr_written, 0);
Chris Mason771ed682008-11-06 22:02:51 -0500784
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100785 /* JDM XXX */
786
Chris Mason771ed682008-11-06 22:02:51 -0500787 /*
788 * if page_started, cow_file_range inserted an
789 * inline extent and took care of all the unlocking
790 * and IO for us. Otherwise, we need to submit
791 * all those pages down to the drive.
792 */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500793 if (!page_started && !ret)
Nikolay Borisov5e3ee232017-12-08 15:55:58 +0200794 extent_write_locked_range(inode,
795 async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500796 async_extent->start +
Chris Mason771ed682008-11-06 22:02:51 -0500797 async_extent->ram_size - 1,
Chris Mason771ed682008-11-06 22:02:51 -0500798 WB_SYNC_ALL);
Chris Mason1d53c9e2019-07-10 12:28:16 -0700799 else if (ret && async_chunk->locked_page)
Nikolay Borisovb5326272019-03-12 17:20:25 +0200800 unlock_page(async_chunk->locked_page);
Chris Mason771ed682008-11-06 22:02:51 -0500801 kfree(async_extent);
802 cond_resched();
803 continue;
804 }
805
Wang Xiaoguang18513092016-07-25 15:51:40 +0800806 ret = btrfs_reserve_extent(root, async_extent->ram_size,
Chris Mason771ed682008-11-06 22:02:51 -0500807 async_extent->compressed_size,
808 async_extent->compressed_size,
Miao Xiee570fd22014-06-19 10:42:50 +0800809 0, alloc_hint, &ins, 1, 1);
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500810 if (ret) {
Filipe Manana40ae8372014-10-06 22:14:24 +0100811 free_async_extent_pages(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500812
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400813 if (ret == -ENOSPC) {
814 unlock_extent(io_tree, async_extent->start,
815 async_extent->start +
816 async_extent->ram_size - 1);
Liu Boce620032014-07-24 22:48:05 +0800817
818 /*
819 * we need to redirty the pages if we decide to
820 * fallback to uncompressed IO, otherwise we
821 * will not submit these pages down to lower
822 * layers.
823 */
824 extent_range_redirty_for_io(inode,
825 async_extent->start,
826 async_extent->start +
827 async_extent->ram_size - 1);
828
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100829 goto retry;
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400830 }
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500831 goto out_free;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500832 }
Yan, Zhengc2167752009-11-12 09:34:21 +0000833 /*
834 * here we're doing allocation and writeback of the
835 * compressed pages
836 */
Liu Bo6f9994d2017-01-31 07:50:22 -0800837 em = create_io_em(inode, async_extent->start,
838 async_extent->ram_size, /* len */
839 async_extent->start, /* orig_start */
840 ins.objectid, /* block_start */
841 ins.offset, /* block_len */
842 ins.offset, /* orig_block_len */
843 async_extent->ram_size, /* ram_bytes */
844 async_extent->compress_type,
845 BTRFS_ORDERED_COMPRESSED);
846 if (IS_ERR(em))
847 /* ret value is not necessary due to void function */
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500848 goto out_free_reserve;
Liu Bo6f9994d2017-01-31 07:50:22 -0800849 free_extent_map(em);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500850
Li Zefan261507a02010-12-17 14:21:50 +0800851 ret = btrfs_add_ordered_extent_compress(inode,
852 async_extent->start,
853 ins.objectid,
854 async_extent->ram_size,
855 ins.offset,
856 BTRFS_ORDERED_COMPRESSED,
857 async_extent->compress_type);
Filipe Mananad9f85962014-08-25 10:43:00 +0100858 if (ret) {
Nikolay Borisovdcdbc052017-02-20 13:50:45 +0200859 btrfs_drop_extent_cache(BTRFS_I(inode),
860 async_extent->start,
Filipe Mananad9f85962014-08-25 10:43:00 +0100861 async_extent->start +
862 async_extent->ram_size - 1, 0);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500863 goto out_free_reserve;
Filipe Mananad9f85962014-08-25 10:43:00 +0100864 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400865 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Chris Mason771ed682008-11-06 22:02:51 -0500866
Chris Mason771ed682008-11-06 22:02:51 -0500867 /*
868 * clear dirty, set writeback and unlock the pages.
869 */
Josef Bacikc2790a22013-07-29 11:20:47 -0400870 extent_clear_unlock_delalloc(inode, async_extent->start,
Chris Masona791e352009-10-08 11:27:10 -0400871 async_extent->start +
872 async_extent->ram_size - 1,
Josef Bacik151a41b2013-07-29 13:22:24 -0400873 NULL, EXTENT_LOCKED | EXTENT_DELALLOC,
874 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -0400875 PAGE_SET_WRITEBACK);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200876 if (btrfs_submit_compressed_write(inode,
Chris Masond3977122009-01-05 21:25:51 -0500877 async_extent->start,
878 async_extent->ram_size,
879 ins.objectid,
880 ins.offset, async_extent->pages,
Liu Bof82b7352017-10-23 23:18:16 -0600881 async_extent->nr_pages,
Chris Masonec39f762019-07-10 12:28:17 -0700882 async_chunk->write_flags,
883 async_chunk->blkcg_css)) {
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100884 struct page *p = async_extent->pages[0];
885 const u64 start = async_extent->start;
886 const u64 end = start + async_extent->ram_size - 1;
887
888 p->mapping = inode->i_mapping;
Nikolay Borisovc6297322018-11-08 10:18:08 +0200889 btrfs_writepage_endio_finish_ordered(p, start, end, 0);
Nikolay Borisov7087a9d2018-11-01 14:09:48 +0200890
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100891 p->mapping = NULL;
Nikolay Borisov74e91942019-07-17 16:18:16 +0300892 extent_clear_unlock_delalloc(inode, start, end,
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800893 NULL, 0,
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100894 PAGE_END_WRITEBACK |
895 PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +0100896 free_async_extent_pages(async_extent);
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100897 }
Chris Mason771ed682008-11-06 22:02:51 -0500898 alloc_hint = ins.objectid + ins.offset;
899 kfree(async_extent);
900 cond_resched();
901 }
Filipe Mananadec8f172014-10-06 22:14:26 +0100902 return;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500903out_free_reserve:
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400904 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400905 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100906out_free:
Josef Bacikc2790a22013-07-29 11:20:47 -0400907 extent_clear_unlock_delalloc(inode, async_extent->start,
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500908 async_extent->start +
909 async_extent->ram_size - 1,
Josef Bacikc2790a22013-07-29 11:20:47 -0400910 NULL, EXTENT_LOCKED | EXTENT_DELALLOC |
Filipe Mananaa7e3b972017-04-03 10:45:46 +0100911 EXTENT_DELALLOC_NEW |
Josef Bacik151a41b2013-07-29 13:22:24 -0400912 EXTENT_DEFRAG | EXTENT_DO_ACCOUNTING,
913 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Filipe Manana704de492014-10-06 22:14:22 +0100914 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK |
915 PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +0100916 free_async_extent_pages(async_extent);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100917 kfree(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500918 goto again;
Chris Mason771ed682008-11-06 22:02:51 -0500919}
920
Josef Bacik4b46fce2010-05-23 11:00:55 -0400921static u64 get_extent_allocation_hint(struct inode *inode, u64 start,
922 u64 num_bytes)
923{
924 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
925 struct extent_map *em;
926 u64 alloc_hint = 0;
927
928 read_lock(&em_tree->lock);
929 em = search_extent_mapping(em_tree, start, num_bytes);
930 if (em) {
931 /*
932 * if block start isn't an actual block number then find the
933 * first block in this inode and use that as a hint. If that
934 * block is also bogus then just don't worry about it.
935 */
936 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
937 free_extent_map(em);
938 em = search_extent_mapping(em_tree, 0, 0);
939 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
940 alloc_hint = em->block_start;
941 if (em)
942 free_extent_map(em);
943 } else {
944 alloc_hint = em->block_start;
945 free_extent_map(em);
946 }
947 }
948 read_unlock(&em_tree->lock);
949
950 return alloc_hint;
951}
952
Chris Mason771ed682008-11-06 22:02:51 -0500953/*
954 * when extent_io.c finds a delayed allocation range in the file,
955 * the call backs end up in this code. The basic idea is to
956 * allocate extents on disk for the range, and create ordered data structs
957 * in ram to track those extents.
958 *
959 * locked_page is the page that writepage had locked already. We use
960 * it to make sure we don't do extra locks or unlocks.
961 *
962 * *page_started is set to one if we unlock locked_page and do everything
963 * required to start IO on it. It may be clean and already done with
964 * IO when we return.
965 */
Josef Bacik00361582013-08-14 14:02:47 -0400966static noinline int cow_file_range(struct inode *inode,
967 struct page *locked_page,
Nikolay Borisov74e91942019-07-17 16:18:16 +0300968 u64 start, u64 end, int *page_started,
Nikolay Borisov330a5822019-07-17 16:18:17 +0300969 unsigned long *nr_written, int unlock)
Chris Mason771ed682008-11-06 22:02:51 -0500970{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400971 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik00361582013-08-14 14:02:47 -0400972 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason771ed682008-11-06 22:02:51 -0500973 u64 alloc_hint = 0;
974 u64 num_bytes;
975 unsigned long ram_size;
Filipe Mananaa315e682017-03-06 23:04:20 +0000976 u64 cur_alloc_size = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400977 u64 blocksize = fs_info->sectorsize;
Chris Mason771ed682008-11-06 22:02:51 -0500978 struct btrfs_key ins;
979 struct extent_map *em;
Filipe Mananaa315e682017-03-06 23:04:20 +0000980 unsigned clear_bits;
981 unsigned long page_ops;
982 bool extent_reserved = false;
Chris Mason771ed682008-11-06 22:02:51 -0500983 int ret = 0;
984
Nikolay Borisov70ddc552017-02-20 13:50:35 +0200985 if (btrfs_is_free_space_inode(BTRFS_I(inode))) {
Josef Bacik02ecd2c2013-10-25 16:19:08 -0400986 WARN_ON_ONCE(1);
Josef Bacik29bce2f2014-02-07 12:21:23 -0500987 ret = -EINVAL;
988 goto out_unlock;
Josef Bacik02ecd2c2013-10-25 16:19:08 -0400989 }
Chris Mason771ed682008-11-06 22:02:51 -0500990
Qu Wenruofda28322013-02-26 08:10:22 +0000991 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Mason771ed682008-11-06 22:02:51 -0500992 num_bytes = max(blocksize, num_bytes);
Anand Jain566b1762018-02-15 18:07:59 +0800993 ASSERT(num_bytes <= btrfs_super_total_bytes(fs_info->super_copy));
Chris Mason771ed682008-11-06 22:02:51 -0500994
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200995 inode_should_defrag(BTRFS_I(inode), start, end, num_bytes, SZ_64K);
Chris Mason4cb53002011-05-24 15:35:30 -0400996
Chris Mason771ed682008-11-06 22:02:51 -0500997 if (start == 0) {
998 /* lets try to make an inline extent */
Nikolay Borisovd02c0e22018-03-02 09:43:15 +0200999 ret = cow_file_range_inline(inode, start, end, 0,
1000 BTRFS_COMPRESS_NONE, NULL);
Chris Mason771ed682008-11-06 22:02:51 -05001001 if (ret == 0) {
Josef Bacik8b62f872017-10-19 14:15:55 -04001002 /*
1003 * We use DO_ACCOUNTING here because we need the
1004 * delalloc_release_metadata to be run _after_ we drop
1005 * our outstanding extent for clearing delalloc for this
1006 * range.
1007 */
Nikolay Borisov74e91942019-07-17 16:18:16 +03001008 extent_clear_unlock_delalloc(inode, start, end, NULL,
Josef Bacikc2790a22013-07-29 11:20:47 -04001009 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik8b62f872017-10-19 14:15:55 -04001010 EXTENT_DELALLOC_NEW | EXTENT_DEFRAG |
1011 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001012 PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
1013 PAGE_END_WRITEBACK);
Chris Mason771ed682008-11-06 22:02:51 -05001014 *nr_written = *nr_written +
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001015 (end - start + PAGE_SIZE) / PAGE_SIZE;
Chris Mason771ed682008-11-06 22:02:51 -05001016 *page_started = 1;
Chris Mason771ed682008-11-06 22:02:51 -05001017 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001018 } else if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001019 goto out_unlock;
Chris Mason771ed682008-11-06 22:02:51 -05001020 }
1021 }
Chris Masonc8b97812008-10-29 14:49:59 -04001022
Josef Bacik4b46fce2010-05-23 11:00:55 -04001023 alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02001024 btrfs_drop_extent_cache(BTRFS_I(inode), start,
1025 start + num_bytes - 1, 0);
Chris Mason3b951512008-04-17 11:29:12 -04001026
Anand Jain3752d222018-02-15 12:29:38 +08001027 while (num_bytes > 0) {
1028 cur_alloc_size = num_bytes;
Wang Xiaoguang18513092016-07-25 15:51:40 +08001029 ret = btrfs_reserve_extent(root, cur_alloc_size, cur_alloc_size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001030 fs_info->sectorsize, 0, alloc_hint,
Miao Xiee570fd22014-06-19 10:42:50 +08001031 &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04001032 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001033 goto out_unlock;
Filipe Mananaa315e682017-03-06 23:04:20 +00001034 cur_alloc_size = ins.offset;
1035 extent_reserved = true;
Chris Masond3977122009-01-05 21:25:51 -05001036
Chris Mason771ed682008-11-06 22:02:51 -05001037 ram_size = ins.offset;
Liu Bo6f9994d2017-01-31 07:50:22 -08001038 em = create_io_em(inode, start, ins.offset, /* len */
1039 start, /* orig_start */
1040 ins.objectid, /* block_start */
1041 ins.offset, /* block_len */
1042 ins.offset, /* orig_block_len */
1043 ram_size, /* ram_bytes */
1044 BTRFS_COMPRESS_NONE, /* compress_type */
Liu Bo1af4a0a2017-02-13 15:35:09 -08001045 BTRFS_ORDERED_REGULAR /* type */);
Su Yue090a127a2018-05-30 16:48:56 +08001046 if (IS_ERR(em)) {
1047 ret = PTR_ERR(em);
Liu Boace68ba2013-04-22 10:53:47 +00001048 goto out_reserve;
Su Yue090a127a2018-05-30 16:48:56 +08001049 }
Liu Bo6f9994d2017-01-31 07:50:22 -08001050 free_extent_map(em);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001051
Chris Masone6dcd2d2008-07-17 12:53:50 -04001052 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
Chris Mason771ed682008-11-06 22:02:51 -05001053 ram_size, cur_alloc_size, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001054 if (ret)
Filipe Mananad9f85962014-08-25 10:43:00 +01001055 goto out_drop_extent_cache;
Chris Masonc8b97812008-10-29 14:49:59 -04001056
Yan Zheng17d217f2008-12-12 10:03:38 -05001057 if (root->root_key.objectid ==
1058 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1059 ret = btrfs_reloc_clone_csums(inode, start,
1060 cur_alloc_size);
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001061 /*
1062 * Only drop cache here, and process as normal.
1063 *
1064 * We must not allow extent_clear_unlock_delalloc()
1065 * at out_unlock label to free meta of this ordered
1066 * extent, as its meta should be freed by
1067 * btrfs_finish_ordered_io().
1068 *
1069 * So we must continue until @start is increased to
1070 * skip current ordered extent.
1071 */
Josef Bacik00361582013-08-14 14:02:47 -04001072 if (ret)
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001073 btrfs_drop_extent_cache(BTRFS_I(inode), start,
1074 start + ram_size - 1, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001075 }
1076
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001077 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana9cfa3e32016-04-26 15:39:32 +01001078
Chris Masonc8b97812008-10-29 14:49:59 -04001079 /* we're not doing compressed IO, don't unlock the first
1080 * page (which the caller expects to stay locked), don't
1081 * clear any dirty bits and don't set any writeback bits
Chris Mason8b62b722009-09-02 16:53:46 -04001082 *
1083 * Do set the Private2 bit so we know this page was properly
1084 * setup for writepage
Chris Masonc8b97812008-10-29 14:49:59 -04001085 */
Filipe Mananaa315e682017-03-06 23:04:20 +00001086 page_ops = unlock ? PAGE_UNLOCK : 0;
1087 page_ops |= PAGE_SET_PRIVATE2;
Chris Masona791e352009-10-08 11:27:10 -04001088
Josef Bacikc2790a22013-07-29 11:20:47 -04001089 extent_clear_unlock_delalloc(inode, start,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001090 start + ram_size - 1,
Nikolay Borisov74e91942019-07-17 16:18:16 +03001091 locked_page,
Josef Bacikc2790a22013-07-29 11:20:47 -04001092 EXTENT_LOCKED | EXTENT_DELALLOC,
Filipe Mananaa315e682017-03-06 23:04:20 +00001093 page_ops);
Anand Jain3752d222018-02-15 12:29:38 +08001094 if (num_bytes < cur_alloc_size)
1095 num_bytes = 0;
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001096 else
Anand Jain3752d222018-02-15 12:29:38 +08001097 num_bytes -= cur_alloc_size;
Chris Masonc59f8952007-12-17 20:14:04 -05001098 alloc_hint = ins.objectid + ins.offset;
1099 start += cur_alloc_size;
Filipe Mananaa315e682017-03-06 23:04:20 +00001100 extent_reserved = false;
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001101
1102 /*
1103 * btrfs_reloc_clone_csums() error, since start is increased
1104 * extent_clear_unlock_delalloc() at out_unlock label won't
1105 * free metadata of current ordered extent, we're OK to exit.
1106 */
1107 if (ret)
1108 goto out_unlock;
Chris Masonb888db22007-08-27 16:49:44 -04001109 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001110out:
Chris Masonbe20aa92007-12-17 20:14:01 -05001111 return ret;
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001112
Filipe Mananad9f85962014-08-25 10:43:00 +01001113out_drop_extent_cache:
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02001114 btrfs_drop_extent_cache(BTRFS_I(inode), start, start + ram_size - 1, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001115out_reserve:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001116 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001117 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001118out_unlock:
Filipe Mananaa7e3b972017-04-03 10:45:46 +01001119 clear_bits = EXTENT_LOCKED | EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
1120 EXTENT_DEFRAG | EXTENT_CLEAR_META_RESV;
Filipe Mananaa315e682017-03-06 23:04:20 +00001121 page_ops = PAGE_UNLOCK | PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
1122 PAGE_END_WRITEBACK;
1123 /*
1124 * If we reserved an extent for our delalloc range (or a subrange) and
1125 * failed to create the respective ordered extent, then it means that
1126 * when we reserved the extent we decremented the extent's size from
1127 * the data space_info's bytes_may_use counter and incremented the
1128 * space_info's bytes_reserved counter by the same amount. We must make
1129 * sure extent_clear_unlock_delalloc() does not try to decrement again
1130 * the data space_info's bytes_may_use counter, therefore we do not pass
1131 * it the flag EXTENT_CLEAR_DATA_RESV.
1132 */
1133 if (extent_reserved) {
1134 extent_clear_unlock_delalloc(inode, start,
1135 start + cur_alloc_size,
Filipe Mananaa315e682017-03-06 23:04:20 +00001136 locked_page,
1137 clear_bits,
1138 page_ops);
1139 start += cur_alloc_size;
1140 if (start >= end)
1141 goto out;
1142 }
Nikolay Borisov74e91942019-07-17 16:18:16 +03001143 extent_clear_unlock_delalloc(inode, start, end, locked_page,
Filipe Mananaa315e682017-03-06 23:04:20 +00001144 clear_bits | EXTENT_CLEAR_DATA_RESV,
1145 page_ops);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001146 goto out;
Chris Mason771ed682008-11-06 22:02:51 -05001147}
Chris Masonc8b97812008-10-29 14:49:59 -04001148
Chris Mason771ed682008-11-06 22:02:51 -05001149/*
1150 * work queue call back to started compression on a file and pages
1151 */
1152static noinline void async_cow_start(struct btrfs_work *work)
1153{
Nikolay Borisovb5326272019-03-12 17:20:25 +02001154 struct async_chunk *async_chunk;
Nikolay Borisovac3e9932019-07-17 14:41:44 +03001155 int compressed_extents;
Chris Mason771ed682008-11-06 22:02:51 -05001156
Nikolay Borisovb5326272019-03-12 17:20:25 +02001157 async_chunk = container_of(work, struct async_chunk, work);
Chris Mason771ed682008-11-06 22:02:51 -05001158
Nikolay Borisovac3e9932019-07-17 14:41:44 +03001159 compressed_extents = compress_file_range(async_chunk);
1160 if (compressed_extents == 0) {
Nikolay Borisovb5326272019-03-12 17:20:25 +02001161 btrfs_add_delayed_iput(async_chunk->inode);
1162 async_chunk->inode = NULL;
Josef Bacik8180ef82012-06-08 15:16:12 -04001163 }
Chris Mason771ed682008-11-06 22:02:51 -05001164}
1165
1166/*
1167 * work queue call back to submit previously compressed pages
1168 */
1169static noinline void async_cow_submit(struct btrfs_work *work)
1170{
Nikolay Borisovc5a68ae2019-03-12 17:20:26 +02001171 struct async_chunk *async_chunk = container_of(work, struct async_chunk,
1172 work);
1173 struct btrfs_fs_info *fs_info = btrfs_work_owner(work);
Chris Mason771ed682008-11-06 22:02:51 -05001174 unsigned long nr_pages;
1175
Nikolay Borisovb5326272019-03-12 17:20:25 +02001176 nr_pages = (async_chunk->end - async_chunk->start + PAGE_SIZE) >>
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001177 PAGE_SHIFT;
Chris Mason771ed682008-11-06 22:02:51 -05001178
David Sterba093258e2018-02-26 16:15:17 +01001179 /* atomic_sub_return implies a barrier */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001180 if (atomic_sub_return(nr_pages, &fs_info->async_delalloc_pages) <
David Sterba093258e2018-02-26 16:15:17 +01001181 5 * SZ_1M)
1182 cond_wake_up_nomb(&fs_info->async_submit_wait);
Chris Mason771ed682008-11-06 22:02:51 -05001183
Nikolay Borisov4546d172019-01-03 10:50:03 +02001184 /*
Nikolay Borisovb5326272019-03-12 17:20:25 +02001185 * ->inode could be NULL if async_chunk_start has failed to compress,
Nikolay Borisov4546d172019-01-03 10:50:03 +02001186 * in which case we don't have anything to submit, yet we need to
1187 * always adjust ->async_delalloc_pages as its paired with the init
1188 * happening in cow_file_range_async
1189 */
Nikolay Borisovb5326272019-03-12 17:20:25 +02001190 if (async_chunk->inode)
1191 submit_compressed_extents(async_chunk);
Chris Mason771ed682008-11-06 22:02:51 -05001192}
Chris Masonc8b97812008-10-29 14:49:59 -04001193
Chris Mason771ed682008-11-06 22:02:51 -05001194static noinline void async_cow_free(struct btrfs_work *work)
1195{
Nikolay Borisovb5326272019-03-12 17:20:25 +02001196 struct async_chunk *async_chunk;
Nikolay Borisov97db1202019-03-12 17:20:24 +02001197
Nikolay Borisovb5326272019-03-12 17:20:25 +02001198 async_chunk = container_of(work, struct async_chunk, work);
1199 if (async_chunk->inode)
1200 btrfs_add_delayed_iput(async_chunk->inode);
Chris Masonec39f762019-07-10 12:28:17 -07001201 if (async_chunk->blkcg_css)
1202 css_put(async_chunk->blkcg_css);
Nikolay Borisov97db1202019-03-12 17:20:24 +02001203 /*
1204 * Since the pointer to 'pending' is at the beginning of the array of
Nikolay Borisovb5326272019-03-12 17:20:25 +02001205 * async_chunk's, freeing it ensures the whole array has been freed.
Nikolay Borisov97db1202019-03-12 17:20:24 +02001206 */
Nikolay Borisovb5326272019-03-12 17:20:25 +02001207 if (atomic_dec_and_test(async_chunk->pending))
Nikolay Borisovb1c16ac2019-04-01 11:29:57 +03001208 kvfree(async_chunk->pending);
Chris Mason771ed682008-11-06 22:02:51 -05001209}
1210
Chris Masonec39f762019-07-10 12:28:17 -07001211static int cow_file_range_async(struct inode *inode,
1212 struct writeback_control *wbc,
1213 struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001214 u64 start, u64 end, int *page_started,
David Sterbafac07d22019-10-29 18:28:57 +01001215 unsigned long *nr_written)
Chris Mason771ed682008-11-06 22:02:51 -05001216{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001217 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masonec39f762019-07-10 12:28:17 -07001218 struct cgroup_subsys_state *blkcg_css = wbc_blkcg_css(wbc);
Nikolay Borisov97db1202019-03-12 17:20:24 +02001219 struct async_cow *ctx;
1220 struct async_chunk *async_chunk;
Chris Mason771ed682008-11-06 22:02:51 -05001221 unsigned long nr_pages;
1222 u64 cur_end;
Nikolay Borisov97db1202019-03-12 17:20:24 +02001223 u64 num_chunks = DIV_ROUND_UP(end - start, SZ_512K);
1224 int i;
1225 bool should_compress;
Nikolay Borisovb1c16ac2019-04-01 11:29:57 +03001226 unsigned nofs_flag;
David Sterbafac07d22019-10-29 18:28:57 +01001227 const unsigned int write_flags = wbc_to_write_flags(wbc);
Chris Mason771ed682008-11-06 22:02:51 -05001228
Nikolay Borisov69684c52019-03-12 17:20:28 +02001229 unlock_extent(&BTRFS_I(inode)->io_tree, start, end);
Nikolay Borisov97db1202019-03-12 17:20:24 +02001230
1231 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS &&
1232 !btrfs_test_opt(fs_info, FORCE_COMPRESS)) {
1233 num_chunks = 1;
1234 should_compress = false;
1235 } else {
1236 should_compress = true;
1237 }
1238
Nikolay Borisovb1c16ac2019-04-01 11:29:57 +03001239 nofs_flag = memalloc_nofs_save();
1240 ctx = kvmalloc(struct_size(ctx, chunks, num_chunks), GFP_KERNEL);
1241 memalloc_nofs_restore(nofs_flag);
1242
Nikolay Borisov97db1202019-03-12 17:20:24 +02001243 if (!ctx) {
1244 unsigned clear_bits = EXTENT_LOCKED | EXTENT_DELALLOC |
1245 EXTENT_DELALLOC_NEW | EXTENT_DEFRAG |
1246 EXTENT_DO_ACCOUNTING;
1247 unsigned long page_ops = PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
1248 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK |
1249 PAGE_SET_ERROR;
1250
Nikolay Borisov74e91942019-07-17 16:18:16 +03001251 extent_clear_unlock_delalloc(inode, start, end, locked_page,
Nikolay Borisov97db1202019-03-12 17:20:24 +02001252 clear_bits, page_ops);
1253 return -ENOMEM;
1254 }
1255
1256 async_chunk = ctx->chunks;
1257 atomic_set(&ctx->num_chunks, num_chunks);
1258
1259 for (i = 0; i < num_chunks; i++) {
1260 if (should_compress)
1261 cur_end = min(end, start + SZ_512K - 1);
1262 else
1263 cur_end = end;
1264
Nikolay Borisovbd4691a2019-01-03 10:50:01 +02001265 /*
1266 * igrab is called higher up in the call chain, take only the
1267 * lightweight reference for the callback lifetime
1268 */
1269 ihold(inode);
Nikolay Borisov97db1202019-03-12 17:20:24 +02001270 async_chunk[i].pending = &ctx->num_chunks;
1271 async_chunk[i].inode = inode;
1272 async_chunk[i].start = start;
1273 async_chunk[i].end = cur_end;
Nikolay Borisov97db1202019-03-12 17:20:24 +02001274 async_chunk[i].write_flags = write_flags;
1275 INIT_LIST_HEAD(&async_chunk[i].extents);
Chris Mason771ed682008-11-06 22:02:51 -05001276
Chris Mason1d53c9e2019-07-10 12:28:16 -07001277 /*
1278 * The locked_page comes all the way from writepage and its
1279 * the original page we were actually given. As we spread
1280 * this large delalloc region across multiple async_chunk
1281 * structs, only the first struct needs a pointer to locked_page
1282 *
1283 * This way we don't need racey decisions about who is supposed
1284 * to unlock it.
1285 */
1286 if (locked_page) {
Chris Masonec39f762019-07-10 12:28:17 -07001287 /*
1288 * Depending on the compressibility, the pages might or
1289 * might not go through async. We want all of them to
1290 * be accounted against wbc once. Let's do it here
1291 * before the paths diverge. wbc accounting is used
1292 * only for foreign writeback detection and doesn't
1293 * need full accuracy. Just account the whole thing
1294 * against the first page.
1295 */
1296 wbc_account_cgroup_owner(wbc, locked_page,
1297 cur_end - start);
Chris Mason1d53c9e2019-07-10 12:28:16 -07001298 async_chunk[i].locked_page = locked_page;
1299 locked_page = NULL;
1300 } else {
1301 async_chunk[i].locked_page = NULL;
1302 }
1303
Chris Masonec39f762019-07-10 12:28:17 -07001304 if (blkcg_css != blkcg_root_css) {
1305 css_get(blkcg_css);
1306 async_chunk[i].blkcg_css = blkcg_css;
1307 } else {
1308 async_chunk[i].blkcg_css = NULL;
1309 }
1310
Omar Sandovala0cac0e2019-09-16 11:30:57 -07001311 btrfs_init_work(&async_chunk[i].work, async_cow_start,
1312 async_cow_submit, async_cow_free);
Chris Mason771ed682008-11-06 22:02:51 -05001313
Nikolay Borisov97db1202019-03-12 17:20:24 +02001314 nr_pages = DIV_ROUND_UP(cur_end - start, PAGE_SIZE);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001315 atomic_add(nr_pages, &fs_info->async_delalloc_pages);
Chris Mason771ed682008-11-06 22:02:51 -05001316
Nikolay Borisov97db1202019-03-12 17:20:24 +02001317 btrfs_queue_work(fs_info->delalloc_workers, &async_chunk[i].work);
Chris Mason771ed682008-11-06 22:02:51 -05001318
Chris Mason771ed682008-11-06 22:02:51 -05001319 *nr_written += nr_pages;
1320 start = cur_end + 1;
1321 }
1322 *page_started = 1;
1323 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001324}
1325
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001326static noinline int csum_exist_in_range(struct btrfs_fs_info *fs_info,
Yan Zheng17d217f2008-12-12 10:03:38 -05001327 u64 bytenr, u64 num_bytes)
1328{
1329 int ret;
1330 struct btrfs_ordered_sum *sums;
1331 LIST_HEAD(list);
1332
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001333 ret = btrfs_lookup_csums_range(fs_info->csum_root, bytenr,
Arne Jansena2de7332011-03-08 14:14:00 +01001334 bytenr + num_bytes - 1, &list, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001335 if (ret == 0 && list_empty(&list))
1336 return 0;
1337
1338 while (!list_empty(&list)) {
1339 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1340 list_del(&sums->list);
1341 kfree(sums);
1342 }
Liu Bo58113752018-01-31 17:09:13 -07001343 if (ret < 0)
1344 return ret;
Yan Zheng17d217f2008-12-12 10:03:38 -05001345 return 1;
1346}
1347
Chris Masond352ac62008-09-29 15:18:18 -04001348/*
1349 * when nowcow writeback call back. This checks for snapshots or COW copies
1350 * of the extents that exist in the file, and COWs the file as required.
1351 *
1352 * If no cow copies or snapshots exist, we write directly to the existing
1353 * blocks on disk
1354 */
Chris Mason7f366cf2009-03-12 20:12:45 -04001355static noinline int run_delalloc_nocow(struct inode *inode,
1356 struct page *locked_page,
Nikolay Borisov3e024842019-08-21 10:42:03 +03001357 const u64 start, const u64 end,
1358 int *page_started, int force,
1359 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001360{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001361 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masonbe20aa92007-12-17 20:14:01 -05001362 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masonbe20aa92007-12-17 20:14:01 -05001363 struct btrfs_path *path;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001364 u64 cow_start = (u64)-1;
1365 u64 cur_offset = start;
Robbie Ko8ecebf4d2018-08-06 10:30:30 +08001366 int ret;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001367 bool check_prev = true;
1368 const bool freespace_inode = btrfs_is_free_space_inode(BTRFS_I(inode));
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02001369 u64 ino = btrfs_ino(BTRFS_I(inode));
Nikolay Borisov762bf092019-08-22 17:24:20 +03001370 bool nocow = false;
1371 u64 disk_bytenr = 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001372
1373 path = btrfs_alloc_path();
Josef Bacik17ca04a2012-05-31 15:58:55 -04001374 if (!path) {
Nikolay Borisov74e91942019-07-17 16:18:16 +03001375 extent_clear_unlock_delalloc(inode, start, end, locked_page,
Josef Bacikc2790a22013-07-29 11:20:47 -04001376 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -04001377 EXTENT_DO_ACCOUNTING |
1378 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001379 PAGE_CLEAR_DIRTY |
1380 PAGE_SET_WRITEBACK |
1381 PAGE_END_WRITEBACK);
Mark Fashehd8926bb2011-07-13 10:38:47 -07001382 return -ENOMEM;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001383 }
Li Zefan82d59022011-04-20 10:33:24 +08001384
Yan Zheng80ff3852008-10-30 14:20:02 -04001385 while (1) {
Nikolay Borisov3e024842019-08-21 10:42:03 +03001386 struct btrfs_key found_key;
1387 struct btrfs_file_extent_item *fi;
1388 struct extent_buffer *leaf;
1389 u64 extent_end;
1390 u64 extent_offset;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001391 u64 num_bytes = 0;
1392 u64 disk_num_bytes;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001393 u64 ram_bytes;
1394 int extent_type;
Nikolay Borisov762bf092019-08-22 17:24:20 +03001395
1396 nocow = false;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001397
Liu Boe4c3b2d2017-01-30 12:25:28 -08001398 ret = btrfs_lookup_file_extent(NULL, root, path, ino,
Yan Zheng80ff3852008-10-30 14:20:02 -04001399 cur_offset, 0);
Josef Bacikd788a342013-10-25 16:55:08 -04001400 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001401 goto error;
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001402
1403 /*
1404 * If there is no extent for our range when doing the initial
1405 * search, then go back to the previous slot as it will be the
1406 * one containing the search offset
1407 */
Yan Zheng80ff3852008-10-30 14:20:02 -04001408 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1409 leaf = path->nodes[0];
1410 btrfs_item_key_to_cpu(leaf, &found_key,
1411 path->slots[0] - 1);
Li Zefan33345d012011-04-20 10:31:50 +08001412 if (found_key.objectid == ino &&
Yan Zheng80ff3852008-10-30 14:20:02 -04001413 found_key.type == BTRFS_EXTENT_DATA_KEY)
1414 path->slots[0]--;
1415 }
Nikolay Borisov3e024842019-08-21 10:42:03 +03001416 check_prev = false;
Yan Zheng80ff3852008-10-30 14:20:02 -04001417next_slot:
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001418 /* Go to next leaf if we have exhausted the current one */
Yan Zheng80ff3852008-10-30 14:20:02 -04001419 leaf = path->nodes[0];
1420 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1421 ret = btrfs_next_leaf(root, path);
Liu Boe8916692018-01-25 11:02:50 -07001422 if (ret < 0) {
1423 if (cow_start != (u64)-1)
1424 cur_offset = cow_start;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001425 goto error;
Liu Boe8916692018-01-25 11:02:50 -07001426 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001427 if (ret > 0)
1428 break;
1429 leaf = path->nodes[0];
1430 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001431
Yan Zheng80ff3852008-10-30 14:20:02 -04001432 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001433
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001434 /* Didn't find anything for our INO */
Filipe Manana1d512cb2015-11-09 00:33:58 +00001435 if (found_key.objectid > ino)
1436 break;
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001437 /*
1438 * Keep searching until we find an EXTENT_ITEM or there are no
1439 * more extents for this inode
1440 */
Filipe Manana1d512cb2015-11-09 00:33:58 +00001441 if (WARN_ON_ONCE(found_key.objectid < ino) ||
1442 found_key.type < BTRFS_EXTENT_DATA_KEY) {
1443 path->slots[0]++;
1444 goto next_slot;
1445 }
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001446
1447 /* Found key is not EXTENT_DATA_KEY or starts after req range */
Filipe Manana1d512cb2015-11-09 00:33:58 +00001448 if (found_key.type > BTRFS_EXTENT_DATA_KEY ||
Yan Zheng80ff3852008-10-30 14:20:02 -04001449 found_key.offset > end)
1450 break;
Chris Masonbe20aa92007-12-17 20:14:01 -05001451
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001452 /*
1453 * If the found extent starts after requested offset, then
1454 * adjust extent_end to be right before this extent begins
1455 */
Yan Zheng80ff3852008-10-30 14:20:02 -04001456 if (found_key.offset > cur_offset) {
1457 extent_end = found_key.offset;
Chris Masone9061e22009-10-09 09:57:45 -04001458 extent_type = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001459 goto out_check;
1460 }
Chris Masonc31f8832008-01-08 15:46:31 -05001461
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001462 /*
1463 * Found extent which begins before our range and potentially
1464 * intersect it
1465 */
Yan Zheng80ff3852008-10-30 14:20:02 -04001466 fi = btrfs_item_ptr(leaf, path->slots[0],
1467 struct btrfs_file_extent_item);
1468 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001469
Josef Bacikcc95bef2013-04-04 14:31:27 -04001470 ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
Yan Zhengd899e052008-10-30 14:25:28 -04001471 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1472 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001473 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001474 extent_offset = btrfs_file_extent_offset(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001475 extent_end = found_key.offset +
1476 btrfs_file_extent_num_bytes(leaf, fi);
Josef Bacikb4939682012-12-03 10:31:19 -05001477 disk_num_bytes =
1478 btrfs_file_extent_disk_num_bytes(leaf, fi);
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001479 /*
Filipe Mananade7999a2019-12-11 09:01:40 +00001480 * If the extent we got ends before our current offset,
1481 * skip to the next extent.
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001482 */
Filipe Mananade7999a2019-12-11 09:01:40 +00001483 if (extent_end <= cur_offset) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001484 path->slots[0]++;
1485 goto next_slot;
1486 }
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001487 /* Skip holes */
Yan Zheng17d217f2008-12-12 10:03:38 -05001488 if (disk_bytenr == 0)
1489 goto out_check;
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001490 /* Skip compressed/encrypted/encoded extents */
Yan Zheng80ff3852008-10-30 14:20:02 -04001491 if (btrfs_file_extent_compression(leaf, fi) ||
1492 btrfs_file_extent_encryption(leaf, fi) ||
1493 btrfs_file_extent_other_encoding(leaf, fi))
1494 goto out_check;
Ethan Lien78d42952018-05-17 14:58:29 +08001495 /*
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001496 * If extent is created before the last volume's snapshot
1497 * this implies the extent is shared, hence we can't do
1498 * nocow. This is the same check as in
1499 * btrfs_cross_ref_exist but without calling
1500 * btrfs_search_slot.
Ethan Lien78d42952018-05-17 14:58:29 +08001501 */
Nikolay Borisov3e024842019-08-21 10:42:03 +03001502 if (!freespace_inode &&
Lu Fengqi27a7ff52018-11-29 17:31:32 +08001503 btrfs_file_extent_generation(leaf, fi) <=
Ethan Lien78d42952018-05-17 14:58:29 +08001504 btrfs_root_last_snapshot(&root->root_item))
1505 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001506 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1507 goto out_check;
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001508 /* If extent is RO, we must COW it */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001509 if (btrfs_extent_readonly(fs_info, disk_bytenr))
Yan Zheng80ff3852008-10-30 14:20:02 -04001510 goto out_check;
Liu Bo58113752018-01-31 17:09:13 -07001511 ret = btrfs_cross_ref_exist(root, ino,
1512 found_key.offset -
1513 extent_offset, disk_bytenr);
1514 if (ret) {
1515 /*
1516 * ret could be -EIO if the above fails to read
1517 * metadata.
1518 */
1519 if (ret < 0) {
1520 if (cow_start != (u64)-1)
1521 cur_offset = cow_start;
1522 goto error;
1523 }
1524
Nikolay Borisov3e024842019-08-21 10:42:03 +03001525 WARN_ON_ONCE(freespace_inode);
Yan Zheng17d217f2008-12-12 10:03:38 -05001526 goto out_check;
Liu Bo58113752018-01-31 17:09:13 -07001527 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001528 disk_bytenr += extent_offset;
Yan Zheng17d217f2008-12-12 10:03:38 -05001529 disk_bytenr += cur_offset - found_key.offset;
1530 num_bytes = min(end + 1, extent_end) - cur_offset;
1531 /*
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001532 * If there are pending snapshots for this root, we
1533 * fall into common COW way
Wang Shilonge9894fd2014-03-27 11:12:25 +08001534 */
Nikolay Borisov3e024842019-08-21 10:42:03 +03001535 if (!freespace_inode && atomic_read(&root->snapshot_force_cow))
Robbie Ko8ecebf4d2018-08-06 10:30:30 +08001536 goto out_check;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001537 /*
Yan Zheng17d217f2008-12-12 10:03:38 -05001538 * force cow if csum exists in the range.
1539 * this ensure that csum for a given extent are
1540 * either valid or do not exist.
1541 */
Liu Bo58113752018-01-31 17:09:13 -07001542 ret = csum_exist_in_range(fs_info, disk_bytenr,
1543 num_bytes);
1544 if (ret) {
Liu Bo58113752018-01-31 17:09:13 -07001545 /*
1546 * ret could be -EIO if the above fails to read
1547 * metadata.
1548 */
1549 if (ret < 0) {
1550 if (cow_start != (u64)-1)
1551 cur_offset = cow_start;
1552 goto error;
1553 }
Nikolay Borisov3e024842019-08-21 10:42:03 +03001554 WARN_ON_ONCE(freespace_inode);
Yan Zheng17d217f2008-12-12 10:03:38 -05001555 goto out_check;
Robbie Ko91e1f562016-10-07 10:01:29 +08001556 }
Robbie Ko8ecebf4d2018-08-06 10:30:30 +08001557 if (!btrfs_inc_nocow_writers(fs_info, disk_bytenr))
Filipe Mananaf78c4362016-05-09 13:15:41 +01001558 goto out_check;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001559 nocow = true;
Yan Zheng80ff3852008-10-30 14:20:02 -04001560 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Nikolay Borisove8e21002019-08-22 17:25:23 +03001561 extent_end = found_key.offset + ram_bytes;
1562 extent_end = ALIGN(extent_end, fs_info->sectorsize);
Nikolay Borisov922f0512019-08-05 17:47:06 +03001563 /* Skip extents outside of our requested range */
1564 if (extent_end <= start) {
1565 path->slots[0]++;
1566 goto next_slot;
1567 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001568 } else {
Nikolay Borisove8e21002019-08-22 17:25:23 +03001569 /* If this triggers then we have a memory corruption */
Arnd Bergmann290342f2019-03-25 14:02:25 +01001570 BUG();
Yan Zheng80ff3852008-10-30 14:20:02 -04001571 }
1572out_check:
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001573 /*
1574 * If nocow is false then record the beginning of the range
1575 * that needs to be COWed
1576 */
Yan Zheng80ff3852008-10-30 14:20:02 -04001577 if (!nocow) {
1578 if (cow_start == (u64)-1)
1579 cow_start = cur_offset;
1580 cur_offset = extent_end;
1581 if (cur_offset > end)
1582 break;
1583 path->slots[0]++;
1584 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001585 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001586
David Sterbab3b4aa72011-04-21 01:20:15 +02001587 btrfs_release_path(path);
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001588
1589 /*
1590 * COW range from cow_start to found_key.offset - 1. As the key
1591 * will contain the beginning of the first extent that can be
1592 * NOCOW, following one which needs to be COW'ed
1593 */
Yan Zheng80ff3852008-10-30 14:20:02 -04001594 if (cow_start != (u64)-1) {
Josef Bacik00361582013-08-14 14:02:47 -04001595 ret = cow_file_range(inode, locked_page,
1596 cow_start, found_key.offset - 1,
Nikolay Borisov330a5822019-07-17 16:18:17 +03001597 page_started, nr_written, 1);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001598 if (ret) {
Filipe Mananaf78c4362016-05-09 13:15:41 +01001599 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001600 btrfs_dec_nocow_writers(fs_info,
Filipe Mananaf78c4362016-05-09 13:15:41 +01001601 disk_bytenr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001602 goto error;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001603 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001604 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001605 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001606
Yan Zhengd899e052008-10-30 14:25:28 -04001607 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Liu Bo6f9994d2017-01-31 07:50:22 -08001608 u64 orig_start = found_key.offset - extent_offset;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001609 struct extent_map *em;
Liu Bo6f9994d2017-01-31 07:50:22 -08001610
1611 em = create_io_em(inode, cur_offset, num_bytes,
1612 orig_start,
1613 disk_bytenr, /* block_start */
1614 num_bytes, /* block_len */
1615 disk_num_bytes, /* orig_block_len */
1616 ram_bytes, BTRFS_COMPRESS_NONE,
1617 BTRFS_ORDERED_PREALLOC);
1618 if (IS_ERR(em)) {
Liu Bo6f9994d2017-01-31 07:50:22 -08001619 if (nocow)
1620 btrfs_dec_nocow_writers(fs_info,
1621 disk_bytenr);
1622 ret = PTR_ERR(em);
1623 goto error;
Yan Zhengd899e052008-10-30 14:25:28 -04001624 }
Liu Bo6f9994d2017-01-31 07:50:22 -08001625 free_extent_map(em);
Nikolay Borisovbb55f622019-08-05 17:47:05 +03001626 ret = btrfs_add_ordered_extent(inode, cur_offset,
1627 disk_bytenr, num_bytes,
1628 num_bytes,
1629 BTRFS_ORDERED_PREALLOC);
Nikolay Borisov762bf092019-08-22 17:24:20 +03001630 if (ret) {
1631 btrfs_drop_extent_cache(BTRFS_I(inode),
1632 cur_offset,
1633 cur_offset + num_bytes - 1,
1634 0);
1635 goto error;
1636 }
Yan Zhengd899e052008-10-30 14:25:28 -04001637 } else {
Nikolay Borisovbb55f622019-08-05 17:47:05 +03001638 ret = btrfs_add_ordered_extent(inode, cur_offset,
1639 disk_bytenr, num_bytes,
1640 num_bytes,
1641 BTRFS_ORDERED_NOCOW);
Nikolay Borisov762bf092019-08-22 17:24:20 +03001642 if (ret)
1643 goto error;
Yan Zhengd899e052008-10-30 14:25:28 -04001644 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001645
Filipe Mananaf78c4362016-05-09 13:15:41 +01001646 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001647 btrfs_dec_nocow_writers(fs_info, disk_bytenr);
Nikolay Borisov762bf092019-08-22 17:24:20 +03001648 nocow = false;
Chris Mason771ed682008-11-06 22:02:51 -05001649
Yan, Zhengefa56462010-05-16 10:49:59 -04001650 if (root->root_key.objectid ==
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001651 BTRFS_DATA_RELOC_TREE_OBJECTID)
1652 /*
1653 * Error handled later, as we must prevent
1654 * extent_clear_unlock_delalloc() in error handler
1655 * from freeing metadata of created ordered extent.
1656 */
Yan, Zhengefa56462010-05-16 10:49:59 -04001657 ret = btrfs_reloc_clone_csums(inode, cur_offset,
1658 num_bytes);
Yan, Zhengefa56462010-05-16 10:49:59 -04001659
Josef Bacikc2790a22013-07-29 11:20:47 -04001660 extent_clear_unlock_delalloc(inode, cur_offset,
Nikolay Borisov74e91942019-07-17 16:18:16 +03001661 cur_offset + num_bytes - 1,
Josef Bacikc2790a22013-07-29 11:20:47 -04001662 locked_page, EXTENT_LOCKED |
Wang Xiaoguang18513092016-07-25 15:51:40 +08001663 EXTENT_DELALLOC |
1664 EXTENT_CLEAR_DATA_RESV,
1665 PAGE_UNLOCK | PAGE_SET_PRIVATE2);
1666
Yan Zheng80ff3852008-10-30 14:20:02 -04001667 cur_offset = extent_end;
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001668
1669 /*
1670 * btrfs_reloc_clone_csums() error, now we're OK to call error
1671 * handler, as metadata for created ordered extent will only
1672 * be freed by btrfs_finish_ordered_io().
1673 */
1674 if (ret)
1675 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001676 if (cur_offset > end)
1677 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001678 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001679 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001680
Robbie Ko506481b2018-10-30 18:04:04 +08001681 if (cur_offset <= end && cow_start == (u64)-1)
Yan Zheng80ff3852008-10-30 14:20:02 -04001682 cow_start = cur_offset;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001683
Yan Zheng80ff3852008-10-30 14:20:02 -04001684 if (cow_start != (u64)-1) {
Robbie Ko506481b2018-10-30 18:04:04 +08001685 cur_offset = end;
Nikolay Borisov74e91942019-07-17 16:18:16 +03001686 ret = cow_file_range(inode, locked_page, cow_start, end,
Nikolay Borisov330a5822019-07-17 16:18:17 +03001687 page_started, nr_written, 1);
Josef Bacikd788a342013-10-25 16:55:08 -04001688 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001689 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001690 }
1691
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001692error:
Nikolay Borisov762bf092019-08-22 17:24:20 +03001693 if (nocow)
1694 btrfs_dec_nocow_writers(fs_info, disk_bytenr);
1695
Josef Bacik17ca04a2012-05-31 15:58:55 -04001696 if (ret && cur_offset < end)
Nikolay Borisov74e91942019-07-17 16:18:16 +03001697 extent_clear_unlock_delalloc(inode, cur_offset, end,
Josef Bacikc2790a22013-07-29 11:20:47 -04001698 locked_page, EXTENT_LOCKED |
Josef Bacik151a41b2013-07-29 13:22:24 -04001699 EXTENT_DELALLOC | EXTENT_DEFRAG |
1700 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
1701 PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -04001702 PAGE_SET_WRITEBACK |
1703 PAGE_END_WRITEBACK);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001704 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001705 return ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001706}
1707
Wang Shilong47059d92014-07-03 18:22:07 +08001708static inline int need_force_cow(struct inode *inode, u64 start, u64 end)
1709{
1710
1711 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
1712 !(BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC))
1713 return 0;
1714
1715 /*
1716 * @defrag_bytes is a hint value, no spinlock held here,
1717 * if is not zero, it means the file is defragging.
1718 * Force cow if given extent needs to be defragged.
1719 */
1720 if (BTRFS_I(inode)->defrag_bytes &&
1721 test_range_bit(&BTRFS_I(inode)->io_tree, start, end,
1722 EXTENT_DEFRAG, 0, NULL))
1723 return 1;
1724
1725 return 0;
1726}
1727
Chris Masond352ac62008-09-29 15:18:18 -04001728/*
Nikolay Borisov5eaad972018-11-01 14:09:46 +02001729 * Function to process delayed allocation (create CoW) for ranges which are
1730 * being touched for the first time.
Chris Masond352ac62008-09-29 15:18:18 -04001731 */
Nikolay Borisovbc9a8bf2018-12-19 09:50:20 +02001732int btrfs_run_delalloc_range(struct inode *inode, struct page *locked_page,
Nikolay Borisov5eaad972018-11-01 14:09:46 +02001733 u64 start, u64 end, int *page_started, unsigned long *nr_written,
1734 struct writeback_control *wbc)
Chris Masonbe20aa92007-12-17 20:14:01 -05001735{
Chris Masonbe20aa92007-12-17 20:14:01 -05001736 int ret;
Wang Shilong47059d92014-07-03 18:22:07 +08001737 int force_cow = need_force_cow(inode, start, end);
Chris Masona2135012008-06-25 16:01:30 -04001738
Wang Shilong47059d92014-07-03 18:22:07 +08001739 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW && !force_cow) {
Chris Masonc8b97812008-10-29 14:49:59 -04001740 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001741 page_started, 1, nr_written);
Wang Shilong47059d92014-07-03 18:22:07 +08001742 } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC && !force_cow) {
Yan Zhengd899e052008-10-30 14:25:28 -04001743 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001744 page_started, 0, nr_written);
Qu Wenruo42c16da2019-07-01 05:12:46 +00001745 } else if (!inode_can_compress(inode) ||
1746 !inode_need_compress(inode, start, end)) {
Nikolay Borisov74e91942019-07-17 16:18:16 +03001747 ret = cow_file_range(inode, locked_page, start, end,
Nikolay Borisov330a5822019-07-17 16:18:17 +03001748 page_started, nr_written, 1);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001749 } else {
1750 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1751 &BTRFS_I(inode)->runtime_flags);
Chris Masonec39f762019-07-10 12:28:17 -07001752 ret = cow_file_range_async(inode, wbc, locked_page, start, end,
David Sterbafac07d22019-10-29 18:28:57 +01001753 page_started, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001754 }
Qu Wenruo524272602017-03-08 10:25:52 +08001755 if (ret)
Nikolay Borisovd1051d62018-11-21 17:10:52 +02001756 btrfs_cleanup_ordered_extents(inode, locked_page, start,
1757 end - start + 1);
Chris Masonb888db22007-08-27 16:49:44 -04001758 return ret;
1759}
1760
Nikolay Borisovabbb55f2018-11-01 14:09:53 +02001761void btrfs_split_delalloc_extent(struct inode *inode,
1762 struct extent_state *orig, u64 split)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001763{
Josef Bacikdcab6a32015-02-11 15:08:59 -05001764 u64 size;
1765
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001766 /* not delalloc, ignore it */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001767 if (!(orig->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001768 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001769
Josef Bacikdcab6a32015-02-11 15:08:59 -05001770 size = orig->end - orig->start + 1;
1771 if (size > BTRFS_MAX_EXTENT_SIZE) {
David Sterba823bb202017-01-04 11:09:51 +01001772 u32 num_extents;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001773 u64 new_size;
1774
1775 /*
Nikolay Borisov5c848192018-11-01 14:09:52 +02001776 * See the explanation in btrfs_merge_delalloc_extent, the same
Josef Bacikba117212015-03-13 15:01:24 -04001777 * applies here, just in reverse.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001778 */
1779 new_size = orig->end - split + 1;
David Sterba823bb202017-01-04 11:09:51 +01001780 num_extents = count_max_extents(new_size);
Josef Bacikba117212015-03-13 15:01:24 -04001781 new_size = split - orig->start;
David Sterba823bb202017-01-04 11:09:51 +01001782 num_extents += count_max_extents(new_size);
1783 if (count_max_extents(size) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001784 return;
1785 }
1786
Josef Bacik9e0baf62011-07-15 15:16:44 +00001787 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04001788 btrfs_mod_outstanding_extents(BTRFS_I(inode), 1);
Josef Bacik9e0baf62011-07-15 15:16:44 +00001789 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001790}
1791
1792/*
Nikolay Borisov5c848192018-11-01 14:09:52 +02001793 * Handle merged delayed allocation extents so we can keep track of new extents
1794 * that are just merged onto old extents, such as when we are doing sequential
1795 * writes, so we can properly account for the metadata space we'll need.
Josef Bacik9ed74f22009-09-11 16:12:44 -04001796 */
Nikolay Borisov5c848192018-11-01 14:09:52 +02001797void btrfs_merge_delalloc_extent(struct inode *inode, struct extent_state *new,
1798 struct extent_state *other)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001799{
Josef Bacikdcab6a32015-02-11 15:08:59 -05001800 u64 new_size, old_size;
David Sterba823bb202017-01-04 11:09:51 +01001801 u32 num_extents;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001802
Josef Bacik9ed74f22009-09-11 16:12:44 -04001803 /* not delalloc, ignore it */
1804 if (!(other->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001805 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001806
Josef Bacik8461a3d2015-03-13 15:12:08 -04001807 if (new->start > other->start)
1808 new_size = new->end - other->start + 1;
1809 else
1810 new_size = other->end - new->start + 1;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001811
1812 /* we're not bigger than the max, unreserve the space and go */
1813 if (new_size <= BTRFS_MAX_EXTENT_SIZE) {
1814 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04001815 btrfs_mod_outstanding_extents(BTRFS_I(inode), -1);
Josef Bacikdcab6a32015-02-11 15:08:59 -05001816 spin_unlock(&BTRFS_I(inode)->lock);
1817 return;
1818 }
1819
1820 /*
Josef Bacikba117212015-03-13 15:01:24 -04001821 * We have to add up either side to figure out how many extents were
1822 * accounted for before we merged into one big extent. If the number of
1823 * extents we accounted for is <= the amount we need for the new range
1824 * then we can return, otherwise drop. Think of it like this
1825 *
1826 * [ 4k][MAX_SIZE]
1827 *
1828 * So we've grown the extent by a MAX_SIZE extent, this would mean we
1829 * need 2 outstanding extents, on one side we have 1 and the other side
1830 * we have 1 so they are == and we can return. But in this case
1831 *
1832 * [MAX_SIZE+4k][MAX_SIZE+4k]
1833 *
1834 * Each range on their own accounts for 2 extents, but merged together
1835 * they are only 3 extents worth of accounting, so we need to drop in
1836 * this case.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001837 */
Josef Bacikba117212015-03-13 15:01:24 -04001838 old_size = other->end - other->start + 1;
David Sterba823bb202017-01-04 11:09:51 +01001839 num_extents = count_max_extents(old_size);
Josef Bacikba117212015-03-13 15:01:24 -04001840 old_size = new->end - new->start + 1;
David Sterba823bb202017-01-04 11:09:51 +01001841 num_extents += count_max_extents(old_size);
1842 if (count_max_extents(new_size) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001843 return;
1844
Josef Bacik9e0baf62011-07-15 15:16:44 +00001845 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04001846 btrfs_mod_outstanding_extents(BTRFS_I(inode), -1);
Josef Bacik9e0baf62011-07-15 15:16:44 +00001847 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001848}
1849
Miao Xieeb73c1b2013-05-15 07:48:22 +00001850static void btrfs_add_delalloc_inodes(struct btrfs_root *root,
1851 struct inode *inode)
1852{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001853 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1854
Miao Xieeb73c1b2013-05-15 07:48:22 +00001855 spin_lock(&root->delalloc_lock);
1856 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1857 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1858 &root->delalloc_inodes);
1859 set_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1860 &BTRFS_I(inode)->runtime_flags);
1861 root->nr_delalloc_inodes++;
1862 if (root->nr_delalloc_inodes == 1) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001863 spin_lock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001864 BUG_ON(!list_empty(&root->delalloc_root));
1865 list_add_tail(&root->delalloc_root,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001866 &fs_info->delalloc_roots);
1867 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001868 }
1869 }
1870 spin_unlock(&root->delalloc_lock);
1871}
1872
Nikolay Borisov2b877332018-04-27 12:21:51 +03001873
1874void __btrfs_del_delalloc_inode(struct btrfs_root *root,
1875 struct btrfs_inode *inode)
Miao Xieeb73c1b2013-05-15 07:48:22 +00001876{
David Sterba3ffbd682018-06-29 10:56:42 +02001877 struct btrfs_fs_info *fs_info = root->fs_info;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001878
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001879 if (!list_empty(&inode->delalloc_inodes)) {
1880 list_del_init(&inode->delalloc_inodes);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001881 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001882 &inode->runtime_flags);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001883 root->nr_delalloc_inodes--;
1884 if (!root->nr_delalloc_inodes) {
Nikolay Borisov7c8a0d32018-04-27 12:21:52 +03001885 ASSERT(list_empty(&root->delalloc_inodes));
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001886 spin_lock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001887 BUG_ON(list_empty(&root->delalloc_root));
1888 list_del_init(&root->delalloc_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001889 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001890 }
1891 }
Nikolay Borisov2b877332018-04-27 12:21:51 +03001892}
1893
1894static void btrfs_del_delalloc_inode(struct btrfs_root *root,
1895 struct btrfs_inode *inode)
1896{
1897 spin_lock(&root->delalloc_lock);
1898 __btrfs_del_delalloc_inode(root, inode);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001899 spin_unlock(&root->delalloc_lock);
1900}
1901
Chris Masond352ac62008-09-29 15:18:18 -04001902/*
Nikolay Borisove06a1fc2018-11-01 14:09:50 +02001903 * Properly track delayed allocation bytes in the inode and to maintain the
1904 * list of inodes that have pending delalloc work to be done.
Chris Masond352ac62008-09-29 15:18:18 -04001905 */
Nikolay Borisove06a1fc2018-11-01 14:09:50 +02001906void btrfs_set_delalloc_extent(struct inode *inode, struct extent_state *state,
1907 unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001908{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001909 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1910
Wang Shilong47059d92014-07-03 18:22:07 +08001911 if ((*bits & EXTENT_DEFRAG) && !(*bits & EXTENT_DELALLOC))
1912 WARN_ON(1);
Chris Mason75eff682008-12-15 15:54:40 -05001913 /*
1914 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001915 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001916 * bit, which is only set or cleared with irqs on
1917 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001918 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001919 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001920 u64 len = state->end + 1 - state->start;
Josef Bacik8b62f872017-10-19 14:15:55 -04001921 u32 num_extents = count_max_extents(len);
Nikolay Borisov70ddc552017-02-20 13:50:35 +02001922 bool do_list = !btrfs_is_free_space_inode(BTRFS_I(inode));
Josef Bacik9ed74f22009-09-11 16:12:44 -04001923
Josef Bacik8b62f872017-10-19 14:15:55 -04001924 spin_lock(&BTRFS_I(inode)->lock);
1925 btrfs_mod_outstanding_extents(BTRFS_I(inode), num_extents);
1926 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik287a0ab2010-03-19 18:07:23 +00001927
Josef Bacik6a3891c2015-03-16 17:38:52 -04001928 /* For sanity tests */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001929 if (btrfs_is_testing(fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04001930 return;
1931
Nikolay Borisov104b4e52017-06-20 21:01:20 +03001932 percpu_counter_add_batch(&fs_info->delalloc_bytes, len,
1933 fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001934 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001935 BTRFS_I(inode)->delalloc_bytes += len;
Wang Shilong47059d92014-07-03 18:22:07 +08001936 if (*bits & EXTENT_DEFRAG)
1937 BTRFS_I(inode)->defrag_bytes += len;
Miao Xiedf0af1a2013-01-29 10:11:59 +00001938 if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00001939 &BTRFS_I(inode)->runtime_flags))
1940 btrfs_add_delalloc_inodes(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001941 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001942 }
Filipe Mananaa7e3b972017-04-03 10:45:46 +01001943
1944 if (!(state->state & EXTENT_DELALLOC_NEW) &&
1945 (*bits & EXTENT_DELALLOC_NEW)) {
1946 spin_lock(&BTRFS_I(inode)->lock);
1947 BTRFS_I(inode)->new_delalloc_bytes += state->end + 1 -
1948 state->start;
1949 spin_unlock(&BTRFS_I(inode)->lock);
1950 }
Chris Mason291d6732008-01-29 15:55:23 -05001951}
1952
Chris Masond352ac62008-09-29 15:18:18 -04001953/*
Nikolay Borisova36bb5f2018-11-01 14:09:51 +02001954 * Once a range is no longer delalloc this function ensures that proper
1955 * accounting happens.
Chris Masond352ac62008-09-29 15:18:18 -04001956 */
Nikolay Borisova36bb5f2018-11-01 14:09:51 +02001957void btrfs_clear_delalloc_extent(struct inode *vfs_inode,
1958 struct extent_state *state, unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001959{
Nikolay Borisova36bb5f2018-11-01 14:09:51 +02001960 struct btrfs_inode *inode = BTRFS_I(vfs_inode);
1961 struct btrfs_fs_info *fs_info = btrfs_sb(vfs_inode->i_sb);
Wang Shilong47059d92014-07-03 18:22:07 +08001962 u64 len = state->end + 1 - state->start;
David Sterba823bb202017-01-04 11:09:51 +01001963 u32 num_extents = count_max_extents(len);
Wang Shilong47059d92014-07-03 18:22:07 +08001964
Filipe Manana4a4b9642017-07-27 19:52:55 +01001965 if ((state->state & EXTENT_DEFRAG) && (*bits & EXTENT_DEFRAG)) {
1966 spin_lock(&inode->lock);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001967 inode->defrag_bytes -= len;
Filipe Manana4a4b9642017-07-27 19:52:55 +01001968 spin_unlock(&inode->lock);
1969 }
Wang Shilong47059d92014-07-03 18:22:07 +08001970
Chris Mason75eff682008-12-15 15:54:40 -05001971 /*
1972 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001973 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001974 * bit, which is only set or cleared with irqs on
1975 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001976 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001977 struct btrfs_root *root = inode->root;
Liu Bo83eea1f2012-07-10 05:28:39 -06001978 bool do_list = !btrfs_is_free_space_inode(inode);
Chris Masonbcbfce82008-04-22 13:26:47 -04001979
Josef Bacik8b62f872017-10-19 14:15:55 -04001980 spin_lock(&inode->lock);
1981 btrfs_mod_outstanding_extents(inode, -num_extents);
1982 spin_unlock(&inode->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001983
Josef Bacikb6d08f02013-09-27 14:57:43 -04001984 /*
1985 * We don't reserve metadata space for space cache inodes so we
Andrea Gelmini52042d82018-11-28 12:05:13 +01001986 * don't need to call delalloc_release_metadata if there is an
Josef Bacikb6d08f02013-09-27 14:57:43 -04001987 * error.
1988 */
Filipe Mananaa315e682017-03-06 23:04:20 +00001989 if (*bits & EXTENT_CLEAR_META_RESV &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001990 root != fs_info->tree_root)
Qu Wenruo43b18592017-12-12 15:34:32 +08001991 btrfs_delalloc_release_metadata(inode, len, false);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001992
Josef Bacik6a3891c2015-03-16 17:38:52 -04001993 /* For sanity tests. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001994 if (btrfs_is_testing(fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04001995 return;
1996
Filipe Mananaa315e682017-03-06 23:04:20 +00001997 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID &&
1998 do_list && !(state->state & EXTENT_NORESERVE) &&
1999 (*bits & EXTENT_CLEAR_DATA_RESV))
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02002000 btrfs_free_reserved_data_space_noquota(
2001 &inode->vfs_inode,
Qu Wenruo51773be2015-10-08 18:19:37 +08002002 state->start, len);
Josef Bacik9ed74f22009-09-11 16:12:44 -04002003
Nikolay Borisov104b4e52017-06-20 21:01:20 +03002004 percpu_counter_add_batch(&fs_info->delalloc_bytes, -len,
2005 fs_info->delalloc_batch);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02002006 spin_lock(&inode->lock);
2007 inode->delalloc_bytes -= len;
2008 if (do_list && inode->delalloc_bytes == 0 &&
Miao Xiedf0af1a2013-01-29 10:11:59 +00002009 test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02002010 &inode->runtime_flags))
Miao Xieeb73c1b2013-05-15 07:48:22 +00002011 btrfs_del_delalloc_inode(root, inode);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02002012 spin_unlock(&inode->lock);
Chris Mason291d6732008-01-29 15:55:23 -05002013 }
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002014
2015 if ((state->state & EXTENT_DELALLOC_NEW) &&
2016 (*bits & EXTENT_DELALLOC_NEW)) {
2017 spin_lock(&inode->lock);
2018 ASSERT(inode->new_delalloc_bytes >= len);
2019 inode->new_delalloc_bytes -= len;
2020 spin_unlock(&inode->lock);
2021 }
Chris Mason291d6732008-01-29 15:55:23 -05002022}
2023
Chris Masond352ac62008-09-29 15:18:18 -04002024/*
Nikolay Borisovda12fe52018-11-27 20:57:58 +02002025 * btrfs_bio_fits_in_stripe - Checks whether the size of the given bio will fit
2026 * in a chunk's stripe. This function ensures that bios do not span a
2027 * stripe/chunk
Liu Bo6f034ec2016-06-22 18:31:49 -07002028 *
Nikolay Borisovda12fe52018-11-27 20:57:58 +02002029 * @page - The page we are about to add to the bio
2030 * @size - size we want to add to the bio
2031 * @bio - bio we want to ensure is smaller than a stripe
2032 * @bio_flags - flags of the bio
2033 *
2034 * return 1 if page cannot be added to the bio
2035 * return 0 if page can be added to the bio
Liu Bo6f034ec2016-06-22 18:31:49 -07002036 * return error otherwise
Chris Masond352ac62008-09-29 15:18:18 -04002037 */
Nikolay Borisovda12fe52018-11-27 20:57:58 +02002038int btrfs_bio_fits_in_stripe(struct page *page, size_t size, struct bio *bio,
2039 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04002040{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002041 struct inode *inode = page->mapping->host;
2042 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Kent Overstreet4f024f32013-10-11 15:44:27 -07002043 u64 logical = (u64)bio->bi_iter.bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04002044 u64 length = 0;
2045 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04002046 int ret;
Nikolay Borisov89b798a2019-06-03 12:05:05 +03002047 struct btrfs_io_geometry geom;
Chris Mason239b14b2008-03-24 15:02:07 -04002048
Chris Mason771ed682008-11-06 22:02:51 -05002049 if (bio_flags & EXTENT_BIO_COMPRESSED)
2050 return 0;
2051
Kent Overstreet4f024f32013-10-11 15:44:27 -07002052 length = bio->bi_iter.bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04002053 map_length = length;
Nikolay Borisov89b798a2019-06-03 12:05:05 +03002054 ret = btrfs_get_io_geometry(fs_info, btrfs_op(bio), logical, map_length,
2055 &geom);
Liu Bo6f034ec2016-06-22 18:31:49 -07002056 if (ret < 0)
2057 return ret;
Nikolay Borisov89b798a2019-06-03 12:05:05 +03002058
2059 if (geom.len < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04002060 return 1;
Jeff Mahoney3444a972011-10-03 23:23:13 -04002061 return 0;
Chris Mason239b14b2008-03-24 15:02:07 -04002062}
2063
Chris Masond352ac62008-09-29 15:18:18 -04002064/*
2065 * in order to insert checksums into the metadata in large chunks,
2066 * we wait until bio submission time. All the pages in the bio are
2067 * checksummed and sums are attached onto the ordered extent record.
2068 *
2069 * At IO completion time the cums attached on the ordered extent record
2070 * are inserted into the btree
2071 */
David Sterbad0ee3932018-03-08 14:35:48 +01002072static blk_status_t btrfs_submit_bio_start(void *private_data, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04002073 u64 bio_offset)
Chris Mason065631f2008-02-20 12:07:25 -05002074{
Josef Bacikc6100a42017-05-05 11:57:13 -04002075 struct inode *inode = private_data;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002076 blk_status_t ret = 0;
Chris Masone0156402008-04-16 11:15:20 -04002077
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002078 ret = btrfs_csum_one_bio(inode, bio, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002079 BUG_ON(ret); /* -ENOMEM */
Chris Mason4a69a412008-11-06 22:03:00 -05002080 return 0;
2081}
Chris Masone0156402008-04-16 11:15:20 -04002082
Chris Mason4a69a412008-11-06 22:03:00 -05002083/*
Chris Masoncad321a2008-12-17 14:51:42 -05002084 * extent_io.c submission hook. This does the right thing for csum calculation
Liu Bo4c274bc2017-11-01 17:19:27 -06002085 * on write, or reading the csums from the tree before a read.
2086 *
2087 * Rules about async/sync submit,
2088 * a) read: sync submit
2089 *
2090 * b) write without checksum: sync submit
2091 *
2092 * c) write with checksum:
2093 * c-1) if bio is issued by fsync: sync submit
2094 * (sync_writers != 0)
2095 *
2096 * c-2) if root is reloc root: sync submit
2097 * (only in case of buffered IO)
2098 *
2099 * c-3) otherwise: async submit
Chris Masond352ac62008-09-29 15:18:18 -04002100 */
Nikolay Borisova56b1c72019-04-10 17:24:39 +03002101static blk_status_t btrfs_submit_bio_hook(struct inode *inode, struct bio *bio,
Nikolay Borisov50489a52019-04-10 19:46:04 +03002102 int mirror_num,
2103 unsigned long bio_flags)
2104
Chris Mason44b8bd72008-04-16 11:14:51 -04002105{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002106 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason44b8bd72008-04-16 11:14:51 -04002107 struct btrfs_root *root = BTRFS_I(inode)->root;
Chandan Rajendra0d51e282015-07-27 15:26:43 +05302108 enum btrfs_wq_endio_type metadata = BTRFS_WQ_ENDIO_DATA;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002109 blk_status_t ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04002110 int skip_sum;
Josef Bacikb812ce22012-11-16 13:56:32 -05002111 int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
Chris Mason44b8bd72008-04-16 11:14:51 -04002112
Christoph Hellwig6cbff002009-04-17 10:37:41 +02002113 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masoncad321a2008-12-17 14:51:42 -05002114
Nikolay Borisov70ddc552017-02-20 13:50:35 +02002115 if (btrfs_is_free_space_inode(BTRFS_I(inode)))
Chandan Rajendra0d51e282015-07-27 15:26:43 +05302116 metadata = BTRFS_WQ_ENDIO_FREE_SPACE;
Jeff Mahoney04173412011-10-03 23:23:12 -04002117
Mike Christie37226b22016-06-05 14:31:52 -05002118 if (bio_op(bio) != REQ_OP_WRITE) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002119 ret = btrfs_bio_wq_end_io(fs_info, bio, metadata);
Josef Bacik5fd02042012-05-02 14:00:54 -04002120 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01002121 goto out;
Josef Bacik5fd02042012-05-02 14:00:54 -04002122
Chris Masond20f7042008-12-08 16:58:54 -05002123 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Stefan Behrens61891922012-11-05 18:51:52 +01002124 ret = btrfs_submit_compressed_read(inode, bio,
2125 mirror_num,
2126 bio_flags);
2127 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00002128 } else if (!skip_sum) {
Omar Sandovaldb72e472019-12-10 10:37:35 -08002129 ret = btrfs_lookup_bio_sums(inode, bio, (u64)-1, NULL);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00002130 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01002131 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00002132 }
Chris Mason4d1b5fb2008-08-20 09:44:52 -04002133 goto mapit;
Josef Bacikb812ce22012-11-16 13:56:32 -05002134 } else if (async && !skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05002135 /* csum items have already been cloned */
2136 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
2137 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04002138 /* we're doing a write, do the async checksumming */
Josef Bacikc6100a42017-05-05 11:57:13 -04002139 ret = btrfs_wq_submit_bio(fs_info, bio, mirror_num, bio_flags,
Nikolay Borisove7681162019-04-10 17:24:41 +03002140 0, inode, btrfs_submit_bio_start);
Stefan Behrens61891922012-11-05 18:51:52 +01002141 goto out;
Josef Bacikb812ce22012-11-16 13:56:32 -05002142 } else if (!skip_sum) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002143 ret = btrfs_csum_one_bio(inode, bio, 0, 0);
Josef Bacikb812ce22012-11-16 13:56:32 -05002144 if (ret)
2145 goto out;
Chris Mason19b9bdb2008-10-30 14:23:13 -04002146 }
2147
Chris Mason0b86a832008-03-24 15:01:56 -04002148mapit:
Chris Mason08635ba2019-07-10 12:28:14 -07002149 ret = btrfs_map_bio(fs_info, bio, mirror_num);
Stefan Behrens61891922012-11-05 18:51:52 +01002150
2151out:
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002152 if (ret) {
2153 bio->bi_status = ret;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002154 bio_endio(bio);
2155 }
Stefan Behrens61891922012-11-05 18:51:52 +01002156 return ret;
Chris Mason065631f2008-02-20 12:07:25 -05002157}
Chris Mason6885f302008-02-20 16:11:05 -05002158
Chris Masond352ac62008-09-29 15:18:18 -04002159/*
2160 * given a list of ordered sums record them in the inode. This happens
2161 * at IO completion time based on sums calculated at bio submission time.
2162 */
Chris Masonba1da2f2008-07-17 12:54:15 -04002163static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
David Sterbadf9f6282017-02-10 19:35:37 +01002164 struct inode *inode, struct list_head *list)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002165{
Chris Masone6dcd2d2008-07-17 12:53:50 -04002166 struct btrfs_ordered_sum *sum;
Nikolay Borisovac01f262018-01-08 10:59:43 +02002167 int ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002168
Qinghuang Fengc6e30872009-01-21 10:59:08 -05002169 list_for_each_entry(sum, list, list) {
David Sterba7c2871a2017-11-08 01:07:43 +01002170 trans->adding_csums = true;
Nikolay Borisovac01f262018-01-08 10:59:43 +02002171 ret = btrfs_csum_file_blocks(trans,
Chris Masond20f7042008-12-08 16:58:54 -05002172 BTRFS_I(inode)->root->fs_info->csum_root, sum);
David Sterba7c2871a2017-11-08 01:07:43 +01002173 trans->adding_csums = false;
Nikolay Borisovac01f262018-01-08 10:59:43 +02002174 if (ret)
2175 return ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002176 }
2177 return 0;
2178}
2179
Josef Bacik2ac55d42010-02-03 19:33:23 +00002180int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
Filipe Mananae3b8a482017-11-04 00:16:59 +00002181 unsigned int extra_bits,
Nikolay Borisov330a5822019-07-17 16:18:17 +03002182 struct extent_state **cached_state)
Chris Masonea8c2812008-08-04 23:17:27 -04002183{
Johannes Thumshirnfdb1e122018-12-05 15:23:04 +01002184 WARN_ON(PAGE_ALIGNED(end));
Chris Masonea8c2812008-08-04 23:17:27 -04002185 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
Filipe Mananae3b8a482017-11-04 00:16:59 +00002186 extra_bits, cached_state);
Chris Masonea8c2812008-08-04 23:17:27 -04002187}
2188
Chris Masond352ac62008-09-29 15:18:18 -04002189/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04002190struct btrfs_writepage_fixup {
2191 struct page *page;
Josef Bacikf4b13632020-01-21 14:34:52 -05002192 struct inode *inode;
Chris Mason247e7432008-07-17 12:53:51 -04002193 struct btrfs_work work;
2194};
2195
Christoph Hellwigb2950862008-12-02 09:54:17 -05002196static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04002197{
2198 struct btrfs_writepage_fixup *fixup;
2199 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002200 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08002201 struct extent_changeset *data_reserved = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04002202 struct page *page;
2203 struct inode *inode;
2204 u64 page_start;
2205 u64 page_end;
Chris Mason25f3c502020-01-21 11:51:42 -05002206 int ret = 0;
Josef Bacikf4b13632020-01-21 14:34:52 -05002207 bool free_delalloc_space = true;
Chris Mason247e7432008-07-17 12:53:51 -04002208
2209 fixup = container_of(work, struct btrfs_writepage_fixup, work);
2210 page = fixup->page;
Josef Bacikf4b13632020-01-21 14:34:52 -05002211 inode = fixup->inode;
2212 page_start = page_offset(page);
2213 page_end = page_offset(page) + PAGE_SIZE - 1;
2214
2215 /*
2216 * This is similar to page_mkwrite, we need to reserve the space before
2217 * we take the page lock.
2218 */
2219 ret = btrfs_delalloc_reserve_space(inode, &data_reserved, page_start,
2220 PAGE_SIZE);
Chris Mason4a096752008-07-21 10:29:44 -04002221again:
Chris Mason247e7432008-07-17 12:53:51 -04002222 lock_page(page);
Chris Mason247e7432008-07-17 12:53:51 -04002223
Chris Mason25f3c502020-01-21 11:51:42 -05002224 /*
2225 * Before we queued this fixup, we took a reference on the page.
2226 * page->mapping may go NULL, but it shouldn't be moved to a different
2227 * address space.
2228 */
Josef Bacikf4b13632020-01-21 14:34:52 -05002229 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
2230 /*
2231 * Unfortunately this is a little tricky, either
2232 *
2233 * 1) We got here and our page had already been dealt with and
2234 * we reserved our space, thus ret == 0, so we need to just
2235 * drop our space reservation and bail. This can happen the
2236 * first time we come into the fixup worker, or could happen
2237 * while waiting for the ordered extent.
2238 * 2) Our page was already dealt with, but we happened to get an
2239 * ENOSPC above from the btrfs_delalloc_reserve_space. In
2240 * this case we obviously don't have anything to release, but
2241 * because the page was already dealt with we don't want to
2242 * mark the page with an error, so make sure we're resetting
2243 * ret to 0. This is why we have this check _before_ the ret
2244 * check, because we do not want to have a surprise ENOSPC
2245 * when the page was already properly dealt with.
2246 */
2247 if (!ret) {
2248 btrfs_delalloc_release_extents(BTRFS_I(inode),
2249 PAGE_SIZE);
2250 btrfs_delalloc_release_space(inode, data_reserved,
2251 page_start, PAGE_SIZE,
2252 true);
2253 }
2254 ret = 0;
Chris Mason25f3c502020-01-21 11:51:42 -05002255 goto out_page;
Josef Bacikf4b13632020-01-21 14:34:52 -05002256 }
Chris Mason25f3c502020-01-21 11:51:42 -05002257
2258 /*
Josef Bacikf4b13632020-01-21 14:34:52 -05002259 * We can't mess with the page state unless it is locked, so now that
2260 * it is locked bail if we failed to make our space reservation.
Chris Mason25f3c502020-01-21 11:51:42 -05002261 */
Josef Bacikf4b13632020-01-21 14:34:52 -05002262 if (ret)
2263 goto out_page;
Chris Mason247e7432008-07-17 12:53:51 -04002264
David Sterbaff13db42015-12-03 14:30:40 +01002265 lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002266 &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04002267
2268 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04002269 if (PagePrivate2(page))
Josef Bacikf4b13632020-01-21 14:34:52 -05002270 goto out_reserved;
Chris Mason4a096752008-07-21 10:29:44 -04002271
Nikolay Borisova776c6f2017-02-20 13:50:49 +02002272 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002273 PAGE_SIZE);
Chris Mason4a096752008-07-21 10:29:44 -04002274 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00002275 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
David Sterbae43bbe52017-12-12 21:43:52 +01002276 page_end, &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04002277 unlock_page(page);
2278 btrfs_start_ordered_extent(inode, ordered, 1);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002279 btrfs_put_ordered_extent(ordered);
Chris Mason4a096752008-07-21 10:29:44 -04002280 goto again;
2281 }
Chris Mason247e7432008-07-17 12:53:51 -04002282
Nikolay Borisovf3038ee2017-12-05 09:29:19 +02002283 ret = btrfs_set_extent_delalloc(inode, page_start, page_end, 0,
Nikolay Borisov330a5822019-07-17 16:18:17 +03002284 &cached_state);
Chris Mason25f3c502020-01-21 11:51:42 -05002285 if (ret)
Filipe Manana53687002019-10-09 17:43:59 +01002286 goto out_reserved;
Nikolay Borisovf3038ee2017-12-05 09:29:19 +02002287
Chris Mason25f3c502020-01-21 11:51:42 -05002288 /*
2289 * Everything went as planned, we're now the owner of a dirty page with
2290 * delayed allocation bits set and space reserved for our COW
2291 * destination.
2292 *
2293 * The page was dirty when we started, nothing should have cleaned it.
2294 */
2295 BUG_ON(!PageDirty(page));
Josef Bacikf4b13632020-01-21 14:34:52 -05002296 free_delalloc_space = false;
Filipe Manana53687002019-10-09 17:43:59 +01002297out_reserved:
Qu Wenruo8702ba92019-10-14 14:34:51 +08002298 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE);
Josef Bacikf4b13632020-01-21 14:34:52 -05002299 if (free_delalloc_space)
Filipe Manana53687002019-10-09 17:43:59 +01002300 btrfs_delalloc_release_space(inode, data_reserved, page_start,
2301 PAGE_SIZE, true);
Josef Bacik2ac55d42010-02-03 19:33:23 +00002302 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01002303 &cached_state);
Chris Mason247e7432008-07-17 12:53:51 -04002304out_page:
Chris Mason25f3c502020-01-21 11:51:42 -05002305 if (ret) {
2306 /*
2307 * We hit ENOSPC or other errors. Update the mapping and page
2308 * to reflect the errors and clean the page.
2309 */
2310 mapping_set_error(page->mapping, ret);
2311 end_extent_writepage(page, ret, page_start, page_end);
2312 clear_page_dirty_for_io(page);
2313 SetPageError(page);
2314 }
2315 ClearPageChecked(page);
Chris Mason247e7432008-07-17 12:53:51 -04002316 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002317 put_page(page);
Miao Xieb897abe2011-01-26 16:19:22 +08002318 kfree(fixup);
Qu Wenruo364ecf32017-02-27 15:10:38 +08002319 extent_changeset_free(data_reserved);
Josef Bacikf4b13632020-01-21 14:34:52 -05002320 /*
2321 * As a precaution, do a delayed iput in case it would be the last iput
2322 * that could need flushing space. Recursing back to fixup worker would
2323 * deadlock.
2324 */
2325 btrfs_add_delayed_iput(inode);
Chris Mason247e7432008-07-17 12:53:51 -04002326}
2327
2328/*
2329 * There are a few paths in the higher layers of the kernel that directly
2330 * set the page dirty bit without asking the filesystem if it is a
2331 * good idea. This causes problems because we want to make sure COW
2332 * properly happens and the data=ordered rules are followed.
2333 *
Chris Masonc8b97812008-10-29 14:49:59 -04002334 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04002335 * hasn't been properly setup for IO. We kick off an async process
2336 * to fix it up. The async helper will wait for ordered extents, set
2337 * the delalloc bit and make it safe to write the page.
2338 */
Nikolay Borisovd75855b2018-11-01 14:09:47 +02002339int btrfs_writepage_cow_fixup(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04002340{
2341 struct inode *inode = page->mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002342 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason247e7432008-07-17 12:53:51 -04002343 struct btrfs_writepage_fixup *fixup;
Chris Mason247e7432008-07-17 12:53:51 -04002344
Chris Mason8b62b722009-09-02 16:53:46 -04002345 /* this page is properly in the ordered list */
2346 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04002347 return 0;
2348
Chris Mason25f3c502020-01-21 11:51:42 -05002349 /*
2350 * PageChecked is set below when we create a fixup worker for this page,
2351 * don't try to create another one if we're already PageChecked()
2352 *
2353 * The extent_io writepage code will redirty the page if we send back
2354 * EAGAIN.
2355 */
Chris Mason247e7432008-07-17 12:53:51 -04002356 if (PageChecked(page))
2357 return -EAGAIN;
2358
2359 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
2360 if (!fixup)
2361 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04002362
Josef Bacikf4b13632020-01-21 14:34:52 -05002363 /*
2364 * We are already holding a reference to this inode from
2365 * write_cache_pages. We need to hold it because the space reservation
2366 * takes place outside of the page lock, and we can't trust
2367 * page->mapping outside of the page lock.
2368 */
2369 ihold(inode);
Chris Mason247e7432008-07-17 12:53:51 -04002370 SetPageChecked(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002371 get_page(page);
Omar Sandovala0cac0e2019-09-16 11:30:57 -07002372 btrfs_init_work(&fixup->work, btrfs_writepage_fixup_worker, NULL, NULL);
Chris Mason247e7432008-07-17 12:53:51 -04002373 fixup->page = page;
Josef Bacikf4b13632020-01-21 14:34:52 -05002374 fixup->inode = inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002375 btrfs_queue_work(fs_info->fixup_workers, &fixup->work);
Chris Mason25f3c502020-01-21 11:51:42 -05002376
2377 return -EAGAIN;
Chris Mason247e7432008-07-17 12:53:51 -04002378}
2379
Yan Zhengd899e052008-10-30 14:25:28 -04002380static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
2381 struct inode *inode, u64 file_pos,
2382 u64 disk_bytenr, u64 disk_num_bytes,
2383 u64 num_bytes, u64 ram_bytes,
2384 u8 compression, u8 encryption,
2385 u16 other_encoding, int extent_type)
2386{
2387 struct btrfs_root *root = BTRFS_I(inode)->root;
2388 struct btrfs_file_extent_item *fi;
2389 struct btrfs_path *path;
2390 struct extent_buffer *leaf;
2391 struct btrfs_key ins;
Qu Wenruoa12b8772017-02-27 15:10:37 +08002392 u64 qg_released;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002393 int extent_inserted = 0;
Yan Zhengd899e052008-10-30 14:25:28 -04002394 int ret;
2395
2396 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07002397 if (!path)
2398 return -ENOMEM;
Yan Zhengd899e052008-10-30 14:25:28 -04002399
Chris Masona1ed8352009-09-11 12:27:37 -04002400 /*
2401 * we may be replacing one extent in the tree with another.
2402 * The new extent is pinned in the extent map, and we don't want
2403 * to drop it from the cache until it is completely in the btree.
2404 *
2405 * So, tell btrfs_drop_extents to leave this extent in the cache.
2406 * the caller is expected to unpin it and allow it to be merged
2407 * with the others.
2408 */
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002409 ret = __btrfs_drop_extents(trans, root, inode, path, file_pos,
2410 file_pos + num_bytes, NULL, 0,
2411 1, sizeof(*fi), &extent_inserted);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002412 if (ret)
2413 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04002414
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002415 if (!extent_inserted) {
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002416 ins.objectid = btrfs_ino(BTRFS_I(inode));
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002417 ins.offset = file_pos;
2418 ins.type = BTRFS_EXTENT_DATA_KEY;
2419
2420 path->leave_spinning = 1;
2421 ret = btrfs_insert_empty_item(trans, root, path, &ins,
2422 sizeof(*fi));
2423 if (ret)
2424 goto out;
2425 }
Yan Zhengd899e052008-10-30 14:25:28 -04002426 leaf = path->nodes[0];
2427 fi = btrfs_item_ptr(leaf, path->slots[0],
2428 struct btrfs_file_extent_item);
2429 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
2430 btrfs_set_file_extent_type(leaf, fi, extent_type);
2431 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
2432 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
2433 btrfs_set_file_extent_offset(leaf, fi, 0);
2434 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
2435 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
2436 btrfs_set_file_extent_compression(leaf, fi, compression);
2437 btrfs_set_file_extent_encryption(leaf, fi, encryption);
2438 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
Chris Masonb9473432009-03-13 11:00:37 -04002439
Yan Zhengd899e052008-10-30 14:25:28 -04002440 btrfs_mark_buffer_dirty(leaf);
Josef Bacikce195332012-09-25 15:26:16 -04002441 btrfs_release_path(path);
Yan Zhengd899e052008-10-30 14:25:28 -04002442
2443 inode_add_bytes(inode, num_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -04002444
2445 ins.objectid = disk_bytenr;
2446 ins.offset = disk_num_bytes;
2447 ins.type = BTRFS_EXTENT_ITEM_KEY;
Qu Wenruoa12b8772017-02-27 15:10:37 +08002448
Qu Wenruo297d7502015-09-08 17:08:37 +08002449 /*
Qu Wenruo5846a3c2015-10-26 14:11:18 +08002450 * Release the reserved range from inode dirty range map, as it is
2451 * already moved into delayed_ref_head
Qu Wenruo297d7502015-09-08 17:08:37 +08002452 */
Qu Wenruoa12b8772017-02-27 15:10:37 +08002453 ret = btrfs_qgroup_release_data(inode, file_pos, ram_bytes);
2454 if (ret < 0)
2455 goto out;
2456 qg_released = ret;
Josef Bacik84f7d8e2017-09-29 15:43:49 -04002457 ret = btrfs_alloc_reserved_file_extent(trans, root,
2458 btrfs_ino(BTRFS_I(inode)),
2459 file_pos, qg_released, &ins);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002460out:
Yan Zhengd899e052008-10-30 14:25:28 -04002461 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04002462
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002463 return ret;
Yan Zhengd899e052008-10-30 14:25:28 -04002464}
2465
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002466static void btrfs_release_delalloc_bytes(struct btrfs_fs_info *fs_info,
Miao Xiee570fd22014-06-19 10:42:50 +08002467 u64 start, u64 len)
2468{
David Sterba32da53862019-10-29 19:20:18 +01002469 struct btrfs_block_group *cache;
Miao Xiee570fd22014-06-19 10:42:50 +08002470
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002471 cache = btrfs_lookup_block_group(fs_info, start);
Miao Xiee570fd22014-06-19 10:42:50 +08002472 ASSERT(cache);
2473
2474 spin_lock(&cache->lock);
2475 cache->delalloc_bytes -= len;
2476 spin_unlock(&cache->lock);
2477
2478 btrfs_put_block_group(cache);
2479}
2480
Chris Masond352ac62008-09-29 15:18:18 -04002481/* as ordered data IO finishes, this gets called so we can finish
2482 * an ordered extent if the range of bytes in the file it covers are
2483 * fully written.
2484 */
Josef Bacik5fd02042012-05-02 14:00:54 -04002485static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002486{
Josef Bacik5fd02042012-05-02 14:00:54 -04002487 struct inode *inode = ordered_extent->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002488 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002489 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002490 struct btrfs_trans_handle *trans = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002491 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002492 struct extent_state *cached_state = NULL;
Omar Sandovalbffe6332019-12-02 17:34:19 -08002493 u64 start, end;
Li Zefan261507a02010-12-17 14:21:50 +08002494 int compress_type = 0;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002495 int ret = 0;
Omar Sandovalbffe6332019-12-02 17:34:19 -08002496 u64 logical_len = ordered_extent->num_bytes;
Nikolay Borisov8d510122019-10-08 20:43:06 +03002497 bool freespace_inode;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002498 bool truncated = false;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002499 bool range_locked = false;
2500 bool clear_new_delalloc_bytes = false;
Josef Bacik49940bd2018-10-11 15:54:21 -04002501 bool clear_reserved_extent = true;
Omar Sandoval313facc2019-12-02 17:34:18 -08002502 unsigned int clear_bits;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002503
Omar Sandovalbffe6332019-12-02 17:34:19 -08002504 start = ordered_extent->file_offset;
2505 end = start + ordered_extent->num_bytes - 1;
2506
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002507 if (!test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
2508 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags) &&
2509 !test_bit(BTRFS_ORDERED_DIRECT, &ordered_extent->flags))
2510 clear_new_delalloc_bytes = true;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002511
Nikolay Borisov8d510122019-10-08 20:43:06 +03002512 freespace_inode = btrfs_is_free_space_inode(BTRFS_I(inode));
Josef Bacik0cb59c92010-07-02 12:14:14 -04002513
Josef Bacik5fd02042012-05-02 14:00:54 -04002514 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
2515 ret = -EIO;
2516 goto out;
2517 }
2518
Omar Sandovalbffe6332019-12-02 17:34:19 -08002519 btrfs_free_io_failure_record(BTRFS_I(inode), start, end);
Miao Xief6124962014-09-12 18:44:04 +08002520
Josef Bacik77cef2e2013-08-29 13:57:21 -04002521 if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) {
2522 truncated = true;
2523 logical_len = ordered_extent->truncated_len;
2524 /* Truncated the entire extent, don't bother adding */
2525 if (!logical_len)
2526 goto out;
2527 }
2528
Yan, Zhengc2167752009-11-12 09:34:21 +00002529 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002530 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
Qu Wenruo94ed9382015-09-08 17:25:56 +08002531
2532 /*
2533 * For mwrite(mmap + memset to write) case, we still reserve
2534 * space for NOCOW range.
2535 * As NOCOW won't cause a new delayed ref, just free the space
2536 */
Omar Sandovalbffe6332019-12-02 17:34:19 -08002537 btrfs_qgroup_free_data(inode, NULL, start,
2538 ordered_extent->num_bytes);
Josef Bacik6c760c02012-11-09 10:53:21 -05002539 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
Nikolay Borisov8d510122019-10-08 20:43:06 +03002540 if (freespace_inode)
2541 trans = btrfs_join_transaction_spacecache(root);
Josef Bacik6c760c02012-11-09 10:53:21 -05002542 else
2543 trans = btrfs_join_transaction(root);
2544 if (IS_ERR(trans)) {
2545 ret = PTR_ERR(trans);
2546 trans = NULL;
2547 goto out;
Yan, Zhengc2167752009-11-12 09:34:21 +00002548 }
Josef Bacik69fe2d72017-10-19 14:15:57 -04002549 trans->block_rsv = &BTRFS_I(inode)->block_rsv;
Josef Bacik6c760c02012-11-09 10:53:21 -05002550 ret = btrfs_update_inode_fallback(trans, root, inode);
2551 if (ret) /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04002552 btrfs_abort_transaction(trans, ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00002553 goto out;
2554 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002555
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002556 range_locked = true;
Omar Sandovalbffe6332019-12-02 17:34:19 -08002557 lock_extent_bits(io_tree, start, end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002558
Nikolay Borisov8d510122019-10-08 20:43:06 +03002559 if (freespace_inode)
2560 trans = btrfs_join_transaction_spacecache(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002561 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002562 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002563 if (IS_ERR(trans)) {
2564 ret = PTR_ERR(trans);
2565 trans = NULL;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002566 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002567 }
Chris Masona79b7d42014-05-22 16:18:52 -07002568
Josef Bacik69fe2d72017-10-19 14:15:57 -04002569 trans->block_rsv = &BTRFS_I(inode)->block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00002570
Chris Masonc8b97812008-10-29 14:49:59 -04002571 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08002572 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04002573 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08002574 BUG_ON(compress_type);
Omar Sandovalbffe6332019-12-02 17:34:19 -08002575 btrfs_qgroup_free_data(inode, NULL, start,
2576 ordered_extent->num_bytes);
Nikolay Borisov7a6d7062017-02-20 13:50:48 +02002577 ret = btrfs_mark_extent_written(trans, BTRFS_I(inode),
Yan Zhengd899e052008-10-30 14:25:28 -04002578 ordered_extent->file_offset,
2579 ordered_extent->file_offset +
Josef Bacik77cef2e2013-08-29 13:57:21 -04002580 logical_len);
Yan Zhengd899e052008-10-30 14:25:28 -04002581 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002582 BUG_ON(root == fs_info->tree_root);
Omar Sandovalbffe6332019-12-02 17:34:19 -08002583 ret = insert_reserved_file_extent(trans, inode, start,
2584 ordered_extent->disk_bytenr,
2585 ordered_extent->disk_num_bytes,
Josef Bacik77cef2e2013-08-29 13:57:21 -04002586 logical_len, logical_len,
Li Zefan261507a02010-12-17 14:21:50 +08002587 compress_type, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04002588 BTRFS_FILE_EXTENT_REG);
Josef Bacik49940bd2018-10-11 15:54:21 -04002589 if (!ret) {
2590 clear_reserved_extent = false;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002591 btrfs_release_delalloc_bytes(fs_info,
Omar Sandovalbffe6332019-12-02 17:34:19 -08002592 ordered_extent->disk_bytenr,
2593 ordered_extent->disk_num_bytes);
Josef Bacik49940bd2018-10-11 15:54:21 -04002594 }
Yan Zhengd899e052008-10-30 14:25:28 -04002595 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04002596 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
Omar Sandovalbffe6332019-12-02 17:34:19 -08002597 ordered_extent->file_offset,
2598 ordered_extent->num_bytes, trans->transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002599 if (ret < 0) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002600 btrfs_abort_transaction(trans, ret);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002601 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002602 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00002603
Nikolay Borisovac01f262018-01-08 10:59:43 +02002604 ret = add_pending_csums(trans, inode, &ordered_extent->list);
2605 if (ret) {
2606 btrfs_abort_transaction(trans, ret);
2607 goto out;
2608 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002609
Josef Bacik6c760c02012-11-09 10:53:21 -05002610 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2611 ret = btrfs_update_inode_fallback(trans, root, inode);
2612 if (ret) { /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04002613 btrfs_abort_transaction(trans, ret);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002614 goto out;
Josef Bacik1ef30be2011-04-05 19:25:36 -04002615 }
2616 ret = 0;
Yan, Zhengc2167752009-11-12 09:34:21 +00002617out:
Omar Sandoval313facc2019-12-02 17:34:18 -08002618 clear_bits = EXTENT_DEFRAG;
2619 if (range_locked)
2620 clear_bits |= EXTENT_LOCKED;
2621 if (clear_new_delalloc_bytes)
2622 clear_bits |= EXTENT_DELALLOC_NEW;
Omar Sandovalbffe6332019-12-02 17:34:19 -08002623 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, clear_bits,
2624 (clear_bits & EXTENT_LOCKED) ? 1 : 0, 0,
Omar Sandoval313facc2019-12-02 17:34:18 -08002625 &cached_state);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002626
Miao Xiea698d0752012-09-20 01:51:59 -06002627 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002628 btrfs_end_transaction(trans);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002629
Josef Bacik77cef2e2013-08-29 13:57:21 -04002630 if (ret || truncated) {
Omar Sandovalbffe6332019-12-02 17:34:19 -08002631 u64 unwritten_start = start;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002632
2633 if (truncated)
Omar Sandovalbffe6332019-12-02 17:34:19 -08002634 unwritten_start += logical_len;
2635 clear_extent_uptodate(io_tree, unwritten_start, end, NULL);
Josef Bacik77cef2e2013-08-29 13:57:21 -04002636
2637 /* Drop the cache for the part of the extent we didn't write. */
Omar Sandovalbffe6332019-12-02 17:34:19 -08002638 btrfs_drop_extent_cache(BTRFS_I(inode), unwritten_start, end, 0);
Josef Bacik5fd02042012-05-02 14:00:54 -04002639
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002640 /*
2641 * If the ordered extent had an IOERR or something else went
2642 * wrong we need to return the space for this ordered extent
Josef Bacik77cef2e2013-08-29 13:57:21 -04002643 * back to the allocator. We only free the extent in the
2644 * truncated case if we didn't write out the extent at all.
Josef Bacik49940bd2018-10-11 15:54:21 -04002645 *
2646 * If we made it past insert_reserved_file_extent before we
2647 * errored out then we don't need to do this as the accounting
2648 * has already been done.
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002649 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04002650 if ((ret || !logical_len) &&
Josef Bacik49940bd2018-10-11 15:54:21 -04002651 clear_reserved_extent &&
Josef Bacik77cef2e2013-08-29 13:57:21 -04002652 !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
Nikolay Borisov4eaaec22019-11-21 14:03:29 +02002653 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
2654 /*
2655 * Discard the range before returning it back to the
2656 * free space pool
2657 */
Dennis Zhou46b27f52019-12-13 16:22:11 -08002658 if (ret && btrfs_test_opt(fs_info, DISCARD_SYNC))
Nikolay Borisov4eaaec22019-11-21 14:03:29 +02002659 btrfs_discard_extent(fs_info,
Omar Sandovalbffe6332019-12-02 17:34:19 -08002660 ordered_extent->disk_bytenr,
2661 ordered_extent->disk_num_bytes,
2662 NULL);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002663 btrfs_free_reserved_extent(fs_info,
Omar Sandovalbffe6332019-12-02 17:34:19 -08002664 ordered_extent->disk_bytenr,
2665 ordered_extent->disk_num_bytes, 1);
Nikolay Borisov4eaaec22019-11-21 14:03:29 +02002666 }
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002667 }
2668
Josef Bacik5fd02042012-05-02 14:00:54 -04002669 /*
Liu Bo8bad3c02012-06-18 12:14:23 +08002670 * This needs to be done to make sure anybody waiting knows we are done
2671 * updating everything for this ordered extent.
Josef Bacik5fd02042012-05-02 14:00:54 -04002672 */
2673 btrfs_remove_ordered_extent(inode, ordered_extent);
2674
Chris Masone6dcd2d2008-07-17 12:53:50 -04002675 /* once for us */
2676 btrfs_put_ordered_extent(ordered_extent);
2677 /* once for the tree */
2678 btrfs_put_ordered_extent(ordered_extent);
2679
Josef Bacik5fd02042012-05-02 14:00:54 -04002680 return ret;
2681}
2682
2683static void finish_ordered_fn(struct btrfs_work *work)
2684{
2685 struct btrfs_ordered_extent *ordered_extent;
2686 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
2687 btrfs_finish_ordered_io(ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002688}
2689
Nikolay Borisovc6297322018-11-08 10:18:08 +02002690void btrfs_writepage_endio_finish_ordered(struct page *page, u64 start,
2691 u64 end, int uptodate)
Chris Mason211f90e2008-07-18 11:56:15 -04002692{
Josef Bacik5fd02042012-05-02 14:00:54 -04002693 struct inode *inode = page->mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002694 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik5fd02042012-05-02 14:00:54 -04002695 struct btrfs_ordered_extent *ordered_extent = NULL;
Liu Bo9e0af232014-08-15 23:36:53 +08002696 struct btrfs_workqueue *wq;
Josef Bacik5fd02042012-05-02 14:00:54 -04002697
liubo1abe9b82011-03-24 11:18:59 +00002698 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
2699
Chris Mason8b62b722009-09-02 16:53:46 -04002700 ClearPagePrivate2(page);
Josef Bacik5fd02042012-05-02 14:00:54 -04002701 if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
2702 end - start + 1, uptodate))
David Sterbac3988d62017-02-17 15:18:32 +01002703 return;
Josef Bacik5fd02042012-05-02 14:00:54 -04002704
Omar Sandovala0cac0e2019-09-16 11:30:57 -07002705 if (btrfs_is_free_space_inode(BTRFS_I(inode)))
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002706 wq = fs_info->endio_freespace_worker;
Omar Sandovala0cac0e2019-09-16 11:30:57 -07002707 else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002708 wq = fs_info->endio_write_workers;
Josef Bacik5fd02042012-05-02 14:00:54 -04002709
Omar Sandovala0cac0e2019-09-16 11:30:57 -07002710 btrfs_init_work(&ordered_extent->work, finish_ordered_fn, NULL, NULL);
Liu Bo9e0af232014-08-15 23:36:53 +08002711 btrfs_queue_work(wq, &ordered_extent->work);
Chris Mason211f90e2008-07-18 11:56:15 -04002712}
2713
Miao Xiedc380ae2014-09-12 18:43:55 +08002714static int __readpage_endio_check(struct inode *inode,
2715 struct btrfs_io_bio *io_bio,
2716 int icsum, struct page *page,
2717 int pgoff, u64 start, size_t len)
2718{
Johannes Thumshirnd5178572019-06-03 16:58:57 +02002719 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
2720 SHASH_DESC_ON_STACK(shash, fs_info->csum_shash);
Miao Xiedc380ae2014-09-12 18:43:55 +08002721 char *kaddr;
Johannes Thumshirnd5178572019-06-03 16:58:57 +02002722 u16 csum_size = btrfs_super_csum_size(fs_info->super_copy);
2723 u8 *csum_expected;
2724 u8 csum[BTRFS_CSUM_SIZE];
Miao Xiedc380ae2014-09-12 18:43:55 +08002725
Johannes Thumshirnd5178572019-06-03 16:58:57 +02002726 csum_expected = ((u8 *)io_bio->csum) + icsum * csum_size;
Miao Xiedc380ae2014-09-12 18:43:55 +08002727
2728 kaddr = kmap_atomic(page);
Johannes Thumshirnd5178572019-06-03 16:58:57 +02002729 shash->tfm = fs_info->csum_shash;
2730
2731 crypto_shash_init(shash);
2732 crypto_shash_update(shash, kaddr + pgoff, len);
2733 crypto_shash_final(shash, csum);
2734
2735 if (memcmp(csum, csum_expected, csum_size))
Miao Xiedc380ae2014-09-12 18:43:55 +08002736 goto zeroit;
2737
2738 kunmap_atomic(kaddr);
2739 return 0;
2740zeroit:
Johannes Thumshirnea41d6b2019-06-03 16:58:58 +02002741 btrfs_print_data_csum_error(BTRFS_I(inode), start, csum, csum_expected,
2742 io_bio->mirror_num);
Miao Xiedc380ae2014-09-12 18:43:55 +08002743 memset(kaddr + pgoff, 1, len);
2744 flush_dcache_page(page);
2745 kunmap_atomic(kaddr);
Miao Xiedc380ae2014-09-12 18:43:55 +08002746 return -EIO;
2747}
2748
Chris Masond352ac62008-09-29 15:18:18 -04002749/*
Chris Masond352ac62008-09-29 15:18:18 -04002750 * when reads are done, we need to check csums to verify the data is correct
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002751 * if there's a match, we allow the bio to finish. If not, the code in
2752 * extent_io.c will try to find good copies for us.
Chris Masond352ac62008-09-29 15:18:18 -04002753 */
Miao Xiefacc8a222013-07-25 19:22:34 +08002754static int btrfs_readpage_end_io_hook(struct btrfs_io_bio *io_bio,
2755 u64 phy_offset, struct page *page,
2756 u64 start, u64 end, int mirror)
Chris Mason07157aa2007-08-30 08:50:51 -04002757{
Miao Xie4eee4fa2012-12-21 09:17:45 +00002758 size_t offset = start - page_offset(page);
Chris Mason07157aa2007-08-30 08:50:51 -04002759 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05002760 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masonff79f812007-10-15 16:22:25 -04002761 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -05002762
Chris Masond20f7042008-12-08 16:58:54 -05002763 if (PageChecked(page)) {
2764 ClearPageChecked(page);
Miao Xiedc380ae2014-09-12 18:43:55 +08002765 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05002766 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02002767
2768 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Miao Xiedc380ae2014-09-12 18:43:55 +08002769 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05002770
Yan Zheng17d217f2008-12-12 10:03:38 -05002771 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04002772 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
David Sterba91166212016-04-26 23:54:39 +02002773 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM);
Yan Zheng17d217f2008-12-12 10:03:38 -05002774 return 0;
2775 }
2776
Miao Xiefacc8a222013-07-25 19:22:34 +08002777 phy_offset >>= inode->i_sb->s_blocksize_bits;
Miao Xiedc380ae2014-09-12 18:43:55 +08002778 return __readpage_endio_check(inode, io_bio, phy_offset, page, offset,
2779 start, (size_t)(end - start + 1));
Chris Mason07157aa2007-08-30 08:50:51 -04002780}
Chris Masonb888db22007-08-27 16:49:44 -04002781
Nikolay Borisovc1c3fac2018-01-16 09:31:58 +02002782/*
2783 * btrfs_add_delayed_iput - perform a delayed iput on @inode
2784 *
2785 * @inode: The inode we want to perform iput on
2786 *
2787 * This function uses the generic vfs_inode::i_count to track whether we should
2788 * just decrement it (in case it's > 1) or if this is the last iput then link
2789 * the inode to the delayed iput machinery. Delayed iputs are processed at
2790 * transaction commit time/superblock commit/cleaner kthread.
2791 */
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002792void btrfs_add_delayed_iput(struct inode *inode)
2793{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002794 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
David Sterba8089fe62015-11-19 14:15:51 +01002795 struct btrfs_inode *binode = BTRFS_I(inode);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002796
2797 if (atomic_add_unless(&inode->i_count, -1, 1))
2798 return;
2799
Josef Bacik034f7842018-12-03 11:06:52 -05002800 atomic_inc(&fs_info->nr_delayed_iputs);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002801 spin_lock(&fs_info->delayed_iput_lock);
Nikolay Borisovc1c3fac2018-01-16 09:31:58 +02002802 ASSERT(list_empty(&binode->delayed_iput));
2803 list_add_tail(&binode->delayed_iput, &fs_info->delayed_iputs);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002804 spin_unlock(&fs_info->delayed_iput_lock);
Josef Bacikfd340d02019-01-11 10:21:02 -05002805 if (!test_bit(BTRFS_FS_CLEANER_RUNNING, &fs_info->flags))
2806 wake_up_process(fs_info->cleaner_kthread);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002807}
2808
Josef Bacik63611e72019-06-18 10:59:18 -04002809static void run_delayed_iput_locked(struct btrfs_fs_info *fs_info,
2810 struct btrfs_inode *inode)
2811{
2812 list_del_init(&inode->delayed_iput);
2813 spin_unlock(&fs_info->delayed_iput_lock);
2814 iput(&inode->vfs_inode);
2815 if (atomic_dec_and_test(&fs_info->nr_delayed_iputs))
2816 wake_up(&fs_info->delayed_iputs_wait);
2817 spin_lock(&fs_info->delayed_iput_lock);
2818}
2819
2820static void btrfs_run_delayed_iput(struct btrfs_fs_info *fs_info,
2821 struct btrfs_inode *inode)
2822{
2823 if (!list_empty(&inode->delayed_iput)) {
2824 spin_lock(&fs_info->delayed_iput_lock);
2825 if (!list_empty(&inode->delayed_iput))
2826 run_delayed_iput_locked(fs_info, inode);
2827 spin_unlock(&fs_info->delayed_iput_lock);
2828 }
2829}
2830
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002831void btrfs_run_delayed_iputs(struct btrfs_fs_info *fs_info)
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002832{
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002833
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002834 spin_lock(&fs_info->delayed_iput_lock);
David Sterba8089fe62015-11-19 14:15:51 +01002835 while (!list_empty(&fs_info->delayed_iputs)) {
2836 struct btrfs_inode *inode;
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002837
David Sterba8089fe62015-11-19 14:15:51 +01002838 inode = list_first_entry(&fs_info->delayed_iputs,
2839 struct btrfs_inode, delayed_iput);
Josef Bacik63611e72019-06-18 10:59:18 -04002840 run_delayed_iput_locked(fs_info, inode);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002841 }
David Sterba8089fe62015-11-19 14:15:51 +01002842 spin_unlock(&fs_info->delayed_iput_lock);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002843}
2844
Josef Bacik034f7842018-12-03 11:06:52 -05002845/**
2846 * btrfs_wait_on_delayed_iputs - wait on the delayed iputs to be done running
2847 * @fs_info - the fs_info for this fs
2848 * @return - EINTR if we were killed, 0 if nothing's pending
2849 *
2850 * This will wait on any delayed iputs that are currently running with KILLABLE
2851 * set. Once they are all done running we will return, unless we are killed in
2852 * which case we return EINTR. This helps in user operations like fallocate etc
2853 * that might get blocked on the iputs.
2854 */
2855int btrfs_wait_on_delayed_iputs(struct btrfs_fs_info *fs_info)
2856{
2857 int ret = wait_event_killable(fs_info->delayed_iputs_wait,
2858 atomic_read(&fs_info->nr_delayed_iputs) == 0);
2859 if (ret)
2860 return -EINTR;
2861 return 0;
2862}
2863
Yan, Zhengd68fc572010-05-16 10:49:58 -04002864/*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07002865 * This creates an orphan entry for the given inode in case something goes wrong
2866 * in the middle of an unlink.
Josef Bacik7b128762008-07-24 12:17:14 -04002867 */
Nikolay Borisov73f2e542017-02-20 13:50:59 +02002868int btrfs_orphan_add(struct btrfs_trans_handle *trans,
Omar Sandoval27919062018-05-11 13:13:37 -07002869 struct btrfs_inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04002870{
Yan, Zhengd68fc572010-05-16 10:49:58 -04002871 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04002872
Omar Sandoval27919062018-05-11 13:13:37 -07002873 ret = btrfs_insert_orphan_item(trans, inode->root, btrfs_ino(inode));
2874 if (ret && ret != -EEXIST) {
2875 btrfs_abort_transaction(trans, ret);
2876 return ret;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002877 }
2878
Yan, Zhengd68fc572010-05-16 10:49:58 -04002879 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04002880}
2881
2882/*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07002883 * We have done the delete so we can go ahead and remove the orphan item for
2884 * this particular inode.
Josef Bacik7b128762008-07-24 12:17:14 -04002885 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00002886static int btrfs_orphan_del(struct btrfs_trans_handle *trans,
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02002887 struct btrfs_inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04002888{
Omar Sandoval27919062018-05-11 13:13:37 -07002889 return btrfs_del_orphan_item(trans, inode->root, btrfs_ino(inode));
Josef Bacik7b128762008-07-24 12:17:14 -04002890}
2891
2892/*
2893 * this cleans up any orphans that may be left on the list from the last use
2894 * of this root.
2895 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002896int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04002897{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002898 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik7b128762008-07-24 12:17:14 -04002899 struct btrfs_path *path;
2900 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04002901 struct btrfs_key key, found_key;
2902 struct btrfs_trans_handle *trans;
2903 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04002904 u64 last_objectid = 0;
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07002905 int ret = 0, nr_unlink = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04002906
Yan, Zhengd68fc572010-05-16 10:49:58 -04002907 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002908 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00002909
2910 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002911 if (!path) {
2912 ret = -ENOMEM;
2913 goto out;
2914 }
David Sterbae4058b52015-11-27 16:31:35 +01002915 path->reada = READA_BACK;
Josef Bacik7b128762008-07-24 12:17:14 -04002916
2917 key.objectid = BTRFS_ORPHAN_OBJECTID;
David Sterba962a2982014-06-04 18:41:45 +02002918 key.type = BTRFS_ORPHAN_ITEM_KEY;
Josef Bacik7b128762008-07-24 12:17:14 -04002919 key.offset = (u64)-1;
2920
Josef Bacik7b128762008-07-24 12:17:14 -04002921 while (1) {
2922 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002923 if (ret < 0)
2924 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04002925
2926 /*
2927 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002928 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04002929 * find the key and see if we have stuff that matches
2930 */
2931 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002932 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04002933 if (path->slots[0] == 0)
2934 break;
2935 path->slots[0]--;
2936 }
2937
2938 /* pull out the item */
2939 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04002940 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2941
2942 /* make sure the item matches what we want */
2943 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
2944 break;
David Sterba962a2982014-06-04 18:41:45 +02002945 if (found_key.type != BTRFS_ORPHAN_ITEM_KEY)
Josef Bacik7b128762008-07-24 12:17:14 -04002946 break;
2947
2948 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02002949 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04002950
2951 /*
2952 * this is where we are basically btrfs_lookup, without the
2953 * crossing root thing. we store the inode number in the
2954 * offset of the orphan item.
2955 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04002956
2957 if (found_key.offset == last_objectid) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002958 btrfs_err(fs_info,
2959 "Error removing orphan entry, stopping orphan cleanup");
Josef Bacik8f6d7f42011-09-26 15:55:20 -04002960 ret = -EINVAL;
2961 goto out;
2962 }
2963
2964 last_objectid = found_key.offset;
2965
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002966 found_key.objectid = found_key.offset;
2967 found_key.type = BTRFS_INODE_ITEM_KEY;
2968 found_key.offset = 0;
David Sterba4c66e0d2019-10-03 19:09:35 +02002969 inode = btrfs_iget(fs_info->sb, &found_key, root);
Rusty Russell8c6ffba2013-07-15 11:20:32 +09302970 ret = PTR_ERR_OR_ZERO(inode);
Filipe Manana67710892016-06-06 11:51:25 +01002971 if (ret && ret != -ENOENT)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002972 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04002973
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002974 if (ret == -ENOENT && root == fs_info->tree_root) {
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05002975 struct btrfs_root *dead_root;
2976 struct btrfs_fs_info *fs_info = root->fs_info;
2977 int is_dead_root = 0;
2978
2979 /*
2980 * this is an orphan in the tree root. Currently these
2981 * could come from 2 sources:
2982 * a) a snapshot deletion in progress
2983 * b) a free space cache inode
2984 * We need to distinguish those two, as the snapshot
2985 * orphan must not get deleted.
2986 * find_dead_roots already ran before us, so if this
2987 * is a snapshot deletion, we should find the root
2988 * in the dead_roots list
2989 */
2990 spin_lock(&fs_info->trans_lock);
2991 list_for_each_entry(dead_root, &fs_info->dead_roots,
2992 root_list) {
2993 if (dead_root->root_key.objectid ==
2994 found_key.objectid) {
2995 is_dead_root = 1;
2996 break;
2997 }
2998 }
2999 spin_unlock(&fs_info->trans_lock);
3000 if (is_dead_root) {
3001 /* prevent this orphan from being found again */
3002 key.offset = found_key.objectid - 1;
3003 continue;
3004 }
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003005
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003006 }
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003007
Josef Bacika8c9e572011-09-21 16:55:59 -04003008 /*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003009 * If we have an inode with links, there are a couple of
3010 * possibilities. Old kernels (before v3.12) used to create an
3011 * orphan item for truncate indicating that there were possibly
3012 * extent items past i_size that needed to be deleted. In v3.12,
3013 * truncate was changed to update i_size in sync with the extent
3014 * items, but the (useless) orphan item was still created. Since
3015 * v4.18, we don't create the orphan item for truncate at all.
3016 *
3017 * So, this item could mean that we need to do a truncate, but
3018 * only if this filesystem was last used on a pre-v3.12 kernel
3019 * and was not cleanly unmounted. The odds of that are quite
3020 * slim, and it's a pain to do the truncate now, so just delete
3021 * the orphan item.
3022 *
3023 * It's also possible that this orphan item was supposed to be
3024 * deleted but wasn't. The inode number may have been reused,
3025 * but either way, we can delete the orphan item.
Josef Bacika8c9e572011-09-21 16:55:59 -04003026 */
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003027 if (ret == -ENOENT || inode->i_nlink) {
3028 if (!ret)
3029 iput(inode);
Josef Bacika8c9e572011-09-21 16:55:59 -04003030 trans = btrfs_start_transaction(root, 1);
3031 if (IS_ERR(trans)) {
3032 ret = PTR_ERR(trans);
3033 goto out;
3034 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003035 btrfs_debug(fs_info, "auto deleting %Lu",
3036 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003037 ret = btrfs_del_orphan_item(trans, root,
3038 found_key.objectid);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003039 btrfs_end_transaction(trans);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003040 if (ret)
3041 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003042 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003043 }
Josef Bacik7b128762008-07-24 12:17:14 -04003044
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003045 nr_unlink++;
Josef Bacik7b128762008-07-24 12:17:14 -04003046
3047 /* this will do delete_inode and everything for us */
3048 iput(inode);
3049 }
Miao Xie3254c872011-11-10 20:45:05 -05003050 /* release the path since we're done with it */
3051 btrfs_release_path(path);
3052
Yan, Zhengd68fc572010-05-16 10:49:58 -04003053 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
3054
Omar Sandovala575cee2018-05-11 13:13:38 -07003055 if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003056 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003057 if (!IS_ERR(trans))
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003058 btrfs_end_transaction(trans);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003059 }
Josef Bacik7b128762008-07-24 12:17:14 -04003060
3061 if (nr_unlink)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003062 btrfs_debug(fs_info, "unlinked %d orphans", nr_unlink);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003063
3064out:
3065 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003066 btrfs_err(fs_info, "could not do orphan cleanup %d", ret);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003067 btrfs_free_path(path);
3068 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003069}
3070
Chris Masond352ac62008-09-29 15:18:18 -04003071/*
Chris Mason46a53cc2009-04-27 11:47:50 -04003072 * very simple check to peek ahead in the leaf looking for xattrs. If we
3073 * don't find any xattrs, we know there can't be any acls.
3074 *
3075 * slot is the slot the inode is in, objectid is the objectid of the inode
3076 */
3077static noinline int acls_after_inode_item(struct extent_buffer *leaf,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003078 int slot, u64 objectid,
3079 int *first_xattr_slot)
Chris Mason46a53cc2009-04-27 11:47:50 -04003080{
3081 u32 nritems = btrfs_header_nritems(leaf);
3082 struct btrfs_key found_key;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003083 static u64 xattr_access = 0;
3084 static u64 xattr_default = 0;
Chris Mason46a53cc2009-04-27 11:47:50 -04003085 int scanned = 0;
3086
Josef Bacikf23b5a52013-06-19 10:16:26 -04003087 if (!xattr_access) {
Andreas Gruenbacher97d79292015-12-02 14:44:35 +01003088 xattr_access = btrfs_name_hash(XATTR_NAME_POSIX_ACL_ACCESS,
3089 strlen(XATTR_NAME_POSIX_ACL_ACCESS));
3090 xattr_default = btrfs_name_hash(XATTR_NAME_POSIX_ACL_DEFAULT,
3091 strlen(XATTR_NAME_POSIX_ACL_DEFAULT));
Josef Bacikf23b5a52013-06-19 10:16:26 -04003092 }
3093
Chris Mason46a53cc2009-04-27 11:47:50 -04003094 slot++;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003095 *first_xattr_slot = -1;
Chris Mason46a53cc2009-04-27 11:47:50 -04003096 while (slot < nritems) {
3097 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3098
3099 /* we found a different objectid, there must not be acls */
3100 if (found_key.objectid != objectid)
3101 return 0;
3102
3103 /* we found an xattr, assume we've got an acl */
Josef Bacikf23b5a52013-06-19 10:16:26 -04003104 if (found_key.type == BTRFS_XATTR_ITEM_KEY) {
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003105 if (*first_xattr_slot == -1)
3106 *first_xattr_slot = slot;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003107 if (found_key.offset == xattr_access ||
3108 found_key.offset == xattr_default)
3109 return 1;
3110 }
Chris Mason46a53cc2009-04-27 11:47:50 -04003111
3112 /*
3113 * we found a key greater than an xattr key, there can't
3114 * be any acls later on
3115 */
3116 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
3117 return 0;
3118
3119 slot++;
3120 scanned++;
3121
3122 /*
3123 * it goes inode, inode backrefs, xattrs, extents,
3124 * so if there are a ton of hard links to an inode there can
3125 * be a lot of backrefs. Don't waste time searching too hard,
3126 * this is just an optimization
3127 */
3128 if (scanned >= 8)
3129 break;
3130 }
3131 /* we hit the end of the leaf before we found an xattr or
3132 * something larger than an xattr. We have to assume the inode
3133 * has acls
3134 */
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003135 if (*first_xattr_slot == -1)
3136 *first_xattr_slot = slot;
Chris Mason46a53cc2009-04-27 11:47:50 -04003137 return 1;
3138}
3139
3140/*
Chris Masond352ac62008-09-29 15:18:18 -04003141 * read an inode from the btree into the in-memory inode
3142 */
Filipe Manana4222ea72018-10-24 10:13:03 +01003143static int btrfs_read_locked_inode(struct inode *inode,
3144 struct btrfs_path *in_path)
Chris Mason39279cc2007-06-12 06:35:45 -04003145{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003146 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Filipe Manana4222ea72018-10-24 10:13:03 +01003147 struct btrfs_path *path = in_path;
Chris Mason5f39d392007-10-15 16:14:19 -04003148 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003149 struct btrfs_inode_item *inode_item;
3150 struct btrfs_root *root = BTRFS_I(inode)->root;
3151 struct btrfs_key location;
Miao Xie67de1172013-12-26 13:07:06 +08003152 unsigned long ptr;
Chris Mason46a53cc2009-04-27 11:47:50 -04003153 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04003154 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04003155 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003156 bool filled = false;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003157 int first_xattr_slot;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003158
3159 ret = btrfs_fill_inode(inode, &rdev);
3160 if (!ret)
3161 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04003162
Filipe Manana4222ea72018-10-24 10:13:03 +01003163 if (!path) {
3164 path = btrfs_alloc_path();
3165 if (!path)
3166 return -ENOMEM;
3167 }
Mark Fasheh1748f842011-07-12 11:25:31 -07003168
Chris Mason39279cc2007-06-12 06:35:45 -04003169 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05003170
Chris Mason39279cc2007-06-12 06:35:45 -04003171 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Filipe Manana67710892016-06-06 11:51:25 +01003172 if (ret) {
Filipe Manana4222ea72018-10-24 10:13:03 +01003173 if (path != in_path)
3174 btrfs_free_path(path);
Al Virof5b3a412018-07-29 23:04:51 +01003175 return ret;
Filipe Manana67710892016-06-06 11:51:25 +01003176 }
Chris Mason39279cc2007-06-12 06:35:45 -04003177
Chris Mason5f39d392007-10-15 16:14:19 -04003178 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00003179
3180 if (filled)
Miao Xie67de1172013-12-26 13:07:06 +08003181 goto cache_index;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003182
Chris Mason5f39d392007-10-15 16:14:19 -04003183 inode_item = btrfs_item_ptr(leaf, path->slots[0],
3184 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003185 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02003186 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08003187 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
3188 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02003189 btrfs_i_size_write(BTRFS_I(inode), btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04003190
David Sterbaa937b972014-12-12 17:39:12 +01003191 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->atime);
3192 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->atime);
Chris Mason5f39d392007-10-15 16:14:19 -04003193
David Sterbaa937b972014-12-12 17:39:12 +01003194 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->mtime);
3195 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->mtime);
Chris Mason5f39d392007-10-15 16:14:19 -04003196
David Sterbaa937b972014-12-12 17:39:12 +01003197 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->ctime);
3198 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->ctime);
Chris Mason5f39d392007-10-15 16:14:19 -04003199
chandan r9cc97d62012-07-04 12:48:07 +05303200 BTRFS_I(inode)->i_otime.tv_sec =
3201 btrfs_timespec_sec(leaf, &inode_item->otime);
3202 BTRFS_I(inode)->i_otime.tv_nsec =
3203 btrfs_timespec_nsec(leaf, &inode_item->otime);
Chris Mason5f39d392007-10-15 16:14:19 -04003204
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003205 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003206 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003207 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
3208
Jeff Laytonc7f88c42017-12-11 06:35:12 -05003209 inode_set_iversion_queried(inode,
3210 btrfs_inode_sequence(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003211 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04003212 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003213 rdev = btrfs_inode_rdev(leaf, inode_item);
3214
Josef Bacikaec74772008-07-24 12:12:38 -04003215 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05003216 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Miao Xie67de1172013-12-26 13:07:06 +08003217
3218cache_index:
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003219 /*
3220 * If we were modified in the current generation and evicted from memory
3221 * and then re-read we need to do a full sync since we don't have any
3222 * idea about which extents were modified before we were evicted from
3223 * cache.
3224 *
3225 * This is required for both inode re-read from disk and delayed inode
3226 * in delayed_nodes_tree.
3227 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003228 if (BTRFS_I(inode)->last_trans == fs_info->generation)
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003229 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3230 &BTRFS_I(inode)->runtime_flags);
3231
Filipe Mananabde6c242015-07-24 00:00:19 +01003232 /*
3233 * We don't persist the id of the transaction where an unlink operation
3234 * against the inode was last made. So here we assume the inode might
3235 * have been evicted, and therefore the exact value of last_unlink_trans
3236 * lost, and set it to last_trans to avoid metadata inconsistencies
3237 * between the inode and its parent if the inode is fsync'ed and the log
3238 * replayed. For example, in the scenario:
3239 *
3240 * touch mydir/foo
3241 * ln mydir/foo mydir/bar
3242 * sync
3243 * unlink mydir/bar
3244 * echo 2 > /proc/sys/vm/drop_caches # evicts inode
3245 * xfs_io -c fsync mydir/foo
3246 * <power failure>
3247 * mount fs, triggers fsync log replay
3248 *
3249 * We must make sure that when we fsync our inode foo we also log its
3250 * parent inode, otherwise after log replay the parent still has the
3251 * dentry with the "bar" name but our inode foo has a link count of 1
3252 * and doesn't have an inode ref with the name "bar" anymore.
3253 *
3254 * Setting last_unlink_trans to last_trans is a pessimistic approach,
Nicholas D Steeves01327612016-05-19 21:18:45 -04003255 * but it guarantees correctness at the expense of occasional full
Filipe Mananabde6c242015-07-24 00:00:19 +01003256 * transaction commits on fsync if our inode is a directory, or if our
3257 * inode is not a directory, logging its parent unnecessarily.
3258 */
3259 BTRFS_I(inode)->last_unlink_trans = BTRFS_I(inode)->last_trans;
3260
Miao Xie67de1172013-12-26 13:07:06 +08003261 path->slots[0]++;
3262 if (inode->i_nlink != 1 ||
3263 path->slots[0] >= btrfs_header_nritems(leaf))
3264 goto cache_acl;
3265
3266 btrfs_item_key_to_cpu(leaf, &location, path->slots[0]);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003267 if (location.objectid != btrfs_ino(BTRFS_I(inode)))
Miao Xie67de1172013-12-26 13:07:06 +08003268 goto cache_acl;
3269
3270 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3271 if (location.type == BTRFS_INODE_REF_KEY) {
3272 struct btrfs_inode_ref *ref;
3273
3274 ref = (struct btrfs_inode_ref *)ptr;
3275 BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref);
3276 } else if (location.type == BTRFS_INODE_EXTREF_KEY) {
3277 struct btrfs_inode_extref *extref;
3278
3279 extref = (struct btrfs_inode_extref *)ptr;
3280 BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf,
3281 extref);
3282 }
Miao Xie2f7e33d2011-06-23 07:27:13 +00003283cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04003284 /*
3285 * try to precache a NULL acl entry for files that don't have
3286 * any xattrs or acls
3287 */
Li Zefan33345d012011-04-20 10:31:50 +08003288 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
David Sterbaf85b7372017-01-20 14:54:07 +01003289 btrfs_ino(BTRFS_I(inode)), &first_xattr_slot);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003290 if (first_xattr_slot != -1) {
3291 path->slots[0] = first_xattr_slot;
3292 ret = btrfs_load_inode_props(inode, path);
3293 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003294 btrfs_err(fs_info,
David Sterba351fd352014-05-15 16:48:20 +02003295 "error loading props for ino %llu (root %llu): %d",
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003296 btrfs_ino(BTRFS_I(inode)),
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003297 root->root_key.objectid, ret);
3298 }
Filipe Manana4222ea72018-10-24 10:13:03 +01003299 if (path != in_path)
3300 btrfs_free_path(path);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003301
Al Viro72c04902009-06-24 16:58:48 -04003302 if (!maybe_acls)
3303 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04003304
Chris Mason39279cc2007-06-12 06:35:45 -04003305 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04003306 case S_IFREG:
3307 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masond1310b22008-01-24 16:13:08 -05003308 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04003309 inode->i_fop = &btrfs_file_operations;
3310 inode->i_op = &btrfs_file_inode_operations;
3311 break;
3312 case S_IFDIR:
3313 inode->i_fop = &btrfs_dir_file_operations;
Omar Sandoval67ade052017-01-25 17:06:38 -08003314 inode->i_op = &btrfs_dir_inode_operations;
Chris Mason39279cc2007-06-12 06:35:45 -04003315 break;
3316 case S_IFLNK:
3317 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -05003318 inode_nohighmem(inode);
Omar Sandoval4779cc02018-09-24 15:16:55 -07003319 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason39279cc2007-06-12 06:35:45 -04003320 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04003321 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05003322 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04003323 init_special_inode(inode, inode->i_mode, rdev);
3324 break;
Chris Mason39279cc2007-06-12 06:35:45 -04003325 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003326
David Sterba7b6a2212018-03-26 18:40:21 +02003327 btrfs_sync_inode_flags_to_i_flags(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003328 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003329}
3330
Chris Masond352ac62008-09-29 15:18:18 -04003331/*
3332 * given a leaf and an inode, copy the inode fields into the leaf
3333 */
Chris Masone02119d2008-09-05 16:13:11 -04003334static void fill_inode_item(struct btrfs_trans_handle *trans,
3335 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04003336 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04003337 struct inode *inode)
3338{
Liu Bo51fab692012-12-27 09:01:21 +00003339 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003340
David Sterbac82f8232019-08-09 17:48:21 +02003341 btrfs_init_map_token(&token, leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04003342
Liu Bo51fab692012-12-27 09:01:21 +00003343 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
3344 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
3345 btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size,
3346 &token);
3347 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3348 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003349
David Sterbaa937b972014-12-12 17:39:12 +01003350 btrfs_set_token_timespec_sec(leaf, &item->atime,
Liu Bo51fab692012-12-27 09:01:21 +00003351 inode->i_atime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003352 btrfs_set_token_timespec_nsec(leaf, &item->atime,
Liu Bo51fab692012-12-27 09:01:21 +00003353 inode->i_atime.tv_nsec, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003354
David Sterbaa937b972014-12-12 17:39:12 +01003355 btrfs_set_token_timespec_sec(leaf, &item->mtime,
Liu Bo51fab692012-12-27 09:01:21 +00003356 inode->i_mtime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003357 btrfs_set_token_timespec_nsec(leaf, &item->mtime,
Liu Bo51fab692012-12-27 09:01:21 +00003358 inode->i_mtime.tv_nsec, &token);
3359
David Sterbaa937b972014-12-12 17:39:12 +01003360 btrfs_set_token_timespec_sec(leaf, &item->ctime,
Liu Bo51fab692012-12-27 09:01:21 +00003361 inode->i_ctime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003362 btrfs_set_token_timespec_nsec(leaf, &item->ctime,
Liu Bo51fab692012-12-27 09:01:21 +00003363 inode->i_ctime.tv_nsec, &token);
3364
chandan r9cc97d62012-07-04 12:48:07 +05303365 btrfs_set_token_timespec_sec(leaf, &item->otime,
3366 BTRFS_I(inode)->i_otime.tv_sec, &token);
3367 btrfs_set_token_timespec_nsec(leaf, &item->otime,
3368 BTRFS_I(inode)->i_otime.tv_nsec, &token);
3369
Liu Bo51fab692012-12-27 09:01:21 +00003370 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3371 &token);
3372 btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation,
3373 &token);
Jeff Laytonc7f88c42017-12-11 06:35:12 -05003374 btrfs_set_token_inode_sequence(leaf, item, inode_peek_iversion(inode),
3375 &token);
Liu Bo51fab692012-12-27 09:01:21 +00003376 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
3377 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3378 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3379 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
Chris Mason39279cc2007-06-12 06:35:45 -04003380}
3381
Chris Masond352ac62008-09-29 15:18:18 -04003382/*
3383 * copy everything in the in-memory inode into the btree.
3384 */
Chris Mason21151332011-11-10 20:39:08 -05003385static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Chris Masond3977122009-01-05 21:25:51 -05003386 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003387{
3388 struct btrfs_inode_item *inode_item;
3389 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003390 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003391 int ret;
3392
3393 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08003394 if (!path)
3395 return -ENOMEM;
3396
Chris Masonb9473432009-03-13 11:00:37 -04003397 path->leave_spinning = 1;
Miao Xie16cdcec2011-04-22 18:12:22 +08003398 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
3399 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003400 if (ret) {
3401 if (ret > 0)
3402 ret = -ENOENT;
3403 goto failed;
3404 }
3405
Chris Mason5f39d392007-10-15 16:14:19 -04003406 leaf = path->nodes[0];
3407 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08003408 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04003409
Chris Masone02119d2008-09-05 16:13:11 -04003410 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04003411 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04003412 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003413 ret = 0;
3414failed:
Chris Mason39279cc2007-06-12 06:35:45 -04003415 btrfs_free_path(path);
3416 return ret;
3417}
3418
Chris Masond352ac62008-09-29 15:18:18 -04003419/*
Chris Mason21151332011-11-10 20:39:08 -05003420 * copy everything in the in-memory inode into the btree.
3421 */
3422noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
3423 struct btrfs_root *root, struct inode *inode)
3424{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003425 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason21151332011-11-10 20:39:08 -05003426 int ret;
3427
3428 /*
3429 * If the inode is a free space inode, we can deadlock during commit
3430 * if we put it into the delayed code.
3431 *
3432 * The data relocation inode should also be directly updated
3433 * without delay
3434 */
Nikolay Borisov70ddc552017-02-20 13:50:35 +02003435 if (!btrfs_is_free_space_inode(BTRFS_I(inode))
Josef Bacik1d52c782014-09-18 11:30:44 -04003436 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003437 && !test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) {
Alexander Block8ea05e32012-07-25 17:35:53 +02003438 btrfs_update_root_times(trans, root);
3439
Chris Mason21151332011-11-10 20:39:08 -05003440 ret = btrfs_delayed_update_inode(trans, root, inode);
3441 if (!ret)
3442 btrfs_set_inode_last_trans(trans, inode);
3443 return ret;
3444 }
3445
3446 return btrfs_update_inode_item(trans, root, inode);
3447}
3448
Josef Bacikbe6aef62012-10-22 15:43:12 -04003449noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
3450 struct btrfs_root *root,
3451 struct inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05003452{
3453 int ret;
3454
3455 ret = btrfs_update_inode(trans, root, inode);
3456 if (ret == -ENOSPC)
3457 return btrfs_update_inode_item(trans, root, inode);
3458 return ret;
3459}
3460
3461/*
Chris Masond352ac62008-09-29 15:18:18 -04003462 * unlink helper that gets used here in inode.c and in the tree logging
3463 * recovery code. It remove a link in a directory with a given name, and
3464 * also drops the back refs in the inode to the directory
3465 */
Al Viro92986792011-03-04 17:14:37 +00003466static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3467 struct btrfs_root *root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003468 struct btrfs_inode *dir,
3469 struct btrfs_inode *inode,
Al Viro92986792011-03-04 17:14:37 +00003470 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04003471{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003472 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04003473 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04003474 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003475 struct btrfs_dir_item *di;
Josef Bacikaec74772008-07-24 12:12:38 -04003476 u64 index;
Li Zefan33345d012011-04-20 10:31:50 +08003477 u64 ino = btrfs_ino(inode);
3478 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04003479
3480 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04003481 if (!path) {
3482 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00003483 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04003484 }
3485
Chris Masonb9473432009-03-13 11:00:37 -04003486 path->leave_spinning = 1;
Li Zefan33345d012011-04-20 10:31:50 +08003487 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04003488 name, name_len, -1);
Liu Bo3cf50682018-09-12 06:06:26 +08003489 if (IS_ERR_OR_NULL(di)) {
3490 ret = di ? PTR_ERR(di) : -ENOENT;
Chris Mason39279cc2007-06-12 06:35:45 -04003491 goto err;
3492 }
Chris Mason39279cc2007-06-12 06:35:45 -04003493 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04003494 if (ret)
3495 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02003496 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003497
Miao Xie67de1172013-12-26 13:07:06 +08003498 /*
3499 * If we don't have dir index, we have to get it by looking up
3500 * the inode ref, since we get the inode ref, remove it directly,
3501 * it is unnecessary to do delayed deletion.
3502 *
3503 * But if we have dir index, needn't search inode ref to get it.
3504 * Since the inode ref is close to the inode item, it is better
3505 * that we delay to delete it, and just do this deletion when
3506 * we update the inode item.
3507 */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003508 if (inode->dir_index) {
Miao Xie67de1172013-12-26 13:07:06 +08003509 ret = btrfs_delayed_delete_inode_ref(inode);
3510 if (!ret) {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003511 index = inode->dir_index;
Miao Xie67de1172013-12-26 13:07:06 +08003512 goto skip_backref;
3513 }
3514 }
3515
Li Zefan33345d012011-04-20 10:31:50 +08003516 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
3517 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04003518 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003519 btrfs_info(fs_info,
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003520 "failed to delete reference to %.*s, inode %llu parent %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003521 name_len, name, ino, dir_ino);
Jeff Mahoney66642832016-06-10 18:19:25 -04003522 btrfs_abort_transaction(trans, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04003523 goto err;
3524 }
Miao Xie67de1172013-12-26 13:07:06 +08003525skip_backref:
Lu Fengqi9add2942018-08-01 11:32:26 +08003526 ret = btrfs_delete_delayed_dir_index(trans, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003527 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003528 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003529 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003530 }
Chris Mason39279cc2007-06-12 06:35:45 -04003531
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003532 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len, inode,
3533 dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003534 if (ret != 0 && ret != -ENOENT) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003535 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003536 goto err;
3537 }
Chris Masone02119d2008-09-05 16:13:11 -04003538
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003539 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len, dir,
3540 index);
Chris Mason6418c962010-10-30 07:34:24 -04003541 if (ret == -ENOENT)
3542 ret = 0;
Zach Brownd4e39912013-04-02 21:02:16 +00003543 else if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04003544 btrfs_abort_transaction(trans, ret);
Josef Bacik63611e72019-06-18 10:59:18 -04003545
3546 /*
3547 * If we have a pending delayed iput we could end up with the final iput
3548 * being run in btrfs-cleaner context. If we have enough of these built
3549 * up we can end up burning a lot of time in btrfs-cleaner without any
3550 * way to throttle the unlinks. Since we're currently holding a ref on
3551 * the inode we can run the delayed iput here without any issues as the
3552 * final iput won't be done until after we drop the ref we're currently
3553 * holding.
3554 */
3555 btrfs_run_delayed_iput(fs_info, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003556err:
3557 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003558 if (ret)
3559 goto out;
3560
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02003561 btrfs_i_size_write(dir, dir->vfs_inode.i_size - name_len * 2);
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003562 inode_inc_iversion(&inode->vfs_inode);
3563 inode_inc_iversion(&dir->vfs_inode);
3564 inode->vfs_inode.i_ctime = dir->vfs_inode.i_mtime =
3565 dir->vfs_inode.i_ctime = current_time(&inode->vfs_inode);
3566 ret = btrfs_update_inode(trans, root, &dir->vfs_inode);
Chris Masone02119d2008-09-05 16:13:11 -04003567out:
Chris Mason39279cc2007-06-12 06:35:45 -04003568 return ret;
3569}
3570
Al Viro92986792011-03-04 17:14:37 +00003571int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3572 struct btrfs_root *root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003573 struct btrfs_inode *dir, struct btrfs_inode *inode,
Al Viro92986792011-03-04 17:14:37 +00003574 const char *name, int name_len)
3575{
3576 int ret;
3577 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
3578 if (!ret) {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003579 drop_nlink(&inode->vfs_inode);
3580 ret = btrfs_update_inode(trans, root, &inode->vfs_inode);
Al Viro92986792011-03-04 17:14:37 +00003581 }
3582 return ret;
3583}
Yan, Zhenga22285a2010-05-16 10:48:46 -04003584
3585/*
3586 * helper to start transaction for unlink and rmdir.
3587 *
Josef Bacikd52be812013-05-29 14:54:47 -04003588 * unlink and rmdir are special in btrfs, they do not always free space, so
3589 * if we cannot make our reservations the normal way try and see if there is
3590 * plenty of slack room in the global reserve to migrate, otherwise we cannot
3591 * allow the unlink to occur.
Yan, Zhenga22285a2010-05-16 10:48:46 -04003592 */
Josef Bacikd52be812013-05-29 14:54:47 -04003593static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir)
Yan, Zhenga22285a2010-05-16 10:48:46 -04003594{
Yan, Zhenga22285a2010-05-16 10:48:46 -04003595 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003596
Josef Bacike70bea52011-10-11 14:18:24 -04003597 /*
3598 * 1 for the possible orphan item
3599 * 1 for the dir item
3600 * 1 for the dir index
3601 * 1 for the inode ref
Josef Bacike70bea52011-10-11 14:18:24 -04003602 * 1 for the inode
3603 */
Filipe Manana8eab77f2015-11-13 23:57:16 +00003604 return btrfs_start_transaction_fallback_global_rsv(root, 5, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003605}
3606
Chris Mason39279cc2007-06-12 06:35:45 -04003607static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
3608{
Yan, Zhenga22285a2010-05-16 10:48:46 -04003609 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003610 struct btrfs_trans_handle *trans;
David Howells2b0143b2015-03-17 22:25:59 +00003611 struct inode *inode = d_inode(dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04003612 int ret;
3613
Josef Bacikd52be812013-05-29 14:54:47 -04003614 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003615 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003616 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04003617
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003618 btrfs_record_unlink_dir(trans, BTRFS_I(dir), BTRFS_I(d_inode(dentry)),
3619 0);
Chris Mason12fcfd22009-03-24 10:24:20 -04003620
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003621 ret = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
3622 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
3623 dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003624 if (ret)
3625 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003626
Yan, Zhenga22285a2010-05-16 10:48:46 -04003627 if (inode->i_nlink == 0) {
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003628 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Tsutomu Itohb5324022011-07-19 07:27:20 +00003629 if (ret)
3630 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003631 }
Josef Bacik7b128762008-07-24 12:17:14 -04003632
Tsutomu Itohb5324022011-07-19 07:27:20 +00003633out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003634 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003635 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04003636 return ret;
3637}
3638
Misono Tomohirof60a2362018-04-18 11:34:52 +09003639static int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
Josef Bacik045d3962019-12-18 17:20:27 -05003640 struct inode *dir, struct dentry *dentry)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003641{
Lu Fengqi401b3b12018-08-01 11:32:30 +08003642 struct btrfs_root *root = BTRFS_I(dir)->root;
Josef Bacik045d3962019-12-18 17:20:27 -05003643 struct btrfs_inode *inode = BTRFS_I(d_inode(dentry));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003644 struct btrfs_path *path;
3645 struct extent_buffer *leaf;
3646 struct btrfs_dir_item *di;
3647 struct btrfs_key key;
Josef Bacik045d3962019-12-18 17:20:27 -05003648 const char *name = dentry->d_name.name;
3649 int name_len = dentry->d_name.len;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003650 u64 index;
3651 int ret;
Josef Bacik045d3962019-12-18 17:20:27 -05003652 u64 objectid;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003653 u64 dir_ino = btrfs_ino(BTRFS_I(dir));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003654
Josef Bacik045d3962019-12-18 17:20:27 -05003655 if (btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID) {
3656 objectid = inode->root->root_key.objectid;
3657 } else if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) {
3658 objectid = inode->location.objectid;
3659 } else {
3660 WARN_ON(1);
3661 return -EINVAL;
3662 }
3663
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003664 path = btrfs_alloc_path();
3665 if (!path)
3666 return -ENOMEM;
3667
Li Zefan33345d012011-04-20 10:31:50 +08003668 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003669 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003670 if (IS_ERR_OR_NULL(di)) {
Liu Bo3cf50682018-09-12 06:06:26 +08003671 ret = di ? PTR_ERR(di) : -ENOENT;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003672 goto out;
3673 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003674
3675 leaf = path->nodes[0];
3676 btrfs_dir_item_key_to_cpu(leaf, di, &key);
3677 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
3678 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003679 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003680 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003681 goto out;
3682 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003683 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003684
Josef Bacikd49d3282019-12-18 17:20:28 -05003685 /*
3686 * This is a placeholder inode for a subvolume we didn't have a
3687 * reference to at the time of the snapshot creation. In the meantime
3688 * we could have renamed the real subvol link into our snapshot, so
3689 * depending on btrfs_del_root_ref to return -ENOENT here is incorret.
3690 * Instead simply lookup the dir_index_item for this entry so we can
3691 * remove it. Otherwise we know we have a ref to the root and we can
3692 * call btrfs_del_root_ref, and it _shouldn't_ fail.
3693 */
3694 if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) {
Li Zefan33345d012011-04-20 10:31:50 +08003695 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003696 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003697 if (IS_ERR_OR_NULL(di)) {
3698 if (!di)
3699 ret = -ENOENT;
3700 else
3701 ret = PTR_ERR(di);
Jeff Mahoney66642832016-06-10 18:19:25 -04003702 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003703 goto out;
3704 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003705
3706 leaf = path->nodes[0];
3707 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003708 index = key.offset;
Josef Bacikd49d3282019-12-18 17:20:28 -05003709 btrfs_release_path(path);
3710 } else {
3711 ret = btrfs_del_root_ref(trans, objectid,
3712 root->root_key.objectid, dir_ino,
3713 &index, name, name_len);
3714 if (ret) {
3715 btrfs_abort_transaction(trans, ret);
3716 goto out;
3717 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003718 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003719
Lu Fengqi9add2942018-08-01 11:32:26 +08003720 ret = btrfs_delete_delayed_dir_index(trans, BTRFS_I(dir), index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003721 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003722 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003723 goto out;
3724 }
Miao Xie16cdcec2011-04-22 18:12:22 +08003725
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02003726 btrfs_i_size_write(BTRFS_I(dir), dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003727 inode_inc_iversion(dir);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07003728 dir->i_mtime = dir->i_ctime = current_time(dir);
Josef Bacik5a24e842012-08-08 10:12:59 -06003729 ret = btrfs_update_inode_fallback(trans, root, dir);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003730 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04003731 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003732out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04003733 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003734 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003735}
3736
Misono Tomohiroec42f162018-04-18 11:34:13 +09003737/*
3738 * Helper to check if the subvolume references other subvolumes or if it's
3739 * default.
3740 */
Misono Tomohirof60a2362018-04-18 11:34:52 +09003741static noinline int may_destroy_subvol(struct btrfs_root *root)
Misono Tomohiroec42f162018-04-18 11:34:13 +09003742{
3743 struct btrfs_fs_info *fs_info = root->fs_info;
3744 struct btrfs_path *path;
3745 struct btrfs_dir_item *di;
3746 struct btrfs_key key;
3747 u64 dir_id;
3748 int ret;
3749
3750 path = btrfs_alloc_path();
3751 if (!path)
3752 return -ENOMEM;
3753
3754 /* Make sure this root isn't set as the default subvol */
3755 dir_id = btrfs_super_root_dir(fs_info->super_copy);
3756 di = btrfs_lookup_dir_item(NULL, fs_info->tree_root, path,
3757 dir_id, "default", 7, 0);
3758 if (di && !IS_ERR(di)) {
3759 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key);
3760 if (key.objectid == root->root_key.objectid) {
3761 ret = -EPERM;
3762 btrfs_err(fs_info,
3763 "deleting default subvolume %llu is not allowed",
3764 key.objectid);
3765 goto out;
3766 }
3767 btrfs_release_path(path);
3768 }
3769
3770 key.objectid = root->root_key.objectid;
3771 key.type = BTRFS_ROOT_REF_KEY;
3772 key.offset = (u64)-1;
3773
3774 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
3775 if (ret < 0)
3776 goto out;
3777 BUG_ON(ret == 0);
3778
3779 ret = 0;
3780 if (path->slots[0] > 0) {
3781 path->slots[0]--;
3782 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
3783 if (key.objectid == root->root_key.objectid &&
3784 key.type == BTRFS_ROOT_REF_KEY)
3785 ret = -ENOTEMPTY;
3786 }
3787out:
3788 btrfs_free_path(path);
3789 return ret;
3790}
3791
Nikolay Borisov20a68002018-04-27 14:36:24 +03003792/* Delete all dentries for inodes belonging to the root */
3793static void btrfs_prune_dentries(struct btrfs_root *root)
3794{
3795 struct btrfs_fs_info *fs_info = root->fs_info;
3796 struct rb_node *node;
3797 struct rb_node *prev;
3798 struct btrfs_inode *entry;
3799 struct inode *inode;
3800 u64 objectid = 0;
3801
3802 if (!test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
3803 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
3804
3805 spin_lock(&root->inode_lock);
3806again:
3807 node = root->inode_tree.rb_node;
3808 prev = NULL;
3809 while (node) {
3810 prev = node;
3811 entry = rb_entry(node, struct btrfs_inode, rb_node);
3812
David Sterba37508512018-06-29 10:56:40 +02003813 if (objectid < btrfs_ino(entry))
Nikolay Borisov20a68002018-04-27 14:36:24 +03003814 node = node->rb_left;
David Sterba37508512018-06-29 10:56:40 +02003815 else if (objectid > btrfs_ino(entry))
Nikolay Borisov20a68002018-04-27 14:36:24 +03003816 node = node->rb_right;
3817 else
3818 break;
3819 }
3820 if (!node) {
3821 while (prev) {
3822 entry = rb_entry(prev, struct btrfs_inode, rb_node);
David Sterba37508512018-06-29 10:56:40 +02003823 if (objectid <= btrfs_ino(entry)) {
Nikolay Borisov20a68002018-04-27 14:36:24 +03003824 node = prev;
3825 break;
3826 }
3827 prev = rb_next(prev);
3828 }
3829 }
3830 while (node) {
3831 entry = rb_entry(node, struct btrfs_inode, rb_node);
David Sterba37508512018-06-29 10:56:40 +02003832 objectid = btrfs_ino(entry) + 1;
Nikolay Borisov20a68002018-04-27 14:36:24 +03003833 inode = igrab(&entry->vfs_inode);
3834 if (inode) {
3835 spin_unlock(&root->inode_lock);
3836 if (atomic_read(&inode->i_count) > 1)
3837 d_prune_aliases(inode);
3838 /*
3839 * btrfs_drop_inode will have it removed from the inode
3840 * cache when its usage count hits zero.
3841 */
3842 iput(inode);
3843 cond_resched();
3844 spin_lock(&root->inode_lock);
3845 goto again;
3846 }
3847
3848 if (cond_resched_lock(&root->inode_lock))
3849 goto again;
3850
3851 node = rb_next(node);
3852 }
3853 spin_unlock(&root->inode_lock);
3854}
3855
Misono Tomohirof60a2362018-04-18 11:34:52 +09003856int btrfs_delete_subvolume(struct inode *dir, struct dentry *dentry)
3857{
3858 struct btrfs_fs_info *fs_info = btrfs_sb(dentry->d_sb);
3859 struct btrfs_root *root = BTRFS_I(dir)->root;
3860 struct inode *inode = d_inode(dentry);
3861 struct btrfs_root *dest = BTRFS_I(inode)->root;
3862 struct btrfs_trans_handle *trans;
3863 struct btrfs_block_rsv block_rsv;
3864 u64 root_flags;
Misono Tomohirof60a2362018-04-18 11:34:52 +09003865 int ret;
3866 int err;
3867
3868 /*
3869 * Don't allow to delete a subvolume with send in progress. This is
3870 * inside the inode lock so the error handling that has to drop the bit
3871 * again is not run concurrently.
3872 */
3873 spin_lock(&dest->root_item_lock);
Lu Fengqia7176f72018-08-04 21:10:53 +08003874 if (dest->send_in_progress) {
Misono Tomohirof60a2362018-04-18 11:34:52 +09003875 spin_unlock(&dest->root_item_lock);
3876 btrfs_warn(fs_info,
3877 "attempt to delete subvolume %llu during send",
3878 dest->root_key.objectid);
3879 return -EPERM;
3880 }
Lu Fengqia7176f72018-08-04 21:10:53 +08003881 root_flags = btrfs_root_flags(&dest->root_item);
3882 btrfs_set_root_flags(&dest->root_item,
3883 root_flags | BTRFS_ROOT_SUBVOL_DEAD);
3884 spin_unlock(&dest->root_item_lock);
Misono Tomohirof60a2362018-04-18 11:34:52 +09003885
3886 down_write(&fs_info->subvol_sem);
3887
3888 err = may_destroy_subvol(dest);
3889 if (err)
3890 goto out_up_write;
3891
3892 btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP);
3893 /*
3894 * One for dir inode,
3895 * two for dir entries,
3896 * two for root ref/backref.
3897 */
Gu JinXiangc4c129d2018-05-30 11:00:38 +08003898 err = btrfs_subvolume_reserve_metadata(root, &block_rsv, 5, true);
Misono Tomohirof60a2362018-04-18 11:34:52 +09003899 if (err)
3900 goto out_up_write;
3901
3902 trans = btrfs_start_transaction(root, 0);
3903 if (IS_ERR(trans)) {
3904 err = PTR_ERR(trans);
3905 goto out_release;
3906 }
3907 trans->block_rsv = &block_rsv;
3908 trans->bytes_reserved = block_rsv.size;
3909
3910 btrfs_record_snapshot_destroy(trans, BTRFS_I(dir));
3911
Josef Bacik045d3962019-12-18 17:20:27 -05003912 ret = btrfs_unlink_subvol(trans, dir, dentry);
Misono Tomohirof60a2362018-04-18 11:34:52 +09003913 if (ret) {
3914 err = ret;
3915 btrfs_abort_transaction(trans, ret);
3916 goto out_end_trans;
3917 }
3918
3919 btrfs_record_root_in_trans(trans, dest);
3920
3921 memset(&dest->root_item.drop_progress, 0,
3922 sizeof(dest->root_item.drop_progress));
3923 dest->root_item.drop_level = 0;
3924 btrfs_set_root_refs(&dest->root_item, 0);
3925
3926 if (!test_and_set_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &dest->state)) {
3927 ret = btrfs_insert_orphan_item(trans,
3928 fs_info->tree_root,
3929 dest->root_key.objectid);
3930 if (ret) {
3931 btrfs_abort_transaction(trans, ret);
3932 err = ret;
3933 goto out_end_trans;
3934 }
3935 }
3936
Lu Fengqid1957792018-05-29 15:01:54 +08003937 ret = btrfs_uuid_tree_remove(trans, dest->root_item.uuid,
Misono Tomohirof60a2362018-04-18 11:34:52 +09003938 BTRFS_UUID_KEY_SUBVOL,
3939 dest->root_key.objectid);
3940 if (ret && ret != -ENOENT) {
3941 btrfs_abort_transaction(trans, ret);
3942 err = ret;
3943 goto out_end_trans;
3944 }
3945 if (!btrfs_is_empty_uuid(dest->root_item.received_uuid)) {
Lu Fengqid1957792018-05-29 15:01:54 +08003946 ret = btrfs_uuid_tree_remove(trans,
Misono Tomohirof60a2362018-04-18 11:34:52 +09003947 dest->root_item.received_uuid,
3948 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
3949 dest->root_key.objectid);
3950 if (ret && ret != -ENOENT) {
3951 btrfs_abort_transaction(trans, ret);
3952 err = ret;
3953 goto out_end_trans;
3954 }
3955 }
3956
3957out_end_trans:
3958 trans->block_rsv = NULL;
3959 trans->bytes_reserved = 0;
3960 ret = btrfs_end_transaction(trans);
3961 if (ret && !err)
3962 err = ret;
3963 inode->i_flags |= S_DEAD;
3964out_release:
3965 btrfs_subvolume_release_metadata(fs_info, &block_rsv);
3966out_up_write:
3967 up_write(&fs_info->subvol_sem);
3968 if (err) {
3969 spin_lock(&dest->root_item_lock);
3970 root_flags = btrfs_root_flags(&dest->root_item);
3971 btrfs_set_root_flags(&dest->root_item,
3972 root_flags & ~BTRFS_ROOT_SUBVOL_DEAD);
3973 spin_unlock(&dest->root_item_lock);
3974 } else {
3975 d_invalidate(dentry);
Nikolay Borisov20a68002018-04-27 14:36:24 +03003976 btrfs_prune_dentries(dest);
Misono Tomohirof60a2362018-04-18 11:34:52 +09003977 ASSERT(dest->send_in_progress == 0);
3978
3979 /* the last ref */
3980 if (dest->ino_cache_inode) {
3981 iput(dest->ino_cache_inode);
3982 dest->ino_cache_inode = NULL;
3983 }
3984 }
3985
3986 return err;
3987}
3988
Chris Mason39279cc2007-06-12 06:35:45 -04003989static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
3990{
David Howells2b0143b2015-03-17 22:25:59 +00003991 struct inode *inode = d_inode(dentry);
Chris Mason1832a6d2007-12-21 16:27:21 -05003992 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003993 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003994 struct btrfs_trans_handle *trans;
Filipe Manana44f714d2016-06-06 16:11:13 +01003995 u64 last_unlink_trans;
Chris Mason39279cc2007-06-12 06:35:45 -04003996
David Sterbab3ae2442012-09-13 16:04:34 -06003997 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Yan134d4512007-10-25 15:49:25 -04003998 return -ENOTEMPTY;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003999 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_FIRST_FREE_OBJECTID)
Misono Tomohiroa79a4642018-04-18 11:35:31 +09004000 return btrfs_delete_subvolume(dir, dentry);
Yan134d4512007-10-25 15:49:25 -04004001
Josef Bacikd52be812013-05-29 14:54:47 -04004002 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004003 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004004 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004005
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004006 if (unlikely(btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Josef Bacik045d3962019-12-18 17:20:27 -05004007 err = btrfs_unlink_subvol(trans, dir, dentry);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004008 goto out;
4009 }
4010
Nikolay Borisov73f2e542017-02-20 13:50:59 +02004011 err = btrfs_orphan_add(trans, BTRFS_I(inode));
Josef Bacik7b128762008-07-24 12:17:14 -04004012 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004013 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004014
Filipe Manana44f714d2016-06-06 16:11:13 +01004015 last_unlink_trans = BTRFS_I(inode)->last_unlink_trans;
4016
Chris Mason39279cc2007-06-12 06:35:45 -04004017 /* now the directory is empty */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004018 err = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
4019 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
4020 dentry->d_name.len);
Filipe Manana44f714d2016-06-06 16:11:13 +01004021 if (!err) {
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004022 btrfs_i_size_write(BTRFS_I(inode), 0);
Filipe Manana44f714d2016-06-06 16:11:13 +01004023 /*
4024 * Propagate the last_unlink_trans value of the deleted dir to
4025 * its parent directory. This is to prevent an unrecoverable
4026 * log tree in the case we do something like this:
4027 * 1) create dir foo
4028 * 2) create snapshot under dir foo
4029 * 3) delete the snapshot
4030 * 4) rmdir foo
4031 * 5) mkdir foo
4032 * 6) fsync foo or some file inside foo
4033 */
4034 if (last_unlink_trans >= trans->transid)
4035 BTRFS_I(dir)->last_unlink_trans = last_unlink_trans;
4036 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004037out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004038 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004039 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39544012007-12-12 14:38:19 -05004040
Chris Mason39279cc2007-06-12 06:35:45 -04004041 return err;
4042}
4043
Josef Bacikddfae632017-10-19 14:16:02 -04004044/*
4045 * Return this if we need to call truncate_block for the last bit of the
4046 * truncate.
4047 */
4048#define NEED_TRUNCATE_BLOCK 1
Filipe Manana0305cd52015-10-16 12:34:25 +01004049
Chris Mason323ac952008-10-01 19:05:46 -04004050/*
Chris Mason39279cc2007-06-12 06:35:45 -04004051 * this can truncate away extent items, csum items and directory items.
4052 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04004053 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04004054 *
4055 * csum items that cross the new i_size are truncated to the new size
4056 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04004057 *
4058 * min_type is the minimum key type to truncate down to. If set to 0, this
4059 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04004060 */
Yan, Zheng80825102009-11-12 09:35:36 +00004061int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
4062 struct btrfs_root *root,
4063 struct inode *inode,
4064 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004065{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004066 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04004067 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004068 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004069 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00004070 struct btrfs_key key;
4071 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04004072 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04004073 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004074 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004075 u64 item_end = 0;
Filipe Mananac1aa4572015-06-20 18:20:09 +01004076 u64 last_size = new_size;
Yan, Zheng80825102009-11-12 09:35:36 +00004077 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04004078 int found_extent;
4079 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05004080 int pending_del_nr = 0;
4081 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04004082 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00004083 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004084 u64 ino = btrfs_ino(BTRFS_I(inode));
Chris Mason28ed1342014-12-17 09:41:04 -08004085 u64 bytes_deleted = 0;
Thomas Meyer897ca812017-10-07 16:02:21 +02004086 bool be_nice = false;
4087 bool should_throttle = false;
Filipe Manana28553fa2020-02-07 12:23:09 +00004088 const u64 lock_start = ALIGN_DOWN(new_size, fs_info->sectorsize);
4089 struct extent_state *cached_state = NULL;
Yan, Zheng80825102009-11-12 09:35:36 +00004090
4091 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04004092
Chris Mason28ed1342014-12-17 09:41:04 -08004093 /*
4094 * for non-free space inodes and ref cows, we want to back off from
4095 * time to time
4096 */
Nikolay Borisov70ddc552017-02-20 13:50:35 +02004097 if (!btrfs_is_free_space_inode(BTRFS_I(inode)) &&
Chris Mason28ed1342014-12-17 09:41:04 -08004098 test_bit(BTRFS_ROOT_REF_COWS, &root->state))
Thomas Meyer897ca812017-10-07 16:02:21 +02004099 be_nice = true;
Chris Mason28ed1342014-12-17 09:41:04 -08004100
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004101 path = btrfs_alloc_path();
4102 if (!path)
4103 return -ENOMEM;
David Sterbae4058b52015-11-27 16:31:35 +01004104 path->reada = READA_BACK;
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004105
Filipe Manana28553fa2020-02-07 12:23:09 +00004106 lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, (u64)-1,
4107 &cached_state);
4108
Josef Bacik5dc562c2012-08-17 13:14:17 -04004109 /*
4110 * We want to drop from the next block forward in case this new size is
4111 * not block aligned since we will be keeping the last block of the
4112 * extent just the way it is.
4113 */
Miao Xie27cdeb72014-04-02 19:51:05 +08004114 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004115 root == fs_info->tree_root)
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02004116 btrfs_drop_extent_cache(BTRFS_I(inode), ALIGN(new_size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004117 fs_info->sectorsize),
Jeff Mahoneyda170662016-06-15 09:22:56 -04004118 (u64)-1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00004119
Miao Xie16cdcec2011-04-22 18:12:22 +08004120 /*
4121 * This function is also used to drop the items in the log tree before
4122 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
Andrea Gelmini52042d82018-11-28 12:05:13 +01004123 * it is used to drop the logged items. So we shouldn't kill the delayed
Miao Xie16cdcec2011-04-22 18:12:22 +08004124 * items.
4125 */
4126 if (min_type == 0 && root == BTRFS_I(inode)->root)
Nikolay Borisov4ccb5c72017-01-10 20:35:38 +02004127 btrfs_kill_delayed_inode_items(BTRFS_I(inode));
Miao Xie16cdcec2011-04-22 18:12:22 +08004128
Li Zefan33345d012011-04-20 10:31:50 +08004129 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04004130 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04004131 key.type = (u8)-1;
4132
Chris Mason85e21ba2008-01-29 15:11:36 -05004133search_again:
Chris Mason28ed1342014-12-17 09:41:04 -08004134 /*
4135 * with a 16K leaf size and 128MB extents, you can actually queue
4136 * up a huge file in a single leaf. Most of the time that
4137 * bytes_deleted is > 0, it will be huge by the time we get here
4138 */
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004139 if (be_nice && bytes_deleted > SZ_32M &&
4140 btrfs_should_end_transaction(trans)) {
4141 ret = -EAGAIN;
Yan, Zheng80825102009-11-12 09:35:36 +00004142 goto out;
4143 }
Chris Masond3977122009-01-05 21:25:51 -05004144
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004145 path->leave_spinning = 1;
4146 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
4147 if (ret < 0)
4148 goto out;
4149
Chris Mason85e21ba2008-01-29 15:11:36 -05004150 if (ret > 0) {
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004151 ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -04004152 /* there are no items in the tree for us to truncate, we're
4153 * done
4154 */
Yan, Zheng80825102009-11-12 09:35:36 +00004155 if (path->slots[0] == 0)
4156 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05004157 path->slots[0]--;
4158 }
4159
Chris Masond3977122009-01-05 21:25:51 -05004160 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04004161 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04004162 leaf = path->nodes[0];
4163 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02004164 found_type = found_key.type;
Chris Mason39279cc2007-06-12 06:35:45 -04004165
Li Zefan33345d012011-04-20 10:31:50 +08004166 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04004167 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004168
Chris Mason85e21ba2008-01-29 15:11:36 -05004169 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004170 break;
4171
Chris Mason5f39d392007-10-15 16:14:19 -04004172 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04004173 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04004174 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04004175 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04004176 extent_type = btrfs_file_extent_type(leaf, fi);
4177 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04004178 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04004179 btrfs_file_extent_num_bytes(leaf, fi);
Liu Bo09ed2f12017-03-10 11:09:48 -08004180
4181 trace_btrfs_truncate_show_fi_regular(
4182 BTRFS_I(inode), leaf, fi,
4183 found_key.offset);
Chris Mason179e29e2007-11-01 11:28:41 -04004184 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Qu Wenruoe41ca582018-06-06 15:41:49 +08004185 item_end += btrfs_file_extent_ram_bytes(leaf,
4186 fi);
Liu Bo09ed2f12017-03-10 11:09:48 -08004187
4188 trace_btrfs_truncate_show_fi_inline(
4189 BTRFS_I(inode), leaf, fi, path->slots[0],
4190 found_key.offset);
Chris Mason39279cc2007-06-12 06:35:45 -04004191 }
Yan008630c2007-11-07 13:31:09 -05004192 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04004193 }
Yan, Zheng80825102009-11-12 09:35:36 +00004194 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04004195 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00004196 } else {
Filipe Manana76b42ab2017-02-14 16:56:01 +00004197 if (item_end < new_size)
Yan, Zheng80825102009-11-12 09:35:36 +00004198 break;
4199 if (found_key.offset >= new_size)
4200 del_item = 1;
4201 else
4202 del_item = 0;
4203 }
Chris Mason39279cc2007-06-12 06:35:45 -04004204 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004205 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04004206 if (found_type != BTRFS_EXTENT_DATA_KEY)
4207 goto delete;
4208
4209 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04004210 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04004211 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05004212 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04004213 u64 orig_num_bytes =
4214 btrfs_file_extent_num_bytes(leaf, fi);
Qu Wenruofda28322013-02-26 08:10:22 +00004215 extent_num_bytes = ALIGN(new_size -
4216 found_key.offset,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004217 fs_info->sectorsize);
Chris Masondb945352007-10-15 16:15:53 -04004218 btrfs_set_file_extent_num_bytes(leaf, fi,
4219 extent_num_bytes);
4220 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05004221 extent_num_bytes);
Miao Xie27cdeb72014-04-02 19:51:05 +08004222 if (test_bit(BTRFS_ROOT_REF_COWS,
4223 &root->state) &&
4224 extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004225 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04004226 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004227 } else {
Chris Masondb945352007-10-15 16:15:53 -04004228 extent_num_bytes =
4229 btrfs_file_extent_disk_num_bytes(leaf,
4230 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004231 extent_offset = found_key.offset -
4232 btrfs_file_extent_offset(leaf, fi);
4233
Chris Mason39279cc2007-06-12 06:35:45 -04004234 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05004235 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004236 if (extent_start != 0) {
4237 found_extent = 1;
Miao Xie27cdeb72014-04-02 19:51:05 +08004238 if (test_bit(BTRFS_ROOT_REF_COWS,
4239 &root->state))
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004240 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04004241 }
4242 }
Chris Mason90692182008-02-08 13:49:28 -05004243 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04004244 /*
4245 * we can't truncate inline items that have had
4246 * special encodings
4247 */
4248 if (!del_item &&
Chris Masonc8b97812008-10-29 14:49:59 -04004249 btrfs_file_extent_encryption(leaf, fi) == 0 &&
Josef Bacikddfae632017-10-19 14:16:02 -04004250 btrfs_file_extent_other_encoding(leaf, fi) == 0 &&
4251 btrfs_file_extent_compression(leaf, fi) == 0) {
4252 u32 size = (u32)(new_size - found_key.offset);
Chris Mason514ac8a2014-01-03 21:07:00 -08004253
Josef Bacikddfae632017-10-19 14:16:02 -04004254 btrfs_set_file_extent_ram_bytes(leaf, fi, size);
4255 size = btrfs_file_extent_calc_inline_size(size);
David Sterba78ac4f92019-03-20 14:49:12 +01004256 btrfs_truncate_item(path, size, 1);
Josef Bacikddfae632017-10-19 14:16:02 -04004257 } else if (!del_item) {
Chris Mason514ac8a2014-01-03 21:07:00 -08004258 /*
Josef Bacikddfae632017-10-19 14:16:02 -04004259 * We have to bail so the last_size is set to
4260 * just before this extent.
Chris Mason514ac8a2014-01-03 21:07:00 -08004261 */
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004262 ret = NEED_TRUNCATE_BLOCK;
Josef Bacikddfae632017-10-19 14:16:02 -04004263 break;
Chris Mason90692182008-02-08 13:49:28 -05004264 }
Josef Bacikddfae632017-10-19 14:16:02 -04004265
4266 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state))
4267 inode_sub_bytes(inode, item_end + 1 - new_size);
Chris Mason39279cc2007-06-12 06:35:45 -04004268 }
Chris Mason179e29e2007-11-01 11:28:41 -04004269delete:
Josef Bacikddfae632017-10-19 14:16:02 -04004270 if (del_item)
4271 last_size = found_key.offset;
4272 else
4273 last_size = new_size;
Chris Mason39279cc2007-06-12 06:35:45 -04004274 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05004275 if (!pending_del_nr) {
4276 /* no pending yet, add ourselves */
4277 pending_del_slot = path->slots[0];
4278 pending_del_nr = 1;
4279 } else if (pending_del_nr &&
4280 path->slots[0] + 1 == pending_del_slot) {
4281 /* hop on the pending chunk */
4282 pending_del_nr++;
4283 pending_del_slot = path->slots[0];
4284 } else {
Chris Masond3977122009-01-05 21:25:51 -05004285 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05004286 }
Chris Mason39279cc2007-06-12 06:35:45 -04004287 } else {
4288 break;
4289 }
Thomas Meyer897ca812017-10-07 16:02:21 +02004290 should_throttle = false;
Chris Mason28f75a02015-02-04 06:59:29 -08004291
Miao Xie27cdeb72014-04-02 19:51:05 +08004292 if (found_extent &&
4293 (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004294 root == fs_info->tree_root)) {
Qu Wenruoffd4bb22019-04-04 14:45:36 +08004295 struct btrfs_ref ref = { 0 };
4296
Chris Masonb9473432009-03-13 11:00:37 -04004297 btrfs_set_path_blocking(path);
Chris Mason28ed1342014-12-17 09:41:04 -08004298 bytes_deleted += extent_num_bytes;
Qu Wenruoffd4bb22019-04-04 14:45:36 +08004299
4300 btrfs_init_generic_ref(&ref, BTRFS_DROP_DELAYED_REF,
4301 extent_start, extent_num_bytes, 0);
4302 ref.real_root = root->root_key.objectid;
4303 btrfs_init_data_ref(&ref, btrfs_header_owner(leaf),
4304 ino, extent_offset);
4305 ret = btrfs_free_extent(trans, &ref);
Omar Sandoval05522102018-05-11 13:13:31 -07004306 if (ret) {
4307 btrfs_abort_transaction(trans, ret);
4308 break;
4309 }
Chris Mason28f75a02015-02-04 06:59:29 -08004310 if (be_nice) {
Lu Fengqi7c861622018-10-11 13:40:36 +08004311 if (btrfs_should_throttle_delayed_refs(trans))
Thomas Meyer897ca812017-10-07 16:02:21 +02004312 should_throttle = true;
Chris Mason28f75a02015-02-04 06:59:29 -08004313 }
Chris Mason39279cc2007-06-12 06:35:45 -04004314 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004315
Yan, Zheng80825102009-11-12 09:35:36 +00004316 if (found_type == BTRFS_INODE_ITEM_KEY)
4317 break;
4318
4319 if (path->slots[0] == 0 ||
Josef Bacik12621332015-02-03 07:50:16 -08004320 path->slots[0] != pending_del_slot ||
Josef Bacik28bad212018-12-03 10:20:38 -05004321 should_throttle) {
Yan, Zheng80825102009-11-12 09:35:36 +00004322 if (pending_del_nr) {
4323 ret = btrfs_del_items(trans, root, path,
4324 pending_del_slot,
4325 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004326 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004327 btrfs_abort_transaction(trans, ret);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004328 break;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004329 }
Yan, Zheng80825102009-11-12 09:35:36 +00004330 pending_del_nr = 0;
4331 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004332 btrfs_release_path(path);
Josef Bacik28bad212018-12-03 10:20:38 -05004333
Chris Mason28f75a02015-02-04 06:59:29 -08004334 /*
Josef Bacik28bad212018-12-03 10:20:38 -05004335 * We can generate a lot of delayed refs, so we need to
4336 * throttle every once and a while and make sure we're
4337 * adding enough space to keep up with the work we are
4338 * generating. Since we hold a transaction here we
4339 * can't flush, and we don't want to FLUSH_LIMIT because
4340 * we could have generated too many delayed refs to
4341 * actually allocate, so just bail if we're short and
4342 * let the normal reservation dance happen higher up.
Chris Mason28f75a02015-02-04 06:59:29 -08004343 */
Josef Bacik28bad212018-12-03 10:20:38 -05004344 if (should_throttle) {
4345 ret = btrfs_delayed_refs_rsv_refill(fs_info,
4346 BTRFS_RESERVE_NO_FLUSH);
4347 if (ret) {
4348 ret = -EAGAIN;
4349 break;
4350 }
Chris Mason28f75a02015-02-04 06:59:29 -08004351 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004352 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00004353 } else {
4354 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05004355 }
Chris Mason39279cc2007-06-12 06:35:45 -04004356 }
Yan, Zheng80825102009-11-12 09:35:36 +00004357out:
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004358 if (ret >= 0 && pending_del_nr) {
4359 int err;
4360
4361 err = btrfs_del_items(trans, root, path, pending_del_slot,
Chris Mason85e21ba2008-01-29 15:11:36 -05004362 pending_del_nr);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004363 if (err) {
4364 btrfs_abort_transaction(trans, err);
4365 ret = err;
4366 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004367 }
Filipe Manana76b42ab2017-02-14 16:56:01 +00004368 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
4369 ASSERT(last_size >= new_size);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004370 if (!ret && last_size > new_size)
Filipe Manana76b42ab2017-02-14 16:56:01 +00004371 last_size = new_size;
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004372 btrfs_ordered_update_i_size(inode, last_size, NULL);
Filipe Manana76b42ab2017-02-14 16:56:01 +00004373 }
Chris Mason28ed1342014-12-17 09:41:04 -08004374
Filipe Manana28553fa2020-02-07 12:23:09 +00004375 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, (u64)-1,
4376 &cached_state);
4377
Chris Mason39279cc2007-06-12 06:35:45 -04004378 btrfs_free_path(path);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004379 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04004380}
4381
Chris Masona52d9a82007-08-27 16:49:44 -04004382/*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304383 * btrfs_truncate_block - read, zero a chunk and write a block
Josef Bacik2aaa6652012-08-29 14:27:18 -04004384 * @inode - inode that we're zeroing
4385 * @from - the offset to start zeroing
4386 * @len - the length to zero, 0 to zero the entire range respective to the
4387 * offset
4388 * @front - zero up to the offset instead of from the offset on
4389 *
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304390 * This will find the block for the "from" offset and cow the block and zero the
Josef Bacik2aaa6652012-08-29 14:27:18 -04004391 * part we want to zero. This is used with truncate and hole punching.
Chris Masona52d9a82007-08-27 16:49:44 -04004392 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304393int btrfs_truncate_block(struct inode *inode, loff_t from, loff_t len,
Josef Bacik2aaa6652012-08-29 14:27:18 -04004394 int front)
Chris Masona52d9a82007-08-27 16:49:44 -04004395{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004396 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004397 struct address_space *mapping = inode->i_mapping;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004398 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4399 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004400 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08004401 struct extent_changeset *data_reserved = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004402 char *kaddr;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004403 u32 blocksize = fs_info->sectorsize;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004404 pgoff_t index = from >> PAGE_SHIFT;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304405 unsigned offset = from & (blocksize - 1);
Chris Masona52d9a82007-08-27 16:49:44 -04004406 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004407 gfp_t mask = btrfs_alloc_write_mask(mapping);
Chris Masona52d9a82007-08-27 16:49:44 -04004408 int ret = 0;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304409 u64 block_start;
4410 u64 block_end;
Chris Masona52d9a82007-08-27 16:49:44 -04004411
Nikolay Borisovb03ebd92018-01-18 14:47:06 +02004412 if (IS_ALIGNED(offset, blocksize) &&
4413 (!len || IS_ALIGNED(len, blocksize)))
Chris Masona52d9a82007-08-27 16:49:44 -04004414 goto out;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304415
Josef Bacik8b62f872017-10-19 14:15:55 -04004416 block_start = round_down(from, blocksize);
4417 block_end = block_start + blocksize - 1;
4418
Qu Wenruo364ecf32017-02-27 15:10:38 +08004419 ret = btrfs_delalloc_reserve_space(inode, &data_reserved,
Josef Bacik8b62f872017-10-19 14:15:55 -04004420 block_start, blocksize);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004421 if (ret)
4422 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004423
Chris Mason211c17f2008-05-15 09:13:45 -04004424again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004425 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004426 if (!page) {
Qu Wenruobc42bda2017-02-27 15:10:39 +08004427 btrfs_delalloc_release_space(inode, data_reserved,
Qu Wenruo43b18592017-12-12 15:34:32 +08004428 block_start, blocksize, true);
Qu Wenruo8702ba92019-10-14 14:34:51 +08004429 btrfs_delalloc_release_extents(BTRFS_I(inode), blocksize);
Miao Xieac6a2b32012-12-05 10:56:13 +00004430 ret = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -04004431 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04004432 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004433
Chris Masona52d9a82007-08-27 16:49:44 -04004434 if (!PageUptodate(page)) {
4435 ret = btrfs_readpage(NULL, page);
4436 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004437 if (page->mapping != mapping) {
4438 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004439 put_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004440 goto again;
4441 }
Chris Masona52d9a82007-08-27 16:49:44 -04004442 if (!PageUptodate(page)) {
4443 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04004444 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04004445 }
4446 }
Chris Mason211c17f2008-05-15 09:13:45 -04004447 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004448
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304449 lock_extent_bits(io_tree, block_start, block_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004450 set_page_extent_mapped(page);
4451
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304452 ordered = btrfs_lookup_ordered_extent(inode, block_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004453 if (ordered) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304454 unlock_extent_cached(io_tree, block_start, block_end,
David Sterbae43bbe52017-12-12 21:43:52 +01004455 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004456 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004457 put_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04004458 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004459 btrfs_put_ordered_extent(ordered);
4460 goto again;
4461 }
4462
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304463 clear_extent_bit(&BTRFS_I(inode)->io_tree, block_start, block_end,
Omar Sandovale1821632019-08-15 14:04:04 -07004464 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
4465 0, 0, &cached_state);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004466
Filipe Mananae3b8a482017-11-04 00:16:59 +00004467 ret = btrfs_set_extent_delalloc(inode, block_start, block_end, 0,
Nikolay Borisov330a5822019-07-17 16:18:17 +03004468 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004469 if (ret) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304470 unlock_extent_cached(io_tree, block_start, block_end,
David Sterbae43bbe52017-12-12 21:43:52 +01004471 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004472 goto out_unlock;
4473 }
4474
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304475 if (offset != blocksize) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04004476 if (!len)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304477 len = blocksize - offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004478 kaddr = kmap(page);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004479 if (front)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304480 memset(kaddr + (block_start - page_offset(page)),
4481 0, offset);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004482 else
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304483 memset(kaddr + (block_start - page_offset(page)) + offset,
4484 0, len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004485 flush_dcache_page(page);
4486 kunmap(page);
4487 }
Chris Mason247e7432008-07-17 12:53:51 -04004488 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004489 set_page_dirty(page);
David Sterbae43bbe52017-12-12 21:43:52 +01004490 unlock_extent_cached(io_tree, block_start, block_end, &cached_state);
Chris Mason39279cc2007-06-12 06:35:45 -04004491
Chris Mason89642222008-07-24 09:41:53 -04004492out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04004493 if (ret)
Qu Wenruobc42bda2017-02-27 15:10:39 +08004494 btrfs_delalloc_release_space(inode, data_reserved, block_start,
Qu Wenruo43b18592017-12-12 15:34:32 +08004495 blocksize, true);
Qu Wenruo8702ba92019-10-14 14:34:51 +08004496 btrfs_delalloc_release_extents(BTRFS_I(inode), blocksize);
Chris Mason39279cc2007-06-12 06:35:45 -04004497 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004498 put_page(page);
Chris Mason39279cc2007-06-12 06:35:45 -04004499out:
Qu Wenruo364ecf32017-02-27 15:10:38 +08004500 extent_changeset_free(data_reserved);
Chris Mason39279cc2007-06-12 06:35:45 -04004501 return ret;
4502}
4503
Josef Bacik16e75492013-10-22 12:18:51 -04004504static int maybe_insert_hole(struct btrfs_root *root, struct inode *inode,
4505 u64 offset, u64 len)
4506{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004507 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik16e75492013-10-22 12:18:51 -04004508 struct btrfs_trans_handle *trans;
4509 int ret;
4510
4511 /*
4512 * Still need to make sure the inode looks like it's been updated so
4513 * that any holes get logged if we fsync.
4514 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004515 if (btrfs_fs_incompat(fs_info, NO_HOLES)) {
4516 BTRFS_I(inode)->last_trans = fs_info->generation;
Josef Bacik16e75492013-10-22 12:18:51 -04004517 BTRFS_I(inode)->last_sub_trans = root->log_transid;
4518 BTRFS_I(inode)->last_log_commit = root->last_log_commit;
4519 return 0;
4520 }
4521
4522 /*
4523 * 1 - for the one we're dropping
4524 * 1 - for the one we're adding
4525 * 1 - for updating the inode.
4526 */
4527 trans = btrfs_start_transaction(root, 3);
4528 if (IS_ERR(trans))
4529 return PTR_ERR(trans);
4530
4531 ret = btrfs_drop_extents(trans, root, inode, offset, offset + len, 1);
4532 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004533 btrfs_abort_transaction(trans, ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004534 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04004535 return ret;
4536 }
4537
David Sterbaf85b7372017-01-20 14:54:07 +01004538 ret = btrfs_insert_file_extent(trans, root, btrfs_ino(BTRFS_I(inode)),
4539 offset, 0, 0, len, 0, len, 0, 0, 0);
Josef Bacik16e75492013-10-22 12:18:51 -04004540 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004541 btrfs_abort_transaction(trans, ret);
Josef Bacik16e75492013-10-22 12:18:51 -04004542 else
4543 btrfs_update_inode(trans, root, inode);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004544 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04004545 return ret;
4546}
4547
Josef Bacik695a0d02011-03-04 15:46:53 -05004548/*
4549 * This function puts in dummy file extents for the area we're creating a hole
4550 * for. So if we are truncating this file to a larger size we need to insert
4551 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
4552 * the range between oldsize and size
4553 */
Josef Bacika41ad392011-01-31 15:30:16 -05004554int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04004555{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004556 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Yan Zheng9036c102008-10-30 14:19:41 -04004557 struct btrfs_root *root = BTRFS_I(inode)->root;
4558 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004559 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004560 struct extent_state *cached_state = NULL;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004561 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004562 u64 hole_start = ALIGN(oldsize, fs_info->sectorsize);
4563 u64 block_end = ALIGN(size, fs_info->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04004564 u64 last_byte;
4565 u64 cur_offset;
4566 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04004567 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04004568
Josef Bacika71754f2013-06-17 17:14:39 -04004569 /*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304570 * If our size started in the middle of a block we need to zero out the
4571 * rest of the block before we expand the i_size, otherwise we could
Josef Bacika71754f2013-06-17 17:14:39 -04004572 * expose stale data.
4573 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304574 err = btrfs_truncate_block(inode, oldsize, 0, 0);
Josef Bacika71754f2013-06-17 17:14:39 -04004575 if (err)
4576 return err;
4577
Yan Zheng9036c102008-10-30 14:19:41 -04004578 if (size <= hole_start)
4579 return 0;
4580
Nikolay Borisov23d31bd2019-05-07 10:19:23 +03004581 btrfs_lock_and_flush_ordered_range(io_tree, BTRFS_I(inode), hole_start,
4582 block_end - 1, &cached_state);
Yan Zheng9036c102008-10-30 14:19:41 -04004583 cur_offset = hole_start;
4584 while (1) {
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02004585 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, cur_offset,
Omar Sandoval39b07b52019-12-02 17:34:23 -08004586 block_end - cur_offset);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004587 if (IS_ERR(em)) {
4588 err = PTR_ERR(em);
Zach Brownf2767952013-01-08 19:37:58 +00004589 em = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004590 break;
4591 }
Yan Zheng9036c102008-10-30 14:19:41 -04004592 last_byte = min(extent_map_end(em), block_end);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004593 last_byte = ALIGN(last_byte, fs_info->sectorsize);
Yan, Zheng80825102009-11-12 09:35:36 +00004594 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04004595 struct extent_map *hole_em;
Yan Zheng9036c102008-10-30 14:19:41 -04004596 hole_size = last_byte - cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004597
Josef Bacik16e75492013-10-22 12:18:51 -04004598 err = maybe_insert_hole(root, inode, cur_offset,
4599 hole_size);
4600 if (err)
Chris Mason771ed682008-11-06 22:02:51 -05004601 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02004602 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04004603 cur_offset + hole_size - 1, 0);
4604 hole_em = alloc_extent_map();
4605 if (!hole_em) {
4606 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4607 &BTRFS_I(inode)->runtime_flags);
4608 goto next;
4609 }
4610 hole_em->start = cur_offset;
4611 hole_em->len = hole_size;
4612 hole_em->orig_start = cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004613
Josef Bacik5dc562c2012-08-17 13:14:17 -04004614 hole_em->block_start = EXTENT_MAP_HOLE;
4615 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05004616 hole_em->orig_block_len = 0;
Josef Bacikcc95bef2013-04-04 14:31:27 -04004617 hole_em->ram_bytes = hole_size;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004618 hole_em->compress_type = BTRFS_COMPRESS_NONE;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004619 hole_em->generation = fs_info->generation;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004620
4621 while (1) {
4622 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04004623 err = add_extent_mapping(em_tree, hole_em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004624 write_unlock(&em_tree->lock);
4625 if (err != -EEXIST)
4626 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02004627 btrfs_drop_extent_cache(BTRFS_I(inode),
4628 cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04004629 cur_offset +
4630 hole_size - 1, 0);
4631 }
4632 free_extent_map(hole_em);
Yan Zheng9036c102008-10-30 14:19:41 -04004633 }
Josef Bacik16e75492013-10-22 12:18:51 -04004634next:
Yan Zheng9036c102008-10-30 14:19:41 -04004635 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004636 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04004637 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00004638 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04004639 break;
4640 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04004641 free_extent_map(em);
David Sterbae43bbe52017-12-12 21:43:52 +01004642 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state);
Yan Zheng9036c102008-10-30 14:19:41 -04004643 return err;
4644}
4645
Eric Sandeen3972f262013-01-12 02:57:22 +00004646static int btrfs_setsize(struct inode *inode, struct iattr *attr)
Yan, Zheng80825102009-11-12 09:35:36 +00004647{
Miao Xief4a2f4c2011-12-14 20:12:01 -05004648 struct btrfs_root *root = BTRFS_I(inode)->root;
4649 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05004650 loff_t oldsize = i_size_read(inode);
Eric Sandeen3972f262013-01-12 02:57:22 +00004651 loff_t newsize = attr->ia_size;
4652 int mask = attr->ia_valid;
Yan, Zheng80825102009-11-12 09:35:36 +00004653 int ret;
4654
Eric Sandeen3972f262013-01-12 02:57:22 +00004655 /*
4656 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
4657 * special case where we need to update the times despite not having
4658 * these flags set. For all other operations the VFS set these flags
4659 * explicitly if it wants a timestamp update.
4660 */
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08004661 if (newsize != oldsize) {
4662 inode_inc_iversion(inode);
4663 if (!(mask & (ATTR_CTIME | ATTR_MTIME)))
4664 inode->i_ctime = inode->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07004665 current_time(inode);
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08004666 }
Eric Sandeen3972f262013-01-12 02:57:22 +00004667
Josef Bacika41ad392011-01-31 15:30:16 -05004668 if (newsize > oldsize) {
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004669 /*
David Sterbaea14b57f2017-06-22 02:19:11 +02004670 * Don't do an expanding truncate while snapshotting is ongoing.
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004671 * This is to ensure the snapshot captures a fully consistent
4672 * state of this file - if the snapshot captures this expanding
4673 * truncation, it must capture all writes that happened before
4674 * this truncation.
4675 */
Zhao Lei0bc19f902016-01-06 18:56:36 +08004676 btrfs_wait_for_snapshot_creation(root);
Josef Bacika41ad392011-01-31 15:30:16 -05004677 ret = btrfs_cont_expand(inode, oldsize, newsize);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004678 if (ret) {
David Sterbaea14b57f2017-06-22 02:19:11 +02004679 btrfs_end_write_no_snapshotting(root);
Yan, Zheng80825102009-11-12 09:35:36 +00004680 return ret;
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004681 }
Yan, Zheng80825102009-11-12 09:35:36 +00004682
Miao Xief4a2f4c2011-12-14 20:12:01 -05004683 trans = btrfs_start_transaction(root, 1);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004684 if (IS_ERR(trans)) {
David Sterbaea14b57f2017-06-22 02:19:11 +02004685 btrfs_end_write_no_snapshotting(root);
Miao Xief4a2f4c2011-12-14 20:12:01 -05004686 return PTR_ERR(trans);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004687 }
Miao Xief4a2f4c2011-12-14 20:12:01 -05004688
4689 i_size_write(inode, newsize);
4690 btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
Chandan Rajendra27772b62016-01-21 15:56:03 +05304691 pagecache_isize_extended(inode, oldsize, newsize);
Miao Xief4a2f4c2011-12-14 20:12:01 -05004692 ret = btrfs_update_inode(trans, root, inode);
David Sterbaea14b57f2017-06-22 02:19:11 +02004693 btrfs_end_write_no_snapshotting(root);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004694 btrfs_end_transaction(trans);
Josef Bacika41ad392011-01-31 15:30:16 -05004695 } else {
Yan, Zheng80825102009-11-12 09:35:36 +00004696
Josef Bacika41ad392011-01-31 15:30:16 -05004697 /*
4698 * We're truncating a file that used to have good data down to
4699 * zero. Make sure it gets into the ordered flush list so that
4700 * any new writes get down to disk quickly.
4701 */
4702 if (newsize == 0)
Josef Bacik72ac3c02012-05-23 14:13:11 -04004703 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
4704 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00004705
Josef Bacika41ad392011-01-31 15:30:16 -05004706 truncate_setsize(inode, newsize);
Miao Xie2e60a512013-02-08 07:01:08 +00004707
Andrea Gelmini52042d82018-11-28 12:05:13 +01004708 /* Disable nonlocked read DIO to avoid the endless truncate */
Nikolay Borisovabcefb12017-02-20 13:51:10 +02004709 btrfs_inode_block_unlocked_dio(BTRFS_I(inode));
Miao Xie2e60a512013-02-08 07:01:08 +00004710 inode_dio_wait(inode);
Nikolay Borisov0b581702017-02-20 13:51:11 +02004711 btrfs_inode_resume_unlocked_dio(BTRFS_I(inode));
Miao Xie2e60a512013-02-08 07:01:08 +00004712
Filipe Manana213e8c52018-02-06 20:40:31 +00004713 ret = btrfs_truncate(inode, newsize == oldsize);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004714 if (ret && inode->i_nlink) {
4715 int err;
4716
4717 /*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07004718 * Truncate failed, so fix up the in-memory size. We
4719 * adjusted disk_i_size down as we removed extents, so
4720 * wait for disk_i_size to be stable and then update the
4721 * in-memory size to match.
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004722 */
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07004723 err = btrfs_wait_ordered_range(inode, 0, (u64)-1);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004724 if (err)
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07004725 return err;
4726 i_size_write(inode, BTRFS_I(inode)->disk_i_size);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004727 }
Yan, Zheng80825102009-11-12 09:35:36 +00004728 }
4729
Josef Bacika41ad392011-01-31 15:30:16 -05004730 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00004731}
4732
Chris Mason39279cc2007-06-12 06:35:45 -04004733static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
4734{
David Howells2b0143b2015-03-17 22:25:59 +00004735 struct inode *inode = d_inode(dentry);
Li Zefanb83cc962010-12-20 16:04:08 +08004736 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004737 int err;
4738
Li Zefanb83cc962010-12-20 16:04:08 +08004739 if (btrfs_root_readonly(root))
4740 return -EROFS;
4741
Jan Kara31051c82016-05-26 16:55:18 +02004742 err = setattr_prepare(dentry, attr);
Chris Mason39279cc2007-06-12 06:35:45 -04004743 if (err)
4744 return err;
4745
Chris Mason5a3f23d2009-03-31 13:27:11 -04004746 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Eric Sandeen3972f262013-01-12 02:57:22 +00004747 err = btrfs_setsize(inode, attr);
Yan, Zheng80825102009-11-12 09:35:36 +00004748 if (err)
4749 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004750 }
Yan Zheng9036c102008-10-30 14:19:41 -04004751
Christoph Hellwig10257742010-06-04 11:30:02 +02004752 if (attr->ia_valid) {
4753 setattr_copy(inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004754 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05004755 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04004756
Josef Bacik22c44fe2011-11-30 10:45:38 -05004757 if (!err && attr->ia_valid & ATTR_MODE)
Christoph Hellwig996a7102013-12-20 05:16:43 -08004758 err = posix_acl_chmod(inode, inode->i_mode);
Christoph Hellwig10257742010-06-04 11:30:02 +02004759 }
4760
Chris Mason39279cc2007-06-12 06:35:45 -04004761 return err;
4762}
Chris Mason61295eb2008-01-14 16:24:38 -05004763
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004764/*
4765 * While truncating the inode pages during eviction, we get the VFS calling
4766 * btrfs_invalidatepage() against each page of the inode. This is slow because
4767 * the calls to btrfs_invalidatepage() result in a huge amount of calls to
4768 * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting
4769 * extent_state structures over and over, wasting lots of time.
4770 *
4771 * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all
4772 * those expensive operations on a per page basis and do only the ordered io
4773 * finishing, while we release here the extent_map and extent_state structures,
4774 * without the excessive merging and splitting.
4775 */
4776static void evict_inode_truncate_pages(struct inode *inode)
4777{
4778 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4779 struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree;
4780 struct rb_node *node;
4781
4782 ASSERT(inode->i_state & I_FREEING);
Johannes Weiner91b0abe2014-04-03 14:47:49 -07004783 truncate_inode_pages_final(&inode->i_data);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004784
4785 write_lock(&map_tree->lock);
Liu Bo07e1ce02018-08-23 03:51:52 +08004786 while (!RB_EMPTY_ROOT(&map_tree->map.rb_root)) {
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004787 struct extent_map *em;
4788
Liu Bo07e1ce02018-08-23 03:51:52 +08004789 node = rb_first_cached(&map_tree->map);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004790 em = rb_entry(node, struct extent_map, rb_node);
Wang Shilong180589e2013-12-14 15:27:31 +08004791 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
4792 clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004793 remove_extent_mapping(map_tree, em);
4794 free_extent_map(em);
Filipe Manana7064dd52014-08-08 02:47:05 +01004795 if (need_resched()) {
4796 write_unlock(&map_tree->lock);
4797 cond_resched();
4798 write_lock(&map_tree->lock);
4799 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004800 }
4801 write_unlock(&map_tree->lock);
4802
Filipe Manana6ca07092015-05-26 00:55:42 +01004803 /*
4804 * Keep looping until we have no more ranges in the io tree.
4805 * We can have ongoing bios started by readpages (called from readahead)
Filipe Manana9c6429d2015-06-10 12:55:41 +01004806 * that have their endio callback (extent_io.c:end_bio_extent_readpage)
4807 * still in progress (unlocked the pages in the bio but did not yet
4808 * unlocked the ranges in the io tree). Therefore this means some
Filipe Manana6ca07092015-05-26 00:55:42 +01004809 * ranges can still be locked and eviction started because before
4810 * submitting those bios, which are executed by a separate task (work
4811 * queue kthread), inode references (inode->i_count) were not taken
4812 * (which would be dropped in the end io callback of each bio).
4813 * Therefore here we effectively end up waiting for those bios and
4814 * anyone else holding locked ranges without having bumped the inode's
4815 * reference count - if we don't do it, when they access the inode's
4816 * io_tree to unlock a range it may be too late, leading to an
4817 * use-after-free issue.
4818 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004819 spin_lock(&io_tree->lock);
4820 while (!RB_EMPTY_ROOT(&io_tree->state)) {
4821 struct extent_state *state;
4822 struct extent_state *cached_state = NULL;
Filipe Manana6ca07092015-05-26 00:55:42 +01004823 u64 start;
4824 u64 end;
Filipe Manana421f0922018-10-12 13:02:48 +01004825 unsigned state_flags;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004826
4827 node = rb_first(&io_tree->state);
4828 state = rb_entry(node, struct extent_state, rb_node);
Filipe Manana6ca07092015-05-26 00:55:42 +01004829 start = state->start;
4830 end = state->end;
Filipe Manana421f0922018-10-12 13:02:48 +01004831 state_flags = state->state;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004832 spin_unlock(&io_tree->lock);
4833
David Sterbaff13db42015-12-03 14:30:40 +01004834 lock_extent_bits(io_tree, start, end, &cached_state);
Qu Wenruob9d0b382015-09-29 10:35:16 +08004835
4836 /*
4837 * If still has DELALLOC flag, the extent didn't reach disk,
4838 * and its reserved space won't be freed by delayed_ref.
4839 * So we need to free its reserved space here.
4840 * (Refer to comment in btrfs_invalidatepage, case 2)
4841 *
4842 * Note, end is the bytenr of last byte, so we need + 1 here.
4843 */
Filipe Manana421f0922018-10-12 13:02:48 +01004844 if (state_flags & EXTENT_DELALLOC)
Qu Wenruobc42bda2017-02-27 15:10:39 +08004845 btrfs_qgroup_free_data(inode, NULL, start, end - start + 1);
Qu Wenruob9d0b382015-09-29 10:35:16 +08004846
Filipe Manana6ca07092015-05-26 00:55:42 +01004847 clear_extent_bit(io_tree, start, end,
Omar Sandovale1821632019-08-15 14:04:04 -07004848 EXTENT_LOCKED | EXTENT_DELALLOC |
4849 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1,
4850 &cached_state);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004851
Filipe Manana7064dd52014-08-08 02:47:05 +01004852 cond_resched();
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004853 spin_lock(&io_tree->lock);
4854 }
4855 spin_unlock(&io_tree->lock);
4856}
4857
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07004858static struct btrfs_trans_handle *evict_refill_and_join(struct btrfs_root *root,
Josef Bacikad80cf52018-09-28 07:18:19 -04004859 struct btrfs_block_rsv *rsv)
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07004860{
4861 struct btrfs_fs_info *fs_info = root->fs_info;
4862 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
Josef Bacikd3984c92019-08-01 18:19:37 -04004863 struct btrfs_trans_handle *trans;
Josef Bacik2bd36e72019-08-22 15:14:33 -04004864 u64 delayed_refs_extra = btrfs_calc_insert_metadata_size(fs_info, 1);
Josef Bacikd3984c92019-08-01 18:19:37 -04004865 int ret;
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07004866
Josef Bacikd3984c92019-08-01 18:19:37 -04004867 /*
4868 * Eviction should be taking place at some place safe because of our
4869 * delayed iputs. However the normal flushing code will run delayed
4870 * iputs, so we cannot use FLUSH_ALL otherwise we'll deadlock.
4871 *
4872 * We reserve the delayed_refs_extra here again because we can't use
4873 * btrfs_start_transaction(root, 0) for the same deadlocky reason as
4874 * above. We reserve our extra bit here because we generate a ton of
4875 * delayed refs activity by truncating.
4876 *
4877 * If we cannot make our reservation we'll attempt to steal from the
4878 * global reserve, because we really want to be able to free up space.
4879 */
4880 ret = btrfs_block_rsv_refill(root, rsv, rsv->size + delayed_refs_extra,
4881 BTRFS_RESERVE_FLUSH_EVICT);
4882 if (ret) {
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07004883 /*
4884 * Try to steal from the global reserve if there is space for
4885 * it.
4886 */
Josef Bacikd3984c92019-08-01 18:19:37 -04004887 if (btrfs_check_space_for_delayed_refs(fs_info) ||
4888 btrfs_block_rsv_migrate(global_rsv, rsv, rsv->size, 0)) {
4889 btrfs_warn(fs_info,
4890 "could not allocate space for delete; will truncate on mount");
4891 return ERR_PTR(-ENOSPC);
4892 }
4893 delayed_refs_extra = 0;
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07004894 }
Josef Bacikd3984c92019-08-01 18:19:37 -04004895
4896 trans = btrfs_join_transaction(root);
4897 if (IS_ERR(trans))
4898 return trans;
4899
4900 if (delayed_refs_extra) {
4901 trans->block_rsv = &fs_info->trans_block_rsv;
4902 trans->bytes_reserved = delayed_refs_extra;
4903 btrfs_block_rsv_migrate(rsv, trans->block_rsv,
4904 delayed_refs_extra, 1);
4905 }
4906 return trans;
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07004907}
4908
Al Virobd555972010-06-07 11:35:40 -04004909void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04004910{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004911 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04004912 struct btrfs_trans_handle *trans;
4913 struct btrfs_root *root = BTRFS_I(inode)->root;
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07004914 struct btrfs_block_rsv *rsv;
Chris Mason39279cc2007-06-12 06:35:45 -04004915 int ret;
4916
liubo1abe9b82011-03-24 11:18:59 +00004917 trace_btrfs_inode_evict(inode);
4918
Nikolay Borisov3d48d982016-06-29 09:46:41 +03004919 if (!root) {
Liu Boe8f1bc12018-01-25 11:02:53 -07004920 clear_inode(inode);
Nikolay Borisov3d48d982016-06-29 09:46:41 +03004921 return;
4922 }
4923
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004924 evict_inode_truncate_pages(inode);
4925
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02004926 if (inode->i_nlink &&
4927 ((btrfs_root_refs(&root->root_item) != 0 &&
4928 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) ||
Nikolay Borisov70ddc552017-02-20 13:50:35 +02004929 btrfs_is_free_space_inode(BTRFS_I(inode))))
Al Virobd555972010-06-07 11:35:40 -04004930 goto no_delete;
4931
Omar Sandoval27919062018-05-11 13:13:37 -07004932 if (is_bad_inode(inode))
Chris Mason39279cc2007-06-12 06:35:45 -04004933 goto no_delete;
Chris Mason5f39d392007-10-15 16:14:19 -04004934
Nikolay Borisov7ab79562017-02-20 13:50:57 +02004935 btrfs_free_io_failure_record(BTRFS_I(inode), 0, (u64)-1);
Miao Xief6124962014-09-12 18:44:04 +08004936
Omar Sandoval7b40b692018-05-11 13:13:33 -07004937 if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags))
Yan, Zhengc71bf092009-11-12 09:34:40 +00004938 goto no_delete;
Yan, Zhengc71bf092009-11-12 09:34:40 +00004939
Yan, Zheng76dda932009-09-21 16:00:26 -04004940 if (inode->i_nlink > 0) {
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02004941 BUG_ON(btrfs_root_refs(&root->root_item) != 0 &&
4942 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID);
Yan, Zheng76dda932009-09-21 16:00:26 -04004943 goto no_delete;
4944 }
4945
Nikolay Borisovaa790212017-01-10 20:35:40 +02004946 ret = btrfs_commit_inode_delayed_inode(BTRFS_I(inode));
Omar Sandoval27919062018-05-11 13:13:37 -07004947 if (ret)
Miao Xie0e8c36a2012-12-19 06:59:51 +00004948 goto no_delete;
Miao Xie0e8c36a2012-12-19 06:59:51 +00004949
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004950 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Omar Sandoval27919062018-05-11 13:13:37 -07004951 if (!rsv)
Josef Bacik4289a662011-08-05 13:22:24 -04004952 goto no_delete;
Josef Bacik2bd36e72019-08-22 15:14:33 -04004953 rsv->size = btrfs_calc_metadata_size(fs_info, 1);
Josef Bacikca7e70f2012-08-27 17:48:15 -04004954 rsv->failfast = 1;
Josef Bacik4289a662011-08-05 13:22:24 -04004955
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004956 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Mason5f39d392007-10-15 16:14:19 -04004957
Yan, Zheng80825102009-11-12 09:35:36 +00004958 while (1) {
Josef Bacikad80cf52018-09-28 07:18:19 -04004959 trans = evict_refill_and_join(root, rsv);
Omar Sandoval27919062018-05-11 13:13:37 -07004960 if (IS_ERR(trans))
4961 goto free_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04004962
4963 trans->block_rsv = rsv;
4964
Yan, Zhengd68fc572010-05-16 10:49:58 -04004965 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Omar Sandoval27919062018-05-11 13:13:37 -07004966 trans->block_rsv = &fs_info->trans_block_rsv;
4967 btrfs_end_transaction(trans);
4968 btrfs_btree_balance_dirty(fs_info);
4969 if (ret && ret != -ENOSPC && ret != -EAGAIN)
4970 goto free_rsv;
4971 else if (!ret)
Yan, Zheng80825102009-11-12 09:35:36 +00004972 break;
Josef Bacik7b128762008-07-24 12:17:14 -04004973 }
4974
Josef Bacik4ef31a42013-08-13 14:10:08 -04004975 /*
Omar Sandoval27919062018-05-11 13:13:37 -07004976 * Errors here aren't a big deal, it just means we leave orphan items in
4977 * the tree. They will be cleaned up on the next mount. If the inode
4978 * number gets reused, cleanup deletes the orphan item without doing
4979 * anything, and unlink reuses the existing orphan item.
4980 *
4981 * If it turns out that we are dropping too many of these, we might want
4982 * to add a mechanism for retrying these after a commit.
Josef Bacik4ef31a42013-08-13 14:10:08 -04004983 */
Josef Bacikad80cf52018-09-28 07:18:19 -04004984 trans = evict_refill_and_join(root, rsv);
Omar Sandoval27919062018-05-11 13:13:37 -07004985 if (!IS_ERR(trans)) {
4986 trans->block_rsv = rsv;
4987 btrfs_orphan_del(trans, BTRFS_I(inode));
4988 trans->block_rsv = &fs_info->trans_block_rsv;
4989 btrfs_end_transaction(trans);
4990 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004991
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004992 if (!(root == fs_info->tree_root ||
Li Zefan581bb052011-04-20 10:06:11 +08004993 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004994 btrfs_return_ino(root, btrfs_ino(BTRFS_I(inode)));
Li Zefan581bb052011-04-20 10:06:11 +08004995
Omar Sandoval27919062018-05-11 13:13:37 -07004996free_rsv:
4997 btrfs_free_block_rsv(fs_info, rsv);
Chris Mason39279cc2007-06-12 06:35:45 -04004998no_delete:
Omar Sandoval27919062018-05-11 13:13:37 -07004999 /*
5000 * If we didn't successfully delete, the orphan item will still be in
5001 * the tree and we'll retry on the next mount. Again, we might also want
5002 * to retry these periodically in the future.
5003 */
Nikolay Borisovf48d1cf2017-01-10 20:35:39 +02005004 btrfs_remove_delayed_node(BTRFS_I(inode));
Jan Karadbd57682012-05-03 14:48:02 +02005005 clear_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005006}
5007
5008/*
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005009 * Return the key found in the dir entry in the location pointer, fill @type
5010 * with BTRFS_FT_*, and return 0.
5011 *
Su Yue005d6712018-03-05 17:13:37 +08005012 * If no dir entries were found, returns -ENOENT.
5013 * If found a corrupted location in dir entry, returns -EUCLEAN.
Chris Mason39279cc2007-06-12 06:35:45 -04005014 */
5015static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005016 struct btrfs_key *location, u8 *type)
Chris Mason39279cc2007-06-12 06:35:45 -04005017{
5018 const char *name = dentry->d_name.name;
5019 int namelen = dentry->d_name.len;
5020 struct btrfs_dir_item *di;
5021 struct btrfs_path *path;
5022 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04005023 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005024
5025 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005026 if (!path)
5027 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05005028
David Sterbaf85b7372017-01-20 14:54:07 +01005029 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(BTRFS_I(dir)),
5030 name, namelen, 0);
Liu Bo3cf50682018-09-12 06:06:26 +08005031 if (IS_ERR_OR_NULL(di)) {
5032 ret = di ? PTR_ERR(di) : -ENOENT;
Su Yue005d6712018-03-05 17:13:37 +08005033 goto out;
5034 }
Chris Masond3977122009-01-05 21:25:51 -05005035
Chris Mason5f39d392007-10-15 16:14:19 -04005036 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Liu Bo56a0e702017-10-30 11:14:38 -06005037 if (location->type != BTRFS_INODE_ITEM_KEY &&
5038 location->type != BTRFS_ROOT_ITEM_KEY) {
Su Yue005d6712018-03-05 17:13:37 +08005039 ret = -EUCLEAN;
Liu Bo56a0e702017-10-30 11:14:38 -06005040 btrfs_warn(root->fs_info,
5041"%s gets something invalid in DIR_ITEM (name %s, directory ino %llu, location(%llu %u %llu))",
5042 __func__, name, btrfs_ino(BTRFS_I(dir)),
5043 location->objectid, location->type, location->offset);
Liu Bo56a0e702017-10-30 11:14:38 -06005044 }
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005045 if (!ret)
5046 *type = btrfs_dir_type(path->nodes[0], di);
Chris Mason39279cc2007-06-12 06:35:45 -04005047out:
Chris Mason39279cc2007-06-12 06:35:45 -04005048 btrfs_free_path(path);
5049 return ret;
5050}
5051
5052/*
5053 * when we hit a tree root in a directory, the btrfs part of the inode
5054 * needs to be changed to reflect the root directory of the tree root. This
5055 * is kind of like crossing a mount point.
5056 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005057static int fixup_tree_root_location(struct btrfs_fs_info *fs_info,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005058 struct inode *dir,
5059 struct dentry *dentry,
5060 struct btrfs_key *location,
5061 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04005062{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005063 struct btrfs_path *path;
5064 struct btrfs_root *new_root;
5065 struct btrfs_root_ref *ref;
5066 struct extent_buffer *leaf;
David Sterba1d4c08e2015-01-02 19:36:14 +01005067 struct btrfs_key key;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005068 int ret;
5069 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005070
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005071 path = btrfs_alloc_path();
5072 if (!path) {
5073 err = -ENOMEM;
5074 goto out;
5075 }
Chris Mason39279cc2007-06-12 06:35:45 -04005076
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005077 err = -ENOENT;
David Sterba1d4c08e2015-01-02 19:36:14 +01005078 key.objectid = BTRFS_I(dir)->root->root_key.objectid;
5079 key.type = BTRFS_ROOT_REF_KEY;
5080 key.offset = location->objectid;
5081
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005082 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005083 if (ret) {
5084 if (ret < 0)
5085 err = ret;
5086 goto out;
5087 }
Chris Mason39279cc2007-06-12 06:35:45 -04005088
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005089 leaf = path->nodes[0];
5090 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005091 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(BTRFS_I(dir)) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005092 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
5093 goto out;
5094
5095 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
5096 (unsigned long)(ref + 1),
5097 dentry->d_name.len);
5098 if (ret)
5099 goto out;
5100
David Sterbab3b4aa72011-04-21 01:20:15 +02005101 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005102
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005103 new_root = btrfs_read_fs_root_no_name(fs_info, location);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005104 if (IS_ERR(new_root)) {
5105 err = PTR_ERR(new_root);
5106 goto out;
5107 }
5108
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005109 *sub_root = new_root;
5110 location->objectid = btrfs_root_dirid(&new_root->root_item);
5111 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04005112 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005113 err = 0;
5114out:
5115 btrfs_free_path(path);
5116 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005117}
5118
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005119static void inode_tree_add(struct inode *inode)
5120{
5121 struct btrfs_root *root = BTRFS_I(inode)->root;
5122 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005123 struct rb_node **p;
5124 struct rb_node *parent;
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005125 struct rb_node *new = &BTRFS_I(inode)->rb_node;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005126 u64 ino = btrfs_ino(BTRFS_I(inode));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005127
Al Viro1d3382cb2010-10-23 15:19:20 -04005128 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005129 return;
Miao Xiee1409ce2013-05-15 07:48:16 +00005130 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005131 spin_lock(&root->inode_lock);
Miao Xiee1409ce2013-05-15 07:48:16 +00005132 p = &root->inode_tree.rb_node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005133 while (*p) {
5134 parent = *p;
5135 entry = rb_entry(parent, struct btrfs_inode, rb_node);
5136
David Sterba37508512018-06-29 10:56:40 +02005137 if (ino < btrfs_ino(entry))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005138 p = &parent->rb_left;
David Sterba37508512018-06-29 10:56:40 +02005139 else if (ino > btrfs_ino(entry))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005140 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005141 else {
5142 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04005143 (I_WILL_FREE | I_FREEING)));
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005144 rb_replace_node(parent, new, &root->inode_tree);
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005145 RB_CLEAR_NODE(parent);
5146 spin_unlock(&root->inode_lock);
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005147 return;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005148 }
5149 }
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005150 rb_link_node(new, parent, p);
5151 rb_insert_color(new, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005152 spin_unlock(&root->inode_lock);
5153}
5154
5155static void inode_tree_del(struct inode *inode)
5156{
5157 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04005158 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005159
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005160 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005161 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005162 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005163 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04005164 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005165 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005166 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04005167
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005168 if (empty && btrfs_root_refs(&root->root_item) == 0) {
Yan, Zheng76dda932009-09-21 16:00:26 -04005169 spin_lock(&root->inode_lock);
5170 empty = RB_EMPTY_ROOT(&root->inode_tree);
5171 spin_unlock(&root->inode_lock);
5172 if (empty)
5173 btrfs_add_dead_root(root);
5174 }
5175}
5176
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005177
Chris Masone02119d2008-09-05 16:13:11 -04005178static int btrfs_init_locked_inode(struct inode *inode, void *p)
5179{
5180 struct btrfs_iget_args *args = p;
Chris Mason90d3e592014-01-09 17:28:00 -08005181 inode->i_ino = args->location->objectid;
5182 memcpy(&BTRFS_I(inode)->location, args->location,
5183 sizeof(*args->location));
Chris Masone02119d2008-09-05 16:13:11 -04005184 BTRFS_I(inode)->root = args->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005185 return 0;
5186}
5187
5188static int btrfs_find_actor(struct inode *inode, void *opaque)
5189{
5190 struct btrfs_iget_args *args = opaque;
Chris Mason90d3e592014-01-09 17:28:00 -08005191 return args->location->objectid == BTRFS_I(inode)->location.objectid &&
Chris Masond3977122009-01-05 21:25:51 -05005192 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005193}
5194
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005195static struct inode *btrfs_iget_locked(struct super_block *s,
Chris Mason90d3e592014-01-09 17:28:00 -08005196 struct btrfs_key *location,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005197 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04005198{
5199 struct inode *inode;
5200 struct btrfs_iget_args args;
Chris Mason90d3e592014-01-09 17:28:00 -08005201 unsigned long hashval = btrfs_inode_hash(location->objectid, root);
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005202
Chris Mason90d3e592014-01-09 17:28:00 -08005203 args.location = location;
Chris Mason39279cc2007-06-12 06:35:45 -04005204 args.root = root;
5205
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005206 inode = iget5_locked(s, hashval, btrfs_find_actor,
Chris Mason39279cc2007-06-12 06:35:45 -04005207 btrfs_init_locked_inode,
5208 (void *)&args);
5209 return inode;
5210}
5211
David Sterba4c66e0d2019-10-03 19:09:35 +02005212/*
5213 * Get an inode object given its location and corresponding root.
5214 * Path can be preallocated to prevent recursing back to iget through
5215 * allocator. NULL is also valid but may require an additional allocation
5216 * later.
Balaji Rao1a54ef82008-07-21 02:01:04 +05305217 */
Filipe Manana4222ea72018-10-24 10:13:03 +01005218struct inode *btrfs_iget_path(struct super_block *s, struct btrfs_key *location,
David Sterba4c66e0d2019-10-03 19:09:35 +02005219 struct btrfs_root *root, struct btrfs_path *path)
Balaji Rao1a54ef82008-07-21 02:01:04 +05305220{
5221 struct inode *inode;
5222
Chris Mason90d3e592014-01-09 17:28:00 -08005223 inode = btrfs_iget_locked(s, location, root);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305224 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005225 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305226
5227 if (inode->i_state & I_NEW) {
Filipe Manana67710892016-06-06 11:51:25 +01005228 int ret;
5229
Filipe Manana4222ea72018-10-24 10:13:03 +01005230 ret = btrfs_read_locked_inode(inode, path);
Al Viro9bc2cef2018-07-29 23:04:50 +01005231 if (!ret) {
Mark Fasheh1748f842011-07-12 11:25:31 -07005232 inode_tree_add(inode);
5233 unlock_new_inode(inode);
Mark Fasheh1748f842011-07-12 11:25:31 -07005234 } else {
Al Virof5b3a412018-07-29 23:04:51 +01005235 iget_failed(inode);
5236 /*
5237 * ret > 0 can come from btrfs_search_slot called by
5238 * btrfs_read_locked_inode, this means the inode item
5239 * was not found.
5240 */
5241 if (ret > 0)
5242 ret = -ENOENT;
5243 inode = ERR_PTR(ret);
Mark Fasheh1748f842011-07-12 11:25:31 -07005244 }
5245 }
5246
Balaji Rao1a54ef82008-07-21 02:01:04 +05305247 return inode;
5248}
5249
Filipe Manana4222ea72018-10-24 10:13:03 +01005250struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
David Sterba4c66e0d2019-10-03 19:09:35 +02005251 struct btrfs_root *root)
Filipe Manana4222ea72018-10-24 10:13:03 +01005252{
David Sterba4c66e0d2019-10-03 19:09:35 +02005253 return btrfs_iget_path(s, location, root, NULL);
Filipe Manana4222ea72018-10-24 10:13:03 +01005254}
5255
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005256static struct inode *new_simple_dir(struct super_block *s,
5257 struct btrfs_key *key,
5258 struct btrfs_root *root)
5259{
5260 struct inode *inode = new_inode(s);
5261
5262 if (!inode)
5263 return ERR_PTR(-ENOMEM);
5264
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005265 BTRFS_I(inode)->root = root;
5266 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04005267 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005268
5269 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Omar Sandoval6bb6b512019-12-05 10:36:04 -08005270 /*
5271 * We only need lookup, the rest is read-only and there's no inode
5272 * associated with the dentry
5273 */
5274 inode->i_op = &simple_dir_inode_operations;
Omar Sandoval1fdf4192017-01-25 17:06:39 -08005275 inode->i_opflags &= ~IOP_XATTR;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005276 inode->i_fop = &simple_dir_operations;
5277 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
Deepa Dinamanic2050a42016-09-14 07:48:06 -07005278 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05305279 inode->i_atime = inode->i_mtime;
5280 inode->i_ctime = inode->i_mtime;
Arnd Bergmannd3c6be6f2018-06-21 18:04:06 +02005281 BTRFS_I(inode)->i_otime = inode->i_mtime;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005282
5283 return inode;
5284}
5285
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005286static inline u8 btrfs_inode_type(struct inode *inode)
5287{
5288 /*
5289 * Compile-time asserts that generic FT_* types still match
5290 * BTRFS_FT_* types
5291 */
5292 BUILD_BUG_ON(BTRFS_FT_UNKNOWN != FT_UNKNOWN);
5293 BUILD_BUG_ON(BTRFS_FT_REG_FILE != FT_REG_FILE);
5294 BUILD_BUG_ON(BTRFS_FT_DIR != FT_DIR);
5295 BUILD_BUG_ON(BTRFS_FT_CHRDEV != FT_CHRDEV);
5296 BUILD_BUG_ON(BTRFS_FT_BLKDEV != FT_BLKDEV);
5297 BUILD_BUG_ON(BTRFS_FT_FIFO != FT_FIFO);
5298 BUILD_BUG_ON(BTRFS_FT_SOCK != FT_SOCK);
5299 BUILD_BUG_ON(BTRFS_FT_SYMLINK != FT_SYMLINK);
5300
5301 return fs_umode_to_ftype(inode->i_mode);
5302}
5303
Chris Mason3de45862008-11-17 21:02:50 -05005304struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04005305{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005306 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masond3977122009-01-05 21:25:51 -05005307 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005308 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005309 struct btrfs_root *sub_root = root;
5310 struct btrfs_key location;
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005311 u8 di_type = 0;
Yan, Zheng76dda932009-09-21 16:00:26 -04005312 int index;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005313 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005314
5315 if (dentry->d_name.len > BTRFS_NAME_LEN)
5316 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04005317
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005318 ret = btrfs_inode_by_name(dir, dentry, &location, &di_type);
Chris Mason39279cc2007-06-12 06:35:45 -04005319 if (ret < 0)
5320 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04005321
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005322 if (location.type == BTRFS_INODE_ITEM_KEY) {
David Sterba4c66e0d2019-10-03 19:09:35 +02005323 inode = btrfs_iget(dir->i_sb, &location, root);
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005324 if (IS_ERR(inode))
5325 return inode;
5326
5327 /* Do extra check against inode mode with di_type */
5328 if (btrfs_inode_type(inode) != di_type) {
5329 btrfs_crit(fs_info,
5330"inode mode mismatch with dir: inode mode=0%o btrfs type=%u dir type=%u",
5331 inode->i_mode, btrfs_inode_type(inode),
5332 di_type);
5333 iput(inode);
5334 return ERR_PTR(-EUCLEAN);
5335 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005336 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04005337 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005338
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005339 index = srcu_read_lock(&fs_info->subvol_srcu);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005340 ret = fixup_tree_root_location(fs_info, dir, dentry,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005341 &location, &sub_root);
5342 if (ret < 0) {
5343 if (ret != -ENOENT)
5344 inode = ERR_PTR(ret);
5345 else
5346 inode = new_simple_dir(dir->i_sb, &location, sub_root);
5347 } else {
David Sterba4c66e0d2019-10-03 19:09:35 +02005348 inode = btrfs_iget(dir->i_sb, &location, sub_root);
Chris Mason39279cc2007-06-12 06:35:45 -04005349 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005350 srcu_read_unlock(&fs_info->subvol_srcu, index);
Yan, Zheng76dda932009-09-21 16:00:26 -04005351
Julia Lawall34d19ba2011-01-24 19:55:19 +00005352 if (!IS_ERR(inode) && root != sub_root) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005353 down_read(&fs_info->cleanup_work_sem);
David Howellsbc98a422017-07-17 08:45:34 +01005354 if (!sb_rdonly(inode->i_sb))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005355 ret = btrfs_orphan_cleanup(sub_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005356 up_read(&fs_info->cleanup_work_sem);
Josef Bacik01cd3362013-06-03 21:39:49 -04005357 if (ret) {
5358 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005359 inode = ERR_PTR(ret);
Josef Bacik01cd3362013-06-03 21:39:49 -04005360 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00005361 }
5362
Chris Mason3de45862008-11-17 21:02:50 -05005363 return inode;
5364}
5365
Nick Pigginfe15ce42011-01-07 17:49:23 +11005366static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04005367{
5368 struct btrfs_root *root;
David Howells2b0143b2015-03-17 22:25:59 +00005369 struct inode *inode = d_inode(dentry);
Yan, Zheng76dda932009-09-21 16:00:26 -04005370
Li Zefan848cce02012-02-21 17:04:28 +08005371 if (!inode && !IS_ROOT(dentry))
David Howells2b0143b2015-03-17 22:25:59 +00005372 inode = d_inode(dentry->d_parent);
Yan, Zheng76dda932009-09-21 16:00:26 -04005373
Li Zefan848cce02012-02-21 17:04:28 +08005374 if (inode) {
5375 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04005376 if (btrfs_root_refs(&root->root_item) == 0)
5377 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08005378
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005379 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Li Zefan848cce02012-02-21 17:04:28 +08005380 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04005381 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005382 return 0;
5383}
5384
Chris Mason3de45862008-11-17 21:02:50 -05005385static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04005386 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05005387{
Al Viro3837d202018-10-10 16:38:27 -04005388 struct inode *inode = btrfs_lookup_dentry(dir, dentry);
Josef Bacika66e7cc2011-09-18 10:34:03 -04005389
Al Viro3837d202018-10-10 16:38:27 -04005390 if (inode == ERR_PTR(-ENOENT))
5391 inode = NULL;
Al Viro41d28bc2014-10-12 22:24:21 -04005392 return d_splice_alias(inode, dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04005393}
5394
Josef Bacik23b5ec72017-07-24 15:14:25 -04005395/*
5396 * All this infrastructure exists because dir_emit can fault, and we are holding
5397 * the tree lock when doing readdir. For now just allocate a buffer and copy
5398 * our information into that, and then dir_emit from the buffer. This is
5399 * similar to what NFS does, only we don't keep the buffer around in pagecache
5400 * because I'm afraid I'll mess that up. Long term we need to make filldir do
5401 * copy_to_user_inatomic so we don't have to worry about page faulting under the
5402 * tree lock.
5403 */
5404static int btrfs_opendir(struct inode *inode, struct file *file)
5405{
5406 struct btrfs_file_private *private;
5407
5408 private = kzalloc(sizeof(struct btrfs_file_private), GFP_KERNEL);
5409 if (!private)
5410 return -ENOMEM;
5411 private->filldir_buf = kzalloc(PAGE_SIZE, GFP_KERNEL);
5412 if (!private->filldir_buf) {
5413 kfree(private);
5414 return -ENOMEM;
5415 }
5416 file->private_data = private;
5417 return 0;
5418}
5419
5420struct dir_entry {
5421 u64 ino;
5422 u64 offset;
5423 unsigned type;
5424 int name_len;
5425};
5426
5427static int btrfs_filldir(void *addr, int entries, struct dir_context *ctx)
5428{
5429 while (entries--) {
5430 struct dir_entry *entry = addr;
5431 char *name = (char *)(entry + 1);
5432
David Sterba92d32172018-04-16 21:10:14 +02005433 ctx->pos = get_unaligned(&entry->offset);
5434 if (!dir_emit(ctx, name, get_unaligned(&entry->name_len),
5435 get_unaligned(&entry->ino),
5436 get_unaligned(&entry->type)))
Josef Bacik23b5ec72017-07-24 15:14:25 -04005437 return 1;
David Sterba92d32172018-04-16 21:10:14 +02005438 addr += sizeof(struct dir_entry) +
5439 get_unaligned(&entry->name_len);
Josef Bacik23b5ec72017-07-24 15:14:25 -04005440 ctx->pos++;
5441 }
5442 return 0;
5443}
5444
Al Viro9cdda8d2013-05-22 16:48:09 -04005445static int btrfs_real_readdir(struct file *file, struct dir_context *ctx)
Chris Mason39279cc2007-06-12 06:35:45 -04005446{
Al Viro9cdda8d2013-05-22 16:48:09 -04005447 struct inode *inode = file_inode(file);
Chris Mason39279cc2007-06-12 06:35:45 -04005448 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005449 struct btrfs_file_private *private = file->private_data;
Chris Mason39279cc2007-06-12 06:35:45 -04005450 struct btrfs_dir_item *di;
5451 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04005452 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04005453 struct btrfs_path *path;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005454 void *addr;
Miao Xie16cdcec2011-04-22 18:12:22 +08005455 struct list_head ins_list;
5456 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04005457 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04005458 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04005459 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04005460 char *name_ptr;
5461 int name_len;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005462 int entries = 0;
5463 int total_len = 0;
Omar Sandoval02dbfc92016-05-20 13:50:33 -07005464 bool put = false;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005465 struct btrfs_key location;
Chris Mason5f39d392007-10-15 16:14:19 -04005466
Al Viro9cdda8d2013-05-22 16:48:09 -04005467 if (!dir_emit_dots(file, ctx))
5468 return 0;
5469
David Woodhouse49593bf2008-08-17 17:08:36 +01005470 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08005471 if (!path)
5472 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04005473
Josef Bacik23b5ec72017-07-24 15:14:25 -04005474 addr = private->filldir_buf;
David Sterbae4058b52015-11-27 16:31:35 +01005475 path->reada = READA_FORWARD;
David Woodhouse49593bf2008-08-17 17:08:36 +01005476
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005477 INIT_LIST_HEAD(&ins_list);
5478 INIT_LIST_HEAD(&del_list);
5479 put = btrfs_readdir_get_delayed_items(inode, &ins_list, &del_list);
Miao Xie16cdcec2011-04-22 18:12:22 +08005480
Josef Bacik23b5ec72017-07-24 15:14:25 -04005481again:
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005482 key.type = BTRFS_DIR_INDEX_KEY;
Al Viro9cdda8d2013-05-22 16:48:09 -04005483 key.offset = ctx->pos;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005484 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason5f39d392007-10-15 16:14:19 -04005485
Chris Mason39279cc2007-06-12 06:35:45 -04005486 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5487 if (ret < 0)
5488 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01005489
5490 while (1) {
Josef Bacik23b5ec72017-07-24 15:14:25 -04005491 struct dir_entry *entry;
5492
Chris Mason5f39d392007-10-15 16:14:19 -04005493 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04005494 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08005495 if (slot >= btrfs_header_nritems(leaf)) {
5496 ret = btrfs_next_leaf(root, path);
5497 if (ret < 0)
5498 goto err;
5499 else if (ret > 0)
5500 break;
5501 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04005502 }
Chris Mason3de45862008-11-17 21:02:50 -05005503
Chris Mason5f39d392007-10-15 16:14:19 -04005504 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5505
5506 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04005507 break;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005508 if (found_key.type != BTRFS_DIR_INDEX_KEY)
Chris Mason39279cc2007-06-12 06:35:45 -04005509 break;
Al Viro9cdda8d2013-05-22 16:48:09 -04005510 if (found_key.offset < ctx->pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08005511 goto next;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005512 if (btrfs_should_delete_dir_index(&del_list, found_key.offset))
Miao Xie16cdcec2011-04-22 18:12:22 +08005513 goto next;
Chris Mason39279cc2007-06-12 06:35:45 -04005514 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005515 name_len = btrfs_dir_name_len(leaf, di);
Josef Bacik23b5ec72017-07-24 15:14:25 -04005516 if ((total_len + sizeof(struct dir_entry) + name_len) >=
5517 PAGE_SIZE) {
5518 btrfs_release_path(path);
5519 ret = btrfs_filldir(private->filldir_buf, entries, ctx);
5520 if (ret)
5521 goto nopos;
5522 addr = private->filldir_buf;
5523 entries = 0;
5524 total_len = 0;
5525 goto again;
Chris Mason39279cc2007-06-12 06:35:45 -04005526 }
Josef Bacik23b5ec72017-07-24 15:14:25 -04005527
5528 entry = addr;
David Sterba92d32172018-04-16 21:10:14 +02005529 put_unaligned(name_len, &entry->name_len);
Josef Bacik23b5ec72017-07-24 15:14:25 -04005530 name_ptr = (char *)(entry + 1);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005531 read_extent_buffer(leaf, name_ptr, (unsigned long)(di + 1),
5532 name_len);
Phillip Potter7d157c32019-03-26 21:39:34 +00005533 put_unaligned(fs_ftype_to_dtype(btrfs_dir_type(leaf, di)),
David Sterba92d32172018-04-16 21:10:14 +02005534 &entry->type);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005535 btrfs_dir_item_key_to_cpu(leaf, di, &location);
David Sterba92d32172018-04-16 21:10:14 +02005536 put_unaligned(location.objectid, &entry->ino);
5537 put_unaligned(found_key.offset, &entry->offset);
Josef Bacik23b5ec72017-07-24 15:14:25 -04005538 entries++;
5539 addr += sizeof(struct dir_entry) + name_len;
5540 total_len += sizeof(struct dir_entry) + name_len;
Li Zefanb9e03af2011-03-23 10:43:58 +08005541next:
5542 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04005543 }
Josef Bacik23b5ec72017-07-24 15:14:25 -04005544 btrfs_release_path(path);
5545
5546 ret = btrfs_filldir(private->filldir_buf, entries, ctx);
5547 if (ret)
5548 goto nopos;
David Woodhouse49593bf2008-08-17 17:08:36 +01005549
Jeff Mahoneyd2fbb2b2016-11-05 13:26:35 -04005550 ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005551 if (ret)
David Sterbabc4ef752015-11-13 13:44:28 +01005552 goto nopos;
5553
Zach Browndb62efb2013-07-11 16:19:42 -07005554 /*
5555 * Stop new entries from being returned after we return the last
5556 * entry.
5557 *
5558 * New directory entries are assigned a strictly increasing
5559 * offset. This means that new entries created during readdir
5560 * are *guaranteed* to be seen in the future by that readdir.
5561 * This has broken buggy programs which operate on names as
5562 * they're returned by readdir. Until we re-use freed offsets
5563 * we have this hack to stop new entries from being returned
5564 * under the assumption that they'll never reach this huge
5565 * offset.
5566 *
5567 * This is being careful not to overflow 32bit loff_t unless the
5568 * last entry requires it because doing so has broken 32bit apps
5569 * in the past.
5570 */
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005571 if (ctx->pos >= INT_MAX)
5572 ctx->pos = LLONG_MAX;
5573 else
5574 ctx->pos = INT_MAX;
Chris Mason39279cc2007-06-12 06:35:45 -04005575nopos:
5576 ret = 0;
5577err:
Omar Sandoval02dbfc92016-05-20 13:50:33 -07005578 if (put)
5579 btrfs_readdir_put_delayed_items(inode, &ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04005580 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005581 return ret;
5582}
5583
Chris Mason39279cc2007-06-12 06:35:45 -04005584/*
Chris Mason54aa1f42007-06-22 14:16:25 -04005585 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04005586 * inode changes. But, it is most likely to find the inode in cache.
5587 * FIXME, needs more benchmarking...there are no reasons other than performance
5588 * to keep or drop this code.
5589 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00005590static int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005591{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005592 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04005593 struct btrfs_root *root = BTRFS_I(inode)->root;
5594 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005595 int ret;
5596
Josef Bacik72ac3c02012-05-23 14:13:11 -04005597 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05005598 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005599
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005600 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005601 if (IS_ERR(trans))
5602 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005603
5604 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005605 if (ret && ret == -ENOSPC) {
5606 /* whoops, lets try again with the full transaction */
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005607 btrfs_end_transaction(trans);
Chris Mason94b60442010-05-26 11:02:00 -04005608 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005609 if (IS_ERR(trans))
5610 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005611
Chris Mason94b60442010-05-26 11:02:00 -04005612 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005613 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005614 btrfs_end_transaction(trans);
Miao Xie16cdcec2011-04-22 18:12:22 +08005615 if (BTRFS_I(inode)->delayed_node)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005616 btrfs_balance_delayed_items(fs_info);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005617
5618 return ret;
5619}
5620
5621/*
5622 * This is a copy of file_update_time. We need this so we can return error on
5623 * ENOSPC for updating the inode in the case of file write and mmap writes.
5624 */
Deepa Dinamani95582b02018-05-08 19:36:02 -07005625static int btrfs_update_time(struct inode *inode, struct timespec64 *now,
Josef Bacike41f9412012-03-26 09:46:47 -04005626 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05005627{
Alexander Block2bc5565282012-06-15 09:49:33 +02005628 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Layton3a8c7232017-12-11 06:35:24 -05005629 bool dirty = flags & ~S_VERSION;
Alexander Block2bc5565282012-06-15 09:49:33 +02005630
5631 if (btrfs_root_readonly(root))
5632 return -EROFS;
5633
Josef Bacike41f9412012-03-26 09:46:47 -04005634 if (flags & S_VERSION)
Jeff Layton3a8c7232017-12-11 06:35:24 -05005635 dirty |= inode_maybe_inc_iversion(inode, dirty);
Josef Bacike41f9412012-03-26 09:46:47 -04005636 if (flags & S_CTIME)
5637 inode->i_ctime = *now;
5638 if (flags & S_MTIME)
5639 inode->i_mtime = *now;
5640 if (flags & S_ATIME)
5641 inode->i_atime = *now;
Jeff Layton3a8c7232017-12-11 06:35:24 -05005642 return dirty ? btrfs_dirty_inode(inode) : 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005643}
5644
Chris Masond352ac62008-09-29 15:18:18 -04005645/*
5646 * find the highest existing sequence number in a directory
5647 * and then set the in-memory index_cnt variable to reflect
5648 * free sequence numbers
5649 */
Nikolay Borisov4c5706552017-02-20 13:50:32 +02005650static int btrfs_set_inode_index_count(struct btrfs_inode *inode)
Josef Bacikaec74772008-07-24 12:12:38 -04005651{
Nikolay Borisov4c5706552017-02-20 13:50:32 +02005652 struct btrfs_root *root = inode->root;
Josef Bacikaec74772008-07-24 12:12:38 -04005653 struct btrfs_key key, found_key;
5654 struct btrfs_path *path;
5655 struct extent_buffer *leaf;
5656 int ret;
5657
Nikolay Borisov4c5706552017-02-20 13:50:32 +02005658 key.objectid = btrfs_ino(inode);
David Sterba962a2982014-06-04 18:41:45 +02005659 key.type = BTRFS_DIR_INDEX_KEY;
Josef Bacikaec74772008-07-24 12:12:38 -04005660 key.offset = (u64)-1;
5661
5662 path = btrfs_alloc_path();
5663 if (!path)
5664 return -ENOMEM;
5665
5666 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5667 if (ret < 0)
5668 goto out;
5669 /* FIXME: we should be able to handle this */
5670 if (ret == 0)
5671 goto out;
5672 ret = 0;
5673
5674 /*
5675 * MAGIC NUMBER EXPLANATION:
5676 * since we search a directory based on f_pos we have to start at 2
5677 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
5678 * else has to start at 2
5679 */
5680 if (path->slots[0] == 0) {
Nikolay Borisov4c5706552017-02-20 13:50:32 +02005681 inode->index_cnt = 2;
Josef Bacikaec74772008-07-24 12:12:38 -04005682 goto out;
5683 }
5684
5685 path->slots[0]--;
5686
5687 leaf = path->nodes[0];
5688 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5689
Nikolay Borisov4c5706552017-02-20 13:50:32 +02005690 if (found_key.objectid != btrfs_ino(inode) ||
David Sterba962a2982014-06-04 18:41:45 +02005691 found_key.type != BTRFS_DIR_INDEX_KEY) {
Nikolay Borisov4c5706552017-02-20 13:50:32 +02005692 inode->index_cnt = 2;
Josef Bacikaec74772008-07-24 12:12:38 -04005693 goto out;
5694 }
5695
Nikolay Borisov4c5706552017-02-20 13:50:32 +02005696 inode->index_cnt = found_key.offset + 1;
Josef Bacikaec74772008-07-24 12:12:38 -04005697out:
5698 btrfs_free_path(path);
5699 return ret;
5700}
5701
Chris Masond352ac62008-09-29 15:18:18 -04005702/*
5703 * helper to find a free sequence number in a given directory. This current
5704 * code is very simple, later versions will do smarter things in the btree
5705 */
Nikolay Borisov877574e2017-02-20 13:50:33 +02005706int btrfs_set_inode_index(struct btrfs_inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04005707{
5708 int ret = 0;
5709
Nikolay Borisov877574e2017-02-20 13:50:33 +02005710 if (dir->index_cnt == (u64)-1) {
5711 ret = btrfs_inode_delayed_dir_index_count(dir);
Miao Xie16cdcec2011-04-22 18:12:22 +08005712 if (ret) {
5713 ret = btrfs_set_inode_index_count(dir);
5714 if (ret)
5715 return ret;
5716 }
Josef Bacikaec74772008-07-24 12:12:38 -04005717 }
5718
Nikolay Borisov877574e2017-02-20 13:50:33 +02005719 *index = dir->index_cnt;
5720 dir->index_cnt++;
Josef Bacikaec74772008-07-24 12:12:38 -04005721
5722 return ret;
5723}
5724
Chris Masonb0d5d102014-09-08 13:08:51 -07005725static int btrfs_insert_inode_locked(struct inode *inode)
5726{
5727 struct btrfs_iget_args args;
5728 args.location = &BTRFS_I(inode)->location;
5729 args.root = BTRFS_I(inode)->root;
5730
5731 return insert_inode_locked4(inode,
5732 btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root),
5733 btrfs_find_actor, &args);
5734}
5735
Anand Jain19aee8d2017-07-18 17:37:05 +08005736/*
5737 * Inherit flags from the parent inode.
5738 *
5739 * Currently only the compression flags and the cow flags are inherited.
5740 */
5741static void btrfs_inherit_iflags(struct inode *inode, struct inode *dir)
5742{
5743 unsigned int flags;
5744
5745 if (!dir)
5746 return;
5747
5748 flags = BTRFS_I(dir)->flags;
5749
5750 if (flags & BTRFS_INODE_NOCOMPRESS) {
5751 BTRFS_I(inode)->flags &= ~BTRFS_INODE_COMPRESS;
5752 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
5753 } else if (flags & BTRFS_INODE_COMPRESS) {
5754 BTRFS_I(inode)->flags &= ~BTRFS_INODE_NOCOMPRESS;
5755 BTRFS_I(inode)->flags |= BTRFS_INODE_COMPRESS;
5756 }
5757
5758 if (flags & BTRFS_INODE_NODATACOW) {
5759 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
5760 if (S_ISREG(inode->i_mode))
5761 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
5762 }
5763
David Sterba7b6a2212018-03-26 18:40:21 +02005764 btrfs_sync_inode_flags_to_i_flags(inode);
Anand Jain19aee8d2017-07-18 17:37:05 +08005765}
5766
Chris Mason39279cc2007-06-12 06:35:45 -04005767static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
5768 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04005769 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05005770 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04005771 u64 ref_objectid, u64 objectid,
5772 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04005773{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005774 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04005775 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04005776 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04005777 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04005778 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05005779 struct btrfs_inode_ref *ref;
5780 struct btrfs_key key[2];
5781 u32 sizes[2];
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005782 int nitems = name ? 2 : 1;
Chris Mason9c583092008-01-29 15:15:18 -05005783 unsigned long ptr;
Josef Bacik11a19a92019-09-09 10:12:04 -04005784 unsigned int nofs_flag;
Chris Mason39279cc2007-06-12 06:35:45 -04005785 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04005786
Chris Mason5f39d392007-10-15 16:14:19 -04005787 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005788 if (!path)
5789 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04005790
Josef Bacik11a19a92019-09-09 10:12:04 -04005791 nofs_flag = memalloc_nofs_save();
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005792 inode = new_inode(fs_info->sb);
Josef Bacik11a19a92019-09-09 10:12:04 -04005793 memalloc_nofs_restore(nofs_flag);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005794 if (!inode) {
5795 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005796 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005797 }
Chris Mason39279cc2007-06-12 06:35:45 -04005798
Li Zefan581bb052011-04-20 10:06:11 +08005799 /*
Filipe Manana5762b5c2014-08-01 00:10:32 +01005800 * O_TMPFILE, set link count to 0, so that after this point,
5801 * we fill in an inode item with the correct link count.
5802 */
5803 if (!name)
5804 set_nlink(inode, 0);
5805
5806 /*
Li Zefan581bb052011-04-20 10:06:11 +08005807 * we have to initialize this early, so we can reclaim the inode
5808 * number if we fail afterwards in this function.
5809 */
5810 inode->i_ino = objectid;
5811
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005812 if (dir && name) {
liubo1abe9b82011-03-24 11:18:59 +00005813 trace_btrfs_inode_request(dir);
5814
Nikolay Borisov877574e2017-02-20 13:50:33 +02005815 ret = btrfs_set_inode_index(BTRFS_I(dir), index);
Shen Feng09771432009-04-02 16:46:06 -04005816 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005817 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04005818 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04005819 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04005820 }
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005821 } else if (dir) {
5822 *index = 0;
Josef Bacikaec74772008-07-24 12:12:38 -04005823 }
5824 /*
5825 * index_cnt is ignored for everything but a dir,
Su Yuedf6703e2018-01-12 11:08:02 +08005826 * btrfs_set_inode_index_count has an explanation for the magic
Josef Bacikaec74772008-07-24 12:12:38 -04005827 * number
5828 */
5829 BTRFS_I(inode)->index_cnt = 2;
Miao Xie67de1172013-12-26 13:07:06 +08005830 BTRFS_I(inode)->dir_index = *index;
Chris Mason39279cc2007-06-12 06:35:45 -04005831 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04005832 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00005833 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db22007-08-27 16:49:44 -04005834
Josef Bacik5dc562c2012-08-17 13:14:17 -04005835 /*
5836 * We could have gotten an inode number from somebody who was fsynced
5837 * and then removed in this same transaction, so let's just set full
5838 * sync since it will be a full sync anyway and this will blow away the
5839 * old info in the log.
5840 */
5841 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
5842
Chris Mason9c583092008-01-29 15:15:18 -05005843 key[0].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02005844 key[0].type = BTRFS_INODE_ITEM_KEY;
Chris Mason9c583092008-01-29 15:15:18 -05005845 key[0].offset = 0;
5846
Chris Mason9c583092008-01-29 15:15:18 -05005847 sizes[0] = sizeof(struct btrfs_inode_item);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005848
5849 if (name) {
5850 /*
5851 * Start new inodes with an inode_ref. This is slightly more
5852 * efficient for small numbers of hard links since they will
5853 * be packed into one item. Extended refs will kick in if we
5854 * add more hard links than can fit in the ref item.
5855 */
5856 key[1].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02005857 key[1].type = BTRFS_INODE_REF_KEY;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005858 key[1].offset = ref_objectid;
5859
5860 sizes[1] = name_len + sizeof(*ref);
5861 }
Chris Mason9c583092008-01-29 15:15:18 -05005862
Chris Masonb0d5d102014-09-08 13:08:51 -07005863 location = &BTRFS_I(inode)->location;
5864 location->objectid = objectid;
5865 location->offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02005866 location->type = BTRFS_INODE_ITEM_KEY;
Chris Masonb0d5d102014-09-08 13:08:51 -07005867
5868 ret = btrfs_insert_inode_locked(inode);
Al Viro32955c52018-05-16 12:20:05 -04005869 if (ret < 0) {
5870 iput(inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07005871 goto fail;
Al Viro32955c52018-05-16 12:20:05 -04005872 }
Chris Masonb0d5d102014-09-08 13:08:51 -07005873
Chris Masonb9473432009-03-13 11:00:37 -04005874 path->leave_spinning = 1;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005875 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems);
Chris Mason9c583092008-01-29 15:15:18 -05005876 if (ret != 0)
Chris Masonb0d5d102014-09-08 13:08:51 -07005877 goto fail_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04005878
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03005879 inode_init_owner(inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04005880 inode_set_bytes(inode, 0);
chandan r9cc97d62012-07-04 12:48:07 +05305881
Deepa Dinamanic2050a42016-09-14 07:48:06 -07005882 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05305883 inode->i_atime = inode->i_mtime;
5884 inode->i_ctime = inode->i_mtime;
Arnd Bergmannd3c6be6f2018-06-21 18:04:06 +02005885 BTRFS_I(inode)->i_otime = inode->i_mtime;
chandan r9cc97d62012-07-04 12:48:07 +05305886
Chris Mason5f39d392007-10-15 16:14:19 -04005887 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
5888 struct btrfs_inode_item);
David Sterbab159fa22016-11-08 18:09:03 +01005889 memzero_extent_buffer(path->nodes[0], (unsigned long)inode_item,
Li Zefan293f7e02012-07-10 00:58:58 -06005890 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04005891 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05005892
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005893 if (name) {
5894 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
5895 struct btrfs_inode_ref);
5896 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
5897 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
5898 ptr = (unsigned long)(ref + 1);
5899 write_extent_buffer(path->nodes[0], name, ptr, name_len);
5900 }
Chris Mason9c583092008-01-29 15:15:18 -05005901
Chris Mason5f39d392007-10-15 16:14:19 -04005902 btrfs_mark_buffer_dirty(path->nodes[0]);
5903 btrfs_free_path(path);
5904
Christoph Hellwig6cbff002009-04-17 10:37:41 +02005905 btrfs_inherit_iflags(inode, dir);
5906
Al Viro569254b2011-07-24 17:08:40 -04005907 if (S_ISREG(mode)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005908 if (btrfs_test_opt(fs_info, NODATASUM))
Chris Mason94272162009-07-02 12:26:06 -04005909 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005910 if (btrfs_test_opt(fs_info, NODATACOW))
Josef Bacikf2bdf9a2013-02-21 15:28:28 -05005911 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
5912 BTRFS_INODE_NODATASUM;
Chris Mason94272162009-07-02 12:26:06 -04005913 }
5914
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005915 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00005916
5917 trace_btrfs_inode_new(inode);
Chris Mason1973f0f2011-06-24 13:13:29 -04005918 btrfs_set_inode_last_trans(trans, inode);
liubo1abe9b82011-03-24 11:18:59 +00005919
Alexander Block8ea05e32012-07-25 17:35:53 +02005920 btrfs_update_root_times(trans, root);
5921
Filipe David Borba Manana63541922014-01-07 11:47:46 +00005922 ret = btrfs_inode_inherit_props(trans, inode, dir);
5923 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005924 btrfs_err(fs_info,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00005925 "error inheriting props for ino %llu (root %llu): %d",
David Sterbaf85b7372017-01-20 14:54:07 +01005926 btrfs_ino(BTRFS_I(inode)), root->root_key.objectid, ret);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00005927
Chris Mason39279cc2007-06-12 06:35:45 -04005928 return inode;
Chris Masonb0d5d102014-09-08 13:08:51 -07005929
5930fail_unlock:
Al Viro32955c52018-05-16 12:20:05 -04005931 discard_new_inode(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005932fail:
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005933 if (dir && name)
Josef Bacikaec74772008-07-24 12:12:38 -04005934 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04005935 btrfs_free_path(path);
5936 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04005937}
5938
Chris Masond352ac62008-09-29 15:18:18 -04005939/*
5940 * utility function to add 'inode' into 'parent_inode' with
5941 * a give name and a given sequence number.
5942 * if 'add_backref' is true, also insert a backref from the
5943 * inode to the parent directory.
5944 */
Chris Masone02119d2008-09-05 16:13:11 -04005945int btrfs_add_link(struct btrfs_trans_handle *trans,
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02005946 struct btrfs_inode *parent_inode, struct btrfs_inode *inode,
Chris Masone02119d2008-09-05 16:13:11 -04005947 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04005948{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005949 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005950 struct btrfs_key key;
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02005951 struct btrfs_root *root = parent_inode->root;
5952 u64 ino = btrfs_ino(inode);
5953 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005954
Li Zefan33345d012011-04-20 10:31:50 +08005955 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02005956 memcpy(&key, &inode->root->root_key, sizeof(key));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005957 } else {
Li Zefan33345d012011-04-20 10:31:50 +08005958 key.objectid = ino;
David Sterba962a2982014-06-04 18:41:45 +02005959 key.type = BTRFS_INODE_ITEM_KEY;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005960 key.offset = 0;
5961 }
Chris Mason39279cc2007-06-12 06:35:45 -04005962
Li Zefan33345d012011-04-20 10:31:50 +08005963 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Lu Fengqi6025c192018-08-01 11:32:29 +08005964 ret = btrfs_add_root_ref(trans, key.objectid,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005965 root->root_key.objectid, parent_ino,
5966 index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005967 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08005968 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
5969 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005970 }
5971
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005972 /* Nothing to clean up yet */
5973 if (ret)
5974 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005975
Lu Fengqi684572d2018-08-04 21:10:57 +08005976 ret = btrfs_insert_dir_item(trans, name, name_len, parent_inode, &key,
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02005977 btrfs_inode_type(&inode->vfs_inode), index);
Chris Mason9c520572012-12-17 14:26:57 -05005978 if (ret == -EEXIST || ret == -EOVERFLOW)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005979 goto fail_dir_item;
5980 else if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04005981 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005982 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04005983 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005984
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02005985 btrfs_i_size_write(parent_inode, parent_inode->vfs_inode.i_size +
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005986 name_len * 2);
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02005987 inode_inc_iversion(&parent_inode->vfs_inode);
Filipe Manana5338e432019-05-15 16:02:47 +01005988 /*
5989 * If we are replaying a log tree, we do not want to update the mtime
5990 * and ctime of the parent directory with the current time, since the
5991 * log replay procedure is responsible for setting them to their correct
5992 * values (the ones it had when the fsync was done).
5993 */
5994 if (!test_bit(BTRFS_FS_LOG_RECOVERING, &root->fs_info->flags)) {
5995 struct timespec64 now = current_time(&parent_inode->vfs_inode);
5996
5997 parent_inode->vfs_inode.i_mtime = now;
5998 parent_inode->vfs_inode.i_ctime = now;
5999 }
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006000 ret = btrfs_update_inode(trans, root, &parent_inode->vfs_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006001 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04006002 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006003 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05006004
6005fail_dir_item:
6006 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
6007 u64 local_index;
6008 int err;
Lu Fengqi3ee1c552018-08-01 11:32:28 +08006009 err = btrfs_del_root_ref(trans, key.objectid,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006010 root->root_key.objectid, parent_ino,
6011 &local_index, name, name_len);
Johannes Thumshirn1690dd412018-12-12 15:14:17 +01006012 if (err)
6013 btrfs_abort_transaction(trans, err);
Chris Masonfe66a052012-02-20 08:40:56 -05006014 } else if (add_backref) {
6015 u64 local_index;
6016 int err;
6017
6018 err = btrfs_del_inode_ref(trans, root, name, name_len,
6019 ino, parent_ino, &local_index);
Johannes Thumshirn1690dd412018-12-12 15:14:17 +01006020 if (err)
6021 btrfs_abort_transaction(trans, err);
Chris Masonfe66a052012-02-20 08:40:56 -05006022 }
Johannes Thumshirn1690dd412018-12-12 15:14:17 +01006023
6024 /* Return the original error code */
Chris Masonfe66a052012-02-20 08:40:56 -05006025 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006026}
6027
6028static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006029 struct btrfs_inode *dir, struct dentry *dentry,
6030 struct btrfs_inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006031{
Josef Bacika1b075d2010-11-19 20:36:11 +00006032 int err = btrfs_add_link(trans, dir, inode,
6033 dentry->d_name.name, dentry->d_name.len,
6034 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006035 if (err > 0)
6036 err = -EEXIST;
6037 return err;
6038}
6039
Josef Bacik618e21d2007-07-11 10:18:17 -04006040static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04006041 umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04006042{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006043 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Josef Bacik618e21d2007-07-11 10:18:17 -04006044 struct btrfs_trans_handle *trans;
6045 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006046 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04006047 int err;
Josef Bacik618e21d2007-07-11 10:18:17 -04006048 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006049 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04006050
Josef Bacik9ed74f22009-09-11 16:12:44 -04006051 /*
6052 * 2 for inode item and ref
6053 * 2 for dir items
6054 * 1 for xattr if selinux is on
6055 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006056 trans = btrfs_start_transaction(root, 5);
6057 if (IS_ERR(trans))
6058 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05006059
Li Zefan581bb052011-04-20 10:06:11 +08006060 err = btrfs_find_free_ino(root, &objectid);
6061 if (err)
6062 goto out_unlock;
6063
Josef Bacikaec74772008-07-24 12:12:38 -04006064 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006065 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6066 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006067 if (IS_ERR(inode)) {
6068 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -04006069 inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04006070 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006071 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006072
Casey Schauflerad19db72011-12-15 10:09:07 -05006073 /*
6074 * If the active LSM wants to access the inode during
6075 * d_instantiate it needs these. Smack checks to see
6076 * if the filesystem supports xattrs by looking at the
6077 * ops vector.
6078 */
Casey Schauflerad19db72011-12-15 10:09:07 -05006079 inode->i_op = &btrfs_special_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006080 init_special_inode(inode, inode->i_mode, rdev);
6081
6082 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik618e21d2007-07-11 10:18:17 -04006083 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006084 goto out_unlock;
Chris Masonb0d5d102014-09-08 13:08:51 -07006085
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006086 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6087 0, index);
Al Viro32955c52018-05-16 12:20:05 -04006088 if (err)
6089 goto out_unlock;
6090
6091 btrfs_update_inode(trans, root, inode);
6092 d_instantiate_new(dentry, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006093
Josef Bacik618e21d2007-07-11 10:18:17 -04006094out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006095 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006096 btrfs_btree_balance_dirty(fs_info);
Al Viro32955c52018-05-16 12:20:05 -04006097 if (err && inode) {
Josef Bacik618e21d2007-07-11 10:18:17 -04006098 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -04006099 discard_new_inode(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006100 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006101 return err;
6102}
6103
Chris Mason39279cc2007-06-12 06:35:45 -04006104static int btrfs_create(struct inode *dir, struct dentry *dentry,
Al Viroebfc3b42012-06-10 18:05:36 -04006105 umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -04006106{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006107 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04006108 struct btrfs_trans_handle *trans;
6109 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006110 struct inode *inode = NULL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006111 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04006112 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006113 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006114
Josef Bacik9ed74f22009-09-11 16:12:44 -04006115 /*
6116 * 2 for inode item and ref
6117 * 2 for dir items
6118 * 1 for xattr if selinux is on
6119 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006120 trans = btrfs_start_transaction(root, 5);
6121 if (IS_ERR(trans))
6122 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006123
Li Zefan581bb052011-04-20 10:06:11 +08006124 err = btrfs_find_free_ino(root, &objectid);
6125 if (err)
6126 goto out_unlock;
6127
Josef Bacikaec74772008-07-24 12:12:38 -04006128 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006129 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6130 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006131 if (IS_ERR(inode)) {
6132 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -04006133 inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006134 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006135 }
Casey Schauflerad19db72011-12-15 10:09:07 -05006136 /*
6137 * If the active LSM wants to access the inode during
6138 * d_instantiate it needs these. Smack checks to see
6139 * if the filesystem supports xattrs by looking at the
6140 * ops vector.
6141 */
6142 inode->i_fop = &btrfs_file_operations;
6143 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006144 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006145
6146 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
6147 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006148 goto out_unlock;
Chris Masonb0d5d102014-09-08 13:08:51 -07006149
6150 err = btrfs_update_inode(trans, root, inode);
6151 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006152 goto out_unlock;
Casey Schauflerad19db72011-12-15 10:09:07 -05006153
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006154 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6155 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006156 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006157 goto out_unlock;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006158
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006159 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Al Viro1e2e5472018-05-04 08:23:01 -04006160 d_instantiate_new(dentry, inode);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006161
Chris Mason39279cc2007-06-12 06:35:45 -04006162out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006163 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -04006164 if (err && inode) {
Chris Mason39279cc2007-06-12 06:35:45 -04006165 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -04006166 discard_new_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006167 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006168 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006169 return err;
6170}
6171
6172static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
6173 struct dentry *dentry)
6174{
Filipe Manana271dba452016-01-05 16:24:05 +00006175 struct btrfs_trans_handle *trans = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006176 struct btrfs_root *root = BTRFS_I(dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00006177 struct inode *inode = d_inode(old_dentry);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006178 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason00e4e6b2008-08-05 11:18:09 -04006179 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04006180 int err;
6181 int drop_inode = 0;
6182
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006183 /* do not allow sys_link's with other subvols of the same device */
Misono Tomohiro4fd786e2018-08-06 14:25:24 +09006184 if (root->root_key.objectid != BTRFS_I(inode)->root->root_key.objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00006185 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006186
Mark Fashehf1863732012-08-08 11:32:27 -07006187 if (inode->i_nlink >= BTRFS_LINK_MAX)
Al Viroc055e992011-03-04 17:15:18 +00006188 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04006189
Nikolay Borisov877574e2017-02-20 13:50:33 +02006190 err = btrfs_set_inode_index(BTRFS_I(dir), &index);
Josef Bacikaec74772008-07-24 12:12:38 -04006191 if (err)
6192 goto fail;
6193
Yan, Zhenga22285a2010-05-16 10:48:46 -04006194 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00006195 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04006196 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00006197 * 1 item for parent inode
Omar Sandoval399b0bb2018-05-11 13:13:40 -07006198 * 1 item for orphan item deletion if O_TMPFILE
Yan, Zhenga22285a2010-05-16 10:48:46 -04006199 */
Omar Sandoval399b0bb2018-05-11 13:13:40 -07006200 trans = btrfs_start_transaction(root, inode->i_nlink ? 5 : 6);
Yan, Zhenga22285a2010-05-16 10:48:46 -04006201 if (IS_ERR(trans)) {
6202 err = PTR_ERR(trans);
Filipe Manana271dba452016-01-05 16:24:05 +00006203 trans = NULL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006204 goto fail;
6205 }
Chris Mason5f39d392007-10-15 16:14:19 -04006206
Miao Xie67de1172013-12-26 13:07:06 +08006207 /* There are several dir indexes for this inode, clear the cache. */
6208 BTRFS_I(inode)->dir_index = 0ULL;
Zach Brown8b558c52013-10-16 12:10:34 -07006209 inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04006210 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006211 inode->i_ctime = current_time(inode);
Al Viro7de9c6ee2010-10-23 11:11:40 -04006212 ihold(inode);
Josef Bacike9976152012-10-11 15:53:56 -04006213 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
Josef Bacikaec74772008-07-24 12:12:38 -04006214
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006215 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6216 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04006217
Yan, Zhenga5719522009-09-24 09:17:31 -04006218 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006219 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04006220 } else {
Al Viro10d9f302011-07-16 23:09:10 -04006221 struct dentry *parent = dentry->d_parent;
Filipe Mananad4682ba2018-06-11 19:24:28 +01006222 int ret;
6223
Yan, Zhenga5719522009-09-24 09:17:31 -04006224 err = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006225 if (err)
6226 goto fail;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006227 if (inode->i_nlink == 1) {
6228 /*
6229 * If new hard link count is 1, it's a file created
6230 * with open(2) O_TMPFILE flag.
6231 */
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02006232 err = btrfs_orphan_del(trans, BTRFS_I(inode));
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006233 if (err)
6234 goto fail;
6235 }
Al Viro08c422c2011-12-23 07:58:13 -05006236 d_instantiate(dentry, inode);
Filipe Mananad4682ba2018-06-11 19:24:28 +01006237 ret = btrfs_log_new_name(trans, BTRFS_I(inode), NULL, parent,
6238 true, NULL);
6239 if (ret == BTRFS_NEED_TRANS_COMMIT) {
6240 err = btrfs_commit_transaction(trans);
6241 trans = NULL;
6242 }
Yan, Zhenga5719522009-09-24 09:17:31 -04006243 }
Chris Mason39279cc2007-06-12 06:35:45 -04006244
Chris Mason1832a6d2007-12-21 16:27:21 -05006245fail:
Filipe Manana271dba452016-01-05 16:24:05 +00006246 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006247 btrfs_end_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006248 if (drop_inode) {
6249 inode_dec_link_count(inode);
6250 iput(inode);
6251 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006252 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006253 return err;
6254}
6255
Al Viro18bb1db2011-07-26 01:41:39 -04006256static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04006257{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006258 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masonb9d86662008-05-02 16:13:49 -04006259 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006260 struct btrfs_trans_handle *trans;
6261 struct btrfs_root *root = BTRFS_I(dir)->root;
6262 int err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04006263 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006264 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006265
Josef Bacik9ed74f22009-09-11 16:12:44 -04006266 /*
6267 * 2 items for inode and ref
6268 * 2 items for dir items
6269 * 1 for xattr if selinux is on
6270 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006271 trans = btrfs_start_transaction(root, 5);
6272 if (IS_ERR(trans))
6273 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006274
Li Zefan581bb052011-04-20 10:06:11 +08006275 err = btrfs_find_free_ino(root, &objectid);
6276 if (err)
6277 goto out_fail;
6278
Josef Bacikaec74772008-07-24 12:12:38 -04006279 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006280 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6281 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04006282 if (IS_ERR(inode)) {
6283 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -04006284 inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006285 goto out_fail;
6286 }
Chris Mason5f39d392007-10-15 16:14:19 -04006287
Chris Masonb0d5d102014-09-08 13:08:51 -07006288 /* these must be set before we unlock the inode */
6289 inode->i_op = &btrfs_dir_inode_operations;
6290 inode->i_fop = &btrfs_dir_file_operations;
Josef Bacik33268ea2008-07-24 12:16:36 -04006291
Eric Paris2a7dba32011-02-01 11:05:39 -05006292 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04006293 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006294 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04006295
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02006296 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Mason39279cc2007-06-12 06:35:45 -04006297 err = btrfs_update_inode(trans, root, inode);
6298 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006299 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04006300
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006301 err = btrfs_add_link(trans, BTRFS_I(dir), BTRFS_I(inode),
6302 dentry->d_name.name,
6303 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006304 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006305 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04006306
Al Viro1e2e5472018-05-04 08:23:01 -04006307 d_instantiate_new(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006308
6309out_fail:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006310 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -04006311 if (err && inode) {
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006312 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -04006313 discard_new_inode(inode);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006314 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006315 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006316 return err;
6317}
6318
Chris Masonc8b97812008-10-29 14:49:59 -04006319static noinline int uncompress_inline(struct btrfs_path *path,
Byongho Leee40da0e2015-05-19 23:46:45 +09006320 struct page *page,
Chris Masonc8b97812008-10-29 14:49:59 -04006321 size_t pg_offset, u64 extent_offset,
6322 struct btrfs_file_extent_item *item)
6323{
6324 int ret;
6325 struct extent_buffer *leaf = path->nodes[0];
6326 char *tmp;
6327 size_t max_size;
6328 unsigned long inline_size;
6329 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08006330 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04006331
6332 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08006333 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04006334 max_size = btrfs_file_extent_ram_bytes(leaf, item);
6335 inline_size = btrfs_file_extent_inline_item_len(leaf,
Ross Kirkdd3cc162013-09-16 15:58:09 +01006336 btrfs_item_nr(path->slots[0]));
Chris Masonc8b97812008-10-29 14:49:59 -04006337 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04006338 if (!tmp)
6339 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04006340 ptr = btrfs_file_extent_inline_start(item);
6341
6342 read_extent_buffer(leaf, tmp, ptr, inline_size);
6343
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006344 max_size = min_t(unsigned long, PAGE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08006345 ret = btrfs_decompress(compress_type, tmp, page,
6346 extent_offset, inline_size, max_size);
Zygo Blaxelle1699d22017-03-10 16:45:44 -05006347
6348 /*
6349 * decompression code contains a memset to fill in any space between the end
6350 * of the uncompressed data and the end of max_size in case the decompressed
6351 * data ends up shorter than ram_bytes. That doesn't cover the hole between
6352 * the end of an inline extent and the beginning of the next block, so we
6353 * cover that region here.
6354 */
6355
6356 if (max_size + pg_offset < PAGE_SIZE) {
6357 char *map = kmap(page);
6358 memset(map + pg_offset + max_size, 0, PAGE_SIZE - max_size - pg_offset);
6359 kunmap(page);
6360 }
Chris Masonc8b97812008-10-29 14:49:59 -04006361 kfree(tmp);
Zach Brown166ae5a2014-05-09 17:15:10 -04006362 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -04006363}
6364
Omar Sandoval39b07b52019-12-02 17:34:23 -08006365/**
6366 * btrfs_get_extent - Lookup the first extent overlapping a range in a file.
6367 * @inode: file to search in
6368 * @page: page to read extent data into if the extent is inline
6369 * @pg_offset: offset into @page to copy to
6370 * @start: file offset
6371 * @len: length of range starting at @start
Chris Masond352ac62008-09-29 15:18:18 -04006372 *
Omar Sandoval39b07b52019-12-02 17:34:23 -08006373 * This returns the first &struct extent_map which overlaps with the given
6374 * range, reading it from the B-tree and caching it if necessary. Note that
6375 * there may be more extents which overlap the given range after the returned
6376 * extent_map.
6377 *
6378 * If @page is not NULL and the extent is inline, this also reads the extent
6379 * data directly into the page and marks the extent up to date in the io_tree.
6380 *
6381 * Return: ERR_PTR on error, non-NULL extent_map on success.
Chris Masond352ac62008-09-29 15:18:18 -04006382 */
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006383struct extent_map *btrfs_get_extent(struct btrfs_inode *inode,
Omar Sandoval39b07b52019-12-02 17:34:23 -08006384 struct page *page, size_t pg_offset,
6385 u64 start, u64 len)
Chris Masona52d9a82007-08-27 16:49:44 -04006386{
David Sterba3ffbd682018-06-29 10:56:42 +02006387 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Chris Masona52d9a82007-08-27 16:49:44 -04006388 int ret;
6389 int err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006390 u64 extent_start = 0;
6391 u64 extent_end = 0;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006392 u64 objectid = btrfs_ino(inode);
Linus Torvalds7e74e232019-05-01 12:19:20 -07006393 int extent_type = -1;
Chris Masonf4219502008-07-22 11:18:09 -04006394 struct btrfs_path *path = NULL;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006395 struct btrfs_root *root = inode->root;
Chris Masona52d9a82007-08-27 16:49:44 -04006396 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04006397 struct extent_buffer *leaf;
6398 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04006399 struct extent_map *em = NULL;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006400 struct extent_map_tree *em_tree = &inode->extent_tree;
6401 struct extent_io_tree *io_tree = &inode->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04006402
Chris Mason890871b2009-09-02 16:24:52 -04006403 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006404 em = lookup_extent_mapping(em_tree, start, len);
Chris Mason890871b2009-09-02 16:24:52 -04006405 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006406
Chris Masona52d9a82007-08-27 16:49:44 -04006407 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04006408 if (em->start > start || em->start + em->len <= start)
6409 free_extent_map(em);
6410 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05006411 free_extent_map(em);
6412 else
6413 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006414 }
David Sterba172ddd62011-04-21 00:48:27 +02006415 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04006416 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05006417 err = -ENOMEM;
6418 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006419 }
Chris Masond1310b22008-01-24 16:13:08 -05006420 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05006421 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05006422 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04006423 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04006424
Liu Bobee6ec82018-08-17 05:05:28 +08006425 path = btrfs_alloc_path();
Chris Masonf4219502008-07-22 11:18:09 -04006426 if (!path) {
Liu Bobee6ec82018-08-17 05:05:28 +08006427 err = -ENOMEM;
6428 goto out;
Chris Masonf4219502008-07-22 11:18:09 -04006429 }
6430
Liu Bobee6ec82018-08-17 05:05:28 +08006431 /* Chances are we'll be called again, so go ahead and do readahead */
6432 path->reada = READA_FORWARD;
6433
Liu Boe49aabd2018-08-25 13:47:09 +08006434 /*
6435 * Unless we're going to uncompress the inline extent, no sleep would
6436 * happen.
6437 */
6438 path->leave_spinning = 1;
6439
Nikolay Borisov5c9a7022017-12-01 11:19:40 +02006440 ret = btrfs_lookup_file_extent(NULL, root, path, objectid, start, 0);
Chris Masona52d9a82007-08-27 16:49:44 -04006441 if (ret < 0) {
6442 err = ret;
6443 goto out;
Nikolay Borisovb8eeab72018-12-17 11:49:00 +02006444 } else if (ret > 0) {
Chris Masona52d9a82007-08-27 16:49:44 -04006445 if (path->slots[0] == 0)
6446 goto not_found;
6447 path->slots[0]--;
6448 }
6449
Chris Mason5f39d392007-10-15 16:14:19 -04006450 leaf = path->nodes[0];
6451 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04006452 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04006453 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04006454 if (found_key.objectid != objectid ||
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006455 found_key.type != BTRFS_EXTENT_DATA_KEY) {
Josef Bacik25a50342013-10-14 12:08:38 -04006456 /*
6457 * If we backup past the first extent we want to move forward
6458 * and see if there is an extent in front of us, otherwise we'll
6459 * say there is a hole for our whole search range which can
6460 * cause problems.
6461 */
6462 extent_end = start;
6463 goto next;
Chris Masona52d9a82007-08-27 16:49:44 -04006464 }
6465
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006466 extent_type = btrfs_file_extent_type(leaf, item);
Chris Mason5f39d392007-10-15 16:14:19 -04006467 extent_start = found_key.offset;
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006468 if (extent_type == BTRFS_FILE_EXTENT_REG ||
6469 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08006470 /* Only regular file could have regular/prealloc extent */
6471 if (!S_ISREG(inode->vfs_inode.i_mode)) {
6472 ret = -EUCLEAN;
6473 btrfs_crit(fs_info,
6474 "regular/prealloc extent found for non-regular inode %llu",
6475 btrfs_ino(inode));
6476 goto out;
6477 }
Chris Masona52d9a82007-08-27 16:49:44 -04006478 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04006479 btrfs_file_extent_num_bytes(leaf, item);
Liu Bo09ed2f12017-03-10 11:09:48 -08006480
6481 trace_btrfs_get_extent_show_fi_regular(inode, leaf, item,
6482 extent_start);
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006483 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Yan Zheng9036c102008-10-30 14:19:41 -04006484 size_t size;
Qu Wenruoe41ca582018-06-06 15:41:49 +08006485
6486 size = btrfs_file_extent_ram_bytes(leaf, item);
Jeff Mahoneyda170662016-06-15 09:22:56 -04006487 extent_end = ALIGN(extent_start + size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006488 fs_info->sectorsize);
Liu Bo09ed2f12017-03-10 11:09:48 -08006489
6490 trace_btrfs_get_extent_show_fi_inline(inode, leaf, item,
6491 path->slots[0],
6492 extent_start);
Yan Zheng9036c102008-10-30 14:19:41 -04006493 }
Josef Bacik25a50342013-10-14 12:08:38 -04006494next:
Yan Zheng9036c102008-10-30 14:19:41 -04006495 if (start >= extent_end) {
6496 path->slots[0]++;
6497 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
6498 ret = btrfs_next_leaf(root, path);
6499 if (ret < 0) {
6500 err = ret;
6501 goto out;
Nikolay Borisovb8eeab72018-12-17 11:49:00 +02006502 } else if (ret > 0) {
Yan Zheng9036c102008-10-30 14:19:41 -04006503 goto not_found;
Nikolay Borisovb8eeab72018-12-17 11:49:00 +02006504 }
Yan Zheng9036c102008-10-30 14:19:41 -04006505 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04006506 }
Yan Zheng9036c102008-10-30 14:19:41 -04006507 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6508 if (found_key.objectid != objectid ||
6509 found_key.type != BTRFS_EXTENT_DATA_KEY)
6510 goto not_found;
6511 if (start + len <= found_key.offset)
6512 goto not_found;
Wang Shilonge2eca692014-07-17 11:44:14 +08006513 if (start > found_key.offset)
6514 goto next;
Nikolay Borisov02a033d2018-12-17 10:36:02 +02006515
6516 /* New extent overlaps with existing one */
Yan Zheng9036c102008-10-30 14:19:41 -04006517 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006518 em->orig_start = start;
Yan Zheng9036c102008-10-30 14:19:41 -04006519 em->len = found_key.offset - start;
Nikolay Borisov02a033d2018-12-17 10:36:02 +02006520 em->block_start = EXTENT_MAP_HOLE;
6521 goto insert;
Yan Zheng9036c102008-10-30 14:19:41 -04006522 }
6523
Omar Sandoval39b07b52019-12-02 17:34:23 -08006524 btrfs_extent_item_to_extent_map(inode, path, item, !page, em);
Filipe Manana7ffbb592014-06-09 03:48:05 +01006525
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006526 if (extent_type == BTRFS_FILE_EXTENT_REG ||
6527 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006528 goto insert;
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006529 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04006530 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04006531 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04006532 size_t size;
6533 size_t extent_offset;
6534 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04006535
Omar Sandoval39b07b52019-12-02 17:34:23 -08006536 if (!page)
Yan689f9342007-10-29 11:41:07 -04006537 goto out;
Yan689f9342007-10-29 11:41:07 -04006538
Qu Wenruoe41ca582018-06-06 15:41:49 +08006539 size = btrfs_file_extent_ram_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04006540 extent_offset = page_offset(page) + pg_offset - extent_start;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006541 copy_size = min_t(u64, PAGE_SIZE - pg_offset,
6542 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04006543 em->start = extent_start + extent_offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006544 em->len = ALIGN(copy_size, fs_info->sectorsize);
Josef Bacikb4939682012-12-03 10:31:19 -05006545 em->orig_block_len = em->len;
Josef Bacik70c8a912012-10-11 16:54:30 -04006546 em->orig_start = em->start;
Yan689f9342007-10-29 11:41:07 -04006547 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Liu Boe49aabd2018-08-25 13:47:09 +08006548
6549 btrfs_set_path_blocking(path);
Edmund Nadolskibf46f522017-11-20 13:24:49 -07006550 if (!PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08006551 if (btrfs_file_extent_compression(leaf, item) !=
6552 BTRFS_COMPRESS_NONE) {
Byongho Leee40da0e2015-05-19 23:46:45 +09006553 ret = uncompress_inline(path, page, pg_offset,
Chris Masonc8b97812008-10-29 14:49:59 -04006554 extent_offset, item);
Zach Brown166ae5a2014-05-09 17:15:10 -04006555 if (ret) {
6556 err = ret;
6557 goto out;
6558 }
Chris Masonc8b97812008-10-29 14:49:59 -04006559 } else {
6560 map = kmap(page);
6561 read_extent_buffer(leaf, map + pg_offset, ptr,
6562 copy_size);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006563 if (pg_offset + copy_size < PAGE_SIZE) {
Chris Mason93c82d52009-09-11 12:36:29 -04006564 memset(map + pg_offset + copy_size, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006565 PAGE_SIZE - pg_offset -
Chris Mason93c82d52009-09-11 12:36:29 -04006566 copy_size);
6567 }
Chris Masonc8b97812008-10-29 14:49:59 -04006568 kunmap(page);
6569 }
Chris Mason179e29e2007-11-01 11:28:41 -04006570 flush_dcache_page(page);
Chris Masona52d9a82007-08-27 16:49:44 -04006571 }
Chris Masond1310b22008-01-24 16:13:08 -05006572 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00006573 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04006574 goto insert;
Chris Masona52d9a82007-08-27 16:49:44 -04006575 }
6576not_found:
6577 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006578 em->orig_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05006579 em->len = len;
Chris Mason5f39d392007-10-15 16:14:19 -04006580 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04006581insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02006582 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05006583 if (em->start > start || extent_map_end(em) <= start) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006584 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04006585 "bad extent! em: [%llu %llu] passed [%llu %llu]",
6586 em->start, em->len, start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04006587 err = -EIO;
6588 goto out;
6589 }
Chris Masond1310b22008-01-24 16:13:08 -05006590
6591 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04006592 write_lock(&em_tree->lock);
David Sterbaf46b24c2018-04-03 21:45:57 +02006593 err = btrfs_add_extent_mapping(fs_info, em_tree, &em, start, len);
Chris Mason890871b2009-09-02 16:24:52 -04006594 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04006595out:
Liu Boc6414282018-08-23 07:36:17 +08006596 btrfs_free_path(path);
liubo1abe9b82011-03-24 11:18:59 +00006597
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006598 trace_btrfs_get_extent(root, inode, em);
liubo1abe9b82011-03-24 11:18:59 +00006599
Chris Masona52d9a82007-08-27 16:49:44 -04006600 if (err) {
6601 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04006602 return ERR_PTR(err);
6603 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006604 BUG_ON(!em); /* Error is always set */
Chris Masona52d9a82007-08-27 16:49:44 -04006605 return em;
6606}
6607
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006608struct extent_map *btrfs_get_extent_fiemap(struct btrfs_inode *inode,
Nikolay Borisov4ab47a82018-12-12 09:42:32 +02006609 u64 start, u64 len)
Chris Masonec29ed52011-02-23 16:23:20 -05006610{
6611 struct extent_map *em;
6612 struct extent_map *hole_em = NULL;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006613 u64 delalloc_start = start;
Chris Masonec29ed52011-02-23 16:23:20 -05006614 u64 end;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006615 u64 delalloc_len;
6616 u64 delalloc_end;
Chris Masonec29ed52011-02-23 16:23:20 -05006617 int err = 0;
6618
Omar Sandoval39b07b52019-12-02 17:34:23 -08006619 em = btrfs_get_extent(inode, NULL, 0, start, len);
Chris Masonec29ed52011-02-23 16:23:20 -05006620 if (IS_ERR(em))
6621 return em;
Dan Carpenter99862772017-04-11 11:57:15 +03006622 /*
6623 * If our em maps to:
6624 * - a hole or
6625 * - a pre-alloc extent,
6626 * there might actually be delalloc bytes behind it.
6627 */
6628 if (em->block_start != EXTENT_MAP_HOLE &&
6629 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
6630 return em;
6631 else
6632 hole_em = em;
Chris Masonec29ed52011-02-23 16:23:20 -05006633
6634 /* check to see if we've wrapped (len == -1 or similar) */
6635 end = start + len;
6636 if (end < start)
6637 end = (u64)-1;
6638 else
6639 end -= 1;
6640
6641 em = NULL;
6642
6643 /* ok, we didn't find anything, lets look for delalloc */
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006644 delalloc_len = count_range_bits(&inode->io_tree, &delalloc_start,
Chris Masonec29ed52011-02-23 16:23:20 -05006645 end, len, EXTENT_DELALLOC, 1);
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006646 delalloc_end = delalloc_start + delalloc_len;
6647 if (delalloc_end < delalloc_start)
6648 delalloc_end = (u64)-1;
Chris Masonec29ed52011-02-23 16:23:20 -05006649
6650 /*
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006651 * We didn't find anything useful, return the original results from
6652 * get_extent()
Chris Masonec29ed52011-02-23 16:23:20 -05006653 */
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006654 if (delalloc_start > end || delalloc_end <= start) {
Chris Masonec29ed52011-02-23 16:23:20 -05006655 em = hole_em;
6656 hole_em = NULL;
6657 goto out;
6658 }
6659
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006660 /*
6661 * Adjust the delalloc_start to make sure it doesn't go backwards from
6662 * the start they passed in
Chris Masonec29ed52011-02-23 16:23:20 -05006663 */
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006664 delalloc_start = max(start, delalloc_start);
6665 delalloc_len = delalloc_end - delalloc_start;
Chris Masonec29ed52011-02-23 16:23:20 -05006666
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006667 if (delalloc_len > 0) {
6668 u64 hole_start;
Nikolay Borisov02950af2018-12-12 09:42:34 +02006669 u64 hole_len;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006670 const u64 hole_end = extent_map_end(hole_em);
Chris Masonec29ed52011-02-23 16:23:20 -05006671
David Sterba172ddd62011-04-21 00:48:27 +02006672 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05006673 if (!em) {
6674 err = -ENOMEM;
6675 goto out;
6676 }
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006677
6678 ASSERT(hole_em);
6679 /*
6680 * When btrfs_get_extent can't find anything it returns one
6681 * huge hole
6682 *
6683 * Make sure what it found really fits our range, and adjust to
6684 * make sure it is based on the start from the caller
6685 */
6686 if (hole_end <= start || hole_em->start > end) {
6687 free_extent_map(hole_em);
6688 hole_em = NULL;
6689 } else {
6690 hole_start = max(hole_em->start, start);
6691 hole_len = hole_end - hole_start;
6692 }
6693
6694 if (hole_em && delalloc_start > hole_start) {
6695 /*
6696 * Our hole starts before our delalloc, so we have to
6697 * return just the parts of the hole that go until the
6698 * delalloc starts
Chris Masonec29ed52011-02-23 16:23:20 -05006699 */
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006700 em->len = min(hole_len, delalloc_start - hole_start);
Chris Masonec29ed52011-02-23 16:23:20 -05006701 em->start = hole_start;
6702 em->orig_start = hole_start;
6703 /*
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006704 * Don't adjust block start at all, it is fixed at
6705 * EXTENT_MAP_HOLE
Chris Masonec29ed52011-02-23 16:23:20 -05006706 */
6707 em->block_start = hole_em->block_start;
6708 em->block_len = hole_len;
Liu Bof9e4fb52013-01-07 10:10:12 +00006709 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
6710 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonec29ed52011-02-23 16:23:20 -05006711 } else {
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006712 /*
6713 * Hole is out of passed range or it starts after
6714 * delalloc range
6715 */
6716 em->start = delalloc_start;
6717 em->len = delalloc_len;
6718 em->orig_start = delalloc_start;
Chris Masonec29ed52011-02-23 16:23:20 -05006719 em->block_start = EXTENT_MAP_DELALLOC;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006720 em->block_len = delalloc_len;
Chris Masonec29ed52011-02-23 16:23:20 -05006721 }
Nikolay Borisovbf8d32b2017-12-01 11:19:43 +02006722 } else {
Chris Masonec29ed52011-02-23 16:23:20 -05006723 return hole_em;
6724 }
6725out:
6726
6727 free_extent_map(hole_em);
6728 if (err) {
6729 free_extent_map(em);
6730 return ERR_PTR(err);
6731 }
6732 return em;
6733}
6734
Filipe Manana5f9a8a52016-05-12 13:53:36 +01006735static struct extent_map *btrfs_create_dio_extent(struct inode *inode,
6736 const u64 start,
6737 const u64 len,
6738 const u64 orig_start,
6739 const u64 block_start,
6740 const u64 block_len,
6741 const u64 orig_block_len,
6742 const u64 ram_bytes,
6743 const int type)
6744{
6745 struct extent_map *em = NULL;
6746 int ret;
6747
Filipe Manana5f9a8a52016-05-12 13:53:36 +01006748 if (type != BTRFS_ORDERED_NOCOW) {
Liu Bo6f9994d2017-01-31 07:50:22 -08006749 em = create_io_em(inode, start, len, orig_start,
6750 block_start, block_len, orig_block_len,
6751 ram_bytes,
6752 BTRFS_COMPRESS_NONE, /* compress_type */
6753 type);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01006754 if (IS_ERR(em))
6755 goto out;
6756 }
6757 ret = btrfs_add_ordered_extent_dio(inode, start, block_start,
6758 len, block_len, type);
6759 if (ret) {
6760 if (em) {
6761 free_extent_map(em);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02006762 btrfs_drop_extent_cache(BTRFS_I(inode), start,
Filipe Manana5f9a8a52016-05-12 13:53:36 +01006763 start + len - 1, 0);
6764 }
6765 em = ERR_PTR(ret);
6766 }
6767 out:
Filipe Manana5f9a8a52016-05-12 13:53:36 +01006768
6769 return em;
6770}
6771
Josef Bacik4b46fce2010-05-23 11:00:55 -04006772static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
6773 u64 start, u64 len)
6774{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006775 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006776 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik70c8a912012-10-11 16:54:30 -04006777 struct extent_map *em;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006778 struct btrfs_key ins;
6779 u64 alloc_hint;
6780 int ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006781
Josef Bacik4b46fce2010-05-23 11:00:55 -04006782 alloc_hint = get_extent_allocation_hint(inode, start, len);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006783 ret = btrfs_reserve_extent(root, len, len, fs_info->sectorsize,
Jeff Mahoneyda170662016-06-15 09:22:56 -04006784 0, alloc_hint, &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04006785 if (ret)
6786 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006787
Filipe Manana5f9a8a52016-05-12 13:53:36 +01006788 em = btrfs_create_dio_extent(inode, start, ins.offset, start,
6789 ins.objectid, ins.offset, ins.offset,
Liu Bo6288d6e2017-02-21 12:12:58 -08006790 ins.offset, BTRFS_ORDERED_REGULAR);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006791 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01006792 if (IS_ERR(em))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006793 btrfs_free_reserved_extent(fs_info, ins.objectid,
6794 ins.offset, 1);
Josef Bacik00361582013-08-14 14:02:47 -04006795
Josef Bacik4b46fce2010-05-23 11:00:55 -04006796 return em;
6797}
6798
Chris Mason46bfbb52010-05-26 11:04:10 -04006799/*
6800 * returns 1 when the nocow is safe, < 1 on error, 0 if the
6801 * block must be cow'd
6802 */
Josef Bacik00361582013-08-14 14:02:47 -04006803noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
Josef Bacik7ee9e442013-06-21 16:37:03 -04006804 u64 *orig_start, u64 *orig_block_len,
6805 u64 *ram_bytes)
Chris Mason46bfbb52010-05-26 11:04:10 -04006806{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006807 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason46bfbb52010-05-26 11:04:10 -04006808 struct btrfs_path *path;
6809 int ret;
6810 struct extent_buffer *leaf;
6811 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xie7b2b7082014-02-27 13:58:05 +08006812 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason46bfbb52010-05-26 11:04:10 -04006813 struct btrfs_file_extent_item *fi;
6814 struct btrfs_key key;
6815 u64 disk_bytenr;
6816 u64 backref_offset;
6817 u64 extent_end;
6818 u64 num_bytes;
6819 int slot;
6820 int found_type;
Josef Bacik7ee9e442013-06-21 16:37:03 -04006821 bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW);
Miao Xiee77751a2013-12-27 21:11:50 +08006822
Chris Mason46bfbb52010-05-26 11:04:10 -04006823 path = btrfs_alloc_path();
6824 if (!path)
6825 return -ENOMEM;
6826
David Sterbaf85b7372017-01-20 14:54:07 +01006827 ret = btrfs_lookup_file_extent(NULL, root, path,
6828 btrfs_ino(BTRFS_I(inode)), offset, 0);
Chris Mason46bfbb52010-05-26 11:04:10 -04006829 if (ret < 0)
6830 goto out;
6831
6832 slot = path->slots[0];
6833 if (ret == 1) {
6834 if (slot == 0) {
6835 /* can't find the item, must cow */
6836 ret = 0;
6837 goto out;
6838 }
6839 slot--;
6840 }
6841 ret = 0;
6842 leaf = path->nodes[0];
6843 btrfs_item_key_to_cpu(leaf, &key, slot);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02006844 if (key.objectid != btrfs_ino(BTRFS_I(inode)) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04006845 key.type != BTRFS_EXTENT_DATA_KEY) {
6846 /* not our file or wrong item type, must cow */
6847 goto out;
6848 }
6849
6850 if (key.offset > offset) {
6851 /* Wrong offset, must cow */
6852 goto out;
6853 }
6854
6855 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
6856 found_type = btrfs_file_extent_type(leaf, fi);
6857 if (found_type != BTRFS_FILE_EXTENT_REG &&
6858 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
6859 /* not a regular extent, must cow */
6860 goto out;
6861 }
Josef Bacik7ee9e442013-06-21 16:37:03 -04006862
6863 if (!nocow && found_type == BTRFS_FILE_EXTENT_REG)
6864 goto out;
6865
Miao Xiee77751a2013-12-27 21:11:50 +08006866 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
6867 if (extent_end <= offset)
6868 goto out;
6869
Chris Mason46bfbb52010-05-26 11:04:10 -04006870 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Josef Bacik7ee9e442013-06-21 16:37:03 -04006871 if (disk_bytenr == 0)
6872 goto out;
6873
6874 if (btrfs_file_extent_compression(leaf, fi) ||
6875 btrfs_file_extent_encryption(leaf, fi) ||
6876 btrfs_file_extent_other_encoding(leaf, fi))
6877 goto out;
6878
Ethan Lien78d42952018-05-17 14:58:29 +08006879 /*
6880 * Do the same check as in btrfs_cross_ref_exist but without the
6881 * unnecessary search.
6882 */
6883 if (btrfs_file_extent_generation(leaf, fi) <=
6884 btrfs_root_last_snapshot(&root->root_item))
6885 goto out;
6886
Chris Mason46bfbb52010-05-26 11:04:10 -04006887 backref_offset = btrfs_file_extent_offset(leaf, fi);
6888
Josef Bacik7ee9e442013-06-21 16:37:03 -04006889 if (orig_start) {
6890 *orig_start = key.offset - backref_offset;
6891 *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
6892 *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
6893 }
Josef Bacikeb384b52013-04-24 16:32:55 -04006894
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006895 if (btrfs_extent_readonly(fs_info, disk_bytenr))
Chris Mason46bfbb52010-05-26 11:04:10 -04006896 goto out;
Miao Xie7b2b7082014-02-27 13:58:05 +08006897
6898 num_bytes = min(offset + *len, extent_end) - offset;
6899 if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) {
6900 u64 range_end;
6901
Jeff Mahoneyda170662016-06-15 09:22:56 -04006902 range_end = round_up(offset + num_bytes,
6903 root->fs_info->sectorsize) - 1;
Miao Xie7b2b7082014-02-27 13:58:05 +08006904 ret = test_range_bit(io_tree, offset, range_end,
6905 EXTENT_DELALLOC, 0, NULL);
6906 if (ret) {
6907 ret = -EAGAIN;
6908 goto out;
6909 }
6910 }
6911
Josef Bacik1bda19e2013-10-18 12:10:36 -04006912 btrfs_release_path(path);
Chris Mason46bfbb52010-05-26 11:04:10 -04006913
6914 /*
6915 * look for other files referencing this extent, if we
6916 * find any we must cow
6917 */
Josef Bacik00361582013-08-14 14:02:47 -04006918
Liu Boe4c3b2d2017-01-30 12:25:28 -08006919 ret = btrfs_cross_ref_exist(root, btrfs_ino(BTRFS_I(inode)),
Josef Bacik00361582013-08-14 14:02:47 -04006920 key.offset - backref_offset, disk_bytenr);
Josef Bacik00361582013-08-14 14:02:47 -04006921 if (ret) {
6922 ret = 0;
6923 goto out;
6924 }
Chris Mason46bfbb52010-05-26 11:04:10 -04006925
6926 /*
6927 * adjust disk_bytenr and num_bytes to cover just the bytes
6928 * in this extent we are about to write. If there
6929 * are any csums in that range we have to cow in order
6930 * to keep the csums correct
6931 */
6932 disk_bytenr += backref_offset;
6933 disk_bytenr += offset - key.offset;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006934 if (csum_exist_in_range(fs_info, disk_bytenr, num_bytes))
6935 goto out;
Chris Mason46bfbb52010-05-26 11:04:10 -04006936 /*
6937 * all of the above have passed, it is safe to overwrite this extent
6938 * without cow
6939 */
Josef Bacikeb384b52013-04-24 16:32:55 -04006940 *len = num_bytes;
Chris Mason46bfbb52010-05-26 11:04:10 -04006941 ret = 1;
6942out:
6943 btrfs_free_path(path);
6944 return ret;
6945}
6946
Josef Bacikeb838e72012-07-31 16:28:48 -04006947static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
6948 struct extent_state **cached_state, int writing)
6949{
6950 struct btrfs_ordered_extent *ordered;
6951 int ret = 0;
6952
6953 while (1) {
6954 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbaff13db42015-12-03 14:30:40 +01006955 cached_state);
Josef Bacikeb838e72012-07-31 16:28:48 -04006956 /*
6957 * We're concerned with the entire range that we're going to be
Nicholas D Steeves01327612016-05-19 21:18:45 -04006958 * doing DIO to, so we need to make sure there's no ordered
Josef Bacikeb838e72012-07-31 16:28:48 -04006959 * extents in this range.
6960 */
Nikolay Borisova776c6f2017-02-20 13:50:49 +02006961 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), lockstart,
Josef Bacikeb838e72012-07-31 16:28:48 -04006962 lockend - lockstart + 1);
6963
6964 /*
6965 * We need to make sure there are no buffered pages in this
6966 * range either, we could have raced between the invalidate in
6967 * generic_file_direct_write and locking the extent. The
6968 * invalidate needs to happen so that reads after a write do not
6969 * get stale data.
6970 */
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07006971 if (!ordered &&
David Sterba051c98e2018-03-07 15:33:22 +01006972 (!writing || !filemap_range_has_page(inode->i_mapping,
6973 lockstart, lockend)))
Josef Bacikeb838e72012-07-31 16:28:48 -04006974 break;
6975
6976 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbae43bbe52017-12-12 21:43:52 +01006977 cached_state);
Josef Bacikeb838e72012-07-31 16:28:48 -04006978
6979 if (ordered) {
Filipe Mananaade77022016-02-18 14:28:55 +00006980 /*
6981 * If we are doing a DIO read and the ordered extent we
6982 * found is for a buffered write, we can not wait for it
6983 * to complete and retry, because if we do so we can
6984 * deadlock with concurrent buffered writes on page
6985 * locks. This happens only if our DIO read covers more
6986 * than one extent map, if at this point has already
6987 * created an ordered extent for a previous extent map
6988 * and locked its range in the inode's io tree, and a
6989 * concurrent write against that previous extent map's
6990 * range and this range started (we unlock the ranges
6991 * in the io tree only when the bios complete and
6992 * buffered writes always lock pages before attempting
6993 * to lock range in the io tree).
6994 */
6995 if (writing ||
6996 test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags))
6997 btrfs_start_ordered_extent(inode, ordered, 1);
6998 else
6999 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007000 btrfs_put_ordered_extent(ordered);
7001 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007002 /*
Filipe Mananab850ae12015-12-08 16:23:16 +00007003 * We could trigger writeback for this range (and wait
7004 * for it to complete) and then invalidate the pages for
7005 * this range (through invalidate_inode_pages2_range()),
7006 * but that can lead us to a deadlock with a concurrent
7007 * call to readpages() (a buffered read or a defrag call
7008 * triggered a readahead) on a page lock due to an
7009 * ordered dio extent we created before but did not have
7010 * yet a corresponding bio submitted (whence it can not
7011 * complete), which makes readpages() wait for that
7012 * ordered extent to complete while holding a lock on
7013 * that page.
Josef Bacikeb838e72012-07-31 16:28:48 -04007014 */
Filipe Mananab850ae12015-12-08 16:23:16 +00007015 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007016 }
7017
Filipe Mananaade77022016-02-18 14:28:55 +00007018 if (ret)
7019 break;
7020
Josef Bacikeb838e72012-07-31 16:28:48 -04007021 cond_resched();
7022 }
7023
7024 return ret;
7025}
7026
Liu Bo6f9994d2017-01-31 07:50:22 -08007027/* The callers of this must take lock_extent() */
7028static struct extent_map *create_io_em(struct inode *inode, u64 start, u64 len,
7029 u64 orig_start, u64 block_start,
7030 u64 block_len, u64 orig_block_len,
7031 u64 ram_bytes, int compress_type,
7032 int type)
Josef Bacik69ffb542012-09-11 15:40:07 -04007033{
7034 struct extent_map_tree *em_tree;
7035 struct extent_map *em;
Josef Bacik69ffb542012-09-11 15:40:07 -04007036 int ret;
7037
Liu Bo6f9994d2017-01-31 07:50:22 -08007038 ASSERT(type == BTRFS_ORDERED_PREALLOC ||
7039 type == BTRFS_ORDERED_COMPRESSED ||
7040 type == BTRFS_ORDERED_NOCOW ||
Liu Bo1af4a0a2017-02-13 15:35:09 -08007041 type == BTRFS_ORDERED_REGULAR);
Liu Bo6f9994d2017-01-31 07:50:22 -08007042
Josef Bacik69ffb542012-09-11 15:40:07 -04007043 em_tree = &BTRFS_I(inode)->extent_tree;
7044 em = alloc_extent_map();
7045 if (!em)
7046 return ERR_PTR(-ENOMEM);
7047
7048 em->start = start;
7049 em->orig_start = orig_start;
7050 em->len = len;
7051 em->block_len = block_len;
7052 em->block_start = block_start;
Josef Bacikb4939682012-12-03 10:31:19 -05007053 em->orig_block_len = orig_block_len;
Josef Bacikcc95bef2013-04-04 14:31:27 -04007054 em->ram_bytes = ram_bytes;
Josef Bacik70c8a912012-10-11 16:54:30 -04007055 em->generation = -1;
Josef Bacik69ffb542012-09-11 15:40:07 -04007056 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Liu Bo1af4a0a2017-02-13 15:35:09 -08007057 if (type == BTRFS_ORDERED_PREALLOC) {
Josef Bacikb11e2342012-12-03 10:58:15 -05007058 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Liu Bo1af4a0a2017-02-13 15:35:09 -08007059 } else if (type == BTRFS_ORDERED_COMPRESSED) {
Liu Bo6f9994d2017-01-31 07:50:22 -08007060 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
7061 em->compress_type = compress_type;
7062 }
Josef Bacik69ffb542012-09-11 15:40:07 -04007063
7064 do {
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02007065 btrfs_drop_extent_cache(BTRFS_I(inode), em->start,
Josef Bacik69ffb542012-09-11 15:40:07 -04007066 em->start + em->len - 1, 0);
7067 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04007068 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik69ffb542012-09-11 15:40:07 -04007069 write_unlock(&em_tree->lock);
Liu Bo6f9994d2017-01-31 07:50:22 -08007070 /*
7071 * The caller has taken lock_extent(), who could race with us
7072 * to add em?
7073 */
Josef Bacik69ffb542012-09-11 15:40:07 -04007074 } while (ret == -EEXIST);
7075
7076 if (ret) {
7077 free_extent_map(em);
7078 return ERR_PTR(ret);
7079 }
7080
Liu Bo6f9994d2017-01-31 07:50:22 -08007081 /* em got 2 refs now, callers needs to do free_extent_map once. */
Josef Bacik69ffb542012-09-11 15:40:07 -04007082 return em;
7083}
7084
Nikolay Borisov1c8d0172018-05-02 15:19:32 +03007085
7086static int btrfs_get_blocks_direct_read(struct extent_map *em,
7087 struct buffer_head *bh_result,
7088 struct inode *inode,
7089 u64 start, u64 len)
7090{
David Sterba8530c372019-08-30 15:42:07 +02007091 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
7092
Nikolay Borisov1c8d0172018-05-02 15:19:32 +03007093 if (em->block_start == EXTENT_MAP_HOLE ||
7094 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7095 return -ENOENT;
7096
7097 len = min(len, em->len - (start - em->start));
7098
7099 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
7100 inode->i_blkbits;
7101 bh_result->b_size = len;
David Sterba8530c372019-08-30 15:42:07 +02007102 bh_result->b_bdev = fs_info->fs_devices->latest_bdev;
Nikolay Borisov1c8d0172018-05-02 15:19:32 +03007103 set_buffer_mapped(bh_result);
7104
7105 return 0;
7106}
7107
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007108static int btrfs_get_blocks_direct_write(struct extent_map **map,
7109 struct buffer_head *bh_result,
7110 struct inode *inode,
7111 struct btrfs_dio_data *dio_data,
7112 u64 start, u64 len)
7113{
7114 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
7115 struct extent_map *em = *map;
7116 int ret = 0;
7117
7118 /*
7119 * We don't allocate a new extent in the following cases
7120 *
7121 * 1) The inode is marked as NODATACOW. In this case we'll just use the
7122 * existing extent.
7123 * 2) The extent is marked as PREALLOC. We're good to go here and can
7124 * just use the extent.
7125 *
7126 */
7127 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
7128 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
7129 em->block_start != EXTENT_MAP_HOLE)) {
7130 int type;
7131 u64 block_start, orig_start, orig_block_len, ram_bytes;
7132
7133 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7134 type = BTRFS_ORDERED_PREALLOC;
7135 else
7136 type = BTRFS_ORDERED_NOCOW;
7137 len = min(len, em->len - (start - em->start));
7138 block_start = em->block_start + (start - em->start);
7139
7140 if (can_nocow_extent(inode, start, &len, &orig_start,
7141 &orig_block_len, &ram_bytes) == 1 &&
7142 btrfs_inc_nocow_writers(fs_info, block_start)) {
7143 struct extent_map *em2;
7144
7145 em2 = btrfs_create_dio_extent(inode, start, len,
7146 orig_start, block_start,
7147 len, orig_block_len,
7148 ram_bytes, type);
7149 btrfs_dec_nocow_writers(fs_info, block_start);
7150 if (type == BTRFS_ORDERED_PREALLOC) {
7151 free_extent_map(em);
7152 *map = em = em2;
7153 }
7154
7155 if (em2 && IS_ERR(em2)) {
7156 ret = PTR_ERR(em2);
7157 goto out;
7158 }
7159 /*
7160 * For inode marked NODATACOW or extent marked PREALLOC,
7161 * use the existing or preallocated extent, so does not
7162 * need to adjust btrfs_space_info's bytes_may_use.
7163 */
7164 btrfs_free_reserved_data_space_noquota(inode, start,
7165 len);
7166 goto skip_cow;
7167 }
7168 }
7169
7170 /* this will cow the extent */
7171 len = bh_result->b_size;
7172 free_extent_map(em);
7173 *map = em = btrfs_new_extent_direct(inode, start, len);
7174 if (IS_ERR(em)) {
7175 ret = PTR_ERR(em);
7176 goto out;
7177 }
7178
7179 len = min(len, em->len - (start - em->start));
7180
7181skip_cow:
7182 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
7183 inode->i_blkbits;
7184 bh_result->b_size = len;
David Sterba8530c372019-08-30 15:42:07 +02007185 bh_result->b_bdev = fs_info->fs_devices->latest_bdev;
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007186 set_buffer_mapped(bh_result);
7187
7188 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7189 set_buffer_new(bh_result);
7190
7191 /*
7192 * Need to update the i_size under the extent lock so buffered
7193 * readers will get the updated i_size when we unlock.
7194 */
7195 if (!dio_data->overwrite && start + len > i_size_read(inode))
7196 i_size_write(inode, start + len);
7197
7198 WARN_ON(dio_data->reserve < len);
7199 dio_data->reserve -= len;
7200 dio_data->unsubmitted_oe_range_end = start + len;
7201 current->journal_info = dio_data;
7202out:
7203 return ret;
7204}
7205
Josef Bacik4b46fce2010-05-23 11:00:55 -04007206static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
7207 struct buffer_head *bh_result, int create)
7208{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007209 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007210 struct extent_map *em;
Josef Bacikeb838e72012-07-31 16:28:48 -04007211 struct extent_state *cached_state = NULL;
chandan50745b02015-08-28 21:10:13 +05307212 struct btrfs_dio_data *dio_data = NULL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007213 u64 start = iblock << inode->i_blkbits;
Josef Bacikeb838e72012-07-31 16:28:48 -04007214 u64 lockstart, lockend;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007215 u64 len = bh_result->b_size;
Miao Xie09348562013-02-07 10:12:07 +00007216 int ret = 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007217
Omar Sandovale1821632019-08-15 14:04:04 -07007218 if (!create)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007219 len = min_t(u64, len, fs_info->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04007220
Josef Bacikc3298612012-08-03 16:49:19 -04007221 lockstart = start;
7222 lockend = start + len - 1;
7223
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007224 if (current->journal_info) {
7225 /*
7226 * Need to pull our outstanding extents and set journal_info to NULL so
Nicholas D Steeves01327612016-05-19 21:18:45 -04007227 * that anything that needs to check if there's a transaction doesn't get
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007228 * confused.
7229 */
chandan50745b02015-08-28 21:10:13 +05307230 dio_data = current->journal_info;
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007231 current->journal_info = NULL;
7232 }
7233
Josef Bacikeb838e72012-07-31 16:28:48 -04007234 /*
7235 * If this errors out it's because we couldn't invalidate pagecache for
7236 * this range and we need to fallback to buffered.
7237 */
Filipe Manana9c9464c2015-11-04 09:52:04 +00007238 if (lock_extent_direct(inode, lockstart, lockend, &cached_state,
7239 create)) {
7240 ret = -ENOTBLK;
7241 goto err;
7242 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007243
Omar Sandoval39b07b52019-12-02 17:34:23 -08007244 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len);
Josef Bacikeb838e72012-07-31 16:28:48 -04007245 if (IS_ERR(em)) {
7246 ret = PTR_ERR(em);
7247 goto unlock_err;
7248 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007249
7250 /*
7251 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
7252 * io. INLINE is special, and we could probably kludge it in here, but
7253 * it's still buffered so for safety lets just fall back to the generic
7254 * buffered path.
7255 *
7256 * For COMPRESSED we _have_ to read the entire extent in so we can
7257 * decompress it, so there will be buffering required no matter what we
7258 * do, so go ahead and fallback to buffered.
7259 *
Nicholas D Steeves01327612016-05-19 21:18:45 -04007260 * We return -ENOTBLK because that's what makes DIO go ahead and go back
Josef Bacik4b46fce2010-05-23 11:00:55 -04007261 * to buffered IO. Don't blame me, this is the price we pay for using
7262 * the generic code.
7263 */
7264 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
7265 em->block_start == EXTENT_MAP_INLINE) {
7266 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007267 ret = -ENOTBLK;
7268 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007269 }
7270
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007271 if (create) {
7272 ret = btrfs_get_blocks_direct_write(&em, bh_result, inode,
7273 dio_data, start, len);
7274 if (ret < 0)
7275 goto unlock_err;
7276
Omar Sandovale1821632019-08-15 14:04:04 -07007277 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart,
7278 lockend, &cached_state);
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007279 } else {
Nikolay Borisov1c8d0172018-05-02 15:19:32 +03007280 ret = btrfs_get_blocks_direct_read(em, bh_result, inode,
7281 start, len);
7282 /* Can be negative only if we read from a hole */
7283 if (ret < 0) {
7284 ret = 0;
7285 free_extent_map(em);
7286 goto unlock_err;
7287 }
7288 /*
7289 * We need to unlock only the end area that we aren't using.
7290 * The rest is going to be unlocked by the endio routine.
7291 */
7292 lockstart = start + bh_result->b_size;
7293 if (lockstart < lockend) {
Omar Sandovale1821632019-08-15 14:04:04 -07007294 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
7295 lockstart, lockend, &cached_state);
Nikolay Borisov1c8d0172018-05-02 15:19:32 +03007296 } else {
7297 free_extent_state(cached_state);
7298 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007299 }
7300
Josef Bacik4b46fce2010-05-23 11:00:55 -04007301 free_extent_map(em);
7302
7303 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007304
7305unlock_err:
Omar Sandovale1821632019-08-15 14:04:04 -07007306 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7307 &cached_state);
Filipe Manana9c9464c2015-11-04 09:52:04 +00007308err:
chandan50745b02015-08-28 21:10:13 +05307309 if (dio_data)
7310 current->journal_info = dio_data;
Josef Bacikeb838e72012-07-31 16:28:48 -04007311 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007312}
7313
Omar Sandoval58efbc92017-08-22 23:45:59 -07007314static inline blk_status_t submit_dio_repair_bio(struct inode *inode,
7315 struct bio *bio,
7316 int mirror_num)
Miao Xie8b110e32014-09-12 18:44:03 +08007317{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007318 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Omar Sandoval58efbc92017-08-22 23:45:59 -07007319 blk_status_t ret;
Miao Xie8b110e32014-09-12 18:44:03 +08007320
Mike Christie37226b22016-06-05 14:31:52 -05007321 BUG_ON(bio_op(bio) == REQ_OP_WRITE);
Miao Xie8b110e32014-09-12 18:44:03 +08007322
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007323 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DIO_REPAIR);
Miao Xie8b110e32014-09-12 18:44:03 +08007324 if (ret)
Nikolay Borisovea057f62017-12-13 10:25:38 +02007325 return ret;
Miao Xie8b110e32014-09-12 18:44:03 +08007326
Chris Mason08635ba2019-07-10 12:28:14 -07007327 ret = btrfs_map_bio(fs_info, bio, mirror_num);
Nikolay Borisovea057f62017-12-13 10:25:38 +02007328
Miao Xie8b110e32014-09-12 18:44:03 +08007329 return ret;
7330}
7331
7332static int btrfs_check_dio_repairable(struct inode *inode,
7333 struct bio *failed_bio,
7334 struct io_failure_record *failrec,
7335 int failed_mirror)
7336{
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007337 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xie8b110e32014-09-12 18:44:03 +08007338 int num_copies;
7339
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007340 num_copies = btrfs_num_copies(fs_info, failrec->logical, failrec->len);
Miao Xie8b110e32014-09-12 18:44:03 +08007341 if (num_copies == 1) {
7342 /*
7343 * we only have a single copy of the data, so don't bother with
7344 * all the retry and error correction code that follows. no
7345 * matter what the error is, it is very likely to persist.
7346 */
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007347 btrfs_debug(fs_info,
7348 "Check DIO Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d",
7349 num_copies, failrec->this_mirror, failed_mirror);
Miao Xie8b110e32014-09-12 18:44:03 +08007350 return 0;
7351 }
7352
7353 failrec->failed_mirror = failed_mirror;
7354 failrec->this_mirror++;
7355 if (failrec->this_mirror == failed_mirror)
7356 failrec->this_mirror++;
7357
7358 if (failrec->this_mirror > num_copies) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007359 btrfs_debug(fs_info,
7360 "Check DIO Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d",
7361 num_copies, failrec->this_mirror, failed_mirror);
Miao Xie8b110e32014-09-12 18:44:03 +08007362 return 0;
7363 }
7364
7365 return 1;
7366}
7367
Omar Sandoval58efbc92017-08-22 23:45:59 -07007368static blk_status_t dio_read_error(struct inode *inode, struct bio *failed_bio,
7369 struct page *page, unsigned int pgoff,
7370 u64 start, u64 end, int failed_mirror,
7371 bio_end_io_t *repair_endio, void *repair_arg)
Miao Xie8b110e32014-09-12 18:44:03 +08007372{
7373 struct io_failure_record *failrec;
Josef Bacik7870d082017-05-05 11:57:15 -04007374 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
7375 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
Miao Xie8b110e32014-09-12 18:44:03 +08007376 struct bio *bio;
7377 int isector;
David Sterbaf1c77c52017-06-06 19:03:49 +02007378 unsigned int read_mode = 0;
Liu Bo17347ce2017-05-15 15:33:27 -07007379 int segs;
Miao Xie8b110e32014-09-12 18:44:03 +08007380 int ret;
Omar Sandoval58efbc92017-08-22 23:45:59 -07007381 blk_status_t status;
Ming Leic16a8ac2017-12-18 20:22:12 +08007382 struct bio_vec bvec;
Miao Xie8b110e32014-09-12 18:44:03 +08007383
Mike Christie37226b22016-06-05 14:31:52 -05007384 BUG_ON(bio_op(failed_bio) == REQ_OP_WRITE);
Miao Xie8b110e32014-09-12 18:44:03 +08007385
7386 ret = btrfs_get_io_failure_record(inode, start, end, &failrec);
7387 if (ret)
Omar Sandoval58efbc92017-08-22 23:45:59 -07007388 return errno_to_blk_status(ret);
Miao Xie8b110e32014-09-12 18:44:03 +08007389
7390 ret = btrfs_check_dio_repairable(inode, failed_bio, failrec,
7391 failed_mirror);
7392 if (!ret) {
Josef Bacik7870d082017-05-05 11:57:15 -04007393 free_io_failure(failure_tree, io_tree, failrec);
Omar Sandoval58efbc92017-08-22 23:45:59 -07007394 return BLK_STS_IOERR;
Miao Xie8b110e32014-09-12 18:44:03 +08007395 }
7396
Liu Bo17347ce2017-05-15 15:33:27 -07007397 segs = bio_segments(failed_bio);
Ming Leic16a8ac2017-12-18 20:22:12 +08007398 bio_get_first_bvec(failed_bio, &bvec);
Liu Bo17347ce2017-05-15 15:33:27 -07007399 if (segs > 1 ||
Ming Leic16a8ac2017-12-18 20:22:12 +08007400 (bvec.bv_len > btrfs_inode_sectorsize(inode)))
Christoph Hellwig70fd7612016-11-01 07:40:10 -06007401 read_mode |= REQ_FAILFAST_DEV;
Miao Xie8b110e32014-09-12 18:44:03 +08007402
7403 isector = start - btrfs_io_bio(failed_bio)->logical;
7404 isector >>= inode->i_sb->s_blocksize_bits;
7405 bio = btrfs_create_repair_bio(inode, failed_bio, failrec, page,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307406 pgoff, isector, repair_endio, repair_arg);
David Sterbaebcc3262018-06-29 10:56:53 +02007407 bio->bi_opf = REQ_OP_READ | read_mode;
Miao Xie8b110e32014-09-12 18:44:03 +08007408
7409 btrfs_debug(BTRFS_I(inode)->root->fs_info,
David Sterba913e1532017-07-13 15:32:18 +02007410 "repair DIO read error: submitting new dio read[%#x] to this_mirror=%d, in_validation=%d",
Miao Xie8b110e32014-09-12 18:44:03 +08007411 read_mode, failrec->this_mirror, failrec->in_validation);
7412
Omar Sandoval58efbc92017-08-22 23:45:59 -07007413 status = submit_dio_repair_bio(inode, bio, failrec->this_mirror);
7414 if (status) {
Josef Bacik7870d082017-05-05 11:57:15 -04007415 free_io_failure(failure_tree, io_tree, failrec);
Miao Xie8b110e32014-09-12 18:44:03 +08007416 bio_put(bio);
7417 }
7418
Omar Sandoval58efbc92017-08-22 23:45:59 -07007419 return status;
Miao Xie8b110e32014-09-12 18:44:03 +08007420}
7421
7422struct btrfs_retry_complete {
7423 struct completion done;
7424 struct inode *inode;
7425 u64 start;
7426 int uptodate;
7427};
7428
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007429static void btrfs_retry_endio_nocsum(struct bio *bio)
Miao Xie8b110e32014-09-12 18:44:03 +08007430{
7431 struct btrfs_retry_complete *done = bio->bi_private;
Josef Bacik7870d082017-05-05 11:57:15 -04007432 struct inode *inode = done->inode;
Miao Xie8b110e32014-09-12 18:44:03 +08007433 struct bio_vec *bvec;
Josef Bacik7870d082017-05-05 11:57:15 -04007434 struct extent_io_tree *io_tree, *failure_tree;
Ming Lei6dc4f102019-02-15 19:13:19 +08007435 struct bvec_iter_all iter_all;
Miao Xie8b110e32014-09-12 18:44:03 +08007436
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02007437 if (bio->bi_status)
Miao Xie8b110e32014-09-12 18:44:03 +08007438 goto end;
7439
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307440 ASSERT(bio->bi_vcnt == 1);
Josef Bacik7870d082017-05-05 11:57:15 -04007441 io_tree = &BTRFS_I(inode)->io_tree;
7442 failure_tree = &BTRFS_I(inode)->io_failure_tree;
Ming Lei263663c2017-12-18 20:22:04 +08007443 ASSERT(bio_first_bvec_all(bio)->bv_len == btrfs_inode_sectorsize(inode));
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307444
Miao Xie8b110e32014-09-12 18:44:03 +08007445 done->uptodate = 1;
David Sterbac09abff2017-07-13 18:10:07 +02007446 ASSERT(!bio_flagged(bio, BIO_CLONED));
Christoph Hellwig2b070cf2019-04-25 09:03:00 +02007447 bio_for_each_segment_all(bvec, bio, iter_all)
Josef Bacik7870d082017-05-05 11:57:15 -04007448 clean_io_failure(BTRFS_I(inode)->root->fs_info, failure_tree,
7449 io_tree, done->start, bvec->bv_page,
7450 btrfs_ino(BTRFS_I(inode)), 0);
Miao Xie8b110e32014-09-12 18:44:03 +08007451end:
7452 complete(&done->done);
7453 bio_put(bio);
7454}
7455
Omar Sandoval58efbc92017-08-22 23:45:59 -07007456static blk_status_t __btrfs_correct_data_nocsum(struct inode *inode,
7457 struct btrfs_io_bio *io_bio)
Josef Bacik4b46fce2010-05-23 11:00:55 -04007458{
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307459 struct btrfs_fs_info *fs_info;
Liu Bo17347ce2017-05-15 15:33:27 -07007460 struct bio_vec bvec;
7461 struct bvec_iter iter;
Miao Xie8b110e32014-09-12 18:44:03 +08007462 struct btrfs_retry_complete done;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007463 u64 start;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307464 unsigned int pgoff;
7465 u32 sectorsize;
7466 int nr_sectors;
Omar Sandoval58efbc92017-08-22 23:45:59 -07007467 blk_status_t ret;
7468 blk_status_t err = BLK_STS_OK;
Miao Xiedc380ae2014-09-12 18:43:55 +08007469
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307470 fs_info = BTRFS_I(inode)->root->fs_info;
Jeff Mahoneyda170662016-06-15 09:22:56 -04007471 sectorsize = fs_info->sectorsize;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307472
Miao Xiec1dc0892014-09-12 18:43:56 +08007473 start = io_bio->logical;
Miao Xie8b110e32014-09-12 18:44:03 +08007474 done.inode = inode;
Liu Bo17347ce2017-05-15 15:33:27 -07007475 io_bio->bio.bi_iter = io_bio->iter;
Miao Xie8b110e32014-09-12 18:44:03 +08007476
Liu Bo17347ce2017-05-15 15:33:27 -07007477 bio_for_each_segment(bvec, &io_bio->bio, iter) {
7478 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec.bv_len);
7479 pgoff = bvec.bv_offset;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307480
7481next_block_or_try_again:
Miao Xie8b110e32014-09-12 18:44:03 +08007482 done.uptodate = 0;
7483 done.start = start;
7484 init_completion(&done.done);
7485
Liu Bo17347ce2017-05-15 15:33:27 -07007486 ret = dio_read_error(inode, &io_bio->bio, bvec.bv_page,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307487 pgoff, start, start + sectorsize - 1,
7488 io_bio->mirror_num,
7489 btrfs_retry_endio_nocsum, &done);
Liu Bo629ebf42017-05-15 17:20:07 -07007490 if (ret) {
7491 err = ret;
7492 goto next;
7493 }
Miao Xie8b110e32014-09-12 18:44:03 +08007494
David Sterba9c17f6c2017-07-19 19:26:45 +02007495 wait_for_completion_io(&done.done);
Miao Xie8b110e32014-09-12 18:44:03 +08007496
7497 if (!done.uptodate) {
7498 /* We might have another mirror, so try again */
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307499 goto next_block_or_try_again;
Miao Xie8b110e32014-09-12 18:44:03 +08007500 }
7501
Liu Bo629ebf42017-05-15 17:20:07 -07007502next:
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307503 start += sectorsize;
7504
Liu Bo97bf5a52017-04-07 13:11:10 -07007505 nr_sectors--;
7506 if (nr_sectors) {
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307507 pgoff += sectorsize;
Liu Bo97bf5a52017-04-07 13:11:10 -07007508 ASSERT(pgoff < PAGE_SIZE);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307509 goto next_block_or_try_again;
7510 }
Miao Xie8b110e32014-09-12 18:44:03 +08007511 }
7512
Liu Bo629ebf42017-05-15 17:20:07 -07007513 return err;
Miao Xie8b110e32014-09-12 18:44:03 +08007514}
7515
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007516static void btrfs_retry_endio(struct bio *bio)
Miao Xie8b110e32014-09-12 18:44:03 +08007517{
7518 struct btrfs_retry_complete *done = bio->bi_private;
7519 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
Josef Bacik7870d082017-05-05 11:57:15 -04007520 struct extent_io_tree *io_tree, *failure_tree;
7521 struct inode *inode = done->inode;
Miao Xie8b110e32014-09-12 18:44:03 +08007522 struct bio_vec *bvec;
7523 int uptodate;
7524 int ret;
Christoph Hellwig2b070cf2019-04-25 09:03:00 +02007525 int i = 0;
Ming Lei6dc4f102019-02-15 19:13:19 +08007526 struct bvec_iter_all iter_all;
Miao Xie8b110e32014-09-12 18:44:03 +08007527
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02007528 if (bio->bi_status)
Miao Xie8b110e32014-09-12 18:44:03 +08007529 goto end;
7530
7531 uptodate = 1;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307532
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307533 ASSERT(bio->bi_vcnt == 1);
Ming Lei263663c2017-12-18 20:22:04 +08007534 ASSERT(bio_first_bvec_all(bio)->bv_len == btrfs_inode_sectorsize(done->inode));
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307535
Josef Bacik7870d082017-05-05 11:57:15 -04007536 io_tree = &BTRFS_I(inode)->io_tree;
7537 failure_tree = &BTRFS_I(inode)->io_failure_tree;
7538
David Sterbac09abff2017-07-13 18:10:07 +02007539 ASSERT(!bio_flagged(bio, BIO_CLONED));
Christoph Hellwig2b070cf2019-04-25 09:03:00 +02007540 bio_for_each_segment_all(bvec, bio, iter_all) {
Josef Bacik7870d082017-05-05 11:57:15 -04007541 ret = __readpage_endio_check(inode, io_bio, i, bvec->bv_page,
7542 bvec->bv_offset, done->start,
7543 bvec->bv_len);
Miao Xie8b110e32014-09-12 18:44:03 +08007544 if (!ret)
Josef Bacik7870d082017-05-05 11:57:15 -04007545 clean_io_failure(BTRFS_I(inode)->root->fs_info,
7546 failure_tree, io_tree, done->start,
7547 bvec->bv_page,
7548 btrfs_ino(BTRFS_I(inode)),
7549 bvec->bv_offset);
Miao Xie8b110e32014-09-12 18:44:03 +08007550 else
7551 uptodate = 0;
Christoph Hellwig2b070cf2019-04-25 09:03:00 +02007552 i++;
Miao Xie8b110e32014-09-12 18:44:03 +08007553 }
7554
7555 done->uptodate = uptodate;
7556end:
7557 complete(&done->done);
7558 bio_put(bio);
7559}
7560
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02007561static blk_status_t __btrfs_subio_endio_read(struct inode *inode,
7562 struct btrfs_io_bio *io_bio, blk_status_t err)
Miao Xie8b110e32014-09-12 18:44:03 +08007563{
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307564 struct btrfs_fs_info *fs_info;
Liu Bo17347ce2017-05-15 15:33:27 -07007565 struct bio_vec bvec;
7566 struct bvec_iter iter;
Miao Xie8b110e32014-09-12 18:44:03 +08007567 struct btrfs_retry_complete done;
7568 u64 start;
7569 u64 offset = 0;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307570 u32 sectorsize;
7571 int nr_sectors;
7572 unsigned int pgoff;
7573 int csum_pos;
Liu Boef7cdac12017-04-13 18:11:48 -07007574 bool uptodate = (err == 0);
Miao Xie8b110e32014-09-12 18:44:03 +08007575 int ret;
Omar Sandoval58efbc92017-08-22 23:45:59 -07007576 blk_status_t status;
Miao Xie8b110e32014-09-12 18:44:03 +08007577
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307578 fs_info = BTRFS_I(inode)->root->fs_info;
Jeff Mahoneyda170662016-06-15 09:22:56 -04007579 sectorsize = fs_info->sectorsize;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307580
Omar Sandoval58efbc92017-08-22 23:45:59 -07007581 err = BLK_STS_OK;
Miao Xie8b110e32014-09-12 18:44:03 +08007582 start = io_bio->logical;
7583 done.inode = inode;
Liu Bo17347ce2017-05-15 15:33:27 -07007584 io_bio->bio.bi_iter = io_bio->iter;
Miao Xie8b110e32014-09-12 18:44:03 +08007585
Liu Bo17347ce2017-05-15 15:33:27 -07007586 bio_for_each_segment(bvec, &io_bio->bio, iter) {
7587 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec.bv_len);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307588
Liu Bo17347ce2017-05-15 15:33:27 -07007589 pgoff = bvec.bv_offset;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307590next_block:
Liu Boef7cdac12017-04-13 18:11:48 -07007591 if (uptodate) {
7592 csum_pos = BTRFS_BYTES_TO_BLKS(fs_info, offset);
7593 ret = __readpage_endio_check(inode, io_bio, csum_pos,
7594 bvec.bv_page, pgoff, start, sectorsize);
7595 if (likely(!ret))
7596 goto next;
7597 }
Miao Xie8b110e32014-09-12 18:44:03 +08007598try_again:
7599 done.uptodate = 0;
7600 done.start = start;
7601 init_completion(&done.done);
7602
Omar Sandoval58efbc92017-08-22 23:45:59 -07007603 status = dio_read_error(inode, &io_bio->bio, bvec.bv_page,
7604 pgoff, start, start + sectorsize - 1,
7605 io_bio->mirror_num, btrfs_retry_endio,
7606 &done);
7607 if (status) {
7608 err = status;
Miao Xie8b110e32014-09-12 18:44:03 +08007609 goto next;
7610 }
7611
David Sterba9c17f6c2017-07-19 19:26:45 +02007612 wait_for_completion_io(&done.done);
Miao Xie8b110e32014-09-12 18:44:03 +08007613
7614 if (!done.uptodate) {
7615 /* We might have another mirror, so try again */
7616 goto try_again;
7617 }
7618next:
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307619 offset += sectorsize;
7620 start += sectorsize;
7621
7622 ASSERT(nr_sectors);
7623
Liu Bo97bf5a52017-04-07 13:11:10 -07007624 nr_sectors--;
7625 if (nr_sectors) {
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307626 pgoff += sectorsize;
Liu Bo97bf5a52017-04-07 13:11:10 -07007627 ASSERT(pgoff < PAGE_SIZE);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307628 goto next_block;
7629 }
Kent Overstreet2c30c712013-11-07 12:20:26 -08007630 }
Miao Xiec1dc0892014-09-12 18:43:56 +08007631
7632 return err;
7633}
7634
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02007635static blk_status_t btrfs_subio_endio_read(struct inode *inode,
7636 struct btrfs_io_bio *io_bio, blk_status_t err)
Miao Xie8b110e32014-09-12 18:44:03 +08007637{
7638 bool skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
7639
7640 if (skip_csum) {
7641 if (unlikely(err))
7642 return __btrfs_correct_data_nocsum(inode, io_bio);
7643 else
Omar Sandoval58efbc92017-08-22 23:45:59 -07007644 return BLK_STS_OK;
Miao Xie8b110e32014-09-12 18:44:03 +08007645 } else {
7646 return __btrfs_subio_endio_read(inode, io_bio, err);
7647 }
7648}
7649
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007650static void btrfs_endio_direct_read(struct bio *bio)
Miao Xiec1dc0892014-09-12 18:43:56 +08007651{
7652 struct btrfs_dio_private *dip = bio->bi_private;
7653 struct inode *inode = dip->inode;
7654 struct bio *dio_bio;
7655 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02007656 blk_status_t err = bio->bi_status;
Miao Xiec1dc0892014-09-12 18:43:56 +08007657
Liu Bo99c4e3b92017-09-15 15:06:51 -06007658 if (dip->flags & BTRFS_DIO_ORIG_BIO_SUBMITTED)
Miao Xie8b110e32014-09-12 18:44:03 +08007659 err = btrfs_subio_endio_read(inode, io_bio, err);
Miao Xiec1dc0892014-09-12 18:43:56 +08007660
Josef Bacik4b46fce2010-05-23 11:00:55 -04007661 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01007662 dip->logical_offset + dip->bytes - 1);
Chris Mason9be33952013-05-17 18:30:14 -04007663 dio_bio = dip->dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007664
Josef Bacik4b46fce2010-05-23 11:00:55 -04007665 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04007666
Liu Bo99c4e3b92017-09-15 15:06:51 -06007667 dio_bio->bi_status = err;
Christoph Hellwig40553512017-06-03 09:37:58 +02007668 dio_end_io(dio_bio);
David Sterbab3a0dd52018-11-22 17:16:49 +01007669 btrfs_io_bio_free_csum(io_bio);
Chris Mason9be33952013-05-17 18:30:14 -04007670 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007671}
7672
Qu Wenruo524272602017-03-08 10:25:52 +08007673static void __endio_write_update_ordered(struct inode *inode,
7674 const u64 offset, const u64 bytes,
7675 const bool uptodate)
Josef Bacik4b46fce2010-05-23 11:00:55 -04007676{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007677 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007678 struct btrfs_ordered_extent *ordered = NULL;
Qu Wenruo524272602017-03-08 10:25:52 +08007679 struct btrfs_workqueue *wq;
Filipe Manana14543772015-11-24 16:23:54 +00007680 u64 ordered_offset = offset;
7681 u64 ordered_bytes = bytes;
Naohiro Aota67c003f2017-09-01 17:59:07 +09007682 u64 last_offset;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007683
Omar Sandovala0cac0e2019-09-16 11:30:57 -07007684 if (btrfs_is_free_space_inode(BTRFS_I(inode)))
Qu Wenruo524272602017-03-08 10:25:52 +08007685 wq = fs_info->endio_freespace_worker;
Omar Sandovala0cac0e2019-09-16 11:30:57 -07007686 else
Qu Wenruo524272602017-03-08 10:25:52 +08007687 wq = fs_info->endio_write_workers;
Qu Wenruo524272602017-03-08 10:25:52 +08007688
Nikolay Borisovb25f0d02018-04-11 11:21:17 +03007689 while (ordered_offset < offset + bytes) {
7690 last_offset = ordered_offset;
7691 if (btrfs_dec_test_first_ordered_pending(inode, &ordered,
7692 &ordered_offset,
7693 ordered_bytes,
7694 uptodate)) {
Omar Sandovala0cac0e2019-09-16 11:30:57 -07007695 btrfs_init_work(&ordered->work, finish_ordered_fn, NULL,
7696 NULL);
Nikolay Borisovb25f0d02018-04-11 11:21:17 +03007697 btrfs_queue_work(wq, &ordered->work);
7698 }
7699 /*
7700 * If btrfs_dec_test_ordered_pending does not find any ordered
7701 * extent in the range, we can exit.
7702 */
7703 if (ordered_offset == last_offset)
7704 return;
7705 /*
7706 * Our bio might span multiple ordered extents. In this case
Andrea Gelmini52042d82018-11-28 12:05:13 +01007707 * we keep going until we have accounted the whole dio.
Nikolay Borisovb25f0d02018-04-11 11:21:17 +03007708 */
7709 if (ordered_offset < offset + bytes) {
7710 ordered_bytes = offset + bytes - ordered_offset;
7711 ordered = NULL;
7712 }
Chris Mason163cf092010-11-28 19:56:33 -05007713 }
Filipe Manana14543772015-11-24 16:23:54 +00007714}
7715
7716static void btrfs_endio_direct_write(struct bio *bio)
7717{
7718 struct btrfs_dio_private *dip = bio->bi_private;
7719 struct bio *dio_bio = dip->dio_bio;
7720
Qu Wenruo524272602017-03-08 10:25:52 +08007721 __endio_write_update_ordered(dip->inode, dip->logical_offset,
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02007722 dip->bytes, !bio->bi_status);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007723
Josef Bacik4b46fce2010-05-23 11:00:55 -04007724 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04007725
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02007726 dio_bio->bi_status = bio->bi_status;
Christoph Hellwig40553512017-06-03 09:37:58 +02007727 dio_end_io(dio_bio);
Chris Mason9be33952013-05-17 18:30:14 -04007728 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007729}
7730
David Sterbad0ee3932018-03-08 14:35:48 +01007731static blk_status_t btrfs_submit_bio_start_direct_io(void *private_data,
David Sterbad0779292018-03-08 13:47:33 +01007732 struct bio *bio, u64 offset)
Chris Masoneaf25d92010-05-25 09:48:28 -04007733{
Josef Bacikc6100a42017-05-05 11:57:13 -04007734 struct inode *inode = private_data;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02007735 blk_status_t ret;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007736 ret = btrfs_csum_one_bio(inode, bio, offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007737 BUG_ON(ret); /* -ENOMEM */
Chris Masoneaf25d92010-05-25 09:48:28 -04007738 return 0;
7739}
7740
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007741static void btrfs_end_dio_bio(struct bio *bio)
Miao Xiee65e1532010-11-22 03:04:43 +00007742{
7743 struct btrfs_dio_private *dip = bio->bi_private;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02007744 blk_status_t err = bio->bi_status;
Miao Xiee65e1532010-11-22 03:04:43 +00007745
Miao Xie8b110e32014-09-12 18:44:03 +08007746 if (err)
7747 btrfs_warn(BTRFS_I(dip->inode)->root->fs_info,
Mike Christie6296b962016-06-05 14:32:21 -05007748 "direct IO failed ino %llu rw %d,%u sector %#Lx len %u err no %d",
David Sterbaf85b7372017-01-20 14:54:07 +01007749 btrfs_ino(BTRFS_I(dip->inode)), bio_op(bio),
7750 bio->bi_opf,
Miao Xie8b110e32014-09-12 18:44:03 +08007751 (unsigned long long)bio->bi_iter.bi_sector,
7752 bio->bi_iter.bi_size, err);
7753
7754 if (dip->subio_endio)
7755 err = dip->subio_endio(dip->inode, btrfs_io_bio(bio), err);
Miao Xiec1dc0892014-09-12 18:43:56 +08007756
7757 if (err) {
Miao Xiee65e1532010-11-22 03:04:43 +00007758 /*
Nikolay Borisovde224b72018-02-14 10:53:36 +02007759 * We want to perceive the errors flag being set before
7760 * decrementing the reference count. We don't need a barrier
7761 * since atomic operations with a return value are fully
7762 * ordered as per atomic_t.txt
Miao Xiee65e1532010-11-22 03:04:43 +00007763 */
Nikolay Borisovde224b72018-02-14 10:53:36 +02007764 dip->errors = 1;
Miao Xiee65e1532010-11-22 03:04:43 +00007765 }
7766
7767 /* if there are more bios still pending for this dio, just exit */
7768 if (!atomic_dec_and_test(&dip->pending_bios))
7769 goto out;
7770
Chris Mason9be33952013-05-17 18:30:14 -04007771 if (dip->errors) {
Miao Xiee65e1532010-11-22 03:04:43 +00007772 bio_io_error(dip->orig_bio);
Chris Mason9be33952013-05-17 18:30:14 -04007773 } else {
Anand Jain2dbe0c72017-10-14 08:35:56 +08007774 dip->dio_bio->bi_status = BLK_STS_OK;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007775 bio_endio(dip->orig_bio);
Miao Xiee65e1532010-11-22 03:04:43 +00007776 }
7777out:
7778 bio_put(bio);
7779}
7780
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02007781static inline blk_status_t btrfs_lookup_and_bind_dio_csum(struct inode *inode,
Miao Xiec1dc0892014-09-12 18:43:56 +08007782 struct btrfs_dio_private *dip,
7783 struct bio *bio,
7784 u64 file_offset)
7785{
7786 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
7787 struct btrfs_io_bio *orig_io_bio = btrfs_io_bio(dip->orig_bio);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02007788 blk_status_t ret;
Miao Xiec1dc0892014-09-12 18:43:56 +08007789
7790 /*
7791 * We load all the csum data we need when we submit
7792 * the first bio to reduce the csum tree search and
7793 * contention.
7794 */
7795 if (dip->logical_offset == file_offset) {
Omar Sandovaldb72e472019-12-10 10:37:35 -08007796 ret = btrfs_lookup_bio_sums(inode, dip->orig_bio, file_offset,
7797 NULL);
Miao Xiec1dc0892014-09-12 18:43:56 +08007798 if (ret)
7799 return ret;
7800 }
7801
7802 if (bio == dip->orig_bio)
7803 return 0;
7804
7805 file_offset -= dip->logical_offset;
7806 file_offset >>= inode->i_sb->s_blocksize_bits;
7807 io_bio->csum = (u8 *)(((u32 *)orig_io_bio->csum) + file_offset);
7808
7809 return 0;
7810}
7811
David Sterbad0ee3932018-03-08 14:35:48 +01007812static inline blk_status_t btrfs_submit_dio_bio(struct bio *bio,
7813 struct inode *inode, u64 file_offset, int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00007814{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007815 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xiefacc8a222013-07-25 19:22:34 +08007816 struct btrfs_dio_private *dip = bio->bi_private;
Mike Christie37226b22016-06-05 14:31:52 -05007817 bool write = bio_op(bio) == REQ_OP_WRITE;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02007818 blk_status_t ret;
Miao Xiee65e1532010-11-22 03:04:43 +00007819
Liu Bo4c274bc2017-11-01 17:19:27 -06007820 /* Check btrfs_submit_bio_hook() for rules about async submit. */
Josef Bacikb812ce22012-11-16 13:56:32 -05007821 if (async_submit)
7822 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
7823
Josef Bacik5fd02042012-05-02 14:00:54 -04007824 if (!write) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007825 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DATA);
Josef Bacik5fd02042012-05-02 14:00:54 -04007826 if (ret)
7827 goto err;
7828 }
Miao Xiee65e1532010-11-22 03:04:43 +00007829
Nikolay Borisove6961ca2017-08-03 15:44:58 +03007830 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Josef Bacik1ae39932011-04-06 14:41:34 -04007831 goto map;
7832
7833 if (write && async_submit) {
Josef Bacikc6100a42017-05-05 11:57:13 -04007834 ret = btrfs_wq_submit_bio(fs_info, bio, 0, 0,
7835 file_offset, inode,
David Sterbae288c082018-07-18 17:36:24 +02007836 btrfs_submit_bio_start_direct_io);
Miao Xiee65e1532010-11-22 03:04:43 +00007837 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04007838 } else if (write) {
7839 /*
7840 * If we aren't doing async submit, calculate the csum of the
7841 * bio now.
7842 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007843 ret = btrfs_csum_one_bio(inode, bio, file_offset, 1);
Josef Bacik1ae39932011-04-06 14:41:34 -04007844 if (ret)
7845 goto err;
Miao Xie23ea8e52014-09-12 18:43:54 +08007846 } else {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007847 ret = btrfs_lookup_and_bind_dio_csum(inode, dip, bio,
Miao Xiec1dc0892014-09-12 18:43:56 +08007848 file_offset);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00007849 if (ret)
7850 goto err;
7851 }
Josef Bacik1ae39932011-04-06 14:41:34 -04007852map:
Chris Mason08635ba2019-07-10 12:28:14 -07007853 ret = btrfs_map_bio(fs_info, bio, 0);
Miao Xiee65e1532010-11-22 03:04:43 +00007854err:
Miao Xiee65e1532010-11-22 03:04:43 +00007855 return ret;
7856}
7857
Nikolay Borisove6961ca2017-08-03 15:44:58 +03007858static int btrfs_submit_direct_hook(struct btrfs_dio_private *dip)
Miao Xiee65e1532010-11-22 03:04:43 +00007859{
7860 struct inode *inode = dip->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007861 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xiee65e1532010-11-22 03:04:43 +00007862 struct bio *bio;
7863 struct bio *orig_bio = dip->orig_bio;
Kent Overstreet4f024f32013-10-11 15:44:27 -07007864 u64 start_sector = orig_bio->bi_iter.bi_sector;
Miao Xiee65e1532010-11-22 03:04:43 +00007865 u64 file_offset = dip->logical_offset;
Josef Bacik1ae39932011-04-06 14:41:34 -04007866 int async_submit = 0;
Liu Bo725130b2017-05-16 09:51:39 -07007867 u64 submit_len;
7868 int clone_offset = 0;
7869 int clone_len;
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05307870 int ret;
Omar Sandoval58efbc92017-08-22 23:45:59 -07007871 blk_status_t status;
Nikolay Borisov89b798a2019-06-03 12:05:05 +03007872 struct btrfs_io_geometry geom;
Miao Xiee65e1532010-11-22 03:04:43 +00007873
Nikolay Borisov89b798a2019-06-03 12:05:05 +03007874 submit_len = orig_bio->bi_iter.bi_size;
7875 ret = btrfs_get_io_geometry(fs_info, btrfs_op(orig_bio),
7876 start_sector << 9, submit_len, &geom);
Miao Xie7a5c3c92014-06-17 18:58:59 +08007877 if (ret)
Miao Xiee65e1532010-11-22 03:04:43 +00007878 return -EIO;
Miao Xiefacc8a222013-07-25 19:22:34 +08007879
Nikolay Borisov89b798a2019-06-03 12:05:05 +03007880 if (geom.len >= submit_len) {
Josef Bacik02f57c72011-04-06 14:25:44 -04007881 bio = orig_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08007882 dip->flags |= BTRFS_DIO_ORIG_BIO_SUBMITTED;
Josef Bacik02f57c72011-04-06 14:25:44 -04007883 goto submit;
7884 }
7885
David Woodhouse53b381b2013-01-29 18:40:14 -05007886 /* async crcs make it difficult to collect full stripe writes. */
Jeff Mahoney1b868262017-05-17 11:38:35 -04007887 if (btrfs_data_alloc_profile(fs_info) & BTRFS_BLOCK_GROUP_RAID56_MASK)
David Woodhouse53b381b2013-01-29 18:40:14 -05007888 async_submit = 0;
7889 else
7890 async_submit = 1;
7891
Liu Bo725130b2017-05-16 09:51:39 -07007892 /* bio split */
Nikolay Borisov89b798a2019-06-03 12:05:05 +03007893 ASSERT(geom.len <= INT_MAX);
Josef Bacik02f57c72011-04-06 14:25:44 -04007894 atomic_inc(&dip->pending_bios);
Arnd Bergmann3c91ee62017-05-18 15:33:29 +02007895 do {
Nikolay Borisov89b798a2019-06-03 12:05:05 +03007896 clone_len = min_t(int, submit_len, geom.len);
Josef Bacik02f57c72011-04-06 14:25:44 -04007897
Liu Bo725130b2017-05-16 09:51:39 -07007898 /*
7899 * This will never fail as it's passing GPF_NOFS and
7900 * the allocation is backed by btrfs_bioset.
7901 */
Liu Boe4770942017-05-16 10:57:14 -07007902 bio = btrfs_bio_clone_partial(orig_bio, clone_offset,
Liu Bo725130b2017-05-16 09:51:39 -07007903 clone_len);
7904 bio->bi_private = dip;
7905 bio->bi_end_io = btrfs_end_dio_bio;
7906 btrfs_io_bio(bio)->logical = file_offset;
Miao Xiee65e1532010-11-22 03:04:43 +00007907
Liu Bo725130b2017-05-16 09:51:39 -07007908 ASSERT(submit_len >= clone_len);
7909 submit_len -= clone_len;
7910 if (submit_len == 0)
7911 break;
Miao Xiee65e1532010-11-22 03:04:43 +00007912
Liu Bo725130b2017-05-16 09:51:39 -07007913 /*
7914 * Increase the count before we submit the bio so we know
7915 * the end IO handler won't happen before we increase the
7916 * count. Otherwise, the dip might get freed before we're
7917 * done setting it up.
7918 */
7919 atomic_inc(&dip->pending_bios);
Miao Xiee65e1532010-11-22 03:04:43 +00007920
David Sterbad0ee3932018-03-08 14:35:48 +01007921 status = btrfs_submit_dio_bio(bio, inode, file_offset,
Omar Sandoval58efbc92017-08-22 23:45:59 -07007922 async_submit);
7923 if (status) {
Liu Bo725130b2017-05-16 09:51:39 -07007924 bio_put(bio);
7925 atomic_dec(&dip->pending_bios);
7926 goto out_err;
Miao Xiee65e1532010-11-22 03:04:43 +00007927 }
Liu Bo725130b2017-05-16 09:51:39 -07007928
7929 clone_offset += clone_len;
7930 start_sector += clone_len >> 9;
7931 file_offset += clone_len;
7932
Nikolay Borisov89b798a2019-06-03 12:05:05 +03007933 ret = btrfs_get_io_geometry(fs_info, btrfs_op(orig_bio),
7934 start_sector << 9, submit_len, &geom);
Liu Bo725130b2017-05-16 09:51:39 -07007935 if (ret)
7936 goto out_err;
Arnd Bergmann3c91ee62017-05-18 15:33:29 +02007937 } while (submit_len > 0);
Miao Xiee65e1532010-11-22 03:04:43 +00007938
Josef Bacik02f57c72011-04-06 14:25:44 -04007939submit:
David Sterbad0ee3932018-03-08 14:35:48 +01007940 status = btrfs_submit_dio_bio(bio, inode, file_offset, async_submit);
Omar Sandoval58efbc92017-08-22 23:45:59 -07007941 if (!status)
Miao Xiee65e1532010-11-22 03:04:43 +00007942 return 0;
7943
7944 bio_put(bio);
7945out_err:
7946 dip->errors = 1;
7947 /*
Nikolay Borisovde224b72018-02-14 10:53:36 +02007948 * Before atomic variable goto zero, we must make sure dip->errors is
7949 * perceived to be set. This ordering is ensured by the fact that an
7950 * atomic operations with a return value are fully ordered as per
7951 * atomic_t.txt
Miao Xiee65e1532010-11-22 03:04:43 +00007952 */
Miao Xiee65e1532010-11-22 03:04:43 +00007953 if (atomic_dec_and_test(&dip->pending_bios))
7954 bio_io_error(dip->orig_bio);
7955
7956 /* bio_end_io() will handle error, so we needn't return it */
7957 return 0;
7958}
7959
Mike Christie8a4c1e42016-06-05 14:31:50 -05007960static void btrfs_submit_direct(struct bio *dio_bio, struct inode *inode,
7961 loff_t file_offset)
Josef Bacik4b46fce2010-05-23 11:00:55 -04007962{
Filipe Manana61de7182015-07-01 12:13:10 +01007963 struct btrfs_dio_private *dip = NULL;
Liu Bo3892ac92017-04-17 15:00:28 -07007964 struct bio *bio = NULL;
7965 struct btrfs_io_bio *io_bio;
Mike Christie8a4c1e42016-06-05 14:31:50 -05007966 bool write = (bio_op(dio_bio) == REQ_OP_WRITE);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007967 int ret = 0;
7968
David Sterba8b6c1d52017-06-02 17:48:13 +02007969 bio = btrfs_bio_clone(dio_bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007970
Miao Xiec1dc0892014-09-12 18:43:56 +08007971 dip = kzalloc(sizeof(*dip), GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04007972 if (!dip) {
7973 ret = -ENOMEM;
Filipe Manana61de7182015-07-01 12:13:10 +01007974 goto free_ordered;
Chris Mason9be33952013-05-17 18:30:14 -04007975 }
7976
7977 dip->private = dio_bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007978 dip->inode = inode;
7979 dip->logical_offset = file_offset;
Kent Overstreet4f024f32013-10-11 15:44:27 -07007980 dip->bytes = dio_bio->bi_iter.bi_size;
7981 dip->disk_bytenr = (u64)dio_bio->bi_iter.bi_sector << 9;
Liu Bo3892ac92017-04-17 15:00:28 -07007982 bio->bi_private = dip;
7983 dip->orig_bio = bio;
Chris Mason9be33952013-05-17 18:30:14 -04007984 dip->dio_bio = dio_bio;
Miao Xiee65e1532010-11-22 03:04:43 +00007985 atomic_set(&dip->pending_bios, 0);
Liu Bo3892ac92017-04-17 15:00:28 -07007986 io_bio = btrfs_io_bio(bio);
7987 io_bio->logical = file_offset;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007988
Miao Xiec1dc0892014-09-12 18:43:56 +08007989 if (write) {
Liu Bo3892ac92017-04-17 15:00:28 -07007990 bio->bi_end_io = btrfs_endio_direct_write;
Miao Xiec1dc0892014-09-12 18:43:56 +08007991 } else {
Liu Bo3892ac92017-04-17 15:00:28 -07007992 bio->bi_end_io = btrfs_endio_direct_read;
Miao Xiec1dc0892014-09-12 18:43:56 +08007993 dip->subio_endio = btrfs_subio_endio_read;
7994 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007995
Filipe Mananaf28a4922015-12-08 19:23:20 +00007996 /*
7997 * Reset the range for unsubmitted ordered extents (to a 0 length range)
7998 * even if we fail to submit a bio, because in such case we do the
7999 * corresponding error handling below and it must not be done a second
8000 * time by btrfs_direct_IO().
8001 */
8002 if (write) {
8003 struct btrfs_dio_data *dio_data = current->journal_info;
8004
8005 dio_data->unsubmitted_oe_range_end = dip->logical_offset +
8006 dip->bytes;
8007 dio_data->unsubmitted_oe_range_start =
8008 dio_data->unsubmitted_oe_range_end;
8009 }
8010
Nikolay Borisove6961ca2017-08-03 15:44:58 +03008011 ret = btrfs_submit_direct_hook(dip);
Miao Xiee65e1532010-11-22 03:04:43 +00008012 if (!ret)
Chris Masoneaf25d92010-05-25 09:48:28 -04008013 return;
Chris Mason9be33952013-05-17 18:30:14 -04008014
David Sterbab3a0dd52018-11-22 17:16:49 +01008015 btrfs_io_bio_free_csum(io_bio);
Chris Mason9be33952013-05-17 18:30:14 -04008016
Josef Bacik4b46fce2010-05-23 11:00:55 -04008017free_ordered:
8018 /*
Filipe Manana61de7182015-07-01 12:13:10 +01008019 * If we arrived here it means either we failed to submit the dip
8020 * or we either failed to clone the dio_bio or failed to allocate the
8021 * dip. If we cloned the dio_bio and allocated the dip, we can just
8022 * call bio_endio against our io_bio so that we get proper resource
8023 * cleanup if we fail to submit the dip, otherwise, we must do the
8024 * same as btrfs_endio_direct_[write|read] because we can't call these
8025 * callbacks - they require an allocated dip and a clone of dio_bio.
Josef Bacik4b46fce2010-05-23 11:00:55 -04008026 */
Liu Bo3892ac92017-04-17 15:00:28 -07008027 if (bio && dip) {
Guoqing Jiang054ec2f2017-06-02 16:08:50 +08008028 bio_io_error(bio);
Filipe Manana61de7182015-07-01 12:13:10 +01008029 /*
Liu Bo3892ac92017-04-17 15:00:28 -07008030 * The end io callbacks free our dip, do the final put on bio
Filipe Manana61de7182015-07-01 12:13:10 +01008031 * and all the cleanup and final put for dio_bio (through
8032 * dio_end_io()).
8033 */
8034 dip = NULL;
Liu Bo3892ac92017-04-17 15:00:28 -07008035 bio = NULL;
Filipe Manana61de7182015-07-01 12:13:10 +01008036 } else {
Filipe Manana14543772015-11-24 16:23:54 +00008037 if (write)
Qu Wenruo524272602017-03-08 10:25:52 +08008038 __endio_write_update_ordered(inode,
Filipe Manana14543772015-11-24 16:23:54 +00008039 file_offset,
8040 dio_bio->bi_iter.bi_size,
Qu Wenruo524272602017-03-08 10:25:52 +08008041 false);
Filipe Manana14543772015-11-24 16:23:54 +00008042 else
Filipe Manana61de7182015-07-01 12:13:10 +01008043 unlock_extent(&BTRFS_I(inode)->io_tree, file_offset,
8044 file_offset + dio_bio->bi_iter.bi_size - 1);
Filipe Manana14543772015-11-24 16:23:54 +00008045
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008046 dio_bio->bi_status = BLK_STS_IOERR;
Filipe Manana61de7182015-07-01 12:13:10 +01008047 /*
8048 * Releases and cleans up our dio_bio, no need to bio_put()
8049 * nor bio_endio()/bio_io_error() against dio_bio.
8050 */
Christoph Hellwig40553512017-06-03 09:37:58 +02008051 dio_end_io(dio_bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008052 }
Liu Bo3892ac92017-04-17 15:00:28 -07008053 if (bio)
8054 bio_put(bio);
Filipe Manana61de7182015-07-01 12:13:10 +01008055 kfree(dip);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008056}
8057
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008058static ssize_t check_direct_IO(struct btrfs_fs_info *fs_info,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008059 const struct iov_iter *iter, loff_t offset)
Chris Mason5a5f79b2010-05-26 21:33:37 -04008060{
8061 int seg;
Josef Bacika1b75f72011-04-08 15:51:18 +00008062 int i;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008063 unsigned int blocksize_mask = fs_info->sectorsize - 1;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008064 ssize_t retval = -EINVAL;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008065
8066 if (offset & blocksize_mask)
8067 goto out;
8068
Al Viro28060d52014-03-22 05:15:17 -04008069 if (iov_iter_alignment(iter) & blocksize_mask)
8070 goto out;
Josef Bacika1b75f72011-04-08 15:51:18 +00008071
Al Viro28060d52014-03-22 05:15:17 -04008072 /* If this is a write we don't need to check anymore */
Al Virocd27e452016-10-10 13:39:05 -04008073 if (iov_iter_rw(iter) != READ || !iter_is_iovec(iter))
Al Viro28060d52014-03-22 05:15:17 -04008074 return 0;
8075 /*
8076 * Check to make sure we don't have duplicate iov_base's in this
8077 * iovec, if so return EINVAL, otherwise we'll get csum errors
8078 * when reading back.
8079 */
8080 for (seg = 0; seg < iter->nr_segs; seg++) {
8081 for (i = seg + 1; i < iter->nr_segs; i++) {
8082 if (iter->iov[seg].iov_base == iter->iov[i].iov_base)
Josef Bacika1b75f72011-04-08 15:51:18 +00008083 goto out;
8084 }
Chris Mason5a5f79b2010-05-26 21:33:37 -04008085 }
8086 retval = 0;
8087out:
8088 return retval;
8089}
Josef Bacikeb838e72012-07-31 16:28:48 -04008090
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008091static ssize_t btrfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
Chris Mason16432982008-04-10 10:23:21 -04008092{
Josef Bacik4b46fce2010-05-23 11:00:55 -04008093 struct file *file = iocb->ki_filp;
8094 struct inode *inode = file->f_mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008095 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
chandan50745b02015-08-28 21:10:13 +05308096 struct btrfs_dio_data dio_data = { 0 };
Qu Wenruo364ecf32017-02-27 15:10:38 +08008097 struct extent_changeset *data_reserved = NULL;
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008098 loff_t offset = iocb->ki_pos;
Miao Xie09348562013-02-07 10:12:07 +00008099 size_t count = 0;
Miao Xie2e60a512013-02-08 07:01:08 +00008100 int flags = 0;
Miao Xie38851cc2013-02-08 07:04:11 +00008101 bool wakeup = true;
8102 bool relock = false;
Miao Xie09348562013-02-07 10:12:07 +00008103 ssize_t ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008104
Nikolay Borisov8c70c9f2017-08-21 12:43:46 +03008105 if (check_direct_IO(fs_info, iter, offset))
Chris Mason5a5f79b2010-05-26 21:33:37 -04008106 return 0;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008107
Jens Axboefe0f07d2015-04-15 17:05:48 -06008108 inode_dio_begin(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008109
Josef Bacik0e267c42013-07-02 10:38:02 -04008110 /*
Miao Xie41bd9ca2014-03-06 13:54:57 +08008111 * The generic stuff only does filemap_write_and_wait_range, which
8112 * isn't enough if we've written compressed pages to this area, so
8113 * we need to flush the dirty pages again to make absolutely sure
8114 * that any outstanding dirty pages are on disk.
Josef Bacik0e267c42013-07-02 10:38:02 -04008115 */
Al Viroa6cbcd42014-03-04 22:38:00 -05008116 count = iov_iter_count(iter);
Miao Xie41bd9ca2014-03-06 13:54:57 +08008117 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
8118 &BTRFS_I(inode)->runtime_flags))
Wang Shilong9a025a02014-07-17 11:44:13 +08008119 filemap_fdatawrite_range(inode->i_mapping, offset,
8120 offset + count - 1);
Josef Bacik0e267c42013-07-02 10:38:02 -04008121
Omar Sandoval6f673762015-03-16 04:33:52 -07008122 if (iov_iter_rw(iter) == WRITE) {
Miao Xie38851cc2013-02-08 07:04:11 +00008123 /*
8124 * If the write DIO is beyond the EOF, we need update
8125 * the isize, but it is protected by i_mutex. So we can
8126 * not unlock the i_mutex at this case.
8127 */
8128 if (offset + count <= inode->i_size) {
Liu Bo4aaedfb2016-12-14 22:36:05 -08008129 dio_data.overwrite = 1;
Al Viro59551022016-01-22 15:40:57 -05008130 inode_unlock(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008131 relock = true;
Goldwyn Rodriguesedf064e2017-06-20 07:05:49 -05008132 } else if (iocb->ki_flags & IOCB_NOWAIT) {
8133 ret = -EAGAIN;
8134 goto out;
Miao Xie38851cc2013-02-08 07:04:11 +00008135 }
Qu Wenruo364ecf32017-02-27 15:10:38 +08008136 ret = btrfs_delalloc_reserve_space(inode, &data_reserved,
8137 offset, count);
Miao Xie09348562013-02-07 10:12:07 +00008138 if (ret)
Miao Xie38851cc2013-02-08 07:04:11 +00008139 goto out;
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008140
8141 /*
8142 * We need to know how many extents we reserved so that we can
8143 * do the accounting properly if we go over the number we
8144 * originally calculated. Abuse current->journal_info for this.
8145 */
Jeff Mahoneyda170662016-06-15 09:22:56 -04008146 dio_data.reserve = round_up(count,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008147 fs_info->sectorsize);
Filipe Mananaf28a4922015-12-08 19:23:20 +00008148 dio_data.unsubmitted_oe_range_start = (u64)offset;
8149 dio_data.unsubmitted_oe_range_end = (u64)offset;
chandan50745b02015-08-28 21:10:13 +05308150 current->journal_info = &dio_data;
Chandan Rajendra97dcdea2016-12-23 15:00:18 +05308151 down_read(&BTRFS_I(inode)->dio_sem);
David Sterbaee39b432014-09-30 01:33:33 +02008152 } else if (test_bit(BTRFS_INODE_READDIO_NEED_LOCK,
8153 &BTRFS_I(inode)->runtime_flags)) {
Jens Axboefe0f07d2015-04-15 17:05:48 -06008154 inode_dio_end(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008155 flags = DIO_LOCKING | DIO_SKIP_HOLES;
8156 wakeup = false;
Miao Xie09348562013-02-07 10:12:07 +00008157 }
8158
Omar Sandoval17f8c842015-03-16 04:33:50 -07008159 ret = __blockdev_direct_IO(iocb, inode,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008160 fs_info->fs_devices->latest_bdev,
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008161 iter, btrfs_get_blocks_direct, NULL,
Omar Sandoval17f8c842015-03-16 04:33:50 -07008162 btrfs_submit_direct, flags);
Omar Sandoval6f673762015-03-16 04:33:52 -07008163 if (iov_iter_rw(iter) == WRITE) {
Chandan Rajendra97dcdea2016-12-23 15:00:18 +05308164 up_read(&BTRFS_I(inode)->dio_sem);
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008165 current->journal_info = NULL;
Liu Boddba1bf2015-06-17 16:59:58 +08008166 if (ret < 0 && ret != -EIOCBQUEUED) {
chandan50745b02015-08-28 21:10:13 +05308167 if (dio_data.reserve)
Qu Wenruobc42bda2017-02-27 15:10:39 +08008168 btrfs_delalloc_release_space(inode, data_reserved,
Qu Wenruo43b18592017-12-12 15:34:32 +08008169 offset, dio_data.reserve, true);
Filipe Mananaf28a4922015-12-08 19:23:20 +00008170 /*
8171 * On error we might have left some ordered extents
8172 * without submitting corresponding bios for them, so
8173 * cleanup them up to avoid other tasks getting them
8174 * and waiting for them to complete forever.
8175 */
8176 if (dio_data.unsubmitted_oe_range_start <
8177 dio_data.unsubmitted_oe_range_end)
Qu Wenruo524272602017-03-08 10:25:52 +08008178 __endio_write_update_ordered(inode,
Filipe Mananaf28a4922015-12-08 19:23:20 +00008179 dio_data.unsubmitted_oe_range_start,
8180 dio_data.unsubmitted_oe_range_end -
8181 dio_data.unsubmitted_oe_range_start,
Qu Wenruo524272602017-03-08 10:25:52 +08008182 false);
Liu Boddba1bf2015-06-17 16:59:58 +08008183 } else if (ret >= 0 && (size_t)ret < count)
Qu Wenruobc42bda2017-02-27 15:10:39 +08008184 btrfs_delalloc_release_space(inode, data_reserved,
Qu Wenruo43b18592017-12-12 15:34:32 +08008185 offset, count - (size_t)ret, true);
Qu Wenruo8702ba92019-10-14 14:34:51 +08008186 btrfs_delalloc_release_extents(BTRFS_I(inode), count);
Miao Xie09348562013-02-07 10:12:07 +00008187 }
Miao Xie38851cc2013-02-08 07:04:11 +00008188out:
Miao Xie2e60a512013-02-08 07:01:08 +00008189 if (wakeup)
Jens Axboefe0f07d2015-04-15 17:05:48 -06008190 inode_dio_end(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008191 if (relock)
Al Viro59551022016-01-22 15:40:57 -05008192 inode_lock(inode);
Miao Xie09348562013-02-07 10:12:07 +00008193
Qu Wenruo364ecf32017-02-27 15:10:38 +08008194 extent_changeset_free(data_reserved);
Miao Xie09348562013-02-07 10:12:07 +00008195 return ret;
Chris Mason16432982008-04-10 10:23:21 -04008196}
8197
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008198#define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
8199
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008200static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
8201 __u64 start, __u64 len)
8202{
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008203 int ret;
8204
8205 ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS);
8206 if (ret)
8207 return ret;
8208
David Sterba2135fb92017-06-23 04:09:57 +02008209 return extent_fiemap(inode, fieinfo, start, len);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008210}
8211
Chris Mason9ebefb182007-06-15 13:50:00 -04008212int btrfs_readpage(struct file *file, struct page *page)
8213{
Chris Masond1310b22008-01-24 16:13:08 -05008214 struct extent_io_tree *tree;
8215 tree = &BTRFS_I(page->mapping->host)->io_tree;
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02008216 return extent_read_full_page(tree, page, btrfs_get_extent, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04008217}
Chris Mason1832a6d2007-12-21 16:27:21 -05008218
Chris Mason39279cc2007-06-12 06:35:45 -04008219static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
8220{
Josef Bacikbe7bd732015-10-22 15:05:09 -04008221 struct inode *inode = page->mapping->host;
8222 int ret;
Chris Masonb888db22007-08-27 16:49:44 -04008223
8224 if (current->flags & PF_MEMALLOC) {
8225 redirty_page_for_writepage(wbc, page);
8226 unlock_page(page);
8227 return 0;
8228 }
Josef Bacikbe7bd732015-10-22 15:05:09 -04008229
8230 /*
8231 * If we are under memory pressure we will call this directly from the
8232 * VM, we need to make sure we have the inode referenced for the ordered
8233 * extent. If not just return like we didn't do anything.
8234 */
8235 if (!igrab(inode)) {
8236 redirty_page_for_writepage(wbc, page);
8237 return AOP_WRITEPAGE_ACTIVATE;
8238 }
Nikolay Borisov0a9b0e52017-12-08 15:55:59 +02008239 ret = extent_write_full_page(page, wbc);
Josef Bacikbe7bd732015-10-22 15:05:09 -04008240 btrfs_add_delayed_iput(inode);
8241 return ret;
Chris Masona52d9a82007-08-27 16:49:44 -04008242}
Chris Mason39279cc2007-06-12 06:35:45 -04008243
Eric Sandeen48a3b632013-04-25 20:41:01 +00008244static int btrfs_writepages(struct address_space *mapping,
8245 struct writeback_control *wbc)
Chris Masonb293f02e2007-11-01 19:45:34 -04008246{
Nikolay Borisov8ae225a2018-04-19 10:46:38 +03008247 return extent_writepages(mapping, wbc);
Chris Masonb293f02e2007-11-01 19:45:34 -04008248}
8249
Chris Mason3ab2fb52007-11-08 10:59:22 -05008250static int
8251btrfs_readpages(struct file *file, struct address_space *mapping,
8252 struct list_head *pages, unsigned nr_pages)
8253{
Nikolay Borisov2a3ff0a2018-04-19 10:46:36 +03008254 return extent_readpages(mapping, pages, nr_pages);
Chris Mason3ab2fb52007-11-08 10:59:22 -05008255}
Nikolay Borisov2a3ff0a2018-04-19 10:46:36 +03008256
Chris Masone6dcd2d2008-07-17 12:53:50 -04008257static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04008258{
Nikolay Borisov477a30b2018-04-19 10:46:34 +03008259 int ret = try_release_extent_mapping(page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04008260 if (ret == 1) {
8261 ClearPagePrivate(page);
8262 set_page_private(page, 0);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008263 put_page(page);
Chris Masona52d9a82007-08-27 16:49:44 -04008264 }
8265 return ret;
8266}
Chris Mason39279cc2007-06-12 06:35:45 -04008267
Chris Masone6dcd2d2008-07-17 12:53:50 -04008268static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
8269{
Chris Mason98509cf2008-09-11 15:51:43 -04008270 if (PageWriteback(page) || PageDirty(page))
8271 return 0;
Michal Hocko3ba7ab22017-01-09 15:39:02 +01008272 return __btrfs_releasepage(page, gfp_flags);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008273}
8274
Lukas Czernerd47992f2013-05-21 23:17:23 -04008275static void btrfs_invalidatepage(struct page *page, unsigned int offset,
8276 unsigned int length)
Chris Masona52d9a82007-08-27 16:49:44 -04008277{
Josef Bacik5fd02042012-05-02 14:00:54 -04008278 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05008279 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008280 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008281 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008282 u64 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008283 u64 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308284 u64 start;
8285 u64 end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008286 int inode_evicting = inode->i_state & I_FREEING;
Chris Masona52d9a82007-08-27 16:49:44 -04008287
Chris Mason8b62b722009-09-02 16:53:46 -04008288 /*
8289 * we have the page locked, so new writeback can't start,
8290 * and the dirty bit won't be cleared while we are here.
8291 *
8292 * Wait for IO on this page so that we can safely clear
8293 * the PagePrivate2 bit and do ordered accounting
8294 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008295 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04008296
Josef Bacik5fd02042012-05-02 14:00:54 -04008297 tree = &BTRFS_I(inode)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008298 if (offset) {
8299 btrfs_releasepage(page, GFP_NOFS);
8300 return;
8301 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008302
8303 if (!inode_evicting)
David Sterbaff13db42015-12-03 14:30:40 +01008304 lock_extent_bits(tree, page_start, page_end, &cached_state);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308305again:
8306 start = page_start;
Nikolay Borisova776c6f2017-02-20 13:50:49 +02008307 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), start,
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308308 page_end - start + 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008309 if (ordered) {
Omar Sandovalbffe6332019-12-02 17:34:19 -08008310 end = min(page_end,
8311 ordered->file_offset + ordered->num_bytes - 1);
Chris Masoneb84ae02008-07-17 13:53:27 -04008312 /*
8313 * IO on this page will never be started, so we need
8314 * to account for any ordered extents now
8315 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008316 if (!inode_evicting)
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308317 clear_extent_bit(tree, start, end,
Omar Sandovale1821632019-08-15 14:04:04 -07008318 EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008319 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
David Sterbaae0f1622017-10-31 16:37:52 +01008320 EXTENT_DEFRAG, 1, 0, &cached_state);
Chris Mason8b62b722009-09-02 16:53:46 -04008321 /*
8322 * whoever cleared the private bit is responsible
8323 * for the finish_ordered_io
8324 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04008325 if (TestClearPagePrivate2(page)) {
8326 struct btrfs_ordered_inode_tree *tree;
8327 u64 new_len;
8328
8329 tree = &BTRFS_I(inode)->ordered_tree;
8330
8331 spin_lock_irq(&tree->lock);
8332 set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308333 new_len = start - ordered->file_offset;
Josef Bacik77cef2e2013-08-29 13:57:21 -04008334 if (new_len < ordered->truncated_len)
8335 ordered->truncated_len = new_len;
8336 spin_unlock_irq(&tree->lock);
8337
8338 if (btrfs_dec_test_ordered_pending(inode, &ordered,
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308339 start,
8340 end - start + 1, 1))
Josef Bacik77cef2e2013-08-29 13:57:21 -04008341 btrfs_finish_ordered_io(ordered);
Chris Mason8b62b722009-09-02 16:53:46 -04008342 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008343 btrfs_put_ordered_extent(ordered);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008344 if (!inode_evicting) {
8345 cached_state = NULL;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308346 lock_extent_bits(tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008347 &cached_state);
8348 }
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308349
8350 start = end + 1;
8351 if (start < page_end)
8352 goto again;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008353 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008354
Qu Wenruob9d0b382015-09-29 10:35:16 +08008355 /*
8356 * Qgroup reserved space handler
8357 * Page here will be either
8358 * 1) Already written to disk
8359 * In this case, its reserved space is released from data rsv map
8360 * and will be freed by delayed_ref handler finally.
8361 * So even we call qgroup_free_data(), it won't decrease reserved
8362 * space.
8363 * 2) Not written to disk
Goldwyn Rodrigues0b34c262016-09-30 10:40:52 -05008364 * This means the reserved space should be freed here. However,
8365 * if a truncate invalidates the page (by clearing PageDirty)
8366 * and the page is accounted for while allocating extent
8367 * in btrfs_check_data_free_space() we let delayed_ref to
8368 * free the entire extent.
Qu Wenruob9d0b382015-09-29 10:35:16 +08008369 */
Goldwyn Rodrigues0b34c262016-09-30 10:40:52 -05008370 if (PageDirty(page))
Qu Wenruobc42bda2017-02-27 15:10:39 +08008371 btrfs_qgroup_free_data(inode, NULL, page_start, PAGE_SIZE);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008372 if (!inode_evicting) {
Omar Sandovale1821632019-08-15 14:04:04 -07008373 clear_extent_bit(tree, page_start, page_end, EXTENT_LOCKED |
Filipe Mananaa7e3b972017-04-03 10:45:46 +01008374 EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
8375 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1,
David Sterbaae0f1622017-10-31 16:37:52 +01008376 &cached_state);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008377
8378 __btrfs_releasepage(page, GFP_NOFS);
8379 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008380
Chris Mason4a096752008-07-21 10:29:44 -04008381 ClearPageChecked(page);
Chris Mason9ad6b7bc2008-04-18 16:11:30 -04008382 if (PagePrivate(page)) {
Chris Mason9ad6b7bc2008-04-18 16:11:30 -04008383 ClearPagePrivate(page);
8384 set_page_private(page, 0);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008385 put_page(page);
Chris Mason9ad6b7bc2008-04-18 16:11:30 -04008386 }
Chris Mason39279cc2007-06-12 06:35:45 -04008387}
8388
Chris Mason9ebefb182007-06-15 13:50:00 -04008389/*
8390 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
8391 * called from a page fault handler when a page is first dirtied. Hence we must
8392 * be careful to check for EOF conditions here. We set the page up correctly
8393 * for a written page which means we get ENOSPC checking when writing into
8394 * holes and correct delalloc and unwritten extent mapping on filesystems that
8395 * support these features.
8396 *
8397 * We are not allowed to take the i_mutex here so we have to play games to
8398 * protect against truncate races as the page could now be beyond EOF. Because
Omar Sandovald1342aa2018-05-11 13:13:29 -07008399 * truncate_setsize() writes the inode size before removing pages, once we have
8400 * the page lock we can determine safely if the page is beyond EOF. If it is not
Chris Mason9ebefb182007-06-15 13:50:00 -04008401 * beyond EOF, then the page is guaranteed safe against truncation until we
8402 * unlock the page.
8403 */
Souptick Joardera528a242018-06-06 19:54:44 +05308404vm_fault_t btrfs_page_mkwrite(struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04008405{
Nick Pigginc2ec1752009-03-31 15:23:21 -07008406 struct page *page = vmf->page;
Dave Jiang11bac802017-02-24 14:56:41 -08008407 struct inode *inode = file_inode(vmf->vma->vm_file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008408 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008409 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
8410 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008411 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08008412 struct extent_changeset *data_reserved = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008413 char *kaddr;
8414 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04008415 loff_t size;
Souptick Joardera528a242018-06-06 19:54:44 +05308416 vm_fault_t ret;
8417 int ret2;
Chris Mason9998eb72012-01-25 13:47:40 -05008418 int reserved = 0;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308419 u64 reserved_space;
Chris Masona52d9a82007-08-27 16:49:44 -04008420 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008421 u64 page_end;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308422 u64 end;
8423
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008424 reserved_space = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04008425
Jan Karab2b5ef52012-06-12 16:20:45 +02008426 sb_start_pagefault(inode->i_sb);
Qu Wenruodf480632015-09-08 17:25:54 +08008427 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008428 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308429 end = page_end;
Qu Wenruodf480632015-09-08 17:25:54 +08008430
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308431 /*
8432 * Reserving delalloc space after obtaining the page lock can lead to
8433 * deadlock. For example, if a dirty page is locked by this function
8434 * and the call to btrfs_delalloc_reserve_space() ends up triggering
8435 * dirty page write out, then the btrfs_writepage() function could
8436 * end up waiting indefinitely to get a lock on the page currently
8437 * being processed by btrfs_page_mkwrite() function.
8438 */
Souptick Joardera528a242018-06-06 19:54:44 +05308439 ret2 = btrfs_delalloc_reserve_space(inode, &data_reserved, page_start,
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308440 reserved_space);
Souptick Joardera528a242018-06-06 19:54:44 +05308441 if (!ret2) {
8442 ret2 = file_update_time(vmf->vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05008443 reserved = 1;
8444 }
Souptick Joardera528a242018-06-06 19:54:44 +05308445 if (ret2) {
8446 ret = vmf_error(ret2);
Chris Mason9998eb72012-01-25 13:47:40 -05008447 if (reserved)
8448 goto out;
8449 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07008450 }
Chris Mason1832a6d2007-12-21 16:27:21 -05008451
Nick Piggin56a76f82009-03-31 15:23:23 -07008452 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008453again:
Chris Mason9ebefb182007-06-15 13:50:00 -04008454 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04008455 size = i_size_read(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04008456
Chris Mason9ebefb182007-06-15 13:50:00 -04008457 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04008458 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04008459 /* page got truncated out from underneath us */
8460 goto out_unlock;
8461 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008462 wait_on_page_writeback(page);
8463
David Sterbaff13db42015-12-03 14:30:40 +01008464 lock_extent_bits(io_tree, page_start, page_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008465 set_page_extent_mapped(page);
8466
Chris Masoneb84ae02008-07-17 13:53:27 -04008467 /*
8468 * we can't set the delalloc bits if there are pending ordered
8469 * extents. Drop our locks and wait for them to finish
8470 */
Nikolay Borisova776c6f2017-02-20 13:50:49 +02008471 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start,
8472 PAGE_SIZE);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008473 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00008474 unlock_extent_cached(io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01008475 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008476 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04008477 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008478 btrfs_put_ordered_extent(ordered);
8479 goto again;
8480 }
8481
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008482 if (page->index == ((size - 1) >> PAGE_SHIFT)) {
Jeff Mahoneyda170662016-06-15 09:22:56 -04008483 reserved_space = round_up(size - page_start,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008484 fs_info->sectorsize);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008485 if (reserved_space < PAGE_SIZE) {
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308486 end = page_start + reserved_space - 1;
Qu Wenruobc42bda2017-02-27 15:10:39 +08008487 btrfs_delalloc_release_space(inode, data_reserved,
Qu Wenruo43b18592017-12-12 15:34:32 +08008488 page_start, PAGE_SIZE - reserved_space,
8489 true);
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308490 }
8491 }
8492
Josef Bacikfbf19082009-10-01 17:10:23 -04008493 /*
Liu Bo54160342016-12-13 12:15:19 -08008494 * page_mkwrite gets called when the page is firstly dirtied after it's
8495 * faulted in, but write(2) could also dirty a page and set delalloc
8496 * bits, thus in this case for space account reason, we still need to
8497 * clear any delalloc bits within this page range since we have to
8498 * reserve data&meta space before lock_page() (see above comments).
Josef Bacikfbf19082009-10-01 17:10:23 -04008499 */
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308500 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, end,
Omar Sandovale1821632019-08-15 14:04:04 -07008501 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
8502 EXTENT_DEFRAG, 0, 0, &cached_state);
Josef Bacikfbf19082009-10-01 17:10:23 -04008503
Souptick Joardera528a242018-06-06 19:54:44 +05308504 ret2 = btrfs_set_extent_delalloc(inode, page_start, end, 0,
Nikolay Borisov330a5822019-07-17 16:18:17 +03008505 &cached_state);
Souptick Joardera528a242018-06-06 19:54:44 +05308506 if (ret2) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00008507 unlock_extent_cached(io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01008508 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04008509 ret = VM_FAULT_SIGBUS;
8510 goto out_unlock;
8511 }
Chris Mason9ebefb182007-06-15 13:50:00 -04008512
8513 /* page is wholly or partially inside EOF */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008514 if (page_start + PAGE_SIZE > size)
Johannes Thumshirn70730172018-12-05 15:23:03 +01008515 zero_start = offset_in_page(size);
Chris Mason9ebefb182007-06-15 13:50:00 -04008516 else
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008517 zero_start = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04008518
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008519 if (zero_start != PAGE_SIZE) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04008520 kaddr = kmap(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008521 memset(kaddr + zero_start, 0, PAGE_SIZE - zero_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008522 flush_dcache_page(page);
8523 kunmap(page);
8524 }
Chris Mason247e7432008-07-17 12:53:51 -04008525 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008526 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04008527 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04008528
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008529 BTRFS_I(inode)->last_trans = fs_info->generation;
Chris Mason257c62e2009-10-13 13:21:08 -04008530 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06008531 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
Chris Mason257c62e2009-10-13 13:21:08 -04008532
David Sterbae43bbe52017-12-12 21:43:52 +01008533 unlock_extent_cached(io_tree, page_start, page_end, &cached_state);
Chris Mason9ebefb182007-06-15 13:50:00 -04008534
Yunfeng Ye76de60e2019-12-03 16:59:25 +08008535 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE);
8536 sb_end_pagefault(inode->i_sb);
8537 extent_changeset_free(data_reserved);
8538 return VM_FAULT_LOCKED;
Chris Mason717beb92018-06-25 10:03:41 -07008539
8540out_unlock:
Chris Mason9ebefb182007-06-15 13:50:00 -04008541 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05008542out:
Qu Wenruo8702ba92019-10-14 14:34:51 +08008543 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE);
Qu Wenruobc42bda2017-02-27 15:10:39 +08008544 btrfs_delalloc_release_space(inode, data_reserved, page_start,
Qu Wenruo43b18592017-12-12 15:34:32 +08008545 reserved_space, (ret != 0));
Chris Mason9998eb72012-01-25 13:47:40 -05008546out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02008547 sb_end_pagefault(inode->i_sb);
Qu Wenruo364ecf32017-02-27 15:10:38 +08008548 extent_changeset_free(data_reserved);
Chris Mason9ebefb182007-06-15 13:50:00 -04008549 return ret;
8550}
8551
Filipe Manana213e8c52018-02-06 20:40:31 +00008552static int btrfs_truncate(struct inode *inode, bool skip_writeback)
Chris Mason39279cc2007-06-12 06:35:45 -04008553{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008554 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04008555 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04008556 struct btrfs_block_rsv *rsv;
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008557 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04008558 struct btrfs_trans_handle *trans;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008559 u64 mask = fs_info->sectorsize - 1;
Josef Bacik2bd36e72019-08-22 15:14:33 -04008560 u64 min_size = btrfs_calc_metadata_size(fs_info, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04008561
Filipe Manana213e8c52018-02-06 20:40:31 +00008562 if (!skip_writeback) {
8563 ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask),
8564 (u64)-1);
8565 if (ret)
8566 return ret;
8567 }
Chris Mason39279cc2007-06-12 06:35:45 -04008568
Josef Bacikfcb80c22011-05-03 10:40:22 -04008569 /*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008570 * Yes ladies and gentlemen, this is indeed ugly. We have a couple of
8571 * things going on here:
Josef Bacikfcb80c22011-05-03 10:40:22 -04008572 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008573 * 1) We need to reserve space to update our inode.
Josef Bacikfcb80c22011-05-03 10:40:22 -04008574 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008575 * 2) We need to have something to cache all the space that is going to
Josef Bacikfcb80c22011-05-03 10:40:22 -04008576 * be free'd up by the truncate operation, but also have some slack
8577 * space reserved in case it uses space during the truncate (thank you
8578 * very much snapshotting).
8579 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008580 * And we need these to be separate. The fact is we can use a lot of
Josef Bacikfcb80c22011-05-03 10:40:22 -04008581 * space doing the truncate, and we have no earthly idea how much space
Nicholas D Steeves01327612016-05-19 21:18:45 -04008582 * we will use, so we need the truncate reservation to be separate so it
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008583 * doesn't end up using space reserved for updating the inode. We also
8584 * need to be able to stop the transaction and start a new one, which
8585 * means we need to be able to update the inode several times, and we
8586 * have no idea of knowing how many times that will be, so we can't just
8587 * reserve 1 item for the entirety of the operation, so that has to be
8588 * done separately as well.
Josef Bacikfcb80c22011-05-03 10:40:22 -04008589 *
8590 * So that leaves us with
8591 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008592 * 1) rsv - for the truncate reservation, which we will steal from the
Josef Bacikfcb80c22011-05-03 10:40:22 -04008593 * transaction reservation.
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008594 * 2) fs_info->trans_block_rsv - this will have 1 items worth left for
Josef Bacikfcb80c22011-05-03 10:40:22 -04008595 * updating the inode.
8596 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008597 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008598 if (!rsv)
8599 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04008600 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04008601 rsv->failfast = 1;
Josef Bacikf0cd8462011-03-04 14:37:08 -05008602
Josef Bacik907cbce2011-08-08 13:46:15 -04008603 /*
Josef Bacik07127182011-08-19 10:29:59 -04008604 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04008605 * 1 for updating the inode.
8606 */
Josef Bacikf3fe8202013-01-07 17:03:21 -05008607 trans = btrfs_start_transaction(root, 2);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008608 if (IS_ERR(trans)) {
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008609 ret = PTR_ERR(trans);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008610 goto out;
8611 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05008612
Josef Bacik907cbce2011-08-08 13:46:15 -04008613 /* Migrate the slack space for the truncate to our reserve */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008614 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, rsv,
Lu Fengqi3a584172018-08-04 21:10:55 +08008615 min_size, false);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008616 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05008617
Chris Mason5a3f23d2009-03-31 13:27:11 -04008618 /*
Josef Bacik5dc562c2012-08-17 13:14:17 -04008619 * So if we truncate and then write and fsync we normally would just
8620 * write the extents that changed, which is a problem if we need to
8621 * first truncate that entire inode. So set this flag so we write out
8622 * all of the extents in the inode to the sync log so we're completely
8623 * safe.
8624 */
8625 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008626 trans->block_rsv = rsv;
Josef Bacik5dc562c2012-08-17 13:14:17 -04008627
Yan, Zheng80825102009-11-12 09:35:36 +00008628 while (1) {
8629 ret = btrfs_truncate_inode_items(trans, root, inode,
8630 inode->i_size,
8631 BTRFS_EXTENT_DATA_KEY);
Josef Bacikddfae632017-10-19 14:16:02 -04008632 trans->block_rsv = &fs_info->trans_block_rsv;
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008633 if (ret != -ENOSPC && ret != -EAGAIN)
Yan, Zheng80825102009-11-12 09:35:36 +00008634 break;
Chris Mason39279cc2007-06-12 06:35:45 -04008635
Yan, Zheng80825102009-11-12 09:35:36 +00008636 ret = btrfs_update_inode(trans, root, inode);
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008637 if (ret)
Josef Bacik3893e332011-01-31 16:03:11 -05008638 break;
Josef Bacikca7e70f2012-08-27 17:48:15 -04008639
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04008640 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008641 btrfs_btree_balance_dirty(fs_info);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008642
8643 trans = btrfs_start_transaction(root, 2);
8644 if (IS_ERR(trans)) {
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008645 ret = PTR_ERR(trans);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008646 trans = NULL;
8647 break;
8648 }
8649
Wang Xiaoguang47b5d642016-09-07 20:17:38 +08008650 btrfs_block_rsv_release(fs_info, rsv, -1);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008651 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv,
Lu Fengqi3a584172018-08-04 21:10:55 +08008652 rsv, min_size, false);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008653 BUG_ON(ret); /* shouldn't happen */
8654 trans->block_rsv = rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00008655 }
8656
Josef Bacikddfae632017-10-19 14:16:02 -04008657 /*
8658 * We can't call btrfs_truncate_block inside a trans handle as we could
8659 * deadlock with freeze, if we got NEED_TRUNCATE_BLOCK then we know
8660 * we've truncated everything except the last little bit, and can do
8661 * btrfs_truncate_block and then update the disk_i_size.
8662 */
8663 if (ret == NEED_TRUNCATE_BLOCK) {
8664 btrfs_end_transaction(trans);
8665 btrfs_btree_balance_dirty(fs_info);
8666
8667 ret = btrfs_truncate_block(inode, inode->i_size, 0, 0);
8668 if (ret)
8669 goto out;
8670 trans = btrfs_start_transaction(root, 1);
8671 if (IS_ERR(trans)) {
8672 ret = PTR_ERR(trans);
8673 goto out;
8674 }
8675 btrfs_ordered_update_i_size(inode, inode->i_size, NULL);
8676 }
8677
Chris Mason917c16b2011-11-08 14:49:59 -05008678 if (trans) {
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008679 int ret2;
Josef Bacik7b128762008-07-24 12:17:14 -04008680
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008681 trans->block_rsv = &fs_info->trans_block_rsv;
8682 ret2 = btrfs_update_inode(trans, root, inode);
8683 if (ret2 && !ret)
8684 ret = ret2;
8685
8686 ret2 = btrfs_end_transaction(trans);
8687 if (ret2 && !ret)
8688 ret = ret2;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008689 btrfs_btree_balance_dirty(fs_info);
Chris Mason917c16b2011-11-08 14:49:59 -05008690 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04008691out:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008692 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008693
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008694 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04008695}
8696
Sven Wegener3b963622008-06-09 21:57:42 -04008697/*
Chris Masond352ac62008-09-29 15:18:18 -04008698 * create a new subvolume directory/inode (helper for the ioctl).
8699 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05008700int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00008701 struct btrfs_root *new_root,
8702 struct btrfs_root *parent_root,
8703 u64 new_dirid)
Chris Mason39279cc2007-06-12 06:35:45 -04008704{
Chris Mason39279cc2007-06-12 06:35:45 -04008705 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04008706 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04008707 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04008708
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01008709 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
8710 new_dirid, new_dirid,
8711 S_IFDIR | (~current_umask() & S_IRWXUGO),
8712 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04008713 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04008714 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008715 inode->i_op = &btrfs_dir_inode_operations;
8716 inode->i_fop = &btrfs_dir_file_operations;
8717
Miklos Szeredibfe86842011-10-28 14:13:29 +02008718 set_nlink(inode, 1);
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02008719 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Masonb0d5d102014-09-08 13:08:51 -07008720 unlock_new_inode(inode);
Sven Wegener3b963622008-06-09 21:57:42 -04008721
Filipe David Borba Manana63541922014-01-07 11:47:46 +00008722 err = btrfs_subvol_inherit_props(trans, new_root, parent_root);
8723 if (err)
8724 btrfs_err(new_root->fs_info,
David Sterba351fd352014-05-15 16:48:20 +02008725 "error inheriting subvolume %llu properties: %d",
Filipe David Borba Manana63541922014-01-07 11:47:46 +00008726 new_root->root_key.objectid, err);
8727
Yan, Zheng76dda932009-09-21 16:00:26 -04008728 err = btrfs_update_inode(trans, new_root, inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04008729
Yan, Zheng76dda932009-09-21 16:00:26 -04008730 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07008731 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04008732}
8733
Chris Mason39279cc2007-06-12 06:35:45 -04008734struct inode *btrfs_alloc_inode(struct super_block *sb)
8735{
Josef Bacik69fe2d72017-10-19 14:15:57 -04008736 struct btrfs_fs_info *fs_info = btrfs_sb(sb);
Chris Mason39279cc2007-06-12 06:35:45 -04008737 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008738 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04008739
David Sterba712e36c2017-10-31 17:08:27 +01008740 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_KERNEL);
Chris Mason39279cc2007-06-12 06:35:45 -04008741 if (!ei)
8742 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008743
8744 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008745 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04008746 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04008747 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04008748 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008749 ei->delalloc_bytes = 0;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01008750 ei->new_delalloc_bytes = 0;
Wang Shilong47059d92014-07-03 18:22:07 +08008751 ei->defrag_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008752 ei->disk_i_size = 0;
8753 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04008754 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008755 ei->index_cnt = (u64)-1;
Miao Xie67de1172013-12-26 13:07:06 +08008756 ei->dir_index = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008757 ei->last_unlink_trans = 0;
Liu Bo46d8bc32012-08-29 01:07:55 -06008758 ei->last_log_commit = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008759
Josef Bacik9e0baf62011-07-15 15:16:44 +00008760 spin_lock_init(&ei->lock);
8761 ei->outstanding_extents = 0;
Josef Bacik69fe2d72017-10-19 14:15:57 -04008762 if (sb->s_magic != BTRFS_TEST_MAGIC)
8763 btrfs_init_metadata_block_rsv(fs_info, &ei->block_rsv,
8764 BTRFS_BLOCK_RSV_DELALLOC);
Josef Bacik72ac3c02012-05-23 14:13:11 -04008765 ei->runtime_flags = 0;
David Sterbab52aa8c2017-07-17 19:17:20 +02008766 ei->prop_compress = BTRFS_COMPRESS_NONE;
David Sterbaeec63c62017-07-17 19:41:31 +02008767 ei->defrag_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008768
Miao Xie16cdcec2011-04-22 18:12:22 +08008769 ei->delayed_node = NULL;
8770
chandan r9cc97d62012-07-04 12:48:07 +05308771 ei->i_otime.tv_sec = 0;
8772 ei->i_otime.tv_nsec = 0;
8773
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008774 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02008775 extent_map_tree_init(&ei->extent_tree);
Qu Wenruo43eb5f22019-03-01 10:47:59 +08008776 extent_io_tree_init(fs_info, &ei->io_tree, IO_TREE_INODE_IO, inode);
8777 extent_io_tree_init(fs_info, &ei->io_failure_tree,
8778 IO_TREE_INODE_IO_FAILURE, inode);
David Sterba7b439732019-03-11 15:58:30 +01008779 ei->io_tree.track_uptodate = true;
8780 ei->io_failure_tree.track_uptodate = true;
Josef Bacikb812ce22012-11-16 13:56:32 -05008781 atomic_set(&ei->sync_writers, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008782 mutex_init(&ei->log_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008783 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008784 INIT_LIST_HEAD(&ei->delalloc_inodes);
David Sterba8089fe62015-11-19 14:15:51 +01008785 INIT_LIST_HEAD(&ei->delayed_iput);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008786 RB_CLEAR_NODE(&ei->rb_node);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01008787 init_rwsem(&ei->dio_sem);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008788
8789 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04008790}
8791
Josef Bacikaaedb552013-10-11 14:44:09 -04008792#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
8793void btrfs_test_destroy_inode(struct inode *inode)
8794{
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02008795 btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0);
Josef Bacikaaedb552013-10-11 14:44:09 -04008796 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
8797}
8798#endif
8799
Al Viro26602ca2019-04-10 15:14:41 -04008800void btrfs_free_inode(struct inode *inode)
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11008801{
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11008802 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
8803}
8804
Chris Mason39279cc2007-06-12 06:35:45 -04008805void btrfs_destroy_inode(struct inode *inode)
8806{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008807 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008808 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04008809 struct btrfs_root *root = BTRFS_I(inode)->root;
8810
Al Virob3d9b7a2012-06-09 13:51:19 -04008811 WARN_ON(!hlist_empty(&inode->i_dentry));
Chris Mason39279cc2007-06-12 06:35:45 -04008812 WARN_ON(inode->i_data.nrpages);
Josef Bacik69fe2d72017-10-19 14:15:57 -04008813 WARN_ON(BTRFS_I(inode)->block_rsv.reserved);
8814 WARN_ON(BTRFS_I(inode)->block_rsv.size);
Josef Bacik9e0baf62011-07-15 15:16:44 +00008815 WARN_ON(BTRFS_I(inode)->outstanding_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04008816 WARN_ON(BTRFS_I(inode)->delalloc_bytes);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01008817 WARN_ON(BTRFS_I(inode)->new_delalloc_bytes);
Josef Bacik7709cde2011-08-04 10:25:02 -04008818 WARN_ON(BTRFS_I(inode)->csum_bytes);
Wang Shilong47059d92014-07-03 18:22:07 +08008819 WARN_ON(BTRFS_I(inode)->defrag_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04008820
Chris Mason5a3f23d2009-03-31 13:27:11 -04008821 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05008822 * This can happen where we create an inode, but somebody else also
8823 * created the same inode and we need to destroy the one we already
8824 * created.
8825 */
8826 if (!root)
Al Viro26602ca2019-04-10 15:14:41 -04008827 return;
Josef Bacika6dbd422009-11-11 15:53:34 -05008828
Chris Masond3977122009-01-05 21:25:51 -05008829 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04008830 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
8831 if (!ordered)
8832 break;
8833 else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008834 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04008835 "found ordered extent %llu %llu on inode cleanup",
Omar Sandovalbffe6332019-12-02 17:34:19 -08008836 ordered->file_offset, ordered->num_bytes);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008837 btrfs_remove_ordered_extent(inode, ordered);
8838 btrfs_put_ordered_extent(ordered);
8839 btrfs_put_ordered_extent(ordered);
8840 }
8841 }
Qu Wenruo56fa9d02015-10-13 09:53:10 +08008842 btrfs_qgroup_check_reserved_leak(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008843 inode_tree_del(inode);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02008844 btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04008845}
8846
Al Viro45321ac2010-06-07 13:43:19 -04008847int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04008848{
8849 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04008850
Naohiro Aota6379ef92013-06-06 09:56:34 +00008851 if (root == NULL)
8852 return 1;
8853
Liu Bofa6ac872013-02-20 14:10:23 +00008854 /* the snap/subvol tree is on deleting */
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02008855 if (btrfs_root_refs(&root->root_item) == 0)
Al Viro45321ac2010-06-07 13:43:19 -04008856 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04008857 else
Al Viro45321ac2010-06-07 13:43:19 -04008858 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04008859}
8860
Sven Wegener0ee0fda2008-07-30 16:54:26 -04008861static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04008862{
8863 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
8864
8865 inode_init_once(&ei->vfs_inode);
8866}
8867
David Sterbae67c7182018-02-19 17:24:18 +01008868void __cold btrfs_destroy_cachep(void)
Chris Mason39279cc2007-06-12 06:35:45 -04008869{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10008870 /*
8871 * Make sure all delayed rcu free inodes are flushed before we
8872 * destroy cache.
8873 */
8874 rcu_barrier();
Kinglong Mee5598e902016-01-29 21:36:35 +08008875 kmem_cache_destroy(btrfs_inode_cachep);
8876 kmem_cache_destroy(btrfs_trans_handle_cachep);
Kinglong Mee5598e902016-01-29 21:36:35 +08008877 kmem_cache_destroy(btrfs_path_cachep);
8878 kmem_cache_destroy(btrfs_free_space_cachep);
Christophe Leroy3acd4852019-08-21 15:05:55 +00008879 kmem_cache_destroy(btrfs_free_space_bitmap_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04008880}
8881
Liu Bof5c29bd2017-11-02 17:21:50 -06008882int __init btrfs_init_cachep(void)
Chris Mason39279cc2007-06-12 06:35:45 -04008883{
David Sterba837e1972012-09-07 03:00:48 -06008884 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008885 sizeof(struct btrfs_inode), 0,
Vladimir Davydov5d097052016-01-14 15:18:21 -08008886 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD | SLAB_ACCOUNT,
8887 init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04008888 if (!btrfs_inode_cachep)
8889 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008890
David Sterba837e1972012-09-07 03:00:48 -06008891 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008892 sizeof(struct btrfs_trans_handle), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03008893 SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04008894 if (!btrfs_trans_handle_cachep)
8895 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008896
David Sterba837e1972012-09-07 03:00:48 -06008897 btrfs_path_cachep = kmem_cache_create("btrfs_path",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008898 sizeof(struct btrfs_path), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03008899 SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04008900 if (!btrfs_path_cachep)
8901 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008902
David Sterba837e1972012-09-07 03:00:48 -06008903 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
Josef Bacikdc89e982011-01-28 17:05:48 -05008904 sizeof(struct btrfs_free_space), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03008905 SLAB_MEM_SPREAD, NULL);
Josef Bacikdc89e982011-01-28 17:05:48 -05008906 if (!btrfs_free_space_cachep)
8907 goto fail;
8908
Christophe Leroy3acd4852019-08-21 15:05:55 +00008909 btrfs_free_space_bitmap_cachep = kmem_cache_create("btrfs_free_space_bitmap",
8910 PAGE_SIZE, PAGE_SIZE,
8911 SLAB_RED_ZONE, NULL);
8912 if (!btrfs_free_space_bitmap_cachep)
8913 goto fail;
8914
Chris Mason39279cc2007-06-12 06:35:45 -04008915 return 0;
8916fail:
8917 btrfs_destroy_cachep();
8918 return -ENOMEM;
8919}
8920
David Howellsa528d352017-01-31 16:46:22 +00008921static int btrfs_getattr(const struct path *path, struct kstat *stat,
8922 u32 request_mask, unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04008923{
Miao Xiedf0af1a2013-01-29 10:11:59 +00008924 u64 delalloc_bytes;
David Howellsa528d352017-01-31 16:46:22 +00008925 struct inode *inode = d_inode(path->dentry);
David Sterbafadc0d82011-11-20 07:33:38 -05008926 u32 blocksize = inode->i_sb->s_blocksize;
Yonghong Song04a87e32017-05-12 15:07:43 -07008927 u32 bi_flags = BTRFS_I(inode)->flags;
8928
8929 stat->result_mask |= STATX_BTIME;
8930 stat->btime.tv_sec = BTRFS_I(inode)->i_otime.tv_sec;
8931 stat->btime.tv_nsec = BTRFS_I(inode)->i_otime.tv_nsec;
8932 if (bi_flags & BTRFS_INODE_APPEND)
8933 stat->attributes |= STATX_ATTR_APPEND;
8934 if (bi_flags & BTRFS_INODE_COMPRESS)
8935 stat->attributes |= STATX_ATTR_COMPRESSED;
8936 if (bi_flags & BTRFS_INODE_IMMUTABLE)
8937 stat->attributes |= STATX_ATTR_IMMUTABLE;
8938 if (bi_flags & BTRFS_INODE_NODUMP)
8939 stat->attributes |= STATX_ATTR_NODUMP;
8940
8941 stat->attributes_mask |= (STATX_ATTR_APPEND |
8942 STATX_ATTR_COMPRESSED |
8943 STATX_ATTR_IMMUTABLE |
8944 STATX_ATTR_NODUMP);
David Sterbafadc0d82011-11-20 07:33:38 -05008945
Chris Mason39279cc2007-06-12 06:35:45 -04008946 generic_fillattr(inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04008947 stat->dev = BTRFS_I(inode)->root->anon_dev;
Miao Xiedf0af1a2013-01-29 10:11:59 +00008948
8949 spin_lock(&BTRFS_I(inode)->lock);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01008950 delalloc_bytes = BTRFS_I(inode)->new_delalloc_bytes;
Miao Xiedf0af1a2013-01-29 10:11:59 +00008951 spin_unlock(&BTRFS_I(inode)->lock);
David Sterbafadc0d82011-11-20 07:33:38 -05008952 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
Miao Xiedf0af1a2013-01-29 10:11:59 +00008953 ALIGN(delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04008954 return 0;
8955}
8956
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008957static int btrfs_rename_exchange(struct inode *old_dir,
8958 struct dentry *old_dentry,
8959 struct inode *new_dir,
8960 struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04008961{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008962 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04008963 struct btrfs_trans_handle *trans;
8964 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008965 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008966 struct inode *new_inode = new_dentry->d_inode;
8967 struct inode *old_inode = old_dentry->d_inode;
Deepa Dinamani95582b02018-05-08 19:36:02 -07008968 struct timespec64 ctime = current_time(old_inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008969 struct dentry *parent;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02008970 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
8971 u64 new_ino = btrfs_ino(BTRFS_I(new_inode));
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008972 u64 old_idx = 0;
8973 u64 new_idx = 0;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008974 int ret;
Filipe Manana86e8aa02016-05-05 02:02:27 +01008975 bool root_log_pinned = false;
8976 bool dest_log_pinned = false;
Filipe Mananad4682ba2018-06-11 19:24:28 +01008977 struct btrfs_log_ctx ctx_root;
8978 struct btrfs_log_ctx ctx_dest;
8979 bool sync_log_root = false;
8980 bool sync_log_dest = false;
8981 bool commit_transaction = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008982
8983 /* we only allow rename subvolume link between subvolumes */
8984 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
8985 return -EXDEV;
8986
Filipe Mananad4682ba2018-06-11 19:24:28 +01008987 btrfs_init_log_ctx(&ctx_root, old_inode);
8988 btrfs_init_log_ctx(&ctx_dest, new_inode);
8989
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008990 /* close the race window with snapshot create/destroy ioctl */
Josef Bacik943eb3b2019-11-19 13:59:20 -05008991 if (old_ino == BTRFS_FIRST_FREE_OBJECTID ||
8992 new_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008993 down_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008994
8995 /*
8996 * We want to reserve the absolute worst case amount of items. So if
8997 * both inodes are subvols and we need to unlink them then that would
8998 * require 4 item modifications, but if they are both normal inodes it
8999 * would require 5 item modifications, so we'll assume their normal
9000 * inodes. So 5 * 2 is 10, plus 2 for the new links, so 12 total items
9001 * should cover the worst case number of items we'll modify.
9002 */
9003 trans = btrfs_start_transaction(root, 12);
9004 if (IS_ERR(trans)) {
9005 ret = PTR_ERR(trans);
9006 goto out_notrans;
9007 }
9008
Josef Bacik3e174092019-11-15 15:43:06 -05009009 if (dest != root)
9010 btrfs_record_root_in_trans(trans, dest);
9011
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009012 /*
9013 * We need to find a free sequence number both in the source and
9014 * in the destination directory for the exchange.
9015 */
Nikolay Borisov877574e2017-02-20 13:50:33 +02009016 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009017 if (ret)
9018 goto out_fail;
Nikolay Borisov877574e2017-02-20 13:50:33 +02009019 ret = btrfs_set_inode_index(BTRFS_I(old_dir), &new_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009020 if (ret)
9021 goto out_fail;
9022
9023 BTRFS_I(old_inode)->dir_index = 0ULL;
9024 BTRFS_I(new_inode)->dir_index = 0ULL;
9025
9026 /* Reference for the source. */
9027 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
9028 /* force full log commit if subvolume involved. */
David Sterba90787762019-03-20 13:28:05 +01009029 btrfs_set_log_full_commit(trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009030 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01009031 btrfs_pin_log_trans(root);
9032 root_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009033 ret = btrfs_insert_inode_ref(trans, dest,
9034 new_dentry->d_name.name,
9035 new_dentry->d_name.len,
9036 old_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01009037 btrfs_ino(BTRFS_I(new_dir)),
9038 old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009039 if (ret)
9040 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009041 }
9042
9043 /* And now for the dest. */
9044 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
9045 /* force full log commit if subvolume involved. */
David Sterba90787762019-03-20 13:28:05 +01009046 btrfs_set_log_full_commit(trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009047 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01009048 btrfs_pin_log_trans(dest);
9049 dest_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009050 ret = btrfs_insert_inode_ref(trans, root,
9051 old_dentry->d_name.name,
9052 old_dentry->d_name.len,
9053 new_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01009054 btrfs_ino(BTRFS_I(old_dir)),
9055 new_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009056 if (ret)
9057 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009058 }
9059
9060 /* Update inode version and ctime/mtime. */
9061 inode_inc_iversion(old_dir);
9062 inode_inc_iversion(new_dir);
9063 inode_inc_iversion(old_inode);
9064 inode_inc_iversion(new_inode);
9065 old_dir->i_ctime = old_dir->i_mtime = ctime;
9066 new_dir->i_ctime = new_dir->i_mtime = ctime;
9067 old_inode->i_ctime = ctime;
9068 new_inode->i_ctime = ctime;
9069
9070 if (old_dentry->d_parent != new_dentry->d_parent) {
David Sterbaf85b7372017-01-20 14:54:07 +01009071 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
9072 BTRFS_I(old_inode), 1);
9073 btrfs_record_unlink_dir(trans, BTRFS_I(new_dir),
9074 BTRFS_I(new_inode), 1);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009075 }
9076
9077 /* src is a subvolume */
9078 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
Josef Bacik045d3962019-12-18 17:20:27 -05009079 ret = btrfs_unlink_subvol(trans, old_dir, old_dentry);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009080 } else { /* src is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009081 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
9082 BTRFS_I(old_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009083 old_dentry->d_name.name,
9084 old_dentry->d_name.len);
9085 if (!ret)
9086 ret = btrfs_update_inode(trans, root, old_inode);
9087 }
9088 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009089 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009090 goto out_fail;
9091 }
9092
9093 /* dest is a subvolume */
9094 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
Josef Bacik045d3962019-12-18 17:20:27 -05009095 ret = btrfs_unlink_subvol(trans, new_dir, new_dentry);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009096 } else { /* dest is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009097 ret = __btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
9098 BTRFS_I(new_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009099 new_dentry->d_name.name,
9100 new_dentry->d_name.len);
9101 if (!ret)
9102 ret = btrfs_update_inode(trans, dest, new_inode);
9103 }
9104 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009105 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009106 goto out_fail;
9107 }
9108
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009109 ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009110 new_dentry->d_name.name,
9111 new_dentry->d_name.len, 0, old_idx);
9112 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009113 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009114 goto out_fail;
9115 }
9116
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009117 ret = btrfs_add_link(trans, BTRFS_I(old_dir), BTRFS_I(new_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009118 old_dentry->d_name.name,
9119 old_dentry->d_name.len, 0, new_idx);
9120 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009121 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009122 goto out_fail;
9123 }
9124
9125 if (old_inode->i_nlink == 1)
9126 BTRFS_I(old_inode)->dir_index = old_idx;
9127 if (new_inode->i_nlink == 1)
9128 BTRFS_I(new_inode)->dir_index = new_idx;
9129
Filipe Manana86e8aa02016-05-05 02:02:27 +01009130 if (root_log_pinned) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009131 parent = new_dentry->d_parent;
Filipe Mananad4682ba2018-06-11 19:24:28 +01009132 ret = btrfs_log_new_name(trans, BTRFS_I(old_inode),
9133 BTRFS_I(old_dir), parent,
9134 false, &ctx_root);
9135 if (ret == BTRFS_NEED_LOG_SYNC)
9136 sync_log_root = true;
9137 else if (ret == BTRFS_NEED_TRANS_COMMIT)
9138 commit_transaction = true;
9139 ret = 0;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009140 btrfs_end_log_trans(root);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009141 root_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009142 }
Filipe Manana86e8aa02016-05-05 02:02:27 +01009143 if (dest_log_pinned) {
Filipe Mananad4682ba2018-06-11 19:24:28 +01009144 if (!commit_transaction) {
9145 parent = old_dentry->d_parent;
9146 ret = btrfs_log_new_name(trans, BTRFS_I(new_inode),
9147 BTRFS_I(new_dir), parent,
9148 false, &ctx_dest);
9149 if (ret == BTRFS_NEED_LOG_SYNC)
9150 sync_log_dest = true;
9151 else if (ret == BTRFS_NEED_TRANS_COMMIT)
9152 commit_transaction = true;
9153 ret = 0;
9154 }
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009155 btrfs_end_log_trans(dest);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009156 dest_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009157 }
9158out_fail:
Filipe Manana86e8aa02016-05-05 02:02:27 +01009159 /*
9160 * If we have pinned a log and an error happened, we unpin tasks
9161 * trying to sync the log and force them to fallback to a transaction
9162 * commit if the log currently contains any of the inodes involved in
9163 * this rename operation (to ensure we do not persist a log with an
9164 * inconsistent state for any of these inodes or leading to any
9165 * inconsistencies when replayed). If the transaction was aborted, the
9166 * abortion reason is propagated to userspace when attempting to commit
9167 * the transaction. If the log does not contain any of these inodes, we
9168 * allow the tasks to sync it.
9169 */
9170 if (ret && (root_log_pinned || dest_log_pinned)) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009171 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
9172 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
9173 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana86e8aa02016-05-05 02:02:27 +01009174 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009175 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
David Sterba90787762019-03-20 13:28:05 +01009176 btrfs_set_log_full_commit(trans);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009177
9178 if (root_log_pinned) {
9179 btrfs_end_log_trans(root);
9180 root_log_pinned = false;
9181 }
9182 if (dest_log_pinned) {
9183 btrfs_end_log_trans(dest);
9184 dest_log_pinned = false;
9185 }
9186 }
Filipe Mananad4682ba2018-06-11 19:24:28 +01009187 if (!ret && sync_log_root && !commit_transaction) {
9188 ret = btrfs_sync_log(trans, BTRFS_I(old_inode)->root,
9189 &ctx_root);
9190 if (ret)
9191 commit_transaction = true;
9192 }
9193 if (!ret && sync_log_dest && !commit_transaction) {
9194 ret = btrfs_sync_log(trans, BTRFS_I(new_inode)->root,
9195 &ctx_dest);
9196 if (ret)
9197 commit_transaction = true;
9198 }
9199 if (commit_transaction) {
Filipe Mananae6c61712019-11-08 16:11:56 +00009200 /*
9201 * We may have set commit_transaction when logging the new name
9202 * in the destination root, in which case we left the source
9203 * root context in the list of log contextes. So make sure we
9204 * remove it to avoid invalid memory accesses, since the context
9205 * was allocated in our stack frame.
9206 */
9207 if (sync_log_root) {
9208 mutex_lock(&root->log_mutex);
9209 list_del_init(&ctx_root.list);
9210 mutex_unlock(&root->log_mutex);
9211 }
Filipe Mananad4682ba2018-06-11 19:24:28 +01009212 ret = btrfs_commit_transaction(trans);
9213 } else {
9214 int ret2;
9215
9216 ret2 = btrfs_end_transaction(trans);
9217 ret = ret ? ret : ret2;
9218 }
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009219out_notrans:
Josef Bacik943eb3b2019-11-19 13:59:20 -05009220 if (new_ino == BTRFS_FIRST_FREE_OBJECTID ||
9221 old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009222 up_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009223
Filipe Mananae6c61712019-11-08 16:11:56 +00009224 ASSERT(list_empty(&ctx_root.list));
9225 ASSERT(list_empty(&ctx_dest.list));
9226
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009227 return ret;
9228}
9229
9230static int btrfs_whiteout_for_rename(struct btrfs_trans_handle *trans,
9231 struct btrfs_root *root,
9232 struct inode *dir,
9233 struct dentry *dentry)
9234{
9235 int ret;
9236 struct inode *inode;
9237 u64 objectid;
9238 u64 index;
9239
9240 ret = btrfs_find_free_ino(root, &objectid);
9241 if (ret)
9242 return ret;
9243
9244 inode = btrfs_new_inode(trans, root, dir,
9245 dentry->d_name.name,
9246 dentry->d_name.len,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009247 btrfs_ino(BTRFS_I(dir)),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009248 objectid,
9249 S_IFCHR | WHITEOUT_MODE,
9250 &index);
9251
9252 if (IS_ERR(inode)) {
9253 ret = PTR_ERR(inode);
9254 return ret;
9255 }
9256
9257 inode->i_op = &btrfs_special_inode_operations;
9258 init_special_inode(inode, inode->i_mode,
9259 WHITEOUT_DEV);
9260
9261 ret = btrfs_init_inode_security(trans, inode, dir,
9262 &dentry->d_name);
9263 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009264 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009265
Nikolay Borisovcef415a2017-02-20 13:51:09 +02009266 ret = btrfs_add_nondir(trans, BTRFS_I(dir), dentry,
9267 BTRFS_I(inode), 0, index);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009268 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009269 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009270
9271 ret = btrfs_update_inode(trans, root, inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009272out:
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009273 unlock_new_inode(inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009274 if (ret)
9275 inode_dec_link_count(inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009276 iput(inode);
9277
Filipe Mananac9901612016-05-05 01:41:57 +01009278 return ret;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009279}
9280
Chris Mason39279cc2007-06-12 06:35:45 -04009281static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009282 struct inode *new_dir, struct dentry *new_dentry,
9283 unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04009284{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009285 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009286 struct btrfs_trans_handle *trans;
Filipe Manana5062af32016-05-05 10:26:26 +01009287 unsigned int trans_num_items;
Chris Mason39279cc2007-06-12 06:35:45 -04009288 struct btrfs_root *root = BTRFS_I(old_dir)->root;
9289 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00009290 struct inode *new_inode = d_inode(new_dentry);
9291 struct inode *old_inode = d_inode(old_dentry);
Chris Mason00e4e6b2008-08-05 11:18:09 -04009292 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009293 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009294 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009295 bool log_pinned = false;
Filipe Mananad4682ba2018-06-11 19:24:28 +01009296 struct btrfs_log_ctx ctx;
9297 bool sync_log = false;
9298 bool commit_transaction = false;
Chris Mason39279cc2007-06-12 06:35:45 -04009299
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009300 if (btrfs_ino(BTRFS_I(new_dir)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04009301 return -EPERM;
9302
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009303 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08009304 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05009305 return -EXDEV;
9306
Li Zefan33345d012011-04-20 10:31:50 +08009307 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009308 (new_inode && btrfs_ino(BTRFS_I(new_inode)) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009309 return -ENOTEMPTY;
9310
Chris Mason39279cc2007-06-12 06:35:45 -04009311 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009312 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04009313 return -ENOTEMPTY;
Chris Mason9c520572012-12-17 14:26:57 -05009314
9315
9316 /* check for collisions, even if the name isn't there */
Josef Bacik4871c152013-10-09 12:24:04 -04009317 ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino,
Chris Mason9c520572012-12-17 14:26:57 -05009318 new_dentry->d_name.name,
9319 new_dentry->d_name.len);
9320
9321 if (ret) {
9322 if (ret == -EEXIST) {
9323 /* we shouldn't get
9324 * eexist without a new_inode */
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05309325 if (WARN_ON(!new_inode)) {
Chris Mason9c520572012-12-17 14:26:57 -05009326 return ret;
9327 }
9328 } else {
9329 /* maybe -EOVERFLOW */
9330 return ret;
9331 }
9332 }
9333 ret = 0;
9334
Chris Mason5a3f23d2009-03-31 13:27:11 -04009335 /*
Chris Mason8d875f92014-08-12 10:47:42 -07009336 * we're using rename to replace one file with another. Start IO on it
9337 * now so we don't add too much work to the end of the transaction
Chris Mason5a3f23d2009-03-31 13:27:11 -04009338 */
Chris Mason8d875f92014-08-12 10:47:42 -07009339 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size)
Chris Mason5a3f23d2009-03-31 13:27:11 -04009340 filemap_flush(old_inode->i_mapping);
9341
Yan, Zheng76dda932009-09-21 16:00:26 -04009342 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08009343 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009344 down_read(&fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04009345 /*
9346 * We want to reserve the absolute worst case amount of items. So if
9347 * both inodes are subvols and we need to unlink them then that would
9348 * require 4 item modifications, but if they are both normal inodes it
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009349 * would require 5 item modifications, so we'll assume they are normal
Yan, Zhenga22285a2010-05-16 10:48:46 -04009350 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
9351 * should cover the worst case number of items we'll modify.
Filipe Manana5062af32016-05-05 10:26:26 +01009352 * If our rename has the whiteout flag, we need more 5 units for the
9353 * new inode (1 inode item, 1 inode ref, 2 dir items and 1 xattr item
9354 * when selinux is enabled).
Yan, Zhenga22285a2010-05-16 10:48:46 -04009355 */
Filipe Manana5062af32016-05-05 10:26:26 +01009356 trans_num_items = 11;
9357 if (flags & RENAME_WHITEOUT)
9358 trans_num_items += 5;
9359 trans = btrfs_start_transaction(root, trans_num_items);
Johann Lombardib44c59a2011-03-31 13:23:47 +00009360 if (IS_ERR(trans)) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009361 ret = PTR_ERR(trans);
9362 goto out_notrans;
9363 }
Chris Mason5f39d392007-10-15 16:14:19 -04009364
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009365 if (dest != root)
9366 btrfs_record_root_in_trans(trans, dest);
9367
Nikolay Borisov877574e2017-02-20 13:50:33 +02009368 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009369 if (ret)
9370 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009371
Miao Xie67de1172013-12-26 13:07:06 +08009372 BTRFS_I(old_inode)->dir_index = 0ULL;
Li Zefan33345d012011-04-20 10:31:50 +08009373 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009374 /* force full log commit if subvolume involved. */
David Sterba90787762019-03-20 13:28:05 +01009375 btrfs_set_log_full_commit(trans);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009376 } else {
Filipe Mananac4aba952016-04-29 13:14:42 +01009377 btrfs_pin_log_trans(root);
9378 log_pinned = true;
Yan, Zhenga5719522009-09-24 09:17:31 -04009379 ret = btrfs_insert_inode_ref(trans, dest,
9380 new_dentry->d_name.name,
9381 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08009382 old_ino,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009383 btrfs_ino(BTRFS_I(new_dir)), index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009384 if (ret)
9385 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009386 }
Chris Mason5a3f23d2009-03-31 13:27:11 -04009387
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009388 inode_inc_iversion(old_dir);
9389 inode_inc_iversion(new_dir);
9390 inode_inc_iversion(old_inode);
Deepa Dinamani04b285f2016-02-06 23:57:21 -08009391 old_dir->i_ctime = old_dir->i_mtime =
9392 new_dir->i_ctime = new_dir->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009393 old_inode->i_ctime = current_time(old_dir);
Chris Mason5f39d392007-10-15 16:14:19 -04009394
Chris Mason12fcfd22009-03-24 10:24:20 -04009395 if (old_dentry->d_parent != new_dentry->d_parent)
David Sterbaf85b7372017-01-20 14:54:07 +01009396 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
9397 BTRFS_I(old_inode), 1);
Chris Mason12fcfd22009-03-24 10:24:20 -04009398
Li Zefan33345d012011-04-20 10:31:50 +08009399 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Josef Bacik045d3962019-12-18 17:20:27 -05009400 ret = btrfs_unlink_subvol(trans, old_dir, old_dentry);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009401 } else {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009402 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
9403 BTRFS_I(d_inode(old_dentry)),
Al Viro92986792011-03-04 17:14:37 +00009404 old_dentry->d_name.name,
9405 old_dentry->d_name.len);
9406 if (!ret)
9407 ret = btrfs_update_inode(trans, root, old_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009408 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009409 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009410 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009411 goto out_fail;
9412 }
Chris Mason39279cc2007-06-12 06:35:45 -04009413
9414 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009415 inode_inc_iversion(new_inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009416 new_inode->i_ctime = current_time(new_inode);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009417 if (unlikely(btrfs_ino(BTRFS_I(new_inode)) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009418 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Josef Bacik045d3962019-12-18 17:20:27 -05009419 ret = btrfs_unlink_subvol(trans, new_dir, new_dentry);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009420 BUG_ON(new_inode->i_nlink == 0);
9421 } else {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009422 ret = btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
9423 BTRFS_I(d_inode(new_dentry)),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009424 new_dentry->d_name.name,
9425 new_dentry->d_name.len);
9426 }
Josef Bacik4ef31a42013-08-13 14:10:08 -04009427 if (!ret && new_inode->i_nlink == 0)
Nikolay Borisov73f2e542017-02-20 13:50:59 +02009428 ret = btrfs_orphan_add(trans,
9429 BTRFS_I(d_inode(new_dentry)));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009430 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009431 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009432 goto out_fail;
9433 }
Chris Mason39279cc2007-06-12 06:35:45 -04009434 }
Josef Bacikaec74772008-07-24 12:12:38 -04009435
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009436 ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009437 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04009438 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009439 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009440 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009441 goto out_fail;
9442 }
Chris Mason39279cc2007-06-12 06:35:45 -04009443
Miao Xie67de1172013-12-26 13:07:06 +08009444 if (old_inode->i_nlink == 1)
9445 BTRFS_I(old_inode)->dir_index = index;
9446
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009447 if (log_pinned) {
Al Viro10d9f302011-07-16 23:09:10 -04009448 struct dentry *parent = new_dentry->d_parent;
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009449
Filipe Mananad4682ba2018-06-11 19:24:28 +01009450 btrfs_init_log_ctx(&ctx, old_inode);
9451 ret = btrfs_log_new_name(trans, BTRFS_I(old_inode),
9452 BTRFS_I(old_dir), parent,
9453 false, &ctx);
9454 if (ret == BTRFS_NEED_LOG_SYNC)
9455 sync_log = true;
9456 else if (ret == BTRFS_NEED_TRANS_COMMIT)
9457 commit_transaction = true;
9458 ret = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009459 btrfs_end_log_trans(root);
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009460 log_pinned = false;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009461 }
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009462
9463 if (flags & RENAME_WHITEOUT) {
9464 ret = btrfs_whiteout_for_rename(trans, root, old_dir,
9465 old_dentry);
9466
9467 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009468 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009469 goto out_fail;
9470 }
Chris Mason12fcfd22009-03-24 10:24:20 -04009471 }
Chris Mason39279cc2007-06-12 06:35:45 -04009472out_fail:
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009473 /*
9474 * If we have pinned the log and an error happened, we unpin tasks
9475 * trying to sync the log and force them to fallback to a transaction
9476 * commit if the log currently contains any of the inodes involved in
9477 * this rename operation (to ensure we do not persist a log with an
9478 * inconsistent state for any of these inodes or leading to any
9479 * inconsistencies when replayed). If the transaction was aborted, the
9480 * abortion reason is propagated to userspace when attempting to commit
9481 * the transaction. If the log does not contain any of these inodes, we
9482 * allow the tasks to sync it.
9483 */
9484 if (ret && log_pinned) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009485 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
9486 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
9487 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009488 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009489 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
David Sterba90787762019-03-20 13:28:05 +01009490 btrfs_set_log_full_commit(trans);
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009491
9492 btrfs_end_log_trans(root);
9493 log_pinned = false;
9494 }
Filipe Mananad4682ba2018-06-11 19:24:28 +01009495 if (!ret && sync_log) {
9496 ret = btrfs_sync_log(trans, BTRFS_I(old_inode)->root, &ctx);
9497 if (ret)
9498 commit_transaction = true;
9499 }
9500 if (commit_transaction) {
9501 ret = btrfs_commit_transaction(trans);
9502 } else {
9503 int ret2;
9504
9505 ret2 = btrfs_end_transaction(trans);
9506 ret = ret ? ret : ret2;
9507 }
Johann Lombardib44c59a2011-03-31 13:23:47 +00009508out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +08009509 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009510 up_read(&fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009511
Chris Mason39279cc2007-06-12 06:35:45 -04009512 return ret;
9513}
9514
Miklos Szeredi80ace852014-07-23 15:15:32 +02009515static int btrfs_rename2(struct inode *old_dir, struct dentry *old_dentry,
9516 struct inode *new_dir, struct dentry *new_dentry,
9517 unsigned int flags)
9518{
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009519 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
Miklos Szeredi80ace852014-07-23 15:15:32 +02009520 return -EINVAL;
9521
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009522 if (flags & RENAME_EXCHANGE)
9523 return btrfs_rename_exchange(old_dir, old_dentry, new_dir,
9524 new_dentry);
9525
9526 return btrfs_rename(old_dir, old_dentry, new_dir, new_dentry, flags);
Miklos Szeredi80ace852014-07-23 15:15:32 +02009527}
9528
Nikolay Borisov3a2f8c02018-04-24 17:23:59 +03009529struct btrfs_delalloc_work {
9530 struct inode *inode;
9531 struct completion completion;
9532 struct list_head list;
9533 struct btrfs_work work;
9534};
9535
Miao Xie8ccf6f192012-10-25 09:28:04 +00009536static void btrfs_run_delalloc_work(struct btrfs_work *work)
9537{
9538 struct btrfs_delalloc_work *delalloc_work;
Josef Bacik9f23e282013-10-28 15:03:41 -04009539 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009540
9541 delalloc_work = container_of(work, struct btrfs_delalloc_work,
9542 work);
Josef Bacik9f23e282013-10-28 15:03:41 -04009543 inode = delalloc_work->inode;
David Sterba30424602015-11-27 19:27:11 +01009544 filemap_flush(inode->i_mapping);
9545 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
9546 &BTRFS_I(inode)->runtime_flags))
Josef Bacik9f23e282013-10-28 15:03:41 -04009547 filemap_flush(inode->i_mapping);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009548
Nikolay Borisov076da912018-04-23 10:54:16 +03009549 iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009550 complete(&delalloc_work->completion);
9551}
9552
Nikolay Borisov3a2f8c02018-04-24 17:23:59 +03009553static struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode)
Miao Xie8ccf6f192012-10-25 09:28:04 +00009554{
9555 struct btrfs_delalloc_work *work;
9556
David Sterba100d5702015-12-08 14:39:32 +01009557 work = kmalloc(sizeof(*work), GFP_NOFS);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009558 if (!work)
9559 return NULL;
9560
9561 init_completion(&work->completion);
9562 INIT_LIST_HEAD(&work->list);
9563 work->inode = inode;
Omar Sandovala0cac0e2019-09-16 11:30:57 -07009564 btrfs_init_work(&work->work, btrfs_run_delalloc_work, NULL, NULL);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009565
9566 return work;
9567}
9568
Chris Masond352ac62008-09-29 15:18:18 -04009569/*
9570 * some fairly slow code that needs optimization. This walks the list
9571 * of all the inodes with pending delalloc and forces them to disk.
9572 */
Ethan Lien3cd24c62018-11-01 14:49:03 +08009573static int start_delalloc_inodes(struct btrfs_root *root, int nr, bool snapshot)
Chris Masonea8c2812008-08-04 23:17:27 -04009574{
Chris Masonea8c2812008-08-04 23:17:27 -04009575 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009576 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009577 struct btrfs_delalloc_work *work, *next;
9578 struct list_head works;
Miao Xie1eafa6c2013-01-22 10:49:00 +00009579 struct list_head splice;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009580 int ret = 0;
Chris Masonea8c2812008-08-04 23:17:27 -04009581
Miao Xie8ccf6f192012-10-25 09:28:04 +00009582 INIT_LIST_HEAD(&works);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009583 INIT_LIST_HEAD(&splice);
Miao Xie63607cc2013-01-22 10:50:35 +00009584
Miao Xie573bfb72014-03-06 13:55:03 +08009585 mutex_lock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009586 spin_lock(&root->delalloc_lock);
9587 list_splice_init(&root->delalloc_inodes, &splice);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009588 while (!list_empty(&splice)) {
9589 binode = list_entry(splice.next, struct btrfs_inode,
Chris Masonea8c2812008-08-04 23:17:27 -04009590 delalloc_inodes);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009591
Miao Xieeb73c1b2013-05-15 07:48:22 +00009592 list_move_tail(&binode->delalloc_inodes,
9593 &root->delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009594 inode = igrab(&binode->vfs_inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00009595 if (!inode) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00009596 cond_resched_lock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009597 continue;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009598 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00009599 spin_unlock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009600
Ethan Lien3cd24c62018-11-01 14:49:03 +08009601 if (snapshot)
9602 set_bit(BTRFS_INODE_SNAPSHOT_FLUSH,
9603 &binode->runtime_flags);
Nikolay Borisov076da912018-04-23 10:54:16 +03009604 work = btrfs_alloc_delalloc_work(inode);
David Sterba5d99a9982014-09-29 19:20:37 +02009605 if (!work) {
Nikolay Borisov4fbb5142018-04-23 10:54:15 +03009606 iput(inode);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009607 ret = -ENOMEM;
Wang Shilonga1ecaab2014-04-02 19:53:32 +08009608 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009609 }
Miao Xie1eafa6c2013-01-22 10:49:00 +00009610 list_add_tail(&work->list, &works);
Qu Wenruoa44903a2014-02-28 10:46:09 +08009611 btrfs_queue_work(root->fs_info->flush_workers,
9612 &work->work);
Miao Xie6c255e62014-03-06 13:55:01 +08009613 ret++;
9614 if (nr != -1 && ret >= nr)
Wang Shilonga1ecaab2014-04-02 19:53:32 +08009615 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009616 cond_resched();
Miao Xieeb73c1b2013-05-15 07:48:22 +00009617 spin_lock(&root->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04009618 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00009619 spin_unlock(&root->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -04009620
Wang Shilonga1ecaab2014-04-02 19:53:32 +08009621out:
Miao Xie1eafa6c2013-01-22 10:49:00 +00009622 list_for_each_entry_safe(work, next, &works, list) {
9623 list_del_init(&work->list);
Nikolay Borisov40012f92018-04-19 10:46:39 +03009624 wait_for_completion(&work->completion);
9625 kfree(work);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009626 }
9627
Nikolay Borisov81f1d392018-04-19 10:46:37 +03009628 if (!list_empty(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00009629 spin_lock(&root->delalloc_lock);
9630 list_splice_tail(&splice, &root->delalloc_inodes);
9631 spin_unlock(&root->delalloc_lock);
9632 }
Miao Xie573bfb72014-03-06 13:55:03 +08009633 mutex_unlock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009634 return ret;
9635}
9636
Ethan Lien3cd24c62018-11-01 14:49:03 +08009637int btrfs_start_delalloc_snapshot(struct btrfs_root *root)
Miao Xieeb73c1b2013-05-15 07:48:22 +00009638{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009639 struct btrfs_fs_info *fs_info = root->fs_info;
Miao Xieeb73c1b2013-05-15 07:48:22 +00009640 int ret;
9641
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009642 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +00009643 return -EROFS;
9644
Ethan Lien3cd24c62018-11-01 14:49:03 +08009645 ret = start_delalloc_inodes(root, -1, true);
Miao Xie6c255e62014-03-06 13:55:01 +08009646 if (ret > 0)
9647 ret = 0;
Miao Xieeb73c1b2013-05-15 07:48:22 +00009648 return ret;
9649}
9650
Nikolay Borisov82b3e532018-04-23 10:54:13 +03009651int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, int nr)
Miao Xieeb73c1b2013-05-15 07:48:22 +00009652{
9653 struct btrfs_root *root;
9654 struct list_head splice;
9655 int ret;
9656
Wang Shilong2c21b4d2014-01-14 19:42:20 +08009657 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +00009658 return -EROFS;
9659
9660 INIT_LIST_HEAD(&splice);
9661
Miao Xie573bfb72014-03-06 13:55:03 +08009662 mutex_lock(&fs_info->delalloc_root_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009663 spin_lock(&fs_info->delalloc_root_lock);
9664 list_splice_init(&fs_info->delalloc_roots, &splice);
Miao Xie6c255e62014-03-06 13:55:01 +08009665 while (!list_empty(&splice) && nr) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00009666 root = list_first_entry(&splice, struct btrfs_root,
9667 delalloc_root);
9668 root = btrfs_grab_fs_root(root);
9669 BUG_ON(!root);
9670 list_move_tail(&root->delalloc_root,
9671 &fs_info->delalloc_roots);
9672 spin_unlock(&fs_info->delalloc_root_lock);
9673
Ethan Lien3cd24c62018-11-01 14:49:03 +08009674 ret = start_delalloc_inodes(root, nr, false);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009675 btrfs_put_fs_root(root);
Miao Xie6c255e62014-03-06 13:55:01 +08009676 if (ret < 0)
Miao Xieeb73c1b2013-05-15 07:48:22 +00009677 goto out;
9678
Miao Xie6c255e62014-03-06 13:55:01 +08009679 if (nr != -1) {
9680 nr -= ret;
9681 WARN_ON(nr < 0);
9682 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00009683 spin_lock(&fs_info->delalloc_root_lock);
9684 }
9685 spin_unlock(&fs_info->delalloc_root_lock);
9686
Miao Xie6c255e62014-03-06 13:55:01 +08009687 ret = 0;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009688out:
Nikolay Borisov81f1d392018-04-19 10:46:37 +03009689 if (!list_empty(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00009690 spin_lock(&fs_info->delalloc_root_lock);
9691 list_splice_tail(&splice, &fs_info->delalloc_roots);
9692 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009693 }
Miao Xie573bfb72014-03-06 13:55:03 +08009694 mutex_unlock(&fs_info->delalloc_root_mutex);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009695 return ret;
Chris Masonea8c2812008-08-04 23:17:27 -04009696}
9697
Chris Mason39279cc2007-06-12 06:35:45 -04009698static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
9699 const char *symname)
9700{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009701 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009702 struct btrfs_trans_handle *trans;
9703 struct btrfs_root *root = BTRFS_I(dir)->root;
9704 struct btrfs_path *path;
9705 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05009706 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04009707 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04009708 u64 objectid;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05309709 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009710 int name_len;
9711 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04009712 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04009713 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04009714 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04009715
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +01009716 name_len = strlen(symname);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009717 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info))
Chris Mason39279cc2007-06-12 06:35:45 -04009718 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05009719
Josef Bacik9ed74f22009-09-11 16:12:44 -04009720 /*
9721 * 2 items for inode item and ref
9722 * 2 items for dir items
Filipe Manana9269d122015-12-31 18:16:29 +00009723 * 1 item for updating parent inode item
9724 * 1 item for the inline extent item
Josef Bacik9ed74f22009-09-11 16:12:44 -04009725 * 1 item for xattr if selinux is on
9726 */
Filipe Manana9269d122015-12-31 18:16:29 +00009727 trans = btrfs_start_transaction(root, 7);
Yan, Zhenga22285a2010-05-16 10:48:46 -04009728 if (IS_ERR(trans))
9729 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05009730
Li Zefan581bb052011-04-20 10:06:11 +08009731 err = btrfs_find_free_ino(root, &objectid);
9732 if (err)
9733 goto out_unlock;
9734
Josef Bacikaec74772008-07-24 12:12:38 -04009735 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01009736 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)),
9737 objectid, S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04009738 if (IS_ERR(inode)) {
9739 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -04009740 inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04009741 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04009742 }
Chris Mason39279cc2007-06-12 06:35:45 -04009743
Casey Schauflerad19db72011-12-15 10:09:07 -05009744 /*
9745 * If the active LSM wants to access the inode during
9746 * d_instantiate it needs these. Smack checks to see
9747 * if the filesystem supports xattrs by looking at the
9748 * ops vector.
9749 */
9750 inode->i_fop = &btrfs_file_operations;
9751 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07009752 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -07009753 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
9754
9755 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
9756 if (err)
Al Viro32955c52018-05-16 12:20:05 -04009757 goto out_unlock;
Casey Schauflerad19db72011-12-15 10:09:07 -05009758
Chris Mason39279cc2007-06-12 06:35:45 -04009759 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07009760 if (!path) {
9761 err = -ENOMEM;
Al Viro32955c52018-05-16 12:20:05 -04009762 goto out_unlock;
Mark Fashehd8926bb2011-07-13 10:38:47 -07009763 }
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009764 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason39279cc2007-06-12 06:35:45 -04009765 key.offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02009766 key.type = BTRFS_EXTENT_DATA_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04009767 datasize = btrfs_file_extent_calc_inline_size(name_len);
9768 err = btrfs_insert_empty_item(trans, root, path, &key,
9769 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04009770 if (err) {
Julia Lawallb0839162011-05-14 07:10:51 +00009771 btrfs_free_path(path);
Al Viro32955c52018-05-16 12:20:05 -04009772 goto out_unlock;
Chris Mason54aa1f42007-06-22 14:16:25 -04009773 }
Chris Mason5f39d392007-10-15 16:14:19 -04009774 leaf = path->nodes[0];
9775 ei = btrfs_item_ptr(leaf, path->slots[0],
9776 struct btrfs_file_extent_item);
9777 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
9778 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04009779 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -04009780 btrfs_set_file_extent_encryption(leaf, ei, 0);
9781 btrfs_set_file_extent_compression(leaf, ei, 0);
9782 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
9783 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
9784
Chris Mason39279cc2007-06-12 06:35:45 -04009785 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04009786 write_extent_buffer(leaf, symname, ptr, name_len);
9787 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04009788 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04009789
Chris Mason39279cc2007-06-12 06:35:45 -04009790 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -05009791 inode_nohighmem(inode);
Yan Zhengd899e052008-10-30 14:25:28 -04009792 inode_set_bytes(inode, name_len);
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02009793 btrfs_i_size_write(BTRFS_I(inode), name_len);
Chris Mason54aa1f42007-06-22 14:16:25 -04009794 err = btrfs_update_inode(trans, root, inode);
Filipe Mananad50866d2015-12-31 18:08:24 +00009795 /*
9796 * Last step, add directory indexes for our symlink inode. This is the
9797 * last step to avoid extra cleanup of these indexes if an error happens
9798 * elsewhere above.
9799 */
9800 if (!err)
Nikolay Borisovcef415a2017-02-20 13:51:09 +02009801 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry,
9802 BTRFS_I(inode), 0, index);
Al Viro32955c52018-05-16 12:20:05 -04009803 if (err)
9804 goto out_unlock;
Chris Masonb0d5d102014-09-08 13:08:51 -07009805
Al Viro1e2e5472018-05-04 08:23:01 -04009806 d_instantiate_new(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009807
9808out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009809 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -04009810 if (err && inode) {
Chris Mason39279cc2007-06-12 06:35:45 -04009811 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -04009812 discard_new_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009813 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009814 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04009815 return err;
9816}
Chris Mason16432982008-04-10 10:23:21 -04009817
Josef Bacik0af3d002010-06-21 14:48:16 -04009818static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
9819 u64 start, u64 num_bytes, u64 min_size,
9820 loff_t actual_len, u64 *alloc_hint,
9821 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -04009822{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009823 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik5dc562c2012-08-17 13:14:17 -04009824 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
9825 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -04009826 struct btrfs_root *root = BTRFS_I(inode)->root;
9827 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -04009828 u64 cur_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +00009829 u64 i_size;
Chris Mason154ea282013-03-05 11:11:26 -05009830 u64 cur_bytes;
Josef Bacik0b670dc2015-09-23 17:11:16 -04009831 u64 last_alloc = (u64)-1;
Yan Zhengd899e052008-10-30 14:25:28 -04009832 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04009833 bool own_trans = true;
Wang Xiaoguang18513092016-07-25 15:51:40 +08009834 u64 end = start + num_bytes - 1;
Yan Zhengd899e052008-10-30 14:25:28 -04009835
Josef Bacik0af3d002010-06-21 14:48:16 -04009836 if (trans)
9837 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -04009838 while (num_bytes > 0) {
Josef Bacik0af3d002010-06-21 14:48:16 -04009839 if (own_trans) {
9840 trans = btrfs_start_transaction(root, 3);
9841 if (IS_ERR(trans)) {
9842 ret = PTR_ERR(trans);
9843 break;
9844 }
Yan Zhengd899e052008-10-30 14:25:28 -04009845 }
Yan, Zheng5a303d52009-11-12 09:34:52 +00009846
Byongho Leeee221842015-12-15 01:42:10 +09009847 cur_bytes = min_t(u64, num_bytes, SZ_256M);
Chris Mason154ea282013-03-05 11:11:26 -05009848 cur_bytes = max(cur_bytes, min_size);
Josef Bacik0b670dc2015-09-23 17:11:16 -04009849 /*
9850 * If we are severely fragmented we could end up with really
9851 * small allocations, so if the allocator is returning small
9852 * chunks lets make its job easier by only searching for those
9853 * sized chunks.
9854 */
9855 cur_bytes = min(cur_bytes, last_alloc);
Wang Xiaoguang18513092016-07-25 15:51:40 +08009856 ret = btrfs_reserve_extent(root, cur_bytes, cur_bytes,
9857 min_size, 0, *alloc_hint, &ins, 1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00009858 if (ret) {
Josef Bacik0af3d002010-06-21 14:48:16 -04009859 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009860 btrfs_end_transaction(trans);
Yan, Zhenga22285a2010-05-16 10:48:46 -04009861 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +00009862 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009863 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Yan, Zheng5a303d52009-11-12 09:34:52 +00009864
Josef Bacik0b670dc2015-09-23 17:11:16 -04009865 last_alloc = ins.offset;
Yan Zhengd899e052008-10-30 14:25:28 -04009866 ret = insert_reserved_file_extent(trans, inode,
9867 cur_offset, ins.objectid,
9868 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +00009869 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04009870 BTRFS_FILE_EXTENT_PREALLOC);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009871 if (ret) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009872 btrfs_free_reserved_extent(fs_info, ins.objectid,
Miao Xiee570fd22014-06-19 10:42:50 +08009873 ins.offset, 0);
Jeff Mahoney66642832016-06-10 18:19:25 -04009874 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009875 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009876 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009877 break;
9878 }
Dongsheng Yang31193212014-12-12 16:44:35 +08009879
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02009880 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Chris Masona1ed8352009-09-11 12:27:37 -04009881 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00009882
Josef Bacik5dc562c2012-08-17 13:14:17 -04009883 em = alloc_extent_map();
9884 if (!em) {
9885 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
9886 &BTRFS_I(inode)->runtime_flags);
9887 goto next;
9888 }
9889
9890 em->start = cur_offset;
9891 em->orig_start = cur_offset;
9892 em->len = ins.offset;
9893 em->block_start = ins.objectid;
9894 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -05009895 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -04009896 em->ram_bytes = ins.offset;
Josef Bacik5dc562c2012-08-17 13:14:17 -04009897 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
9898 em->generation = trans->transid;
9899
9900 while (1) {
9901 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04009902 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04009903 write_unlock(&em_tree->lock);
9904 if (ret != -EEXIST)
9905 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02009906 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04009907 cur_offset + ins.offset - 1,
9908 0);
9909 }
9910 free_extent_map(em);
9911next:
Yan Zhengd899e052008-10-30 14:25:28 -04009912 num_bytes -= ins.offset;
9913 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -04009914 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +00009915
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009916 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009917 inode->i_ctime = current_time(inode);
Christoph Hellwig6cbff002009-04-17 10:37:41 +02009918 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -04009919 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -04009920 (actual_len > inode->i_size) &&
9921 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00009922 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +00009923 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00009924 else
Josef Bacik55a61d12010-11-22 18:50:32 +00009925 i_size = cur_offset;
9926 i_size_write(inode, i_size);
9927 btrfs_ordered_update_i_size(inode, i_size, NULL);
Yan, Zheng5a303d52009-11-12 09:34:52 +00009928 }
9929
Yan Zhengd899e052008-10-30 14:25:28 -04009930 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009931
9932 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009933 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009934 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009935 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009936 break;
9937 }
Yan Zhengd899e052008-10-30 14:25:28 -04009938
Josef Bacik0af3d002010-06-21 14:48:16 -04009939 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009940 btrfs_end_transaction(trans);
Yan, Zheng5a303d52009-11-12 09:34:52 +00009941 }
Wang Xiaoguang18513092016-07-25 15:51:40 +08009942 if (cur_offset < end)
Qu Wenruobc42bda2017-02-27 15:10:39 +08009943 btrfs_free_reserved_data_space(inode, NULL, cur_offset,
Wang Xiaoguang18513092016-07-25 15:51:40 +08009944 end - cur_offset + 1);
Yan Zhengd899e052008-10-30 14:25:28 -04009945 return ret;
9946}
9947
Josef Bacik0af3d002010-06-21 14:48:16 -04009948int btrfs_prealloc_file_range(struct inode *inode, int mode,
9949 u64 start, u64 num_bytes, u64 min_size,
9950 loff_t actual_len, u64 *alloc_hint)
9951{
9952 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
9953 min_size, actual_len, alloc_hint,
9954 NULL);
9955}
9956
9957int btrfs_prealloc_file_range_trans(struct inode *inode,
9958 struct btrfs_trans_handle *trans, int mode,
9959 u64 start, u64 num_bytes, u64 min_size,
9960 loff_t actual_len, u64 *alloc_hint)
9961{
9962 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
9963 min_size, actual_len, alloc_hint, trans);
9964}
9965
Chris Masone6dcd2d2008-07-17 12:53:50 -04009966static int btrfs_set_page_dirty(struct page *page)
9967{
Chris Masone6dcd2d2008-07-17 12:53:50 -04009968 return __set_page_dirty_nobuffers(page);
9969}
9970
Al Viro10556cb22011-06-20 19:28:19 -04009971static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -05009972{
Li Zefanb83cc962010-12-20 16:04:08 +08009973 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00009974 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +08009975
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00009976 if (mask & MAY_WRITE &&
9977 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
9978 if (btrfs_root_readonly(root))
9979 return -EROFS;
9980 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
9981 return -EACCES;
9982 }
Al Viro2830ba72011-06-20 19:16:29 -04009983 return generic_permission(inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -05009984}
Chris Mason39279cc2007-06-12 06:35:45 -04009985
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009986static int btrfs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
9987{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009988 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009989 struct btrfs_trans_handle *trans;
9990 struct btrfs_root *root = BTRFS_I(dir)->root;
9991 struct inode *inode = NULL;
9992 u64 objectid;
9993 u64 index;
9994 int ret = 0;
9995
9996 /*
9997 * 5 units required for adding orphan entry
9998 */
9999 trans = btrfs_start_transaction(root, 5);
10000 if (IS_ERR(trans))
10001 return PTR_ERR(trans);
10002
10003 ret = btrfs_find_free_ino(root, &objectid);
10004 if (ret)
10005 goto out;
10006
10007 inode = btrfs_new_inode(trans, root, dir, NULL, 0,
David Sterbaf85b7372017-01-20 14:54:07 +010010008 btrfs_ino(BTRFS_I(dir)), objectid, mode, &index);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010009 if (IS_ERR(inode)) {
10010 ret = PTR_ERR(inode);
10011 inode = NULL;
10012 goto out;
10013 }
10014
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010015 inode->i_fop = &btrfs_file_operations;
10016 inode->i_op = &btrfs_file_inode_operations;
10017
10018 inode->i_mapping->a_ops = &btrfs_aops;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010019 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
10020
Chris Masonb0d5d102014-09-08 13:08:51 -070010021 ret = btrfs_init_inode_security(trans, inode, dir, NULL);
10022 if (ret)
Al Viro32955c52018-05-16 12:20:05 -040010023 goto out;
Chris Masonb0d5d102014-09-08 13:08:51 -070010024
10025 ret = btrfs_update_inode(trans, root, inode);
10026 if (ret)
Al Viro32955c52018-05-16 12:20:05 -040010027 goto out;
Nikolay Borisov73f2e542017-02-20 13:50:59 +020010028 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010029 if (ret)
Al Viro32955c52018-05-16 12:20:05 -040010030 goto out;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010031
Filipe Manana5762b5c2014-08-01 00:10:32 +010010032 /*
10033 * We set number of links to 0 in btrfs_new_inode(), and here we set
10034 * it to 1 because d_tmpfile() will issue a warning if the count is 0,
10035 * through:
10036 *
10037 * d_tmpfile() -> inode_dec_link_count() -> drop_nlink()
10038 */
10039 set_nlink(inode, 1);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010040 d_tmpfile(dentry, inode);
Al Viro32955c52018-05-16 12:20:05 -040010041 unlock_new_inode(inode);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010042 mark_inode_dirty(inode);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010043out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010044 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -040010045 if (ret && inode)
10046 discard_new_inode(inode);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010047 btrfs_btree_balance_dirty(fs_info);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010048 return ret;
10049}
10050
David Sterba5cdc84b2018-07-18 20:32:52 +020010051void btrfs_set_range_writeback(struct extent_io_tree *tree, u64 start, u64 end)
Josef Bacikc6100a42017-05-05 11:57:13 -040010052{
David Sterba5cdc84b2018-07-18 20:32:52 +020010053 struct inode *inode = tree->private_data;
Josef Bacikc6100a42017-05-05 11:57:13 -040010054 unsigned long index = start >> PAGE_SHIFT;
10055 unsigned long end_index = end >> PAGE_SHIFT;
10056 struct page *page;
10057
10058 while (index <= end_index) {
10059 page = find_get_page(inode->i_mapping, index);
10060 ASSERT(page); /* Pages should be in the extent_io_tree */
10061 set_page_writeback(page);
10062 put_page(page);
10063 index++;
10064 }
10065}
10066
Omar Sandovaled46ff32016-11-03 10:28:14 -070010067#ifdef CONFIG_SWAP
10068/*
10069 * Add an entry indicating a block group or device which is pinned by a
10070 * swapfile. Returns 0 on success, 1 if there is already an entry for it, or a
10071 * negative errno on failure.
10072 */
10073static int btrfs_add_swapfile_pin(struct inode *inode, void *ptr,
10074 bool is_block_group)
10075{
10076 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
10077 struct btrfs_swapfile_pin *sp, *entry;
10078 struct rb_node **p;
10079 struct rb_node *parent = NULL;
10080
10081 sp = kmalloc(sizeof(*sp), GFP_NOFS);
10082 if (!sp)
10083 return -ENOMEM;
10084 sp->ptr = ptr;
10085 sp->inode = inode;
10086 sp->is_block_group = is_block_group;
10087
10088 spin_lock(&fs_info->swapfile_pins_lock);
10089 p = &fs_info->swapfile_pins.rb_node;
10090 while (*p) {
10091 parent = *p;
10092 entry = rb_entry(parent, struct btrfs_swapfile_pin, node);
10093 if (sp->ptr < entry->ptr ||
10094 (sp->ptr == entry->ptr && sp->inode < entry->inode)) {
10095 p = &(*p)->rb_left;
10096 } else if (sp->ptr > entry->ptr ||
10097 (sp->ptr == entry->ptr && sp->inode > entry->inode)) {
10098 p = &(*p)->rb_right;
10099 } else {
10100 spin_unlock(&fs_info->swapfile_pins_lock);
10101 kfree(sp);
10102 return 1;
10103 }
10104 }
10105 rb_link_node(&sp->node, parent, p);
10106 rb_insert_color(&sp->node, &fs_info->swapfile_pins);
10107 spin_unlock(&fs_info->swapfile_pins_lock);
10108 return 0;
10109}
10110
10111/* Free all of the entries pinned by this swapfile. */
10112static void btrfs_free_swapfile_pins(struct inode *inode)
10113{
10114 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
10115 struct btrfs_swapfile_pin *sp;
10116 struct rb_node *node, *next;
10117
10118 spin_lock(&fs_info->swapfile_pins_lock);
10119 node = rb_first(&fs_info->swapfile_pins);
10120 while (node) {
10121 next = rb_next(node);
10122 sp = rb_entry(node, struct btrfs_swapfile_pin, node);
10123 if (sp->inode == inode) {
10124 rb_erase(&sp->node, &fs_info->swapfile_pins);
10125 if (sp->is_block_group)
10126 btrfs_put_block_group(sp->ptr);
10127 kfree(sp);
10128 }
10129 node = next;
10130 }
10131 spin_unlock(&fs_info->swapfile_pins_lock);
10132}
10133
10134struct btrfs_swap_info {
10135 u64 start;
10136 u64 block_start;
10137 u64 block_len;
10138 u64 lowest_ppage;
10139 u64 highest_ppage;
10140 unsigned long nr_pages;
10141 int nr_extents;
10142};
10143
10144static int btrfs_add_swap_extent(struct swap_info_struct *sis,
10145 struct btrfs_swap_info *bsi)
10146{
10147 unsigned long nr_pages;
10148 u64 first_ppage, first_ppage_reported, next_ppage;
10149 int ret;
10150
10151 first_ppage = ALIGN(bsi->block_start, PAGE_SIZE) >> PAGE_SHIFT;
10152 next_ppage = ALIGN_DOWN(bsi->block_start + bsi->block_len,
10153 PAGE_SIZE) >> PAGE_SHIFT;
10154
10155 if (first_ppage >= next_ppage)
10156 return 0;
10157 nr_pages = next_ppage - first_ppage;
10158
10159 first_ppage_reported = first_ppage;
10160 if (bsi->start == 0)
10161 first_ppage_reported++;
10162 if (bsi->lowest_ppage > first_ppage_reported)
10163 bsi->lowest_ppage = first_ppage_reported;
10164 if (bsi->highest_ppage < (next_ppage - 1))
10165 bsi->highest_ppage = next_ppage - 1;
10166
10167 ret = add_swap_extent(sis, bsi->nr_pages, nr_pages, first_ppage);
10168 if (ret < 0)
10169 return ret;
10170 bsi->nr_extents += ret;
10171 bsi->nr_pages += nr_pages;
10172 return 0;
10173}
10174
10175static void btrfs_swap_deactivate(struct file *file)
10176{
10177 struct inode *inode = file_inode(file);
10178
10179 btrfs_free_swapfile_pins(inode);
10180 atomic_dec(&BTRFS_I(inode)->root->nr_swapfiles);
10181}
10182
10183static int btrfs_swap_activate(struct swap_info_struct *sis, struct file *file,
10184 sector_t *span)
10185{
10186 struct inode *inode = file_inode(file);
10187 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
10188 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
10189 struct extent_state *cached_state = NULL;
10190 struct extent_map *em = NULL;
10191 struct btrfs_device *device = NULL;
10192 struct btrfs_swap_info bsi = {
10193 .lowest_ppage = (sector_t)-1ULL,
10194 };
10195 int ret = 0;
10196 u64 isize;
10197 u64 start;
10198
10199 /*
10200 * If the swap file was just created, make sure delalloc is done. If the
10201 * file changes again after this, the user is doing something stupid and
10202 * we don't really care.
10203 */
10204 ret = btrfs_wait_ordered_range(inode, 0, (u64)-1);
10205 if (ret)
10206 return ret;
10207
10208 /*
10209 * The inode is locked, so these flags won't change after we check them.
10210 */
10211 if (BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS) {
10212 btrfs_warn(fs_info, "swapfile must not be compressed");
10213 return -EINVAL;
10214 }
10215 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW)) {
10216 btrfs_warn(fs_info, "swapfile must not be copy-on-write");
10217 return -EINVAL;
10218 }
10219 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
10220 btrfs_warn(fs_info, "swapfile must not be checksummed");
10221 return -EINVAL;
10222 }
10223
10224 /*
10225 * Balance or device remove/replace/resize can move stuff around from
10226 * under us. The EXCL_OP flag makes sure they aren't running/won't run
10227 * concurrently while we are mapping the swap extents, and
10228 * fs_info->swapfile_pins prevents them from running while the swap file
10229 * is active and moving the extents. Note that this also prevents a
10230 * concurrent device add which isn't actually necessary, but it's not
10231 * really worth the trouble to allow it.
10232 */
10233 if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) {
10234 btrfs_warn(fs_info,
10235 "cannot activate swapfile while exclusive operation is running");
10236 return -EBUSY;
10237 }
10238 /*
10239 * Snapshots can create extents which require COW even if NODATACOW is
10240 * set. We use this counter to prevent snapshots. We must increment it
10241 * before walking the extents because we don't want a concurrent
10242 * snapshot to run after we've already checked the extents.
10243 */
10244 atomic_inc(&BTRFS_I(inode)->root->nr_swapfiles);
10245
10246 isize = ALIGN_DOWN(inode->i_size, fs_info->sectorsize);
10247
10248 lock_extent_bits(io_tree, 0, isize - 1, &cached_state);
10249 start = 0;
10250 while (start < isize) {
10251 u64 logical_block_start, physical_block_start;
David Sterba32da53862019-10-29 19:20:18 +010010252 struct btrfs_block_group *bg;
Omar Sandovaled46ff32016-11-03 10:28:14 -070010253 u64 len = isize - start;
10254
Omar Sandoval39b07b52019-12-02 17:34:23 -080010255 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len);
Omar Sandovaled46ff32016-11-03 10:28:14 -070010256 if (IS_ERR(em)) {
10257 ret = PTR_ERR(em);
10258 goto out;
10259 }
10260
10261 if (em->block_start == EXTENT_MAP_HOLE) {
10262 btrfs_warn(fs_info, "swapfile must not have holes");
10263 ret = -EINVAL;
10264 goto out;
10265 }
10266 if (em->block_start == EXTENT_MAP_INLINE) {
10267 /*
10268 * It's unlikely we'll ever actually find ourselves
10269 * here, as a file small enough to fit inline won't be
10270 * big enough to store more than the swap header, but in
10271 * case something changes in the future, let's catch it
10272 * here rather than later.
10273 */
10274 btrfs_warn(fs_info, "swapfile must not be inline");
10275 ret = -EINVAL;
10276 goto out;
10277 }
10278 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
10279 btrfs_warn(fs_info, "swapfile must not be compressed");
10280 ret = -EINVAL;
10281 goto out;
10282 }
10283
10284 logical_block_start = em->block_start + (start - em->start);
10285 len = min(len, em->len - (start - em->start));
10286 free_extent_map(em);
10287 em = NULL;
10288
10289 ret = can_nocow_extent(inode, start, &len, NULL, NULL, NULL);
10290 if (ret < 0) {
10291 goto out;
10292 } else if (ret) {
10293 ret = 0;
10294 } else {
10295 btrfs_warn(fs_info,
10296 "swapfile must not be copy-on-write");
10297 ret = -EINVAL;
10298 goto out;
10299 }
10300
10301 em = btrfs_get_chunk_map(fs_info, logical_block_start, len);
10302 if (IS_ERR(em)) {
10303 ret = PTR_ERR(em);
10304 goto out;
10305 }
10306
10307 if (em->map_lookup->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
10308 btrfs_warn(fs_info,
10309 "swapfile must have single data profile");
10310 ret = -EINVAL;
10311 goto out;
10312 }
10313
10314 if (device == NULL) {
10315 device = em->map_lookup->stripes[0].dev;
10316 ret = btrfs_add_swapfile_pin(inode, device, false);
10317 if (ret == 1)
10318 ret = 0;
10319 else if (ret)
10320 goto out;
10321 } else if (device != em->map_lookup->stripes[0].dev) {
10322 btrfs_warn(fs_info, "swapfile must be on one device");
10323 ret = -EINVAL;
10324 goto out;
10325 }
10326
10327 physical_block_start = (em->map_lookup->stripes[0].physical +
10328 (logical_block_start - em->start));
10329 len = min(len, em->len - (logical_block_start - em->start));
10330 free_extent_map(em);
10331 em = NULL;
10332
10333 bg = btrfs_lookup_block_group(fs_info, logical_block_start);
10334 if (!bg) {
10335 btrfs_warn(fs_info,
10336 "could not find block group containing swapfile");
10337 ret = -EINVAL;
10338 goto out;
10339 }
10340
10341 ret = btrfs_add_swapfile_pin(inode, bg, true);
10342 if (ret) {
10343 btrfs_put_block_group(bg);
10344 if (ret == 1)
10345 ret = 0;
10346 else
10347 goto out;
10348 }
10349
10350 if (bsi.block_len &&
10351 bsi.block_start + bsi.block_len == physical_block_start) {
10352 bsi.block_len += len;
10353 } else {
10354 if (bsi.block_len) {
10355 ret = btrfs_add_swap_extent(sis, &bsi);
10356 if (ret)
10357 goto out;
10358 }
10359 bsi.start = start;
10360 bsi.block_start = physical_block_start;
10361 bsi.block_len = len;
10362 }
10363
10364 start += len;
10365 }
10366
10367 if (bsi.block_len)
10368 ret = btrfs_add_swap_extent(sis, &bsi);
10369
10370out:
10371 if (!IS_ERR_OR_NULL(em))
10372 free_extent_map(em);
10373
10374 unlock_extent_cached(io_tree, 0, isize - 1, &cached_state);
10375
10376 if (ret)
10377 btrfs_swap_deactivate(file);
10378
10379 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
10380
10381 if (ret)
10382 return ret;
10383
10384 if (device)
10385 sis->bdev = device->bdev;
10386 *span = bsi.highest_ppage - bsi.lowest_ppage + 1;
10387 sis->max = bsi.nr_pages;
10388 sis->pages = bsi.nr_pages - 1;
10389 sis->highest_bit = bsi.nr_pages - 1;
10390 return bsi.nr_extents;
10391}
10392#else
10393static void btrfs_swap_deactivate(struct file *file)
10394{
10395}
10396
10397static int btrfs_swap_activate(struct swap_info_struct *sis, struct file *file,
10398 sector_t *span)
10399{
10400 return -EOPNOTSUPP;
10401}
10402#endif
10403
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010404static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -050010405 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -040010406 .lookup = btrfs_lookup,
10407 .create = btrfs_create,
10408 .unlink = btrfs_unlink,
10409 .link = btrfs_link,
10410 .mkdir = btrfs_mkdir,
10411 .rmdir = btrfs_rmdir,
Miklos Szeredi2773bf02016-09-27 11:03:58 +020010412 .rename = btrfs_rename2,
Chris Mason39279cc2007-06-12 06:35:45 -040010413 .symlink = btrfs_symlink,
10414 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -040010415 .mknod = btrfs_mknod,
Josef Bacik5103e942007-11-16 11:45:54 -050010416 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010417 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010418 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010419 .set_acl = btrfs_set_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070010420 .update_time = btrfs_update_time,
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010421 .tmpfile = btrfs_tmpfile,
Chris Mason39279cc2007-06-12 06:35:45 -040010422};
Yan, Zheng76dda932009-09-21 16:00:26 -040010423
Alexey Dobriyan828c0952009-10-01 15:43:56 -070010424static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010425 .llseek = generic_file_llseek,
10426 .read = generic_read_dir,
Omar Sandoval02dbfc92016-05-20 13:50:33 -070010427 .iterate_shared = btrfs_real_readdir,
Josef Bacik23b5ec72017-07-24 15:14:25 -040010428 .open = btrfs_opendir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -040010429 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010430#ifdef CONFIG_COMPAT
Luke Dashjr4c63c242015-10-29 08:22:21 +000010431 .compat_ioctl = btrfs_compat_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010432#endif
Sage Weil6bf13c02008-06-10 10:07:39 -040010433 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -040010434 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -040010435};
10436
David Sterba20e55062015-11-19 11:42:28 +010010437static const struct extent_io_ops btrfs_extent_io_ops = {
David Sterba4d53ddd2017-02-17 15:27:44 +010010438 /* mandatory callbacks */
Chris Mason065631f2008-02-20 12:07:25 -050010439 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -040010440 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
10441};
10442
Chris Mason35054392009-01-21 13:11:13 -050010443/*
10444 * btrfs doesn't support the bmap operation because swapfiles
10445 * use bmap to make a mapping of extents in the file. They assume
10446 * these extents won't change over the life of the file and they
10447 * use the bmap result to do IO directly to the drive.
10448 *
10449 * the btrfs bmap call would return logical addresses that aren't
10450 * suitable for IO and they also will change frequently as COW
10451 * operations happen. So, swapfile + btrfs == corruption.
10452 *
10453 * For now we're avoiding this by dropping bmap.
10454 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -070010455static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -040010456 .readpage = btrfs_readpage,
10457 .writepage = btrfs_writepage,
Chris Masonb293f02e2007-11-01 19:45:34 -040010458 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -050010459 .readpages = btrfs_readpages,
Chris Mason16432982008-04-10 10:23:21 -040010460 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -040010461 .invalidatepage = btrfs_invalidatepage,
10462 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -040010463 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +020010464 .error_remove_page = generic_error_remove_page,
Omar Sandovaled46ff32016-11-03 10:28:14 -070010465 .swap_activate = btrfs_swap_activate,
10466 .swap_deactivate = btrfs_swap_deactivate,
Chris Mason39279cc2007-06-12 06:35:45 -040010467};
10468
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010469static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010470 .getattr = btrfs_getattr,
10471 .setattr = btrfs_setattr,
Josef Bacik5103e942007-11-16 11:45:54 -050010472 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010473 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -050010474 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010475 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010476 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010477 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010478};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010479static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -040010480 .getattr = btrfs_getattr,
10481 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010482 .permission = btrfs_permission,
Josef Bacik33268ea2008-07-24 12:16:36 -040010483 .listxattr = btrfs_listxattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010484 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010485 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010486 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -040010487};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010488static const struct inode_operations btrfs_symlink_inode_operations = {
Al Viro6b255392015-11-17 10:20:54 -050010489 .get_link = page_get_link,
Li Zefanf2095612010-11-19 02:05:24 +000010490 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -050010491 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010492 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -050010493 .listxattr = btrfs_listxattr,
Josef Bacike41f9412012-03-26 09:46:47 -040010494 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010495};
Yan, Zheng76dda932009-09-21 16:00:26 -040010496
Alexey Dobriyan82d339d2009-10-09 09:54:36 -040010497const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -040010498 .d_delete = btrfs_dentry_delete,
10499};