blob: 6bebc0ca751db92633519ed39908636c30512dc7 [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>
Chris Mason39279cc2007-06-12 06:35:45 -040033#include "ctree.h"
34#include "disk-io.h"
35#include "transaction.h"
36#include "btrfs_inode.h"
Chris Mason39279cc2007-06-12 06:35:45 -040037#include "print-tree.h"
Chris Masone6dcd2d2008-07-17 12:53:50 -040038#include "ordered-data.h"
Christoph Hellwig95819c02008-08-28 06:21:17 -040039#include "xattr.h"
Chris Masone02119d2008-09-05 16:13:11 -040040#include "tree-log.h"
Jan Schmidt4a54c8c2011-07-22 15:41:52 +020041#include "volumes.h"
Chris Masonc8b97812008-10-29 14:49:59 -040042#include "compression.h"
Chris Masonb4ce94d2009-02-04 09:25:08 -050043#include "locking.h"
Josef Bacikdc89e982011-01-28 17:05:48 -050044#include "free-space-cache.h"
Li Zefan581bb052011-04-20 10:06:11 +080045#include "inode-map.h"
Liu Bo38c227d2013-01-29 03:18:40 +000046#include "backref.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"
Wang Xiaoguangdda32452016-07-11 11:05:29 +080049#include "dedupe.h"
Chris Mason39279cc2007-06-12 06:35:45 -040050
51struct btrfs_iget_args {
Chris Mason90d3e592014-01-09 17:28:00 -080052 struct btrfs_key *location;
Chris Mason39279cc2007-06-12 06:35:45 -040053 struct btrfs_root *root;
54};
55
Filipe Mananaf28a4922015-12-08 19:23:20 +000056struct btrfs_dio_data {
Filipe Mananaf28a4922015-12-08 19:23:20 +000057 u64 reserve;
58 u64 unsubmitted_oe_range_start;
59 u64 unsubmitted_oe_range_end;
Liu Bo4aaedfb2016-12-14 22:36:05 -080060 int overwrite;
Filipe Mananaf28a4922015-12-08 19:23:20 +000061};
62
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070063static const struct inode_operations btrfs_dir_inode_operations;
64static const struct inode_operations btrfs_symlink_inode_operations;
65static const struct inode_operations btrfs_dir_ro_inode_operations;
66static 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;
Chris Mason39279cc2007-06-12 06:35:45 -040076
Eric Sandeen3972f262013-01-12 02:57:22 +000077static int btrfs_setsize(struct inode *inode, struct iattr *attr);
Filipe Manana213e8c52018-02-06 20:40:31 +000078static int btrfs_truncate(struct inode *inode, bool skip_writeback);
Josef Bacik5fd02042012-05-02 14:00:54 -040079static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent);
Chris Mason771ed682008-11-06 22:02:51 -050080static noinline int cow_file_range(struct inode *inode,
81 struct page *locked_page,
Wang Xiaoguangdda32452016-07-11 11:05:29 +080082 u64 start, u64 end, u64 delalloc_end,
83 int *page_started, unsigned long *nr_written,
84 int unlock, struct btrfs_dedupe_hash *hash);
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
Li Zefanfe3f5662011-03-28 08:30:38 +0000180 if (compressed_size && compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400181 cur_size = compressed_size;
Chris Masonc8b97812008-10-29 14:49:59 -0400182
Chris Masonc8b97812008-10-29 14:49:59 -0400183 inode_add_bytes(inode, size);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000184
185 if (!extent_inserted) {
186 struct btrfs_key key;
187 size_t datasize;
188
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +0200189 key.objectid = btrfs_ino(BTRFS_I(inode));
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000190 key.offset = start;
David Sterba962a2982014-06-04 18:41:45 +0200191 key.type = BTRFS_EXTENT_DATA_KEY;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000192
193 datasize = btrfs_file_extent_calc_inline_size(cur_size);
194 path->leave_spinning = 1;
195 ret = btrfs_insert_empty_item(trans, root, path, &key,
196 datasize);
David Sterba79b4f4c2017-06-15 19:09:51 +0200197 if (ret)
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000198 goto fail;
Chris Masonc8b97812008-10-29 14:49:59 -0400199 }
200 leaf = path->nodes[0];
201 ei = btrfs_item_ptr(leaf, path->slots[0],
202 struct btrfs_file_extent_item);
203 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
204 btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
205 btrfs_set_file_extent_encryption(leaf, ei, 0);
206 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
207 btrfs_set_file_extent_ram_bytes(leaf, ei, size);
208 ptr = btrfs_file_extent_inline_start(ei);
209
Li Zefan261507a02010-12-17 14:21:50 +0800210 if (compress_type != BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -0400211 struct page *cpage;
212 int i = 0;
Chris Masond3977122009-01-05 21:25:51 -0500213 while (compressed_size > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -0400214 cpage = compressed_pages[i];
Chris Mason5b050f02008-11-11 09:34:41 -0500215 cur_size = min_t(unsigned long, compressed_size,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300216 PAGE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400217
Cong Wang7ac687d2011-11-25 23:14:28 +0800218 kaddr = kmap_atomic(cpage);
Chris Masonc8b97812008-10-29 14:49:59 -0400219 write_extent_buffer(leaf, kaddr, ptr, cur_size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800220 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400221
222 i++;
223 ptr += cur_size;
224 compressed_size -= cur_size;
225 }
226 btrfs_set_file_extent_compression(leaf, ei,
Li Zefan261507a02010-12-17 14:21:50 +0800227 compress_type);
Chris Masonc8b97812008-10-29 14:49:59 -0400228 } else {
229 page = find_get_page(inode->i_mapping,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300230 start >> PAGE_SHIFT);
Chris Masonc8b97812008-10-29 14:49:59 -0400231 btrfs_set_file_extent_compression(leaf, ei, 0);
Cong Wang7ac687d2011-11-25 23:14:28 +0800232 kaddr = kmap_atomic(page);
Johannes Thumshirn70730172018-12-05 15:23:03 +0100233 offset = offset_in_page(start);
Chris Masonc8b97812008-10-29 14:49:59 -0400234 write_extent_buffer(leaf, kaddr + offset, ptr, size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800235 kunmap_atomic(kaddr);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300236 put_page(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400237 }
238 btrfs_mark_buffer_dirty(leaf);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000239 btrfs_release_path(path);
Chris Masonc8b97812008-10-29 14:49:59 -0400240
Yan, Zhengc2167752009-11-12 09:34:21 +0000241 /*
242 * we're an inline extent, so nobody can
243 * extend the file past i_size without locking
244 * a page we already have locked.
245 *
246 * We must do any isize and inode updates
247 * before we unlock the pages. Otherwise we
248 * could end up racing with unlink.
249 */
Chris Masonc8b97812008-10-29 14:49:59 -0400250 BTRFS_I(inode)->disk_i_size = inode->i_size;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100251 ret = btrfs_update_inode(trans, root, inode);
Yan, Zhengc2167752009-11-12 09:34:21 +0000252
Chris Masonc8b97812008-10-29 14:49:59 -0400253fail:
David Sterba79b4f4c2017-06-15 19:09:51 +0200254 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400255}
256
257
258/*
259 * conditionally insert an inline extent into the file. This
260 * does the checks required to make sure the data is small enough
261 * to fit as an inline extent.
262 */
Nikolay Borisovd02c0e22018-03-02 09:43:15 +0200263static noinline int cow_file_range_inline(struct inode *inode, u64 start,
Josef Bacik00361582013-08-14 14:02:47 -0400264 u64 end, size_t compressed_size,
265 int compress_type,
266 struct page **compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400267{
Nikolay Borisovd02c0e22018-03-02 09:43:15 +0200268 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400269 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik00361582013-08-14 14:02:47 -0400270 struct btrfs_trans_handle *trans;
Chris Masonc8b97812008-10-29 14:49:59 -0400271 u64 isize = i_size_read(inode);
272 u64 actual_end = min(end + 1, isize);
273 u64 inline_len = actual_end - start;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400274 u64 aligned_end = ALIGN(end, fs_info->sectorsize);
Chris Masonc8b97812008-10-29 14:49:59 -0400275 u64 data_len = inline_len;
276 int ret;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000277 struct btrfs_path *path;
278 int extent_inserted = 0;
279 u32 extent_item_size;
Chris Masonc8b97812008-10-29 14:49:59 -0400280
281 if (compressed_size)
282 data_len = compressed_size;
283
284 if (start > 0 ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400285 actual_end > fs_info->sectorsize ||
286 data_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info) ||
Chris Masonc8b97812008-10-29 14:49:59 -0400287 (!compressed_size &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400288 (actual_end & (fs_info->sectorsize - 1)) == 0) ||
Chris Masonc8b97812008-10-29 14:49:59 -0400289 end + 1 < isize ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400290 data_len > fs_info->max_inline) {
Chris Masonc8b97812008-10-29 14:49:59 -0400291 return 1;
292 }
293
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000294 path = btrfs_alloc_path();
295 if (!path)
296 return -ENOMEM;
297
Josef Bacik00361582013-08-14 14:02:47 -0400298 trans = btrfs_join_transaction(root);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000299 if (IS_ERR(trans)) {
300 btrfs_free_path(path);
Josef Bacik00361582013-08-14 14:02:47 -0400301 return PTR_ERR(trans);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000302 }
Josef Bacik69fe2d72017-10-19 14:15:57 -0400303 trans->block_rsv = &BTRFS_I(inode)->block_rsv;
Josef Bacik00361582013-08-14 14:02:47 -0400304
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000305 if (compressed_size && compressed_pages)
306 extent_item_size = btrfs_file_extent_calc_inline_size(
307 compressed_size);
308 else
309 extent_item_size = btrfs_file_extent_calc_inline_size(
310 inline_len);
311
312 ret = __btrfs_drop_extents(trans, root, inode, path,
313 start, aligned_end, NULL,
314 1, 1, extent_item_size, &extent_inserted);
Josef Bacik00361582013-08-14 14:02:47 -0400315 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -0400316 btrfs_abort_transaction(trans, ret);
Josef Bacik00361582013-08-14 14:02:47 -0400317 goto out;
318 }
Chris Masonc8b97812008-10-29 14:49:59 -0400319
320 if (isize > actual_end)
321 inline_len = min_t(u64, isize, actual_end);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000322 ret = insert_inline_extent(trans, path, extent_inserted,
323 root, inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -0400324 inline_len, compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000325 compress_type, compressed_pages);
Josef Bacik2adcac12012-05-23 16:10:14 -0400326 if (ret && ret != -ENOSPC) {
Jeff Mahoney66642832016-06-10 18:19:25 -0400327 btrfs_abort_transaction(trans, ret);
Josef Bacik00361582013-08-14 14:02:47 -0400328 goto out;
Josef Bacik2adcac12012-05-23 16:10:14 -0400329 } else if (ret == -ENOSPC) {
Josef Bacik00361582013-08-14 14:02:47 -0400330 ret = 1;
331 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100332 }
Josef Bacik2adcac12012-05-23 16:10:14 -0400333
Josef Bacikbdc20e62013-02-28 13:23:38 -0500334 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +0200335 btrfs_drop_extent_cache(BTRFS_I(inode), start, aligned_end - 1, 0);
Josef Bacik00361582013-08-14 14:02:47 -0400336out:
Qu Wenruo94ed9382015-09-08 17:25:56 +0800337 /*
338 * Don't forget to free the reserved space, as for inlined extent
339 * it won't count as data extent, free them directly here.
340 * And at reserve time, it's always aligned to page size, so
341 * just free one page here.
342 */
Qu Wenruobc42bda2017-02-27 15:10:39 +0800343 btrfs_qgroup_free_data(inode, NULL, 0, PAGE_SIZE);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000344 btrfs_free_path(path);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -0400345 btrfs_end_transaction(trans);
Josef Bacik00361582013-08-14 14:02:47 -0400346 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400347}
348
Chris Mason771ed682008-11-06 22:02:51 -0500349struct async_extent {
350 u64 start;
351 u64 ram_size;
352 u64 compressed_size;
353 struct page **pages;
354 unsigned long nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800355 int compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500356 struct list_head list;
357};
358
Nikolay Borisov97db1202019-03-12 17:20:24 +0200359struct async_chunk {
Chris Mason771ed682008-11-06 22:02:51 -0500360 struct inode *inode;
Chris Mason771ed682008-11-06 22:02:51 -0500361 struct page *locked_page;
362 u64 start;
363 u64 end;
Liu Bof82b7352017-10-23 23:18:16 -0600364 unsigned int write_flags;
Chris Mason771ed682008-11-06 22:02:51 -0500365 struct list_head extents;
366 struct btrfs_work work;
Nikolay Borisov97db1202019-03-12 17:20:24 +0200367 atomic_t *pending;
Chris Mason771ed682008-11-06 22:02:51 -0500368};
369
Nikolay Borisov97db1202019-03-12 17:20:24 +0200370struct async_cow {
371 /* Number of chunks in flight; must be first in the structure */
372 atomic_t num_chunks;
373 struct async_chunk chunks[];
374};
375
376static noinline int add_async_extent(struct async_chunk *cow,
Chris Mason771ed682008-11-06 22:02:51 -0500377 u64 start, u64 ram_size,
378 u64 compressed_size,
379 struct page **pages,
Li Zefan261507a02010-12-17 14:21:50 +0800380 unsigned long nr_pages,
381 int compress_type)
Chris Mason771ed682008-11-06 22:02:51 -0500382{
383 struct async_extent *async_extent;
384
385 async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100386 BUG_ON(!async_extent); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -0500387 async_extent->start = start;
388 async_extent->ram_size = ram_size;
389 async_extent->compressed_size = compressed_size;
390 async_extent->pages = pages;
391 async_extent->nr_pages = nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800392 async_extent->compress_type = compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500393 list_add_tail(&async_extent->list, &cow->extents);
394 return 0;
395}
396
Timofey Titovetsc2fcdcd2017-07-17 16:52:58 +0300397static inline int inode_need_compress(struct inode *inode, u64 start, u64 end)
Wang Shilongf79707b2014-07-17 11:44:09 +0800398{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400399 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Wang Shilongf79707b2014-07-17 11:44:09 +0800400
401 /* force compress */
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400402 if (btrfs_test_opt(fs_info, FORCE_COMPRESS))
Wang Shilongf79707b2014-07-17 11:44:09 +0800403 return 1;
David Sterbaeec63c62017-07-17 19:41:31 +0200404 /* defrag ioctl */
405 if (BTRFS_I(inode)->defrag_compress)
406 return 1;
Wang Shilongf79707b2014-07-17 11:44:09 +0800407 /* bad compression ratios */
408 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS)
409 return 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400410 if (btrfs_test_opt(fs_info, COMPRESS) ||
Wang Shilongf79707b2014-07-17 11:44:09 +0800411 BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS ||
David Sterbab52aa8c2017-07-17 19:17:20 +0200412 BTRFS_I(inode)->prop_compress)
Timofey Titovetsc2fcdcd2017-07-17 16:52:58 +0300413 return btrfs_compress_heuristic(inode, start, end);
Wang Shilongf79707b2014-07-17 11:44:09 +0800414 return 0;
415}
416
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200417static inline void inode_should_defrag(struct btrfs_inode *inode,
Anand Jain26d30f82016-12-19 19:09:06 +0800418 u64 start, u64 end, u64 num_bytes, u64 small_write)
419{
420 /* If this is a small write inside eof, kick off a defrag */
421 if (num_bytes < small_write &&
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200422 (start > 0 || end + 1 < inode->disk_i_size))
Anand Jain26d30f82016-12-19 19:09:06 +0800423 btrfs_add_inode_defrag(NULL, inode);
424}
425
Chris Masonc8b97812008-10-29 14:49:59 -0400426/*
Chris Mason771ed682008-11-06 22:02:51 -0500427 * we create compressed extents in two phases. The first
428 * phase compresses a range of pages that have already been
429 * locked (both pages and state bits are locked).
Chris Masonc8b97812008-10-29 14:49:59 -0400430 *
Chris Mason771ed682008-11-06 22:02:51 -0500431 * This is done inside an ordered work queue, and the compression
432 * is spread across many cpus. The actual IO submission is step
433 * two, and the ordered work queue takes care of making sure that
434 * happens in the same order things were put onto the queue by
435 * writepages and friends.
Chris Masonc8b97812008-10-29 14:49:59 -0400436 *
Chris Mason771ed682008-11-06 22:02:51 -0500437 * If this code finds it can't get good compression, it puts an
438 * entry onto the work queue to write the uncompressed bytes. This
439 * makes sure that both compressed inodes and uncompressed inodes
Artem Bityutskiyb2570312012-07-25 18:12:06 +0300440 * are written in the same order that the flusher thread sent them
441 * down.
Chris Masond352ac62008-09-29 15:18:18 -0400442 */
Nikolay Borisov1368c6d2019-03-12 17:20:27 +0200443static noinline void compress_file_range(struct async_chunk *async_chunk,
444 int *num_added)
Chris Masonb888db22007-08-27 16:49:44 -0400445{
Nikolay Borisov1368c6d2019-03-12 17:20:27 +0200446 struct inode *inode = async_chunk->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400447 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400448 u64 blocksize = fs_info->sectorsize;
Nikolay Borisov1368c6d2019-03-12 17:20:27 +0200449 u64 start = async_chunk->start;
450 u64 end = async_chunk->end;
Chris Masonc8b97812008-10-29 14:49:59 -0400451 u64 actual_end;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400452 int ret = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400453 struct page **pages = NULL;
454 unsigned long nr_pages;
Chris Masonc8b97812008-10-29 14:49:59 -0400455 unsigned long total_compressed = 0;
456 unsigned long total_in = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400457 int i;
458 int will_compress;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400459 int compress_type = fs_info->compress_type;
Chris Mason4adaa612013-03-26 13:07:00 -0400460 int redirty = 0;
Chris Masonb888db22007-08-27 16:49:44 -0400461
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200462 inode_should_defrag(BTRFS_I(inode), start, end, end - start + 1,
463 SZ_16K);
Chris Mason4cb53002011-05-24 15:35:30 -0400464
Nikolay Borisov62b37622019-01-03 10:50:00 +0200465 actual_end = min_t(u64, i_size_read(inode), end + 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400466again:
467 will_compress = 0;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300468 nr_pages = (end >> PAGE_SHIFT) - (start >> PAGE_SHIFT) + 1;
David Sterba069eac72017-02-14 19:36:54 +0100469 BUILD_BUG_ON((BTRFS_MAX_COMPRESSED % PAGE_SIZE) != 0);
470 nr_pages = min_t(unsigned long, nr_pages,
471 BTRFS_MAX_COMPRESSED / PAGE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400472
Chris Masonf03d9301f2009-02-04 09:31:06 -0500473 /*
474 * we don't want to send crud past the end of i_size through
475 * compression, that's just a waste of CPU time. So, if the
476 * end of the file is before the start of our current
477 * requested range of bytes, we bail out to the uncompressed
478 * cleanup code that can deal with all of this.
479 *
480 * It isn't really the fastest way to fix things, but this is a
481 * very uncommon corner.
482 */
483 if (actual_end <= start)
484 goto cleanup_and_bail_uncompressed;
485
Chris Masonc8b97812008-10-29 14:49:59 -0400486 total_compressed = actual_end - start;
487
Shilong Wang4bcbb332014-10-07 18:44:35 -0400488 /*
489 * skip compression for a small file range(<=blocksize) that
Nicholas D Steeves01327612016-05-19 21:18:45 -0400490 * isn't an inline extent, since it doesn't save disk space at all.
Shilong Wang4bcbb332014-10-07 18:44:35 -0400491 */
492 if (total_compressed <= blocksize &&
493 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
494 goto cleanup_and_bail_uncompressed;
495
David Sterba069eac72017-02-14 19:36:54 +0100496 total_compressed = min_t(unsigned long, total_compressed,
497 BTRFS_MAX_UNCOMPRESSED);
Chris Masonc8b97812008-10-29 14:49:59 -0400498 total_in = 0;
499 ret = 0;
Chris Masondb945352007-10-15 16:15:53 -0400500
Chris Mason771ed682008-11-06 22:02:51 -0500501 /*
502 * we do compression for mount -o compress and when the
503 * inode has not been flagged as nocompress. This flag can
504 * change at any time if we discover bad compression ratios.
Chris Masonc8b97812008-10-29 14:49:59 -0400505 */
Timofey Titovetsc2fcdcd2017-07-17 16:52:58 +0300506 if (inode_need_compress(inode, start, end)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400507 WARN_ON(pages);
David Sterba31e818f2015-02-20 18:00:26 +0100508 pages = kcalloc(nr_pages, sizeof(struct page *), GFP_NOFS);
Li Zefan560f7d72011-09-08 10:22:01 +0800509 if (!pages) {
510 /* just bail out to the uncompressed code */
Filipe Manana3527a012018-10-13 00:37:25 +0100511 nr_pages = 0;
Li Zefan560f7d72011-09-08 10:22:01 +0800512 goto cont;
513 }
Chris Mason179e29e2007-11-01 11:28:41 -0400514
David Sterbaeec63c62017-07-17 19:41:31 +0200515 if (BTRFS_I(inode)->defrag_compress)
516 compress_type = BTRFS_I(inode)->defrag_compress;
517 else if (BTRFS_I(inode)->prop_compress)
David Sterbab52aa8c2017-07-17 19:17:20 +0200518 compress_type = BTRFS_I(inode)->prop_compress;
Li Zefan261507a02010-12-17 14:21:50 +0800519
Chris Mason4adaa612013-03-26 13:07:00 -0400520 /*
521 * we need to call clear_page_dirty_for_io on each
522 * page in the range. Otherwise applications with the file
523 * mmap'd can wander in and change the page contents while
524 * we are compressing them.
525 *
526 * If the compression fails for any reason, we set the pages
527 * dirty again later on.
Timofey Titovetse9679de2017-10-24 01:29:48 +0300528 *
529 * Note that the remaining part is redirtied, the start pointer
530 * has moved, the end is the original one.
Chris Mason4adaa612013-03-26 13:07:00 -0400531 */
Timofey Titovetse9679de2017-10-24 01:29:48 +0300532 if (!redirty) {
533 extent_range_clear_dirty_for_io(inode, start, end);
534 redirty = 1;
535 }
David Sterbaf51d2b52017-09-15 17:36:57 +0200536
537 /* Compression level is applied here and only here */
538 ret = btrfs_compress_pages(
539 compress_type | (fs_info->compress_level << 4),
Li Zefan261507a02010-12-17 14:21:50 +0800540 inode->i_mapping, start,
David Sterba38c31462017-02-14 19:04:07 +0100541 pages,
David Sterba4d3a8002017-02-14 19:04:07 +0100542 &nr_pages,
Li Zefan261507a02010-12-17 14:21:50 +0800543 &total_in,
David Sterbae5d74902017-02-14 19:45:05 +0100544 &total_compressed);
Chris Masonc8b97812008-10-29 14:49:59 -0400545
546 if (!ret) {
Johannes Thumshirn70730172018-12-05 15:23:03 +0100547 unsigned long offset = offset_in_page(total_compressed);
David Sterba4d3a8002017-02-14 19:04:07 +0100548 struct page *page = pages[nr_pages - 1];
Chris Masonc8b97812008-10-29 14:49:59 -0400549 char *kaddr;
550
551 /* zero the tail end of the last page, we might be
552 * sending it down to disk
553 */
554 if (offset) {
Cong Wang7ac687d2011-11-25 23:14:28 +0800555 kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400556 memset(kaddr + offset, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300557 PAGE_SIZE - offset);
Cong Wang7ac687d2011-11-25 23:14:28 +0800558 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400559 }
560 will_compress = 1;
561 }
562 }
Li Zefan560f7d72011-09-08 10:22:01 +0800563cont:
Chris Masonc8b97812008-10-29 14:49:59 -0400564 if (start == 0) {
565 /* lets try to make an inline extent */
Timofey Titovets6018ba02017-09-15 01:57:26 +0300566 if (ret || total_in < actual_end) {
Chris Masonc8b97812008-10-29 14:49:59 -0400567 /* we didn't compress the entire range, try
Chris Mason771ed682008-11-06 22:02:51 -0500568 * to make an uncompressed inline extent.
Chris Masonc8b97812008-10-29 14:49:59 -0400569 */
Nikolay Borisovd02c0e22018-03-02 09:43:15 +0200570 ret = cow_file_range_inline(inode, start, end, 0,
571 BTRFS_COMPRESS_NONE, NULL);
Chris Masonc8b97812008-10-29 14:49:59 -0400572 } else {
Chris Mason771ed682008-11-06 22:02:51 -0500573 /* try making a compressed inline extent */
Nikolay Borisovd02c0e22018-03-02 09:43:15 +0200574 ret = cow_file_range_inline(inode, start, end,
Li Zefanfe3f5662011-03-28 08:30:38 +0000575 total_compressed,
576 compress_type, pages);
Chris Masonc8b97812008-10-29 14:49:59 -0400577 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100578 if (ret <= 0) {
Josef Bacik151a41b2013-07-29 13:22:24 -0400579 unsigned long clear_flags = EXTENT_DELALLOC |
Josef Bacik8b62f872017-10-19 14:15:55 -0400580 EXTENT_DELALLOC_NEW | EXTENT_DEFRAG |
581 EXTENT_DO_ACCOUNTING;
Filipe Mananae6eb4312014-10-10 10:45:12 +0100582 unsigned long page_error_op;
583
Filipe Mananae6eb4312014-10-10 10:45:12 +0100584 page_error_op = ret < 0 ? PAGE_SET_ERROR : 0;
Josef Bacik151a41b2013-07-29 13:22:24 -0400585
Chris Mason771ed682008-11-06 22:02:51 -0500586 /*
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100587 * inline extent creation worked or returned error,
588 * we don't need to create any more async work items.
589 * Unlock and free up our temp pages.
Josef Bacik8b62f872017-10-19 14:15:55 -0400590 *
591 * We use DO_ACCOUNTING here because we need the
592 * delalloc_release_metadata to be done _after_ we drop
593 * our outstanding extent for clearing delalloc for this
594 * range.
Chris Mason771ed682008-11-06 22:02:51 -0500595 */
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800596 extent_clear_unlock_delalloc(inode, start, end, end,
597 NULL, clear_flags,
598 PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -0400599 PAGE_CLEAR_DIRTY |
600 PAGE_SET_WRITEBACK |
Filipe Mananae6eb4312014-10-10 10:45:12 +0100601 page_error_op |
Josef Bacikc2790a22013-07-29 11:20:47 -0400602 PAGE_END_WRITEBACK);
Chris Masonc8b97812008-10-29 14:49:59 -0400603 goto free_pages_out;
604 }
605 }
606
607 if (will_compress) {
608 /*
609 * we aren't doing an inline extent round the compressed size
610 * up to a block size boundary so the allocator does sane
611 * things
612 */
Qu Wenruofda28322013-02-26 08:10:22 +0000613 total_compressed = ALIGN(total_compressed, blocksize);
Chris Masonc8b97812008-10-29 14:49:59 -0400614
615 /*
616 * one last check to make sure the compression is really a
Timofey Titovets170607e2017-06-06 14:41:15 +0300617 * win, compare the page count read with the blocks on disk,
618 * compression must free at least one sector size
Chris Masonc8b97812008-10-29 14:49:59 -0400619 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300620 total_in = ALIGN(total_in, PAGE_SIZE);
Timofey Titovets170607e2017-06-06 14:41:15 +0300621 if (total_compressed + blocksize <= total_in) {
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700622 *num_added += 1;
623
624 /*
625 * The async work queues will take care of doing actual
626 * allocation on disk for these compressed pages, and
627 * will submit them to the elevator.
628 */
Nikolay Borisovb5326272019-03-12 17:20:25 +0200629 add_async_extent(async_chunk, start, total_in,
David Sterba4d3a8002017-02-14 19:04:07 +0100630 total_compressed, pages, nr_pages,
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700631 compress_type);
632
Timofey Titovets11708622017-10-03 18:06:01 +0300633 if (start + total_in < end) {
634 start += total_in;
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700635 pages = NULL;
636 cond_resched();
637 goto again;
638 }
639 return;
Chris Masonc8b97812008-10-29 14:49:59 -0400640 }
641 }
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700642 if (pages) {
Chris Masonc8b97812008-10-29 14:49:59 -0400643 /*
644 * the compression code ran but failed to make things smaller,
645 * free any pages it allocated and our page pointer array
646 */
David Sterba4d3a8002017-02-14 19:04:07 +0100647 for (i = 0; i < nr_pages; i++) {
Chris Mason70b99e62008-10-31 12:46:39 -0400648 WARN_ON(pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300649 put_page(pages[i]);
Chris Masonc8b97812008-10-29 14:49:59 -0400650 }
651 kfree(pages);
652 pages = NULL;
653 total_compressed = 0;
David Sterba4d3a8002017-02-14 19:04:07 +0100654 nr_pages = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400655
656 /* flag the file so we don't compress in the future */
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400657 if (!btrfs_test_opt(fs_info, FORCE_COMPRESS) &&
David Sterbab52aa8c2017-07-17 19:17:20 +0200658 !(BTRFS_I(inode)->prop_compress)) {
Chris Masona555f812010-01-28 16:18:15 -0500659 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
Chris Mason1e701a32010-03-11 09:42:04 -0500660 }
Chris Masonc8b97812008-10-29 14:49:59 -0400661 }
Chris Masonf03d9301f2009-02-04 09:31:06 -0500662cleanup_and_bail_uncompressed:
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700663 /*
664 * No compression, but we still need to write the pages in the file
665 * we've been given so far. redirty the locked page if it corresponds
666 * to our extent and set things up for the async work queue to run
667 * cow_file_range to do the normal delalloc dance.
668 */
Nikolay Borisov1368c6d2019-03-12 17:20:27 +0200669 if (page_offset(async_chunk->locked_page) >= start &&
670 page_offset(async_chunk->locked_page) <= end)
671 __set_page_dirty_nobuffers(async_chunk->locked_page);
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700672 /* unlocked later on in the async handlers */
673
674 if (redirty)
675 extent_range_redirty_for_io(inode, start, end);
Nikolay Borisovb5326272019-03-12 17:20:25 +0200676 add_async_extent(async_chunk, start, end - start + 1, 0, NULL, 0,
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700677 BTRFS_COMPRESS_NONE);
678 *num_added += 1;
Chris Mason771ed682008-11-06 22:02:51 -0500679
Filipe Mananac44f6492014-10-09 21:15:44 +0100680 return;
Chris Mason771ed682008-11-06 22:02:51 -0500681
682free_pages_out:
David Sterba4d3a8002017-02-14 19:04:07 +0100683 for (i = 0; i < nr_pages; i++) {
Chris Mason771ed682008-11-06 22:02:51 -0500684 WARN_ON(pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300685 put_page(pages[i]);
Chris Mason771ed682008-11-06 22:02:51 -0500686 }
Chris Masond3977122009-01-05 21:25:51 -0500687 kfree(pages);
Chris Mason771ed682008-11-06 22:02:51 -0500688}
Chris Mason771ed682008-11-06 22:02:51 -0500689
Filipe Manana40ae8372014-10-06 22:14:24 +0100690static void free_async_extent_pages(struct async_extent *async_extent)
691{
692 int i;
693
694 if (!async_extent->pages)
695 return;
696
697 for (i = 0; i < async_extent->nr_pages; i++) {
698 WARN_ON(async_extent->pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300699 put_page(async_extent->pages[i]);
Filipe Manana40ae8372014-10-06 22:14:24 +0100700 }
701 kfree(async_extent->pages);
702 async_extent->nr_pages = 0;
703 async_extent->pages = NULL;
Chris Mason771ed682008-11-06 22:02:51 -0500704}
705
706/*
707 * phase two of compressed writeback. This is the ordered portion
708 * of the code, which only gets called in the order the work was
709 * queued. We walk all the async extents created by compress_file_range
710 * and send them down to the disk.
711 */
Nikolay Borisovb5326272019-03-12 17:20:25 +0200712static noinline void submit_compressed_extents(struct async_chunk *async_chunk)
Chris Mason771ed682008-11-06 22:02:51 -0500713{
Nikolay Borisovb5326272019-03-12 17:20:25 +0200714 struct inode *inode = async_chunk->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400715 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason771ed682008-11-06 22:02:51 -0500716 struct async_extent *async_extent;
717 u64 alloc_hint = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500718 struct btrfs_key ins;
719 struct extent_map *em;
720 struct btrfs_root *root = BTRFS_I(inode)->root;
Nikolay Borisov43366502019-03-12 17:20:29 +0200721 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500722 int ret = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500723
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500724again:
Nikolay Borisovb5326272019-03-12 17:20:25 +0200725 while (!list_empty(&async_chunk->extents)) {
726 async_extent = list_entry(async_chunk->extents.next,
Chris Mason771ed682008-11-06 22:02:51 -0500727 struct async_extent, list);
728 list_del(&async_extent->list);
Nikolay Borisov74475552019-03-12 17:20:30 +0200729
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500730retry:
Nikolay Borisov74475552019-03-12 17:20:30 +0200731 lock_extent(io_tree, async_extent->start,
732 async_extent->start + async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500733 /* did the compression code fall back to uncompressed IO? */
734 if (!async_extent->pages) {
735 int page_started = 0;
736 unsigned long nr_written = 0;
737
Chris Mason771ed682008-11-06 22:02:51 -0500738 /* allocate blocks */
Nikolay Borisovb5326272019-03-12 17:20:25 +0200739 ret = cow_file_range(inode, async_chunk->locked_page,
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500740 async_extent->start,
741 async_extent->start +
742 async_extent->ram_size - 1,
Wang Xiaoguangdda32452016-07-11 11:05:29 +0800743 async_extent->start +
744 async_extent->ram_size - 1,
745 &page_started, &nr_written, 0,
746 NULL);
Chris Mason771ed682008-11-06 22:02:51 -0500747
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100748 /* JDM XXX */
749
Chris Mason771ed682008-11-06 22:02:51 -0500750 /*
751 * if page_started, cow_file_range inserted an
752 * inline extent and took care of all the unlocking
753 * and IO for us. Otherwise, we need to submit
754 * all those pages down to the drive.
755 */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500756 if (!page_started && !ret)
Nikolay Borisov5e3ee232017-12-08 15:55:58 +0200757 extent_write_locked_range(inode,
758 async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500759 async_extent->start +
Chris Mason771ed682008-11-06 22:02:51 -0500760 async_extent->ram_size - 1,
Chris Mason771ed682008-11-06 22:02:51 -0500761 WB_SYNC_ALL);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500762 else if (ret)
Nikolay Borisovb5326272019-03-12 17:20:25 +0200763 unlock_page(async_chunk->locked_page);
Chris Mason771ed682008-11-06 22:02:51 -0500764 kfree(async_extent);
765 cond_resched();
766 continue;
767 }
768
Wang Xiaoguang18513092016-07-25 15:51:40 +0800769 ret = btrfs_reserve_extent(root, async_extent->ram_size,
Chris Mason771ed682008-11-06 22:02:51 -0500770 async_extent->compressed_size,
771 async_extent->compressed_size,
Miao Xiee570fd22014-06-19 10:42:50 +0800772 0, alloc_hint, &ins, 1, 1);
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500773 if (ret) {
Filipe Manana40ae8372014-10-06 22:14:24 +0100774 free_async_extent_pages(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500775
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400776 if (ret == -ENOSPC) {
777 unlock_extent(io_tree, async_extent->start,
778 async_extent->start +
779 async_extent->ram_size - 1);
Liu Boce620032014-07-24 22:48:05 +0800780
781 /*
782 * we need to redirty the pages if we decide to
783 * fallback to uncompressed IO, otherwise we
784 * will not submit these pages down to lower
785 * layers.
786 */
787 extent_range_redirty_for_io(inode,
788 async_extent->start,
789 async_extent->start +
790 async_extent->ram_size - 1);
791
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100792 goto retry;
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400793 }
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500794 goto out_free;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500795 }
Yan, Zhengc2167752009-11-12 09:34:21 +0000796 /*
797 * here we're doing allocation and writeback of the
798 * compressed pages
799 */
Liu Bo6f9994d2017-01-31 07:50:22 -0800800 em = create_io_em(inode, async_extent->start,
801 async_extent->ram_size, /* len */
802 async_extent->start, /* orig_start */
803 ins.objectid, /* block_start */
804 ins.offset, /* block_len */
805 ins.offset, /* orig_block_len */
806 async_extent->ram_size, /* ram_bytes */
807 async_extent->compress_type,
808 BTRFS_ORDERED_COMPRESSED);
809 if (IS_ERR(em))
810 /* ret value is not necessary due to void function */
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500811 goto out_free_reserve;
Liu Bo6f9994d2017-01-31 07:50:22 -0800812 free_extent_map(em);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500813
Li Zefan261507a02010-12-17 14:21:50 +0800814 ret = btrfs_add_ordered_extent_compress(inode,
815 async_extent->start,
816 ins.objectid,
817 async_extent->ram_size,
818 ins.offset,
819 BTRFS_ORDERED_COMPRESSED,
820 async_extent->compress_type);
Filipe Mananad9f85962014-08-25 10:43:00 +0100821 if (ret) {
Nikolay Borisovdcdbc052017-02-20 13:50:45 +0200822 btrfs_drop_extent_cache(BTRFS_I(inode),
823 async_extent->start,
Filipe Mananad9f85962014-08-25 10:43:00 +0100824 async_extent->start +
825 async_extent->ram_size - 1, 0);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500826 goto out_free_reserve;
Filipe Mananad9f85962014-08-25 10:43:00 +0100827 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400828 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Chris Mason771ed682008-11-06 22:02:51 -0500829
Chris Mason771ed682008-11-06 22:02:51 -0500830 /*
831 * clear dirty, set writeback and unlock the pages.
832 */
Josef Bacikc2790a22013-07-29 11:20:47 -0400833 extent_clear_unlock_delalloc(inode, async_extent->start,
Chris Masona791e352009-10-08 11:27:10 -0400834 async_extent->start +
835 async_extent->ram_size - 1,
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800836 async_extent->start +
837 async_extent->ram_size - 1,
Josef Bacik151a41b2013-07-29 13:22:24 -0400838 NULL, EXTENT_LOCKED | EXTENT_DELALLOC,
839 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -0400840 PAGE_SET_WRITEBACK);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200841 if (btrfs_submit_compressed_write(inode,
Chris Masond3977122009-01-05 21:25:51 -0500842 async_extent->start,
843 async_extent->ram_size,
844 ins.objectid,
845 ins.offset, async_extent->pages,
Liu Bof82b7352017-10-23 23:18:16 -0600846 async_extent->nr_pages,
Nikolay Borisovb5326272019-03-12 17:20:25 +0200847 async_chunk->write_flags)) {
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100848 struct page *p = async_extent->pages[0];
849 const u64 start = async_extent->start;
850 const u64 end = start + async_extent->ram_size - 1;
851
852 p->mapping = inode->i_mapping;
Nikolay Borisovc6297322018-11-08 10:18:08 +0200853 btrfs_writepage_endio_finish_ordered(p, start, end, 0);
Nikolay Borisov7087a9d2018-11-01 14:09:48 +0200854
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100855 p->mapping = NULL;
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800856 extent_clear_unlock_delalloc(inode, start, end, end,
857 NULL, 0,
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100858 PAGE_END_WRITEBACK |
859 PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +0100860 free_async_extent_pages(async_extent);
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100861 }
Chris Mason771ed682008-11-06 22:02:51 -0500862 alloc_hint = ins.objectid + ins.offset;
863 kfree(async_extent);
864 cond_resched();
865 }
Filipe Mananadec8f172014-10-06 22:14:26 +0100866 return;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500867out_free_reserve:
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400868 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400869 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100870out_free:
Josef Bacikc2790a22013-07-29 11:20:47 -0400871 extent_clear_unlock_delalloc(inode, async_extent->start,
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500872 async_extent->start +
873 async_extent->ram_size - 1,
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800874 async_extent->start +
875 async_extent->ram_size - 1,
Josef Bacikc2790a22013-07-29 11:20:47 -0400876 NULL, EXTENT_LOCKED | EXTENT_DELALLOC |
Filipe Mananaa7e3b972017-04-03 10:45:46 +0100877 EXTENT_DELALLOC_NEW |
Josef Bacik151a41b2013-07-29 13:22:24 -0400878 EXTENT_DEFRAG | EXTENT_DO_ACCOUNTING,
879 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Filipe Manana704de492014-10-06 22:14:22 +0100880 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK |
881 PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +0100882 free_async_extent_pages(async_extent);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100883 kfree(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500884 goto again;
Chris Mason771ed682008-11-06 22:02:51 -0500885}
886
Josef Bacik4b46fce2010-05-23 11:00:55 -0400887static u64 get_extent_allocation_hint(struct inode *inode, u64 start,
888 u64 num_bytes)
889{
890 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
891 struct extent_map *em;
892 u64 alloc_hint = 0;
893
894 read_lock(&em_tree->lock);
895 em = search_extent_mapping(em_tree, start, num_bytes);
896 if (em) {
897 /*
898 * if block start isn't an actual block number then find the
899 * first block in this inode and use that as a hint. If that
900 * block is also bogus then just don't worry about it.
901 */
902 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
903 free_extent_map(em);
904 em = search_extent_mapping(em_tree, 0, 0);
905 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
906 alloc_hint = em->block_start;
907 if (em)
908 free_extent_map(em);
909 } else {
910 alloc_hint = em->block_start;
911 free_extent_map(em);
912 }
913 }
914 read_unlock(&em_tree->lock);
915
916 return alloc_hint;
917}
918
Chris Mason771ed682008-11-06 22:02:51 -0500919/*
920 * when extent_io.c finds a delayed allocation range in the file,
921 * the call backs end up in this code. The basic idea is to
922 * allocate extents on disk for the range, and create ordered data structs
923 * in ram to track those extents.
924 *
925 * locked_page is the page that writepage had locked already. We use
926 * it to make sure we don't do extra locks or unlocks.
927 *
928 * *page_started is set to one if we unlock locked_page and do everything
929 * required to start IO on it. It may be clean and already done with
930 * IO when we return.
931 */
Josef Bacik00361582013-08-14 14:02:47 -0400932static noinline int cow_file_range(struct inode *inode,
933 struct page *locked_page,
Wang Xiaoguangdda32452016-07-11 11:05:29 +0800934 u64 start, u64 end, u64 delalloc_end,
935 int *page_started, unsigned long *nr_written,
936 int unlock, struct btrfs_dedupe_hash *hash)
Chris Mason771ed682008-11-06 22:02:51 -0500937{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400938 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik00361582013-08-14 14:02:47 -0400939 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason771ed682008-11-06 22:02:51 -0500940 u64 alloc_hint = 0;
941 u64 num_bytes;
942 unsigned long ram_size;
Filipe Mananaa315e682017-03-06 23:04:20 +0000943 u64 cur_alloc_size = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400944 u64 blocksize = fs_info->sectorsize;
Chris Mason771ed682008-11-06 22:02:51 -0500945 struct btrfs_key ins;
946 struct extent_map *em;
Filipe Mananaa315e682017-03-06 23:04:20 +0000947 unsigned clear_bits;
948 unsigned long page_ops;
949 bool extent_reserved = false;
Chris Mason771ed682008-11-06 22:02:51 -0500950 int ret = 0;
951
Nikolay Borisov70ddc552017-02-20 13:50:35 +0200952 if (btrfs_is_free_space_inode(BTRFS_I(inode))) {
Josef Bacik02ecd2c2013-10-25 16:19:08 -0400953 WARN_ON_ONCE(1);
Josef Bacik29bce2f2014-02-07 12:21:23 -0500954 ret = -EINVAL;
955 goto out_unlock;
Josef Bacik02ecd2c2013-10-25 16:19:08 -0400956 }
Chris Mason771ed682008-11-06 22:02:51 -0500957
Qu Wenruofda28322013-02-26 08:10:22 +0000958 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Mason771ed682008-11-06 22:02:51 -0500959 num_bytes = max(blocksize, num_bytes);
Anand Jain566b1762018-02-15 18:07:59 +0800960 ASSERT(num_bytes <= btrfs_super_total_bytes(fs_info->super_copy));
Chris Mason771ed682008-11-06 22:02:51 -0500961
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200962 inode_should_defrag(BTRFS_I(inode), start, end, num_bytes, SZ_64K);
Chris Mason4cb53002011-05-24 15:35:30 -0400963
Chris Mason771ed682008-11-06 22:02:51 -0500964 if (start == 0) {
965 /* lets try to make an inline extent */
Nikolay Borisovd02c0e22018-03-02 09:43:15 +0200966 ret = cow_file_range_inline(inode, start, end, 0,
967 BTRFS_COMPRESS_NONE, NULL);
Chris Mason771ed682008-11-06 22:02:51 -0500968 if (ret == 0) {
Josef Bacik8b62f872017-10-19 14:15:55 -0400969 /*
970 * We use DO_ACCOUNTING here because we need the
971 * delalloc_release_metadata to be run _after_ we drop
972 * our outstanding extent for clearing delalloc for this
973 * range.
974 */
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800975 extent_clear_unlock_delalloc(inode, start, end,
976 delalloc_end, NULL,
Josef Bacikc2790a22013-07-29 11:20:47 -0400977 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik8b62f872017-10-19 14:15:55 -0400978 EXTENT_DELALLOC_NEW | EXTENT_DEFRAG |
979 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -0400980 PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
981 PAGE_END_WRITEBACK);
Chris Mason771ed682008-11-06 22:02:51 -0500982 *nr_written = *nr_written +
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300983 (end - start + PAGE_SIZE) / PAGE_SIZE;
Chris Mason771ed682008-11-06 22:02:51 -0500984 *page_started = 1;
Chris Mason771ed682008-11-06 22:02:51 -0500985 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100986 } else if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100987 goto out_unlock;
Chris Mason771ed682008-11-06 22:02:51 -0500988 }
989 }
Chris Masonc8b97812008-10-29 14:49:59 -0400990
Josef Bacik4b46fce2010-05-23 11:00:55 -0400991 alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +0200992 btrfs_drop_extent_cache(BTRFS_I(inode), start,
993 start + num_bytes - 1, 0);
Chris Mason3b951512008-04-17 11:29:12 -0400994
Anand Jain3752d222018-02-15 12:29:38 +0800995 while (num_bytes > 0) {
996 cur_alloc_size = num_bytes;
Wang Xiaoguang18513092016-07-25 15:51:40 +0800997 ret = btrfs_reserve_extent(root, cur_alloc_size, cur_alloc_size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400998 fs_info->sectorsize, 0, alloc_hint,
Miao Xiee570fd22014-06-19 10:42:50 +0800999 &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04001000 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001001 goto out_unlock;
Filipe Mananaa315e682017-03-06 23:04:20 +00001002 cur_alloc_size = ins.offset;
1003 extent_reserved = true;
Chris Masond3977122009-01-05 21:25:51 -05001004
Chris Mason771ed682008-11-06 22:02:51 -05001005 ram_size = ins.offset;
Liu Bo6f9994d2017-01-31 07:50:22 -08001006 em = create_io_em(inode, start, ins.offset, /* len */
1007 start, /* orig_start */
1008 ins.objectid, /* block_start */
1009 ins.offset, /* block_len */
1010 ins.offset, /* orig_block_len */
1011 ram_size, /* ram_bytes */
1012 BTRFS_COMPRESS_NONE, /* compress_type */
Liu Bo1af4a0a2017-02-13 15:35:09 -08001013 BTRFS_ORDERED_REGULAR /* type */);
Su Yue090a127a2018-05-30 16:48:56 +08001014 if (IS_ERR(em)) {
1015 ret = PTR_ERR(em);
Liu Boace68ba2013-04-22 10:53:47 +00001016 goto out_reserve;
Su Yue090a127a2018-05-30 16:48:56 +08001017 }
Liu Bo6f9994d2017-01-31 07:50:22 -08001018 free_extent_map(em);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001019
Chris Masone6dcd2d2008-07-17 12:53:50 -04001020 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
Chris Mason771ed682008-11-06 22:02:51 -05001021 ram_size, cur_alloc_size, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001022 if (ret)
Filipe Mananad9f85962014-08-25 10:43:00 +01001023 goto out_drop_extent_cache;
Chris Masonc8b97812008-10-29 14:49:59 -04001024
Yan Zheng17d217f2008-12-12 10:03:38 -05001025 if (root->root_key.objectid ==
1026 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1027 ret = btrfs_reloc_clone_csums(inode, start,
1028 cur_alloc_size);
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001029 /*
1030 * Only drop cache here, and process as normal.
1031 *
1032 * We must not allow extent_clear_unlock_delalloc()
1033 * at out_unlock label to free meta of this ordered
1034 * extent, as its meta should be freed by
1035 * btrfs_finish_ordered_io().
1036 *
1037 * So we must continue until @start is increased to
1038 * skip current ordered extent.
1039 */
Josef Bacik00361582013-08-14 14:02:47 -04001040 if (ret)
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001041 btrfs_drop_extent_cache(BTRFS_I(inode), start,
1042 start + ram_size - 1, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001043 }
1044
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001045 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana9cfa3e32016-04-26 15:39:32 +01001046
Chris Masonc8b97812008-10-29 14:49:59 -04001047 /* we're not doing compressed IO, don't unlock the first
1048 * page (which the caller expects to stay locked), don't
1049 * clear any dirty bits and don't set any writeback bits
Chris Mason8b62b722009-09-02 16:53:46 -04001050 *
1051 * Do set the Private2 bit so we know this page was properly
1052 * setup for writepage
Chris Masonc8b97812008-10-29 14:49:59 -04001053 */
Filipe Mananaa315e682017-03-06 23:04:20 +00001054 page_ops = unlock ? PAGE_UNLOCK : 0;
1055 page_ops |= PAGE_SET_PRIVATE2;
Chris Masona791e352009-10-08 11:27:10 -04001056
Josef Bacikc2790a22013-07-29 11:20:47 -04001057 extent_clear_unlock_delalloc(inode, start,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001058 start + ram_size - 1,
1059 delalloc_end, locked_page,
Josef Bacikc2790a22013-07-29 11:20:47 -04001060 EXTENT_LOCKED | EXTENT_DELALLOC,
Filipe Mananaa315e682017-03-06 23:04:20 +00001061 page_ops);
Anand Jain3752d222018-02-15 12:29:38 +08001062 if (num_bytes < cur_alloc_size)
1063 num_bytes = 0;
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001064 else
Anand Jain3752d222018-02-15 12:29:38 +08001065 num_bytes -= cur_alloc_size;
Chris Masonc59f8952007-12-17 20:14:04 -05001066 alloc_hint = ins.objectid + ins.offset;
1067 start += cur_alloc_size;
Filipe Mananaa315e682017-03-06 23:04:20 +00001068 extent_reserved = false;
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001069
1070 /*
1071 * btrfs_reloc_clone_csums() error, since start is increased
1072 * extent_clear_unlock_delalloc() at out_unlock label won't
1073 * free metadata of current ordered extent, we're OK to exit.
1074 */
1075 if (ret)
1076 goto out_unlock;
Chris Masonb888db22007-08-27 16:49:44 -04001077 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001078out:
Chris Masonbe20aa92007-12-17 20:14:01 -05001079 return ret;
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001080
Filipe Mananad9f85962014-08-25 10:43:00 +01001081out_drop_extent_cache:
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02001082 btrfs_drop_extent_cache(BTRFS_I(inode), start, start + ram_size - 1, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001083out_reserve:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001084 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001085 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001086out_unlock:
Filipe Mananaa7e3b972017-04-03 10:45:46 +01001087 clear_bits = EXTENT_LOCKED | EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
1088 EXTENT_DEFRAG | EXTENT_CLEAR_META_RESV;
Filipe Mananaa315e682017-03-06 23:04:20 +00001089 page_ops = PAGE_UNLOCK | PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
1090 PAGE_END_WRITEBACK;
1091 /*
1092 * If we reserved an extent for our delalloc range (or a subrange) and
1093 * failed to create the respective ordered extent, then it means that
1094 * when we reserved the extent we decremented the extent's size from
1095 * the data space_info's bytes_may_use counter and incremented the
1096 * space_info's bytes_reserved counter by the same amount. We must make
1097 * sure extent_clear_unlock_delalloc() does not try to decrement again
1098 * the data space_info's bytes_may_use counter, therefore we do not pass
1099 * it the flag EXTENT_CLEAR_DATA_RESV.
1100 */
1101 if (extent_reserved) {
1102 extent_clear_unlock_delalloc(inode, start,
1103 start + cur_alloc_size,
1104 start + cur_alloc_size,
1105 locked_page,
1106 clear_bits,
1107 page_ops);
1108 start += cur_alloc_size;
1109 if (start >= end)
1110 goto out;
1111 }
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001112 extent_clear_unlock_delalloc(inode, start, end, delalloc_end,
1113 locked_page,
Filipe Mananaa315e682017-03-06 23:04:20 +00001114 clear_bits | EXTENT_CLEAR_DATA_RESV,
1115 page_ops);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001116 goto out;
Chris Mason771ed682008-11-06 22:02:51 -05001117}
Chris Masonc8b97812008-10-29 14:49:59 -04001118
Chris Mason771ed682008-11-06 22:02:51 -05001119/*
1120 * work queue call back to started compression on a file and pages
1121 */
1122static noinline void async_cow_start(struct btrfs_work *work)
1123{
Nikolay Borisovb5326272019-03-12 17:20:25 +02001124 struct async_chunk *async_chunk;
Chris Mason771ed682008-11-06 22:02:51 -05001125 int num_added = 0;
Nikolay Borisov97db1202019-03-12 17:20:24 +02001126
Nikolay Borisovb5326272019-03-12 17:20:25 +02001127 async_chunk = container_of(work, struct async_chunk, work);
Chris Mason771ed682008-11-06 22:02:51 -05001128
Nikolay Borisov1368c6d2019-03-12 17:20:27 +02001129 compress_file_range(async_chunk, &num_added);
Josef Bacik8180ef82012-06-08 15:16:12 -04001130 if (num_added == 0) {
Nikolay Borisovb5326272019-03-12 17:20:25 +02001131 btrfs_add_delayed_iput(async_chunk->inode);
1132 async_chunk->inode = NULL;
Josef Bacik8180ef82012-06-08 15:16:12 -04001133 }
Chris Mason771ed682008-11-06 22:02:51 -05001134}
1135
1136/*
1137 * work queue call back to submit previously compressed pages
1138 */
1139static noinline void async_cow_submit(struct btrfs_work *work)
1140{
Nikolay Borisovc5a68ae2019-03-12 17:20:26 +02001141 struct async_chunk *async_chunk = container_of(work, struct async_chunk,
1142 work);
1143 struct btrfs_fs_info *fs_info = btrfs_work_owner(work);
Chris Mason771ed682008-11-06 22:02:51 -05001144 unsigned long nr_pages;
1145
Nikolay Borisovb5326272019-03-12 17:20:25 +02001146 nr_pages = (async_chunk->end - async_chunk->start + PAGE_SIZE) >>
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001147 PAGE_SHIFT;
Chris Mason771ed682008-11-06 22:02:51 -05001148
David Sterba093258e2018-02-26 16:15:17 +01001149 /* atomic_sub_return implies a barrier */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001150 if (atomic_sub_return(nr_pages, &fs_info->async_delalloc_pages) <
David Sterba093258e2018-02-26 16:15:17 +01001151 5 * SZ_1M)
1152 cond_wake_up_nomb(&fs_info->async_submit_wait);
Chris Mason771ed682008-11-06 22:02:51 -05001153
Nikolay Borisov4546d172019-01-03 10:50:03 +02001154 /*
Nikolay Borisovb5326272019-03-12 17:20:25 +02001155 * ->inode could be NULL if async_chunk_start has failed to compress,
Nikolay Borisov4546d172019-01-03 10:50:03 +02001156 * in which case we don't have anything to submit, yet we need to
1157 * always adjust ->async_delalloc_pages as its paired with the init
1158 * happening in cow_file_range_async
1159 */
Nikolay Borisovb5326272019-03-12 17:20:25 +02001160 if (async_chunk->inode)
1161 submit_compressed_extents(async_chunk);
Chris Mason771ed682008-11-06 22:02:51 -05001162}
Chris Masonc8b97812008-10-29 14:49:59 -04001163
Chris Mason771ed682008-11-06 22:02:51 -05001164static noinline void async_cow_free(struct btrfs_work *work)
1165{
Nikolay Borisovb5326272019-03-12 17:20:25 +02001166 struct async_chunk *async_chunk;
Nikolay Borisov97db1202019-03-12 17:20:24 +02001167
Nikolay Borisovb5326272019-03-12 17:20:25 +02001168 async_chunk = container_of(work, struct async_chunk, work);
1169 if (async_chunk->inode)
1170 btrfs_add_delayed_iput(async_chunk->inode);
Nikolay Borisov97db1202019-03-12 17:20:24 +02001171 /*
1172 * Since the pointer to 'pending' is at the beginning of the array of
Nikolay Borisovb5326272019-03-12 17:20:25 +02001173 * async_chunk's, freeing it ensures the whole array has been freed.
Nikolay Borisov97db1202019-03-12 17:20:24 +02001174 */
Nikolay Borisovb5326272019-03-12 17:20:25 +02001175 if (atomic_dec_and_test(async_chunk->pending))
Nikolay Borisovb1c16ac2019-04-01 11:29:57 +03001176 kvfree(async_chunk->pending);
Chris Mason771ed682008-11-06 22:02:51 -05001177}
1178
1179static int cow_file_range_async(struct inode *inode, struct page *locked_page,
1180 u64 start, u64 end, int *page_started,
Liu Bof82b7352017-10-23 23:18:16 -06001181 unsigned long *nr_written,
1182 unsigned int write_flags)
Chris Mason771ed682008-11-06 22:02:51 -05001183{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001184 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Nikolay Borisov97db1202019-03-12 17:20:24 +02001185 struct async_cow *ctx;
1186 struct async_chunk *async_chunk;
Chris Mason771ed682008-11-06 22:02:51 -05001187 unsigned long nr_pages;
1188 u64 cur_end;
Nikolay Borisov97db1202019-03-12 17:20:24 +02001189 u64 num_chunks = DIV_ROUND_UP(end - start, SZ_512K);
1190 int i;
1191 bool should_compress;
Nikolay Borisovb1c16ac2019-04-01 11:29:57 +03001192 unsigned nofs_flag;
Chris Mason771ed682008-11-06 22:02:51 -05001193
Nikolay Borisov69684c52019-03-12 17:20:28 +02001194 unlock_extent(&BTRFS_I(inode)->io_tree, start, end);
Nikolay Borisov97db1202019-03-12 17:20:24 +02001195
1196 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS &&
1197 !btrfs_test_opt(fs_info, FORCE_COMPRESS)) {
1198 num_chunks = 1;
1199 should_compress = false;
1200 } else {
1201 should_compress = true;
1202 }
1203
Nikolay Borisovb1c16ac2019-04-01 11:29:57 +03001204 nofs_flag = memalloc_nofs_save();
1205 ctx = kvmalloc(struct_size(ctx, chunks, num_chunks), GFP_KERNEL);
1206 memalloc_nofs_restore(nofs_flag);
1207
Nikolay Borisov97db1202019-03-12 17:20:24 +02001208 if (!ctx) {
1209 unsigned clear_bits = EXTENT_LOCKED | EXTENT_DELALLOC |
1210 EXTENT_DELALLOC_NEW | EXTENT_DEFRAG |
1211 EXTENT_DO_ACCOUNTING;
1212 unsigned long page_ops = PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
1213 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK |
1214 PAGE_SET_ERROR;
1215
1216 extent_clear_unlock_delalloc(inode, start, end, 0, locked_page,
1217 clear_bits, page_ops);
1218 return -ENOMEM;
1219 }
1220
1221 async_chunk = ctx->chunks;
1222 atomic_set(&ctx->num_chunks, num_chunks);
1223
1224 for (i = 0; i < num_chunks; i++) {
1225 if (should_compress)
1226 cur_end = min(end, start + SZ_512K - 1);
1227 else
1228 cur_end = end;
1229
Nikolay Borisovbd4691a2019-01-03 10:50:01 +02001230 /*
1231 * igrab is called higher up in the call chain, take only the
1232 * lightweight reference for the callback lifetime
1233 */
1234 ihold(inode);
Nikolay Borisov97db1202019-03-12 17:20:24 +02001235 async_chunk[i].pending = &ctx->num_chunks;
1236 async_chunk[i].inode = inode;
1237 async_chunk[i].start = start;
1238 async_chunk[i].end = cur_end;
Nikolay Borisov97db1202019-03-12 17:20:24 +02001239 async_chunk[i].locked_page = locked_page;
1240 async_chunk[i].write_flags = write_flags;
1241 INIT_LIST_HEAD(&async_chunk[i].extents);
Chris Mason771ed682008-11-06 22:02:51 -05001242
Nikolay Borisov97db1202019-03-12 17:20:24 +02001243 btrfs_init_work(&async_chunk[i].work,
Liu Bo9e0af232014-08-15 23:36:53 +08001244 btrfs_delalloc_helper,
1245 async_cow_start, async_cow_submit,
1246 async_cow_free);
Chris Mason771ed682008-11-06 22:02:51 -05001247
Nikolay Borisov97db1202019-03-12 17:20:24 +02001248 nr_pages = DIV_ROUND_UP(cur_end - start, PAGE_SIZE);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001249 atomic_add(nr_pages, &fs_info->async_delalloc_pages);
Chris Mason771ed682008-11-06 22:02:51 -05001250
Nikolay Borisov97db1202019-03-12 17:20:24 +02001251 btrfs_queue_work(fs_info->delalloc_workers, &async_chunk[i].work);
Chris Mason771ed682008-11-06 22:02:51 -05001252
Chris Mason771ed682008-11-06 22:02:51 -05001253 *nr_written += nr_pages;
1254 start = cur_end + 1;
1255 }
1256 *page_started = 1;
1257 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001258}
1259
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001260static noinline int csum_exist_in_range(struct btrfs_fs_info *fs_info,
Yan Zheng17d217f2008-12-12 10:03:38 -05001261 u64 bytenr, u64 num_bytes)
1262{
1263 int ret;
1264 struct btrfs_ordered_sum *sums;
1265 LIST_HEAD(list);
1266
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001267 ret = btrfs_lookup_csums_range(fs_info->csum_root, bytenr,
Arne Jansena2de7332011-03-08 14:14:00 +01001268 bytenr + num_bytes - 1, &list, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001269 if (ret == 0 && list_empty(&list))
1270 return 0;
1271
1272 while (!list_empty(&list)) {
1273 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1274 list_del(&sums->list);
1275 kfree(sums);
1276 }
Liu Bo58113752018-01-31 17:09:13 -07001277 if (ret < 0)
1278 return ret;
Yan Zheng17d217f2008-12-12 10:03:38 -05001279 return 1;
1280}
1281
Chris Masond352ac62008-09-29 15:18:18 -04001282/*
1283 * when nowcow writeback call back. This checks for snapshots or COW copies
1284 * of the extents that exist in the file, and COWs the file as required.
1285 *
1286 * If no cow copies or snapshots exist, we write directly to the existing
1287 * blocks on disk
1288 */
Chris Mason7f366cf2009-03-12 20:12:45 -04001289static noinline int run_delalloc_nocow(struct inode *inode,
1290 struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001291 u64 start, u64 end, int *page_started, int force,
1292 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001293{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001294 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masonbe20aa92007-12-17 20:14:01 -05001295 struct btrfs_root *root = BTRFS_I(inode)->root;
1296 struct extent_buffer *leaf;
Chris Masonbe20aa92007-12-17 20:14:01 -05001297 struct btrfs_path *path;
Yan Zheng80ff3852008-10-30 14:20:02 -04001298 struct btrfs_file_extent_item *fi;
Chris Masonbe20aa92007-12-17 20:14:01 -05001299 struct btrfs_key found_key;
Liu Bo6f9994d2017-01-31 07:50:22 -08001300 struct extent_map *em;
Yan Zheng80ff3852008-10-30 14:20:02 -04001301 u64 cow_start;
1302 u64 cur_offset;
1303 u64 extent_end;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001304 u64 extent_offset;
Yan Zheng80ff3852008-10-30 14:20:02 -04001305 u64 disk_bytenr;
1306 u64 num_bytes;
Josef Bacikb4939682012-12-03 10:31:19 -05001307 u64 disk_num_bytes;
Josef Bacikcc95bef2013-04-04 14:31:27 -04001308 u64 ram_bytes;
Yan Zheng80ff3852008-10-30 14:20:02 -04001309 int extent_type;
Robbie Ko8ecebf4d2018-08-06 10:30:30 +08001310 int ret;
Yan Zhengd899e052008-10-30 14:25:28 -04001311 int type;
Yan Zheng80ff3852008-10-30 14:20:02 -04001312 int nocow;
1313 int check_prev = 1;
Li Zefan82d59022011-04-20 10:33:24 +08001314 bool nolock;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02001315 u64 ino = btrfs_ino(BTRFS_I(inode));
Chris Masonbe20aa92007-12-17 20:14:01 -05001316
1317 path = btrfs_alloc_path();
Josef Bacik17ca04a2012-05-31 15:58:55 -04001318 if (!path) {
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001319 extent_clear_unlock_delalloc(inode, start, end, end,
1320 locked_page,
Josef Bacikc2790a22013-07-29 11:20:47 -04001321 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -04001322 EXTENT_DO_ACCOUNTING |
1323 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001324 PAGE_CLEAR_DIRTY |
1325 PAGE_SET_WRITEBACK |
1326 PAGE_END_WRITEBACK);
Mark Fashehd8926bb2011-07-13 10:38:47 -07001327 return -ENOMEM;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001328 }
Li Zefan82d59022011-04-20 10:33:24 +08001329
Nikolay Borisov70ddc552017-02-20 13:50:35 +02001330 nolock = btrfs_is_free_space_inode(BTRFS_I(inode));
Li Zefan82d59022011-04-20 10:33:24 +08001331
Yan Zheng80ff3852008-10-30 14:20:02 -04001332 cow_start = (u64)-1;
1333 cur_offset = start;
1334 while (1) {
Liu Boe4c3b2d2017-01-30 12:25:28 -08001335 ret = btrfs_lookup_file_extent(NULL, root, path, ino,
Yan Zheng80ff3852008-10-30 14:20:02 -04001336 cur_offset, 0);
Josef Bacikd788a342013-10-25 16:55:08 -04001337 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001338 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001339 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1340 leaf = path->nodes[0];
1341 btrfs_item_key_to_cpu(leaf, &found_key,
1342 path->slots[0] - 1);
Li Zefan33345d012011-04-20 10:31:50 +08001343 if (found_key.objectid == ino &&
Yan Zheng80ff3852008-10-30 14:20:02 -04001344 found_key.type == BTRFS_EXTENT_DATA_KEY)
1345 path->slots[0]--;
1346 }
1347 check_prev = 0;
1348next_slot:
1349 leaf = path->nodes[0];
1350 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1351 ret = btrfs_next_leaf(root, path);
Liu Boe8916692018-01-25 11:02:50 -07001352 if (ret < 0) {
1353 if (cow_start != (u64)-1)
1354 cur_offset = cow_start;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001355 goto error;
Liu Boe8916692018-01-25 11:02:50 -07001356 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001357 if (ret > 0)
1358 break;
1359 leaf = path->nodes[0];
1360 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001361
Yan Zheng80ff3852008-10-30 14:20:02 -04001362 nocow = 0;
1363 disk_bytenr = 0;
Yan Zheng17d217f2008-12-12 10:03:38 -05001364 num_bytes = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001365 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001366
Filipe Manana1d512cb2015-11-09 00:33:58 +00001367 if (found_key.objectid > ino)
1368 break;
1369 if (WARN_ON_ONCE(found_key.objectid < ino) ||
1370 found_key.type < BTRFS_EXTENT_DATA_KEY) {
1371 path->slots[0]++;
1372 goto next_slot;
1373 }
1374 if (found_key.type > BTRFS_EXTENT_DATA_KEY ||
Yan Zheng80ff3852008-10-30 14:20:02 -04001375 found_key.offset > end)
1376 break;
Chris Masonbe20aa92007-12-17 20:14:01 -05001377
Yan Zheng80ff3852008-10-30 14:20:02 -04001378 if (found_key.offset > cur_offset) {
1379 extent_end = found_key.offset;
Chris Masone9061e22009-10-09 09:57:45 -04001380 extent_type = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001381 goto out_check;
1382 }
Chris Masonc31f8832008-01-08 15:46:31 -05001383
Yan Zheng80ff3852008-10-30 14:20:02 -04001384 fi = btrfs_item_ptr(leaf, path->slots[0],
1385 struct btrfs_file_extent_item);
1386 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001387
Josef Bacikcc95bef2013-04-04 14:31:27 -04001388 ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
Yan Zhengd899e052008-10-30 14:25:28 -04001389 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1390 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001391 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001392 extent_offset = btrfs_file_extent_offset(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001393 extent_end = found_key.offset +
1394 btrfs_file_extent_num_bytes(leaf, fi);
Josef Bacikb4939682012-12-03 10:31:19 -05001395 disk_num_bytes =
1396 btrfs_file_extent_disk_num_bytes(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001397 if (extent_end <= start) {
1398 path->slots[0]++;
1399 goto next_slot;
1400 }
Yan Zheng17d217f2008-12-12 10:03:38 -05001401 if (disk_bytenr == 0)
1402 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001403 if (btrfs_file_extent_compression(leaf, fi) ||
1404 btrfs_file_extent_encryption(leaf, fi) ||
1405 btrfs_file_extent_other_encoding(leaf, fi))
1406 goto out_check;
Ethan Lien78d42952018-05-17 14:58:29 +08001407 /*
1408 * Do the same check as in btrfs_cross_ref_exist but
1409 * without the unnecessary search.
1410 */
Lu Fengqi27a7ff52018-11-29 17:31:32 +08001411 if (!nolock &&
1412 btrfs_file_extent_generation(leaf, fi) <=
Ethan Lien78d42952018-05-17 14:58:29 +08001413 btrfs_root_last_snapshot(&root->root_item))
1414 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001415 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1416 goto out_check;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001417 if (btrfs_extent_readonly(fs_info, disk_bytenr))
Yan Zheng80ff3852008-10-30 14:20:02 -04001418 goto out_check;
Liu Bo58113752018-01-31 17:09:13 -07001419 ret = btrfs_cross_ref_exist(root, ino,
1420 found_key.offset -
1421 extent_offset, disk_bytenr);
1422 if (ret) {
1423 /*
1424 * ret could be -EIO if the above fails to read
1425 * metadata.
1426 */
1427 if (ret < 0) {
1428 if (cow_start != (u64)-1)
1429 cur_offset = cow_start;
1430 goto error;
1431 }
1432
1433 WARN_ON_ONCE(nolock);
Yan Zheng17d217f2008-12-12 10:03:38 -05001434 goto out_check;
Liu Bo58113752018-01-31 17:09:13 -07001435 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001436 disk_bytenr += extent_offset;
Yan Zheng17d217f2008-12-12 10:03:38 -05001437 disk_bytenr += cur_offset - found_key.offset;
1438 num_bytes = min(end + 1, extent_end) - cur_offset;
1439 /*
Wang Shilonge9894fd2014-03-27 11:12:25 +08001440 * if there are pending snapshots for this root,
1441 * we fall into common COW way.
1442 */
Robbie Ko8ecebf4d2018-08-06 10:30:30 +08001443 if (!nolock && atomic_read(&root->snapshot_force_cow))
1444 goto out_check;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001445 /*
Yan Zheng17d217f2008-12-12 10:03:38 -05001446 * force cow if csum exists in the range.
1447 * this ensure that csum for a given extent are
1448 * either valid or do not exist.
1449 */
Liu Bo58113752018-01-31 17:09:13 -07001450 ret = csum_exist_in_range(fs_info, disk_bytenr,
1451 num_bytes);
1452 if (ret) {
Liu Bo58113752018-01-31 17:09:13 -07001453 /*
1454 * ret could be -EIO if the above fails to read
1455 * metadata.
1456 */
1457 if (ret < 0) {
1458 if (cow_start != (u64)-1)
1459 cur_offset = cow_start;
1460 goto error;
1461 }
1462 WARN_ON_ONCE(nolock);
Yan Zheng17d217f2008-12-12 10:03:38 -05001463 goto out_check;
Robbie Ko91e1f562016-10-07 10:01:29 +08001464 }
Robbie Ko8ecebf4d2018-08-06 10:30:30 +08001465 if (!btrfs_inc_nocow_writers(fs_info, disk_bytenr))
Filipe Mananaf78c4362016-05-09 13:15:41 +01001466 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001467 nocow = 1;
1468 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1469 extent_end = found_key.offset +
Qu Wenruoe41ca582018-06-06 15:41:49 +08001470 btrfs_file_extent_ram_bytes(leaf, fi);
Jeff Mahoneyda170662016-06-15 09:22:56 -04001471 extent_end = ALIGN(extent_end,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001472 fs_info->sectorsize);
Yan Zheng80ff3852008-10-30 14:20:02 -04001473 } else {
Arnd Bergmann290342f2019-03-25 14:02:25 +01001474 BUG();
Yan Zheng80ff3852008-10-30 14:20:02 -04001475 }
1476out_check:
1477 if (extent_end <= start) {
1478 path->slots[0]++;
Filipe Mananaf78c4362016-05-09 13:15:41 +01001479 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001480 btrfs_dec_nocow_writers(fs_info, disk_bytenr);
Yan Zheng80ff3852008-10-30 14:20:02 -04001481 goto next_slot;
1482 }
1483 if (!nocow) {
1484 if (cow_start == (u64)-1)
1485 cow_start = cur_offset;
1486 cur_offset = extent_end;
1487 if (cur_offset > end)
1488 break;
1489 path->slots[0]++;
1490 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001491 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001492
David Sterbab3b4aa72011-04-21 01:20:15 +02001493 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001494 if (cow_start != (u64)-1) {
Josef Bacik00361582013-08-14 14:02:47 -04001495 ret = cow_file_range(inode, locked_page,
1496 cow_start, found_key.offset - 1,
Wang Xiaoguangdda32452016-07-11 11:05:29 +08001497 end, page_started, nr_written, 1,
1498 NULL);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001499 if (ret) {
Filipe Mananaf78c4362016-05-09 13:15:41 +01001500 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001501 btrfs_dec_nocow_writers(fs_info,
Filipe Mananaf78c4362016-05-09 13:15:41 +01001502 disk_bytenr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001503 goto error;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001504 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001505 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001506 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001507
Yan Zhengd899e052008-10-30 14:25:28 -04001508 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Liu Bo6f9994d2017-01-31 07:50:22 -08001509 u64 orig_start = found_key.offset - extent_offset;
1510
1511 em = create_io_em(inode, cur_offset, num_bytes,
1512 orig_start,
1513 disk_bytenr, /* block_start */
1514 num_bytes, /* block_len */
1515 disk_num_bytes, /* orig_block_len */
1516 ram_bytes, BTRFS_COMPRESS_NONE,
1517 BTRFS_ORDERED_PREALLOC);
1518 if (IS_ERR(em)) {
Liu Bo6f9994d2017-01-31 07:50:22 -08001519 if (nocow)
1520 btrfs_dec_nocow_writers(fs_info,
1521 disk_bytenr);
1522 ret = PTR_ERR(em);
1523 goto error;
Yan Zhengd899e052008-10-30 14:25:28 -04001524 }
Liu Bo6f9994d2017-01-31 07:50:22 -08001525 free_extent_map(em);
1526 }
1527
1528 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zhengd899e052008-10-30 14:25:28 -04001529 type = BTRFS_ORDERED_PREALLOC;
1530 } else {
1531 type = BTRFS_ORDERED_NOCOW;
1532 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001533
1534 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
Yan Zhengd899e052008-10-30 14:25:28 -04001535 num_bytes, num_bytes, type);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001536 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001537 btrfs_dec_nocow_writers(fs_info, disk_bytenr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001538 BUG_ON(ret); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -05001539
Yan, Zhengefa56462010-05-16 10:49:59 -04001540 if (root->root_key.objectid ==
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001541 BTRFS_DATA_RELOC_TREE_OBJECTID)
1542 /*
1543 * Error handled later, as we must prevent
1544 * extent_clear_unlock_delalloc() in error handler
1545 * from freeing metadata of created ordered extent.
1546 */
Yan, Zhengefa56462010-05-16 10:49:59 -04001547 ret = btrfs_reloc_clone_csums(inode, cur_offset,
1548 num_bytes);
Yan, Zhengefa56462010-05-16 10:49:59 -04001549
Josef Bacikc2790a22013-07-29 11:20:47 -04001550 extent_clear_unlock_delalloc(inode, cur_offset,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001551 cur_offset + num_bytes - 1, end,
Josef Bacikc2790a22013-07-29 11:20:47 -04001552 locked_page, EXTENT_LOCKED |
Wang Xiaoguang18513092016-07-25 15:51:40 +08001553 EXTENT_DELALLOC |
1554 EXTENT_CLEAR_DATA_RESV,
1555 PAGE_UNLOCK | PAGE_SET_PRIVATE2);
1556
Yan Zheng80ff3852008-10-30 14:20:02 -04001557 cur_offset = extent_end;
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001558
1559 /*
1560 * btrfs_reloc_clone_csums() error, now we're OK to call error
1561 * handler, as metadata for created ordered extent will only
1562 * be freed by btrfs_finish_ordered_io().
1563 */
1564 if (ret)
1565 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001566 if (cur_offset > end)
1567 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001568 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001569 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001570
Robbie Ko506481b2018-10-30 18:04:04 +08001571 if (cur_offset <= end && cow_start == (u64)-1)
Yan Zheng80ff3852008-10-30 14:20:02 -04001572 cow_start = cur_offset;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001573
Yan Zheng80ff3852008-10-30 14:20:02 -04001574 if (cow_start != (u64)-1) {
Robbie Ko506481b2018-10-30 18:04:04 +08001575 cur_offset = end;
Wang Xiaoguangdda32452016-07-11 11:05:29 +08001576 ret = cow_file_range(inode, locked_page, cow_start, end, end,
1577 page_started, nr_written, 1, NULL);
Josef Bacikd788a342013-10-25 16:55:08 -04001578 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001579 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001580 }
1581
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001582error:
Josef Bacik17ca04a2012-05-31 15:58:55 -04001583 if (ret && cur_offset < end)
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001584 extent_clear_unlock_delalloc(inode, cur_offset, end, end,
Josef Bacikc2790a22013-07-29 11:20:47 -04001585 locked_page, EXTENT_LOCKED |
Josef Bacik151a41b2013-07-29 13:22:24 -04001586 EXTENT_DELALLOC | EXTENT_DEFRAG |
1587 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
1588 PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -04001589 PAGE_SET_WRITEBACK |
1590 PAGE_END_WRITEBACK);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001591 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001592 return ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001593}
1594
Wang Shilong47059d92014-07-03 18:22:07 +08001595static inline int need_force_cow(struct inode *inode, u64 start, u64 end)
1596{
1597
1598 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
1599 !(BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC))
1600 return 0;
1601
1602 /*
1603 * @defrag_bytes is a hint value, no spinlock held here,
1604 * if is not zero, it means the file is defragging.
1605 * Force cow if given extent needs to be defragged.
1606 */
1607 if (BTRFS_I(inode)->defrag_bytes &&
1608 test_range_bit(&BTRFS_I(inode)->io_tree, start, end,
1609 EXTENT_DEFRAG, 0, NULL))
1610 return 1;
1611
1612 return 0;
1613}
1614
Chris Masond352ac62008-09-29 15:18:18 -04001615/*
Nikolay Borisov5eaad972018-11-01 14:09:46 +02001616 * Function to process delayed allocation (create CoW) for ranges which are
1617 * being touched for the first time.
Chris Masond352ac62008-09-29 15:18:18 -04001618 */
Nikolay Borisovbc9a8bf2018-12-19 09:50:20 +02001619int btrfs_run_delalloc_range(struct inode *inode, struct page *locked_page,
Nikolay Borisov5eaad972018-11-01 14:09:46 +02001620 u64 start, u64 end, int *page_started, unsigned long *nr_written,
1621 struct writeback_control *wbc)
Chris Masonbe20aa92007-12-17 20:14:01 -05001622{
Chris Masonbe20aa92007-12-17 20:14:01 -05001623 int ret;
Wang Shilong47059d92014-07-03 18:22:07 +08001624 int force_cow = need_force_cow(inode, start, end);
Liu Bof82b7352017-10-23 23:18:16 -06001625 unsigned int write_flags = wbc_to_write_flags(wbc);
Chris Masona2135012008-06-25 16:01:30 -04001626
Wang Shilong47059d92014-07-03 18:22:07 +08001627 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW && !force_cow) {
Chris Masonc8b97812008-10-29 14:49:59 -04001628 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001629 page_started, 1, nr_written);
Wang Shilong47059d92014-07-03 18:22:07 +08001630 } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC && !force_cow) {
Yan Zhengd899e052008-10-30 14:25:28 -04001631 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001632 page_started, 0, nr_written);
Timofey Titovetsc2fcdcd2017-07-17 16:52:58 +03001633 } else if (!inode_need_compress(inode, start, end)) {
Wang Xiaoguangdda32452016-07-11 11:05:29 +08001634 ret = cow_file_range(inode, locked_page, start, end, end,
1635 page_started, nr_written, 1, NULL);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001636 } else {
1637 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1638 &BTRFS_I(inode)->runtime_flags);
Chris Mason771ed682008-11-06 22:02:51 -05001639 ret = cow_file_range_async(inode, locked_page, start, end,
Liu Bof82b7352017-10-23 23:18:16 -06001640 page_started, nr_written,
1641 write_flags);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001642 }
Qu Wenruo524272602017-03-08 10:25:52 +08001643 if (ret)
Nikolay Borisovd1051d62018-11-21 17:10:52 +02001644 btrfs_cleanup_ordered_extents(inode, locked_page, start,
1645 end - start + 1);
Chris Masonb888db22007-08-27 16:49:44 -04001646 return ret;
1647}
1648
Nikolay Borisovabbb55f2018-11-01 14:09:53 +02001649void btrfs_split_delalloc_extent(struct inode *inode,
1650 struct extent_state *orig, u64 split)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001651{
Josef Bacikdcab6a32015-02-11 15:08:59 -05001652 u64 size;
1653
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001654 /* not delalloc, ignore it */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001655 if (!(orig->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001656 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001657
Josef Bacikdcab6a32015-02-11 15:08:59 -05001658 size = orig->end - orig->start + 1;
1659 if (size > BTRFS_MAX_EXTENT_SIZE) {
David Sterba823bb202017-01-04 11:09:51 +01001660 u32 num_extents;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001661 u64 new_size;
1662
1663 /*
Nikolay Borisov5c848192018-11-01 14:09:52 +02001664 * See the explanation in btrfs_merge_delalloc_extent, the same
Josef Bacikba117212015-03-13 15:01:24 -04001665 * applies here, just in reverse.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001666 */
1667 new_size = orig->end - split + 1;
David Sterba823bb202017-01-04 11:09:51 +01001668 num_extents = count_max_extents(new_size);
Josef Bacikba117212015-03-13 15:01:24 -04001669 new_size = split - orig->start;
David Sterba823bb202017-01-04 11:09:51 +01001670 num_extents += count_max_extents(new_size);
1671 if (count_max_extents(size) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001672 return;
1673 }
1674
Josef Bacik9e0baf62011-07-15 15:16:44 +00001675 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04001676 btrfs_mod_outstanding_extents(BTRFS_I(inode), 1);
Josef Bacik9e0baf62011-07-15 15:16:44 +00001677 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001678}
1679
1680/*
Nikolay Borisov5c848192018-11-01 14:09:52 +02001681 * Handle merged delayed allocation extents so we can keep track of new extents
1682 * that are just merged onto old extents, such as when we are doing sequential
1683 * writes, so we can properly account for the metadata space we'll need.
Josef Bacik9ed74f22009-09-11 16:12:44 -04001684 */
Nikolay Borisov5c848192018-11-01 14:09:52 +02001685void btrfs_merge_delalloc_extent(struct inode *inode, struct extent_state *new,
1686 struct extent_state *other)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001687{
Josef Bacikdcab6a32015-02-11 15:08:59 -05001688 u64 new_size, old_size;
David Sterba823bb202017-01-04 11:09:51 +01001689 u32 num_extents;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001690
Josef Bacik9ed74f22009-09-11 16:12:44 -04001691 /* not delalloc, ignore it */
1692 if (!(other->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001693 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001694
Josef Bacik8461a3d2015-03-13 15:12:08 -04001695 if (new->start > other->start)
1696 new_size = new->end - other->start + 1;
1697 else
1698 new_size = other->end - new->start + 1;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001699
1700 /* we're not bigger than the max, unreserve the space and go */
1701 if (new_size <= BTRFS_MAX_EXTENT_SIZE) {
1702 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04001703 btrfs_mod_outstanding_extents(BTRFS_I(inode), -1);
Josef Bacikdcab6a32015-02-11 15:08:59 -05001704 spin_unlock(&BTRFS_I(inode)->lock);
1705 return;
1706 }
1707
1708 /*
Josef Bacikba117212015-03-13 15:01:24 -04001709 * We have to add up either side to figure out how many extents were
1710 * accounted for before we merged into one big extent. If the number of
1711 * extents we accounted for is <= the amount we need for the new range
1712 * then we can return, otherwise drop. Think of it like this
1713 *
1714 * [ 4k][MAX_SIZE]
1715 *
1716 * So we've grown the extent by a MAX_SIZE extent, this would mean we
1717 * need 2 outstanding extents, on one side we have 1 and the other side
1718 * we have 1 so they are == and we can return. But in this case
1719 *
1720 * [MAX_SIZE+4k][MAX_SIZE+4k]
1721 *
1722 * Each range on their own accounts for 2 extents, but merged together
1723 * they are only 3 extents worth of accounting, so we need to drop in
1724 * this case.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001725 */
Josef Bacikba117212015-03-13 15:01:24 -04001726 old_size = other->end - other->start + 1;
David Sterba823bb202017-01-04 11:09:51 +01001727 num_extents = count_max_extents(old_size);
Josef Bacikba117212015-03-13 15:01:24 -04001728 old_size = new->end - new->start + 1;
David Sterba823bb202017-01-04 11:09:51 +01001729 num_extents += count_max_extents(old_size);
1730 if (count_max_extents(new_size) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001731 return;
1732
Josef Bacik9e0baf62011-07-15 15:16:44 +00001733 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04001734 btrfs_mod_outstanding_extents(BTRFS_I(inode), -1);
Josef Bacik9e0baf62011-07-15 15:16:44 +00001735 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001736}
1737
Miao Xieeb73c1b2013-05-15 07:48:22 +00001738static void btrfs_add_delalloc_inodes(struct btrfs_root *root,
1739 struct inode *inode)
1740{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001741 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1742
Miao Xieeb73c1b2013-05-15 07:48:22 +00001743 spin_lock(&root->delalloc_lock);
1744 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1745 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1746 &root->delalloc_inodes);
1747 set_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1748 &BTRFS_I(inode)->runtime_flags);
1749 root->nr_delalloc_inodes++;
1750 if (root->nr_delalloc_inodes == 1) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001751 spin_lock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001752 BUG_ON(!list_empty(&root->delalloc_root));
1753 list_add_tail(&root->delalloc_root,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001754 &fs_info->delalloc_roots);
1755 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001756 }
1757 }
1758 spin_unlock(&root->delalloc_lock);
1759}
1760
Nikolay Borisov2b877332018-04-27 12:21:51 +03001761
1762void __btrfs_del_delalloc_inode(struct btrfs_root *root,
1763 struct btrfs_inode *inode)
Miao Xieeb73c1b2013-05-15 07:48:22 +00001764{
David Sterba3ffbd682018-06-29 10:56:42 +02001765 struct btrfs_fs_info *fs_info = root->fs_info;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001766
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001767 if (!list_empty(&inode->delalloc_inodes)) {
1768 list_del_init(&inode->delalloc_inodes);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001769 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001770 &inode->runtime_flags);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001771 root->nr_delalloc_inodes--;
1772 if (!root->nr_delalloc_inodes) {
Nikolay Borisov7c8a0d32018-04-27 12:21:52 +03001773 ASSERT(list_empty(&root->delalloc_inodes));
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001774 spin_lock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001775 BUG_ON(list_empty(&root->delalloc_root));
1776 list_del_init(&root->delalloc_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001777 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001778 }
1779 }
Nikolay Borisov2b877332018-04-27 12:21:51 +03001780}
1781
1782static void btrfs_del_delalloc_inode(struct btrfs_root *root,
1783 struct btrfs_inode *inode)
1784{
1785 spin_lock(&root->delalloc_lock);
1786 __btrfs_del_delalloc_inode(root, inode);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001787 spin_unlock(&root->delalloc_lock);
1788}
1789
Chris Masond352ac62008-09-29 15:18:18 -04001790/*
Nikolay Borisove06a1fc2018-11-01 14:09:50 +02001791 * Properly track delayed allocation bytes in the inode and to maintain the
1792 * list of inodes that have pending delalloc work to be done.
Chris Masond352ac62008-09-29 15:18:18 -04001793 */
Nikolay Borisove06a1fc2018-11-01 14:09:50 +02001794void btrfs_set_delalloc_extent(struct inode *inode, struct extent_state *state,
1795 unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001796{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001797 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1798
Wang Shilong47059d92014-07-03 18:22:07 +08001799 if ((*bits & EXTENT_DEFRAG) && !(*bits & EXTENT_DELALLOC))
1800 WARN_ON(1);
Chris Mason75eff682008-12-15 15:54:40 -05001801 /*
1802 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001803 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001804 * bit, which is only set or cleared with irqs on
1805 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001806 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001807 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001808 u64 len = state->end + 1 - state->start;
Josef Bacik8b62f872017-10-19 14:15:55 -04001809 u32 num_extents = count_max_extents(len);
Nikolay Borisov70ddc552017-02-20 13:50:35 +02001810 bool do_list = !btrfs_is_free_space_inode(BTRFS_I(inode));
Josef Bacik9ed74f22009-09-11 16:12:44 -04001811
Josef Bacik8b62f872017-10-19 14:15:55 -04001812 spin_lock(&BTRFS_I(inode)->lock);
1813 btrfs_mod_outstanding_extents(BTRFS_I(inode), num_extents);
1814 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik287a0ab2010-03-19 18:07:23 +00001815
Josef Bacik6a3891c2015-03-16 17:38:52 -04001816 /* For sanity tests */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001817 if (btrfs_is_testing(fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04001818 return;
1819
Nikolay Borisov104b4e52017-06-20 21:01:20 +03001820 percpu_counter_add_batch(&fs_info->delalloc_bytes, len,
1821 fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001822 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001823 BTRFS_I(inode)->delalloc_bytes += len;
Wang Shilong47059d92014-07-03 18:22:07 +08001824 if (*bits & EXTENT_DEFRAG)
1825 BTRFS_I(inode)->defrag_bytes += len;
Miao Xiedf0af1a2013-01-29 10:11:59 +00001826 if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00001827 &BTRFS_I(inode)->runtime_flags))
1828 btrfs_add_delalloc_inodes(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001829 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001830 }
Filipe Mananaa7e3b972017-04-03 10:45:46 +01001831
1832 if (!(state->state & EXTENT_DELALLOC_NEW) &&
1833 (*bits & EXTENT_DELALLOC_NEW)) {
1834 spin_lock(&BTRFS_I(inode)->lock);
1835 BTRFS_I(inode)->new_delalloc_bytes += state->end + 1 -
1836 state->start;
1837 spin_unlock(&BTRFS_I(inode)->lock);
1838 }
Chris Mason291d6732008-01-29 15:55:23 -05001839}
1840
Chris Masond352ac62008-09-29 15:18:18 -04001841/*
Nikolay Borisova36bb5f2018-11-01 14:09:51 +02001842 * Once a range is no longer delalloc this function ensures that proper
1843 * accounting happens.
Chris Masond352ac62008-09-29 15:18:18 -04001844 */
Nikolay Borisova36bb5f2018-11-01 14:09:51 +02001845void btrfs_clear_delalloc_extent(struct inode *vfs_inode,
1846 struct extent_state *state, unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001847{
Nikolay Borisova36bb5f2018-11-01 14:09:51 +02001848 struct btrfs_inode *inode = BTRFS_I(vfs_inode);
1849 struct btrfs_fs_info *fs_info = btrfs_sb(vfs_inode->i_sb);
Wang Shilong47059d92014-07-03 18:22:07 +08001850 u64 len = state->end + 1 - state->start;
David Sterba823bb202017-01-04 11:09:51 +01001851 u32 num_extents = count_max_extents(len);
Wang Shilong47059d92014-07-03 18:22:07 +08001852
Filipe Manana4a4b9642017-07-27 19:52:55 +01001853 if ((state->state & EXTENT_DEFRAG) && (*bits & EXTENT_DEFRAG)) {
1854 spin_lock(&inode->lock);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001855 inode->defrag_bytes -= len;
Filipe Manana4a4b9642017-07-27 19:52:55 +01001856 spin_unlock(&inode->lock);
1857 }
Wang Shilong47059d92014-07-03 18:22:07 +08001858
Chris Mason75eff682008-12-15 15:54:40 -05001859 /*
1860 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001861 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001862 * bit, which is only set or cleared with irqs on
1863 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001864 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001865 struct btrfs_root *root = inode->root;
Liu Bo83eea1f2012-07-10 05:28:39 -06001866 bool do_list = !btrfs_is_free_space_inode(inode);
Chris Masonbcbfce82008-04-22 13:26:47 -04001867
Josef Bacik8b62f872017-10-19 14:15:55 -04001868 spin_lock(&inode->lock);
1869 btrfs_mod_outstanding_extents(inode, -num_extents);
1870 spin_unlock(&inode->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001871
Josef Bacikb6d08f02013-09-27 14:57:43 -04001872 /*
1873 * We don't reserve metadata space for space cache inodes so we
Andrea Gelmini52042d82018-11-28 12:05:13 +01001874 * don't need to call delalloc_release_metadata if there is an
Josef Bacikb6d08f02013-09-27 14:57:43 -04001875 * error.
1876 */
Filipe Mananaa315e682017-03-06 23:04:20 +00001877 if (*bits & EXTENT_CLEAR_META_RESV &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001878 root != fs_info->tree_root)
Qu Wenruo43b18592017-12-12 15:34:32 +08001879 btrfs_delalloc_release_metadata(inode, len, false);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001880
Josef Bacik6a3891c2015-03-16 17:38:52 -04001881 /* For sanity tests. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001882 if (btrfs_is_testing(fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04001883 return;
1884
Filipe Mananaa315e682017-03-06 23:04:20 +00001885 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID &&
1886 do_list && !(state->state & EXTENT_NORESERVE) &&
1887 (*bits & EXTENT_CLEAR_DATA_RESV))
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001888 btrfs_free_reserved_data_space_noquota(
1889 &inode->vfs_inode,
Qu Wenruo51773be2015-10-08 18:19:37 +08001890 state->start, len);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001891
Nikolay Borisov104b4e52017-06-20 21:01:20 +03001892 percpu_counter_add_batch(&fs_info->delalloc_bytes, -len,
1893 fs_info->delalloc_batch);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001894 spin_lock(&inode->lock);
1895 inode->delalloc_bytes -= len;
1896 if (do_list && inode->delalloc_bytes == 0 &&
Miao Xiedf0af1a2013-01-29 10:11:59 +00001897 test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001898 &inode->runtime_flags))
Miao Xieeb73c1b2013-05-15 07:48:22 +00001899 btrfs_del_delalloc_inode(root, inode);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001900 spin_unlock(&inode->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001901 }
Filipe Mananaa7e3b972017-04-03 10:45:46 +01001902
1903 if ((state->state & EXTENT_DELALLOC_NEW) &&
1904 (*bits & EXTENT_DELALLOC_NEW)) {
1905 spin_lock(&inode->lock);
1906 ASSERT(inode->new_delalloc_bytes >= len);
1907 inode->new_delalloc_bytes -= len;
1908 spin_unlock(&inode->lock);
1909 }
Chris Mason291d6732008-01-29 15:55:23 -05001910}
1911
Chris Masond352ac62008-09-29 15:18:18 -04001912/*
Nikolay Borisovda12fe52018-11-27 20:57:58 +02001913 * btrfs_bio_fits_in_stripe - Checks whether the size of the given bio will fit
1914 * in a chunk's stripe. This function ensures that bios do not span a
1915 * stripe/chunk
Liu Bo6f034ec2016-06-22 18:31:49 -07001916 *
Nikolay Borisovda12fe52018-11-27 20:57:58 +02001917 * @page - The page we are about to add to the bio
1918 * @size - size we want to add to the bio
1919 * @bio - bio we want to ensure is smaller than a stripe
1920 * @bio_flags - flags of the bio
1921 *
1922 * return 1 if page cannot be added to the bio
1923 * return 0 if page can be added to the bio
Liu Bo6f034ec2016-06-22 18:31:49 -07001924 * return error otherwise
Chris Masond352ac62008-09-29 15:18:18 -04001925 */
Nikolay Borisovda12fe52018-11-27 20:57:58 +02001926int btrfs_bio_fits_in_stripe(struct page *page, size_t size, struct bio *bio,
1927 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04001928{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001929 struct inode *inode = page->mapping->host;
1930 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001931 u64 logical = (u64)bio->bi_iter.bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04001932 u64 length = 0;
1933 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04001934 int ret;
1935
Chris Mason771ed682008-11-06 22:02:51 -05001936 if (bio_flags & EXTENT_BIO_COMPRESSED)
1937 return 0;
1938
Kent Overstreet4f024f32013-10-11 15:44:27 -07001939 length = bio->bi_iter.bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04001940 map_length = length;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001941 ret = btrfs_map_block(fs_info, btrfs_op(bio), logical, &map_length,
1942 NULL, 0);
Liu Bo6f034ec2016-06-22 18:31:49 -07001943 if (ret < 0)
1944 return ret;
Chris Masond3977122009-01-05 21:25:51 -05001945 if (map_length < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04001946 return 1;
Jeff Mahoney3444a972011-10-03 23:23:13 -04001947 return 0;
Chris Mason239b14b2008-03-24 15:02:07 -04001948}
1949
Chris Masond352ac62008-09-29 15:18:18 -04001950/*
1951 * in order to insert checksums into the metadata in large chunks,
1952 * we wait until bio submission time. All the pages in the bio are
1953 * checksummed and sums are attached onto the ordered extent record.
1954 *
1955 * At IO completion time the cums attached on the ordered extent record
1956 * are inserted into the btree
1957 */
David Sterbad0ee3932018-03-08 14:35:48 +01001958static blk_status_t btrfs_submit_bio_start(void *private_data, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001959 u64 bio_offset)
Chris Mason065631f2008-02-20 12:07:25 -05001960{
Josef Bacikc6100a42017-05-05 11:57:13 -04001961 struct inode *inode = private_data;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001962 blk_status_t ret = 0;
Chris Masone0156402008-04-16 11:15:20 -04001963
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001964 ret = btrfs_csum_one_bio(inode, bio, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001965 BUG_ON(ret); /* -ENOMEM */
Chris Mason4a69a412008-11-06 22:03:00 -05001966 return 0;
1967}
Chris Masone0156402008-04-16 11:15:20 -04001968
Chris Mason4a69a412008-11-06 22:03:00 -05001969/*
Chris Masoncad321a2008-12-17 14:51:42 -05001970 * extent_io.c submission hook. This does the right thing for csum calculation
Liu Bo4c274bc2017-11-01 17:19:27 -06001971 * on write, or reading the csums from the tree before a read.
1972 *
1973 * Rules about async/sync submit,
1974 * a) read: sync submit
1975 *
1976 * b) write without checksum: sync submit
1977 *
1978 * c) write with checksum:
1979 * c-1) if bio is issued by fsync: sync submit
1980 * (sync_writers != 0)
1981 *
1982 * c-2) if root is reloc root: sync submit
1983 * (only in case of buffered IO)
1984 *
1985 * c-3) otherwise: async submit
Chris Masond352ac62008-09-29 15:18:18 -04001986 */
Nikolay Borisova56b1c72019-04-10 17:24:39 +03001987static blk_status_t btrfs_submit_bio_hook(struct inode *inode, struct bio *bio,
Nikolay Borisov50489a52019-04-10 19:46:04 +03001988 int mirror_num,
1989 unsigned long bio_flags)
1990
Chris Mason44b8bd72008-04-16 11:14:51 -04001991{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001992 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason44b8bd72008-04-16 11:14:51 -04001993 struct btrfs_root *root = BTRFS_I(inode)->root;
Chandan Rajendra0d51e282015-07-27 15:26:43 +05301994 enum btrfs_wq_endio_type metadata = BTRFS_WQ_ENDIO_DATA;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001995 blk_status_t ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001996 int skip_sum;
Josef Bacikb812ce22012-11-16 13:56:32 -05001997 int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
Chris Mason44b8bd72008-04-16 11:14:51 -04001998
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001999 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masoncad321a2008-12-17 14:51:42 -05002000
Nikolay Borisov70ddc552017-02-20 13:50:35 +02002001 if (btrfs_is_free_space_inode(BTRFS_I(inode)))
Chandan Rajendra0d51e282015-07-27 15:26:43 +05302002 metadata = BTRFS_WQ_ENDIO_FREE_SPACE;
Jeff Mahoney04173412011-10-03 23:23:12 -04002003
Mike Christie37226b22016-06-05 14:31:52 -05002004 if (bio_op(bio) != REQ_OP_WRITE) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002005 ret = btrfs_bio_wq_end_io(fs_info, bio, metadata);
Josef Bacik5fd02042012-05-02 14:00:54 -04002006 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01002007 goto out;
Josef Bacik5fd02042012-05-02 14:00:54 -04002008
Chris Masond20f7042008-12-08 16:58:54 -05002009 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Stefan Behrens61891922012-11-05 18:51:52 +01002010 ret = btrfs_submit_compressed_read(inode, bio,
2011 mirror_num,
2012 bio_flags);
2013 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00002014 } else if (!skip_sum) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002015 ret = btrfs_lookup_bio_sums(inode, bio, NULL);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00002016 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01002017 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00002018 }
Chris Mason4d1b5fb2008-08-20 09:44:52 -04002019 goto mapit;
Josef Bacikb812ce22012-11-16 13:56:32 -05002020 } else if (async && !skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05002021 /* csum items have already been cloned */
2022 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
2023 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04002024 /* we're doing a write, do the async checksumming */
Josef Bacikc6100a42017-05-05 11:57:13 -04002025 ret = btrfs_wq_submit_bio(fs_info, bio, mirror_num, bio_flags,
Nikolay Borisove7681162019-04-10 17:24:41 +03002026 0, inode, btrfs_submit_bio_start);
Stefan Behrens61891922012-11-05 18:51:52 +01002027 goto out;
Josef Bacikb812ce22012-11-16 13:56:32 -05002028 } else if (!skip_sum) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002029 ret = btrfs_csum_one_bio(inode, bio, 0, 0);
Josef Bacikb812ce22012-11-16 13:56:32 -05002030 if (ret)
2031 goto out;
Chris Mason19b9bdb2008-10-30 14:23:13 -04002032 }
2033
Chris Mason0b86a832008-03-24 15:01:56 -04002034mapit:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002035 ret = btrfs_map_bio(fs_info, bio, mirror_num, 0);
Stefan Behrens61891922012-11-05 18:51:52 +01002036
2037out:
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002038 if (ret) {
2039 bio->bi_status = ret;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002040 bio_endio(bio);
2041 }
Stefan Behrens61891922012-11-05 18:51:52 +01002042 return ret;
Chris Mason065631f2008-02-20 12:07:25 -05002043}
Chris Mason6885f302008-02-20 16:11:05 -05002044
Chris Masond352ac62008-09-29 15:18:18 -04002045/*
2046 * given a list of ordered sums record them in the inode. This happens
2047 * at IO completion time based on sums calculated at bio submission time.
2048 */
Chris Masonba1da2f2008-07-17 12:54:15 -04002049static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
David Sterbadf9f6282017-02-10 19:35:37 +01002050 struct inode *inode, struct list_head *list)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002051{
Chris Masone6dcd2d2008-07-17 12:53:50 -04002052 struct btrfs_ordered_sum *sum;
Nikolay Borisovac01f262018-01-08 10:59:43 +02002053 int ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002054
Qinghuang Fengc6e30872009-01-21 10:59:08 -05002055 list_for_each_entry(sum, list, list) {
David Sterba7c2871a2017-11-08 01:07:43 +01002056 trans->adding_csums = true;
Nikolay Borisovac01f262018-01-08 10:59:43 +02002057 ret = btrfs_csum_file_blocks(trans,
Chris Masond20f7042008-12-08 16:58:54 -05002058 BTRFS_I(inode)->root->fs_info->csum_root, sum);
David Sterba7c2871a2017-11-08 01:07:43 +01002059 trans->adding_csums = false;
Nikolay Borisovac01f262018-01-08 10:59:43 +02002060 if (ret)
2061 return ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002062 }
2063 return 0;
2064}
2065
Josef Bacik2ac55d42010-02-03 19:33:23 +00002066int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
Filipe Mananae3b8a482017-11-04 00:16:59 +00002067 unsigned int extra_bits,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08002068 struct extent_state **cached_state, int dedupe)
Chris Masonea8c2812008-08-04 23:17:27 -04002069{
Johannes Thumshirnfdb1e122018-12-05 15:23:04 +01002070 WARN_ON(PAGE_ALIGNED(end));
Chris Masonea8c2812008-08-04 23:17:27 -04002071 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
Filipe Mananae3b8a482017-11-04 00:16:59 +00002072 extra_bits, cached_state);
Chris Masonea8c2812008-08-04 23:17:27 -04002073}
2074
Chris Masond352ac62008-09-29 15:18:18 -04002075/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04002076struct btrfs_writepage_fixup {
2077 struct page *page;
2078 struct btrfs_work work;
2079};
2080
Christoph Hellwigb2950862008-12-02 09:54:17 -05002081static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04002082{
2083 struct btrfs_writepage_fixup *fixup;
2084 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002085 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08002086 struct extent_changeset *data_reserved = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04002087 struct page *page;
2088 struct inode *inode;
2089 u64 page_start;
2090 u64 page_end;
Jeff Mahoney87826df2012-02-15 16:23:57 +01002091 int ret;
Chris Mason247e7432008-07-17 12:53:51 -04002092
2093 fixup = container_of(work, struct btrfs_writepage_fixup, work);
2094 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -04002095again:
Chris Mason247e7432008-07-17 12:53:51 -04002096 lock_page(page);
2097 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
2098 ClearPageChecked(page);
2099 goto out_page;
2100 }
2101
2102 inode = page->mapping->host;
2103 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002104 page_end = page_offset(page) + PAGE_SIZE - 1;
Chris Mason247e7432008-07-17 12:53:51 -04002105
David Sterbaff13db42015-12-03 14:30:40 +01002106 lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002107 &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04002108
2109 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04002110 if (PagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04002111 goto out;
Chris Mason4a096752008-07-21 10:29:44 -04002112
Nikolay Borisova776c6f2017-02-20 13:50:49 +02002113 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002114 PAGE_SIZE);
Chris Mason4a096752008-07-21 10:29:44 -04002115 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00002116 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
David Sterbae43bbe52017-12-12 21:43:52 +01002117 page_end, &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04002118 unlock_page(page);
2119 btrfs_start_ordered_extent(inode, ordered, 1);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002120 btrfs_put_ordered_extent(ordered);
Chris Mason4a096752008-07-21 10:29:44 -04002121 goto again;
2122 }
Chris Mason247e7432008-07-17 12:53:51 -04002123
Qu Wenruo364ecf32017-02-27 15:10:38 +08002124 ret = btrfs_delalloc_reserve_space(inode, &data_reserved, page_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002125 PAGE_SIZE);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002126 if (ret) {
2127 mapping_set_error(page->mapping, ret);
2128 end_extent_writepage(page, ret, page_start, page_end);
2129 ClearPageChecked(page);
2130 goto out;
2131 }
2132
Nikolay Borisovf3038ee2017-12-05 09:29:19 +02002133 ret = btrfs_set_extent_delalloc(inode, page_start, page_end, 0,
2134 &cached_state, 0);
2135 if (ret) {
2136 mapping_set_error(page->mapping, ret);
2137 end_extent_writepage(page, ret, page_start, page_end);
2138 ClearPageChecked(page);
2139 goto out;
2140 }
2141
Chris Mason247e7432008-07-17 12:53:51 -04002142 ClearPageChecked(page);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002143 set_page_dirty(page);
Qu Wenruo43b18592017-12-12 15:34:32 +08002144 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE, false);
Chris Mason247e7432008-07-17 12:53:51 -04002145out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00002146 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01002147 &cached_state);
Chris Mason247e7432008-07-17 12:53:51 -04002148out_page:
2149 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002150 put_page(page);
Miao Xieb897abe2011-01-26 16:19:22 +08002151 kfree(fixup);
Qu Wenruo364ecf32017-02-27 15:10:38 +08002152 extent_changeset_free(data_reserved);
Chris Mason247e7432008-07-17 12:53:51 -04002153}
2154
2155/*
2156 * There are a few paths in the higher layers of the kernel that directly
2157 * set the page dirty bit without asking the filesystem if it is a
2158 * good idea. This causes problems because we want to make sure COW
2159 * properly happens and the data=ordered rules are followed.
2160 *
Chris Masonc8b97812008-10-29 14:49:59 -04002161 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04002162 * hasn't been properly setup for IO. We kick off an async process
2163 * to fix it up. The async helper will wait for ordered extents, set
2164 * the delalloc bit and make it safe to write the page.
2165 */
Nikolay Borisovd75855b2018-11-01 14:09:47 +02002166int btrfs_writepage_cow_fixup(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04002167{
2168 struct inode *inode = page->mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002169 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason247e7432008-07-17 12:53:51 -04002170 struct btrfs_writepage_fixup *fixup;
Chris Mason247e7432008-07-17 12:53:51 -04002171
Chris Mason8b62b722009-09-02 16:53:46 -04002172 /* this page is properly in the ordered list */
2173 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04002174 return 0;
2175
2176 if (PageChecked(page))
2177 return -EAGAIN;
2178
2179 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
2180 if (!fixup)
2181 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04002182
Chris Mason247e7432008-07-17 12:53:51 -04002183 SetPageChecked(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002184 get_page(page);
Liu Bo9e0af232014-08-15 23:36:53 +08002185 btrfs_init_work(&fixup->work, btrfs_fixup_helper,
2186 btrfs_writepage_fixup_worker, NULL, NULL);
Chris Mason247e7432008-07-17 12:53:51 -04002187 fixup->page = page;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002188 btrfs_queue_work(fs_info->fixup_workers, &fixup->work);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002189 return -EBUSY;
Chris Mason247e7432008-07-17 12:53:51 -04002190}
2191
Yan Zhengd899e052008-10-30 14:25:28 -04002192static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
2193 struct inode *inode, u64 file_pos,
2194 u64 disk_bytenr, u64 disk_num_bytes,
2195 u64 num_bytes, u64 ram_bytes,
2196 u8 compression, u8 encryption,
2197 u16 other_encoding, int extent_type)
2198{
2199 struct btrfs_root *root = BTRFS_I(inode)->root;
2200 struct btrfs_file_extent_item *fi;
2201 struct btrfs_path *path;
2202 struct extent_buffer *leaf;
2203 struct btrfs_key ins;
Qu Wenruoa12b8772017-02-27 15:10:37 +08002204 u64 qg_released;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002205 int extent_inserted = 0;
Yan Zhengd899e052008-10-30 14:25:28 -04002206 int ret;
2207
2208 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07002209 if (!path)
2210 return -ENOMEM;
Yan Zhengd899e052008-10-30 14:25:28 -04002211
Chris Masona1ed8352009-09-11 12:27:37 -04002212 /*
2213 * we may be replacing one extent in the tree with another.
2214 * The new extent is pinned in the extent map, and we don't want
2215 * to drop it from the cache until it is completely in the btree.
2216 *
2217 * So, tell btrfs_drop_extents to leave this extent in the cache.
2218 * the caller is expected to unpin it and allow it to be merged
2219 * with the others.
2220 */
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002221 ret = __btrfs_drop_extents(trans, root, inode, path, file_pos,
2222 file_pos + num_bytes, NULL, 0,
2223 1, sizeof(*fi), &extent_inserted);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002224 if (ret)
2225 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04002226
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002227 if (!extent_inserted) {
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002228 ins.objectid = btrfs_ino(BTRFS_I(inode));
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002229 ins.offset = file_pos;
2230 ins.type = BTRFS_EXTENT_DATA_KEY;
2231
2232 path->leave_spinning = 1;
2233 ret = btrfs_insert_empty_item(trans, root, path, &ins,
2234 sizeof(*fi));
2235 if (ret)
2236 goto out;
2237 }
Yan Zhengd899e052008-10-30 14:25:28 -04002238 leaf = path->nodes[0];
2239 fi = btrfs_item_ptr(leaf, path->slots[0],
2240 struct btrfs_file_extent_item);
2241 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
2242 btrfs_set_file_extent_type(leaf, fi, extent_type);
2243 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
2244 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
2245 btrfs_set_file_extent_offset(leaf, fi, 0);
2246 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
2247 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
2248 btrfs_set_file_extent_compression(leaf, fi, compression);
2249 btrfs_set_file_extent_encryption(leaf, fi, encryption);
2250 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
Chris Masonb9473432009-03-13 11:00:37 -04002251
Yan Zhengd899e052008-10-30 14:25:28 -04002252 btrfs_mark_buffer_dirty(leaf);
Josef Bacikce195332012-09-25 15:26:16 -04002253 btrfs_release_path(path);
Yan Zhengd899e052008-10-30 14:25:28 -04002254
2255 inode_add_bytes(inode, num_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -04002256
2257 ins.objectid = disk_bytenr;
2258 ins.offset = disk_num_bytes;
2259 ins.type = BTRFS_EXTENT_ITEM_KEY;
Qu Wenruoa12b8772017-02-27 15:10:37 +08002260
Qu Wenruo297d7502015-09-08 17:08:37 +08002261 /*
Qu Wenruo5846a3c2015-10-26 14:11:18 +08002262 * Release the reserved range from inode dirty range map, as it is
2263 * already moved into delayed_ref_head
Qu Wenruo297d7502015-09-08 17:08:37 +08002264 */
Qu Wenruoa12b8772017-02-27 15:10:37 +08002265 ret = btrfs_qgroup_release_data(inode, file_pos, ram_bytes);
2266 if (ret < 0)
2267 goto out;
2268 qg_released = ret;
Josef Bacik84f7d8e2017-09-29 15:43:49 -04002269 ret = btrfs_alloc_reserved_file_extent(trans, root,
2270 btrfs_ino(BTRFS_I(inode)),
2271 file_pos, qg_released, &ins);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002272out:
Yan Zhengd899e052008-10-30 14:25:28 -04002273 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04002274
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002275 return ret;
Yan Zhengd899e052008-10-30 14:25:28 -04002276}
2277
Liu Bo38c227d2013-01-29 03:18:40 +00002278/* snapshot-aware defrag */
2279struct sa_defrag_extent_backref {
2280 struct rb_node node;
2281 struct old_sa_defrag_extent *old;
2282 u64 root_id;
2283 u64 inum;
2284 u64 file_pos;
2285 u64 extent_offset;
2286 u64 num_bytes;
2287 u64 generation;
2288};
2289
2290struct old_sa_defrag_extent {
2291 struct list_head list;
2292 struct new_sa_defrag_extent *new;
2293
2294 u64 extent_offset;
2295 u64 bytenr;
2296 u64 offset;
2297 u64 len;
2298 int count;
2299};
2300
2301struct new_sa_defrag_extent {
2302 struct rb_root root;
2303 struct list_head head;
2304 struct btrfs_path *path;
2305 struct inode *inode;
2306 u64 file_pos;
2307 u64 len;
2308 u64 bytenr;
2309 u64 disk_len;
2310 u8 compress_type;
2311};
2312
2313static int backref_comp(struct sa_defrag_extent_backref *b1,
2314 struct sa_defrag_extent_backref *b2)
2315{
2316 if (b1->root_id < b2->root_id)
2317 return -1;
2318 else if (b1->root_id > b2->root_id)
2319 return 1;
2320
2321 if (b1->inum < b2->inum)
2322 return -1;
2323 else if (b1->inum > b2->inum)
2324 return 1;
2325
2326 if (b1->file_pos < b2->file_pos)
2327 return -1;
2328 else if (b1->file_pos > b2->file_pos)
2329 return 1;
2330
2331 /*
2332 * [------------------------------] ===> (a range of space)
2333 * |<--->| |<---->| =============> (fs/file tree A)
2334 * |<---------------------------->| ===> (fs/file tree B)
2335 *
2336 * A range of space can refer to two file extents in one tree while
2337 * refer to only one file extent in another tree.
2338 *
2339 * So we may process a disk offset more than one time(two extents in A)
2340 * and locate at the same extent(one extent in B), then insert two same
2341 * backrefs(both refer to the extent in B).
2342 */
2343 return 0;
2344}
2345
2346static void backref_insert(struct rb_root *root,
2347 struct sa_defrag_extent_backref *backref)
2348{
2349 struct rb_node **p = &root->rb_node;
2350 struct rb_node *parent = NULL;
2351 struct sa_defrag_extent_backref *entry;
2352 int ret;
2353
2354 while (*p) {
2355 parent = *p;
2356 entry = rb_entry(parent, struct sa_defrag_extent_backref, node);
2357
2358 ret = backref_comp(backref, entry);
2359 if (ret < 0)
2360 p = &(*p)->rb_left;
2361 else
2362 p = &(*p)->rb_right;
2363 }
2364
2365 rb_link_node(&backref->node, parent, p);
2366 rb_insert_color(&backref->node, root);
2367}
2368
2369/*
2370 * Note the backref might has changed, and in this case we just return 0.
2371 */
2372static noinline int record_one_backref(u64 inum, u64 offset, u64 root_id,
2373 void *ctx)
2374{
2375 struct btrfs_file_extent_item *extent;
Liu Bo38c227d2013-01-29 03:18:40 +00002376 struct old_sa_defrag_extent *old = ctx;
2377 struct new_sa_defrag_extent *new = old->new;
2378 struct btrfs_path *path = new->path;
2379 struct btrfs_key key;
2380 struct btrfs_root *root;
2381 struct sa_defrag_extent_backref *backref;
2382 struct extent_buffer *leaf;
2383 struct inode *inode = new->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002384 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002385 int slot;
2386 int ret;
2387 u64 extent_offset;
2388 u64 num_bytes;
2389
2390 if (BTRFS_I(inode)->root->root_key.objectid == root_id &&
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002391 inum == btrfs_ino(BTRFS_I(inode)))
Liu Bo38c227d2013-01-29 03:18:40 +00002392 return 0;
2393
2394 key.objectid = root_id;
2395 key.type = BTRFS_ROOT_ITEM_KEY;
2396 key.offset = (u64)-1;
2397
Liu Bo38c227d2013-01-29 03:18:40 +00002398 root = btrfs_read_fs_root_no_name(fs_info, &key);
2399 if (IS_ERR(root)) {
2400 if (PTR_ERR(root) == -ENOENT)
2401 return 0;
2402 WARN_ON(1);
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002403 btrfs_debug(fs_info, "inum=%llu, offset=%llu, root_id=%llu",
Liu Bo38c227d2013-01-29 03:18:40 +00002404 inum, offset, root_id);
2405 return PTR_ERR(root);
2406 }
2407
2408 key.objectid = inum;
2409 key.type = BTRFS_EXTENT_DATA_KEY;
2410 if (offset > (u64)-1 << 32)
2411 key.offset = 0;
2412 else
2413 key.offset = offset;
2414
2415 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05302416 if (WARN_ON(ret < 0))
Liu Bo38c227d2013-01-29 03:18:40 +00002417 return ret;
Josef Bacik50f13192013-07-22 12:50:37 -04002418 ret = 0;
Liu Bo38c227d2013-01-29 03:18:40 +00002419
2420 while (1) {
2421 cond_resched();
2422
2423 leaf = path->nodes[0];
2424 slot = path->slots[0];
2425
2426 if (slot >= btrfs_header_nritems(leaf)) {
2427 ret = btrfs_next_leaf(root, path);
2428 if (ret < 0) {
2429 goto out;
2430 } else if (ret > 0) {
2431 ret = 0;
2432 goto out;
2433 }
2434 continue;
2435 }
2436
2437 path->slots[0]++;
2438
2439 btrfs_item_key_to_cpu(leaf, &key, slot);
2440
2441 if (key.objectid > inum)
2442 goto out;
2443
2444 if (key.objectid < inum || key.type != BTRFS_EXTENT_DATA_KEY)
2445 continue;
2446
2447 extent = btrfs_item_ptr(leaf, slot,
2448 struct btrfs_file_extent_item);
2449
2450 if (btrfs_file_extent_disk_bytenr(leaf, extent) != old->bytenr)
2451 continue;
2452
Liu Boe68afa42013-07-01 22:13:26 +08002453 /*
2454 * 'offset' refers to the exact key.offset,
2455 * NOT the 'offset' field in btrfs_extent_data_ref, ie.
2456 * (key.offset - extent_offset).
2457 */
2458 if (key.offset != offset)
Liu Bo38c227d2013-01-29 03:18:40 +00002459 continue;
2460
Liu Boe68afa42013-07-01 22:13:26 +08002461 extent_offset = btrfs_file_extent_offset(leaf, extent);
Liu Bo38c227d2013-01-29 03:18:40 +00002462 num_bytes = btrfs_file_extent_num_bytes(leaf, extent);
Liu Boe68afa42013-07-01 22:13:26 +08002463
Liu Bo38c227d2013-01-29 03:18:40 +00002464 if (extent_offset >= old->extent_offset + old->offset +
2465 old->len || extent_offset + num_bytes <=
2466 old->extent_offset + old->offset)
2467 continue;
Liu Bo38c227d2013-01-29 03:18:40 +00002468 break;
2469 }
2470
2471 backref = kmalloc(sizeof(*backref), GFP_NOFS);
2472 if (!backref) {
2473 ret = -ENOENT;
2474 goto out;
2475 }
2476
2477 backref->root_id = root_id;
2478 backref->inum = inum;
Liu Boe68afa42013-07-01 22:13:26 +08002479 backref->file_pos = offset;
Liu Bo38c227d2013-01-29 03:18:40 +00002480 backref->num_bytes = num_bytes;
2481 backref->extent_offset = extent_offset;
2482 backref->generation = btrfs_file_extent_generation(leaf, extent);
2483 backref->old = old;
2484 backref_insert(&new->root, backref);
2485 old->count++;
2486out:
2487 btrfs_release_path(path);
2488 WARN_ON(ret);
2489 return ret;
2490}
2491
2492static noinline bool record_extent_backrefs(struct btrfs_path *path,
2493 struct new_sa_defrag_extent *new)
2494{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002495 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002496 struct old_sa_defrag_extent *old, *tmp;
2497 int ret;
2498
2499 new->path = path;
2500
2501 list_for_each_entry_safe(old, tmp, &new->head, list) {
Liu Boe68afa42013-07-01 22:13:26 +08002502 ret = iterate_inodes_from_logical(old->bytenr +
2503 old->extent_offset, fs_info,
Liu Bo38c227d2013-01-29 03:18:40 +00002504 path, record_one_backref,
Zygo Blaxellc995ab32017-09-22 13:58:45 -04002505 old, false);
Josef Bacik4724b102013-11-05 11:11:40 -05002506 if (ret < 0 && ret != -ENOENT)
2507 return false;
Liu Bo38c227d2013-01-29 03:18:40 +00002508
2509 /* no backref to be processed for this extent */
2510 if (!old->count) {
2511 list_del(&old->list);
2512 kfree(old);
2513 }
2514 }
2515
2516 if (list_empty(&new->head))
2517 return false;
2518
2519 return true;
2520}
2521
2522static int relink_is_mergable(struct extent_buffer *leaf,
2523 struct btrfs_file_extent_item *fi,
Liu Bo116e0022013-08-02 16:30:40 +08002524 struct new_sa_defrag_extent *new)
Liu Bo38c227d2013-01-29 03:18:40 +00002525{
Liu Bo116e0022013-08-02 16:30:40 +08002526 if (btrfs_file_extent_disk_bytenr(leaf, fi) != new->bytenr)
Liu Bo38c227d2013-01-29 03:18:40 +00002527 return 0;
2528
2529 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG)
2530 return 0;
2531
Liu Bo116e0022013-08-02 16:30:40 +08002532 if (btrfs_file_extent_compression(leaf, fi) != new->compress_type)
2533 return 0;
2534
2535 if (btrfs_file_extent_encryption(leaf, fi) ||
Liu Bo38c227d2013-01-29 03:18:40 +00002536 btrfs_file_extent_other_encoding(leaf, fi))
2537 return 0;
2538
2539 return 1;
2540}
2541
2542/*
2543 * Note the backref might has changed, and in this case we just return 0.
2544 */
2545static noinline int relink_extent_backref(struct btrfs_path *path,
2546 struct sa_defrag_extent_backref *prev,
2547 struct sa_defrag_extent_backref *backref)
2548{
2549 struct btrfs_file_extent_item *extent;
2550 struct btrfs_file_extent_item *item;
2551 struct btrfs_ordered_extent *ordered;
2552 struct btrfs_trans_handle *trans;
Qu Wenruo82fa1132019-04-04 14:45:35 +08002553 struct btrfs_ref ref = { 0 };
Liu Bo38c227d2013-01-29 03:18:40 +00002554 struct btrfs_root *root;
2555 struct btrfs_key key;
2556 struct extent_buffer *leaf;
2557 struct old_sa_defrag_extent *old = backref->old;
2558 struct new_sa_defrag_extent *new = old->new;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002559 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002560 struct inode *inode;
2561 struct extent_state *cached = NULL;
2562 int ret = 0;
2563 u64 start;
2564 u64 len;
2565 u64 lock_start;
2566 u64 lock_end;
2567 bool merge = false;
2568 int index;
2569
2570 if (prev && prev->root_id == backref->root_id &&
2571 prev->inum == backref->inum &&
2572 prev->file_pos + prev->num_bytes == backref->file_pos)
2573 merge = true;
2574
2575 /* step 1: get root */
2576 key.objectid = backref->root_id;
2577 key.type = BTRFS_ROOT_ITEM_KEY;
2578 key.offset = (u64)-1;
2579
Liu Bo38c227d2013-01-29 03:18:40 +00002580 index = srcu_read_lock(&fs_info->subvol_srcu);
2581
2582 root = btrfs_read_fs_root_no_name(fs_info, &key);
2583 if (IS_ERR(root)) {
2584 srcu_read_unlock(&fs_info->subvol_srcu, index);
2585 if (PTR_ERR(root) == -ENOENT)
2586 return 0;
2587 return PTR_ERR(root);
2588 }
Liu Bo38c227d2013-01-29 03:18:40 +00002589
Wang Shilongbcbba5e2014-02-08 23:46:35 +08002590 if (btrfs_root_readonly(root)) {
2591 srcu_read_unlock(&fs_info->subvol_srcu, index);
2592 return 0;
2593 }
2594
Liu Bo38c227d2013-01-29 03:18:40 +00002595 /* step 2: get inode */
2596 key.objectid = backref->inum;
2597 key.type = BTRFS_INODE_ITEM_KEY;
2598 key.offset = 0;
2599
2600 inode = btrfs_iget(fs_info->sb, &key, root, NULL);
2601 if (IS_ERR(inode)) {
2602 srcu_read_unlock(&fs_info->subvol_srcu, index);
2603 return 0;
2604 }
2605
2606 srcu_read_unlock(&fs_info->subvol_srcu, index);
2607
2608 /* step 3: relink backref */
2609 lock_start = backref->file_pos;
2610 lock_end = backref->file_pos + backref->num_bytes - 1;
2611 lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
David Sterbaff13db42015-12-03 14:30:40 +01002612 &cached);
Liu Bo38c227d2013-01-29 03:18:40 +00002613
2614 ordered = btrfs_lookup_first_ordered_extent(inode, lock_end);
2615 if (ordered) {
2616 btrfs_put_ordered_extent(ordered);
2617 goto out_unlock;
2618 }
2619
2620 trans = btrfs_join_transaction(root);
2621 if (IS_ERR(trans)) {
2622 ret = PTR_ERR(trans);
2623 goto out_unlock;
2624 }
2625
2626 key.objectid = backref->inum;
2627 key.type = BTRFS_EXTENT_DATA_KEY;
2628 key.offset = backref->file_pos;
2629
2630 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2631 if (ret < 0) {
2632 goto out_free_path;
2633 } else if (ret > 0) {
2634 ret = 0;
2635 goto out_free_path;
2636 }
2637
2638 extent = btrfs_item_ptr(path->nodes[0], path->slots[0],
2639 struct btrfs_file_extent_item);
2640
2641 if (btrfs_file_extent_generation(path->nodes[0], extent) !=
2642 backref->generation)
2643 goto out_free_path;
2644
2645 btrfs_release_path(path);
2646
2647 start = backref->file_pos;
2648 if (backref->extent_offset < old->extent_offset + old->offset)
2649 start += old->extent_offset + old->offset -
2650 backref->extent_offset;
2651
2652 len = min(backref->extent_offset + backref->num_bytes,
2653 old->extent_offset + old->offset + old->len);
2654 len -= max(backref->extent_offset, old->extent_offset + old->offset);
2655
2656 ret = btrfs_drop_extents(trans, root, inode, start,
2657 start + len, 1);
2658 if (ret)
2659 goto out_free_path;
2660again:
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002661 key.objectid = btrfs_ino(BTRFS_I(inode));
Liu Bo38c227d2013-01-29 03:18:40 +00002662 key.type = BTRFS_EXTENT_DATA_KEY;
2663 key.offset = start;
2664
Liu Boa09a0a72013-03-11 09:20:58 +00002665 path->leave_spinning = 1;
Liu Bo38c227d2013-01-29 03:18:40 +00002666 if (merge) {
2667 struct btrfs_file_extent_item *fi;
2668 u64 extent_len;
2669 struct btrfs_key found_key;
2670
Gui Hecheng3c9665d2014-01-23 13:41:09 +08002671 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
Liu Bo38c227d2013-01-29 03:18:40 +00002672 if (ret < 0)
2673 goto out_free_path;
2674
2675 path->slots[0]--;
2676 leaf = path->nodes[0];
2677 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2678
2679 fi = btrfs_item_ptr(leaf, path->slots[0],
2680 struct btrfs_file_extent_item);
2681 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
2682
Liu Bo116e0022013-08-02 16:30:40 +08002683 if (extent_len + found_key.offset == start &&
2684 relink_is_mergable(leaf, fi, new)) {
Liu Bo38c227d2013-01-29 03:18:40 +00002685 btrfs_set_file_extent_num_bytes(leaf, fi,
2686 extent_len + len);
2687 btrfs_mark_buffer_dirty(leaf);
2688 inode_add_bytes(inode, len);
2689
2690 ret = 1;
2691 goto out_free_path;
2692 } else {
2693 merge = false;
2694 btrfs_release_path(path);
2695 goto again;
2696 }
2697 }
2698
2699 ret = btrfs_insert_empty_item(trans, root, path, &key,
2700 sizeof(*extent));
2701 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002702 btrfs_abort_transaction(trans, ret);
Liu Bo38c227d2013-01-29 03:18:40 +00002703 goto out_free_path;
2704 }
2705
2706 leaf = path->nodes[0];
2707 item = btrfs_item_ptr(leaf, path->slots[0],
2708 struct btrfs_file_extent_item);
2709 btrfs_set_file_extent_disk_bytenr(leaf, item, new->bytenr);
2710 btrfs_set_file_extent_disk_num_bytes(leaf, item, new->disk_len);
2711 btrfs_set_file_extent_offset(leaf, item, start - new->file_pos);
2712 btrfs_set_file_extent_num_bytes(leaf, item, len);
2713 btrfs_set_file_extent_ram_bytes(leaf, item, new->len);
2714 btrfs_set_file_extent_generation(leaf, item, trans->transid);
2715 btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG);
2716 btrfs_set_file_extent_compression(leaf, item, new->compress_type);
2717 btrfs_set_file_extent_encryption(leaf, item, 0);
2718 btrfs_set_file_extent_other_encoding(leaf, item, 0);
2719
2720 btrfs_mark_buffer_dirty(leaf);
2721 inode_add_bytes(inode, len);
Liu Boa09a0a72013-03-11 09:20:58 +00002722 btrfs_release_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002723
Qu Wenruo82fa1132019-04-04 14:45:35 +08002724 btrfs_init_generic_ref(&ref, BTRFS_ADD_DELAYED_REF, new->bytenr,
2725 new->disk_len, 0);
2726 btrfs_init_data_ref(&ref, backref->root_id, backref->inum,
2727 new->file_pos); /* start - extent_offset */
2728 ret = btrfs_inc_extent_ref(trans, &ref);
Liu Bo38c227d2013-01-29 03:18:40 +00002729 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002730 btrfs_abort_transaction(trans, ret);
Liu Bo38c227d2013-01-29 03:18:40 +00002731 goto out_free_path;
2732 }
2733
2734 ret = 1;
2735out_free_path:
2736 btrfs_release_path(path);
Liu Boa09a0a72013-03-11 09:20:58 +00002737 path->leave_spinning = 0;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002738 btrfs_end_transaction(trans);
Liu Bo38c227d2013-01-29 03:18:40 +00002739out_unlock:
2740 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
David Sterbae43bbe52017-12-12 21:43:52 +01002741 &cached);
Liu Bo38c227d2013-01-29 03:18:40 +00002742 iput(inode);
2743 return ret;
2744}
2745
Liu Bo6f519562013-10-29 10:45:05 +08002746static void free_sa_defrag_extent(struct new_sa_defrag_extent *new)
2747{
2748 struct old_sa_defrag_extent *old, *tmp;
2749
2750 if (!new)
2751 return;
2752
2753 list_for_each_entry_safe(old, tmp, &new->head, list) {
Liu Bo6f519562013-10-29 10:45:05 +08002754 kfree(old);
2755 }
2756 kfree(new);
2757}
2758
Liu Bo38c227d2013-01-29 03:18:40 +00002759static void relink_file_extents(struct new_sa_defrag_extent *new)
2760{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002761 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002762 struct btrfs_path *path;
Liu Bo38c227d2013-01-29 03:18:40 +00002763 struct sa_defrag_extent_backref *backref;
2764 struct sa_defrag_extent_backref *prev = NULL;
Liu Bo38c227d2013-01-29 03:18:40 +00002765 struct rb_node *node;
2766 int ret;
2767
Liu Bo38c227d2013-01-29 03:18:40 +00002768 path = btrfs_alloc_path();
2769 if (!path)
2770 return;
2771
2772 if (!record_extent_backrefs(path, new)) {
2773 btrfs_free_path(path);
2774 goto out;
2775 }
2776 btrfs_release_path(path);
2777
2778 while (1) {
2779 node = rb_first(&new->root);
2780 if (!node)
2781 break;
2782 rb_erase(node, &new->root);
2783
2784 backref = rb_entry(node, struct sa_defrag_extent_backref, node);
2785
2786 ret = relink_extent_backref(path, prev, backref);
2787 WARN_ON(ret < 0);
2788
2789 kfree(prev);
2790
2791 if (ret == 1)
2792 prev = backref;
2793 else
2794 prev = NULL;
2795 cond_resched();
2796 }
2797 kfree(prev);
2798
2799 btrfs_free_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002800out:
Liu Bo6f519562013-10-29 10:45:05 +08002801 free_sa_defrag_extent(new);
2802
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002803 atomic_dec(&fs_info->defrag_running);
2804 wake_up(&fs_info->transaction_wait);
Liu Bo38c227d2013-01-29 03:18:40 +00002805}
2806
2807static struct new_sa_defrag_extent *
2808record_old_file_extents(struct inode *inode,
2809 struct btrfs_ordered_extent *ordered)
2810{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002811 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002812 struct btrfs_root *root = BTRFS_I(inode)->root;
2813 struct btrfs_path *path;
2814 struct btrfs_key key;
Liu Bo6f519562013-10-29 10:45:05 +08002815 struct old_sa_defrag_extent *old;
Liu Bo38c227d2013-01-29 03:18:40 +00002816 struct new_sa_defrag_extent *new;
2817 int ret;
2818
2819 new = kmalloc(sizeof(*new), GFP_NOFS);
2820 if (!new)
2821 return NULL;
2822
2823 new->inode = inode;
2824 new->file_pos = ordered->file_offset;
2825 new->len = ordered->len;
2826 new->bytenr = ordered->start;
2827 new->disk_len = ordered->disk_len;
2828 new->compress_type = ordered->compress_type;
2829 new->root = RB_ROOT;
2830 INIT_LIST_HEAD(&new->head);
2831
2832 path = btrfs_alloc_path();
2833 if (!path)
2834 goto out_kfree;
2835
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002836 key.objectid = btrfs_ino(BTRFS_I(inode));
Liu Bo38c227d2013-01-29 03:18:40 +00002837 key.type = BTRFS_EXTENT_DATA_KEY;
2838 key.offset = new->file_pos;
2839
2840 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2841 if (ret < 0)
2842 goto out_free_path;
2843 if (ret > 0 && path->slots[0] > 0)
2844 path->slots[0]--;
2845
2846 /* find out all the old extents for the file range */
2847 while (1) {
2848 struct btrfs_file_extent_item *extent;
2849 struct extent_buffer *l;
2850 int slot;
2851 u64 num_bytes;
2852 u64 offset;
2853 u64 end;
2854 u64 disk_bytenr;
2855 u64 extent_offset;
2856
2857 l = path->nodes[0];
2858 slot = path->slots[0];
2859
2860 if (slot >= btrfs_header_nritems(l)) {
2861 ret = btrfs_next_leaf(root, path);
2862 if (ret < 0)
Liu Bo6f519562013-10-29 10:45:05 +08002863 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002864 else if (ret > 0)
2865 break;
2866 continue;
2867 }
2868
2869 btrfs_item_key_to_cpu(l, &key, slot);
2870
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002871 if (key.objectid != btrfs_ino(BTRFS_I(inode)))
Liu Bo38c227d2013-01-29 03:18:40 +00002872 break;
2873 if (key.type != BTRFS_EXTENT_DATA_KEY)
2874 break;
2875 if (key.offset >= new->file_pos + new->len)
2876 break;
2877
2878 extent = btrfs_item_ptr(l, slot, struct btrfs_file_extent_item);
2879
2880 num_bytes = btrfs_file_extent_num_bytes(l, extent);
2881 if (key.offset + num_bytes < new->file_pos)
2882 goto next;
2883
2884 disk_bytenr = btrfs_file_extent_disk_bytenr(l, extent);
2885 if (!disk_bytenr)
2886 goto next;
2887
2888 extent_offset = btrfs_file_extent_offset(l, extent);
2889
2890 old = kmalloc(sizeof(*old), GFP_NOFS);
2891 if (!old)
Liu Bo6f519562013-10-29 10:45:05 +08002892 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002893
2894 offset = max(new->file_pos, key.offset);
2895 end = min(new->file_pos + new->len, key.offset + num_bytes);
2896
2897 old->bytenr = disk_bytenr;
2898 old->extent_offset = extent_offset;
2899 old->offset = offset - key.offset;
2900 old->len = end - offset;
2901 old->new = new;
2902 old->count = 0;
2903 list_add_tail(&old->list, &new->head);
2904next:
2905 path->slots[0]++;
2906 cond_resched();
2907 }
2908
2909 btrfs_free_path(path);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002910 atomic_inc(&fs_info->defrag_running);
Liu Bo38c227d2013-01-29 03:18:40 +00002911
2912 return new;
2913
Liu Bo38c227d2013-01-29 03:18:40 +00002914out_free_path:
2915 btrfs_free_path(path);
2916out_kfree:
Liu Bo6f519562013-10-29 10:45:05 +08002917 free_sa_defrag_extent(new);
Liu Bo38c227d2013-01-29 03:18:40 +00002918 return NULL;
2919}
2920
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002921static void btrfs_release_delalloc_bytes(struct btrfs_fs_info *fs_info,
Miao Xiee570fd22014-06-19 10:42:50 +08002922 u64 start, u64 len)
2923{
2924 struct btrfs_block_group_cache *cache;
2925
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002926 cache = btrfs_lookup_block_group(fs_info, start);
Miao Xiee570fd22014-06-19 10:42:50 +08002927 ASSERT(cache);
2928
2929 spin_lock(&cache->lock);
2930 cache->delalloc_bytes -= len;
2931 spin_unlock(&cache->lock);
2932
2933 btrfs_put_block_group(cache);
2934}
2935
Chris Masond352ac62008-09-29 15:18:18 -04002936/* as ordered data IO finishes, this gets called so we can finish
2937 * an ordered extent if the range of bytes in the file it covers are
2938 * fully written.
2939 */
Josef Bacik5fd02042012-05-02 14:00:54 -04002940static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002941{
Josef Bacik5fd02042012-05-02 14:00:54 -04002942 struct inode *inode = ordered_extent->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002943 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002944 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002945 struct btrfs_trans_handle *trans = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002946 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002947 struct extent_state *cached_state = NULL;
Liu Bo38c227d2013-01-29 03:18:40 +00002948 struct new_sa_defrag_extent *new = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08002949 int compress_type = 0;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002950 int ret = 0;
2951 u64 logical_len = ordered_extent->len;
Li Zefan82d59022011-04-20 10:33:24 +08002952 bool nolock;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002953 bool truncated = false;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002954 bool range_locked = false;
2955 bool clear_new_delalloc_bytes = false;
Josef Bacik49940bd2018-10-11 15:54:21 -04002956 bool clear_reserved_extent = true;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002957
2958 if (!test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
2959 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags) &&
2960 !test_bit(BTRFS_ORDERED_DIRECT, &ordered_extent->flags))
2961 clear_new_delalloc_bytes = true;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002962
Nikolay Borisov70ddc552017-02-20 13:50:35 +02002963 nolock = btrfs_is_free_space_inode(BTRFS_I(inode));
Josef Bacik0cb59c92010-07-02 12:14:14 -04002964
Josef Bacik5fd02042012-05-02 14:00:54 -04002965 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
2966 ret = -EIO;
2967 goto out;
2968 }
2969
Nikolay Borisov7ab79562017-02-20 13:50:57 +02002970 btrfs_free_io_failure_record(BTRFS_I(inode),
2971 ordered_extent->file_offset,
2972 ordered_extent->file_offset +
2973 ordered_extent->len - 1);
Miao Xief6124962014-09-12 18:44:04 +08002974
Josef Bacik77cef2e2013-08-29 13:57:21 -04002975 if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) {
2976 truncated = true;
2977 logical_len = ordered_extent->truncated_len;
2978 /* Truncated the entire extent, don't bother adding */
2979 if (!logical_len)
2980 goto out;
2981 }
2982
Yan, Zhengc2167752009-11-12 09:34:21 +00002983 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002984 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
Qu Wenruo94ed9382015-09-08 17:25:56 +08002985
2986 /*
2987 * For mwrite(mmap + memset to write) case, we still reserve
2988 * space for NOCOW range.
2989 * As NOCOW won't cause a new delayed ref, just free the space
2990 */
Qu Wenruobc42bda2017-02-27 15:10:39 +08002991 btrfs_qgroup_free_data(inode, NULL, ordered_extent->file_offset,
Qu Wenruo94ed9382015-09-08 17:25:56 +08002992 ordered_extent->len);
Josef Bacik6c760c02012-11-09 10:53:21 -05002993 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2994 if (nolock)
2995 trans = btrfs_join_transaction_nolock(root);
2996 else
2997 trans = btrfs_join_transaction(root);
2998 if (IS_ERR(trans)) {
2999 ret = PTR_ERR(trans);
3000 trans = NULL;
3001 goto out;
Yan, Zhengc2167752009-11-12 09:34:21 +00003002 }
Josef Bacik69fe2d72017-10-19 14:15:57 -04003003 trans->block_rsv = &BTRFS_I(inode)->block_rsv;
Josef Bacik6c760c02012-11-09 10:53:21 -05003004 ret = btrfs_update_inode_fallback(trans, root, inode);
3005 if (ret) /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04003006 btrfs_abort_transaction(trans, ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00003007 goto out;
3008 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04003009
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003010 range_locked = true;
Josef Bacik2ac55d42010-02-03 19:33:23 +00003011 lock_extent_bits(io_tree, ordered_extent->file_offset,
3012 ordered_extent->file_offset + ordered_extent->len - 1,
David Sterbaff13db42015-12-03 14:30:40 +01003013 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003014
Liu Bo38c227d2013-01-29 03:18:40 +00003015 ret = test_range_bit(io_tree, ordered_extent->file_offset,
3016 ordered_extent->file_offset + ordered_extent->len - 1,
Liu Bo452e62b2017-05-26 17:44:23 -06003017 EXTENT_DEFRAG, 0, cached_state);
Liu Bo38c227d2013-01-29 03:18:40 +00003018 if (ret) {
3019 u64 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
Josef Bacik8101c8d2014-01-29 16:05:30 -05003020 if (0 && last_snapshot >= BTRFS_I(inode)->generation)
Liu Bo38c227d2013-01-29 03:18:40 +00003021 /* the inode is shared */
3022 new = record_old_file_extents(inode, ordered_extent);
3023
3024 clear_extent_bit(io_tree, ordered_extent->file_offset,
3025 ordered_extent->file_offset + ordered_extent->len - 1,
David Sterbaae0f1622017-10-31 16:37:52 +01003026 EXTENT_DEFRAG, 0, 0, &cached_state);
Liu Bo38c227d2013-01-29 03:18:40 +00003027 }
3028
Josef Bacik0cb59c92010-07-02 12:14:14 -04003029 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003030 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04003031 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003032 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003033 if (IS_ERR(trans)) {
3034 ret = PTR_ERR(trans);
3035 trans = NULL;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003036 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003037 }
Chris Masona79b7d42014-05-22 16:18:52 -07003038
Josef Bacik69fe2d72017-10-19 14:15:57 -04003039 trans->block_rsv = &BTRFS_I(inode)->block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00003040
Chris Masonc8b97812008-10-29 14:49:59 -04003041 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08003042 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04003043 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08003044 BUG_ON(compress_type);
Justin Maggardb430b772017-10-30 15:29:10 -07003045 btrfs_qgroup_free_data(inode, NULL, ordered_extent->file_offset,
3046 ordered_extent->len);
Nikolay Borisov7a6d7062017-02-20 13:50:48 +02003047 ret = btrfs_mark_extent_written(trans, BTRFS_I(inode),
Yan Zhengd899e052008-10-30 14:25:28 -04003048 ordered_extent->file_offset,
3049 ordered_extent->file_offset +
Josef Bacik77cef2e2013-08-29 13:57:21 -04003050 logical_len);
Yan Zhengd899e052008-10-30 14:25:28 -04003051 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003052 BUG_ON(root == fs_info->tree_root);
Yan Zhengd899e052008-10-30 14:25:28 -04003053 ret = insert_reserved_file_extent(trans, inode,
3054 ordered_extent->file_offset,
3055 ordered_extent->start,
3056 ordered_extent->disk_len,
Josef Bacik77cef2e2013-08-29 13:57:21 -04003057 logical_len, logical_len,
Li Zefan261507a02010-12-17 14:21:50 +08003058 compress_type, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04003059 BTRFS_FILE_EXTENT_REG);
Josef Bacik49940bd2018-10-11 15:54:21 -04003060 if (!ret) {
3061 clear_reserved_extent = false;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003062 btrfs_release_delalloc_bytes(fs_info,
Miao Xiee570fd22014-06-19 10:42:50 +08003063 ordered_extent->start,
3064 ordered_extent->disk_len);
Josef Bacik49940bd2018-10-11 15:54:21 -04003065 }
Yan Zhengd899e052008-10-30 14:25:28 -04003066 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04003067 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
3068 ordered_extent->file_offset, ordered_extent->len,
3069 trans->transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003070 if (ret < 0) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003071 btrfs_abort_transaction(trans, ret);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003072 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003073 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00003074
Nikolay Borisovac01f262018-01-08 10:59:43 +02003075 ret = add_pending_csums(trans, inode, &ordered_extent->list);
3076 if (ret) {
3077 btrfs_abort_transaction(trans, ret);
3078 goto out;
3079 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04003080
Josef Bacik6c760c02012-11-09 10:53:21 -05003081 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
3082 ret = btrfs_update_inode_fallback(trans, root, inode);
3083 if (ret) { /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04003084 btrfs_abort_transaction(trans, ret);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003085 goto out;
Josef Bacik1ef30be2011-04-05 19:25:36 -04003086 }
3087 ret = 0;
Yan, Zhengc2167752009-11-12 09:34:21 +00003088out:
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003089 if (range_locked || clear_new_delalloc_bytes) {
3090 unsigned int clear_bits = 0;
3091
3092 if (range_locked)
3093 clear_bits |= EXTENT_LOCKED;
3094 if (clear_new_delalloc_bytes)
3095 clear_bits |= EXTENT_DELALLOC_NEW;
3096 clear_extent_bit(&BTRFS_I(inode)->io_tree,
3097 ordered_extent->file_offset,
3098 ordered_extent->file_offset +
3099 ordered_extent->len - 1,
3100 clear_bits,
3101 (clear_bits & EXTENT_LOCKED) ? 1 : 0,
David Sterbaae0f1622017-10-31 16:37:52 +01003102 0, &cached_state);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003103 }
3104
Miao Xiea698d0752012-09-20 01:51:59 -06003105 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003106 btrfs_end_transaction(trans);
Josef Bacik0cb59c92010-07-02 12:14:14 -04003107
Josef Bacik77cef2e2013-08-29 13:57:21 -04003108 if (ret || truncated) {
3109 u64 start, end;
3110
3111 if (truncated)
3112 start = ordered_extent->file_offset + logical_len;
3113 else
3114 start = ordered_extent->file_offset;
3115 end = ordered_extent->file_offset + ordered_extent->len - 1;
David Sterbaf08dc362017-10-31 17:02:39 +01003116 clear_extent_uptodate(io_tree, start, end, NULL);
Josef Bacik77cef2e2013-08-29 13:57:21 -04003117
3118 /* Drop the cache for the part of the extent we didn't write. */
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02003119 btrfs_drop_extent_cache(BTRFS_I(inode), start, end, 0);
Josef Bacik5fd02042012-05-02 14:00:54 -04003120
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003121 /*
3122 * If the ordered extent had an IOERR or something else went
3123 * wrong we need to return the space for this ordered extent
Josef Bacik77cef2e2013-08-29 13:57:21 -04003124 * back to the allocator. We only free the extent in the
3125 * truncated case if we didn't write out the extent at all.
Josef Bacik49940bd2018-10-11 15:54:21 -04003126 *
3127 * If we made it past insert_reserved_file_extent before we
3128 * errored out then we don't need to do this as the accounting
3129 * has already been done.
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003130 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04003131 if ((ret || !logical_len) &&
Josef Bacik49940bd2018-10-11 15:54:21 -04003132 clear_reserved_extent &&
Josef Bacik77cef2e2013-08-29 13:57:21 -04003133 !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003134 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003135 btrfs_free_reserved_extent(fs_info,
3136 ordered_extent->start,
Miao Xiee570fd22014-06-19 10:42:50 +08003137 ordered_extent->disk_len, 1);
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003138 }
3139
3140
Josef Bacik5fd02042012-05-02 14:00:54 -04003141 /*
Liu Bo8bad3c02012-06-18 12:14:23 +08003142 * This needs to be done to make sure anybody waiting knows we are done
3143 * updating everything for this ordered extent.
Josef Bacik5fd02042012-05-02 14:00:54 -04003144 */
3145 btrfs_remove_ordered_extent(inode, ordered_extent);
3146
Liu Bo38c227d2013-01-29 03:18:40 +00003147 /* for snapshot-aware defrag */
Liu Bo6f519562013-10-29 10:45:05 +08003148 if (new) {
3149 if (ret) {
3150 free_sa_defrag_extent(new);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003151 atomic_dec(&fs_info->defrag_running);
Liu Bo6f519562013-10-29 10:45:05 +08003152 } else {
3153 relink_file_extents(new);
3154 }
3155 }
Liu Bo38c227d2013-01-29 03:18:40 +00003156
Chris Masone6dcd2d2008-07-17 12:53:50 -04003157 /* once for us */
3158 btrfs_put_ordered_extent(ordered_extent);
3159 /* once for the tree */
3160 btrfs_put_ordered_extent(ordered_extent);
3161
Josef Bacik5fd02042012-05-02 14:00:54 -04003162 return ret;
3163}
3164
3165static void finish_ordered_fn(struct btrfs_work *work)
3166{
3167 struct btrfs_ordered_extent *ordered_extent;
3168 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
3169 btrfs_finish_ordered_io(ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003170}
3171
Nikolay Borisovc6297322018-11-08 10:18:08 +02003172void btrfs_writepage_endio_finish_ordered(struct page *page, u64 start,
3173 u64 end, int uptodate)
Chris Mason211f90e2008-07-18 11:56:15 -04003174{
Josef Bacik5fd02042012-05-02 14:00:54 -04003175 struct inode *inode = page->mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003176 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik5fd02042012-05-02 14:00:54 -04003177 struct btrfs_ordered_extent *ordered_extent = NULL;
Liu Bo9e0af232014-08-15 23:36:53 +08003178 struct btrfs_workqueue *wq;
3179 btrfs_work_func_t func;
Josef Bacik5fd02042012-05-02 14:00:54 -04003180
liubo1abe9b82011-03-24 11:18:59 +00003181 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
3182
Chris Mason8b62b722009-09-02 16:53:46 -04003183 ClearPagePrivate2(page);
Josef Bacik5fd02042012-05-02 14:00:54 -04003184 if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
3185 end - start + 1, uptodate))
David Sterbac3988d62017-02-17 15:18:32 +01003186 return;
Josef Bacik5fd02042012-05-02 14:00:54 -04003187
Nikolay Borisov70ddc552017-02-20 13:50:35 +02003188 if (btrfs_is_free_space_inode(BTRFS_I(inode))) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003189 wq = fs_info->endio_freespace_worker;
Liu Bo9e0af232014-08-15 23:36:53 +08003190 func = btrfs_freespace_write_helper;
3191 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003192 wq = fs_info->endio_write_workers;
Liu Bo9e0af232014-08-15 23:36:53 +08003193 func = btrfs_endio_write_helper;
3194 }
Josef Bacik5fd02042012-05-02 14:00:54 -04003195
Liu Bo9e0af232014-08-15 23:36:53 +08003196 btrfs_init_work(&ordered_extent->work, func, finish_ordered_fn, NULL,
3197 NULL);
3198 btrfs_queue_work(wq, &ordered_extent->work);
Chris Mason211f90e2008-07-18 11:56:15 -04003199}
3200
Miao Xiedc380ae2014-09-12 18:43:55 +08003201static int __readpage_endio_check(struct inode *inode,
3202 struct btrfs_io_bio *io_bio,
3203 int icsum, struct page *page,
3204 int pgoff, u64 start, size_t len)
3205{
3206 char *kaddr;
3207 u32 csum_expected;
3208 u32 csum = ~(u32)0;
Miao Xiedc380ae2014-09-12 18:43:55 +08003209
3210 csum_expected = *(((u32 *)io_bio->csum) + icsum);
3211
3212 kaddr = kmap_atomic(page);
3213 csum = btrfs_csum_data(kaddr + pgoff, csum, len);
Domagoj Tršan0b5e3da2016-10-27 08:52:33 +01003214 btrfs_csum_final(csum, (u8 *)&csum);
Miao Xiedc380ae2014-09-12 18:43:55 +08003215 if (csum != csum_expected)
3216 goto zeroit;
3217
3218 kunmap_atomic(kaddr);
3219 return 0;
3220zeroit:
Nikolay Borisov0970a222017-02-20 13:50:53 +02003221 btrfs_print_data_csum_error(BTRFS_I(inode), start, csum, csum_expected,
Qu Wenruo6f6b6432017-02-09 10:45:06 +08003222 io_bio->mirror_num);
Miao Xiedc380ae2014-09-12 18:43:55 +08003223 memset(kaddr + pgoff, 1, len);
3224 flush_dcache_page(page);
3225 kunmap_atomic(kaddr);
Miao Xiedc380ae2014-09-12 18:43:55 +08003226 return -EIO;
3227}
3228
Chris Masond352ac62008-09-29 15:18:18 -04003229/*
Chris Masond352ac62008-09-29 15:18:18 -04003230 * when reads are done, we need to check csums to verify the data is correct
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02003231 * if there's a match, we allow the bio to finish. If not, the code in
3232 * extent_io.c will try to find good copies for us.
Chris Masond352ac62008-09-29 15:18:18 -04003233 */
Miao Xiefacc8a222013-07-25 19:22:34 +08003234static int btrfs_readpage_end_io_hook(struct btrfs_io_bio *io_bio,
3235 u64 phy_offset, struct page *page,
3236 u64 start, u64 end, int mirror)
Chris Mason07157aa2007-08-30 08:50:51 -04003237{
Miao Xie4eee4fa2012-12-21 09:17:45 +00003238 size_t offset = start - page_offset(page);
Chris Mason07157aa2007-08-30 08:50:51 -04003239 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05003240 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masonff79f812007-10-15 16:22:25 -04003241 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -05003242
Chris Masond20f7042008-12-08 16:58:54 -05003243 if (PageChecked(page)) {
3244 ClearPageChecked(page);
Miao Xiedc380ae2014-09-12 18:43:55 +08003245 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003246 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003247
3248 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Miao Xiedc380ae2014-09-12 18:43:55 +08003249 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003250
Yan Zheng17d217f2008-12-12 10:03:38 -05003251 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04003252 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
David Sterba91166212016-04-26 23:54:39 +02003253 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM);
Yan Zheng17d217f2008-12-12 10:03:38 -05003254 return 0;
3255 }
3256
Miao Xiefacc8a222013-07-25 19:22:34 +08003257 phy_offset >>= inode->i_sb->s_blocksize_bits;
Miao Xiedc380ae2014-09-12 18:43:55 +08003258 return __readpage_endio_check(inode, io_bio, phy_offset, page, offset,
3259 start, (size_t)(end - start + 1));
Chris Mason07157aa2007-08-30 08:50:51 -04003260}
Chris Masonb888db22007-08-27 16:49:44 -04003261
Nikolay Borisovc1c3fac2018-01-16 09:31:58 +02003262/*
3263 * btrfs_add_delayed_iput - perform a delayed iput on @inode
3264 *
3265 * @inode: The inode we want to perform iput on
3266 *
3267 * This function uses the generic vfs_inode::i_count to track whether we should
3268 * just decrement it (in case it's > 1) or if this is the last iput then link
3269 * the inode to the delayed iput machinery. Delayed iputs are processed at
3270 * transaction commit time/superblock commit/cleaner kthread.
3271 */
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003272void btrfs_add_delayed_iput(struct inode *inode)
3273{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003274 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
David Sterba8089fe62015-11-19 14:15:51 +01003275 struct btrfs_inode *binode = BTRFS_I(inode);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003276
3277 if (atomic_add_unless(&inode->i_count, -1, 1))
3278 return;
3279
Josef Bacik034f7842018-12-03 11:06:52 -05003280 atomic_inc(&fs_info->nr_delayed_iputs);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003281 spin_lock(&fs_info->delayed_iput_lock);
Nikolay Borisovc1c3fac2018-01-16 09:31:58 +02003282 ASSERT(list_empty(&binode->delayed_iput));
3283 list_add_tail(&binode->delayed_iput, &fs_info->delayed_iputs);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003284 spin_unlock(&fs_info->delayed_iput_lock);
Josef Bacikfd340d02019-01-11 10:21:02 -05003285 if (!test_bit(BTRFS_FS_CLEANER_RUNNING, &fs_info->flags))
3286 wake_up_process(fs_info->cleaner_kthread);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003287}
3288
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003289void btrfs_run_delayed_iputs(struct btrfs_fs_info *fs_info)
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003290{
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003291
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003292 spin_lock(&fs_info->delayed_iput_lock);
David Sterba8089fe62015-11-19 14:15:51 +01003293 while (!list_empty(&fs_info->delayed_iputs)) {
3294 struct btrfs_inode *inode;
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003295
David Sterba8089fe62015-11-19 14:15:51 +01003296 inode = list_first_entry(&fs_info->delayed_iputs,
3297 struct btrfs_inode, delayed_iput);
Nikolay Borisovc1c3fac2018-01-16 09:31:58 +02003298 list_del_init(&inode->delayed_iput);
David Sterba8089fe62015-11-19 14:15:51 +01003299 spin_unlock(&fs_info->delayed_iput_lock);
3300 iput(&inode->vfs_inode);
Josef Bacik034f7842018-12-03 11:06:52 -05003301 if (atomic_dec_and_test(&fs_info->nr_delayed_iputs))
3302 wake_up(&fs_info->delayed_iputs_wait);
David Sterba8089fe62015-11-19 14:15:51 +01003303 spin_lock(&fs_info->delayed_iput_lock);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003304 }
David Sterba8089fe62015-11-19 14:15:51 +01003305 spin_unlock(&fs_info->delayed_iput_lock);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003306}
3307
Josef Bacik034f7842018-12-03 11:06:52 -05003308/**
3309 * btrfs_wait_on_delayed_iputs - wait on the delayed iputs to be done running
3310 * @fs_info - the fs_info for this fs
3311 * @return - EINTR if we were killed, 0 if nothing's pending
3312 *
3313 * This will wait on any delayed iputs that are currently running with KILLABLE
3314 * set. Once they are all done running we will return, unless we are killed in
3315 * which case we return EINTR. This helps in user operations like fallocate etc
3316 * that might get blocked on the iputs.
3317 */
3318int btrfs_wait_on_delayed_iputs(struct btrfs_fs_info *fs_info)
3319{
3320 int ret = wait_event_killable(fs_info->delayed_iputs_wait,
3321 atomic_read(&fs_info->nr_delayed_iputs) == 0);
3322 if (ret)
3323 return -EINTR;
3324 return 0;
3325}
3326
Yan, Zhengd68fc572010-05-16 10:49:58 -04003327/*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003328 * This creates an orphan entry for the given inode in case something goes wrong
3329 * in the middle of an unlink.
Josef Bacik7b128762008-07-24 12:17:14 -04003330 */
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003331int btrfs_orphan_add(struct btrfs_trans_handle *trans,
Omar Sandoval27919062018-05-11 13:13:37 -07003332 struct btrfs_inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003333{
Yan, Zhengd68fc572010-05-16 10:49:58 -04003334 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003335
Omar Sandoval27919062018-05-11 13:13:37 -07003336 ret = btrfs_insert_orphan_item(trans, inode->root, btrfs_ino(inode));
3337 if (ret && ret != -EEXIST) {
3338 btrfs_abort_transaction(trans, ret);
3339 return ret;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003340 }
3341
Yan, Zhengd68fc572010-05-16 10:49:58 -04003342 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003343}
3344
3345/*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003346 * We have done the delete so we can go ahead and remove the orphan item for
3347 * this particular inode.
Josef Bacik7b128762008-07-24 12:17:14 -04003348 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00003349static int btrfs_orphan_del(struct btrfs_trans_handle *trans,
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003350 struct btrfs_inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003351{
Omar Sandoval27919062018-05-11 13:13:37 -07003352 return btrfs_del_orphan_item(trans, inode->root, btrfs_ino(inode));
Josef Bacik7b128762008-07-24 12:17:14 -04003353}
3354
3355/*
3356 * this cleans up any orphans that may be left on the list from the last use
3357 * of this root.
3358 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003359int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04003360{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003361 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik7b128762008-07-24 12:17:14 -04003362 struct btrfs_path *path;
3363 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04003364 struct btrfs_key key, found_key;
3365 struct btrfs_trans_handle *trans;
3366 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003367 u64 last_objectid = 0;
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003368 int ret = 0, nr_unlink = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003369
Yan, Zhengd68fc572010-05-16 10:49:58 -04003370 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003371 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00003372
3373 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003374 if (!path) {
3375 ret = -ENOMEM;
3376 goto out;
3377 }
David Sterbae4058b52015-11-27 16:31:35 +01003378 path->reada = READA_BACK;
Josef Bacik7b128762008-07-24 12:17:14 -04003379
3380 key.objectid = BTRFS_ORPHAN_OBJECTID;
David Sterba962a2982014-06-04 18:41:45 +02003381 key.type = BTRFS_ORPHAN_ITEM_KEY;
Josef Bacik7b128762008-07-24 12:17:14 -04003382 key.offset = (u64)-1;
3383
Josef Bacik7b128762008-07-24 12:17:14 -04003384 while (1) {
3385 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003386 if (ret < 0)
3387 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003388
3389 /*
3390 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003391 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04003392 * find the key and see if we have stuff that matches
3393 */
3394 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003395 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003396 if (path->slots[0] == 0)
3397 break;
3398 path->slots[0]--;
3399 }
3400
3401 /* pull out the item */
3402 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04003403 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3404
3405 /* make sure the item matches what we want */
3406 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
3407 break;
David Sterba962a2982014-06-04 18:41:45 +02003408 if (found_key.type != BTRFS_ORPHAN_ITEM_KEY)
Josef Bacik7b128762008-07-24 12:17:14 -04003409 break;
3410
3411 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02003412 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04003413
3414 /*
3415 * this is where we are basically btrfs_lookup, without the
3416 * crossing root thing. we store the inode number in the
3417 * offset of the orphan item.
3418 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003419
3420 if (found_key.offset == last_objectid) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003421 btrfs_err(fs_info,
3422 "Error removing orphan entry, stopping orphan cleanup");
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003423 ret = -EINVAL;
3424 goto out;
3425 }
3426
3427 last_objectid = found_key.offset;
3428
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003429 found_key.objectid = found_key.offset;
3430 found_key.type = BTRFS_INODE_ITEM_KEY;
3431 found_key.offset = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003432 inode = btrfs_iget(fs_info->sb, &found_key, root, NULL);
Rusty Russell8c6ffba2013-07-15 11:20:32 +09303433 ret = PTR_ERR_OR_ZERO(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003434 if (ret && ret != -ENOENT)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003435 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003436
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003437 if (ret == -ENOENT && root == fs_info->tree_root) {
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003438 struct btrfs_root *dead_root;
3439 struct btrfs_fs_info *fs_info = root->fs_info;
3440 int is_dead_root = 0;
3441
3442 /*
3443 * this is an orphan in the tree root. Currently these
3444 * could come from 2 sources:
3445 * a) a snapshot deletion in progress
3446 * b) a free space cache inode
3447 * We need to distinguish those two, as the snapshot
3448 * orphan must not get deleted.
3449 * find_dead_roots already ran before us, so if this
3450 * is a snapshot deletion, we should find the root
3451 * in the dead_roots list
3452 */
3453 spin_lock(&fs_info->trans_lock);
3454 list_for_each_entry(dead_root, &fs_info->dead_roots,
3455 root_list) {
3456 if (dead_root->root_key.objectid ==
3457 found_key.objectid) {
3458 is_dead_root = 1;
3459 break;
3460 }
3461 }
3462 spin_unlock(&fs_info->trans_lock);
3463 if (is_dead_root) {
3464 /* prevent this orphan from being found again */
3465 key.offset = found_key.objectid - 1;
3466 continue;
3467 }
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003468
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003469 }
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003470
Josef Bacika8c9e572011-09-21 16:55:59 -04003471 /*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003472 * If we have an inode with links, there are a couple of
3473 * possibilities. Old kernels (before v3.12) used to create an
3474 * orphan item for truncate indicating that there were possibly
3475 * extent items past i_size that needed to be deleted. In v3.12,
3476 * truncate was changed to update i_size in sync with the extent
3477 * items, but the (useless) orphan item was still created. Since
3478 * v4.18, we don't create the orphan item for truncate at all.
3479 *
3480 * So, this item could mean that we need to do a truncate, but
3481 * only if this filesystem was last used on a pre-v3.12 kernel
3482 * and was not cleanly unmounted. The odds of that are quite
3483 * slim, and it's a pain to do the truncate now, so just delete
3484 * the orphan item.
3485 *
3486 * It's also possible that this orphan item was supposed to be
3487 * deleted but wasn't. The inode number may have been reused,
3488 * but either way, we can delete the orphan item.
Josef Bacika8c9e572011-09-21 16:55:59 -04003489 */
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003490 if (ret == -ENOENT || inode->i_nlink) {
3491 if (!ret)
3492 iput(inode);
Josef Bacika8c9e572011-09-21 16:55:59 -04003493 trans = btrfs_start_transaction(root, 1);
3494 if (IS_ERR(trans)) {
3495 ret = PTR_ERR(trans);
3496 goto out;
3497 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003498 btrfs_debug(fs_info, "auto deleting %Lu",
3499 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003500 ret = btrfs_del_orphan_item(trans, root,
3501 found_key.objectid);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003502 btrfs_end_transaction(trans);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003503 if (ret)
3504 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003505 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003506 }
Josef Bacik7b128762008-07-24 12:17:14 -04003507
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003508 nr_unlink++;
Josef Bacik7b128762008-07-24 12:17:14 -04003509
3510 /* this will do delete_inode and everything for us */
3511 iput(inode);
3512 }
Miao Xie3254c872011-11-10 20:45:05 -05003513 /* release the path since we're done with it */
3514 btrfs_release_path(path);
3515
Yan, Zhengd68fc572010-05-16 10:49:58 -04003516 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
3517
Omar Sandovala575cee2018-05-11 13:13:38 -07003518 if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003519 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003520 if (!IS_ERR(trans))
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003521 btrfs_end_transaction(trans);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003522 }
Josef Bacik7b128762008-07-24 12:17:14 -04003523
3524 if (nr_unlink)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003525 btrfs_debug(fs_info, "unlinked %d orphans", nr_unlink);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003526
3527out:
3528 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003529 btrfs_err(fs_info, "could not do orphan cleanup %d", ret);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003530 btrfs_free_path(path);
3531 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003532}
3533
Chris Masond352ac62008-09-29 15:18:18 -04003534/*
Chris Mason46a53cc2009-04-27 11:47:50 -04003535 * very simple check to peek ahead in the leaf looking for xattrs. If we
3536 * don't find any xattrs, we know there can't be any acls.
3537 *
3538 * slot is the slot the inode is in, objectid is the objectid of the inode
3539 */
3540static noinline int acls_after_inode_item(struct extent_buffer *leaf,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003541 int slot, u64 objectid,
3542 int *first_xattr_slot)
Chris Mason46a53cc2009-04-27 11:47:50 -04003543{
3544 u32 nritems = btrfs_header_nritems(leaf);
3545 struct btrfs_key found_key;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003546 static u64 xattr_access = 0;
3547 static u64 xattr_default = 0;
Chris Mason46a53cc2009-04-27 11:47:50 -04003548 int scanned = 0;
3549
Josef Bacikf23b5a52013-06-19 10:16:26 -04003550 if (!xattr_access) {
Andreas Gruenbacher97d79292015-12-02 14:44:35 +01003551 xattr_access = btrfs_name_hash(XATTR_NAME_POSIX_ACL_ACCESS,
3552 strlen(XATTR_NAME_POSIX_ACL_ACCESS));
3553 xattr_default = btrfs_name_hash(XATTR_NAME_POSIX_ACL_DEFAULT,
3554 strlen(XATTR_NAME_POSIX_ACL_DEFAULT));
Josef Bacikf23b5a52013-06-19 10:16:26 -04003555 }
3556
Chris Mason46a53cc2009-04-27 11:47:50 -04003557 slot++;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003558 *first_xattr_slot = -1;
Chris Mason46a53cc2009-04-27 11:47:50 -04003559 while (slot < nritems) {
3560 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3561
3562 /* we found a different objectid, there must not be acls */
3563 if (found_key.objectid != objectid)
3564 return 0;
3565
3566 /* we found an xattr, assume we've got an acl */
Josef Bacikf23b5a52013-06-19 10:16:26 -04003567 if (found_key.type == BTRFS_XATTR_ITEM_KEY) {
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003568 if (*first_xattr_slot == -1)
3569 *first_xattr_slot = slot;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003570 if (found_key.offset == xattr_access ||
3571 found_key.offset == xattr_default)
3572 return 1;
3573 }
Chris Mason46a53cc2009-04-27 11:47:50 -04003574
3575 /*
3576 * we found a key greater than an xattr key, there can't
3577 * be any acls later on
3578 */
3579 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
3580 return 0;
3581
3582 slot++;
3583 scanned++;
3584
3585 /*
3586 * it goes inode, inode backrefs, xattrs, extents,
3587 * so if there are a ton of hard links to an inode there can
3588 * be a lot of backrefs. Don't waste time searching too hard,
3589 * this is just an optimization
3590 */
3591 if (scanned >= 8)
3592 break;
3593 }
3594 /* we hit the end of the leaf before we found an xattr or
3595 * something larger than an xattr. We have to assume the inode
3596 * has acls
3597 */
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003598 if (*first_xattr_slot == -1)
3599 *first_xattr_slot = slot;
Chris Mason46a53cc2009-04-27 11:47:50 -04003600 return 1;
3601}
3602
3603/*
Chris Masond352ac62008-09-29 15:18:18 -04003604 * read an inode from the btree into the in-memory inode
3605 */
Filipe Manana4222ea72018-10-24 10:13:03 +01003606static int btrfs_read_locked_inode(struct inode *inode,
3607 struct btrfs_path *in_path)
Chris Mason39279cc2007-06-12 06:35:45 -04003608{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003609 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Filipe Manana4222ea72018-10-24 10:13:03 +01003610 struct btrfs_path *path = in_path;
Chris Mason5f39d392007-10-15 16:14:19 -04003611 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003612 struct btrfs_inode_item *inode_item;
3613 struct btrfs_root *root = BTRFS_I(inode)->root;
3614 struct btrfs_key location;
Miao Xie67de1172013-12-26 13:07:06 +08003615 unsigned long ptr;
Chris Mason46a53cc2009-04-27 11:47:50 -04003616 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04003617 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04003618 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003619 bool filled = false;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003620 int first_xattr_slot;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003621
3622 ret = btrfs_fill_inode(inode, &rdev);
3623 if (!ret)
3624 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04003625
Filipe Manana4222ea72018-10-24 10:13:03 +01003626 if (!path) {
3627 path = btrfs_alloc_path();
3628 if (!path)
3629 return -ENOMEM;
3630 }
Mark Fasheh1748f842011-07-12 11:25:31 -07003631
Chris Mason39279cc2007-06-12 06:35:45 -04003632 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05003633
Chris Mason39279cc2007-06-12 06:35:45 -04003634 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Filipe Manana67710892016-06-06 11:51:25 +01003635 if (ret) {
Filipe Manana4222ea72018-10-24 10:13:03 +01003636 if (path != in_path)
3637 btrfs_free_path(path);
Al Virof5b3a412018-07-29 23:04:51 +01003638 return ret;
Filipe Manana67710892016-06-06 11:51:25 +01003639 }
Chris Mason39279cc2007-06-12 06:35:45 -04003640
Chris Mason5f39d392007-10-15 16:14:19 -04003641 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00003642
3643 if (filled)
Miao Xie67de1172013-12-26 13:07:06 +08003644 goto cache_index;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003645
Chris Mason5f39d392007-10-15 16:14:19 -04003646 inode_item = btrfs_item_ptr(leaf, path->slots[0],
3647 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003648 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02003649 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08003650 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
3651 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02003652 btrfs_i_size_write(BTRFS_I(inode), btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04003653
David Sterbaa937b972014-12-12 17:39:12 +01003654 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->atime);
3655 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->atime);
Chris Mason5f39d392007-10-15 16:14:19 -04003656
David Sterbaa937b972014-12-12 17:39:12 +01003657 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->mtime);
3658 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->mtime);
Chris Mason5f39d392007-10-15 16:14:19 -04003659
David Sterbaa937b972014-12-12 17:39:12 +01003660 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->ctime);
3661 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->ctime);
Chris Mason5f39d392007-10-15 16:14:19 -04003662
chandan r9cc97d62012-07-04 12:48:07 +05303663 BTRFS_I(inode)->i_otime.tv_sec =
3664 btrfs_timespec_sec(leaf, &inode_item->otime);
3665 BTRFS_I(inode)->i_otime.tv_nsec =
3666 btrfs_timespec_nsec(leaf, &inode_item->otime);
Chris Mason5f39d392007-10-15 16:14:19 -04003667
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003668 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003669 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003670 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
3671
Jeff Laytonc7f88c42017-12-11 06:35:12 -05003672 inode_set_iversion_queried(inode,
3673 btrfs_inode_sequence(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003674 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04003675 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003676 rdev = btrfs_inode_rdev(leaf, inode_item);
3677
Josef Bacikaec74772008-07-24 12:12:38 -04003678 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05003679 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Miao Xie67de1172013-12-26 13:07:06 +08003680
3681cache_index:
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003682 /*
3683 * If we were modified in the current generation and evicted from memory
3684 * and then re-read we need to do a full sync since we don't have any
3685 * idea about which extents were modified before we were evicted from
3686 * cache.
3687 *
3688 * This is required for both inode re-read from disk and delayed inode
3689 * in delayed_nodes_tree.
3690 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003691 if (BTRFS_I(inode)->last_trans == fs_info->generation)
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003692 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3693 &BTRFS_I(inode)->runtime_flags);
3694
Filipe Mananabde6c242015-07-24 00:00:19 +01003695 /*
3696 * We don't persist the id of the transaction where an unlink operation
3697 * against the inode was last made. So here we assume the inode might
3698 * have been evicted, and therefore the exact value of last_unlink_trans
3699 * lost, and set it to last_trans to avoid metadata inconsistencies
3700 * between the inode and its parent if the inode is fsync'ed and the log
3701 * replayed. For example, in the scenario:
3702 *
3703 * touch mydir/foo
3704 * ln mydir/foo mydir/bar
3705 * sync
3706 * unlink mydir/bar
3707 * echo 2 > /proc/sys/vm/drop_caches # evicts inode
3708 * xfs_io -c fsync mydir/foo
3709 * <power failure>
3710 * mount fs, triggers fsync log replay
3711 *
3712 * We must make sure that when we fsync our inode foo we also log its
3713 * parent inode, otherwise after log replay the parent still has the
3714 * dentry with the "bar" name but our inode foo has a link count of 1
3715 * and doesn't have an inode ref with the name "bar" anymore.
3716 *
3717 * Setting last_unlink_trans to last_trans is a pessimistic approach,
Nicholas D Steeves01327612016-05-19 21:18:45 -04003718 * but it guarantees correctness at the expense of occasional full
Filipe Mananabde6c242015-07-24 00:00:19 +01003719 * transaction commits on fsync if our inode is a directory, or if our
3720 * inode is not a directory, logging its parent unnecessarily.
3721 */
3722 BTRFS_I(inode)->last_unlink_trans = BTRFS_I(inode)->last_trans;
3723
Miao Xie67de1172013-12-26 13:07:06 +08003724 path->slots[0]++;
3725 if (inode->i_nlink != 1 ||
3726 path->slots[0] >= btrfs_header_nritems(leaf))
3727 goto cache_acl;
3728
3729 btrfs_item_key_to_cpu(leaf, &location, path->slots[0]);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003730 if (location.objectid != btrfs_ino(BTRFS_I(inode)))
Miao Xie67de1172013-12-26 13:07:06 +08003731 goto cache_acl;
3732
3733 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3734 if (location.type == BTRFS_INODE_REF_KEY) {
3735 struct btrfs_inode_ref *ref;
3736
3737 ref = (struct btrfs_inode_ref *)ptr;
3738 BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref);
3739 } else if (location.type == BTRFS_INODE_EXTREF_KEY) {
3740 struct btrfs_inode_extref *extref;
3741
3742 extref = (struct btrfs_inode_extref *)ptr;
3743 BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf,
3744 extref);
3745 }
Miao Xie2f7e33d2011-06-23 07:27:13 +00003746cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04003747 /*
3748 * try to precache a NULL acl entry for files that don't have
3749 * any xattrs or acls
3750 */
Li Zefan33345d012011-04-20 10:31:50 +08003751 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
David Sterbaf85b7372017-01-20 14:54:07 +01003752 btrfs_ino(BTRFS_I(inode)), &first_xattr_slot);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003753 if (first_xattr_slot != -1) {
3754 path->slots[0] = first_xattr_slot;
3755 ret = btrfs_load_inode_props(inode, path);
3756 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003757 btrfs_err(fs_info,
David Sterba351fd352014-05-15 16:48:20 +02003758 "error loading props for ino %llu (root %llu): %d",
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003759 btrfs_ino(BTRFS_I(inode)),
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003760 root->root_key.objectid, ret);
3761 }
Filipe Manana4222ea72018-10-24 10:13:03 +01003762 if (path != in_path)
3763 btrfs_free_path(path);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003764
Al Viro72c04902009-06-24 16:58:48 -04003765 if (!maybe_acls)
3766 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04003767
Chris Mason39279cc2007-06-12 06:35:45 -04003768 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04003769 case S_IFREG:
3770 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masond1310b22008-01-24 16:13:08 -05003771 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04003772 inode->i_fop = &btrfs_file_operations;
3773 inode->i_op = &btrfs_file_inode_operations;
3774 break;
3775 case S_IFDIR:
3776 inode->i_fop = &btrfs_dir_file_operations;
Omar Sandoval67ade052017-01-25 17:06:38 -08003777 inode->i_op = &btrfs_dir_inode_operations;
Chris Mason39279cc2007-06-12 06:35:45 -04003778 break;
3779 case S_IFLNK:
3780 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -05003781 inode_nohighmem(inode);
Omar Sandoval4779cc02018-09-24 15:16:55 -07003782 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason39279cc2007-06-12 06:35:45 -04003783 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04003784 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05003785 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04003786 init_special_inode(inode, inode->i_mode, rdev);
3787 break;
Chris Mason39279cc2007-06-12 06:35:45 -04003788 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003789
David Sterba7b6a2212018-03-26 18:40:21 +02003790 btrfs_sync_inode_flags_to_i_flags(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003791 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003792}
3793
Chris Masond352ac62008-09-29 15:18:18 -04003794/*
3795 * given a leaf and an inode, copy the inode fields into the leaf
3796 */
Chris Masone02119d2008-09-05 16:13:11 -04003797static void fill_inode_item(struct btrfs_trans_handle *trans,
3798 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04003799 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04003800 struct inode *inode)
3801{
Liu Bo51fab692012-12-27 09:01:21 +00003802 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003803
Liu Bo51fab692012-12-27 09:01:21 +00003804 btrfs_init_map_token(&token);
Chris Mason5f39d392007-10-15 16:14:19 -04003805
Liu Bo51fab692012-12-27 09:01:21 +00003806 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
3807 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
3808 btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size,
3809 &token);
3810 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3811 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003812
David Sterbaa937b972014-12-12 17:39:12 +01003813 btrfs_set_token_timespec_sec(leaf, &item->atime,
Liu Bo51fab692012-12-27 09:01:21 +00003814 inode->i_atime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003815 btrfs_set_token_timespec_nsec(leaf, &item->atime,
Liu Bo51fab692012-12-27 09:01:21 +00003816 inode->i_atime.tv_nsec, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003817
David Sterbaa937b972014-12-12 17:39:12 +01003818 btrfs_set_token_timespec_sec(leaf, &item->mtime,
Liu Bo51fab692012-12-27 09:01:21 +00003819 inode->i_mtime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003820 btrfs_set_token_timespec_nsec(leaf, &item->mtime,
Liu Bo51fab692012-12-27 09:01:21 +00003821 inode->i_mtime.tv_nsec, &token);
3822
David Sterbaa937b972014-12-12 17:39:12 +01003823 btrfs_set_token_timespec_sec(leaf, &item->ctime,
Liu Bo51fab692012-12-27 09:01:21 +00003824 inode->i_ctime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003825 btrfs_set_token_timespec_nsec(leaf, &item->ctime,
Liu Bo51fab692012-12-27 09:01:21 +00003826 inode->i_ctime.tv_nsec, &token);
3827
chandan r9cc97d62012-07-04 12:48:07 +05303828 btrfs_set_token_timespec_sec(leaf, &item->otime,
3829 BTRFS_I(inode)->i_otime.tv_sec, &token);
3830 btrfs_set_token_timespec_nsec(leaf, &item->otime,
3831 BTRFS_I(inode)->i_otime.tv_nsec, &token);
3832
Liu Bo51fab692012-12-27 09:01:21 +00003833 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3834 &token);
3835 btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation,
3836 &token);
Jeff Laytonc7f88c42017-12-11 06:35:12 -05003837 btrfs_set_token_inode_sequence(leaf, item, inode_peek_iversion(inode),
3838 &token);
Liu Bo51fab692012-12-27 09:01:21 +00003839 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
3840 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3841 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3842 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
Chris Mason39279cc2007-06-12 06:35:45 -04003843}
3844
Chris Masond352ac62008-09-29 15:18:18 -04003845/*
3846 * copy everything in the in-memory inode into the btree.
3847 */
Chris Mason21151332011-11-10 20:39:08 -05003848static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Chris Masond3977122009-01-05 21:25:51 -05003849 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003850{
3851 struct btrfs_inode_item *inode_item;
3852 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003853 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003854 int ret;
3855
3856 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08003857 if (!path)
3858 return -ENOMEM;
3859
Chris Masonb9473432009-03-13 11:00:37 -04003860 path->leave_spinning = 1;
Miao Xie16cdcec2011-04-22 18:12:22 +08003861 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
3862 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003863 if (ret) {
3864 if (ret > 0)
3865 ret = -ENOENT;
3866 goto failed;
3867 }
3868
Chris Mason5f39d392007-10-15 16:14:19 -04003869 leaf = path->nodes[0];
3870 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08003871 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04003872
Chris Masone02119d2008-09-05 16:13:11 -04003873 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04003874 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04003875 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003876 ret = 0;
3877failed:
Chris Mason39279cc2007-06-12 06:35:45 -04003878 btrfs_free_path(path);
3879 return ret;
3880}
3881
Chris Masond352ac62008-09-29 15:18:18 -04003882/*
Chris Mason21151332011-11-10 20:39:08 -05003883 * copy everything in the in-memory inode into the btree.
3884 */
3885noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
3886 struct btrfs_root *root, struct inode *inode)
3887{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003888 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason21151332011-11-10 20:39:08 -05003889 int ret;
3890
3891 /*
3892 * If the inode is a free space inode, we can deadlock during commit
3893 * if we put it into the delayed code.
3894 *
3895 * The data relocation inode should also be directly updated
3896 * without delay
3897 */
Nikolay Borisov70ddc552017-02-20 13:50:35 +02003898 if (!btrfs_is_free_space_inode(BTRFS_I(inode))
Josef Bacik1d52c782014-09-18 11:30:44 -04003899 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003900 && !test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) {
Alexander Block8ea05e32012-07-25 17:35:53 +02003901 btrfs_update_root_times(trans, root);
3902
Chris Mason21151332011-11-10 20:39:08 -05003903 ret = btrfs_delayed_update_inode(trans, root, inode);
3904 if (!ret)
3905 btrfs_set_inode_last_trans(trans, inode);
3906 return ret;
3907 }
3908
3909 return btrfs_update_inode_item(trans, root, inode);
3910}
3911
Josef Bacikbe6aef62012-10-22 15:43:12 -04003912noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
3913 struct btrfs_root *root,
3914 struct inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05003915{
3916 int ret;
3917
3918 ret = btrfs_update_inode(trans, root, inode);
3919 if (ret == -ENOSPC)
3920 return btrfs_update_inode_item(trans, root, inode);
3921 return ret;
3922}
3923
3924/*
Chris Masond352ac62008-09-29 15:18:18 -04003925 * unlink helper that gets used here in inode.c and in the tree logging
3926 * recovery code. It remove a link in a directory with a given name, and
3927 * also drops the back refs in the inode to the directory
3928 */
Al Viro92986792011-03-04 17:14:37 +00003929static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3930 struct btrfs_root *root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003931 struct btrfs_inode *dir,
3932 struct btrfs_inode *inode,
Al Viro92986792011-03-04 17:14:37 +00003933 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04003934{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003935 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04003936 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04003937 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003938 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003939 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04003940 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04003941 u64 index;
Li Zefan33345d012011-04-20 10:31:50 +08003942 u64 ino = btrfs_ino(inode);
3943 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04003944
3945 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04003946 if (!path) {
3947 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00003948 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04003949 }
3950
Chris Masonb9473432009-03-13 11:00:37 -04003951 path->leave_spinning = 1;
Li Zefan33345d012011-04-20 10:31:50 +08003952 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04003953 name, name_len, -1);
Liu Bo3cf50682018-09-12 06:06:26 +08003954 if (IS_ERR_OR_NULL(di)) {
3955 ret = di ? PTR_ERR(di) : -ENOENT;
Chris Mason39279cc2007-06-12 06:35:45 -04003956 goto err;
3957 }
Chris Mason5f39d392007-10-15 16:14:19 -04003958 leaf = path->nodes[0];
3959 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04003960 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04003961 if (ret)
3962 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02003963 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003964
Miao Xie67de1172013-12-26 13:07:06 +08003965 /*
3966 * If we don't have dir index, we have to get it by looking up
3967 * the inode ref, since we get the inode ref, remove it directly,
3968 * it is unnecessary to do delayed deletion.
3969 *
3970 * But if we have dir index, needn't search inode ref to get it.
3971 * Since the inode ref is close to the inode item, it is better
3972 * that we delay to delete it, and just do this deletion when
3973 * we update the inode item.
3974 */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003975 if (inode->dir_index) {
Miao Xie67de1172013-12-26 13:07:06 +08003976 ret = btrfs_delayed_delete_inode_ref(inode);
3977 if (!ret) {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003978 index = inode->dir_index;
Miao Xie67de1172013-12-26 13:07:06 +08003979 goto skip_backref;
3980 }
3981 }
3982
Li Zefan33345d012011-04-20 10:31:50 +08003983 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
3984 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04003985 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003986 btrfs_info(fs_info,
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003987 "failed to delete reference to %.*s, inode %llu parent %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003988 name_len, name, ino, dir_ino);
Jeff Mahoney66642832016-06-10 18:19:25 -04003989 btrfs_abort_transaction(trans, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04003990 goto err;
3991 }
Miao Xie67de1172013-12-26 13:07:06 +08003992skip_backref:
Lu Fengqi9add2942018-08-01 11:32:26 +08003993 ret = btrfs_delete_delayed_dir_index(trans, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003994 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003995 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003996 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003997 }
Chris Mason39279cc2007-06-12 06:35:45 -04003998
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003999 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len, inode,
4000 dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004001 if (ret != 0 && ret != -ENOENT) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004002 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004003 goto err;
4004 }
Chris Masone02119d2008-09-05 16:13:11 -04004005
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004006 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len, dir,
4007 index);
Chris Mason6418c962010-10-30 07:34:24 -04004008 if (ret == -ENOENT)
4009 ret = 0;
Zach Brownd4e39912013-04-02 21:02:16 +00004010 else if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004011 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04004012err:
4013 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04004014 if (ret)
4015 goto out;
4016
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004017 btrfs_i_size_write(dir, dir->vfs_inode.i_size - name_len * 2);
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004018 inode_inc_iversion(&inode->vfs_inode);
4019 inode_inc_iversion(&dir->vfs_inode);
4020 inode->vfs_inode.i_ctime = dir->vfs_inode.i_mtime =
4021 dir->vfs_inode.i_ctime = current_time(&inode->vfs_inode);
4022 ret = btrfs_update_inode(trans, root, &dir->vfs_inode);
Chris Masone02119d2008-09-05 16:13:11 -04004023out:
Chris Mason39279cc2007-06-12 06:35:45 -04004024 return ret;
4025}
4026
Al Viro92986792011-03-04 17:14:37 +00004027int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
4028 struct btrfs_root *root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004029 struct btrfs_inode *dir, struct btrfs_inode *inode,
Al Viro92986792011-03-04 17:14:37 +00004030 const char *name, int name_len)
4031{
4032 int ret;
4033 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
4034 if (!ret) {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004035 drop_nlink(&inode->vfs_inode);
4036 ret = btrfs_update_inode(trans, root, &inode->vfs_inode);
Al Viro92986792011-03-04 17:14:37 +00004037 }
4038 return ret;
4039}
Yan, Zhenga22285a2010-05-16 10:48:46 -04004040
4041/*
4042 * helper to start transaction for unlink and rmdir.
4043 *
Josef Bacikd52be812013-05-29 14:54:47 -04004044 * unlink and rmdir are special in btrfs, they do not always free space, so
4045 * if we cannot make our reservations the normal way try and see if there is
4046 * plenty of slack room in the global reserve to migrate, otherwise we cannot
4047 * allow the unlink to occur.
Yan, Zhenga22285a2010-05-16 10:48:46 -04004048 */
Josef Bacikd52be812013-05-29 14:54:47 -04004049static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir)
Yan, Zhenga22285a2010-05-16 10:48:46 -04004050{
Yan, Zhenga22285a2010-05-16 10:48:46 -04004051 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004052
Josef Bacike70bea52011-10-11 14:18:24 -04004053 /*
4054 * 1 for the possible orphan item
4055 * 1 for the dir item
4056 * 1 for the dir index
4057 * 1 for the inode ref
Josef Bacike70bea52011-10-11 14:18:24 -04004058 * 1 for the inode
4059 */
Filipe Manana8eab77f2015-11-13 23:57:16 +00004060 return btrfs_start_transaction_fallback_global_rsv(root, 5, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004061}
4062
Chris Mason39279cc2007-06-12 06:35:45 -04004063static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
4064{
Yan, Zhenga22285a2010-05-16 10:48:46 -04004065 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004066 struct btrfs_trans_handle *trans;
David Howells2b0143b2015-03-17 22:25:59 +00004067 struct inode *inode = d_inode(dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04004068 int ret;
4069
Josef Bacikd52be812013-05-29 14:54:47 -04004070 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004071 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004072 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04004073
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004074 btrfs_record_unlink_dir(trans, BTRFS_I(dir), BTRFS_I(d_inode(dentry)),
4075 0);
Chris Mason12fcfd22009-03-24 10:24:20 -04004076
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004077 ret = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
4078 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
4079 dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00004080 if (ret)
4081 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004082
Yan, Zhenga22285a2010-05-16 10:48:46 -04004083 if (inode->i_nlink == 0) {
Nikolay Borisov73f2e542017-02-20 13:50:59 +02004084 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Tsutomu Itohb5324022011-07-19 07:27:20 +00004085 if (ret)
4086 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004087 }
Josef Bacik7b128762008-07-24 12:17:14 -04004088
Tsutomu Itohb5324022011-07-19 07:27:20 +00004089out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004090 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004091 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04004092 return ret;
4093}
4094
Misono Tomohirof60a2362018-04-18 11:34:52 +09004095static int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
Lu Fengqi401b3b12018-08-01 11:32:30 +08004096 struct inode *dir, u64 objectid,
4097 const char *name, int name_len)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004098{
Lu Fengqi401b3b12018-08-01 11:32:30 +08004099 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004100 struct btrfs_path *path;
4101 struct extent_buffer *leaf;
4102 struct btrfs_dir_item *di;
4103 struct btrfs_key key;
4104 u64 index;
4105 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004106 u64 dir_ino = btrfs_ino(BTRFS_I(dir));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004107
4108 path = btrfs_alloc_path();
4109 if (!path)
4110 return -ENOMEM;
4111
Li Zefan33345d012011-04-20 10:31:50 +08004112 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004113 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004114 if (IS_ERR_OR_NULL(di)) {
Liu Bo3cf50682018-09-12 06:06:26 +08004115 ret = di ? PTR_ERR(di) : -ENOENT;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004116 goto out;
4117 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004118
4119 leaf = path->nodes[0];
4120 btrfs_dir_item_key_to_cpu(leaf, di, &key);
4121 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
4122 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004123 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004124 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004125 goto out;
4126 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004127 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004128
Lu Fengqi3ee1c552018-08-01 11:32:28 +08004129 ret = btrfs_del_root_ref(trans, objectid, root->root_key.objectid,
4130 dir_ino, &index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004131 if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004132 if (ret != -ENOENT) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004133 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004134 goto out;
4135 }
Li Zefan33345d012011-04-20 10:31:50 +08004136 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004137 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004138 if (IS_ERR_OR_NULL(di)) {
4139 if (!di)
4140 ret = -ENOENT;
4141 else
4142 ret = PTR_ERR(di);
Jeff Mahoney66642832016-06-10 18:19:25 -04004143 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004144 goto out;
4145 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004146
4147 leaf = path->nodes[0];
4148 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004149 index = key.offset;
4150 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004151 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004152
Lu Fengqi9add2942018-08-01 11:32:26 +08004153 ret = btrfs_delete_delayed_dir_index(trans, BTRFS_I(dir), index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004154 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004155 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004156 goto out;
4157 }
Miao Xie16cdcec2011-04-22 18:12:22 +08004158
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004159 btrfs_i_size_write(BTRFS_I(dir), dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004160 inode_inc_iversion(dir);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07004161 dir->i_mtime = dir->i_ctime = current_time(dir);
Josef Bacik5a24e842012-08-08 10:12:59 -06004162 ret = btrfs_update_inode_fallback(trans, root, dir);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004163 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004164 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004165out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04004166 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004167 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004168}
4169
Misono Tomohiroec42f162018-04-18 11:34:13 +09004170/*
4171 * Helper to check if the subvolume references other subvolumes or if it's
4172 * default.
4173 */
Misono Tomohirof60a2362018-04-18 11:34:52 +09004174static noinline int may_destroy_subvol(struct btrfs_root *root)
Misono Tomohiroec42f162018-04-18 11:34:13 +09004175{
4176 struct btrfs_fs_info *fs_info = root->fs_info;
4177 struct btrfs_path *path;
4178 struct btrfs_dir_item *di;
4179 struct btrfs_key key;
4180 u64 dir_id;
4181 int ret;
4182
4183 path = btrfs_alloc_path();
4184 if (!path)
4185 return -ENOMEM;
4186
4187 /* Make sure this root isn't set as the default subvol */
4188 dir_id = btrfs_super_root_dir(fs_info->super_copy);
4189 di = btrfs_lookup_dir_item(NULL, fs_info->tree_root, path,
4190 dir_id, "default", 7, 0);
4191 if (di && !IS_ERR(di)) {
4192 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key);
4193 if (key.objectid == root->root_key.objectid) {
4194 ret = -EPERM;
4195 btrfs_err(fs_info,
4196 "deleting default subvolume %llu is not allowed",
4197 key.objectid);
4198 goto out;
4199 }
4200 btrfs_release_path(path);
4201 }
4202
4203 key.objectid = root->root_key.objectid;
4204 key.type = BTRFS_ROOT_REF_KEY;
4205 key.offset = (u64)-1;
4206
4207 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
4208 if (ret < 0)
4209 goto out;
4210 BUG_ON(ret == 0);
4211
4212 ret = 0;
4213 if (path->slots[0] > 0) {
4214 path->slots[0]--;
4215 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
4216 if (key.objectid == root->root_key.objectid &&
4217 key.type == BTRFS_ROOT_REF_KEY)
4218 ret = -ENOTEMPTY;
4219 }
4220out:
4221 btrfs_free_path(path);
4222 return ret;
4223}
4224
Nikolay Borisov20a68002018-04-27 14:36:24 +03004225/* Delete all dentries for inodes belonging to the root */
4226static void btrfs_prune_dentries(struct btrfs_root *root)
4227{
4228 struct btrfs_fs_info *fs_info = root->fs_info;
4229 struct rb_node *node;
4230 struct rb_node *prev;
4231 struct btrfs_inode *entry;
4232 struct inode *inode;
4233 u64 objectid = 0;
4234
4235 if (!test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
4236 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
4237
4238 spin_lock(&root->inode_lock);
4239again:
4240 node = root->inode_tree.rb_node;
4241 prev = NULL;
4242 while (node) {
4243 prev = node;
4244 entry = rb_entry(node, struct btrfs_inode, rb_node);
4245
David Sterba37508512018-06-29 10:56:40 +02004246 if (objectid < btrfs_ino(entry))
Nikolay Borisov20a68002018-04-27 14:36:24 +03004247 node = node->rb_left;
David Sterba37508512018-06-29 10:56:40 +02004248 else if (objectid > btrfs_ino(entry))
Nikolay Borisov20a68002018-04-27 14:36:24 +03004249 node = node->rb_right;
4250 else
4251 break;
4252 }
4253 if (!node) {
4254 while (prev) {
4255 entry = rb_entry(prev, struct btrfs_inode, rb_node);
David Sterba37508512018-06-29 10:56:40 +02004256 if (objectid <= btrfs_ino(entry)) {
Nikolay Borisov20a68002018-04-27 14:36:24 +03004257 node = prev;
4258 break;
4259 }
4260 prev = rb_next(prev);
4261 }
4262 }
4263 while (node) {
4264 entry = rb_entry(node, struct btrfs_inode, rb_node);
David Sterba37508512018-06-29 10:56:40 +02004265 objectid = btrfs_ino(entry) + 1;
Nikolay Borisov20a68002018-04-27 14:36:24 +03004266 inode = igrab(&entry->vfs_inode);
4267 if (inode) {
4268 spin_unlock(&root->inode_lock);
4269 if (atomic_read(&inode->i_count) > 1)
4270 d_prune_aliases(inode);
4271 /*
4272 * btrfs_drop_inode will have it removed from the inode
4273 * cache when its usage count hits zero.
4274 */
4275 iput(inode);
4276 cond_resched();
4277 spin_lock(&root->inode_lock);
4278 goto again;
4279 }
4280
4281 if (cond_resched_lock(&root->inode_lock))
4282 goto again;
4283
4284 node = rb_next(node);
4285 }
4286 spin_unlock(&root->inode_lock);
4287}
4288
Misono Tomohirof60a2362018-04-18 11:34:52 +09004289int btrfs_delete_subvolume(struct inode *dir, struct dentry *dentry)
4290{
4291 struct btrfs_fs_info *fs_info = btrfs_sb(dentry->d_sb);
4292 struct btrfs_root *root = BTRFS_I(dir)->root;
4293 struct inode *inode = d_inode(dentry);
4294 struct btrfs_root *dest = BTRFS_I(inode)->root;
4295 struct btrfs_trans_handle *trans;
4296 struct btrfs_block_rsv block_rsv;
4297 u64 root_flags;
Misono Tomohirof60a2362018-04-18 11:34:52 +09004298 int ret;
4299 int err;
4300
4301 /*
4302 * Don't allow to delete a subvolume with send in progress. This is
4303 * inside the inode lock so the error handling that has to drop the bit
4304 * again is not run concurrently.
4305 */
4306 spin_lock(&dest->root_item_lock);
Lu Fengqia7176f72018-08-04 21:10:53 +08004307 if (dest->send_in_progress) {
Misono Tomohirof60a2362018-04-18 11:34:52 +09004308 spin_unlock(&dest->root_item_lock);
4309 btrfs_warn(fs_info,
4310 "attempt to delete subvolume %llu during send",
4311 dest->root_key.objectid);
4312 return -EPERM;
4313 }
Lu Fengqia7176f72018-08-04 21:10:53 +08004314 root_flags = btrfs_root_flags(&dest->root_item);
4315 btrfs_set_root_flags(&dest->root_item,
4316 root_flags | BTRFS_ROOT_SUBVOL_DEAD);
4317 spin_unlock(&dest->root_item_lock);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004318
4319 down_write(&fs_info->subvol_sem);
4320
4321 err = may_destroy_subvol(dest);
4322 if (err)
4323 goto out_up_write;
4324
4325 btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP);
4326 /*
4327 * One for dir inode,
4328 * two for dir entries,
4329 * two for root ref/backref.
4330 */
Gu JinXiangc4c129d2018-05-30 11:00:38 +08004331 err = btrfs_subvolume_reserve_metadata(root, &block_rsv, 5, true);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004332 if (err)
4333 goto out_up_write;
4334
4335 trans = btrfs_start_transaction(root, 0);
4336 if (IS_ERR(trans)) {
4337 err = PTR_ERR(trans);
4338 goto out_release;
4339 }
4340 trans->block_rsv = &block_rsv;
4341 trans->bytes_reserved = block_rsv.size;
4342
4343 btrfs_record_snapshot_destroy(trans, BTRFS_I(dir));
4344
Lu Fengqi401b3b12018-08-01 11:32:30 +08004345 ret = btrfs_unlink_subvol(trans, dir, dest->root_key.objectid,
4346 dentry->d_name.name, dentry->d_name.len);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004347 if (ret) {
4348 err = ret;
4349 btrfs_abort_transaction(trans, ret);
4350 goto out_end_trans;
4351 }
4352
4353 btrfs_record_root_in_trans(trans, dest);
4354
4355 memset(&dest->root_item.drop_progress, 0,
4356 sizeof(dest->root_item.drop_progress));
4357 dest->root_item.drop_level = 0;
4358 btrfs_set_root_refs(&dest->root_item, 0);
4359
4360 if (!test_and_set_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &dest->state)) {
4361 ret = btrfs_insert_orphan_item(trans,
4362 fs_info->tree_root,
4363 dest->root_key.objectid);
4364 if (ret) {
4365 btrfs_abort_transaction(trans, ret);
4366 err = ret;
4367 goto out_end_trans;
4368 }
4369 }
4370
Lu Fengqid1957792018-05-29 15:01:54 +08004371 ret = btrfs_uuid_tree_remove(trans, dest->root_item.uuid,
Misono Tomohirof60a2362018-04-18 11:34:52 +09004372 BTRFS_UUID_KEY_SUBVOL,
4373 dest->root_key.objectid);
4374 if (ret && ret != -ENOENT) {
4375 btrfs_abort_transaction(trans, ret);
4376 err = ret;
4377 goto out_end_trans;
4378 }
4379 if (!btrfs_is_empty_uuid(dest->root_item.received_uuid)) {
Lu Fengqid1957792018-05-29 15:01:54 +08004380 ret = btrfs_uuid_tree_remove(trans,
Misono Tomohirof60a2362018-04-18 11:34:52 +09004381 dest->root_item.received_uuid,
4382 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
4383 dest->root_key.objectid);
4384 if (ret && ret != -ENOENT) {
4385 btrfs_abort_transaction(trans, ret);
4386 err = ret;
4387 goto out_end_trans;
4388 }
4389 }
4390
4391out_end_trans:
4392 trans->block_rsv = NULL;
4393 trans->bytes_reserved = 0;
4394 ret = btrfs_end_transaction(trans);
4395 if (ret && !err)
4396 err = ret;
4397 inode->i_flags |= S_DEAD;
4398out_release:
4399 btrfs_subvolume_release_metadata(fs_info, &block_rsv);
4400out_up_write:
4401 up_write(&fs_info->subvol_sem);
4402 if (err) {
4403 spin_lock(&dest->root_item_lock);
4404 root_flags = btrfs_root_flags(&dest->root_item);
4405 btrfs_set_root_flags(&dest->root_item,
4406 root_flags & ~BTRFS_ROOT_SUBVOL_DEAD);
4407 spin_unlock(&dest->root_item_lock);
4408 } else {
4409 d_invalidate(dentry);
Nikolay Borisov20a68002018-04-27 14:36:24 +03004410 btrfs_prune_dentries(dest);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004411 ASSERT(dest->send_in_progress == 0);
4412
4413 /* the last ref */
4414 if (dest->ino_cache_inode) {
4415 iput(dest->ino_cache_inode);
4416 dest->ino_cache_inode = NULL;
4417 }
4418 }
4419
4420 return err;
4421}
4422
Chris Mason39279cc2007-06-12 06:35:45 -04004423static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
4424{
David Howells2b0143b2015-03-17 22:25:59 +00004425 struct inode *inode = d_inode(dentry);
Chris Mason1832a6d2007-12-21 16:27:21 -05004426 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004427 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004428 struct btrfs_trans_handle *trans;
Filipe Manana44f714d2016-06-06 16:11:13 +01004429 u64 last_unlink_trans;
Chris Mason39279cc2007-06-12 06:35:45 -04004430
David Sterbab3ae2442012-09-13 16:04:34 -06004431 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Yan134d4512007-10-25 15:49:25 -04004432 return -ENOTEMPTY;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004433 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_FIRST_FREE_OBJECTID)
Misono Tomohiroa79a4642018-04-18 11:35:31 +09004434 return btrfs_delete_subvolume(dir, dentry);
Yan134d4512007-10-25 15:49:25 -04004435
Josef Bacikd52be812013-05-29 14:54:47 -04004436 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004437 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004438 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004439
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004440 if (unlikely(btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Lu Fengqi401b3b12018-08-01 11:32:30 +08004441 err = btrfs_unlink_subvol(trans, dir,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004442 BTRFS_I(inode)->location.objectid,
4443 dentry->d_name.name,
4444 dentry->d_name.len);
4445 goto out;
4446 }
4447
Nikolay Borisov73f2e542017-02-20 13:50:59 +02004448 err = btrfs_orphan_add(trans, BTRFS_I(inode));
Josef Bacik7b128762008-07-24 12:17:14 -04004449 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004450 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004451
Filipe Manana44f714d2016-06-06 16:11:13 +01004452 last_unlink_trans = BTRFS_I(inode)->last_unlink_trans;
4453
Chris Mason39279cc2007-06-12 06:35:45 -04004454 /* now the directory is empty */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004455 err = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
4456 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
4457 dentry->d_name.len);
Filipe Manana44f714d2016-06-06 16:11:13 +01004458 if (!err) {
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004459 btrfs_i_size_write(BTRFS_I(inode), 0);
Filipe Manana44f714d2016-06-06 16:11:13 +01004460 /*
4461 * Propagate the last_unlink_trans value of the deleted dir to
4462 * its parent directory. This is to prevent an unrecoverable
4463 * log tree in the case we do something like this:
4464 * 1) create dir foo
4465 * 2) create snapshot under dir foo
4466 * 3) delete the snapshot
4467 * 4) rmdir foo
4468 * 5) mkdir foo
4469 * 6) fsync foo or some file inside foo
4470 */
4471 if (last_unlink_trans >= trans->transid)
4472 BTRFS_I(dir)->last_unlink_trans = last_unlink_trans;
4473 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004474out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004475 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004476 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39544012007-12-12 14:38:19 -05004477
Chris Mason39279cc2007-06-12 06:35:45 -04004478 return err;
4479}
4480
Josef Bacikddfae632017-10-19 14:16:02 -04004481/*
4482 * Return this if we need to call truncate_block for the last bit of the
4483 * truncate.
4484 */
4485#define NEED_TRUNCATE_BLOCK 1
Filipe Manana0305cd52015-10-16 12:34:25 +01004486
Chris Mason323ac952008-10-01 19:05:46 -04004487/*
Chris Mason39279cc2007-06-12 06:35:45 -04004488 * this can truncate away extent items, csum items and directory items.
4489 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04004490 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04004491 *
4492 * csum items that cross the new i_size are truncated to the new size
4493 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04004494 *
4495 * min_type is the minimum key type to truncate down to. If set to 0, this
4496 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04004497 */
Yan, Zheng80825102009-11-12 09:35:36 +00004498int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
4499 struct btrfs_root *root,
4500 struct inode *inode,
4501 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004502{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004503 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04004504 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004505 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004506 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00004507 struct btrfs_key key;
4508 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04004509 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04004510 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004511 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004512 u64 item_end = 0;
Filipe Mananac1aa4572015-06-20 18:20:09 +01004513 u64 last_size = new_size;
Yan, Zheng80825102009-11-12 09:35:36 +00004514 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04004515 int found_extent;
4516 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05004517 int pending_del_nr = 0;
4518 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04004519 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00004520 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004521 u64 ino = btrfs_ino(BTRFS_I(inode));
Chris Mason28ed1342014-12-17 09:41:04 -08004522 u64 bytes_deleted = 0;
Thomas Meyer897ca812017-10-07 16:02:21 +02004523 bool be_nice = false;
4524 bool should_throttle = false;
Yan, Zheng80825102009-11-12 09:35:36 +00004525
4526 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04004527
Chris Mason28ed1342014-12-17 09:41:04 -08004528 /*
4529 * for non-free space inodes and ref cows, we want to back off from
4530 * time to time
4531 */
Nikolay Borisov70ddc552017-02-20 13:50:35 +02004532 if (!btrfs_is_free_space_inode(BTRFS_I(inode)) &&
Chris Mason28ed1342014-12-17 09:41:04 -08004533 test_bit(BTRFS_ROOT_REF_COWS, &root->state))
Thomas Meyer897ca812017-10-07 16:02:21 +02004534 be_nice = true;
Chris Mason28ed1342014-12-17 09:41:04 -08004535
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004536 path = btrfs_alloc_path();
4537 if (!path)
4538 return -ENOMEM;
David Sterbae4058b52015-11-27 16:31:35 +01004539 path->reada = READA_BACK;
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004540
Josef Bacik5dc562c2012-08-17 13:14:17 -04004541 /*
4542 * We want to drop from the next block forward in case this new size is
4543 * not block aligned since we will be keeping the last block of the
4544 * extent just the way it is.
4545 */
Miao Xie27cdeb72014-04-02 19:51:05 +08004546 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004547 root == fs_info->tree_root)
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02004548 btrfs_drop_extent_cache(BTRFS_I(inode), ALIGN(new_size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004549 fs_info->sectorsize),
Jeff Mahoneyda170662016-06-15 09:22:56 -04004550 (u64)-1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00004551
Miao Xie16cdcec2011-04-22 18:12:22 +08004552 /*
4553 * This function is also used to drop the items in the log tree before
4554 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
Andrea Gelmini52042d82018-11-28 12:05:13 +01004555 * it is used to drop the logged items. So we shouldn't kill the delayed
Miao Xie16cdcec2011-04-22 18:12:22 +08004556 * items.
4557 */
4558 if (min_type == 0 && root == BTRFS_I(inode)->root)
Nikolay Borisov4ccb5c72017-01-10 20:35:38 +02004559 btrfs_kill_delayed_inode_items(BTRFS_I(inode));
Miao Xie16cdcec2011-04-22 18:12:22 +08004560
Li Zefan33345d012011-04-20 10:31:50 +08004561 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04004562 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04004563 key.type = (u8)-1;
4564
Chris Mason85e21ba2008-01-29 15:11:36 -05004565search_again:
Chris Mason28ed1342014-12-17 09:41:04 -08004566 /*
4567 * with a 16K leaf size and 128MB extents, you can actually queue
4568 * up a huge file in a single leaf. Most of the time that
4569 * bytes_deleted is > 0, it will be huge by the time we get here
4570 */
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004571 if (be_nice && bytes_deleted > SZ_32M &&
4572 btrfs_should_end_transaction(trans)) {
4573 ret = -EAGAIN;
Yan, Zheng80825102009-11-12 09:35:36 +00004574 goto out;
4575 }
Chris Masond3977122009-01-05 21:25:51 -05004576
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004577 path->leave_spinning = 1;
4578 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
4579 if (ret < 0)
4580 goto out;
4581
Chris Mason85e21ba2008-01-29 15:11:36 -05004582 if (ret > 0) {
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004583 ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -04004584 /* there are no items in the tree for us to truncate, we're
4585 * done
4586 */
Yan, Zheng80825102009-11-12 09:35:36 +00004587 if (path->slots[0] == 0)
4588 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05004589 path->slots[0]--;
4590 }
4591
Chris Masond3977122009-01-05 21:25:51 -05004592 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04004593 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04004594 leaf = path->nodes[0];
4595 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02004596 found_type = found_key.type;
Chris Mason39279cc2007-06-12 06:35:45 -04004597
Li Zefan33345d012011-04-20 10:31:50 +08004598 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04004599 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004600
Chris Mason85e21ba2008-01-29 15:11:36 -05004601 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004602 break;
4603
Chris Mason5f39d392007-10-15 16:14:19 -04004604 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04004605 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04004606 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04004607 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04004608 extent_type = btrfs_file_extent_type(leaf, fi);
4609 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04004610 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04004611 btrfs_file_extent_num_bytes(leaf, fi);
Liu Bo09ed2f12017-03-10 11:09:48 -08004612
4613 trace_btrfs_truncate_show_fi_regular(
4614 BTRFS_I(inode), leaf, fi,
4615 found_key.offset);
Chris Mason179e29e2007-11-01 11:28:41 -04004616 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Qu Wenruoe41ca582018-06-06 15:41:49 +08004617 item_end += btrfs_file_extent_ram_bytes(leaf,
4618 fi);
Liu Bo09ed2f12017-03-10 11:09:48 -08004619
4620 trace_btrfs_truncate_show_fi_inline(
4621 BTRFS_I(inode), leaf, fi, path->slots[0],
4622 found_key.offset);
Chris Mason39279cc2007-06-12 06:35:45 -04004623 }
Yan008630c2007-11-07 13:31:09 -05004624 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04004625 }
Yan, Zheng80825102009-11-12 09:35:36 +00004626 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04004627 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00004628 } else {
Filipe Manana76b42ab2017-02-14 16:56:01 +00004629 if (item_end < new_size)
Yan, Zheng80825102009-11-12 09:35:36 +00004630 break;
4631 if (found_key.offset >= new_size)
4632 del_item = 1;
4633 else
4634 del_item = 0;
4635 }
Chris Mason39279cc2007-06-12 06:35:45 -04004636 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004637 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04004638 if (found_type != BTRFS_EXTENT_DATA_KEY)
4639 goto delete;
4640
4641 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04004642 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04004643 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05004644 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04004645 u64 orig_num_bytes =
4646 btrfs_file_extent_num_bytes(leaf, fi);
Qu Wenruofda28322013-02-26 08:10:22 +00004647 extent_num_bytes = ALIGN(new_size -
4648 found_key.offset,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004649 fs_info->sectorsize);
Chris Masondb945352007-10-15 16:15:53 -04004650 btrfs_set_file_extent_num_bytes(leaf, fi,
4651 extent_num_bytes);
4652 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05004653 extent_num_bytes);
Miao Xie27cdeb72014-04-02 19:51:05 +08004654 if (test_bit(BTRFS_ROOT_REF_COWS,
4655 &root->state) &&
4656 extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004657 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04004658 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004659 } else {
Chris Masondb945352007-10-15 16:15:53 -04004660 extent_num_bytes =
4661 btrfs_file_extent_disk_num_bytes(leaf,
4662 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004663 extent_offset = found_key.offset -
4664 btrfs_file_extent_offset(leaf, fi);
4665
Chris Mason39279cc2007-06-12 06:35:45 -04004666 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05004667 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004668 if (extent_start != 0) {
4669 found_extent = 1;
Miao Xie27cdeb72014-04-02 19:51:05 +08004670 if (test_bit(BTRFS_ROOT_REF_COWS,
4671 &root->state))
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004672 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04004673 }
4674 }
Chris Mason90692182008-02-08 13:49:28 -05004675 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04004676 /*
4677 * we can't truncate inline items that have had
4678 * special encodings
4679 */
4680 if (!del_item &&
Chris Masonc8b97812008-10-29 14:49:59 -04004681 btrfs_file_extent_encryption(leaf, fi) == 0 &&
Josef Bacikddfae632017-10-19 14:16:02 -04004682 btrfs_file_extent_other_encoding(leaf, fi) == 0 &&
4683 btrfs_file_extent_compression(leaf, fi) == 0) {
4684 u32 size = (u32)(new_size - found_key.offset);
Chris Mason514ac8a2014-01-03 21:07:00 -08004685
Josef Bacikddfae632017-10-19 14:16:02 -04004686 btrfs_set_file_extent_ram_bytes(leaf, fi, size);
4687 size = btrfs_file_extent_calc_inline_size(size);
David Sterba78ac4f92019-03-20 14:49:12 +01004688 btrfs_truncate_item(path, size, 1);
Josef Bacikddfae632017-10-19 14:16:02 -04004689 } else if (!del_item) {
Chris Mason514ac8a2014-01-03 21:07:00 -08004690 /*
Josef Bacikddfae632017-10-19 14:16:02 -04004691 * We have to bail so the last_size is set to
4692 * just before this extent.
Chris Mason514ac8a2014-01-03 21:07:00 -08004693 */
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004694 ret = NEED_TRUNCATE_BLOCK;
Josef Bacikddfae632017-10-19 14:16:02 -04004695 break;
Chris Mason90692182008-02-08 13:49:28 -05004696 }
Josef Bacikddfae632017-10-19 14:16:02 -04004697
4698 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state))
4699 inode_sub_bytes(inode, item_end + 1 - new_size);
Chris Mason39279cc2007-06-12 06:35:45 -04004700 }
Chris Mason179e29e2007-11-01 11:28:41 -04004701delete:
Josef Bacikddfae632017-10-19 14:16:02 -04004702 if (del_item)
4703 last_size = found_key.offset;
4704 else
4705 last_size = new_size;
Chris Mason39279cc2007-06-12 06:35:45 -04004706 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05004707 if (!pending_del_nr) {
4708 /* no pending yet, add ourselves */
4709 pending_del_slot = path->slots[0];
4710 pending_del_nr = 1;
4711 } else if (pending_del_nr &&
4712 path->slots[0] + 1 == pending_del_slot) {
4713 /* hop on the pending chunk */
4714 pending_del_nr++;
4715 pending_del_slot = path->slots[0];
4716 } else {
Chris Masond3977122009-01-05 21:25:51 -05004717 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05004718 }
Chris Mason39279cc2007-06-12 06:35:45 -04004719 } else {
4720 break;
4721 }
Thomas Meyer897ca812017-10-07 16:02:21 +02004722 should_throttle = false;
Chris Mason28f75a02015-02-04 06:59:29 -08004723
Miao Xie27cdeb72014-04-02 19:51:05 +08004724 if (found_extent &&
4725 (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004726 root == fs_info->tree_root)) {
Qu Wenruoffd4bb22019-04-04 14:45:36 +08004727 struct btrfs_ref ref = { 0 };
4728
Chris Masonb9473432009-03-13 11:00:37 -04004729 btrfs_set_path_blocking(path);
Chris Mason28ed1342014-12-17 09:41:04 -08004730 bytes_deleted += extent_num_bytes;
Qu Wenruoffd4bb22019-04-04 14:45:36 +08004731
4732 btrfs_init_generic_ref(&ref, BTRFS_DROP_DELAYED_REF,
4733 extent_start, extent_num_bytes, 0);
4734 ref.real_root = root->root_key.objectid;
4735 btrfs_init_data_ref(&ref, btrfs_header_owner(leaf),
4736 ino, extent_offset);
4737 ret = btrfs_free_extent(trans, &ref);
Omar Sandoval05522102018-05-11 13:13:31 -07004738 if (ret) {
4739 btrfs_abort_transaction(trans, ret);
4740 break;
4741 }
Chris Mason28f75a02015-02-04 06:59:29 -08004742 if (be_nice) {
Lu Fengqi7c861622018-10-11 13:40:36 +08004743 if (btrfs_should_throttle_delayed_refs(trans))
Thomas Meyer897ca812017-10-07 16:02:21 +02004744 should_throttle = true;
Chris Mason28f75a02015-02-04 06:59:29 -08004745 }
Chris Mason39279cc2007-06-12 06:35:45 -04004746 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004747
Yan, Zheng80825102009-11-12 09:35:36 +00004748 if (found_type == BTRFS_INODE_ITEM_KEY)
4749 break;
4750
4751 if (path->slots[0] == 0 ||
Josef Bacik12621332015-02-03 07:50:16 -08004752 path->slots[0] != pending_del_slot ||
Josef Bacik28bad212018-12-03 10:20:38 -05004753 should_throttle) {
Yan, Zheng80825102009-11-12 09:35:36 +00004754 if (pending_del_nr) {
4755 ret = btrfs_del_items(trans, root, path,
4756 pending_del_slot,
4757 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004758 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004759 btrfs_abort_transaction(trans, ret);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004760 break;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004761 }
Yan, Zheng80825102009-11-12 09:35:36 +00004762 pending_del_nr = 0;
4763 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004764 btrfs_release_path(path);
Josef Bacik28bad212018-12-03 10:20:38 -05004765
Chris Mason28f75a02015-02-04 06:59:29 -08004766 /*
Josef Bacik28bad212018-12-03 10:20:38 -05004767 * We can generate a lot of delayed refs, so we need to
4768 * throttle every once and a while and make sure we're
4769 * adding enough space to keep up with the work we are
4770 * generating. Since we hold a transaction here we
4771 * can't flush, and we don't want to FLUSH_LIMIT because
4772 * we could have generated too many delayed refs to
4773 * actually allocate, so just bail if we're short and
4774 * let the normal reservation dance happen higher up.
Chris Mason28f75a02015-02-04 06:59:29 -08004775 */
Josef Bacik28bad212018-12-03 10:20:38 -05004776 if (should_throttle) {
4777 ret = btrfs_delayed_refs_rsv_refill(fs_info,
4778 BTRFS_RESERVE_NO_FLUSH);
4779 if (ret) {
4780 ret = -EAGAIN;
4781 break;
4782 }
Chris Mason28f75a02015-02-04 06:59:29 -08004783 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004784 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00004785 } else {
4786 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05004787 }
Chris Mason39279cc2007-06-12 06:35:45 -04004788 }
Yan, Zheng80825102009-11-12 09:35:36 +00004789out:
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004790 if (ret >= 0 && pending_del_nr) {
4791 int err;
4792
4793 err = btrfs_del_items(trans, root, path, pending_del_slot,
Chris Mason85e21ba2008-01-29 15:11:36 -05004794 pending_del_nr);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004795 if (err) {
4796 btrfs_abort_transaction(trans, err);
4797 ret = err;
4798 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004799 }
Filipe Manana76b42ab2017-02-14 16:56:01 +00004800 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
4801 ASSERT(last_size >= new_size);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004802 if (!ret && last_size > new_size)
Filipe Manana76b42ab2017-02-14 16:56:01 +00004803 last_size = new_size;
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004804 btrfs_ordered_update_i_size(inode, last_size, NULL);
Filipe Manana76b42ab2017-02-14 16:56:01 +00004805 }
Chris Mason28ed1342014-12-17 09:41:04 -08004806
Chris Mason39279cc2007-06-12 06:35:45 -04004807 btrfs_free_path(path);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004808 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04004809}
4810
Chris Masona52d9a82007-08-27 16:49:44 -04004811/*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304812 * btrfs_truncate_block - read, zero a chunk and write a block
Josef Bacik2aaa6652012-08-29 14:27:18 -04004813 * @inode - inode that we're zeroing
4814 * @from - the offset to start zeroing
4815 * @len - the length to zero, 0 to zero the entire range respective to the
4816 * offset
4817 * @front - zero up to the offset instead of from the offset on
4818 *
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304819 * This will find the block for the "from" offset and cow the block and zero the
Josef Bacik2aaa6652012-08-29 14:27:18 -04004820 * part we want to zero. This is used with truncate and hole punching.
Chris Masona52d9a82007-08-27 16:49:44 -04004821 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304822int btrfs_truncate_block(struct inode *inode, loff_t from, loff_t len,
Josef Bacik2aaa6652012-08-29 14:27:18 -04004823 int front)
Chris Masona52d9a82007-08-27 16:49:44 -04004824{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004825 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004826 struct address_space *mapping = inode->i_mapping;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004827 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4828 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004829 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08004830 struct extent_changeset *data_reserved = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004831 char *kaddr;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004832 u32 blocksize = fs_info->sectorsize;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004833 pgoff_t index = from >> PAGE_SHIFT;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304834 unsigned offset = from & (blocksize - 1);
Chris Masona52d9a82007-08-27 16:49:44 -04004835 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004836 gfp_t mask = btrfs_alloc_write_mask(mapping);
Chris Masona52d9a82007-08-27 16:49:44 -04004837 int ret = 0;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304838 u64 block_start;
4839 u64 block_end;
Chris Masona52d9a82007-08-27 16:49:44 -04004840
Nikolay Borisovb03ebd92018-01-18 14:47:06 +02004841 if (IS_ALIGNED(offset, blocksize) &&
4842 (!len || IS_ALIGNED(len, blocksize)))
Chris Masona52d9a82007-08-27 16:49:44 -04004843 goto out;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304844
Josef Bacik8b62f872017-10-19 14:15:55 -04004845 block_start = round_down(from, blocksize);
4846 block_end = block_start + blocksize - 1;
4847
Qu Wenruo364ecf32017-02-27 15:10:38 +08004848 ret = btrfs_delalloc_reserve_space(inode, &data_reserved,
Josef Bacik8b62f872017-10-19 14:15:55 -04004849 block_start, blocksize);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004850 if (ret)
4851 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004852
Chris Mason211c17f2008-05-15 09:13:45 -04004853again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004854 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004855 if (!page) {
Qu Wenruobc42bda2017-02-27 15:10:39 +08004856 btrfs_delalloc_release_space(inode, data_reserved,
Qu Wenruo43b18592017-12-12 15:34:32 +08004857 block_start, blocksize, true);
4858 btrfs_delalloc_release_extents(BTRFS_I(inode), blocksize, true);
Miao Xieac6a2b32012-12-05 10:56:13 +00004859 ret = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -04004860 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04004861 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004862
Chris Masona52d9a82007-08-27 16:49:44 -04004863 if (!PageUptodate(page)) {
4864 ret = btrfs_readpage(NULL, page);
4865 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004866 if (page->mapping != mapping) {
4867 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004868 put_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004869 goto again;
4870 }
Chris Masona52d9a82007-08-27 16:49:44 -04004871 if (!PageUptodate(page)) {
4872 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04004873 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04004874 }
4875 }
Chris Mason211c17f2008-05-15 09:13:45 -04004876 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004877
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304878 lock_extent_bits(io_tree, block_start, block_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004879 set_page_extent_mapped(page);
4880
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304881 ordered = btrfs_lookup_ordered_extent(inode, block_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004882 if (ordered) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304883 unlock_extent_cached(io_tree, block_start, block_end,
David Sterbae43bbe52017-12-12 21:43:52 +01004884 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004885 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004886 put_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04004887 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004888 btrfs_put_ordered_extent(ordered);
4889 goto again;
4890 }
4891
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304892 clear_extent_bit(&BTRFS_I(inode)->io_tree, block_start, block_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06004893 EXTENT_DIRTY | EXTENT_DELALLOC |
4894 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
David Sterbaae0f1622017-10-31 16:37:52 +01004895 0, 0, &cached_state);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004896
Filipe Mananae3b8a482017-11-04 00:16:59 +00004897 ret = btrfs_set_extent_delalloc(inode, block_start, block_end, 0,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08004898 &cached_state, 0);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004899 if (ret) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304900 unlock_extent_cached(io_tree, block_start, block_end,
David Sterbae43bbe52017-12-12 21:43:52 +01004901 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004902 goto out_unlock;
4903 }
4904
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304905 if (offset != blocksize) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04004906 if (!len)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304907 len = blocksize - offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004908 kaddr = kmap(page);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004909 if (front)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304910 memset(kaddr + (block_start - page_offset(page)),
4911 0, offset);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004912 else
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304913 memset(kaddr + (block_start - page_offset(page)) + offset,
4914 0, len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004915 flush_dcache_page(page);
4916 kunmap(page);
4917 }
Chris Mason247e7432008-07-17 12:53:51 -04004918 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004919 set_page_dirty(page);
David Sterbae43bbe52017-12-12 21:43:52 +01004920 unlock_extent_cached(io_tree, block_start, block_end, &cached_state);
Chris Mason39279cc2007-06-12 06:35:45 -04004921
Chris Mason89642222008-07-24 09:41:53 -04004922out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04004923 if (ret)
Qu Wenruobc42bda2017-02-27 15:10:39 +08004924 btrfs_delalloc_release_space(inode, data_reserved, block_start,
Qu Wenruo43b18592017-12-12 15:34:32 +08004925 blocksize, true);
4926 btrfs_delalloc_release_extents(BTRFS_I(inode), blocksize, (ret != 0));
Chris Mason39279cc2007-06-12 06:35:45 -04004927 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004928 put_page(page);
Chris Mason39279cc2007-06-12 06:35:45 -04004929out:
Qu Wenruo364ecf32017-02-27 15:10:38 +08004930 extent_changeset_free(data_reserved);
Chris Mason39279cc2007-06-12 06:35:45 -04004931 return ret;
4932}
4933
Josef Bacik16e75492013-10-22 12:18:51 -04004934static int maybe_insert_hole(struct btrfs_root *root, struct inode *inode,
4935 u64 offset, u64 len)
4936{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004937 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik16e75492013-10-22 12:18:51 -04004938 struct btrfs_trans_handle *trans;
4939 int ret;
4940
4941 /*
4942 * Still need to make sure the inode looks like it's been updated so
4943 * that any holes get logged if we fsync.
4944 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004945 if (btrfs_fs_incompat(fs_info, NO_HOLES)) {
4946 BTRFS_I(inode)->last_trans = fs_info->generation;
Josef Bacik16e75492013-10-22 12:18:51 -04004947 BTRFS_I(inode)->last_sub_trans = root->log_transid;
4948 BTRFS_I(inode)->last_log_commit = root->last_log_commit;
4949 return 0;
4950 }
4951
4952 /*
4953 * 1 - for the one we're dropping
4954 * 1 - for the one we're adding
4955 * 1 - for updating the inode.
4956 */
4957 trans = btrfs_start_transaction(root, 3);
4958 if (IS_ERR(trans))
4959 return PTR_ERR(trans);
4960
4961 ret = btrfs_drop_extents(trans, root, inode, offset, offset + len, 1);
4962 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004963 btrfs_abort_transaction(trans, ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004964 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04004965 return ret;
4966 }
4967
David Sterbaf85b7372017-01-20 14:54:07 +01004968 ret = btrfs_insert_file_extent(trans, root, btrfs_ino(BTRFS_I(inode)),
4969 offset, 0, 0, len, 0, len, 0, 0, 0);
Josef Bacik16e75492013-10-22 12:18:51 -04004970 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004971 btrfs_abort_transaction(trans, ret);
Josef Bacik16e75492013-10-22 12:18:51 -04004972 else
4973 btrfs_update_inode(trans, root, inode);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004974 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04004975 return ret;
4976}
4977
Josef Bacik695a0d02011-03-04 15:46:53 -05004978/*
4979 * This function puts in dummy file extents for the area we're creating a hole
4980 * for. So if we are truncating this file to a larger size we need to insert
4981 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
4982 * the range between oldsize and size
4983 */
Josef Bacika41ad392011-01-31 15:30:16 -05004984int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04004985{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004986 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Yan Zheng9036c102008-10-30 14:19:41 -04004987 struct btrfs_root *root = BTRFS_I(inode)->root;
4988 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004989 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004990 struct extent_state *cached_state = NULL;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004991 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004992 u64 hole_start = ALIGN(oldsize, fs_info->sectorsize);
4993 u64 block_end = ALIGN(size, fs_info->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04004994 u64 last_byte;
4995 u64 cur_offset;
4996 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04004997 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04004998
Josef Bacika71754f2013-06-17 17:14:39 -04004999 /*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05305000 * If our size started in the middle of a block we need to zero out the
5001 * rest of the block before we expand the i_size, otherwise we could
Josef Bacika71754f2013-06-17 17:14:39 -04005002 * expose stale data.
5003 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05305004 err = btrfs_truncate_block(inode, oldsize, 0, 0);
Josef Bacika71754f2013-06-17 17:14:39 -04005005 if (err)
5006 return err;
5007
Yan Zheng9036c102008-10-30 14:19:41 -04005008 if (size <= hole_start)
5009 return 0;
5010
Yan Zheng9036c102008-10-30 14:19:41 -04005011 while (1) {
5012 struct btrfs_ordered_extent *ordered;
Miao Xiefa7c1492013-09-26 13:15:27 +08005013
David Sterbaff13db42015-12-03 14:30:40 +01005014 lock_extent_bits(io_tree, hole_start, block_end - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01005015 &cached_state);
Nikolay Borisova776c6f2017-02-20 13:50:49 +02005016 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), hole_start,
Miao Xiefa7c1492013-09-26 13:15:27 +08005017 block_end - hole_start);
Yan Zheng9036c102008-10-30 14:19:41 -04005018 if (!ordered)
5019 break;
Josef Bacik2ac55d42010-02-03 19:33:23 +00005020 unlock_extent_cached(io_tree, hole_start, block_end - 1,
David Sterbae43bbe52017-12-12 21:43:52 +01005021 &cached_state);
Miao Xiefa7c1492013-09-26 13:15:27 +08005022 btrfs_start_ordered_extent(inode, ordered, 1);
Yan Zheng9036c102008-10-30 14:19:41 -04005023 btrfs_put_ordered_extent(ordered);
5024 }
5025
Yan Zheng9036c102008-10-30 14:19:41 -04005026 cur_offset = hole_start;
5027 while (1) {
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02005028 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, cur_offset,
Yan Zheng9036c102008-10-30 14:19:41 -04005029 block_end - cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005030 if (IS_ERR(em)) {
5031 err = PTR_ERR(em);
Zach Brownf2767952013-01-08 19:37:58 +00005032 em = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005033 break;
5034 }
Yan Zheng9036c102008-10-30 14:19:41 -04005035 last_byte = min(extent_map_end(em), block_end);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005036 last_byte = ALIGN(last_byte, fs_info->sectorsize);
Yan, Zheng80825102009-11-12 09:35:36 +00005037 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04005038 struct extent_map *hole_em;
Yan Zheng9036c102008-10-30 14:19:41 -04005039 hole_size = last_byte - cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00005040
Josef Bacik16e75492013-10-22 12:18:51 -04005041 err = maybe_insert_hole(root, inode, cur_offset,
5042 hole_size);
5043 if (err)
Chris Mason771ed682008-11-06 22:02:51 -05005044 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02005045 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04005046 cur_offset + hole_size - 1, 0);
5047 hole_em = alloc_extent_map();
5048 if (!hole_em) {
5049 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
5050 &BTRFS_I(inode)->runtime_flags);
5051 goto next;
5052 }
5053 hole_em->start = cur_offset;
5054 hole_em->len = hole_size;
5055 hole_em->orig_start = cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00005056
Josef Bacik5dc562c2012-08-17 13:14:17 -04005057 hole_em->block_start = EXTENT_MAP_HOLE;
5058 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05005059 hole_em->orig_block_len = 0;
Josef Bacikcc95bef2013-04-04 14:31:27 -04005060 hole_em->ram_bytes = hole_size;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005061 hole_em->bdev = fs_info->fs_devices->latest_bdev;
Josef Bacik5dc562c2012-08-17 13:14:17 -04005062 hole_em->compress_type = BTRFS_COMPRESS_NONE;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005063 hole_em->generation = fs_info->generation;
Josef Bacik5dc562c2012-08-17 13:14:17 -04005064
5065 while (1) {
5066 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04005067 err = add_extent_mapping(em_tree, hole_em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04005068 write_unlock(&em_tree->lock);
5069 if (err != -EEXIST)
5070 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02005071 btrfs_drop_extent_cache(BTRFS_I(inode),
5072 cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04005073 cur_offset +
5074 hole_size - 1, 0);
5075 }
5076 free_extent_map(hole_em);
Yan Zheng9036c102008-10-30 14:19:41 -04005077 }
Josef Bacik16e75492013-10-22 12:18:51 -04005078next:
Yan Zheng9036c102008-10-30 14:19:41 -04005079 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04005080 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04005081 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00005082 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04005083 break;
5084 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04005085 free_extent_map(em);
David Sterbae43bbe52017-12-12 21:43:52 +01005086 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state);
Yan Zheng9036c102008-10-30 14:19:41 -04005087 return err;
5088}
5089
Eric Sandeen3972f262013-01-12 02:57:22 +00005090static int btrfs_setsize(struct inode *inode, struct iattr *attr)
Yan, Zheng80825102009-11-12 09:35:36 +00005091{
Miao Xief4a2f4c2011-12-14 20:12:01 -05005092 struct btrfs_root *root = BTRFS_I(inode)->root;
5093 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05005094 loff_t oldsize = i_size_read(inode);
Eric Sandeen3972f262013-01-12 02:57:22 +00005095 loff_t newsize = attr->ia_size;
5096 int mask = attr->ia_valid;
Yan, Zheng80825102009-11-12 09:35:36 +00005097 int ret;
5098
Eric Sandeen3972f262013-01-12 02:57:22 +00005099 /*
5100 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
5101 * special case where we need to update the times despite not having
5102 * these flags set. For all other operations the VFS set these flags
5103 * explicitly if it wants a timestamp update.
5104 */
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08005105 if (newsize != oldsize) {
5106 inode_inc_iversion(inode);
5107 if (!(mask & (ATTR_CTIME | ATTR_MTIME)))
5108 inode->i_ctime = inode->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07005109 current_time(inode);
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08005110 }
Eric Sandeen3972f262013-01-12 02:57:22 +00005111
Josef Bacika41ad392011-01-31 15:30:16 -05005112 if (newsize > oldsize) {
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005113 /*
David Sterbaea14b57f2017-06-22 02:19:11 +02005114 * Don't do an expanding truncate while snapshotting is ongoing.
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005115 * This is to ensure the snapshot captures a fully consistent
5116 * state of this file - if the snapshot captures this expanding
5117 * truncation, it must capture all writes that happened before
5118 * this truncation.
5119 */
Zhao Lei0bc19f902016-01-06 18:56:36 +08005120 btrfs_wait_for_snapshot_creation(root);
Josef Bacika41ad392011-01-31 15:30:16 -05005121 ret = btrfs_cont_expand(inode, oldsize, newsize);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005122 if (ret) {
David Sterbaea14b57f2017-06-22 02:19:11 +02005123 btrfs_end_write_no_snapshotting(root);
Yan, Zheng80825102009-11-12 09:35:36 +00005124 return ret;
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005125 }
Yan, Zheng80825102009-11-12 09:35:36 +00005126
Miao Xief4a2f4c2011-12-14 20:12:01 -05005127 trans = btrfs_start_transaction(root, 1);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005128 if (IS_ERR(trans)) {
David Sterbaea14b57f2017-06-22 02:19:11 +02005129 btrfs_end_write_no_snapshotting(root);
Miao Xief4a2f4c2011-12-14 20:12:01 -05005130 return PTR_ERR(trans);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005131 }
Miao Xief4a2f4c2011-12-14 20:12:01 -05005132
5133 i_size_write(inode, newsize);
5134 btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
Chandan Rajendra27772b62016-01-21 15:56:03 +05305135 pagecache_isize_extended(inode, oldsize, newsize);
Miao Xief4a2f4c2011-12-14 20:12:01 -05005136 ret = btrfs_update_inode(trans, root, inode);
David Sterbaea14b57f2017-06-22 02:19:11 +02005137 btrfs_end_write_no_snapshotting(root);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005138 btrfs_end_transaction(trans);
Josef Bacika41ad392011-01-31 15:30:16 -05005139 } else {
Yan, Zheng80825102009-11-12 09:35:36 +00005140
Josef Bacika41ad392011-01-31 15:30:16 -05005141 /*
5142 * We're truncating a file that used to have good data down to
5143 * zero. Make sure it gets into the ordered flush list so that
5144 * any new writes get down to disk quickly.
5145 */
5146 if (newsize == 0)
Josef Bacik72ac3c02012-05-23 14:13:11 -04005147 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
5148 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00005149
Josef Bacika41ad392011-01-31 15:30:16 -05005150 truncate_setsize(inode, newsize);
Miao Xie2e60a512013-02-08 07:01:08 +00005151
Andrea Gelmini52042d82018-11-28 12:05:13 +01005152 /* Disable nonlocked read DIO to avoid the endless truncate */
Nikolay Borisovabcefb12017-02-20 13:51:10 +02005153 btrfs_inode_block_unlocked_dio(BTRFS_I(inode));
Miao Xie2e60a512013-02-08 07:01:08 +00005154 inode_dio_wait(inode);
Nikolay Borisov0b581702017-02-20 13:51:11 +02005155 btrfs_inode_resume_unlocked_dio(BTRFS_I(inode));
Miao Xie2e60a512013-02-08 07:01:08 +00005156
Filipe Manana213e8c52018-02-06 20:40:31 +00005157 ret = btrfs_truncate(inode, newsize == oldsize);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005158 if (ret && inode->i_nlink) {
5159 int err;
5160
5161 /*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07005162 * Truncate failed, so fix up the in-memory size. We
5163 * adjusted disk_i_size down as we removed extents, so
5164 * wait for disk_i_size to be stable and then update the
5165 * in-memory size to match.
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005166 */
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07005167 err = btrfs_wait_ordered_range(inode, 0, (u64)-1);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005168 if (err)
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07005169 return err;
5170 i_size_write(inode, BTRFS_I(inode)->disk_i_size);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005171 }
Yan, Zheng80825102009-11-12 09:35:36 +00005172 }
5173
Josef Bacika41ad392011-01-31 15:30:16 -05005174 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00005175}
5176
Chris Mason39279cc2007-06-12 06:35:45 -04005177static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
5178{
David Howells2b0143b2015-03-17 22:25:59 +00005179 struct inode *inode = d_inode(dentry);
Li Zefanb83cc962010-12-20 16:04:08 +08005180 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005181 int err;
5182
Li Zefanb83cc962010-12-20 16:04:08 +08005183 if (btrfs_root_readonly(root))
5184 return -EROFS;
5185
Jan Kara31051c82016-05-26 16:55:18 +02005186 err = setattr_prepare(dentry, attr);
Chris Mason39279cc2007-06-12 06:35:45 -04005187 if (err)
5188 return err;
5189
Chris Mason5a3f23d2009-03-31 13:27:11 -04005190 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Eric Sandeen3972f262013-01-12 02:57:22 +00005191 err = btrfs_setsize(inode, attr);
Yan, Zheng80825102009-11-12 09:35:36 +00005192 if (err)
5193 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005194 }
Yan Zheng9036c102008-10-30 14:19:41 -04005195
Christoph Hellwig10257742010-06-04 11:30:02 +02005196 if (attr->ia_valid) {
5197 setattr_copy(inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005198 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005199 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04005200
Josef Bacik22c44fe2011-11-30 10:45:38 -05005201 if (!err && attr->ia_valid & ATTR_MODE)
Christoph Hellwig996a7102013-12-20 05:16:43 -08005202 err = posix_acl_chmod(inode, inode->i_mode);
Christoph Hellwig10257742010-06-04 11:30:02 +02005203 }
5204
Chris Mason39279cc2007-06-12 06:35:45 -04005205 return err;
5206}
Chris Mason61295eb2008-01-14 16:24:38 -05005207
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005208/*
5209 * While truncating the inode pages during eviction, we get the VFS calling
5210 * btrfs_invalidatepage() against each page of the inode. This is slow because
5211 * the calls to btrfs_invalidatepage() result in a huge amount of calls to
5212 * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting
5213 * extent_state structures over and over, wasting lots of time.
5214 *
5215 * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all
5216 * those expensive operations on a per page basis and do only the ordered io
5217 * finishing, while we release here the extent_map and extent_state structures,
5218 * without the excessive merging and splitting.
5219 */
5220static void evict_inode_truncate_pages(struct inode *inode)
5221{
5222 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
5223 struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree;
5224 struct rb_node *node;
5225
5226 ASSERT(inode->i_state & I_FREEING);
Johannes Weiner91b0abe2014-04-03 14:47:49 -07005227 truncate_inode_pages_final(&inode->i_data);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005228
5229 write_lock(&map_tree->lock);
Liu Bo07e1ce02018-08-23 03:51:52 +08005230 while (!RB_EMPTY_ROOT(&map_tree->map.rb_root)) {
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005231 struct extent_map *em;
5232
Liu Bo07e1ce02018-08-23 03:51:52 +08005233 node = rb_first_cached(&map_tree->map);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005234 em = rb_entry(node, struct extent_map, rb_node);
Wang Shilong180589e2013-12-14 15:27:31 +08005235 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
5236 clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005237 remove_extent_mapping(map_tree, em);
5238 free_extent_map(em);
Filipe Manana7064dd52014-08-08 02:47:05 +01005239 if (need_resched()) {
5240 write_unlock(&map_tree->lock);
5241 cond_resched();
5242 write_lock(&map_tree->lock);
5243 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005244 }
5245 write_unlock(&map_tree->lock);
5246
Filipe Manana6ca07092015-05-26 00:55:42 +01005247 /*
5248 * Keep looping until we have no more ranges in the io tree.
5249 * We can have ongoing bios started by readpages (called from readahead)
Filipe Manana9c6429d2015-06-10 12:55:41 +01005250 * that have their endio callback (extent_io.c:end_bio_extent_readpage)
5251 * still in progress (unlocked the pages in the bio but did not yet
5252 * unlocked the ranges in the io tree). Therefore this means some
Filipe Manana6ca07092015-05-26 00:55:42 +01005253 * ranges can still be locked and eviction started because before
5254 * submitting those bios, which are executed by a separate task (work
5255 * queue kthread), inode references (inode->i_count) were not taken
5256 * (which would be dropped in the end io callback of each bio).
5257 * Therefore here we effectively end up waiting for those bios and
5258 * anyone else holding locked ranges without having bumped the inode's
5259 * reference count - if we don't do it, when they access the inode's
5260 * io_tree to unlock a range it may be too late, leading to an
5261 * use-after-free issue.
5262 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005263 spin_lock(&io_tree->lock);
5264 while (!RB_EMPTY_ROOT(&io_tree->state)) {
5265 struct extent_state *state;
5266 struct extent_state *cached_state = NULL;
Filipe Manana6ca07092015-05-26 00:55:42 +01005267 u64 start;
5268 u64 end;
Filipe Manana421f0922018-10-12 13:02:48 +01005269 unsigned state_flags;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005270
5271 node = rb_first(&io_tree->state);
5272 state = rb_entry(node, struct extent_state, rb_node);
Filipe Manana6ca07092015-05-26 00:55:42 +01005273 start = state->start;
5274 end = state->end;
Filipe Manana421f0922018-10-12 13:02:48 +01005275 state_flags = state->state;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005276 spin_unlock(&io_tree->lock);
5277
David Sterbaff13db42015-12-03 14:30:40 +01005278 lock_extent_bits(io_tree, start, end, &cached_state);
Qu Wenruob9d0b382015-09-29 10:35:16 +08005279
5280 /*
5281 * If still has DELALLOC flag, the extent didn't reach disk,
5282 * and its reserved space won't be freed by delayed_ref.
5283 * So we need to free its reserved space here.
5284 * (Refer to comment in btrfs_invalidatepage, case 2)
5285 *
5286 * Note, end is the bytenr of last byte, so we need + 1 here.
5287 */
Filipe Manana421f0922018-10-12 13:02:48 +01005288 if (state_flags & EXTENT_DELALLOC)
Qu Wenruobc42bda2017-02-27 15:10:39 +08005289 btrfs_qgroup_free_data(inode, NULL, start, end - start + 1);
Qu Wenruob9d0b382015-09-29 10:35:16 +08005290
Filipe Manana6ca07092015-05-26 00:55:42 +01005291 clear_extent_bit(io_tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005292 EXTENT_LOCKED | EXTENT_DIRTY |
5293 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
David Sterbaae0f1622017-10-31 16:37:52 +01005294 EXTENT_DEFRAG, 1, 1, &cached_state);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005295
Filipe Manana7064dd52014-08-08 02:47:05 +01005296 cond_resched();
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005297 spin_lock(&io_tree->lock);
5298 }
5299 spin_unlock(&io_tree->lock);
5300}
5301
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005302static struct btrfs_trans_handle *evict_refill_and_join(struct btrfs_root *root,
Josef Bacikad80cf52018-09-28 07:18:19 -04005303 struct btrfs_block_rsv *rsv)
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005304{
5305 struct btrfs_fs_info *fs_info = root->fs_info;
5306 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
Josef Bacik260e7702018-11-21 14:03:13 -05005307 u64 delayed_refs_extra = btrfs_calc_trans_metadata_size(fs_info, 1);
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005308 int failures = 0;
5309
5310 for (;;) {
5311 struct btrfs_trans_handle *trans;
5312 int ret;
5313
Josef Bacik260e7702018-11-21 14:03:13 -05005314 ret = btrfs_block_rsv_refill(root, rsv,
5315 rsv->size + delayed_refs_extra,
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005316 BTRFS_RESERVE_FLUSH_LIMIT);
5317
5318 if (ret && ++failures > 2) {
5319 btrfs_warn(fs_info,
5320 "could not allocate space for a delete; will truncate on mount");
5321 return ERR_PTR(-ENOSPC);
5322 }
5323
Josef Bacik260e7702018-11-21 14:03:13 -05005324 /*
5325 * Evict can generate a large amount of delayed refs without
5326 * having a way to add space back since we exhaust our temporary
5327 * block rsv. We aren't allowed to do FLUSH_ALL in this case
5328 * because we could deadlock with so many things in the flushing
5329 * code, so we have to try and hold some extra space to
5330 * compensate for our delayed ref generation. If we can't get
5331 * that space then we need see if we can steal our minimum from
5332 * the global reserve. We will be ratelimited by the amount of
5333 * space we have for the delayed refs rsv, so we'll end up
5334 * committing and trying again.
5335 */
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005336 trans = btrfs_join_transaction(root);
Josef Bacik260e7702018-11-21 14:03:13 -05005337 if (IS_ERR(trans) || !ret) {
5338 if (!IS_ERR(trans)) {
5339 trans->block_rsv = &fs_info->trans_block_rsv;
5340 trans->bytes_reserved = delayed_refs_extra;
5341 btrfs_block_rsv_migrate(rsv, trans->block_rsv,
5342 delayed_refs_extra, 1);
5343 }
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005344 return trans;
Josef Bacik260e7702018-11-21 14:03:13 -05005345 }
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005346
5347 /*
5348 * Try to steal from the global reserve if there is space for
5349 * it.
5350 */
Josef Bacik644036122018-12-03 10:20:36 -05005351 if (!btrfs_check_space_for_delayed_refs(fs_info) &&
5352 !btrfs_block_rsv_migrate(global_rsv, rsv, rsv->size, 0))
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005353 return trans;
5354
5355 /* If not, commit and try again. */
5356 ret = btrfs_commit_transaction(trans);
5357 if (ret)
5358 return ERR_PTR(ret);
5359 }
5360}
5361
Al Virobd555972010-06-07 11:35:40 -04005362void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005363{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005364 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04005365 struct btrfs_trans_handle *trans;
5366 struct btrfs_root *root = BTRFS_I(inode)->root;
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005367 struct btrfs_block_rsv *rsv;
Chris Mason39279cc2007-06-12 06:35:45 -04005368 int ret;
5369
liubo1abe9b82011-03-24 11:18:59 +00005370 trace_btrfs_inode_evict(inode);
5371
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005372 if (!root) {
Liu Boe8f1bc12018-01-25 11:02:53 -07005373 clear_inode(inode);
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005374 return;
5375 }
5376
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005377 evict_inode_truncate_pages(inode);
5378
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005379 if (inode->i_nlink &&
5380 ((btrfs_root_refs(&root->root_item) != 0 &&
5381 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) ||
Nikolay Borisov70ddc552017-02-20 13:50:35 +02005382 btrfs_is_free_space_inode(BTRFS_I(inode))))
Al Virobd555972010-06-07 11:35:40 -04005383 goto no_delete;
5384
Omar Sandoval27919062018-05-11 13:13:37 -07005385 if (is_bad_inode(inode))
Chris Mason39279cc2007-06-12 06:35:45 -04005386 goto no_delete;
Chris Mason5f39d392007-10-15 16:14:19 -04005387
Nikolay Borisov7ab79562017-02-20 13:50:57 +02005388 btrfs_free_io_failure_record(BTRFS_I(inode), 0, (u64)-1);
Miao Xief6124962014-09-12 18:44:04 +08005389
Omar Sandoval7b40b692018-05-11 13:13:33 -07005390 if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags))
Yan, Zhengc71bf092009-11-12 09:34:40 +00005391 goto no_delete;
Yan, Zhengc71bf092009-11-12 09:34:40 +00005392
Yan, Zheng76dda932009-09-21 16:00:26 -04005393 if (inode->i_nlink > 0) {
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005394 BUG_ON(btrfs_root_refs(&root->root_item) != 0 &&
5395 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID);
Yan, Zheng76dda932009-09-21 16:00:26 -04005396 goto no_delete;
5397 }
5398
Nikolay Borisovaa790212017-01-10 20:35:40 +02005399 ret = btrfs_commit_inode_delayed_inode(BTRFS_I(inode));
Omar Sandoval27919062018-05-11 13:13:37 -07005400 if (ret)
Miao Xie0e8c36a2012-12-19 06:59:51 +00005401 goto no_delete;
Miao Xie0e8c36a2012-12-19 06:59:51 +00005402
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005403 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Omar Sandoval27919062018-05-11 13:13:37 -07005404 if (!rsv)
Josef Bacik4289a662011-08-05 13:22:24 -04005405 goto no_delete;
Josef Bacikad80cf52018-09-28 07:18:19 -04005406 rsv->size = btrfs_calc_trunc_metadata_size(fs_info, 1);
Josef Bacikca7e70f2012-08-27 17:48:15 -04005407 rsv->failfast = 1;
Josef Bacik4289a662011-08-05 13:22:24 -04005408
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02005409 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Mason5f39d392007-10-15 16:14:19 -04005410
Yan, Zheng80825102009-11-12 09:35:36 +00005411 while (1) {
Josef Bacikad80cf52018-09-28 07:18:19 -04005412 trans = evict_refill_and_join(root, rsv);
Omar Sandoval27919062018-05-11 13:13:37 -07005413 if (IS_ERR(trans))
5414 goto free_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04005415
5416 trans->block_rsv = rsv;
5417
Yan, Zhengd68fc572010-05-16 10:49:58 -04005418 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Omar Sandoval27919062018-05-11 13:13:37 -07005419 trans->block_rsv = &fs_info->trans_block_rsv;
5420 btrfs_end_transaction(trans);
5421 btrfs_btree_balance_dirty(fs_info);
5422 if (ret && ret != -ENOSPC && ret != -EAGAIN)
5423 goto free_rsv;
5424 else if (!ret)
Yan, Zheng80825102009-11-12 09:35:36 +00005425 break;
Josef Bacik7b128762008-07-24 12:17:14 -04005426 }
5427
Josef Bacik4ef31a42013-08-13 14:10:08 -04005428 /*
Omar Sandoval27919062018-05-11 13:13:37 -07005429 * Errors here aren't a big deal, it just means we leave orphan items in
5430 * the tree. They will be cleaned up on the next mount. If the inode
5431 * number gets reused, cleanup deletes the orphan item without doing
5432 * anything, and unlink reuses the existing orphan item.
5433 *
5434 * If it turns out that we are dropping too many of these, we might want
5435 * to add a mechanism for retrying these after a commit.
Josef Bacik4ef31a42013-08-13 14:10:08 -04005436 */
Josef Bacikad80cf52018-09-28 07:18:19 -04005437 trans = evict_refill_and_join(root, rsv);
Omar Sandoval27919062018-05-11 13:13:37 -07005438 if (!IS_ERR(trans)) {
5439 trans->block_rsv = rsv;
5440 btrfs_orphan_del(trans, BTRFS_I(inode));
5441 trans->block_rsv = &fs_info->trans_block_rsv;
5442 btrfs_end_transaction(trans);
5443 }
Chris Mason85e21ba2008-01-29 15:11:36 -05005444
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005445 if (!(root == fs_info->tree_root ||
Li Zefan581bb052011-04-20 10:06:11 +08005446 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005447 btrfs_return_ino(root, btrfs_ino(BTRFS_I(inode)));
Li Zefan581bb052011-04-20 10:06:11 +08005448
Omar Sandoval27919062018-05-11 13:13:37 -07005449free_rsv:
5450 btrfs_free_block_rsv(fs_info, rsv);
Chris Mason39279cc2007-06-12 06:35:45 -04005451no_delete:
Omar Sandoval27919062018-05-11 13:13:37 -07005452 /*
5453 * If we didn't successfully delete, the orphan item will still be in
5454 * the tree and we'll retry on the next mount. Again, we might also want
5455 * to retry these periodically in the future.
5456 */
Nikolay Borisovf48d1cf2017-01-10 20:35:39 +02005457 btrfs_remove_delayed_node(BTRFS_I(inode));
Jan Karadbd57682012-05-03 14:48:02 +02005458 clear_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005459}
5460
5461/*
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005462 * Return the key found in the dir entry in the location pointer, fill @type
5463 * with BTRFS_FT_*, and return 0.
5464 *
Su Yue005d6712018-03-05 17:13:37 +08005465 * If no dir entries were found, returns -ENOENT.
5466 * If found a corrupted location in dir entry, returns -EUCLEAN.
Chris Mason39279cc2007-06-12 06:35:45 -04005467 */
5468static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005469 struct btrfs_key *location, u8 *type)
Chris Mason39279cc2007-06-12 06:35:45 -04005470{
5471 const char *name = dentry->d_name.name;
5472 int namelen = dentry->d_name.len;
5473 struct btrfs_dir_item *di;
5474 struct btrfs_path *path;
5475 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04005476 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005477
5478 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005479 if (!path)
5480 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05005481
David Sterbaf85b7372017-01-20 14:54:07 +01005482 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(BTRFS_I(dir)),
5483 name, namelen, 0);
Liu Bo3cf50682018-09-12 06:06:26 +08005484 if (IS_ERR_OR_NULL(di)) {
5485 ret = di ? PTR_ERR(di) : -ENOENT;
Su Yue005d6712018-03-05 17:13:37 +08005486 goto out;
5487 }
Chris Masond3977122009-01-05 21:25:51 -05005488
Chris Mason5f39d392007-10-15 16:14:19 -04005489 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Liu Bo56a0e702017-10-30 11:14:38 -06005490 if (location->type != BTRFS_INODE_ITEM_KEY &&
5491 location->type != BTRFS_ROOT_ITEM_KEY) {
Su Yue005d6712018-03-05 17:13:37 +08005492 ret = -EUCLEAN;
Liu Bo56a0e702017-10-30 11:14:38 -06005493 btrfs_warn(root->fs_info,
5494"%s gets something invalid in DIR_ITEM (name %s, directory ino %llu, location(%llu %u %llu))",
5495 __func__, name, btrfs_ino(BTRFS_I(dir)),
5496 location->objectid, location->type, location->offset);
Liu Bo56a0e702017-10-30 11:14:38 -06005497 }
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005498 if (!ret)
5499 *type = btrfs_dir_type(path->nodes[0], di);
Chris Mason39279cc2007-06-12 06:35:45 -04005500out:
Chris Mason39279cc2007-06-12 06:35:45 -04005501 btrfs_free_path(path);
5502 return ret;
5503}
5504
5505/*
5506 * when we hit a tree root in a directory, the btrfs part of the inode
5507 * needs to be changed to reflect the root directory of the tree root. This
5508 * is kind of like crossing a mount point.
5509 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005510static int fixup_tree_root_location(struct btrfs_fs_info *fs_info,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005511 struct inode *dir,
5512 struct dentry *dentry,
5513 struct btrfs_key *location,
5514 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04005515{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005516 struct btrfs_path *path;
5517 struct btrfs_root *new_root;
5518 struct btrfs_root_ref *ref;
5519 struct extent_buffer *leaf;
David Sterba1d4c08e2015-01-02 19:36:14 +01005520 struct btrfs_key key;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005521 int ret;
5522 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005523
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005524 path = btrfs_alloc_path();
5525 if (!path) {
5526 err = -ENOMEM;
5527 goto out;
5528 }
Chris Mason39279cc2007-06-12 06:35:45 -04005529
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005530 err = -ENOENT;
David Sterba1d4c08e2015-01-02 19:36:14 +01005531 key.objectid = BTRFS_I(dir)->root->root_key.objectid;
5532 key.type = BTRFS_ROOT_REF_KEY;
5533 key.offset = location->objectid;
5534
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005535 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005536 if (ret) {
5537 if (ret < 0)
5538 err = ret;
5539 goto out;
5540 }
Chris Mason39279cc2007-06-12 06:35:45 -04005541
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005542 leaf = path->nodes[0];
5543 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005544 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(BTRFS_I(dir)) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005545 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
5546 goto out;
5547
5548 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
5549 (unsigned long)(ref + 1),
5550 dentry->d_name.len);
5551 if (ret)
5552 goto out;
5553
David Sterbab3b4aa72011-04-21 01:20:15 +02005554 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005555
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005556 new_root = btrfs_read_fs_root_no_name(fs_info, location);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005557 if (IS_ERR(new_root)) {
5558 err = PTR_ERR(new_root);
5559 goto out;
5560 }
5561
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005562 *sub_root = new_root;
5563 location->objectid = btrfs_root_dirid(&new_root->root_item);
5564 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04005565 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005566 err = 0;
5567out:
5568 btrfs_free_path(path);
5569 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005570}
5571
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005572static void inode_tree_add(struct inode *inode)
5573{
5574 struct btrfs_root *root = BTRFS_I(inode)->root;
5575 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005576 struct rb_node **p;
5577 struct rb_node *parent;
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005578 struct rb_node *new = &BTRFS_I(inode)->rb_node;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005579 u64 ino = btrfs_ino(BTRFS_I(inode));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005580
Al Viro1d3382cb2010-10-23 15:19:20 -04005581 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005582 return;
Miao Xiee1409ce2013-05-15 07:48:16 +00005583 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005584 spin_lock(&root->inode_lock);
Miao Xiee1409ce2013-05-15 07:48:16 +00005585 p = &root->inode_tree.rb_node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005586 while (*p) {
5587 parent = *p;
5588 entry = rb_entry(parent, struct btrfs_inode, rb_node);
5589
David Sterba37508512018-06-29 10:56:40 +02005590 if (ino < btrfs_ino(entry))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005591 p = &parent->rb_left;
David Sterba37508512018-06-29 10:56:40 +02005592 else if (ino > btrfs_ino(entry))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005593 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005594 else {
5595 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04005596 (I_WILL_FREE | I_FREEING)));
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005597 rb_replace_node(parent, new, &root->inode_tree);
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005598 RB_CLEAR_NODE(parent);
5599 spin_unlock(&root->inode_lock);
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005600 return;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005601 }
5602 }
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005603 rb_link_node(new, parent, p);
5604 rb_insert_color(new, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005605 spin_unlock(&root->inode_lock);
5606}
5607
5608static void inode_tree_del(struct inode *inode)
5609{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005610 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005611 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04005612 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005613
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005614 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005615 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005616 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005617 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04005618 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005619 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005620 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04005621
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005622 if (empty && btrfs_root_refs(&root->root_item) == 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005623 synchronize_srcu(&fs_info->subvol_srcu);
Yan, Zheng76dda932009-09-21 16:00:26 -04005624 spin_lock(&root->inode_lock);
5625 empty = RB_EMPTY_ROOT(&root->inode_tree);
5626 spin_unlock(&root->inode_lock);
5627 if (empty)
5628 btrfs_add_dead_root(root);
5629 }
5630}
5631
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005632
Chris Masone02119d2008-09-05 16:13:11 -04005633static int btrfs_init_locked_inode(struct inode *inode, void *p)
5634{
5635 struct btrfs_iget_args *args = p;
Chris Mason90d3e592014-01-09 17:28:00 -08005636 inode->i_ino = args->location->objectid;
5637 memcpy(&BTRFS_I(inode)->location, args->location,
5638 sizeof(*args->location));
Chris Masone02119d2008-09-05 16:13:11 -04005639 BTRFS_I(inode)->root = args->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005640 return 0;
5641}
5642
5643static int btrfs_find_actor(struct inode *inode, void *opaque)
5644{
5645 struct btrfs_iget_args *args = opaque;
Chris Mason90d3e592014-01-09 17:28:00 -08005646 return args->location->objectid == BTRFS_I(inode)->location.objectid &&
Chris Masond3977122009-01-05 21:25:51 -05005647 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005648}
5649
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005650static struct inode *btrfs_iget_locked(struct super_block *s,
Chris Mason90d3e592014-01-09 17:28:00 -08005651 struct btrfs_key *location,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005652 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04005653{
5654 struct inode *inode;
5655 struct btrfs_iget_args args;
Chris Mason90d3e592014-01-09 17:28:00 -08005656 unsigned long hashval = btrfs_inode_hash(location->objectid, root);
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005657
Chris Mason90d3e592014-01-09 17:28:00 -08005658 args.location = location;
Chris Mason39279cc2007-06-12 06:35:45 -04005659 args.root = root;
5660
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005661 inode = iget5_locked(s, hashval, btrfs_find_actor,
Chris Mason39279cc2007-06-12 06:35:45 -04005662 btrfs_init_locked_inode,
5663 (void *)&args);
5664 return inode;
5665}
5666
Balaji Rao1a54ef82008-07-21 02:01:04 +05305667/* Get an inode object given its location and corresponding root.
5668 * Returns in *is_new if the inode was read from disk
5669 */
Filipe Manana4222ea72018-10-24 10:13:03 +01005670struct inode *btrfs_iget_path(struct super_block *s, struct btrfs_key *location,
5671 struct btrfs_root *root, int *new,
5672 struct btrfs_path *path)
Balaji Rao1a54ef82008-07-21 02:01:04 +05305673{
5674 struct inode *inode;
5675
Chris Mason90d3e592014-01-09 17:28:00 -08005676 inode = btrfs_iget_locked(s, location, root);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305677 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005678 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305679
5680 if (inode->i_state & I_NEW) {
Filipe Manana67710892016-06-06 11:51:25 +01005681 int ret;
5682
Filipe Manana4222ea72018-10-24 10:13:03 +01005683 ret = btrfs_read_locked_inode(inode, path);
Al Viro9bc2cef2018-07-29 23:04:50 +01005684 if (!ret) {
Mark Fasheh1748f842011-07-12 11:25:31 -07005685 inode_tree_add(inode);
5686 unlock_new_inode(inode);
5687 if (new)
5688 *new = 1;
5689 } else {
Al Virof5b3a412018-07-29 23:04:51 +01005690 iget_failed(inode);
5691 /*
5692 * ret > 0 can come from btrfs_search_slot called by
5693 * btrfs_read_locked_inode, this means the inode item
5694 * was not found.
5695 */
5696 if (ret > 0)
5697 ret = -ENOENT;
5698 inode = ERR_PTR(ret);
Mark Fasheh1748f842011-07-12 11:25:31 -07005699 }
5700 }
5701
Balaji Rao1a54ef82008-07-21 02:01:04 +05305702 return inode;
5703}
5704
Filipe Manana4222ea72018-10-24 10:13:03 +01005705struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
5706 struct btrfs_root *root, int *new)
5707{
5708 return btrfs_iget_path(s, location, root, new, NULL);
5709}
5710
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005711static struct inode *new_simple_dir(struct super_block *s,
5712 struct btrfs_key *key,
5713 struct btrfs_root *root)
5714{
5715 struct inode *inode = new_inode(s);
5716
5717 if (!inode)
5718 return ERR_PTR(-ENOMEM);
5719
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005720 BTRFS_I(inode)->root = root;
5721 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04005722 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005723
5724 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Li Zefan848cce02012-02-21 17:04:28 +08005725 inode->i_op = &btrfs_dir_ro_inode_operations;
Omar Sandoval1fdf4192017-01-25 17:06:39 -08005726 inode->i_opflags &= ~IOP_XATTR;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005727 inode->i_fop = &simple_dir_operations;
5728 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
Deepa Dinamanic2050a42016-09-14 07:48:06 -07005729 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05305730 inode->i_atime = inode->i_mtime;
5731 inode->i_ctime = inode->i_mtime;
Arnd Bergmannd3c6be6f2018-06-21 18:04:06 +02005732 BTRFS_I(inode)->i_otime = inode->i_mtime;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005733
5734 return inode;
5735}
5736
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005737static inline u8 btrfs_inode_type(struct inode *inode)
5738{
5739 /*
5740 * Compile-time asserts that generic FT_* types still match
5741 * BTRFS_FT_* types
5742 */
5743 BUILD_BUG_ON(BTRFS_FT_UNKNOWN != FT_UNKNOWN);
5744 BUILD_BUG_ON(BTRFS_FT_REG_FILE != FT_REG_FILE);
5745 BUILD_BUG_ON(BTRFS_FT_DIR != FT_DIR);
5746 BUILD_BUG_ON(BTRFS_FT_CHRDEV != FT_CHRDEV);
5747 BUILD_BUG_ON(BTRFS_FT_BLKDEV != FT_BLKDEV);
5748 BUILD_BUG_ON(BTRFS_FT_FIFO != FT_FIFO);
5749 BUILD_BUG_ON(BTRFS_FT_SOCK != FT_SOCK);
5750 BUILD_BUG_ON(BTRFS_FT_SYMLINK != FT_SYMLINK);
5751
5752 return fs_umode_to_ftype(inode->i_mode);
5753}
5754
Chris Mason3de45862008-11-17 21:02:50 -05005755struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04005756{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005757 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masond3977122009-01-05 21:25:51 -05005758 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005759 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005760 struct btrfs_root *sub_root = root;
5761 struct btrfs_key location;
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005762 u8 di_type = 0;
Yan, Zheng76dda932009-09-21 16:00:26 -04005763 int index;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005764 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005765
5766 if (dentry->d_name.len > BTRFS_NAME_LEN)
5767 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04005768
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005769 ret = btrfs_inode_by_name(dir, dentry, &location, &di_type);
Chris Mason39279cc2007-06-12 06:35:45 -04005770 if (ret < 0)
5771 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04005772
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005773 if (location.type == BTRFS_INODE_ITEM_KEY) {
Josef Bacik73f73412009-12-04 17:38:27 +00005774 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005775 if (IS_ERR(inode))
5776 return inode;
5777
5778 /* Do extra check against inode mode with di_type */
5779 if (btrfs_inode_type(inode) != di_type) {
5780 btrfs_crit(fs_info,
5781"inode mode mismatch with dir: inode mode=0%o btrfs type=%u dir type=%u",
5782 inode->i_mode, btrfs_inode_type(inode),
5783 di_type);
5784 iput(inode);
5785 return ERR_PTR(-EUCLEAN);
5786 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005787 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04005788 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005789
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005790 index = srcu_read_lock(&fs_info->subvol_srcu);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005791 ret = fixup_tree_root_location(fs_info, dir, dentry,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005792 &location, &sub_root);
5793 if (ret < 0) {
5794 if (ret != -ENOENT)
5795 inode = ERR_PTR(ret);
5796 else
5797 inode = new_simple_dir(dir->i_sb, &location, sub_root);
5798 } else {
Josef Bacik73f73412009-12-04 17:38:27 +00005799 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04005800 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005801 srcu_read_unlock(&fs_info->subvol_srcu, index);
Yan, Zheng76dda932009-09-21 16:00:26 -04005802
Julia Lawall34d19ba2011-01-24 19:55:19 +00005803 if (!IS_ERR(inode) && root != sub_root) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005804 down_read(&fs_info->cleanup_work_sem);
David Howellsbc98a422017-07-17 08:45:34 +01005805 if (!sb_rdonly(inode->i_sb))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005806 ret = btrfs_orphan_cleanup(sub_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005807 up_read(&fs_info->cleanup_work_sem);
Josef Bacik01cd3362013-06-03 21:39:49 -04005808 if (ret) {
5809 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005810 inode = ERR_PTR(ret);
Josef Bacik01cd3362013-06-03 21:39:49 -04005811 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00005812 }
5813
Chris Mason3de45862008-11-17 21:02:50 -05005814 return inode;
5815}
5816
Nick Pigginfe15ce42011-01-07 17:49:23 +11005817static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04005818{
5819 struct btrfs_root *root;
David Howells2b0143b2015-03-17 22:25:59 +00005820 struct inode *inode = d_inode(dentry);
Yan, Zheng76dda932009-09-21 16:00:26 -04005821
Li Zefan848cce02012-02-21 17:04:28 +08005822 if (!inode && !IS_ROOT(dentry))
David Howells2b0143b2015-03-17 22:25:59 +00005823 inode = d_inode(dentry->d_parent);
Yan, Zheng76dda932009-09-21 16:00:26 -04005824
Li Zefan848cce02012-02-21 17:04:28 +08005825 if (inode) {
5826 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04005827 if (btrfs_root_refs(&root->root_item) == 0)
5828 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08005829
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005830 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Li Zefan848cce02012-02-21 17:04:28 +08005831 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04005832 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005833 return 0;
5834}
5835
Chris Mason3de45862008-11-17 21:02:50 -05005836static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04005837 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05005838{
Al Viro3837d202018-10-10 16:38:27 -04005839 struct inode *inode = btrfs_lookup_dentry(dir, dentry);
Josef Bacika66e7cc2011-09-18 10:34:03 -04005840
Al Viro3837d202018-10-10 16:38:27 -04005841 if (inode == ERR_PTR(-ENOENT))
5842 inode = NULL;
Al Viro41d28bc2014-10-12 22:24:21 -04005843 return d_splice_alias(inode, dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04005844}
5845
Josef Bacik23b5ec72017-07-24 15:14:25 -04005846/*
5847 * All this infrastructure exists because dir_emit can fault, and we are holding
5848 * the tree lock when doing readdir. For now just allocate a buffer and copy
5849 * our information into that, and then dir_emit from the buffer. This is
5850 * similar to what NFS does, only we don't keep the buffer around in pagecache
5851 * because I'm afraid I'll mess that up. Long term we need to make filldir do
5852 * copy_to_user_inatomic so we don't have to worry about page faulting under the
5853 * tree lock.
5854 */
5855static int btrfs_opendir(struct inode *inode, struct file *file)
5856{
5857 struct btrfs_file_private *private;
5858
5859 private = kzalloc(sizeof(struct btrfs_file_private), GFP_KERNEL);
5860 if (!private)
5861 return -ENOMEM;
5862 private->filldir_buf = kzalloc(PAGE_SIZE, GFP_KERNEL);
5863 if (!private->filldir_buf) {
5864 kfree(private);
5865 return -ENOMEM;
5866 }
5867 file->private_data = private;
5868 return 0;
5869}
5870
5871struct dir_entry {
5872 u64 ino;
5873 u64 offset;
5874 unsigned type;
5875 int name_len;
5876};
5877
5878static int btrfs_filldir(void *addr, int entries, struct dir_context *ctx)
5879{
5880 while (entries--) {
5881 struct dir_entry *entry = addr;
5882 char *name = (char *)(entry + 1);
5883
David Sterba92d32172018-04-16 21:10:14 +02005884 ctx->pos = get_unaligned(&entry->offset);
5885 if (!dir_emit(ctx, name, get_unaligned(&entry->name_len),
5886 get_unaligned(&entry->ino),
5887 get_unaligned(&entry->type)))
Josef Bacik23b5ec72017-07-24 15:14:25 -04005888 return 1;
David Sterba92d32172018-04-16 21:10:14 +02005889 addr += sizeof(struct dir_entry) +
5890 get_unaligned(&entry->name_len);
Josef Bacik23b5ec72017-07-24 15:14:25 -04005891 ctx->pos++;
5892 }
5893 return 0;
5894}
5895
Al Viro9cdda8d2013-05-22 16:48:09 -04005896static int btrfs_real_readdir(struct file *file, struct dir_context *ctx)
Chris Mason39279cc2007-06-12 06:35:45 -04005897{
Al Viro9cdda8d2013-05-22 16:48:09 -04005898 struct inode *inode = file_inode(file);
Chris Mason39279cc2007-06-12 06:35:45 -04005899 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005900 struct btrfs_file_private *private = file->private_data;
Chris Mason39279cc2007-06-12 06:35:45 -04005901 struct btrfs_dir_item *di;
5902 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04005903 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04005904 struct btrfs_path *path;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005905 void *addr;
Miao Xie16cdcec2011-04-22 18:12:22 +08005906 struct list_head ins_list;
5907 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04005908 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04005909 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04005910 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04005911 char *name_ptr;
5912 int name_len;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005913 int entries = 0;
5914 int total_len = 0;
Omar Sandoval02dbfc92016-05-20 13:50:33 -07005915 bool put = false;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005916 struct btrfs_key location;
Chris Mason5f39d392007-10-15 16:14:19 -04005917
Al Viro9cdda8d2013-05-22 16:48:09 -04005918 if (!dir_emit_dots(file, ctx))
5919 return 0;
5920
David Woodhouse49593bf2008-08-17 17:08:36 +01005921 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08005922 if (!path)
5923 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04005924
Josef Bacik23b5ec72017-07-24 15:14:25 -04005925 addr = private->filldir_buf;
David Sterbae4058b52015-11-27 16:31:35 +01005926 path->reada = READA_FORWARD;
David Woodhouse49593bf2008-08-17 17:08:36 +01005927
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005928 INIT_LIST_HEAD(&ins_list);
5929 INIT_LIST_HEAD(&del_list);
5930 put = btrfs_readdir_get_delayed_items(inode, &ins_list, &del_list);
Miao Xie16cdcec2011-04-22 18:12:22 +08005931
Josef Bacik23b5ec72017-07-24 15:14:25 -04005932again:
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005933 key.type = BTRFS_DIR_INDEX_KEY;
Al Viro9cdda8d2013-05-22 16:48:09 -04005934 key.offset = ctx->pos;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005935 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason5f39d392007-10-15 16:14:19 -04005936
Chris Mason39279cc2007-06-12 06:35:45 -04005937 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5938 if (ret < 0)
5939 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01005940
5941 while (1) {
Josef Bacik23b5ec72017-07-24 15:14:25 -04005942 struct dir_entry *entry;
5943
Chris Mason5f39d392007-10-15 16:14:19 -04005944 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04005945 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08005946 if (slot >= btrfs_header_nritems(leaf)) {
5947 ret = btrfs_next_leaf(root, path);
5948 if (ret < 0)
5949 goto err;
5950 else if (ret > 0)
5951 break;
5952 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04005953 }
Chris Mason3de45862008-11-17 21:02:50 -05005954
Chris Mason5f39d392007-10-15 16:14:19 -04005955 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5956
5957 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04005958 break;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005959 if (found_key.type != BTRFS_DIR_INDEX_KEY)
Chris Mason39279cc2007-06-12 06:35:45 -04005960 break;
Al Viro9cdda8d2013-05-22 16:48:09 -04005961 if (found_key.offset < ctx->pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08005962 goto next;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005963 if (btrfs_should_delete_dir_index(&del_list, found_key.offset))
Miao Xie16cdcec2011-04-22 18:12:22 +08005964 goto next;
Chris Mason39279cc2007-06-12 06:35:45 -04005965 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005966 name_len = btrfs_dir_name_len(leaf, di);
Josef Bacik23b5ec72017-07-24 15:14:25 -04005967 if ((total_len + sizeof(struct dir_entry) + name_len) >=
5968 PAGE_SIZE) {
5969 btrfs_release_path(path);
5970 ret = btrfs_filldir(private->filldir_buf, entries, ctx);
5971 if (ret)
5972 goto nopos;
5973 addr = private->filldir_buf;
5974 entries = 0;
5975 total_len = 0;
5976 goto again;
Chris Mason39279cc2007-06-12 06:35:45 -04005977 }
Josef Bacik23b5ec72017-07-24 15:14:25 -04005978
5979 entry = addr;
David Sterba92d32172018-04-16 21:10:14 +02005980 put_unaligned(name_len, &entry->name_len);
Josef Bacik23b5ec72017-07-24 15:14:25 -04005981 name_ptr = (char *)(entry + 1);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005982 read_extent_buffer(leaf, name_ptr, (unsigned long)(di + 1),
5983 name_len);
Phillip Potter7d157c32019-03-26 21:39:34 +00005984 put_unaligned(fs_ftype_to_dtype(btrfs_dir_type(leaf, di)),
David Sterba92d32172018-04-16 21:10:14 +02005985 &entry->type);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005986 btrfs_dir_item_key_to_cpu(leaf, di, &location);
David Sterba92d32172018-04-16 21:10:14 +02005987 put_unaligned(location.objectid, &entry->ino);
5988 put_unaligned(found_key.offset, &entry->offset);
Josef Bacik23b5ec72017-07-24 15:14:25 -04005989 entries++;
5990 addr += sizeof(struct dir_entry) + name_len;
5991 total_len += sizeof(struct dir_entry) + name_len;
Li Zefanb9e03af2011-03-23 10:43:58 +08005992next:
5993 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04005994 }
Josef Bacik23b5ec72017-07-24 15:14:25 -04005995 btrfs_release_path(path);
5996
5997 ret = btrfs_filldir(private->filldir_buf, entries, ctx);
5998 if (ret)
5999 goto nopos;
David Woodhouse49593bf2008-08-17 17:08:36 +01006000
Jeff Mahoneyd2fbb2b2016-11-05 13:26:35 -04006001 ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006002 if (ret)
David Sterbabc4ef752015-11-13 13:44:28 +01006003 goto nopos;
6004
Zach Browndb62efb2013-07-11 16:19:42 -07006005 /*
6006 * Stop new entries from being returned after we return the last
6007 * entry.
6008 *
6009 * New directory entries are assigned a strictly increasing
6010 * offset. This means that new entries created during readdir
6011 * are *guaranteed* to be seen in the future by that readdir.
6012 * This has broken buggy programs which operate on names as
6013 * they're returned by readdir. Until we re-use freed offsets
6014 * we have this hack to stop new entries from being returned
6015 * under the assumption that they'll never reach this huge
6016 * offset.
6017 *
6018 * This is being careful not to overflow 32bit loff_t unless the
6019 * last entry requires it because doing so has broken 32bit apps
6020 * in the past.
6021 */
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006022 if (ctx->pos >= INT_MAX)
6023 ctx->pos = LLONG_MAX;
6024 else
6025 ctx->pos = INT_MAX;
Chris Mason39279cc2007-06-12 06:35:45 -04006026nopos:
6027 ret = 0;
6028err:
Omar Sandoval02dbfc92016-05-20 13:50:33 -07006029 if (put)
6030 btrfs_readdir_put_delayed_items(inode, &ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04006031 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04006032 return ret;
6033}
6034
Chris Mason39279cc2007-06-12 06:35:45 -04006035/*
Chris Mason54aa1f42007-06-22 14:16:25 -04006036 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04006037 * inode changes. But, it is most likely to find the inode in cache.
6038 * FIXME, needs more benchmarking...there are no reasons other than performance
6039 * to keep or drop this code.
6040 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00006041static int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04006042{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006043 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04006044 struct btrfs_root *root = BTRFS_I(inode)->root;
6045 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006046 int ret;
6047
Josef Bacik72ac3c02012-05-23 14:13:11 -04006048 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05006049 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006050
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006051 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006052 if (IS_ERR(trans))
6053 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006054
6055 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04006056 if (ret && ret == -ENOSPC) {
6057 /* whoops, lets try again with the full transaction */
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006058 btrfs_end_transaction(trans);
Chris Mason94b60442010-05-26 11:02:00 -04006059 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006060 if (IS_ERR(trans))
6061 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006062
Chris Mason94b60442010-05-26 11:02:00 -04006063 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04006064 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006065 btrfs_end_transaction(trans);
Miao Xie16cdcec2011-04-22 18:12:22 +08006066 if (BTRFS_I(inode)->delayed_node)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006067 btrfs_balance_delayed_items(fs_info);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006068
6069 return ret;
6070}
6071
6072/*
6073 * This is a copy of file_update_time. We need this so we can return error on
6074 * ENOSPC for updating the inode in the case of file write and mmap writes.
6075 */
Deepa Dinamani95582b02018-05-08 19:36:02 -07006076static int btrfs_update_time(struct inode *inode, struct timespec64 *now,
Josef Bacike41f9412012-03-26 09:46:47 -04006077 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05006078{
Alexander Block2bc5565282012-06-15 09:49:33 +02006079 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Layton3a8c7232017-12-11 06:35:24 -05006080 bool dirty = flags & ~S_VERSION;
Alexander Block2bc5565282012-06-15 09:49:33 +02006081
6082 if (btrfs_root_readonly(root))
6083 return -EROFS;
6084
Josef Bacike41f9412012-03-26 09:46:47 -04006085 if (flags & S_VERSION)
Jeff Layton3a8c7232017-12-11 06:35:24 -05006086 dirty |= inode_maybe_inc_iversion(inode, dirty);
Josef Bacike41f9412012-03-26 09:46:47 -04006087 if (flags & S_CTIME)
6088 inode->i_ctime = *now;
6089 if (flags & S_MTIME)
6090 inode->i_mtime = *now;
6091 if (flags & S_ATIME)
6092 inode->i_atime = *now;
Jeff Layton3a8c7232017-12-11 06:35:24 -05006093 return dirty ? btrfs_dirty_inode(inode) : 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006094}
6095
Chris Masond352ac62008-09-29 15:18:18 -04006096/*
6097 * find the highest existing sequence number in a directory
6098 * and then set the in-memory index_cnt variable to reflect
6099 * free sequence numbers
6100 */
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006101static int btrfs_set_inode_index_count(struct btrfs_inode *inode)
Josef Bacikaec74772008-07-24 12:12:38 -04006102{
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006103 struct btrfs_root *root = inode->root;
Josef Bacikaec74772008-07-24 12:12:38 -04006104 struct btrfs_key key, found_key;
6105 struct btrfs_path *path;
6106 struct extent_buffer *leaf;
6107 int ret;
6108
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006109 key.objectid = btrfs_ino(inode);
David Sterba962a2982014-06-04 18:41:45 +02006110 key.type = BTRFS_DIR_INDEX_KEY;
Josef Bacikaec74772008-07-24 12:12:38 -04006111 key.offset = (u64)-1;
6112
6113 path = btrfs_alloc_path();
6114 if (!path)
6115 return -ENOMEM;
6116
6117 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
6118 if (ret < 0)
6119 goto out;
6120 /* FIXME: we should be able to handle this */
6121 if (ret == 0)
6122 goto out;
6123 ret = 0;
6124
6125 /*
6126 * MAGIC NUMBER EXPLANATION:
6127 * since we search a directory based on f_pos we have to start at 2
6128 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
6129 * else has to start at 2
6130 */
6131 if (path->slots[0] == 0) {
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006132 inode->index_cnt = 2;
Josef Bacikaec74772008-07-24 12:12:38 -04006133 goto out;
6134 }
6135
6136 path->slots[0]--;
6137
6138 leaf = path->nodes[0];
6139 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6140
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006141 if (found_key.objectid != btrfs_ino(inode) ||
David Sterba962a2982014-06-04 18:41:45 +02006142 found_key.type != BTRFS_DIR_INDEX_KEY) {
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006143 inode->index_cnt = 2;
Josef Bacikaec74772008-07-24 12:12:38 -04006144 goto out;
6145 }
6146
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006147 inode->index_cnt = found_key.offset + 1;
Josef Bacikaec74772008-07-24 12:12:38 -04006148out:
6149 btrfs_free_path(path);
6150 return ret;
6151}
6152
Chris Masond352ac62008-09-29 15:18:18 -04006153/*
6154 * helper to find a free sequence number in a given directory. This current
6155 * code is very simple, later versions will do smarter things in the btree
6156 */
Nikolay Borisov877574e2017-02-20 13:50:33 +02006157int btrfs_set_inode_index(struct btrfs_inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04006158{
6159 int ret = 0;
6160
Nikolay Borisov877574e2017-02-20 13:50:33 +02006161 if (dir->index_cnt == (u64)-1) {
6162 ret = btrfs_inode_delayed_dir_index_count(dir);
Miao Xie16cdcec2011-04-22 18:12:22 +08006163 if (ret) {
6164 ret = btrfs_set_inode_index_count(dir);
6165 if (ret)
6166 return ret;
6167 }
Josef Bacikaec74772008-07-24 12:12:38 -04006168 }
6169
Nikolay Borisov877574e2017-02-20 13:50:33 +02006170 *index = dir->index_cnt;
6171 dir->index_cnt++;
Josef Bacikaec74772008-07-24 12:12:38 -04006172
6173 return ret;
6174}
6175
Chris Masonb0d5d102014-09-08 13:08:51 -07006176static int btrfs_insert_inode_locked(struct inode *inode)
6177{
6178 struct btrfs_iget_args args;
6179 args.location = &BTRFS_I(inode)->location;
6180 args.root = BTRFS_I(inode)->root;
6181
6182 return insert_inode_locked4(inode,
6183 btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root),
6184 btrfs_find_actor, &args);
6185}
6186
Anand Jain19aee8d2017-07-18 17:37:05 +08006187/*
6188 * Inherit flags from the parent inode.
6189 *
6190 * Currently only the compression flags and the cow flags are inherited.
6191 */
6192static void btrfs_inherit_iflags(struct inode *inode, struct inode *dir)
6193{
6194 unsigned int flags;
6195
6196 if (!dir)
6197 return;
6198
6199 flags = BTRFS_I(dir)->flags;
6200
6201 if (flags & BTRFS_INODE_NOCOMPRESS) {
6202 BTRFS_I(inode)->flags &= ~BTRFS_INODE_COMPRESS;
6203 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
6204 } else if (flags & BTRFS_INODE_COMPRESS) {
6205 BTRFS_I(inode)->flags &= ~BTRFS_INODE_NOCOMPRESS;
6206 BTRFS_I(inode)->flags |= BTRFS_INODE_COMPRESS;
6207 }
6208
6209 if (flags & BTRFS_INODE_NODATACOW) {
6210 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
6211 if (S_ISREG(inode->i_mode))
6212 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
6213 }
6214
David Sterba7b6a2212018-03-26 18:40:21 +02006215 btrfs_sync_inode_flags_to_i_flags(inode);
Anand Jain19aee8d2017-07-18 17:37:05 +08006216}
6217
Chris Mason39279cc2007-06-12 06:35:45 -04006218static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
6219 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04006220 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05006221 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04006222 u64 ref_objectid, u64 objectid,
6223 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04006224{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006225 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04006226 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006227 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04006228 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04006229 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05006230 struct btrfs_inode_ref *ref;
6231 struct btrfs_key key[2];
6232 u32 sizes[2];
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006233 int nitems = name ? 2 : 1;
Chris Mason9c583092008-01-29 15:15:18 -05006234 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04006235 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006236
Chris Mason5f39d392007-10-15 16:14:19 -04006237 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07006238 if (!path)
6239 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04006240
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006241 inode = new_inode(fs_info->sb);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006242 if (!inode) {
6243 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04006244 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006245 }
Chris Mason39279cc2007-06-12 06:35:45 -04006246
Li Zefan581bb052011-04-20 10:06:11 +08006247 /*
Filipe Manana5762b5c2014-08-01 00:10:32 +01006248 * O_TMPFILE, set link count to 0, so that after this point,
6249 * we fill in an inode item with the correct link count.
6250 */
6251 if (!name)
6252 set_nlink(inode, 0);
6253
6254 /*
Li Zefan581bb052011-04-20 10:06:11 +08006255 * we have to initialize this early, so we can reclaim the inode
6256 * number if we fail afterwards in this function.
6257 */
6258 inode->i_ino = objectid;
6259
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006260 if (dir && name) {
liubo1abe9b82011-03-24 11:18:59 +00006261 trace_btrfs_inode_request(dir);
6262
Nikolay Borisov877574e2017-02-20 13:50:33 +02006263 ret = btrfs_set_inode_index(BTRFS_I(dir), index);
Shen Feng09771432009-04-02 16:46:06 -04006264 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006265 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04006266 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04006267 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04006268 }
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006269 } else if (dir) {
6270 *index = 0;
Josef Bacikaec74772008-07-24 12:12:38 -04006271 }
6272 /*
6273 * index_cnt is ignored for everything but a dir,
Su Yuedf6703e2018-01-12 11:08:02 +08006274 * btrfs_set_inode_index_count has an explanation for the magic
Josef Bacikaec74772008-07-24 12:12:38 -04006275 * number
6276 */
6277 BTRFS_I(inode)->index_cnt = 2;
Miao Xie67de1172013-12-26 13:07:06 +08006278 BTRFS_I(inode)->dir_index = *index;
Chris Mason39279cc2007-06-12 06:35:45 -04006279 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04006280 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00006281 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db22007-08-27 16:49:44 -04006282
Josef Bacik5dc562c2012-08-17 13:14:17 -04006283 /*
6284 * We could have gotten an inode number from somebody who was fsynced
6285 * and then removed in this same transaction, so let's just set full
6286 * sync since it will be a full sync anyway and this will blow away the
6287 * old info in the log.
6288 */
6289 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
6290
Chris Mason9c583092008-01-29 15:15:18 -05006291 key[0].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006292 key[0].type = BTRFS_INODE_ITEM_KEY;
Chris Mason9c583092008-01-29 15:15:18 -05006293 key[0].offset = 0;
6294
Chris Mason9c583092008-01-29 15:15:18 -05006295 sizes[0] = sizeof(struct btrfs_inode_item);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006296
6297 if (name) {
6298 /*
6299 * Start new inodes with an inode_ref. This is slightly more
6300 * efficient for small numbers of hard links since they will
6301 * be packed into one item. Extended refs will kick in if we
6302 * add more hard links than can fit in the ref item.
6303 */
6304 key[1].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006305 key[1].type = BTRFS_INODE_REF_KEY;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006306 key[1].offset = ref_objectid;
6307
6308 sizes[1] = name_len + sizeof(*ref);
6309 }
Chris Mason9c583092008-01-29 15:15:18 -05006310
Chris Masonb0d5d102014-09-08 13:08:51 -07006311 location = &BTRFS_I(inode)->location;
6312 location->objectid = objectid;
6313 location->offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02006314 location->type = BTRFS_INODE_ITEM_KEY;
Chris Masonb0d5d102014-09-08 13:08:51 -07006315
6316 ret = btrfs_insert_inode_locked(inode);
Al Viro32955c52018-05-16 12:20:05 -04006317 if (ret < 0) {
6318 iput(inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006319 goto fail;
Al Viro32955c52018-05-16 12:20:05 -04006320 }
Chris Masonb0d5d102014-09-08 13:08:51 -07006321
Chris Masonb9473432009-03-13 11:00:37 -04006322 path->leave_spinning = 1;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006323 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems);
Chris Mason9c583092008-01-29 15:15:18 -05006324 if (ret != 0)
Chris Masonb0d5d102014-09-08 13:08:51 -07006325 goto fail_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04006326
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03006327 inode_init_owner(inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04006328 inode_set_bytes(inode, 0);
chandan r9cc97d62012-07-04 12:48:07 +05306329
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006330 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05306331 inode->i_atime = inode->i_mtime;
6332 inode->i_ctime = inode->i_mtime;
Arnd Bergmannd3c6be6f2018-06-21 18:04:06 +02006333 BTRFS_I(inode)->i_otime = inode->i_mtime;
chandan r9cc97d62012-07-04 12:48:07 +05306334
Chris Mason5f39d392007-10-15 16:14:19 -04006335 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
6336 struct btrfs_inode_item);
David Sterbab159fa22016-11-08 18:09:03 +01006337 memzero_extent_buffer(path->nodes[0], (unsigned long)inode_item,
Li Zefan293f7e02012-07-10 00:58:58 -06006338 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04006339 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05006340
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006341 if (name) {
6342 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
6343 struct btrfs_inode_ref);
6344 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
6345 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
6346 ptr = (unsigned long)(ref + 1);
6347 write_extent_buffer(path->nodes[0], name, ptr, name_len);
6348 }
Chris Mason9c583092008-01-29 15:15:18 -05006349
Chris Mason5f39d392007-10-15 16:14:19 -04006350 btrfs_mark_buffer_dirty(path->nodes[0]);
6351 btrfs_free_path(path);
6352
Christoph Hellwig6cbff002009-04-17 10:37:41 +02006353 btrfs_inherit_iflags(inode, dir);
6354
Al Viro569254b2011-07-24 17:08:40 -04006355 if (S_ISREG(mode)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006356 if (btrfs_test_opt(fs_info, NODATASUM))
Chris Mason94272162009-07-02 12:26:06 -04006357 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006358 if (btrfs_test_opt(fs_info, NODATACOW))
Josef Bacikf2bdf9a2013-02-21 15:28:28 -05006359 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
6360 BTRFS_INODE_NODATASUM;
Chris Mason94272162009-07-02 12:26:06 -04006361 }
6362
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006363 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00006364
6365 trace_btrfs_inode_new(inode);
Chris Mason1973f0f2011-06-24 13:13:29 -04006366 btrfs_set_inode_last_trans(trans, inode);
liubo1abe9b82011-03-24 11:18:59 +00006367
Alexander Block8ea05e32012-07-25 17:35:53 +02006368 btrfs_update_root_times(trans, root);
6369
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006370 ret = btrfs_inode_inherit_props(trans, inode, dir);
6371 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006372 btrfs_err(fs_info,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006373 "error inheriting props for ino %llu (root %llu): %d",
David Sterbaf85b7372017-01-20 14:54:07 +01006374 btrfs_ino(BTRFS_I(inode)), root->root_key.objectid, ret);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006375
Chris Mason39279cc2007-06-12 06:35:45 -04006376 return inode;
Chris Masonb0d5d102014-09-08 13:08:51 -07006377
6378fail_unlock:
Al Viro32955c52018-05-16 12:20:05 -04006379 discard_new_inode(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006380fail:
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006381 if (dir && name)
Josef Bacikaec74772008-07-24 12:12:38 -04006382 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04006383 btrfs_free_path(path);
6384 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006385}
6386
Chris Masond352ac62008-09-29 15:18:18 -04006387/*
6388 * utility function to add 'inode' into 'parent_inode' with
6389 * a give name and a given sequence number.
6390 * if 'add_backref' is true, also insert a backref from the
6391 * inode to the parent directory.
6392 */
Chris Masone02119d2008-09-05 16:13:11 -04006393int btrfs_add_link(struct btrfs_trans_handle *trans,
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006394 struct btrfs_inode *parent_inode, struct btrfs_inode *inode,
Chris Masone02119d2008-09-05 16:13:11 -04006395 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006396{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006397 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006398 struct btrfs_key key;
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006399 struct btrfs_root *root = parent_inode->root;
6400 u64 ino = btrfs_ino(inode);
6401 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006402
Li Zefan33345d012011-04-20 10:31:50 +08006403 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006404 memcpy(&key, &inode->root->root_key, sizeof(key));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006405 } else {
Li Zefan33345d012011-04-20 10:31:50 +08006406 key.objectid = ino;
David Sterba962a2982014-06-04 18:41:45 +02006407 key.type = BTRFS_INODE_ITEM_KEY;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006408 key.offset = 0;
6409 }
Chris Mason39279cc2007-06-12 06:35:45 -04006410
Li Zefan33345d012011-04-20 10:31:50 +08006411 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Lu Fengqi6025c192018-08-01 11:32:29 +08006412 ret = btrfs_add_root_ref(trans, key.objectid,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006413 root->root_key.objectid, parent_ino,
6414 index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006415 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08006416 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
6417 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006418 }
6419
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006420 /* Nothing to clean up yet */
6421 if (ret)
6422 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006423
Lu Fengqi684572d2018-08-04 21:10:57 +08006424 ret = btrfs_insert_dir_item(trans, name, name_len, parent_inode, &key,
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006425 btrfs_inode_type(&inode->vfs_inode), index);
Chris Mason9c520572012-12-17 14:26:57 -05006426 if (ret == -EEXIST || ret == -EOVERFLOW)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006427 goto fail_dir_item;
6428 else if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04006429 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006430 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006431 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006432
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006433 btrfs_i_size_write(parent_inode, parent_inode->vfs_inode.i_size +
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006434 name_len * 2);
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006435 inode_inc_iversion(&parent_inode->vfs_inode);
Filipe Manana5338e432019-05-15 16:02:47 +01006436 /*
6437 * If we are replaying a log tree, we do not want to update the mtime
6438 * and ctime of the parent directory with the current time, since the
6439 * log replay procedure is responsible for setting them to their correct
6440 * values (the ones it had when the fsync was done).
6441 */
6442 if (!test_bit(BTRFS_FS_LOG_RECOVERING, &root->fs_info->flags)) {
6443 struct timespec64 now = current_time(&parent_inode->vfs_inode);
6444
6445 parent_inode->vfs_inode.i_mtime = now;
6446 parent_inode->vfs_inode.i_ctime = now;
6447 }
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006448 ret = btrfs_update_inode(trans, root, &parent_inode->vfs_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006449 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04006450 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006451 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05006452
6453fail_dir_item:
6454 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
6455 u64 local_index;
6456 int err;
Lu Fengqi3ee1c552018-08-01 11:32:28 +08006457 err = btrfs_del_root_ref(trans, key.objectid,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006458 root->root_key.objectid, parent_ino,
6459 &local_index, name, name_len);
Johannes Thumshirn1690dd412018-12-12 15:14:17 +01006460 if (err)
6461 btrfs_abort_transaction(trans, err);
Chris Masonfe66a052012-02-20 08:40:56 -05006462 } else if (add_backref) {
6463 u64 local_index;
6464 int err;
6465
6466 err = btrfs_del_inode_ref(trans, root, name, name_len,
6467 ino, parent_ino, &local_index);
Johannes Thumshirn1690dd412018-12-12 15:14:17 +01006468 if (err)
6469 btrfs_abort_transaction(trans, err);
Chris Masonfe66a052012-02-20 08:40:56 -05006470 }
Johannes Thumshirn1690dd412018-12-12 15:14:17 +01006471
6472 /* Return the original error code */
Chris Masonfe66a052012-02-20 08:40:56 -05006473 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006474}
6475
6476static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006477 struct btrfs_inode *dir, struct dentry *dentry,
6478 struct btrfs_inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006479{
Josef Bacika1b075d2010-11-19 20:36:11 +00006480 int err = btrfs_add_link(trans, dir, inode,
6481 dentry->d_name.name, dentry->d_name.len,
6482 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006483 if (err > 0)
6484 err = -EEXIST;
6485 return err;
6486}
6487
Josef Bacik618e21d2007-07-11 10:18:17 -04006488static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04006489 umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04006490{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006491 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Josef Bacik618e21d2007-07-11 10:18:17 -04006492 struct btrfs_trans_handle *trans;
6493 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006494 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04006495 int err;
Josef Bacik618e21d2007-07-11 10:18:17 -04006496 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006497 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04006498
Josef Bacik9ed74f22009-09-11 16:12:44 -04006499 /*
6500 * 2 for inode item and ref
6501 * 2 for dir items
6502 * 1 for xattr if selinux is on
6503 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006504 trans = btrfs_start_transaction(root, 5);
6505 if (IS_ERR(trans))
6506 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05006507
Li Zefan581bb052011-04-20 10:06:11 +08006508 err = btrfs_find_free_ino(root, &objectid);
6509 if (err)
6510 goto out_unlock;
6511
Josef Bacikaec74772008-07-24 12:12:38 -04006512 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006513 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6514 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006515 if (IS_ERR(inode)) {
6516 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -04006517 inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04006518 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006519 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006520
Casey Schauflerad19db72011-12-15 10:09:07 -05006521 /*
6522 * If the active LSM wants to access the inode during
6523 * d_instantiate it needs these. Smack checks to see
6524 * if the filesystem supports xattrs by looking at the
6525 * ops vector.
6526 */
Casey Schauflerad19db72011-12-15 10:09:07 -05006527 inode->i_op = &btrfs_special_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006528 init_special_inode(inode, inode->i_mode, rdev);
6529
6530 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik618e21d2007-07-11 10:18:17 -04006531 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006532 goto out_unlock;
Chris Masonb0d5d102014-09-08 13:08:51 -07006533
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006534 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6535 0, index);
Al Viro32955c52018-05-16 12:20:05 -04006536 if (err)
6537 goto out_unlock;
6538
6539 btrfs_update_inode(trans, root, inode);
6540 d_instantiate_new(dentry, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006541
Josef Bacik618e21d2007-07-11 10:18:17 -04006542out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006543 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006544 btrfs_btree_balance_dirty(fs_info);
Al Viro32955c52018-05-16 12:20:05 -04006545 if (err && inode) {
Josef Bacik618e21d2007-07-11 10:18:17 -04006546 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -04006547 discard_new_inode(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006548 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006549 return err;
6550}
6551
Chris Mason39279cc2007-06-12 06:35:45 -04006552static int btrfs_create(struct inode *dir, struct dentry *dentry,
Al Viroebfc3b42012-06-10 18:05:36 -04006553 umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -04006554{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006555 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04006556 struct btrfs_trans_handle *trans;
6557 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006558 struct inode *inode = NULL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006559 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04006560 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006561 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006562
Josef Bacik9ed74f22009-09-11 16:12:44 -04006563 /*
6564 * 2 for inode item and ref
6565 * 2 for dir items
6566 * 1 for xattr if selinux is on
6567 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006568 trans = btrfs_start_transaction(root, 5);
6569 if (IS_ERR(trans))
6570 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006571
Li Zefan581bb052011-04-20 10:06:11 +08006572 err = btrfs_find_free_ino(root, &objectid);
6573 if (err)
6574 goto out_unlock;
6575
Josef Bacikaec74772008-07-24 12:12:38 -04006576 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006577 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6578 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006579 if (IS_ERR(inode)) {
6580 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -04006581 inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006582 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006583 }
Casey Schauflerad19db72011-12-15 10:09:07 -05006584 /*
6585 * If the active LSM wants to access the inode during
6586 * d_instantiate it needs these. Smack checks to see
6587 * if the filesystem supports xattrs by looking at the
6588 * ops vector.
6589 */
6590 inode->i_fop = &btrfs_file_operations;
6591 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006592 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006593
6594 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
6595 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006596 goto out_unlock;
Chris Masonb0d5d102014-09-08 13:08:51 -07006597
6598 err = btrfs_update_inode(trans, root, inode);
6599 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006600 goto out_unlock;
Casey Schauflerad19db72011-12-15 10:09:07 -05006601
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006602 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6603 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006604 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006605 goto out_unlock;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006606
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006607 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Al Viro1e2e5472018-05-04 08:23:01 -04006608 d_instantiate_new(dentry, inode);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006609
Chris Mason39279cc2007-06-12 06:35:45 -04006610out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006611 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -04006612 if (err && inode) {
Chris Mason39279cc2007-06-12 06:35:45 -04006613 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -04006614 discard_new_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006615 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006616 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006617 return err;
6618}
6619
6620static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
6621 struct dentry *dentry)
6622{
Filipe Manana271dba452016-01-05 16:24:05 +00006623 struct btrfs_trans_handle *trans = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006624 struct btrfs_root *root = BTRFS_I(dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00006625 struct inode *inode = d_inode(old_dentry);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006626 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason00e4e6b2008-08-05 11:18:09 -04006627 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04006628 int err;
6629 int drop_inode = 0;
6630
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006631 /* do not allow sys_link's with other subvols of the same device */
Misono Tomohiro4fd786e2018-08-06 14:25:24 +09006632 if (root->root_key.objectid != BTRFS_I(inode)->root->root_key.objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00006633 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006634
Mark Fashehf1863732012-08-08 11:32:27 -07006635 if (inode->i_nlink >= BTRFS_LINK_MAX)
Al Viroc055e992011-03-04 17:15:18 +00006636 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04006637
Nikolay Borisov877574e2017-02-20 13:50:33 +02006638 err = btrfs_set_inode_index(BTRFS_I(dir), &index);
Josef Bacikaec74772008-07-24 12:12:38 -04006639 if (err)
6640 goto fail;
6641
Yan, Zhenga22285a2010-05-16 10:48:46 -04006642 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00006643 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04006644 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00006645 * 1 item for parent inode
Omar Sandoval399b0bb2018-05-11 13:13:40 -07006646 * 1 item for orphan item deletion if O_TMPFILE
Yan, Zhenga22285a2010-05-16 10:48:46 -04006647 */
Omar Sandoval399b0bb2018-05-11 13:13:40 -07006648 trans = btrfs_start_transaction(root, inode->i_nlink ? 5 : 6);
Yan, Zhenga22285a2010-05-16 10:48:46 -04006649 if (IS_ERR(trans)) {
6650 err = PTR_ERR(trans);
Filipe Manana271dba452016-01-05 16:24:05 +00006651 trans = NULL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006652 goto fail;
6653 }
Chris Mason5f39d392007-10-15 16:14:19 -04006654
Miao Xie67de1172013-12-26 13:07:06 +08006655 /* There are several dir indexes for this inode, clear the cache. */
6656 BTRFS_I(inode)->dir_index = 0ULL;
Zach Brown8b558c52013-10-16 12:10:34 -07006657 inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04006658 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006659 inode->i_ctime = current_time(inode);
Al Viro7de9c6ee2010-10-23 11:11:40 -04006660 ihold(inode);
Josef Bacike9976152012-10-11 15:53:56 -04006661 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
Josef Bacikaec74772008-07-24 12:12:38 -04006662
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006663 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6664 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04006665
Yan, Zhenga5719522009-09-24 09:17:31 -04006666 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006667 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04006668 } else {
Al Viro10d9f302011-07-16 23:09:10 -04006669 struct dentry *parent = dentry->d_parent;
Filipe Mananad4682ba2018-06-11 19:24:28 +01006670 int ret;
6671
Yan, Zhenga5719522009-09-24 09:17:31 -04006672 err = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006673 if (err)
6674 goto fail;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006675 if (inode->i_nlink == 1) {
6676 /*
6677 * If new hard link count is 1, it's a file created
6678 * with open(2) O_TMPFILE flag.
6679 */
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02006680 err = btrfs_orphan_del(trans, BTRFS_I(inode));
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006681 if (err)
6682 goto fail;
6683 }
Al Viro08c422c2011-12-23 07:58:13 -05006684 d_instantiate(dentry, inode);
Filipe Mananad4682ba2018-06-11 19:24:28 +01006685 ret = btrfs_log_new_name(trans, BTRFS_I(inode), NULL, parent,
6686 true, NULL);
6687 if (ret == BTRFS_NEED_TRANS_COMMIT) {
6688 err = btrfs_commit_transaction(trans);
6689 trans = NULL;
6690 }
Yan, Zhenga5719522009-09-24 09:17:31 -04006691 }
Chris Mason39279cc2007-06-12 06:35:45 -04006692
Chris Mason1832a6d2007-12-21 16:27:21 -05006693fail:
Filipe Manana271dba452016-01-05 16:24:05 +00006694 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006695 btrfs_end_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006696 if (drop_inode) {
6697 inode_dec_link_count(inode);
6698 iput(inode);
6699 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006700 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006701 return err;
6702}
6703
Al Viro18bb1db2011-07-26 01:41:39 -04006704static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04006705{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006706 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masonb9d86662008-05-02 16:13:49 -04006707 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006708 struct btrfs_trans_handle *trans;
6709 struct btrfs_root *root = BTRFS_I(dir)->root;
6710 int err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04006711 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006712 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006713
Josef Bacik9ed74f22009-09-11 16:12:44 -04006714 /*
6715 * 2 items for inode and ref
6716 * 2 items for dir items
6717 * 1 for xattr if selinux is on
6718 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006719 trans = btrfs_start_transaction(root, 5);
6720 if (IS_ERR(trans))
6721 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006722
Li Zefan581bb052011-04-20 10:06:11 +08006723 err = btrfs_find_free_ino(root, &objectid);
6724 if (err)
6725 goto out_fail;
6726
Josef Bacikaec74772008-07-24 12:12:38 -04006727 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006728 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6729 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04006730 if (IS_ERR(inode)) {
6731 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -04006732 inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006733 goto out_fail;
6734 }
Chris Mason5f39d392007-10-15 16:14:19 -04006735
Chris Masonb0d5d102014-09-08 13:08:51 -07006736 /* these must be set before we unlock the inode */
6737 inode->i_op = &btrfs_dir_inode_operations;
6738 inode->i_fop = &btrfs_dir_file_operations;
Josef Bacik33268ea2008-07-24 12:16:36 -04006739
Eric Paris2a7dba32011-02-01 11:05:39 -05006740 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04006741 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006742 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04006743
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02006744 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Mason39279cc2007-06-12 06:35:45 -04006745 err = btrfs_update_inode(trans, root, inode);
6746 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006747 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04006748
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006749 err = btrfs_add_link(trans, BTRFS_I(dir), BTRFS_I(inode),
6750 dentry->d_name.name,
6751 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006752 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006753 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04006754
Al Viro1e2e5472018-05-04 08:23:01 -04006755 d_instantiate_new(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006756
6757out_fail:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006758 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -04006759 if (err && inode) {
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006760 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -04006761 discard_new_inode(inode);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006762 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006763 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006764 return err;
6765}
6766
Chris Masonc8b97812008-10-29 14:49:59 -04006767static noinline int uncompress_inline(struct btrfs_path *path,
Byongho Leee40da0e2015-05-19 23:46:45 +09006768 struct page *page,
Chris Masonc8b97812008-10-29 14:49:59 -04006769 size_t pg_offset, u64 extent_offset,
6770 struct btrfs_file_extent_item *item)
6771{
6772 int ret;
6773 struct extent_buffer *leaf = path->nodes[0];
6774 char *tmp;
6775 size_t max_size;
6776 unsigned long inline_size;
6777 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08006778 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04006779
6780 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08006781 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04006782 max_size = btrfs_file_extent_ram_bytes(leaf, item);
6783 inline_size = btrfs_file_extent_inline_item_len(leaf,
Ross Kirkdd3cc162013-09-16 15:58:09 +01006784 btrfs_item_nr(path->slots[0]));
Chris Masonc8b97812008-10-29 14:49:59 -04006785 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04006786 if (!tmp)
6787 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04006788 ptr = btrfs_file_extent_inline_start(item);
6789
6790 read_extent_buffer(leaf, tmp, ptr, inline_size);
6791
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006792 max_size = min_t(unsigned long, PAGE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08006793 ret = btrfs_decompress(compress_type, tmp, page,
6794 extent_offset, inline_size, max_size);
Zygo Blaxelle1699d22017-03-10 16:45:44 -05006795
6796 /*
6797 * decompression code contains a memset to fill in any space between the end
6798 * of the uncompressed data and the end of max_size in case the decompressed
6799 * data ends up shorter than ram_bytes. That doesn't cover the hole between
6800 * the end of an inline extent and the beginning of the next block, so we
6801 * cover that region here.
6802 */
6803
6804 if (max_size + pg_offset < PAGE_SIZE) {
6805 char *map = kmap(page);
6806 memset(map + pg_offset + max_size, 0, PAGE_SIZE - max_size - pg_offset);
6807 kunmap(page);
6808 }
Chris Masonc8b97812008-10-29 14:49:59 -04006809 kfree(tmp);
Zach Brown166ae5a2014-05-09 17:15:10 -04006810 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -04006811}
6812
Chris Masond352ac62008-09-29 15:18:18 -04006813/*
6814 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05006815 * the ugly parts come from merging extents from the disk with the in-ram
6816 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04006817 * where the in-ram extents might be locked pending data=ordered completion.
6818 *
6819 * This also copies inline extents directly into the page.
6820 */
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006821struct extent_map *btrfs_get_extent(struct btrfs_inode *inode,
Liu Bode2c6612018-08-25 13:47:59 +08006822 struct page *page,
6823 size_t pg_offset, u64 start, u64 len,
6824 int create)
Chris Masona52d9a82007-08-27 16:49:44 -04006825{
David Sterba3ffbd682018-06-29 10:56:42 +02006826 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Chris Masona52d9a82007-08-27 16:49:44 -04006827 int ret;
6828 int err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006829 u64 extent_start = 0;
6830 u64 extent_end = 0;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006831 u64 objectid = btrfs_ino(inode);
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006832 u8 extent_type;
Chris Masonf4219502008-07-22 11:18:09 -04006833 struct btrfs_path *path = NULL;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006834 struct btrfs_root *root = inode->root;
Chris Masona52d9a82007-08-27 16:49:44 -04006835 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04006836 struct extent_buffer *leaf;
6837 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04006838 struct extent_map *em = NULL;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006839 struct extent_map_tree *em_tree = &inode->extent_tree;
6840 struct extent_io_tree *io_tree = &inode->io_tree;
Filipe Manana7ffbb592014-06-09 03:48:05 +01006841 const bool new_inline = !page || create;
Chris Masona52d9a82007-08-27 16:49:44 -04006842
Chris Mason890871b2009-09-02 16:24:52 -04006843 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006844 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04006845 if (em)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006846 em->bdev = fs_info->fs_devices->latest_bdev;
Chris Mason890871b2009-09-02 16:24:52 -04006847 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006848
Chris Masona52d9a82007-08-27 16:49:44 -04006849 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04006850 if (em->start > start || em->start + em->len <= start)
6851 free_extent_map(em);
6852 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05006853 free_extent_map(em);
6854 else
6855 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006856 }
David Sterba172ddd62011-04-21 00:48:27 +02006857 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04006858 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05006859 err = -ENOMEM;
6860 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006861 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006862 em->bdev = fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05006863 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05006864 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05006865 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04006866 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04006867
Liu Bobee6ec82018-08-17 05:05:28 +08006868 path = btrfs_alloc_path();
Chris Masonf4219502008-07-22 11:18:09 -04006869 if (!path) {
Liu Bobee6ec82018-08-17 05:05:28 +08006870 err = -ENOMEM;
6871 goto out;
Chris Masonf4219502008-07-22 11:18:09 -04006872 }
6873
Liu Bobee6ec82018-08-17 05:05:28 +08006874 /* Chances are we'll be called again, so go ahead and do readahead */
6875 path->reada = READA_FORWARD;
6876
Liu Boe49aabd2018-08-25 13:47:09 +08006877 /*
6878 * Unless we're going to uncompress the inline extent, no sleep would
6879 * happen.
6880 */
6881 path->leave_spinning = 1;
6882
Nikolay Borisov5c9a7022017-12-01 11:19:40 +02006883 ret = btrfs_lookup_file_extent(NULL, root, path, objectid, start, 0);
Chris Masona52d9a82007-08-27 16:49:44 -04006884 if (ret < 0) {
6885 err = ret;
6886 goto out;
Nikolay Borisovb8eeab72018-12-17 11:49:00 +02006887 } else if (ret > 0) {
Chris Masona52d9a82007-08-27 16:49:44 -04006888 if (path->slots[0] == 0)
6889 goto not_found;
6890 path->slots[0]--;
6891 }
6892
Chris Mason5f39d392007-10-15 16:14:19 -04006893 leaf = path->nodes[0];
6894 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04006895 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04006896 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04006897 if (found_key.objectid != objectid ||
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006898 found_key.type != BTRFS_EXTENT_DATA_KEY) {
Josef Bacik25a50342013-10-14 12:08:38 -04006899 /*
6900 * If we backup past the first extent we want to move forward
6901 * and see if there is an extent in front of us, otherwise we'll
6902 * say there is a hole for our whole search range which can
6903 * cause problems.
6904 */
6905 extent_end = start;
6906 goto next;
Chris Masona52d9a82007-08-27 16:49:44 -04006907 }
6908
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006909 extent_type = btrfs_file_extent_type(leaf, item);
Chris Mason5f39d392007-10-15 16:14:19 -04006910 extent_start = found_key.offset;
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006911 if (extent_type == BTRFS_FILE_EXTENT_REG ||
6912 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08006913 /* Only regular file could have regular/prealloc extent */
6914 if (!S_ISREG(inode->vfs_inode.i_mode)) {
6915 ret = -EUCLEAN;
6916 btrfs_crit(fs_info,
6917 "regular/prealloc extent found for non-regular inode %llu",
6918 btrfs_ino(inode));
6919 goto out;
6920 }
Chris Masona52d9a82007-08-27 16:49:44 -04006921 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04006922 btrfs_file_extent_num_bytes(leaf, item);
Liu Bo09ed2f12017-03-10 11:09:48 -08006923
6924 trace_btrfs_get_extent_show_fi_regular(inode, leaf, item,
6925 extent_start);
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006926 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Yan Zheng9036c102008-10-30 14:19:41 -04006927 size_t size;
Qu Wenruoe41ca582018-06-06 15:41:49 +08006928
6929 size = btrfs_file_extent_ram_bytes(leaf, item);
Jeff Mahoneyda170662016-06-15 09:22:56 -04006930 extent_end = ALIGN(extent_start + size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006931 fs_info->sectorsize);
Liu Bo09ed2f12017-03-10 11:09:48 -08006932
6933 trace_btrfs_get_extent_show_fi_inline(inode, leaf, item,
6934 path->slots[0],
6935 extent_start);
Yan Zheng9036c102008-10-30 14:19:41 -04006936 }
Josef Bacik25a50342013-10-14 12:08:38 -04006937next:
Yan Zheng9036c102008-10-30 14:19:41 -04006938 if (start >= extent_end) {
6939 path->slots[0]++;
6940 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
6941 ret = btrfs_next_leaf(root, path);
6942 if (ret < 0) {
6943 err = ret;
6944 goto out;
Nikolay Borisovb8eeab72018-12-17 11:49:00 +02006945 } else if (ret > 0) {
Yan Zheng9036c102008-10-30 14:19:41 -04006946 goto not_found;
Nikolay Borisovb8eeab72018-12-17 11:49:00 +02006947 }
Yan Zheng9036c102008-10-30 14:19:41 -04006948 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04006949 }
Yan Zheng9036c102008-10-30 14:19:41 -04006950 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6951 if (found_key.objectid != objectid ||
6952 found_key.type != BTRFS_EXTENT_DATA_KEY)
6953 goto not_found;
6954 if (start + len <= found_key.offset)
6955 goto not_found;
Wang Shilonge2eca692014-07-17 11:44:14 +08006956 if (start > found_key.offset)
6957 goto next;
Nikolay Borisov02a033d2018-12-17 10:36:02 +02006958
6959 /* New extent overlaps with existing one */
Yan Zheng9036c102008-10-30 14:19:41 -04006960 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006961 em->orig_start = start;
Yan Zheng9036c102008-10-30 14:19:41 -04006962 em->len = found_key.offset - start;
Nikolay Borisov02a033d2018-12-17 10:36:02 +02006963 em->block_start = EXTENT_MAP_HOLE;
6964 goto insert;
Yan Zheng9036c102008-10-30 14:19:41 -04006965 }
6966
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006967 btrfs_extent_item_to_extent_map(inode, path, item,
Nikolay Borisov9cdc5122017-02-20 13:51:02 +02006968 new_inline, em);
Filipe Manana7ffbb592014-06-09 03:48:05 +01006969
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006970 if (extent_type == BTRFS_FILE_EXTENT_REG ||
6971 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006972 goto insert;
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006973 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04006974 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04006975 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04006976 size_t size;
6977 size_t extent_offset;
6978 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04006979
Filipe Manana7ffbb592014-06-09 03:48:05 +01006980 if (new_inline)
Yan689f9342007-10-29 11:41:07 -04006981 goto out;
Yan689f9342007-10-29 11:41:07 -04006982
Qu Wenruoe41ca582018-06-06 15:41:49 +08006983 size = btrfs_file_extent_ram_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04006984 extent_offset = page_offset(page) + pg_offset - extent_start;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006985 copy_size = min_t(u64, PAGE_SIZE - pg_offset,
6986 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04006987 em->start = extent_start + extent_offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006988 em->len = ALIGN(copy_size, fs_info->sectorsize);
Josef Bacikb4939682012-12-03 10:31:19 -05006989 em->orig_block_len = em->len;
Josef Bacik70c8a912012-10-11 16:54:30 -04006990 em->orig_start = em->start;
Yan689f9342007-10-29 11:41:07 -04006991 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Liu Boe49aabd2018-08-25 13:47:09 +08006992
6993 btrfs_set_path_blocking(path);
Edmund Nadolskibf46f522017-11-20 13:24:49 -07006994 if (!PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08006995 if (btrfs_file_extent_compression(leaf, item) !=
6996 BTRFS_COMPRESS_NONE) {
Byongho Leee40da0e2015-05-19 23:46:45 +09006997 ret = uncompress_inline(path, page, pg_offset,
Chris Masonc8b97812008-10-29 14:49:59 -04006998 extent_offset, item);
Zach Brown166ae5a2014-05-09 17:15:10 -04006999 if (ret) {
7000 err = ret;
7001 goto out;
7002 }
Chris Masonc8b97812008-10-29 14:49:59 -04007003 } else {
7004 map = kmap(page);
7005 read_extent_buffer(leaf, map + pg_offset, ptr,
7006 copy_size);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007007 if (pg_offset + copy_size < PAGE_SIZE) {
Chris Mason93c82d52009-09-11 12:36:29 -04007008 memset(map + pg_offset + copy_size, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007009 PAGE_SIZE - pg_offset -
Chris Mason93c82d52009-09-11 12:36:29 -04007010 copy_size);
7011 }
Chris Masonc8b97812008-10-29 14:49:59 -04007012 kunmap(page);
7013 }
Chris Mason179e29e2007-11-01 11:28:41 -04007014 flush_dcache_page(page);
Chris Masona52d9a82007-08-27 16:49:44 -04007015 }
Chris Masond1310b22008-01-24 16:13:08 -05007016 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00007017 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04007018 goto insert;
Chris Masona52d9a82007-08-27 16:49:44 -04007019 }
7020not_found:
7021 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04007022 em->orig_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05007023 em->len = len;
Chris Mason5f39d392007-10-15 16:14:19 -04007024 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04007025insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02007026 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05007027 if (em->start > start || extent_map_end(em) <= start) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007028 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04007029 "bad extent! em: [%llu %llu] passed [%llu %llu]",
7030 em->start, em->len, start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04007031 err = -EIO;
7032 goto out;
7033 }
Chris Masond1310b22008-01-24 16:13:08 -05007034
7035 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04007036 write_lock(&em_tree->lock);
David Sterbaf46b24c2018-04-03 21:45:57 +02007037 err = btrfs_add_extent_mapping(fs_info, em_tree, &em, start, len);
Chris Mason890871b2009-09-02 16:24:52 -04007038 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04007039out:
Liu Boc6414282018-08-23 07:36:17 +08007040 btrfs_free_path(path);
liubo1abe9b82011-03-24 11:18:59 +00007041
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007042 trace_btrfs_get_extent(root, inode, em);
liubo1abe9b82011-03-24 11:18:59 +00007043
Chris Masona52d9a82007-08-27 16:49:44 -04007044 if (err) {
7045 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04007046 return ERR_PTR(err);
7047 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007048 BUG_ON(!em); /* Error is always set */
Chris Masona52d9a82007-08-27 16:49:44 -04007049 return em;
7050}
7051
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007052struct extent_map *btrfs_get_extent_fiemap(struct btrfs_inode *inode,
Nikolay Borisov4ab47a82018-12-12 09:42:32 +02007053 u64 start, u64 len)
Chris Masonec29ed52011-02-23 16:23:20 -05007054{
7055 struct extent_map *em;
7056 struct extent_map *hole_em = NULL;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007057 u64 delalloc_start = start;
Chris Masonec29ed52011-02-23 16:23:20 -05007058 u64 end;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007059 u64 delalloc_len;
7060 u64 delalloc_end;
Chris Masonec29ed52011-02-23 16:23:20 -05007061 int err = 0;
7062
Nikolay Borisov4ab47a82018-12-12 09:42:32 +02007063 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
Chris Masonec29ed52011-02-23 16:23:20 -05007064 if (IS_ERR(em))
7065 return em;
Dan Carpenter99862772017-04-11 11:57:15 +03007066 /*
7067 * If our em maps to:
7068 * - a hole or
7069 * - a pre-alloc extent,
7070 * there might actually be delalloc bytes behind it.
7071 */
7072 if (em->block_start != EXTENT_MAP_HOLE &&
7073 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7074 return em;
7075 else
7076 hole_em = em;
Chris Masonec29ed52011-02-23 16:23:20 -05007077
7078 /* check to see if we've wrapped (len == -1 or similar) */
7079 end = start + len;
7080 if (end < start)
7081 end = (u64)-1;
7082 else
7083 end -= 1;
7084
7085 em = NULL;
7086
7087 /* ok, we didn't find anything, lets look for delalloc */
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007088 delalloc_len = count_range_bits(&inode->io_tree, &delalloc_start,
Chris Masonec29ed52011-02-23 16:23:20 -05007089 end, len, EXTENT_DELALLOC, 1);
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007090 delalloc_end = delalloc_start + delalloc_len;
7091 if (delalloc_end < delalloc_start)
7092 delalloc_end = (u64)-1;
Chris Masonec29ed52011-02-23 16:23:20 -05007093
7094 /*
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007095 * We didn't find anything useful, return the original results from
7096 * get_extent()
Chris Masonec29ed52011-02-23 16:23:20 -05007097 */
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007098 if (delalloc_start > end || delalloc_end <= start) {
Chris Masonec29ed52011-02-23 16:23:20 -05007099 em = hole_em;
7100 hole_em = NULL;
7101 goto out;
7102 }
7103
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007104 /*
7105 * Adjust the delalloc_start to make sure it doesn't go backwards from
7106 * the start they passed in
Chris Masonec29ed52011-02-23 16:23:20 -05007107 */
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007108 delalloc_start = max(start, delalloc_start);
7109 delalloc_len = delalloc_end - delalloc_start;
Chris Masonec29ed52011-02-23 16:23:20 -05007110
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007111 if (delalloc_len > 0) {
7112 u64 hole_start;
Nikolay Borisov02950af2018-12-12 09:42:34 +02007113 u64 hole_len;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007114 const u64 hole_end = extent_map_end(hole_em);
Chris Masonec29ed52011-02-23 16:23:20 -05007115
David Sterba172ddd62011-04-21 00:48:27 +02007116 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05007117 if (!em) {
7118 err = -ENOMEM;
7119 goto out;
7120 }
Chris Masonec29ed52011-02-23 16:23:20 -05007121 em->bdev = NULL;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007122
7123 ASSERT(hole_em);
7124 /*
7125 * When btrfs_get_extent can't find anything it returns one
7126 * huge hole
7127 *
7128 * Make sure what it found really fits our range, and adjust to
7129 * make sure it is based on the start from the caller
7130 */
7131 if (hole_end <= start || hole_em->start > end) {
7132 free_extent_map(hole_em);
7133 hole_em = NULL;
7134 } else {
7135 hole_start = max(hole_em->start, start);
7136 hole_len = hole_end - hole_start;
7137 }
7138
7139 if (hole_em && delalloc_start > hole_start) {
7140 /*
7141 * Our hole starts before our delalloc, so we have to
7142 * return just the parts of the hole that go until the
7143 * delalloc starts
Chris Masonec29ed52011-02-23 16:23:20 -05007144 */
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007145 em->len = min(hole_len, delalloc_start - hole_start);
Chris Masonec29ed52011-02-23 16:23:20 -05007146 em->start = hole_start;
7147 em->orig_start = hole_start;
7148 /*
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007149 * Don't adjust block start at all, it is fixed at
7150 * EXTENT_MAP_HOLE
Chris Masonec29ed52011-02-23 16:23:20 -05007151 */
7152 em->block_start = hole_em->block_start;
7153 em->block_len = hole_len;
Liu Bof9e4fb52013-01-07 10:10:12 +00007154 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
7155 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonec29ed52011-02-23 16:23:20 -05007156 } else {
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007157 /*
7158 * Hole is out of passed range or it starts after
7159 * delalloc range
7160 */
7161 em->start = delalloc_start;
7162 em->len = delalloc_len;
7163 em->orig_start = delalloc_start;
Chris Masonec29ed52011-02-23 16:23:20 -05007164 em->block_start = EXTENT_MAP_DELALLOC;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007165 em->block_len = delalloc_len;
Chris Masonec29ed52011-02-23 16:23:20 -05007166 }
Nikolay Borisovbf8d32b2017-12-01 11:19:43 +02007167 } else {
Chris Masonec29ed52011-02-23 16:23:20 -05007168 return hole_em;
7169 }
7170out:
7171
7172 free_extent_map(hole_em);
7173 if (err) {
7174 free_extent_map(em);
7175 return ERR_PTR(err);
7176 }
7177 return em;
7178}
7179
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007180static struct extent_map *btrfs_create_dio_extent(struct inode *inode,
7181 const u64 start,
7182 const u64 len,
7183 const u64 orig_start,
7184 const u64 block_start,
7185 const u64 block_len,
7186 const u64 orig_block_len,
7187 const u64 ram_bytes,
7188 const int type)
7189{
7190 struct extent_map *em = NULL;
7191 int ret;
7192
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007193 if (type != BTRFS_ORDERED_NOCOW) {
Liu Bo6f9994d2017-01-31 07:50:22 -08007194 em = create_io_em(inode, start, len, orig_start,
7195 block_start, block_len, orig_block_len,
7196 ram_bytes,
7197 BTRFS_COMPRESS_NONE, /* compress_type */
7198 type);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007199 if (IS_ERR(em))
7200 goto out;
7201 }
7202 ret = btrfs_add_ordered_extent_dio(inode, start, block_start,
7203 len, block_len, type);
7204 if (ret) {
7205 if (em) {
7206 free_extent_map(em);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02007207 btrfs_drop_extent_cache(BTRFS_I(inode), start,
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007208 start + len - 1, 0);
7209 }
7210 em = ERR_PTR(ret);
7211 }
7212 out:
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007213
7214 return em;
7215}
7216
Josef Bacik4b46fce2010-05-23 11:00:55 -04007217static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
7218 u64 start, u64 len)
7219{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007220 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007221 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik70c8a912012-10-11 16:54:30 -04007222 struct extent_map *em;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007223 struct btrfs_key ins;
7224 u64 alloc_hint;
7225 int ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007226
Josef Bacik4b46fce2010-05-23 11:00:55 -04007227 alloc_hint = get_extent_allocation_hint(inode, start, len);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007228 ret = btrfs_reserve_extent(root, len, len, fs_info->sectorsize,
Jeff Mahoneyda170662016-06-15 09:22:56 -04007229 0, alloc_hint, &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007230 if (ret)
7231 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007232
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007233 em = btrfs_create_dio_extent(inode, start, ins.offset, start,
7234 ins.objectid, ins.offset, ins.offset,
Liu Bo6288d6e2017-02-21 12:12:58 -08007235 ins.offset, BTRFS_ORDERED_REGULAR);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007236 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007237 if (IS_ERR(em))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007238 btrfs_free_reserved_extent(fs_info, ins.objectid,
7239 ins.offset, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007240
Josef Bacik4b46fce2010-05-23 11:00:55 -04007241 return em;
7242}
7243
Chris Mason46bfbb52010-05-26 11:04:10 -04007244/*
7245 * returns 1 when the nocow is safe, < 1 on error, 0 if the
7246 * block must be cow'd
7247 */
Josef Bacik00361582013-08-14 14:02:47 -04007248noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
Josef Bacik7ee9e442013-06-21 16:37:03 -04007249 u64 *orig_start, u64 *orig_block_len,
7250 u64 *ram_bytes)
Chris Mason46bfbb52010-05-26 11:04:10 -04007251{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007252 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason46bfbb52010-05-26 11:04:10 -04007253 struct btrfs_path *path;
7254 int ret;
7255 struct extent_buffer *leaf;
7256 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xie7b2b7082014-02-27 13:58:05 +08007257 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason46bfbb52010-05-26 11:04:10 -04007258 struct btrfs_file_extent_item *fi;
7259 struct btrfs_key key;
7260 u64 disk_bytenr;
7261 u64 backref_offset;
7262 u64 extent_end;
7263 u64 num_bytes;
7264 int slot;
7265 int found_type;
Josef Bacik7ee9e442013-06-21 16:37:03 -04007266 bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW);
Miao Xiee77751a2013-12-27 21:11:50 +08007267
Chris Mason46bfbb52010-05-26 11:04:10 -04007268 path = btrfs_alloc_path();
7269 if (!path)
7270 return -ENOMEM;
7271
David Sterbaf85b7372017-01-20 14:54:07 +01007272 ret = btrfs_lookup_file_extent(NULL, root, path,
7273 btrfs_ino(BTRFS_I(inode)), offset, 0);
Chris Mason46bfbb52010-05-26 11:04:10 -04007274 if (ret < 0)
7275 goto out;
7276
7277 slot = path->slots[0];
7278 if (ret == 1) {
7279 if (slot == 0) {
7280 /* can't find the item, must cow */
7281 ret = 0;
7282 goto out;
7283 }
7284 slot--;
7285 }
7286 ret = 0;
7287 leaf = path->nodes[0];
7288 btrfs_item_key_to_cpu(leaf, &key, slot);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02007289 if (key.objectid != btrfs_ino(BTRFS_I(inode)) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04007290 key.type != BTRFS_EXTENT_DATA_KEY) {
7291 /* not our file or wrong item type, must cow */
7292 goto out;
7293 }
7294
7295 if (key.offset > offset) {
7296 /* Wrong offset, must cow */
7297 goto out;
7298 }
7299
7300 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
7301 found_type = btrfs_file_extent_type(leaf, fi);
7302 if (found_type != BTRFS_FILE_EXTENT_REG &&
7303 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
7304 /* not a regular extent, must cow */
7305 goto out;
7306 }
Josef Bacik7ee9e442013-06-21 16:37:03 -04007307
7308 if (!nocow && found_type == BTRFS_FILE_EXTENT_REG)
7309 goto out;
7310
Miao Xiee77751a2013-12-27 21:11:50 +08007311 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
7312 if (extent_end <= offset)
7313 goto out;
7314
Chris Mason46bfbb52010-05-26 11:04:10 -04007315 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Josef Bacik7ee9e442013-06-21 16:37:03 -04007316 if (disk_bytenr == 0)
7317 goto out;
7318
7319 if (btrfs_file_extent_compression(leaf, fi) ||
7320 btrfs_file_extent_encryption(leaf, fi) ||
7321 btrfs_file_extent_other_encoding(leaf, fi))
7322 goto out;
7323
Ethan Lien78d42952018-05-17 14:58:29 +08007324 /*
7325 * Do the same check as in btrfs_cross_ref_exist but without the
7326 * unnecessary search.
7327 */
7328 if (btrfs_file_extent_generation(leaf, fi) <=
7329 btrfs_root_last_snapshot(&root->root_item))
7330 goto out;
7331
Chris Mason46bfbb52010-05-26 11:04:10 -04007332 backref_offset = btrfs_file_extent_offset(leaf, fi);
7333
Josef Bacik7ee9e442013-06-21 16:37:03 -04007334 if (orig_start) {
7335 *orig_start = key.offset - backref_offset;
7336 *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
7337 *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
7338 }
Josef Bacikeb384b52013-04-24 16:32:55 -04007339
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007340 if (btrfs_extent_readonly(fs_info, disk_bytenr))
Chris Mason46bfbb52010-05-26 11:04:10 -04007341 goto out;
Miao Xie7b2b7082014-02-27 13:58:05 +08007342
7343 num_bytes = min(offset + *len, extent_end) - offset;
7344 if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) {
7345 u64 range_end;
7346
Jeff Mahoneyda170662016-06-15 09:22:56 -04007347 range_end = round_up(offset + num_bytes,
7348 root->fs_info->sectorsize) - 1;
Miao Xie7b2b7082014-02-27 13:58:05 +08007349 ret = test_range_bit(io_tree, offset, range_end,
7350 EXTENT_DELALLOC, 0, NULL);
7351 if (ret) {
7352 ret = -EAGAIN;
7353 goto out;
7354 }
7355 }
7356
Josef Bacik1bda19e2013-10-18 12:10:36 -04007357 btrfs_release_path(path);
Chris Mason46bfbb52010-05-26 11:04:10 -04007358
7359 /*
7360 * look for other files referencing this extent, if we
7361 * find any we must cow
7362 */
Josef Bacik00361582013-08-14 14:02:47 -04007363
Liu Boe4c3b2d2017-01-30 12:25:28 -08007364 ret = btrfs_cross_ref_exist(root, btrfs_ino(BTRFS_I(inode)),
Josef Bacik00361582013-08-14 14:02:47 -04007365 key.offset - backref_offset, disk_bytenr);
Josef Bacik00361582013-08-14 14:02:47 -04007366 if (ret) {
7367 ret = 0;
7368 goto out;
7369 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007370
7371 /*
7372 * adjust disk_bytenr and num_bytes to cover just the bytes
7373 * in this extent we are about to write. If there
7374 * are any csums in that range we have to cow in order
7375 * to keep the csums correct
7376 */
7377 disk_bytenr += backref_offset;
7378 disk_bytenr += offset - key.offset;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007379 if (csum_exist_in_range(fs_info, disk_bytenr, num_bytes))
7380 goto out;
Chris Mason46bfbb52010-05-26 11:04:10 -04007381 /*
7382 * all of the above have passed, it is safe to overwrite this extent
7383 * without cow
7384 */
Josef Bacikeb384b52013-04-24 16:32:55 -04007385 *len = num_bytes;
Chris Mason46bfbb52010-05-26 11:04:10 -04007386 ret = 1;
7387out:
7388 btrfs_free_path(path);
7389 return ret;
7390}
7391
Josef Bacikeb838e72012-07-31 16:28:48 -04007392static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
7393 struct extent_state **cached_state, int writing)
7394{
7395 struct btrfs_ordered_extent *ordered;
7396 int ret = 0;
7397
7398 while (1) {
7399 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbaff13db42015-12-03 14:30:40 +01007400 cached_state);
Josef Bacikeb838e72012-07-31 16:28:48 -04007401 /*
7402 * We're concerned with the entire range that we're going to be
Nicholas D Steeves01327612016-05-19 21:18:45 -04007403 * doing DIO to, so we need to make sure there's no ordered
Josef Bacikeb838e72012-07-31 16:28:48 -04007404 * extents in this range.
7405 */
Nikolay Borisova776c6f2017-02-20 13:50:49 +02007406 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), lockstart,
Josef Bacikeb838e72012-07-31 16:28:48 -04007407 lockend - lockstart + 1);
7408
7409 /*
7410 * We need to make sure there are no buffered pages in this
7411 * range either, we could have raced between the invalidate in
7412 * generic_file_direct_write and locking the extent. The
7413 * invalidate needs to happen so that reads after a write do not
7414 * get stale data.
7415 */
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007416 if (!ordered &&
David Sterba051c98e2018-03-07 15:33:22 +01007417 (!writing || !filemap_range_has_page(inode->i_mapping,
7418 lockstart, lockend)))
Josef Bacikeb838e72012-07-31 16:28:48 -04007419 break;
7420
7421 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbae43bbe52017-12-12 21:43:52 +01007422 cached_state);
Josef Bacikeb838e72012-07-31 16:28:48 -04007423
7424 if (ordered) {
Filipe Mananaade77022016-02-18 14:28:55 +00007425 /*
7426 * If we are doing a DIO read and the ordered extent we
7427 * found is for a buffered write, we can not wait for it
7428 * to complete and retry, because if we do so we can
7429 * deadlock with concurrent buffered writes on page
7430 * locks. This happens only if our DIO read covers more
7431 * than one extent map, if at this point has already
7432 * created an ordered extent for a previous extent map
7433 * and locked its range in the inode's io tree, and a
7434 * concurrent write against that previous extent map's
7435 * range and this range started (we unlock the ranges
7436 * in the io tree only when the bios complete and
7437 * buffered writes always lock pages before attempting
7438 * to lock range in the io tree).
7439 */
7440 if (writing ||
7441 test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags))
7442 btrfs_start_ordered_extent(inode, ordered, 1);
7443 else
7444 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007445 btrfs_put_ordered_extent(ordered);
7446 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007447 /*
Filipe Mananab850ae12015-12-08 16:23:16 +00007448 * We could trigger writeback for this range (and wait
7449 * for it to complete) and then invalidate the pages for
7450 * this range (through invalidate_inode_pages2_range()),
7451 * but that can lead us to a deadlock with a concurrent
7452 * call to readpages() (a buffered read or a defrag call
7453 * triggered a readahead) on a page lock due to an
7454 * ordered dio extent we created before but did not have
7455 * yet a corresponding bio submitted (whence it can not
7456 * complete), which makes readpages() wait for that
7457 * ordered extent to complete while holding a lock on
7458 * that page.
Josef Bacikeb838e72012-07-31 16:28:48 -04007459 */
Filipe Mananab850ae12015-12-08 16:23:16 +00007460 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007461 }
7462
Filipe Mananaade77022016-02-18 14:28:55 +00007463 if (ret)
7464 break;
7465
Josef Bacikeb838e72012-07-31 16:28:48 -04007466 cond_resched();
7467 }
7468
7469 return ret;
7470}
7471
Liu Bo6f9994d2017-01-31 07:50:22 -08007472/* The callers of this must take lock_extent() */
7473static struct extent_map *create_io_em(struct inode *inode, u64 start, u64 len,
7474 u64 orig_start, u64 block_start,
7475 u64 block_len, u64 orig_block_len,
7476 u64 ram_bytes, int compress_type,
7477 int type)
Josef Bacik69ffb542012-09-11 15:40:07 -04007478{
7479 struct extent_map_tree *em_tree;
7480 struct extent_map *em;
7481 struct btrfs_root *root = BTRFS_I(inode)->root;
7482 int ret;
7483
Liu Bo6f9994d2017-01-31 07:50:22 -08007484 ASSERT(type == BTRFS_ORDERED_PREALLOC ||
7485 type == BTRFS_ORDERED_COMPRESSED ||
7486 type == BTRFS_ORDERED_NOCOW ||
Liu Bo1af4a0a2017-02-13 15:35:09 -08007487 type == BTRFS_ORDERED_REGULAR);
Liu Bo6f9994d2017-01-31 07:50:22 -08007488
Josef Bacik69ffb542012-09-11 15:40:07 -04007489 em_tree = &BTRFS_I(inode)->extent_tree;
7490 em = alloc_extent_map();
7491 if (!em)
7492 return ERR_PTR(-ENOMEM);
7493
7494 em->start = start;
7495 em->orig_start = orig_start;
7496 em->len = len;
7497 em->block_len = block_len;
7498 em->block_start = block_start;
7499 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacikb4939682012-12-03 10:31:19 -05007500 em->orig_block_len = orig_block_len;
Josef Bacikcc95bef2013-04-04 14:31:27 -04007501 em->ram_bytes = ram_bytes;
Josef Bacik70c8a912012-10-11 16:54:30 -04007502 em->generation = -1;
Josef Bacik69ffb542012-09-11 15:40:07 -04007503 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Liu Bo1af4a0a2017-02-13 15:35:09 -08007504 if (type == BTRFS_ORDERED_PREALLOC) {
Josef Bacikb11e2342012-12-03 10:58:15 -05007505 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Liu Bo1af4a0a2017-02-13 15:35:09 -08007506 } else if (type == BTRFS_ORDERED_COMPRESSED) {
Liu Bo6f9994d2017-01-31 07:50:22 -08007507 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
7508 em->compress_type = compress_type;
7509 }
Josef Bacik69ffb542012-09-11 15:40:07 -04007510
7511 do {
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02007512 btrfs_drop_extent_cache(BTRFS_I(inode), em->start,
Josef Bacik69ffb542012-09-11 15:40:07 -04007513 em->start + em->len - 1, 0);
7514 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04007515 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik69ffb542012-09-11 15:40:07 -04007516 write_unlock(&em_tree->lock);
Liu Bo6f9994d2017-01-31 07:50:22 -08007517 /*
7518 * The caller has taken lock_extent(), who could race with us
7519 * to add em?
7520 */
Josef Bacik69ffb542012-09-11 15:40:07 -04007521 } while (ret == -EEXIST);
7522
7523 if (ret) {
7524 free_extent_map(em);
7525 return ERR_PTR(ret);
7526 }
7527
Liu Bo6f9994d2017-01-31 07:50:22 -08007528 /* em got 2 refs now, callers needs to do free_extent_map once. */
Josef Bacik69ffb542012-09-11 15:40:07 -04007529 return em;
7530}
7531
Nikolay Borisov1c8d0172018-05-02 15:19:32 +03007532
7533static int btrfs_get_blocks_direct_read(struct extent_map *em,
7534 struct buffer_head *bh_result,
7535 struct inode *inode,
7536 u64 start, u64 len)
7537{
7538 if (em->block_start == EXTENT_MAP_HOLE ||
7539 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7540 return -ENOENT;
7541
7542 len = min(len, em->len - (start - em->start));
7543
7544 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
7545 inode->i_blkbits;
7546 bh_result->b_size = len;
7547 bh_result->b_bdev = em->bdev;
7548 set_buffer_mapped(bh_result);
7549
7550 return 0;
7551}
7552
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007553static int btrfs_get_blocks_direct_write(struct extent_map **map,
7554 struct buffer_head *bh_result,
7555 struct inode *inode,
7556 struct btrfs_dio_data *dio_data,
7557 u64 start, u64 len)
7558{
7559 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
7560 struct extent_map *em = *map;
7561 int ret = 0;
7562
7563 /*
7564 * We don't allocate a new extent in the following cases
7565 *
7566 * 1) The inode is marked as NODATACOW. In this case we'll just use the
7567 * existing extent.
7568 * 2) The extent is marked as PREALLOC. We're good to go here and can
7569 * just use the extent.
7570 *
7571 */
7572 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
7573 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
7574 em->block_start != EXTENT_MAP_HOLE)) {
7575 int type;
7576 u64 block_start, orig_start, orig_block_len, ram_bytes;
7577
7578 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7579 type = BTRFS_ORDERED_PREALLOC;
7580 else
7581 type = BTRFS_ORDERED_NOCOW;
7582 len = min(len, em->len - (start - em->start));
7583 block_start = em->block_start + (start - em->start);
7584
7585 if (can_nocow_extent(inode, start, &len, &orig_start,
7586 &orig_block_len, &ram_bytes) == 1 &&
7587 btrfs_inc_nocow_writers(fs_info, block_start)) {
7588 struct extent_map *em2;
7589
7590 em2 = btrfs_create_dio_extent(inode, start, len,
7591 orig_start, block_start,
7592 len, orig_block_len,
7593 ram_bytes, type);
7594 btrfs_dec_nocow_writers(fs_info, block_start);
7595 if (type == BTRFS_ORDERED_PREALLOC) {
7596 free_extent_map(em);
7597 *map = em = em2;
7598 }
7599
7600 if (em2 && IS_ERR(em2)) {
7601 ret = PTR_ERR(em2);
7602 goto out;
7603 }
7604 /*
7605 * For inode marked NODATACOW or extent marked PREALLOC,
7606 * use the existing or preallocated extent, so does not
7607 * need to adjust btrfs_space_info's bytes_may_use.
7608 */
7609 btrfs_free_reserved_data_space_noquota(inode, start,
7610 len);
7611 goto skip_cow;
7612 }
7613 }
7614
7615 /* this will cow the extent */
7616 len = bh_result->b_size;
7617 free_extent_map(em);
7618 *map = em = btrfs_new_extent_direct(inode, start, len);
7619 if (IS_ERR(em)) {
7620 ret = PTR_ERR(em);
7621 goto out;
7622 }
7623
7624 len = min(len, em->len - (start - em->start));
7625
7626skip_cow:
7627 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
7628 inode->i_blkbits;
7629 bh_result->b_size = len;
7630 bh_result->b_bdev = em->bdev;
7631 set_buffer_mapped(bh_result);
7632
7633 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7634 set_buffer_new(bh_result);
7635
7636 /*
7637 * Need to update the i_size under the extent lock so buffered
7638 * readers will get the updated i_size when we unlock.
7639 */
7640 if (!dio_data->overwrite && start + len > i_size_read(inode))
7641 i_size_write(inode, start + len);
7642
7643 WARN_ON(dio_data->reserve < len);
7644 dio_data->reserve -= len;
7645 dio_data->unsubmitted_oe_range_end = start + len;
7646 current->journal_info = dio_data;
7647out:
7648 return ret;
7649}
7650
Josef Bacik4b46fce2010-05-23 11:00:55 -04007651static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
7652 struct buffer_head *bh_result, int create)
7653{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007654 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007655 struct extent_map *em;
Josef Bacikeb838e72012-07-31 16:28:48 -04007656 struct extent_state *cached_state = NULL;
chandan50745b02015-08-28 21:10:13 +05307657 struct btrfs_dio_data *dio_data = NULL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007658 u64 start = iblock << inode->i_blkbits;
Josef Bacikeb838e72012-07-31 16:28:48 -04007659 u64 lockstart, lockend;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007660 u64 len = bh_result->b_size;
Josef Bacikeb838e72012-07-31 16:28:48 -04007661 int unlock_bits = EXTENT_LOCKED;
Miao Xie09348562013-02-07 10:12:07 +00007662 int ret = 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007663
Miao Xie172a5042013-02-21 02:48:22 -07007664 if (create)
Josef Bacik32667892015-02-11 15:08:58 -05007665 unlock_bits |= EXTENT_DIRTY;
Miao Xie172a5042013-02-21 02:48:22 -07007666 else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007667 len = min_t(u64, len, fs_info->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04007668
Josef Bacikc3298612012-08-03 16:49:19 -04007669 lockstart = start;
7670 lockend = start + len - 1;
7671
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007672 if (current->journal_info) {
7673 /*
7674 * Need to pull our outstanding extents and set journal_info to NULL so
Nicholas D Steeves01327612016-05-19 21:18:45 -04007675 * that anything that needs to check if there's a transaction doesn't get
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007676 * confused.
7677 */
chandan50745b02015-08-28 21:10:13 +05307678 dio_data = current->journal_info;
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007679 current->journal_info = NULL;
7680 }
7681
Josef Bacikeb838e72012-07-31 16:28:48 -04007682 /*
7683 * If this errors out it's because we couldn't invalidate pagecache for
7684 * this range and we need to fallback to buffered.
7685 */
Filipe Manana9c9464c2015-11-04 09:52:04 +00007686 if (lock_extent_direct(inode, lockstart, lockend, &cached_state,
7687 create)) {
7688 ret = -ENOTBLK;
7689 goto err;
7690 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007691
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007692 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len, 0);
Josef Bacikeb838e72012-07-31 16:28:48 -04007693 if (IS_ERR(em)) {
7694 ret = PTR_ERR(em);
7695 goto unlock_err;
7696 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007697
7698 /*
7699 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
7700 * io. INLINE is special, and we could probably kludge it in here, but
7701 * it's still buffered so for safety lets just fall back to the generic
7702 * buffered path.
7703 *
7704 * For COMPRESSED we _have_ to read the entire extent in so we can
7705 * decompress it, so there will be buffering required no matter what we
7706 * do, so go ahead and fallback to buffered.
7707 *
Nicholas D Steeves01327612016-05-19 21:18:45 -04007708 * We return -ENOTBLK because that's what makes DIO go ahead and go back
Josef Bacik4b46fce2010-05-23 11:00:55 -04007709 * to buffered IO. Don't blame me, this is the price we pay for using
7710 * the generic code.
7711 */
7712 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
7713 em->block_start == EXTENT_MAP_INLINE) {
7714 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007715 ret = -ENOTBLK;
7716 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007717 }
7718
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007719 if (create) {
7720 ret = btrfs_get_blocks_direct_write(&em, bh_result, inode,
7721 dio_data, start, len);
7722 if (ret < 0)
7723 goto unlock_err;
7724
7725 /* clear and unlock the entire range */
7726 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7727 unlock_bits, 1, 0, &cached_state);
7728 } else {
Nikolay Borisov1c8d0172018-05-02 15:19:32 +03007729 ret = btrfs_get_blocks_direct_read(em, bh_result, inode,
7730 start, len);
7731 /* Can be negative only if we read from a hole */
7732 if (ret < 0) {
7733 ret = 0;
7734 free_extent_map(em);
7735 goto unlock_err;
7736 }
7737 /*
7738 * We need to unlock only the end area that we aren't using.
7739 * The rest is going to be unlocked by the endio routine.
7740 */
7741 lockstart = start + bh_result->b_size;
7742 if (lockstart < lockend) {
7743 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
7744 lockend, unlock_bits, 1, 0,
7745 &cached_state);
7746 } else {
7747 free_extent_state(cached_state);
7748 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007749 }
7750
Josef Bacik4b46fce2010-05-23 11:00:55 -04007751 free_extent_map(em);
7752
7753 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007754
7755unlock_err:
Josef Bacikeb838e72012-07-31 16:28:48 -04007756 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbaae0f1622017-10-31 16:37:52 +01007757 unlock_bits, 1, 0, &cached_state);
Filipe Manana9c9464c2015-11-04 09:52:04 +00007758err:
chandan50745b02015-08-28 21:10:13 +05307759 if (dio_data)
7760 current->journal_info = dio_data;
Josef Bacikeb838e72012-07-31 16:28:48 -04007761 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007762}
7763
Omar Sandoval58efbc92017-08-22 23:45:59 -07007764static inline blk_status_t submit_dio_repair_bio(struct inode *inode,
7765 struct bio *bio,
7766 int mirror_num)
Miao Xie8b110e32014-09-12 18:44:03 +08007767{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007768 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Omar Sandoval58efbc92017-08-22 23:45:59 -07007769 blk_status_t ret;
Miao Xie8b110e32014-09-12 18:44:03 +08007770
Mike Christie37226b22016-06-05 14:31:52 -05007771 BUG_ON(bio_op(bio) == REQ_OP_WRITE);
Miao Xie8b110e32014-09-12 18:44:03 +08007772
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007773 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DIO_REPAIR);
Miao Xie8b110e32014-09-12 18:44:03 +08007774 if (ret)
Nikolay Borisovea057f62017-12-13 10:25:38 +02007775 return ret;
Miao Xie8b110e32014-09-12 18:44:03 +08007776
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007777 ret = btrfs_map_bio(fs_info, bio, mirror_num, 0);
Nikolay Borisovea057f62017-12-13 10:25:38 +02007778
Miao Xie8b110e32014-09-12 18:44:03 +08007779 return ret;
7780}
7781
7782static int btrfs_check_dio_repairable(struct inode *inode,
7783 struct bio *failed_bio,
7784 struct io_failure_record *failrec,
7785 int failed_mirror)
7786{
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007787 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xie8b110e32014-09-12 18:44:03 +08007788 int num_copies;
7789
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007790 num_copies = btrfs_num_copies(fs_info, failrec->logical, failrec->len);
Miao Xie8b110e32014-09-12 18:44:03 +08007791 if (num_copies == 1) {
7792 /*
7793 * we only have a single copy of the data, so don't bother with
7794 * all the retry and error correction code that follows. no
7795 * matter what the error is, it is very likely to persist.
7796 */
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007797 btrfs_debug(fs_info,
7798 "Check DIO Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d",
7799 num_copies, failrec->this_mirror, failed_mirror);
Miao Xie8b110e32014-09-12 18:44:03 +08007800 return 0;
7801 }
7802
7803 failrec->failed_mirror = failed_mirror;
7804 failrec->this_mirror++;
7805 if (failrec->this_mirror == failed_mirror)
7806 failrec->this_mirror++;
7807
7808 if (failrec->this_mirror > num_copies) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007809 btrfs_debug(fs_info,
7810 "Check DIO Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d",
7811 num_copies, failrec->this_mirror, failed_mirror);
Miao Xie8b110e32014-09-12 18:44:03 +08007812 return 0;
7813 }
7814
7815 return 1;
7816}
7817
Omar Sandoval58efbc92017-08-22 23:45:59 -07007818static blk_status_t dio_read_error(struct inode *inode, struct bio *failed_bio,
7819 struct page *page, unsigned int pgoff,
7820 u64 start, u64 end, int failed_mirror,
7821 bio_end_io_t *repair_endio, void *repair_arg)
Miao Xie8b110e32014-09-12 18:44:03 +08007822{
7823 struct io_failure_record *failrec;
Josef Bacik7870d082017-05-05 11:57:15 -04007824 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
7825 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
Miao Xie8b110e32014-09-12 18:44:03 +08007826 struct bio *bio;
7827 int isector;
David Sterbaf1c77c52017-06-06 19:03:49 +02007828 unsigned int read_mode = 0;
Liu Bo17347ce2017-05-15 15:33:27 -07007829 int segs;
Miao Xie8b110e32014-09-12 18:44:03 +08007830 int ret;
Omar Sandoval58efbc92017-08-22 23:45:59 -07007831 blk_status_t status;
Ming Leic16a8ac2017-12-18 20:22:12 +08007832 struct bio_vec bvec;
Miao Xie8b110e32014-09-12 18:44:03 +08007833
Mike Christie37226b22016-06-05 14:31:52 -05007834 BUG_ON(bio_op(failed_bio) == REQ_OP_WRITE);
Miao Xie8b110e32014-09-12 18:44:03 +08007835
7836 ret = btrfs_get_io_failure_record(inode, start, end, &failrec);
7837 if (ret)
Omar Sandoval58efbc92017-08-22 23:45:59 -07007838 return errno_to_blk_status(ret);
Miao Xie8b110e32014-09-12 18:44:03 +08007839
7840 ret = btrfs_check_dio_repairable(inode, failed_bio, failrec,
7841 failed_mirror);
7842 if (!ret) {
Josef Bacik7870d082017-05-05 11:57:15 -04007843 free_io_failure(failure_tree, io_tree, failrec);
Omar Sandoval58efbc92017-08-22 23:45:59 -07007844 return BLK_STS_IOERR;
Miao Xie8b110e32014-09-12 18:44:03 +08007845 }
7846
Liu Bo17347ce2017-05-15 15:33:27 -07007847 segs = bio_segments(failed_bio);
Ming Leic16a8ac2017-12-18 20:22:12 +08007848 bio_get_first_bvec(failed_bio, &bvec);
Liu Bo17347ce2017-05-15 15:33:27 -07007849 if (segs > 1 ||
Ming Leic16a8ac2017-12-18 20:22:12 +08007850 (bvec.bv_len > btrfs_inode_sectorsize(inode)))
Christoph Hellwig70fd7612016-11-01 07:40:10 -06007851 read_mode |= REQ_FAILFAST_DEV;
Miao Xie8b110e32014-09-12 18:44:03 +08007852
7853 isector = start - btrfs_io_bio(failed_bio)->logical;
7854 isector >>= inode->i_sb->s_blocksize_bits;
7855 bio = btrfs_create_repair_bio(inode, failed_bio, failrec, page,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307856 pgoff, isector, repair_endio, repair_arg);
David Sterbaebcc3262018-06-29 10:56:53 +02007857 bio->bi_opf = REQ_OP_READ | read_mode;
Miao Xie8b110e32014-09-12 18:44:03 +08007858
7859 btrfs_debug(BTRFS_I(inode)->root->fs_info,
David Sterba913e1532017-07-13 15:32:18 +02007860 "repair DIO read error: submitting new dio read[%#x] to this_mirror=%d, in_validation=%d",
Miao Xie8b110e32014-09-12 18:44:03 +08007861 read_mode, failrec->this_mirror, failrec->in_validation);
7862
Omar Sandoval58efbc92017-08-22 23:45:59 -07007863 status = submit_dio_repair_bio(inode, bio, failrec->this_mirror);
7864 if (status) {
Josef Bacik7870d082017-05-05 11:57:15 -04007865 free_io_failure(failure_tree, io_tree, failrec);
Miao Xie8b110e32014-09-12 18:44:03 +08007866 bio_put(bio);
7867 }
7868
Omar Sandoval58efbc92017-08-22 23:45:59 -07007869 return status;
Miao Xie8b110e32014-09-12 18:44:03 +08007870}
7871
7872struct btrfs_retry_complete {
7873 struct completion done;
7874 struct inode *inode;
7875 u64 start;
7876 int uptodate;
7877};
7878
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007879static void btrfs_retry_endio_nocsum(struct bio *bio)
Miao Xie8b110e32014-09-12 18:44:03 +08007880{
7881 struct btrfs_retry_complete *done = bio->bi_private;
Josef Bacik7870d082017-05-05 11:57:15 -04007882 struct inode *inode = done->inode;
Miao Xie8b110e32014-09-12 18:44:03 +08007883 struct bio_vec *bvec;
Josef Bacik7870d082017-05-05 11:57:15 -04007884 struct extent_io_tree *io_tree, *failure_tree;
Miao Xie8b110e32014-09-12 18:44:03 +08007885 int i;
Ming Lei6dc4f102019-02-15 19:13:19 +08007886 struct bvec_iter_all iter_all;
Miao Xie8b110e32014-09-12 18:44:03 +08007887
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02007888 if (bio->bi_status)
Miao Xie8b110e32014-09-12 18:44:03 +08007889 goto end;
7890
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307891 ASSERT(bio->bi_vcnt == 1);
Josef Bacik7870d082017-05-05 11:57:15 -04007892 io_tree = &BTRFS_I(inode)->io_tree;
7893 failure_tree = &BTRFS_I(inode)->io_failure_tree;
Ming Lei263663c2017-12-18 20:22:04 +08007894 ASSERT(bio_first_bvec_all(bio)->bv_len == btrfs_inode_sectorsize(inode));
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307895
Miao Xie8b110e32014-09-12 18:44:03 +08007896 done->uptodate = 1;
David Sterbac09abff2017-07-13 18:10:07 +02007897 ASSERT(!bio_flagged(bio, BIO_CLONED));
Ming Lei6dc4f102019-02-15 19:13:19 +08007898 bio_for_each_segment_all(bvec, bio, i, iter_all)
Josef Bacik7870d082017-05-05 11:57:15 -04007899 clean_io_failure(BTRFS_I(inode)->root->fs_info, failure_tree,
7900 io_tree, done->start, bvec->bv_page,
7901 btrfs_ino(BTRFS_I(inode)), 0);
Miao Xie8b110e32014-09-12 18:44:03 +08007902end:
7903 complete(&done->done);
7904 bio_put(bio);
7905}
7906
Omar Sandoval58efbc92017-08-22 23:45:59 -07007907static blk_status_t __btrfs_correct_data_nocsum(struct inode *inode,
7908 struct btrfs_io_bio *io_bio)
Josef Bacik4b46fce2010-05-23 11:00:55 -04007909{
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307910 struct btrfs_fs_info *fs_info;
Liu Bo17347ce2017-05-15 15:33:27 -07007911 struct bio_vec bvec;
7912 struct bvec_iter iter;
Miao Xie8b110e32014-09-12 18:44:03 +08007913 struct btrfs_retry_complete done;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007914 u64 start;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307915 unsigned int pgoff;
7916 u32 sectorsize;
7917 int nr_sectors;
Omar Sandoval58efbc92017-08-22 23:45:59 -07007918 blk_status_t ret;
7919 blk_status_t err = BLK_STS_OK;
Miao Xiedc380ae2014-09-12 18:43:55 +08007920
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307921 fs_info = BTRFS_I(inode)->root->fs_info;
Jeff Mahoneyda170662016-06-15 09:22:56 -04007922 sectorsize = fs_info->sectorsize;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307923
Miao Xiec1dc0892014-09-12 18:43:56 +08007924 start = io_bio->logical;
Miao Xie8b110e32014-09-12 18:44:03 +08007925 done.inode = inode;
Liu Bo17347ce2017-05-15 15:33:27 -07007926 io_bio->bio.bi_iter = io_bio->iter;
Miao Xie8b110e32014-09-12 18:44:03 +08007927
Liu Bo17347ce2017-05-15 15:33:27 -07007928 bio_for_each_segment(bvec, &io_bio->bio, iter) {
7929 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec.bv_len);
7930 pgoff = bvec.bv_offset;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307931
7932next_block_or_try_again:
Miao Xie8b110e32014-09-12 18:44:03 +08007933 done.uptodate = 0;
7934 done.start = start;
7935 init_completion(&done.done);
7936
Liu Bo17347ce2017-05-15 15:33:27 -07007937 ret = dio_read_error(inode, &io_bio->bio, bvec.bv_page,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307938 pgoff, start, start + sectorsize - 1,
7939 io_bio->mirror_num,
7940 btrfs_retry_endio_nocsum, &done);
Liu Bo629ebf42017-05-15 17:20:07 -07007941 if (ret) {
7942 err = ret;
7943 goto next;
7944 }
Miao Xie8b110e32014-09-12 18:44:03 +08007945
David Sterba9c17f6c2017-07-19 19:26:45 +02007946 wait_for_completion_io(&done.done);
Miao Xie8b110e32014-09-12 18:44:03 +08007947
7948 if (!done.uptodate) {
7949 /* We might have another mirror, so try again */
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307950 goto next_block_or_try_again;
Miao Xie8b110e32014-09-12 18:44:03 +08007951 }
7952
Liu Bo629ebf42017-05-15 17:20:07 -07007953next:
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307954 start += sectorsize;
7955
Liu Bo97bf5a52017-04-07 13:11:10 -07007956 nr_sectors--;
7957 if (nr_sectors) {
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307958 pgoff += sectorsize;
Liu Bo97bf5a52017-04-07 13:11:10 -07007959 ASSERT(pgoff < PAGE_SIZE);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307960 goto next_block_or_try_again;
7961 }
Miao Xie8b110e32014-09-12 18:44:03 +08007962 }
7963
Liu Bo629ebf42017-05-15 17:20:07 -07007964 return err;
Miao Xie8b110e32014-09-12 18:44:03 +08007965}
7966
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007967static void btrfs_retry_endio(struct bio *bio)
Miao Xie8b110e32014-09-12 18:44:03 +08007968{
7969 struct btrfs_retry_complete *done = bio->bi_private;
7970 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
Josef Bacik7870d082017-05-05 11:57:15 -04007971 struct extent_io_tree *io_tree, *failure_tree;
7972 struct inode *inode = done->inode;
Miao Xie8b110e32014-09-12 18:44:03 +08007973 struct bio_vec *bvec;
7974 int uptodate;
7975 int ret;
7976 int i;
Ming Lei6dc4f102019-02-15 19:13:19 +08007977 struct bvec_iter_all iter_all;
Miao Xie8b110e32014-09-12 18:44:03 +08007978
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02007979 if (bio->bi_status)
Miao Xie8b110e32014-09-12 18:44:03 +08007980 goto end;
7981
7982 uptodate = 1;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307983
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307984 ASSERT(bio->bi_vcnt == 1);
Ming Lei263663c2017-12-18 20:22:04 +08007985 ASSERT(bio_first_bvec_all(bio)->bv_len == btrfs_inode_sectorsize(done->inode));
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307986
Josef Bacik7870d082017-05-05 11:57:15 -04007987 io_tree = &BTRFS_I(inode)->io_tree;
7988 failure_tree = &BTRFS_I(inode)->io_failure_tree;
7989
David Sterbac09abff2017-07-13 18:10:07 +02007990 ASSERT(!bio_flagged(bio, BIO_CLONED));
Ming Lei6dc4f102019-02-15 19:13:19 +08007991 bio_for_each_segment_all(bvec, bio, i, iter_all) {
Josef Bacik7870d082017-05-05 11:57:15 -04007992 ret = __readpage_endio_check(inode, io_bio, i, bvec->bv_page,
7993 bvec->bv_offset, done->start,
7994 bvec->bv_len);
Miao Xie8b110e32014-09-12 18:44:03 +08007995 if (!ret)
Josef Bacik7870d082017-05-05 11:57:15 -04007996 clean_io_failure(BTRFS_I(inode)->root->fs_info,
7997 failure_tree, io_tree, done->start,
7998 bvec->bv_page,
7999 btrfs_ino(BTRFS_I(inode)),
8000 bvec->bv_offset);
Miao Xie8b110e32014-09-12 18:44:03 +08008001 else
8002 uptodate = 0;
8003 }
8004
8005 done->uptodate = uptodate;
8006end:
8007 complete(&done->done);
8008 bio_put(bio);
8009}
8010
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008011static blk_status_t __btrfs_subio_endio_read(struct inode *inode,
8012 struct btrfs_io_bio *io_bio, blk_status_t err)
Miao Xie8b110e32014-09-12 18:44:03 +08008013{
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308014 struct btrfs_fs_info *fs_info;
Liu Bo17347ce2017-05-15 15:33:27 -07008015 struct bio_vec bvec;
8016 struct bvec_iter iter;
Miao Xie8b110e32014-09-12 18:44:03 +08008017 struct btrfs_retry_complete done;
8018 u64 start;
8019 u64 offset = 0;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308020 u32 sectorsize;
8021 int nr_sectors;
8022 unsigned int pgoff;
8023 int csum_pos;
Liu Boef7cdac12017-04-13 18:11:48 -07008024 bool uptodate = (err == 0);
Miao Xie8b110e32014-09-12 18:44:03 +08008025 int ret;
Omar Sandoval58efbc92017-08-22 23:45:59 -07008026 blk_status_t status;
Miao Xie8b110e32014-09-12 18:44:03 +08008027
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308028 fs_info = BTRFS_I(inode)->root->fs_info;
Jeff Mahoneyda170662016-06-15 09:22:56 -04008029 sectorsize = fs_info->sectorsize;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308030
Omar Sandoval58efbc92017-08-22 23:45:59 -07008031 err = BLK_STS_OK;
Miao Xie8b110e32014-09-12 18:44:03 +08008032 start = io_bio->logical;
8033 done.inode = inode;
Liu Bo17347ce2017-05-15 15:33:27 -07008034 io_bio->bio.bi_iter = io_bio->iter;
Miao Xie8b110e32014-09-12 18:44:03 +08008035
Liu Bo17347ce2017-05-15 15:33:27 -07008036 bio_for_each_segment(bvec, &io_bio->bio, iter) {
8037 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec.bv_len);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308038
Liu Bo17347ce2017-05-15 15:33:27 -07008039 pgoff = bvec.bv_offset;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308040next_block:
Liu Boef7cdac12017-04-13 18:11:48 -07008041 if (uptodate) {
8042 csum_pos = BTRFS_BYTES_TO_BLKS(fs_info, offset);
8043 ret = __readpage_endio_check(inode, io_bio, csum_pos,
8044 bvec.bv_page, pgoff, start, sectorsize);
8045 if (likely(!ret))
8046 goto next;
8047 }
Miao Xie8b110e32014-09-12 18:44:03 +08008048try_again:
8049 done.uptodate = 0;
8050 done.start = start;
8051 init_completion(&done.done);
8052
Omar Sandoval58efbc92017-08-22 23:45:59 -07008053 status = dio_read_error(inode, &io_bio->bio, bvec.bv_page,
8054 pgoff, start, start + sectorsize - 1,
8055 io_bio->mirror_num, btrfs_retry_endio,
8056 &done);
8057 if (status) {
8058 err = status;
Miao Xie8b110e32014-09-12 18:44:03 +08008059 goto next;
8060 }
8061
David Sterba9c17f6c2017-07-19 19:26:45 +02008062 wait_for_completion_io(&done.done);
Miao Xie8b110e32014-09-12 18:44:03 +08008063
8064 if (!done.uptodate) {
8065 /* We might have another mirror, so try again */
8066 goto try_again;
8067 }
8068next:
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308069 offset += sectorsize;
8070 start += sectorsize;
8071
8072 ASSERT(nr_sectors);
8073
Liu Bo97bf5a52017-04-07 13:11:10 -07008074 nr_sectors--;
8075 if (nr_sectors) {
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308076 pgoff += sectorsize;
Liu Bo97bf5a52017-04-07 13:11:10 -07008077 ASSERT(pgoff < PAGE_SIZE);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308078 goto next_block;
8079 }
Kent Overstreet2c30c712013-11-07 12:20:26 -08008080 }
Miao Xiec1dc0892014-09-12 18:43:56 +08008081
8082 return err;
8083}
8084
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008085static blk_status_t btrfs_subio_endio_read(struct inode *inode,
8086 struct btrfs_io_bio *io_bio, blk_status_t err)
Miao Xie8b110e32014-09-12 18:44:03 +08008087{
8088 bool skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
8089
8090 if (skip_csum) {
8091 if (unlikely(err))
8092 return __btrfs_correct_data_nocsum(inode, io_bio);
8093 else
Omar Sandoval58efbc92017-08-22 23:45:59 -07008094 return BLK_STS_OK;
Miao Xie8b110e32014-09-12 18:44:03 +08008095 } else {
8096 return __btrfs_subio_endio_read(inode, io_bio, err);
8097 }
8098}
8099
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008100static void btrfs_endio_direct_read(struct bio *bio)
Miao Xiec1dc0892014-09-12 18:43:56 +08008101{
8102 struct btrfs_dio_private *dip = bio->bi_private;
8103 struct inode *inode = dip->inode;
8104 struct bio *dio_bio;
8105 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008106 blk_status_t err = bio->bi_status;
Miao Xiec1dc0892014-09-12 18:43:56 +08008107
Liu Bo99c4e3b92017-09-15 15:06:51 -06008108 if (dip->flags & BTRFS_DIO_ORIG_BIO_SUBMITTED)
Miao Xie8b110e32014-09-12 18:44:03 +08008109 err = btrfs_subio_endio_read(inode, io_bio, err);
Miao Xiec1dc0892014-09-12 18:43:56 +08008110
Josef Bacik4b46fce2010-05-23 11:00:55 -04008111 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01008112 dip->logical_offset + dip->bytes - 1);
Chris Mason9be33952013-05-17 18:30:14 -04008113 dio_bio = dip->dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008114
Josef Bacik4b46fce2010-05-23 11:00:55 -04008115 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04008116
Liu Bo99c4e3b92017-09-15 15:06:51 -06008117 dio_bio->bi_status = err;
Christoph Hellwig40553512017-06-03 09:37:58 +02008118 dio_end_io(dio_bio);
David Sterbab3a0dd52018-11-22 17:16:49 +01008119 btrfs_io_bio_free_csum(io_bio);
Chris Mason9be33952013-05-17 18:30:14 -04008120 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008121}
8122
Qu Wenruo524272602017-03-08 10:25:52 +08008123static void __endio_write_update_ordered(struct inode *inode,
8124 const u64 offset, const u64 bytes,
8125 const bool uptodate)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008126{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008127 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008128 struct btrfs_ordered_extent *ordered = NULL;
Qu Wenruo524272602017-03-08 10:25:52 +08008129 struct btrfs_workqueue *wq;
8130 btrfs_work_func_t func;
Filipe Manana14543772015-11-24 16:23:54 +00008131 u64 ordered_offset = offset;
8132 u64 ordered_bytes = bytes;
Naohiro Aota67c003f2017-09-01 17:59:07 +09008133 u64 last_offset;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008134
Qu Wenruo524272602017-03-08 10:25:52 +08008135 if (btrfs_is_free_space_inode(BTRFS_I(inode))) {
8136 wq = fs_info->endio_freespace_worker;
8137 func = btrfs_freespace_write_helper;
8138 } else {
8139 wq = fs_info->endio_write_workers;
8140 func = btrfs_endio_write_helper;
8141 }
8142
Nikolay Borisovb25f0d02018-04-11 11:21:17 +03008143 while (ordered_offset < offset + bytes) {
8144 last_offset = ordered_offset;
8145 if (btrfs_dec_test_first_ordered_pending(inode, &ordered,
8146 &ordered_offset,
8147 ordered_bytes,
8148 uptodate)) {
8149 btrfs_init_work(&ordered->work, func,
8150 finish_ordered_fn,
8151 NULL, NULL);
8152 btrfs_queue_work(wq, &ordered->work);
8153 }
8154 /*
8155 * If btrfs_dec_test_ordered_pending does not find any ordered
8156 * extent in the range, we can exit.
8157 */
8158 if (ordered_offset == last_offset)
8159 return;
8160 /*
8161 * Our bio might span multiple ordered extents. In this case
Andrea Gelmini52042d82018-11-28 12:05:13 +01008162 * we keep going until we have accounted the whole dio.
Nikolay Borisovb25f0d02018-04-11 11:21:17 +03008163 */
8164 if (ordered_offset < offset + bytes) {
8165 ordered_bytes = offset + bytes - ordered_offset;
8166 ordered = NULL;
8167 }
Chris Mason163cf092010-11-28 19:56:33 -05008168 }
Filipe Manana14543772015-11-24 16:23:54 +00008169}
8170
8171static void btrfs_endio_direct_write(struct bio *bio)
8172{
8173 struct btrfs_dio_private *dip = bio->bi_private;
8174 struct bio *dio_bio = dip->dio_bio;
8175
Qu Wenruo524272602017-03-08 10:25:52 +08008176 __endio_write_update_ordered(dip->inode, dip->logical_offset,
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008177 dip->bytes, !bio->bi_status);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008178
Josef Bacik4b46fce2010-05-23 11:00:55 -04008179 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04008180
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008181 dio_bio->bi_status = bio->bi_status;
Christoph Hellwig40553512017-06-03 09:37:58 +02008182 dio_end_io(dio_bio);
Chris Mason9be33952013-05-17 18:30:14 -04008183 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008184}
8185
David Sterbad0ee3932018-03-08 14:35:48 +01008186static blk_status_t btrfs_submit_bio_start_direct_io(void *private_data,
David Sterbad0779292018-03-08 13:47:33 +01008187 struct bio *bio, u64 offset)
Chris Masoneaf25d92010-05-25 09:48:28 -04008188{
Josef Bacikc6100a42017-05-05 11:57:13 -04008189 struct inode *inode = private_data;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008190 blk_status_t ret;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008191 ret = btrfs_csum_one_bio(inode, bio, offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008192 BUG_ON(ret); /* -ENOMEM */
Chris Masoneaf25d92010-05-25 09:48:28 -04008193 return 0;
8194}
8195
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008196static void btrfs_end_dio_bio(struct bio *bio)
Miao Xiee65e1532010-11-22 03:04:43 +00008197{
8198 struct btrfs_dio_private *dip = bio->bi_private;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008199 blk_status_t err = bio->bi_status;
Miao Xiee65e1532010-11-22 03:04:43 +00008200
Miao Xie8b110e32014-09-12 18:44:03 +08008201 if (err)
8202 btrfs_warn(BTRFS_I(dip->inode)->root->fs_info,
Mike Christie6296b962016-06-05 14:32:21 -05008203 "direct IO failed ino %llu rw %d,%u sector %#Lx len %u err no %d",
David Sterbaf85b7372017-01-20 14:54:07 +01008204 btrfs_ino(BTRFS_I(dip->inode)), bio_op(bio),
8205 bio->bi_opf,
Miao Xie8b110e32014-09-12 18:44:03 +08008206 (unsigned long long)bio->bi_iter.bi_sector,
8207 bio->bi_iter.bi_size, err);
8208
8209 if (dip->subio_endio)
8210 err = dip->subio_endio(dip->inode, btrfs_io_bio(bio), err);
Miao Xiec1dc0892014-09-12 18:43:56 +08008211
8212 if (err) {
Miao Xiee65e1532010-11-22 03:04:43 +00008213 /*
Nikolay Borisovde224b72018-02-14 10:53:36 +02008214 * We want to perceive the errors flag being set before
8215 * decrementing the reference count. We don't need a barrier
8216 * since atomic operations with a return value are fully
8217 * ordered as per atomic_t.txt
Miao Xiee65e1532010-11-22 03:04:43 +00008218 */
Nikolay Borisovde224b72018-02-14 10:53:36 +02008219 dip->errors = 1;
Miao Xiee65e1532010-11-22 03:04:43 +00008220 }
8221
8222 /* if there are more bios still pending for this dio, just exit */
8223 if (!atomic_dec_and_test(&dip->pending_bios))
8224 goto out;
8225
Chris Mason9be33952013-05-17 18:30:14 -04008226 if (dip->errors) {
Miao Xiee65e1532010-11-22 03:04:43 +00008227 bio_io_error(dip->orig_bio);
Chris Mason9be33952013-05-17 18:30:14 -04008228 } else {
Anand Jain2dbe0c72017-10-14 08:35:56 +08008229 dip->dio_bio->bi_status = BLK_STS_OK;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008230 bio_endio(dip->orig_bio);
Miao Xiee65e1532010-11-22 03:04:43 +00008231 }
8232out:
8233 bio_put(bio);
8234}
8235
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008236static inline blk_status_t btrfs_lookup_and_bind_dio_csum(struct inode *inode,
Miao Xiec1dc0892014-09-12 18:43:56 +08008237 struct btrfs_dio_private *dip,
8238 struct bio *bio,
8239 u64 file_offset)
8240{
8241 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
8242 struct btrfs_io_bio *orig_io_bio = btrfs_io_bio(dip->orig_bio);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008243 blk_status_t ret;
Miao Xiec1dc0892014-09-12 18:43:56 +08008244
8245 /*
8246 * We load all the csum data we need when we submit
8247 * the first bio to reduce the csum tree search and
8248 * contention.
8249 */
8250 if (dip->logical_offset == file_offset) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008251 ret = btrfs_lookup_bio_sums_dio(inode, dip->orig_bio,
Miao Xiec1dc0892014-09-12 18:43:56 +08008252 file_offset);
8253 if (ret)
8254 return ret;
8255 }
8256
8257 if (bio == dip->orig_bio)
8258 return 0;
8259
8260 file_offset -= dip->logical_offset;
8261 file_offset >>= inode->i_sb->s_blocksize_bits;
8262 io_bio->csum = (u8 *)(((u32 *)orig_io_bio->csum) + file_offset);
8263
8264 return 0;
8265}
8266
David Sterbad0ee3932018-03-08 14:35:48 +01008267static inline blk_status_t btrfs_submit_dio_bio(struct bio *bio,
8268 struct inode *inode, u64 file_offset, int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00008269{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008270 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xiefacc8a222013-07-25 19:22:34 +08008271 struct btrfs_dio_private *dip = bio->bi_private;
Mike Christie37226b22016-06-05 14:31:52 -05008272 bool write = bio_op(bio) == REQ_OP_WRITE;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008273 blk_status_t ret;
Miao Xiee65e1532010-11-22 03:04:43 +00008274
Liu Bo4c274bc2017-11-01 17:19:27 -06008275 /* Check btrfs_submit_bio_hook() for rules about async submit. */
Josef Bacikb812ce22012-11-16 13:56:32 -05008276 if (async_submit)
8277 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
8278
Josef Bacik5fd02042012-05-02 14:00:54 -04008279 if (!write) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008280 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DATA);
Josef Bacik5fd02042012-05-02 14:00:54 -04008281 if (ret)
8282 goto err;
8283 }
Miao Xiee65e1532010-11-22 03:04:43 +00008284
Nikolay Borisove6961ca2017-08-03 15:44:58 +03008285 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Josef Bacik1ae39932011-04-06 14:41:34 -04008286 goto map;
8287
8288 if (write && async_submit) {
Josef Bacikc6100a42017-05-05 11:57:13 -04008289 ret = btrfs_wq_submit_bio(fs_info, bio, 0, 0,
8290 file_offset, inode,
David Sterbae288c082018-07-18 17:36:24 +02008291 btrfs_submit_bio_start_direct_io);
Miao Xiee65e1532010-11-22 03:04:43 +00008292 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04008293 } else if (write) {
8294 /*
8295 * If we aren't doing async submit, calculate the csum of the
8296 * bio now.
8297 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008298 ret = btrfs_csum_one_bio(inode, bio, file_offset, 1);
Josef Bacik1ae39932011-04-06 14:41:34 -04008299 if (ret)
8300 goto err;
Miao Xie23ea8e52014-09-12 18:43:54 +08008301 } else {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008302 ret = btrfs_lookup_and_bind_dio_csum(inode, dip, bio,
Miao Xiec1dc0892014-09-12 18:43:56 +08008303 file_offset);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00008304 if (ret)
8305 goto err;
8306 }
Josef Bacik1ae39932011-04-06 14:41:34 -04008307map:
Liu Bo9b4a9b22017-08-18 11:54:02 -06008308 ret = btrfs_map_bio(fs_info, bio, 0, 0);
Miao Xiee65e1532010-11-22 03:04:43 +00008309err:
Miao Xiee65e1532010-11-22 03:04:43 +00008310 return ret;
8311}
8312
Nikolay Borisove6961ca2017-08-03 15:44:58 +03008313static int btrfs_submit_direct_hook(struct btrfs_dio_private *dip)
Miao Xiee65e1532010-11-22 03:04:43 +00008314{
8315 struct inode *inode = dip->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008316 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xiee65e1532010-11-22 03:04:43 +00008317 struct bio *bio;
8318 struct bio *orig_bio = dip->orig_bio;
Kent Overstreet4f024f32013-10-11 15:44:27 -07008319 u64 start_sector = orig_bio->bi_iter.bi_sector;
Miao Xiee65e1532010-11-22 03:04:43 +00008320 u64 file_offset = dip->logical_offset;
Miao Xiee65e1532010-11-22 03:04:43 +00008321 u64 map_length;
Josef Bacik1ae39932011-04-06 14:41:34 -04008322 int async_submit = 0;
Liu Bo725130b2017-05-16 09:51:39 -07008323 u64 submit_len;
8324 int clone_offset = 0;
8325 int clone_len;
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05308326 int ret;
Omar Sandoval58efbc92017-08-22 23:45:59 -07008327 blk_status_t status;
Miao Xiee65e1532010-11-22 03:04:43 +00008328
Kent Overstreet4f024f32013-10-11 15:44:27 -07008329 map_length = orig_bio->bi_iter.bi_size;
Liu Bo725130b2017-05-16 09:51:39 -07008330 submit_len = map_length;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008331 ret = btrfs_map_block(fs_info, btrfs_op(orig_bio), start_sector << 9,
8332 &map_length, NULL, 0);
Miao Xie7a5c3c92014-06-17 18:58:59 +08008333 if (ret)
Miao Xiee65e1532010-11-22 03:04:43 +00008334 return -EIO;
Miao Xiefacc8a222013-07-25 19:22:34 +08008335
Liu Bo725130b2017-05-16 09:51:39 -07008336 if (map_length >= submit_len) {
Josef Bacik02f57c72011-04-06 14:25:44 -04008337 bio = orig_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08008338 dip->flags |= BTRFS_DIO_ORIG_BIO_SUBMITTED;
Josef Bacik02f57c72011-04-06 14:25:44 -04008339 goto submit;
8340 }
8341
David Woodhouse53b381b2013-01-29 18:40:14 -05008342 /* async crcs make it difficult to collect full stripe writes. */
Jeff Mahoney1b868262017-05-17 11:38:35 -04008343 if (btrfs_data_alloc_profile(fs_info) & BTRFS_BLOCK_GROUP_RAID56_MASK)
David Woodhouse53b381b2013-01-29 18:40:14 -05008344 async_submit = 0;
8345 else
8346 async_submit = 1;
8347
Liu Bo725130b2017-05-16 09:51:39 -07008348 /* bio split */
8349 ASSERT(map_length <= INT_MAX);
Josef Bacik02f57c72011-04-06 14:25:44 -04008350 atomic_inc(&dip->pending_bios);
Arnd Bergmann3c91ee62017-05-18 15:33:29 +02008351 do {
Liu Bo725130b2017-05-16 09:51:39 -07008352 clone_len = min_t(int, submit_len, map_length);
Josef Bacik02f57c72011-04-06 14:25:44 -04008353
Liu Bo725130b2017-05-16 09:51:39 -07008354 /*
8355 * This will never fail as it's passing GPF_NOFS and
8356 * the allocation is backed by btrfs_bioset.
8357 */
Liu Boe4770942017-05-16 10:57:14 -07008358 bio = btrfs_bio_clone_partial(orig_bio, clone_offset,
Liu Bo725130b2017-05-16 09:51:39 -07008359 clone_len);
8360 bio->bi_private = dip;
8361 bio->bi_end_io = btrfs_end_dio_bio;
8362 btrfs_io_bio(bio)->logical = file_offset;
Miao Xiee65e1532010-11-22 03:04:43 +00008363
Liu Bo725130b2017-05-16 09:51:39 -07008364 ASSERT(submit_len >= clone_len);
8365 submit_len -= clone_len;
8366 if (submit_len == 0)
8367 break;
Miao Xiee65e1532010-11-22 03:04:43 +00008368
Liu Bo725130b2017-05-16 09:51:39 -07008369 /*
8370 * Increase the count before we submit the bio so we know
8371 * the end IO handler won't happen before we increase the
8372 * count. Otherwise, the dip might get freed before we're
8373 * done setting it up.
8374 */
8375 atomic_inc(&dip->pending_bios);
Miao Xiee65e1532010-11-22 03:04:43 +00008376
David Sterbad0ee3932018-03-08 14:35:48 +01008377 status = btrfs_submit_dio_bio(bio, inode, file_offset,
Omar Sandoval58efbc92017-08-22 23:45:59 -07008378 async_submit);
8379 if (status) {
Liu Bo725130b2017-05-16 09:51:39 -07008380 bio_put(bio);
8381 atomic_dec(&dip->pending_bios);
8382 goto out_err;
Miao Xiee65e1532010-11-22 03:04:43 +00008383 }
Liu Bo725130b2017-05-16 09:51:39 -07008384
8385 clone_offset += clone_len;
8386 start_sector += clone_len >> 9;
8387 file_offset += clone_len;
8388
8389 map_length = submit_len;
8390 ret = btrfs_map_block(fs_info, btrfs_op(orig_bio),
8391 start_sector << 9, &map_length, NULL, 0);
8392 if (ret)
8393 goto out_err;
Arnd Bergmann3c91ee62017-05-18 15:33:29 +02008394 } while (submit_len > 0);
Miao Xiee65e1532010-11-22 03:04:43 +00008395
Josef Bacik02f57c72011-04-06 14:25:44 -04008396submit:
David Sterbad0ee3932018-03-08 14:35:48 +01008397 status = btrfs_submit_dio_bio(bio, inode, file_offset, async_submit);
Omar Sandoval58efbc92017-08-22 23:45:59 -07008398 if (!status)
Miao Xiee65e1532010-11-22 03:04:43 +00008399 return 0;
8400
8401 bio_put(bio);
8402out_err:
8403 dip->errors = 1;
8404 /*
Nikolay Borisovde224b72018-02-14 10:53:36 +02008405 * Before atomic variable goto zero, we must make sure dip->errors is
8406 * perceived to be set. This ordering is ensured by the fact that an
8407 * atomic operations with a return value are fully ordered as per
8408 * atomic_t.txt
Miao Xiee65e1532010-11-22 03:04:43 +00008409 */
Miao Xiee65e1532010-11-22 03:04:43 +00008410 if (atomic_dec_and_test(&dip->pending_bios))
8411 bio_io_error(dip->orig_bio);
8412
8413 /* bio_end_io() will handle error, so we needn't return it */
8414 return 0;
8415}
8416
Mike Christie8a4c1e42016-06-05 14:31:50 -05008417static void btrfs_submit_direct(struct bio *dio_bio, struct inode *inode,
8418 loff_t file_offset)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008419{
Filipe Manana61de7182015-07-01 12:13:10 +01008420 struct btrfs_dio_private *dip = NULL;
Liu Bo3892ac92017-04-17 15:00:28 -07008421 struct bio *bio = NULL;
8422 struct btrfs_io_bio *io_bio;
Mike Christie8a4c1e42016-06-05 14:31:50 -05008423 bool write = (bio_op(dio_bio) == REQ_OP_WRITE);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008424 int ret = 0;
8425
David Sterba8b6c1d52017-06-02 17:48:13 +02008426 bio = btrfs_bio_clone(dio_bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008427
Miao Xiec1dc0892014-09-12 18:43:56 +08008428 dip = kzalloc(sizeof(*dip), GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04008429 if (!dip) {
8430 ret = -ENOMEM;
Filipe Manana61de7182015-07-01 12:13:10 +01008431 goto free_ordered;
Chris Mason9be33952013-05-17 18:30:14 -04008432 }
8433
8434 dip->private = dio_bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008435 dip->inode = inode;
8436 dip->logical_offset = file_offset;
Kent Overstreet4f024f32013-10-11 15:44:27 -07008437 dip->bytes = dio_bio->bi_iter.bi_size;
8438 dip->disk_bytenr = (u64)dio_bio->bi_iter.bi_sector << 9;
Liu Bo3892ac92017-04-17 15:00:28 -07008439 bio->bi_private = dip;
8440 dip->orig_bio = bio;
Chris Mason9be33952013-05-17 18:30:14 -04008441 dip->dio_bio = dio_bio;
Miao Xiee65e1532010-11-22 03:04:43 +00008442 atomic_set(&dip->pending_bios, 0);
Liu Bo3892ac92017-04-17 15:00:28 -07008443 io_bio = btrfs_io_bio(bio);
8444 io_bio->logical = file_offset;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008445
Miao Xiec1dc0892014-09-12 18:43:56 +08008446 if (write) {
Liu Bo3892ac92017-04-17 15:00:28 -07008447 bio->bi_end_io = btrfs_endio_direct_write;
Miao Xiec1dc0892014-09-12 18:43:56 +08008448 } else {
Liu Bo3892ac92017-04-17 15:00:28 -07008449 bio->bi_end_io = btrfs_endio_direct_read;
Miao Xiec1dc0892014-09-12 18:43:56 +08008450 dip->subio_endio = btrfs_subio_endio_read;
8451 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04008452
Filipe Mananaf28a4922015-12-08 19:23:20 +00008453 /*
8454 * Reset the range for unsubmitted ordered extents (to a 0 length range)
8455 * even if we fail to submit a bio, because in such case we do the
8456 * corresponding error handling below and it must not be done a second
8457 * time by btrfs_direct_IO().
8458 */
8459 if (write) {
8460 struct btrfs_dio_data *dio_data = current->journal_info;
8461
8462 dio_data->unsubmitted_oe_range_end = dip->logical_offset +
8463 dip->bytes;
8464 dio_data->unsubmitted_oe_range_start =
8465 dio_data->unsubmitted_oe_range_end;
8466 }
8467
Nikolay Borisove6961ca2017-08-03 15:44:58 +03008468 ret = btrfs_submit_direct_hook(dip);
Miao Xiee65e1532010-11-22 03:04:43 +00008469 if (!ret)
Chris Masoneaf25d92010-05-25 09:48:28 -04008470 return;
Chris Mason9be33952013-05-17 18:30:14 -04008471
David Sterbab3a0dd52018-11-22 17:16:49 +01008472 btrfs_io_bio_free_csum(io_bio);
Chris Mason9be33952013-05-17 18:30:14 -04008473
Josef Bacik4b46fce2010-05-23 11:00:55 -04008474free_ordered:
8475 /*
Filipe Manana61de7182015-07-01 12:13:10 +01008476 * If we arrived here it means either we failed to submit the dip
8477 * or we either failed to clone the dio_bio or failed to allocate the
8478 * dip. If we cloned the dio_bio and allocated the dip, we can just
8479 * call bio_endio against our io_bio so that we get proper resource
8480 * cleanup if we fail to submit the dip, otherwise, we must do the
8481 * same as btrfs_endio_direct_[write|read] because we can't call these
8482 * callbacks - they require an allocated dip and a clone of dio_bio.
Josef Bacik4b46fce2010-05-23 11:00:55 -04008483 */
Liu Bo3892ac92017-04-17 15:00:28 -07008484 if (bio && dip) {
Guoqing Jiang054ec2f2017-06-02 16:08:50 +08008485 bio_io_error(bio);
Filipe Manana61de7182015-07-01 12:13:10 +01008486 /*
Liu Bo3892ac92017-04-17 15:00:28 -07008487 * The end io callbacks free our dip, do the final put on bio
Filipe Manana61de7182015-07-01 12:13:10 +01008488 * and all the cleanup and final put for dio_bio (through
8489 * dio_end_io()).
8490 */
8491 dip = NULL;
Liu Bo3892ac92017-04-17 15:00:28 -07008492 bio = NULL;
Filipe Manana61de7182015-07-01 12:13:10 +01008493 } else {
Filipe Manana14543772015-11-24 16:23:54 +00008494 if (write)
Qu Wenruo524272602017-03-08 10:25:52 +08008495 __endio_write_update_ordered(inode,
Filipe Manana14543772015-11-24 16:23:54 +00008496 file_offset,
8497 dio_bio->bi_iter.bi_size,
Qu Wenruo524272602017-03-08 10:25:52 +08008498 false);
Filipe Manana14543772015-11-24 16:23:54 +00008499 else
Filipe Manana61de7182015-07-01 12:13:10 +01008500 unlock_extent(&BTRFS_I(inode)->io_tree, file_offset,
8501 file_offset + dio_bio->bi_iter.bi_size - 1);
Filipe Manana14543772015-11-24 16:23:54 +00008502
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008503 dio_bio->bi_status = BLK_STS_IOERR;
Filipe Manana61de7182015-07-01 12:13:10 +01008504 /*
8505 * Releases and cleans up our dio_bio, no need to bio_put()
8506 * nor bio_endio()/bio_io_error() against dio_bio.
8507 */
Christoph Hellwig40553512017-06-03 09:37:58 +02008508 dio_end_io(dio_bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008509 }
Liu Bo3892ac92017-04-17 15:00:28 -07008510 if (bio)
8511 bio_put(bio);
Filipe Manana61de7182015-07-01 12:13:10 +01008512 kfree(dip);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008513}
8514
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008515static ssize_t check_direct_IO(struct btrfs_fs_info *fs_info,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008516 const struct iov_iter *iter, loff_t offset)
Chris Mason5a5f79b2010-05-26 21:33:37 -04008517{
8518 int seg;
Josef Bacika1b75f72011-04-08 15:51:18 +00008519 int i;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008520 unsigned int blocksize_mask = fs_info->sectorsize - 1;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008521 ssize_t retval = -EINVAL;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008522
8523 if (offset & blocksize_mask)
8524 goto out;
8525
Al Viro28060d52014-03-22 05:15:17 -04008526 if (iov_iter_alignment(iter) & blocksize_mask)
8527 goto out;
Josef Bacika1b75f72011-04-08 15:51:18 +00008528
Al Viro28060d52014-03-22 05:15:17 -04008529 /* If this is a write we don't need to check anymore */
Al Virocd27e452016-10-10 13:39:05 -04008530 if (iov_iter_rw(iter) != READ || !iter_is_iovec(iter))
Al Viro28060d52014-03-22 05:15:17 -04008531 return 0;
8532 /*
8533 * Check to make sure we don't have duplicate iov_base's in this
8534 * iovec, if so return EINVAL, otherwise we'll get csum errors
8535 * when reading back.
8536 */
8537 for (seg = 0; seg < iter->nr_segs; seg++) {
8538 for (i = seg + 1; i < iter->nr_segs; i++) {
8539 if (iter->iov[seg].iov_base == iter->iov[i].iov_base)
Josef Bacika1b75f72011-04-08 15:51:18 +00008540 goto out;
8541 }
Chris Mason5a5f79b2010-05-26 21:33:37 -04008542 }
8543 retval = 0;
8544out:
8545 return retval;
8546}
Josef Bacikeb838e72012-07-31 16:28:48 -04008547
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008548static ssize_t btrfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
Chris Mason16432982008-04-10 10:23:21 -04008549{
Josef Bacik4b46fce2010-05-23 11:00:55 -04008550 struct file *file = iocb->ki_filp;
8551 struct inode *inode = file->f_mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008552 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
chandan50745b02015-08-28 21:10:13 +05308553 struct btrfs_dio_data dio_data = { 0 };
Qu Wenruo364ecf32017-02-27 15:10:38 +08008554 struct extent_changeset *data_reserved = NULL;
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008555 loff_t offset = iocb->ki_pos;
Miao Xie09348562013-02-07 10:12:07 +00008556 size_t count = 0;
Miao Xie2e60a512013-02-08 07:01:08 +00008557 int flags = 0;
Miao Xie38851cc2013-02-08 07:04:11 +00008558 bool wakeup = true;
8559 bool relock = false;
Miao Xie09348562013-02-07 10:12:07 +00008560 ssize_t ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008561
Nikolay Borisov8c70c9f2017-08-21 12:43:46 +03008562 if (check_direct_IO(fs_info, iter, offset))
Chris Mason5a5f79b2010-05-26 21:33:37 -04008563 return 0;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008564
Jens Axboefe0f07d2015-04-15 17:05:48 -06008565 inode_dio_begin(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008566
Josef Bacik0e267c42013-07-02 10:38:02 -04008567 /*
Miao Xie41bd9ca2014-03-06 13:54:57 +08008568 * The generic stuff only does filemap_write_and_wait_range, which
8569 * isn't enough if we've written compressed pages to this area, so
8570 * we need to flush the dirty pages again to make absolutely sure
8571 * that any outstanding dirty pages are on disk.
Josef Bacik0e267c42013-07-02 10:38:02 -04008572 */
Al Viroa6cbcd42014-03-04 22:38:00 -05008573 count = iov_iter_count(iter);
Miao Xie41bd9ca2014-03-06 13:54:57 +08008574 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
8575 &BTRFS_I(inode)->runtime_flags))
Wang Shilong9a025a02014-07-17 11:44:13 +08008576 filemap_fdatawrite_range(inode->i_mapping, offset,
8577 offset + count - 1);
Josef Bacik0e267c42013-07-02 10:38:02 -04008578
Omar Sandoval6f673762015-03-16 04:33:52 -07008579 if (iov_iter_rw(iter) == WRITE) {
Miao Xie38851cc2013-02-08 07:04:11 +00008580 /*
8581 * If the write DIO is beyond the EOF, we need update
8582 * the isize, but it is protected by i_mutex. So we can
8583 * not unlock the i_mutex at this case.
8584 */
8585 if (offset + count <= inode->i_size) {
Liu Bo4aaedfb2016-12-14 22:36:05 -08008586 dio_data.overwrite = 1;
Al Viro59551022016-01-22 15:40:57 -05008587 inode_unlock(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008588 relock = true;
Goldwyn Rodriguesedf064e2017-06-20 07:05:49 -05008589 } else if (iocb->ki_flags & IOCB_NOWAIT) {
8590 ret = -EAGAIN;
8591 goto out;
Miao Xie38851cc2013-02-08 07:04:11 +00008592 }
Qu Wenruo364ecf32017-02-27 15:10:38 +08008593 ret = btrfs_delalloc_reserve_space(inode, &data_reserved,
8594 offset, count);
Miao Xie09348562013-02-07 10:12:07 +00008595 if (ret)
Miao Xie38851cc2013-02-08 07:04:11 +00008596 goto out;
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008597
8598 /*
8599 * We need to know how many extents we reserved so that we can
8600 * do the accounting properly if we go over the number we
8601 * originally calculated. Abuse current->journal_info for this.
8602 */
Jeff Mahoneyda170662016-06-15 09:22:56 -04008603 dio_data.reserve = round_up(count,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008604 fs_info->sectorsize);
Filipe Mananaf28a4922015-12-08 19:23:20 +00008605 dio_data.unsubmitted_oe_range_start = (u64)offset;
8606 dio_data.unsubmitted_oe_range_end = (u64)offset;
chandan50745b02015-08-28 21:10:13 +05308607 current->journal_info = &dio_data;
Chandan Rajendra97dcdea2016-12-23 15:00:18 +05308608 down_read(&BTRFS_I(inode)->dio_sem);
David Sterbaee39b432014-09-30 01:33:33 +02008609 } else if (test_bit(BTRFS_INODE_READDIO_NEED_LOCK,
8610 &BTRFS_I(inode)->runtime_flags)) {
Jens Axboefe0f07d2015-04-15 17:05:48 -06008611 inode_dio_end(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008612 flags = DIO_LOCKING | DIO_SKIP_HOLES;
8613 wakeup = false;
Miao Xie09348562013-02-07 10:12:07 +00008614 }
8615
Omar Sandoval17f8c842015-03-16 04:33:50 -07008616 ret = __blockdev_direct_IO(iocb, inode,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008617 fs_info->fs_devices->latest_bdev,
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008618 iter, btrfs_get_blocks_direct, NULL,
Omar Sandoval17f8c842015-03-16 04:33:50 -07008619 btrfs_submit_direct, flags);
Omar Sandoval6f673762015-03-16 04:33:52 -07008620 if (iov_iter_rw(iter) == WRITE) {
Chandan Rajendra97dcdea2016-12-23 15:00:18 +05308621 up_read(&BTRFS_I(inode)->dio_sem);
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008622 current->journal_info = NULL;
Liu Boddba1bf2015-06-17 16:59:58 +08008623 if (ret < 0 && ret != -EIOCBQUEUED) {
chandan50745b02015-08-28 21:10:13 +05308624 if (dio_data.reserve)
Qu Wenruobc42bda2017-02-27 15:10:39 +08008625 btrfs_delalloc_release_space(inode, data_reserved,
Qu Wenruo43b18592017-12-12 15:34:32 +08008626 offset, dio_data.reserve, true);
Filipe Mananaf28a4922015-12-08 19:23:20 +00008627 /*
8628 * On error we might have left some ordered extents
8629 * without submitting corresponding bios for them, so
8630 * cleanup them up to avoid other tasks getting them
8631 * and waiting for them to complete forever.
8632 */
8633 if (dio_data.unsubmitted_oe_range_start <
8634 dio_data.unsubmitted_oe_range_end)
Qu Wenruo524272602017-03-08 10:25:52 +08008635 __endio_write_update_ordered(inode,
Filipe Mananaf28a4922015-12-08 19:23:20 +00008636 dio_data.unsubmitted_oe_range_start,
8637 dio_data.unsubmitted_oe_range_end -
8638 dio_data.unsubmitted_oe_range_start,
Qu Wenruo524272602017-03-08 10:25:52 +08008639 false);
Liu Boddba1bf2015-06-17 16:59:58 +08008640 } else if (ret >= 0 && (size_t)ret < count)
Qu Wenruobc42bda2017-02-27 15:10:39 +08008641 btrfs_delalloc_release_space(inode, data_reserved,
Qu Wenruo43b18592017-12-12 15:34:32 +08008642 offset, count - (size_t)ret, true);
8643 btrfs_delalloc_release_extents(BTRFS_I(inode), count, false);
Miao Xie09348562013-02-07 10:12:07 +00008644 }
Miao Xie38851cc2013-02-08 07:04:11 +00008645out:
Miao Xie2e60a512013-02-08 07:01:08 +00008646 if (wakeup)
Jens Axboefe0f07d2015-04-15 17:05:48 -06008647 inode_dio_end(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008648 if (relock)
Al Viro59551022016-01-22 15:40:57 -05008649 inode_lock(inode);
Miao Xie09348562013-02-07 10:12:07 +00008650
Qu Wenruo364ecf32017-02-27 15:10:38 +08008651 extent_changeset_free(data_reserved);
Miao Xie09348562013-02-07 10:12:07 +00008652 return ret;
Chris Mason16432982008-04-10 10:23:21 -04008653}
8654
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008655#define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
8656
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008657static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
8658 __u64 start, __u64 len)
8659{
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008660 int ret;
8661
8662 ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS);
8663 if (ret)
8664 return ret;
8665
David Sterba2135fb92017-06-23 04:09:57 +02008666 return extent_fiemap(inode, fieinfo, start, len);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008667}
8668
Chris Mason9ebefb182007-06-15 13:50:00 -04008669int btrfs_readpage(struct file *file, struct page *page)
8670{
Chris Masond1310b22008-01-24 16:13:08 -05008671 struct extent_io_tree *tree;
8672 tree = &BTRFS_I(page->mapping->host)->io_tree;
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02008673 return extent_read_full_page(tree, page, btrfs_get_extent, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04008674}
Chris Mason1832a6d2007-12-21 16:27:21 -05008675
Chris Mason39279cc2007-06-12 06:35:45 -04008676static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
8677{
Josef Bacikbe7bd732015-10-22 15:05:09 -04008678 struct inode *inode = page->mapping->host;
8679 int ret;
Chris Masonb888db22007-08-27 16:49:44 -04008680
8681 if (current->flags & PF_MEMALLOC) {
8682 redirty_page_for_writepage(wbc, page);
8683 unlock_page(page);
8684 return 0;
8685 }
Josef Bacikbe7bd732015-10-22 15:05:09 -04008686
8687 /*
8688 * If we are under memory pressure we will call this directly from the
8689 * VM, we need to make sure we have the inode referenced for the ordered
8690 * extent. If not just return like we didn't do anything.
8691 */
8692 if (!igrab(inode)) {
8693 redirty_page_for_writepage(wbc, page);
8694 return AOP_WRITEPAGE_ACTIVATE;
8695 }
Nikolay Borisov0a9b0e52017-12-08 15:55:59 +02008696 ret = extent_write_full_page(page, wbc);
Josef Bacikbe7bd732015-10-22 15:05:09 -04008697 btrfs_add_delayed_iput(inode);
8698 return ret;
Chris Masona52d9a82007-08-27 16:49:44 -04008699}
Chris Mason39279cc2007-06-12 06:35:45 -04008700
Eric Sandeen48a3b632013-04-25 20:41:01 +00008701static int btrfs_writepages(struct address_space *mapping,
8702 struct writeback_control *wbc)
Chris Masonb293f02e2007-11-01 19:45:34 -04008703{
Nikolay Borisov8ae225a2018-04-19 10:46:38 +03008704 return extent_writepages(mapping, wbc);
Chris Masonb293f02e2007-11-01 19:45:34 -04008705}
8706
Chris Mason3ab2fb52007-11-08 10:59:22 -05008707static int
8708btrfs_readpages(struct file *file, struct address_space *mapping,
8709 struct list_head *pages, unsigned nr_pages)
8710{
Nikolay Borisov2a3ff0a2018-04-19 10:46:36 +03008711 return extent_readpages(mapping, pages, nr_pages);
Chris Mason3ab2fb52007-11-08 10:59:22 -05008712}
Nikolay Borisov2a3ff0a2018-04-19 10:46:36 +03008713
Chris Masone6dcd2d2008-07-17 12:53:50 -04008714static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04008715{
Nikolay Borisov477a30b2018-04-19 10:46:34 +03008716 int ret = try_release_extent_mapping(page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04008717 if (ret == 1) {
8718 ClearPagePrivate(page);
8719 set_page_private(page, 0);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008720 put_page(page);
Chris Masona52d9a82007-08-27 16:49:44 -04008721 }
8722 return ret;
8723}
Chris Mason39279cc2007-06-12 06:35:45 -04008724
Chris Masone6dcd2d2008-07-17 12:53:50 -04008725static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
8726{
Chris Mason98509cf2008-09-11 15:51:43 -04008727 if (PageWriteback(page) || PageDirty(page))
8728 return 0;
Michal Hocko3ba7ab22017-01-09 15:39:02 +01008729 return __btrfs_releasepage(page, gfp_flags);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008730}
8731
Lukas Czernerd47992f2013-05-21 23:17:23 -04008732static void btrfs_invalidatepage(struct page *page, unsigned int offset,
8733 unsigned int length)
Chris Masona52d9a82007-08-27 16:49:44 -04008734{
Josef Bacik5fd02042012-05-02 14:00:54 -04008735 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05008736 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008737 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008738 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008739 u64 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008740 u64 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308741 u64 start;
8742 u64 end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008743 int inode_evicting = inode->i_state & I_FREEING;
Chris Masona52d9a82007-08-27 16:49:44 -04008744
Chris Mason8b62b722009-09-02 16:53:46 -04008745 /*
8746 * we have the page locked, so new writeback can't start,
8747 * and the dirty bit won't be cleared while we are here.
8748 *
8749 * Wait for IO on this page so that we can safely clear
8750 * the PagePrivate2 bit and do ordered accounting
8751 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008752 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04008753
Josef Bacik5fd02042012-05-02 14:00:54 -04008754 tree = &BTRFS_I(inode)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008755 if (offset) {
8756 btrfs_releasepage(page, GFP_NOFS);
8757 return;
8758 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008759
8760 if (!inode_evicting)
David Sterbaff13db42015-12-03 14:30:40 +01008761 lock_extent_bits(tree, page_start, page_end, &cached_state);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308762again:
8763 start = page_start;
Nikolay Borisova776c6f2017-02-20 13:50:49 +02008764 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), start,
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308765 page_end - start + 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008766 if (ordered) {
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308767 end = min(page_end, ordered->file_offset + ordered->len - 1);
Chris Masoneb84ae02008-07-17 13:53:27 -04008768 /*
8769 * IO on this page will never be started, so we need
8770 * to account for any ordered extents now
8771 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008772 if (!inode_evicting)
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308773 clear_extent_bit(tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008774 EXTENT_DIRTY | EXTENT_DELALLOC |
Filipe Mananaa7e3b972017-04-03 10:45:46 +01008775 EXTENT_DELALLOC_NEW |
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008776 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
David Sterbaae0f1622017-10-31 16:37:52 +01008777 EXTENT_DEFRAG, 1, 0, &cached_state);
Chris Mason8b62b722009-09-02 16:53:46 -04008778 /*
8779 * whoever cleared the private bit is responsible
8780 * for the finish_ordered_io
8781 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04008782 if (TestClearPagePrivate2(page)) {
8783 struct btrfs_ordered_inode_tree *tree;
8784 u64 new_len;
8785
8786 tree = &BTRFS_I(inode)->ordered_tree;
8787
8788 spin_lock_irq(&tree->lock);
8789 set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308790 new_len = start - ordered->file_offset;
Josef Bacik77cef2e2013-08-29 13:57:21 -04008791 if (new_len < ordered->truncated_len)
8792 ordered->truncated_len = new_len;
8793 spin_unlock_irq(&tree->lock);
8794
8795 if (btrfs_dec_test_ordered_pending(inode, &ordered,
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308796 start,
8797 end - start + 1, 1))
Josef Bacik77cef2e2013-08-29 13:57:21 -04008798 btrfs_finish_ordered_io(ordered);
Chris Mason8b62b722009-09-02 16:53:46 -04008799 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008800 btrfs_put_ordered_extent(ordered);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008801 if (!inode_evicting) {
8802 cached_state = NULL;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308803 lock_extent_bits(tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008804 &cached_state);
8805 }
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308806
8807 start = end + 1;
8808 if (start < page_end)
8809 goto again;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008810 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008811
Qu Wenruob9d0b382015-09-29 10:35:16 +08008812 /*
8813 * Qgroup reserved space handler
8814 * Page here will be either
8815 * 1) Already written to disk
8816 * In this case, its reserved space is released from data rsv map
8817 * and will be freed by delayed_ref handler finally.
8818 * So even we call qgroup_free_data(), it won't decrease reserved
8819 * space.
8820 * 2) Not written to disk
Goldwyn Rodrigues0b34c262016-09-30 10:40:52 -05008821 * This means the reserved space should be freed here. However,
8822 * if a truncate invalidates the page (by clearing PageDirty)
8823 * and the page is accounted for while allocating extent
8824 * in btrfs_check_data_free_space() we let delayed_ref to
8825 * free the entire extent.
Qu Wenruob9d0b382015-09-29 10:35:16 +08008826 */
Goldwyn Rodrigues0b34c262016-09-30 10:40:52 -05008827 if (PageDirty(page))
Qu Wenruobc42bda2017-02-27 15:10:39 +08008828 btrfs_qgroup_free_data(inode, NULL, page_start, PAGE_SIZE);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008829 if (!inode_evicting) {
8830 clear_extent_bit(tree, page_start, page_end,
8831 EXTENT_LOCKED | EXTENT_DIRTY |
Filipe Mananaa7e3b972017-04-03 10:45:46 +01008832 EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
8833 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1,
David Sterbaae0f1622017-10-31 16:37:52 +01008834 &cached_state);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008835
8836 __btrfs_releasepage(page, GFP_NOFS);
8837 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008838
Chris Mason4a096752008-07-21 10:29:44 -04008839 ClearPageChecked(page);
Chris Mason9ad6b7bc2008-04-18 16:11:30 -04008840 if (PagePrivate(page)) {
Chris Mason9ad6b7bc2008-04-18 16:11:30 -04008841 ClearPagePrivate(page);
8842 set_page_private(page, 0);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008843 put_page(page);
Chris Mason9ad6b7bc2008-04-18 16:11:30 -04008844 }
Chris Mason39279cc2007-06-12 06:35:45 -04008845}
8846
Chris Mason9ebefb182007-06-15 13:50:00 -04008847/*
8848 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
8849 * called from a page fault handler when a page is first dirtied. Hence we must
8850 * be careful to check for EOF conditions here. We set the page up correctly
8851 * for a written page which means we get ENOSPC checking when writing into
8852 * holes and correct delalloc and unwritten extent mapping on filesystems that
8853 * support these features.
8854 *
8855 * We are not allowed to take the i_mutex here so we have to play games to
8856 * protect against truncate races as the page could now be beyond EOF. Because
Omar Sandovald1342aa2018-05-11 13:13:29 -07008857 * truncate_setsize() writes the inode size before removing pages, once we have
8858 * the page lock we can determine safely if the page is beyond EOF. If it is not
Chris Mason9ebefb182007-06-15 13:50:00 -04008859 * beyond EOF, then the page is guaranteed safe against truncation until we
8860 * unlock the page.
8861 */
Souptick Joardera528a242018-06-06 19:54:44 +05308862vm_fault_t btrfs_page_mkwrite(struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04008863{
Nick Pigginc2ec1752009-03-31 15:23:21 -07008864 struct page *page = vmf->page;
Dave Jiang11bac802017-02-24 14:56:41 -08008865 struct inode *inode = file_inode(vmf->vma->vm_file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008866 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008867 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
8868 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008869 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08008870 struct extent_changeset *data_reserved = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008871 char *kaddr;
8872 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04008873 loff_t size;
Souptick Joardera528a242018-06-06 19:54:44 +05308874 vm_fault_t ret;
8875 int ret2;
Chris Mason9998eb72012-01-25 13:47:40 -05008876 int reserved = 0;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308877 u64 reserved_space;
Chris Masona52d9a82007-08-27 16:49:44 -04008878 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008879 u64 page_end;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308880 u64 end;
8881
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008882 reserved_space = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04008883
Jan Karab2b5ef52012-06-12 16:20:45 +02008884 sb_start_pagefault(inode->i_sb);
Qu Wenruodf480632015-09-08 17:25:54 +08008885 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008886 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308887 end = page_end;
Qu Wenruodf480632015-09-08 17:25:54 +08008888
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308889 /*
8890 * Reserving delalloc space after obtaining the page lock can lead to
8891 * deadlock. For example, if a dirty page is locked by this function
8892 * and the call to btrfs_delalloc_reserve_space() ends up triggering
8893 * dirty page write out, then the btrfs_writepage() function could
8894 * end up waiting indefinitely to get a lock on the page currently
8895 * being processed by btrfs_page_mkwrite() function.
8896 */
Souptick Joardera528a242018-06-06 19:54:44 +05308897 ret2 = btrfs_delalloc_reserve_space(inode, &data_reserved, page_start,
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308898 reserved_space);
Souptick Joardera528a242018-06-06 19:54:44 +05308899 if (!ret2) {
8900 ret2 = file_update_time(vmf->vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05008901 reserved = 1;
8902 }
Souptick Joardera528a242018-06-06 19:54:44 +05308903 if (ret2) {
8904 ret = vmf_error(ret2);
Chris Mason9998eb72012-01-25 13:47:40 -05008905 if (reserved)
8906 goto out;
8907 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07008908 }
Chris Mason1832a6d2007-12-21 16:27:21 -05008909
Nick Piggin56a76f82009-03-31 15:23:23 -07008910 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008911again:
Chris Mason9ebefb182007-06-15 13:50:00 -04008912 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04008913 size = i_size_read(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04008914
Chris Mason9ebefb182007-06-15 13:50:00 -04008915 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04008916 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04008917 /* page got truncated out from underneath us */
8918 goto out_unlock;
8919 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008920 wait_on_page_writeback(page);
8921
David Sterbaff13db42015-12-03 14:30:40 +01008922 lock_extent_bits(io_tree, page_start, page_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008923 set_page_extent_mapped(page);
8924
Chris Masoneb84ae02008-07-17 13:53:27 -04008925 /*
8926 * we can't set the delalloc bits if there are pending ordered
8927 * extents. Drop our locks and wait for them to finish
8928 */
Nikolay Borisova776c6f2017-02-20 13:50:49 +02008929 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start,
8930 PAGE_SIZE);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008931 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00008932 unlock_extent_cached(io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01008933 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008934 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04008935 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008936 btrfs_put_ordered_extent(ordered);
8937 goto again;
8938 }
8939
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008940 if (page->index == ((size - 1) >> PAGE_SHIFT)) {
Jeff Mahoneyda170662016-06-15 09:22:56 -04008941 reserved_space = round_up(size - page_start,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008942 fs_info->sectorsize);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008943 if (reserved_space < PAGE_SIZE) {
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308944 end = page_start + reserved_space - 1;
Qu Wenruobc42bda2017-02-27 15:10:39 +08008945 btrfs_delalloc_release_space(inode, data_reserved,
Qu Wenruo43b18592017-12-12 15:34:32 +08008946 page_start, PAGE_SIZE - reserved_space,
8947 true);
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308948 }
8949 }
8950
Josef Bacikfbf19082009-10-01 17:10:23 -04008951 /*
Liu Bo54160342016-12-13 12:15:19 -08008952 * page_mkwrite gets called when the page is firstly dirtied after it's
8953 * faulted in, but write(2) could also dirty a page and set delalloc
8954 * bits, thus in this case for space account reason, we still need to
8955 * clear any delalloc bits within this page range since we have to
8956 * reserve data&meta space before lock_page() (see above comments).
Josef Bacikfbf19082009-10-01 17:10:23 -04008957 */
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308958 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06008959 EXTENT_DIRTY | EXTENT_DELALLOC |
8960 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
David Sterbaae0f1622017-10-31 16:37:52 +01008961 0, 0, &cached_state);
Josef Bacikfbf19082009-10-01 17:10:23 -04008962
Souptick Joardera528a242018-06-06 19:54:44 +05308963 ret2 = btrfs_set_extent_delalloc(inode, page_start, end, 0,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08008964 &cached_state, 0);
Souptick Joardera528a242018-06-06 19:54:44 +05308965 if (ret2) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00008966 unlock_extent_cached(io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01008967 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04008968 ret = VM_FAULT_SIGBUS;
8969 goto out_unlock;
8970 }
Souptick Joardera528a242018-06-06 19:54:44 +05308971 ret2 = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04008972
8973 /* page is wholly or partially inside EOF */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008974 if (page_start + PAGE_SIZE > size)
Johannes Thumshirn70730172018-12-05 15:23:03 +01008975 zero_start = offset_in_page(size);
Chris Mason9ebefb182007-06-15 13:50:00 -04008976 else
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008977 zero_start = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04008978
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008979 if (zero_start != PAGE_SIZE) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04008980 kaddr = kmap(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008981 memset(kaddr + zero_start, 0, PAGE_SIZE - zero_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008982 flush_dcache_page(page);
8983 kunmap(page);
8984 }
Chris Mason247e7432008-07-17 12:53:51 -04008985 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008986 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04008987 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04008988
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008989 BTRFS_I(inode)->last_trans = fs_info->generation;
Chris Mason257c62e2009-10-13 13:21:08 -04008990 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06008991 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
Chris Mason257c62e2009-10-13 13:21:08 -04008992
David Sterbae43bbe52017-12-12 21:43:52 +01008993 unlock_extent_cached(io_tree, page_start, page_end, &cached_state);
Chris Mason9ebefb182007-06-15 13:50:00 -04008994
Souptick Joardera528a242018-06-06 19:54:44 +05308995 if (!ret2) {
Qu Wenruo43b18592017-12-12 15:34:32 +08008996 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE, true);
Jan Karab2b5ef52012-06-12 16:20:45 +02008997 sb_end_pagefault(inode->i_sb);
Qu Wenruo364ecf32017-02-27 15:10:38 +08008998 extent_changeset_free(data_reserved);
Chris Mason50a9b212009-09-11 12:33:12 -04008999 return VM_FAULT_LOCKED;
Jan Karab2b5ef52012-06-12 16:20:45 +02009000 }
Chris Mason717beb92018-06-25 10:03:41 -07009001
9002out_unlock:
Chris Mason9ebefb182007-06-15 13:50:00 -04009003 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05009004out:
Qu Wenruo43b18592017-12-12 15:34:32 +08009005 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE, (ret != 0));
Qu Wenruobc42bda2017-02-27 15:10:39 +08009006 btrfs_delalloc_release_space(inode, data_reserved, page_start,
Qu Wenruo43b18592017-12-12 15:34:32 +08009007 reserved_space, (ret != 0));
Chris Mason9998eb72012-01-25 13:47:40 -05009008out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02009009 sb_end_pagefault(inode->i_sb);
Qu Wenruo364ecf32017-02-27 15:10:38 +08009010 extent_changeset_free(data_reserved);
Chris Mason9ebefb182007-06-15 13:50:00 -04009011 return ret;
9012}
9013
Filipe Manana213e8c52018-02-06 20:40:31 +00009014static int btrfs_truncate(struct inode *inode, bool skip_writeback)
Chris Mason39279cc2007-06-12 06:35:45 -04009015{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009016 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009017 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04009018 struct btrfs_block_rsv *rsv;
Omar Sandovalad7e1a72018-05-22 09:59:50 -07009019 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04009020 struct btrfs_trans_handle *trans;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009021 u64 mask = fs_info->sectorsize - 1;
9022 u64 min_size = btrfs_calc_trunc_metadata_size(fs_info, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04009023
Filipe Manana213e8c52018-02-06 20:40:31 +00009024 if (!skip_writeback) {
9025 ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask),
9026 (u64)-1);
9027 if (ret)
9028 return ret;
9029 }
Chris Mason39279cc2007-06-12 06:35:45 -04009030
Josef Bacikfcb80c22011-05-03 10:40:22 -04009031 /*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07009032 * Yes ladies and gentlemen, this is indeed ugly. We have a couple of
9033 * things going on here:
Josef Bacikfcb80c22011-05-03 10:40:22 -04009034 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07009035 * 1) We need to reserve space to update our inode.
Josef Bacikfcb80c22011-05-03 10:40:22 -04009036 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07009037 * 2) We need to have something to cache all the space that is going to
Josef Bacikfcb80c22011-05-03 10:40:22 -04009038 * be free'd up by the truncate operation, but also have some slack
9039 * space reserved in case it uses space during the truncate (thank you
9040 * very much snapshotting).
9041 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07009042 * And we need these to be separate. The fact is we can use a lot of
Josef Bacikfcb80c22011-05-03 10:40:22 -04009043 * space doing the truncate, and we have no earthly idea how much space
Nicholas D Steeves01327612016-05-19 21:18:45 -04009044 * we will use, so we need the truncate reservation to be separate so it
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07009045 * doesn't end up using space reserved for updating the inode. We also
9046 * need to be able to stop the transaction and start a new one, which
9047 * means we need to be able to update the inode several times, and we
9048 * have no idea of knowing how many times that will be, so we can't just
9049 * reserve 1 item for the entirety of the operation, so that has to be
9050 * done separately as well.
Josef Bacikfcb80c22011-05-03 10:40:22 -04009051 *
9052 * So that leaves us with
9053 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07009054 * 1) rsv - for the truncate reservation, which we will steal from the
Josef Bacikfcb80c22011-05-03 10:40:22 -04009055 * transaction reservation.
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07009056 * 2) fs_info->trans_block_rsv - this will have 1 items worth left for
Josef Bacikfcb80c22011-05-03 10:40:22 -04009057 * updating the inode.
9058 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009059 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009060 if (!rsv)
9061 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04009062 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04009063 rsv->failfast = 1;
Josef Bacikf0cd8462011-03-04 14:37:08 -05009064
Josef Bacik907cbce2011-08-08 13:46:15 -04009065 /*
Josef Bacik07127182011-08-19 10:29:59 -04009066 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04009067 * 1 for updating the inode.
9068 */
Josef Bacikf3fe8202013-01-07 17:03:21 -05009069 trans = btrfs_start_transaction(root, 2);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009070 if (IS_ERR(trans)) {
Omar Sandovalad7e1a72018-05-22 09:59:50 -07009071 ret = PTR_ERR(trans);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009072 goto out;
9073 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05009074
Josef Bacik907cbce2011-08-08 13:46:15 -04009075 /* Migrate the slack space for the truncate to our reserve */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009076 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, rsv,
Lu Fengqi3a584172018-08-04 21:10:55 +08009077 min_size, false);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009078 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05009079
Chris Mason5a3f23d2009-03-31 13:27:11 -04009080 /*
Josef Bacik5dc562c2012-08-17 13:14:17 -04009081 * So if we truncate and then write and fsync we normally would just
9082 * write the extents that changed, which is a problem if we need to
9083 * first truncate that entire inode. So set this flag so we write out
9084 * all of the extents in the inode to the sync log so we're completely
9085 * safe.
9086 */
9087 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009088 trans->block_rsv = rsv;
Josef Bacik5dc562c2012-08-17 13:14:17 -04009089
Yan, Zheng80825102009-11-12 09:35:36 +00009090 while (1) {
9091 ret = btrfs_truncate_inode_items(trans, root, inode,
9092 inode->i_size,
9093 BTRFS_EXTENT_DATA_KEY);
Josef Bacikddfae632017-10-19 14:16:02 -04009094 trans->block_rsv = &fs_info->trans_block_rsv;
Omar Sandovalad7e1a72018-05-22 09:59:50 -07009095 if (ret != -ENOSPC && ret != -EAGAIN)
Yan, Zheng80825102009-11-12 09:35:36 +00009096 break;
Chris Mason39279cc2007-06-12 06:35:45 -04009097
Yan, Zheng80825102009-11-12 09:35:36 +00009098 ret = btrfs_update_inode(trans, root, inode);
Omar Sandovalad7e1a72018-05-22 09:59:50 -07009099 if (ret)
Josef Bacik3893e332011-01-31 16:03:11 -05009100 break;
Josef Bacikca7e70f2012-08-27 17:48:15 -04009101
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009102 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009103 btrfs_btree_balance_dirty(fs_info);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009104
9105 trans = btrfs_start_transaction(root, 2);
9106 if (IS_ERR(trans)) {
Omar Sandovalad7e1a72018-05-22 09:59:50 -07009107 ret = PTR_ERR(trans);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009108 trans = NULL;
9109 break;
9110 }
9111
Wang Xiaoguang47b5d642016-09-07 20:17:38 +08009112 btrfs_block_rsv_release(fs_info, rsv, -1);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009113 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv,
Lu Fengqi3a584172018-08-04 21:10:55 +08009114 rsv, min_size, false);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009115 BUG_ON(ret); /* shouldn't happen */
9116 trans->block_rsv = rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00009117 }
9118
Josef Bacikddfae632017-10-19 14:16:02 -04009119 /*
9120 * We can't call btrfs_truncate_block inside a trans handle as we could
9121 * deadlock with freeze, if we got NEED_TRUNCATE_BLOCK then we know
9122 * we've truncated everything except the last little bit, and can do
9123 * btrfs_truncate_block and then update the disk_i_size.
9124 */
9125 if (ret == NEED_TRUNCATE_BLOCK) {
9126 btrfs_end_transaction(trans);
9127 btrfs_btree_balance_dirty(fs_info);
9128
9129 ret = btrfs_truncate_block(inode, inode->i_size, 0, 0);
9130 if (ret)
9131 goto out;
9132 trans = btrfs_start_transaction(root, 1);
9133 if (IS_ERR(trans)) {
9134 ret = PTR_ERR(trans);
9135 goto out;
9136 }
9137 btrfs_ordered_update_i_size(inode, inode->i_size, NULL);
9138 }
9139
Chris Mason917c16b2011-11-08 14:49:59 -05009140 if (trans) {
Omar Sandovalad7e1a72018-05-22 09:59:50 -07009141 int ret2;
Josef Bacik7b128762008-07-24 12:17:14 -04009142
Omar Sandovalad7e1a72018-05-22 09:59:50 -07009143 trans->block_rsv = &fs_info->trans_block_rsv;
9144 ret2 = btrfs_update_inode(trans, root, inode);
9145 if (ret2 && !ret)
9146 ret = ret2;
9147
9148 ret2 = btrfs_end_transaction(trans);
9149 if (ret2 && !ret)
9150 ret = ret2;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009151 btrfs_btree_balance_dirty(fs_info);
Chris Mason917c16b2011-11-08 14:49:59 -05009152 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04009153out:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009154 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009155
Omar Sandovalad7e1a72018-05-22 09:59:50 -07009156 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04009157}
9158
Sven Wegener3b963622008-06-09 21:57:42 -04009159/*
Chris Masond352ac62008-09-29 15:18:18 -04009160 * create a new subvolume directory/inode (helper for the ioctl).
9161 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05009162int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009163 struct btrfs_root *new_root,
9164 struct btrfs_root *parent_root,
9165 u64 new_dirid)
Chris Mason39279cc2007-06-12 06:35:45 -04009166{
Chris Mason39279cc2007-06-12 06:35:45 -04009167 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04009168 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04009169 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009170
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01009171 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
9172 new_dirid, new_dirid,
9173 S_IFDIR | (~current_umask() & S_IRWXUGO),
9174 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04009175 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04009176 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009177 inode->i_op = &btrfs_dir_inode_operations;
9178 inode->i_fop = &btrfs_dir_file_operations;
9179
Miklos Szeredibfe86842011-10-28 14:13:29 +02009180 set_nlink(inode, 1);
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02009181 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Masonb0d5d102014-09-08 13:08:51 -07009182 unlock_new_inode(inode);
Sven Wegener3b963622008-06-09 21:57:42 -04009183
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009184 err = btrfs_subvol_inherit_props(trans, new_root, parent_root);
9185 if (err)
9186 btrfs_err(new_root->fs_info,
David Sterba351fd352014-05-15 16:48:20 +02009187 "error inheriting subvolume %llu properties: %d",
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009188 new_root->root_key.objectid, err);
9189
Yan, Zheng76dda932009-09-21 16:00:26 -04009190 err = btrfs_update_inode(trans, new_root, inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04009191
Yan, Zheng76dda932009-09-21 16:00:26 -04009192 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07009193 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04009194}
9195
Chris Mason39279cc2007-06-12 06:35:45 -04009196struct inode *btrfs_alloc_inode(struct super_block *sb)
9197{
Josef Bacik69fe2d72017-10-19 14:15:57 -04009198 struct btrfs_fs_info *fs_info = btrfs_sb(sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009199 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009200 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009201
David Sterba712e36c2017-10-31 17:08:27 +01009202 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_KERNEL);
Chris Mason39279cc2007-06-12 06:35:45 -04009203 if (!ei)
9204 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009205
9206 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009207 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04009208 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04009209 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04009210 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009211 ei->delalloc_bytes = 0;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01009212 ei->new_delalloc_bytes = 0;
Wang Shilong47059d92014-07-03 18:22:07 +08009213 ei->defrag_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009214 ei->disk_i_size = 0;
9215 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04009216 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009217 ei->index_cnt = (u64)-1;
Miao Xie67de1172013-12-26 13:07:06 +08009218 ei->dir_index = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009219 ei->last_unlink_trans = 0;
Liu Bo46d8bc32012-08-29 01:07:55 -06009220 ei->last_log_commit = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009221
Josef Bacik9e0baf62011-07-15 15:16:44 +00009222 spin_lock_init(&ei->lock);
9223 ei->outstanding_extents = 0;
Josef Bacik69fe2d72017-10-19 14:15:57 -04009224 if (sb->s_magic != BTRFS_TEST_MAGIC)
9225 btrfs_init_metadata_block_rsv(fs_info, &ei->block_rsv,
9226 BTRFS_BLOCK_RSV_DELALLOC);
Josef Bacik72ac3c02012-05-23 14:13:11 -04009227 ei->runtime_flags = 0;
David Sterbab52aa8c2017-07-17 19:17:20 +02009228 ei->prop_compress = BTRFS_COMPRESS_NONE;
David Sterbaeec63c62017-07-17 19:41:31 +02009229 ei->defrag_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009230
Miao Xie16cdcec2011-04-22 18:12:22 +08009231 ei->delayed_node = NULL;
9232
chandan r9cc97d62012-07-04 12:48:07 +05309233 ei->i_otime.tv_sec = 0;
9234 ei->i_otime.tv_nsec = 0;
9235
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009236 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02009237 extent_map_tree_init(&ei->extent_tree);
Qu Wenruo43eb5f22019-03-01 10:47:59 +08009238 extent_io_tree_init(fs_info, &ei->io_tree, IO_TREE_INODE_IO, inode);
9239 extent_io_tree_init(fs_info, &ei->io_failure_tree,
9240 IO_TREE_INODE_IO_FAILURE, inode);
David Sterba7b439732019-03-11 15:58:30 +01009241 ei->io_tree.track_uptodate = true;
9242 ei->io_failure_tree.track_uptodate = true;
Josef Bacikb812ce22012-11-16 13:56:32 -05009243 atomic_set(&ei->sync_writers, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009244 mutex_init(&ei->log_mutex);
Josef Bacikf2486792012-01-13 12:09:22 -05009245 mutex_init(&ei->delalloc_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009246 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009247 INIT_LIST_HEAD(&ei->delalloc_inodes);
David Sterba8089fe62015-11-19 14:15:51 +01009248 INIT_LIST_HEAD(&ei->delayed_iput);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009249 RB_CLEAR_NODE(&ei->rb_node);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01009250 init_rwsem(&ei->dio_sem);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009251
9252 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009253}
9254
Josef Bacikaaedb552013-10-11 14:44:09 -04009255#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
9256void btrfs_test_destroy_inode(struct inode *inode)
9257{
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02009258 btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0);
Josef Bacikaaedb552013-10-11 14:44:09 -04009259 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
9260}
9261#endif
9262
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009263static void btrfs_i_callback(struct rcu_head *head)
9264{
9265 struct inode *inode = container_of(head, struct inode, i_rcu);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009266 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
9267}
9268
Chris Mason39279cc2007-06-12 06:35:45 -04009269void btrfs_destroy_inode(struct inode *inode)
9270{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009271 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009272 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009273 struct btrfs_root *root = BTRFS_I(inode)->root;
9274
Al Virob3d9b7a2012-06-09 13:51:19 -04009275 WARN_ON(!hlist_empty(&inode->i_dentry));
Chris Mason39279cc2007-06-12 06:35:45 -04009276 WARN_ON(inode->i_data.nrpages);
Josef Bacik69fe2d72017-10-19 14:15:57 -04009277 WARN_ON(BTRFS_I(inode)->block_rsv.reserved);
9278 WARN_ON(BTRFS_I(inode)->block_rsv.size);
Josef Bacik9e0baf62011-07-15 15:16:44 +00009279 WARN_ON(BTRFS_I(inode)->outstanding_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04009280 WARN_ON(BTRFS_I(inode)->delalloc_bytes);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01009281 WARN_ON(BTRFS_I(inode)->new_delalloc_bytes);
Josef Bacik7709cde2011-08-04 10:25:02 -04009282 WARN_ON(BTRFS_I(inode)->csum_bytes);
Wang Shilong47059d92014-07-03 18:22:07 +08009283 WARN_ON(BTRFS_I(inode)->defrag_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04009284
Chris Mason5a3f23d2009-03-31 13:27:11 -04009285 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05009286 * This can happen where we create an inode, but somebody else also
9287 * created the same inode and we need to destroy the one we already
9288 * created.
9289 */
9290 if (!root)
9291 goto free;
9292
Chris Masond3977122009-01-05 21:25:51 -05009293 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04009294 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
9295 if (!ordered)
9296 break;
9297 else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009298 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04009299 "found ordered extent %llu %llu on inode cleanup",
9300 ordered->file_offset, ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009301 btrfs_remove_ordered_extent(inode, ordered);
9302 btrfs_put_ordered_extent(ordered);
9303 btrfs_put_ordered_extent(ordered);
9304 }
9305 }
Qu Wenruo56fa9d02015-10-13 09:53:10 +08009306 btrfs_qgroup_check_reserved_leak(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04009307 inode_tree_del(inode);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02009308 btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0);
Josef Bacika6dbd422009-11-11 15:53:34 -05009309free:
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009310 call_rcu(&inode->i_rcu, btrfs_i_callback);
Chris Mason39279cc2007-06-12 06:35:45 -04009311}
9312
Al Viro45321ac2010-06-07 13:43:19 -04009313int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04009314{
9315 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04009316
Naohiro Aota6379ef92013-06-06 09:56:34 +00009317 if (root == NULL)
9318 return 1;
9319
Liu Bofa6ac872013-02-20 14:10:23 +00009320 /* the snap/subvol tree is on deleting */
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02009321 if (btrfs_root_refs(&root->root_item) == 0)
Al Viro45321ac2010-06-07 13:43:19 -04009322 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04009323 else
Al Viro45321ac2010-06-07 13:43:19 -04009324 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04009325}
9326
Sven Wegener0ee0fda2008-07-30 16:54:26 -04009327static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04009328{
9329 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
9330
9331 inode_init_once(&ei->vfs_inode);
9332}
9333
David Sterbae67c7182018-02-19 17:24:18 +01009334void __cold btrfs_destroy_cachep(void)
Chris Mason39279cc2007-06-12 06:35:45 -04009335{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10009336 /*
9337 * Make sure all delayed rcu free inodes are flushed before we
9338 * destroy cache.
9339 */
9340 rcu_barrier();
Kinglong Mee5598e902016-01-29 21:36:35 +08009341 kmem_cache_destroy(btrfs_inode_cachep);
9342 kmem_cache_destroy(btrfs_trans_handle_cachep);
Kinglong Mee5598e902016-01-29 21:36:35 +08009343 kmem_cache_destroy(btrfs_path_cachep);
9344 kmem_cache_destroy(btrfs_free_space_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04009345}
9346
Liu Bof5c29bd2017-11-02 17:21:50 -06009347int __init btrfs_init_cachep(void)
Chris Mason39279cc2007-06-12 06:35:45 -04009348{
David Sterba837e1972012-09-07 03:00:48 -06009349 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009350 sizeof(struct btrfs_inode), 0,
Vladimir Davydov5d097052016-01-14 15:18:21 -08009351 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD | SLAB_ACCOUNT,
9352 init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04009353 if (!btrfs_inode_cachep)
9354 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009355
David Sterba837e1972012-09-07 03:00:48 -06009356 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009357 sizeof(struct btrfs_trans_handle), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009358 SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009359 if (!btrfs_trans_handle_cachep)
9360 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009361
David Sterba837e1972012-09-07 03:00:48 -06009362 btrfs_path_cachep = kmem_cache_create("btrfs_path",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009363 sizeof(struct btrfs_path), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009364 SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009365 if (!btrfs_path_cachep)
9366 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009367
David Sterba837e1972012-09-07 03:00:48 -06009368 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
Josef Bacikdc89e982011-01-28 17:05:48 -05009369 sizeof(struct btrfs_free_space), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009370 SLAB_MEM_SPREAD, NULL);
Josef Bacikdc89e982011-01-28 17:05:48 -05009371 if (!btrfs_free_space_cachep)
9372 goto fail;
9373
Chris Mason39279cc2007-06-12 06:35:45 -04009374 return 0;
9375fail:
9376 btrfs_destroy_cachep();
9377 return -ENOMEM;
9378}
9379
David Howellsa528d352017-01-31 16:46:22 +00009380static int btrfs_getattr(const struct path *path, struct kstat *stat,
9381 u32 request_mask, unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04009382{
Miao Xiedf0af1a2013-01-29 10:11:59 +00009383 u64 delalloc_bytes;
David Howellsa528d352017-01-31 16:46:22 +00009384 struct inode *inode = d_inode(path->dentry);
David Sterbafadc0d82011-11-20 07:33:38 -05009385 u32 blocksize = inode->i_sb->s_blocksize;
Yonghong Song04a87e32017-05-12 15:07:43 -07009386 u32 bi_flags = BTRFS_I(inode)->flags;
9387
9388 stat->result_mask |= STATX_BTIME;
9389 stat->btime.tv_sec = BTRFS_I(inode)->i_otime.tv_sec;
9390 stat->btime.tv_nsec = BTRFS_I(inode)->i_otime.tv_nsec;
9391 if (bi_flags & BTRFS_INODE_APPEND)
9392 stat->attributes |= STATX_ATTR_APPEND;
9393 if (bi_flags & BTRFS_INODE_COMPRESS)
9394 stat->attributes |= STATX_ATTR_COMPRESSED;
9395 if (bi_flags & BTRFS_INODE_IMMUTABLE)
9396 stat->attributes |= STATX_ATTR_IMMUTABLE;
9397 if (bi_flags & BTRFS_INODE_NODUMP)
9398 stat->attributes |= STATX_ATTR_NODUMP;
9399
9400 stat->attributes_mask |= (STATX_ATTR_APPEND |
9401 STATX_ATTR_COMPRESSED |
9402 STATX_ATTR_IMMUTABLE |
9403 STATX_ATTR_NODUMP);
David Sterbafadc0d82011-11-20 07:33:38 -05009404
Chris Mason39279cc2007-06-12 06:35:45 -04009405 generic_fillattr(inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04009406 stat->dev = BTRFS_I(inode)->root->anon_dev;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009407
9408 spin_lock(&BTRFS_I(inode)->lock);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01009409 delalloc_bytes = BTRFS_I(inode)->new_delalloc_bytes;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009410 spin_unlock(&BTRFS_I(inode)->lock);
David Sterbafadc0d82011-11-20 07:33:38 -05009411 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
Miao Xiedf0af1a2013-01-29 10:11:59 +00009412 ALIGN(delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04009413 return 0;
9414}
9415
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009416static int btrfs_rename_exchange(struct inode *old_dir,
9417 struct dentry *old_dentry,
9418 struct inode *new_dir,
9419 struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04009420{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009421 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009422 struct btrfs_trans_handle *trans;
9423 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009424 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009425 struct inode *new_inode = new_dentry->d_inode;
9426 struct inode *old_inode = old_dentry->d_inode;
Deepa Dinamani95582b02018-05-08 19:36:02 -07009427 struct timespec64 ctime = current_time(old_inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009428 struct dentry *parent;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009429 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
9430 u64 new_ino = btrfs_ino(BTRFS_I(new_inode));
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009431 u64 old_idx = 0;
9432 u64 new_idx = 0;
9433 u64 root_objectid;
9434 int ret;
Filipe Manana86e8aa02016-05-05 02:02:27 +01009435 bool root_log_pinned = false;
9436 bool dest_log_pinned = false;
Filipe Mananad4682ba2018-06-11 19:24:28 +01009437 struct btrfs_log_ctx ctx_root;
9438 struct btrfs_log_ctx ctx_dest;
9439 bool sync_log_root = false;
9440 bool sync_log_dest = false;
9441 bool commit_transaction = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009442
9443 /* we only allow rename subvolume link between subvolumes */
9444 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
9445 return -EXDEV;
9446
Filipe Mananad4682ba2018-06-11 19:24:28 +01009447 btrfs_init_log_ctx(&ctx_root, old_inode);
9448 btrfs_init_log_ctx(&ctx_dest, new_inode);
9449
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009450 /* close the race window with snapshot create/destroy ioctl */
9451 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009452 down_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009453 if (new_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009454 down_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009455
9456 /*
9457 * We want to reserve the absolute worst case amount of items. So if
9458 * both inodes are subvols and we need to unlink them then that would
9459 * require 4 item modifications, but if they are both normal inodes it
9460 * would require 5 item modifications, so we'll assume their normal
9461 * inodes. So 5 * 2 is 10, plus 2 for the new links, so 12 total items
9462 * should cover the worst case number of items we'll modify.
9463 */
9464 trans = btrfs_start_transaction(root, 12);
9465 if (IS_ERR(trans)) {
9466 ret = PTR_ERR(trans);
9467 goto out_notrans;
9468 }
9469
9470 /*
9471 * We need to find a free sequence number both in the source and
9472 * in the destination directory for the exchange.
9473 */
Nikolay Borisov877574e2017-02-20 13:50:33 +02009474 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009475 if (ret)
9476 goto out_fail;
Nikolay Borisov877574e2017-02-20 13:50:33 +02009477 ret = btrfs_set_inode_index(BTRFS_I(old_dir), &new_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009478 if (ret)
9479 goto out_fail;
9480
9481 BTRFS_I(old_inode)->dir_index = 0ULL;
9482 BTRFS_I(new_inode)->dir_index = 0ULL;
9483
9484 /* Reference for the source. */
9485 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
9486 /* force full log commit if subvolume involved. */
David Sterba90787762019-03-20 13:28:05 +01009487 btrfs_set_log_full_commit(trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009488 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01009489 btrfs_pin_log_trans(root);
9490 root_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009491 ret = btrfs_insert_inode_ref(trans, dest,
9492 new_dentry->d_name.name,
9493 new_dentry->d_name.len,
9494 old_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01009495 btrfs_ino(BTRFS_I(new_dir)),
9496 old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009497 if (ret)
9498 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009499 }
9500
9501 /* And now for the dest. */
9502 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
9503 /* force full log commit if subvolume involved. */
David Sterba90787762019-03-20 13:28:05 +01009504 btrfs_set_log_full_commit(trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009505 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01009506 btrfs_pin_log_trans(dest);
9507 dest_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009508 ret = btrfs_insert_inode_ref(trans, root,
9509 old_dentry->d_name.name,
9510 old_dentry->d_name.len,
9511 new_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01009512 btrfs_ino(BTRFS_I(old_dir)),
9513 new_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009514 if (ret)
9515 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009516 }
9517
9518 /* Update inode version and ctime/mtime. */
9519 inode_inc_iversion(old_dir);
9520 inode_inc_iversion(new_dir);
9521 inode_inc_iversion(old_inode);
9522 inode_inc_iversion(new_inode);
9523 old_dir->i_ctime = old_dir->i_mtime = ctime;
9524 new_dir->i_ctime = new_dir->i_mtime = ctime;
9525 old_inode->i_ctime = ctime;
9526 new_inode->i_ctime = ctime;
9527
9528 if (old_dentry->d_parent != new_dentry->d_parent) {
David Sterbaf85b7372017-01-20 14:54:07 +01009529 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
9530 BTRFS_I(old_inode), 1);
9531 btrfs_record_unlink_dir(trans, BTRFS_I(new_dir),
9532 BTRFS_I(new_inode), 1);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009533 }
9534
9535 /* src is a subvolume */
9536 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
9537 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
Lu Fengqi401b3b12018-08-01 11:32:30 +08009538 ret = btrfs_unlink_subvol(trans, old_dir, root_objectid,
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009539 old_dentry->d_name.name,
9540 old_dentry->d_name.len);
9541 } else { /* src is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009542 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
9543 BTRFS_I(old_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009544 old_dentry->d_name.name,
9545 old_dentry->d_name.len);
9546 if (!ret)
9547 ret = btrfs_update_inode(trans, root, old_inode);
9548 }
9549 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009550 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009551 goto out_fail;
9552 }
9553
9554 /* dest is a subvolume */
9555 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
9556 root_objectid = BTRFS_I(new_inode)->root->root_key.objectid;
Lu Fengqi401b3b12018-08-01 11:32:30 +08009557 ret = btrfs_unlink_subvol(trans, new_dir, root_objectid,
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009558 new_dentry->d_name.name,
9559 new_dentry->d_name.len);
9560 } else { /* dest is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009561 ret = __btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
9562 BTRFS_I(new_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009563 new_dentry->d_name.name,
9564 new_dentry->d_name.len);
9565 if (!ret)
9566 ret = btrfs_update_inode(trans, dest, new_inode);
9567 }
9568 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009569 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009570 goto out_fail;
9571 }
9572
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009573 ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009574 new_dentry->d_name.name,
9575 new_dentry->d_name.len, 0, old_idx);
9576 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009577 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009578 goto out_fail;
9579 }
9580
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009581 ret = btrfs_add_link(trans, BTRFS_I(old_dir), BTRFS_I(new_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009582 old_dentry->d_name.name,
9583 old_dentry->d_name.len, 0, new_idx);
9584 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009585 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009586 goto out_fail;
9587 }
9588
9589 if (old_inode->i_nlink == 1)
9590 BTRFS_I(old_inode)->dir_index = old_idx;
9591 if (new_inode->i_nlink == 1)
9592 BTRFS_I(new_inode)->dir_index = new_idx;
9593
Filipe Manana86e8aa02016-05-05 02:02:27 +01009594 if (root_log_pinned) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009595 parent = new_dentry->d_parent;
Filipe Mananad4682ba2018-06-11 19:24:28 +01009596 ret = btrfs_log_new_name(trans, BTRFS_I(old_inode),
9597 BTRFS_I(old_dir), parent,
9598 false, &ctx_root);
9599 if (ret == BTRFS_NEED_LOG_SYNC)
9600 sync_log_root = true;
9601 else if (ret == BTRFS_NEED_TRANS_COMMIT)
9602 commit_transaction = true;
9603 ret = 0;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009604 btrfs_end_log_trans(root);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009605 root_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009606 }
Filipe Manana86e8aa02016-05-05 02:02:27 +01009607 if (dest_log_pinned) {
Filipe Mananad4682ba2018-06-11 19:24:28 +01009608 if (!commit_transaction) {
9609 parent = old_dentry->d_parent;
9610 ret = btrfs_log_new_name(trans, BTRFS_I(new_inode),
9611 BTRFS_I(new_dir), parent,
9612 false, &ctx_dest);
9613 if (ret == BTRFS_NEED_LOG_SYNC)
9614 sync_log_dest = true;
9615 else if (ret == BTRFS_NEED_TRANS_COMMIT)
9616 commit_transaction = true;
9617 ret = 0;
9618 }
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009619 btrfs_end_log_trans(dest);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009620 dest_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009621 }
9622out_fail:
Filipe Manana86e8aa02016-05-05 02:02:27 +01009623 /*
9624 * If we have pinned a log and an error happened, we unpin tasks
9625 * trying to sync the log and force them to fallback to a transaction
9626 * commit if the log currently contains any of the inodes involved in
9627 * this rename operation (to ensure we do not persist a log with an
9628 * inconsistent state for any of these inodes or leading to any
9629 * inconsistencies when replayed). If the transaction was aborted, the
9630 * abortion reason is propagated to userspace when attempting to commit
9631 * the transaction. If the log does not contain any of these inodes, we
9632 * allow the tasks to sync it.
9633 */
9634 if (ret && (root_log_pinned || dest_log_pinned)) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009635 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
9636 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
9637 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana86e8aa02016-05-05 02:02:27 +01009638 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009639 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
David Sterba90787762019-03-20 13:28:05 +01009640 btrfs_set_log_full_commit(trans);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009641
9642 if (root_log_pinned) {
9643 btrfs_end_log_trans(root);
9644 root_log_pinned = false;
9645 }
9646 if (dest_log_pinned) {
9647 btrfs_end_log_trans(dest);
9648 dest_log_pinned = false;
9649 }
9650 }
Filipe Mananad4682ba2018-06-11 19:24:28 +01009651 if (!ret && sync_log_root && !commit_transaction) {
9652 ret = btrfs_sync_log(trans, BTRFS_I(old_inode)->root,
9653 &ctx_root);
9654 if (ret)
9655 commit_transaction = true;
9656 }
9657 if (!ret && sync_log_dest && !commit_transaction) {
9658 ret = btrfs_sync_log(trans, BTRFS_I(new_inode)->root,
9659 &ctx_dest);
9660 if (ret)
9661 commit_transaction = true;
9662 }
9663 if (commit_transaction) {
9664 ret = btrfs_commit_transaction(trans);
9665 } else {
9666 int ret2;
9667
9668 ret2 = btrfs_end_transaction(trans);
9669 ret = ret ? ret : ret2;
9670 }
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009671out_notrans:
9672 if (new_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009673 up_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009674 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009675 up_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009676
9677 return ret;
9678}
9679
9680static int btrfs_whiteout_for_rename(struct btrfs_trans_handle *trans,
9681 struct btrfs_root *root,
9682 struct inode *dir,
9683 struct dentry *dentry)
9684{
9685 int ret;
9686 struct inode *inode;
9687 u64 objectid;
9688 u64 index;
9689
9690 ret = btrfs_find_free_ino(root, &objectid);
9691 if (ret)
9692 return ret;
9693
9694 inode = btrfs_new_inode(trans, root, dir,
9695 dentry->d_name.name,
9696 dentry->d_name.len,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009697 btrfs_ino(BTRFS_I(dir)),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009698 objectid,
9699 S_IFCHR | WHITEOUT_MODE,
9700 &index);
9701
9702 if (IS_ERR(inode)) {
9703 ret = PTR_ERR(inode);
9704 return ret;
9705 }
9706
9707 inode->i_op = &btrfs_special_inode_operations;
9708 init_special_inode(inode, inode->i_mode,
9709 WHITEOUT_DEV);
9710
9711 ret = btrfs_init_inode_security(trans, inode, dir,
9712 &dentry->d_name);
9713 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009714 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009715
Nikolay Borisovcef415a2017-02-20 13:51:09 +02009716 ret = btrfs_add_nondir(trans, BTRFS_I(dir), dentry,
9717 BTRFS_I(inode), 0, index);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009718 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009719 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009720
9721 ret = btrfs_update_inode(trans, root, inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009722out:
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009723 unlock_new_inode(inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009724 if (ret)
9725 inode_dec_link_count(inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009726 iput(inode);
9727
Filipe Mananac9901612016-05-05 01:41:57 +01009728 return ret;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009729}
9730
Chris Mason39279cc2007-06-12 06:35:45 -04009731static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009732 struct inode *new_dir, struct dentry *new_dentry,
9733 unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04009734{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009735 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009736 struct btrfs_trans_handle *trans;
Filipe Manana5062af32016-05-05 10:26:26 +01009737 unsigned int trans_num_items;
Chris Mason39279cc2007-06-12 06:35:45 -04009738 struct btrfs_root *root = BTRFS_I(old_dir)->root;
9739 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00009740 struct inode *new_inode = d_inode(new_dentry);
9741 struct inode *old_inode = d_inode(old_dentry);
Chris Mason00e4e6b2008-08-05 11:18:09 -04009742 u64 index = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009743 u64 root_objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04009744 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009745 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009746 bool log_pinned = false;
Filipe Mananad4682ba2018-06-11 19:24:28 +01009747 struct btrfs_log_ctx ctx;
9748 bool sync_log = false;
9749 bool commit_transaction = false;
Chris Mason39279cc2007-06-12 06:35:45 -04009750
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009751 if (btrfs_ino(BTRFS_I(new_dir)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04009752 return -EPERM;
9753
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009754 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08009755 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05009756 return -EXDEV;
9757
Li Zefan33345d012011-04-20 10:31:50 +08009758 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009759 (new_inode && btrfs_ino(BTRFS_I(new_inode)) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009760 return -ENOTEMPTY;
9761
Chris Mason39279cc2007-06-12 06:35:45 -04009762 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009763 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04009764 return -ENOTEMPTY;
Chris Mason9c520572012-12-17 14:26:57 -05009765
9766
9767 /* check for collisions, even if the name isn't there */
Josef Bacik4871c152013-10-09 12:24:04 -04009768 ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino,
Chris Mason9c520572012-12-17 14:26:57 -05009769 new_dentry->d_name.name,
9770 new_dentry->d_name.len);
9771
9772 if (ret) {
9773 if (ret == -EEXIST) {
9774 /* we shouldn't get
9775 * eexist without a new_inode */
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05309776 if (WARN_ON(!new_inode)) {
Chris Mason9c520572012-12-17 14:26:57 -05009777 return ret;
9778 }
9779 } else {
9780 /* maybe -EOVERFLOW */
9781 return ret;
9782 }
9783 }
9784 ret = 0;
9785
Chris Mason5a3f23d2009-03-31 13:27:11 -04009786 /*
Chris Mason8d875f92014-08-12 10:47:42 -07009787 * we're using rename to replace one file with another. Start IO on it
9788 * now so we don't add too much work to the end of the transaction
Chris Mason5a3f23d2009-03-31 13:27:11 -04009789 */
Chris Mason8d875f92014-08-12 10:47:42 -07009790 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size)
Chris Mason5a3f23d2009-03-31 13:27:11 -04009791 filemap_flush(old_inode->i_mapping);
9792
Yan, Zheng76dda932009-09-21 16:00:26 -04009793 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08009794 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009795 down_read(&fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04009796 /*
9797 * We want to reserve the absolute worst case amount of items. So if
9798 * both inodes are subvols and we need to unlink them then that would
9799 * require 4 item modifications, but if they are both normal inodes it
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009800 * would require 5 item modifications, so we'll assume they are normal
Yan, Zhenga22285a2010-05-16 10:48:46 -04009801 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
9802 * should cover the worst case number of items we'll modify.
Filipe Manana5062af32016-05-05 10:26:26 +01009803 * If our rename has the whiteout flag, we need more 5 units for the
9804 * new inode (1 inode item, 1 inode ref, 2 dir items and 1 xattr item
9805 * when selinux is enabled).
Yan, Zhenga22285a2010-05-16 10:48:46 -04009806 */
Filipe Manana5062af32016-05-05 10:26:26 +01009807 trans_num_items = 11;
9808 if (flags & RENAME_WHITEOUT)
9809 trans_num_items += 5;
9810 trans = btrfs_start_transaction(root, trans_num_items);
Johann Lombardib44c59a2011-03-31 13:23:47 +00009811 if (IS_ERR(trans)) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009812 ret = PTR_ERR(trans);
9813 goto out_notrans;
9814 }
Chris Mason5f39d392007-10-15 16:14:19 -04009815
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009816 if (dest != root)
9817 btrfs_record_root_in_trans(trans, dest);
9818
Nikolay Borisov877574e2017-02-20 13:50:33 +02009819 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009820 if (ret)
9821 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009822
Miao Xie67de1172013-12-26 13:07:06 +08009823 BTRFS_I(old_inode)->dir_index = 0ULL;
Li Zefan33345d012011-04-20 10:31:50 +08009824 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009825 /* force full log commit if subvolume involved. */
David Sterba90787762019-03-20 13:28:05 +01009826 btrfs_set_log_full_commit(trans);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009827 } else {
Filipe Mananac4aba952016-04-29 13:14:42 +01009828 btrfs_pin_log_trans(root);
9829 log_pinned = true;
Yan, Zhenga5719522009-09-24 09:17:31 -04009830 ret = btrfs_insert_inode_ref(trans, dest,
9831 new_dentry->d_name.name,
9832 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08009833 old_ino,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009834 btrfs_ino(BTRFS_I(new_dir)), index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009835 if (ret)
9836 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009837 }
Chris Mason5a3f23d2009-03-31 13:27:11 -04009838
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009839 inode_inc_iversion(old_dir);
9840 inode_inc_iversion(new_dir);
9841 inode_inc_iversion(old_inode);
Deepa Dinamani04b285f2016-02-06 23:57:21 -08009842 old_dir->i_ctime = old_dir->i_mtime =
9843 new_dir->i_ctime = new_dir->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009844 old_inode->i_ctime = current_time(old_dir);
Chris Mason5f39d392007-10-15 16:14:19 -04009845
Chris Mason12fcfd22009-03-24 10:24:20 -04009846 if (old_dentry->d_parent != new_dentry->d_parent)
David Sterbaf85b7372017-01-20 14:54:07 +01009847 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
9848 BTRFS_I(old_inode), 1);
Chris Mason12fcfd22009-03-24 10:24:20 -04009849
Li Zefan33345d012011-04-20 10:31:50 +08009850 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009851 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
Lu Fengqi401b3b12018-08-01 11:32:30 +08009852 ret = btrfs_unlink_subvol(trans, old_dir, root_objectid,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009853 old_dentry->d_name.name,
9854 old_dentry->d_name.len);
9855 } else {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009856 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
9857 BTRFS_I(d_inode(old_dentry)),
Al Viro92986792011-03-04 17:14:37 +00009858 old_dentry->d_name.name,
9859 old_dentry->d_name.len);
9860 if (!ret)
9861 ret = btrfs_update_inode(trans, root, old_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009862 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009863 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009864 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009865 goto out_fail;
9866 }
Chris Mason39279cc2007-06-12 06:35:45 -04009867
9868 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009869 inode_inc_iversion(new_inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009870 new_inode->i_ctime = current_time(new_inode);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009871 if (unlikely(btrfs_ino(BTRFS_I(new_inode)) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009872 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
9873 root_objectid = BTRFS_I(new_inode)->location.objectid;
Lu Fengqi401b3b12018-08-01 11:32:30 +08009874 ret = btrfs_unlink_subvol(trans, new_dir, root_objectid,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009875 new_dentry->d_name.name,
9876 new_dentry->d_name.len);
9877 BUG_ON(new_inode->i_nlink == 0);
9878 } else {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009879 ret = btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
9880 BTRFS_I(d_inode(new_dentry)),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009881 new_dentry->d_name.name,
9882 new_dentry->d_name.len);
9883 }
Josef Bacik4ef31a42013-08-13 14:10:08 -04009884 if (!ret && new_inode->i_nlink == 0)
Nikolay Borisov73f2e542017-02-20 13:50:59 +02009885 ret = btrfs_orphan_add(trans,
9886 BTRFS_I(d_inode(new_dentry)));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009887 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009888 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009889 goto out_fail;
9890 }
Chris Mason39279cc2007-06-12 06:35:45 -04009891 }
Josef Bacikaec74772008-07-24 12:12:38 -04009892
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009893 ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009894 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04009895 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009896 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009897 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009898 goto out_fail;
9899 }
Chris Mason39279cc2007-06-12 06:35:45 -04009900
Miao Xie67de1172013-12-26 13:07:06 +08009901 if (old_inode->i_nlink == 1)
9902 BTRFS_I(old_inode)->dir_index = index;
9903
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009904 if (log_pinned) {
Al Viro10d9f302011-07-16 23:09:10 -04009905 struct dentry *parent = new_dentry->d_parent;
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009906
Filipe Mananad4682ba2018-06-11 19:24:28 +01009907 btrfs_init_log_ctx(&ctx, old_inode);
9908 ret = btrfs_log_new_name(trans, BTRFS_I(old_inode),
9909 BTRFS_I(old_dir), parent,
9910 false, &ctx);
9911 if (ret == BTRFS_NEED_LOG_SYNC)
9912 sync_log = true;
9913 else if (ret == BTRFS_NEED_TRANS_COMMIT)
9914 commit_transaction = true;
9915 ret = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009916 btrfs_end_log_trans(root);
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009917 log_pinned = false;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009918 }
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009919
9920 if (flags & RENAME_WHITEOUT) {
9921 ret = btrfs_whiteout_for_rename(trans, root, old_dir,
9922 old_dentry);
9923
9924 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009925 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009926 goto out_fail;
9927 }
Chris Mason12fcfd22009-03-24 10:24:20 -04009928 }
Chris Mason39279cc2007-06-12 06:35:45 -04009929out_fail:
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009930 /*
9931 * If we have pinned the log and an error happened, we unpin tasks
9932 * trying to sync the log and force them to fallback to a transaction
9933 * commit if the log currently contains any of the inodes involved in
9934 * this rename operation (to ensure we do not persist a log with an
9935 * inconsistent state for any of these inodes or leading to any
9936 * inconsistencies when replayed). If the transaction was aborted, the
9937 * abortion reason is propagated to userspace when attempting to commit
9938 * the transaction. If the log does not contain any of these inodes, we
9939 * allow the tasks to sync it.
9940 */
9941 if (ret && log_pinned) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009942 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
9943 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
9944 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009945 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009946 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
David Sterba90787762019-03-20 13:28:05 +01009947 btrfs_set_log_full_commit(trans);
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009948
9949 btrfs_end_log_trans(root);
9950 log_pinned = false;
9951 }
Filipe Mananad4682ba2018-06-11 19:24:28 +01009952 if (!ret && sync_log) {
9953 ret = btrfs_sync_log(trans, BTRFS_I(old_inode)->root, &ctx);
9954 if (ret)
9955 commit_transaction = true;
9956 }
9957 if (commit_transaction) {
9958 ret = btrfs_commit_transaction(trans);
9959 } else {
9960 int ret2;
9961
9962 ret2 = btrfs_end_transaction(trans);
9963 ret = ret ? ret : ret2;
9964 }
Johann Lombardib44c59a2011-03-31 13:23:47 +00009965out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +08009966 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009967 up_read(&fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009968
Chris Mason39279cc2007-06-12 06:35:45 -04009969 return ret;
9970}
9971
Miklos Szeredi80ace852014-07-23 15:15:32 +02009972static int btrfs_rename2(struct inode *old_dir, struct dentry *old_dentry,
9973 struct inode *new_dir, struct dentry *new_dentry,
9974 unsigned int flags)
9975{
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009976 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
Miklos Szeredi80ace852014-07-23 15:15:32 +02009977 return -EINVAL;
9978
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009979 if (flags & RENAME_EXCHANGE)
9980 return btrfs_rename_exchange(old_dir, old_dentry, new_dir,
9981 new_dentry);
9982
9983 return btrfs_rename(old_dir, old_dentry, new_dir, new_dentry, flags);
Miklos Szeredi80ace852014-07-23 15:15:32 +02009984}
9985
Nikolay Borisov3a2f8c02018-04-24 17:23:59 +03009986struct btrfs_delalloc_work {
9987 struct inode *inode;
9988 struct completion completion;
9989 struct list_head list;
9990 struct btrfs_work work;
9991};
9992
Miao Xie8ccf6f192012-10-25 09:28:04 +00009993static void btrfs_run_delalloc_work(struct btrfs_work *work)
9994{
9995 struct btrfs_delalloc_work *delalloc_work;
Josef Bacik9f23e282013-10-28 15:03:41 -04009996 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009997
9998 delalloc_work = container_of(work, struct btrfs_delalloc_work,
9999 work);
Josef Bacik9f23e282013-10-28 15:03:41 -040010000 inode = delalloc_work->inode;
David Sterba30424602015-11-27 19:27:11 +010010001 filemap_flush(inode->i_mapping);
10002 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
10003 &BTRFS_I(inode)->runtime_flags))
Josef Bacik9f23e282013-10-28 15:03:41 -040010004 filemap_flush(inode->i_mapping);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010005
Nikolay Borisov076da912018-04-23 10:54:16 +030010006 iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010007 complete(&delalloc_work->completion);
10008}
10009
Nikolay Borisov3a2f8c02018-04-24 17:23:59 +030010010static struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode)
Miao Xie8ccf6f192012-10-25 09:28:04 +000010011{
10012 struct btrfs_delalloc_work *work;
10013
David Sterba100d5702015-12-08 14:39:32 +010010014 work = kmalloc(sizeof(*work), GFP_NOFS);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010015 if (!work)
10016 return NULL;
10017
10018 init_completion(&work->completion);
10019 INIT_LIST_HEAD(&work->list);
10020 work->inode = inode;
Liu Bo9e0af232014-08-15 23:36:53 +080010021 btrfs_init_work(&work->work, btrfs_flush_delalloc_helper,
10022 btrfs_run_delalloc_work, NULL, NULL);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010023
10024 return work;
10025}
10026
Chris Masond352ac62008-09-29 15:18:18 -040010027/*
10028 * some fairly slow code that needs optimization. This walks the list
10029 * of all the inodes with pending delalloc and forces them to disk.
10030 */
Ethan Lien3cd24c62018-11-01 14:49:03 +080010031static int start_delalloc_inodes(struct btrfs_root *root, int nr, bool snapshot)
Chris Masonea8c2812008-08-04 23:17:27 -040010032{
Chris Masonea8c2812008-08-04 23:17:27 -040010033 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010034 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010035 struct btrfs_delalloc_work *work, *next;
10036 struct list_head works;
Miao Xie1eafa6c2013-01-22 10:49:00 +000010037 struct list_head splice;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010038 int ret = 0;
Chris Masonea8c2812008-08-04 23:17:27 -040010039
Miao Xie8ccf6f192012-10-25 09:28:04 +000010040 INIT_LIST_HEAD(&works);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010041 INIT_LIST_HEAD(&splice);
Miao Xie63607cc2013-01-22 10:50:35 +000010042
Miao Xie573bfb72014-03-06 13:55:03 +080010043 mutex_lock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010044 spin_lock(&root->delalloc_lock);
10045 list_splice_init(&root->delalloc_inodes, &splice);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010046 while (!list_empty(&splice)) {
10047 binode = list_entry(splice.next, struct btrfs_inode,
Chris Masonea8c2812008-08-04 23:17:27 -040010048 delalloc_inodes);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010049
Miao Xieeb73c1b2013-05-15 07:48:22 +000010050 list_move_tail(&binode->delalloc_inodes,
10051 &root->delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010052 inode = igrab(&binode->vfs_inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +000010053 if (!inode) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010054 cond_resched_lock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010055 continue;
Miao Xiedf0af1a2013-01-29 10:11:59 +000010056 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010057 spin_unlock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010058
Ethan Lien3cd24c62018-11-01 14:49:03 +080010059 if (snapshot)
10060 set_bit(BTRFS_INODE_SNAPSHOT_FLUSH,
10061 &binode->runtime_flags);
Nikolay Borisov076da912018-04-23 10:54:16 +030010062 work = btrfs_alloc_delalloc_work(inode);
David Sterba5d99a9982014-09-29 19:20:37 +020010063 if (!work) {
Nikolay Borisov4fbb5142018-04-23 10:54:15 +030010064 iput(inode);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010065 ret = -ENOMEM;
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010066 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010067 }
Miao Xie1eafa6c2013-01-22 10:49:00 +000010068 list_add_tail(&work->list, &works);
Qu Wenruoa44903a2014-02-28 10:46:09 +080010069 btrfs_queue_work(root->fs_info->flush_workers,
10070 &work->work);
Miao Xie6c255e62014-03-06 13:55:01 +080010071 ret++;
10072 if (nr != -1 && ret >= nr)
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010073 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010074 cond_resched();
Miao Xieeb73c1b2013-05-15 07:48:22 +000010075 spin_lock(&root->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -040010076 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010077 spin_unlock(&root->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -040010078
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010079out:
Miao Xie1eafa6c2013-01-22 10:49:00 +000010080 list_for_each_entry_safe(work, next, &works, list) {
10081 list_del_init(&work->list);
Nikolay Borisov40012f92018-04-19 10:46:39 +030010082 wait_for_completion(&work->completion);
10083 kfree(work);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010084 }
10085
Nikolay Borisov81f1d392018-04-19 10:46:37 +030010086 if (!list_empty(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010087 spin_lock(&root->delalloc_lock);
10088 list_splice_tail(&splice, &root->delalloc_inodes);
10089 spin_unlock(&root->delalloc_lock);
10090 }
Miao Xie573bfb72014-03-06 13:55:03 +080010091 mutex_unlock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010092 return ret;
10093}
10094
Ethan Lien3cd24c62018-11-01 14:49:03 +080010095int btrfs_start_delalloc_snapshot(struct btrfs_root *root)
Miao Xieeb73c1b2013-05-15 07:48:22 +000010096{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010097 struct btrfs_fs_info *fs_info = root->fs_info;
Miao Xieeb73c1b2013-05-15 07:48:22 +000010098 int ret;
10099
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010100 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +000010101 return -EROFS;
10102
Ethan Lien3cd24c62018-11-01 14:49:03 +080010103 ret = start_delalloc_inodes(root, -1, true);
Miao Xie6c255e62014-03-06 13:55:01 +080010104 if (ret > 0)
10105 ret = 0;
Miao Xieeb73c1b2013-05-15 07:48:22 +000010106 return ret;
10107}
10108
Nikolay Borisov82b3e532018-04-23 10:54:13 +030010109int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, int nr)
Miao Xieeb73c1b2013-05-15 07:48:22 +000010110{
10111 struct btrfs_root *root;
10112 struct list_head splice;
10113 int ret;
10114
Wang Shilong2c21b4d2014-01-14 19:42:20 +080010115 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +000010116 return -EROFS;
10117
10118 INIT_LIST_HEAD(&splice);
10119
Miao Xie573bfb72014-03-06 13:55:03 +080010120 mutex_lock(&fs_info->delalloc_root_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010121 spin_lock(&fs_info->delalloc_root_lock);
10122 list_splice_init(&fs_info->delalloc_roots, &splice);
Miao Xie6c255e62014-03-06 13:55:01 +080010123 while (!list_empty(&splice) && nr) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010124 root = list_first_entry(&splice, struct btrfs_root,
10125 delalloc_root);
10126 root = btrfs_grab_fs_root(root);
10127 BUG_ON(!root);
10128 list_move_tail(&root->delalloc_root,
10129 &fs_info->delalloc_roots);
10130 spin_unlock(&fs_info->delalloc_root_lock);
10131
Ethan Lien3cd24c62018-11-01 14:49:03 +080010132 ret = start_delalloc_inodes(root, nr, false);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010133 btrfs_put_fs_root(root);
Miao Xie6c255e62014-03-06 13:55:01 +080010134 if (ret < 0)
Miao Xieeb73c1b2013-05-15 07:48:22 +000010135 goto out;
10136
Miao Xie6c255e62014-03-06 13:55:01 +080010137 if (nr != -1) {
10138 nr -= ret;
10139 WARN_ON(nr < 0);
10140 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010141 spin_lock(&fs_info->delalloc_root_lock);
10142 }
10143 spin_unlock(&fs_info->delalloc_root_lock);
10144
Miao Xie6c255e62014-03-06 13:55:01 +080010145 ret = 0;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010146out:
Nikolay Borisov81f1d392018-04-19 10:46:37 +030010147 if (!list_empty(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010148 spin_lock(&fs_info->delalloc_root_lock);
10149 list_splice_tail(&splice, &fs_info->delalloc_roots);
10150 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010151 }
Miao Xie573bfb72014-03-06 13:55:03 +080010152 mutex_unlock(&fs_info->delalloc_root_mutex);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010153 return ret;
Chris Masonea8c2812008-08-04 23:17:27 -040010154}
10155
Chris Mason39279cc2007-06-12 06:35:45 -040010156static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
10157 const char *symname)
10158{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010159 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -040010160 struct btrfs_trans_handle *trans;
10161 struct btrfs_root *root = BTRFS_I(dir)->root;
10162 struct btrfs_path *path;
10163 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -050010164 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -040010165 int err;
Chris Mason39279cc2007-06-12 06:35:45 -040010166 u64 objectid;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +053010167 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -040010168 int name_len;
10169 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -040010170 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -040010171 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -040010172 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -040010173
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +010010174 name_len = strlen(symname);
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010175 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info))
Chris Mason39279cc2007-06-12 06:35:45 -040010176 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -050010177
Josef Bacik9ed74f22009-09-11 16:12:44 -040010178 /*
10179 * 2 items for inode item and ref
10180 * 2 items for dir items
Filipe Manana9269d122015-12-31 18:16:29 +000010181 * 1 item for updating parent inode item
10182 * 1 item for the inline extent item
Josef Bacik9ed74f22009-09-11 16:12:44 -040010183 * 1 item for xattr if selinux is on
10184 */
Filipe Manana9269d122015-12-31 18:16:29 +000010185 trans = btrfs_start_transaction(root, 7);
Yan, Zhenga22285a2010-05-16 10:48:46 -040010186 if (IS_ERR(trans))
10187 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -050010188
Li Zefan581bb052011-04-20 10:06:11 +080010189 err = btrfs_find_free_ino(root, &objectid);
10190 if (err)
10191 goto out_unlock;
10192
Josef Bacikaec74772008-07-24 12:12:38 -040010193 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +010010194 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)),
10195 objectid, S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -040010196 if (IS_ERR(inode)) {
10197 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -040010198 inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -040010199 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -040010200 }
Chris Mason39279cc2007-06-12 06:35:45 -040010201
Casey Schauflerad19db72011-12-15 10:09:07 -050010202 /*
10203 * If the active LSM wants to access the inode during
10204 * d_instantiate it needs these. Smack checks to see
10205 * if the filesystem supports xattrs by looking at the
10206 * ops vector.
10207 */
10208 inode->i_fop = &btrfs_file_operations;
10209 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -070010210 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -070010211 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
10212
10213 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
10214 if (err)
Al Viro32955c52018-05-16 12:20:05 -040010215 goto out_unlock;
Casey Schauflerad19db72011-12-15 10:09:07 -050010216
Chris Mason39279cc2007-06-12 06:35:45 -040010217 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -070010218 if (!path) {
10219 err = -ENOMEM;
Al Viro32955c52018-05-16 12:20:05 -040010220 goto out_unlock;
Mark Fashehd8926bb2011-07-13 10:38:47 -070010221 }
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +020010222 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason39279cc2007-06-12 06:35:45 -040010223 key.offset = 0;
David Sterba962a2982014-06-04 18:41:45 +020010224 key.type = BTRFS_EXTENT_DATA_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -040010225 datasize = btrfs_file_extent_calc_inline_size(name_len);
10226 err = btrfs_insert_empty_item(trans, root, path, &key,
10227 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -040010228 if (err) {
Julia Lawallb0839162011-05-14 07:10:51 +000010229 btrfs_free_path(path);
Al Viro32955c52018-05-16 12:20:05 -040010230 goto out_unlock;
Chris Mason54aa1f42007-06-22 14:16:25 -040010231 }
Chris Mason5f39d392007-10-15 16:14:19 -040010232 leaf = path->nodes[0];
10233 ei = btrfs_item_ptr(leaf, path->slots[0],
10234 struct btrfs_file_extent_item);
10235 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
10236 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -040010237 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -040010238 btrfs_set_file_extent_encryption(leaf, ei, 0);
10239 btrfs_set_file_extent_compression(leaf, ei, 0);
10240 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
10241 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
10242
Chris Mason39279cc2007-06-12 06:35:45 -040010243 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -040010244 write_extent_buffer(leaf, symname, ptr, name_len);
10245 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -040010246 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -040010247
Chris Mason39279cc2007-06-12 06:35:45 -040010248 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -050010249 inode_nohighmem(inode);
Yan Zhengd899e052008-10-30 14:25:28 -040010250 inode_set_bytes(inode, name_len);
Nikolay Borisov6ef06d22017-02-20 13:50:34 +020010251 btrfs_i_size_write(BTRFS_I(inode), name_len);
Chris Mason54aa1f42007-06-22 14:16:25 -040010252 err = btrfs_update_inode(trans, root, inode);
Filipe Mananad50866d2015-12-31 18:08:24 +000010253 /*
10254 * Last step, add directory indexes for our symlink inode. This is the
10255 * last step to avoid extra cleanup of these indexes if an error happens
10256 * elsewhere above.
10257 */
10258 if (!err)
Nikolay Borisovcef415a2017-02-20 13:51:09 +020010259 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry,
10260 BTRFS_I(inode), 0, index);
Al Viro32955c52018-05-16 12:20:05 -040010261 if (err)
10262 goto out_unlock;
Chris Masonb0d5d102014-09-08 13:08:51 -070010263
Al Viro1e2e5472018-05-04 08:23:01 -040010264 d_instantiate_new(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010265
10266out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010267 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -040010268 if (err && inode) {
Chris Mason39279cc2007-06-12 06:35:45 -040010269 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -040010270 discard_new_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010271 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010272 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -040010273 return err;
10274}
Chris Mason16432982008-04-10 10:23:21 -040010275
Josef Bacik0af3d002010-06-21 14:48:16 -040010276static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
10277 u64 start, u64 num_bytes, u64 min_size,
10278 loff_t actual_len, u64 *alloc_hint,
10279 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -040010280{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010281 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik5dc562c2012-08-17 13:14:17 -040010282 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
10283 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -040010284 struct btrfs_root *root = BTRFS_I(inode)->root;
10285 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -040010286 u64 cur_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +000010287 u64 i_size;
Chris Mason154ea282013-03-05 11:11:26 -050010288 u64 cur_bytes;
Josef Bacik0b670dc2015-09-23 17:11:16 -040010289 u64 last_alloc = (u64)-1;
Yan Zhengd899e052008-10-30 14:25:28 -040010290 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -040010291 bool own_trans = true;
Wang Xiaoguang18513092016-07-25 15:51:40 +080010292 u64 end = start + num_bytes - 1;
Yan Zhengd899e052008-10-30 14:25:28 -040010293
Josef Bacik0af3d002010-06-21 14:48:16 -040010294 if (trans)
10295 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -040010296 while (num_bytes > 0) {
Josef Bacik0af3d002010-06-21 14:48:16 -040010297 if (own_trans) {
10298 trans = btrfs_start_transaction(root, 3);
10299 if (IS_ERR(trans)) {
10300 ret = PTR_ERR(trans);
10301 break;
10302 }
Yan Zhengd899e052008-10-30 14:25:28 -040010303 }
Yan, Zheng5a303d52009-11-12 09:34:52 +000010304
Byongho Leeee221842015-12-15 01:42:10 +090010305 cur_bytes = min_t(u64, num_bytes, SZ_256M);
Chris Mason154ea282013-03-05 11:11:26 -050010306 cur_bytes = max(cur_bytes, min_size);
Josef Bacik0b670dc2015-09-23 17:11:16 -040010307 /*
10308 * If we are severely fragmented we could end up with really
10309 * small allocations, so if the allocator is returning small
10310 * chunks lets make its job easier by only searching for those
10311 * sized chunks.
10312 */
10313 cur_bytes = min(cur_bytes, last_alloc);
Wang Xiaoguang18513092016-07-25 15:51:40 +080010314 ret = btrfs_reserve_extent(root, cur_bytes, cur_bytes,
10315 min_size, 0, *alloc_hint, &ins, 1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010316 if (ret) {
Josef Bacik0af3d002010-06-21 14:48:16 -040010317 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010318 btrfs_end_transaction(trans);
Yan, Zhenga22285a2010-05-16 10:48:46 -040010319 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +000010320 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010321 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010322
Josef Bacik0b670dc2015-09-23 17:11:16 -040010323 last_alloc = ins.offset;
Yan Zhengd899e052008-10-30 14:25:28 -040010324 ret = insert_reserved_file_extent(trans, inode,
10325 cur_offset, ins.objectid,
10326 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +000010327 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -040010328 BTRFS_FILE_EXTENT_PREALLOC);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010329 if (ret) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010330 btrfs_free_reserved_extent(fs_info, ins.objectid,
Miao Xiee570fd22014-06-19 10:42:50 +080010331 ins.offset, 0);
Jeff Mahoney66642832016-06-10 18:19:25 -040010332 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010333 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010334 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010335 break;
10336 }
Dongsheng Yang31193212014-12-12 16:44:35 +080010337
Nikolay Borisovdcdbc052017-02-20 13:50:45 +020010338 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Chris Masona1ed8352009-09-11 12:27:37 -040010339 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010340
Josef Bacik5dc562c2012-08-17 13:14:17 -040010341 em = alloc_extent_map();
10342 if (!em) {
10343 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
10344 &BTRFS_I(inode)->runtime_flags);
10345 goto next;
10346 }
10347
10348 em->start = cur_offset;
10349 em->orig_start = cur_offset;
10350 em->len = ins.offset;
10351 em->block_start = ins.objectid;
10352 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -050010353 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -040010354 em->ram_bytes = ins.offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010355 em->bdev = fs_info->fs_devices->latest_bdev;
Josef Bacik5dc562c2012-08-17 13:14:17 -040010356 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
10357 em->generation = trans->transid;
10358
10359 while (1) {
10360 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -040010361 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -040010362 write_unlock(&em_tree->lock);
10363 if (ret != -EEXIST)
10364 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +020010365 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -040010366 cur_offset + ins.offset - 1,
10367 0);
10368 }
10369 free_extent_map(em);
10370next:
Yan Zhengd899e052008-10-30 14:25:28 -040010371 num_bytes -= ins.offset;
10372 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -040010373 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +000010374
Josef Bacik0c4d2d92012-04-05 15:03:02 -040010375 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -070010376 inode->i_ctime = current_time(inode);
Christoph Hellwig6cbff002009-04-17 10:37:41 +020010377 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -040010378 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -040010379 (actual_len > inode->i_size) &&
10380 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +000010381 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +000010382 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +000010383 else
Josef Bacik55a61d12010-11-22 18:50:32 +000010384 i_size = cur_offset;
10385 i_size_write(inode, i_size);
10386 btrfs_ordered_update_i_size(inode, i_size, NULL);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010387 }
10388
Yan Zhengd899e052008-10-30 14:25:28 -040010389 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010390
10391 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -040010392 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010393 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010394 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010395 break;
10396 }
Yan Zhengd899e052008-10-30 14:25:28 -040010397
Josef Bacik0af3d002010-06-21 14:48:16 -040010398 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010399 btrfs_end_transaction(trans);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010400 }
Wang Xiaoguang18513092016-07-25 15:51:40 +080010401 if (cur_offset < end)
Qu Wenruobc42bda2017-02-27 15:10:39 +080010402 btrfs_free_reserved_data_space(inode, NULL, cur_offset,
Wang Xiaoguang18513092016-07-25 15:51:40 +080010403 end - cur_offset + 1);
Yan Zhengd899e052008-10-30 14:25:28 -040010404 return ret;
10405}
10406
Josef Bacik0af3d002010-06-21 14:48:16 -040010407int btrfs_prealloc_file_range(struct inode *inode, int mode,
10408 u64 start, u64 num_bytes, u64 min_size,
10409 loff_t actual_len, u64 *alloc_hint)
10410{
10411 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
10412 min_size, actual_len, alloc_hint,
10413 NULL);
10414}
10415
10416int btrfs_prealloc_file_range_trans(struct inode *inode,
10417 struct btrfs_trans_handle *trans, int mode,
10418 u64 start, u64 num_bytes, u64 min_size,
10419 loff_t actual_len, u64 *alloc_hint)
10420{
10421 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
10422 min_size, actual_len, alloc_hint, trans);
10423}
10424
Chris Masone6dcd2d2008-07-17 12:53:50 -040010425static int btrfs_set_page_dirty(struct page *page)
10426{
Chris Masone6dcd2d2008-07-17 12:53:50 -040010427 return __set_page_dirty_nobuffers(page);
10428}
10429
Al Viro10556cb22011-06-20 19:28:19 -040010430static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -050010431{
Li Zefanb83cc962010-12-20 16:04:08 +080010432 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +000010433 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +080010434
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +000010435 if (mask & MAY_WRITE &&
10436 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
10437 if (btrfs_root_readonly(root))
10438 return -EROFS;
10439 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
10440 return -EACCES;
10441 }
Al Viro2830ba72011-06-20 19:16:29 -040010442 return generic_permission(inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -050010443}
Chris Mason39279cc2007-06-12 06:35:45 -040010444
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010445static int btrfs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
10446{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010447 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010448 struct btrfs_trans_handle *trans;
10449 struct btrfs_root *root = BTRFS_I(dir)->root;
10450 struct inode *inode = NULL;
10451 u64 objectid;
10452 u64 index;
10453 int ret = 0;
10454
10455 /*
10456 * 5 units required for adding orphan entry
10457 */
10458 trans = btrfs_start_transaction(root, 5);
10459 if (IS_ERR(trans))
10460 return PTR_ERR(trans);
10461
10462 ret = btrfs_find_free_ino(root, &objectid);
10463 if (ret)
10464 goto out;
10465
10466 inode = btrfs_new_inode(trans, root, dir, NULL, 0,
David Sterbaf85b7372017-01-20 14:54:07 +010010467 btrfs_ino(BTRFS_I(dir)), objectid, mode, &index);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010468 if (IS_ERR(inode)) {
10469 ret = PTR_ERR(inode);
10470 inode = NULL;
10471 goto out;
10472 }
10473
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010474 inode->i_fop = &btrfs_file_operations;
10475 inode->i_op = &btrfs_file_inode_operations;
10476
10477 inode->i_mapping->a_ops = &btrfs_aops;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010478 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
10479
Chris Masonb0d5d102014-09-08 13:08:51 -070010480 ret = btrfs_init_inode_security(trans, inode, dir, NULL);
10481 if (ret)
Al Viro32955c52018-05-16 12:20:05 -040010482 goto out;
Chris Masonb0d5d102014-09-08 13:08:51 -070010483
10484 ret = btrfs_update_inode(trans, root, inode);
10485 if (ret)
Al Viro32955c52018-05-16 12:20:05 -040010486 goto out;
Nikolay Borisov73f2e542017-02-20 13:50:59 +020010487 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010488 if (ret)
Al Viro32955c52018-05-16 12:20:05 -040010489 goto out;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010490
Filipe Manana5762b5c2014-08-01 00:10:32 +010010491 /*
10492 * We set number of links to 0 in btrfs_new_inode(), and here we set
10493 * it to 1 because d_tmpfile() will issue a warning if the count is 0,
10494 * through:
10495 *
10496 * d_tmpfile() -> inode_dec_link_count() -> drop_nlink()
10497 */
10498 set_nlink(inode, 1);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010499 d_tmpfile(dentry, inode);
Al Viro32955c52018-05-16 12:20:05 -040010500 unlock_new_inode(inode);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010501 mark_inode_dirty(inode);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010502out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010503 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -040010504 if (ret && inode)
10505 discard_new_inode(inode);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010506 btrfs_btree_balance_dirty(fs_info);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010507 return ret;
10508}
10509
David Sterba5cdc84b2018-07-18 20:32:52 +020010510void btrfs_set_range_writeback(struct extent_io_tree *tree, u64 start, u64 end)
Josef Bacikc6100a42017-05-05 11:57:13 -040010511{
David Sterba5cdc84b2018-07-18 20:32:52 +020010512 struct inode *inode = tree->private_data;
Josef Bacikc6100a42017-05-05 11:57:13 -040010513 unsigned long index = start >> PAGE_SHIFT;
10514 unsigned long end_index = end >> PAGE_SHIFT;
10515 struct page *page;
10516
10517 while (index <= end_index) {
10518 page = find_get_page(inode->i_mapping, index);
10519 ASSERT(page); /* Pages should be in the extent_io_tree */
10520 set_page_writeback(page);
10521 put_page(page);
10522 index++;
10523 }
10524}
10525
Omar Sandovaled46ff32016-11-03 10:28:14 -070010526#ifdef CONFIG_SWAP
10527/*
10528 * Add an entry indicating a block group or device which is pinned by a
10529 * swapfile. Returns 0 on success, 1 if there is already an entry for it, or a
10530 * negative errno on failure.
10531 */
10532static int btrfs_add_swapfile_pin(struct inode *inode, void *ptr,
10533 bool is_block_group)
10534{
10535 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
10536 struct btrfs_swapfile_pin *sp, *entry;
10537 struct rb_node **p;
10538 struct rb_node *parent = NULL;
10539
10540 sp = kmalloc(sizeof(*sp), GFP_NOFS);
10541 if (!sp)
10542 return -ENOMEM;
10543 sp->ptr = ptr;
10544 sp->inode = inode;
10545 sp->is_block_group = is_block_group;
10546
10547 spin_lock(&fs_info->swapfile_pins_lock);
10548 p = &fs_info->swapfile_pins.rb_node;
10549 while (*p) {
10550 parent = *p;
10551 entry = rb_entry(parent, struct btrfs_swapfile_pin, node);
10552 if (sp->ptr < entry->ptr ||
10553 (sp->ptr == entry->ptr && sp->inode < entry->inode)) {
10554 p = &(*p)->rb_left;
10555 } else if (sp->ptr > entry->ptr ||
10556 (sp->ptr == entry->ptr && sp->inode > entry->inode)) {
10557 p = &(*p)->rb_right;
10558 } else {
10559 spin_unlock(&fs_info->swapfile_pins_lock);
10560 kfree(sp);
10561 return 1;
10562 }
10563 }
10564 rb_link_node(&sp->node, parent, p);
10565 rb_insert_color(&sp->node, &fs_info->swapfile_pins);
10566 spin_unlock(&fs_info->swapfile_pins_lock);
10567 return 0;
10568}
10569
10570/* Free all of the entries pinned by this swapfile. */
10571static void btrfs_free_swapfile_pins(struct inode *inode)
10572{
10573 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
10574 struct btrfs_swapfile_pin *sp;
10575 struct rb_node *node, *next;
10576
10577 spin_lock(&fs_info->swapfile_pins_lock);
10578 node = rb_first(&fs_info->swapfile_pins);
10579 while (node) {
10580 next = rb_next(node);
10581 sp = rb_entry(node, struct btrfs_swapfile_pin, node);
10582 if (sp->inode == inode) {
10583 rb_erase(&sp->node, &fs_info->swapfile_pins);
10584 if (sp->is_block_group)
10585 btrfs_put_block_group(sp->ptr);
10586 kfree(sp);
10587 }
10588 node = next;
10589 }
10590 spin_unlock(&fs_info->swapfile_pins_lock);
10591}
10592
10593struct btrfs_swap_info {
10594 u64 start;
10595 u64 block_start;
10596 u64 block_len;
10597 u64 lowest_ppage;
10598 u64 highest_ppage;
10599 unsigned long nr_pages;
10600 int nr_extents;
10601};
10602
10603static int btrfs_add_swap_extent(struct swap_info_struct *sis,
10604 struct btrfs_swap_info *bsi)
10605{
10606 unsigned long nr_pages;
10607 u64 first_ppage, first_ppage_reported, next_ppage;
10608 int ret;
10609
10610 first_ppage = ALIGN(bsi->block_start, PAGE_SIZE) >> PAGE_SHIFT;
10611 next_ppage = ALIGN_DOWN(bsi->block_start + bsi->block_len,
10612 PAGE_SIZE) >> PAGE_SHIFT;
10613
10614 if (first_ppage >= next_ppage)
10615 return 0;
10616 nr_pages = next_ppage - first_ppage;
10617
10618 first_ppage_reported = first_ppage;
10619 if (bsi->start == 0)
10620 first_ppage_reported++;
10621 if (bsi->lowest_ppage > first_ppage_reported)
10622 bsi->lowest_ppage = first_ppage_reported;
10623 if (bsi->highest_ppage < (next_ppage - 1))
10624 bsi->highest_ppage = next_ppage - 1;
10625
10626 ret = add_swap_extent(sis, bsi->nr_pages, nr_pages, first_ppage);
10627 if (ret < 0)
10628 return ret;
10629 bsi->nr_extents += ret;
10630 bsi->nr_pages += nr_pages;
10631 return 0;
10632}
10633
10634static void btrfs_swap_deactivate(struct file *file)
10635{
10636 struct inode *inode = file_inode(file);
10637
10638 btrfs_free_swapfile_pins(inode);
10639 atomic_dec(&BTRFS_I(inode)->root->nr_swapfiles);
10640}
10641
10642static int btrfs_swap_activate(struct swap_info_struct *sis, struct file *file,
10643 sector_t *span)
10644{
10645 struct inode *inode = file_inode(file);
10646 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
10647 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
10648 struct extent_state *cached_state = NULL;
10649 struct extent_map *em = NULL;
10650 struct btrfs_device *device = NULL;
10651 struct btrfs_swap_info bsi = {
10652 .lowest_ppage = (sector_t)-1ULL,
10653 };
10654 int ret = 0;
10655 u64 isize;
10656 u64 start;
10657
10658 /*
10659 * If the swap file was just created, make sure delalloc is done. If the
10660 * file changes again after this, the user is doing something stupid and
10661 * we don't really care.
10662 */
10663 ret = btrfs_wait_ordered_range(inode, 0, (u64)-1);
10664 if (ret)
10665 return ret;
10666
10667 /*
10668 * The inode is locked, so these flags won't change after we check them.
10669 */
10670 if (BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS) {
10671 btrfs_warn(fs_info, "swapfile must not be compressed");
10672 return -EINVAL;
10673 }
10674 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW)) {
10675 btrfs_warn(fs_info, "swapfile must not be copy-on-write");
10676 return -EINVAL;
10677 }
10678 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
10679 btrfs_warn(fs_info, "swapfile must not be checksummed");
10680 return -EINVAL;
10681 }
10682
10683 /*
10684 * Balance or device remove/replace/resize can move stuff around from
10685 * under us. The EXCL_OP flag makes sure they aren't running/won't run
10686 * concurrently while we are mapping the swap extents, and
10687 * fs_info->swapfile_pins prevents them from running while the swap file
10688 * is active and moving the extents. Note that this also prevents a
10689 * concurrent device add which isn't actually necessary, but it's not
10690 * really worth the trouble to allow it.
10691 */
10692 if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) {
10693 btrfs_warn(fs_info,
10694 "cannot activate swapfile while exclusive operation is running");
10695 return -EBUSY;
10696 }
10697 /*
10698 * Snapshots can create extents which require COW even if NODATACOW is
10699 * set. We use this counter to prevent snapshots. We must increment it
10700 * before walking the extents because we don't want a concurrent
10701 * snapshot to run after we've already checked the extents.
10702 */
10703 atomic_inc(&BTRFS_I(inode)->root->nr_swapfiles);
10704
10705 isize = ALIGN_DOWN(inode->i_size, fs_info->sectorsize);
10706
10707 lock_extent_bits(io_tree, 0, isize - 1, &cached_state);
10708 start = 0;
10709 while (start < isize) {
10710 u64 logical_block_start, physical_block_start;
10711 struct btrfs_block_group_cache *bg;
10712 u64 len = isize - start;
10713
10714 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len, 0);
10715 if (IS_ERR(em)) {
10716 ret = PTR_ERR(em);
10717 goto out;
10718 }
10719
10720 if (em->block_start == EXTENT_MAP_HOLE) {
10721 btrfs_warn(fs_info, "swapfile must not have holes");
10722 ret = -EINVAL;
10723 goto out;
10724 }
10725 if (em->block_start == EXTENT_MAP_INLINE) {
10726 /*
10727 * It's unlikely we'll ever actually find ourselves
10728 * here, as a file small enough to fit inline won't be
10729 * big enough to store more than the swap header, but in
10730 * case something changes in the future, let's catch it
10731 * here rather than later.
10732 */
10733 btrfs_warn(fs_info, "swapfile must not be inline");
10734 ret = -EINVAL;
10735 goto out;
10736 }
10737 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
10738 btrfs_warn(fs_info, "swapfile must not be compressed");
10739 ret = -EINVAL;
10740 goto out;
10741 }
10742
10743 logical_block_start = em->block_start + (start - em->start);
10744 len = min(len, em->len - (start - em->start));
10745 free_extent_map(em);
10746 em = NULL;
10747
10748 ret = can_nocow_extent(inode, start, &len, NULL, NULL, NULL);
10749 if (ret < 0) {
10750 goto out;
10751 } else if (ret) {
10752 ret = 0;
10753 } else {
10754 btrfs_warn(fs_info,
10755 "swapfile must not be copy-on-write");
10756 ret = -EINVAL;
10757 goto out;
10758 }
10759
10760 em = btrfs_get_chunk_map(fs_info, logical_block_start, len);
10761 if (IS_ERR(em)) {
10762 ret = PTR_ERR(em);
10763 goto out;
10764 }
10765
10766 if (em->map_lookup->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
10767 btrfs_warn(fs_info,
10768 "swapfile must have single data profile");
10769 ret = -EINVAL;
10770 goto out;
10771 }
10772
10773 if (device == NULL) {
10774 device = em->map_lookup->stripes[0].dev;
10775 ret = btrfs_add_swapfile_pin(inode, device, false);
10776 if (ret == 1)
10777 ret = 0;
10778 else if (ret)
10779 goto out;
10780 } else if (device != em->map_lookup->stripes[0].dev) {
10781 btrfs_warn(fs_info, "swapfile must be on one device");
10782 ret = -EINVAL;
10783 goto out;
10784 }
10785
10786 physical_block_start = (em->map_lookup->stripes[0].physical +
10787 (logical_block_start - em->start));
10788 len = min(len, em->len - (logical_block_start - em->start));
10789 free_extent_map(em);
10790 em = NULL;
10791
10792 bg = btrfs_lookup_block_group(fs_info, logical_block_start);
10793 if (!bg) {
10794 btrfs_warn(fs_info,
10795 "could not find block group containing swapfile");
10796 ret = -EINVAL;
10797 goto out;
10798 }
10799
10800 ret = btrfs_add_swapfile_pin(inode, bg, true);
10801 if (ret) {
10802 btrfs_put_block_group(bg);
10803 if (ret == 1)
10804 ret = 0;
10805 else
10806 goto out;
10807 }
10808
10809 if (bsi.block_len &&
10810 bsi.block_start + bsi.block_len == physical_block_start) {
10811 bsi.block_len += len;
10812 } else {
10813 if (bsi.block_len) {
10814 ret = btrfs_add_swap_extent(sis, &bsi);
10815 if (ret)
10816 goto out;
10817 }
10818 bsi.start = start;
10819 bsi.block_start = physical_block_start;
10820 bsi.block_len = len;
10821 }
10822
10823 start += len;
10824 }
10825
10826 if (bsi.block_len)
10827 ret = btrfs_add_swap_extent(sis, &bsi);
10828
10829out:
10830 if (!IS_ERR_OR_NULL(em))
10831 free_extent_map(em);
10832
10833 unlock_extent_cached(io_tree, 0, isize - 1, &cached_state);
10834
10835 if (ret)
10836 btrfs_swap_deactivate(file);
10837
10838 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
10839
10840 if (ret)
10841 return ret;
10842
10843 if (device)
10844 sis->bdev = device->bdev;
10845 *span = bsi.highest_ppage - bsi.lowest_ppage + 1;
10846 sis->max = bsi.nr_pages;
10847 sis->pages = bsi.nr_pages - 1;
10848 sis->highest_bit = bsi.nr_pages - 1;
10849 return bsi.nr_extents;
10850}
10851#else
10852static void btrfs_swap_deactivate(struct file *file)
10853{
10854}
10855
10856static int btrfs_swap_activate(struct swap_info_struct *sis, struct file *file,
10857 sector_t *span)
10858{
10859 return -EOPNOTSUPP;
10860}
10861#endif
10862
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010863static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -050010864 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -040010865 .lookup = btrfs_lookup,
10866 .create = btrfs_create,
10867 .unlink = btrfs_unlink,
10868 .link = btrfs_link,
10869 .mkdir = btrfs_mkdir,
10870 .rmdir = btrfs_rmdir,
Miklos Szeredi2773bf02016-09-27 11:03:58 +020010871 .rename = btrfs_rename2,
Chris Mason39279cc2007-06-12 06:35:45 -040010872 .symlink = btrfs_symlink,
10873 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -040010874 .mknod = btrfs_mknod,
Josef Bacik5103e942007-11-16 11:45:54 -050010875 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010876 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010877 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010878 .set_acl = btrfs_set_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070010879 .update_time = btrfs_update_time,
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010880 .tmpfile = btrfs_tmpfile,
Chris Mason39279cc2007-06-12 06:35:45 -040010881};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010882static const struct inode_operations btrfs_dir_ro_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010883 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -050010884 .permission = btrfs_permission,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070010885 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010886};
Yan, Zheng76dda932009-09-21 16:00:26 -040010887
Alexey Dobriyan828c0952009-10-01 15:43:56 -070010888static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010889 .llseek = generic_file_llseek,
10890 .read = generic_read_dir,
Omar Sandoval02dbfc92016-05-20 13:50:33 -070010891 .iterate_shared = btrfs_real_readdir,
Josef Bacik23b5ec72017-07-24 15:14:25 -040010892 .open = btrfs_opendir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -040010893 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010894#ifdef CONFIG_COMPAT
Luke Dashjr4c63c242015-10-29 08:22:21 +000010895 .compat_ioctl = btrfs_compat_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010896#endif
Sage Weil6bf13c02008-06-10 10:07:39 -040010897 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -040010898 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -040010899};
10900
David Sterba20e55062015-11-19 11:42:28 +010010901static const struct extent_io_ops btrfs_extent_io_ops = {
David Sterba4d53ddd2017-02-17 15:27:44 +010010902 /* mandatory callbacks */
Chris Mason065631f2008-02-20 12:07:25 -050010903 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -040010904 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
10905};
10906
Chris Mason35054392009-01-21 13:11:13 -050010907/*
10908 * btrfs doesn't support the bmap operation because swapfiles
10909 * use bmap to make a mapping of extents in the file. They assume
10910 * these extents won't change over the life of the file and they
10911 * use the bmap result to do IO directly to the drive.
10912 *
10913 * the btrfs bmap call would return logical addresses that aren't
10914 * suitable for IO and they also will change frequently as COW
10915 * operations happen. So, swapfile + btrfs == corruption.
10916 *
10917 * For now we're avoiding this by dropping bmap.
10918 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -070010919static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -040010920 .readpage = btrfs_readpage,
10921 .writepage = btrfs_writepage,
Chris Masonb293f02e2007-11-01 19:45:34 -040010922 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -050010923 .readpages = btrfs_readpages,
Chris Mason16432982008-04-10 10:23:21 -040010924 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -040010925 .invalidatepage = btrfs_invalidatepage,
10926 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -040010927 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +020010928 .error_remove_page = generic_error_remove_page,
Omar Sandovaled46ff32016-11-03 10:28:14 -070010929 .swap_activate = btrfs_swap_activate,
10930 .swap_deactivate = btrfs_swap_deactivate,
Chris Mason39279cc2007-06-12 06:35:45 -040010931};
10932
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010933static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010934 .getattr = btrfs_getattr,
10935 .setattr = btrfs_setattr,
Josef Bacik5103e942007-11-16 11:45:54 -050010936 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010937 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -050010938 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010939 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010940 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010941 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010942};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010943static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -040010944 .getattr = btrfs_getattr,
10945 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010946 .permission = btrfs_permission,
Josef Bacik33268ea2008-07-24 12:16:36 -040010947 .listxattr = btrfs_listxattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010948 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010949 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010950 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -040010951};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010952static const struct inode_operations btrfs_symlink_inode_operations = {
Al Viro6b255392015-11-17 10:20:54 -050010953 .get_link = page_get_link,
Li Zefanf2095612010-11-19 02:05:24 +000010954 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -050010955 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010956 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -050010957 .listxattr = btrfs_listxattr,
Josef Bacike41f9412012-03-26 09:46:47 -040010958 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010959};
Yan, Zheng76dda932009-09-21 16:00:26 -040010960
Alexey Dobriyan82d339d2009-10-09 09:54:36 -040010961const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -040010962 .d_delete = btrfs_dentry_delete,
10963};