blob: 3f51ddc18f9885055d1c085af082d5648a79e52e [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>
David Sterba92d32172018-04-16 21:10:14 +020030#include <asm/unaligned.h>
Chris Mason39279cc2007-06-12 06:35:45 -040031#include "ctree.h"
32#include "disk-io.h"
33#include "transaction.h"
34#include "btrfs_inode.h"
Chris Mason39279cc2007-06-12 06:35:45 -040035#include "print-tree.h"
Chris Masone6dcd2d2008-07-17 12:53:50 -040036#include "ordered-data.h"
Christoph Hellwig95819c02008-08-28 06:21:17 -040037#include "xattr.h"
Chris Masone02119d2008-09-05 16:13:11 -040038#include "tree-log.h"
Jan Schmidt4a54c8c2011-07-22 15:41:52 +020039#include "volumes.h"
Chris Masonc8b97812008-10-29 14:49:59 -040040#include "compression.h"
Chris Masonb4ce94d2009-02-04 09:25:08 -050041#include "locking.h"
Josef Bacikdc89e982011-01-28 17:05:48 -050042#include "free-space-cache.h"
Li Zefan581bb052011-04-20 10:06:11 +080043#include "inode-map.h"
Liu Bo38c227d2013-01-29 03:18:40 +000044#include "backref.h"
Filipe David Borba Manana63541922014-01-07 11:47:46 +000045#include "props.h"
Dongsheng Yang31193212014-12-12 16:44:35 +080046#include "qgroup.h"
Wang Xiaoguangdda32452016-07-11 11:05:29 +080047#include "dedupe.h"
Chris Mason39279cc2007-06-12 06:35:45 -040048
49struct btrfs_iget_args {
Chris Mason90d3e592014-01-09 17:28:00 -080050 struct btrfs_key *location;
Chris Mason39279cc2007-06-12 06:35:45 -040051 struct btrfs_root *root;
52};
53
Filipe Mananaf28a4922015-12-08 19:23:20 +000054struct btrfs_dio_data {
Filipe Mananaf28a4922015-12-08 19:23:20 +000055 u64 reserve;
56 u64 unsubmitted_oe_range_start;
57 u64 unsubmitted_oe_range_end;
Liu Bo4aaedfb2016-12-14 22:36:05 -080058 int overwrite;
Filipe Mananaf28a4922015-12-08 19:23:20 +000059};
60
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070061static const struct inode_operations btrfs_dir_inode_operations;
62static const struct inode_operations btrfs_symlink_inode_operations;
63static const struct inode_operations btrfs_dir_ro_inode_operations;
64static const struct inode_operations btrfs_special_inode_operations;
65static const struct inode_operations btrfs_file_inode_operations;
Alexey Dobriyan7f094102009-09-21 17:01:10 -070066static const struct address_space_operations btrfs_aops;
67static const struct address_space_operations btrfs_symlink_aops;
Alexey Dobriyan828c0952009-10-01 15:43:56 -070068static const struct file_operations btrfs_dir_file_operations;
David Sterba20e55062015-11-19 11:42:28 +010069static const struct extent_io_ops btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -040070
71static struct kmem_cache *btrfs_inode_cachep;
72struct kmem_cache *btrfs_trans_handle_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040073struct kmem_cache *btrfs_path_cachep;
Josef Bacikdc89e982011-01-28 17:05:48 -050074struct kmem_cache *btrfs_free_space_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040075
76#define S_SHIFT 12
David Sterba4d4ab6d2015-11-19 11:42:31 +010077static const unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
Chris Mason39279cc2007-06-12 06:35:45 -040078 [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
79 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
80 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
81 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
82 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
83 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
84 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
85};
86
Eric Sandeen3972f262013-01-12 02:57:22 +000087static int btrfs_setsize(struct inode *inode, struct iattr *attr);
Filipe Manana213e8c52018-02-06 20:40:31 +000088static int btrfs_truncate(struct inode *inode, bool skip_writeback);
Josef Bacik5fd02042012-05-02 14:00:54 -040089static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent);
Chris Mason771ed682008-11-06 22:02:51 -050090static noinline int cow_file_range(struct inode *inode,
91 struct page *locked_page,
Wang Xiaoguangdda32452016-07-11 11:05:29 +080092 u64 start, u64 end, u64 delalloc_end,
93 int *page_started, unsigned long *nr_written,
94 int unlock, struct btrfs_dedupe_hash *hash);
Liu Bo6f9994d2017-01-31 07:50:22 -080095static struct extent_map *create_io_em(struct inode *inode, u64 start, u64 len,
96 u64 orig_start, u64 block_start,
97 u64 block_len, u64 orig_block_len,
98 u64 ram_bytes, int compress_type,
99 int type);
Josef Bacik7b128762008-07-24 12:17:14 -0400100
Qu Wenruo524272602017-03-08 10:25:52 +0800101static void __endio_write_update_ordered(struct inode *inode,
102 const u64 offset, const u64 bytes,
103 const bool uptodate);
104
105/*
106 * Cleanup all submitted ordered extents in specified range to handle errors
107 * from the fill_dellaloc() callback.
108 *
109 * NOTE: caller must ensure that when an error happens, it can not call
110 * extent_clear_unlock_delalloc() to clear both the bits EXTENT_DO_ACCOUNTING
111 * and EXTENT_DELALLOC simultaneously, because that causes the reserved metadata
112 * to be released, which we want to happen only when finishing the ordered
113 * extent (btrfs_finish_ordered_io()). Also note that the caller of the
114 * fill_delalloc() callback already does proper cleanup for the first page of
115 * the range, that is, it invokes the callback writepage_end_io_hook() for the
116 * range of the first page.
117 */
118static inline void btrfs_cleanup_ordered_extents(struct inode *inode,
119 const u64 offset,
120 const u64 bytes)
121{
Naohiro Aota63d71450c2017-09-01 17:58:47 +0900122 unsigned long index = offset >> PAGE_SHIFT;
123 unsigned long end_index = (offset + bytes - 1) >> PAGE_SHIFT;
124 struct page *page;
125
126 while (index <= end_index) {
127 page = find_get_page(inode->i_mapping, index);
128 index++;
129 if (!page)
130 continue;
131 ClearPagePrivate2(page);
132 put_page(page);
133 }
Qu Wenruo524272602017-03-08 10:25:52 +0800134 return __endio_write_update_ordered(inode, offset + PAGE_SIZE,
135 bytes - PAGE_SIZE, false);
136}
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);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300233 offset = start & (PAGE_SIZE - 1);
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
359struct async_cow {
360 struct inode *inode;
361 struct btrfs_root *root;
362 struct page *locked_page;
363 u64 start;
364 u64 end;
Liu Bof82b7352017-10-23 23:18:16 -0600365 unsigned int write_flags;
Chris Mason771ed682008-11-06 22:02:51 -0500366 struct list_head extents;
367 struct btrfs_work work;
368};
369
370static noinline int add_async_extent(struct async_cow *cow,
371 u64 start, u64 ram_size,
372 u64 compressed_size,
373 struct page **pages,
Li Zefan261507a02010-12-17 14:21:50 +0800374 unsigned long nr_pages,
375 int compress_type)
Chris Mason771ed682008-11-06 22:02:51 -0500376{
377 struct async_extent *async_extent;
378
379 async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100380 BUG_ON(!async_extent); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -0500381 async_extent->start = start;
382 async_extent->ram_size = ram_size;
383 async_extent->compressed_size = compressed_size;
384 async_extent->pages = pages;
385 async_extent->nr_pages = nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800386 async_extent->compress_type = compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500387 list_add_tail(&async_extent->list, &cow->extents);
388 return 0;
389}
390
Timofey Titovetsc2fcdcd2017-07-17 16:52:58 +0300391static inline int inode_need_compress(struct inode *inode, u64 start, u64 end)
Wang Shilongf79707b2014-07-17 11:44:09 +0800392{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400393 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Wang Shilongf79707b2014-07-17 11:44:09 +0800394
395 /* force compress */
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400396 if (btrfs_test_opt(fs_info, FORCE_COMPRESS))
Wang Shilongf79707b2014-07-17 11:44:09 +0800397 return 1;
David Sterbaeec63c62017-07-17 19:41:31 +0200398 /* defrag ioctl */
399 if (BTRFS_I(inode)->defrag_compress)
400 return 1;
Wang Shilongf79707b2014-07-17 11:44:09 +0800401 /* bad compression ratios */
402 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS)
403 return 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400404 if (btrfs_test_opt(fs_info, COMPRESS) ||
Wang Shilongf79707b2014-07-17 11:44:09 +0800405 BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS ||
David Sterbab52aa8c2017-07-17 19:17:20 +0200406 BTRFS_I(inode)->prop_compress)
Timofey Titovetsc2fcdcd2017-07-17 16:52:58 +0300407 return btrfs_compress_heuristic(inode, start, end);
Wang Shilongf79707b2014-07-17 11:44:09 +0800408 return 0;
409}
410
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200411static inline void inode_should_defrag(struct btrfs_inode *inode,
Anand Jain26d30f82016-12-19 19:09:06 +0800412 u64 start, u64 end, u64 num_bytes, u64 small_write)
413{
414 /* If this is a small write inside eof, kick off a defrag */
415 if (num_bytes < small_write &&
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200416 (start > 0 || end + 1 < inode->disk_i_size))
Anand Jain26d30f82016-12-19 19:09:06 +0800417 btrfs_add_inode_defrag(NULL, inode);
418}
419
Chris Masonc8b97812008-10-29 14:49:59 -0400420/*
Chris Mason771ed682008-11-06 22:02:51 -0500421 * we create compressed extents in two phases. The first
422 * phase compresses a range of pages that have already been
423 * locked (both pages and state bits are locked).
Chris Masonc8b97812008-10-29 14:49:59 -0400424 *
Chris Mason771ed682008-11-06 22:02:51 -0500425 * This is done inside an ordered work queue, and the compression
426 * is spread across many cpus. The actual IO submission is step
427 * two, and the ordered work queue takes care of making sure that
428 * happens in the same order things were put onto the queue by
429 * writepages and friends.
Chris Masonc8b97812008-10-29 14:49:59 -0400430 *
Chris Mason771ed682008-11-06 22:02:51 -0500431 * If this code finds it can't get good compression, it puts an
432 * entry onto the work queue to write the uncompressed bytes. This
433 * makes sure that both compressed inodes and uncompressed inodes
Artem Bityutskiyb2570312012-07-25 18:12:06 +0300434 * are written in the same order that the flusher thread sent them
435 * down.
Chris Masond352ac62008-09-29 15:18:18 -0400436 */
Filipe Mananac44f6492014-10-09 21:15:44 +0100437static noinline void compress_file_range(struct inode *inode,
Chris Mason771ed682008-11-06 22:02:51 -0500438 struct page *locked_page,
439 u64 start, u64 end,
440 struct async_cow *async_cow,
441 int *num_added)
Chris Masonb888db22007-08-27 16:49:44 -0400442{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400443 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400444 u64 blocksize = fs_info->sectorsize;
Chris Masonc8b97812008-10-29 14:49:59 -0400445 u64 actual_end;
Chris Mason42dc7ba2008-12-15 11:44:56 -0500446 u64 isize = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400447 int ret = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400448 struct page **pages = NULL;
449 unsigned long nr_pages;
Chris Masonc8b97812008-10-29 14:49:59 -0400450 unsigned long total_compressed = 0;
451 unsigned long total_in = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400452 int i;
453 int will_compress;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400454 int compress_type = fs_info->compress_type;
Chris Mason4adaa612013-03-26 13:07:00 -0400455 int redirty = 0;
Chris Masonb888db22007-08-27 16:49:44 -0400456
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200457 inode_should_defrag(BTRFS_I(inode), start, end, end - start + 1,
458 SZ_16K);
Chris Mason4cb53002011-05-24 15:35:30 -0400459
Chris Mason42dc7ba2008-12-15 11:44:56 -0500460 actual_end = min_t(u64, isize, end + 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400461again:
462 will_compress = 0;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300463 nr_pages = (end >> PAGE_SHIFT) - (start >> PAGE_SHIFT) + 1;
David Sterba069eac72017-02-14 19:36:54 +0100464 BUILD_BUG_ON((BTRFS_MAX_COMPRESSED % PAGE_SIZE) != 0);
465 nr_pages = min_t(unsigned long, nr_pages,
466 BTRFS_MAX_COMPRESSED / PAGE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400467
Chris Masonf03d9301f2009-02-04 09:31:06 -0500468 /*
469 * we don't want to send crud past the end of i_size through
470 * compression, that's just a waste of CPU time. So, if the
471 * end of the file is before the start of our current
472 * requested range of bytes, we bail out to the uncompressed
473 * cleanup code that can deal with all of this.
474 *
475 * It isn't really the fastest way to fix things, but this is a
476 * very uncommon corner.
477 */
478 if (actual_end <= start)
479 goto cleanup_and_bail_uncompressed;
480
Chris Masonc8b97812008-10-29 14:49:59 -0400481 total_compressed = actual_end - start;
482
Shilong Wang4bcbb332014-10-07 18:44:35 -0400483 /*
484 * skip compression for a small file range(<=blocksize) that
Nicholas D Steeves01327612016-05-19 21:18:45 -0400485 * isn't an inline extent, since it doesn't save disk space at all.
Shilong Wang4bcbb332014-10-07 18:44:35 -0400486 */
487 if (total_compressed <= blocksize &&
488 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
489 goto cleanup_and_bail_uncompressed;
490
David Sterba069eac72017-02-14 19:36:54 +0100491 total_compressed = min_t(unsigned long, total_compressed,
492 BTRFS_MAX_UNCOMPRESSED);
Chris Masonc8b97812008-10-29 14:49:59 -0400493 total_in = 0;
494 ret = 0;
Chris Masondb945352007-10-15 16:15:53 -0400495
Chris Mason771ed682008-11-06 22:02:51 -0500496 /*
497 * we do compression for mount -o compress and when the
498 * inode has not been flagged as nocompress. This flag can
499 * change at any time if we discover bad compression ratios.
Chris Masonc8b97812008-10-29 14:49:59 -0400500 */
Timofey Titovetsc2fcdcd2017-07-17 16:52:58 +0300501 if (inode_need_compress(inode, start, end)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400502 WARN_ON(pages);
David Sterba31e818f2015-02-20 18:00:26 +0100503 pages = kcalloc(nr_pages, sizeof(struct page *), GFP_NOFS);
Li Zefan560f7d72011-09-08 10:22:01 +0800504 if (!pages) {
505 /* just bail out to the uncompressed code */
506 goto cont;
507 }
Chris Mason179e29e2007-11-01 11:28:41 -0400508
David Sterbaeec63c62017-07-17 19:41:31 +0200509 if (BTRFS_I(inode)->defrag_compress)
510 compress_type = BTRFS_I(inode)->defrag_compress;
511 else if (BTRFS_I(inode)->prop_compress)
David Sterbab52aa8c2017-07-17 19:17:20 +0200512 compress_type = BTRFS_I(inode)->prop_compress;
Li Zefan261507a02010-12-17 14:21:50 +0800513
Chris Mason4adaa612013-03-26 13:07:00 -0400514 /*
515 * we need to call clear_page_dirty_for_io on each
516 * page in the range. Otherwise applications with the file
517 * mmap'd can wander in and change the page contents while
518 * we are compressing them.
519 *
520 * If the compression fails for any reason, we set the pages
521 * dirty again later on.
Timofey Titovetse9679de2017-10-24 01:29:48 +0300522 *
523 * Note that the remaining part is redirtied, the start pointer
524 * has moved, the end is the original one.
Chris Mason4adaa612013-03-26 13:07:00 -0400525 */
Timofey Titovetse9679de2017-10-24 01:29:48 +0300526 if (!redirty) {
527 extent_range_clear_dirty_for_io(inode, start, end);
528 redirty = 1;
529 }
David Sterbaf51d2b52017-09-15 17:36:57 +0200530
531 /* Compression level is applied here and only here */
532 ret = btrfs_compress_pages(
533 compress_type | (fs_info->compress_level << 4),
Li Zefan261507a02010-12-17 14:21:50 +0800534 inode->i_mapping, start,
David Sterba38c31462017-02-14 19:04:07 +0100535 pages,
David Sterba4d3a8002017-02-14 19:04:07 +0100536 &nr_pages,
Li Zefan261507a02010-12-17 14:21:50 +0800537 &total_in,
David Sterbae5d74902017-02-14 19:45:05 +0100538 &total_compressed);
Chris Masonc8b97812008-10-29 14:49:59 -0400539
540 if (!ret) {
541 unsigned long offset = total_compressed &
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300542 (PAGE_SIZE - 1);
David Sterba4d3a8002017-02-14 19:04:07 +0100543 struct page *page = pages[nr_pages - 1];
Chris Masonc8b97812008-10-29 14:49:59 -0400544 char *kaddr;
545
546 /* zero the tail end of the last page, we might be
547 * sending it down to disk
548 */
549 if (offset) {
Cong Wang7ac687d2011-11-25 23:14:28 +0800550 kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400551 memset(kaddr + offset, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300552 PAGE_SIZE - offset);
Cong Wang7ac687d2011-11-25 23:14:28 +0800553 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400554 }
555 will_compress = 1;
556 }
557 }
Li Zefan560f7d72011-09-08 10:22:01 +0800558cont:
Chris Masonc8b97812008-10-29 14:49:59 -0400559 if (start == 0) {
560 /* lets try to make an inline extent */
Timofey Titovets6018ba02017-09-15 01:57:26 +0300561 if (ret || total_in < actual_end) {
Chris Masonc8b97812008-10-29 14:49:59 -0400562 /* we didn't compress the entire range, try
Chris Mason771ed682008-11-06 22:02:51 -0500563 * to make an uncompressed inline extent.
Chris Masonc8b97812008-10-29 14:49:59 -0400564 */
Nikolay Borisovd02c0e22018-03-02 09:43:15 +0200565 ret = cow_file_range_inline(inode, start, end, 0,
566 BTRFS_COMPRESS_NONE, NULL);
Chris Masonc8b97812008-10-29 14:49:59 -0400567 } else {
Chris Mason771ed682008-11-06 22:02:51 -0500568 /* try making a compressed inline extent */
Nikolay Borisovd02c0e22018-03-02 09:43:15 +0200569 ret = cow_file_range_inline(inode, start, end,
Li Zefanfe3f5662011-03-28 08:30:38 +0000570 total_compressed,
571 compress_type, pages);
Chris Masonc8b97812008-10-29 14:49:59 -0400572 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100573 if (ret <= 0) {
Josef Bacik151a41b2013-07-29 13:22:24 -0400574 unsigned long clear_flags = EXTENT_DELALLOC |
Josef Bacik8b62f872017-10-19 14:15:55 -0400575 EXTENT_DELALLOC_NEW | EXTENT_DEFRAG |
576 EXTENT_DO_ACCOUNTING;
Filipe Mananae6eb4312014-10-10 10:45:12 +0100577 unsigned long page_error_op;
578
Filipe Mananae6eb4312014-10-10 10:45:12 +0100579 page_error_op = ret < 0 ? PAGE_SET_ERROR : 0;
Josef Bacik151a41b2013-07-29 13:22:24 -0400580
Chris Mason771ed682008-11-06 22:02:51 -0500581 /*
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100582 * inline extent creation worked or returned error,
583 * we don't need to create any more async work items.
584 * Unlock and free up our temp pages.
Josef Bacik8b62f872017-10-19 14:15:55 -0400585 *
586 * We use DO_ACCOUNTING here because we need the
587 * delalloc_release_metadata to be done _after_ we drop
588 * our outstanding extent for clearing delalloc for this
589 * range.
Chris Mason771ed682008-11-06 22:02:51 -0500590 */
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800591 extent_clear_unlock_delalloc(inode, start, end, end,
592 NULL, clear_flags,
593 PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -0400594 PAGE_CLEAR_DIRTY |
595 PAGE_SET_WRITEBACK |
Filipe Mananae6eb4312014-10-10 10:45:12 +0100596 page_error_op |
Josef Bacikc2790a22013-07-29 11:20:47 -0400597 PAGE_END_WRITEBACK);
Chris Masonc8b97812008-10-29 14:49:59 -0400598 goto free_pages_out;
599 }
600 }
601
602 if (will_compress) {
603 /*
604 * we aren't doing an inline extent round the compressed size
605 * up to a block size boundary so the allocator does sane
606 * things
607 */
Qu Wenruofda28322013-02-26 08:10:22 +0000608 total_compressed = ALIGN(total_compressed, blocksize);
Chris Masonc8b97812008-10-29 14:49:59 -0400609
610 /*
611 * one last check to make sure the compression is really a
Timofey Titovets170607e2017-06-06 14:41:15 +0300612 * win, compare the page count read with the blocks on disk,
613 * compression must free at least one sector size
Chris Masonc8b97812008-10-29 14:49:59 -0400614 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300615 total_in = ALIGN(total_in, PAGE_SIZE);
Timofey Titovets170607e2017-06-06 14:41:15 +0300616 if (total_compressed + blocksize <= total_in) {
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700617 *num_added += 1;
618
619 /*
620 * The async work queues will take care of doing actual
621 * allocation on disk for these compressed pages, and
622 * will submit them to the elevator.
623 */
Timofey Titovets11708622017-10-03 18:06:01 +0300624 add_async_extent(async_cow, start, total_in,
David Sterba4d3a8002017-02-14 19:04:07 +0100625 total_compressed, pages, nr_pages,
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700626 compress_type);
627
Timofey Titovets11708622017-10-03 18:06:01 +0300628 if (start + total_in < end) {
629 start += total_in;
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700630 pages = NULL;
631 cond_resched();
632 goto again;
633 }
634 return;
Chris Masonc8b97812008-10-29 14:49:59 -0400635 }
636 }
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700637 if (pages) {
Chris Masonc8b97812008-10-29 14:49:59 -0400638 /*
639 * the compression code ran but failed to make things smaller,
640 * free any pages it allocated and our page pointer array
641 */
David Sterba4d3a8002017-02-14 19:04:07 +0100642 for (i = 0; i < nr_pages; i++) {
Chris Mason70b99e62008-10-31 12:46:39 -0400643 WARN_ON(pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300644 put_page(pages[i]);
Chris Masonc8b97812008-10-29 14:49:59 -0400645 }
646 kfree(pages);
647 pages = NULL;
648 total_compressed = 0;
David Sterba4d3a8002017-02-14 19:04:07 +0100649 nr_pages = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400650
651 /* flag the file so we don't compress in the future */
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400652 if (!btrfs_test_opt(fs_info, FORCE_COMPRESS) &&
David Sterbab52aa8c2017-07-17 19:17:20 +0200653 !(BTRFS_I(inode)->prop_compress)) {
Chris Masona555f812010-01-28 16:18:15 -0500654 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
Chris Mason1e701a32010-03-11 09:42:04 -0500655 }
Chris Masonc8b97812008-10-29 14:49:59 -0400656 }
Chris Masonf03d9301f2009-02-04 09:31:06 -0500657cleanup_and_bail_uncompressed:
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700658 /*
659 * No compression, but we still need to write the pages in the file
660 * we've been given so far. redirty the locked page if it corresponds
661 * to our extent and set things up for the async work queue to run
662 * cow_file_range to do the normal delalloc dance.
663 */
664 if (page_offset(locked_page) >= start &&
665 page_offset(locked_page) <= end)
666 __set_page_dirty_nobuffers(locked_page);
667 /* unlocked later on in the async handlers */
668
669 if (redirty)
670 extent_range_redirty_for_io(inode, start, end);
671 add_async_extent(async_cow, start, end - start + 1, 0, NULL, 0,
672 BTRFS_COMPRESS_NONE);
673 *num_added += 1;
Chris Mason771ed682008-11-06 22:02:51 -0500674
Filipe Mananac44f6492014-10-09 21:15:44 +0100675 return;
Chris Mason771ed682008-11-06 22:02:51 -0500676
677free_pages_out:
David Sterba4d3a8002017-02-14 19:04:07 +0100678 for (i = 0; i < nr_pages; i++) {
Chris Mason771ed682008-11-06 22:02:51 -0500679 WARN_ON(pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300680 put_page(pages[i]);
Chris Mason771ed682008-11-06 22:02:51 -0500681 }
Chris Masond3977122009-01-05 21:25:51 -0500682 kfree(pages);
Chris Mason771ed682008-11-06 22:02:51 -0500683}
Chris Mason771ed682008-11-06 22:02:51 -0500684
Filipe Manana40ae8372014-10-06 22:14:24 +0100685static void free_async_extent_pages(struct async_extent *async_extent)
686{
687 int i;
688
689 if (!async_extent->pages)
690 return;
691
692 for (i = 0; i < async_extent->nr_pages; i++) {
693 WARN_ON(async_extent->pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300694 put_page(async_extent->pages[i]);
Filipe Manana40ae8372014-10-06 22:14:24 +0100695 }
696 kfree(async_extent->pages);
697 async_extent->nr_pages = 0;
698 async_extent->pages = NULL;
Chris Mason771ed682008-11-06 22:02:51 -0500699}
700
701/*
702 * phase two of compressed writeback. This is the ordered portion
703 * of the code, which only gets called in the order the work was
704 * queued. We walk all the async extents created by compress_file_range
705 * and send them down to the disk.
706 */
Filipe Mananadec8f172014-10-06 22:14:26 +0100707static noinline void submit_compressed_extents(struct inode *inode,
Chris Mason771ed682008-11-06 22:02:51 -0500708 struct async_cow *async_cow)
709{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400710 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason771ed682008-11-06 22:02:51 -0500711 struct async_extent *async_extent;
712 u64 alloc_hint = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500713 struct btrfs_key ins;
714 struct extent_map *em;
715 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason771ed682008-11-06 22:02:51 -0500716 struct extent_io_tree *io_tree;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500717 int ret = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500718
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500719again:
Chris Masond3977122009-01-05 21:25:51 -0500720 while (!list_empty(&async_cow->extents)) {
Chris Mason771ed682008-11-06 22:02:51 -0500721 async_extent = list_entry(async_cow->extents.next,
722 struct async_extent, list);
723 list_del(&async_extent->list);
724
725 io_tree = &BTRFS_I(inode)->io_tree;
726
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500727retry:
Chris Mason771ed682008-11-06 22:02:51 -0500728 /* did the compression code fall back to uncompressed IO? */
729 if (!async_extent->pages) {
730 int page_started = 0;
731 unsigned long nr_written = 0;
732
733 lock_extent(io_tree, async_extent->start,
Josef Bacik2ac55d42010-02-03 19:33:23 +0000734 async_extent->start +
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100735 async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500736
737 /* allocate blocks */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500738 ret = cow_file_range(inode, async_cow->locked_page,
739 async_extent->start,
740 async_extent->start +
741 async_extent->ram_size - 1,
Wang Xiaoguangdda32452016-07-11 11:05:29 +0800742 async_extent->start +
743 async_extent->ram_size - 1,
744 &page_started, &nr_written, 0,
745 NULL);
Chris Mason771ed682008-11-06 22:02:51 -0500746
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100747 /* JDM XXX */
748
Chris Mason771ed682008-11-06 22:02:51 -0500749 /*
750 * if page_started, cow_file_range inserted an
751 * inline extent and took care of all the unlocking
752 * and IO for us. Otherwise, we need to submit
753 * all those pages down to the drive.
754 */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500755 if (!page_started && !ret)
Nikolay Borisov5e3ee232017-12-08 15:55:58 +0200756 extent_write_locked_range(inode,
757 async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500758 async_extent->start +
Chris Mason771ed682008-11-06 22:02:51 -0500759 async_extent->ram_size - 1,
Chris Mason771ed682008-11-06 22:02:51 -0500760 WB_SYNC_ALL);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500761 else if (ret)
762 unlock_page(async_cow->locked_page);
Chris Mason771ed682008-11-06 22:02:51 -0500763 kfree(async_extent);
764 cond_resched();
765 continue;
766 }
767
768 lock_extent(io_tree, async_extent->start,
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100769 async_extent->start + async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500770
Wang Xiaoguang18513092016-07-25 15:51:40 +0800771 ret = btrfs_reserve_extent(root, async_extent->ram_size,
Chris Mason771ed682008-11-06 22:02:51 -0500772 async_extent->compressed_size,
773 async_extent->compressed_size,
Miao Xiee570fd22014-06-19 10:42:50 +0800774 0, alloc_hint, &ins, 1, 1);
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500775 if (ret) {
Filipe Manana40ae8372014-10-06 22:14:24 +0100776 free_async_extent_pages(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500777
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400778 if (ret == -ENOSPC) {
779 unlock_extent(io_tree, async_extent->start,
780 async_extent->start +
781 async_extent->ram_size - 1);
Liu Boce620032014-07-24 22:48:05 +0800782
783 /*
784 * we need to redirty the pages if we decide to
785 * fallback to uncompressed IO, otherwise we
786 * will not submit these pages down to lower
787 * layers.
788 */
789 extent_range_redirty_for_io(inode,
790 async_extent->start,
791 async_extent->start +
792 async_extent->ram_size - 1);
793
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100794 goto retry;
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400795 }
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500796 goto out_free;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500797 }
Yan, Zhengc2167752009-11-12 09:34:21 +0000798 /*
799 * here we're doing allocation and writeback of the
800 * compressed pages
801 */
Liu Bo6f9994d2017-01-31 07:50:22 -0800802 em = create_io_em(inode, async_extent->start,
803 async_extent->ram_size, /* len */
804 async_extent->start, /* orig_start */
805 ins.objectid, /* block_start */
806 ins.offset, /* block_len */
807 ins.offset, /* orig_block_len */
808 async_extent->ram_size, /* ram_bytes */
809 async_extent->compress_type,
810 BTRFS_ORDERED_COMPRESSED);
811 if (IS_ERR(em))
812 /* ret value is not necessary due to void function */
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500813 goto out_free_reserve;
Liu Bo6f9994d2017-01-31 07:50:22 -0800814 free_extent_map(em);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500815
Li Zefan261507a02010-12-17 14:21:50 +0800816 ret = btrfs_add_ordered_extent_compress(inode,
817 async_extent->start,
818 ins.objectid,
819 async_extent->ram_size,
820 ins.offset,
821 BTRFS_ORDERED_COMPRESSED,
822 async_extent->compress_type);
Filipe Mananad9f85962014-08-25 10:43:00 +0100823 if (ret) {
Nikolay Borisovdcdbc052017-02-20 13:50:45 +0200824 btrfs_drop_extent_cache(BTRFS_I(inode),
825 async_extent->start,
Filipe Mananad9f85962014-08-25 10:43:00 +0100826 async_extent->start +
827 async_extent->ram_size - 1, 0);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500828 goto out_free_reserve;
Filipe Mananad9f85962014-08-25 10:43:00 +0100829 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400830 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Chris Mason771ed682008-11-06 22:02:51 -0500831
Chris Mason771ed682008-11-06 22:02:51 -0500832 /*
833 * clear dirty, set writeback and unlock the pages.
834 */
Josef Bacikc2790a22013-07-29 11:20:47 -0400835 extent_clear_unlock_delalloc(inode, async_extent->start,
Chris Masona791e352009-10-08 11:27:10 -0400836 async_extent->start +
837 async_extent->ram_size - 1,
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800838 async_extent->start +
839 async_extent->ram_size - 1,
Josef Bacik151a41b2013-07-29 13:22:24 -0400840 NULL, EXTENT_LOCKED | EXTENT_DELALLOC,
841 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -0400842 PAGE_SET_WRITEBACK);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200843 if (btrfs_submit_compressed_write(inode,
Chris Masond3977122009-01-05 21:25:51 -0500844 async_extent->start,
845 async_extent->ram_size,
846 ins.objectid,
847 ins.offset, async_extent->pages,
Liu Bof82b7352017-10-23 23:18:16 -0600848 async_extent->nr_pages,
849 async_cow->write_flags)) {
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100850 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
851 struct page *p = async_extent->pages[0];
852 const u64 start = async_extent->start;
853 const u64 end = start + async_extent->ram_size - 1;
854
855 p->mapping = inode->i_mapping;
856 tree->ops->writepage_end_io_hook(p, start, end,
857 NULL, 0);
858 p->mapping = NULL;
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800859 extent_clear_unlock_delalloc(inode, start, end, end,
860 NULL, 0,
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100861 PAGE_END_WRITEBACK |
862 PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +0100863 free_async_extent_pages(async_extent);
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100864 }
Chris Mason771ed682008-11-06 22:02:51 -0500865 alloc_hint = ins.objectid + ins.offset;
866 kfree(async_extent);
867 cond_resched();
868 }
Filipe Mananadec8f172014-10-06 22:14:26 +0100869 return;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500870out_free_reserve:
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400871 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400872 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100873out_free:
Josef Bacikc2790a22013-07-29 11:20:47 -0400874 extent_clear_unlock_delalloc(inode, async_extent->start,
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500875 async_extent->start +
876 async_extent->ram_size - 1,
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800877 async_extent->start +
878 async_extent->ram_size - 1,
Josef Bacikc2790a22013-07-29 11:20:47 -0400879 NULL, EXTENT_LOCKED | EXTENT_DELALLOC |
Filipe Mananaa7e3b972017-04-03 10:45:46 +0100880 EXTENT_DELALLOC_NEW |
Josef Bacik151a41b2013-07-29 13:22:24 -0400881 EXTENT_DEFRAG | EXTENT_DO_ACCOUNTING,
882 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Filipe Manana704de492014-10-06 22:14:22 +0100883 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK |
884 PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +0100885 free_async_extent_pages(async_extent);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100886 kfree(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500887 goto again;
Chris Mason771ed682008-11-06 22:02:51 -0500888}
889
Josef Bacik4b46fce2010-05-23 11:00:55 -0400890static u64 get_extent_allocation_hint(struct inode *inode, u64 start,
891 u64 num_bytes)
892{
893 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
894 struct extent_map *em;
895 u64 alloc_hint = 0;
896
897 read_lock(&em_tree->lock);
898 em = search_extent_mapping(em_tree, start, num_bytes);
899 if (em) {
900 /*
901 * if block start isn't an actual block number then find the
902 * first block in this inode and use that as a hint. If that
903 * block is also bogus then just don't worry about it.
904 */
905 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
906 free_extent_map(em);
907 em = search_extent_mapping(em_tree, 0, 0);
908 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
909 alloc_hint = em->block_start;
910 if (em)
911 free_extent_map(em);
912 } else {
913 alloc_hint = em->block_start;
914 free_extent_map(em);
915 }
916 }
917 read_unlock(&em_tree->lock);
918
919 return alloc_hint;
920}
921
Chris Mason771ed682008-11-06 22:02:51 -0500922/*
923 * when extent_io.c finds a delayed allocation range in the file,
924 * the call backs end up in this code. The basic idea is to
925 * allocate extents on disk for the range, and create ordered data structs
926 * in ram to track those extents.
927 *
928 * locked_page is the page that writepage had locked already. We use
929 * it to make sure we don't do extra locks or unlocks.
930 *
931 * *page_started is set to one if we unlock locked_page and do everything
932 * required to start IO on it. It may be clean and already done with
933 * IO when we return.
934 */
Josef Bacik00361582013-08-14 14:02:47 -0400935static noinline int cow_file_range(struct inode *inode,
936 struct page *locked_page,
Wang Xiaoguangdda32452016-07-11 11:05:29 +0800937 u64 start, u64 end, u64 delalloc_end,
938 int *page_started, unsigned long *nr_written,
939 int unlock, struct btrfs_dedupe_hash *hash)
Chris Mason771ed682008-11-06 22:02:51 -0500940{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400941 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik00361582013-08-14 14:02:47 -0400942 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason771ed682008-11-06 22:02:51 -0500943 u64 alloc_hint = 0;
944 u64 num_bytes;
945 unsigned long ram_size;
Filipe Mananaa315e682017-03-06 23:04:20 +0000946 u64 cur_alloc_size = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400947 u64 blocksize = fs_info->sectorsize;
Chris Mason771ed682008-11-06 22:02:51 -0500948 struct btrfs_key ins;
949 struct extent_map *em;
Filipe Mananaa315e682017-03-06 23:04:20 +0000950 unsigned clear_bits;
951 unsigned long page_ops;
952 bool extent_reserved = false;
Chris Mason771ed682008-11-06 22:02:51 -0500953 int ret = 0;
954
Nikolay Borisov70ddc552017-02-20 13:50:35 +0200955 if (btrfs_is_free_space_inode(BTRFS_I(inode))) {
Josef Bacik02ecd2c2013-10-25 16:19:08 -0400956 WARN_ON_ONCE(1);
Josef Bacik29bce2f2014-02-07 12:21:23 -0500957 ret = -EINVAL;
958 goto out_unlock;
Josef Bacik02ecd2c2013-10-25 16:19:08 -0400959 }
Chris Mason771ed682008-11-06 22:02:51 -0500960
Qu Wenruofda28322013-02-26 08:10:22 +0000961 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Mason771ed682008-11-06 22:02:51 -0500962 num_bytes = max(blocksize, num_bytes);
Anand Jain566b1762018-02-15 18:07:59 +0800963 ASSERT(num_bytes <= btrfs_super_total_bytes(fs_info->super_copy));
Chris Mason771ed682008-11-06 22:02:51 -0500964
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200965 inode_should_defrag(BTRFS_I(inode), start, end, num_bytes, SZ_64K);
Chris Mason4cb53002011-05-24 15:35:30 -0400966
Chris Mason771ed682008-11-06 22:02:51 -0500967 if (start == 0) {
968 /* lets try to make an inline extent */
Nikolay Borisovd02c0e22018-03-02 09:43:15 +0200969 ret = cow_file_range_inline(inode, start, end, 0,
970 BTRFS_COMPRESS_NONE, NULL);
Chris Mason771ed682008-11-06 22:02:51 -0500971 if (ret == 0) {
Josef Bacik8b62f872017-10-19 14:15:55 -0400972 /*
973 * We use DO_ACCOUNTING here because we need the
974 * delalloc_release_metadata to be run _after_ we drop
975 * our outstanding extent for clearing delalloc for this
976 * range.
977 */
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800978 extent_clear_unlock_delalloc(inode, start, end,
979 delalloc_end, NULL,
Josef Bacikc2790a22013-07-29 11:20:47 -0400980 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik8b62f872017-10-19 14:15:55 -0400981 EXTENT_DELALLOC_NEW | EXTENT_DEFRAG |
982 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -0400983 PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
984 PAGE_END_WRITEBACK);
Chris Mason771ed682008-11-06 22:02:51 -0500985 *nr_written = *nr_written +
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300986 (end - start + PAGE_SIZE) / PAGE_SIZE;
Chris Mason771ed682008-11-06 22:02:51 -0500987 *page_started = 1;
Chris Mason771ed682008-11-06 22:02:51 -0500988 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100989 } else if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100990 goto out_unlock;
Chris Mason771ed682008-11-06 22:02:51 -0500991 }
992 }
Chris Masonc8b97812008-10-29 14:49:59 -0400993
Josef Bacik4b46fce2010-05-23 11:00:55 -0400994 alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +0200995 btrfs_drop_extent_cache(BTRFS_I(inode), start,
996 start + num_bytes - 1, 0);
Chris Mason3b951512008-04-17 11:29:12 -0400997
Anand Jain3752d222018-02-15 12:29:38 +0800998 while (num_bytes > 0) {
999 cur_alloc_size = num_bytes;
Wang Xiaoguang18513092016-07-25 15:51:40 +08001000 ret = btrfs_reserve_extent(root, cur_alloc_size, cur_alloc_size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001001 fs_info->sectorsize, 0, alloc_hint,
Miao Xiee570fd22014-06-19 10:42:50 +08001002 &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04001003 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001004 goto out_unlock;
Filipe Mananaa315e682017-03-06 23:04:20 +00001005 cur_alloc_size = ins.offset;
1006 extent_reserved = true;
Chris Masond3977122009-01-05 21:25:51 -05001007
Chris Mason771ed682008-11-06 22:02:51 -05001008 ram_size = ins.offset;
Liu Bo6f9994d2017-01-31 07:50:22 -08001009 em = create_io_em(inode, start, ins.offset, /* len */
1010 start, /* orig_start */
1011 ins.objectid, /* block_start */
1012 ins.offset, /* block_len */
1013 ins.offset, /* orig_block_len */
1014 ram_size, /* ram_bytes */
1015 BTRFS_COMPRESS_NONE, /* compress_type */
Liu Bo1af4a0a2017-02-13 15:35:09 -08001016 BTRFS_ORDERED_REGULAR /* type */);
Su Yue090a127a2018-05-30 16:48:56 +08001017 if (IS_ERR(em)) {
1018 ret = PTR_ERR(em);
Liu Boace68ba2013-04-22 10:53:47 +00001019 goto out_reserve;
Su Yue090a127a2018-05-30 16:48:56 +08001020 }
Liu Bo6f9994d2017-01-31 07:50:22 -08001021 free_extent_map(em);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001022
Chris Masone6dcd2d2008-07-17 12:53:50 -04001023 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
Chris Mason771ed682008-11-06 22:02:51 -05001024 ram_size, cur_alloc_size, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001025 if (ret)
Filipe Mananad9f85962014-08-25 10:43:00 +01001026 goto out_drop_extent_cache;
Chris Masonc8b97812008-10-29 14:49:59 -04001027
Yan Zheng17d217f2008-12-12 10:03:38 -05001028 if (root->root_key.objectid ==
1029 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1030 ret = btrfs_reloc_clone_csums(inode, start,
1031 cur_alloc_size);
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001032 /*
1033 * Only drop cache here, and process as normal.
1034 *
1035 * We must not allow extent_clear_unlock_delalloc()
1036 * at out_unlock label to free meta of this ordered
1037 * extent, as its meta should be freed by
1038 * btrfs_finish_ordered_io().
1039 *
1040 * So we must continue until @start is increased to
1041 * skip current ordered extent.
1042 */
Josef Bacik00361582013-08-14 14:02:47 -04001043 if (ret)
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001044 btrfs_drop_extent_cache(BTRFS_I(inode), start,
1045 start + ram_size - 1, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001046 }
1047
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001048 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana9cfa3e32016-04-26 15:39:32 +01001049
Chris Masonc8b97812008-10-29 14:49:59 -04001050 /* we're not doing compressed IO, don't unlock the first
1051 * page (which the caller expects to stay locked), don't
1052 * clear any dirty bits and don't set any writeback bits
Chris Mason8b62b722009-09-02 16:53:46 -04001053 *
1054 * Do set the Private2 bit so we know this page was properly
1055 * setup for writepage
Chris Masonc8b97812008-10-29 14:49:59 -04001056 */
Filipe Mananaa315e682017-03-06 23:04:20 +00001057 page_ops = unlock ? PAGE_UNLOCK : 0;
1058 page_ops |= PAGE_SET_PRIVATE2;
Chris Masona791e352009-10-08 11:27:10 -04001059
Josef Bacikc2790a22013-07-29 11:20:47 -04001060 extent_clear_unlock_delalloc(inode, start,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001061 start + ram_size - 1,
1062 delalloc_end, locked_page,
Josef Bacikc2790a22013-07-29 11:20:47 -04001063 EXTENT_LOCKED | EXTENT_DELALLOC,
Filipe Mananaa315e682017-03-06 23:04:20 +00001064 page_ops);
Anand Jain3752d222018-02-15 12:29:38 +08001065 if (num_bytes < cur_alloc_size)
1066 num_bytes = 0;
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001067 else
Anand Jain3752d222018-02-15 12:29:38 +08001068 num_bytes -= cur_alloc_size;
Chris Masonc59f8952007-12-17 20:14:04 -05001069 alloc_hint = ins.objectid + ins.offset;
1070 start += cur_alloc_size;
Filipe Mananaa315e682017-03-06 23:04:20 +00001071 extent_reserved = false;
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001072
1073 /*
1074 * btrfs_reloc_clone_csums() error, since start is increased
1075 * extent_clear_unlock_delalloc() at out_unlock label won't
1076 * free metadata of current ordered extent, we're OK to exit.
1077 */
1078 if (ret)
1079 goto out_unlock;
Chris Masonb888db22007-08-27 16:49:44 -04001080 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001081out:
Chris Masonbe20aa92007-12-17 20:14:01 -05001082 return ret;
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001083
Filipe Mananad9f85962014-08-25 10:43:00 +01001084out_drop_extent_cache:
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02001085 btrfs_drop_extent_cache(BTRFS_I(inode), start, start + ram_size - 1, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001086out_reserve:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001087 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001088 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001089out_unlock:
Filipe Mananaa7e3b972017-04-03 10:45:46 +01001090 clear_bits = EXTENT_LOCKED | EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
1091 EXTENT_DEFRAG | EXTENT_CLEAR_META_RESV;
Filipe Mananaa315e682017-03-06 23:04:20 +00001092 page_ops = PAGE_UNLOCK | PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
1093 PAGE_END_WRITEBACK;
1094 /*
1095 * If we reserved an extent for our delalloc range (or a subrange) and
1096 * failed to create the respective ordered extent, then it means that
1097 * when we reserved the extent we decremented the extent's size from
1098 * the data space_info's bytes_may_use counter and incremented the
1099 * space_info's bytes_reserved counter by the same amount. We must make
1100 * sure extent_clear_unlock_delalloc() does not try to decrement again
1101 * the data space_info's bytes_may_use counter, therefore we do not pass
1102 * it the flag EXTENT_CLEAR_DATA_RESV.
1103 */
1104 if (extent_reserved) {
1105 extent_clear_unlock_delalloc(inode, start,
1106 start + cur_alloc_size,
1107 start + cur_alloc_size,
1108 locked_page,
1109 clear_bits,
1110 page_ops);
1111 start += cur_alloc_size;
1112 if (start >= end)
1113 goto out;
1114 }
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001115 extent_clear_unlock_delalloc(inode, start, end, delalloc_end,
1116 locked_page,
Filipe Mananaa315e682017-03-06 23:04:20 +00001117 clear_bits | EXTENT_CLEAR_DATA_RESV,
1118 page_ops);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001119 goto out;
Chris Mason771ed682008-11-06 22:02:51 -05001120}
Chris Masonc8b97812008-10-29 14:49:59 -04001121
Chris Mason771ed682008-11-06 22:02:51 -05001122/*
1123 * work queue call back to started compression on a file and pages
1124 */
1125static noinline void async_cow_start(struct btrfs_work *work)
1126{
1127 struct async_cow *async_cow;
1128 int num_added = 0;
1129 async_cow = container_of(work, struct async_cow, work);
1130
1131 compress_file_range(async_cow->inode, async_cow->locked_page,
1132 async_cow->start, async_cow->end, async_cow,
1133 &num_added);
Josef Bacik8180ef82012-06-08 15:16:12 -04001134 if (num_added == 0) {
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001135 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001136 async_cow->inode = NULL;
Josef Bacik8180ef82012-06-08 15:16:12 -04001137 }
Chris Mason771ed682008-11-06 22:02:51 -05001138}
1139
1140/*
1141 * work queue call back to submit previously compressed pages
1142 */
1143static noinline void async_cow_submit(struct btrfs_work *work)
1144{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001145 struct btrfs_fs_info *fs_info;
Chris Mason771ed682008-11-06 22:02:51 -05001146 struct async_cow *async_cow;
1147 struct btrfs_root *root;
1148 unsigned long nr_pages;
1149
1150 async_cow = container_of(work, struct async_cow, work);
1151
1152 root = async_cow->root;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001153 fs_info = root->fs_info;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001154 nr_pages = (async_cow->end - async_cow->start + PAGE_SIZE) >>
1155 PAGE_SHIFT;
Chris Mason771ed682008-11-06 22:02:51 -05001156
David Sterba093258e2018-02-26 16:15:17 +01001157 /* atomic_sub_return implies a barrier */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001158 if (atomic_sub_return(nr_pages, &fs_info->async_delalloc_pages) <
David Sterba093258e2018-02-26 16:15:17 +01001159 5 * SZ_1M)
1160 cond_wake_up_nomb(&fs_info->async_submit_wait);
Chris Mason771ed682008-11-06 22:02:51 -05001161
Chris Masond3977122009-01-05 21:25:51 -05001162 if (async_cow->inode)
Chris Mason771ed682008-11-06 22:02:51 -05001163 submit_compressed_extents(async_cow->inode, async_cow);
Chris Mason771ed682008-11-06 22:02:51 -05001164}
Chris Masonc8b97812008-10-29 14:49:59 -04001165
Chris Mason771ed682008-11-06 22:02:51 -05001166static noinline void async_cow_free(struct btrfs_work *work)
1167{
1168 struct async_cow *async_cow;
1169 async_cow = container_of(work, struct async_cow, work);
Josef Bacik8180ef82012-06-08 15:16:12 -04001170 if (async_cow->inode)
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001171 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001172 kfree(async_cow);
1173}
1174
1175static int cow_file_range_async(struct inode *inode, struct page *locked_page,
1176 u64 start, u64 end, int *page_started,
Liu Bof82b7352017-10-23 23:18:16 -06001177 unsigned long *nr_written,
1178 unsigned int write_flags)
Chris Mason771ed682008-11-06 22:02:51 -05001179{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001180 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason771ed682008-11-06 22:02:51 -05001181 struct async_cow *async_cow;
1182 struct btrfs_root *root = BTRFS_I(inode)->root;
1183 unsigned long nr_pages;
1184 u64 cur_end;
Chris Mason771ed682008-11-06 22:02:51 -05001185
Chris Masona3429ab2009-10-08 12:30:20 -04001186 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED,
David Sterbaae0f1622017-10-31 16:37:52 +01001187 1, 0, NULL);
Chris Masond3977122009-01-05 21:25:51 -05001188 while (start < end) {
Chris Mason771ed682008-11-06 22:02:51 -05001189 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001190 BUG_ON(!async_cow); /* -ENOMEM */
Josef Bacik8180ef82012-06-08 15:16:12 -04001191 async_cow->inode = igrab(inode);
Chris Mason771ed682008-11-06 22:02:51 -05001192 async_cow->root = root;
1193 async_cow->locked_page = locked_page;
1194 async_cow->start = start;
Liu Bof82b7352017-10-23 23:18:16 -06001195 async_cow->write_flags = write_flags;
Chris Mason771ed682008-11-06 22:02:51 -05001196
Wang Shilongf79707b2014-07-17 11:44:09 +08001197 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001198 !btrfs_test_opt(fs_info, FORCE_COMPRESS))
Chris Mason771ed682008-11-06 22:02:51 -05001199 cur_end = end;
1200 else
Byongho Leeee221842015-12-15 01:42:10 +09001201 cur_end = min(end, start + SZ_512K - 1);
Chris Mason771ed682008-11-06 22:02:51 -05001202
1203 async_cow->end = cur_end;
1204 INIT_LIST_HEAD(&async_cow->extents);
1205
Liu Bo9e0af232014-08-15 23:36:53 +08001206 btrfs_init_work(&async_cow->work,
1207 btrfs_delalloc_helper,
1208 async_cow_start, async_cow_submit,
1209 async_cow_free);
Chris Mason771ed682008-11-06 22:02:51 -05001210
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001211 nr_pages = (cur_end - start + PAGE_SIZE) >>
1212 PAGE_SHIFT;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001213 atomic_add(nr_pages, &fs_info->async_delalloc_pages);
Chris Mason771ed682008-11-06 22:02:51 -05001214
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001215 btrfs_queue_work(fs_info->delalloc_workers, &async_cow->work);
Chris Mason771ed682008-11-06 22:02:51 -05001216
Chris Mason771ed682008-11-06 22:02:51 -05001217 *nr_written += nr_pages;
1218 start = cur_end + 1;
1219 }
1220 *page_started = 1;
1221 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001222}
1223
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001224static noinline int csum_exist_in_range(struct btrfs_fs_info *fs_info,
Yan Zheng17d217f2008-12-12 10:03:38 -05001225 u64 bytenr, u64 num_bytes)
1226{
1227 int ret;
1228 struct btrfs_ordered_sum *sums;
1229 LIST_HEAD(list);
1230
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001231 ret = btrfs_lookup_csums_range(fs_info->csum_root, bytenr,
Arne Jansena2de7332011-03-08 14:14:00 +01001232 bytenr + num_bytes - 1, &list, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001233 if (ret == 0 && list_empty(&list))
1234 return 0;
1235
1236 while (!list_empty(&list)) {
1237 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1238 list_del(&sums->list);
1239 kfree(sums);
1240 }
Liu Bo58113752018-01-31 17:09:13 -07001241 if (ret < 0)
1242 return ret;
Yan Zheng17d217f2008-12-12 10:03:38 -05001243 return 1;
1244}
1245
Chris Masond352ac62008-09-29 15:18:18 -04001246/*
1247 * when nowcow writeback call back. This checks for snapshots or COW copies
1248 * of the extents that exist in the file, and COWs the file as required.
1249 *
1250 * If no cow copies or snapshots exist, we write directly to the existing
1251 * blocks on disk
1252 */
Chris Mason7f366cf2009-03-12 20:12:45 -04001253static noinline int run_delalloc_nocow(struct inode *inode,
1254 struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001255 u64 start, u64 end, int *page_started, int force,
1256 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001257{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001258 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masonbe20aa92007-12-17 20:14:01 -05001259 struct btrfs_root *root = BTRFS_I(inode)->root;
1260 struct extent_buffer *leaf;
Chris Masonbe20aa92007-12-17 20:14:01 -05001261 struct btrfs_path *path;
Yan Zheng80ff3852008-10-30 14:20:02 -04001262 struct btrfs_file_extent_item *fi;
Chris Masonbe20aa92007-12-17 20:14:01 -05001263 struct btrfs_key found_key;
Liu Bo6f9994d2017-01-31 07:50:22 -08001264 struct extent_map *em;
Yan Zheng80ff3852008-10-30 14:20:02 -04001265 u64 cow_start;
1266 u64 cur_offset;
1267 u64 extent_end;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001268 u64 extent_offset;
Yan Zheng80ff3852008-10-30 14:20:02 -04001269 u64 disk_bytenr;
1270 u64 num_bytes;
Josef Bacikb4939682012-12-03 10:31:19 -05001271 u64 disk_num_bytes;
Josef Bacikcc95bef2013-04-04 14:31:27 -04001272 u64 ram_bytes;
Yan Zheng80ff3852008-10-30 14:20:02 -04001273 int extent_type;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001274 int ret, err;
Yan Zhengd899e052008-10-30 14:25:28 -04001275 int type;
Yan Zheng80ff3852008-10-30 14:20:02 -04001276 int nocow;
1277 int check_prev = 1;
Li Zefan82d59022011-04-20 10:33:24 +08001278 bool nolock;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02001279 u64 ino = btrfs_ino(BTRFS_I(inode));
Chris Masonbe20aa92007-12-17 20:14:01 -05001280
1281 path = btrfs_alloc_path();
Josef Bacik17ca04a2012-05-31 15:58:55 -04001282 if (!path) {
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001283 extent_clear_unlock_delalloc(inode, start, end, end,
1284 locked_page,
Josef Bacikc2790a22013-07-29 11:20:47 -04001285 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -04001286 EXTENT_DO_ACCOUNTING |
1287 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001288 PAGE_CLEAR_DIRTY |
1289 PAGE_SET_WRITEBACK |
1290 PAGE_END_WRITEBACK);
Mark Fashehd8926bb2011-07-13 10:38:47 -07001291 return -ENOMEM;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001292 }
Li Zefan82d59022011-04-20 10:33:24 +08001293
Nikolay Borisov70ddc552017-02-20 13:50:35 +02001294 nolock = btrfs_is_free_space_inode(BTRFS_I(inode));
Li Zefan82d59022011-04-20 10:33:24 +08001295
Yan Zheng80ff3852008-10-30 14:20:02 -04001296 cow_start = (u64)-1;
1297 cur_offset = start;
1298 while (1) {
Liu Boe4c3b2d2017-01-30 12:25:28 -08001299 ret = btrfs_lookup_file_extent(NULL, root, path, ino,
Yan Zheng80ff3852008-10-30 14:20:02 -04001300 cur_offset, 0);
Josef Bacikd788a342013-10-25 16:55:08 -04001301 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001302 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001303 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1304 leaf = path->nodes[0];
1305 btrfs_item_key_to_cpu(leaf, &found_key,
1306 path->slots[0] - 1);
Li Zefan33345d012011-04-20 10:31:50 +08001307 if (found_key.objectid == ino &&
Yan Zheng80ff3852008-10-30 14:20:02 -04001308 found_key.type == BTRFS_EXTENT_DATA_KEY)
1309 path->slots[0]--;
1310 }
1311 check_prev = 0;
1312next_slot:
1313 leaf = path->nodes[0];
1314 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1315 ret = btrfs_next_leaf(root, path);
Liu Boe8916692018-01-25 11:02:50 -07001316 if (ret < 0) {
1317 if (cow_start != (u64)-1)
1318 cur_offset = cow_start;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001319 goto error;
Liu Boe8916692018-01-25 11:02:50 -07001320 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001321 if (ret > 0)
1322 break;
1323 leaf = path->nodes[0];
1324 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001325
Yan Zheng80ff3852008-10-30 14:20:02 -04001326 nocow = 0;
1327 disk_bytenr = 0;
Yan Zheng17d217f2008-12-12 10:03:38 -05001328 num_bytes = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001329 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001330
Filipe Manana1d512cb2015-11-09 00:33:58 +00001331 if (found_key.objectid > ino)
1332 break;
1333 if (WARN_ON_ONCE(found_key.objectid < ino) ||
1334 found_key.type < BTRFS_EXTENT_DATA_KEY) {
1335 path->slots[0]++;
1336 goto next_slot;
1337 }
1338 if (found_key.type > BTRFS_EXTENT_DATA_KEY ||
Yan Zheng80ff3852008-10-30 14:20:02 -04001339 found_key.offset > end)
1340 break;
Chris Masonbe20aa92007-12-17 20:14:01 -05001341
Yan Zheng80ff3852008-10-30 14:20:02 -04001342 if (found_key.offset > cur_offset) {
1343 extent_end = found_key.offset;
Chris Masone9061e22009-10-09 09:57:45 -04001344 extent_type = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001345 goto out_check;
1346 }
Chris Masonc31f8832008-01-08 15:46:31 -05001347
Yan Zheng80ff3852008-10-30 14:20:02 -04001348 fi = btrfs_item_ptr(leaf, path->slots[0],
1349 struct btrfs_file_extent_item);
1350 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001351
Josef Bacikcc95bef2013-04-04 14:31:27 -04001352 ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
Yan Zhengd899e052008-10-30 14:25:28 -04001353 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1354 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001355 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001356 extent_offset = btrfs_file_extent_offset(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001357 extent_end = found_key.offset +
1358 btrfs_file_extent_num_bytes(leaf, fi);
Josef Bacikb4939682012-12-03 10:31:19 -05001359 disk_num_bytes =
1360 btrfs_file_extent_disk_num_bytes(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001361 if (extent_end <= start) {
1362 path->slots[0]++;
1363 goto next_slot;
1364 }
Yan Zheng17d217f2008-12-12 10:03:38 -05001365 if (disk_bytenr == 0)
1366 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001367 if (btrfs_file_extent_compression(leaf, fi) ||
1368 btrfs_file_extent_encryption(leaf, fi) ||
1369 btrfs_file_extent_other_encoding(leaf, fi))
1370 goto out_check;
Ethan Lien78d42952018-05-17 14:58:29 +08001371 /*
1372 * Do the same check as in btrfs_cross_ref_exist but
1373 * without the unnecessary search.
1374 */
1375 if (btrfs_file_extent_generation(leaf, fi) <=
1376 btrfs_root_last_snapshot(&root->root_item))
1377 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001378 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1379 goto out_check;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001380 if (btrfs_extent_readonly(fs_info, disk_bytenr))
Yan Zheng80ff3852008-10-30 14:20:02 -04001381 goto out_check;
Liu Bo58113752018-01-31 17:09:13 -07001382 ret = btrfs_cross_ref_exist(root, ino,
1383 found_key.offset -
1384 extent_offset, disk_bytenr);
1385 if (ret) {
1386 /*
1387 * ret could be -EIO if the above fails to read
1388 * metadata.
1389 */
1390 if (ret < 0) {
1391 if (cow_start != (u64)-1)
1392 cur_offset = cow_start;
1393 goto error;
1394 }
1395
1396 WARN_ON_ONCE(nolock);
Yan Zheng17d217f2008-12-12 10:03:38 -05001397 goto out_check;
Liu Bo58113752018-01-31 17:09:13 -07001398 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001399 disk_bytenr += extent_offset;
Yan Zheng17d217f2008-12-12 10:03:38 -05001400 disk_bytenr += cur_offset - found_key.offset;
1401 num_bytes = min(end + 1, extent_end) - cur_offset;
1402 /*
Wang Shilonge9894fd2014-03-27 11:12:25 +08001403 * if there are pending snapshots for this root,
1404 * we fall into common COW way.
1405 */
1406 if (!nolock) {
David Sterbaea14b57f2017-06-22 02:19:11 +02001407 err = btrfs_start_write_no_snapshotting(root);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001408 if (!err)
1409 goto out_check;
1410 }
1411 /*
Yan Zheng17d217f2008-12-12 10:03:38 -05001412 * force cow if csum exists in the range.
1413 * this ensure that csum for a given extent are
1414 * either valid or do not exist.
1415 */
Liu Bo58113752018-01-31 17:09:13 -07001416 ret = csum_exist_in_range(fs_info, disk_bytenr,
1417 num_bytes);
1418 if (ret) {
Robbie Ko91e1f562016-10-07 10:01:29 +08001419 if (!nolock)
David Sterbaea14b57f2017-06-22 02:19:11 +02001420 btrfs_end_write_no_snapshotting(root);
Liu Bo58113752018-01-31 17:09:13 -07001421
1422 /*
1423 * ret could be -EIO if the above fails to read
1424 * metadata.
1425 */
1426 if (ret < 0) {
1427 if (cow_start != (u64)-1)
1428 cur_offset = cow_start;
1429 goto error;
1430 }
1431 WARN_ON_ONCE(nolock);
Yan Zheng17d217f2008-12-12 10:03:38 -05001432 goto out_check;
Robbie Ko91e1f562016-10-07 10:01:29 +08001433 }
1434 if (!btrfs_inc_nocow_writers(fs_info, disk_bytenr)) {
1435 if (!nolock)
David Sterbaea14b57f2017-06-22 02:19:11 +02001436 btrfs_end_write_no_snapshotting(root);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001437 goto out_check;
Robbie Ko91e1f562016-10-07 10:01:29 +08001438 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001439 nocow = 1;
1440 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1441 extent_end = found_key.offset +
Qu Wenruoe41ca582018-06-06 15:41:49 +08001442 btrfs_file_extent_ram_bytes(leaf, fi);
Jeff Mahoneyda170662016-06-15 09:22:56 -04001443 extent_end = ALIGN(extent_end,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001444 fs_info->sectorsize);
Yan Zheng80ff3852008-10-30 14:20:02 -04001445 } else {
1446 BUG_ON(1);
1447 }
1448out_check:
1449 if (extent_end <= start) {
1450 path->slots[0]++;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001451 if (!nolock && nocow)
David Sterbaea14b57f2017-06-22 02:19:11 +02001452 btrfs_end_write_no_snapshotting(root);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001453 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001454 btrfs_dec_nocow_writers(fs_info, disk_bytenr);
Yan Zheng80ff3852008-10-30 14:20:02 -04001455 goto next_slot;
1456 }
1457 if (!nocow) {
1458 if (cow_start == (u64)-1)
1459 cow_start = cur_offset;
1460 cur_offset = extent_end;
1461 if (cur_offset > end)
1462 break;
1463 path->slots[0]++;
1464 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001465 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001466
David Sterbab3b4aa72011-04-21 01:20:15 +02001467 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001468 if (cow_start != (u64)-1) {
Josef Bacik00361582013-08-14 14:02:47 -04001469 ret = cow_file_range(inode, locked_page,
1470 cow_start, found_key.offset - 1,
Wang Xiaoguangdda32452016-07-11 11:05:29 +08001471 end, page_started, nr_written, 1,
1472 NULL);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001473 if (ret) {
1474 if (!nolock && nocow)
David Sterbaea14b57f2017-06-22 02:19:11 +02001475 btrfs_end_write_no_snapshotting(root);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001476 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001477 btrfs_dec_nocow_writers(fs_info,
Filipe Mananaf78c4362016-05-09 13:15:41 +01001478 disk_bytenr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001479 goto error;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001480 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001481 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001482 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001483
Yan Zhengd899e052008-10-30 14:25:28 -04001484 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Liu Bo6f9994d2017-01-31 07:50:22 -08001485 u64 orig_start = found_key.offset - extent_offset;
1486
1487 em = create_io_em(inode, cur_offset, num_bytes,
1488 orig_start,
1489 disk_bytenr, /* block_start */
1490 num_bytes, /* block_len */
1491 disk_num_bytes, /* orig_block_len */
1492 ram_bytes, BTRFS_COMPRESS_NONE,
1493 BTRFS_ORDERED_PREALLOC);
1494 if (IS_ERR(em)) {
1495 if (!nolock && nocow)
David Sterbaea14b57f2017-06-22 02:19:11 +02001496 btrfs_end_write_no_snapshotting(root);
Liu Bo6f9994d2017-01-31 07:50:22 -08001497 if (nocow)
1498 btrfs_dec_nocow_writers(fs_info,
1499 disk_bytenr);
1500 ret = PTR_ERR(em);
1501 goto error;
Yan Zhengd899e052008-10-30 14:25:28 -04001502 }
Liu Bo6f9994d2017-01-31 07:50:22 -08001503 free_extent_map(em);
1504 }
1505
1506 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zhengd899e052008-10-30 14:25:28 -04001507 type = BTRFS_ORDERED_PREALLOC;
1508 } else {
1509 type = BTRFS_ORDERED_NOCOW;
1510 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001511
1512 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
Yan Zhengd899e052008-10-30 14:25:28 -04001513 num_bytes, num_bytes, type);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001514 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001515 btrfs_dec_nocow_writers(fs_info, disk_bytenr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001516 BUG_ON(ret); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -05001517
Yan, Zhengefa56462010-05-16 10:49:59 -04001518 if (root->root_key.objectid ==
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001519 BTRFS_DATA_RELOC_TREE_OBJECTID)
1520 /*
1521 * Error handled later, as we must prevent
1522 * extent_clear_unlock_delalloc() in error handler
1523 * from freeing metadata of created ordered extent.
1524 */
Yan, Zhengefa56462010-05-16 10:49:59 -04001525 ret = btrfs_reloc_clone_csums(inode, cur_offset,
1526 num_bytes);
Yan, Zhengefa56462010-05-16 10:49:59 -04001527
Josef Bacikc2790a22013-07-29 11:20:47 -04001528 extent_clear_unlock_delalloc(inode, cur_offset,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001529 cur_offset + num_bytes - 1, end,
Josef Bacikc2790a22013-07-29 11:20:47 -04001530 locked_page, EXTENT_LOCKED |
Wang Xiaoguang18513092016-07-25 15:51:40 +08001531 EXTENT_DELALLOC |
1532 EXTENT_CLEAR_DATA_RESV,
1533 PAGE_UNLOCK | PAGE_SET_PRIVATE2);
1534
Wang Shilonge9894fd2014-03-27 11:12:25 +08001535 if (!nolock && nocow)
David Sterbaea14b57f2017-06-22 02:19:11 +02001536 btrfs_end_write_no_snapshotting(root);
Yan Zheng80ff3852008-10-30 14:20:02 -04001537 cur_offset = extent_end;
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001538
1539 /*
1540 * btrfs_reloc_clone_csums() error, now we're OK to call error
1541 * handler, as metadata for created ordered extent will only
1542 * be freed by btrfs_finish_ordered_io().
1543 */
1544 if (ret)
1545 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001546 if (cur_offset > end)
1547 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001548 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001549 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001550
Josef Bacik17ca04a2012-05-31 15:58:55 -04001551 if (cur_offset <= end && cow_start == (u64)-1) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001552 cow_start = cur_offset;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001553 cur_offset = end;
1554 }
1555
Yan Zheng80ff3852008-10-30 14:20:02 -04001556 if (cow_start != (u64)-1) {
Wang Xiaoguangdda32452016-07-11 11:05:29 +08001557 ret = cow_file_range(inode, locked_page, cow_start, end, end,
1558 page_started, nr_written, 1, NULL);
Josef Bacikd788a342013-10-25 16:55:08 -04001559 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001560 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001561 }
1562
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001563error:
Josef Bacik17ca04a2012-05-31 15:58:55 -04001564 if (ret && cur_offset < end)
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001565 extent_clear_unlock_delalloc(inode, cur_offset, end, end,
Josef Bacikc2790a22013-07-29 11:20:47 -04001566 locked_page, EXTENT_LOCKED |
Josef Bacik151a41b2013-07-29 13:22:24 -04001567 EXTENT_DELALLOC | EXTENT_DEFRAG |
1568 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
1569 PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -04001570 PAGE_SET_WRITEBACK |
1571 PAGE_END_WRITEBACK);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001572 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001573 return ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001574}
1575
Wang Shilong47059d92014-07-03 18:22:07 +08001576static inline int need_force_cow(struct inode *inode, u64 start, u64 end)
1577{
1578
1579 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
1580 !(BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC))
1581 return 0;
1582
1583 /*
1584 * @defrag_bytes is a hint value, no spinlock held here,
1585 * if is not zero, it means the file is defragging.
1586 * Force cow if given extent needs to be defragged.
1587 */
1588 if (BTRFS_I(inode)->defrag_bytes &&
1589 test_range_bit(&BTRFS_I(inode)->io_tree, start, end,
1590 EXTENT_DEFRAG, 0, NULL))
1591 return 1;
1592
1593 return 0;
1594}
1595
Chris Masond352ac62008-09-29 15:18:18 -04001596/*
1597 * extent_io.c call back to do delayed allocation processing
1598 */
Josef Bacikc6100a42017-05-05 11:57:13 -04001599static int run_delalloc_range(void *private_data, struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001600 u64 start, u64 end, int *page_started,
Liu Bof82b7352017-10-23 23:18:16 -06001601 unsigned long *nr_written,
1602 struct writeback_control *wbc)
Chris Masonbe20aa92007-12-17 20:14:01 -05001603{
Josef Bacikc6100a42017-05-05 11:57:13 -04001604 struct inode *inode = private_data;
Chris Masonbe20aa92007-12-17 20:14:01 -05001605 int ret;
Wang Shilong47059d92014-07-03 18:22:07 +08001606 int force_cow = need_force_cow(inode, start, end);
Liu Bof82b7352017-10-23 23:18:16 -06001607 unsigned int write_flags = wbc_to_write_flags(wbc);
Chris Masona2135012008-06-25 16:01:30 -04001608
Wang Shilong47059d92014-07-03 18:22:07 +08001609 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW && !force_cow) {
Chris Masonc8b97812008-10-29 14:49:59 -04001610 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001611 page_started, 1, nr_written);
Wang Shilong47059d92014-07-03 18:22:07 +08001612 } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC && !force_cow) {
Yan Zhengd899e052008-10-30 14:25:28 -04001613 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001614 page_started, 0, nr_written);
Timofey Titovetsc2fcdcd2017-07-17 16:52:58 +03001615 } else if (!inode_need_compress(inode, start, end)) {
Wang Xiaoguangdda32452016-07-11 11:05:29 +08001616 ret = cow_file_range(inode, locked_page, start, end, end,
1617 page_started, nr_written, 1, NULL);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001618 } else {
1619 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1620 &BTRFS_I(inode)->runtime_flags);
Chris Mason771ed682008-11-06 22:02:51 -05001621 ret = cow_file_range_async(inode, locked_page, start, end,
Liu Bof82b7352017-10-23 23:18:16 -06001622 page_started, nr_written,
1623 write_flags);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001624 }
Qu Wenruo524272602017-03-08 10:25:52 +08001625 if (ret)
1626 btrfs_cleanup_ordered_extents(inode, start, end - start + 1);
Chris Masonb888db22007-08-27 16:49:44 -04001627 return ret;
1628}
1629
Josef Bacikc6100a42017-05-05 11:57:13 -04001630static void btrfs_split_extent_hook(void *private_data,
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001631 struct extent_state *orig, u64 split)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001632{
Josef Bacikc6100a42017-05-05 11:57:13 -04001633 struct inode *inode = private_data;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001634 u64 size;
1635
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001636 /* not delalloc, ignore it */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001637 if (!(orig->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001638 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001639
Josef Bacikdcab6a32015-02-11 15:08:59 -05001640 size = orig->end - orig->start + 1;
1641 if (size > BTRFS_MAX_EXTENT_SIZE) {
David Sterba823bb202017-01-04 11:09:51 +01001642 u32 num_extents;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001643 u64 new_size;
1644
1645 /*
Josef Bacikba117212015-03-13 15:01:24 -04001646 * See the explanation in btrfs_merge_extent_hook, the same
1647 * applies here, just in reverse.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001648 */
1649 new_size = orig->end - split + 1;
David Sterba823bb202017-01-04 11:09:51 +01001650 num_extents = count_max_extents(new_size);
Josef Bacikba117212015-03-13 15:01:24 -04001651 new_size = split - orig->start;
David Sterba823bb202017-01-04 11:09:51 +01001652 num_extents += count_max_extents(new_size);
1653 if (count_max_extents(size) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001654 return;
1655 }
1656
Josef Bacik9e0baf62011-07-15 15:16:44 +00001657 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04001658 btrfs_mod_outstanding_extents(BTRFS_I(inode), 1);
Josef Bacik9e0baf62011-07-15 15:16:44 +00001659 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001660}
1661
1662/*
1663 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1664 * extents so we can keep track of new extents that are just merged onto old
1665 * extents, such as when we are doing sequential writes, so we can properly
1666 * account for the metadata space we'll need.
1667 */
Josef Bacikc6100a42017-05-05 11:57:13 -04001668static void btrfs_merge_extent_hook(void *private_data,
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001669 struct extent_state *new,
1670 struct extent_state *other)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001671{
Josef Bacikc6100a42017-05-05 11:57:13 -04001672 struct inode *inode = private_data;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001673 u64 new_size, old_size;
David Sterba823bb202017-01-04 11:09:51 +01001674 u32 num_extents;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001675
Josef Bacik9ed74f22009-09-11 16:12:44 -04001676 /* not delalloc, ignore it */
1677 if (!(other->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001678 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001679
Josef Bacik8461a3d2015-03-13 15:12:08 -04001680 if (new->start > other->start)
1681 new_size = new->end - other->start + 1;
1682 else
1683 new_size = other->end - new->start + 1;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001684
1685 /* we're not bigger than the max, unreserve the space and go */
1686 if (new_size <= BTRFS_MAX_EXTENT_SIZE) {
1687 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04001688 btrfs_mod_outstanding_extents(BTRFS_I(inode), -1);
Josef Bacikdcab6a32015-02-11 15:08:59 -05001689 spin_unlock(&BTRFS_I(inode)->lock);
1690 return;
1691 }
1692
1693 /*
Josef Bacikba117212015-03-13 15:01:24 -04001694 * We have to add up either side to figure out how many extents were
1695 * accounted for before we merged into one big extent. If the number of
1696 * extents we accounted for is <= the amount we need for the new range
1697 * then we can return, otherwise drop. Think of it like this
1698 *
1699 * [ 4k][MAX_SIZE]
1700 *
1701 * So we've grown the extent by a MAX_SIZE extent, this would mean we
1702 * need 2 outstanding extents, on one side we have 1 and the other side
1703 * we have 1 so they are == and we can return. But in this case
1704 *
1705 * [MAX_SIZE+4k][MAX_SIZE+4k]
1706 *
1707 * Each range on their own accounts for 2 extents, but merged together
1708 * they are only 3 extents worth of accounting, so we need to drop in
1709 * this case.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001710 */
Josef Bacikba117212015-03-13 15:01:24 -04001711 old_size = other->end - other->start + 1;
David Sterba823bb202017-01-04 11:09:51 +01001712 num_extents = count_max_extents(old_size);
Josef Bacikba117212015-03-13 15:01:24 -04001713 old_size = new->end - new->start + 1;
David Sterba823bb202017-01-04 11:09:51 +01001714 num_extents += count_max_extents(old_size);
1715 if (count_max_extents(new_size) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001716 return;
1717
Josef Bacik9e0baf62011-07-15 15:16:44 +00001718 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04001719 btrfs_mod_outstanding_extents(BTRFS_I(inode), -1);
Josef Bacik9e0baf62011-07-15 15:16:44 +00001720 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001721}
1722
Miao Xieeb73c1b2013-05-15 07:48:22 +00001723static void btrfs_add_delalloc_inodes(struct btrfs_root *root,
1724 struct inode *inode)
1725{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001726 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1727
Miao Xieeb73c1b2013-05-15 07:48:22 +00001728 spin_lock(&root->delalloc_lock);
1729 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1730 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1731 &root->delalloc_inodes);
1732 set_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1733 &BTRFS_I(inode)->runtime_flags);
1734 root->nr_delalloc_inodes++;
1735 if (root->nr_delalloc_inodes == 1) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001736 spin_lock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001737 BUG_ON(!list_empty(&root->delalloc_root));
1738 list_add_tail(&root->delalloc_root,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001739 &fs_info->delalloc_roots);
1740 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001741 }
1742 }
1743 spin_unlock(&root->delalloc_lock);
1744}
1745
Nikolay Borisov2b877332018-04-27 12:21:51 +03001746
1747void __btrfs_del_delalloc_inode(struct btrfs_root *root,
1748 struct btrfs_inode *inode)
Miao Xieeb73c1b2013-05-15 07:48:22 +00001749{
David Sterba3ffbd682018-06-29 10:56:42 +02001750 struct btrfs_fs_info *fs_info = root->fs_info;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001751
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001752 if (!list_empty(&inode->delalloc_inodes)) {
1753 list_del_init(&inode->delalloc_inodes);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001754 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001755 &inode->runtime_flags);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001756 root->nr_delalloc_inodes--;
1757 if (!root->nr_delalloc_inodes) {
Nikolay Borisov7c8a0d32018-04-27 12:21:52 +03001758 ASSERT(list_empty(&root->delalloc_inodes));
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001759 spin_lock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001760 BUG_ON(list_empty(&root->delalloc_root));
1761 list_del_init(&root->delalloc_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001762 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001763 }
1764 }
Nikolay Borisov2b877332018-04-27 12:21:51 +03001765}
1766
1767static void btrfs_del_delalloc_inode(struct btrfs_root *root,
1768 struct btrfs_inode *inode)
1769{
1770 spin_lock(&root->delalloc_lock);
1771 __btrfs_del_delalloc_inode(root, inode);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001772 spin_unlock(&root->delalloc_lock);
1773}
1774
Chris Masond352ac62008-09-29 15:18:18 -04001775/*
1776 * extent_io.c set_bit_hook, used to track delayed allocation
1777 * bytes in this file, and to maintain the list of inodes that
1778 * have pending delalloc work to be done.
1779 */
Josef Bacikc6100a42017-05-05 11:57:13 -04001780static void btrfs_set_bit_hook(void *private_data,
David Sterba9ee49a042015-01-14 19:52:13 +01001781 struct extent_state *state, unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001782{
Josef Bacikc6100a42017-05-05 11:57:13 -04001783 struct inode *inode = private_data;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001784
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001785 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1786
Wang Shilong47059d92014-07-03 18:22:07 +08001787 if ((*bits & EXTENT_DEFRAG) && !(*bits & EXTENT_DELALLOC))
1788 WARN_ON(1);
Chris Mason75eff682008-12-15 15:54:40 -05001789 /*
1790 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001791 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001792 * bit, which is only set or cleared with irqs on
1793 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001794 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001795 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001796 u64 len = state->end + 1 - state->start;
Josef Bacik8b62f872017-10-19 14:15:55 -04001797 u32 num_extents = count_max_extents(len);
Nikolay Borisov70ddc552017-02-20 13:50:35 +02001798 bool do_list = !btrfs_is_free_space_inode(BTRFS_I(inode));
Josef Bacik9ed74f22009-09-11 16:12:44 -04001799
Josef Bacik8b62f872017-10-19 14:15:55 -04001800 spin_lock(&BTRFS_I(inode)->lock);
1801 btrfs_mod_outstanding_extents(BTRFS_I(inode), num_extents);
1802 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik287a0ab2010-03-19 18:07:23 +00001803
Josef Bacik6a3891c2015-03-16 17:38:52 -04001804 /* For sanity tests */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001805 if (btrfs_is_testing(fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04001806 return;
1807
Nikolay Borisov104b4e52017-06-20 21:01:20 +03001808 percpu_counter_add_batch(&fs_info->delalloc_bytes, len,
1809 fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001810 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001811 BTRFS_I(inode)->delalloc_bytes += len;
Wang Shilong47059d92014-07-03 18:22:07 +08001812 if (*bits & EXTENT_DEFRAG)
1813 BTRFS_I(inode)->defrag_bytes += len;
Miao Xiedf0af1a2013-01-29 10:11:59 +00001814 if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00001815 &BTRFS_I(inode)->runtime_flags))
1816 btrfs_add_delalloc_inodes(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001817 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001818 }
Filipe Mananaa7e3b972017-04-03 10:45:46 +01001819
1820 if (!(state->state & EXTENT_DELALLOC_NEW) &&
1821 (*bits & EXTENT_DELALLOC_NEW)) {
1822 spin_lock(&BTRFS_I(inode)->lock);
1823 BTRFS_I(inode)->new_delalloc_bytes += state->end + 1 -
1824 state->start;
1825 spin_unlock(&BTRFS_I(inode)->lock);
1826 }
Chris Mason291d6732008-01-29 15:55:23 -05001827}
1828
Chris Masond352ac62008-09-29 15:18:18 -04001829/*
1830 * extent_io.c clear_bit_hook, see set_bit_hook for why
1831 */
Josef Bacikc6100a42017-05-05 11:57:13 -04001832static void btrfs_clear_bit_hook(void *private_data,
David Sterba41074882013-04-29 13:38:46 +00001833 struct extent_state *state,
David Sterba9ee49a042015-01-14 19:52:13 +01001834 unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001835{
Josef Bacikc6100a42017-05-05 11:57:13 -04001836 struct btrfs_inode *inode = BTRFS_I((struct inode *)private_data);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001837 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
Wang Shilong47059d92014-07-03 18:22:07 +08001838 u64 len = state->end + 1 - state->start;
David Sterba823bb202017-01-04 11:09:51 +01001839 u32 num_extents = count_max_extents(len);
Wang Shilong47059d92014-07-03 18:22:07 +08001840
Filipe Manana4a4b9642017-07-27 19:52:55 +01001841 if ((state->state & EXTENT_DEFRAG) && (*bits & EXTENT_DEFRAG)) {
1842 spin_lock(&inode->lock);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001843 inode->defrag_bytes -= len;
Filipe Manana4a4b9642017-07-27 19:52:55 +01001844 spin_unlock(&inode->lock);
1845 }
Wang Shilong47059d92014-07-03 18:22:07 +08001846
Chris Mason75eff682008-12-15 15:54:40 -05001847 /*
1848 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001849 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001850 * bit, which is only set or cleared with irqs on
1851 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001852 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001853 struct btrfs_root *root = inode->root;
Liu Bo83eea1f2012-07-10 05:28:39 -06001854 bool do_list = !btrfs_is_free_space_inode(inode);
Chris Masonbcbfce82008-04-22 13:26:47 -04001855
Josef Bacik8b62f872017-10-19 14:15:55 -04001856 spin_lock(&inode->lock);
1857 btrfs_mod_outstanding_extents(inode, -num_extents);
1858 spin_unlock(&inode->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001859
Josef Bacikb6d08f02013-09-27 14:57:43 -04001860 /*
1861 * We don't reserve metadata space for space cache inodes so we
1862 * don't need to call dellalloc_release_metadata if there is an
1863 * error.
1864 */
Filipe Mananaa315e682017-03-06 23:04:20 +00001865 if (*bits & EXTENT_CLEAR_META_RESV &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001866 root != fs_info->tree_root)
Qu Wenruo43b18592017-12-12 15:34:32 +08001867 btrfs_delalloc_release_metadata(inode, len, false);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001868
Josef Bacik6a3891c2015-03-16 17:38:52 -04001869 /* For sanity tests. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001870 if (btrfs_is_testing(fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04001871 return;
1872
Filipe Mananaa315e682017-03-06 23:04:20 +00001873 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID &&
1874 do_list && !(state->state & EXTENT_NORESERVE) &&
1875 (*bits & EXTENT_CLEAR_DATA_RESV))
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001876 btrfs_free_reserved_data_space_noquota(
1877 &inode->vfs_inode,
Qu Wenruo51773be2015-10-08 18:19:37 +08001878 state->start, len);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001879
Nikolay Borisov104b4e52017-06-20 21:01:20 +03001880 percpu_counter_add_batch(&fs_info->delalloc_bytes, -len,
1881 fs_info->delalloc_batch);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001882 spin_lock(&inode->lock);
1883 inode->delalloc_bytes -= len;
1884 if (do_list && inode->delalloc_bytes == 0 &&
Miao Xiedf0af1a2013-01-29 10:11:59 +00001885 test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001886 &inode->runtime_flags))
Miao Xieeb73c1b2013-05-15 07:48:22 +00001887 btrfs_del_delalloc_inode(root, inode);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001888 spin_unlock(&inode->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001889 }
Filipe Mananaa7e3b972017-04-03 10:45:46 +01001890
1891 if ((state->state & EXTENT_DELALLOC_NEW) &&
1892 (*bits & EXTENT_DELALLOC_NEW)) {
1893 spin_lock(&inode->lock);
1894 ASSERT(inode->new_delalloc_bytes >= len);
1895 inode->new_delalloc_bytes -= len;
1896 spin_unlock(&inode->lock);
1897 }
Chris Mason291d6732008-01-29 15:55:23 -05001898}
1899
Chris Masond352ac62008-09-29 15:18:18 -04001900/*
David Sterba00032d32018-07-18 19:28:09 +02001901 * Merge bio hook, this must check the chunk tree to make sure we don't create
1902 * bios that span stripes or chunks
Liu Bo6f034ec2016-06-22 18:31:49 -07001903 *
1904 * return 1 if page cannot be merged to bio
1905 * return 0 if page can be merged to bio
1906 * return error otherwise
Chris Masond352ac62008-09-29 15:18:18 -04001907 */
Mike Christie81a75f672016-06-05 14:31:54 -05001908int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
Chris Masonc8b97812008-10-29 14:49:59 -04001909 size_t size, struct bio *bio,
1910 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04001911{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001912 struct inode *inode = page->mapping->host;
1913 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001914 u64 logical = (u64)bio->bi_iter.bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04001915 u64 length = 0;
1916 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04001917 int ret;
1918
Chris Mason771ed682008-11-06 22:02:51 -05001919 if (bio_flags & EXTENT_BIO_COMPRESSED)
1920 return 0;
1921
Kent Overstreet4f024f32013-10-11 15:44:27 -07001922 length = bio->bi_iter.bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04001923 map_length = length;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001924 ret = btrfs_map_block(fs_info, btrfs_op(bio), logical, &map_length,
1925 NULL, 0);
Liu Bo6f034ec2016-06-22 18:31:49 -07001926 if (ret < 0)
1927 return ret;
Chris Masond3977122009-01-05 21:25:51 -05001928 if (map_length < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04001929 return 1;
Jeff Mahoney3444a972011-10-03 23:23:13 -04001930 return 0;
Chris Mason239b14b2008-03-24 15:02:07 -04001931}
1932
Chris Masond352ac62008-09-29 15:18:18 -04001933/*
1934 * in order to insert checksums into the metadata in large chunks,
1935 * we wait until bio submission time. All the pages in the bio are
1936 * checksummed and sums are attached onto the ordered extent record.
1937 *
1938 * At IO completion time the cums attached on the ordered extent record
1939 * are inserted into the btree
1940 */
David Sterbad0ee3932018-03-08 14:35:48 +01001941static blk_status_t btrfs_submit_bio_start(void *private_data, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001942 u64 bio_offset)
Chris Mason065631f2008-02-20 12:07:25 -05001943{
Josef Bacikc6100a42017-05-05 11:57:13 -04001944 struct inode *inode = private_data;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001945 blk_status_t ret = 0;
Chris Masone0156402008-04-16 11:15:20 -04001946
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001947 ret = btrfs_csum_one_bio(inode, bio, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001948 BUG_ON(ret); /* -ENOMEM */
Chris Mason4a69a412008-11-06 22:03:00 -05001949 return 0;
1950}
Chris Masone0156402008-04-16 11:15:20 -04001951
Chris Mason4a69a412008-11-06 22:03:00 -05001952/*
1953 * in order to insert checksums into the metadata in large chunks,
1954 * we wait until bio submission time. All the pages in the bio are
1955 * checksummed and sums are attached onto the ordered extent record.
1956 *
1957 * At IO completion time the cums attached on the ordered extent record
1958 * are inserted into the btree
1959 */
David Sterbae288c082018-07-18 17:36:24 +02001960blk_status_t btrfs_submit_bio_done(void *private_data, struct bio *bio,
David Sterba6c553432018-03-08 13:47:33 +01001961 int mirror_num)
Chris Mason4a69a412008-11-06 22:03:00 -05001962{
Josef Bacikc6100a42017-05-05 11:57:13 -04001963 struct inode *inode = private_data;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001964 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001965 blk_status_t ret;
Stefan Behrens61891922012-11-05 18:51:52 +01001966
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001967 ret = btrfs_map_bio(fs_info, bio, mirror_num, 1);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001968 if (ret) {
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001969 bio->bi_status = ret;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001970 bio_endio(bio);
1971 }
Stefan Behrens61891922012-11-05 18:51:52 +01001972 return ret;
Chris Mason44b8bd72008-04-16 11:14:51 -04001973}
1974
Chris Masond352ac62008-09-29 15:18:18 -04001975/*
Chris Masoncad321a2008-12-17 14:51:42 -05001976 * extent_io.c submission hook. This does the right thing for csum calculation
Liu Bo4c274bc2017-11-01 17:19:27 -06001977 * on write, or reading the csums from the tree before a read.
1978 *
1979 * Rules about async/sync submit,
1980 * a) read: sync submit
1981 *
1982 * b) write without checksum: sync submit
1983 *
1984 * c) write with checksum:
1985 * c-1) if bio is issued by fsync: sync submit
1986 * (sync_writers != 0)
1987 *
1988 * c-2) if root is reloc root: sync submit
1989 * (only in case of buffered IO)
1990 *
1991 * c-3) otherwise: async submit
Chris Masond352ac62008-09-29 15:18:18 -04001992 */
Linus Torvalds8c27cb32017-07-05 16:41:23 -07001993static blk_status_t btrfs_submit_bio_hook(void *private_data, struct bio *bio,
Josef Bacikc6100a42017-05-05 11:57:13 -04001994 int mirror_num, unsigned long bio_flags,
1995 u64 bio_offset)
Chris Mason44b8bd72008-04-16 11:14:51 -04001996{
Josef Bacikc6100a42017-05-05 11:57:13 -04001997 struct inode *inode = private_data;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001998 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason44b8bd72008-04-16 11:14:51 -04001999 struct btrfs_root *root = BTRFS_I(inode)->root;
Chandan Rajendra0d51e282015-07-27 15:26:43 +05302000 enum btrfs_wq_endio_type metadata = BTRFS_WQ_ENDIO_DATA;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002001 blk_status_t ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04002002 int skip_sum;
Josef Bacikb812ce22012-11-16 13:56:32 -05002003 int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
Chris Mason44b8bd72008-04-16 11:14:51 -04002004
Christoph Hellwig6cbff002009-04-17 10:37:41 +02002005 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masoncad321a2008-12-17 14:51:42 -05002006
Nikolay Borisov70ddc552017-02-20 13:50:35 +02002007 if (btrfs_is_free_space_inode(BTRFS_I(inode)))
Chandan Rajendra0d51e282015-07-27 15:26:43 +05302008 metadata = BTRFS_WQ_ENDIO_FREE_SPACE;
Jeff Mahoney04173412011-10-03 23:23:12 -04002009
Mike Christie37226b22016-06-05 14:31:52 -05002010 if (bio_op(bio) != REQ_OP_WRITE) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002011 ret = btrfs_bio_wq_end_io(fs_info, bio, metadata);
Josef Bacik5fd02042012-05-02 14:00:54 -04002012 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01002013 goto out;
Josef Bacik5fd02042012-05-02 14:00:54 -04002014
Chris Masond20f7042008-12-08 16:58:54 -05002015 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Stefan Behrens61891922012-11-05 18:51:52 +01002016 ret = btrfs_submit_compressed_read(inode, bio,
2017 mirror_num,
2018 bio_flags);
2019 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00002020 } else if (!skip_sum) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002021 ret = btrfs_lookup_bio_sums(inode, bio, NULL);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00002022 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01002023 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00002024 }
Chris Mason4d1b5fb2008-08-20 09:44:52 -04002025 goto mapit;
Josef Bacikb812ce22012-11-16 13:56:32 -05002026 } else if (async && !skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05002027 /* csum items have already been cloned */
2028 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
2029 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04002030 /* we're doing a write, do the async checksumming */
Josef Bacikc6100a42017-05-05 11:57:13 -04002031 ret = btrfs_wq_submit_bio(fs_info, bio, mirror_num, bio_flags,
2032 bio_offset, inode,
David Sterbae288c082018-07-18 17:36:24 +02002033 btrfs_submit_bio_start);
Stefan Behrens61891922012-11-05 18:51:52 +01002034 goto out;
Josef Bacikb812ce22012-11-16 13:56:32 -05002035 } else if (!skip_sum) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002036 ret = btrfs_csum_one_bio(inode, bio, 0, 0);
Josef Bacikb812ce22012-11-16 13:56:32 -05002037 if (ret)
2038 goto out;
Chris Mason19b9bdb2008-10-30 14:23:13 -04002039 }
2040
Chris Mason0b86a832008-03-24 15:01:56 -04002041mapit:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002042 ret = btrfs_map_bio(fs_info, bio, mirror_num, 0);
Stefan Behrens61891922012-11-05 18:51:52 +01002043
2044out:
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002045 if (ret) {
2046 bio->bi_status = ret;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002047 bio_endio(bio);
2048 }
Stefan Behrens61891922012-11-05 18:51:52 +01002049 return ret;
Chris Mason065631f2008-02-20 12:07:25 -05002050}
Chris Mason6885f302008-02-20 16:11:05 -05002051
Chris Masond352ac62008-09-29 15:18:18 -04002052/*
2053 * given a list of ordered sums record them in the inode. This happens
2054 * at IO completion time based on sums calculated at bio submission time.
2055 */
Chris Masonba1da2f2008-07-17 12:54:15 -04002056static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
David Sterbadf9f6282017-02-10 19:35:37 +01002057 struct inode *inode, struct list_head *list)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002058{
Chris Masone6dcd2d2008-07-17 12:53:50 -04002059 struct btrfs_ordered_sum *sum;
Nikolay Borisovac01f262018-01-08 10:59:43 +02002060 int ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002061
Qinghuang Fengc6e30872009-01-21 10:59:08 -05002062 list_for_each_entry(sum, list, list) {
David Sterba7c2871a2017-11-08 01:07:43 +01002063 trans->adding_csums = true;
Nikolay Borisovac01f262018-01-08 10:59:43 +02002064 ret = btrfs_csum_file_blocks(trans,
Chris Masond20f7042008-12-08 16:58:54 -05002065 BTRFS_I(inode)->root->fs_info->csum_root, sum);
David Sterba7c2871a2017-11-08 01:07:43 +01002066 trans->adding_csums = false;
Nikolay Borisovac01f262018-01-08 10:59:43 +02002067 if (ret)
2068 return ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002069 }
2070 return 0;
2071}
2072
Josef Bacik2ac55d42010-02-03 19:33:23 +00002073int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
Filipe Mananae3b8a482017-11-04 00:16:59 +00002074 unsigned int extra_bits,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08002075 struct extent_state **cached_state, int dedupe)
Chris Masonea8c2812008-08-04 23:17:27 -04002076{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002077 WARN_ON((end & (PAGE_SIZE - 1)) == 0);
Chris Masonea8c2812008-08-04 23:17:27 -04002078 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
Filipe Mananae3b8a482017-11-04 00:16:59 +00002079 extra_bits, cached_state);
Chris Masonea8c2812008-08-04 23:17:27 -04002080}
2081
Chris Masond352ac62008-09-29 15:18:18 -04002082/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04002083struct btrfs_writepage_fixup {
2084 struct page *page;
2085 struct btrfs_work work;
2086};
2087
Christoph Hellwigb2950862008-12-02 09:54:17 -05002088static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04002089{
2090 struct btrfs_writepage_fixup *fixup;
2091 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002092 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08002093 struct extent_changeset *data_reserved = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04002094 struct page *page;
2095 struct inode *inode;
2096 u64 page_start;
2097 u64 page_end;
Jeff Mahoney87826df2012-02-15 16:23:57 +01002098 int ret;
Chris Mason247e7432008-07-17 12:53:51 -04002099
2100 fixup = container_of(work, struct btrfs_writepage_fixup, work);
2101 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -04002102again:
Chris Mason247e7432008-07-17 12:53:51 -04002103 lock_page(page);
2104 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
2105 ClearPageChecked(page);
2106 goto out_page;
2107 }
2108
2109 inode = page->mapping->host;
2110 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002111 page_end = page_offset(page) + PAGE_SIZE - 1;
Chris Mason247e7432008-07-17 12:53:51 -04002112
David Sterbaff13db42015-12-03 14:30:40 +01002113 lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002114 &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04002115
2116 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04002117 if (PagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04002118 goto out;
Chris Mason4a096752008-07-21 10:29:44 -04002119
Nikolay Borisova776c6f2017-02-20 13:50:49 +02002120 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002121 PAGE_SIZE);
Chris Mason4a096752008-07-21 10:29:44 -04002122 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00002123 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
David Sterbae43bbe52017-12-12 21:43:52 +01002124 page_end, &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04002125 unlock_page(page);
2126 btrfs_start_ordered_extent(inode, ordered, 1);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002127 btrfs_put_ordered_extent(ordered);
Chris Mason4a096752008-07-21 10:29:44 -04002128 goto again;
2129 }
Chris Mason247e7432008-07-17 12:53:51 -04002130
Qu Wenruo364ecf32017-02-27 15:10:38 +08002131 ret = btrfs_delalloc_reserve_space(inode, &data_reserved, page_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002132 PAGE_SIZE);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002133 if (ret) {
2134 mapping_set_error(page->mapping, ret);
2135 end_extent_writepage(page, ret, page_start, page_end);
2136 ClearPageChecked(page);
2137 goto out;
2138 }
2139
Nikolay Borisovf3038ee2017-12-05 09:29:19 +02002140 ret = btrfs_set_extent_delalloc(inode, page_start, page_end, 0,
2141 &cached_state, 0);
2142 if (ret) {
2143 mapping_set_error(page->mapping, ret);
2144 end_extent_writepage(page, ret, page_start, page_end);
2145 ClearPageChecked(page);
2146 goto out;
2147 }
2148
Chris Mason247e7432008-07-17 12:53:51 -04002149 ClearPageChecked(page);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002150 set_page_dirty(page);
Qu Wenruo43b18592017-12-12 15:34:32 +08002151 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE, false);
Chris Mason247e7432008-07-17 12:53:51 -04002152out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00002153 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01002154 &cached_state);
Chris Mason247e7432008-07-17 12:53:51 -04002155out_page:
2156 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002157 put_page(page);
Miao Xieb897abe2011-01-26 16:19:22 +08002158 kfree(fixup);
Qu Wenruo364ecf32017-02-27 15:10:38 +08002159 extent_changeset_free(data_reserved);
Chris Mason247e7432008-07-17 12:53:51 -04002160}
2161
2162/*
2163 * There are a few paths in the higher layers of the kernel that directly
2164 * set the page dirty bit without asking the filesystem if it is a
2165 * good idea. This causes problems because we want to make sure COW
2166 * properly happens and the data=ordered rules are followed.
2167 *
Chris Masonc8b97812008-10-29 14:49:59 -04002168 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04002169 * hasn't been properly setup for IO. We kick off an async process
2170 * to fix it up. The async helper will wait for ordered extents, set
2171 * the delalloc bit and make it safe to write the page.
2172 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05002173static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04002174{
2175 struct inode *inode = page->mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002176 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason247e7432008-07-17 12:53:51 -04002177 struct btrfs_writepage_fixup *fixup;
Chris Mason247e7432008-07-17 12:53:51 -04002178
Chris Mason8b62b722009-09-02 16:53:46 -04002179 /* this page is properly in the ordered list */
2180 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04002181 return 0;
2182
2183 if (PageChecked(page))
2184 return -EAGAIN;
2185
2186 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
2187 if (!fixup)
2188 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04002189
Chris Mason247e7432008-07-17 12:53:51 -04002190 SetPageChecked(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002191 get_page(page);
Liu Bo9e0af232014-08-15 23:36:53 +08002192 btrfs_init_work(&fixup->work, btrfs_fixup_helper,
2193 btrfs_writepage_fixup_worker, NULL, NULL);
Chris Mason247e7432008-07-17 12:53:51 -04002194 fixup->page = page;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002195 btrfs_queue_work(fs_info->fixup_workers, &fixup->work);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002196 return -EBUSY;
Chris Mason247e7432008-07-17 12:53:51 -04002197}
2198
Yan Zhengd899e052008-10-30 14:25:28 -04002199static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
2200 struct inode *inode, u64 file_pos,
2201 u64 disk_bytenr, u64 disk_num_bytes,
2202 u64 num_bytes, u64 ram_bytes,
2203 u8 compression, u8 encryption,
2204 u16 other_encoding, int extent_type)
2205{
2206 struct btrfs_root *root = BTRFS_I(inode)->root;
2207 struct btrfs_file_extent_item *fi;
2208 struct btrfs_path *path;
2209 struct extent_buffer *leaf;
2210 struct btrfs_key ins;
Qu Wenruoa12b8772017-02-27 15:10:37 +08002211 u64 qg_released;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002212 int extent_inserted = 0;
Yan Zhengd899e052008-10-30 14:25:28 -04002213 int ret;
2214
2215 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07002216 if (!path)
2217 return -ENOMEM;
Yan Zhengd899e052008-10-30 14:25:28 -04002218
Chris Masona1ed8352009-09-11 12:27:37 -04002219 /*
2220 * we may be replacing one extent in the tree with another.
2221 * The new extent is pinned in the extent map, and we don't want
2222 * to drop it from the cache until it is completely in the btree.
2223 *
2224 * So, tell btrfs_drop_extents to leave this extent in the cache.
2225 * the caller is expected to unpin it and allow it to be merged
2226 * with the others.
2227 */
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002228 ret = __btrfs_drop_extents(trans, root, inode, path, file_pos,
2229 file_pos + num_bytes, NULL, 0,
2230 1, sizeof(*fi), &extent_inserted);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002231 if (ret)
2232 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04002233
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002234 if (!extent_inserted) {
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002235 ins.objectid = btrfs_ino(BTRFS_I(inode));
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002236 ins.offset = file_pos;
2237 ins.type = BTRFS_EXTENT_DATA_KEY;
2238
2239 path->leave_spinning = 1;
2240 ret = btrfs_insert_empty_item(trans, root, path, &ins,
2241 sizeof(*fi));
2242 if (ret)
2243 goto out;
2244 }
Yan Zhengd899e052008-10-30 14:25:28 -04002245 leaf = path->nodes[0];
2246 fi = btrfs_item_ptr(leaf, path->slots[0],
2247 struct btrfs_file_extent_item);
2248 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
2249 btrfs_set_file_extent_type(leaf, fi, extent_type);
2250 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
2251 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
2252 btrfs_set_file_extent_offset(leaf, fi, 0);
2253 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
2254 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
2255 btrfs_set_file_extent_compression(leaf, fi, compression);
2256 btrfs_set_file_extent_encryption(leaf, fi, encryption);
2257 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
Chris Masonb9473432009-03-13 11:00:37 -04002258
Yan Zhengd899e052008-10-30 14:25:28 -04002259 btrfs_mark_buffer_dirty(leaf);
Josef Bacikce195332012-09-25 15:26:16 -04002260 btrfs_release_path(path);
Yan Zhengd899e052008-10-30 14:25:28 -04002261
2262 inode_add_bytes(inode, num_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -04002263
2264 ins.objectid = disk_bytenr;
2265 ins.offset = disk_num_bytes;
2266 ins.type = BTRFS_EXTENT_ITEM_KEY;
Qu Wenruoa12b8772017-02-27 15:10:37 +08002267
Qu Wenruo297d7502015-09-08 17:08:37 +08002268 /*
Qu Wenruo5846a3c2015-10-26 14:11:18 +08002269 * Release the reserved range from inode dirty range map, as it is
2270 * already moved into delayed_ref_head
Qu Wenruo297d7502015-09-08 17:08:37 +08002271 */
Qu Wenruoa12b8772017-02-27 15:10:37 +08002272 ret = btrfs_qgroup_release_data(inode, file_pos, ram_bytes);
2273 if (ret < 0)
2274 goto out;
2275 qg_released = ret;
Josef Bacik84f7d8e2017-09-29 15:43:49 -04002276 ret = btrfs_alloc_reserved_file_extent(trans, root,
2277 btrfs_ino(BTRFS_I(inode)),
2278 file_pos, qg_released, &ins);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002279out:
Yan Zhengd899e052008-10-30 14:25:28 -04002280 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04002281
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002282 return ret;
Yan Zhengd899e052008-10-30 14:25:28 -04002283}
2284
Liu Bo38c227d2013-01-29 03:18:40 +00002285/* snapshot-aware defrag */
2286struct sa_defrag_extent_backref {
2287 struct rb_node node;
2288 struct old_sa_defrag_extent *old;
2289 u64 root_id;
2290 u64 inum;
2291 u64 file_pos;
2292 u64 extent_offset;
2293 u64 num_bytes;
2294 u64 generation;
2295};
2296
2297struct old_sa_defrag_extent {
2298 struct list_head list;
2299 struct new_sa_defrag_extent *new;
2300
2301 u64 extent_offset;
2302 u64 bytenr;
2303 u64 offset;
2304 u64 len;
2305 int count;
2306};
2307
2308struct new_sa_defrag_extent {
2309 struct rb_root root;
2310 struct list_head head;
2311 struct btrfs_path *path;
2312 struct inode *inode;
2313 u64 file_pos;
2314 u64 len;
2315 u64 bytenr;
2316 u64 disk_len;
2317 u8 compress_type;
2318};
2319
2320static int backref_comp(struct sa_defrag_extent_backref *b1,
2321 struct sa_defrag_extent_backref *b2)
2322{
2323 if (b1->root_id < b2->root_id)
2324 return -1;
2325 else if (b1->root_id > b2->root_id)
2326 return 1;
2327
2328 if (b1->inum < b2->inum)
2329 return -1;
2330 else if (b1->inum > b2->inum)
2331 return 1;
2332
2333 if (b1->file_pos < b2->file_pos)
2334 return -1;
2335 else if (b1->file_pos > b2->file_pos)
2336 return 1;
2337
2338 /*
2339 * [------------------------------] ===> (a range of space)
2340 * |<--->| |<---->| =============> (fs/file tree A)
2341 * |<---------------------------->| ===> (fs/file tree B)
2342 *
2343 * A range of space can refer to two file extents in one tree while
2344 * refer to only one file extent in another tree.
2345 *
2346 * So we may process a disk offset more than one time(two extents in A)
2347 * and locate at the same extent(one extent in B), then insert two same
2348 * backrefs(both refer to the extent in B).
2349 */
2350 return 0;
2351}
2352
2353static void backref_insert(struct rb_root *root,
2354 struct sa_defrag_extent_backref *backref)
2355{
2356 struct rb_node **p = &root->rb_node;
2357 struct rb_node *parent = NULL;
2358 struct sa_defrag_extent_backref *entry;
2359 int ret;
2360
2361 while (*p) {
2362 parent = *p;
2363 entry = rb_entry(parent, struct sa_defrag_extent_backref, node);
2364
2365 ret = backref_comp(backref, entry);
2366 if (ret < 0)
2367 p = &(*p)->rb_left;
2368 else
2369 p = &(*p)->rb_right;
2370 }
2371
2372 rb_link_node(&backref->node, parent, p);
2373 rb_insert_color(&backref->node, root);
2374}
2375
2376/*
2377 * Note the backref might has changed, and in this case we just return 0.
2378 */
2379static noinline int record_one_backref(u64 inum, u64 offset, u64 root_id,
2380 void *ctx)
2381{
2382 struct btrfs_file_extent_item *extent;
Liu Bo38c227d2013-01-29 03:18:40 +00002383 struct old_sa_defrag_extent *old = ctx;
2384 struct new_sa_defrag_extent *new = old->new;
2385 struct btrfs_path *path = new->path;
2386 struct btrfs_key key;
2387 struct btrfs_root *root;
2388 struct sa_defrag_extent_backref *backref;
2389 struct extent_buffer *leaf;
2390 struct inode *inode = new->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002391 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002392 int slot;
2393 int ret;
2394 u64 extent_offset;
2395 u64 num_bytes;
2396
2397 if (BTRFS_I(inode)->root->root_key.objectid == root_id &&
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002398 inum == btrfs_ino(BTRFS_I(inode)))
Liu Bo38c227d2013-01-29 03:18:40 +00002399 return 0;
2400
2401 key.objectid = root_id;
2402 key.type = BTRFS_ROOT_ITEM_KEY;
2403 key.offset = (u64)-1;
2404
Liu Bo38c227d2013-01-29 03:18:40 +00002405 root = btrfs_read_fs_root_no_name(fs_info, &key);
2406 if (IS_ERR(root)) {
2407 if (PTR_ERR(root) == -ENOENT)
2408 return 0;
2409 WARN_ON(1);
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002410 btrfs_debug(fs_info, "inum=%llu, offset=%llu, root_id=%llu",
Liu Bo38c227d2013-01-29 03:18:40 +00002411 inum, offset, root_id);
2412 return PTR_ERR(root);
2413 }
2414
2415 key.objectid = inum;
2416 key.type = BTRFS_EXTENT_DATA_KEY;
2417 if (offset > (u64)-1 << 32)
2418 key.offset = 0;
2419 else
2420 key.offset = offset;
2421
2422 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05302423 if (WARN_ON(ret < 0))
Liu Bo38c227d2013-01-29 03:18:40 +00002424 return ret;
Josef Bacik50f13192013-07-22 12:50:37 -04002425 ret = 0;
Liu Bo38c227d2013-01-29 03:18:40 +00002426
2427 while (1) {
2428 cond_resched();
2429
2430 leaf = path->nodes[0];
2431 slot = path->slots[0];
2432
2433 if (slot >= btrfs_header_nritems(leaf)) {
2434 ret = btrfs_next_leaf(root, path);
2435 if (ret < 0) {
2436 goto out;
2437 } else if (ret > 0) {
2438 ret = 0;
2439 goto out;
2440 }
2441 continue;
2442 }
2443
2444 path->slots[0]++;
2445
2446 btrfs_item_key_to_cpu(leaf, &key, slot);
2447
2448 if (key.objectid > inum)
2449 goto out;
2450
2451 if (key.objectid < inum || key.type != BTRFS_EXTENT_DATA_KEY)
2452 continue;
2453
2454 extent = btrfs_item_ptr(leaf, slot,
2455 struct btrfs_file_extent_item);
2456
2457 if (btrfs_file_extent_disk_bytenr(leaf, extent) != old->bytenr)
2458 continue;
2459
Liu Boe68afa42013-07-01 22:13:26 +08002460 /*
2461 * 'offset' refers to the exact key.offset,
2462 * NOT the 'offset' field in btrfs_extent_data_ref, ie.
2463 * (key.offset - extent_offset).
2464 */
2465 if (key.offset != offset)
Liu Bo38c227d2013-01-29 03:18:40 +00002466 continue;
2467
Liu Boe68afa42013-07-01 22:13:26 +08002468 extent_offset = btrfs_file_extent_offset(leaf, extent);
Liu Bo38c227d2013-01-29 03:18:40 +00002469 num_bytes = btrfs_file_extent_num_bytes(leaf, extent);
Liu Boe68afa42013-07-01 22:13:26 +08002470
Liu Bo38c227d2013-01-29 03:18:40 +00002471 if (extent_offset >= old->extent_offset + old->offset +
2472 old->len || extent_offset + num_bytes <=
2473 old->extent_offset + old->offset)
2474 continue;
Liu Bo38c227d2013-01-29 03:18:40 +00002475 break;
2476 }
2477
2478 backref = kmalloc(sizeof(*backref), GFP_NOFS);
2479 if (!backref) {
2480 ret = -ENOENT;
2481 goto out;
2482 }
2483
2484 backref->root_id = root_id;
2485 backref->inum = inum;
Liu Boe68afa42013-07-01 22:13:26 +08002486 backref->file_pos = offset;
Liu Bo38c227d2013-01-29 03:18:40 +00002487 backref->num_bytes = num_bytes;
2488 backref->extent_offset = extent_offset;
2489 backref->generation = btrfs_file_extent_generation(leaf, extent);
2490 backref->old = old;
2491 backref_insert(&new->root, backref);
2492 old->count++;
2493out:
2494 btrfs_release_path(path);
2495 WARN_ON(ret);
2496 return ret;
2497}
2498
2499static noinline bool record_extent_backrefs(struct btrfs_path *path,
2500 struct new_sa_defrag_extent *new)
2501{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002502 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002503 struct old_sa_defrag_extent *old, *tmp;
2504 int ret;
2505
2506 new->path = path;
2507
2508 list_for_each_entry_safe(old, tmp, &new->head, list) {
Liu Boe68afa42013-07-01 22:13:26 +08002509 ret = iterate_inodes_from_logical(old->bytenr +
2510 old->extent_offset, fs_info,
Liu Bo38c227d2013-01-29 03:18:40 +00002511 path, record_one_backref,
Zygo Blaxellc995ab32017-09-22 13:58:45 -04002512 old, false);
Josef Bacik4724b102013-11-05 11:11:40 -05002513 if (ret < 0 && ret != -ENOENT)
2514 return false;
Liu Bo38c227d2013-01-29 03:18:40 +00002515
2516 /* no backref to be processed for this extent */
2517 if (!old->count) {
2518 list_del(&old->list);
2519 kfree(old);
2520 }
2521 }
2522
2523 if (list_empty(&new->head))
2524 return false;
2525
2526 return true;
2527}
2528
2529static int relink_is_mergable(struct extent_buffer *leaf,
2530 struct btrfs_file_extent_item *fi,
Liu Bo116e0022013-08-02 16:30:40 +08002531 struct new_sa_defrag_extent *new)
Liu Bo38c227d2013-01-29 03:18:40 +00002532{
Liu Bo116e0022013-08-02 16:30:40 +08002533 if (btrfs_file_extent_disk_bytenr(leaf, fi) != new->bytenr)
Liu Bo38c227d2013-01-29 03:18:40 +00002534 return 0;
2535
2536 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG)
2537 return 0;
2538
Liu Bo116e0022013-08-02 16:30:40 +08002539 if (btrfs_file_extent_compression(leaf, fi) != new->compress_type)
2540 return 0;
2541
2542 if (btrfs_file_extent_encryption(leaf, fi) ||
Liu Bo38c227d2013-01-29 03:18:40 +00002543 btrfs_file_extent_other_encoding(leaf, fi))
2544 return 0;
2545
2546 return 1;
2547}
2548
2549/*
2550 * Note the backref might has changed, and in this case we just return 0.
2551 */
2552static noinline int relink_extent_backref(struct btrfs_path *path,
2553 struct sa_defrag_extent_backref *prev,
2554 struct sa_defrag_extent_backref *backref)
2555{
2556 struct btrfs_file_extent_item *extent;
2557 struct btrfs_file_extent_item *item;
2558 struct btrfs_ordered_extent *ordered;
2559 struct btrfs_trans_handle *trans;
Liu Bo38c227d2013-01-29 03:18:40 +00002560 struct btrfs_root *root;
2561 struct btrfs_key key;
2562 struct extent_buffer *leaf;
2563 struct old_sa_defrag_extent *old = backref->old;
2564 struct new_sa_defrag_extent *new = old->new;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002565 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002566 struct inode *inode;
2567 struct extent_state *cached = NULL;
2568 int ret = 0;
2569 u64 start;
2570 u64 len;
2571 u64 lock_start;
2572 u64 lock_end;
2573 bool merge = false;
2574 int index;
2575
2576 if (prev && prev->root_id == backref->root_id &&
2577 prev->inum == backref->inum &&
2578 prev->file_pos + prev->num_bytes == backref->file_pos)
2579 merge = true;
2580
2581 /* step 1: get root */
2582 key.objectid = backref->root_id;
2583 key.type = BTRFS_ROOT_ITEM_KEY;
2584 key.offset = (u64)-1;
2585
Liu Bo38c227d2013-01-29 03:18:40 +00002586 index = srcu_read_lock(&fs_info->subvol_srcu);
2587
2588 root = btrfs_read_fs_root_no_name(fs_info, &key);
2589 if (IS_ERR(root)) {
2590 srcu_read_unlock(&fs_info->subvol_srcu, index);
2591 if (PTR_ERR(root) == -ENOENT)
2592 return 0;
2593 return PTR_ERR(root);
2594 }
Liu Bo38c227d2013-01-29 03:18:40 +00002595
Wang Shilongbcbba5e2014-02-08 23:46:35 +08002596 if (btrfs_root_readonly(root)) {
2597 srcu_read_unlock(&fs_info->subvol_srcu, index);
2598 return 0;
2599 }
2600
Liu Bo38c227d2013-01-29 03:18:40 +00002601 /* step 2: get inode */
2602 key.objectid = backref->inum;
2603 key.type = BTRFS_INODE_ITEM_KEY;
2604 key.offset = 0;
2605
2606 inode = btrfs_iget(fs_info->sb, &key, root, NULL);
2607 if (IS_ERR(inode)) {
2608 srcu_read_unlock(&fs_info->subvol_srcu, index);
2609 return 0;
2610 }
2611
2612 srcu_read_unlock(&fs_info->subvol_srcu, index);
2613
2614 /* step 3: relink backref */
2615 lock_start = backref->file_pos;
2616 lock_end = backref->file_pos + backref->num_bytes - 1;
2617 lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
David Sterbaff13db42015-12-03 14:30:40 +01002618 &cached);
Liu Bo38c227d2013-01-29 03:18:40 +00002619
2620 ordered = btrfs_lookup_first_ordered_extent(inode, lock_end);
2621 if (ordered) {
2622 btrfs_put_ordered_extent(ordered);
2623 goto out_unlock;
2624 }
2625
2626 trans = btrfs_join_transaction(root);
2627 if (IS_ERR(trans)) {
2628 ret = PTR_ERR(trans);
2629 goto out_unlock;
2630 }
2631
2632 key.objectid = backref->inum;
2633 key.type = BTRFS_EXTENT_DATA_KEY;
2634 key.offset = backref->file_pos;
2635
2636 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2637 if (ret < 0) {
2638 goto out_free_path;
2639 } else if (ret > 0) {
2640 ret = 0;
2641 goto out_free_path;
2642 }
2643
2644 extent = btrfs_item_ptr(path->nodes[0], path->slots[0],
2645 struct btrfs_file_extent_item);
2646
2647 if (btrfs_file_extent_generation(path->nodes[0], extent) !=
2648 backref->generation)
2649 goto out_free_path;
2650
2651 btrfs_release_path(path);
2652
2653 start = backref->file_pos;
2654 if (backref->extent_offset < old->extent_offset + old->offset)
2655 start += old->extent_offset + old->offset -
2656 backref->extent_offset;
2657
2658 len = min(backref->extent_offset + backref->num_bytes,
2659 old->extent_offset + old->offset + old->len);
2660 len -= max(backref->extent_offset, old->extent_offset + old->offset);
2661
2662 ret = btrfs_drop_extents(trans, root, inode, start,
2663 start + len, 1);
2664 if (ret)
2665 goto out_free_path;
2666again:
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002667 key.objectid = btrfs_ino(BTRFS_I(inode));
Liu Bo38c227d2013-01-29 03:18:40 +00002668 key.type = BTRFS_EXTENT_DATA_KEY;
2669 key.offset = start;
2670
Liu Boa09a0a72013-03-11 09:20:58 +00002671 path->leave_spinning = 1;
Liu Bo38c227d2013-01-29 03:18:40 +00002672 if (merge) {
2673 struct btrfs_file_extent_item *fi;
2674 u64 extent_len;
2675 struct btrfs_key found_key;
2676
Gui Hecheng3c9665d2014-01-23 13:41:09 +08002677 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
Liu Bo38c227d2013-01-29 03:18:40 +00002678 if (ret < 0)
2679 goto out_free_path;
2680
2681 path->slots[0]--;
2682 leaf = path->nodes[0];
2683 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2684
2685 fi = btrfs_item_ptr(leaf, path->slots[0],
2686 struct btrfs_file_extent_item);
2687 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
2688
Liu Bo116e0022013-08-02 16:30:40 +08002689 if (extent_len + found_key.offset == start &&
2690 relink_is_mergable(leaf, fi, new)) {
Liu Bo38c227d2013-01-29 03:18:40 +00002691 btrfs_set_file_extent_num_bytes(leaf, fi,
2692 extent_len + len);
2693 btrfs_mark_buffer_dirty(leaf);
2694 inode_add_bytes(inode, len);
2695
2696 ret = 1;
2697 goto out_free_path;
2698 } else {
2699 merge = false;
2700 btrfs_release_path(path);
2701 goto again;
2702 }
2703 }
2704
2705 ret = btrfs_insert_empty_item(trans, root, path, &key,
2706 sizeof(*extent));
2707 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002708 btrfs_abort_transaction(trans, ret);
Liu Bo38c227d2013-01-29 03:18:40 +00002709 goto out_free_path;
2710 }
2711
2712 leaf = path->nodes[0];
2713 item = btrfs_item_ptr(leaf, path->slots[0],
2714 struct btrfs_file_extent_item);
2715 btrfs_set_file_extent_disk_bytenr(leaf, item, new->bytenr);
2716 btrfs_set_file_extent_disk_num_bytes(leaf, item, new->disk_len);
2717 btrfs_set_file_extent_offset(leaf, item, start - new->file_pos);
2718 btrfs_set_file_extent_num_bytes(leaf, item, len);
2719 btrfs_set_file_extent_ram_bytes(leaf, item, new->len);
2720 btrfs_set_file_extent_generation(leaf, item, trans->transid);
2721 btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG);
2722 btrfs_set_file_extent_compression(leaf, item, new->compress_type);
2723 btrfs_set_file_extent_encryption(leaf, item, 0);
2724 btrfs_set_file_extent_other_encoding(leaf, item, 0);
2725
2726 btrfs_mark_buffer_dirty(leaf);
2727 inode_add_bytes(inode, len);
Liu Boa09a0a72013-03-11 09:20:58 +00002728 btrfs_release_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002729
Josef Bacik84f7d8e2017-09-29 15:43:49 -04002730 ret = btrfs_inc_extent_ref(trans, root, new->bytenr,
Liu Bo38c227d2013-01-29 03:18:40 +00002731 new->disk_len, 0,
2732 backref->root_id, backref->inum,
Filipe Mananab06c4bf2015-10-23 07:52:54 +01002733 new->file_pos); /* start - extent_offset */
Liu Bo38c227d2013-01-29 03:18:40 +00002734 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002735 btrfs_abort_transaction(trans, ret);
Liu Bo38c227d2013-01-29 03:18:40 +00002736 goto out_free_path;
2737 }
2738
2739 ret = 1;
2740out_free_path:
2741 btrfs_release_path(path);
Liu Boa09a0a72013-03-11 09:20:58 +00002742 path->leave_spinning = 0;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002743 btrfs_end_transaction(trans);
Liu Bo38c227d2013-01-29 03:18:40 +00002744out_unlock:
2745 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
David Sterbae43bbe52017-12-12 21:43:52 +01002746 &cached);
Liu Bo38c227d2013-01-29 03:18:40 +00002747 iput(inode);
2748 return ret;
2749}
2750
Liu Bo6f519562013-10-29 10:45:05 +08002751static void free_sa_defrag_extent(struct new_sa_defrag_extent *new)
2752{
2753 struct old_sa_defrag_extent *old, *tmp;
2754
2755 if (!new)
2756 return;
2757
2758 list_for_each_entry_safe(old, tmp, &new->head, list) {
Liu Bo6f519562013-10-29 10:45:05 +08002759 kfree(old);
2760 }
2761 kfree(new);
2762}
2763
Liu Bo38c227d2013-01-29 03:18:40 +00002764static void relink_file_extents(struct new_sa_defrag_extent *new)
2765{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002766 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002767 struct btrfs_path *path;
Liu Bo38c227d2013-01-29 03:18:40 +00002768 struct sa_defrag_extent_backref *backref;
2769 struct sa_defrag_extent_backref *prev = NULL;
2770 struct inode *inode;
Liu Bo38c227d2013-01-29 03:18:40 +00002771 struct rb_node *node;
2772 int ret;
2773
2774 inode = new->inode;
Liu Bo38c227d2013-01-29 03:18:40 +00002775
2776 path = btrfs_alloc_path();
2777 if (!path)
2778 return;
2779
2780 if (!record_extent_backrefs(path, new)) {
2781 btrfs_free_path(path);
2782 goto out;
2783 }
2784 btrfs_release_path(path);
2785
2786 while (1) {
2787 node = rb_first(&new->root);
2788 if (!node)
2789 break;
2790 rb_erase(node, &new->root);
2791
2792 backref = rb_entry(node, struct sa_defrag_extent_backref, node);
2793
2794 ret = relink_extent_backref(path, prev, backref);
2795 WARN_ON(ret < 0);
2796
2797 kfree(prev);
2798
2799 if (ret == 1)
2800 prev = backref;
2801 else
2802 prev = NULL;
2803 cond_resched();
2804 }
2805 kfree(prev);
2806
2807 btrfs_free_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002808out:
Liu Bo6f519562013-10-29 10:45:05 +08002809 free_sa_defrag_extent(new);
2810
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002811 atomic_dec(&fs_info->defrag_running);
2812 wake_up(&fs_info->transaction_wait);
Liu Bo38c227d2013-01-29 03:18:40 +00002813}
2814
2815static struct new_sa_defrag_extent *
2816record_old_file_extents(struct inode *inode,
2817 struct btrfs_ordered_extent *ordered)
2818{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002819 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002820 struct btrfs_root *root = BTRFS_I(inode)->root;
2821 struct btrfs_path *path;
2822 struct btrfs_key key;
Liu Bo6f519562013-10-29 10:45:05 +08002823 struct old_sa_defrag_extent *old;
Liu Bo38c227d2013-01-29 03:18:40 +00002824 struct new_sa_defrag_extent *new;
2825 int ret;
2826
2827 new = kmalloc(sizeof(*new), GFP_NOFS);
2828 if (!new)
2829 return NULL;
2830
2831 new->inode = inode;
2832 new->file_pos = ordered->file_offset;
2833 new->len = ordered->len;
2834 new->bytenr = ordered->start;
2835 new->disk_len = ordered->disk_len;
2836 new->compress_type = ordered->compress_type;
2837 new->root = RB_ROOT;
2838 INIT_LIST_HEAD(&new->head);
2839
2840 path = btrfs_alloc_path();
2841 if (!path)
2842 goto out_kfree;
2843
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002844 key.objectid = btrfs_ino(BTRFS_I(inode));
Liu Bo38c227d2013-01-29 03:18:40 +00002845 key.type = BTRFS_EXTENT_DATA_KEY;
2846 key.offset = new->file_pos;
2847
2848 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2849 if (ret < 0)
2850 goto out_free_path;
2851 if (ret > 0 && path->slots[0] > 0)
2852 path->slots[0]--;
2853
2854 /* find out all the old extents for the file range */
2855 while (1) {
2856 struct btrfs_file_extent_item *extent;
2857 struct extent_buffer *l;
2858 int slot;
2859 u64 num_bytes;
2860 u64 offset;
2861 u64 end;
2862 u64 disk_bytenr;
2863 u64 extent_offset;
2864
2865 l = path->nodes[0];
2866 slot = path->slots[0];
2867
2868 if (slot >= btrfs_header_nritems(l)) {
2869 ret = btrfs_next_leaf(root, path);
2870 if (ret < 0)
Liu Bo6f519562013-10-29 10:45:05 +08002871 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002872 else if (ret > 0)
2873 break;
2874 continue;
2875 }
2876
2877 btrfs_item_key_to_cpu(l, &key, slot);
2878
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002879 if (key.objectid != btrfs_ino(BTRFS_I(inode)))
Liu Bo38c227d2013-01-29 03:18:40 +00002880 break;
2881 if (key.type != BTRFS_EXTENT_DATA_KEY)
2882 break;
2883 if (key.offset >= new->file_pos + new->len)
2884 break;
2885
2886 extent = btrfs_item_ptr(l, slot, struct btrfs_file_extent_item);
2887
2888 num_bytes = btrfs_file_extent_num_bytes(l, extent);
2889 if (key.offset + num_bytes < new->file_pos)
2890 goto next;
2891
2892 disk_bytenr = btrfs_file_extent_disk_bytenr(l, extent);
2893 if (!disk_bytenr)
2894 goto next;
2895
2896 extent_offset = btrfs_file_extent_offset(l, extent);
2897
2898 old = kmalloc(sizeof(*old), GFP_NOFS);
2899 if (!old)
Liu Bo6f519562013-10-29 10:45:05 +08002900 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002901
2902 offset = max(new->file_pos, key.offset);
2903 end = min(new->file_pos + new->len, key.offset + num_bytes);
2904
2905 old->bytenr = disk_bytenr;
2906 old->extent_offset = extent_offset;
2907 old->offset = offset - key.offset;
2908 old->len = end - offset;
2909 old->new = new;
2910 old->count = 0;
2911 list_add_tail(&old->list, &new->head);
2912next:
2913 path->slots[0]++;
2914 cond_resched();
2915 }
2916
2917 btrfs_free_path(path);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002918 atomic_inc(&fs_info->defrag_running);
Liu Bo38c227d2013-01-29 03:18:40 +00002919
2920 return new;
2921
Liu Bo38c227d2013-01-29 03:18:40 +00002922out_free_path:
2923 btrfs_free_path(path);
2924out_kfree:
Liu Bo6f519562013-10-29 10:45:05 +08002925 free_sa_defrag_extent(new);
Liu Bo38c227d2013-01-29 03:18:40 +00002926 return NULL;
2927}
2928
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002929static void btrfs_release_delalloc_bytes(struct btrfs_fs_info *fs_info,
Miao Xiee570fd22014-06-19 10:42:50 +08002930 u64 start, u64 len)
2931{
2932 struct btrfs_block_group_cache *cache;
2933
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002934 cache = btrfs_lookup_block_group(fs_info, start);
Miao Xiee570fd22014-06-19 10:42:50 +08002935 ASSERT(cache);
2936
2937 spin_lock(&cache->lock);
2938 cache->delalloc_bytes -= len;
2939 spin_unlock(&cache->lock);
2940
2941 btrfs_put_block_group(cache);
2942}
2943
Chris Masond352ac62008-09-29 15:18:18 -04002944/* as ordered data IO finishes, this gets called so we can finish
2945 * an ordered extent if the range of bytes in the file it covers are
2946 * fully written.
2947 */
Josef Bacik5fd02042012-05-02 14:00:54 -04002948static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002949{
Josef Bacik5fd02042012-05-02 14:00:54 -04002950 struct inode *inode = ordered_extent->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002951 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002952 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002953 struct btrfs_trans_handle *trans = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002954 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002955 struct extent_state *cached_state = NULL;
Liu Bo38c227d2013-01-29 03:18:40 +00002956 struct new_sa_defrag_extent *new = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08002957 int compress_type = 0;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002958 int ret = 0;
2959 u64 logical_len = ordered_extent->len;
Li Zefan82d59022011-04-20 10:33:24 +08002960 bool nolock;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002961 bool truncated = false;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002962 bool range_locked = false;
2963 bool clear_new_delalloc_bytes = false;
2964
2965 if (!test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
2966 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags) &&
2967 !test_bit(BTRFS_ORDERED_DIRECT, &ordered_extent->flags))
2968 clear_new_delalloc_bytes = true;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002969
Nikolay Borisov70ddc552017-02-20 13:50:35 +02002970 nolock = btrfs_is_free_space_inode(BTRFS_I(inode));
Josef Bacik0cb59c92010-07-02 12:14:14 -04002971
Josef Bacik5fd02042012-05-02 14:00:54 -04002972 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
2973 ret = -EIO;
2974 goto out;
2975 }
2976
Nikolay Borisov7ab79562017-02-20 13:50:57 +02002977 btrfs_free_io_failure_record(BTRFS_I(inode),
2978 ordered_extent->file_offset,
2979 ordered_extent->file_offset +
2980 ordered_extent->len - 1);
Miao Xief6124962014-09-12 18:44:04 +08002981
Josef Bacik77cef2e2013-08-29 13:57:21 -04002982 if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) {
2983 truncated = true;
2984 logical_len = ordered_extent->truncated_len;
2985 /* Truncated the entire extent, don't bother adding */
2986 if (!logical_len)
2987 goto out;
2988 }
2989
Yan, Zhengc2167752009-11-12 09:34:21 +00002990 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002991 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
Qu Wenruo94ed9382015-09-08 17:25:56 +08002992
2993 /*
2994 * For mwrite(mmap + memset to write) case, we still reserve
2995 * space for NOCOW range.
2996 * As NOCOW won't cause a new delayed ref, just free the space
2997 */
Qu Wenruobc42bda2017-02-27 15:10:39 +08002998 btrfs_qgroup_free_data(inode, NULL, ordered_extent->file_offset,
Qu Wenruo94ed9382015-09-08 17:25:56 +08002999 ordered_extent->len);
Josef Bacik6c760c02012-11-09 10:53:21 -05003000 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
3001 if (nolock)
3002 trans = btrfs_join_transaction_nolock(root);
3003 else
3004 trans = btrfs_join_transaction(root);
3005 if (IS_ERR(trans)) {
3006 ret = PTR_ERR(trans);
3007 trans = NULL;
3008 goto out;
Yan, Zhengc2167752009-11-12 09:34:21 +00003009 }
Josef Bacik69fe2d72017-10-19 14:15:57 -04003010 trans->block_rsv = &BTRFS_I(inode)->block_rsv;
Josef Bacik6c760c02012-11-09 10:53:21 -05003011 ret = btrfs_update_inode_fallback(trans, root, inode);
3012 if (ret) /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04003013 btrfs_abort_transaction(trans, ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00003014 goto out;
3015 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04003016
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003017 range_locked = true;
Josef Bacik2ac55d42010-02-03 19:33:23 +00003018 lock_extent_bits(io_tree, ordered_extent->file_offset,
3019 ordered_extent->file_offset + ordered_extent->len - 1,
David Sterbaff13db42015-12-03 14:30:40 +01003020 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003021
Liu Bo38c227d2013-01-29 03:18:40 +00003022 ret = test_range_bit(io_tree, ordered_extent->file_offset,
3023 ordered_extent->file_offset + ordered_extent->len - 1,
Liu Bo452e62b2017-05-26 17:44:23 -06003024 EXTENT_DEFRAG, 0, cached_state);
Liu Bo38c227d2013-01-29 03:18:40 +00003025 if (ret) {
3026 u64 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
Josef Bacik8101c8d2014-01-29 16:05:30 -05003027 if (0 && last_snapshot >= BTRFS_I(inode)->generation)
Liu Bo38c227d2013-01-29 03:18:40 +00003028 /* the inode is shared */
3029 new = record_old_file_extents(inode, ordered_extent);
3030
3031 clear_extent_bit(io_tree, ordered_extent->file_offset,
3032 ordered_extent->file_offset + ordered_extent->len - 1,
David Sterbaae0f1622017-10-31 16:37:52 +01003033 EXTENT_DEFRAG, 0, 0, &cached_state);
Liu Bo38c227d2013-01-29 03:18:40 +00003034 }
3035
Josef Bacik0cb59c92010-07-02 12:14:14 -04003036 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003037 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04003038 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003039 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003040 if (IS_ERR(trans)) {
3041 ret = PTR_ERR(trans);
3042 trans = NULL;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003043 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003044 }
Chris Masona79b7d42014-05-22 16:18:52 -07003045
Josef Bacik69fe2d72017-10-19 14:15:57 -04003046 trans->block_rsv = &BTRFS_I(inode)->block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00003047
Chris Masonc8b97812008-10-29 14:49:59 -04003048 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08003049 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04003050 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08003051 BUG_ON(compress_type);
Justin Maggardb430b772017-10-30 15:29:10 -07003052 btrfs_qgroup_free_data(inode, NULL, ordered_extent->file_offset,
3053 ordered_extent->len);
Nikolay Borisov7a6d7062017-02-20 13:50:48 +02003054 ret = btrfs_mark_extent_written(trans, BTRFS_I(inode),
Yan Zhengd899e052008-10-30 14:25:28 -04003055 ordered_extent->file_offset,
3056 ordered_extent->file_offset +
Josef Bacik77cef2e2013-08-29 13:57:21 -04003057 logical_len);
Yan Zhengd899e052008-10-30 14:25:28 -04003058 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003059 BUG_ON(root == fs_info->tree_root);
Yan Zhengd899e052008-10-30 14:25:28 -04003060 ret = insert_reserved_file_extent(trans, inode,
3061 ordered_extent->file_offset,
3062 ordered_extent->start,
3063 ordered_extent->disk_len,
Josef Bacik77cef2e2013-08-29 13:57:21 -04003064 logical_len, logical_len,
Li Zefan261507a02010-12-17 14:21:50 +08003065 compress_type, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04003066 BTRFS_FILE_EXTENT_REG);
Miao Xiee570fd22014-06-19 10:42:50 +08003067 if (!ret)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003068 btrfs_release_delalloc_bytes(fs_info,
Miao Xiee570fd22014-06-19 10:42:50 +08003069 ordered_extent->start,
3070 ordered_extent->disk_len);
Yan Zhengd899e052008-10-30 14:25:28 -04003071 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04003072 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
3073 ordered_extent->file_offset, ordered_extent->len,
3074 trans->transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003075 if (ret < 0) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003076 btrfs_abort_transaction(trans, ret);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003077 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003078 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00003079
Nikolay Borisovac01f262018-01-08 10:59:43 +02003080 ret = add_pending_csums(trans, inode, &ordered_extent->list);
3081 if (ret) {
3082 btrfs_abort_transaction(trans, ret);
3083 goto out;
3084 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04003085
Josef Bacik6c760c02012-11-09 10:53:21 -05003086 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
3087 ret = btrfs_update_inode_fallback(trans, root, inode);
3088 if (ret) { /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04003089 btrfs_abort_transaction(trans, ret);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003090 goto out;
Josef Bacik1ef30be2011-04-05 19:25:36 -04003091 }
3092 ret = 0;
Yan, Zhengc2167752009-11-12 09:34:21 +00003093out:
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003094 if (range_locked || clear_new_delalloc_bytes) {
3095 unsigned int clear_bits = 0;
3096
3097 if (range_locked)
3098 clear_bits |= EXTENT_LOCKED;
3099 if (clear_new_delalloc_bytes)
3100 clear_bits |= EXTENT_DELALLOC_NEW;
3101 clear_extent_bit(&BTRFS_I(inode)->io_tree,
3102 ordered_extent->file_offset,
3103 ordered_extent->file_offset +
3104 ordered_extent->len - 1,
3105 clear_bits,
3106 (clear_bits & EXTENT_LOCKED) ? 1 : 0,
David Sterbaae0f1622017-10-31 16:37:52 +01003107 0, &cached_state);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003108 }
3109
Miao Xiea698d0752012-09-20 01:51:59 -06003110 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003111 btrfs_end_transaction(trans);
Josef Bacik0cb59c92010-07-02 12:14:14 -04003112
Josef Bacik77cef2e2013-08-29 13:57:21 -04003113 if (ret || truncated) {
3114 u64 start, end;
3115
3116 if (truncated)
3117 start = ordered_extent->file_offset + logical_len;
3118 else
3119 start = ordered_extent->file_offset;
3120 end = ordered_extent->file_offset + ordered_extent->len - 1;
David Sterbaf08dc362017-10-31 17:02:39 +01003121 clear_extent_uptodate(io_tree, start, end, NULL);
Josef Bacik77cef2e2013-08-29 13:57:21 -04003122
3123 /* Drop the cache for the part of the extent we didn't write. */
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02003124 btrfs_drop_extent_cache(BTRFS_I(inode), start, end, 0);
Josef Bacik5fd02042012-05-02 14:00:54 -04003125
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003126 /*
3127 * If the ordered extent had an IOERR or something else went
3128 * wrong we need to return the space for this ordered extent
Josef Bacik77cef2e2013-08-29 13:57:21 -04003129 * back to the allocator. We only free the extent in the
3130 * truncated case if we didn't write out the extent at all.
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003131 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04003132 if ((ret || !logical_len) &&
3133 !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
Ethan Liene73e81b2018-05-28 13:48:20 +08003162 /* Try to release some metadata so we don't get an OOM but don't wait */
3163 btrfs_btree_balance_dirty_nodelay(fs_info);
3164
Josef Bacik5fd02042012-05-02 14:00:54 -04003165 return ret;
3166}
3167
3168static void finish_ordered_fn(struct btrfs_work *work)
3169{
3170 struct btrfs_ordered_extent *ordered_extent;
3171 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
3172 btrfs_finish_ordered_io(ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003173}
3174
David Sterbac3988d62017-02-17 15:18:32 +01003175static void btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason211f90e2008-07-18 11:56:15 -04003176 struct extent_state *state, int uptodate)
3177{
Josef Bacik5fd02042012-05-02 14:00:54 -04003178 struct inode *inode = page->mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003179 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik5fd02042012-05-02 14:00:54 -04003180 struct btrfs_ordered_extent *ordered_extent = NULL;
Liu Bo9e0af232014-08-15 23:36:53 +08003181 struct btrfs_workqueue *wq;
3182 btrfs_work_func_t func;
Josef Bacik5fd02042012-05-02 14:00:54 -04003183
liubo1abe9b82011-03-24 11:18:59 +00003184 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
3185
Chris Mason8b62b722009-09-02 16:53:46 -04003186 ClearPagePrivate2(page);
Josef Bacik5fd02042012-05-02 14:00:54 -04003187 if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
3188 end - start + 1, uptodate))
David Sterbac3988d62017-02-17 15:18:32 +01003189 return;
Josef Bacik5fd02042012-05-02 14:00:54 -04003190
Nikolay Borisov70ddc552017-02-20 13:50:35 +02003191 if (btrfs_is_free_space_inode(BTRFS_I(inode))) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003192 wq = fs_info->endio_freespace_worker;
Liu Bo9e0af232014-08-15 23:36:53 +08003193 func = btrfs_freespace_write_helper;
3194 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003195 wq = fs_info->endio_write_workers;
Liu Bo9e0af232014-08-15 23:36:53 +08003196 func = btrfs_endio_write_helper;
3197 }
Josef Bacik5fd02042012-05-02 14:00:54 -04003198
Liu Bo9e0af232014-08-15 23:36:53 +08003199 btrfs_init_work(&ordered_extent->work, func, finish_ordered_fn, NULL,
3200 NULL);
3201 btrfs_queue_work(wq, &ordered_extent->work);
Chris Mason211f90e2008-07-18 11:56:15 -04003202}
3203
Miao Xiedc380ae2014-09-12 18:43:55 +08003204static int __readpage_endio_check(struct inode *inode,
3205 struct btrfs_io_bio *io_bio,
3206 int icsum, struct page *page,
3207 int pgoff, u64 start, size_t len)
3208{
3209 char *kaddr;
3210 u32 csum_expected;
3211 u32 csum = ~(u32)0;
Miao Xiedc380ae2014-09-12 18:43:55 +08003212
3213 csum_expected = *(((u32 *)io_bio->csum) + icsum);
3214
3215 kaddr = kmap_atomic(page);
3216 csum = btrfs_csum_data(kaddr + pgoff, csum, len);
Domagoj Tršan0b5e3da2016-10-27 08:52:33 +01003217 btrfs_csum_final(csum, (u8 *)&csum);
Miao Xiedc380ae2014-09-12 18:43:55 +08003218 if (csum != csum_expected)
3219 goto zeroit;
3220
3221 kunmap_atomic(kaddr);
3222 return 0;
3223zeroit:
Nikolay Borisov0970a222017-02-20 13:50:53 +02003224 btrfs_print_data_csum_error(BTRFS_I(inode), start, csum, csum_expected,
Qu Wenruo6f6b6432017-02-09 10:45:06 +08003225 io_bio->mirror_num);
Miao Xiedc380ae2014-09-12 18:43:55 +08003226 memset(kaddr + pgoff, 1, len);
3227 flush_dcache_page(page);
3228 kunmap_atomic(kaddr);
Miao Xiedc380ae2014-09-12 18:43:55 +08003229 return -EIO;
3230}
3231
Chris Masond352ac62008-09-29 15:18:18 -04003232/*
Chris Masond352ac62008-09-29 15:18:18 -04003233 * when reads are done, we need to check csums to verify the data is correct
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02003234 * if there's a match, we allow the bio to finish. If not, the code in
3235 * extent_io.c will try to find good copies for us.
Chris Masond352ac62008-09-29 15:18:18 -04003236 */
Miao Xiefacc8a222013-07-25 19:22:34 +08003237static int btrfs_readpage_end_io_hook(struct btrfs_io_bio *io_bio,
3238 u64 phy_offset, struct page *page,
3239 u64 start, u64 end, int mirror)
Chris Mason07157aa2007-08-30 08:50:51 -04003240{
Miao Xie4eee4fa2012-12-21 09:17:45 +00003241 size_t offset = start - page_offset(page);
Chris Mason07157aa2007-08-30 08:50:51 -04003242 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05003243 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masonff79f812007-10-15 16:22:25 -04003244 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -05003245
Chris Masond20f7042008-12-08 16:58:54 -05003246 if (PageChecked(page)) {
3247 ClearPageChecked(page);
Miao Xiedc380ae2014-09-12 18:43:55 +08003248 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003249 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003250
3251 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Miao Xiedc380ae2014-09-12 18:43:55 +08003252 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003253
Yan Zheng17d217f2008-12-12 10:03:38 -05003254 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04003255 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
David Sterba91166212016-04-26 23:54:39 +02003256 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM);
Yan Zheng17d217f2008-12-12 10:03:38 -05003257 return 0;
3258 }
3259
Miao Xiefacc8a222013-07-25 19:22:34 +08003260 phy_offset >>= inode->i_sb->s_blocksize_bits;
Miao Xiedc380ae2014-09-12 18:43:55 +08003261 return __readpage_endio_check(inode, io_bio, phy_offset, page, offset,
3262 start, (size_t)(end - start + 1));
Chris Mason07157aa2007-08-30 08:50:51 -04003263}
Chris Masonb888db22007-08-27 16:49:44 -04003264
Nikolay Borisovc1c3fac2018-01-16 09:31:58 +02003265/*
3266 * btrfs_add_delayed_iput - perform a delayed iput on @inode
3267 *
3268 * @inode: The inode we want to perform iput on
3269 *
3270 * This function uses the generic vfs_inode::i_count to track whether we should
3271 * just decrement it (in case it's > 1) or if this is the last iput then link
3272 * the inode to the delayed iput machinery. Delayed iputs are processed at
3273 * transaction commit time/superblock commit/cleaner kthread.
3274 */
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003275void btrfs_add_delayed_iput(struct inode *inode)
3276{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003277 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
David Sterba8089fe62015-11-19 14:15:51 +01003278 struct btrfs_inode *binode = BTRFS_I(inode);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003279
3280 if (atomic_add_unless(&inode->i_count, -1, 1))
3281 return;
3282
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003283 spin_lock(&fs_info->delayed_iput_lock);
Nikolay Borisovc1c3fac2018-01-16 09:31:58 +02003284 ASSERT(list_empty(&binode->delayed_iput));
3285 list_add_tail(&binode->delayed_iput, &fs_info->delayed_iputs);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003286 spin_unlock(&fs_info->delayed_iput_lock);
3287}
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);
3301 spin_lock(&fs_info->delayed_iput_lock);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003302 }
David Sterba8089fe62015-11-19 14:15:51 +01003303 spin_unlock(&fs_info->delayed_iput_lock);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003304}
3305
Yan, Zhengd68fc572010-05-16 10:49:58 -04003306/*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003307 * This creates an orphan entry for the given inode in case something goes wrong
3308 * in the middle of an unlink.
Josef Bacik7b128762008-07-24 12:17:14 -04003309 */
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003310int btrfs_orphan_add(struct btrfs_trans_handle *trans,
Omar Sandoval27919062018-05-11 13:13:37 -07003311 struct btrfs_inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003312{
Yan, Zhengd68fc572010-05-16 10:49:58 -04003313 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003314
Omar Sandoval27919062018-05-11 13:13:37 -07003315 ret = btrfs_insert_orphan_item(trans, inode->root, btrfs_ino(inode));
3316 if (ret && ret != -EEXIST) {
3317 btrfs_abort_transaction(trans, ret);
3318 return ret;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003319 }
3320
Yan, Zhengd68fc572010-05-16 10:49:58 -04003321 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003322}
3323
3324/*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003325 * We have done the delete so we can go ahead and remove the orphan item for
3326 * this particular inode.
Josef Bacik7b128762008-07-24 12:17:14 -04003327 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00003328static int btrfs_orphan_del(struct btrfs_trans_handle *trans,
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003329 struct btrfs_inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003330{
Omar Sandoval27919062018-05-11 13:13:37 -07003331 return btrfs_del_orphan_item(trans, inode->root, btrfs_ino(inode));
Josef Bacik7b128762008-07-24 12:17:14 -04003332}
3333
3334/*
3335 * this cleans up any orphans that may be left on the list from the last use
3336 * of this root.
3337 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003338int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04003339{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003340 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik7b128762008-07-24 12:17:14 -04003341 struct btrfs_path *path;
3342 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04003343 struct btrfs_key key, found_key;
3344 struct btrfs_trans_handle *trans;
3345 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003346 u64 last_objectid = 0;
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003347 int ret = 0, nr_unlink = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003348
Yan, Zhengd68fc572010-05-16 10:49:58 -04003349 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003350 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00003351
3352 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003353 if (!path) {
3354 ret = -ENOMEM;
3355 goto out;
3356 }
David Sterbae4058b52015-11-27 16:31:35 +01003357 path->reada = READA_BACK;
Josef Bacik7b128762008-07-24 12:17:14 -04003358
3359 key.objectid = BTRFS_ORPHAN_OBJECTID;
David Sterba962a2982014-06-04 18:41:45 +02003360 key.type = BTRFS_ORPHAN_ITEM_KEY;
Josef Bacik7b128762008-07-24 12:17:14 -04003361 key.offset = (u64)-1;
3362
Josef Bacik7b128762008-07-24 12:17:14 -04003363 while (1) {
3364 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003365 if (ret < 0)
3366 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003367
3368 /*
3369 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003370 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04003371 * find the key and see if we have stuff that matches
3372 */
3373 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003374 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003375 if (path->slots[0] == 0)
3376 break;
3377 path->slots[0]--;
3378 }
3379
3380 /* pull out the item */
3381 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04003382 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3383
3384 /* make sure the item matches what we want */
3385 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
3386 break;
David Sterba962a2982014-06-04 18:41:45 +02003387 if (found_key.type != BTRFS_ORPHAN_ITEM_KEY)
Josef Bacik7b128762008-07-24 12:17:14 -04003388 break;
3389
3390 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02003391 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04003392
3393 /*
3394 * this is where we are basically btrfs_lookup, without the
3395 * crossing root thing. we store the inode number in the
3396 * offset of the orphan item.
3397 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003398
3399 if (found_key.offset == last_objectid) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003400 btrfs_err(fs_info,
3401 "Error removing orphan entry, stopping orphan cleanup");
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003402 ret = -EINVAL;
3403 goto out;
3404 }
3405
3406 last_objectid = found_key.offset;
3407
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003408 found_key.objectid = found_key.offset;
3409 found_key.type = BTRFS_INODE_ITEM_KEY;
3410 found_key.offset = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003411 inode = btrfs_iget(fs_info->sb, &found_key, root, NULL);
Rusty Russell8c6ffba2013-07-15 11:20:32 +09303412 ret = PTR_ERR_OR_ZERO(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003413 if (ret && ret != -ENOENT)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003414 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003415
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003416 if (ret == -ENOENT && root == fs_info->tree_root) {
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003417 struct btrfs_root *dead_root;
3418 struct btrfs_fs_info *fs_info = root->fs_info;
3419 int is_dead_root = 0;
3420
3421 /*
3422 * this is an orphan in the tree root. Currently these
3423 * could come from 2 sources:
3424 * a) a snapshot deletion in progress
3425 * b) a free space cache inode
3426 * We need to distinguish those two, as the snapshot
3427 * orphan must not get deleted.
3428 * find_dead_roots already ran before us, so if this
3429 * is a snapshot deletion, we should find the root
3430 * in the dead_roots list
3431 */
3432 spin_lock(&fs_info->trans_lock);
3433 list_for_each_entry(dead_root, &fs_info->dead_roots,
3434 root_list) {
3435 if (dead_root->root_key.objectid ==
3436 found_key.objectid) {
3437 is_dead_root = 1;
3438 break;
3439 }
3440 }
3441 spin_unlock(&fs_info->trans_lock);
3442 if (is_dead_root) {
3443 /* prevent this orphan from being found again */
3444 key.offset = found_key.objectid - 1;
3445 continue;
3446 }
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003447
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003448 }
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003449
Josef Bacika8c9e572011-09-21 16:55:59 -04003450 /*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003451 * If we have an inode with links, there are a couple of
3452 * possibilities. Old kernels (before v3.12) used to create an
3453 * orphan item for truncate indicating that there were possibly
3454 * extent items past i_size that needed to be deleted. In v3.12,
3455 * truncate was changed to update i_size in sync with the extent
3456 * items, but the (useless) orphan item was still created. Since
3457 * v4.18, we don't create the orphan item for truncate at all.
3458 *
3459 * So, this item could mean that we need to do a truncate, but
3460 * only if this filesystem was last used on a pre-v3.12 kernel
3461 * and was not cleanly unmounted. The odds of that are quite
3462 * slim, and it's a pain to do the truncate now, so just delete
3463 * the orphan item.
3464 *
3465 * It's also possible that this orphan item was supposed to be
3466 * deleted but wasn't. The inode number may have been reused,
3467 * but either way, we can delete the orphan item.
Josef Bacika8c9e572011-09-21 16:55:59 -04003468 */
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003469 if (ret == -ENOENT || inode->i_nlink) {
3470 if (!ret)
3471 iput(inode);
Josef Bacika8c9e572011-09-21 16:55:59 -04003472 trans = btrfs_start_transaction(root, 1);
3473 if (IS_ERR(trans)) {
3474 ret = PTR_ERR(trans);
3475 goto out;
3476 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003477 btrfs_debug(fs_info, "auto deleting %Lu",
3478 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003479 ret = btrfs_del_orphan_item(trans, root,
3480 found_key.objectid);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003481 btrfs_end_transaction(trans);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003482 if (ret)
3483 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003484 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003485 }
Josef Bacik7b128762008-07-24 12:17:14 -04003486
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003487 nr_unlink++;
Josef Bacik7b128762008-07-24 12:17:14 -04003488
3489 /* this will do delete_inode and everything for us */
3490 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003491 if (ret)
3492 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003493 }
Miao Xie3254c872011-11-10 20:45:05 -05003494 /* release the path since we're done with it */
3495 btrfs_release_path(path);
3496
Yan, Zhengd68fc572010-05-16 10:49:58 -04003497 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
3498
Omar Sandovala575cee2018-05-11 13:13:38 -07003499 if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003500 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003501 if (!IS_ERR(trans))
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003502 btrfs_end_transaction(trans);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003503 }
Josef Bacik7b128762008-07-24 12:17:14 -04003504
3505 if (nr_unlink)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003506 btrfs_debug(fs_info, "unlinked %d orphans", nr_unlink);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003507
3508out:
3509 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003510 btrfs_err(fs_info, "could not do orphan cleanup %d", ret);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003511 btrfs_free_path(path);
3512 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003513}
3514
Chris Masond352ac62008-09-29 15:18:18 -04003515/*
Chris Mason46a53cc2009-04-27 11:47:50 -04003516 * very simple check to peek ahead in the leaf looking for xattrs. If we
3517 * don't find any xattrs, we know there can't be any acls.
3518 *
3519 * slot is the slot the inode is in, objectid is the objectid of the inode
3520 */
3521static noinline int acls_after_inode_item(struct extent_buffer *leaf,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003522 int slot, u64 objectid,
3523 int *first_xattr_slot)
Chris Mason46a53cc2009-04-27 11:47:50 -04003524{
3525 u32 nritems = btrfs_header_nritems(leaf);
3526 struct btrfs_key found_key;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003527 static u64 xattr_access = 0;
3528 static u64 xattr_default = 0;
Chris Mason46a53cc2009-04-27 11:47:50 -04003529 int scanned = 0;
3530
Josef Bacikf23b5a52013-06-19 10:16:26 -04003531 if (!xattr_access) {
Andreas Gruenbacher97d79292015-12-02 14:44:35 +01003532 xattr_access = btrfs_name_hash(XATTR_NAME_POSIX_ACL_ACCESS,
3533 strlen(XATTR_NAME_POSIX_ACL_ACCESS));
3534 xattr_default = btrfs_name_hash(XATTR_NAME_POSIX_ACL_DEFAULT,
3535 strlen(XATTR_NAME_POSIX_ACL_DEFAULT));
Josef Bacikf23b5a52013-06-19 10:16:26 -04003536 }
3537
Chris Mason46a53cc2009-04-27 11:47:50 -04003538 slot++;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003539 *first_xattr_slot = -1;
Chris Mason46a53cc2009-04-27 11:47:50 -04003540 while (slot < nritems) {
3541 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3542
3543 /* we found a different objectid, there must not be acls */
3544 if (found_key.objectid != objectid)
3545 return 0;
3546
3547 /* we found an xattr, assume we've got an acl */
Josef Bacikf23b5a52013-06-19 10:16:26 -04003548 if (found_key.type == BTRFS_XATTR_ITEM_KEY) {
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003549 if (*first_xattr_slot == -1)
3550 *first_xattr_slot = slot;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003551 if (found_key.offset == xattr_access ||
3552 found_key.offset == xattr_default)
3553 return 1;
3554 }
Chris Mason46a53cc2009-04-27 11:47:50 -04003555
3556 /*
3557 * we found a key greater than an xattr key, there can't
3558 * be any acls later on
3559 */
3560 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
3561 return 0;
3562
3563 slot++;
3564 scanned++;
3565
3566 /*
3567 * it goes inode, inode backrefs, xattrs, extents,
3568 * so if there are a ton of hard links to an inode there can
3569 * be a lot of backrefs. Don't waste time searching too hard,
3570 * this is just an optimization
3571 */
3572 if (scanned >= 8)
3573 break;
3574 }
3575 /* we hit the end of the leaf before we found an xattr or
3576 * something larger than an xattr. We have to assume the inode
3577 * has acls
3578 */
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003579 if (*first_xattr_slot == -1)
3580 *first_xattr_slot = slot;
Chris Mason46a53cc2009-04-27 11:47:50 -04003581 return 1;
3582}
3583
3584/*
Chris Masond352ac62008-09-29 15:18:18 -04003585 * read an inode from the btree into the in-memory inode
3586 */
Filipe Manana67710892016-06-06 11:51:25 +01003587static int btrfs_read_locked_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003588{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003589 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04003590 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003591 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003592 struct btrfs_inode_item *inode_item;
3593 struct btrfs_root *root = BTRFS_I(inode)->root;
3594 struct btrfs_key location;
Miao Xie67de1172013-12-26 13:07:06 +08003595 unsigned long ptr;
Chris Mason46a53cc2009-04-27 11:47:50 -04003596 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04003597 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04003598 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003599 bool filled = false;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003600 int first_xattr_slot;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003601
3602 ret = btrfs_fill_inode(inode, &rdev);
3603 if (!ret)
3604 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04003605
3606 path = btrfs_alloc_path();
Al Virof5b3a412018-07-29 23:04:51 +01003607 if (!path)
3608 return -ENOMEM;
Mark Fasheh1748f842011-07-12 11:25:31 -07003609
Chris Mason39279cc2007-06-12 06:35:45 -04003610 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05003611
Chris Mason39279cc2007-06-12 06:35:45 -04003612 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Filipe Manana67710892016-06-06 11:51:25 +01003613 if (ret) {
Al Virof5b3a412018-07-29 23:04:51 +01003614 btrfs_free_path(path);
3615 return ret;
Filipe Manana67710892016-06-06 11:51:25 +01003616 }
Chris Mason39279cc2007-06-12 06:35:45 -04003617
Chris Mason5f39d392007-10-15 16:14:19 -04003618 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00003619
3620 if (filled)
Miao Xie67de1172013-12-26 13:07:06 +08003621 goto cache_index;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003622
Chris Mason5f39d392007-10-15 16:14:19 -04003623 inode_item = btrfs_item_ptr(leaf, path->slots[0],
3624 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003625 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02003626 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08003627 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
3628 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02003629 btrfs_i_size_write(BTRFS_I(inode), btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04003630
David Sterbaa937b972014-12-12 17:39:12 +01003631 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->atime);
3632 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->atime);
Chris Mason5f39d392007-10-15 16:14:19 -04003633
David Sterbaa937b972014-12-12 17:39:12 +01003634 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->mtime);
3635 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->mtime);
Chris Mason5f39d392007-10-15 16:14:19 -04003636
David Sterbaa937b972014-12-12 17:39:12 +01003637 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->ctime);
3638 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->ctime);
Chris Mason5f39d392007-10-15 16:14:19 -04003639
chandan r9cc97d62012-07-04 12:48:07 +05303640 BTRFS_I(inode)->i_otime.tv_sec =
3641 btrfs_timespec_sec(leaf, &inode_item->otime);
3642 BTRFS_I(inode)->i_otime.tv_nsec =
3643 btrfs_timespec_nsec(leaf, &inode_item->otime);
Chris Mason5f39d392007-10-15 16:14:19 -04003644
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003645 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003646 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003647 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
3648
Jeff Laytonc7f88c42017-12-11 06:35:12 -05003649 inode_set_iversion_queried(inode,
3650 btrfs_inode_sequence(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003651 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04003652 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003653 rdev = btrfs_inode_rdev(leaf, inode_item);
3654
Josef Bacikaec74772008-07-24 12:12:38 -04003655 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05003656 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Miao Xie67de1172013-12-26 13:07:06 +08003657
3658cache_index:
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003659 /*
3660 * If we were modified in the current generation and evicted from memory
3661 * and then re-read we need to do a full sync since we don't have any
3662 * idea about which extents were modified before we were evicted from
3663 * cache.
3664 *
3665 * This is required for both inode re-read from disk and delayed inode
3666 * in delayed_nodes_tree.
3667 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003668 if (BTRFS_I(inode)->last_trans == fs_info->generation)
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003669 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3670 &BTRFS_I(inode)->runtime_flags);
3671
Filipe Mananabde6c242015-07-24 00:00:19 +01003672 /*
3673 * We don't persist the id of the transaction where an unlink operation
3674 * against the inode was last made. So here we assume the inode might
3675 * have been evicted, and therefore the exact value of last_unlink_trans
3676 * lost, and set it to last_trans to avoid metadata inconsistencies
3677 * between the inode and its parent if the inode is fsync'ed and the log
3678 * replayed. For example, in the scenario:
3679 *
3680 * touch mydir/foo
3681 * ln mydir/foo mydir/bar
3682 * sync
3683 * unlink mydir/bar
3684 * echo 2 > /proc/sys/vm/drop_caches # evicts inode
3685 * xfs_io -c fsync mydir/foo
3686 * <power failure>
3687 * mount fs, triggers fsync log replay
3688 *
3689 * We must make sure that when we fsync our inode foo we also log its
3690 * parent inode, otherwise after log replay the parent still has the
3691 * dentry with the "bar" name but our inode foo has a link count of 1
3692 * and doesn't have an inode ref with the name "bar" anymore.
3693 *
3694 * Setting last_unlink_trans to last_trans is a pessimistic approach,
Nicholas D Steeves01327612016-05-19 21:18:45 -04003695 * but it guarantees correctness at the expense of occasional full
Filipe Mananabde6c242015-07-24 00:00:19 +01003696 * transaction commits on fsync if our inode is a directory, or if our
3697 * inode is not a directory, logging its parent unnecessarily.
3698 */
3699 BTRFS_I(inode)->last_unlink_trans = BTRFS_I(inode)->last_trans;
3700
Miao Xie67de1172013-12-26 13:07:06 +08003701 path->slots[0]++;
3702 if (inode->i_nlink != 1 ||
3703 path->slots[0] >= btrfs_header_nritems(leaf))
3704 goto cache_acl;
3705
3706 btrfs_item_key_to_cpu(leaf, &location, path->slots[0]);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003707 if (location.objectid != btrfs_ino(BTRFS_I(inode)))
Miao Xie67de1172013-12-26 13:07:06 +08003708 goto cache_acl;
3709
3710 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3711 if (location.type == BTRFS_INODE_REF_KEY) {
3712 struct btrfs_inode_ref *ref;
3713
3714 ref = (struct btrfs_inode_ref *)ptr;
3715 BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref);
3716 } else if (location.type == BTRFS_INODE_EXTREF_KEY) {
3717 struct btrfs_inode_extref *extref;
3718
3719 extref = (struct btrfs_inode_extref *)ptr;
3720 BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf,
3721 extref);
3722 }
Miao Xie2f7e33d2011-06-23 07:27:13 +00003723cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04003724 /*
3725 * try to precache a NULL acl entry for files that don't have
3726 * any xattrs or acls
3727 */
Li Zefan33345d012011-04-20 10:31:50 +08003728 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
David Sterbaf85b7372017-01-20 14:54:07 +01003729 btrfs_ino(BTRFS_I(inode)), &first_xattr_slot);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003730 if (first_xattr_slot != -1) {
3731 path->slots[0] = first_xattr_slot;
3732 ret = btrfs_load_inode_props(inode, path);
3733 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003734 btrfs_err(fs_info,
David Sterba351fd352014-05-15 16:48:20 +02003735 "error loading props for ino %llu (root %llu): %d",
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003736 btrfs_ino(BTRFS_I(inode)),
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003737 root->root_key.objectid, ret);
3738 }
3739 btrfs_free_path(path);
3740
Al Viro72c04902009-06-24 16:58:48 -04003741 if (!maybe_acls)
3742 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04003743
Chris Mason39279cc2007-06-12 06:35:45 -04003744 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04003745 case S_IFREG:
3746 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masond1310b22008-01-24 16:13:08 -05003747 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04003748 inode->i_fop = &btrfs_file_operations;
3749 inode->i_op = &btrfs_file_inode_operations;
3750 break;
3751 case S_IFDIR:
3752 inode->i_fop = &btrfs_dir_file_operations;
Omar Sandoval67ade052017-01-25 17:06:38 -08003753 inode->i_op = &btrfs_dir_inode_operations;
Chris Mason39279cc2007-06-12 06:35:45 -04003754 break;
3755 case S_IFLNK:
3756 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -05003757 inode_nohighmem(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003758 inode->i_mapping->a_ops = &btrfs_symlink_aops;
3759 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04003760 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05003761 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04003762 init_special_inode(inode, inode->i_mode, rdev);
3763 break;
Chris Mason39279cc2007-06-12 06:35:45 -04003764 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003765
David Sterba7b6a2212018-03-26 18:40:21 +02003766 btrfs_sync_inode_flags_to_i_flags(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003767 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003768}
3769
Chris Masond352ac62008-09-29 15:18:18 -04003770/*
3771 * given a leaf and an inode, copy the inode fields into the leaf
3772 */
Chris Masone02119d2008-09-05 16:13:11 -04003773static void fill_inode_item(struct btrfs_trans_handle *trans,
3774 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04003775 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04003776 struct inode *inode)
3777{
Liu Bo51fab692012-12-27 09:01:21 +00003778 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003779
Liu Bo51fab692012-12-27 09:01:21 +00003780 btrfs_init_map_token(&token);
Chris Mason5f39d392007-10-15 16:14:19 -04003781
Liu Bo51fab692012-12-27 09:01:21 +00003782 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
3783 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
3784 btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size,
3785 &token);
3786 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3787 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003788
David Sterbaa937b972014-12-12 17:39:12 +01003789 btrfs_set_token_timespec_sec(leaf, &item->atime,
Liu Bo51fab692012-12-27 09:01:21 +00003790 inode->i_atime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003791 btrfs_set_token_timespec_nsec(leaf, &item->atime,
Liu Bo51fab692012-12-27 09:01:21 +00003792 inode->i_atime.tv_nsec, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003793
David Sterbaa937b972014-12-12 17:39:12 +01003794 btrfs_set_token_timespec_sec(leaf, &item->mtime,
Liu Bo51fab692012-12-27 09:01:21 +00003795 inode->i_mtime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003796 btrfs_set_token_timespec_nsec(leaf, &item->mtime,
Liu Bo51fab692012-12-27 09:01:21 +00003797 inode->i_mtime.tv_nsec, &token);
3798
David Sterbaa937b972014-12-12 17:39:12 +01003799 btrfs_set_token_timespec_sec(leaf, &item->ctime,
Liu Bo51fab692012-12-27 09:01:21 +00003800 inode->i_ctime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003801 btrfs_set_token_timespec_nsec(leaf, &item->ctime,
Liu Bo51fab692012-12-27 09:01:21 +00003802 inode->i_ctime.tv_nsec, &token);
3803
chandan r9cc97d62012-07-04 12:48:07 +05303804 btrfs_set_token_timespec_sec(leaf, &item->otime,
3805 BTRFS_I(inode)->i_otime.tv_sec, &token);
3806 btrfs_set_token_timespec_nsec(leaf, &item->otime,
3807 BTRFS_I(inode)->i_otime.tv_nsec, &token);
3808
Liu Bo51fab692012-12-27 09:01:21 +00003809 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3810 &token);
3811 btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation,
3812 &token);
Jeff Laytonc7f88c42017-12-11 06:35:12 -05003813 btrfs_set_token_inode_sequence(leaf, item, inode_peek_iversion(inode),
3814 &token);
Liu Bo51fab692012-12-27 09:01:21 +00003815 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
3816 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3817 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3818 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
Chris Mason39279cc2007-06-12 06:35:45 -04003819}
3820
Chris Masond352ac62008-09-29 15:18:18 -04003821/*
3822 * copy everything in the in-memory inode into the btree.
3823 */
Chris Mason21151332011-11-10 20:39:08 -05003824static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Chris Masond3977122009-01-05 21:25:51 -05003825 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003826{
3827 struct btrfs_inode_item *inode_item;
3828 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003829 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003830 int ret;
3831
3832 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08003833 if (!path)
3834 return -ENOMEM;
3835
Chris Masonb9473432009-03-13 11:00:37 -04003836 path->leave_spinning = 1;
Miao Xie16cdcec2011-04-22 18:12:22 +08003837 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
3838 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003839 if (ret) {
3840 if (ret > 0)
3841 ret = -ENOENT;
3842 goto failed;
3843 }
3844
Chris Mason5f39d392007-10-15 16:14:19 -04003845 leaf = path->nodes[0];
3846 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08003847 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04003848
Chris Masone02119d2008-09-05 16:13:11 -04003849 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04003850 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04003851 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003852 ret = 0;
3853failed:
Chris Mason39279cc2007-06-12 06:35:45 -04003854 btrfs_free_path(path);
3855 return ret;
3856}
3857
Chris Masond352ac62008-09-29 15:18:18 -04003858/*
Chris Mason21151332011-11-10 20:39:08 -05003859 * copy everything in the in-memory inode into the btree.
3860 */
3861noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
3862 struct btrfs_root *root, struct inode *inode)
3863{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003864 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason21151332011-11-10 20:39:08 -05003865 int ret;
3866
3867 /*
3868 * If the inode is a free space inode, we can deadlock during commit
3869 * if we put it into the delayed code.
3870 *
3871 * The data relocation inode should also be directly updated
3872 * without delay
3873 */
Nikolay Borisov70ddc552017-02-20 13:50:35 +02003874 if (!btrfs_is_free_space_inode(BTRFS_I(inode))
Josef Bacik1d52c782014-09-18 11:30:44 -04003875 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003876 && !test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) {
Alexander Block8ea05e32012-07-25 17:35:53 +02003877 btrfs_update_root_times(trans, root);
3878
Chris Mason21151332011-11-10 20:39:08 -05003879 ret = btrfs_delayed_update_inode(trans, root, inode);
3880 if (!ret)
3881 btrfs_set_inode_last_trans(trans, inode);
3882 return ret;
3883 }
3884
3885 return btrfs_update_inode_item(trans, root, inode);
3886}
3887
Josef Bacikbe6aef62012-10-22 15:43:12 -04003888noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
3889 struct btrfs_root *root,
3890 struct inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05003891{
3892 int ret;
3893
3894 ret = btrfs_update_inode(trans, root, inode);
3895 if (ret == -ENOSPC)
3896 return btrfs_update_inode_item(trans, root, inode);
3897 return ret;
3898}
3899
3900/*
Chris Masond352ac62008-09-29 15:18:18 -04003901 * unlink helper that gets used here in inode.c and in the tree logging
3902 * recovery code. It remove a link in a directory with a given name, and
3903 * also drops the back refs in the inode to the directory
3904 */
Al Viro92986792011-03-04 17:14:37 +00003905static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3906 struct btrfs_root *root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003907 struct btrfs_inode *dir,
3908 struct btrfs_inode *inode,
Al Viro92986792011-03-04 17:14:37 +00003909 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04003910{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003911 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04003912 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04003913 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003914 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003915 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04003916 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04003917 u64 index;
Li Zefan33345d012011-04-20 10:31:50 +08003918 u64 ino = btrfs_ino(inode);
3919 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04003920
3921 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04003922 if (!path) {
3923 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00003924 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04003925 }
3926
Chris Masonb9473432009-03-13 11:00:37 -04003927 path->leave_spinning = 1;
Li Zefan33345d012011-04-20 10:31:50 +08003928 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04003929 name, name_len, -1);
3930 if (IS_ERR(di)) {
3931 ret = PTR_ERR(di);
3932 goto err;
3933 }
3934 if (!di) {
3935 ret = -ENOENT;
3936 goto err;
3937 }
Chris Mason5f39d392007-10-15 16:14:19 -04003938 leaf = path->nodes[0];
3939 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04003940 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04003941 if (ret)
3942 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02003943 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003944
Miao Xie67de1172013-12-26 13:07:06 +08003945 /*
3946 * If we don't have dir index, we have to get it by looking up
3947 * the inode ref, since we get the inode ref, remove it directly,
3948 * it is unnecessary to do delayed deletion.
3949 *
3950 * But if we have dir index, needn't search inode ref to get it.
3951 * Since the inode ref is close to the inode item, it is better
3952 * that we delay to delete it, and just do this deletion when
3953 * we update the inode item.
3954 */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003955 if (inode->dir_index) {
Miao Xie67de1172013-12-26 13:07:06 +08003956 ret = btrfs_delayed_delete_inode_ref(inode);
3957 if (!ret) {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003958 index = inode->dir_index;
Miao Xie67de1172013-12-26 13:07:06 +08003959 goto skip_backref;
3960 }
3961 }
3962
Li Zefan33345d012011-04-20 10:31:50 +08003963 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
3964 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04003965 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003966 btrfs_info(fs_info,
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003967 "failed to delete reference to %.*s, inode %llu parent %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003968 name_len, name, ino, dir_ino);
Jeff Mahoney66642832016-06-10 18:19:25 -04003969 btrfs_abort_transaction(trans, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04003970 goto err;
3971 }
Miao Xie67de1172013-12-26 13:07:06 +08003972skip_backref:
Lu Fengqi9add2942018-08-01 11:32:26 +08003973 ret = btrfs_delete_delayed_dir_index(trans, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003974 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003975 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003976 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003977 }
Chris Mason39279cc2007-06-12 06:35:45 -04003978
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003979 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len, inode,
3980 dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003981 if (ret != 0 && ret != -ENOENT) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003982 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003983 goto err;
3984 }
Chris Masone02119d2008-09-05 16:13:11 -04003985
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003986 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len, dir,
3987 index);
Chris Mason6418c962010-10-30 07:34:24 -04003988 if (ret == -ENOENT)
3989 ret = 0;
Zach Brownd4e39912013-04-02 21:02:16 +00003990 else if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04003991 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003992err:
3993 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003994 if (ret)
3995 goto out;
3996
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02003997 btrfs_i_size_write(dir, dir->vfs_inode.i_size - name_len * 2);
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003998 inode_inc_iversion(&inode->vfs_inode);
3999 inode_inc_iversion(&dir->vfs_inode);
4000 inode->vfs_inode.i_ctime = dir->vfs_inode.i_mtime =
4001 dir->vfs_inode.i_ctime = current_time(&inode->vfs_inode);
4002 ret = btrfs_update_inode(trans, root, &dir->vfs_inode);
Chris Masone02119d2008-09-05 16:13:11 -04004003out:
Chris Mason39279cc2007-06-12 06:35:45 -04004004 return ret;
4005}
4006
Al Viro92986792011-03-04 17:14:37 +00004007int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
4008 struct btrfs_root *root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004009 struct btrfs_inode *dir, struct btrfs_inode *inode,
Al Viro92986792011-03-04 17:14:37 +00004010 const char *name, int name_len)
4011{
4012 int ret;
4013 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
4014 if (!ret) {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004015 drop_nlink(&inode->vfs_inode);
4016 ret = btrfs_update_inode(trans, root, &inode->vfs_inode);
Al Viro92986792011-03-04 17:14:37 +00004017 }
4018 return ret;
4019}
Yan, Zhenga22285a2010-05-16 10:48:46 -04004020
4021/*
4022 * helper to start transaction for unlink and rmdir.
4023 *
Josef Bacikd52be812013-05-29 14:54:47 -04004024 * unlink and rmdir are special in btrfs, they do not always free space, so
4025 * if we cannot make our reservations the normal way try and see if there is
4026 * plenty of slack room in the global reserve to migrate, otherwise we cannot
4027 * allow the unlink to occur.
Yan, Zhenga22285a2010-05-16 10:48:46 -04004028 */
Josef Bacikd52be812013-05-29 14:54:47 -04004029static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir)
Yan, Zhenga22285a2010-05-16 10:48:46 -04004030{
Yan, Zhenga22285a2010-05-16 10:48:46 -04004031 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004032
Josef Bacike70bea52011-10-11 14:18:24 -04004033 /*
4034 * 1 for the possible orphan item
4035 * 1 for the dir item
4036 * 1 for the dir index
4037 * 1 for the inode ref
Josef Bacike70bea52011-10-11 14:18:24 -04004038 * 1 for the inode
4039 */
Filipe Manana8eab77f2015-11-13 23:57:16 +00004040 return btrfs_start_transaction_fallback_global_rsv(root, 5, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004041}
4042
Chris Mason39279cc2007-06-12 06:35:45 -04004043static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
4044{
Yan, Zhenga22285a2010-05-16 10:48:46 -04004045 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004046 struct btrfs_trans_handle *trans;
David Howells2b0143b2015-03-17 22:25:59 +00004047 struct inode *inode = d_inode(dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04004048 int ret;
4049
Josef Bacikd52be812013-05-29 14:54:47 -04004050 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004051 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004052 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04004053
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004054 btrfs_record_unlink_dir(trans, BTRFS_I(dir), BTRFS_I(d_inode(dentry)),
4055 0);
Chris Mason12fcfd22009-03-24 10:24:20 -04004056
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004057 ret = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
4058 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
4059 dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00004060 if (ret)
4061 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004062
Yan, Zhenga22285a2010-05-16 10:48:46 -04004063 if (inode->i_nlink == 0) {
Nikolay Borisov73f2e542017-02-20 13:50:59 +02004064 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Tsutomu Itohb5324022011-07-19 07:27:20 +00004065 if (ret)
4066 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004067 }
Josef Bacik7b128762008-07-24 12:17:14 -04004068
Tsutomu Itohb5324022011-07-19 07:27:20 +00004069out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004070 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004071 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04004072 return ret;
4073}
4074
Misono Tomohirof60a2362018-04-18 11:34:52 +09004075static int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
Lu Fengqi401b3b12018-08-01 11:32:30 +08004076 struct inode *dir, u64 objectid,
4077 const char *name, int name_len)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004078{
Lu Fengqi401b3b12018-08-01 11:32:30 +08004079 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004080 struct btrfs_path *path;
4081 struct extent_buffer *leaf;
4082 struct btrfs_dir_item *di;
4083 struct btrfs_key key;
4084 u64 index;
4085 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004086 u64 dir_ino = btrfs_ino(BTRFS_I(dir));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004087
4088 path = btrfs_alloc_path();
4089 if (!path)
4090 return -ENOMEM;
4091
Li Zefan33345d012011-04-20 10:31:50 +08004092 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004093 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004094 if (IS_ERR_OR_NULL(di)) {
4095 if (!di)
4096 ret = -ENOENT;
4097 else
4098 ret = PTR_ERR(di);
4099 goto out;
4100 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004101
4102 leaf = path->nodes[0];
4103 btrfs_dir_item_key_to_cpu(leaf, di, &key);
4104 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
4105 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004106 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004107 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004108 goto out;
4109 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004110 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004111
Lu Fengqi3ee1c552018-08-01 11:32:28 +08004112 ret = btrfs_del_root_ref(trans, objectid, root->root_key.objectid,
4113 dir_ino, &index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004114 if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004115 if (ret != -ENOENT) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004116 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004117 goto out;
4118 }
Li Zefan33345d012011-04-20 10:31:50 +08004119 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004120 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004121 if (IS_ERR_OR_NULL(di)) {
4122 if (!di)
4123 ret = -ENOENT;
4124 else
4125 ret = PTR_ERR(di);
Jeff Mahoney66642832016-06-10 18:19:25 -04004126 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004127 goto out;
4128 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004129
4130 leaf = path->nodes[0];
4131 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004132 index = key.offset;
4133 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004134 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004135
Lu Fengqi9add2942018-08-01 11:32:26 +08004136 ret = btrfs_delete_delayed_dir_index(trans, BTRFS_I(dir), index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004137 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004138 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004139 goto out;
4140 }
Miao Xie16cdcec2011-04-22 18:12:22 +08004141
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004142 btrfs_i_size_write(BTRFS_I(dir), dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004143 inode_inc_iversion(dir);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07004144 dir->i_mtime = dir->i_ctime = current_time(dir);
Josef Bacik5a24e842012-08-08 10:12:59 -06004145 ret = btrfs_update_inode_fallback(trans, root, dir);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004146 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004147 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004148out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04004149 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004150 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004151}
4152
Misono Tomohiroec42f162018-04-18 11:34:13 +09004153/*
4154 * Helper to check if the subvolume references other subvolumes or if it's
4155 * default.
4156 */
Misono Tomohirof60a2362018-04-18 11:34:52 +09004157static noinline int may_destroy_subvol(struct btrfs_root *root)
Misono Tomohiroec42f162018-04-18 11:34:13 +09004158{
4159 struct btrfs_fs_info *fs_info = root->fs_info;
4160 struct btrfs_path *path;
4161 struct btrfs_dir_item *di;
4162 struct btrfs_key key;
4163 u64 dir_id;
4164 int ret;
4165
4166 path = btrfs_alloc_path();
4167 if (!path)
4168 return -ENOMEM;
4169
4170 /* Make sure this root isn't set as the default subvol */
4171 dir_id = btrfs_super_root_dir(fs_info->super_copy);
4172 di = btrfs_lookup_dir_item(NULL, fs_info->tree_root, path,
4173 dir_id, "default", 7, 0);
4174 if (di && !IS_ERR(di)) {
4175 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key);
4176 if (key.objectid == root->root_key.objectid) {
4177 ret = -EPERM;
4178 btrfs_err(fs_info,
4179 "deleting default subvolume %llu is not allowed",
4180 key.objectid);
4181 goto out;
4182 }
4183 btrfs_release_path(path);
4184 }
4185
4186 key.objectid = root->root_key.objectid;
4187 key.type = BTRFS_ROOT_REF_KEY;
4188 key.offset = (u64)-1;
4189
4190 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
4191 if (ret < 0)
4192 goto out;
4193 BUG_ON(ret == 0);
4194
4195 ret = 0;
4196 if (path->slots[0] > 0) {
4197 path->slots[0]--;
4198 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
4199 if (key.objectid == root->root_key.objectid &&
4200 key.type == BTRFS_ROOT_REF_KEY)
4201 ret = -ENOTEMPTY;
4202 }
4203out:
4204 btrfs_free_path(path);
4205 return ret;
4206}
4207
Nikolay Borisov20a68002018-04-27 14:36:24 +03004208/* Delete all dentries for inodes belonging to the root */
4209static void btrfs_prune_dentries(struct btrfs_root *root)
4210{
4211 struct btrfs_fs_info *fs_info = root->fs_info;
4212 struct rb_node *node;
4213 struct rb_node *prev;
4214 struct btrfs_inode *entry;
4215 struct inode *inode;
4216 u64 objectid = 0;
4217
4218 if (!test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
4219 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
4220
4221 spin_lock(&root->inode_lock);
4222again:
4223 node = root->inode_tree.rb_node;
4224 prev = NULL;
4225 while (node) {
4226 prev = node;
4227 entry = rb_entry(node, struct btrfs_inode, rb_node);
4228
David Sterba37508512018-06-29 10:56:40 +02004229 if (objectid < btrfs_ino(entry))
Nikolay Borisov20a68002018-04-27 14:36:24 +03004230 node = node->rb_left;
David Sterba37508512018-06-29 10:56:40 +02004231 else if (objectid > btrfs_ino(entry))
Nikolay Borisov20a68002018-04-27 14:36:24 +03004232 node = node->rb_right;
4233 else
4234 break;
4235 }
4236 if (!node) {
4237 while (prev) {
4238 entry = rb_entry(prev, struct btrfs_inode, rb_node);
David Sterba37508512018-06-29 10:56:40 +02004239 if (objectid <= btrfs_ino(entry)) {
Nikolay Borisov20a68002018-04-27 14:36:24 +03004240 node = prev;
4241 break;
4242 }
4243 prev = rb_next(prev);
4244 }
4245 }
4246 while (node) {
4247 entry = rb_entry(node, struct btrfs_inode, rb_node);
David Sterba37508512018-06-29 10:56:40 +02004248 objectid = btrfs_ino(entry) + 1;
Nikolay Borisov20a68002018-04-27 14:36:24 +03004249 inode = igrab(&entry->vfs_inode);
4250 if (inode) {
4251 spin_unlock(&root->inode_lock);
4252 if (atomic_read(&inode->i_count) > 1)
4253 d_prune_aliases(inode);
4254 /*
4255 * btrfs_drop_inode will have it removed from the inode
4256 * cache when its usage count hits zero.
4257 */
4258 iput(inode);
4259 cond_resched();
4260 spin_lock(&root->inode_lock);
4261 goto again;
4262 }
4263
4264 if (cond_resched_lock(&root->inode_lock))
4265 goto again;
4266
4267 node = rb_next(node);
4268 }
4269 spin_unlock(&root->inode_lock);
4270}
4271
Misono Tomohirof60a2362018-04-18 11:34:52 +09004272int btrfs_delete_subvolume(struct inode *dir, struct dentry *dentry)
4273{
4274 struct btrfs_fs_info *fs_info = btrfs_sb(dentry->d_sb);
4275 struct btrfs_root *root = BTRFS_I(dir)->root;
4276 struct inode *inode = d_inode(dentry);
4277 struct btrfs_root *dest = BTRFS_I(inode)->root;
4278 struct btrfs_trans_handle *trans;
4279 struct btrfs_block_rsv block_rsv;
4280 u64 root_flags;
Misono Tomohirof60a2362018-04-18 11:34:52 +09004281 int ret;
4282 int err;
4283
4284 /*
4285 * Don't allow to delete a subvolume with send in progress. This is
4286 * inside the inode lock so the error handling that has to drop the bit
4287 * again is not run concurrently.
4288 */
4289 spin_lock(&dest->root_item_lock);
4290 root_flags = btrfs_root_flags(&dest->root_item);
4291 if (dest->send_in_progress == 0) {
4292 btrfs_set_root_flags(&dest->root_item,
4293 root_flags | BTRFS_ROOT_SUBVOL_DEAD);
4294 spin_unlock(&dest->root_item_lock);
4295 } else {
4296 spin_unlock(&dest->root_item_lock);
4297 btrfs_warn(fs_info,
4298 "attempt to delete subvolume %llu during send",
4299 dest->root_key.objectid);
4300 return -EPERM;
4301 }
4302
4303 down_write(&fs_info->subvol_sem);
4304
4305 err = may_destroy_subvol(dest);
4306 if (err)
4307 goto out_up_write;
4308
4309 btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP);
4310 /*
4311 * One for dir inode,
4312 * two for dir entries,
4313 * two for root ref/backref.
4314 */
Gu JinXiangc4c129d2018-05-30 11:00:38 +08004315 err = btrfs_subvolume_reserve_metadata(root, &block_rsv, 5, true);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004316 if (err)
4317 goto out_up_write;
4318
4319 trans = btrfs_start_transaction(root, 0);
4320 if (IS_ERR(trans)) {
4321 err = PTR_ERR(trans);
4322 goto out_release;
4323 }
4324 trans->block_rsv = &block_rsv;
4325 trans->bytes_reserved = block_rsv.size;
4326
4327 btrfs_record_snapshot_destroy(trans, BTRFS_I(dir));
4328
Lu Fengqi401b3b12018-08-01 11:32:30 +08004329 ret = btrfs_unlink_subvol(trans, dir, dest->root_key.objectid,
4330 dentry->d_name.name, dentry->d_name.len);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004331 if (ret) {
4332 err = ret;
4333 btrfs_abort_transaction(trans, ret);
4334 goto out_end_trans;
4335 }
4336
4337 btrfs_record_root_in_trans(trans, dest);
4338
4339 memset(&dest->root_item.drop_progress, 0,
4340 sizeof(dest->root_item.drop_progress));
4341 dest->root_item.drop_level = 0;
4342 btrfs_set_root_refs(&dest->root_item, 0);
4343
4344 if (!test_and_set_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &dest->state)) {
4345 ret = btrfs_insert_orphan_item(trans,
4346 fs_info->tree_root,
4347 dest->root_key.objectid);
4348 if (ret) {
4349 btrfs_abort_transaction(trans, ret);
4350 err = ret;
4351 goto out_end_trans;
4352 }
4353 }
4354
Lu Fengqid1957792018-05-29 15:01:54 +08004355 ret = btrfs_uuid_tree_remove(trans, dest->root_item.uuid,
Misono Tomohirof60a2362018-04-18 11:34:52 +09004356 BTRFS_UUID_KEY_SUBVOL,
4357 dest->root_key.objectid);
4358 if (ret && ret != -ENOENT) {
4359 btrfs_abort_transaction(trans, ret);
4360 err = ret;
4361 goto out_end_trans;
4362 }
4363 if (!btrfs_is_empty_uuid(dest->root_item.received_uuid)) {
Lu Fengqid1957792018-05-29 15:01:54 +08004364 ret = btrfs_uuid_tree_remove(trans,
Misono Tomohirof60a2362018-04-18 11:34:52 +09004365 dest->root_item.received_uuid,
4366 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
4367 dest->root_key.objectid);
4368 if (ret && ret != -ENOENT) {
4369 btrfs_abort_transaction(trans, ret);
4370 err = ret;
4371 goto out_end_trans;
4372 }
4373 }
4374
4375out_end_trans:
4376 trans->block_rsv = NULL;
4377 trans->bytes_reserved = 0;
4378 ret = btrfs_end_transaction(trans);
4379 if (ret && !err)
4380 err = ret;
4381 inode->i_flags |= S_DEAD;
4382out_release:
4383 btrfs_subvolume_release_metadata(fs_info, &block_rsv);
4384out_up_write:
4385 up_write(&fs_info->subvol_sem);
4386 if (err) {
4387 spin_lock(&dest->root_item_lock);
4388 root_flags = btrfs_root_flags(&dest->root_item);
4389 btrfs_set_root_flags(&dest->root_item,
4390 root_flags & ~BTRFS_ROOT_SUBVOL_DEAD);
4391 spin_unlock(&dest->root_item_lock);
4392 } else {
4393 d_invalidate(dentry);
Nikolay Borisov20a68002018-04-27 14:36:24 +03004394 btrfs_prune_dentries(dest);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004395 ASSERT(dest->send_in_progress == 0);
4396
4397 /* the last ref */
4398 if (dest->ino_cache_inode) {
4399 iput(dest->ino_cache_inode);
4400 dest->ino_cache_inode = NULL;
4401 }
4402 }
4403
4404 return err;
4405}
4406
Chris Mason39279cc2007-06-12 06:35:45 -04004407static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
4408{
David Howells2b0143b2015-03-17 22:25:59 +00004409 struct inode *inode = d_inode(dentry);
Chris Mason1832a6d2007-12-21 16:27:21 -05004410 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004411 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004412 struct btrfs_trans_handle *trans;
Filipe Manana44f714d2016-06-06 16:11:13 +01004413 u64 last_unlink_trans;
Chris Mason39279cc2007-06-12 06:35:45 -04004414
David Sterbab3ae2442012-09-13 16:04:34 -06004415 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Yan134d4512007-10-25 15:49:25 -04004416 return -ENOTEMPTY;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004417 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_FIRST_FREE_OBJECTID)
Misono Tomohiroa79a4642018-04-18 11:35:31 +09004418 return btrfs_delete_subvolume(dir, dentry);
Yan134d4512007-10-25 15:49:25 -04004419
Josef Bacikd52be812013-05-29 14:54:47 -04004420 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004421 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004422 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004423
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004424 if (unlikely(btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Lu Fengqi401b3b12018-08-01 11:32:30 +08004425 err = btrfs_unlink_subvol(trans, dir,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004426 BTRFS_I(inode)->location.objectid,
4427 dentry->d_name.name,
4428 dentry->d_name.len);
4429 goto out;
4430 }
4431
Nikolay Borisov73f2e542017-02-20 13:50:59 +02004432 err = btrfs_orphan_add(trans, BTRFS_I(inode));
Josef Bacik7b128762008-07-24 12:17:14 -04004433 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004434 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004435
Filipe Manana44f714d2016-06-06 16:11:13 +01004436 last_unlink_trans = BTRFS_I(inode)->last_unlink_trans;
4437
Chris Mason39279cc2007-06-12 06:35:45 -04004438 /* now the directory is empty */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004439 err = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
4440 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
4441 dentry->d_name.len);
Filipe Manana44f714d2016-06-06 16:11:13 +01004442 if (!err) {
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004443 btrfs_i_size_write(BTRFS_I(inode), 0);
Filipe Manana44f714d2016-06-06 16:11:13 +01004444 /*
4445 * Propagate the last_unlink_trans value of the deleted dir to
4446 * its parent directory. This is to prevent an unrecoverable
4447 * log tree in the case we do something like this:
4448 * 1) create dir foo
4449 * 2) create snapshot under dir foo
4450 * 3) delete the snapshot
4451 * 4) rmdir foo
4452 * 5) mkdir foo
4453 * 6) fsync foo or some file inside foo
4454 */
4455 if (last_unlink_trans >= trans->transid)
4456 BTRFS_I(dir)->last_unlink_trans = last_unlink_trans;
4457 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004458out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004459 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004460 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39544012007-12-12 14:38:19 -05004461
Chris Mason39279cc2007-06-12 06:35:45 -04004462 return err;
4463}
4464
Chris Mason28f75a02015-02-04 06:59:29 -08004465static int truncate_space_check(struct btrfs_trans_handle *trans,
4466 struct btrfs_root *root,
4467 u64 bytes_deleted)
4468{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004469 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason28f75a02015-02-04 06:59:29 -08004470 int ret;
4471
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004472 /*
4473 * This is only used to apply pressure to the enospc system, we don't
4474 * intend to use this reservation at all.
4475 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004476 bytes_deleted = btrfs_csum_bytes_to_leaves(fs_info, bytes_deleted);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004477 bytes_deleted *= fs_info->nodesize;
4478 ret = btrfs_block_rsv_add(root, &fs_info->trans_block_rsv,
Chris Mason28f75a02015-02-04 06:59:29 -08004479 bytes_deleted, BTRFS_RESERVE_NO_FLUSH);
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004480 if (!ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004481 trace_btrfs_space_reservation(fs_info, "transaction",
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004482 trans->transid,
4483 bytes_deleted, 1);
Chris Mason28f75a02015-02-04 06:59:29 -08004484 trans->bytes_reserved += bytes_deleted;
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004485 }
Chris Mason28f75a02015-02-04 06:59:29 -08004486 return ret;
4487
4488}
4489
Josef Bacikddfae632017-10-19 14:16:02 -04004490/*
4491 * Return this if we need to call truncate_block for the last bit of the
4492 * truncate.
4493 */
4494#define NEED_TRUNCATE_BLOCK 1
Filipe Manana0305cd52015-10-16 12:34:25 +01004495
Chris Mason323ac952008-10-01 19:05:46 -04004496/*
Chris Mason39279cc2007-06-12 06:35:45 -04004497 * this can truncate away extent items, csum items and directory items.
4498 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04004499 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04004500 *
4501 * csum items that cross the new i_size are truncated to the new size
4502 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04004503 *
4504 * min_type is the minimum key type to truncate down to. If set to 0, this
4505 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04004506 */
Yan, Zheng80825102009-11-12 09:35:36 +00004507int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
4508 struct btrfs_root *root,
4509 struct inode *inode,
4510 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004511{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004512 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04004513 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004514 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004515 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00004516 struct btrfs_key key;
4517 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04004518 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04004519 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004520 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004521 u64 item_end = 0;
Filipe Mananac1aa4572015-06-20 18:20:09 +01004522 u64 last_size = new_size;
Yan, Zheng80825102009-11-12 09:35:36 +00004523 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04004524 int found_extent;
4525 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05004526 int pending_del_nr = 0;
4527 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04004528 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00004529 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004530 u64 ino = btrfs_ino(BTRFS_I(inode));
Chris Mason28ed1342014-12-17 09:41:04 -08004531 u64 bytes_deleted = 0;
Thomas Meyer897ca812017-10-07 16:02:21 +02004532 bool be_nice = false;
4533 bool should_throttle = false;
4534 bool should_end = false;
Yan, Zheng80825102009-11-12 09:35:36 +00004535
4536 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04004537
Chris Mason28ed1342014-12-17 09:41:04 -08004538 /*
4539 * for non-free space inodes and ref cows, we want to back off from
4540 * time to time
4541 */
Nikolay Borisov70ddc552017-02-20 13:50:35 +02004542 if (!btrfs_is_free_space_inode(BTRFS_I(inode)) &&
Chris Mason28ed1342014-12-17 09:41:04 -08004543 test_bit(BTRFS_ROOT_REF_COWS, &root->state))
Thomas Meyer897ca812017-10-07 16:02:21 +02004544 be_nice = true;
Chris Mason28ed1342014-12-17 09:41:04 -08004545
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004546 path = btrfs_alloc_path();
4547 if (!path)
4548 return -ENOMEM;
David Sterbae4058b52015-11-27 16:31:35 +01004549 path->reada = READA_BACK;
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004550
Josef Bacik5dc562c2012-08-17 13:14:17 -04004551 /*
4552 * We want to drop from the next block forward in case this new size is
4553 * not block aligned since we will be keeping the last block of the
4554 * extent just the way it is.
4555 */
Miao Xie27cdeb72014-04-02 19:51:05 +08004556 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004557 root == fs_info->tree_root)
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02004558 btrfs_drop_extent_cache(BTRFS_I(inode), ALIGN(new_size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004559 fs_info->sectorsize),
Jeff Mahoneyda170662016-06-15 09:22:56 -04004560 (u64)-1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00004561
Miao Xie16cdcec2011-04-22 18:12:22 +08004562 /*
4563 * This function is also used to drop the items in the log tree before
4564 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
4565 * it is used to drop the loged items. So we shouldn't kill the delayed
4566 * items.
4567 */
4568 if (min_type == 0 && root == BTRFS_I(inode)->root)
Nikolay Borisov4ccb5c72017-01-10 20:35:38 +02004569 btrfs_kill_delayed_inode_items(BTRFS_I(inode));
Miao Xie16cdcec2011-04-22 18:12:22 +08004570
Li Zefan33345d012011-04-20 10:31:50 +08004571 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04004572 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04004573 key.type = (u8)-1;
4574
Chris Mason85e21ba2008-01-29 15:11:36 -05004575search_again:
Chris Mason28ed1342014-12-17 09:41:04 -08004576 /*
4577 * with a 16K leaf size and 128MB extents, you can actually queue
4578 * up a huge file in a single leaf. Most of the time that
4579 * bytes_deleted is > 0, it will be huge by the time we get here
4580 */
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004581 if (be_nice && bytes_deleted > SZ_32M &&
4582 btrfs_should_end_transaction(trans)) {
4583 ret = -EAGAIN;
Yan, Zheng80825102009-11-12 09:35:36 +00004584 goto out;
4585 }
Chris Masond3977122009-01-05 21:25:51 -05004586
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004587 path->leave_spinning = 1;
4588 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
4589 if (ret < 0)
4590 goto out;
4591
Chris Mason85e21ba2008-01-29 15:11:36 -05004592 if (ret > 0) {
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004593 ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -04004594 /* there are no items in the tree for us to truncate, we're
4595 * done
4596 */
Yan, Zheng80825102009-11-12 09:35:36 +00004597 if (path->slots[0] == 0)
4598 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05004599 path->slots[0]--;
4600 }
4601
Chris Masond3977122009-01-05 21:25:51 -05004602 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04004603 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04004604 leaf = path->nodes[0];
4605 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02004606 found_type = found_key.type;
Chris Mason39279cc2007-06-12 06:35:45 -04004607
Li Zefan33345d012011-04-20 10:31:50 +08004608 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04004609 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004610
Chris Mason85e21ba2008-01-29 15:11:36 -05004611 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004612 break;
4613
Chris Mason5f39d392007-10-15 16:14:19 -04004614 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04004615 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04004616 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04004617 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04004618 extent_type = btrfs_file_extent_type(leaf, fi);
4619 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04004620 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04004621 btrfs_file_extent_num_bytes(leaf, fi);
Liu Bo09ed2f12017-03-10 11:09:48 -08004622
4623 trace_btrfs_truncate_show_fi_regular(
4624 BTRFS_I(inode), leaf, fi,
4625 found_key.offset);
Chris Mason179e29e2007-11-01 11:28:41 -04004626 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Qu Wenruoe41ca582018-06-06 15:41:49 +08004627 item_end += btrfs_file_extent_ram_bytes(leaf,
4628 fi);
Liu Bo09ed2f12017-03-10 11:09:48 -08004629
4630 trace_btrfs_truncate_show_fi_inline(
4631 BTRFS_I(inode), leaf, fi, path->slots[0],
4632 found_key.offset);
Chris Mason39279cc2007-06-12 06:35:45 -04004633 }
Yan008630c2007-11-07 13:31:09 -05004634 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04004635 }
Yan, Zheng80825102009-11-12 09:35:36 +00004636 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04004637 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00004638 } else {
Filipe Manana76b42ab2017-02-14 16:56:01 +00004639 if (item_end < new_size)
Yan, Zheng80825102009-11-12 09:35:36 +00004640 break;
4641 if (found_key.offset >= new_size)
4642 del_item = 1;
4643 else
4644 del_item = 0;
4645 }
Chris Mason39279cc2007-06-12 06:35:45 -04004646 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004647 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04004648 if (found_type != BTRFS_EXTENT_DATA_KEY)
4649 goto delete;
4650
4651 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04004652 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04004653 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05004654 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04004655 u64 orig_num_bytes =
4656 btrfs_file_extent_num_bytes(leaf, fi);
Qu Wenruofda28322013-02-26 08:10:22 +00004657 extent_num_bytes = ALIGN(new_size -
4658 found_key.offset,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004659 fs_info->sectorsize);
Chris Masondb945352007-10-15 16:15:53 -04004660 btrfs_set_file_extent_num_bytes(leaf, fi,
4661 extent_num_bytes);
4662 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05004663 extent_num_bytes);
Miao Xie27cdeb72014-04-02 19:51:05 +08004664 if (test_bit(BTRFS_ROOT_REF_COWS,
4665 &root->state) &&
4666 extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004667 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04004668 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004669 } else {
Chris Masondb945352007-10-15 16:15:53 -04004670 extent_num_bytes =
4671 btrfs_file_extent_disk_num_bytes(leaf,
4672 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004673 extent_offset = found_key.offset -
4674 btrfs_file_extent_offset(leaf, fi);
4675
Chris Mason39279cc2007-06-12 06:35:45 -04004676 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05004677 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004678 if (extent_start != 0) {
4679 found_extent = 1;
Miao Xie27cdeb72014-04-02 19:51:05 +08004680 if (test_bit(BTRFS_ROOT_REF_COWS,
4681 &root->state))
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004682 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04004683 }
4684 }
Chris Mason90692182008-02-08 13:49:28 -05004685 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04004686 /*
4687 * we can't truncate inline items that have had
4688 * special encodings
4689 */
4690 if (!del_item &&
Chris Masonc8b97812008-10-29 14:49:59 -04004691 btrfs_file_extent_encryption(leaf, fi) == 0 &&
Josef Bacikddfae632017-10-19 14:16:02 -04004692 btrfs_file_extent_other_encoding(leaf, fi) == 0 &&
4693 btrfs_file_extent_compression(leaf, fi) == 0) {
4694 u32 size = (u32)(new_size - found_key.offset);
Chris Mason514ac8a2014-01-03 21:07:00 -08004695
Josef Bacikddfae632017-10-19 14:16:02 -04004696 btrfs_set_file_extent_ram_bytes(leaf, fi, size);
4697 size = btrfs_file_extent_calc_inline_size(size);
4698 btrfs_truncate_item(root->fs_info, path, size, 1);
4699 } else if (!del_item) {
Chris Mason514ac8a2014-01-03 21:07:00 -08004700 /*
Josef Bacikddfae632017-10-19 14:16:02 -04004701 * We have to bail so the last_size is set to
4702 * just before this extent.
Chris Mason514ac8a2014-01-03 21:07:00 -08004703 */
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004704 ret = NEED_TRUNCATE_BLOCK;
Josef Bacikddfae632017-10-19 14:16:02 -04004705 break;
Chris Mason90692182008-02-08 13:49:28 -05004706 }
Josef Bacikddfae632017-10-19 14:16:02 -04004707
4708 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state))
4709 inode_sub_bytes(inode, item_end + 1 - new_size);
Chris Mason39279cc2007-06-12 06:35:45 -04004710 }
Chris Mason179e29e2007-11-01 11:28:41 -04004711delete:
Josef Bacikddfae632017-10-19 14:16:02 -04004712 if (del_item)
4713 last_size = found_key.offset;
4714 else
4715 last_size = new_size;
Chris Mason39279cc2007-06-12 06:35:45 -04004716 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05004717 if (!pending_del_nr) {
4718 /* no pending yet, add ourselves */
4719 pending_del_slot = path->slots[0];
4720 pending_del_nr = 1;
4721 } else if (pending_del_nr &&
4722 path->slots[0] + 1 == pending_del_slot) {
4723 /* hop on the pending chunk */
4724 pending_del_nr++;
4725 pending_del_slot = path->slots[0];
4726 } else {
Chris Masond3977122009-01-05 21:25:51 -05004727 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05004728 }
Chris Mason39279cc2007-06-12 06:35:45 -04004729 } else {
4730 break;
4731 }
Thomas Meyer897ca812017-10-07 16:02:21 +02004732 should_throttle = false;
Chris Mason28f75a02015-02-04 06:59:29 -08004733
Miao Xie27cdeb72014-04-02 19:51:05 +08004734 if (found_extent &&
4735 (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004736 root == fs_info->tree_root)) {
Chris Masonb9473432009-03-13 11:00:37 -04004737 btrfs_set_path_blocking(path);
Chris Mason28ed1342014-12-17 09:41:04 -08004738 bytes_deleted += extent_num_bytes;
Josef Bacik84f7d8e2017-09-29 15:43:49 -04004739 ret = btrfs_free_extent(trans, root, extent_start,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004740 extent_num_bytes, 0,
4741 btrfs_header_owner(leaf),
Filipe Mananab06c4bf2015-10-23 07:52:54 +01004742 ino, extent_offset);
Omar Sandoval05522102018-05-11 13:13:31 -07004743 if (ret) {
4744 btrfs_abort_transaction(trans, ret);
4745 break;
4746 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004747 if (btrfs_should_throttle_delayed_refs(trans, fs_info))
4748 btrfs_async_run_delayed_refs(fs_info,
Wang Xiaoguangdd4b8572016-10-18 15:56:13 +08004749 trans->delayed_ref_updates * 2,
4750 trans->transid, 0);
Chris Mason28f75a02015-02-04 06:59:29 -08004751 if (be_nice) {
4752 if (truncate_space_check(trans, root,
4753 extent_num_bytes)) {
Thomas Meyer897ca812017-10-07 16:02:21 +02004754 should_end = true;
Chris Mason28f75a02015-02-04 06:59:29 -08004755 }
4756 if (btrfs_should_throttle_delayed_refs(trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004757 fs_info))
Thomas Meyer897ca812017-10-07 16:02:21 +02004758 should_throttle = true;
Chris Mason28f75a02015-02-04 06:59:29 -08004759 }
Chris Mason39279cc2007-06-12 06:35:45 -04004760 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004761
Yan, Zheng80825102009-11-12 09:35:36 +00004762 if (found_type == BTRFS_INODE_ITEM_KEY)
4763 break;
4764
4765 if (path->slots[0] == 0 ||
Josef Bacik12621332015-02-03 07:50:16 -08004766 path->slots[0] != pending_del_slot ||
Chris Mason28f75a02015-02-04 06:59:29 -08004767 should_throttle || should_end) {
Yan, Zheng80825102009-11-12 09:35:36 +00004768 if (pending_del_nr) {
4769 ret = btrfs_del_items(trans, root, path,
4770 pending_del_slot,
4771 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004772 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004773 btrfs_abort_transaction(trans, ret);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004774 break;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004775 }
Yan, Zheng80825102009-11-12 09:35:36 +00004776 pending_del_nr = 0;
4777 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004778 btrfs_release_path(path);
Chris Mason28f75a02015-02-04 06:59:29 -08004779 if (should_throttle) {
Josef Bacik12621332015-02-03 07:50:16 -08004780 unsigned long updates = trans->delayed_ref_updates;
4781 if (updates) {
4782 trans->delayed_ref_updates = 0;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004783 ret = btrfs_run_delayed_refs(trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004784 updates * 2);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004785 if (ret)
4786 break;
Josef Bacik12621332015-02-03 07:50:16 -08004787 }
4788 }
Chris Mason28f75a02015-02-04 06:59:29 -08004789 /*
4790 * if we failed to refill our space rsv, bail out
4791 * and let the transaction restart
4792 */
4793 if (should_end) {
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004794 ret = -EAGAIN;
4795 break;
Chris Mason28f75a02015-02-04 06:59:29 -08004796 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004797 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00004798 } else {
4799 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05004800 }
Chris Mason39279cc2007-06-12 06:35:45 -04004801 }
Yan, Zheng80825102009-11-12 09:35:36 +00004802out:
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004803 if (ret >= 0 && pending_del_nr) {
4804 int err;
4805
4806 err = btrfs_del_items(trans, root, path, pending_del_slot,
Chris Mason85e21ba2008-01-29 15:11:36 -05004807 pending_del_nr);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004808 if (err) {
4809 btrfs_abort_transaction(trans, err);
4810 ret = err;
4811 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004812 }
Filipe Manana76b42ab2017-02-14 16:56:01 +00004813 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
4814 ASSERT(last_size >= new_size);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004815 if (!ret && last_size > new_size)
Filipe Manana76b42ab2017-02-14 16:56:01 +00004816 last_size = new_size;
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004817 btrfs_ordered_update_i_size(inode, last_size, NULL);
Filipe Manana76b42ab2017-02-14 16:56:01 +00004818 }
Chris Mason28ed1342014-12-17 09:41:04 -08004819
Chris Mason39279cc2007-06-12 06:35:45 -04004820 btrfs_free_path(path);
Chris Mason28ed1342014-12-17 09:41:04 -08004821
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004822 if (be_nice && bytes_deleted > SZ_32M && (ret >= 0 || ret == -EAGAIN)) {
Chris Mason28ed1342014-12-17 09:41:04 -08004823 unsigned long updates = trans->delayed_ref_updates;
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004824 int err;
4825
Chris Mason28ed1342014-12-17 09:41:04 -08004826 if (updates) {
4827 trans->delayed_ref_updates = 0;
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004828 err = btrfs_run_delayed_refs(trans, updates * 2);
4829 if (err)
4830 ret = err;
Chris Mason28ed1342014-12-17 09:41:04 -08004831 }
4832 }
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004833 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04004834}
4835
Chris Masona52d9a82007-08-27 16:49:44 -04004836/*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304837 * btrfs_truncate_block - read, zero a chunk and write a block
Josef Bacik2aaa6652012-08-29 14:27:18 -04004838 * @inode - inode that we're zeroing
4839 * @from - the offset to start zeroing
4840 * @len - the length to zero, 0 to zero the entire range respective to the
4841 * offset
4842 * @front - zero up to the offset instead of from the offset on
4843 *
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304844 * This will find the block for the "from" offset and cow the block and zero the
Josef Bacik2aaa6652012-08-29 14:27:18 -04004845 * part we want to zero. This is used with truncate and hole punching.
Chris Masona52d9a82007-08-27 16:49:44 -04004846 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304847int btrfs_truncate_block(struct inode *inode, loff_t from, loff_t len,
Josef Bacik2aaa6652012-08-29 14:27:18 -04004848 int front)
Chris Masona52d9a82007-08-27 16:49:44 -04004849{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004850 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004851 struct address_space *mapping = inode->i_mapping;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004852 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4853 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004854 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08004855 struct extent_changeset *data_reserved = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004856 char *kaddr;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004857 u32 blocksize = fs_info->sectorsize;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004858 pgoff_t index = from >> PAGE_SHIFT;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304859 unsigned offset = from & (blocksize - 1);
Chris Masona52d9a82007-08-27 16:49:44 -04004860 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004861 gfp_t mask = btrfs_alloc_write_mask(mapping);
Chris Masona52d9a82007-08-27 16:49:44 -04004862 int ret = 0;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304863 u64 block_start;
4864 u64 block_end;
Chris Masona52d9a82007-08-27 16:49:44 -04004865
Nikolay Borisovb03ebd92018-01-18 14:47:06 +02004866 if (IS_ALIGNED(offset, blocksize) &&
4867 (!len || IS_ALIGNED(len, blocksize)))
Chris Masona52d9a82007-08-27 16:49:44 -04004868 goto out;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304869
Josef Bacik8b62f872017-10-19 14:15:55 -04004870 block_start = round_down(from, blocksize);
4871 block_end = block_start + blocksize - 1;
4872
Qu Wenruo364ecf32017-02-27 15:10:38 +08004873 ret = btrfs_delalloc_reserve_space(inode, &data_reserved,
Josef Bacik8b62f872017-10-19 14:15:55 -04004874 block_start, blocksize);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004875 if (ret)
4876 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004877
Chris Mason211c17f2008-05-15 09:13:45 -04004878again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004879 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004880 if (!page) {
Qu Wenruobc42bda2017-02-27 15:10:39 +08004881 btrfs_delalloc_release_space(inode, data_reserved,
Qu Wenruo43b18592017-12-12 15:34:32 +08004882 block_start, blocksize, true);
4883 btrfs_delalloc_release_extents(BTRFS_I(inode), blocksize, true);
Miao Xieac6a2b32012-12-05 10:56:13 +00004884 ret = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -04004885 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04004886 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004887
Chris Masona52d9a82007-08-27 16:49:44 -04004888 if (!PageUptodate(page)) {
4889 ret = btrfs_readpage(NULL, page);
4890 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004891 if (page->mapping != mapping) {
4892 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004893 put_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004894 goto again;
4895 }
Chris Masona52d9a82007-08-27 16:49:44 -04004896 if (!PageUptodate(page)) {
4897 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04004898 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04004899 }
4900 }
Chris Mason211c17f2008-05-15 09:13:45 -04004901 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004902
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304903 lock_extent_bits(io_tree, block_start, block_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004904 set_page_extent_mapped(page);
4905
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304906 ordered = btrfs_lookup_ordered_extent(inode, block_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004907 if (ordered) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304908 unlock_extent_cached(io_tree, block_start, block_end,
David Sterbae43bbe52017-12-12 21:43:52 +01004909 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004910 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004911 put_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04004912 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004913 btrfs_put_ordered_extent(ordered);
4914 goto again;
4915 }
4916
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304917 clear_extent_bit(&BTRFS_I(inode)->io_tree, block_start, block_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06004918 EXTENT_DIRTY | EXTENT_DELALLOC |
4919 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
David Sterbaae0f1622017-10-31 16:37:52 +01004920 0, 0, &cached_state);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004921
Filipe Mananae3b8a482017-11-04 00:16:59 +00004922 ret = btrfs_set_extent_delalloc(inode, block_start, block_end, 0,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08004923 &cached_state, 0);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004924 if (ret) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304925 unlock_extent_cached(io_tree, block_start, block_end,
David Sterbae43bbe52017-12-12 21:43:52 +01004926 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004927 goto out_unlock;
4928 }
4929
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304930 if (offset != blocksize) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04004931 if (!len)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304932 len = blocksize - offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004933 kaddr = kmap(page);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004934 if (front)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304935 memset(kaddr + (block_start - page_offset(page)),
4936 0, offset);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004937 else
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304938 memset(kaddr + (block_start - page_offset(page)) + offset,
4939 0, len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004940 flush_dcache_page(page);
4941 kunmap(page);
4942 }
Chris Mason247e7432008-07-17 12:53:51 -04004943 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004944 set_page_dirty(page);
David Sterbae43bbe52017-12-12 21:43:52 +01004945 unlock_extent_cached(io_tree, block_start, block_end, &cached_state);
Chris Mason39279cc2007-06-12 06:35:45 -04004946
Chris Mason89642222008-07-24 09:41:53 -04004947out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04004948 if (ret)
Qu Wenruobc42bda2017-02-27 15:10:39 +08004949 btrfs_delalloc_release_space(inode, data_reserved, block_start,
Qu Wenruo43b18592017-12-12 15:34:32 +08004950 blocksize, true);
4951 btrfs_delalloc_release_extents(BTRFS_I(inode), blocksize, (ret != 0));
Chris Mason39279cc2007-06-12 06:35:45 -04004952 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004953 put_page(page);
Chris Mason39279cc2007-06-12 06:35:45 -04004954out:
Qu Wenruo364ecf32017-02-27 15:10:38 +08004955 extent_changeset_free(data_reserved);
Chris Mason39279cc2007-06-12 06:35:45 -04004956 return ret;
4957}
4958
Josef Bacik16e75492013-10-22 12:18:51 -04004959static int maybe_insert_hole(struct btrfs_root *root, struct inode *inode,
4960 u64 offset, u64 len)
4961{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004962 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik16e75492013-10-22 12:18:51 -04004963 struct btrfs_trans_handle *trans;
4964 int ret;
4965
4966 /*
4967 * Still need to make sure the inode looks like it's been updated so
4968 * that any holes get logged if we fsync.
4969 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004970 if (btrfs_fs_incompat(fs_info, NO_HOLES)) {
4971 BTRFS_I(inode)->last_trans = fs_info->generation;
Josef Bacik16e75492013-10-22 12:18:51 -04004972 BTRFS_I(inode)->last_sub_trans = root->log_transid;
4973 BTRFS_I(inode)->last_log_commit = root->last_log_commit;
4974 return 0;
4975 }
4976
4977 /*
4978 * 1 - for the one we're dropping
4979 * 1 - for the one we're adding
4980 * 1 - for updating the inode.
4981 */
4982 trans = btrfs_start_transaction(root, 3);
4983 if (IS_ERR(trans))
4984 return PTR_ERR(trans);
4985
4986 ret = btrfs_drop_extents(trans, root, inode, offset, offset + len, 1);
4987 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004988 btrfs_abort_transaction(trans, ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004989 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04004990 return ret;
4991 }
4992
David Sterbaf85b7372017-01-20 14:54:07 +01004993 ret = btrfs_insert_file_extent(trans, root, btrfs_ino(BTRFS_I(inode)),
4994 offset, 0, 0, len, 0, len, 0, 0, 0);
Josef Bacik16e75492013-10-22 12:18:51 -04004995 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004996 btrfs_abort_transaction(trans, ret);
Josef Bacik16e75492013-10-22 12:18:51 -04004997 else
4998 btrfs_update_inode(trans, root, inode);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004999 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04005000 return ret;
5001}
5002
Josef Bacik695a0d02011-03-04 15:46:53 -05005003/*
5004 * This function puts in dummy file extents for the area we're creating a hole
5005 * for. So if we are truncating this file to a larger size we need to insert
5006 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
5007 * the range between oldsize and size
5008 */
Josef Bacika41ad392011-01-31 15:30:16 -05005009int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04005010{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005011 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Yan Zheng9036c102008-10-30 14:19:41 -04005012 struct btrfs_root *root = BTRFS_I(inode)->root;
5013 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04005014 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00005015 struct extent_state *cached_state = NULL;
Josef Bacik5dc562c2012-08-17 13:14:17 -04005016 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005017 u64 hole_start = ALIGN(oldsize, fs_info->sectorsize);
5018 u64 block_end = ALIGN(size, fs_info->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04005019 u64 last_byte;
5020 u64 cur_offset;
5021 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04005022 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04005023
Josef Bacika71754f2013-06-17 17:14:39 -04005024 /*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05305025 * If our size started in the middle of a block we need to zero out the
5026 * rest of the block before we expand the i_size, otherwise we could
Josef Bacika71754f2013-06-17 17:14:39 -04005027 * expose stale data.
5028 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05305029 err = btrfs_truncate_block(inode, oldsize, 0, 0);
Josef Bacika71754f2013-06-17 17:14:39 -04005030 if (err)
5031 return err;
5032
Yan Zheng9036c102008-10-30 14:19:41 -04005033 if (size <= hole_start)
5034 return 0;
5035
Yan Zheng9036c102008-10-30 14:19:41 -04005036 while (1) {
5037 struct btrfs_ordered_extent *ordered;
Miao Xiefa7c1492013-09-26 13:15:27 +08005038
David Sterbaff13db42015-12-03 14:30:40 +01005039 lock_extent_bits(io_tree, hole_start, block_end - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01005040 &cached_state);
Nikolay Borisova776c6f2017-02-20 13:50:49 +02005041 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), hole_start,
Miao Xiefa7c1492013-09-26 13:15:27 +08005042 block_end - hole_start);
Yan Zheng9036c102008-10-30 14:19:41 -04005043 if (!ordered)
5044 break;
Josef Bacik2ac55d42010-02-03 19:33:23 +00005045 unlock_extent_cached(io_tree, hole_start, block_end - 1,
David Sterbae43bbe52017-12-12 21:43:52 +01005046 &cached_state);
Miao Xiefa7c1492013-09-26 13:15:27 +08005047 btrfs_start_ordered_extent(inode, ordered, 1);
Yan Zheng9036c102008-10-30 14:19:41 -04005048 btrfs_put_ordered_extent(ordered);
5049 }
5050
Yan Zheng9036c102008-10-30 14:19:41 -04005051 cur_offset = hole_start;
5052 while (1) {
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02005053 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, cur_offset,
Yan Zheng9036c102008-10-30 14:19:41 -04005054 block_end - cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005055 if (IS_ERR(em)) {
5056 err = PTR_ERR(em);
Zach Brownf2767952013-01-08 19:37:58 +00005057 em = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005058 break;
5059 }
Yan Zheng9036c102008-10-30 14:19:41 -04005060 last_byte = min(extent_map_end(em), block_end);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005061 last_byte = ALIGN(last_byte, fs_info->sectorsize);
Yan, Zheng80825102009-11-12 09:35:36 +00005062 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04005063 struct extent_map *hole_em;
Yan Zheng9036c102008-10-30 14:19:41 -04005064 hole_size = last_byte - cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00005065
Josef Bacik16e75492013-10-22 12:18:51 -04005066 err = maybe_insert_hole(root, inode, cur_offset,
5067 hole_size);
5068 if (err)
Chris Mason771ed682008-11-06 22:02:51 -05005069 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02005070 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04005071 cur_offset + hole_size - 1, 0);
5072 hole_em = alloc_extent_map();
5073 if (!hole_em) {
5074 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
5075 &BTRFS_I(inode)->runtime_flags);
5076 goto next;
5077 }
5078 hole_em->start = cur_offset;
5079 hole_em->len = hole_size;
5080 hole_em->orig_start = cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00005081
Josef Bacik5dc562c2012-08-17 13:14:17 -04005082 hole_em->block_start = EXTENT_MAP_HOLE;
5083 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05005084 hole_em->orig_block_len = 0;
Josef Bacikcc95bef2013-04-04 14:31:27 -04005085 hole_em->ram_bytes = hole_size;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005086 hole_em->bdev = fs_info->fs_devices->latest_bdev;
Josef Bacik5dc562c2012-08-17 13:14:17 -04005087 hole_em->compress_type = BTRFS_COMPRESS_NONE;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005088 hole_em->generation = fs_info->generation;
Josef Bacik5dc562c2012-08-17 13:14:17 -04005089
5090 while (1) {
5091 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04005092 err = add_extent_mapping(em_tree, hole_em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04005093 write_unlock(&em_tree->lock);
5094 if (err != -EEXIST)
5095 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02005096 btrfs_drop_extent_cache(BTRFS_I(inode),
5097 cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04005098 cur_offset +
5099 hole_size - 1, 0);
5100 }
5101 free_extent_map(hole_em);
Yan Zheng9036c102008-10-30 14:19:41 -04005102 }
Josef Bacik16e75492013-10-22 12:18:51 -04005103next:
Yan Zheng9036c102008-10-30 14:19:41 -04005104 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04005105 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04005106 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00005107 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04005108 break;
5109 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04005110 free_extent_map(em);
David Sterbae43bbe52017-12-12 21:43:52 +01005111 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state);
Yan Zheng9036c102008-10-30 14:19:41 -04005112 return err;
5113}
5114
Eric Sandeen3972f262013-01-12 02:57:22 +00005115static int btrfs_setsize(struct inode *inode, struct iattr *attr)
Yan, Zheng80825102009-11-12 09:35:36 +00005116{
Miao Xief4a2f4c2011-12-14 20:12:01 -05005117 struct btrfs_root *root = BTRFS_I(inode)->root;
5118 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05005119 loff_t oldsize = i_size_read(inode);
Eric Sandeen3972f262013-01-12 02:57:22 +00005120 loff_t newsize = attr->ia_size;
5121 int mask = attr->ia_valid;
Yan, Zheng80825102009-11-12 09:35:36 +00005122 int ret;
5123
Eric Sandeen3972f262013-01-12 02:57:22 +00005124 /*
5125 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
5126 * special case where we need to update the times despite not having
5127 * these flags set. For all other operations the VFS set these flags
5128 * explicitly if it wants a timestamp update.
5129 */
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08005130 if (newsize != oldsize) {
5131 inode_inc_iversion(inode);
5132 if (!(mask & (ATTR_CTIME | ATTR_MTIME)))
5133 inode->i_ctime = inode->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07005134 current_time(inode);
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08005135 }
Eric Sandeen3972f262013-01-12 02:57:22 +00005136
Josef Bacika41ad392011-01-31 15:30:16 -05005137 if (newsize > oldsize) {
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005138 /*
David Sterbaea14b57f2017-06-22 02:19:11 +02005139 * Don't do an expanding truncate while snapshotting is ongoing.
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005140 * This is to ensure the snapshot captures a fully consistent
5141 * state of this file - if the snapshot captures this expanding
5142 * truncation, it must capture all writes that happened before
5143 * this truncation.
5144 */
Zhao Lei0bc19f902016-01-06 18:56:36 +08005145 btrfs_wait_for_snapshot_creation(root);
Josef Bacika41ad392011-01-31 15:30:16 -05005146 ret = btrfs_cont_expand(inode, oldsize, newsize);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005147 if (ret) {
David Sterbaea14b57f2017-06-22 02:19:11 +02005148 btrfs_end_write_no_snapshotting(root);
Yan, Zheng80825102009-11-12 09:35:36 +00005149 return ret;
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005150 }
Yan, Zheng80825102009-11-12 09:35:36 +00005151
Miao Xief4a2f4c2011-12-14 20:12:01 -05005152 trans = btrfs_start_transaction(root, 1);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005153 if (IS_ERR(trans)) {
David Sterbaea14b57f2017-06-22 02:19:11 +02005154 btrfs_end_write_no_snapshotting(root);
Miao Xief4a2f4c2011-12-14 20:12:01 -05005155 return PTR_ERR(trans);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005156 }
Miao Xief4a2f4c2011-12-14 20:12:01 -05005157
5158 i_size_write(inode, newsize);
5159 btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
Chandan Rajendra27772b62016-01-21 15:56:03 +05305160 pagecache_isize_extended(inode, oldsize, newsize);
Miao Xief4a2f4c2011-12-14 20:12:01 -05005161 ret = btrfs_update_inode(trans, root, inode);
David Sterbaea14b57f2017-06-22 02:19:11 +02005162 btrfs_end_write_no_snapshotting(root);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005163 btrfs_end_transaction(trans);
Josef Bacika41ad392011-01-31 15:30:16 -05005164 } else {
Yan, Zheng80825102009-11-12 09:35:36 +00005165
Josef Bacika41ad392011-01-31 15:30:16 -05005166 /*
5167 * We're truncating a file that used to have good data down to
5168 * zero. Make sure it gets into the ordered flush list so that
5169 * any new writes get down to disk quickly.
5170 */
5171 if (newsize == 0)
Josef Bacik72ac3c02012-05-23 14:13:11 -04005172 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
5173 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00005174
Josef Bacika41ad392011-01-31 15:30:16 -05005175 truncate_setsize(inode, newsize);
Miao Xie2e60a512013-02-08 07:01:08 +00005176
5177 /* Disable nonlocked read DIO to avoid the end less truncate */
Nikolay Borisovabcefb12017-02-20 13:51:10 +02005178 btrfs_inode_block_unlocked_dio(BTRFS_I(inode));
Miao Xie2e60a512013-02-08 07:01:08 +00005179 inode_dio_wait(inode);
Nikolay Borisov0b581702017-02-20 13:51:11 +02005180 btrfs_inode_resume_unlocked_dio(BTRFS_I(inode));
Miao Xie2e60a512013-02-08 07:01:08 +00005181
Filipe Manana213e8c52018-02-06 20:40:31 +00005182 ret = btrfs_truncate(inode, newsize == oldsize);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005183 if (ret && inode->i_nlink) {
5184 int err;
5185
5186 /*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07005187 * Truncate failed, so fix up the in-memory size. We
5188 * adjusted disk_i_size down as we removed extents, so
5189 * wait for disk_i_size to be stable and then update the
5190 * in-memory size to match.
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005191 */
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07005192 err = btrfs_wait_ordered_range(inode, 0, (u64)-1);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005193 if (err)
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07005194 return err;
5195 i_size_write(inode, BTRFS_I(inode)->disk_i_size);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005196 }
Yan, Zheng80825102009-11-12 09:35:36 +00005197 }
5198
Josef Bacika41ad392011-01-31 15:30:16 -05005199 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00005200}
5201
Chris Mason39279cc2007-06-12 06:35:45 -04005202static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
5203{
David Howells2b0143b2015-03-17 22:25:59 +00005204 struct inode *inode = d_inode(dentry);
Li Zefanb83cc962010-12-20 16:04:08 +08005205 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005206 int err;
5207
Li Zefanb83cc962010-12-20 16:04:08 +08005208 if (btrfs_root_readonly(root))
5209 return -EROFS;
5210
Jan Kara31051c82016-05-26 16:55:18 +02005211 err = setattr_prepare(dentry, attr);
Chris Mason39279cc2007-06-12 06:35:45 -04005212 if (err)
5213 return err;
5214
Chris Mason5a3f23d2009-03-31 13:27:11 -04005215 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Eric Sandeen3972f262013-01-12 02:57:22 +00005216 err = btrfs_setsize(inode, attr);
Yan, Zheng80825102009-11-12 09:35:36 +00005217 if (err)
5218 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005219 }
Yan Zheng9036c102008-10-30 14:19:41 -04005220
Christoph Hellwig10257742010-06-04 11:30:02 +02005221 if (attr->ia_valid) {
5222 setattr_copy(inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005223 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005224 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04005225
Josef Bacik22c44fe2011-11-30 10:45:38 -05005226 if (!err && attr->ia_valid & ATTR_MODE)
Christoph Hellwig996a7102013-12-20 05:16:43 -08005227 err = posix_acl_chmod(inode, inode->i_mode);
Christoph Hellwig10257742010-06-04 11:30:02 +02005228 }
5229
Chris Mason39279cc2007-06-12 06:35:45 -04005230 return err;
5231}
Chris Mason61295eb2008-01-14 16:24:38 -05005232
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005233/*
5234 * While truncating the inode pages during eviction, we get the VFS calling
5235 * btrfs_invalidatepage() against each page of the inode. This is slow because
5236 * the calls to btrfs_invalidatepage() result in a huge amount of calls to
5237 * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting
5238 * extent_state structures over and over, wasting lots of time.
5239 *
5240 * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all
5241 * those expensive operations on a per page basis and do only the ordered io
5242 * finishing, while we release here the extent_map and extent_state structures,
5243 * without the excessive merging and splitting.
5244 */
5245static void evict_inode_truncate_pages(struct inode *inode)
5246{
5247 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
5248 struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree;
5249 struct rb_node *node;
5250
5251 ASSERT(inode->i_state & I_FREEING);
Johannes Weiner91b0abe2014-04-03 14:47:49 -07005252 truncate_inode_pages_final(&inode->i_data);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005253
5254 write_lock(&map_tree->lock);
5255 while (!RB_EMPTY_ROOT(&map_tree->map)) {
5256 struct extent_map *em;
5257
5258 node = rb_first(&map_tree->map);
5259 em = rb_entry(node, struct extent_map, rb_node);
Wang Shilong180589e2013-12-14 15:27:31 +08005260 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
5261 clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005262 remove_extent_mapping(map_tree, em);
5263 free_extent_map(em);
Filipe Manana7064dd52014-08-08 02:47:05 +01005264 if (need_resched()) {
5265 write_unlock(&map_tree->lock);
5266 cond_resched();
5267 write_lock(&map_tree->lock);
5268 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005269 }
5270 write_unlock(&map_tree->lock);
5271
Filipe Manana6ca07092015-05-26 00:55:42 +01005272 /*
5273 * Keep looping until we have no more ranges in the io tree.
5274 * We can have ongoing bios started by readpages (called from readahead)
Filipe Manana9c6429d2015-06-10 12:55:41 +01005275 * that have their endio callback (extent_io.c:end_bio_extent_readpage)
5276 * still in progress (unlocked the pages in the bio but did not yet
5277 * unlocked the ranges in the io tree). Therefore this means some
Filipe Manana6ca07092015-05-26 00:55:42 +01005278 * ranges can still be locked and eviction started because before
5279 * submitting those bios, which are executed by a separate task (work
5280 * queue kthread), inode references (inode->i_count) were not taken
5281 * (which would be dropped in the end io callback of each bio).
5282 * Therefore here we effectively end up waiting for those bios and
5283 * anyone else holding locked ranges without having bumped the inode's
5284 * reference count - if we don't do it, when they access the inode's
5285 * io_tree to unlock a range it may be too late, leading to an
5286 * use-after-free issue.
5287 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005288 spin_lock(&io_tree->lock);
5289 while (!RB_EMPTY_ROOT(&io_tree->state)) {
5290 struct extent_state *state;
5291 struct extent_state *cached_state = NULL;
Filipe Manana6ca07092015-05-26 00:55:42 +01005292 u64 start;
5293 u64 end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005294
5295 node = rb_first(&io_tree->state);
5296 state = rb_entry(node, struct extent_state, rb_node);
Filipe Manana6ca07092015-05-26 00:55:42 +01005297 start = state->start;
5298 end = state->end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005299 spin_unlock(&io_tree->lock);
5300
David Sterbaff13db42015-12-03 14:30:40 +01005301 lock_extent_bits(io_tree, start, end, &cached_state);
Qu Wenruob9d0b382015-09-29 10:35:16 +08005302
5303 /*
5304 * If still has DELALLOC flag, the extent didn't reach disk,
5305 * and its reserved space won't be freed by delayed_ref.
5306 * So we need to free its reserved space here.
5307 * (Refer to comment in btrfs_invalidatepage, case 2)
5308 *
5309 * Note, end is the bytenr of last byte, so we need + 1 here.
5310 */
5311 if (state->state & EXTENT_DELALLOC)
Qu Wenruobc42bda2017-02-27 15:10:39 +08005312 btrfs_qgroup_free_data(inode, NULL, start, end - start + 1);
Qu Wenruob9d0b382015-09-29 10:35:16 +08005313
Filipe Manana6ca07092015-05-26 00:55:42 +01005314 clear_extent_bit(io_tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005315 EXTENT_LOCKED | EXTENT_DIRTY |
5316 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
David Sterbaae0f1622017-10-31 16:37:52 +01005317 EXTENT_DEFRAG, 1, 1, &cached_state);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005318
Filipe Manana7064dd52014-08-08 02:47:05 +01005319 cond_resched();
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005320 spin_lock(&io_tree->lock);
5321 }
5322 spin_unlock(&io_tree->lock);
5323}
5324
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005325static struct btrfs_trans_handle *evict_refill_and_join(struct btrfs_root *root,
5326 struct btrfs_block_rsv *rsv,
5327 u64 min_size)
5328{
5329 struct btrfs_fs_info *fs_info = root->fs_info;
5330 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
5331 int failures = 0;
5332
5333 for (;;) {
5334 struct btrfs_trans_handle *trans;
5335 int ret;
5336
5337 ret = btrfs_block_rsv_refill(root, rsv, min_size,
5338 BTRFS_RESERVE_FLUSH_LIMIT);
5339
5340 if (ret && ++failures > 2) {
5341 btrfs_warn(fs_info,
5342 "could not allocate space for a delete; will truncate on mount");
5343 return ERR_PTR(-ENOSPC);
5344 }
5345
5346 trans = btrfs_join_transaction(root);
5347 if (IS_ERR(trans) || !ret)
5348 return trans;
5349
5350 /*
5351 * Try to steal from the global reserve if there is space for
5352 * it.
5353 */
5354 if (!btrfs_check_space_for_delayed_refs(trans, fs_info) &&
5355 !btrfs_block_rsv_migrate(global_rsv, rsv, min_size, 0))
5356 return trans;
5357
5358 /* If not, commit and try again. */
5359 ret = btrfs_commit_transaction(trans);
5360 if (ret)
5361 return ERR_PTR(ret);
5362 }
5363}
5364
Al Virobd555972010-06-07 11:35:40 -04005365void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005366{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005367 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04005368 struct btrfs_trans_handle *trans;
5369 struct btrfs_root *root = BTRFS_I(inode)->root;
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005370 struct btrfs_block_rsv *rsv;
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005371 u64 min_size;
Chris Mason39279cc2007-06-12 06:35:45 -04005372 int ret;
5373
liubo1abe9b82011-03-24 11:18:59 +00005374 trace_btrfs_inode_evict(inode);
5375
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005376 if (!root) {
Liu Boe8f1bc12018-01-25 11:02:53 -07005377 clear_inode(inode);
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005378 return;
5379 }
5380
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005381 min_size = btrfs_calc_trunc_metadata_size(fs_info, 1);
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005382
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005383 evict_inode_truncate_pages(inode);
5384
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005385 if (inode->i_nlink &&
5386 ((btrfs_root_refs(&root->root_item) != 0 &&
5387 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) ||
Nikolay Borisov70ddc552017-02-20 13:50:35 +02005388 btrfs_is_free_space_inode(BTRFS_I(inode))))
Al Virobd555972010-06-07 11:35:40 -04005389 goto no_delete;
5390
Omar Sandoval27919062018-05-11 13:13:37 -07005391 if (is_bad_inode(inode))
Chris Mason39279cc2007-06-12 06:35:45 -04005392 goto no_delete;
Al Virobd555972010-06-07 11:35:40 -04005393 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
Jeff Mahoneya30e5772015-09-11 21:44:17 -04005394 if (!special_file(inode->i_mode))
5395 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04005396
Nikolay Borisov7ab79562017-02-20 13:50:57 +02005397 btrfs_free_io_failure_record(BTRFS_I(inode), 0, (u64)-1);
Miao Xief6124962014-09-12 18:44:04 +08005398
Omar Sandoval7b40b692018-05-11 13:13:33 -07005399 if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags))
Yan, Zhengc71bf092009-11-12 09:34:40 +00005400 goto no_delete;
Yan, Zhengc71bf092009-11-12 09:34:40 +00005401
Yan, Zheng76dda932009-09-21 16:00:26 -04005402 if (inode->i_nlink > 0) {
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005403 BUG_ON(btrfs_root_refs(&root->root_item) != 0 &&
5404 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID);
Yan, Zheng76dda932009-09-21 16:00:26 -04005405 goto no_delete;
5406 }
5407
Nikolay Borisovaa790212017-01-10 20:35:40 +02005408 ret = btrfs_commit_inode_delayed_inode(BTRFS_I(inode));
Omar Sandoval27919062018-05-11 13:13:37 -07005409 if (ret)
Miao Xie0e8c36a2012-12-19 06:59:51 +00005410 goto no_delete;
Miao Xie0e8c36a2012-12-19 06:59:51 +00005411
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005412 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Omar Sandoval27919062018-05-11 13:13:37 -07005413 if (!rsv)
Josef Bacik4289a662011-08-05 13:22:24 -04005414 goto no_delete;
Josef Bacik4a338542011-08-29 11:01:31 -04005415 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04005416 rsv->failfast = 1;
Josef Bacik4289a662011-08-05 13:22:24 -04005417
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02005418 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Mason5f39d392007-10-15 16:14:19 -04005419
Yan, Zheng80825102009-11-12 09:35:36 +00005420 while (1) {
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005421 trans = evict_refill_and_join(root, rsv, min_size);
Omar Sandoval27919062018-05-11 13:13:37 -07005422 if (IS_ERR(trans))
5423 goto free_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04005424
5425 trans->block_rsv = rsv;
5426
Yan, Zhengd68fc572010-05-16 10:49:58 -04005427 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Omar Sandoval27919062018-05-11 13:13:37 -07005428 trans->block_rsv = &fs_info->trans_block_rsv;
5429 btrfs_end_transaction(trans);
5430 btrfs_btree_balance_dirty(fs_info);
5431 if (ret && ret != -ENOSPC && ret != -EAGAIN)
5432 goto free_rsv;
5433 else if (!ret)
Yan, Zheng80825102009-11-12 09:35:36 +00005434 break;
Josef Bacik7b128762008-07-24 12:17:14 -04005435 }
5436
Josef Bacik4ef31a42013-08-13 14:10:08 -04005437 /*
Omar Sandoval27919062018-05-11 13:13:37 -07005438 * Errors here aren't a big deal, it just means we leave orphan items in
5439 * the tree. They will be cleaned up on the next mount. If the inode
5440 * number gets reused, cleanup deletes the orphan item without doing
5441 * anything, and unlink reuses the existing orphan item.
5442 *
5443 * If it turns out that we are dropping too many of these, we might want
5444 * to add a mechanism for retrying these after a commit.
Josef Bacik4ef31a42013-08-13 14:10:08 -04005445 */
Omar Sandoval27919062018-05-11 13:13:37 -07005446 trans = evict_refill_and_join(root, rsv, min_size);
5447 if (!IS_ERR(trans)) {
5448 trans->block_rsv = rsv;
5449 btrfs_orphan_del(trans, BTRFS_I(inode));
5450 trans->block_rsv = &fs_info->trans_block_rsv;
5451 btrfs_end_transaction(trans);
5452 }
Chris Mason85e21ba2008-01-29 15:11:36 -05005453
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005454 if (!(root == fs_info->tree_root ||
Li Zefan581bb052011-04-20 10:06:11 +08005455 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005456 btrfs_return_ino(root, btrfs_ino(BTRFS_I(inode)));
Li Zefan581bb052011-04-20 10:06:11 +08005457
Omar Sandoval27919062018-05-11 13:13:37 -07005458free_rsv:
5459 btrfs_free_block_rsv(fs_info, rsv);
Chris Mason39279cc2007-06-12 06:35:45 -04005460no_delete:
Omar Sandoval27919062018-05-11 13:13:37 -07005461 /*
5462 * If we didn't successfully delete, the orphan item will still be in
5463 * the tree and we'll retry on the next mount. Again, we might also want
5464 * to retry these periodically in the future.
5465 */
Nikolay Borisovf48d1cf2017-01-10 20:35:39 +02005466 btrfs_remove_delayed_node(BTRFS_I(inode));
Jan Karadbd57682012-05-03 14:48:02 +02005467 clear_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005468}
5469
5470/*
5471 * this returns the key found in the dir entry in the location pointer.
Su Yue005d6712018-03-05 17:13:37 +08005472 * If no dir entries were found, returns -ENOENT.
5473 * If found a corrupted location in dir entry, returns -EUCLEAN.
Chris Mason39279cc2007-06-12 06:35:45 -04005474 */
5475static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
5476 struct btrfs_key *location)
5477{
5478 const char *name = dentry->d_name.name;
5479 int namelen = dentry->d_name.len;
5480 struct btrfs_dir_item *di;
5481 struct btrfs_path *path;
5482 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04005483 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005484
5485 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005486 if (!path)
5487 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05005488
David Sterbaf85b7372017-01-20 14:54:07 +01005489 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(BTRFS_I(dir)),
5490 name, namelen, 0);
Su Yue005d6712018-03-05 17:13:37 +08005491 if (!di) {
5492 ret = -ENOENT;
5493 goto out;
5494 }
5495 if (IS_ERR(di)) {
Yan0d9f7f32007-10-25 15:48:28 -04005496 ret = PTR_ERR(di);
Su Yue005d6712018-03-05 17:13:37 +08005497 goto out;
5498 }
Chris Masond3977122009-01-05 21:25:51 -05005499
Chris Mason5f39d392007-10-15 16:14:19 -04005500 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Liu Bo56a0e702017-10-30 11:14:38 -06005501 if (location->type != BTRFS_INODE_ITEM_KEY &&
5502 location->type != BTRFS_ROOT_ITEM_KEY) {
Su Yue005d6712018-03-05 17:13:37 +08005503 ret = -EUCLEAN;
Liu Bo56a0e702017-10-30 11:14:38 -06005504 btrfs_warn(root->fs_info,
5505"%s gets something invalid in DIR_ITEM (name %s, directory ino %llu, location(%llu %u %llu))",
5506 __func__, name, btrfs_ino(BTRFS_I(dir)),
5507 location->objectid, location->type, location->offset);
Liu Bo56a0e702017-10-30 11:14:38 -06005508 }
Chris Mason39279cc2007-06-12 06:35:45 -04005509out:
Chris Mason39279cc2007-06-12 06:35:45 -04005510 btrfs_free_path(path);
5511 return ret;
5512}
5513
5514/*
5515 * when we hit a tree root in a directory, the btrfs part of the inode
5516 * needs to be changed to reflect the root directory of the tree root. This
5517 * is kind of like crossing a mount point.
5518 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005519static int fixup_tree_root_location(struct btrfs_fs_info *fs_info,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005520 struct inode *dir,
5521 struct dentry *dentry,
5522 struct btrfs_key *location,
5523 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04005524{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005525 struct btrfs_path *path;
5526 struct btrfs_root *new_root;
5527 struct btrfs_root_ref *ref;
5528 struct extent_buffer *leaf;
David Sterba1d4c08e2015-01-02 19:36:14 +01005529 struct btrfs_key key;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005530 int ret;
5531 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005532
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005533 path = btrfs_alloc_path();
5534 if (!path) {
5535 err = -ENOMEM;
5536 goto out;
5537 }
Chris Mason39279cc2007-06-12 06:35:45 -04005538
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005539 err = -ENOENT;
David Sterba1d4c08e2015-01-02 19:36:14 +01005540 key.objectid = BTRFS_I(dir)->root->root_key.objectid;
5541 key.type = BTRFS_ROOT_REF_KEY;
5542 key.offset = location->objectid;
5543
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005544 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005545 if (ret) {
5546 if (ret < 0)
5547 err = ret;
5548 goto out;
5549 }
Chris Mason39279cc2007-06-12 06:35:45 -04005550
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005551 leaf = path->nodes[0];
5552 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005553 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(BTRFS_I(dir)) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005554 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
5555 goto out;
5556
5557 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
5558 (unsigned long)(ref + 1),
5559 dentry->d_name.len);
5560 if (ret)
5561 goto out;
5562
David Sterbab3b4aa72011-04-21 01:20:15 +02005563 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005564
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005565 new_root = btrfs_read_fs_root_no_name(fs_info, location);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005566 if (IS_ERR(new_root)) {
5567 err = PTR_ERR(new_root);
5568 goto out;
5569 }
5570
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005571 *sub_root = new_root;
5572 location->objectid = btrfs_root_dirid(&new_root->root_item);
5573 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04005574 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005575 err = 0;
5576out:
5577 btrfs_free_path(path);
5578 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005579}
5580
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005581static void inode_tree_add(struct inode *inode)
5582{
5583 struct btrfs_root *root = BTRFS_I(inode)->root;
5584 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005585 struct rb_node **p;
5586 struct rb_node *parent;
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005587 struct rb_node *new = &BTRFS_I(inode)->rb_node;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005588 u64 ino = btrfs_ino(BTRFS_I(inode));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005589
Al Viro1d3382cb2010-10-23 15:19:20 -04005590 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005591 return;
Miao Xiee1409ce2013-05-15 07:48:16 +00005592 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005593 spin_lock(&root->inode_lock);
Miao Xiee1409ce2013-05-15 07:48:16 +00005594 p = &root->inode_tree.rb_node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005595 while (*p) {
5596 parent = *p;
5597 entry = rb_entry(parent, struct btrfs_inode, rb_node);
5598
David Sterba37508512018-06-29 10:56:40 +02005599 if (ino < btrfs_ino(entry))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005600 p = &parent->rb_left;
David Sterba37508512018-06-29 10:56:40 +02005601 else if (ino > btrfs_ino(entry))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005602 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005603 else {
5604 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04005605 (I_WILL_FREE | I_FREEING)));
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005606 rb_replace_node(parent, new, &root->inode_tree);
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005607 RB_CLEAR_NODE(parent);
5608 spin_unlock(&root->inode_lock);
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005609 return;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005610 }
5611 }
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005612 rb_link_node(new, parent, p);
5613 rb_insert_color(new, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005614 spin_unlock(&root->inode_lock);
5615}
5616
5617static void inode_tree_del(struct inode *inode)
5618{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005619 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005620 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04005621 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005622
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005623 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005624 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005625 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005626 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04005627 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005628 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005629 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04005630
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005631 if (empty && btrfs_root_refs(&root->root_item) == 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005632 synchronize_srcu(&fs_info->subvol_srcu);
Yan, Zheng76dda932009-09-21 16:00:26 -04005633 spin_lock(&root->inode_lock);
5634 empty = RB_EMPTY_ROOT(&root->inode_tree);
5635 spin_unlock(&root->inode_lock);
5636 if (empty)
5637 btrfs_add_dead_root(root);
5638 }
5639}
5640
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005641
Chris Masone02119d2008-09-05 16:13:11 -04005642static int btrfs_init_locked_inode(struct inode *inode, void *p)
5643{
5644 struct btrfs_iget_args *args = p;
Chris Mason90d3e592014-01-09 17:28:00 -08005645 inode->i_ino = args->location->objectid;
5646 memcpy(&BTRFS_I(inode)->location, args->location,
5647 sizeof(*args->location));
Chris Masone02119d2008-09-05 16:13:11 -04005648 BTRFS_I(inode)->root = args->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005649 return 0;
5650}
5651
5652static int btrfs_find_actor(struct inode *inode, void *opaque)
5653{
5654 struct btrfs_iget_args *args = opaque;
Chris Mason90d3e592014-01-09 17:28:00 -08005655 return args->location->objectid == BTRFS_I(inode)->location.objectid &&
Chris Masond3977122009-01-05 21:25:51 -05005656 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005657}
5658
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005659static struct inode *btrfs_iget_locked(struct super_block *s,
Chris Mason90d3e592014-01-09 17:28:00 -08005660 struct btrfs_key *location,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005661 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04005662{
5663 struct inode *inode;
5664 struct btrfs_iget_args args;
Chris Mason90d3e592014-01-09 17:28:00 -08005665 unsigned long hashval = btrfs_inode_hash(location->objectid, root);
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005666
Chris Mason90d3e592014-01-09 17:28:00 -08005667 args.location = location;
Chris Mason39279cc2007-06-12 06:35:45 -04005668 args.root = root;
5669
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005670 inode = iget5_locked(s, hashval, btrfs_find_actor,
Chris Mason39279cc2007-06-12 06:35:45 -04005671 btrfs_init_locked_inode,
5672 (void *)&args);
5673 return inode;
5674}
5675
Balaji Rao1a54ef82008-07-21 02:01:04 +05305676/* Get an inode object given its location and corresponding root.
5677 * Returns in *is_new if the inode was read from disk
5678 */
5679struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
Josef Bacik73f73412009-12-04 17:38:27 +00005680 struct btrfs_root *root, int *new)
Balaji Rao1a54ef82008-07-21 02:01:04 +05305681{
5682 struct inode *inode;
5683
Chris Mason90d3e592014-01-09 17:28:00 -08005684 inode = btrfs_iget_locked(s, location, root);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305685 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005686 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305687
5688 if (inode->i_state & I_NEW) {
Filipe Manana67710892016-06-06 11:51:25 +01005689 int ret;
5690
5691 ret = btrfs_read_locked_inode(inode);
Al Viro9bc2cef2018-07-29 23:04:50 +01005692 if (!ret) {
Mark Fasheh1748f842011-07-12 11:25:31 -07005693 inode_tree_add(inode);
5694 unlock_new_inode(inode);
5695 if (new)
5696 *new = 1;
5697 } else {
Al Virof5b3a412018-07-29 23:04:51 +01005698 iget_failed(inode);
5699 /*
5700 * ret > 0 can come from btrfs_search_slot called by
5701 * btrfs_read_locked_inode, this means the inode item
5702 * was not found.
5703 */
5704 if (ret > 0)
5705 ret = -ENOENT;
5706 inode = ERR_PTR(ret);
Mark Fasheh1748f842011-07-12 11:25:31 -07005707 }
5708 }
5709
Balaji Rao1a54ef82008-07-21 02:01:04 +05305710 return inode;
5711}
5712
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005713static struct inode *new_simple_dir(struct super_block *s,
5714 struct btrfs_key *key,
5715 struct btrfs_root *root)
5716{
5717 struct inode *inode = new_inode(s);
5718
5719 if (!inode)
5720 return ERR_PTR(-ENOMEM);
5721
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005722 BTRFS_I(inode)->root = root;
5723 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04005724 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005725
5726 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Li Zefan848cce02012-02-21 17:04:28 +08005727 inode->i_op = &btrfs_dir_ro_inode_operations;
Omar Sandoval1fdf4192017-01-25 17:06:39 -08005728 inode->i_opflags &= ~IOP_XATTR;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005729 inode->i_fop = &simple_dir_operations;
5730 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
Deepa Dinamanic2050a42016-09-14 07:48:06 -07005731 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05305732 inode->i_atime = inode->i_mtime;
5733 inode->i_ctime = inode->i_mtime;
Arnd Bergmannd3c6be6f2018-06-21 18:04:06 +02005734 BTRFS_I(inode)->i_otime = inode->i_mtime;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005735
5736 return inode;
5737}
5738
Chris Mason3de45862008-11-17 21:02:50 -05005739struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04005740{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005741 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masond3977122009-01-05 21:25:51 -05005742 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005743 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005744 struct btrfs_root *sub_root = root;
5745 struct btrfs_key location;
Yan, Zheng76dda932009-09-21 16:00:26 -04005746 int index;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005747 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005748
5749 if (dentry->d_name.len > BTRFS_NAME_LEN)
5750 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04005751
Jeff Layton39e3c952012-11-28 11:30:53 -05005752 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason39279cc2007-06-12 06:35:45 -04005753 if (ret < 0)
5754 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04005755
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005756 if (location.type == BTRFS_INODE_ITEM_KEY) {
Josef Bacik73f73412009-12-04 17:38:27 +00005757 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005758 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04005759 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005760
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005761 index = srcu_read_lock(&fs_info->subvol_srcu);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005762 ret = fixup_tree_root_location(fs_info, dir, dentry,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005763 &location, &sub_root);
5764 if (ret < 0) {
5765 if (ret != -ENOENT)
5766 inode = ERR_PTR(ret);
5767 else
5768 inode = new_simple_dir(dir->i_sb, &location, sub_root);
5769 } else {
Josef Bacik73f73412009-12-04 17:38:27 +00005770 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04005771 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005772 srcu_read_unlock(&fs_info->subvol_srcu, index);
Yan, Zheng76dda932009-09-21 16:00:26 -04005773
Julia Lawall34d19ba2011-01-24 19:55:19 +00005774 if (!IS_ERR(inode) && root != sub_root) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005775 down_read(&fs_info->cleanup_work_sem);
David Howellsbc98a422017-07-17 08:45:34 +01005776 if (!sb_rdonly(inode->i_sb))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005777 ret = btrfs_orphan_cleanup(sub_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005778 up_read(&fs_info->cleanup_work_sem);
Josef Bacik01cd3362013-06-03 21:39:49 -04005779 if (ret) {
5780 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005781 inode = ERR_PTR(ret);
Josef Bacik01cd3362013-06-03 21:39:49 -04005782 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00005783 }
5784
Chris Mason3de45862008-11-17 21:02:50 -05005785 return inode;
5786}
5787
Nick Pigginfe15ce42011-01-07 17:49:23 +11005788static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04005789{
5790 struct btrfs_root *root;
David Howells2b0143b2015-03-17 22:25:59 +00005791 struct inode *inode = d_inode(dentry);
Yan, Zheng76dda932009-09-21 16:00:26 -04005792
Li Zefan848cce02012-02-21 17:04:28 +08005793 if (!inode && !IS_ROOT(dentry))
David Howells2b0143b2015-03-17 22:25:59 +00005794 inode = d_inode(dentry->d_parent);
Yan, Zheng76dda932009-09-21 16:00:26 -04005795
Li Zefan848cce02012-02-21 17:04:28 +08005796 if (inode) {
5797 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04005798 if (btrfs_root_refs(&root->root_item) == 0)
5799 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08005800
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005801 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Li Zefan848cce02012-02-21 17:04:28 +08005802 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04005803 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005804 return 0;
5805}
5806
Chris Mason3de45862008-11-17 21:02:50 -05005807static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04005808 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05005809{
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005810 struct inode *inode;
Josef Bacika66e7cc2011-09-18 10:34:03 -04005811
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005812 inode = btrfs_lookup_dentry(dir, dentry);
5813 if (IS_ERR(inode)) {
5814 if (PTR_ERR(inode) == -ENOENT)
5815 inode = NULL;
5816 else
5817 return ERR_CAST(inode);
5818 }
5819
Al Viro41d28bc2014-10-12 22:24:21 -04005820 return d_splice_alias(inode, dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04005821}
5822
Miao Xie16cdcec2011-04-22 18:12:22 +08005823unsigned char btrfs_filetype_table[] = {
Chris Mason39279cc2007-06-12 06:35:45 -04005824 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
5825};
5826
Josef Bacik23b5ec72017-07-24 15:14:25 -04005827/*
5828 * All this infrastructure exists because dir_emit can fault, and we are holding
5829 * the tree lock when doing readdir. For now just allocate a buffer and copy
5830 * our information into that, and then dir_emit from the buffer. This is
5831 * similar to what NFS does, only we don't keep the buffer around in pagecache
5832 * because I'm afraid I'll mess that up. Long term we need to make filldir do
5833 * copy_to_user_inatomic so we don't have to worry about page faulting under the
5834 * tree lock.
5835 */
5836static int btrfs_opendir(struct inode *inode, struct file *file)
5837{
5838 struct btrfs_file_private *private;
5839
5840 private = kzalloc(sizeof(struct btrfs_file_private), GFP_KERNEL);
5841 if (!private)
5842 return -ENOMEM;
5843 private->filldir_buf = kzalloc(PAGE_SIZE, GFP_KERNEL);
5844 if (!private->filldir_buf) {
5845 kfree(private);
5846 return -ENOMEM;
5847 }
5848 file->private_data = private;
5849 return 0;
5850}
5851
5852struct dir_entry {
5853 u64 ino;
5854 u64 offset;
5855 unsigned type;
5856 int name_len;
5857};
5858
5859static int btrfs_filldir(void *addr, int entries, struct dir_context *ctx)
5860{
5861 while (entries--) {
5862 struct dir_entry *entry = addr;
5863 char *name = (char *)(entry + 1);
5864
David Sterba92d32172018-04-16 21:10:14 +02005865 ctx->pos = get_unaligned(&entry->offset);
5866 if (!dir_emit(ctx, name, get_unaligned(&entry->name_len),
5867 get_unaligned(&entry->ino),
5868 get_unaligned(&entry->type)))
Josef Bacik23b5ec72017-07-24 15:14:25 -04005869 return 1;
David Sterba92d32172018-04-16 21:10:14 +02005870 addr += sizeof(struct dir_entry) +
5871 get_unaligned(&entry->name_len);
Josef Bacik23b5ec72017-07-24 15:14:25 -04005872 ctx->pos++;
5873 }
5874 return 0;
5875}
5876
Al Viro9cdda8d2013-05-22 16:48:09 -04005877static int btrfs_real_readdir(struct file *file, struct dir_context *ctx)
Chris Mason39279cc2007-06-12 06:35:45 -04005878{
Al Viro9cdda8d2013-05-22 16:48:09 -04005879 struct inode *inode = file_inode(file);
Chris Mason39279cc2007-06-12 06:35:45 -04005880 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005881 struct btrfs_file_private *private = file->private_data;
Chris Mason39279cc2007-06-12 06:35:45 -04005882 struct btrfs_dir_item *di;
5883 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04005884 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04005885 struct btrfs_path *path;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005886 void *addr;
Miao Xie16cdcec2011-04-22 18:12:22 +08005887 struct list_head ins_list;
5888 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04005889 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04005890 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04005891 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04005892 char *name_ptr;
5893 int name_len;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005894 int entries = 0;
5895 int total_len = 0;
Omar Sandoval02dbfc92016-05-20 13:50:33 -07005896 bool put = false;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005897 struct btrfs_key location;
Chris Mason5f39d392007-10-15 16:14:19 -04005898
Al Viro9cdda8d2013-05-22 16:48:09 -04005899 if (!dir_emit_dots(file, ctx))
5900 return 0;
5901
David Woodhouse49593bf2008-08-17 17:08:36 +01005902 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08005903 if (!path)
5904 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04005905
Josef Bacik23b5ec72017-07-24 15:14:25 -04005906 addr = private->filldir_buf;
David Sterbae4058b52015-11-27 16:31:35 +01005907 path->reada = READA_FORWARD;
David Woodhouse49593bf2008-08-17 17:08:36 +01005908
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005909 INIT_LIST_HEAD(&ins_list);
5910 INIT_LIST_HEAD(&del_list);
5911 put = btrfs_readdir_get_delayed_items(inode, &ins_list, &del_list);
Miao Xie16cdcec2011-04-22 18:12:22 +08005912
Josef Bacik23b5ec72017-07-24 15:14:25 -04005913again:
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005914 key.type = BTRFS_DIR_INDEX_KEY;
Al Viro9cdda8d2013-05-22 16:48:09 -04005915 key.offset = ctx->pos;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005916 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason5f39d392007-10-15 16:14:19 -04005917
Chris Mason39279cc2007-06-12 06:35:45 -04005918 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5919 if (ret < 0)
5920 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01005921
5922 while (1) {
Josef Bacik23b5ec72017-07-24 15:14:25 -04005923 struct dir_entry *entry;
5924
Chris Mason5f39d392007-10-15 16:14:19 -04005925 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04005926 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08005927 if (slot >= btrfs_header_nritems(leaf)) {
5928 ret = btrfs_next_leaf(root, path);
5929 if (ret < 0)
5930 goto err;
5931 else if (ret > 0)
5932 break;
5933 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04005934 }
Chris Mason3de45862008-11-17 21:02:50 -05005935
Chris Mason5f39d392007-10-15 16:14:19 -04005936 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5937
5938 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04005939 break;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005940 if (found_key.type != BTRFS_DIR_INDEX_KEY)
Chris Mason39279cc2007-06-12 06:35:45 -04005941 break;
Al Viro9cdda8d2013-05-22 16:48:09 -04005942 if (found_key.offset < ctx->pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08005943 goto next;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005944 if (btrfs_should_delete_dir_index(&del_list, found_key.offset))
Miao Xie16cdcec2011-04-22 18:12:22 +08005945 goto next;
Chris Mason39279cc2007-06-12 06:35:45 -04005946 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005947 name_len = btrfs_dir_name_len(leaf, di);
Josef Bacik23b5ec72017-07-24 15:14:25 -04005948 if ((total_len + sizeof(struct dir_entry) + name_len) >=
5949 PAGE_SIZE) {
5950 btrfs_release_path(path);
5951 ret = btrfs_filldir(private->filldir_buf, entries, ctx);
5952 if (ret)
5953 goto nopos;
5954 addr = private->filldir_buf;
5955 entries = 0;
5956 total_len = 0;
5957 goto again;
Chris Mason39279cc2007-06-12 06:35:45 -04005958 }
Josef Bacik23b5ec72017-07-24 15:14:25 -04005959
5960 entry = addr;
David Sterba92d32172018-04-16 21:10:14 +02005961 put_unaligned(name_len, &entry->name_len);
Josef Bacik23b5ec72017-07-24 15:14:25 -04005962 name_ptr = (char *)(entry + 1);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005963 read_extent_buffer(leaf, name_ptr, (unsigned long)(di + 1),
5964 name_len);
David Sterba92d32172018-04-16 21:10:14 +02005965 put_unaligned(btrfs_filetype_table[btrfs_dir_type(leaf, di)],
5966 &entry->type);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005967 btrfs_dir_item_key_to_cpu(leaf, di, &location);
David Sterba92d32172018-04-16 21:10:14 +02005968 put_unaligned(location.objectid, &entry->ino);
5969 put_unaligned(found_key.offset, &entry->offset);
Josef Bacik23b5ec72017-07-24 15:14:25 -04005970 entries++;
5971 addr += sizeof(struct dir_entry) + name_len;
5972 total_len += sizeof(struct dir_entry) + name_len;
Li Zefanb9e03af2011-03-23 10:43:58 +08005973next:
5974 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04005975 }
Josef Bacik23b5ec72017-07-24 15:14:25 -04005976 btrfs_release_path(path);
5977
5978 ret = btrfs_filldir(private->filldir_buf, entries, ctx);
5979 if (ret)
5980 goto nopos;
David Woodhouse49593bf2008-08-17 17:08:36 +01005981
Jeff Mahoneyd2fbb2b2016-11-05 13:26:35 -04005982 ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005983 if (ret)
David Sterbabc4ef752015-11-13 13:44:28 +01005984 goto nopos;
5985
Zach Browndb62efb2013-07-11 16:19:42 -07005986 /*
5987 * Stop new entries from being returned after we return the last
5988 * entry.
5989 *
5990 * New directory entries are assigned a strictly increasing
5991 * offset. This means that new entries created during readdir
5992 * are *guaranteed* to be seen in the future by that readdir.
5993 * This has broken buggy programs which operate on names as
5994 * they're returned by readdir. Until we re-use freed offsets
5995 * we have this hack to stop new entries from being returned
5996 * under the assumption that they'll never reach this huge
5997 * offset.
5998 *
5999 * This is being careful not to overflow 32bit loff_t unless the
6000 * last entry requires it because doing so has broken 32bit apps
6001 * in the past.
6002 */
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006003 if (ctx->pos >= INT_MAX)
6004 ctx->pos = LLONG_MAX;
6005 else
6006 ctx->pos = INT_MAX;
Chris Mason39279cc2007-06-12 06:35:45 -04006007nopos:
6008 ret = 0;
6009err:
Omar Sandoval02dbfc92016-05-20 13:50:33 -07006010 if (put)
6011 btrfs_readdir_put_delayed_items(inode, &ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04006012 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04006013 return ret;
6014}
6015
Chris Mason39279cc2007-06-12 06:35:45 -04006016/*
Chris Mason54aa1f42007-06-22 14:16:25 -04006017 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04006018 * inode changes. But, it is most likely to find the inode in cache.
6019 * FIXME, needs more benchmarking...there are no reasons other than performance
6020 * to keep or drop this code.
6021 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00006022static int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04006023{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006024 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04006025 struct btrfs_root *root = BTRFS_I(inode)->root;
6026 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006027 int ret;
6028
Josef Bacik72ac3c02012-05-23 14:13:11 -04006029 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05006030 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006031
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006032 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006033 if (IS_ERR(trans))
6034 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006035
6036 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04006037 if (ret && ret == -ENOSPC) {
6038 /* whoops, lets try again with the full transaction */
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006039 btrfs_end_transaction(trans);
Chris Mason94b60442010-05-26 11:02:00 -04006040 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006041 if (IS_ERR(trans))
6042 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006043
Chris Mason94b60442010-05-26 11:02:00 -04006044 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04006045 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006046 btrfs_end_transaction(trans);
Miao Xie16cdcec2011-04-22 18:12:22 +08006047 if (BTRFS_I(inode)->delayed_node)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006048 btrfs_balance_delayed_items(fs_info);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006049
6050 return ret;
6051}
6052
6053/*
6054 * This is a copy of file_update_time. We need this so we can return error on
6055 * ENOSPC for updating the inode in the case of file write and mmap writes.
6056 */
Deepa Dinamani95582b02018-05-08 19:36:02 -07006057static int btrfs_update_time(struct inode *inode, struct timespec64 *now,
Josef Bacike41f9412012-03-26 09:46:47 -04006058 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05006059{
Alexander Block2bc5565282012-06-15 09:49:33 +02006060 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Layton3a8c7232017-12-11 06:35:24 -05006061 bool dirty = flags & ~S_VERSION;
Alexander Block2bc5565282012-06-15 09:49:33 +02006062
6063 if (btrfs_root_readonly(root))
6064 return -EROFS;
6065
Josef Bacike41f9412012-03-26 09:46:47 -04006066 if (flags & S_VERSION)
Jeff Layton3a8c7232017-12-11 06:35:24 -05006067 dirty |= inode_maybe_inc_iversion(inode, dirty);
Josef Bacike41f9412012-03-26 09:46:47 -04006068 if (flags & S_CTIME)
6069 inode->i_ctime = *now;
6070 if (flags & S_MTIME)
6071 inode->i_mtime = *now;
6072 if (flags & S_ATIME)
6073 inode->i_atime = *now;
Jeff Layton3a8c7232017-12-11 06:35:24 -05006074 return dirty ? btrfs_dirty_inode(inode) : 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006075}
6076
Chris Masond352ac62008-09-29 15:18:18 -04006077/*
6078 * find the highest existing sequence number in a directory
6079 * and then set the in-memory index_cnt variable to reflect
6080 * free sequence numbers
6081 */
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006082static int btrfs_set_inode_index_count(struct btrfs_inode *inode)
Josef Bacikaec74772008-07-24 12:12:38 -04006083{
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006084 struct btrfs_root *root = inode->root;
Josef Bacikaec74772008-07-24 12:12:38 -04006085 struct btrfs_key key, found_key;
6086 struct btrfs_path *path;
6087 struct extent_buffer *leaf;
6088 int ret;
6089
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006090 key.objectid = btrfs_ino(inode);
David Sterba962a2982014-06-04 18:41:45 +02006091 key.type = BTRFS_DIR_INDEX_KEY;
Josef Bacikaec74772008-07-24 12:12:38 -04006092 key.offset = (u64)-1;
6093
6094 path = btrfs_alloc_path();
6095 if (!path)
6096 return -ENOMEM;
6097
6098 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
6099 if (ret < 0)
6100 goto out;
6101 /* FIXME: we should be able to handle this */
6102 if (ret == 0)
6103 goto out;
6104 ret = 0;
6105
6106 /*
6107 * MAGIC NUMBER EXPLANATION:
6108 * since we search a directory based on f_pos we have to start at 2
6109 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
6110 * else has to start at 2
6111 */
6112 if (path->slots[0] == 0) {
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006113 inode->index_cnt = 2;
Josef Bacikaec74772008-07-24 12:12:38 -04006114 goto out;
6115 }
6116
6117 path->slots[0]--;
6118
6119 leaf = path->nodes[0];
6120 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6121
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006122 if (found_key.objectid != btrfs_ino(inode) ||
David Sterba962a2982014-06-04 18:41:45 +02006123 found_key.type != BTRFS_DIR_INDEX_KEY) {
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006124 inode->index_cnt = 2;
Josef Bacikaec74772008-07-24 12:12:38 -04006125 goto out;
6126 }
6127
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006128 inode->index_cnt = found_key.offset + 1;
Josef Bacikaec74772008-07-24 12:12:38 -04006129out:
6130 btrfs_free_path(path);
6131 return ret;
6132}
6133
Chris Masond352ac62008-09-29 15:18:18 -04006134/*
6135 * helper to find a free sequence number in a given directory. This current
6136 * code is very simple, later versions will do smarter things in the btree
6137 */
Nikolay Borisov877574e2017-02-20 13:50:33 +02006138int btrfs_set_inode_index(struct btrfs_inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04006139{
6140 int ret = 0;
6141
Nikolay Borisov877574e2017-02-20 13:50:33 +02006142 if (dir->index_cnt == (u64)-1) {
6143 ret = btrfs_inode_delayed_dir_index_count(dir);
Miao Xie16cdcec2011-04-22 18:12:22 +08006144 if (ret) {
6145 ret = btrfs_set_inode_index_count(dir);
6146 if (ret)
6147 return ret;
6148 }
Josef Bacikaec74772008-07-24 12:12:38 -04006149 }
6150
Nikolay Borisov877574e2017-02-20 13:50:33 +02006151 *index = dir->index_cnt;
6152 dir->index_cnt++;
Josef Bacikaec74772008-07-24 12:12:38 -04006153
6154 return ret;
6155}
6156
Chris Masonb0d5d102014-09-08 13:08:51 -07006157static int btrfs_insert_inode_locked(struct inode *inode)
6158{
6159 struct btrfs_iget_args args;
6160 args.location = &BTRFS_I(inode)->location;
6161 args.root = BTRFS_I(inode)->root;
6162
6163 return insert_inode_locked4(inode,
6164 btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root),
6165 btrfs_find_actor, &args);
6166}
6167
Anand Jain19aee8d2017-07-18 17:37:05 +08006168/*
6169 * Inherit flags from the parent inode.
6170 *
6171 * Currently only the compression flags and the cow flags are inherited.
6172 */
6173static void btrfs_inherit_iflags(struct inode *inode, struct inode *dir)
6174{
6175 unsigned int flags;
6176
6177 if (!dir)
6178 return;
6179
6180 flags = BTRFS_I(dir)->flags;
6181
6182 if (flags & BTRFS_INODE_NOCOMPRESS) {
6183 BTRFS_I(inode)->flags &= ~BTRFS_INODE_COMPRESS;
6184 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
6185 } else if (flags & BTRFS_INODE_COMPRESS) {
6186 BTRFS_I(inode)->flags &= ~BTRFS_INODE_NOCOMPRESS;
6187 BTRFS_I(inode)->flags |= BTRFS_INODE_COMPRESS;
6188 }
6189
6190 if (flags & BTRFS_INODE_NODATACOW) {
6191 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
6192 if (S_ISREG(inode->i_mode))
6193 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
6194 }
6195
David Sterba7b6a2212018-03-26 18:40:21 +02006196 btrfs_sync_inode_flags_to_i_flags(inode);
Anand Jain19aee8d2017-07-18 17:37:05 +08006197}
6198
Chris Mason39279cc2007-06-12 06:35:45 -04006199static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
6200 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04006201 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05006202 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04006203 u64 ref_objectid, u64 objectid,
6204 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04006205{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006206 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04006207 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006208 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04006209 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04006210 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05006211 struct btrfs_inode_ref *ref;
6212 struct btrfs_key key[2];
6213 u32 sizes[2];
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006214 int nitems = name ? 2 : 1;
Chris Mason9c583092008-01-29 15:15:18 -05006215 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04006216 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006217
Chris Mason5f39d392007-10-15 16:14:19 -04006218 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07006219 if (!path)
6220 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04006221
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006222 inode = new_inode(fs_info->sb);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006223 if (!inode) {
6224 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04006225 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006226 }
Chris Mason39279cc2007-06-12 06:35:45 -04006227
Li Zefan581bb052011-04-20 10:06:11 +08006228 /*
Filipe Manana5762b5c2014-08-01 00:10:32 +01006229 * O_TMPFILE, set link count to 0, so that after this point,
6230 * we fill in an inode item with the correct link count.
6231 */
6232 if (!name)
6233 set_nlink(inode, 0);
6234
6235 /*
Li Zefan581bb052011-04-20 10:06:11 +08006236 * we have to initialize this early, so we can reclaim the inode
6237 * number if we fail afterwards in this function.
6238 */
6239 inode->i_ino = objectid;
6240
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006241 if (dir && name) {
liubo1abe9b82011-03-24 11:18:59 +00006242 trace_btrfs_inode_request(dir);
6243
Nikolay Borisov877574e2017-02-20 13:50:33 +02006244 ret = btrfs_set_inode_index(BTRFS_I(dir), index);
Shen Feng09771432009-04-02 16:46:06 -04006245 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006246 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04006247 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04006248 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04006249 }
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006250 } else if (dir) {
6251 *index = 0;
Josef Bacikaec74772008-07-24 12:12:38 -04006252 }
6253 /*
6254 * index_cnt is ignored for everything but a dir,
Su Yuedf6703e2018-01-12 11:08:02 +08006255 * btrfs_set_inode_index_count has an explanation for the magic
Josef Bacikaec74772008-07-24 12:12:38 -04006256 * number
6257 */
6258 BTRFS_I(inode)->index_cnt = 2;
Miao Xie67de1172013-12-26 13:07:06 +08006259 BTRFS_I(inode)->dir_index = *index;
Chris Mason39279cc2007-06-12 06:35:45 -04006260 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04006261 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00006262 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db22007-08-27 16:49:44 -04006263
Josef Bacik5dc562c2012-08-17 13:14:17 -04006264 /*
6265 * We could have gotten an inode number from somebody who was fsynced
6266 * and then removed in this same transaction, so let's just set full
6267 * sync since it will be a full sync anyway and this will blow away the
6268 * old info in the log.
6269 */
6270 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
6271
Chris Mason9c583092008-01-29 15:15:18 -05006272 key[0].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006273 key[0].type = BTRFS_INODE_ITEM_KEY;
Chris Mason9c583092008-01-29 15:15:18 -05006274 key[0].offset = 0;
6275
Chris Mason9c583092008-01-29 15:15:18 -05006276 sizes[0] = sizeof(struct btrfs_inode_item);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006277
6278 if (name) {
6279 /*
6280 * Start new inodes with an inode_ref. This is slightly more
6281 * efficient for small numbers of hard links since they will
6282 * be packed into one item. Extended refs will kick in if we
6283 * add more hard links than can fit in the ref item.
6284 */
6285 key[1].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006286 key[1].type = BTRFS_INODE_REF_KEY;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006287 key[1].offset = ref_objectid;
6288
6289 sizes[1] = name_len + sizeof(*ref);
6290 }
Chris Mason9c583092008-01-29 15:15:18 -05006291
Chris Masonb0d5d102014-09-08 13:08:51 -07006292 location = &BTRFS_I(inode)->location;
6293 location->objectid = objectid;
6294 location->offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02006295 location->type = BTRFS_INODE_ITEM_KEY;
Chris Masonb0d5d102014-09-08 13:08:51 -07006296
6297 ret = btrfs_insert_inode_locked(inode);
6298 if (ret < 0)
6299 goto fail;
6300
Chris Masonb9473432009-03-13 11:00:37 -04006301 path->leave_spinning = 1;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006302 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems);
Chris Mason9c583092008-01-29 15:15:18 -05006303 if (ret != 0)
Chris Masonb0d5d102014-09-08 13:08:51 -07006304 goto fail_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04006305
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03006306 inode_init_owner(inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04006307 inode_set_bytes(inode, 0);
chandan r9cc97d62012-07-04 12:48:07 +05306308
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006309 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05306310 inode->i_atime = inode->i_mtime;
6311 inode->i_ctime = inode->i_mtime;
Arnd Bergmannd3c6be6f2018-06-21 18:04:06 +02006312 BTRFS_I(inode)->i_otime = inode->i_mtime;
chandan r9cc97d62012-07-04 12:48:07 +05306313
Chris Mason5f39d392007-10-15 16:14:19 -04006314 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
6315 struct btrfs_inode_item);
David Sterbab159fa22016-11-08 18:09:03 +01006316 memzero_extent_buffer(path->nodes[0], (unsigned long)inode_item,
Li Zefan293f7e02012-07-10 00:58:58 -06006317 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04006318 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05006319
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006320 if (name) {
6321 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
6322 struct btrfs_inode_ref);
6323 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
6324 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
6325 ptr = (unsigned long)(ref + 1);
6326 write_extent_buffer(path->nodes[0], name, ptr, name_len);
6327 }
Chris Mason9c583092008-01-29 15:15:18 -05006328
Chris Mason5f39d392007-10-15 16:14:19 -04006329 btrfs_mark_buffer_dirty(path->nodes[0]);
6330 btrfs_free_path(path);
6331
Christoph Hellwig6cbff002009-04-17 10:37:41 +02006332 btrfs_inherit_iflags(inode, dir);
6333
Al Viro569254b2011-07-24 17:08:40 -04006334 if (S_ISREG(mode)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006335 if (btrfs_test_opt(fs_info, NODATASUM))
Chris Mason94272162009-07-02 12:26:06 -04006336 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006337 if (btrfs_test_opt(fs_info, NODATACOW))
Josef Bacikf2bdf9a2013-02-21 15:28:28 -05006338 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
6339 BTRFS_INODE_NODATASUM;
Chris Mason94272162009-07-02 12:26:06 -04006340 }
6341
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006342 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00006343
6344 trace_btrfs_inode_new(inode);
Chris Mason1973f0f2011-06-24 13:13:29 -04006345 btrfs_set_inode_last_trans(trans, inode);
liubo1abe9b82011-03-24 11:18:59 +00006346
Alexander Block8ea05e32012-07-25 17:35:53 +02006347 btrfs_update_root_times(trans, root);
6348
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006349 ret = btrfs_inode_inherit_props(trans, inode, dir);
6350 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006351 btrfs_err(fs_info,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006352 "error inheriting props for ino %llu (root %llu): %d",
David Sterbaf85b7372017-01-20 14:54:07 +01006353 btrfs_ino(BTRFS_I(inode)), root->root_key.objectid, ret);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006354
Chris Mason39279cc2007-06-12 06:35:45 -04006355 return inode;
Chris Masonb0d5d102014-09-08 13:08:51 -07006356
6357fail_unlock:
6358 unlock_new_inode(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006359fail:
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006360 if (dir && name)
Josef Bacikaec74772008-07-24 12:12:38 -04006361 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04006362 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04006363 iput(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006364 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006365}
6366
6367static inline u8 btrfs_inode_type(struct inode *inode)
6368{
6369 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
6370}
6371
Chris Masond352ac62008-09-29 15:18:18 -04006372/*
6373 * utility function to add 'inode' into 'parent_inode' with
6374 * a give name and a given sequence number.
6375 * if 'add_backref' is true, also insert a backref from the
6376 * inode to the parent directory.
6377 */
Chris Masone02119d2008-09-05 16:13:11 -04006378int btrfs_add_link(struct btrfs_trans_handle *trans,
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006379 struct btrfs_inode *parent_inode, struct btrfs_inode *inode,
Chris Masone02119d2008-09-05 16:13:11 -04006380 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006381{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006382 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006383 struct btrfs_key key;
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006384 struct btrfs_root *root = parent_inode->root;
6385 u64 ino = btrfs_ino(inode);
6386 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006387
Li Zefan33345d012011-04-20 10:31:50 +08006388 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006389 memcpy(&key, &inode->root->root_key, sizeof(key));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006390 } else {
Li Zefan33345d012011-04-20 10:31:50 +08006391 key.objectid = ino;
David Sterba962a2982014-06-04 18:41:45 +02006392 key.type = BTRFS_INODE_ITEM_KEY;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006393 key.offset = 0;
6394 }
Chris Mason39279cc2007-06-12 06:35:45 -04006395
Li Zefan33345d012011-04-20 10:31:50 +08006396 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Lu Fengqi6025c192018-08-01 11:32:29 +08006397 ret = btrfs_add_root_ref(trans, key.objectid,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006398 root->root_key.objectid, parent_ino,
6399 index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006400 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08006401 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
6402 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006403 }
6404
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006405 /* Nothing to clean up yet */
6406 if (ret)
6407 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006408
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006409 ret = btrfs_insert_dir_item(trans, root, name, name_len,
6410 parent_inode, &key,
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006411 btrfs_inode_type(&inode->vfs_inode), index);
Chris Mason9c520572012-12-17 14:26:57 -05006412 if (ret == -EEXIST || ret == -EOVERFLOW)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006413 goto fail_dir_item;
6414 else if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04006415 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006416 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006417 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006418
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006419 btrfs_i_size_write(parent_inode, parent_inode->vfs_inode.i_size +
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006420 name_len * 2);
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006421 inode_inc_iversion(&parent_inode->vfs_inode);
6422 parent_inode->vfs_inode.i_mtime = parent_inode->vfs_inode.i_ctime =
6423 current_time(&parent_inode->vfs_inode);
6424 ret = btrfs_update_inode(trans, root, &parent_inode->vfs_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006425 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04006426 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006427 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05006428
6429fail_dir_item:
6430 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
6431 u64 local_index;
6432 int err;
Lu Fengqi3ee1c552018-08-01 11:32:28 +08006433 err = btrfs_del_root_ref(trans, key.objectid,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006434 root->root_key.objectid, parent_ino,
6435 &local_index, name, name_len);
Chris Masonfe66a052012-02-20 08:40:56 -05006436
6437 } else if (add_backref) {
6438 u64 local_index;
6439 int err;
6440
6441 err = btrfs_del_inode_ref(trans, root, name, name_len,
6442 ino, parent_ino, &local_index);
6443 }
6444 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006445}
6446
6447static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006448 struct btrfs_inode *dir, struct dentry *dentry,
6449 struct btrfs_inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006450{
Josef Bacika1b075d2010-11-19 20:36:11 +00006451 int err = btrfs_add_link(trans, dir, inode,
6452 dentry->d_name.name, dentry->d_name.len,
6453 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006454 if (err > 0)
6455 err = -EEXIST;
6456 return err;
6457}
6458
Josef Bacik618e21d2007-07-11 10:18:17 -04006459static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04006460 umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04006461{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006462 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Josef Bacik618e21d2007-07-11 10:18:17 -04006463 struct btrfs_trans_handle *trans;
6464 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006465 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04006466 int err;
6467 int drop_inode = 0;
6468 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006469 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04006470
Josef Bacik9ed74f22009-09-11 16:12:44 -04006471 /*
6472 * 2 for inode item and ref
6473 * 2 for dir items
6474 * 1 for xattr if selinux is on
6475 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006476 trans = btrfs_start_transaction(root, 5);
6477 if (IS_ERR(trans))
6478 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05006479
Li Zefan581bb052011-04-20 10:06:11 +08006480 err = btrfs_find_free_ino(root, &objectid);
6481 if (err)
6482 goto out_unlock;
6483
Josef Bacikaec74772008-07-24 12:12:38 -04006484 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006485 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6486 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006487 if (IS_ERR(inode)) {
6488 err = PTR_ERR(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006489 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006490 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006491
Casey Schauflerad19db72011-12-15 10:09:07 -05006492 /*
6493 * If the active LSM wants to access the inode during
6494 * d_instantiate it needs these. Smack checks to see
6495 * if the filesystem supports xattrs by looking at the
6496 * ops vector.
6497 */
Casey Schauflerad19db72011-12-15 10:09:07 -05006498 inode->i_op = &btrfs_special_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006499 init_special_inode(inode, inode->i_mode, rdev);
6500
6501 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik618e21d2007-07-11 10:18:17 -04006502 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006503 goto out_unlock_inode;
6504
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006505 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6506 0, index);
Chris Masonb0d5d102014-09-08 13:08:51 -07006507 if (err) {
6508 goto out_unlock_inode;
6509 } else {
Yan1b4ab1b2007-08-29 09:11:44 -04006510 btrfs_update_inode(trans, root, inode);
Al Viro1e2e5472018-05-04 08:23:01 -04006511 d_instantiate_new(dentry, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006512 }
Chris Masonb0d5d102014-09-08 13:08:51 -07006513
Josef Bacik618e21d2007-07-11 10:18:17 -04006514out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006515 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006516 btrfs_btree_balance_dirty(fs_info);
Josef Bacik618e21d2007-07-11 10:18:17 -04006517 if (drop_inode) {
6518 inode_dec_link_count(inode);
6519 iput(inode);
6520 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006521 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006522
6523out_unlock_inode:
6524 drop_inode = 1;
6525 unlock_new_inode(inode);
6526 goto out_unlock;
6527
Josef Bacik618e21d2007-07-11 10:18:17 -04006528}
6529
Chris Mason39279cc2007-06-12 06:35:45 -04006530static int btrfs_create(struct inode *dir, struct dentry *dentry,
Al Viroebfc3b42012-06-10 18:05:36 -04006531 umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -04006532{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006533 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04006534 struct btrfs_trans_handle *trans;
6535 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006536 struct inode *inode = NULL;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006537 int drop_inode_on_err = 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006538 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04006539 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006540 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006541
Josef Bacik9ed74f22009-09-11 16:12:44 -04006542 /*
6543 * 2 for inode item and ref
6544 * 2 for dir items
6545 * 1 for xattr if selinux is on
6546 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006547 trans = btrfs_start_transaction(root, 5);
6548 if (IS_ERR(trans))
6549 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006550
Li Zefan581bb052011-04-20 10:06:11 +08006551 err = btrfs_find_free_ino(root, &objectid);
6552 if (err)
6553 goto out_unlock;
6554
Josef Bacikaec74772008-07-24 12:12:38 -04006555 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006556 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6557 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006558 if (IS_ERR(inode)) {
6559 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006560 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006561 }
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006562 drop_inode_on_err = 1;
Casey Schauflerad19db72011-12-15 10:09:07 -05006563 /*
6564 * If the active LSM wants to access the inode during
6565 * d_instantiate it needs these. Smack checks to see
6566 * if the filesystem supports xattrs by looking at the
6567 * ops vector.
6568 */
6569 inode->i_fop = &btrfs_file_operations;
6570 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006571 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006572
6573 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
6574 if (err)
6575 goto out_unlock_inode;
6576
6577 err = btrfs_update_inode(trans, root, inode);
6578 if (err)
6579 goto out_unlock_inode;
Casey Schauflerad19db72011-12-15 10:09:07 -05006580
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006581 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6582 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006583 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006584 goto out_unlock_inode;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006585
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006586 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Al Viro1e2e5472018-05-04 08:23:01 -04006587 d_instantiate_new(dentry, inode);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006588
Chris Mason39279cc2007-06-12 06:35:45 -04006589out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006590 btrfs_end_transaction(trans);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006591 if (err && drop_inode_on_err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006592 inode_dec_link_count(inode);
6593 iput(inode);
6594 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006595 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006596 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006597
6598out_unlock_inode:
6599 unlock_new_inode(inode);
6600 goto out_unlock;
6601
Chris Mason39279cc2007-06-12 06:35:45 -04006602}
6603
6604static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
6605 struct dentry *dentry)
6606{
Filipe Manana271dba452016-01-05 16:24:05 +00006607 struct btrfs_trans_handle *trans = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006608 struct btrfs_root *root = BTRFS_I(dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00006609 struct inode *inode = d_inode(old_dentry);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006610 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason00e4e6b2008-08-05 11:18:09 -04006611 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04006612 int err;
6613 int drop_inode = 0;
6614
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006615 /* do not allow sys_link's with other subvols of the same device */
6616 if (root->objectid != BTRFS_I(inode)->root->objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00006617 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006618
Mark Fashehf1863732012-08-08 11:32:27 -07006619 if (inode->i_nlink >= BTRFS_LINK_MAX)
Al Viroc055e992011-03-04 17:15:18 +00006620 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04006621
Nikolay Borisov877574e2017-02-20 13:50:33 +02006622 err = btrfs_set_inode_index(BTRFS_I(dir), &index);
Josef Bacikaec74772008-07-24 12:12:38 -04006623 if (err)
6624 goto fail;
6625
Yan, Zhenga22285a2010-05-16 10:48:46 -04006626 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00006627 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04006628 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00006629 * 1 item for parent inode
Omar Sandoval399b0bb2018-05-11 13:13:40 -07006630 * 1 item for orphan item deletion if O_TMPFILE
Yan, Zhenga22285a2010-05-16 10:48:46 -04006631 */
Omar Sandoval399b0bb2018-05-11 13:13:40 -07006632 trans = btrfs_start_transaction(root, inode->i_nlink ? 5 : 6);
Yan, Zhenga22285a2010-05-16 10:48:46 -04006633 if (IS_ERR(trans)) {
6634 err = PTR_ERR(trans);
Filipe Manana271dba452016-01-05 16:24:05 +00006635 trans = NULL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006636 goto fail;
6637 }
Chris Mason5f39d392007-10-15 16:14:19 -04006638
Miao Xie67de1172013-12-26 13:07:06 +08006639 /* There are several dir indexes for this inode, clear the cache. */
6640 BTRFS_I(inode)->dir_index = 0ULL;
Zach Brown8b558c52013-10-16 12:10:34 -07006641 inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04006642 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006643 inode->i_ctime = current_time(inode);
Al Viro7de9c6ee2010-10-23 11:11:40 -04006644 ihold(inode);
Josef Bacike9976152012-10-11 15:53:56 -04006645 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
Josef Bacikaec74772008-07-24 12:12:38 -04006646
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006647 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6648 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04006649
Yan, Zhenga5719522009-09-24 09:17:31 -04006650 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006651 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04006652 } else {
Al Viro10d9f302011-07-16 23:09:10 -04006653 struct dentry *parent = dentry->d_parent;
Yan, Zhenga5719522009-09-24 09:17:31 -04006654 err = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006655 if (err)
6656 goto fail;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006657 if (inode->i_nlink == 1) {
6658 /*
6659 * If new hard link count is 1, it's a file created
6660 * with open(2) O_TMPFILE flag.
6661 */
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02006662 err = btrfs_orphan_del(trans, BTRFS_I(inode));
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006663 if (err)
6664 goto fail;
6665 }
Al Viro08c422c2011-12-23 07:58:13 -05006666 d_instantiate(dentry, inode);
Nikolay Borisov9ca5fbfb2017-01-18 00:31:31 +02006667 btrfs_log_new_name(trans, BTRFS_I(inode), NULL, parent);
Yan, Zhenga5719522009-09-24 09:17:31 -04006668 }
Chris Mason39279cc2007-06-12 06:35:45 -04006669
Chris Mason1832a6d2007-12-21 16:27:21 -05006670fail:
Filipe Manana271dba452016-01-05 16:24:05 +00006671 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006672 btrfs_end_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006673 if (drop_inode) {
6674 inode_dec_link_count(inode);
6675 iput(inode);
6676 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006677 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006678 return err;
6679}
6680
Al Viro18bb1db2011-07-26 01:41:39 -04006681static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04006682{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006683 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masonb9d86662008-05-02 16:13:49 -04006684 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006685 struct btrfs_trans_handle *trans;
6686 struct btrfs_root *root = BTRFS_I(dir)->root;
6687 int err = 0;
6688 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04006689 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006690 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006691
Josef Bacik9ed74f22009-09-11 16:12:44 -04006692 /*
6693 * 2 items for inode and ref
6694 * 2 items for dir items
6695 * 1 for xattr if selinux is on
6696 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006697 trans = btrfs_start_transaction(root, 5);
6698 if (IS_ERR(trans))
6699 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006700
Li Zefan581bb052011-04-20 10:06:11 +08006701 err = btrfs_find_free_ino(root, &objectid);
6702 if (err)
6703 goto out_fail;
6704
Josef Bacikaec74772008-07-24 12:12:38 -04006705 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006706 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6707 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04006708 if (IS_ERR(inode)) {
6709 err = PTR_ERR(inode);
6710 goto out_fail;
6711 }
Chris Mason5f39d392007-10-15 16:14:19 -04006712
Chris Mason39279cc2007-06-12 06:35:45 -04006713 drop_on_err = 1;
Chris Masonb0d5d102014-09-08 13:08:51 -07006714 /* these must be set before we unlock the inode */
6715 inode->i_op = &btrfs_dir_inode_operations;
6716 inode->i_fop = &btrfs_dir_file_operations;
Josef Bacik33268ea2008-07-24 12:16:36 -04006717
Eric Paris2a7dba32011-02-01 11:05:39 -05006718 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04006719 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006720 goto out_fail_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04006721
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02006722 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Mason39279cc2007-06-12 06:35:45 -04006723 err = btrfs_update_inode(trans, root, inode);
6724 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006725 goto out_fail_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006726
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006727 err = btrfs_add_link(trans, BTRFS_I(dir), BTRFS_I(inode),
6728 dentry->d_name.name,
6729 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006730 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006731 goto out_fail_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006732
Al Viro1e2e5472018-05-04 08:23:01 -04006733 d_instantiate_new(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006734 drop_on_err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006735
6736out_fail:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006737 btrfs_end_transaction(trans);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006738 if (drop_on_err) {
6739 inode_dec_link_count(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006740 iput(inode);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006741 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006742 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006743 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006744
6745out_fail_inode:
6746 unlock_new_inode(inode);
6747 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04006748}
6749
Chris Masonc8b97812008-10-29 14:49:59 -04006750static noinline int uncompress_inline(struct btrfs_path *path,
Byongho Leee40da0e2015-05-19 23:46:45 +09006751 struct page *page,
Chris Masonc8b97812008-10-29 14:49:59 -04006752 size_t pg_offset, u64 extent_offset,
6753 struct btrfs_file_extent_item *item)
6754{
6755 int ret;
6756 struct extent_buffer *leaf = path->nodes[0];
6757 char *tmp;
6758 size_t max_size;
6759 unsigned long inline_size;
6760 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08006761 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04006762
6763 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08006764 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04006765 max_size = btrfs_file_extent_ram_bytes(leaf, item);
6766 inline_size = btrfs_file_extent_inline_item_len(leaf,
Ross Kirkdd3cc162013-09-16 15:58:09 +01006767 btrfs_item_nr(path->slots[0]));
Chris Masonc8b97812008-10-29 14:49:59 -04006768 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04006769 if (!tmp)
6770 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04006771 ptr = btrfs_file_extent_inline_start(item);
6772
6773 read_extent_buffer(leaf, tmp, ptr, inline_size);
6774
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006775 max_size = min_t(unsigned long, PAGE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08006776 ret = btrfs_decompress(compress_type, tmp, page,
6777 extent_offset, inline_size, max_size);
Zygo Blaxelle1699d22017-03-10 16:45:44 -05006778
6779 /*
6780 * decompression code contains a memset to fill in any space between the end
6781 * of the uncompressed data and the end of max_size in case the decompressed
6782 * data ends up shorter than ram_bytes. That doesn't cover the hole between
6783 * the end of an inline extent and the beginning of the next block, so we
6784 * cover that region here.
6785 */
6786
6787 if (max_size + pg_offset < PAGE_SIZE) {
6788 char *map = kmap(page);
6789 memset(map + pg_offset + max_size, 0, PAGE_SIZE - max_size - pg_offset);
6790 kunmap(page);
6791 }
Chris Masonc8b97812008-10-29 14:49:59 -04006792 kfree(tmp);
Zach Brown166ae5a2014-05-09 17:15:10 -04006793 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -04006794}
6795
Chris Masond352ac62008-09-29 15:18:18 -04006796/*
6797 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05006798 * the ugly parts come from merging extents from the disk with the in-ram
6799 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04006800 * where the in-ram extents might be locked pending data=ordered completion.
6801 *
6802 * This also copies inline extents directly into the page.
6803 */
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006804struct extent_map *btrfs_get_extent(struct btrfs_inode *inode,
6805 struct page *page,
6806 size_t pg_offset, u64 start, u64 len,
6807 int create)
Chris Masona52d9a82007-08-27 16:49:44 -04006808{
David Sterba3ffbd682018-06-29 10:56:42 +02006809 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Chris Masona52d9a82007-08-27 16:49:44 -04006810 int ret;
6811 int err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006812 u64 extent_start = 0;
6813 u64 extent_end = 0;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006814 u64 objectid = btrfs_ino(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04006815 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04006816 struct btrfs_path *path = NULL;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006817 struct btrfs_root *root = inode->root;
Chris Masona52d9a82007-08-27 16:49:44 -04006818 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04006819 struct extent_buffer *leaf;
6820 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04006821 struct extent_map *em = NULL;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006822 struct extent_map_tree *em_tree = &inode->extent_tree;
6823 struct extent_io_tree *io_tree = &inode->io_tree;
Filipe Manana7ffbb592014-06-09 03:48:05 +01006824 const bool new_inline = !page || create;
Chris Masona52d9a82007-08-27 16:49:44 -04006825
Chris Mason890871b2009-09-02 16:24:52 -04006826 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006827 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04006828 if (em)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006829 em->bdev = fs_info->fs_devices->latest_bdev;
Chris Mason890871b2009-09-02 16:24:52 -04006830 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006831
Chris Masona52d9a82007-08-27 16:49:44 -04006832 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04006833 if (em->start > start || em->start + em->len <= start)
6834 free_extent_map(em);
6835 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05006836 free_extent_map(em);
6837 else
6838 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006839 }
David Sterba172ddd62011-04-21 00:48:27 +02006840 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04006841 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05006842 err = -ENOMEM;
6843 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006844 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006845 em->bdev = fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05006846 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05006847 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05006848 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04006849 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04006850
6851 if (!path) {
6852 path = btrfs_alloc_path();
Josef Bacik026fd312011-05-13 10:32:11 -04006853 if (!path) {
6854 err = -ENOMEM;
6855 goto out;
6856 }
6857 /*
6858 * Chances are we'll be called again, so go ahead and do
6859 * readahead
6860 */
David Sterbae4058b52015-11-27 16:31:35 +01006861 path->reada = READA_FORWARD;
Chris Masonf4219502008-07-22 11:18:09 -04006862 }
6863
Nikolay Borisov5c9a7022017-12-01 11:19:40 +02006864 ret = btrfs_lookup_file_extent(NULL, root, path, objectid, start, 0);
Chris Masona52d9a82007-08-27 16:49:44 -04006865 if (ret < 0) {
6866 err = ret;
6867 goto out;
6868 }
6869
6870 if (ret != 0) {
6871 if (path->slots[0] == 0)
6872 goto not_found;
6873 path->slots[0]--;
6874 }
6875
Chris Mason5f39d392007-10-15 16:14:19 -04006876 leaf = path->nodes[0];
6877 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04006878 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04006879 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04006880 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02006881 found_type = found_key.type;
Chris Mason5f39d392007-10-15 16:14:19 -04006882 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04006883 found_type != BTRFS_EXTENT_DATA_KEY) {
Josef Bacik25a50342013-10-14 12:08:38 -04006884 /*
6885 * If we backup past the first extent we want to move forward
6886 * and see if there is an extent in front of us, otherwise we'll
6887 * say there is a hole for our whole search range which can
6888 * cause problems.
6889 */
6890 extent_end = start;
6891 goto next;
Chris Masona52d9a82007-08-27 16:49:44 -04006892 }
6893
Chris Mason5f39d392007-10-15 16:14:19 -04006894 found_type = btrfs_file_extent_type(leaf, item);
6895 extent_start = found_key.offset;
Yan Zhengd899e052008-10-30 14:25:28 -04006896 if (found_type == BTRFS_FILE_EXTENT_REG ||
6897 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006898 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04006899 btrfs_file_extent_num_bytes(leaf, item);
Liu Bo09ed2f12017-03-10 11:09:48 -08006900
6901 trace_btrfs_get_extent_show_fi_regular(inode, leaf, item,
6902 extent_start);
Yan Zheng9036c102008-10-30 14:19:41 -04006903 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
6904 size_t size;
Qu Wenruoe41ca582018-06-06 15:41:49 +08006905
6906 size = btrfs_file_extent_ram_bytes(leaf, item);
Jeff Mahoneyda170662016-06-15 09:22:56 -04006907 extent_end = ALIGN(extent_start + size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006908 fs_info->sectorsize);
Liu Bo09ed2f12017-03-10 11:09:48 -08006909
6910 trace_btrfs_get_extent_show_fi_inline(inode, leaf, item,
6911 path->slots[0],
6912 extent_start);
Yan Zheng9036c102008-10-30 14:19:41 -04006913 }
Josef Bacik25a50342013-10-14 12:08:38 -04006914next:
Yan Zheng9036c102008-10-30 14:19:41 -04006915 if (start >= extent_end) {
6916 path->slots[0]++;
6917 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
6918 ret = btrfs_next_leaf(root, path);
6919 if (ret < 0) {
6920 err = ret;
6921 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006922 }
Yan Zheng9036c102008-10-30 14:19:41 -04006923 if (ret > 0)
6924 goto not_found;
6925 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04006926 }
Yan Zheng9036c102008-10-30 14:19:41 -04006927 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6928 if (found_key.objectid != objectid ||
6929 found_key.type != BTRFS_EXTENT_DATA_KEY)
6930 goto not_found;
6931 if (start + len <= found_key.offset)
6932 goto not_found;
Wang Shilonge2eca692014-07-17 11:44:14 +08006933 if (start > found_key.offset)
6934 goto next;
Yan Zheng9036c102008-10-30 14:19:41 -04006935 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006936 em->orig_start = start;
Yan Zheng9036c102008-10-30 14:19:41 -04006937 em->len = found_key.offset - start;
6938 goto not_found_em;
6939 }
6940
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006941 btrfs_extent_item_to_extent_map(inode, path, item,
Nikolay Borisov9cdc5122017-02-20 13:51:02 +02006942 new_inline, em);
Filipe Manana7ffbb592014-06-09 03:48:05 +01006943
Yan Zhengd899e052008-10-30 14:25:28 -04006944 if (found_type == BTRFS_FILE_EXTENT_REG ||
6945 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006946 goto insert;
6947 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04006948 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04006949 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04006950 size_t size;
6951 size_t extent_offset;
6952 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04006953
Filipe Manana7ffbb592014-06-09 03:48:05 +01006954 if (new_inline)
Yan689f9342007-10-29 11:41:07 -04006955 goto out;
Yan689f9342007-10-29 11:41:07 -04006956
Qu Wenruoe41ca582018-06-06 15:41:49 +08006957 size = btrfs_file_extent_ram_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04006958 extent_offset = page_offset(page) + pg_offset - extent_start;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006959 copy_size = min_t(u64, PAGE_SIZE - pg_offset,
6960 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04006961 em->start = extent_start + extent_offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006962 em->len = ALIGN(copy_size, fs_info->sectorsize);
Josef Bacikb4939682012-12-03 10:31:19 -05006963 em->orig_block_len = em->len;
Josef Bacik70c8a912012-10-11 16:54:30 -04006964 em->orig_start = em->start;
Yan689f9342007-10-29 11:41:07 -04006965 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Edmund Nadolskibf46f522017-11-20 13:24:49 -07006966 if (!PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08006967 if (btrfs_file_extent_compression(leaf, item) !=
6968 BTRFS_COMPRESS_NONE) {
Byongho Leee40da0e2015-05-19 23:46:45 +09006969 ret = uncompress_inline(path, page, pg_offset,
Chris Masonc8b97812008-10-29 14:49:59 -04006970 extent_offset, item);
Zach Brown166ae5a2014-05-09 17:15:10 -04006971 if (ret) {
6972 err = ret;
6973 goto out;
6974 }
Chris Masonc8b97812008-10-29 14:49:59 -04006975 } else {
6976 map = kmap(page);
6977 read_extent_buffer(leaf, map + pg_offset, ptr,
6978 copy_size);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006979 if (pg_offset + copy_size < PAGE_SIZE) {
Chris Mason93c82d52009-09-11 12:36:29 -04006980 memset(map + pg_offset + copy_size, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006981 PAGE_SIZE - pg_offset -
Chris Mason93c82d52009-09-11 12:36:29 -04006982 copy_size);
6983 }
Chris Masonc8b97812008-10-29 14:49:59 -04006984 kunmap(page);
6985 }
Chris Mason179e29e2007-11-01 11:28:41 -04006986 flush_dcache_page(page);
Chris Masona52d9a82007-08-27 16:49:44 -04006987 }
Chris Masond1310b22008-01-24 16:13:08 -05006988 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00006989 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04006990 goto insert;
Chris Masona52d9a82007-08-27 16:49:44 -04006991 }
6992not_found:
6993 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006994 em->orig_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05006995 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04006996not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04006997 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04006998insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02006999 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05007000 if (em->start > start || extent_map_end(em) <= start) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007001 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04007002 "bad extent! em: [%llu %llu] passed [%llu %llu]",
7003 em->start, em->len, start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04007004 err = -EIO;
7005 goto out;
7006 }
Chris Masond1310b22008-01-24 16:13:08 -05007007
7008 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04007009 write_lock(&em_tree->lock);
David Sterbaf46b24c2018-04-03 21:45:57 +02007010 err = btrfs_add_extent_mapping(fs_info, em_tree, &em, start, len);
Chris Mason890871b2009-09-02 16:24:52 -04007011 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04007012out:
liubo1abe9b82011-03-24 11:18:59 +00007013
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007014 trace_btrfs_get_extent(root, inode, em);
liubo1abe9b82011-03-24 11:18:59 +00007015
Tsutomu Itoh527afb42015-08-19 14:55:00 +09007016 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04007017 if (err) {
7018 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04007019 return ERR_PTR(err);
7020 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007021 BUG_ON(!em); /* Error is always set */
Chris Masona52d9a82007-08-27 16:49:44 -04007022 return em;
7023}
7024
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007025struct extent_map *btrfs_get_extent_fiemap(struct btrfs_inode *inode,
7026 struct page *page,
7027 size_t pg_offset, u64 start, u64 len,
7028 int create)
Chris Masonec29ed52011-02-23 16:23:20 -05007029{
7030 struct extent_map *em;
7031 struct extent_map *hole_em = NULL;
7032 u64 range_start = start;
7033 u64 end;
7034 u64 found;
7035 u64 found_end;
7036 int err = 0;
7037
7038 em = btrfs_get_extent(inode, page, pg_offset, start, len, create);
7039 if (IS_ERR(em))
7040 return em;
Dan Carpenter99862772017-04-11 11:57:15 +03007041 /*
7042 * If our em maps to:
7043 * - a hole or
7044 * - a pre-alloc extent,
7045 * there might actually be delalloc bytes behind it.
7046 */
7047 if (em->block_start != EXTENT_MAP_HOLE &&
7048 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7049 return em;
7050 else
7051 hole_em = em;
Chris Masonec29ed52011-02-23 16:23:20 -05007052
7053 /* check to see if we've wrapped (len == -1 or similar) */
7054 end = start + len;
7055 if (end < start)
7056 end = (u64)-1;
7057 else
7058 end -= 1;
7059
7060 em = NULL;
7061
7062 /* ok, we didn't find anything, lets look for delalloc */
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007063 found = count_range_bits(&inode->io_tree, &range_start,
Chris Masonec29ed52011-02-23 16:23:20 -05007064 end, len, EXTENT_DELALLOC, 1);
7065 found_end = range_start + found;
7066 if (found_end < range_start)
7067 found_end = (u64)-1;
7068
7069 /*
7070 * we didn't find anything useful, return
7071 * the original results from get_extent()
7072 */
7073 if (range_start > end || found_end <= start) {
7074 em = hole_em;
7075 hole_em = NULL;
7076 goto out;
7077 }
7078
7079 /* adjust the range_start to make sure it doesn't
7080 * go backwards from the start they passed in
7081 */
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05307082 range_start = max(start, range_start);
Chris Masonec29ed52011-02-23 16:23:20 -05007083 found = found_end - range_start;
7084
7085 if (found > 0) {
7086 u64 hole_start = start;
7087 u64 hole_len = len;
7088
David Sterba172ddd62011-04-21 00:48:27 +02007089 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05007090 if (!em) {
7091 err = -ENOMEM;
7092 goto out;
7093 }
7094 /*
7095 * when btrfs_get_extent can't find anything it
7096 * returns one huge hole
7097 *
7098 * make sure what it found really fits our range, and
7099 * adjust to make sure it is based on the start from
7100 * the caller
7101 */
7102 if (hole_em) {
7103 u64 calc_end = extent_map_end(hole_em);
7104
7105 if (calc_end <= start || (hole_em->start > end)) {
7106 free_extent_map(hole_em);
7107 hole_em = NULL;
7108 } else {
7109 hole_start = max(hole_em->start, start);
7110 hole_len = calc_end - hole_start;
7111 }
7112 }
7113 em->bdev = NULL;
7114 if (hole_em && range_start > hole_start) {
7115 /* our hole starts before our delalloc, so we
7116 * have to return just the parts of the hole
7117 * that go until the delalloc starts
7118 */
7119 em->len = min(hole_len,
7120 range_start - hole_start);
7121 em->start = hole_start;
7122 em->orig_start = hole_start;
7123 /*
7124 * don't adjust block start at all,
7125 * it is fixed at EXTENT_MAP_HOLE
7126 */
7127 em->block_start = hole_em->block_start;
7128 em->block_len = hole_len;
Liu Bof9e4fb52013-01-07 10:10:12 +00007129 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
7130 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonec29ed52011-02-23 16:23:20 -05007131 } else {
7132 em->start = range_start;
7133 em->len = found;
7134 em->orig_start = range_start;
7135 em->block_start = EXTENT_MAP_DELALLOC;
7136 em->block_len = found;
7137 }
Nikolay Borisovbf8d32b2017-12-01 11:19:43 +02007138 } else {
Chris Masonec29ed52011-02-23 16:23:20 -05007139 return hole_em;
7140 }
7141out:
7142
7143 free_extent_map(hole_em);
7144 if (err) {
7145 free_extent_map(em);
7146 return ERR_PTR(err);
7147 }
7148 return em;
7149}
7150
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007151static struct extent_map *btrfs_create_dio_extent(struct inode *inode,
7152 const u64 start,
7153 const u64 len,
7154 const u64 orig_start,
7155 const u64 block_start,
7156 const u64 block_len,
7157 const u64 orig_block_len,
7158 const u64 ram_bytes,
7159 const int type)
7160{
7161 struct extent_map *em = NULL;
7162 int ret;
7163
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007164 if (type != BTRFS_ORDERED_NOCOW) {
Liu Bo6f9994d2017-01-31 07:50:22 -08007165 em = create_io_em(inode, start, len, orig_start,
7166 block_start, block_len, orig_block_len,
7167 ram_bytes,
7168 BTRFS_COMPRESS_NONE, /* compress_type */
7169 type);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007170 if (IS_ERR(em))
7171 goto out;
7172 }
7173 ret = btrfs_add_ordered_extent_dio(inode, start, block_start,
7174 len, block_len, type);
7175 if (ret) {
7176 if (em) {
7177 free_extent_map(em);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02007178 btrfs_drop_extent_cache(BTRFS_I(inode), start,
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007179 start + len - 1, 0);
7180 }
7181 em = ERR_PTR(ret);
7182 }
7183 out:
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007184
7185 return em;
7186}
7187
Josef Bacik4b46fce2010-05-23 11:00:55 -04007188static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
7189 u64 start, u64 len)
7190{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007191 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007192 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik70c8a912012-10-11 16:54:30 -04007193 struct extent_map *em;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007194 struct btrfs_key ins;
7195 u64 alloc_hint;
7196 int ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007197
Josef Bacik4b46fce2010-05-23 11:00:55 -04007198 alloc_hint = get_extent_allocation_hint(inode, start, len);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007199 ret = btrfs_reserve_extent(root, len, len, fs_info->sectorsize,
Jeff Mahoneyda170662016-06-15 09:22:56 -04007200 0, alloc_hint, &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007201 if (ret)
7202 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007203
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007204 em = btrfs_create_dio_extent(inode, start, ins.offset, start,
7205 ins.objectid, ins.offset, ins.offset,
Liu Bo6288d6e2017-02-21 12:12:58 -08007206 ins.offset, BTRFS_ORDERED_REGULAR);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007207 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007208 if (IS_ERR(em))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007209 btrfs_free_reserved_extent(fs_info, ins.objectid,
7210 ins.offset, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007211
Josef Bacik4b46fce2010-05-23 11:00:55 -04007212 return em;
7213}
7214
Chris Mason46bfbb52010-05-26 11:04:10 -04007215/*
7216 * returns 1 when the nocow is safe, < 1 on error, 0 if the
7217 * block must be cow'd
7218 */
Josef Bacik00361582013-08-14 14:02:47 -04007219noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
Josef Bacik7ee9e442013-06-21 16:37:03 -04007220 u64 *orig_start, u64 *orig_block_len,
7221 u64 *ram_bytes)
Chris Mason46bfbb52010-05-26 11:04:10 -04007222{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007223 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason46bfbb52010-05-26 11:04:10 -04007224 struct btrfs_path *path;
7225 int ret;
7226 struct extent_buffer *leaf;
7227 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xie7b2b7082014-02-27 13:58:05 +08007228 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason46bfbb52010-05-26 11:04:10 -04007229 struct btrfs_file_extent_item *fi;
7230 struct btrfs_key key;
7231 u64 disk_bytenr;
7232 u64 backref_offset;
7233 u64 extent_end;
7234 u64 num_bytes;
7235 int slot;
7236 int found_type;
Josef Bacik7ee9e442013-06-21 16:37:03 -04007237 bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW);
Miao Xiee77751a2013-12-27 21:11:50 +08007238
Chris Mason46bfbb52010-05-26 11:04:10 -04007239 path = btrfs_alloc_path();
7240 if (!path)
7241 return -ENOMEM;
7242
David Sterbaf85b7372017-01-20 14:54:07 +01007243 ret = btrfs_lookup_file_extent(NULL, root, path,
7244 btrfs_ino(BTRFS_I(inode)), offset, 0);
Chris Mason46bfbb52010-05-26 11:04:10 -04007245 if (ret < 0)
7246 goto out;
7247
7248 slot = path->slots[0];
7249 if (ret == 1) {
7250 if (slot == 0) {
7251 /* can't find the item, must cow */
7252 ret = 0;
7253 goto out;
7254 }
7255 slot--;
7256 }
7257 ret = 0;
7258 leaf = path->nodes[0];
7259 btrfs_item_key_to_cpu(leaf, &key, slot);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02007260 if (key.objectid != btrfs_ino(BTRFS_I(inode)) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04007261 key.type != BTRFS_EXTENT_DATA_KEY) {
7262 /* not our file or wrong item type, must cow */
7263 goto out;
7264 }
7265
7266 if (key.offset > offset) {
7267 /* Wrong offset, must cow */
7268 goto out;
7269 }
7270
7271 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
7272 found_type = btrfs_file_extent_type(leaf, fi);
7273 if (found_type != BTRFS_FILE_EXTENT_REG &&
7274 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
7275 /* not a regular extent, must cow */
7276 goto out;
7277 }
Josef Bacik7ee9e442013-06-21 16:37:03 -04007278
7279 if (!nocow && found_type == BTRFS_FILE_EXTENT_REG)
7280 goto out;
7281
Miao Xiee77751a2013-12-27 21:11:50 +08007282 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
7283 if (extent_end <= offset)
7284 goto out;
7285
Chris Mason46bfbb52010-05-26 11:04:10 -04007286 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Josef Bacik7ee9e442013-06-21 16:37:03 -04007287 if (disk_bytenr == 0)
7288 goto out;
7289
7290 if (btrfs_file_extent_compression(leaf, fi) ||
7291 btrfs_file_extent_encryption(leaf, fi) ||
7292 btrfs_file_extent_other_encoding(leaf, fi))
7293 goto out;
7294
Ethan Lien78d42952018-05-17 14:58:29 +08007295 /*
7296 * Do the same check as in btrfs_cross_ref_exist but without the
7297 * unnecessary search.
7298 */
7299 if (btrfs_file_extent_generation(leaf, fi) <=
7300 btrfs_root_last_snapshot(&root->root_item))
7301 goto out;
7302
Chris Mason46bfbb52010-05-26 11:04:10 -04007303 backref_offset = btrfs_file_extent_offset(leaf, fi);
7304
Josef Bacik7ee9e442013-06-21 16:37:03 -04007305 if (orig_start) {
7306 *orig_start = key.offset - backref_offset;
7307 *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
7308 *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
7309 }
Josef Bacikeb384b52013-04-24 16:32:55 -04007310
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007311 if (btrfs_extent_readonly(fs_info, disk_bytenr))
Chris Mason46bfbb52010-05-26 11:04:10 -04007312 goto out;
Miao Xie7b2b7082014-02-27 13:58:05 +08007313
7314 num_bytes = min(offset + *len, extent_end) - offset;
7315 if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) {
7316 u64 range_end;
7317
Jeff Mahoneyda170662016-06-15 09:22:56 -04007318 range_end = round_up(offset + num_bytes,
7319 root->fs_info->sectorsize) - 1;
Miao Xie7b2b7082014-02-27 13:58:05 +08007320 ret = test_range_bit(io_tree, offset, range_end,
7321 EXTENT_DELALLOC, 0, NULL);
7322 if (ret) {
7323 ret = -EAGAIN;
7324 goto out;
7325 }
7326 }
7327
Josef Bacik1bda19e2013-10-18 12:10:36 -04007328 btrfs_release_path(path);
Chris Mason46bfbb52010-05-26 11:04:10 -04007329
7330 /*
7331 * look for other files referencing this extent, if we
7332 * find any we must cow
7333 */
Josef Bacik00361582013-08-14 14:02:47 -04007334
Liu Boe4c3b2d2017-01-30 12:25:28 -08007335 ret = btrfs_cross_ref_exist(root, btrfs_ino(BTRFS_I(inode)),
Josef Bacik00361582013-08-14 14:02:47 -04007336 key.offset - backref_offset, disk_bytenr);
Josef Bacik00361582013-08-14 14:02:47 -04007337 if (ret) {
7338 ret = 0;
7339 goto out;
7340 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007341
7342 /*
7343 * adjust disk_bytenr and num_bytes to cover just the bytes
7344 * in this extent we are about to write. If there
7345 * are any csums in that range we have to cow in order
7346 * to keep the csums correct
7347 */
7348 disk_bytenr += backref_offset;
7349 disk_bytenr += offset - key.offset;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007350 if (csum_exist_in_range(fs_info, disk_bytenr, num_bytes))
7351 goto out;
Chris Mason46bfbb52010-05-26 11:04:10 -04007352 /*
7353 * all of the above have passed, it is safe to overwrite this extent
7354 * without cow
7355 */
Josef Bacikeb384b52013-04-24 16:32:55 -04007356 *len = num_bytes;
Chris Mason46bfbb52010-05-26 11:04:10 -04007357 ret = 1;
7358out:
7359 btrfs_free_path(path);
7360 return ret;
7361}
7362
Josef Bacikeb838e72012-07-31 16:28:48 -04007363static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
7364 struct extent_state **cached_state, int writing)
7365{
7366 struct btrfs_ordered_extent *ordered;
7367 int ret = 0;
7368
7369 while (1) {
7370 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbaff13db42015-12-03 14:30:40 +01007371 cached_state);
Josef Bacikeb838e72012-07-31 16:28:48 -04007372 /*
7373 * We're concerned with the entire range that we're going to be
Nicholas D Steeves01327612016-05-19 21:18:45 -04007374 * doing DIO to, so we need to make sure there's no ordered
Josef Bacikeb838e72012-07-31 16:28:48 -04007375 * extents in this range.
7376 */
Nikolay Borisova776c6f2017-02-20 13:50:49 +02007377 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), lockstart,
Josef Bacikeb838e72012-07-31 16:28:48 -04007378 lockend - lockstart + 1);
7379
7380 /*
7381 * We need to make sure there are no buffered pages in this
7382 * range either, we could have raced between the invalidate in
7383 * generic_file_direct_write and locking the extent. The
7384 * invalidate needs to happen so that reads after a write do not
7385 * get stale data.
7386 */
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007387 if (!ordered &&
David Sterba051c98e2018-03-07 15:33:22 +01007388 (!writing || !filemap_range_has_page(inode->i_mapping,
7389 lockstart, lockend)))
Josef Bacikeb838e72012-07-31 16:28:48 -04007390 break;
7391
7392 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbae43bbe52017-12-12 21:43:52 +01007393 cached_state);
Josef Bacikeb838e72012-07-31 16:28:48 -04007394
7395 if (ordered) {
Filipe Mananaade77022016-02-18 14:28:55 +00007396 /*
7397 * If we are doing a DIO read and the ordered extent we
7398 * found is for a buffered write, we can not wait for it
7399 * to complete and retry, because if we do so we can
7400 * deadlock with concurrent buffered writes on page
7401 * locks. This happens only if our DIO read covers more
7402 * than one extent map, if at this point has already
7403 * created an ordered extent for a previous extent map
7404 * and locked its range in the inode's io tree, and a
7405 * concurrent write against that previous extent map's
7406 * range and this range started (we unlock the ranges
7407 * in the io tree only when the bios complete and
7408 * buffered writes always lock pages before attempting
7409 * to lock range in the io tree).
7410 */
7411 if (writing ||
7412 test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags))
7413 btrfs_start_ordered_extent(inode, ordered, 1);
7414 else
7415 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007416 btrfs_put_ordered_extent(ordered);
7417 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007418 /*
Filipe Mananab850ae12015-12-08 16:23:16 +00007419 * We could trigger writeback for this range (and wait
7420 * for it to complete) and then invalidate the pages for
7421 * this range (through invalidate_inode_pages2_range()),
7422 * but that can lead us to a deadlock with a concurrent
7423 * call to readpages() (a buffered read or a defrag call
7424 * triggered a readahead) on a page lock due to an
7425 * ordered dio extent we created before but did not have
7426 * yet a corresponding bio submitted (whence it can not
7427 * complete), which makes readpages() wait for that
7428 * ordered extent to complete while holding a lock on
7429 * that page.
Josef Bacikeb838e72012-07-31 16:28:48 -04007430 */
Filipe Mananab850ae12015-12-08 16:23:16 +00007431 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007432 }
7433
Filipe Mananaade77022016-02-18 14:28:55 +00007434 if (ret)
7435 break;
7436
Josef Bacikeb838e72012-07-31 16:28:48 -04007437 cond_resched();
7438 }
7439
7440 return ret;
7441}
7442
Liu Bo6f9994d2017-01-31 07:50:22 -08007443/* The callers of this must take lock_extent() */
7444static struct extent_map *create_io_em(struct inode *inode, u64 start, u64 len,
7445 u64 orig_start, u64 block_start,
7446 u64 block_len, u64 orig_block_len,
7447 u64 ram_bytes, int compress_type,
7448 int type)
Josef Bacik69ffb542012-09-11 15:40:07 -04007449{
7450 struct extent_map_tree *em_tree;
7451 struct extent_map *em;
7452 struct btrfs_root *root = BTRFS_I(inode)->root;
7453 int ret;
7454
Liu Bo6f9994d2017-01-31 07:50:22 -08007455 ASSERT(type == BTRFS_ORDERED_PREALLOC ||
7456 type == BTRFS_ORDERED_COMPRESSED ||
7457 type == BTRFS_ORDERED_NOCOW ||
Liu Bo1af4a0a2017-02-13 15:35:09 -08007458 type == BTRFS_ORDERED_REGULAR);
Liu Bo6f9994d2017-01-31 07:50:22 -08007459
Josef Bacik69ffb542012-09-11 15:40:07 -04007460 em_tree = &BTRFS_I(inode)->extent_tree;
7461 em = alloc_extent_map();
7462 if (!em)
7463 return ERR_PTR(-ENOMEM);
7464
7465 em->start = start;
7466 em->orig_start = orig_start;
7467 em->len = len;
7468 em->block_len = block_len;
7469 em->block_start = block_start;
7470 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacikb4939682012-12-03 10:31:19 -05007471 em->orig_block_len = orig_block_len;
Josef Bacikcc95bef2013-04-04 14:31:27 -04007472 em->ram_bytes = ram_bytes;
Josef Bacik70c8a912012-10-11 16:54:30 -04007473 em->generation = -1;
Josef Bacik69ffb542012-09-11 15:40:07 -04007474 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Liu Bo1af4a0a2017-02-13 15:35:09 -08007475 if (type == BTRFS_ORDERED_PREALLOC) {
Josef Bacikb11e2342012-12-03 10:58:15 -05007476 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Liu Bo1af4a0a2017-02-13 15:35:09 -08007477 } else if (type == BTRFS_ORDERED_COMPRESSED) {
Liu Bo6f9994d2017-01-31 07:50:22 -08007478 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
7479 em->compress_type = compress_type;
7480 }
Josef Bacik69ffb542012-09-11 15:40:07 -04007481
7482 do {
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02007483 btrfs_drop_extent_cache(BTRFS_I(inode), em->start,
Josef Bacik69ffb542012-09-11 15:40:07 -04007484 em->start + em->len - 1, 0);
7485 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04007486 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik69ffb542012-09-11 15:40:07 -04007487 write_unlock(&em_tree->lock);
Liu Bo6f9994d2017-01-31 07:50:22 -08007488 /*
7489 * The caller has taken lock_extent(), who could race with us
7490 * to add em?
7491 */
Josef Bacik69ffb542012-09-11 15:40:07 -04007492 } while (ret == -EEXIST);
7493
7494 if (ret) {
7495 free_extent_map(em);
7496 return ERR_PTR(ret);
7497 }
7498
Liu Bo6f9994d2017-01-31 07:50:22 -08007499 /* em got 2 refs now, callers needs to do free_extent_map once. */
Josef Bacik69ffb542012-09-11 15:40:07 -04007500 return em;
7501}
7502
Nikolay Borisov1c8d0172018-05-02 15:19:32 +03007503
7504static int btrfs_get_blocks_direct_read(struct extent_map *em,
7505 struct buffer_head *bh_result,
7506 struct inode *inode,
7507 u64 start, u64 len)
7508{
7509 if (em->block_start == EXTENT_MAP_HOLE ||
7510 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7511 return -ENOENT;
7512
7513 len = min(len, em->len - (start - em->start));
7514
7515 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
7516 inode->i_blkbits;
7517 bh_result->b_size = len;
7518 bh_result->b_bdev = em->bdev;
7519 set_buffer_mapped(bh_result);
7520
7521 return 0;
7522}
7523
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007524static int btrfs_get_blocks_direct_write(struct extent_map **map,
7525 struct buffer_head *bh_result,
7526 struct inode *inode,
7527 struct btrfs_dio_data *dio_data,
7528 u64 start, u64 len)
7529{
7530 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
7531 struct extent_map *em = *map;
7532 int ret = 0;
7533
7534 /*
7535 * We don't allocate a new extent in the following cases
7536 *
7537 * 1) The inode is marked as NODATACOW. In this case we'll just use the
7538 * existing extent.
7539 * 2) The extent is marked as PREALLOC. We're good to go here and can
7540 * just use the extent.
7541 *
7542 */
7543 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
7544 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
7545 em->block_start != EXTENT_MAP_HOLE)) {
7546 int type;
7547 u64 block_start, orig_start, orig_block_len, ram_bytes;
7548
7549 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7550 type = BTRFS_ORDERED_PREALLOC;
7551 else
7552 type = BTRFS_ORDERED_NOCOW;
7553 len = min(len, em->len - (start - em->start));
7554 block_start = em->block_start + (start - em->start);
7555
7556 if (can_nocow_extent(inode, start, &len, &orig_start,
7557 &orig_block_len, &ram_bytes) == 1 &&
7558 btrfs_inc_nocow_writers(fs_info, block_start)) {
7559 struct extent_map *em2;
7560
7561 em2 = btrfs_create_dio_extent(inode, start, len,
7562 orig_start, block_start,
7563 len, orig_block_len,
7564 ram_bytes, type);
7565 btrfs_dec_nocow_writers(fs_info, block_start);
7566 if (type == BTRFS_ORDERED_PREALLOC) {
7567 free_extent_map(em);
7568 *map = em = em2;
7569 }
7570
7571 if (em2 && IS_ERR(em2)) {
7572 ret = PTR_ERR(em2);
7573 goto out;
7574 }
7575 /*
7576 * For inode marked NODATACOW or extent marked PREALLOC,
7577 * use the existing or preallocated extent, so does not
7578 * need to adjust btrfs_space_info's bytes_may_use.
7579 */
7580 btrfs_free_reserved_data_space_noquota(inode, start,
7581 len);
7582 goto skip_cow;
7583 }
7584 }
7585
7586 /* this will cow the extent */
7587 len = bh_result->b_size;
7588 free_extent_map(em);
7589 *map = em = btrfs_new_extent_direct(inode, start, len);
7590 if (IS_ERR(em)) {
7591 ret = PTR_ERR(em);
7592 goto out;
7593 }
7594
7595 len = min(len, em->len - (start - em->start));
7596
7597skip_cow:
7598 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
7599 inode->i_blkbits;
7600 bh_result->b_size = len;
7601 bh_result->b_bdev = em->bdev;
7602 set_buffer_mapped(bh_result);
7603
7604 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7605 set_buffer_new(bh_result);
7606
7607 /*
7608 * Need to update the i_size under the extent lock so buffered
7609 * readers will get the updated i_size when we unlock.
7610 */
7611 if (!dio_data->overwrite && start + len > i_size_read(inode))
7612 i_size_write(inode, start + len);
7613
7614 WARN_ON(dio_data->reserve < len);
7615 dio_data->reserve -= len;
7616 dio_data->unsubmitted_oe_range_end = start + len;
7617 current->journal_info = dio_data;
7618out:
7619 return ret;
7620}
7621
Josef Bacik4b46fce2010-05-23 11:00:55 -04007622static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
7623 struct buffer_head *bh_result, int create)
7624{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007625 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007626 struct extent_map *em;
Josef Bacikeb838e72012-07-31 16:28:48 -04007627 struct extent_state *cached_state = NULL;
chandan50745b02015-08-28 21:10:13 +05307628 struct btrfs_dio_data *dio_data = NULL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007629 u64 start = iblock << inode->i_blkbits;
Josef Bacikeb838e72012-07-31 16:28:48 -04007630 u64 lockstart, lockend;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007631 u64 len = bh_result->b_size;
Josef Bacikeb838e72012-07-31 16:28:48 -04007632 int unlock_bits = EXTENT_LOCKED;
Miao Xie09348562013-02-07 10:12:07 +00007633 int ret = 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007634
Miao Xie172a5042013-02-21 02:48:22 -07007635 if (create)
Josef Bacik32667892015-02-11 15:08:58 -05007636 unlock_bits |= EXTENT_DIRTY;
Miao Xie172a5042013-02-21 02:48:22 -07007637 else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007638 len = min_t(u64, len, fs_info->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04007639
Josef Bacikc3298612012-08-03 16:49:19 -04007640 lockstart = start;
7641 lockend = start + len - 1;
7642
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007643 if (current->journal_info) {
7644 /*
7645 * Need to pull our outstanding extents and set journal_info to NULL so
Nicholas D Steeves01327612016-05-19 21:18:45 -04007646 * that anything that needs to check if there's a transaction doesn't get
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007647 * confused.
7648 */
chandan50745b02015-08-28 21:10:13 +05307649 dio_data = current->journal_info;
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007650 current->journal_info = NULL;
7651 }
7652
Josef Bacikeb838e72012-07-31 16:28:48 -04007653 /*
7654 * If this errors out it's because we couldn't invalidate pagecache for
7655 * this range and we need to fallback to buffered.
7656 */
Filipe Manana9c9464c2015-11-04 09:52:04 +00007657 if (lock_extent_direct(inode, lockstart, lockend, &cached_state,
7658 create)) {
7659 ret = -ENOTBLK;
7660 goto err;
7661 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007662
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007663 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len, 0);
Josef Bacikeb838e72012-07-31 16:28:48 -04007664 if (IS_ERR(em)) {
7665 ret = PTR_ERR(em);
7666 goto unlock_err;
7667 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007668
7669 /*
7670 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
7671 * io. INLINE is special, and we could probably kludge it in here, but
7672 * it's still buffered so for safety lets just fall back to the generic
7673 * buffered path.
7674 *
7675 * For COMPRESSED we _have_ to read the entire extent in so we can
7676 * decompress it, so there will be buffering required no matter what we
7677 * do, so go ahead and fallback to buffered.
7678 *
Nicholas D Steeves01327612016-05-19 21:18:45 -04007679 * We return -ENOTBLK because that's what makes DIO go ahead and go back
Josef Bacik4b46fce2010-05-23 11:00:55 -04007680 * to buffered IO. Don't blame me, this is the price we pay for using
7681 * the generic code.
7682 */
7683 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
7684 em->block_start == EXTENT_MAP_INLINE) {
7685 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007686 ret = -ENOTBLK;
7687 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007688 }
7689
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007690 if (create) {
7691 ret = btrfs_get_blocks_direct_write(&em, bh_result, inode,
7692 dio_data, start, len);
7693 if (ret < 0)
7694 goto unlock_err;
7695
7696 /* clear and unlock the entire range */
7697 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7698 unlock_bits, 1, 0, &cached_state);
7699 } else {
Nikolay Borisov1c8d0172018-05-02 15:19:32 +03007700 ret = btrfs_get_blocks_direct_read(em, bh_result, inode,
7701 start, len);
7702 /* Can be negative only if we read from a hole */
7703 if (ret < 0) {
7704 ret = 0;
7705 free_extent_map(em);
7706 goto unlock_err;
7707 }
7708 /*
7709 * We need to unlock only the end area that we aren't using.
7710 * The rest is going to be unlocked by the endio routine.
7711 */
7712 lockstart = start + bh_result->b_size;
7713 if (lockstart < lockend) {
7714 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
7715 lockend, unlock_bits, 1, 0,
7716 &cached_state);
7717 } else {
7718 free_extent_state(cached_state);
7719 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007720 }
7721
Josef Bacik4b46fce2010-05-23 11:00:55 -04007722 free_extent_map(em);
7723
7724 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007725
7726unlock_err:
Josef Bacikeb838e72012-07-31 16:28:48 -04007727 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbaae0f1622017-10-31 16:37:52 +01007728 unlock_bits, 1, 0, &cached_state);
Filipe Manana9c9464c2015-11-04 09:52:04 +00007729err:
chandan50745b02015-08-28 21:10:13 +05307730 if (dio_data)
7731 current->journal_info = dio_data;
Josef Bacikeb838e72012-07-31 16:28:48 -04007732 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007733}
7734
Omar Sandoval58efbc92017-08-22 23:45:59 -07007735static inline blk_status_t submit_dio_repair_bio(struct inode *inode,
7736 struct bio *bio,
7737 int mirror_num)
Miao Xie8b110e32014-09-12 18:44:03 +08007738{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007739 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Omar Sandoval58efbc92017-08-22 23:45:59 -07007740 blk_status_t ret;
Miao Xie8b110e32014-09-12 18:44:03 +08007741
Mike Christie37226b22016-06-05 14:31:52 -05007742 BUG_ON(bio_op(bio) == REQ_OP_WRITE);
Miao Xie8b110e32014-09-12 18:44:03 +08007743
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007744 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DIO_REPAIR);
Miao Xie8b110e32014-09-12 18:44:03 +08007745 if (ret)
Nikolay Borisovea057f62017-12-13 10:25:38 +02007746 return ret;
Miao Xie8b110e32014-09-12 18:44:03 +08007747
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007748 ret = btrfs_map_bio(fs_info, bio, mirror_num, 0);
Nikolay Borisovea057f62017-12-13 10:25:38 +02007749
Miao Xie8b110e32014-09-12 18:44:03 +08007750 return ret;
7751}
7752
7753static int btrfs_check_dio_repairable(struct inode *inode,
7754 struct bio *failed_bio,
7755 struct io_failure_record *failrec,
7756 int failed_mirror)
7757{
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007758 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xie8b110e32014-09-12 18:44:03 +08007759 int num_copies;
7760
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007761 num_copies = btrfs_num_copies(fs_info, failrec->logical, failrec->len);
Miao Xie8b110e32014-09-12 18:44:03 +08007762 if (num_copies == 1) {
7763 /*
7764 * we only have a single copy of the data, so don't bother with
7765 * all the retry and error correction code that follows. no
7766 * matter what the error is, it is very likely to persist.
7767 */
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007768 btrfs_debug(fs_info,
7769 "Check DIO Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d",
7770 num_copies, failrec->this_mirror, failed_mirror);
Miao Xie8b110e32014-09-12 18:44:03 +08007771 return 0;
7772 }
7773
7774 failrec->failed_mirror = failed_mirror;
7775 failrec->this_mirror++;
7776 if (failrec->this_mirror == failed_mirror)
7777 failrec->this_mirror++;
7778
7779 if (failrec->this_mirror > num_copies) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007780 btrfs_debug(fs_info,
7781 "Check DIO Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d",
7782 num_copies, failrec->this_mirror, failed_mirror);
Miao Xie8b110e32014-09-12 18:44:03 +08007783 return 0;
7784 }
7785
7786 return 1;
7787}
7788
Omar Sandoval58efbc92017-08-22 23:45:59 -07007789static blk_status_t dio_read_error(struct inode *inode, struct bio *failed_bio,
7790 struct page *page, unsigned int pgoff,
7791 u64 start, u64 end, int failed_mirror,
7792 bio_end_io_t *repair_endio, void *repair_arg)
Miao Xie8b110e32014-09-12 18:44:03 +08007793{
7794 struct io_failure_record *failrec;
Josef Bacik7870d082017-05-05 11:57:15 -04007795 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
7796 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
Miao Xie8b110e32014-09-12 18:44:03 +08007797 struct bio *bio;
7798 int isector;
David Sterbaf1c77c52017-06-06 19:03:49 +02007799 unsigned int read_mode = 0;
Liu Bo17347ce2017-05-15 15:33:27 -07007800 int segs;
Miao Xie8b110e32014-09-12 18:44:03 +08007801 int ret;
Omar Sandoval58efbc92017-08-22 23:45:59 -07007802 blk_status_t status;
Ming Leic16a8ac2017-12-18 20:22:12 +08007803 struct bio_vec bvec;
Miao Xie8b110e32014-09-12 18:44:03 +08007804
Mike Christie37226b22016-06-05 14:31:52 -05007805 BUG_ON(bio_op(failed_bio) == REQ_OP_WRITE);
Miao Xie8b110e32014-09-12 18:44:03 +08007806
7807 ret = btrfs_get_io_failure_record(inode, start, end, &failrec);
7808 if (ret)
Omar Sandoval58efbc92017-08-22 23:45:59 -07007809 return errno_to_blk_status(ret);
Miao Xie8b110e32014-09-12 18:44:03 +08007810
7811 ret = btrfs_check_dio_repairable(inode, failed_bio, failrec,
7812 failed_mirror);
7813 if (!ret) {
Josef Bacik7870d082017-05-05 11:57:15 -04007814 free_io_failure(failure_tree, io_tree, failrec);
Omar Sandoval58efbc92017-08-22 23:45:59 -07007815 return BLK_STS_IOERR;
Miao Xie8b110e32014-09-12 18:44:03 +08007816 }
7817
Liu Bo17347ce2017-05-15 15:33:27 -07007818 segs = bio_segments(failed_bio);
Ming Leic16a8ac2017-12-18 20:22:12 +08007819 bio_get_first_bvec(failed_bio, &bvec);
Liu Bo17347ce2017-05-15 15:33:27 -07007820 if (segs > 1 ||
Ming Leic16a8ac2017-12-18 20:22:12 +08007821 (bvec.bv_len > btrfs_inode_sectorsize(inode)))
Christoph Hellwig70fd7612016-11-01 07:40:10 -06007822 read_mode |= REQ_FAILFAST_DEV;
Miao Xie8b110e32014-09-12 18:44:03 +08007823
7824 isector = start - btrfs_io_bio(failed_bio)->logical;
7825 isector >>= inode->i_sb->s_blocksize_bits;
7826 bio = btrfs_create_repair_bio(inode, failed_bio, failrec, page,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307827 pgoff, isector, repair_endio, repair_arg);
David Sterbaebcc3262018-06-29 10:56:53 +02007828 bio->bi_opf = REQ_OP_READ | read_mode;
Miao Xie8b110e32014-09-12 18:44:03 +08007829
7830 btrfs_debug(BTRFS_I(inode)->root->fs_info,
David Sterba913e1532017-07-13 15:32:18 +02007831 "repair DIO read error: submitting new dio read[%#x] to this_mirror=%d, in_validation=%d",
Miao Xie8b110e32014-09-12 18:44:03 +08007832 read_mode, failrec->this_mirror, failrec->in_validation);
7833
Omar Sandoval58efbc92017-08-22 23:45:59 -07007834 status = submit_dio_repair_bio(inode, bio, failrec->this_mirror);
7835 if (status) {
Josef Bacik7870d082017-05-05 11:57:15 -04007836 free_io_failure(failure_tree, io_tree, failrec);
Miao Xie8b110e32014-09-12 18:44:03 +08007837 bio_put(bio);
7838 }
7839
Omar Sandoval58efbc92017-08-22 23:45:59 -07007840 return status;
Miao Xie8b110e32014-09-12 18:44:03 +08007841}
7842
7843struct btrfs_retry_complete {
7844 struct completion done;
7845 struct inode *inode;
7846 u64 start;
7847 int uptodate;
7848};
7849
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007850static void btrfs_retry_endio_nocsum(struct bio *bio)
Miao Xie8b110e32014-09-12 18:44:03 +08007851{
7852 struct btrfs_retry_complete *done = bio->bi_private;
Josef Bacik7870d082017-05-05 11:57:15 -04007853 struct inode *inode = done->inode;
Miao Xie8b110e32014-09-12 18:44:03 +08007854 struct bio_vec *bvec;
Josef Bacik7870d082017-05-05 11:57:15 -04007855 struct extent_io_tree *io_tree, *failure_tree;
Miao Xie8b110e32014-09-12 18:44:03 +08007856 int i;
7857
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02007858 if (bio->bi_status)
Miao Xie8b110e32014-09-12 18:44:03 +08007859 goto end;
7860
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307861 ASSERT(bio->bi_vcnt == 1);
Josef Bacik7870d082017-05-05 11:57:15 -04007862 io_tree = &BTRFS_I(inode)->io_tree;
7863 failure_tree = &BTRFS_I(inode)->io_failure_tree;
Ming Lei263663c2017-12-18 20:22:04 +08007864 ASSERT(bio_first_bvec_all(bio)->bv_len == btrfs_inode_sectorsize(inode));
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307865
Miao Xie8b110e32014-09-12 18:44:03 +08007866 done->uptodate = 1;
David Sterbac09abff2017-07-13 18:10:07 +02007867 ASSERT(!bio_flagged(bio, BIO_CLONED));
Miao Xie8b110e32014-09-12 18:44:03 +08007868 bio_for_each_segment_all(bvec, bio, i)
Josef Bacik7870d082017-05-05 11:57:15 -04007869 clean_io_failure(BTRFS_I(inode)->root->fs_info, failure_tree,
7870 io_tree, done->start, bvec->bv_page,
7871 btrfs_ino(BTRFS_I(inode)), 0);
Miao Xie8b110e32014-09-12 18:44:03 +08007872end:
7873 complete(&done->done);
7874 bio_put(bio);
7875}
7876
Omar Sandoval58efbc92017-08-22 23:45:59 -07007877static blk_status_t __btrfs_correct_data_nocsum(struct inode *inode,
7878 struct btrfs_io_bio *io_bio)
Josef Bacik4b46fce2010-05-23 11:00:55 -04007879{
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307880 struct btrfs_fs_info *fs_info;
Liu Bo17347ce2017-05-15 15:33:27 -07007881 struct bio_vec bvec;
7882 struct bvec_iter iter;
Miao Xie8b110e32014-09-12 18:44:03 +08007883 struct btrfs_retry_complete done;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007884 u64 start;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307885 unsigned int pgoff;
7886 u32 sectorsize;
7887 int nr_sectors;
Omar Sandoval58efbc92017-08-22 23:45:59 -07007888 blk_status_t ret;
7889 blk_status_t err = BLK_STS_OK;
Miao Xiedc380ae2014-09-12 18:43:55 +08007890
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307891 fs_info = BTRFS_I(inode)->root->fs_info;
Jeff Mahoneyda170662016-06-15 09:22:56 -04007892 sectorsize = fs_info->sectorsize;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307893
Miao Xiec1dc0892014-09-12 18:43:56 +08007894 start = io_bio->logical;
Miao Xie8b110e32014-09-12 18:44:03 +08007895 done.inode = inode;
Liu Bo17347ce2017-05-15 15:33:27 -07007896 io_bio->bio.bi_iter = io_bio->iter;
Miao Xie8b110e32014-09-12 18:44:03 +08007897
Liu Bo17347ce2017-05-15 15:33:27 -07007898 bio_for_each_segment(bvec, &io_bio->bio, iter) {
7899 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec.bv_len);
7900 pgoff = bvec.bv_offset;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307901
7902next_block_or_try_again:
Miao Xie8b110e32014-09-12 18:44:03 +08007903 done.uptodate = 0;
7904 done.start = start;
7905 init_completion(&done.done);
7906
Liu Bo17347ce2017-05-15 15:33:27 -07007907 ret = dio_read_error(inode, &io_bio->bio, bvec.bv_page,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307908 pgoff, start, start + sectorsize - 1,
7909 io_bio->mirror_num,
7910 btrfs_retry_endio_nocsum, &done);
Liu Bo629ebf42017-05-15 17:20:07 -07007911 if (ret) {
7912 err = ret;
7913 goto next;
7914 }
Miao Xie8b110e32014-09-12 18:44:03 +08007915
David Sterba9c17f6c2017-07-19 19:26:45 +02007916 wait_for_completion_io(&done.done);
Miao Xie8b110e32014-09-12 18:44:03 +08007917
7918 if (!done.uptodate) {
7919 /* We might have another mirror, so try again */
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307920 goto next_block_or_try_again;
Miao Xie8b110e32014-09-12 18:44:03 +08007921 }
7922
Liu Bo629ebf42017-05-15 17:20:07 -07007923next:
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307924 start += sectorsize;
7925
Liu Bo97bf5a52017-04-07 13:11:10 -07007926 nr_sectors--;
7927 if (nr_sectors) {
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307928 pgoff += sectorsize;
Liu Bo97bf5a52017-04-07 13:11:10 -07007929 ASSERT(pgoff < PAGE_SIZE);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307930 goto next_block_or_try_again;
7931 }
Miao Xie8b110e32014-09-12 18:44:03 +08007932 }
7933
Liu Bo629ebf42017-05-15 17:20:07 -07007934 return err;
Miao Xie8b110e32014-09-12 18:44:03 +08007935}
7936
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007937static void btrfs_retry_endio(struct bio *bio)
Miao Xie8b110e32014-09-12 18:44:03 +08007938{
7939 struct btrfs_retry_complete *done = bio->bi_private;
7940 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
Josef Bacik7870d082017-05-05 11:57:15 -04007941 struct extent_io_tree *io_tree, *failure_tree;
7942 struct inode *inode = done->inode;
Miao Xie8b110e32014-09-12 18:44:03 +08007943 struct bio_vec *bvec;
7944 int uptodate;
7945 int ret;
7946 int i;
7947
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02007948 if (bio->bi_status)
Miao Xie8b110e32014-09-12 18:44:03 +08007949 goto end;
7950
7951 uptodate = 1;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307952
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307953 ASSERT(bio->bi_vcnt == 1);
Ming Lei263663c2017-12-18 20:22:04 +08007954 ASSERT(bio_first_bvec_all(bio)->bv_len == btrfs_inode_sectorsize(done->inode));
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307955
Josef Bacik7870d082017-05-05 11:57:15 -04007956 io_tree = &BTRFS_I(inode)->io_tree;
7957 failure_tree = &BTRFS_I(inode)->io_failure_tree;
7958
David Sterbac09abff2017-07-13 18:10:07 +02007959 ASSERT(!bio_flagged(bio, BIO_CLONED));
Miao Xie8b110e32014-09-12 18:44:03 +08007960 bio_for_each_segment_all(bvec, bio, i) {
Josef Bacik7870d082017-05-05 11:57:15 -04007961 ret = __readpage_endio_check(inode, io_bio, i, bvec->bv_page,
7962 bvec->bv_offset, done->start,
7963 bvec->bv_len);
Miao Xie8b110e32014-09-12 18:44:03 +08007964 if (!ret)
Josef Bacik7870d082017-05-05 11:57:15 -04007965 clean_io_failure(BTRFS_I(inode)->root->fs_info,
7966 failure_tree, io_tree, done->start,
7967 bvec->bv_page,
7968 btrfs_ino(BTRFS_I(inode)),
7969 bvec->bv_offset);
Miao Xie8b110e32014-09-12 18:44:03 +08007970 else
7971 uptodate = 0;
7972 }
7973
7974 done->uptodate = uptodate;
7975end:
7976 complete(&done->done);
7977 bio_put(bio);
7978}
7979
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02007980static blk_status_t __btrfs_subio_endio_read(struct inode *inode,
7981 struct btrfs_io_bio *io_bio, blk_status_t err)
Miao Xie8b110e32014-09-12 18:44:03 +08007982{
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307983 struct btrfs_fs_info *fs_info;
Liu Bo17347ce2017-05-15 15:33:27 -07007984 struct bio_vec bvec;
7985 struct bvec_iter iter;
Miao Xie8b110e32014-09-12 18:44:03 +08007986 struct btrfs_retry_complete done;
7987 u64 start;
7988 u64 offset = 0;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307989 u32 sectorsize;
7990 int nr_sectors;
7991 unsigned int pgoff;
7992 int csum_pos;
Liu Boef7cdac12017-04-13 18:11:48 -07007993 bool uptodate = (err == 0);
Miao Xie8b110e32014-09-12 18:44:03 +08007994 int ret;
Omar Sandoval58efbc92017-08-22 23:45:59 -07007995 blk_status_t status;
Miao Xie8b110e32014-09-12 18:44:03 +08007996
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307997 fs_info = BTRFS_I(inode)->root->fs_info;
Jeff Mahoneyda170662016-06-15 09:22:56 -04007998 sectorsize = fs_info->sectorsize;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307999
Omar Sandoval58efbc92017-08-22 23:45:59 -07008000 err = BLK_STS_OK;
Miao Xie8b110e32014-09-12 18:44:03 +08008001 start = io_bio->logical;
8002 done.inode = inode;
Liu Bo17347ce2017-05-15 15:33:27 -07008003 io_bio->bio.bi_iter = io_bio->iter;
Miao Xie8b110e32014-09-12 18:44:03 +08008004
Liu Bo17347ce2017-05-15 15:33:27 -07008005 bio_for_each_segment(bvec, &io_bio->bio, iter) {
8006 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec.bv_len);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308007
Liu Bo17347ce2017-05-15 15:33:27 -07008008 pgoff = bvec.bv_offset;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308009next_block:
Liu Boef7cdac12017-04-13 18:11:48 -07008010 if (uptodate) {
8011 csum_pos = BTRFS_BYTES_TO_BLKS(fs_info, offset);
8012 ret = __readpage_endio_check(inode, io_bio, csum_pos,
8013 bvec.bv_page, pgoff, start, sectorsize);
8014 if (likely(!ret))
8015 goto next;
8016 }
Miao Xie8b110e32014-09-12 18:44:03 +08008017try_again:
8018 done.uptodate = 0;
8019 done.start = start;
8020 init_completion(&done.done);
8021
Omar Sandoval58efbc92017-08-22 23:45:59 -07008022 status = dio_read_error(inode, &io_bio->bio, bvec.bv_page,
8023 pgoff, start, start + sectorsize - 1,
8024 io_bio->mirror_num, btrfs_retry_endio,
8025 &done);
8026 if (status) {
8027 err = status;
Miao Xie8b110e32014-09-12 18:44:03 +08008028 goto next;
8029 }
8030
David Sterba9c17f6c2017-07-19 19:26:45 +02008031 wait_for_completion_io(&done.done);
Miao Xie8b110e32014-09-12 18:44:03 +08008032
8033 if (!done.uptodate) {
8034 /* We might have another mirror, so try again */
8035 goto try_again;
8036 }
8037next:
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308038 offset += sectorsize;
8039 start += sectorsize;
8040
8041 ASSERT(nr_sectors);
8042
Liu Bo97bf5a52017-04-07 13:11:10 -07008043 nr_sectors--;
8044 if (nr_sectors) {
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308045 pgoff += sectorsize;
Liu Bo97bf5a52017-04-07 13:11:10 -07008046 ASSERT(pgoff < PAGE_SIZE);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308047 goto next_block;
8048 }
Kent Overstreet2c30c712013-11-07 12:20:26 -08008049 }
Miao Xiec1dc0892014-09-12 18:43:56 +08008050
8051 return err;
8052}
8053
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008054static blk_status_t btrfs_subio_endio_read(struct inode *inode,
8055 struct btrfs_io_bio *io_bio, blk_status_t err)
Miao Xie8b110e32014-09-12 18:44:03 +08008056{
8057 bool skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
8058
8059 if (skip_csum) {
8060 if (unlikely(err))
8061 return __btrfs_correct_data_nocsum(inode, io_bio);
8062 else
Omar Sandoval58efbc92017-08-22 23:45:59 -07008063 return BLK_STS_OK;
Miao Xie8b110e32014-09-12 18:44:03 +08008064 } else {
8065 return __btrfs_subio_endio_read(inode, io_bio, err);
8066 }
8067}
8068
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008069static void btrfs_endio_direct_read(struct bio *bio)
Miao Xiec1dc0892014-09-12 18:43:56 +08008070{
8071 struct btrfs_dio_private *dip = bio->bi_private;
8072 struct inode *inode = dip->inode;
8073 struct bio *dio_bio;
8074 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008075 blk_status_t err = bio->bi_status;
Miao Xiec1dc0892014-09-12 18:43:56 +08008076
Liu Bo99c4e3b92017-09-15 15:06:51 -06008077 if (dip->flags & BTRFS_DIO_ORIG_BIO_SUBMITTED)
Miao Xie8b110e32014-09-12 18:44:03 +08008078 err = btrfs_subio_endio_read(inode, io_bio, err);
Miao Xiec1dc0892014-09-12 18:43:56 +08008079
Josef Bacik4b46fce2010-05-23 11:00:55 -04008080 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01008081 dip->logical_offset + dip->bytes - 1);
Chris Mason9be33952013-05-17 18:30:14 -04008082 dio_bio = dip->dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008083
Josef Bacik4b46fce2010-05-23 11:00:55 -04008084 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04008085
Liu Bo99c4e3b92017-09-15 15:06:51 -06008086 dio_bio->bi_status = err;
Christoph Hellwig40553512017-06-03 09:37:58 +02008087 dio_end_io(dio_bio);
Miao Xie23ea8e52014-09-12 18:43:54 +08008088
8089 if (io_bio->end_io)
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008090 io_bio->end_io(io_bio, blk_status_to_errno(err));
Chris Mason9be33952013-05-17 18:30:14 -04008091 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008092}
8093
Qu Wenruo524272602017-03-08 10:25:52 +08008094static void __endio_write_update_ordered(struct inode *inode,
8095 const u64 offset, const u64 bytes,
8096 const bool uptodate)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008097{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008098 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008099 struct btrfs_ordered_extent *ordered = NULL;
Qu Wenruo524272602017-03-08 10:25:52 +08008100 struct btrfs_workqueue *wq;
8101 btrfs_work_func_t func;
Filipe Manana14543772015-11-24 16:23:54 +00008102 u64 ordered_offset = offset;
8103 u64 ordered_bytes = bytes;
Naohiro Aota67c003f2017-09-01 17:59:07 +09008104 u64 last_offset;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008105
Qu Wenruo524272602017-03-08 10:25:52 +08008106 if (btrfs_is_free_space_inode(BTRFS_I(inode))) {
8107 wq = fs_info->endio_freespace_worker;
8108 func = btrfs_freespace_write_helper;
8109 } else {
8110 wq = fs_info->endio_write_workers;
8111 func = btrfs_endio_write_helper;
8112 }
8113
Nikolay Borisovb25f0d02018-04-11 11:21:17 +03008114 while (ordered_offset < offset + bytes) {
8115 last_offset = ordered_offset;
8116 if (btrfs_dec_test_first_ordered_pending(inode, &ordered,
8117 &ordered_offset,
8118 ordered_bytes,
8119 uptodate)) {
8120 btrfs_init_work(&ordered->work, func,
8121 finish_ordered_fn,
8122 NULL, NULL);
8123 btrfs_queue_work(wq, &ordered->work);
8124 }
8125 /*
8126 * If btrfs_dec_test_ordered_pending does not find any ordered
8127 * extent in the range, we can exit.
8128 */
8129 if (ordered_offset == last_offset)
8130 return;
8131 /*
8132 * Our bio might span multiple ordered extents. In this case
8133 * we keep goin until we have accounted the whole dio.
8134 */
8135 if (ordered_offset < offset + bytes) {
8136 ordered_bytes = offset + bytes - ordered_offset;
8137 ordered = NULL;
8138 }
Chris Mason163cf092010-11-28 19:56:33 -05008139 }
Filipe Manana14543772015-11-24 16:23:54 +00008140}
8141
8142static void btrfs_endio_direct_write(struct bio *bio)
8143{
8144 struct btrfs_dio_private *dip = bio->bi_private;
8145 struct bio *dio_bio = dip->dio_bio;
8146
Qu Wenruo524272602017-03-08 10:25:52 +08008147 __endio_write_update_ordered(dip->inode, dip->logical_offset,
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008148 dip->bytes, !bio->bi_status);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008149
Josef Bacik4b46fce2010-05-23 11:00:55 -04008150 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04008151
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008152 dio_bio->bi_status = bio->bi_status;
Christoph Hellwig40553512017-06-03 09:37:58 +02008153 dio_end_io(dio_bio);
Chris Mason9be33952013-05-17 18:30:14 -04008154 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008155}
8156
David Sterbad0ee3932018-03-08 14:35:48 +01008157static blk_status_t btrfs_submit_bio_start_direct_io(void *private_data,
David Sterbad0779292018-03-08 13:47:33 +01008158 struct bio *bio, u64 offset)
Chris Masoneaf25d92010-05-25 09:48:28 -04008159{
Josef Bacikc6100a42017-05-05 11:57:13 -04008160 struct inode *inode = private_data;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008161 blk_status_t ret;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008162 ret = btrfs_csum_one_bio(inode, bio, offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008163 BUG_ON(ret); /* -ENOMEM */
Chris Masoneaf25d92010-05-25 09:48:28 -04008164 return 0;
8165}
8166
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008167static void btrfs_end_dio_bio(struct bio *bio)
Miao Xiee65e1532010-11-22 03:04:43 +00008168{
8169 struct btrfs_dio_private *dip = bio->bi_private;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008170 blk_status_t err = bio->bi_status;
Miao Xiee65e1532010-11-22 03:04:43 +00008171
Miao Xie8b110e32014-09-12 18:44:03 +08008172 if (err)
8173 btrfs_warn(BTRFS_I(dip->inode)->root->fs_info,
Mike Christie6296b962016-06-05 14:32:21 -05008174 "direct IO failed ino %llu rw %d,%u sector %#Lx len %u err no %d",
David Sterbaf85b7372017-01-20 14:54:07 +01008175 btrfs_ino(BTRFS_I(dip->inode)), bio_op(bio),
8176 bio->bi_opf,
Miao Xie8b110e32014-09-12 18:44:03 +08008177 (unsigned long long)bio->bi_iter.bi_sector,
8178 bio->bi_iter.bi_size, err);
8179
8180 if (dip->subio_endio)
8181 err = dip->subio_endio(dip->inode, btrfs_io_bio(bio), err);
Miao Xiec1dc0892014-09-12 18:43:56 +08008182
8183 if (err) {
Miao Xiee65e1532010-11-22 03:04:43 +00008184 /*
Nikolay Borisovde224b72018-02-14 10:53:36 +02008185 * We want to perceive the errors flag being set before
8186 * decrementing the reference count. We don't need a barrier
8187 * since atomic operations with a return value are fully
8188 * ordered as per atomic_t.txt
Miao Xiee65e1532010-11-22 03:04:43 +00008189 */
Nikolay Borisovde224b72018-02-14 10:53:36 +02008190 dip->errors = 1;
Miao Xiee65e1532010-11-22 03:04:43 +00008191 }
8192
8193 /* if there are more bios still pending for this dio, just exit */
8194 if (!atomic_dec_and_test(&dip->pending_bios))
8195 goto out;
8196
Chris Mason9be33952013-05-17 18:30:14 -04008197 if (dip->errors) {
Miao Xiee65e1532010-11-22 03:04:43 +00008198 bio_io_error(dip->orig_bio);
Chris Mason9be33952013-05-17 18:30:14 -04008199 } else {
Anand Jain2dbe0c72017-10-14 08:35:56 +08008200 dip->dio_bio->bi_status = BLK_STS_OK;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008201 bio_endio(dip->orig_bio);
Miao Xiee65e1532010-11-22 03:04:43 +00008202 }
8203out:
8204 bio_put(bio);
8205}
8206
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008207static inline blk_status_t btrfs_lookup_and_bind_dio_csum(struct inode *inode,
Miao Xiec1dc0892014-09-12 18:43:56 +08008208 struct btrfs_dio_private *dip,
8209 struct bio *bio,
8210 u64 file_offset)
8211{
8212 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
8213 struct btrfs_io_bio *orig_io_bio = btrfs_io_bio(dip->orig_bio);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008214 blk_status_t ret;
Miao Xiec1dc0892014-09-12 18:43:56 +08008215
8216 /*
8217 * We load all the csum data we need when we submit
8218 * the first bio to reduce the csum tree search and
8219 * contention.
8220 */
8221 if (dip->logical_offset == file_offset) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008222 ret = btrfs_lookup_bio_sums_dio(inode, dip->orig_bio,
Miao Xiec1dc0892014-09-12 18:43:56 +08008223 file_offset);
8224 if (ret)
8225 return ret;
8226 }
8227
8228 if (bio == dip->orig_bio)
8229 return 0;
8230
8231 file_offset -= dip->logical_offset;
8232 file_offset >>= inode->i_sb->s_blocksize_bits;
8233 io_bio->csum = (u8 *)(((u32 *)orig_io_bio->csum) + file_offset);
8234
8235 return 0;
8236}
8237
David Sterbad0ee3932018-03-08 14:35:48 +01008238static inline blk_status_t btrfs_submit_dio_bio(struct bio *bio,
8239 struct inode *inode, u64 file_offset, int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00008240{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008241 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xiefacc8a222013-07-25 19:22:34 +08008242 struct btrfs_dio_private *dip = bio->bi_private;
Mike Christie37226b22016-06-05 14:31:52 -05008243 bool write = bio_op(bio) == REQ_OP_WRITE;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008244 blk_status_t ret;
Miao Xiee65e1532010-11-22 03:04:43 +00008245
Liu Bo4c274bc2017-11-01 17:19:27 -06008246 /* Check btrfs_submit_bio_hook() for rules about async submit. */
Josef Bacikb812ce22012-11-16 13:56:32 -05008247 if (async_submit)
8248 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
8249
Josef Bacik5fd02042012-05-02 14:00:54 -04008250 if (!write) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008251 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DATA);
Josef Bacik5fd02042012-05-02 14:00:54 -04008252 if (ret)
8253 goto err;
8254 }
Miao Xiee65e1532010-11-22 03:04:43 +00008255
Nikolay Borisove6961ca2017-08-03 15:44:58 +03008256 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Josef Bacik1ae39932011-04-06 14:41:34 -04008257 goto map;
8258
8259 if (write && async_submit) {
Josef Bacikc6100a42017-05-05 11:57:13 -04008260 ret = btrfs_wq_submit_bio(fs_info, bio, 0, 0,
8261 file_offset, inode,
David Sterbae288c082018-07-18 17:36:24 +02008262 btrfs_submit_bio_start_direct_io);
Miao Xiee65e1532010-11-22 03:04:43 +00008263 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04008264 } else if (write) {
8265 /*
8266 * If we aren't doing async submit, calculate the csum of the
8267 * bio now.
8268 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008269 ret = btrfs_csum_one_bio(inode, bio, file_offset, 1);
Josef Bacik1ae39932011-04-06 14:41:34 -04008270 if (ret)
8271 goto err;
Miao Xie23ea8e52014-09-12 18:43:54 +08008272 } else {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008273 ret = btrfs_lookup_and_bind_dio_csum(inode, dip, bio,
Miao Xiec1dc0892014-09-12 18:43:56 +08008274 file_offset);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00008275 if (ret)
8276 goto err;
8277 }
Josef Bacik1ae39932011-04-06 14:41:34 -04008278map:
Liu Bo9b4a9b22017-08-18 11:54:02 -06008279 ret = btrfs_map_bio(fs_info, bio, 0, 0);
Miao Xiee65e1532010-11-22 03:04:43 +00008280err:
Miao Xiee65e1532010-11-22 03:04:43 +00008281 return ret;
8282}
8283
Nikolay Borisove6961ca2017-08-03 15:44:58 +03008284static int btrfs_submit_direct_hook(struct btrfs_dio_private *dip)
Miao Xiee65e1532010-11-22 03:04:43 +00008285{
8286 struct inode *inode = dip->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008287 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xiee65e1532010-11-22 03:04:43 +00008288 struct bio *bio;
8289 struct bio *orig_bio = dip->orig_bio;
Kent Overstreet4f024f32013-10-11 15:44:27 -07008290 u64 start_sector = orig_bio->bi_iter.bi_sector;
Miao Xiee65e1532010-11-22 03:04:43 +00008291 u64 file_offset = dip->logical_offset;
Miao Xiee65e1532010-11-22 03:04:43 +00008292 u64 map_length;
Josef Bacik1ae39932011-04-06 14:41:34 -04008293 int async_submit = 0;
Liu Bo725130b2017-05-16 09:51:39 -07008294 u64 submit_len;
8295 int clone_offset = 0;
8296 int clone_len;
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05308297 int ret;
Omar Sandoval58efbc92017-08-22 23:45:59 -07008298 blk_status_t status;
Miao Xiee65e1532010-11-22 03:04:43 +00008299
Kent Overstreet4f024f32013-10-11 15:44:27 -07008300 map_length = orig_bio->bi_iter.bi_size;
Liu Bo725130b2017-05-16 09:51:39 -07008301 submit_len = map_length;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008302 ret = btrfs_map_block(fs_info, btrfs_op(orig_bio), start_sector << 9,
8303 &map_length, NULL, 0);
Miao Xie7a5c3c92014-06-17 18:58:59 +08008304 if (ret)
Miao Xiee65e1532010-11-22 03:04:43 +00008305 return -EIO;
Miao Xiefacc8a222013-07-25 19:22:34 +08008306
Liu Bo725130b2017-05-16 09:51:39 -07008307 if (map_length >= submit_len) {
Josef Bacik02f57c72011-04-06 14:25:44 -04008308 bio = orig_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08008309 dip->flags |= BTRFS_DIO_ORIG_BIO_SUBMITTED;
Josef Bacik02f57c72011-04-06 14:25:44 -04008310 goto submit;
8311 }
8312
David Woodhouse53b381b2013-01-29 18:40:14 -05008313 /* async crcs make it difficult to collect full stripe writes. */
Jeff Mahoney1b868262017-05-17 11:38:35 -04008314 if (btrfs_data_alloc_profile(fs_info) & BTRFS_BLOCK_GROUP_RAID56_MASK)
David Woodhouse53b381b2013-01-29 18:40:14 -05008315 async_submit = 0;
8316 else
8317 async_submit = 1;
8318
Liu Bo725130b2017-05-16 09:51:39 -07008319 /* bio split */
8320 ASSERT(map_length <= INT_MAX);
Josef Bacik02f57c72011-04-06 14:25:44 -04008321 atomic_inc(&dip->pending_bios);
Arnd Bergmann3c91ee62017-05-18 15:33:29 +02008322 do {
Liu Bo725130b2017-05-16 09:51:39 -07008323 clone_len = min_t(int, submit_len, map_length);
Josef Bacik02f57c72011-04-06 14:25:44 -04008324
Liu Bo725130b2017-05-16 09:51:39 -07008325 /*
8326 * This will never fail as it's passing GPF_NOFS and
8327 * the allocation is backed by btrfs_bioset.
8328 */
Liu Boe4770942017-05-16 10:57:14 -07008329 bio = btrfs_bio_clone_partial(orig_bio, clone_offset,
Liu Bo725130b2017-05-16 09:51:39 -07008330 clone_len);
8331 bio->bi_private = dip;
8332 bio->bi_end_io = btrfs_end_dio_bio;
8333 btrfs_io_bio(bio)->logical = file_offset;
Miao Xiee65e1532010-11-22 03:04:43 +00008334
Liu Bo725130b2017-05-16 09:51:39 -07008335 ASSERT(submit_len >= clone_len);
8336 submit_len -= clone_len;
8337 if (submit_len == 0)
8338 break;
Miao Xiee65e1532010-11-22 03:04:43 +00008339
Liu Bo725130b2017-05-16 09:51:39 -07008340 /*
8341 * Increase the count before we submit the bio so we know
8342 * the end IO handler won't happen before we increase the
8343 * count. Otherwise, the dip might get freed before we're
8344 * done setting it up.
8345 */
8346 atomic_inc(&dip->pending_bios);
Miao Xiee65e1532010-11-22 03:04:43 +00008347
David Sterbad0ee3932018-03-08 14:35:48 +01008348 status = btrfs_submit_dio_bio(bio, inode, file_offset,
Omar Sandoval58efbc92017-08-22 23:45:59 -07008349 async_submit);
8350 if (status) {
Liu Bo725130b2017-05-16 09:51:39 -07008351 bio_put(bio);
8352 atomic_dec(&dip->pending_bios);
8353 goto out_err;
Miao Xiee65e1532010-11-22 03:04:43 +00008354 }
Liu Bo725130b2017-05-16 09:51:39 -07008355
8356 clone_offset += clone_len;
8357 start_sector += clone_len >> 9;
8358 file_offset += clone_len;
8359
8360 map_length = submit_len;
8361 ret = btrfs_map_block(fs_info, btrfs_op(orig_bio),
8362 start_sector << 9, &map_length, NULL, 0);
8363 if (ret)
8364 goto out_err;
Arnd Bergmann3c91ee62017-05-18 15:33:29 +02008365 } while (submit_len > 0);
Miao Xiee65e1532010-11-22 03:04:43 +00008366
Josef Bacik02f57c72011-04-06 14:25:44 -04008367submit:
David Sterbad0ee3932018-03-08 14:35:48 +01008368 status = btrfs_submit_dio_bio(bio, inode, file_offset, async_submit);
Omar Sandoval58efbc92017-08-22 23:45:59 -07008369 if (!status)
Miao Xiee65e1532010-11-22 03:04:43 +00008370 return 0;
8371
8372 bio_put(bio);
8373out_err:
8374 dip->errors = 1;
8375 /*
Nikolay Borisovde224b72018-02-14 10:53:36 +02008376 * Before atomic variable goto zero, we must make sure dip->errors is
8377 * perceived to be set. This ordering is ensured by the fact that an
8378 * atomic operations with a return value are fully ordered as per
8379 * atomic_t.txt
Miao Xiee65e1532010-11-22 03:04:43 +00008380 */
Miao Xiee65e1532010-11-22 03:04:43 +00008381 if (atomic_dec_and_test(&dip->pending_bios))
8382 bio_io_error(dip->orig_bio);
8383
8384 /* bio_end_io() will handle error, so we needn't return it */
8385 return 0;
8386}
8387
Mike Christie8a4c1e42016-06-05 14:31:50 -05008388static void btrfs_submit_direct(struct bio *dio_bio, struct inode *inode,
8389 loff_t file_offset)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008390{
Filipe Manana61de7182015-07-01 12:13:10 +01008391 struct btrfs_dio_private *dip = NULL;
Liu Bo3892ac92017-04-17 15:00:28 -07008392 struct bio *bio = NULL;
8393 struct btrfs_io_bio *io_bio;
Mike Christie8a4c1e42016-06-05 14:31:50 -05008394 bool write = (bio_op(dio_bio) == REQ_OP_WRITE);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008395 int ret = 0;
8396
David Sterba8b6c1d52017-06-02 17:48:13 +02008397 bio = btrfs_bio_clone(dio_bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008398
Miao Xiec1dc0892014-09-12 18:43:56 +08008399 dip = kzalloc(sizeof(*dip), GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04008400 if (!dip) {
8401 ret = -ENOMEM;
Filipe Manana61de7182015-07-01 12:13:10 +01008402 goto free_ordered;
Chris Mason9be33952013-05-17 18:30:14 -04008403 }
8404
8405 dip->private = dio_bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008406 dip->inode = inode;
8407 dip->logical_offset = file_offset;
Kent Overstreet4f024f32013-10-11 15:44:27 -07008408 dip->bytes = dio_bio->bi_iter.bi_size;
8409 dip->disk_bytenr = (u64)dio_bio->bi_iter.bi_sector << 9;
Liu Bo3892ac92017-04-17 15:00:28 -07008410 bio->bi_private = dip;
8411 dip->orig_bio = bio;
Chris Mason9be33952013-05-17 18:30:14 -04008412 dip->dio_bio = dio_bio;
Miao Xiee65e1532010-11-22 03:04:43 +00008413 atomic_set(&dip->pending_bios, 0);
Liu Bo3892ac92017-04-17 15:00:28 -07008414 io_bio = btrfs_io_bio(bio);
8415 io_bio->logical = file_offset;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008416
Miao Xiec1dc0892014-09-12 18:43:56 +08008417 if (write) {
Liu Bo3892ac92017-04-17 15:00:28 -07008418 bio->bi_end_io = btrfs_endio_direct_write;
Miao Xiec1dc0892014-09-12 18:43:56 +08008419 } else {
Liu Bo3892ac92017-04-17 15:00:28 -07008420 bio->bi_end_io = btrfs_endio_direct_read;
Miao Xiec1dc0892014-09-12 18:43:56 +08008421 dip->subio_endio = btrfs_subio_endio_read;
8422 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04008423
Filipe Mananaf28a4922015-12-08 19:23:20 +00008424 /*
8425 * Reset the range for unsubmitted ordered extents (to a 0 length range)
8426 * even if we fail to submit a bio, because in such case we do the
8427 * corresponding error handling below and it must not be done a second
8428 * time by btrfs_direct_IO().
8429 */
8430 if (write) {
8431 struct btrfs_dio_data *dio_data = current->journal_info;
8432
8433 dio_data->unsubmitted_oe_range_end = dip->logical_offset +
8434 dip->bytes;
8435 dio_data->unsubmitted_oe_range_start =
8436 dio_data->unsubmitted_oe_range_end;
8437 }
8438
Nikolay Borisove6961ca2017-08-03 15:44:58 +03008439 ret = btrfs_submit_direct_hook(dip);
Miao Xiee65e1532010-11-22 03:04:43 +00008440 if (!ret)
Chris Masoneaf25d92010-05-25 09:48:28 -04008441 return;
Chris Mason9be33952013-05-17 18:30:14 -04008442
Liu Bo3892ac92017-04-17 15:00:28 -07008443 if (io_bio->end_io)
8444 io_bio->end_io(io_bio, ret);
Chris Mason9be33952013-05-17 18:30:14 -04008445
Josef Bacik4b46fce2010-05-23 11:00:55 -04008446free_ordered:
8447 /*
Filipe Manana61de7182015-07-01 12:13:10 +01008448 * If we arrived here it means either we failed to submit the dip
8449 * or we either failed to clone the dio_bio or failed to allocate the
8450 * dip. If we cloned the dio_bio and allocated the dip, we can just
8451 * call bio_endio against our io_bio so that we get proper resource
8452 * cleanup if we fail to submit the dip, otherwise, we must do the
8453 * same as btrfs_endio_direct_[write|read] because we can't call these
8454 * callbacks - they require an allocated dip and a clone of dio_bio.
Josef Bacik4b46fce2010-05-23 11:00:55 -04008455 */
Liu Bo3892ac92017-04-17 15:00:28 -07008456 if (bio && dip) {
Guoqing Jiang054ec2f2017-06-02 16:08:50 +08008457 bio_io_error(bio);
Filipe Manana61de7182015-07-01 12:13:10 +01008458 /*
Liu Bo3892ac92017-04-17 15:00:28 -07008459 * The end io callbacks free our dip, do the final put on bio
Filipe Manana61de7182015-07-01 12:13:10 +01008460 * and all the cleanup and final put for dio_bio (through
8461 * dio_end_io()).
8462 */
8463 dip = NULL;
Liu Bo3892ac92017-04-17 15:00:28 -07008464 bio = NULL;
Filipe Manana61de7182015-07-01 12:13:10 +01008465 } else {
Filipe Manana14543772015-11-24 16:23:54 +00008466 if (write)
Qu Wenruo524272602017-03-08 10:25:52 +08008467 __endio_write_update_ordered(inode,
Filipe Manana14543772015-11-24 16:23:54 +00008468 file_offset,
8469 dio_bio->bi_iter.bi_size,
Qu Wenruo524272602017-03-08 10:25:52 +08008470 false);
Filipe Manana14543772015-11-24 16:23:54 +00008471 else
Filipe Manana61de7182015-07-01 12:13:10 +01008472 unlock_extent(&BTRFS_I(inode)->io_tree, file_offset,
8473 file_offset + dio_bio->bi_iter.bi_size - 1);
Filipe Manana14543772015-11-24 16:23:54 +00008474
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008475 dio_bio->bi_status = BLK_STS_IOERR;
Filipe Manana61de7182015-07-01 12:13:10 +01008476 /*
8477 * Releases and cleans up our dio_bio, no need to bio_put()
8478 * nor bio_endio()/bio_io_error() against dio_bio.
8479 */
Christoph Hellwig40553512017-06-03 09:37:58 +02008480 dio_end_io(dio_bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008481 }
Liu Bo3892ac92017-04-17 15:00:28 -07008482 if (bio)
8483 bio_put(bio);
Filipe Manana61de7182015-07-01 12:13:10 +01008484 kfree(dip);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008485}
8486
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008487static ssize_t check_direct_IO(struct btrfs_fs_info *fs_info,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008488 const struct iov_iter *iter, loff_t offset)
Chris Mason5a5f79b2010-05-26 21:33:37 -04008489{
8490 int seg;
Josef Bacika1b75f72011-04-08 15:51:18 +00008491 int i;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008492 unsigned int blocksize_mask = fs_info->sectorsize - 1;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008493 ssize_t retval = -EINVAL;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008494
8495 if (offset & blocksize_mask)
8496 goto out;
8497
Al Viro28060d52014-03-22 05:15:17 -04008498 if (iov_iter_alignment(iter) & blocksize_mask)
8499 goto out;
Josef Bacika1b75f72011-04-08 15:51:18 +00008500
Al Viro28060d52014-03-22 05:15:17 -04008501 /* If this is a write we don't need to check anymore */
Al Virocd27e452016-10-10 13:39:05 -04008502 if (iov_iter_rw(iter) != READ || !iter_is_iovec(iter))
Al Viro28060d52014-03-22 05:15:17 -04008503 return 0;
8504 /*
8505 * Check to make sure we don't have duplicate iov_base's in this
8506 * iovec, if so return EINVAL, otherwise we'll get csum errors
8507 * when reading back.
8508 */
8509 for (seg = 0; seg < iter->nr_segs; seg++) {
8510 for (i = seg + 1; i < iter->nr_segs; i++) {
8511 if (iter->iov[seg].iov_base == iter->iov[i].iov_base)
Josef Bacika1b75f72011-04-08 15:51:18 +00008512 goto out;
8513 }
Chris Mason5a5f79b2010-05-26 21:33:37 -04008514 }
8515 retval = 0;
8516out:
8517 return retval;
8518}
Josef Bacikeb838e72012-07-31 16:28:48 -04008519
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008520static ssize_t btrfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
Chris Mason16432982008-04-10 10:23:21 -04008521{
Josef Bacik4b46fce2010-05-23 11:00:55 -04008522 struct file *file = iocb->ki_filp;
8523 struct inode *inode = file->f_mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008524 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
chandan50745b02015-08-28 21:10:13 +05308525 struct btrfs_dio_data dio_data = { 0 };
Qu Wenruo364ecf32017-02-27 15:10:38 +08008526 struct extent_changeset *data_reserved = NULL;
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008527 loff_t offset = iocb->ki_pos;
Miao Xie09348562013-02-07 10:12:07 +00008528 size_t count = 0;
Miao Xie2e60a512013-02-08 07:01:08 +00008529 int flags = 0;
Miao Xie38851cc2013-02-08 07:04:11 +00008530 bool wakeup = true;
8531 bool relock = false;
Miao Xie09348562013-02-07 10:12:07 +00008532 ssize_t ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008533
Nikolay Borisov8c70c9f2017-08-21 12:43:46 +03008534 if (check_direct_IO(fs_info, iter, offset))
Chris Mason5a5f79b2010-05-26 21:33:37 -04008535 return 0;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008536
Jens Axboefe0f07d2015-04-15 17:05:48 -06008537 inode_dio_begin(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008538
Josef Bacik0e267c42013-07-02 10:38:02 -04008539 /*
Miao Xie41bd9ca2014-03-06 13:54:57 +08008540 * The generic stuff only does filemap_write_and_wait_range, which
8541 * isn't enough if we've written compressed pages to this area, so
8542 * we need to flush the dirty pages again to make absolutely sure
8543 * that any outstanding dirty pages are on disk.
Josef Bacik0e267c42013-07-02 10:38:02 -04008544 */
Al Viroa6cbcd42014-03-04 22:38:00 -05008545 count = iov_iter_count(iter);
Miao Xie41bd9ca2014-03-06 13:54:57 +08008546 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
8547 &BTRFS_I(inode)->runtime_flags))
Wang Shilong9a025a02014-07-17 11:44:13 +08008548 filemap_fdatawrite_range(inode->i_mapping, offset,
8549 offset + count - 1);
Josef Bacik0e267c42013-07-02 10:38:02 -04008550
Omar Sandoval6f673762015-03-16 04:33:52 -07008551 if (iov_iter_rw(iter) == WRITE) {
Miao Xie38851cc2013-02-08 07:04:11 +00008552 /*
8553 * If the write DIO is beyond the EOF, we need update
8554 * the isize, but it is protected by i_mutex. So we can
8555 * not unlock the i_mutex at this case.
8556 */
8557 if (offset + count <= inode->i_size) {
Liu Bo4aaedfb2016-12-14 22:36:05 -08008558 dio_data.overwrite = 1;
Al Viro59551022016-01-22 15:40:57 -05008559 inode_unlock(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008560 relock = true;
Goldwyn Rodriguesedf064e2017-06-20 07:05:49 -05008561 } else if (iocb->ki_flags & IOCB_NOWAIT) {
8562 ret = -EAGAIN;
8563 goto out;
Miao Xie38851cc2013-02-08 07:04:11 +00008564 }
Qu Wenruo364ecf32017-02-27 15:10:38 +08008565 ret = btrfs_delalloc_reserve_space(inode, &data_reserved,
8566 offset, count);
Miao Xie09348562013-02-07 10:12:07 +00008567 if (ret)
Miao Xie38851cc2013-02-08 07:04:11 +00008568 goto out;
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008569
8570 /*
8571 * We need to know how many extents we reserved so that we can
8572 * do the accounting properly if we go over the number we
8573 * originally calculated. Abuse current->journal_info for this.
8574 */
Jeff Mahoneyda170662016-06-15 09:22:56 -04008575 dio_data.reserve = round_up(count,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008576 fs_info->sectorsize);
Filipe Mananaf28a4922015-12-08 19:23:20 +00008577 dio_data.unsubmitted_oe_range_start = (u64)offset;
8578 dio_data.unsubmitted_oe_range_end = (u64)offset;
chandan50745b02015-08-28 21:10:13 +05308579 current->journal_info = &dio_data;
Chandan Rajendra97dcdea2016-12-23 15:00:18 +05308580 down_read(&BTRFS_I(inode)->dio_sem);
David Sterbaee39b432014-09-30 01:33:33 +02008581 } else if (test_bit(BTRFS_INODE_READDIO_NEED_LOCK,
8582 &BTRFS_I(inode)->runtime_flags)) {
Jens Axboefe0f07d2015-04-15 17:05:48 -06008583 inode_dio_end(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008584 flags = DIO_LOCKING | DIO_SKIP_HOLES;
8585 wakeup = false;
Miao Xie09348562013-02-07 10:12:07 +00008586 }
8587
Omar Sandoval17f8c842015-03-16 04:33:50 -07008588 ret = __blockdev_direct_IO(iocb, inode,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008589 fs_info->fs_devices->latest_bdev,
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008590 iter, btrfs_get_blocks_direct, NULL,
Omar Sandoval17f8c842015-03-16 04:33:50 -07008591 btrfs_submit_direct, flags);
Omar Sandoval6f673762015-03-16 04:33:52 -07008592 if (iov_iter_rw(iter) == WRITE) {
Chandan Rajendra97dcdea2016-12-23 15:00:18 +05308593 up_read(&BTRFS_I(inode)->dio_sem);
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008594 current->journal_info = NULL;
Liu Boddba1bf2015-06-17 16:59:58 +08008595 if (ret < 0 && ret != -EIOCBQUEUED) {
chandan50745b02015-08-28 21:10:13 +05308596 if (dio_data.reserve)
Qu Wenruobc42bda2017-02-27 15:10:39 +08008597 btrfs_delalloc_release_space(inode, data_reserved,
Qu Wenruo43b18592017-12-12 15:34:32 +08008598 offset, dio_data.reserve, true);
Filipe Mananaf28a4922015-12-08 19:23:20 +00008599 /*
8600 * On error we might have left some ordered extents
8601 * without submitting corresponding bios for them, so
8602 * cleanup them up to avoid other tasks getting them
8603 * and waiting for them to complete forever.
8604 */
8605 if (dio_data.unsubmitted_oe_range_start <
8606 dio_data.unsubmitted_oe_range_end)
Qu Wenruo524272602017-03-08 10:25:52 +08008607 __endio_write_update_ordered(inode,
Filipe Mananaf28a4922015-12-08 19:23:20 +00008608 dio_data.unsubmitted_oe_range_start,
8609 dio_data.unsubmitted_oe_range_end -
8610 dio_data.unsubmitted_oe_range_start,
Qu Wenruo524272602017-03-08 10:25:52 +08008611 false);
Liu Boddba1bf2015-06-17 16:59:58 +08008612 } else if (ret >= 0 && (size_t)ret < count)
Qu Wenruobc42bda2017-02-27 15:10:39 +08008613 btrfs_delalloc_release_space(inode, data_reserved,
Qu Wenruo43b18592017-12-12 15:34:32 +08008614 offset, count - (size_t)ret, true);
8615 btrfs_delalloc_release_extents(BTRFS_I(inode), count, false);
Miao Xie09348562013-02-07 10:12:07 +00008616 }
Miao Xie38851cc2013-02-08 07:04:11 +00008617out:
Miao Xie2e60a512013-02-08 07:01:08 +00008618 if (wakeup)
Jens Axboefe0f07d2015-04-15 17:05:48 -06008619 inode_dio_end(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008620 if (relock)
Al Viro59551022016-01-22 15:40:57 -05008621 inode_lock(inode);
Miao Xie09348562013-02-07 10:12:07 +00008622
Qu Wenruo364ecf32017-02-27 15:10:38 +08008623 extent_changeset_free(data_reserved);
Miao Xie09348562013-02-07 10:12:07 +00008624 return ret;
Chris Mason16432982008-04-10 10:23:21 -04008625}
8626
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008627#define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
8628
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008629static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
8630 __u64 start, __u64 len)
8631{
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008632 int ret;
8633
8634 ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS);
8635 if (ret)
8636 return ret;
8637
David Sterba2135fb92017-06-23 04:09:57 +02008638 return extent_fiemap(inode, fieinfo, start, len);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008639}
8640
Chris Mason9ebefb182007-06-15 13:50:00 -04008641int btrfs_readpage(struct file *file, struct page *page)
8642{
Chris Masond1310b22008-01-24 16:13:08 -05008643 struct extent_io_tree *tree;
8644 tree = &BTRFS_I(page->mapping->host)->io_tree;
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02008645 return extent_read_full_page(tree, page, btrfs_get_extent, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04008646}
Chris Mason1832a6d2007-12-21 16:27:21 -05008647
Chris Mason39279cc2007-06-12 06:35:45 -04008648static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
8649{
Josef Bacikbe7bd732015-10-22 15:05:09 -04008650 struct inode *inode = page->mapping->host;
8651 int ret;
Chris Masonb888db22007-08-27 16:49:44 -04008652
8653 if (current->flags & PF_MEMALLOC) {
8654 redirty_page_for_writepage(wbc, page);
8655 unlock_page(page);
8656 return 0;
8657 }
Josef Bacikbe7bd732015-10-22 15:05:09 -04008658
8659 /*
8660 * If we are under memory pressure we will call this directly from the
8661 * VM, we need to make sure we have the inode referenced for the ordered
8662 * extent. If not just return like we didn't do anything.
8663 */
8664 if (!igrab(inode)) {
8665 redirty_page_for_writepage(wbc, page);
8666 return AOP_WRITEPAGE_ACTIVATE;
8667 }
Nikolay Borisov0a9b0e52017-12-08 15:55:59 +02008668 ret = extent_write_full_page(page, wbc);
Josef Bacikbe7bd732015-10-22 15:05:09 -04008669 btrfs_add_delayed_iput(inode);
8670 return ret;
Chris Masona52d9a82007-08-27 16:49:44 -04008671}
Chris Mason39279cc2007-06-12 06:35:45 -04008672
Eric Sandeen48a3b632013-04-25 20:41:01 +00008673static int btrfs_writepages(struct address_space *mapping,
8674 struct writeback_control *wbc)
Chris Masonb293f02e2007-11-01 19:45:34 -04008675{
Nikolay Borisov8ae225a2018-04-19 10:46:38 +03008676 return extent_writepages(mapping, wbc);
Chris Masonb293f02e2007-11-01 19:45:34 -04008677}
8678
Chris Mason3ab2fb52007-11-08 10:59:22 -05008679static int
8680btrfs_readpages(struct file *file, struct address_space *mapping,
8681 struct list_head *pages, unsigned nr_pages)
8682{
Nikolay Borisov2a3ff0a2018-04-19 10:46:36 +03008683 return extent_readpages(mapping, pages, nr_pages);
Chris Mason3ab2fb52007-11-08 10:59:22 -05008684}
Nikolay Borisov2a3ff0a2018-04-19 10:46:36 +03008685
Chris Masone6dcd2d2008-07-17 12:53:50 -04008686static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04008687{
Nikolay Borisov477a30b2018-04-19 10:46:34 +03008688 int ret = try_release_extent_mapping(page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04008689 if (ret == 1) {
8690 ClearPagePrivate(page);
8691 set_page_private(page, 0);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008692 put_page(page);
Chris Masona52d9a82007-08-27 16:49:44 -04008693 }
8694 return ret;
8695}
Chris Mason39279cc2007-06-12 06:35:45 -04008696
Chris Masone6dcd2d2008-07-17 12:53:50 -04008697static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
8698{
Chris Mason98509cf2008-09-11 15:51:43 -04008699 if (PageWriteback(page) || PageDirty(page))
8700 return 0;
Michal Hocko3ba7ab22017-01-09 15:39:02 +01008701 return __btrfs_releasepage(page, gfp_flags);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008702}
8703
Lukas Czernerd47992f2013-05-21 23:17:23 -04008704static void btrfs_invalidatepage(struct page *page, unsigned int offset,
8705 unsigned int length)
Chris Masona52d9a82007-08-27 16:49:44 -04008706{
Josef Bacik5fd02042012-05-02 14:00:54 -04008707 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05008708 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008709 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008710 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008711 u64 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008712 u64 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308713 u64 start;
8714 u64 end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008715 int inode_evicting = inode->i_state & I_FREEING;
Chris Masona52d9a82007-08-27 16:49:44 -04008716
Chris Mason8b62b722009-09-02 16:53:46 -04008717 /*
8718 * we have the page locked, so new writeback can't start,
8719 * and the dirty bit won't be cleared while we are here.
8720 *
8721 * Wait for IO on this page so that we can safely clear
8722 * the PagePrivate2 bit and do ordered accounting
8723 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008724 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04008725
Josef Bacik5fd02042012-05-02 14:00:54 -04008726 tree = &BTRFS_I(inode)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008727 if (offset) {
8728 btrfs_releasepage(page, GFP_NOFS);
8729 return;
8730 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008731
8732 if (!inode_evicting)
David Sterbaff13db42015-12-03 14:30:40 +01008733 lock_extent_bits(tree, page_start, page_end, &cached_state);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308734again:
8735 start = page_start;
Nikolay Borisova776c6f2017-02-20 13:50:49 +02008736 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), start,
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308737 page_end - start + 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008738 if (ordered) {
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308739 end = min(page_end, ordered->file_offset + ordered->len - 1);
Chris Masoneb84ae02008-07-17 13:53:27 -04008740 /*
8741 * IO on this page will never be started, so we need
8742 * to account for any ordered extents now
8743 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008744 if (!inode_evicting)
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308745 clear_extent_bit(tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008746 EXTENT_DIRTY | EXTENT_DELALLOC |
Filipe Mananaa7e3b972017-04-03 10:45:46 +01008747 EXTENT_DELALLOC_NEW |
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008748 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
David Sterbaae0f1622017-10-31 16:37:52 +01008749 EXTENT_DEFRAG, 1, 0, &cached_state);
Chris Mason8b62b722009-09-02 16:53:46 -04008750 /*
8751 * whoever cleared the private bit is responsible
8752 * for the finish_ordered_io
8753 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04008754 if (TestClearPagePrivate2(page)) {
8755 struct btrfs_ordered_inode_tree *tree;
8756 u64 new_len;
8757
8758 tree = &BTRFS_I(inode)->ordered_tree;
8759
8760 spin_lock_irq(&tree->lock);
8761 set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308762 new_len = start - ordered->file_offset;
Josef Bacik77cef2e2013-08-29 13:57:21 -04008763 if (new_len < ordered->truncated_len)
8764 ordered->truncated_len = new_len;
8765 spin_unlock_irq(&tree->lock);
8766
8767 if (btrfs_dec_test_ordered_pending(inode, &ordered,
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308768 start,
8769 end - start + 1, 1))
Josef Bacik77cef2e2013-08-29 13:57:21 -04008770 btrfs_finish_ordered_io(ordered);
Chris Mason8b62b722009-09-02 16:53:46 -04008771 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008772 btrfs_put_ordered_extent(ordered);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008773 if (!inode_evicting) {
8774 cached_state = NULL;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308775 lock_extent_bits(tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008776 &cached_state);
8777 }
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308778
8779 start = end + 1;
8780 if (start < page_end)
8781 goto again;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008782 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008783
Qu Wenruob9d0b382015-09-29 10:35:16 +08008784 /*
8785 * Qgroup reserved space handler
8786 * Page here will be either
8787 * 1) Already written to disk
8788 * In this case, its reserved space is released from data rsv map
8789 * and will be freed by delayed_ref handler finally.
8790 * So even we call qgroup_free_data(), it won't decrease reserved
8791 * space.
8792 * 2) Not written to disk
Goldwyn Rodrigues0b34c262016-09-30 10:40:52 -05008793 * This means the reserved space should be freed here. However,
8794 * if a truncate invalidates the page (by clearing PageDirty)
8795 * and the page is accounted for while allocating extent
8796 * in btrfs_check_data_free_space() we let delayed_ref to
8797 * free the entire extent.
Qu Wenruob9d0b382015-09-29 10:35:16 +08008798 */
Goldwyn Rodrigues0b34c262016-09-30 10:40:52 -05008799 if (PageDirty(page))
Qu Wenruobc42bda2017-02-27 15:10:39 +08008800 btrfs_qgroup_free_data(inode, NULL, page_start, PAGE_SIZE);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008801 if (!inode_evicting) {
8802 clear_extent_bit(tree, page_start, page_end,
8803 EXTENT_LOCKED | EXTENT_DIRTY |
Filipe Mananaa7e3b972017-04-03 10:45:46 +01008804 EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
8805 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1,
David Sterbaae0f1622017-10-31 16:37:52 +01008806 &cached_state);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008807
8808 __btrfs_releasepage(page, GFP_NOFS);
8809 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008810
Chris Mason4a096752008-07-21 10:29:44 -04008811 ClearPageChecked(page);
Chris Mason9ad6b7bc2008-04-18 16:11:30 -04008812 if (PagePrivate(page)) {
Chris Mason9ad6b7bc2008-04-18 16:11:30 -04008813 ClearPagePrivate(page);
8814 set_page_private(page, 0);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008815 put_page(page);
Chris Mason9ad6b7bc2008-04-18 16:11:30 -04008816 }
Chris Mason39279cc2007-06-12 06:35:45 -04008817}
8818
Chris Mason9ebefb182007-06-15 13:50:00 -04008819/*
8820 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
8821 * called from a page fault handler when a page is first dirtied. Hence we must
8822 * be careful to check for EOF conditions here. We set the page up correctly
8823 * for a written page which means we get ENOSPC checking when writing into
8824 * holes and correct delalloc and unwritten extent mapping on filesystems that
8825 * support these features.
8826 *
8827 * We are not allowed to take the i_mutex here so we have to play games to
8828 * protect against truncate races as the page could now be beyond EOF. Because
Omar Sandovald1342aa2018-05-11 13:13:29 -07008829 * truncate_setsize() writes the inode size before removing pages, once we have
8830 * the page lock we can determine safely if the page is beyond EOF. If it is not
Chris Mason9ebefb182007-06-15 13:50:00 -04008831 * beyond EOF, then the page is guaranteed safe against truncation until we
8832 * unlock the page.
8833 */
Souptick Joardera528a242018-06-06 19:54:44 +05308834vm_fault_t btrfs_page_mkwrite(struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04008835{
Nick Pigginc2ec1752009-03-31 15:23:21 -07008836 struct page *page = vmf->page;
Dave Jiang11bac802017-02-24 14:56:41 -08008837 struct inode *inode = file_inode(vmf->vma->vm_file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008838 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008839 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
8840 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008841 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08008842 struct extent_changeset *data_reserved = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008843 char *kaddr;
8844 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04008845 loff_t size;
Souptick Joardera528a242018-06-06 19:54:44 +05308846 vm_fault_t ret;
8847 int ret2;
Chris Mason9998eb72012-01-25 13:47:40 -05008848 int reserved = 0;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308849 u64 reserved_space;
Chris Masona52d9a82007-08-27 16:49:44 -04008850 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008851 u64 page_end;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308852 u64 end;
8853
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008854 reserved_space = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04008855
Jan Karab2b5ef52012-06-12 16:20:45 +02008856 sb_start_pagefault(inode->i_sb);
Qu Wenruodf480632015-09-08 17:25:54 +08008857 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008858 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308859 end = page_end;
Qu Wenruodf480632015-09-08 17:25:54 +08008860
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308861 /*
8862 * Reserving delalloc space after obtaining the page lock can lead to
8863 * deadlock. For example, if a dirty page is locked by this function
8864 * and the call to btrfs_delalloc_reserve_space() ends up triggering
8865 * dirty page write out, then the btrfs_writepage() function could
8866 * end up waiting indefinitely to get a lock on the page currently
8867 * being processed by btrfs_page_mkwrite() function.
8868 */
Souptick Joardera528a242018-06-06 19:54:44 +05308869 ret2 = btrfs_delalloc_reserve_space(inode, &data_reserved, page_start,
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308870 reserved_space);
Souptick Joardera528a242018-06-06 19:54:44 +05308871 if (!ret2) {
8872 ret2 = file_update_time(vmf->vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05008873 reserved = 1;
8874 }
Souptick Joardera528a242018-06-06 19:54:44 +05308875 if (ret2) {
8876 ret = vmf_error(ret2);
Chris Mason9998eb72012-01-25 13:47:40 -05008877 if (reserved)
8878 goto out;
8879 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07008880 }
Chris Mason1832a6d2007-12-21 16:27:21 -05008881
Nick Piggin56a76f82009-03-31 15:23:23 -07008882 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008883again:
Chris Mason9ebefb182007-06-15 13:50:00 -04008884 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04008885 size = i_size_read(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04008886
Chris Mason9ebefb182007-06-15 13:50:00 -04008887 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04008888 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04008889 /* page got truncated out from underneath us */
8890 goto out_unlock;
8891 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008892 wait_on_page_writeback(page);
8893
David Sterbaff13db42015-12-03 14:30:40 +01008894 lock_extent_bits(io_tree, page_start, page_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008895 set_page_extent_mapped(page);
8896
Chris Masoneb84ae02008-07-17 13:53:27 -04008897 /*
8898 * we can't set the delalloc bits if there are pending ordered
8899 * extents. Drop our locks and wait for them to finish
8900 */
Nikolay Borisova776c6f2017-02-20 13:50:49 +02008901 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start,
8902 PAGE_SIZE);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008903 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00008904 unlock_extent_cached(io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01008905 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008906 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04008907 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008908 btrfs_put_ordered_extent(ordered);
8909 goto again;
8910 }
8911
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008912 if (page->index == ((size - 1) >> PAGE_SHIFT)) {
Jeff Mahoneyda170662016-06-15 09:22:56 -04008913 reserved_space = round_up(size - page_start,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008914 fs_info->sectorsize);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008915 if (reserved_space < PAGE_SIZE) {
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308916 end = page_start + reserved_space - 1;
Qu Wenruobc42bda2017-02-27 15:10:39 +08008917 btrfs_delalloc_release_space(inode, data_reserved,
Qu Wenruo43b18592017-12-12 15:34:32 +08008918 page_start, PAGE_SIZE - reserved_space,
8919 true);
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308920 }
8921 }
8922
Josef Bacikfbf19082009-10-01 17:10:23 -04008923 /*
Liu Bo54160342016-12-13 12:15:19 -08008924 * page_mkwrite gets called when the page is firstly dirtied after it's
8925 * faulted in, but write(2) could also dirty a page and set delalloc
8926 * bits, thus in this case for space account reason, we still need to
8927 * clear any delalloc bits within this page range since we have to
8928 * reserve data&meta space before lock_page() (see above comments).
Josef Bacikfbf19082009-10-01 17:10:23 -04008929 */
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308930 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06008931 EXTENT_DIRTY | EXTENT_DELALLOC |
8932 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
David Sterbaae0f1622017-10-31 16:37:52 +01008933 0, 0, &cached_state);
Josef Bacikfbf19082009-10-01 17:10:23 -04008934
Souptick Joardera528a242018-06-06 19:54:44 +05308935 ret2 = btrfs_set_extent_delalloc(inode, page_start, end, 0,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08008936 &cached_state, 0);
Souptick Joardera528a242018-06-06 19:54:44 +05308937 if (ret2) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00008938 unlock_extent_cached(io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01008939 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04008940 ret = VM_FAULT_SIGBUS;
8941 goto out_unlock;
8942 }
Souptick Joardera528a242018-06-06 19:54:44 +05308943 ret2 = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04008944
8945 /* page is wholly or partially inside EOF */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008946 if (page_start + PAGE_SIZE > size)
8947 zero_start = size & ~PAGE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04008948 else
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008949 zero_start = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04008950
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008951 if (zero_start != PAGE_SIZE) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04008952 kaddr = kmap(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008953 memset(kaddr + zero_start, 0, PAGE_SIZE - zero_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008954 flush_dcache_page(page);
8955 kunmap(page);
8956 }
Chris Mason247e7432008-07-17 12:53:51 -04008957 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008958 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04008959 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04008960
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008961 BTRFS_I(inode)->last_trans = fs_info->generation;
Chris Mason257c62e2009-10-13 13:21:08 -04008962 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06008963 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
Chris Mason257c62e2009-10-13 13:21:08 -04008964
David Sterbae43bbe52017-12-12 21:43:52 +01008965 unlock_extent_cached(io_tree, page_start, page_end, &cached_state);
Chris Mason9ebefb182007-06-15 13:50:00 -04008966
Souptick Joardera528a242018-06-06 19:54:44 +05308967 if (!ret2) {
Qu Wenruo43b18592017-12-12 15:34:32 +08008968 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE, true);
Jan Karab2b5ef52012-06-12 16:20:45 +02008969 sb_end_pagefault(inode->i_sb);
Qu Wenruo364ecf32017-02-27 15:10:38 +08008970 extent_changeset_free(data_reserved);
Chris Mason50a9b212009-09-11 12:33:12 -04008971 return VM_FAULT_LOCKED;
Jan Karab2b5ef52012-06-12 16:20:45 +02008972 }
Chris Mason717beb92018-06-25 10:03:41 -07008973
8974out_unlock:
Chris Mason9ebefb182007-06-15 13:50:00 -04008975 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05008976out:
Qu Wenruo43b18592017-12-12 15:34:32 +08008977 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE, (ret != 0));
Qu Wenruobc42bda2017-02-27 15:10:39 +08008978 btrfs_delalloc_release_space(inode, data_reserved, page_start,
Qu Wenruo43b18592017-12-12 15:34:32 +08008979 reserved_space, (ret != 0));
Chris Mason9998eb72012-01-25 13:47:40 -05008980out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02008981 sb_end_pagefault(inode->i_sb);
Qu Wenruo364ecf32017-02-27 15:10:38 +08008982 extent_changeset_free(data_reserved);
Chris Mason9ebefb182007-06-15 13:50:00 -04008983 return ret;
8984}
8985
Filipe Manana213e8c52018-02-06 20:40:31 +00008986static int btrfs_truncate(struct inode *inode, bool skip_writeback)
Chris Mason39279cc2007-06-12 06:35:45 -04008987{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008988 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04008989 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04008990 struct btrfs_block_rsv *rsv;
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008991 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04008992 struct btrfs_trans_handle *trans;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008993 u64 mask = fs_info->sectorsize - 1;
8994 u64 min_size = btrfs_calc_trunc_metadata_size(fs_info, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04008995
Filipe Manana213e8c52018-02-06 20:40:31 +00008996 if (!skip_writeback) {
8997 ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask),
8998 (u64)-1);
8999 if (ret)
9000 return ret;
9001 }
Chris Mason39279cc2007-06-12 06:35:45 -04009002
Josef Bacikfcb80c22011-05-03 10:40:22 -04009003 /*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07009004 * Yes ladies and gentlemen, this is indeed ugly. We have a couple of
9005 * things going on here:
Josef Bacikfcb80c22011-05-03 10:40:22 -04009006 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07009007 * 1) We need to reserve space to update our inode.
Josef Bacikfcb80c22011-05-03 10:40:22 -04009008 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07009009 * 2) We need to have something to cache all the space that is going to
Josef Bacikfcb80c22011-05-03 10:40:22 -04009010 * be free'd up by the truncate operation, but also have some slack
9011 * space reserved in case it uses space during the truncate (thank you
9012 * very much snapshotting).
9013 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07009014 * And we need these to be separate. The fact is we can use a lot of
Josef Bacikfcb80c22011-05-03 10:40:22 -04009015 * space doing the truncate, and we have no earthly idea how much space
Nicholas D Steeves01327612016-05-19 21:18:45 -04009016 * we will use, so we need the truncate reservation to be separate so it
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07009017 * doesn't end up using space reserved for updating the inode. We also
9018 * need to be able to stop the transaction and start a new one, which
9019 * means we need to be able to update the inode several times, and we
9020 * have no idea of knowing how many times that will be, so we can't just
9021 * reserve 1 item for the entirety of the operation, so that has to be
9022 * done separately as well.
Josef Bacikfcb80c22011-05-03 10:40:22 -04009023 *
9024 * So that leaves us with
9025 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07009026 * 1) rsv - for the truncate reservation, which we will steal from the
Josef Bacikfcb80c22011-05-03 10:40:22 -04009027 * transaction reservation.
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07009028 * 2) fs_info->trans_block_rsv - this will have 1 items worth left for
Josef Bacikfcb80c22011-05-03 10:40:22 -04009029 * updating the inode.
9030 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009031 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009032 if (!rsv)
9033 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04009034 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04009035 rsv->failfast = 1;
Josef Bacikf0cd8462011-03-04 14:37:08 -05009036
Josef Bacik907cbce2011-08-08 13:46:15 -04009037 /*
Josef Bacik07127182011-08-19 10:29:59 -04009038 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04009039 * 1 for updating the inode.
9040 */
Josef Bacikf3fe8202013-01-07 17:03:21 -05009041 trans = btrfs_start_transaction(root, 2);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009042 if (IS_ERR(trans)) {
Omar Sandovalad7e1a72018-05-22 09:59:50 -07009043 ret = PTR_ERR(trans);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009044 goto out;
9045 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05009046
Josef Bacik907cbce2011-08-08 13:46:15 -04009047 /* Migrate the slack space for the truncate to our reserve */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009048 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, rsv,
Josef Bacik25d609f2016-03-25 13:25:48 -04009049 min_size, 0);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009050 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05009051
Chris Mason5a3f23d2009-03-31 13:27:11 -04009052 /*
Josef Bacik5dc562c2012-08-17 13:14:17 -04009053 * So if we truncate and then write and fsync we normally would just
9054 * write the extents that changed, which is a problem if we need to
9055 * first truncate that entire inode. So set this flag so we write out
9056 * all of the extents in the inode to the sync log so we're completely
9057 * safe.
9058 */
9059 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009060 trans->block_rsv = rsv;
Josef Bacik5dc562c2012-08-17 13:14:17 -04009061
Yan, Zheng80825102009-11-12 09:35:36 +00009062 while (1) {
9063 ret = btrfs_truncate_inode_items(trans, root, inode,
9064 inode->i_size,
9065 BTRFS_EXTENT_DATA_KEY);
Josef Bacikddfae632017-10-19 14:16:02 -04009066 trans->block_rsv = &fs_info->trans_block_rsv;
Omar Sandovalad7e1a72018-05-22 09:59:50 -07009067 if (ret != -ENOSPC && ret != -EAGAIN)
Yan, Zheng80825102009-11-12 09:35:36 +00009068 break;
Chris Mason39279cc2007-06-12 06:35:45 -04009069
Yan, Zheng80825102009-11-12 09:35:36 +00009070 ret = btrfs_update_inode(trans, root, inode);
Omar Sandovalad7e1a72018-05-22 09:59:50 -07009071 if (ret)
Josef Bacik3893e332011-01-31 16:03:11 -05009072 break;
Josef Bacikca7e70f2012-08-27 17:48:15 -04009073
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009074 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009075 btrfs_btree_balance_dirty(fs_info);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009076
9077 trans = btrfs_start_transaction(root, 2);
9078 if (IS_ERR(trans)) {
Omar Sandovalad7e1a72018-05-22 09:59:50 -07009079 ret = PTR_ERR(trans);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009080 trans = NULL;
9081 break;
9082 }
9083
Wang Xiaoguang47b5d642016-09-07 20:17:38 +08009084 btrfs_block_rsv_release(fs_info, rsv, -1);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009085 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv,
Josef Bacik25d609f2016-03-25 13:25:48 -04009086 rsv, min_size, 0);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009087 BUG_ON(ret); /* shouldn't happen */
9088 trans->block_rsv = rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00009089 }
9090
Josef Bacikddfae632017-10-19 14:16:02 -04009091 /*
9092 * We can't call btrfs_truncate_block inside a trans handle as we could
9093 * deadlock with freeze, if we got NEED_TRUNCATE_BLOCK then we know
9094 * we've truncated everything except the last little bit, and can do
9095 * btrfs_truncate_block and then update the disk_i_size.
9096 */
9097 if (ret == NEED_TRUNCATE_BLOCK) {
9098 btrfs_end_transaction(trans);
9099 btrfs_btree_balance_dirty(fs_info);
9100
9101 ret = btrfs_truncate_block(inode, inode->i_size, 0, 0);
9102 if (ret)
9103 goto out;
9104 trans = btrfs_start_transaction(root, 1);
9105 if (IS_ERR(trans)) {
9106 ret = PTR_ERR(trans);
9107 goto out;
9108 }
9109 btrfs_ordered_update_i_size(inode, inode->i_size, NULL);
9110 }
9111
Chris Mason917c16b2011-11-08 14:49:59 -05009112 if (trans) {
Omar Sandovalad7e1a72018-05-22 09:59:50 -07009113 int ret2;
Josef Bacik7b128762008-07-24 12:17:14 -04009114
Omar Sandovalad7e1a72018-05-22 09:59:50 -07009115 trans->block_rsv = &fs_info->trans_block_rsv;
9116 ret2 = btrfs_update_inode(trans, root, inode);
9117 if (ret2 && !ret)
9118 ret = ret2;
9119
9120 ret2 = btrfs_end_transaction(trans);
9121 if (ret2 && !ret)
9122 ret = ret2;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009123 btrfs_btree_balance_dirty(fs_info);
Chris Mason917c16b2011-11-08 14:49:59 -05009124 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04009125out:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009126 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009127
Omar Sandovalad7e1a72018-05-22 09:59:50 -07009128 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04009129}
9130
Sven Wegener3b963622008-06-09 21:57:42 -04009131/*
Chris Masond352ac62008-09-29 15:18:18 -04009132 * create a new subvolume directory/inode (helper for the ioctl).
9133 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05009134int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009135 struct btrfs_root *new_root,
9136 struct btrfs_root *parent_root,
9137 u64 new_dirid)
Chris Mason39279cc2007-06-12 06:35:45 -04009138{
Chris Mason39279cc2007-06-12 06:35:45 -04009139 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04009140 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04009141 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009142
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01009143 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
9144 new_dirid, new_dirid,
9145 S_IFDIR | (~current_umask() & S_IRWXUGO),
9146 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04009147 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04009148 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009149 inode->i_op = &btrfs_dir_inode_operations;
9150 inode->i_fop = &btrfs_dir_file_operations;
9151
Miklos Szeredibfe86842011-10-28 14:13:29 +02009152 set_nlink(inode, 1);
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02009153 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Masonb0d5d102014-09-08 13:08:51 -07009154 unlock_new_inode(inode);
Sven Wegener3b963622008-06-09 21:57:42 -04009155
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009156 err = btrfs_subvol_inherit_props(trans, new_root, parent_root);
9157 if (err)
9158 btrfs_err(new_root->fs_info,
David Sterba351fd352014-05-15 16:48:20 +02009159 "error inheriting subvolume %llu properties: %d",
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009160 new_root->root_key.objectid, err);
9161
Yan, Zheng76dda932009-09-21 16:00:26 -04009162 err = btrfs_update_inode(trans, new_root, inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04009163
Yan, Zheng76dda932009-09-21 16:00:26 -04009164 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07009165 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04009166}
9167
Chris Mason39279cc2007-06-12 06:35:45 -04009168struct inode *btrfs_alloc_inode(struct super_block *sb)
9169{
Josef Bacik69fe2d72017-10-19 14:15:57 -04009170 struct btrfs_fs_info *fs_info = btrfs_sb(sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009171 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009172 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009173
David Sterba712e36c2017-10-31 17:08:27 +01009174 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_KERNEL);
Chris Mason39279cc2007-06-12 06:35:45 -04009175 if (!ei)
9176 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009177
9178 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009179 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04009180 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04009181 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04009182 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009183 ei->delalloc_bytes = 0;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01009184 ei->new_delalloc_bytes = 0;
Wang Shilong47059d92014-07-03 18:22:07 +08009185 ei->defrag_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009186 ei->disk_i_size = 0;
9187 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04009188 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009189 ei->index_cnt = (u64)-1;
Miao Xie67de1172013-12-26 13:07:06 +08009190 ei->dir_index = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009191 ei->last_unlink_trans = 0;
Liu Bo46d8bc32012-08-29 01:07:55 -06009192 ei->last_log_commit = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009193
Josef Bacik9e0baf62011-07-15 15:16:44 +00009194 spin_lock_init(&ei->lock);
9195 ei->outstanding_extents = 0;
Josef Bacik69fe2d72017-10-19 14:15:57 -04009196 if (sb->s_magic != BTRFS_TEST_MAGIC)
9197 btrfs_init_metadata_block_rsv(fs_info, &ei->block_rsv,
9198 BTRFS_BLOCK_RSV_DELALLOC);
Josef Bacik72ac3c02012-05-23 14:13:11 -04009199 ei->runtime_flags = 0;
David Sterbab52aa8c2017-07-17 19:17:20 +02009200 ei->prop_compress = BTRFS_COMPRESS_NONE;
David Sterbaeec63c62017-07-17 19:41:31 +02009201 ei->defrag_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009202
Miao Xie16cdcec2011-04-22 18:12:22 +08009203 ei->delayed_node = NULL;
9204
chandan r9cc97d62012-07-04 12:48:07 +05309205 ei->i_otime.tv_sec = 0;
9206 ei->i_otime.tv_nsec = 0;
9207
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009208 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02009209 extent_map_tree_init(&ei->extent_tree);
Josef Bacikc6100a42017-05-05 11:57:13 -04009210 extent_io_tree_init(&ei->io_tree, inode);
9211 extent_io_tree_init(&ei->io_failure_tree, inode);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04009212 ei->io_tree.track_uptodate = 1;
9213 ei->io_failure_tree.track_uptodate = 1;
Josef Bacikb812ce22012-11-16 13:56:32 -05009214 atomic_set(&ei->sync_writers, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009215 mutex_init(&ei->log_mutex);
Josef Bacikf2486792012-01-13 12:09:22 -05009216 mutex_init(&ei->delalloc_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009217 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009218 INIT_LIST_HEAD(&ei->delalloc_inodes);
David Sterba8089fe62015-11-19 14:15:51 +01009219 INIT_LIST_HEAD(&ei->delayed_iput);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009220 RB_CLEAR_NODE(&ei->rb_node);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01009221 init_rwsem(&ei->dio_sem);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009222
9223 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009224}
9225
Josef Bacikaaedb552013-10-11 14:44:09 -04009226#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
9227void btrfs_test_destroy_inode(struct inode *inode)
9228{
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02009229 btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0);
Josef Bacikaaedb552013-10-11 14:44:09 -04009230 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
9231}
9232#endif
9233
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009234static void btrfs_i_callback(struct rcu_head *head)
9235{
9236 struct inode *inode = container_of(head, struct inode, i_rcu);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009237 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
9238}
9239
Chris Mason39279cc2007-06-12 06:35:45 -04009240void btrfs_destroy_inode(struct inode *inode)
9241{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009242 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009243 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009244 struct btrfs_root *root = BTRFS_I(inode)->root;
9245
Al Virob3d9b7a2012-06-09 13:51:19 -04009246 WARN_ON(!hlist_empty(&inode->i_dentry));
Chris Mason39279cc2007-06-12 06:35:45 -04009247 WARN_ON(inode->i_data.nrpages);
Josef Bacik69fe2d72017-10-19 14:15:57 -04009248 WARN_ON(BTRFS_I(inode)->block_rsv.reserved);
9249 WARN_ON(BTRFS_I(inode)->block_rsv.size);
Josef Bacik9e0baf62011-07-15 15:16:44 +00009250 WARN_ON(BTRFS_I(inode)->outstanding_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04009251 WARN_ON(BTRFS_I(inode)->delalloc_bytes);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01009252 WARN_ON(BTRFS_I(inode)->new_delalloc_bytes);
Josef Bacik7709cde2011-08-04 10:25:02 -04009253 WARN_ON(BTRFS_I(inode)->csum_bytes);
Wang Shilong47059d92014-07-03 18:22:07 +08009254 WARN_ON(BTRFS_I(inode)->defrag_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04009255
Chris Mason5a3f23d2009-03-31 13:27:11 -04009256 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05009257 * This can happen where we create an inode, but somebody else also
9258 * created the same inode and we need to destroy the one we already
9259 * created.
9260 */
9261 if (!root)
9262 goto free;
9263
Chris Masond3977122009-01-05 21:25:51 -05009264 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04009265 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
9266 if (!ordered)
9267 break;
9268 else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009269 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04009270 "found ordered extent %llu %llu on inode cleanup",
9271 ordered->file_offset, ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009272 btrfs_remove_ordered_extent(inode, ordered);
9273 btrfs_put_ordered_extent(ordered);
9274 btrfs_put_ordered_extent(ordered);
9275 }
9276 }
Qu Wenruo56fa9d02015-10-13 09:53:10 +08009277 btrfs_qgroup_check_reserved_leak(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04009278 inode_tree_del(inode);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02009279 btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0);
Josef Bacika6dbd422009-11-11 15:53:34 -05009280free:
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009281 call_rcu(&inode->i_rcu, btrfs_i_callback);
Chris Mason39279cc2007-06-12 06:35:45 -04009282}
9283
Al Viro45321ac2010-06-07 13:43:19 -04009284int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04009285{
9286 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04009287
Naohiro Aota6379ef92013-06-06 09:56:34 +00009288 if (root == NULL)
9289 return 1;
9290
Liu Bofa6ac872013-02-20 14:10:23 +00009291 /* the snap/subvol tree is on deleting */
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02009292 if (btrfs_root_refs(&root->root_item) == 0)
Al Viro45321ac2010-06-07 13:43:19 -04009293 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04009294 else
Al Viro45321ac2010-06-07 13:43:19 -04009295 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04009296}
9297
Sven Wegener0ee0fda2008-07-30 16:54:26 -04009298static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04009299{
9300 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
9301
9302 inode_init_once(&ei->vfs_inode);
9303}
9304
David Sterbae67c7182018-02-19 17:24:18 +01009305void __cold btrfs_destroy_cachep(void)
Chris Mason39279cc2007-06-12 06:35:45 -04009306{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10009307 /*
9308 * Make sure all delayed rcu free inodes are flushed before we
9309 * destroy cache.
9310 */
9311 rcu_barrier();
Kinglong Mee5598e902016-01-29 21:36:35 +08009312 kmem_cache_destroy(btrfs_inode_cachep);
9313 kmem_cache_destroy(btrfs_trans_handle_cachep);
Kinglong Mee5598e902016-01-29 21:36:35 +08009314 kmem_cache_destroy(btrfs_path_cachep);
9315 kmem_cache_destroy(btrfs_free_space_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04009316}
9317
Liu Bof5c29bd2017-11-02 17:21:50 -06009318int __init btrfs_init_cachep(void)
Chris Mason39279cc2007-06-12 06:35:45 -04009319{
David Sterba837e1972012-09-07 03:00:48 -06009320 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009321 sizeof(struct btrfs_inode), 0,
Vladimir Davydov5d097052016-01-14 15:18:21 -08009322 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD | SLAB_ACCOUNT,
9323 init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04009324 if (!btrfs_inode_cachep)
9325 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009326
David Sterba837e1972012-09-07 03:00:48 -06009327 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009328 sizeof(struct btrfs_trans_handle), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009329 SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009330 if (!btrfs_trans_handle_cachep)
9331 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009332
David Sterba837e1972012-09-07 03:00:48 -06009333 btrfs_path_cachep = kmem_cache_create("btrfs_path",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009334 sizeof(struct btrfs_path), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009335 SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009336 if (!btrfs_path_cachep)
9337 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009338
David Sterba837e1972012-09-07 03:00:48 -06009339 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
Josef Bacikdc89e982011-01-28 17:05:48 -05009340 sizeof(struct btrfs_free_space), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009341 SLAB_MEM_SPREAD, NULL);
Josef Bacikdc89e982011-01-28 17:05:48 -05009342 if (!btrfs_free_space_cachep)
9343 goto fail;
9344
Chris Mason39279cc2007-06-12 06:35:45 -04009345 return 0;
9346fail:
9347 btrfs_destroy_cachep();
9348 return -ENOMEM;
9349}
9350
David Howellsa528d352017-01-31 16:46:22 +00009351static int btrfs_getattr(const struct path *path, struct kstat *stat,
9352 u32 request_mask, unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04009353{
Miao Xiedf0af1a2013-01-29 10:11:59 +00009354 u64 delalloc_bytes;
David Howellsa528d352017-01-31 16:46:22 +00009355 struct inode *inode = d_inode(path->dentry);
David Sterbafadc0d82011-11-20 07:33:38 -05009356 u32 blocksize = inode->i_sb->s_blocksize;
Yonghong Song04a87e32017-05-12 15:07:43 -07009357 u32 bi_flags = BTRFS_I(inode)->flags;
9358
9359 stat->result_mask |= STATX_BTIME;
9360 stat->btime.tv_sec = BTRFS_I(inode)->i_otime.tv_sec;
9361 stat->btime.tv_nsec = BTRFS_I(inode)->i_otime.tv_nsec;
9362 if (bi_flags & BTRFS_INODE_APPEND)
9363 stat->attributes |= STATX_ATTR_APPEND;
9364 if (bi_flags & BTRFS_INODE_COMPRESS)
9365 stat->attributes |= STATX_ATTR_COMPRESSED;
9366 if (bi_flags & BTRFS_INODE_IMMUTABLE)
9367 stat->attributes |= STATX_ATTR_IMMUTABLE;
9368 if (bi_flags & BTRFS_INODE_NODUMP)
9369 stat->attributes |= STATX_ATTR_NODUMP;
9370
9371 stat->attributes_mask |= (STATX_ATTR_APPEND |
9372 STATX_ATTR_COMPRESSED |
9373 STATX_ATTR_IMMUTABLE |
9374 STATX_ATTR_NODUMP);
David Sterbafadc0d82011-11-20 07:33:38 -05009375
Chris Mason39279cc2007-06-12 06:35:45 -04009376 generic_fillattr(inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04009377 stat->dev = BTRFS_I(inode)->root->anon_dev;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009378
9379 spin_lock(&BTRFS_I(inode)->lock);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01009380 delalloc_bytes = BTRFS_I(inode)->new_delalloc_bytes;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009381 spin_unlock(&BTRFS_I(inode)->lock);
David Sterbafadc0d82011-11-20 07:33:38 -05009382 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
Miao Xiedf0af1a2013-01-29 10:11:59 +00009383 ALIGN(delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04009384 return 0;
9385}
9386
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009387static int btrfs_rename_exchange(struct inode *old_dir,
9388 struct dentry *old_dentry,
9389 struct inode *new_dir,
9390 struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04009391{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009392 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009393 struct btrfs_trans_handle *trans;
9394 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009395 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009396 struct inode *new_inode = new_dentry->d_inode;
9397 struct inode *old_inode = old_dentry->d_inode;
Deepa Dinamani95582b02018-05-08 19:36:02 -07009398 struct timespec64 ctime = current_time(old_inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009399 struct dentry *parent;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009400 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
9401 u64 new_ino = btrfs_ino(BTRFS_I(new_inode));
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009402 u64 old_idx = 0;
9403 u64 new_idx = 0;
9404 u64 root_objectid;
9405 int ret;
Filipe Mananac5b4a502018-06-11 19:24:16 +01009406 int ret2;
Filipe Manana86e8aa02016-05-05 02:02:27 +01009407 bool root_log_pinned = false;
9408 bool dest_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009409
9410 /* we only allow rename subvolume link between subvolumes */
9411 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
9412 return -EXDEV;
9413
9414 /* close the race window with snapshot create/destroy ioctl */
9415 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009416 down_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009417 if (new_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009418 down_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009419
9420 /*
9421 * We want to reserve the absolute worst case amount of items. So if
9422 * both inodes are subvols and we need to unlink them then that would
9423 * require 4 item modifications, but if they are both normal inodes it
9424 * would require 5 item modifications, so we'll assume their normal
9425 * inodes. So 5 * 2 is 10, plus 2 for the new links, so 12 total items
9426 * should cover the worst case number of items we'll modify.
9427 */
9428 trans = btrfs_start_transaction(root, 12);
9429 if (IS_ERR(trans)) {
9430 ret = PTR_ERR(trans);
9431 goto out_notrans;
9432 }
9433
9434 /*
9435 * We need to find a free sequence number both in the source and
9436 * in the destination directory for the exchange.
9437 */
Nikolay Borisov877574e2017-02-20 13:50:33 +02009438 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009439 if (ret)
9440 goto out_fail;
Nikolay Borisov877574e2017-02-20 13:50:33 +02009441 ret = btrfs_set_inode_index(BTRFS_I(old_dir), &new_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009442 if (ret)
9443 goto out_fail;
9444
9445 BTRFS_I(old_inode)->dir_index = 0ULL;
9446 BTRFS_I(new_inode)->dir_index = 0ULL;
9447
9448 /* Reference for the source. */
9449 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
9450 /* force full log commit if subvolume involved. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009451 btrfs_set_log_full_commit(fs_info, trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009452 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01009453 btrfs_pin_log_trans(root);
9454 root_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009455 ret = btrfs_insert_inode_ref(trans, dest,
9456 new_dentry->d_name.name,
9457 new_dentry->d_name.len,
9458 old_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01009459 btrfs_ino(BTRFS_I(new_dir)),
9460 old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009461 if (ret)
9462 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009463 }
9464
9465 /* And now for the dest. */
9466 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
9467 /* force full log commit if subvolume involved. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009468 btrfs_set_log_full_commit(fs_info, trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009469 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01009470 btrfs_pin_log_trans(dest);
9471 dest_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009472 ret = btrfs_insert_inode_ref(trans, root,
9473 old_dentry->d_name.name,
9474 old_dentry->d_name.len,
9475 new_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01009476 btrfs_ino(BTRFS_I(old_dir)),
9477 new_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009478 if (ret)
9479 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009480 }
9481
9482 /* Update inode version and ctime/mtime. */
9483 inode_inc_iversion(old_dir);
9484 inode_inc_iversion(new_dir);
9485 inode_inc_iversion(old_inode);
9486 inode_inc_iversion(new_inode);
9487 old_dir->i_ctime = old_dir->i_mtime = ctime;
9488 new_dir->i_ctime = new_dir->i_mtime = ctime;
9489 old_inode->i_ctime = ctime;
9490 new_inode->i_ctime = ctime;
9491
9492 if (old_dentry->d_parent != new_dentry->d_parent) {
David Sterbaf85b7372017-01-20 14:54:07 +01009493 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
9494 BTRFS_I(old_inode), 1);
9495 btrfs_record_unlink_dir(trans, BTRFS_I(new_dir),
9496 BTRFS_I(new_inode), 1);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009497 }
9498
9499 /* src is a subvolume */
9500 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
9501 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
Lu Fengqi401b3b12018-08-01 11:32:30 +08009502 ret = btrfs_unlink_subvol(trans, old_dir, root_objectid,
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009503 old_dentry->d_name.name,
9504 old_dentry->d_name.len);
9505 } else { /* src is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009506 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
9507 BTRFS_I(old_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009508 old_dentry->d_name.name,
9509 old_dentry->d_name.len);
9510 if (!ret)
9511 ret = btrfs_update_inode(trans, root, old_inode);
9512 }
9513 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009514 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009515 goto out_fail;
9516 }
9517
9518 /* dest is a subvolume */
9519 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
9520 root_objectid = BTRFS_I(new_inode)->root->root_key.objectid;
Lu Fengqi401b3b12018-08-01 11:32:30 +08009521 ret = btrfs_unlink_subvol(trans, new_dir, root_objectid,
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009522 new_dentry->d_name.name,
9523 new_dentry->d_name.len);
9524 } else { /* dest is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009525 ret = __btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
9526 BTRFS_I(new_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009527 new_dentry->d_name.name,
9528 new_dentry->d_name.len);
9529 if (!ret)
9530 ret = btrfs_update_inode(trans, dest, new_inode);
9531 }
9532 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009533 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009534 goto out_fail;
9535 }
9536
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009537 ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009538 new_dentry->d_name.name,
9539 new_dentry->d_name.len, 0, old_idx);
9540 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009541 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009542 goto out_fail;
9543 }
9544
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009545 ret = btrfs_add_link(trans, BTRFS_I(old_dir), BTRFS_I(new_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009546 old_dentry->d_name.name,
9547 old_dentry->d_name.len, 0, new_idx);
9548 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009549 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009550 goto out_fail;
9551 }
9552
9553 if (old_inode->i_nlink == 1)
9554 BTRFS_I(old_inode)->dir_index = old_idx;
9555 if (new_inode->i_nlink == 1)
9556 BTRFS_I(new_inode)->dir_index = new_idx;
9557
Filipe Manana86e8aa02016-05-05 02:02:27 +01009558 if (root_log_pinned) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009559 parent = new_dentry->d_parent;
David Sterbaf85b7372017-01-20 14:54:07 +01009560 btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir),
9561 parent);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009562 btrfs_end_log_trans(root);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009563 root_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009564 }
Filipe Manana86e8aa02016-05-05 02:02:27 +01009565 if (dest_log_pinned) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009566 parent = old_dentry->d_parent;
David Sterbaf85b7372017-01-20 14:54:07 +01009567 btrfs_log_new_name(trans, BTRFS_I(new_inode), BTRFS_I(new_dir),
9568 parent);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009569 btrfs_end_log_trans(dest);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009570 dest_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009571 }
9572out_fail:
Filipe Manana86e8aa02016-05-05 02:02:27 +01009573 /*
9574 * If we have pinned a log and an error happened, we unpin tasks
9575 * trying to sync the log and force them to fallback to a transaction
9576 * commit if the log currently contains any of the inodes involved in
9577 * this rename operation (to ensure we do not persist a log with an
9578 * inconsistent state for any of these inodes or leading to any
9579 * inconsistencies when replayed). If the transaction was aborted, the
9580 * abortion reason is propagated to userspace when attempting to commit
9581 * the transaction. If the log does not contain any of these inodes, we
9582 * allow the tasks to sync it.
9583 */
9584 if (ret && (root_log_pinned || dest_log_pinned)) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009585 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
9586 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
9587 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana86e8aa02016-05-05 02:02:27 +01009588 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009589 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009590 btrfs_set_log_full_commit(fs_info, trans);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009591
9592 if (root_log_pinned) {
9593 btrfs_end_log_trans(root);
9594 root_log_pinned = false;
9595 }
9596 if (dest_log_pinned) {
9597 btrfs_end_log_trans(dest);
9598 dest_log_pinned = false;
9599 }
9600 }
Filipe Mananac5b4a502018-06-11 19:24:16 +01009601 ret2 = btrfs_end_transaction(trans);
9602 ret = ret ? ret : ret2;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009603out_notrans:
9604 if (new_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009605 up_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009606 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009607 up_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009608
9609 return ret;
9610}
9611
9612static int btrfs_whiteout_for_rename(struct btrfs_trans_handle *trans,
9613 struct btrfs_root *root,
9614 struct inode *dir,
9615 struct dentry *dentry)
9616{
9617 int ret;
9618 struct inode *inode;
9619 u64 objectid;
9620 u64 index;
9621
9622 ret = btrfs_find_free_ino(root, &objectid);
9623 if (ret)
9624 return ret;
9625
9626 inode = btrfs_new_inode(trans, root, dir,
9627 dentry->d_name.name,
9628 dentry->d_name.len,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009629 btrfs_ino(BTRFS_I(dir)),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009630 objectid,
9631 S_IFCHR | WHITEOUT_MODE,
9632 &index);
9633
9634 if (IS_ERR(inode)) {
9635 ret = PTR_ERR(inode);
9636 return ret;
9637 }
9638
9639 inode->i_op = &btrfs_special_inode_operations;
9640 init_special_inode(inode, inode->i_mode,
9641 WHITEOUT_DEV);
9642
9643 ret = btrfs_init_inode_security(trans, inode, dir,
9644 &dentry->d_name);
9645 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009646 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009647
Nikolay Borisovcef415a2017-02-20 13:51:09 +02009648 ret = btrfs_add_nondir(trans, BTRFS_I(dir), dentry,
9649 BTRFS_I(inode), 0, index);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009650 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009651 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009652
9653 ret = btrfs_update_inode(trans, root, inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009654out:
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009655 unlock_new_inode(inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009656 if (ret)
9657 inode_dec_link_count(inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009658 iput(inode);
9659
Filipe Mananac9901612016-05-05 01:41:57 +01009660 return ret;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009661}
9662
Chris Mason39279cc2007-06-12 06:35:45 -04009663static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009664 struct inode *new_dir, struct dentry *new_dentry,
9665 unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04009666{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009667 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009668 struct btrfs_trans_handle *trans;
Filipe Manana5062af32016-05-05 10:26:26 +01009669 unsigned int trans_num_items;
Chris Mason39279cc2007-06-12 06:35:45 -04009670 struct btrfs_root *root = BTRFS_I(old_dir)->root;
9671 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00009672 struct inode *new_inode = d_inode(new_dentry);
9673 struct inode *old_inode = d_inode(old_dentry);
Chris Mason00e4e6b2008-08-05 11:18:09 -04009674 u64 index = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009675 u64 root_objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04009676 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009677 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009678 bool log_pinned = false;
Chris Mason39279cc2007-06-12 06:35:45 -04009679
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009680 if (btrfs_ino(BTRFS_I(new_dir)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04009681 return -EPERM;
9682
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009683 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08009684 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05009685 return -EXDEV;
9686
Li Zefan33345d012011-04-20 10:31:50 +08009687 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009688 (new_inode && btrfs_ino(BTRFS_I(new_inode)) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009689 return -ENOTEMPTY;
9690
Chris Mason39279cc2007-06-12 06:35:45 -04009691 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009692 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04009693 return -ENOTEMPTY;
Chris Mason9c520572012-12-17 14:26:57 -05009694
9695
9696 /* check for collisions, even if the name isn't there */
Josef Bacik4871c152013-10-09 12:24:04 -04009697 ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino,
Chris Mason9c520572012-12-17 14:26:57 -05009698 new_dentry->d_name.name,
9699 new_dentry->d_name.len);
9700
9701 if (ret) {
9702 if (ret == -EEXIST) {
9703 /* we shouldn't get
9704 * eexist without a new_inode */
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05309705 if (WARN_ON(!new_inode)) {
Chris Mason9c520572012-12-17 14:26:57 -05009706 return ret;
9707 }
9708 } else {
9709 /* maybe -EOVERFLOW */
9710 return ret;
9711 }
9712 }
9713 ret = 0;
9714
Chris Mason5a3f23d2009-03-31 13:27:11 -04009715 /*
Chris Mason8d875f92014-08-12 10:47:42 -07009716 * we're using rename to replace one file with another. Start IO on it
9717 * now so we don't add too much work to the end of the transaction
Chris Mason5a3f23d2009-03-31 13:27:11 -04009718 */
Chris Mason8d875f92014-08-12 10:47:42 -07009719 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size)
Chris Mason5a3f23d2009-03-31 13:27:11 -04009720 filemap_flush(old_inode->i_mapping);
9721
Yan, Zheng76dda932009-09-21 16:00:26 -04009722 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08009723 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009724 down_read(&fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04009725 /*
9726 * We want to reserve the absolute worst case amount of items. So if
9727 * both inodes are subvols and we need to unlink them then that would
9728 * require 4 item modifications, but if they are both normal inodes it
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009729 * would require 5 item modifications, so we'll assume they are normal
Yan, Zhenga22285a2010-05-16 10:48:46 -04009730 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
9731 * should cover the worst case number of items we'll modify.
Filipe Manana5062af32016-05-05 10:26:26 +01009732 * If our rename has the whiteout flag, we need more 5 units for the
9733 * new inode (1 inode item, 1 inode ref, 2 dir items and 1 xattr item
9734 * when selinux is enabled).
Yan, Zhenga22285a2010-05-16 10:48:46 -04009735 */
Filipe Manana5062af32016-05-05 10:26:26 +01009736 trans_num_items = 11;
9737 if (flags & RENAME_WHITEOUT)
9738 trans_num_items += 5;
9739 trans = btrfs_start_transaction(root, trans_num_items);
Johann Lombardib44c59a2011-03-31 13:23:47 +00009740 if (IS_ERR(trans)) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009741 ret = PTR_ERR(trans);
9742 goto out_notrans;
9743 }
Chris Mason5f39d392007-10-15 16:14:19 -04009744
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009745 if (dest != root)
9746 btrfs_record_root_in_trans(trans, dest);
9747
Nikolay Borisov877574e2017-02-20 13:50:33 +02009748 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009749 if (ret)
9750 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009751
Miao Xie67de1172013-12-26 13:07:06 +08009752 BTRFS_I(old_inode)->dir_index = 0ULL;
Li Zefan33345d012011-04-20 10:31:50 +08009753 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009754 /* force full log commit if subvolume involved. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009755 btrfs_set_log_full_commit(fs_info, trans);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009756 } else {
Filipe Mananac4aba952016-04-29 13:14:42 +01009757 btrfs_pin_log_trans(root);
9758 log_pinned = true;
Yan, Zhenga5719522009-09-24 09:17:31 -04009759 ret = btrfs_insert_inode_ref(trans, dest,
9760 new_dentry->d_name.name,
9761 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08009762 old_ino,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009763 btrfs_ino(BTRFS_I(new_dir)), index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009764 if (ret)
9765 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009766 }
Chris Mason5a3f23d2009-03-31 13:27:11 -04009767
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009768 inode_inc_iversion(old_dir);
9769 inode_inc_iversion(new_dir);
9770 inode_inc_iversion(old_inode);
Deepa Dinamani04b285f2016-02-06 23:57:21 -08009771 old_dir->i_ctime = old_dir->i_mtime =
9772 new_dir->i_ctime = new_dir->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009773 old_inode->i_ctime = current_time(old_dir);
Chris Mason5f39d392007-10-15 16:14:19 -04009774
Chris Mason12fcfd22009-03-24 10:24:20 -04009775 if (old_dentry->d_parent != new_dentry->d_parent)
David Sterbaf85b7372017-01-20 14:54:07 +01009776 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
9777 BTRFS_I(old_inode), 1);
Chris Mason12fcfd22009-03-24 10:24:20 -04009778
Li Zefan33345d012011-04-20 10:31:50 +08009779 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009780 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
Lu Fengqi401b3b12018-08-01 11:32:30 +08009781 ret = btrfs_unlink_subvol(trans, old_dir, root_objectid,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009782 old_dentry->d_name.name,
9783 old_dentry->d_name.len);
9784 } else {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009785 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
9786 BTRFS_I(d_inode(old_dentry)),
Al Viro92986792011-03-04 17:14:37 +00009787 old_dentry->d_name.name,
9788 old_dentry->d_name.len);
9789 if (!ret)
9790 ret = btrfs_update_inode(trans, root, old_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009791 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009792 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009793 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009794 goto out_fail;
9795 }
Chris Mason39279cc2007-06-12 06:35:45 -04009796
9797 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009798 inode_inc_iversion(new_inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009799 new_inode->i_ctime = current_time(new_inode);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009800 if (unlikely(btrfs_ino(BTRFS_I(new_inode)) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009801 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
9802 root_objectid = BTRFS_I(new_inode)->location.objectid;
Lu Fengqi401b3b12018-08-01 11:32:30 +08009803 ret = btrfs_unlink_subvol(trans, new_dir, root_objectid,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009804 new_dentry->d_name.name,
9805 new_dentry->d_name.len);
9806 BUG_ON(new_inode->i_nlink == 0);
9807 } else {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009808 ret = btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
9809 BTRFS_I(d_inode(new_dentry)),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009810 new_dentry->d_name.name,
9811 new_dentry->d_name.len);
9812 }
Josef Bacik4ef31a42013-08-13 14:10:08 -04009813 if (!ret && new_inode->i_nlink == 0)
Nikolay Borisov73f2e542017-02-20 13:50:59 +02009814 ret = btrfs_orphan_add(trans,
9815 BTRFS_I(d_inode(new_dentry)));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009816 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009817 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009818 goto out_fail;
9819 }
Chris Mason39279cc2007-06-12 06:35:45 -04009820 }
Josef Bacikaec74772008-07-24 12:12:38 -04009821
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009822 ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009823 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04009824 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009825 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009826 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009827 goto out_fail;
9828 }
Chris Mason39279cc2007-06-12 06:35:45 -04009829
Miao Xie67de1172013-12-26 13:07:06 +08009830 if (old_inode->i_nlink == 1)
9831 BTRFS_I(old_inode)->dir_index = index;
9832
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009833 if (log_pinned) {
Al Viro10d9f302011-07-16 23:09:10 -04009834 struct dentry *parent = new_dentry->d_parent;
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009835
David Sterbaf85b7372017-01-20 14:54:07 +01009836 btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir),
9837 parent);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009838 btrfs_end_log_trans(root);
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009839 log_pinned = false;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009840 }
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009841
9842 if (flags & RENAME_WHITEOUT) {
9843 ret = btrfs_whiteout_for_rename(trans, root, old_dir,
9844 old_dentry);
9845
9846 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009847 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009848 goto out_fail;
9849 }
Chris Mason12fcfd22009-03-24 10:24:20 -04009850 }
Chris Mason39279cc2007-06-12 06:35:45 -04009851out_fail:
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009852 /*
9853 * If we have pinned the log and an error happened, we unpin tasks
9854 * trying to sync the log and force them to fallback to a transaction
9855 * commit if the log currently contains any of the inodes involved in
9856 * this rename operation (to ensure we do not persist a log with an
9857 * inconsistent state for any of these inodes or leading to any
9858 * inconsistencies when replayed). If the transaction was aborted, the
9859 * abortion reason is propagated to userspace when attempting to commit
9860 * the transaction. If the log does not contain any of these inodes, we
9861 * allow the tasks to sync it.
9862 */
9863 if (ret && log_pinned) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009864 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
9865 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
9866 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009867 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009868 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009869 btrfs_set_log_full_commit(fs_info, trans);
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009870
9871 btrfs_end_log_trans(root);
9872 log_pinned = false;
9873 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009874 btrfs_end_transaction(trans);
Johann Lombardib44c59a2011-03-31 13:23:47 +00009875out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +08009876 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009877 up_read(&fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009878
Chris Mason39279cc2007-06-12 06:35:45 -04009879 return ret;
9880}
9881
Miklos Szeredi80ace852014-07-23 15:15:32 +02009882static int btrfs_rename2(struct inode *old_dir, struct dentry *old_dentry,
9883 struct inode *new_dir, struct dentry *new_dentry,
9884 unsigned int flags)
9885{
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009886 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
Miklos Szeredi80ace852014-07-23 15:15:32 +02009887 return -EINVAL;
9888
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009889 if (flags & RENAME_EXCHANGE)
9890 return btrfs_rename_exchange(old_dir, old_dentry, new_dir,
9891 new_dentry);
9892
9893 return btrfs_rename(old_dir, old_dentry, new_dir, new_dentry, flags);
Miklos Szeredi80ace852014-07-23 15:15:32 +02009894}
9895
Nikolay Borisov3a2f8c02018-04-24 17:23:59 +03009896struct btrfs_delalloc_work {
9897 struct inode *inode;
9898 struct completion completion;
9899 struct list_head list;
9900 struct btrfs_work work;
9901};
9902
Miao Xie8ccf6f192012-10-25 09:28:04 +00009903static void btrfs_run_delalloc_work(struct btrfs_work *work)
9904{
9905 struct btrfs_delalloc_work *delalloc_work;
Josef Bacik9f23e282013-10-28 15:03:41 -04009906 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009907
9908 delalloc_work = container_of(work, struct btrfs_delalloc_work,
9909 work);
Josef Bacik9f23e282013-10-28 15:03:41 -04009910 inode = delalloc_work->inode;
David Sterba30424602015-11-27 19:27:11 +01009911 filemap_flush(inode->i_mapping);
9912 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
9913 &BTRFS_I(inode)->runtime_flags))
Josef Bacik9f23e282013-10-28 15:03:41 -04009914 filemap_flush(inode->i_mapping);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009915
Nikolay Borisov076da912018-04-23 10:54:16 +03009916 iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009917 complete(&delalloc_work->completion);
9918}
9919
Nikolay Borisov3a2f8c02018-04-24 17:23:59 +03009920static struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode)
Miao Xie8ccf6f192012-10-25 09:28:04 +00009921{
9922 struct btrfs_delalloc_work *work;
9923
David Sterba100d5702015-12-08 14:39:32 +01009924 work = kmalloc(sizeof(*work), GFP_NOFS);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009925 if (!work)
9926 return NULL;
9927
9928 init_completion(&work->completion);
9929 INIT_LIST_HEAD(&work->list);
9930 work->inode = inode;
Liu Bo9e0af232014-08-15 23:36:53 +08009931 WARN_ON_ONCE(!inode);
9932 btrfs_init_work(&work->work, btrfs_flush_delalloc_helper,
9933 btrfs_run_delalloc_work, NULL, NULL);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009934
9935 return work;
9936}
9937
Chris Masond352ac62008-09-29 15:18:18 -04009938/*
9939 * some fairly slow code that needs optimization. This walks the list
9940 * of all the inodes with pending delalloc and forces them to disk.
9941 */
Nikolay Borisov4fbb5142018-04-23 10:54:15 +03009942static int start_delalloc_inodes(struct btrfs_root *root, int nr)
Chris Masonea8c2812008-08-04 23:17:27 -04009943{
Chris Masonea8c2812008-08-04 23:17:27 -04009944 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009945 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009946 struct btrfs_delalloc_work *work, *next;
9947 struct list_head works;
Miao Xie1eafa6c2013-01-22 10:49:00 +00009948 struct list_head splice;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009949 int ret = 0;
Chris Masonea8c2812008-08-04 23:17:27 -04009950
Miao Xie8ccf6f192012-10-25 09:28:04 +00009951 INIT_LIST_HEAD(&works);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009952 INIT_LIST_HEAD(&splice);
Miao Xie63607cc2013-01-22 10:50:35 +00009953
Miao Xie573bfb72014-03-06 13:55:03 +08009954 mutex_lock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009955 spin_lock(&root->delalloc_lock);
9956 list_splice_init(&root->delalloc_inodes, &splice);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009957 while (!list_empty(&splice)) {
9958 binode = list_entry(splice.next, struct btrfs_inode,
Chris Masonea8c2812008-08-04 23:17:27 -04009959 delalloc_inodes);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009960
Miao Xieeb73c1b2013-05-15 07:48:22 +00009961 list_move_tail(&binode->delalloc_inodes,
9962 &root->delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009963 inode = igrab(&binode->vfs_inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00009964 if (!inode) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00009965 cond_resched_lock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009966 continue;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009967 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00009968 spin_unlock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009969
Nikolay Borisov076da912018-04-23 10:54:16 +03009970 work = btrfs_alloc_delalloc_work(inode);
David Sterba5d99a9982014-09-29 19:20:37 +02009971 if (!work) {
Nikolay Borisov4fbb5142018-04-23 10:54:15 +03009972 iput(inode);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009973 ret = -ENOMEM;
Wang Shilonga1ecaab2014-04-02 19:53:32 +08009974 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009975 }
Miao Xie1eafa6c2013-01-22 10:49:00 +00009976 list_add_tail(&work->list, &works);
Qu Wenruoa44903a2014-02-28 10:46:09 +08009977 btrfs_queue_work(root->fs_info->flush_workers,
9978 &work->work);
Miao Xie6c255e62014-03-06 13:55:01 +08009979 ret++;
9980 if (nr != -1 && ret >= nr)
Wang Shilonga1ecaab2014-04-02 19:53:32 +08009981 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009982 cond_resched();
Miao Xieeb73c1b2013-05-15 07:48:22 +00009983 spin_lock(&root->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04009984 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00009985 spin_unlock(&root->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -04009986
Wang Shilonga1ecaab2014-04-02 19:53:32 +08009987out:
Miao Xie1eafa6c2013-01-22 10:49:00 +00009988 list_for_each_entry_safe(work, next, &works, list) {
9989 list_del_init(&work->list);
Nikolay Borisov40012f92018-04-19 10:46:39 +03009990 wait_for_completion(&work->completion);
9991 kfree(work);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009992 }
9993
Nikolay Borisov81f1d392018-04-19 10:46:37 +03009994 if (!list_empty(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00009995 spin_lock(&root->delalloc_lock);
9996 list_splice_tail(&splice, &root->delalloc_inodes);
9997 spin_unlock(&root->delalloc_lock);
9998 }
Miao Xie573bfb72014-03-06 13:55:03 +08009999 mutex_unlock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010000 return ret;
10001}
10002
Nikolay Borisov76f32e22018-04-23 10:54:14 +030010003int btrfs_start_delalloc_inodes(struct btrfs_root *root)
Miao Xieeb73c1b2013-05-15 07:48:22 +000010004{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010005 struct btrfs_fs_info *fs_info = root->fs_info;
Miao Xieeb73c1b2013-05-15 07:48:22 +000010006 int ret;
10007
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010008 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +000010009 return -EROFS;
10010
Nikolay Borisov4fbb5142018-04-23 10:54:15 +030010011 ret = start_delalloc_inodes(root, -1);
Miao Xie6c255e62014-03-06 13:55:01 +080010012 if (ret > 0)
10013 ret = 0;
Miao Xieeb73c1b2013-05-15 07:48:22 +000010014 return ret;
10015}
10016
Nikolay Borisov82b3e532018-04-23 10:54:13 +030010017int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, int nr)
Miao Xieeb73c1b2013-05-15 07:48:22 +000010018{
10019 struct btrfs_root *root;
10020 struct list_head splice;
10021 int ret;
10022
Wang Shilong2c21b4d2014-01-14 19:42:20 +080010023 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +000010024 return -EROFS;
10025
10026 INIT_LIST_HEAD(&splice);
10027
Miao Xie573bfb72014-03-06 13:55:03 +080010028 mutex_lock(&fs_info->delalloc_root_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010029 spin_lock(&fs_info->delalloc_root_lock);
10030 list_splice_init(&fs_info->delalloc_roots, &splice);
Miao Xie6c255e62014-03-06 13:55:01 +080010031 while (!list_empty(&splice) && nr) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010032 root = list_first_entry(&splice, struct btrfs_root,
10033 delalloc_root);
10034 root = btrfs_grab_fs_root(root);
10035 BUG_ON(!root);
10036 list_move_tail(&root->delalloc_root,
10037 &fs_info->delalloc_roots);
10038 spin_unlock(&fs_info->delalloc_root_lock);
10039
Nikolay Borisov4fbb5142018-04-23 10:54:15 +030010040 ret = start_delalloc_inodes(root, nr);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010041 btrfs_put_fs_root(root);
Miao Xie6c255e62014-03-06 13:55:01 +080010042 if (ret < 0)
Miao Xieeb73c1b2013-05-15 07:48:22 +000010043 goto out;
10044
Miao Xie6c255e62014-03-06 13:55:01 +080010045 if (nr != -1) {
10046 nr -= ret;
10047 WARN_ON(nr < 0);
10048 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010049 spin_lock(&fs_info->delalloc_root_lock);
10050 }
10051 spin_unlock(&fs_info->delalloc_root_lock);
10052
Miao Xie6c255e62014-03-06 13:55:01 +080010053 ret = 0;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010054out:
Nikolay Borisov81f1d392018-04-19 10:46:37 +030010055 if (!list_empty(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010056 spin_lock(&fs_info->delalloc_root_lock);
10057 list_splice_tail(&splice, &fs_info->delalloc_roots);
10058 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010059 }
Miao Xie573bfb72014-03-06 13:55:03 +080010060 mutex_unlock(&fs_info->delalloc_root_mutex);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010061 return ret;
Chris Masonea8c2812008-08-04 23:17:27 -040010062}
10063
Chris Mason39279cc2007-06-12 06:35:45 -040010064static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
10065 const char *symname)
10066{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010067 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -040010068 struct btrfs_trans_handle *trans;
10069 struct btrfs_root *root = BTRFS_I(dir)->root;
10070 struct btrfs_path *path;
10071 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -050010072 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -040010073 int err;
10074 int drop_inode = 0;
10075 u64 objectid;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +053010076 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -040010077 int name_len;
10078 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -040010079 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -040010080 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -040010081 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -040010082
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +010010083 name_len = strlen(symname);
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010084 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info))
Chris Mason39279cc2007-06-12 06:35:45 -040010085 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -050010086
Josef Bacik9ed74f22009-09-11 16:12:44 -040010087 /*
10088 * 2 items for inode item and ref
10089 * 2 items for dir items
Filipe Manana9269d122015-12-31 18:16:29 +000010090 * 1 item for updating parent inode item
10091 * 1 item for the inline extent item
Josef Bacik9ed74f22009-09-11 16:12:44 -040010092 * 1 item for xattr if selinux is on
10093 */
Filipe Manana9269d122015-12-31 18:16:29 +000010094 trans = btrfs_start_transaction(root, 7);
Yan, Zhenga22285a2010-05-16 10:48:46 -040010095 if (IS_ERR(trans))
10096 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -050010097
Li Zefan581bb052011-04-20 10:06:11 +080010098 err = btrfs_find_free_ino(root, &objectid);
10099 if (err)
10100 goto out_unlock;
10101
Josef Bacikaec74772008-07-24 12:12:38 -040010102 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +010010103 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)),
10104 objectid, S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -040010105 if (IS_ERR(inode)) {
10106 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010107 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -040010108 }
Chris Mason39279cc2007-06-12 06:35:45 -040010109
Casey Schauflerad19db72011-12-15 10:09:07 -050010110 /*
10111 * If the active LSM wants to access the inode during
10112 * d_instantiate it needs these. Smack checks to see
10113 * if the filesystem supports xattrs by looking at the
10114 * ops vector.
10115 */
10116 inode->i_fop = &btrfs_file_operations;
10117 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -070010118 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -070010119 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
10120
10121 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
10122 if (err)
10123 goto out_unlock_inode;
Casey Schauflerad19db72011-12-15 10:09:07 -050010124
Chris Mason39279cc2007-06-12 06:35:45 -040010125 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -070010126 if (!path) {
10127 err = -ENOMEM;
Chris Masonb0d5d102014-09-08 13:08:51 -070010128 goto out_unlock_inode;
Mark Fashehd8926bb2011-07-13 10:38:47 -070010129 }
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +020010130 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason39279cc2007-06-12 06:35:45 -040010131 key.offset = 0;
David Sterba962a2982014-06-04 18:41:45 +020010132 key.type = BTRFS_EXTENT_DATA_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -040010133 datasize = btrfs_file_extent_calc_inline_size(name_len);
10134 err = btrfs_insert_empty_item(trans, root, path, &key,
10135 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -040010136 if (err) {
Julia Lawallb0839162011-05-14 07:10:51 +000010137 btrfs_free_path(path);
Chris Masonb0d5d102014-09-08 13:08:51 -070010138 goto out_unlock_inode;
Chris Mason54aa1f42007-06-22 14:16:25 -040010139 }
Chris Mason5f39d392007-10-15 16:14:19 -040010140 leaf = path->nodes[0];
10141 ei = btrfs_item_ptr(leaf, path->slots[0],
10142 struct btrfs_file_extent_item);
10143 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
10144 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -040010145 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -040010146 btrfs_set_file_extent_encryption(leaf, ei, 0);
10147 btrfs_set_file_extent_compression(leaf, ei, 0);
10148 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
10149 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
10150
Chris Mason39279cc2007-06-12 06:35:45 -040010151 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -040010152 write_extent_buffer(leaf, symname, ptr, name_len);
10153 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -040010154 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -040010155
Chris Mason39279cc2007-06-12 06:35:45 -040010156 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -050010157 inode_nohighmem(inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010158 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Yan Zhengd899e052008-10-30 14:25:28 -040010159 inode_set_bytes(inode, name_len);
Nikolay Borisov6ef06d22017-02-20 13:50:34 +020010160 btrfs_i_size_write(BTRFS_I(inode), name_len);
Chris Mason54aa1f42007-06-22 14:16:25 -040010161 err = btrfs_update_inode(trans, root, inode);
Filipe Mananad50866d2015-12-31 18:08:24 +000010162 /*
10163 * Last step, add directory indexes for our symlink inode. This is the
10164 * last step to avoid extra cleanup of these indexes if an error happens
10165 * elsewhere above.
10166 */
10167 if (!err)
Nikolay Borisovcef415a2017-02-20 13:51:09 +020010168 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry,
10169 BTRFS_I(inode), 0, index);
Chris Masonb0d5d102014-09-08 13:08:51 -070010170 if (err) {
Chris Mason54aa1f42007-06-22 14:16:25 -040010171 drop_inode = 1;
Chris Masonb0d5d102014-09-08 13:08:51 -070010172 goto out_unlock_inode;
10173 }
10174
Al Viro1e2e5472018-05-04 08:23:01 -040010175 d_instantiate_new(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010176
10177out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010178 btrfs_end_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -040010179 if (drop_inode) {
10180 inode_dec_link_count(inode);
10181 iput(inode);
10182 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010183 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -040010184 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -070010185
10186out_unlock_inode:
10187 drop_inode = 1;
10188 unlock_new_inode(inode);
10189 goto out_unlock;
Chris Mason39279cc2007-06-12 06:35:45 -040010190}
Chris Mason16432982008-04-10 10:23:21 -040010191
Josef Bacik0af3d002010-06-21 14:48:16 -040010192static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
10193 u64 start, u64 num_bytes, u64 min_size,
10194 loff_t actual_len, u64 *alloc_hint,
10195 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -040010196{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010197 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik5dc562c2012-08-17 13:14:17 -040010198 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
10199 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -040010200 struct btrfs_root *root = BTRFS_I(inode)->root;
10201 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -040010202 u64 cur_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +000010203 u64 i_size;
Chris Mason154ea282013-03-05 11:11:26 -050010204 u64 cur_bytes;
Josef Bacik0b670dc2015-09-23 17:11:16 -040010205 u64 last_alloc = (u64)-1;
Yan Zhengd899e052008-10-30 14:25:28 -040010206 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -040010207 bool own_trans = true;
Wang Xiaoguang18513092016-07-25 15:51:40 +080010208 u64 end = start + num_bytes - 1;
Yan Zhengd899e052008-10-30 14:25:28 -040010209
Josef Bacik0af3d002010-06-21 14:48:16 -040010210 if (trans)
10211 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -040010212 while (num_bytes > 0) {
Josef Bacik0af3d002010-06-21 14:48:16 -040010213 if (own_trans) {
10214 trans = btrfs_start_transaction(root, 3);
10215 if (IS_ERR(trans)) {
10216 ret = PTR_ERR(trans);
10217 break;
10218 }
Yan Zhengd899e052008-10-30 14:25:28 -040010219 }
Yan, Zheng5a303d52009-11-12 09:34:52 +000010220
Byongho Leeee221842015-12-15 01:42:10 +090010221 cur_bytes = min_t(u64, num_bytes, SZ_256M);
Chris Mason154ea282013-03-05 11:11:26 -050010222 cur_bytes = max(cur_bytes, min_size);
Josef Bacik0b670dc2015-09-23 17:11:16 -040010223 /*
10224 * If we are severely fragmented we could end up with really
10225 * small allocations, so if the allocator is returning small
10226 * chunks lets make its job easier by only searching for those
10227 * sized chunks.
10228 */
10229 cur_bytes = min(cur_bytes, last_alloc);
Wang Xiaoguang18513092016-07-25 15:51:40 +080010230 ret = btrfs_reserve_extent(root, cur_bytes, cur_bytes,
10231 min_size, 0, *alloc_hint, &ins, 1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010232 if (ret) {
Josef Bacik0af3d002010-06-21 14:48:16 -040010233 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010234 btrfs_end_transaction(trans);
Yan, Zhenga22285a2010-05-16 10:48:46 -040010235 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +000010236 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010237 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010238
Josef Bacik0b670dc2015-09-23 17:11:16 -040010239 last_alloc = ins.offset;
Yan Zhengd899e052008-10-30 14:25:28 -040010240 ret = insert_reserved_file_extent(trans, inode,
10241 cur_offset, ins.objectid,
10242 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +000010243 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -040010244 BTRFS_FILE_EXTENT_PREALLOC);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010245 if (ret) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010246 btrfs_free_reserved_extent(fs_info, ins.objectid,
Miao Xiee570fd22014-06-19 10:42:50 +080010247 ins.offset, 0);
Jeff Mahoney66642832016-06-10 18:19:25 -040010248 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010249 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010250 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010251 break;
10252 }
Dongsheng Yang31193212014-12-12 16:44:35 +080010253
Nikolay Borisovdcdbc052017-02-20 13:50:45 +020010254 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Chris Masona1ed8352009-09-11 12:27:37 -040010255 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010256
Josef Bacik5dc562c2012-08-17 13:14:17 -040010257 em = alloc_extent_map();
10258 if (!em) {
10259 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
10260 &BTRFS_I(inode)->runtime_flags);
10261 goto next;
10262 }
10263
10264 em->start = cur_offset;
10265 em->orig_start = cur_offset;
10266 em->len = ins.offset;
10267 em->block_start = ins.objectid;
10268 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -050010269 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -040010270 em->ram_bytes = ins.offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010271 em->bdev = fs_info->fs_devices->latest_bdev;
Josef Bacik5dc562c2012-08-17 13:14:17 -040010272 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
10273 em->generation = trans->transid;
10274
10275 while (1) {
10276 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -040010277 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -040010278 write_unlock(&em_tree->lock);
10279 if (ret != -EEXIST)
10280 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +020010281 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -040010282 cur_offset + ins.offset - 1,
10283 0);
10284 }
10285 free_extent_map(em);
10286next:
Yan Zhengd899e052008-10-30 14:25:28 -040010287 num_bytes -= ins.offset;
10288 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -040010289 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +000010290
Josef Bacik0c4d2d92012-04-05 15:03:02 -040010291 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -070010292 inode->i_ctime = current_time(inode);
Christoph Hellwig6cbff002009-04-17 10:37:41 +020010293 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -040010294 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -040010295 (actual_len > inode->i_size) &&
10296 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +000010297 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +000010298 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +000010299 else
Josef Bacik55a61d12010-11-22 18:50:32 +000010300 i_size = cur_offset;
10301 i_size_write(inode, i_size);
10302 btrfs_ordered_update_i_size(inode, i_size, NULL);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010303 }
10304
Yan Zhengd899e052008-10-30 14:25:28 -040010305 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010306
10307 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -040010308 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010309 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010310 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010311 break;
10312 }
Yan Zhengd899e052008-10-30 14:25:28 -040010313
Josef Bacik0af3d002010-06-21 14:48:16 -040010314 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010315 btrfs_end_transaction(trans);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010316 }
Wang Xiaoguang18513092016-07-25 15:51:40 +080010317 if (cur_offset < end)
Qu Wenruobc42bda2017-02-27 15:10:39 +080010318 btrfs_free_reserved_data_space(inode, NULL, cur_offset,
Wang Xiaoguang18513092016-07-25 15:51:40 +080010319 end - cur_offset + 1);
Yan Zhengd899e052008-10-30 14:25:28 -040010320 return ret;
10321}
10322
Josef Bacik0af3d002010-06-21 14:48:16 -040010323int btrfs_prealloc_file_range(struct inode *inode, int mode,
10324 u64 start, u64 num_bytes, u64 min_size,
10325 loff_t actual_len, u64 *alloc_hint)
10326{
10327 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
10328 min_size, actual_len, alloc_hint,
10329 NULL);
10330}
10331
10332int btrfs_prealloc_file_range_trans(struct inode *inode,
10333 struct btrfs_trans_handle *trans, int mode,
10334 u64 start, u64 num_bytes, u64 min_size,
10335 loff_t actual_len, u64 *alloc_hint)
10336{
10337 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
10338 min_size, actual_len, alloc_hint, trans);
10339}
10340
Chris Masone6dcd2d2008-07-17 12:53:50 -040010341static int btrfs_set_page_dirty(struct page *page)
10342{
Chris Masone6dcd2d2008-07-17 12:53:50 -040010343 return __set_page_dirty_nobuffers(page);
10344}
10345
Al Viro10556cb22011-06-20 19:28:19 -040010346static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -050010347{
Li Zefanb83cc962010-12-20 16:04:08 +080010348 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +000010349 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +080010350
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +000010351 if (mask & MAY_WRITE &&
10352 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
10353 if (btrfs_root_readonly(root))
10354 return -EROFS;
10355 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
10356 return -EACCES;
10357 }
Al Viro2830ba72011-06-20 19:16:29 -040010358 return generic_permission(inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -050010359}
Chris Mason39279cc2007-06-12 06:35:45 -040010360
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010361static int btrfs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
10362{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010363 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010364 struct btrfs_trans_handle *trans;
10365 struct btrfs_root *root = BTRFS_I(dir)->root;
10366 struct inode *inode = NULL;
10367 u64 objectid;
10368 u64 index;
10369 int ret = 0;
10370
10371 /*
10372 * 5 units required for adding orphan entry
10373 */
10374 trans = btrfs_start_transaction(root, 5);
10375 if (IS_ERR(trans))
10376 return PTR_ERR(trans);
10377
10378 ret = btrfs_find_free_ino(root, &objectid);
10379 if (ret)
10380 goto out;
10381
10382 inode = btrfs_new_inode(trans, root, dir, NULL, 0,
David Sterbaf85b7372017-01-20 14:54:07 +010010383 btrfs_ino(BTRFS_I(dir)), objectid, mode, &index);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010384 if (IS_ERR(inode)) {
10385 ret = PTR_ERR(inode);
10386 inode = NULL;
10387 goto out;
10388 }
10389
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010390 inode->i_fop = &btrfs_file_operations;
10391 inode->i_op = &btrfs_file_inode_operations;
10392
10393 inode->i_mapping->a_ops = &btrfs_aops;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010394 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
10395
Chris Masonb0d5d102014-09-08 13:08:51 -070010396 ret = btrfs_init_inode_security(trans, inode, dir, NULL);
10397 if (ret)
10398 goto out_inode;
10399
10400 ret = btrfs_update_inode(trans, root, inode);
10401 if (ret)
10402 goto out_inode;
Nikolay Borisov73f2e542017-02-20 13:50:59 +020010403 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010404 if (ret)
Chris Masonb0d5d102014-09-08 13:08:51 -070010405 goto out_inode;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010406
Filipe Manana5762b5c2014-08-01 00:10:32 +010010407 /*
10408 * We set number of links to 0 in btrfs_new_inode(), and here we set
10409 * it to 1 because d_tmpfile() will issue a warning if the count is 0,
10410 * through:
10411 *
10412 * d_tmpfile() -> inode_dec_link_count() -> drop_nlink()
10413 */
10414 set_nlink(inode, 1);
Chris Masonb0d5d102014-09-08 13:08:51 -070010415 unlock_new_inode(inode);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010416 d_tmpfile(dentry, inode);
10417 mark_inode_dirty(inode);
10418
10419out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010420 btrfs_end_transaction(trans);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010421 if (ret)
10422 iput(inode);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010423 btrfs_btree_balance_dirty(fs_info);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010424 return ret;
Chris Masonb0d5d102014-09-08 13:08:51 -070010425
10426out_inode:
10427 unlock_new_inode(inode);
10428 goto out;
10429
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010430}
10431
David Sterba20a7db82017-02-17 16:24:29 +010010432__attribute__((const))
Liu Bo9d0d1c82017-03-24 15:04:50 -070010433static int btrfs_readpage_io_failed_hook(struct page *page, int failed_mirror)
David Sterba20a7db82017-02-17 16:24:29 +010010434{
Liu Bo9d0d1c82017-03-24 15:04:50 -070010435 return -EAGAIN;
David Sterba20a7db82017-02-17 16:24:29 +010010436}
10437
Josef Bacikc6100a42017-05-05 11:57:13 -040010438static void btrfs_check_extent_io_range(void *private_data, const char *caller,
10439 u64 start, u64 end)
10440{
10441 struct inode *inode = private_data;
10442 u64 isize;
10443
10444 isize = i_size_read(inode);
10445 if (end >= PAGE_SIZE && (end % 2) == 0 && end != isize - 1) {
10446 btrfs_debug_rl(BTRFS_I(inode)->root->fs_info,
10447 "%s: ino %llu isize %llu odd range [%llu,%llu]",
10448 caller, btrfs_ino(BTRFS_I(inode)), isize, start, end);
10449 }
10450}
10451
David Sterba5cdc84b2018-07-18 20:32:52 +020010452void btrfs_set_range_writeback(struct extent_io_tree *tree, u64 start, u64 end)
Josef Bacikc6100a42017-05-05 11:57:13 -040010453{
David Sterba5cdc84b2018-07-18 20:32:52 +020010454 struct inode *inode = tree->private_data;
Josef Bacikc6100a42017-05-05 11:57:13 -040010455 unsigned long index = start >> PAGE_SHIFT;
10456 unsigned long end_index = end >> PAGE_SHIFT;
10457 struct page *page;
10458
10459 while (index <= end_index) {
10460 page = find_get_page(inode->i_mapping, index);
10461 ASSERT(page); /* Pages should be in the extent_io_tree */
10462 set_page_writeback(page);
10463 put_page(page);
10464 index++;
10465 }
10466}
10467
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010468static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -050010469 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -040010470 .lookup = btrfs_lookup,
10471 .create = btrfs_create,
10472 .unlink = btrfs_unlink,
10473 .link = btrfs_link,
10474 .mkdir = btrfs_mkdir,
10475 .rmdir = btrfs_rmdir,
Miklos Szeredi2773bf02016-09-27 11:03:58 +020010476 .rename = btrfs_rename2,
Chris Mason39279cc2007-06-12 06:35:45 -040010477 .symlink = btrfs_symlink,
10478 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -040010479 .mknod = btrfs_mknod,
Josef Bacik5103e942007-11-16 11:45:54 -050010480 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010481 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010482 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010483 .set_acl = btrfs_set_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070010484 .update_time = btrfs_update_time,
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010485 .tmpfile = btrfs_tmpfile,
Chris Mason39279cc2007-06-12 06:35:45 -040010486};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010487static const struct inode_operations btrfs_dir_ro_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010488 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -050010489 .permission = btrfs_permission,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070010490 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010491};
Yan, Zheng76dda932009-09-21 16:00:26 -040010492
Alexey Dobriyan828c0952009-10-01 15:43:56 -070010493static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010494 .llseek = generic_file_llseek,
10495 .read = generic_read_dir,
Omar Sandoval02dbfc92016-05-20 13:50:33 -070010496 .iterate_shared = btrfs_real_readdir,
Josef Bacik23b5ec72017-07-24 15:14:25 -040010497 .open = btrfs_opendir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -040010498 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010499#ifdef CONFIG_COMPAT
Luke Dashjr4c63c242015-10-29 08:22:21 +000010500 .compat_ioctl = btrfs_compat_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010501#endif
Sage Weil6bf13c02008-06-10 10:07:39 -040010502 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -040010503 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -040010504};
10505
David Sterba20e55062015-11-19 11:42:28 +010010506static const struct extent_io_ops btrfs_extent_io_ops = {
David Sterba4d53ddd2017-02-17 15:27:44 +010010507 /* mandatory callbacks */
Chris Mason065631f2008-02-20 12:07:25 -050010508 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -040010509 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
Liu Bo9d0d1c82017-03-24 15:04:50 -070010510 .readpage_io_failed_hook = btrfs_readpage_io_failed_hook,
David Sterba4d53ddd2017-02-17 15:27:44 +010010511
10512 /* optional callbacks */
10513 .fill_delalloc = run_delalloc_range,
Chris Masone6dcd2d2008-07-17 12:53:50 -040010514 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -040010515 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -050010516 .set_bit_hook = btrfs_set_bit_hook,
10517 .clear_bit_hook = btrfs_clear_bit_hook,
Josef Bacik9ed74f22009-09-11 16:12:44 -040010518 .merge_extent_hook = btrfs_merge_extent_hook,
10519 .split_extent_hook = btrfs_split_extent_hook,
Josef Bacikc6100a42017-05-05 11:57:13 -040010520 .check_extent_io_range = btrfs_check_extent_io_range,
Chris Mason07157aa2007-08-30 08:50:51 -040010521};
10522
Chris Mason35054392009-01-21 13:11:13 -050010523/*
10524 * btrfs doesn't support the bmap operation because swapfiles
10525 * use bmap to make a mapping of extents in the file. They assume
10526 * these extents won't change over the life of the file and they
10527 * use the bmap result to do IO directly to the drive.
10528 *
10529 * the btrfs bmap call would return logical addresses that aren't
10530 * suitable for IO and they also will change frequently as COW
10531 * operations happen. So, swapfile + btrfs == corruption.
10532 *
10533 * For now we're avoiding this by dropping bmap.
10534 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -070010535static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -040010536 .readpage = btrfs_readpage,
10537 .writepage = btrfs_writepage,
Chris Masonb293f02e2007-11-01 19:45:34 -040010538 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -050010539 .readpages = btrfs_readpages,
Chris Mason16432982008-04-10 10:23:21 -040010540 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -040010541 .invalidatepage = btrfs_invalidatepage,
10542 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -040010543 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +020010544 .error_remove_page = generic_error_remove_page,
Chris Mason39279cc2007-06-12 06:35:45 -040010545};
10546
Alexey Dobriyan7f094102009-09-21 17:01:10 -070010547static const struct address_space_operations btrfs_symlink_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -040010548 .readpage = btrfs_readpage,
10549 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -040010550 .invalidatepage = btrfs_invalidatepage,
10551 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -040010552};
10553
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010554static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010555 .getattr = btrfs_getattr,
10556 .setattr = btrfs_setattr,
Josef Bacik5103e942007-11-16 11:45:54 -050010557 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010558 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -050010559 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010560 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010561 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010562 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010563};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010564static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -040010565 .getattr = btrfs_getattr,
10566 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010567 .permission = btrfs_permission,
Josef Bacik33268ea2008-07-24 12:16:36 -040010568 .listxattr = btrfs_listxattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010569 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010570 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010571 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -040010572};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010573static const struct inode_operations btrfs_symlink_inode_operations = {
Al Viro6b255392015-11-17 10:20:54 -050010574 .get_link = page_get_link,
Li Zefanf2095612010-11-19 02:05:24 +000010575 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -050010576 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010577 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -050010578 .listxattr = btrfs_listxattr,
Josef Bacike41f9412012-03-26 09:46:47 -040010579 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010580};
Yan, Zheng76dda932009-09-21 16:00:26 -040010581
Alexey Dobriyan82d339d2009-10-09 09:54:36 -040010582const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -040010583 .d_delete = btrfs_dentry_delete,
10584};