blob: 26b8bec7c2dcace79a2efeedf97ab86f2b957721 [file] [log] [blame]
David Sterbac1d7c512018-04-03 19:23:33 +02001// SPDX-License-Identifier: GPL-2.0
Chris Mason6cbd5572007-06-12 09:07:21 -04002/*
3 * Copyright (C) 2007 Oracle. All rights reserved.
Chris Mason6cbd5572007-06-12 09:07:21 -04004 */
5
Chris Mason8f18cf12008-04-25 16:53:30 -04006#include <linux/kernel.h>
Chris Mason065631f2008-02-20 12:07:25 -05007#include <linux/bio.h>
Chris Mason39279cc2007-06-12 06:35:45 -04008#include <linux/buffer_head.h>
Sage Weilf2eb0a22008-05-02 14:43:14 -04009#include <linux/file.h>
Chris Mason39279cc2007-06-12 06:35:45 -040010#include <linux/fs.h>
11#include <linux/pagemap.h>
12#include <linux/highmem.h>
13#include <linux/time.h>
14#include <linux/init.h>
15#include <linux/string.h>
Chris Mason39279cc2007-06-12 06:35:45 -040016#include <linux/backing-dev.h>
Chris Mason39279cc2007-06-12 06:35:45 -040017#include <linux/writeback.h>
Chris Mason39279cc2007-06-12 06:35:45 -040018#include <linux/compat.h>
Josef Bacik5103e942007-11-16 11:45:54 -050019#include <linux/xattr.h>
Josef Bacik33268ea2008-07-24 12:16:36 -040020#include <linux/posix_acl.h>
Yan Zhengd899e052008-10-30 14:25:28 -040021#include <linux/falloc.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090022#include <linux/slab.h>
David Sterba7a36dde2011-05-06 15:33:15 +020023#include <linux/ratelimit.h>
Filipe Brandenburger55e301f2013-01-29 06:04:50 +000024#include <linux/btrfs.h>
David Woodhouse53b381b2013-01-29 18:40:14 -050025#include <linux/blkdev.h>
Josef Bacikf23b5a52013-06-19 10:16:26 -040026#include <linux/posix_acl_xattr.h>
Christoph Hellwige2e40f22015-02-22 08:58:50 -080027#include <linux/uio.h>
Josef Bacik69fe2d72017-10-19 14:15:57 -040028#include <linux/magic.h>
Jeff Laytonae5e1652018-01-29 06:41:30 -050029#include <linux/iversion.h>
Omar Sandovaled46ff32016-11-03 10:28:14 -070030#include <linux/swap.h>
David Sterba92d32172018-04-16 21:10:14 +020031#include <asm/unaligned.h>
Chris Mason39279cc2007-06-12 06:35:45 -040032#include "ctree.h"
33#include "disk-io.h"
34#include "transaction.h"
35#include "btrfs_inode.h"
Chris Mason39279cc2007-06-12 06:35:45 -040036#include "print-tree.h"
Chris Masone6dcd2d2008-07-17 12:53:50 -040037#include "ordered-data.h"
Christoph Hellwig95819c02008-08-28 06:21:17 -040038#include "xattr.h"
Chris Masone02119d2008-09-05 16:13:11 -040039#include "tree-log.h"
Jan Schmidt4a54c8c2011-07-22 15:41:52 +020040#include "volumes.h"
Chris Masonc8b97812008-10-29 14:49:59 -040041#include "compression.h"
Chris Masonb4ce94d2009-02-04 09:25:08 -050042#include "locking.h"
Josef Bacikdc89e982011-01-28 17:05:48 -050043#include "free-space-cache.h"
Li Zefan581bb052011-04-20 10:06:11 +080044#include "inode-map.h"
Liu Bo38c227d2013-01-29 03:18:40 +000045#include "backref.h"
Filipe David Borba Manana63541922014-01-07 11:47:46 +000046#include "props.h"
Dongsheng Yang31193212014-12-12 16:44:35 +080047#include "qgroup.h"
Wang Xiaoguangdda32452016-07-11 11:05:29 +080048#include "dedupe.h"
Chris Mason39279cc2007-06-12 06:35:45 -040049
50struct btrfs_iget_args {
Chris Mason90d3e592014-01-09 17:28:00 -080051 struct btrfs_key *location;
Chris Mason39279cc2007-06-12 06:35:45 -040052 struct btrfs_root *root;
53};
54
Filipe Mananaf28a4922015-12-08 19:23:20 +000055struct btrfs_dio_data {
Filipe Mananaf28a4922015-12-08 19:23:20 +000056 u64 reserve;
57 u64 unsubmitted_oe_range_start;
58 u64 unsubmitted_oe_range_end;
Liu Bo4aaedfb2016-12-14 22:36:05 -080059 int overwrite;
Filipe Mananaf28a4922015-12-08 19:23:20 +000060};
61
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070062static const struct inode_operations btrfs_dir_inode_operations;
63static const struct inode_operations btrfs_symlink_inode_operations;
64static const struct inode_operations btrfs_dir_ro_inode_operations;
65static const struct inode_operations btrfs_special_inode_operations;
66static const struct inode_operations btrfs_file_inode_operations;
Alexey Dobriyan7f094102009-09-21 17:01:10 -070067static const struct address_space_operations btrfs_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
Nikolay Borisov5eaad972018-11-01 14:09:46 +0200113 * extent (btrfs_finish_ordered_io()). Also note that the caller of
114 * btrfs_run_delalloc_range already does proper cleanup for the first page of
Qu Wenruo524272602017-03-08 10:25:52 +0800115 * 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;
David Sterba600b6cf2018-11-22 17:16:44 +0100361 struct btrfs_fs_info *fs_info;
Chris Mason771ed682008-11-06 22:02:51 -0500362 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 */
Filipe Manana3527a012018-10-13 00:37:25 +0100506 nr_pages = 0;
Li Zefan560f7d72011-09-08 10:22:01 +0800507 goto cont;
508 }
Chris Mason179e29e2007-11-01 11:28:41 -0400509
David Sterbaeec63c62017-07-17 19:41:31 +0200510 if (BTRFS_I(inode)->defrag_compress)
511 compress_type = BTRFS_I(inode)->defrag_compress;
512 else if (BTRFS_I(inode)->prop_compress)
David Sterbab52aa8c2017-07-17 19:17:20 +0200513 compress_type = BTRFS_I(inode)->prop_compress;
Li Zefan261507a02010-12-17 14:21:50 +0800514
Chris Mason4adaa612013-03-26 13:07:00 -0400515 /*
516 * we need to call clear_page_dirty_for_io on each
517 * page in the range. Otherwise applications with the file
518 * mmap'd can wander in and change the page contents while
519 * we are compressing them.
520 *
521 * If the compression fails for any reason, we set the pages
522 * dirty again later on.
Timofey Titovetse9679de2017-10-24 01:29:48 +0300523 *
524 * Note that the remaining part is redirtied, the start pointer
525 * has moved, the end is the original one.
Chris Mason4adaa612013-03-26 13:07:00 -0400526 */
Timofey Titovetse9679de2017-10-24 01:29:48 +0300527 if (!redirty) {
528 extent_range_clear_dirty_for_io(inode, start, end);
529 redirty = 1;
530 }
David Sterbaf51d2b52017-09-15 17:36:57 +0200531
532 /* Compression level is applied here and only here */
533 ret = btrfs_compress_pages(
534 compress_type | (fs_info->compress_level << 4),
Li Zefan261507a02010-12-17 14:21:50 +0800535 inode->i_mapping, start,
David Sterba38c31462017-02-14 19:04:07 +0100536 pages,
David Sterba4d3a8002017-02-14 19:04:07 +0100537 &nr_pages,
Li Zefan261507a02010-12-17 14:21:50 +0800538 &total_in,
David Sterbae5d74902017-02-14 19:45:05 +0100539 &total_compressed);
Chris Masonc8b97812008-10-29 14:49:59 -0400540
541 if (!ret) {
542 unsigned long offset = total_compressed &
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300543 (PAGE_SIZE - 1);
David Sterba4d3a8002017-02-14 19:04:07 +0100544 struct page *page = pages[nr_pages - 1];
Chris Masonc8b97812008-10-29 14:49:59 -0400545 char *kaddr;
546
547 /* zero the tail end of the last page, we might be
548 * sending it down to disk
549 */
550 if (offset) {
Cong Wang7ac687d2011-11-25 23:14:28 +0800551 kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400552 memset(kaddr + offset, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300553 PAGE_SIZE - offset);
Cong Wang7ac687d2011-11-25 23:14:28 +0800554 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400555 }
556 will_compress = 1;
557 }
558 }
Li Zefan560f7d72011-09-08 10:22:01 +0800559cont:
Chris Masonc8b97812008-10-29 14:49:59 -0400560 if (start == 0) {
561 /* lets try to make an inline extent */
Timofey Titovets6018ba02017-09-15 01:57:26 +0300562 if (ret || total_in < actual_end) {
Chris Masonc8b97812008-10-29 14:49:59 -0400563 /* we didn't compress the entire range, try
Chris Mason771ed682008-11-06 22:02:51 -0500564 * to make an uncompressed inline extent.
Chris Masonc8b97812008-10-29 14:49:59 -0400565 */
Nikolay Borisovd02c0e22018-03-02 09:43:15 +0200566 ret = cow_file_range_inline(inode, start, end, 0,
567 BTRFS_COMPRESS_NONE, NULL);
Chris Masonc8b97812008-10-29 14:49:59 -0400568 } else {
Chris Mason771ed682008-11-06 22:02:51 -0500569 /* try making a compressed inline extent */
Nikolay Borisovd02c0e22018-03-02 09:43:15 +0200570 ret = cow_file_range_inline(inode, start, end,
Li Zefanfe3f5662011-03-28 08:30:38 +0000571 total_compressed,
572 compress_type, pages);
Chris Masonc8b97812008-10-29 14:49:59 -0400573 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100574 if (ret <= 0) {
Josef Bacik151a41b2013-07-29 13:22:24 -0400575 unsigned long clear_flags = EXTENT_DELALLOC |
Josef Bacik8b62f872017-10-19 14:15:55 -0400576 EXTENT_DELALLOC_NEW | EXTENT_DEFRAG |
577 EXTENT_DO_ACCOUNTING;
Filipe Mananae6eb4312014-10-10 10:45:12 +0100578 unsigned long page_error_op;
579
Filipe Mananae6eb4312014-10-10 10:45:12 +0100580 page_error_op = ret < 0 ? PAGE_SET_ERROR : 0;
Josef Bacik151a41b2013-07-29 13:22:24 -0400581
Chris Mason771ed682008-11-06 22:02:51 -0500582 /*
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100583 * inline extent creation worked or returned error,
584 * we don't need to create any more async work items.
585 * Unlock and free up our temp pages.
Josef Bacik8b62f872017-10-19 14:15:55 -0400586 *
587 * We use DO_ACCOUNTING here because we need the
588 * delalloc_release_metadata to be done _after_ we drop
589 * our outstanding extent for clearing delalloc for this
590 * range.
Chris Mason771ed682008-11-06 22:02:51 -0500591 */
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800592 extent_clear_unlock_delalloc(inode, start, end, end,
593 NULL, clear_flags,
594 PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -0400595 PAGE_CLEAR_DIRTY |
596 PAGE_SET_WRITEBACK |
Filipe Mananae6eb4312014-10-10 10:45:12 +0100597 page_error_op |
Josef Bacikc2790a22013-07-29 11:20:47 -0400598 PAGE_END_WRITEBACK);
Chris Masonc8b97812008-10-29 14:49:59 -0400599 goto free_pages_out;
600 }
601 }
602
603 if (will_compress) {
604 /*
605 * we aren't doing an inline extent round the compressed size
606 * up to a block size boundary so the allocator does sane
607 * things
608 */
Qu Wenruofda28322013-02-26 08:10:22 +0000609 total_compressed = ALIGN(total_compressed, blocksize);
Chris Masonc8b97812008-10-29 14:49:59 -0400610
611 /*
612 * one last check to make sure the compression is really a
Timofey Titovets170607e2017-06-06 14:41:15 +0300613 * win, compare the page count read with the blocks on disk,
614 * compression must free at least one sector size
Chris Masonc8b97812008-10-29 14:49:59 -0400615 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300616 total_in = ALIGN(total_in, PAGE_SIZE);
Timofey Titovets170607e2017-06-06 14:41:15 +0300617 if (total_compressed + blocksize <= total_in) {
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700618 *num_added += 1;
619
620 /*
621 * The async work queues will take care of doing actual
622 * allocation on disk for these compressed pages, and
623 * will submit them to the elevator.
624 */
Timofey Titovets11708622017-10-03 18:06:01 +0300625 add_async_extent(async_cow, start, total_in,
David Sterba4d3a8002017-02-14 19:04:07 +0100626 total_compressed, pages, nr_pages,
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700627 compress_type);
628
Timofey Titovets11708622017-10-03 18:06:01 +0300629 if (start + total_in < end) {
630 start += total_in;
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700631 pages = NULL;
632 cond_resched();
633 goto again;
634 }
635 return;
Chris Masonc8b97812008-10-29 14:49:59 -0400636 }
637 }
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700638 if (pages) {
Chris Masonc8b97812008-10-29 14:49:59 -0400639 /*
640 * the compression code ran but failed to make things smaller,
641 * free any pages it allocated and our page pointer array
642 */
David Sterba4d3a8002017-02-14 19:04:07 +0100643 for (i = 0; i < nr_pages; i++) {
Chris Mason70b99e62008-10-31 12:46:39 -0400644 WARN_ON(pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300645 put_page(pages[i]);
Chris Masonc8b97812008-10-29 14:49:59 -0400646 }
647 kfree(pages);
648 pages = NULL;
649 total_compressed = 0;
David Sterba4d3a8002017-02-14 19:04:07 +0100650 nr_pages = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400651
652 /* flag the file so we don't compress in the future */
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400653 if (!btrfs_test_opt(fs_info, FORCE_COMPRESS) &&
David Sterbab52aa8c2017-07-17 19:17:20 +0200654 !(BTRFS_I(inode)->prop_compress)) {
Chris Masona555f812010-01-28 16:18:15 -0500655 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
Chris Mason1e701a32010-03-11 09:42:04 -0500656 }
Chris Masonc8b97812008-10-29 14:49:59 -0400657 }
Chris Masonf03d9301f2009-02-04 09:31:06 -0500658cleanup_and_bail_uncompressed:
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700659 /*
660 * No compression, but we still need to write the pages in the file
661 * we've been given so far. redirty the locked page if it corresponds
662 * to our extent and set things up for the async work queue to run
663 * cow_file_range to do the normal delalloc dance.
664 */
665 if (page_offset(locked_page) >= start &&
666 page_offset(locked_page) <= end)
667 __set_page_dirty_nobuffers(locked_page);
668 /* unlocked later on in the async handlers */
669
670 if (redirty)
671 extent_range_redirty_for_io(inode, start, end);
672 add_async_extent(async_cow, start, end - start + 1, 0, NULL, 0,
673 BTRFS_COMPRESS_NONE);
674 *num_added += 1;
Chris Mason771ed682008-11-06 22:02:51 -0500675
Filipe Mananac44f6492014-10-09 21:15:44 +0100676 return;
Chris Mason771ed682008-11-06 22:02:51 -0500677
678free_pages_out:
David Sterba4d3a8002017-02-14 19:04:07 +0100679 for (i = 0; i < nr_pages; i++) {
Chris Mason771ed682008-11-06 22:02:51 -0500680 WARN_ON(pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300681 put_page(pages[i]);
Chris Mason771ed682008-11-06 22:02:51 -0500682 }
Chris Masond3977122009-01-05 21:25:51 -0500683 kfree(pages);
Chris Mason771ed682008-11-06 22:02:51 -0500684}
Chris Mason771ed682008-11-06 22:02:51 -0500685
Filipe Manana40ae8372014-10-06 22:14:24 +0100686static void free_async_extent_pages(struct async_extent *async_extent)
687{
688 int i;
689
690 if (!async_extent->pages)
691 return;
692
693 for (i = 0; i < async_extent->nr_pages; i++) {
694 WARN_ON(async_extent->pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300695 put_page(async_extent->pages[i]);
Filipe Manana40ae8372014-10-06 22:14:24 +0100696 }
697 kfree(async_extent->pages);
698 async_extent->nr_pages = 0;
699 async_extent->pages = NULL;
Chris Mason771ed682008-11-06 22:02:51 -0500700}
701
702/*
703 * phase two of compressed writeback. This is the ordered portion
704 * of the code, which only gets called in the order the work was
705 * queued. We walk all the async extents created by compress_file_range
706 * and send them down to the disk.
707 */
Filipe Mananadec8f172014-10-06 22:14:26 +0100708static noinline void submit_compressed_extents(struct inode *inode,
Chris Mason771ed682008-11-06 22:02:51 -0500709 struct async_cow *async_cow)
710{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400711 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason771ed682008-11-06 22:02:51 -0500712 struct async_extent *async_extent;
713 u64 alloc_hint = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500714 struct btrfs_key ins;
715 struct extent_map *em;
716 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason771ed682008-11-06 22:02:51 -0500717 struct extent_io_tree *io_tree;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500718 int ret = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500719
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500720again:
Chris Masond3977122009-01-05 21:25:51 -0500721 while (!list_empty(&async_cow->extents)) {
Chris Mason771ed682008-11-06 22:02:51 -0500722 async_extent = list_entry(async_cow->extents.next,
723 struct async_extent, list);
724 list_del(&async_extent->list);
725
726 io_tree = &BTRFS_I(inode)->io_tree;
727
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500728retry:
Chris Mason771ed682008-11-06 22:02:51 -0500729 /* did the compression code fall back to uncompressed IO? */
730 if (!async_extent->pages) {
731 int page_started = 0;
732 unsigned long nr_written = 0;
733
734 lock_extent(io_tree, async_extent->start,
Josef Bacik2ac55d42010-02-03 19:33:23 +0000735 async_extent->start +
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100736 async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500737
738 /* allocate blocks */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500739 ret = cow_file_range(inode, async_cow->locked_page,
740 async_extent->start,
741 async_extent->start +
742 async_extent->ram_size - 1,
Wang Xiaoguangdda32452016-07-11 11:05:29 +0800743 async_extent->start +
744 async_extent->ram_size - 1,
745 &page_started, &nr_written, 0,
746 NULL);
Chris Mason771ed682008-11-06 22:02:51 -0500747
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100748 /* JDM XXX */
749
Chris Mason771ed682008-11-06 22:02:51 -0500750 /*
751 * if page_started, cow_file_range inserted an
752 * inline extent and took care of all the unlocking
753 * and IO for us. Otherwise, we need to submit
754 * all those pages down to the drive.
755 */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500756 if (!page_started && !ret)
Nikolay Borisov5e3ee232017-12-08 15:55:58 +0200757 extent_write_locked_range(inode,
758 async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500759 async_extent->start +
Chris Mason771ed682008-11-06 22:02:51 -0500760 async_extent->ram_size - 1,
Chris Mason771ed682008-11-06 22:02:51 -0500761 WB_SYNC_ALL);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500762 else if (ret)
763 unlock_page(async_cow->locked_page);
Chris Mason771ed682008-11-06 22:02:51 -0500764 kfree(async_extent);
765 cond_resched();
766 continue;
767 }
768
769 lock_extent(io_tree, async_extent->start,
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100770 async_extent->start + async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500771
Wang Xiaoguang18513092016-07-25 15:51:40 +0800772 ret = btrfs_reserve_extent(root, async_extent->ram_size,
Chris Mason771ed682008-11-06 22:02:51 -0500773 async_extent->compressed_size,
774 async_extent->compressed_size,
Miao Xiee570fd22014-06-19 10:42:50 +0800775 0, alloc_hint, &ins, 1, 1);
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500776 if (ret) {
Filipe Manana40ae8372014-10-06 22:14:24 +0100777 free_async_extent_pages(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500778
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400779 if (ret == -ENOSPC) {
780 unlock_extent(io_tree, async_extent->start,
781 async_extent->start +
782 async_extent->ram_size - 1);
Liu Boce620032014-07-24 22:48:05 +0800783
784 /*
785 * we need to redirty the pages if we decide to
786 * fallback to uncompressed IO, otherwise we
787 * will not submit these pages down to lower
788 * layers.
789 */
790 extent_range_redirty_for_io(inode,
791 async_extent->start,
792 async_extent->start +
793 async_extent->ram_size - 1);
794
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100795 goto retry;
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400796 }
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500797 goto out_free;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500798 }
Yan, Zhengc2167752009-11-12 09:34:21 +0000799 /*
800 * here we're doing allocation and writeback of the
801 * compressed pages
802 */
Liu Bo6f9994d2017-01-31 07:50:22 -0800803 em = create_io_em(inode, async_extent->start,
804 async_extent->ram_size, /* len */
805 async_extent->start, /* orig_start */
806 ins.objectid, /* block_start */
807 ins.offset, /* block_len */
808 ins.offset, /* orig_block_len */
809 async_extent->ram_size, /* ram_bytes */
810 async_extent->compress_type,
811 BTRFS_ORDERED_COMPRESSED);
812 if (IS_ERR(em))
813 /* ret value is not necessary due to void function */
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500814 goto out_free_reserve;
Liu Bo6f9994d2017-01-31 07:50:22 -0800815 free_extent_map(em);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500816
Li Zefan261507a02010-12-17 14:21:50 +0800817 ret = btrfs_add_ordered_extent_compress(inode,
818 async_extent->start,
819 ins.objectid,
820 async_extent->ram_size,
821 ins.offset,
822 BTRFS_ORDERED_COMPRESSED,
823 async_extent->compress_type);
Filipe Mananad9f85962014-08-25 10:43:00 +0100824 if (ret) {
Nikolay Borisovdcdbc052017-02-20 13:50:45 +0200825 btrfs_drop_extent_cache(BTRFS_I(inode),
826 async_extent->start,
Filipe Mananad9f85962014-08-25 10:43:00 +0100827 async_extent->start +
828 async_extent->ram_size - 1, 0);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500829 goto out_free_reserve;
Filipe Mananad9f85962014-08-25 10:43:00 +0100830 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400831 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Chris Mason771ed682008-11-06 22:02:51 -0500832
Chris Mason771ed682008-11-06 22:02:51 -0500833 /*
834 * clear dirty, set writeback and unlock the pages.
835 */
Josef Bacikc2790a22013-07-29 11:20:47 -0400836 extent_clear_unlock_delalloc(inode, async_extent->start,
Chris Masona791e352009-10-08 11:27:10 -0400837 async_extent->start +
838 async_extent->ram_size - 1,
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800839 async_extent->start +
840 async_extent->ram_size - 1,
Josef Bacik151a41b2013-07-29 13:22:24 -0400841 NULL, EXTENT_LOCKED | EXTENT_DELALLOC,
842 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -0400843 PAGE_SET_WRITEBACK);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200844 if (btrfs_submit_compressed_write(inode,
Chris Masond3977122009-01-05 21:25:51 -0500845 async_extent->start,
846 async_extent->ram_size,
847 ins.objectid,
848 ins.offset, async_extent->pages,
Liu Bof82b7352017-10-23 23:18:16 -0600849 async_extent->nr_pages,
850 async_cow->write_flags)) {
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100851 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;
Nikolay Borisovc6297322018-11-08 10:18:08 +0200856 btrfs_writepage_endio_finish_ordered(p, start, end, 0);
Nikolay Borisov7087a9d2018-11-01 14:09:48 +0200857
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100858 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;
Chris Mason771ed682008-11-06 22:02:51 -05001147 unsigned long nr_pages;
1148
1149 async_cow = container_of(work, struct async_cow, work);
1150
David Sterba600b6cf2018-11-22 17:16:44 +01001151 fs_info = async_cow->fs_info;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001152 nr_pages = (async_cow->end - async_cow->start + PAGE_SIZE) >>
1153 PAGE_SHIFT;
Chris Mason771ed682008-11-06 22:02:51 -05001154
David Sterba093258e2018-02-26 16:15:17 +01001155 /* atomic_sub_return implies a barrier */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001156 if (atomic_sub_return(nr_pages, &fs_info->async_delalloc_pages) <
David Sterba093258e2018-02-26 16:15:17 +01001157 5 * SZ_1M)
1158 cond_wake_up_nomb(&fs_info->async_submit_wait);
Chris Mason771ed682008-11-06 22:02:51 -05001159
Chris Masond3977122009-01-05 21:25:51 -05001160 if (async_cow->inode)
Chris Mason771ed682008-11-06 22:02:51 -05001161 submit_compressed_extents(async_cow->inode, async_cow);
Chris Mason771ed682008-11-06 22:02:51 -05001162}
Chris Masonc8b97812008-10-29 14:49:59 -04001163
Chris Mason771ed682008-11-06 22:02:51 -05001164static noinline void async_cow_free(struct btrfs_work *work)
1165{
1166 struct async_cow *async_cow;
1167 async_cow = container_of(work, struct async_cow, work);
Josef Bacik8180ef82012-06-08 15:16:12 -04001168 if (async_cow->inode)
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001169 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001170 kfree(async_cow);
1171}
1172
1173static int cow_file_range_async(struct inode *inode, struct page *locked_page,
1174 u64 start, u64 end, int *page_started,
Liu Bof82b7352017-10-23 23:18:16 -06001175 unsigned long *nr_written,
1176 unsigned int write_flags)
Chris Mason771ed682008-11-06 22:02:51 -05001177{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001178 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason771ed682008-11-06 22:02:51 -05001179 struct async_cow *async_cow;
Chris Mason771ed682008-11-06 22:02:51 -05001180 unsigned long nr_pages;
1181 u64 cur_end;
Chris Mason771ed682008-11-06 22:02:51 -05001182
Chris Masona3429ab2009-10-08 12:30:20 -04001183 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED,
David Sterbaae0f1622017-10-31 16:37:52 +01001184 1, 0, NULL);
Chris Masond3977122009-01-05 21:25:51 -05001185 while (start < end) {
Chris Mason771ed682008-11-06 22:02:51 -05001186 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001187 BUG_ON(!async_cow); /* -ENOMEM */
Josef Bacik8180ef82012-06-08 15:16:12 -04001188 async_cow->inode = igrab(inode);
David Sterba600b6cf2018-11-22 17:16:44 +01001189 async_cow->fs_info = fs_info;
Chris Mason771ed682008-11-06 22:02:51 -05001190 async_cow->locked_page = locked_page;
1191 async_cow->start = start;
Liu Bof82b7352017-10-23 23:18:16 -06001192 async_cow->write_flags = write_flags;
Chris Mason771ed682008-11-06 22:02:51 -05001193
Wang Shilongf79707b2014-07-17 11:44:09 +08001194 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001195 !btrfs_test_opt(fs_info, FORCE_COMPRESS))
Chris Mason771ed682008-11-06 22:02:51 -05001196 cur_end = end;
1197 else
Byongho Leeee221842015-12-15 01:42:10 +09001198 cur_end = min(end, start + SZ_512K - 1);
Chris Mason771ed682008-11-06 22:02:51 -05001199
1200 async_cow->end = cur_end;
1201 INIT_LIST_HEAD(&async_cow->extents);
1202
Liu Bo9e0af232014-08-15 23:36:53 +08001203 btrfs_init_work(&async_cow->work,
1204 btrfs_delalloc_helper,
1205 async_cow_start, async_cow_submit,
1206 async_cow_free);
Chris Mason771ed682008-11-06 22:02:51 -05001207
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001208 nr_pages = (cur_end - start + PAGE_SIZE) >>
1209 PAGE_SHIFT;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001210 atomic_add(nr_pages, &fs_info->async_delalloc_pages);
Chris Mason771ed682008-11-06 22:02:51 -05001211
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001212 btrfs_queue_work(fs_info->delalloc_workers, &async_cow->work);
Chris Mason771ed682008-11-06 22:02:51 -05001213
Chris Mason771ed682008-11-06 22:02:51 -05001214 *nr_written += nr_pages;
1215 start = cur_end + 1;
1216 }
1217 *page_started = 1;
1218 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001219}
1220
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001221static noinline int csum_exist_in_range(struct btrfs_fs_info *fs_info,
Yan Zheng17d217f2008-12-12 10:03:38 -05001222 u64 bytenr, u64 num_bytes)
1223{
1224 int ret;
1225 struct btrfs_ordered_sum *sums;
1226 LIST_HEAD(list);
1227
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001228 ret = btrfs_lookup_csums_range(fs_info->csum_root, bytenr,
Arne Jansena2de7332011-03-08 14:14:00 +01001229 bytenr + num_bytes - 1, &list, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001230 if (ret == 0 && list_empty(&list))
1231 return 0;
1232
1233 while (!list_empty(&list)) {
1234 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1235 list_del(&sums->list);
1236 kfree(sums);
1237 }
Liu Bo58113752018-01-31 17:09:13 -07001238 if (ret < 0)
1239 return ret;
Yan Zheng17d217f2008-12-12 10:03:38 -05001240 return 1;
1241}
1242
Chris Masond352ac62008-09-29 15:18:18 -04001243/*
1244 * when nowcow writeback call back. This checks for snapshots or COW copies
1245 * of the extents that exist in the file, and COWs the file as required.
1246 *
1247 * If no cow copies or snapshots exist, we write directly to the existing
1248 * blocks on disk
1249 */
Chris Mason7f366cf2009-03-12 20:12:45 -04001250static noinline int run_delalloc_nocow(struct inode *inode,
1251 struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001252 u64 start, u64 end, int *page_started, int force,
1253 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001254{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001255 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masonbe20aa92007-12-17 20:14:01 -05001256 struct btrfs_root *root = BTRFS_I(inode)->root;
1257 struct extent_buffer *leaf;
Chris Masonbe20aa92007-12-17 20:14:01 -05001258 struct btrfs_path *path;
Yan Zheng80ff3852008-10-30 14:20:02 -04001259 struct btrfs_file_extent_item *fi;
Chris Masonbe20aa92007-12-17 20:14:01 -05001260 struct btrfs_key found_key;
Liu Bo6f9994d2017-01-31 07:50:22 -08001261 struct extent_map *em;
Yan Zheng80ff3852008-10-30 14:20:02 -04001262 u64 cow_start;
1263 u64 cur_offset;
1264 u64 extent_end;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001265 u64 extent_offset;
Yan Zheng80ff3852008-10-30 14:20:02 -04001266 u64 disk_bytenr;
1267 u64 num_bytes;
Josef Bacikb4939682012-12-03 10:31:19 -05001268 u64 disk_num_bytes;
Josef Bacikcc95bef2013-04-04 14:31:27 -04001269 u64 ram_bytes;
Yan Zheng80ff3852008-10-30 14:20:02 -04001270 int extent_type;
Robbie Ko8ecebf4d2018-08-06 10:30:30 +08001271 int ret;
Yan Zhengd899e052008-10-30 14:25:28 -04001272 int type;
Yan Zheng80ff3852008-10-30 14:20:02 -04001273 int nocow;
1274 int check_prev = 1;
Li Zefan82d59022011-04-20 10:33:24 +08001275 bool nolock;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02001276 u64 ino = btrfs_ino(BTRFS_I(inode));
Chris Masonbe20aa92007-12-17 20:14:01 -05001277
1278 path = btrfs_alloc_path();
Josef Bacik17ca04a2012-05-31 15:58:55 -04001279 if (!path) {
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001280 extent_clear_unlock_delalloc(inode, start, end, end,
1281 locked_page,
Josef Bacikc2790a22013-07-29 11:20:47 -04001282 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -04001283 EXTENT_DO_ACCOUNTING |
1284 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001285 PAGE_CLEAR_DIRTY |
1286 PAGE_SET_WRITEBACK |
1287 PAGE_END_WRITEBACK);
Mark Fashehd8926bb2011-07-13 10:38:47 -07001288 return -ENOMEM;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001289 }
Li Zefan82d59022011-04-20 10:33:24 +08001290
Nikolay Borisov70ddc552017-02-20 13:50:35 +02001291 nolock = btrfs_is_free_space_inode(BTRFS_I(inode));
Li Zefan82d59022011-04-20 10:33:24 +08001292
Yan Zheng80ff3852008-10-30 14:20:02 -04001293 cow_start = (u64)-1;
1294 cur_offset = start;
1295 while (1) {
Liu Boe4c3b2d2017-01-30 12:25:28 -08001296 ret = btrfs_lookup_file_extent(NULL, root, path, ino,
Yan Zheng80ff3852008-10-30 14:20:02 -04001297 cur_offset, 0);
Josef Bacikd788a342013-10-25 16:55:08 -04001298 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001299 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001300 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1301 leaf = path->nodes[0];
1302 btrfs_item_key_to_cpu(leaf, &found_key,
1303 path->slots[0] - 1);
Li Zefan33345d012011-04-20 10:31:50 +08001304 if (found_key.objectid == ino &&
Yan Zheng80ff3852008-10-30 14:20:02 -04001305 found_key.type == BTRFS_EXTENT_DATA_KEY)
1306 path->slots[0]--;
1307 }
1308 check_prev = 0;
1309next_slot:
1310 leaf = path->nodes[0];
1311 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1312 ret = btrfs_next_leaf(root, path);
Liu Boe8916692018-01-25 11:02:50 -07001313 if (ret < 0) {
1314 if (cow_start != (u64)-1)
1315 cur_offset = cow_start;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001316 goto error;
Liu Boe8916692018-01-25 11:02:50 -07001317 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001318 if (ret > 0)
1319 break;
1320 leaf = path->nodes[0];
1321 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001322
Yan Zheng80ff3852008-10-30 14:20:02 -04001323 nocow = 0;
1324 disk_bytenr = 0;
Yan Zheng17d217f2008-12-12 10:03:38 -05001325 num_bytes = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001326 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001327
Filipe Manana1d512cb2015-11-09 00:33:58 +00001328 if (found_key.objectid > ino)
1329 break;
1330 if (WARN_ON_ONCE(found_key.objectid < ino) ||
1331 found_key.type < BTRFS_EXTENT_DATA_KEY) {
1332 path->slots[0]++;
1333 goto next_slot;
1334 }
1335 if (found_key.type > BTRFS_EXTENT_DATA_KEY ||
Yan Zheng80ff3852008-10-30 14:20:02 -04001336 found_key.offset > end)
1337 break;
Chris Masonbe20aa92007-12-17 20:14:01 -05001338
Yan Zheng80ff3852008-10-30 14:20:02 -04001339 if (found_key.offset > cur_offset) {
1340 extent_end = found_key.offset;
Chris Masone9061e22009-10-09 09:57:45 -04001341 extent_type = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001342 goto out_check;
1343 }
Chris Masonc31f8832008-01-08 15:46:31 -05001344
Yan Zheng80ff3852008-10-30 14:20:02 -04001345 fi = btrfs_item_ptr(leaf, path->slots[0],
1346 struct btrfs_file_extent_item);
1347 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001348
Josef Bacikcc95bef2013-04-04 14:31:27 -04001349 ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
Yan Zhengd899e052008-10-30 14:25:28 -04001350 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1351 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001352 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001353 extent_offset = btrfs_file_extent_offset(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001354 extent_end = found_key.offset +
1355 btrfs_file_extent_num_bytes(leaf, fi);
Josef Bacikb4939682012-12-03 10:31:19 -05001356 disk_num_bytes =
1357 btrfs_file_extent_disk_num_bytes(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001358 if (extent_end <= start) {
1359 path->slots[0]++;
1360 goto next_slot;
1361 }
Yan Zheng17d217f2008-12-12 10:03:38 -05001362 if (disk_bytenr == 0)
1363 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001364 if (btrfs_file_extent_compression(leaf, fi) ||
1365 btrfs_file_extent_encryption(leaf, fi) ||
1366 btrfs_file_extent_other_encoding(leaf, fi))
1367 goto out_check;
Ethan Lien78d42952018-05-17 14:58:29 +08001368 /*
1369 * Do the same check as in btrfs_cross_ref_exist but
1370 * without the unnecessary search.
1371 */
1372 if (btrfs_file_extent_generation(leaf, fi) <=
1373 btrfs_root_last_snapshot(&root->root_item))
1374 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001375 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1376 goto out_check;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001377 if (btrfs_extent_readonly(fs_info, disk_bytenr))
Yan Zheng80ff3852008-10-30 14:20:02 -04001378 goto out_check;
Liu Bo58113752018-01-31 17:09:13 -07001379 ret = btrfs_cross_ref_exist(root, ino,
1380 found_key.offset -
1381 extent_offset, disk_bytenr);
1382 if (ret) {
1383 /*
1384 * ret could be -EIO if the above fails to read
1385 * metadata.
1386 */
1387 if (ret < 0) {
1388 if (cow_start != (u64)-1)
1389 cur_offset = cow_start;
1390 goto error;
1391 }
1392
1393 WARN_ON_ONCE(nolock);
Yan Zheng17d217f2008-12-12 10:03:38 -05001394 goto out_check;
Liu Bo58113752018-01-31 17:09:13 -07001395 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001396 disk_bytenr += extent_offset;
Yan Zheng17d217f2008-12-12 10:03:38 -05001397 disk_bytenr += cur_offset - found_key.offset;
1398 num_bytes = min(end + 1, extent_end) - cur_offset;
1399 /*
Wang Shilonge9894fd2014-03-27 11:12:25 +08001400 * if there are pending snapshots for this root,
1401 * we fall into common COW way.
1402 */
Robbie Ko8ecebf4d2018-08-06 10:30:30 +08001403 if (!nolock && atomic_read(&root->snapshot_force_cow))
1404 goto out_check;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001405 /*
Yan Zheng17d217f2008-12-12 10:03:38 -05001406 * force cow if csum exists in the range.
1407 * this ensure that csum for a given extent are
1408 * either valid or do not exist.
1409 */
Liu Bo58113752018-01-31 17:09:13 -07001410 ret = csum_exist_in_range(fs_info, disk_bytenr,
1411 num_bytes);
1412 if (ret) {
Liu Bo58113752018-01-31 17:09:13 -07001413 /*
1414 * ret could be -EIO if the above fails to read
1415 * metadata.
1416 */
1417 if (ret < 0) {
1418 if (cow_start != (u64)-1)
1419 cur_offset = cow_start;
1420 goto error;
1421 }
1422 WARN_ON_ONCE(nolock);
Yan Zheng17d217f2008-12-12 10:03:38 -05001423 goto out_check;
Robbie Ko91e1f562016-10-07 10:01:29 +08001424 }
Robbie Ko8ecebf4d2018-08-06 10:30:30 +08001425 if (!btrfs_inc_nocow_writers(fs_info, disk_bytenr))
Filipe Mananaf78c4362016-05-09 13:15:41 +01001426 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001427 nocow = 1;
1428 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1429 extent_end = found_key.offset +
Qu Wenruoe41ca582018-06-06 15:41:49 +08001430 btrfs_file_extent_ram_bytes(leaf, fi);
Jeff Mahoneyda170662016-06-15 09:22:56 -04001431 extent_end = ALIGN(extent_end,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001432 fs_info->sectorsize);
Yan Zheng80ff3852008-10-30 14:20:02 -04001433 } else {
1434 BUG_ON(1);
1435 }
1436out_check:
1437 if (extent_end <= start) {
1438 path->slots[0]++;
Filipe Mananaf78c4362016-05-09 13:15:41 +01001439 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001440 btrfs_dec_nocow_writers(fs_info, disk_bytenr);
Yan Zheng80ff3852008-10-30 14:20:02 -04001441 goto next_slot;
1442 }
1443 if (!nocow) {
1444 if (cow_start == (u64)-1)
1445 cow_start = cur_offset;
1446 cur_offset = extent_end;
1447 if (cur_offset > end)
1448 break;
1449 path->slots[0]++;
1450 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001451 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001452
David Sterbab3b4aa72011-04-21 01:20:15 +02001453 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001454 if (cow_start != (u64)-1) {
Josef Bacik00361582013-08-14 14:02:47 -04001455 ret = cow_file_range(inode, locked_page,
1456 cow_start, found_key.offset - 1,
Wang Xiaoguangdda32452016-07-11 11:05:29 +08001457 end, page_started, nr_written, 1,
1458 NULL);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001459 if (ret) {
Filipe Mananaf78c4362016-05-09 13:15:41 +01001460 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001461 btrfs_dec_nocow_writers(fs_info,
Filipe Mananaf78c4362016-05-09 13:15:41 +01001462 disk_bytenr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001463 goto error;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001464 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001465 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001466 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001467
Yan Zhengd899e052008-10-30 14:25:28 -04001468 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Liu Bo6f9994d2017-01-31 07:50:22 -08001469 u64 orig_start = found_key.offset - extent_offset;
1470
1471 em = create_io_em(inode, cur_offset, num_bytes,
1472 orig_start,
1473 disk_bytenr, /* block_start */
1474 num_bytes, /* block_len */
1475 disk_num_bytes, /* orig_block_len */
1476 ram_bytes, BTRFS_COMPRESS_NONE,
1477 BTRFS_ORDERED_PREALLOC);
1478 if (IS_ERR(em)) {
Liu Bo6f9994d2017-01-31 07:50:22 -08001479 if (nocow)
1480 btrfs_dec_nocow_writers(fs_info,
1481 disk_bytenr);
1482 ret = PTR_ERR(em);
1483 goto error;
Yan Zhengd899e052008-10-30 14:25:28 -04001484 }
Liu Bo6f9994d2017-01-31 07:50:22 -08001485 free_extent_map(em);
1486 }
1487
1488 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zhengd899e052008-10-30 14:25:28 -04001489 type = BTRFS_ORDERED_PREALLOC;
1490 } else {
1491 type = BTRFS_ORDERED_NOCOW;
1492 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001493
1494 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
Yan Zhengd899e052008-10-30 14:25:28 -04001495 num_bytes, num_bytes, type);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001496 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001497 btrfs_dec_nocow_writers(fs_info, disk_bytenr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001498 BUG_ON(ret); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -05001499
Yan, Zhengefa56462010-05-16 10:49:59 -04001500 if (root->root_key.objectid ==
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001501 BTRFS_DATA_RELOC_TREE_OBJECTID)
1502 /*
1503 * Error handled later, as we must prevent
1504 * extent_clear_unlock_delalloc() in error handler
1505 * from freeing metadata of created ordered extent.
1506 */
Yan, Zhengefa56462010-05-16 10:49:59 -04001507 ret = btrfs_reloc_clone_csums(inode, cur_offset,
1508 num_bytes);
Yan, Zhengefa56462010-05-16 10:49:59 -04001509
Josef Bacikc2790a22013-07-29 11:20:47 -04001510 extent_clear_unlock_delalloc(inode, cur_offset,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001511 cur_offset + num_bytes - 1, end,
Josef Bacikc2790a22013-07-29 11:20:47 -04001512 locked_page, EXTENT_LOCKED |
Wang Xiaoguang18513092016-07-25 15:51:40 +08001513 EXTENT_DELALLOC |
1514 EXTENT_CLEAR_DATA_RESV,
1515 PAGE_UNLOCK | PAGE_SET_PRIVATE2);
1516
Yan Zheng80ff3852008-10-30 14:20:02 -04001517 cur_offset = extent_end;
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001518
1519 /*
1520 * btrfs_reloc_clone_csums() error, now we're OK to call error
1521 * handler, as metadata for created ordered extent will only
1522 * be freed by btrfs_finish_ordered_io().
1523 */
1524 if (ret)
1525 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001526 if (cur_offset > end)
1527 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001528 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001529 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001530
Robbie Ko506481b2018-10-30 18:04:04 +08001531 if (cur_offset <= end && cow_start == (u64)-1)
Yan Zheng80ff3852008-10-30 14:20:02 -04001532 cow_start = cur_offset;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001533
Yan Zheng80ff3852008-10-30 14:20:02 -04001534 if (cow_start != (u64)-1) {
Robbie Ko506481b2018-10-30 18:04:04 +08001535 cur_offset = end;
Wang Xiaoguangdda32452016-07-11 11:05:29 +08001536 ret = cow_file_range(inode, locked_page, cow_start, end, end,
1537 page_started, nr_written, 1, NULL);
Josef Bacikd788a342013-10-25 16:55:08 -04001538 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001539 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001540 }
1541
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001542error:
Josef Bacik17ca04a2012-05-31 15:58:55 -04001543 if (ret && cur_offset < end)
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001544 extent_clear_unlock_delalloc(inode, cur_offset, end, end,
Josef Bacikc2790a22013-07-29 11:20:47 -04001545 locked_page, EXTENT_LOCKED |
Josef Bacik151a41b2013-07-29 13:22:24 -04001546 EXTENT_DELALLOC | EXTENT_DEFRAG |
1547 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
1548 PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -04001549 PAGE_SET_WRITEBACK |
1550 PAGE_END_WRITEBACK);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001551 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001552 return ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001553}
1554
Wang Shilong47059d92014-07-03 18:22:07 +08001555static inline int need_force_cow(struct inode *inode, u64 start, u64 end)
1556{
1557
1558 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
1559 !(BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC))
1560 return 0;
1561
1562 /*
1563 * @defrag_bytes is a hint value, no spinlock held here,
1564 * if is not zero, it means the file is defragging.
1565 * Force cow if given extent needs to be defragged.
1566 */
1567 if (BTRFS_I(inode)->defrag_bytes &&
1568 test_range_bit(&BTRFS_I(inode)->io_tree, start, end,
1569 EXTENT_DEFRAG, 0, NULL))
1570 return 1;
1571
1572 return 0;
1573}
1574
Chris Masond352ac62008-09-29 15:18:18 -04001575/*
Nikolay Borisov5eaad972018-11-01 14:09:46 +02001576 * Function to process delayed allocation (create CoW) for ranges which are
1577 * being touched for the first time.
Chris Masond352ac62008-09-29 15:18:18 -04001578 */
Nikolay Borisov5eaad972018-11-01 14:09:46 +02001579int btrfs_run_delalloc_range(void *private_data, struct page *locked_page,
1580 u64 start, u64 end, int *page_started, unsigned long *nr_written,
1581 struct writeback_control *wbc)
Chris Masonbe20aa92007-12-17 20:14:01 -05001582{
Josef Bacikc6100a42017-05-05 11:57:13 -04001583 struct inode *inode = private_data;
Chris Masonbe20aa92007-12-17 20:14:01 -05001584 int ret;
Wang Shilong47059d92014-07-03 18:22:07 +08001585 int force_cow = need_force_cow(inode, start, end);
Liu Bof82b7352017-10-23 23:18:16 -06001586 unsigned int write_flags = wbc_to_write_flags(wbc);
Chris Masona2135012008-06-25 16:01:30 -04001587
Wang Shilong47059d92014-07-03 18:22:07 +08001588 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW && !force_cow) {
Chris Masonc8b97812008-10-29 14:49:59 -04001589 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001590 page_started, 1, nr_written);
Wang Shilong47059d92014-07-03 18:22:07 +08001591 } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC && !force_cow) {
Yan Zhengd899e052008-10-30 14:25:28 -04001592 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001593 page_started, 0, nr_written);
Timofey Titovetsc2fcdcd2017-07-17 16:52:58 +03001594 } else if (!inode_need_compress(inode, start, end)) {
Wang Xiaoguangdda32452016-07-11 11:05:29 +08001595 ret = cow_file_range(inode, locked_page, start, end, end,
1596 page_started, nr_written, 1, NULL);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001597 } else {
1598 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1599 &BTRFS_I(inode)->runtime_flags);
Chris Mason771ed682008-11-06 22:02:51 -05001600 ret = cow_file_range_async(inode, locked_page, start, end,
Liu Bof82b7352017-10-23 23:18:16 -06001601 page_started, nr_written,
1602 write_flags);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001603 }
Qu Wenruo524272602017-03-08 10:25:52 +08001604 if (ret)
1605 btrfs_cleanup_ordered_extents(inode, start, end - start + 1);
Chris Masonb888db22007-08-27 16:49:44 -04001606 return ret;
1607}
1608
Nikolay Borisovabbb55f2018-11-01 14:09:53 +02001609void btrfs_split_delalloc_extent(struct inode *inode,
1610 struct extent_state *orig, u64 split)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001611{
Josef Bacikdcab6a32015-02-11 15:08:59 -05001612 u64 size;
1613
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001614 /* not delalloc, ignore it */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001615 if (!(orig->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001616 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001617
Josef Bacikdcab6a32015-02-11 15:08:59 -05001618 size = orig->end - orig->start + 1;
1619 if (size > BTRFS_MAX_EXTENT_SIZE) {
David Sterba823bb202017-01-04 11:09:51 +01001620 u32 num_extents;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001621 u64 new_size;
1622
1623 /*
Nikolay Borisov5c848192018-11-01 14:09:52 +02001624 * See the explanation in btrfs_merge_delalloc_extent, the same
Josef Bacikba117212015-03-13 15:01:24 -04001625 * applies here, just in reverse.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001626 */
1627 new_size = orig->end - split + 1;
David Sterba823bb202017-01-04 11:09:51 +01001628 num_extents = count_max_extents(new_size);
Josef Bacikba117212015-03-13 15:01:24 -04001629 new_size = split - orig->start;
David Sterba823bb202017-01-04 11:09:51 +01001630 num_extents += count_max_extents(new_size);
1631 if (count_max_extents(size) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001632 return;
1633 }
1634
Josef Bacik9e0baf62011-07-15 15:16:44 +00001635 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04001636 btrfs_mod_outstanding_extents(BTRFS_I(inode), 1);
Josef Bacik9e0baf62011-07-15 15:16:44 +00001637 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001638}
1639
1640/*
Nikolay Borisov5c848192018-11-01 14:09:52 +02001641 * Handle merged delayed allocation extents so we can keep track of new extents
1642 * that are just merged onto old extents, such as when we are doing sequential
1643 * writes, so we can properly account for the metadata space we'll need.
Josef Bacik9ed74f22009-09-11 16:12:44 -04001644 */
Nikolay Borisov5c848192018-11-01 14:09:52 +02001645void btrfs_merge_delalloc_extent(struct inode *inode, struct extent_state *new,
1646 struct extent_state *other)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001647{
Josef Bacikdcab6a32015-02-11 15:08:59 -05001648 u64 new_size, old_size;
David Sterba823bb202017-01-04 11:09:51 +01001649 u32 num_extents;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001650
Josef Bacik9ed74f22009-09-11 16:12:44 -04001651 /* not delalloc, ignore it */
1652 if (!(other->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001653 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001654
Josef Bacik8461a3d2015-03-13 15:12:08 -04001655 if (new->start > other->start)
1656 new_size = new->end - other->start + 1;
1657 else
1658 new_size = other->end - new->start + 1;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001659
1660 /* we're not bigger than the max, unreserve the space and go */
1661 if (new_size <= BTRFS_MAX_EXTENT_SIZE) {
1662 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04001663 btrfs_mod_outstanding_extents(BTRFS_I(inode), -1);
Josef Bacikdcab6a32015-02-11 15:08:59 -05001664 spin_unlock(&BTRFS_I(inode)->lock);
1665 return;
1666 }
1667
1668 /*
Josef Bacikba117212015-03-13 15:01:24 -04001669 * We have to add up either side to figure out how many extents were
1670 * accounted for before we merged into one big extent. If the number of
1671 * extents we accounted for is <= the amount we need for the new range
1672 * then we can return, otherwise drop. Think of it like this
1673 *
1674 * [ 4k][MAX_SIZE]
1675 *
1676 * So we've grown the extent by a MAX_SIZE extent, this would mean we
1677 * need 2 outstanding extents, on one side we have 1 and the other side
1678 * we have 1 so they are == and we can return. But in this case
1679 *
1680 * [MAX_SIZE+4k][MAX_SIZE+4k]
1681 *
1682 * Each range on their own accounts for 2 extents, but merged together
1683 * they are only 3 extents worth of accounting, so we need to drop in
1684 * this case.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001685 */
Josef Bacikba117212015-03-13 15:01:24 -04001686 old_size = other->end - other->start + 1;
David Sterba823bb202017-01-04 11:09:51 +01001687 num_extents = count_max_extents(old_size);
Josef Bacikba117212015-03-13 15:01:24 -04001688 old_size = new->end - new->start + 1;
David Sterba823bb202017-01-04 11:09:51 +01001689 num_extents += count_max_extents(old_size);
1690 if (count_max_extents(new_size) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001691 return;
1692
Josef Bacik9e0baf62011-07-15 15:16:44 +00001693 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04001694 btrfs_mod_outstanding_extents(BTRFS_I(inode), -1);
Josef Bacik9e0baf62011-07-15 15:16:44 +00001695 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001696}
1697
Miao Xieeb73c1b2013-05-15 07:48:22 +00001698static void btrfs_add_delalloc_inodes(struct btrfs_root *root,
1699 struct inode *inode)
1700{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001701 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1702
Miao Xieeb73c1b2013-05-15 07:48:22 +00001703 spin_lock(&root->delalloc_lock);
1704 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1705 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1706 &root->delalloc_inodes);
1707 set_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1708 &BTRFS_I(inode)->runtime_flags);
1709 root->nr_delalloc_inodes++;
1710 if (root->nr_delalloc_inodes == 1) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001711 spin_lock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001712 BUG_ON(!list_empty(&root->delalloc_root));
1713 list_add_tail(&root->delalloc_root,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001714 &fs_info->delalloc_roots);
1715 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001716 }
1717 }
1718 spin_unlock(&root->delalloc_lock);
1719}
1720
Nikolay Borisov2b877332018-04-27 12:21:51 +03001721
1722void __btrfs_del_delalloc_inode(struct btrfs_root *root,
1723 struct btrfs_inode *inode)
Miao Xieeb73c1b2013-05-15 07:48:22 +00001724{
David Sterba3ffbd682018-06-29 10:56:42 +02001725 struct btrfs_fs_info *fs_info = root->fs_info;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001726
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001727 if (!list_empty(&inode->delalloc_inodes)) {
1728 list_del_init(&inode->delalloc_inodes);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001729 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001730 &inode->runtime_flags);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001731 root->nr_delalloc_inodes--;
1732 if (!root->nr_delalloc_inodes) {
Nikolay Borisov7c8a0d32018-04-27 12:21:52 +03001733 ASSERT(list_empty(&root->delalloc_inodes));
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001734 spin_lock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001735 BUG_ON(list_empty(&root->delalloc_root));
1736 list_del_init(&root->delalloc_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001737 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001738 }
1739 }
Nikolay Borisov2b877332018-04-27 12:21:51 +03001740}
1741
1742static void btrfs_del_delalloc_inode(struct btrfs_root *root,
1743 struct btrfs_inode *inode)
1744{
1745 spin_lock(&root->delalloc_lock);
1746 __btrfs_del_delalloc_inode(root, inode);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001747 spin_unlock(&root->delalloc_lock);
1748}
1749
Chris Masond352ac62008-09-29 15:18:18 -04001750/*
Nikolay Borisove06a1fc2018-11-01 14:09:50 +02001751 * Properly track delayed allocation bytes in the inode and to maintain the
1752 * list of inodes that have pending delalloc work to be done.
Chris Masond352ac62008-09-29 15:18:18 -04001753 */
Nikolay Borisove06a1fc2018-11-01 14:09:50 +02001754void btrfs_set_delalloc_extent(struct inode *inode, struct extent_state *state,
1755 unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001756{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001757 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1758
Wang Shilong47059d92014-07-03 18:22:07 +08001759 if ((*bits & EXTENT_DEFRAG) && !(*bits & EXTENT_DELALLOC))
1760 WARN_ON(1);
Chris Mason75eff682008-12-15 15:54:40 -05001761 /*
1762 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001763 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001764 * bit, which is only set or cleared with irqs on
1765 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001766 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001767 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001768 u64 len = state->end + 1 - state->start;
Josef Bacik8b62f872017-10-19 14:15:55 -04001769 u32 num_extents = count_max_extents(len);
Nikolay Borisov70ddc552017-02-20 13:50:35 +02001770 bool do_list = !btrfs_is_free_space_inode(BTRFS_I(inode));
Josef Bacik9ed74f22009-09-11 16:12:44 -04001771
Josef Bacik8b62f872017-10-19 14:15:55 -04001772 spin_lock(&BTRFS_I(inode)->lock);
1773 btrfs_mod_outstanding_extents(BTRFS_I(inode), num_extents);
1774 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik287a0ab2010-03-19 18:07:23 +00001775
Josef Bacik6a3891c2015-03-16 17:38:52 -04001776 /* For sanity tests */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001777 if (btrfs_is_testing(fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04001778 return;
1779
Nikolay Borisov104b4e52017-06-20 21:01:20 +03001780 percpu_counter_add_batch(&fs_info->delalloc_bytes, len,
1781 fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001782 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001783 BTRFS_I(inode)->delalloc_bytes += len;
Wang Shilong47059d92014-07-03 18:22:07 +08001784 if (*bits & EXTENT_DEFRAG)
1785 BTRFS_I(inode)->defrag_bytes += len;
Miao Xiedf0af1a2013-01-29 10:11:59 +00001786 if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00001787 &BTRFS_I(inode)->runtime_flags))
1788 btrfs_add_delalloc_inodes(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001789 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001790 }
Filipe Mananaa7e3b972017-04-03 10:45:46 +01001791
1792 if (!(state->state & EXTENT_DELALLOC_NEW) &&
1793 (*bits & EXTENT_DELALLOC_NEW)) {
1794 spin_lock(&BTRFS_I(inode)->lock);
1795 BTRFS_I(inode)->new_delalloc_bytes += state->end + 1 -
1796 state->start;
1797 spin_unlock(&BTRFS_I(inode)->lock);
1798 }
Chris Mason291d6732008-01-29 15:55:23 -05001799}
1800
Chris Masond352ac62008-09-29 15:18:18 -04001801/*
Nikolay Borisova36bb5f2018-11-01 14:09:51 +02001802 * Once a range is no longer delalloc this function ensures that proper
1803 * accounting happens.
Chris Masond352ac62008-09-29 15:18:18 -04001804 */
Nikolay Borisova36bb5f2018-11-01 14:09:51 +02001805void btrfs_clear_delalloc_extent(struct inode *vfs_inode,
1806 struct extent_state *state, unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001807{
Nikolay Borisova36bb5f2018-11-01 14:09:51 +02001808 struct btrfs_inode *inode = BTRFS_I(vfs_inode);
1809 struct btrfs_fs_info *fs_info = btrfs_sb(vfs_inode->i_sb);
Wang Shilong47059d92014-07-03 18:22:07 +08001810 u64 len = state->end + 1 - state->start;
David Sterba823bb202017-01-04 11:09:51 +01001811 u32 num_extents = count_max_extents(len);
Wang Shilong47059d92014-07-03 18:22:07 +08001812
Filipe Manana4a4b9642017-07-27 19:52:55 +01001813 if ((state->state & EXTENT_DEFRAG) && (*bits & EXTENT_DEFRAG)) {
1814 spin_lock(&inode->lock);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001815 inode->defrag_bytes -= len;
Filipe Manana4a4b9642017-07-27 19:52:55 +01001816 spin_unlock(&inode->lock);
1817 }
Wang Shilong47059d92014-07-03 18:22:07 +08001818
Chris Mason75eff682008-12-15 15:54:40 -05001819 /*
1820 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001821 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001822 * bit, which is only set or cleared with irqs on
1823 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001824 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001825 struct btrfs_root *root = inode->root;
Liu Bo83eea1f2012-07-10 05:28:39 -06001826 bool do_list = !btrfs_is_free_space_inode(inode);
Chris Masonbcbfce82008-04-22 13:26:47 -04001827
Josef Bacik8b62f872017-10-19 14:15:55 -04001828 spin_lock(&inode->lock);
1829 btrfs_mod_outstanding_extents(inode, -num_extents);
1830 spin_unlock(&inode->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001831
Josef Bacikb6d08f02013-09-27 14:57:43 -04001832 /*
1833 * We don't reserve metadata space for space cache inodes so we
1834 * don't need to call dellalloc_release_metadata if there is an
1835 * error.
1836 */
Filipe Mananaa315e682017-03-06 23:04:20 +00001837 if (*bits & EXTENT_CLEAR_META_RESV &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001838 root != fs_info->tree_root)
Qu Wenruo43b18592017-12-12 15:34:32 +08001839 btrfs_delalloc_release_metadata(inode, len, false);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001840
Josef Bacik6a3891c2015-03-16 17:38:52 -04001841 /* For sanity tests. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001842 if (btrfs_is_testing(fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04001843 return;
1844
Filipe Mananaa315e682017-03-06 23:04:20 +00001845 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID &&
1846 do_list && !(state->state & EXTENT_NORESERVE) &&
1847 (*bits & EXTENT_CLEAR_DATA_RESV))
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001848 btrfs_free_reserved_data_space_noquota(
1849 &inode->vfs_inode,
Qu Wenruo51773be2015-10-08 18:19:37 +08001850 state->start, len);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001851
Nikolay Borisov104b4e52017-06-20 21:01:20 +03001852 percpu_counter_add_batch(&fs_info->delalloc_bytes, -len,
1853 fs_info->delalloc_batch);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001854 spin_lock(&inode->lock);
1855 inode->delalloc_bytes -= len;
1856 if (do_list && inode->delalloc_bytes == 0 &&
Miao Xiedf0af1a2013-01-29 10:11:59 +00001857 test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001858 &inode->runtime_flags))
Miao Xieeb73c1b2013-05-15 07:48:22 +00001859 btrfs_del_delalloc_inode(root, inode);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001860 spin_unlock(&inode->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001861 }
Filipe Mananaa7e3b972017-04-03 10:45:46 +01001862
1863 if ((state->state & EXTENT_DELALLOC_NEW) &&
1864 (*bits & EXTENT_DELALLOC_NEW)) {
1865 spin_lock(&inode->lock);
1866 ASSERT(inode->new_delalloc_bytes >= len);
1867 inode->new_delalloc_bytes -= len;
1868 spin_unlock(&inode->lock);
1869 }
Chris Mason291d6732008-01-29 15:55:23 -05001870}
1871
Chris Masond352ac62008-09-29 15:18:18 -04001872/*
David Sterba00032d32018-07-18 19:28:09 +02001873 * Merge bio hook, this must check the chunk tree to make sure we don't create
1874 * bios that span stripes or chunks
Liu Bo6f034ec2016-06-22 18:31:49 -07001875 *
1876 * return 1 if page cannot be merged to bio
1877 * return 0 if page can be merged to bio
1878 * return error otherwise
Chris Masond352ac62008-09-29 15:18:18 -04001879 */
Mike Christie81a75f672016-06-05 14:31:54 -05001880int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
Chris Masonc8b97812008-10-29 14:49:59 -04001881 size_t size, struct bio *bio,
1882 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04001883{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001884 struct inode *inode = page->mapping->host;
1885 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001886 u64 logical = (u64)bio->bi_iter.bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04001887 u64 length = 0;
1888 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04001889 int ret;
1890
Chris Mason771ed682008-11-06 22:02:51 -05001891 if (bio_flags & EXTENT_BIO_COMPRESSED)
1892 return 0;
1893
Kent Overstreet4f024f32013-10-11 15:44:27 -07001894 length = bio->bi_iter.bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04001895 map_length = length;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001896 ret = btrfs_map_block(fs_info, btrfs_op(bio), logical, &map_length,
1897 NULL, 0);
Liu Bo6f034ec2016-06-22 18:31:49 -07001898 if (ret < 0)
1899 return ret;
Chris Masond3977122009-01-05 21:25:51 -05001900 if (map_length < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04001901 return 1;
Jeff Mahoney3444a972011-10-03 23:23:13 -04001902 return 0;
Chris Mason239b14b2008-03-24 15:02:07 -04001903}
1904
Chris Masond352ac62008-09-29 15:18:18 -04001905/*
1906 * in order to insert checksums into the metadata in large chunks,
1907 * we wait until bio submission time. All the pages in the bio are
1908 * checksummed and sums are attached onto the ordered extent record.
1909 *
1910 * At IO completion time the cums attached on the ordered extent record
1911 * are inserted into the btree
1912 */
David Sterbad0ee3932018-03-08 14:35:48 +01001913static blk_status_t btrfs_submit_bio_start(void *private_data, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001914 u64 bio_offset)
Chris Mason065631f2008-02-20 12:07:25 -05001915{
Josef Bacikc6100a42017-05-05 11:57:13 -04001916 struct inode *inode = private_data;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001917 blk_status_t ret = 0;
Chris Masone0156402008-04-16 11:15:20 -04001918
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001919 ret = btrfs_csum_one_bio(inode, bio, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001920 BUG_ON(ret); /* -ENOMEM */
Chris Mason4a69a412008-11-06 22:03:00 -05001921 return 0;
1922}
Chris Masone0156402008-04-16 11:15:20 -04001923
Chris Mason4a69a412008-11-06 22:03:00 -05001924/*
Chris Masoncad321a2008-12-17 14:51:42 -05001925 * extent_io.c submission hook. This does the right thing for csum calculation
Liu Bo4c274bc2017-11-01 17:19:27 -06001926 * on write, or reading the csums from the tree before a read.
1927 *
1928 * Rules about async/sync submit,
1929 * a) read: sync submit
1930 *
1931 * b) write without checksum: sync submit
1932 *
1933 * c) write with checksum:
1934 * c-1) if bio is issued by fsync: sync submit
1935 * (sync_writers != 0)
1936 *
1937 * c-2) if root is reloc root: sync submit
1938 * (only in case of buffered IO)
1939 *
1940 * c-3) otherwise: async submit
Chris Masond352ac62008-09-29 15:18:18 -04001941 */
Linus Torvalds8c27cb32017-07-05 16:41:23 -07001942static blk_status_t btrfs_submit_bio_hook(void *private_data, struct bio *bio,
Josef Bacikc6100a42017-05-05 11:57:13 -04001943 int mirror_num, unsigned long bio_flags,
1944 u64 bio_offset)
Chris Mason44b8bd72008-04-16 11:14:51 -04001945{
Josef Bacikc6100a42017-05-05 11:57:13 -04001946 struct inode *inode = private_data;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001947 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason44b8bd72008-04-16 11:14:51 -04001948 struct btrfs_root *root = BTRFS_I(inode)->root;
Chandan Rajendra0d51e282015-07-27 15:26:43 +05301949 enum btrfs_wq_endio_type metadata = BTRFS_WQ_ENDIO_DATA;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001950 blk_status_t ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001951 int skip_sum;
Josef Bacikb812ce22012-11-16 13:56:32 -05001952 int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
Chris Mason44b8bd72008-04-16 11:14:51 -04001953
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001954 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masoncad321a2008-12-17 14:51:42 -05001955
Nikolay Borisov70ddc552017-02-20 13:50:35 +02001956 if (btrfs_is_free_space_inode(BTRFS_I(inode)))
Chandan Rajendra0d51e282015-07-27 15:26:43 +05301957 metadata = BTRFS_WQ_ENDIO_FREE_SPACE;
Jeff Mahoney04173412011-10-03 23:23:12 -04001958
Mike Christie37226b22016-06-05 14:31:52 -05001959 if (bio_op(bio) != REQ_OP_WRITE) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001960 ret = btrfs_bio_wq_end_io(fs_info, bio, metadata);
Josef Bacik5fd02042012-05-02 14:00:54 -04001961 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001962 goto out;
Josef Bacik5fd02042012-05-02 14:00:54 -04001963
Chris Masond20f7042008-12-08 16:58:54 -05001964 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Stefan Behrens61891922012-11-05 18:51:52 +01001965 ret = btrfs_submit_compressed_read(inode, bio,
1966 mirror_num,
1967 bio_flags);
1968 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001969 } else if (!skip_sum) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001970 ret = btrfs_lookup_bio_sums(inode, bio, NULL);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001971 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001972 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001973 }
Chris Mason4d1b5fb2008-08-20 09:44:52 -04001974 goto mapit;
Josef Bacikb812ce22012-11-16 13:56:32 -05001975 } else if (async && !skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05001976 /* csum items have already been cloned */
1977 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1978 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001979 /* we're doing a write, do the async checksumming */
Josef Bacikc6100a42017-05-05 11:57:13 -04001980 ret = btrfs_wq_submit_bio(fs_info, bio, mirror_num, bio_flags,
1981 bio_offset, inode,
David Sterbae288c082018-07-18 17:36:24 +02001982 btrfs_submit_bio_start);
Stefan Behrens61891922012-11-05 18:51:52 +01001983 goto out;
Josef Bacikb812ce22012-11-16 13:56:32 -05001984 } else if (!skip_sum) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001985 ret = btrfs_csum_one_bio(inode, bio, 0, 0);
Josef Bacikb812ce22012-11-16 13:56:32 -05001986 if (ret)
1987 goto out;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001988 }
1989
Chris Mason0b86a832008-03-24 15:01:56 -04001990mapit:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001991 ret = btrfs_map_bio(fs_info, bio, mirror_num, 0);
Stefan Behrens61891922012-11-05 18:51:52 +01001992
1993out:
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001994 if (ret) {
1995 bio->bi_status = ret;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001996 bio_endio(bio);
1997 }
Stefan Behrens61891922012-11-05 18:51:52 +01001998 return ret;
Chris Mason065631f2008-02-20 12:07:25 -05001999}
Chris Mason6885f302008-02-20 16:11:05 -05002000
Chris Masond352ac62008-09-29 15:18:18 -04002001/*
2002 * given a list of ordered sums record them in the inode. This happens
2003 * at IO completion time based on sums calculated at bio submission time.
2004 */
Chris Masonba1da2f2008-07-17 12:54:15 -04002005static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
David Sterbadf9f6282017-02-10 19:35:37 +01002006 struct inode *inode, struct list_head *list)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002007{
Chris Masone6dcd2d2008-07-17 12:53:50 -04002008 struct btrfs_ordered_sum *sum;
Nikolay Borisovac01f262018-01-08 10:59:43 +02002009 int ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002010
Qinghuang Fengc6e30872009-01-21 10:59:08 -05002011 list_for_each_entry(sum, list, list) {
David Sterba7c2871a2017-11-08 01:07:43 +01002012 trans->adding_csums = true;
Nikolay Borisovac01f262018-01-08 10:59:43 +02002013 ret = btrfs_csum_file_blocks(trans,
Chris Masond20f7042008-12-08 16:58:54 -05002014 BTRFS_I(inode)->root->fs_info->csum_root, sum);
David Sterba7c2871a2017-11-08 01:07:43 +01002015 trans->adding_csums = false;
Nikolay Borisovac01f262018-01-08 10:59:43 +02002016 if (ret)
2017 return ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002018 }
2019 return 0;
2020}
2021
Josef Bacik2ac55d42010-02-03 19:33:23 +00002022int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
Filipe Mananae3b8a482017-11-04 00:16:59 +00002023 unsigned int extra_bits,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08002024 struct extent_state **cached_state, int dedupe)
Chris Masonea8c2812008-08-04 23:17:27 -04002025{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002026 WARN_ON((end & (PAGE_SIZE - 1)) == 0);
Chris Masonea8c2812008-08-04 23:17:27 -04002027 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
Filipe Mananae3b8a482017-11-04 00:16:59 +00002028 extra_bits, cached_state);
Chris Masonea8c2812008-08-04 23:17:27 -04002029}
2030
Chris Masond352ac62008-09-29 15:18:18 -04002031/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04002032struct btrfs_writepage_fixup {
2033 struct page *page;
2034 struct btrfs_work work;
2035};
2036
Christoph Hellwigb2950862008-12-02 09:54:17 -05002037static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04002038{
2039 struct btrfs_writepage_fixup *fixup;
2040 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002041 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08002042 struct extent_changeset *data_reserved = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04002043 struct page *page;
2044 struct inode *inode;
2045 u64 page_start;
2046 u64 page_end;
Jeff Mahoney87826df2012-02-15 16:23:57 +01002047 int ret;
Chris Mason247e7432008-07-17 12:53:51 -04002048
2049 fixup = container_of(work, struct btrfs_writepage_fixup, work);
2050 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -04002051again:
Chris Mason247e7432008-07-17 12:53:51 -04002052 lock_page(page);
2053 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
2054 ClearPageChecked(page);
2055 goto out_page;
2056 }
2057
2058 inode = page->mapping->host;
2059 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002060 page_end = page_offset(page) + PAGE_SIZE - 1;
Chris Mason247e7432008-07-17 12:53:51 -04002061
David Sterbaff13db42015-12-03 14:30:40 +01002062 lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002063 &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04002064
2065 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04002066 if (PagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04002067 goto out;
Chris Mason4a096752008-07-21 10:29:44 -04002068
Nikolay Borisova776c6f2017-02-20 13:50:49 +02002069 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002070 PAGE_SIZE);
Chris Mason4a096752008-07-21 10:29:44 -04002071 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00002072 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
David Sterbae43bbe52017-12-12 21:43:52 +01002073 page_end, &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04002074 unlock_page(page);
2075 btrfs_start_ordered_extent(inode, ordered, 1);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002076 btrfs_put_ordered_extent(ordered);
Chris Mason4a096752008-07-21 10:29:44 -04002077 goto again;
2078 }
Chris Mason247e7432008-07-17 12:53:51 -04002079
Qu Wenruo364ecf32017-02-27 15:10:38 +08002080 ret = btrfs_delalloc_reserve_space(inode, &data_reserved, page_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002081 PAGE_SIZE);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002082 if (ret) {
2083 mapping_set_error(page->mapping, ret);
2084 end_extent_writepage(page, ret, page_start, page_end);
2085 ClearPageChecked(page);
2086 goto out;
2087 }
2088
Nikolay Borisovf3038ee2017-12-05 09:29:19 +02002089 ret = btrfs_set_extent_delalloc(inode, page_start, page_end, 0,
2090 &cached_state, 0);
2091 if (ret) {
2092 mapping_set_error(page->mapping, ret);
2093 end_extent_writepage(page, ret, page_start, page_end);
2094 ClearPageChecked(page);
2095 goto out;
2096 }
2097
Chris Mason247e7432008-07-17 12:53:51 -04002098 ClearPageChecked(page);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002099 set_page_dirty(page);
Qu Wenruo43b18592017-12-12 15:34:32 +08002100 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE, false);
Chris Mason247e7432008-07-17 12:53:51 -04002101out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00002102 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01002103 &cached_state);
Chris Mason247e7432008-07-17 12:53:51 -04002104out_page:
2105 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002106 put_page(page);
Miao Xieb897abe2011-01-26 16:19:22 +08002107 kfree(fixup);
Qu Wenruo364ecf32017-02-27 15:10:38 +08002108 extent_changeset_free(data_reserved);
Chris Mason247e7432008-07-17 12:53:51 -04002109}
2110
2111/*
2112 * There are a few paths in the higher layers of the kernel that directly
2113 * set the page dirty bit without asking the filesystem if it is a
2114 * good idea. This causes problems because we want to make sure COW
2115 * properly happens and the data=ordered rules are followed.
2116 *
Chris Masonc8b97812008-10-29 14:49:59 -04002117 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04002118 * hasn't been properly setup for IO. We kick off an async process
2119 * to fix it up. The async helper will wait for ordered extents, set
2120 * the delalloc bit and make it safe to write the page.
2121 */
Nikolay Borisovd75855b2018-11-01 14:09:47 +02002122int btrfs_writepage_cow_fixup(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04002123{
2124 struct inode *inode = page->mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002125 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason247e7432008-07-17 12:53:51 -04002126 struct btrfs_writepage_fixup *fixup;
Chris Mason247e7432008-07-17 12:53:51 -04002127
Chris Mason8b62b722009-09-02 16:53:46 -04002128 /* this page is properly in the ordered list */
2129 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04002130 return 0;
2131
2132 if (PageChecked(page))
2133 return -EAGAIN;
2134
2135 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
2136 if (!fixup)
2137 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04002138
Chris Mason247e7432008-07-17 12:53:51 -04002139 SetPageChecked(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002140 get_page(page);
Liu Bo9e0af232014-08-15 23:36:53 +08002141 btrfs_init_work(&fixup->work, btrfs_fixup_helper,
2142 btrfs_writepage_fixup_worker, NULL, NULL);
Chris Mason247e7432008-07-17 12:53:51 -04002143 fixup->page = page;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002144 btrfs_queue_work(fs_info->fixup_workers, &fixup->work);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002145 return -EBUSY;
Chris Mason247e7432008-07-17 12:53:51 -04002146}
2147
Yan Zhengd899e052008-10-30 14:25:28 -04002148static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
2149 struct inode *inode, u64 file_pos,
2150 u64 disk_bytenr, u64 disk_num_bytes,
2151 u64 num_bytes, u64 ram_bytes,
2152 u8 compression, u8 encryption,
2153 u16 other_encoding, int extent_type)
2154{
2155 struct btrfs_root *root = BTRFS_I(inode)->root;
2156 struct btrfs_file_extent_item *fi;
2157 struct btrfs_path *path;
2158 struct extent_buffer *leaf;
2159 struct btrfs_key ins;
Qu Wenruoa12b8772017-02-27 15:10:37 +08002160 u64 qg_released;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002161 int extent_inserted = 0;
Yan Zhengd899e052008-10-30 14:25:28 -04002162 int ret;
2163
2164 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07002165 if (!path)
2166 return -ENOMEM;
Yan Zhengd899e052008-10-30 14:25:28 -04002167
Chris Masona1ed8352009-09-11 12:27:37 -04002168 /*
2169 * we may be replacing one extent in the tree with another.
2170 * The new extent is pinned in the extent map, and we don't want
2171 * to drop it from the cache until it is completely in the btree.
2172 *
2173 * So, tell btrfs_drop_extents to leave this extent in the cache.
2174 * the caller is expected to unpin it and allow it to be merged
2175 * with the others.
2176 */
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002177 ret = __btrfs_drop_extents(trans, root, inode, path, file_pos,
2178 file_pos + num_bytes, NULL, 0,
2179 1, sizeof(*fi), &extent_inserted);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002180 if (ret)
2181 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04002182
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002183 if (!extent_inserted) {
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002184 ins.objectid = btrfs_ino(BTRFS_I(inode));
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002185 ins.offset = file_pos;
2186 ins.type = BTRFS_EXTENT_DATA_KEY;
2187
2188 path->leave_spinning = 1;
2189 ret = btrfs_insert_empty_item(trans, root, path, &ins,
2190 sizeof(*fi));
2191 if (ret)
2192 goto out;
2193 }
Yan Zhengd899e052008-10-30 14:25:28 -04002194 leaf = path->nodes[0];
2195 fi = btrfs_item_ptr(leaf, path->slots[0],
2196 struct btrfs_file_extent_item);
2197 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
2198 btrfs_set_file_extent_type(leaf, fi, extent_type);
2199 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
2200 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
2201 btrfs_set_file_extent_offset(leaf, fi, 0);
2202 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
2203 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
2204 btrfs_set_file_extent_compression(leaf, fi, compression);
2205 btrfs_set_file_extent_encryption(leaf, fi, encryption);
2206 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
Chris Masonb9473432009-03-13 11:00:37 -04002207
Yan Zhengd899e052008-10-30 14:25:28 -04002208 btrfs_mark_buffer_dirty(leaf);
Josef Bacikce195332012-09-25 15:26:16 -04002209 btrfs_release_path(path);
Yan Zhengd899e052008-10-30 14:25:28 -04002210
2211 inode_add_bytes(inode, num_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -04002212
2213 ins.objectid = disk_bytenr;
2214 ins.offset = disk_num_bytes;
2215 ins.type = BTRFS_EXTENT_ITEM_KEY;
Qu Wenruoa12b8772017-02-27 15:10:37 +08002216
Qu Wenruo297d7502015-09-08 17:08:37 +08002217 /*
Qu Wenruo5846a3c2015-10-26 14:11:18 +08002218 * Release the reserved range from inode dirty range map, as it is
2219 * already moved into delayed_ref_head
Qu Wenruo297d7502015-09-08 17:08:37 +08002220 */
Qu Wenruoa12b8772017-02-27 15:10:37 +08002221 ret = btrfs_qgroup_release_data(inode, file_pos, ram_bytes);
2222 if (ret < 0)
2223 goto out;
2224 qg_released = ret;
Josef Bacik84f7d8e2017-09-29 15:43:49 -04002225 ret = btrfs_alloc_reserved_file_extent(trans, root,
2226 btrfs_ino(BTRFS_I(inode)),
2227 file_pos, qg_released, &ins);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002228out:
Yan Zhengd899e052008-10-30 14:25:28 -04002229 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04002230
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002231 return ret;
Yan Zhengd899e052008-10-30 14:25:28 -04002232}
2233
Liu Bo38c227d2013-01-29 03:18:40 +00002234/* snapshot-aware defrag */
2235struct sa_defrag_extent_backref {
2236 struct rb_node node;
2237 struct old_sa_defrag_extent *old;
2238 u64 root_id;
2239 u64 inum;
2240 u64 file_pos;
2241 u64 extent_offset;
2242 u64 num_bytes;
2243 u64 generation;
2244};
2245
2246struct old_sa_defrag_extent {
2247 struct list_head list;
2248 struct new_sa_defrag_extent *new;
2249
2250 u64 extent_offset;
2251 u64 bytenr;
2252 u64 offset;
2253 u64 len;
2254 int count;
2255};
2256
2257struct new_sa_defrag_extent {
2258 struct rb_root root;
2259 struct list_head head;
2260 struct btrfs_path *path;
2261 struct inode *inode;
2262 u64 file_pos;
2263 u64 len;
2264 u64 bytenr;
2265 u64 disk_len;
2266 u8 compress_type;
2267};
2268
2269static int backref_comp(struct sa_defrag_extent_backref *b1,
2270 struct sa_defrag_extent_backref *b2)
2271{
2272 if (b1->root_id < b2->root_id)
2273 return -1;
2274 else if (b1->root_id > b2->root_id)
2275 return 1;
2276
2277 if (b1->inum < b2->inum)
2278 return -1;
2279 else if (b1->inum > b2->inum)
2280 return 1;
2281
2282 if (b1->file_pos < b2->file_pos)
2283 return -1;
2284 else if (b1->file_pos > b2->file_pos)
2285 return 1;
2286
2287 /*
2288 * [------------------------------] ===> (a range of space)
2289 * |<--->| |<---->| =============> (fs/file tree A)
2290 * |<---------------------------->| ===> (fs/file tree B)
2291 *
2292 * A range of space can refer to two file extents in one tree while
2293 * refer to only one file extent in another tree.
2294 *
2295 * So we may process a disk offset more than one time(two extents in A)
2296 * and locate at the same extent(one extent in B), then insert two same
2297 * backrefs(both refer to the extent in B).
2298 */
2299 return 0;
2300}
2301
2302static void backref_insert(struct rb_root *root,
2303 struct sa_defrag_extent_backref *backref)
2304{
2305 struct rb_node **p = &root->rb_node;
2306 struct rb_node *parent = NULL;
2307 struct sa_defrag_extent_backref *entry;
2308 int ret;
2309
2310 while (*p) {
2311 parent = *p;
2312 entry = rb_entry(parent, struct sa_defrag_extent_backref, node);
2313
2314 ret = backref_comp(backref, entry);
2315 if (ret < 0)
2316 p = &(*p)->rb_left;
2317 else
2318 p = &(*p)->rb_right;
2319 }
2320
2321 rb_link_node(&backref->node, parent, p);
2322 rb_insert_color(&backref->node, root);
2323}
2324
2325/*
2326 * Note the backref might has changed, and in this case we just return 0.
2327 */
2328static noinline int record_one_backref(u64 inum, u64 offset, u64 root_id,
2329 void *ctx)
2330{
2331 struct btrfs_file_extent_item *extent;
Liu Bo38c227d2013-01-29 03:18:40 +00002332 struct old_sa_defrag_extent *old = ctx;
2333 struct new_sa_defrag_extent *new = old->new;
2334 struct btrfs_path *path = new->path;
2335 struct btrfs_key key;
2336 struct btrfs_root *root;
2337 struct sa_defrag_extent_backref *backref;
2338 struct extent_buffer *leaf;
2339 struct inode *inode = new->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002340 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002341 int slot;
2342 int ret;
2343 u64 extent_offset;
2344 u64 num_bytes;
2345
2346 if (BTRFS_I(inode)->root->root_key.objectid == root_id &&
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002347 inum == btrfs_ino(BTRFS_I(inode)))
Liu Bo38c227d2013-01-29 03:18:40 +00002348 return 0;
2349
2350 key.objectid = root_id;
2351 key.type = BTRFS_ROOT_ITEM_KEY;
2352 key.offset = (u64)-1;
2353
Liu Bo38c227d2013-01-29 03:18:40 +00002354 root = btrfs_read_fs_root_no_name(fs_info, &key);
2355 if (IS_ERR(root)) {
2356 if (PTR_ERR(root) == -ENOENT)
2357 return 0;
2358 WARN_ON(1);
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002359 btrfs_debug(fs_info, "inum=%llu, offset=%llu, root_id=%llu",
Liu Bo38c227d2013-01-29 03:18:40 +00002360 inum, offset, root_id);
2361 return PTR_ERR(root);
2362 }
2363
2364 key.objectid = inum;
2365 key.type = BTRFS_EXTENT_DATA_KEY;
2366 if (offset > (u64)-1 << 32)
2367 key.offset = 0;
2368 else
2369 key.offset = offset;
2370
2371 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05302372 if (WARN_ON(ret < 0))
Liu Bo38c227d2013-01-29 03:18:40 +00002373 return ret;
Josef Bacik50f13192013-07-22 12:50:37 -04002374 ret = 0;
Liu Bo38c227d2013-01-29 03:18:40 +00002375
2376 while (1) {
2377 cond_resched();
2378
2379 leaf = path->nodes[0];
2380 slot = path->slots[0];
2381
2382 if (slot >= btrfs_header_nritems(leaf)) {
2383 ret = btrfs_next_leaf(root, path);
2384 if (ret < 0) {
2385 goto out;
2386 } else if (ret > 0) {
2387 ret = 0;
2388 goto out;
2389 }
2390 continue;
2391 }
2392
2393 path->slots[0]++;
2394
2395 btrfs_item_key_to_cpu(leaf, &key, slot);
2396
2397 if (key.objectid > inum)
2398 goto out;
2399
2400 if (key.objectid < inum || key.type != BTRFS_EXTENT_DATA_KEY)
2401 continue;
2402
2403 extent = btrfs_item_ptr(leaf, slot,
2404 struct btrfs_file_extent_item);
2405
2406 if (btrfs_file_extent_disk_bytenr(leaf, extent) != old->bytenr)
2407 continue;
2408
Liu Boe68afa42013-07-01 22:13:26 +08002409 /*
2410 * 'offset' refers to the exact key.offset,
2411 * NOT the 'offset' field in btrfs_extent_data_ref, ie.
2412 * (key.offset - extent_offset).
2413 */
2414 if (key.offset != offset)
Liu Bo38c227d2013-01-29 03:18:40 +00002415 continue;
2416
Liu Boe68afa42013-07-01 22:13:26 +08002417 extent_offset = btrfs_file_extent_offset(leaf, extent);
Liu Bo38c227d2013-01-29 03:18:40 +00002418 num_bytes = btrfs_file_extent_num_bytes(leaf, extent);
Liu Boe68afa42013-07-01 22:13:26 +08002419
Liu Bo38c227d2013-01-29 03:18:40 +00002420 if (extent_offset >= old->extent_offset + old->offset +
2421 old->len || extent_offset + num_bytes <=
2422 old->extent_offset + old->offset)
2423 continue;
Liu Bo38c227d2013-01-29 03:18:40 +00002424 break;
2425 }
2426
2427 backref = kmalloc(sizeof(*backref), GFP_NOFS);
2428 if (!backref) {
2429 ret = -ENOENT;
2430 goto out;
2431 }
2432
2433 backref->root_id = root_id;
2434 backref->inum = inum;
Liu Boe68afa42013-07-01 22:13:26 +08002435 backref->file_pos = offset;
Liu Bo38c227d2013-01-29 03:18:40 +00002436 backref->num_bytes = num_bytes;
2437 backref->extent_offset = extent_offset;
2438 backref->generation = btrfs_file_extent_generation(leaf, extent);
2439 backref->old = old;
2440 backref_insert(&new->root, backref);
2441 old->count++;
2442out:
2443 btrfs_release_path(path);
2444 WARN_ON(ret);
2445 return ret;
2446}
2447
2448static noinline bool record_extent_backrefs(struct btrfs_path *path,
2449 struct new_sa_defrag_extent *new)
2450{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002451 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002452 struct old_sa_defrag_extent *old, *tmp;
2453 int ret;
2454
2455 new->path = path;
2456
2457 list_for_each_entry_safe(old, tmp, &new->head, list) {
Liu Boe68afa42013-07-01 22:13:26 +08002458 ret = iterate_inodes_from_logical(old->bytenr +
2459 old->extent_offset, fs_info,
Liu Bo38c227d2013-01-29 03:18:40 +00002460 path, record_one_backref,
Zygo Blaxellc995ab32017-09-22 13:58:45 -04002461 old, false);
Josef Bacik4724b102013-11-05 11:11:40 -05002462 if (ret < 0 && ret != -ENOENT)
2463 return false;
Liu Bo38c227d2013-01-29 03:18:40 +00002464
2465 /* no backref to be processed for this extent */
2466 if (!old->count) {
2467 list_del(&old->list);
2468 kfree(old);
2469 }
2470 }
2471
2472 if (list_empty(&new->head))
2473 return false;
2474
2475 return true;
2476}
2477
2478static int relink_is_mergable(struct extent_buffer *leaf,
2479 struct btrfs_file_extent_item *fi,
Liu Bo116e0022013-08-02 16:30:40 +08002480 struct new_sa_defrag_extent *new)
Liu Bo38c227d2013-01-29 03:18:40 +00002481{
Liu Bo116e0022013-08-02 16:30:40 +08002482 if (btrfs_file_extent_disk_bytenr(leaf, fi) != new->bytenr)
Liu Bo38c227d2013-01-29 03:18:40 +00002483 return 0;
2484
2485 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG)
2486 return 0;
2487
Liu Bo116e0022013-08-02 16:30:40 +08002488 if (btrfs_file_extent_compression(leaf, fi) != new->compress_type)
2489 return 0;
2490
2491 if (btrfs_file_extent_encryption(leaf, fi) ||
Liu Bo38c227d2013-01-29 03:18:40 +00002492 btrfs_file_extent_other_encoding(leaf, fi))
2493 return 0;
2494
2495 return 1;
2496}
2497
2498/*
2499 * Note the backref might has changed, and in this case we just return 0.
2500 */
2501static noinline int relink_extent_backref(struct btrfs_path *path,
2502 struct sa_defrag_extent_backref *prev,
2503 struct sa_defrag_extent_backref *backref)
2504{
2505 struct btrfs_file_extent_item *extent;
2506 struct btrfs_file_extent_item *item;
2507 struct btrfs_ordered_extent *ordered;
2508 struct btrfs_trans_handle *trans;
Liu Bo38c227d2013-01-29 03:18:40 +00002509 struct btrfs_root *root;
2510 struct btrfs_key key;
2511 struct extent_buffer *leaf;
2512 struct old_sa_defrag_extent *old = backref->old;
2513 struct new_sa_defrag_extent *new = old->new;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002514 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002515 struct inode *inode;
2516 struct extent_state *cached = NULL;
2517 int ret = 0;
2518 u64 start;
2519 u64 len;
2520 u64 lock_start;
2521 u64 lock_end;
2522 bool merge = false;
2523 int index;
2524
2525 if (prev && prev->root_id == backref->root_id &&
2526 prev->inum == backref->inum &&
2527 prev->file_pos + prev->num_bytes == backref->file_pos)
2528 merge = true;
2529
2530 /* step 1: get root */
2531 key.objectid = backref->root_id;
2532 key.type = BTRFS_ROOT_ITEM_KEY;
2533 key.offset = (u64)-1;
2534
Liu Bo38c227d2013-01-29 03:18:40 +00002535 index = srcu_read_lock(&fs_info->subvol_srcu);
2536
2537 root = btrfs_read_fs_root_no_name(fs_info, &key);
2538 if (IS_ERR(root)) {
2539 srcu_read_unlock(&fs_info->subvol_srcu, index);
2540 if (PTR_ERR(root) == -ENOENT)
2541 return 0;
2542 return PTR_ERR(root);
2543 }
Liu Bo38c227d2013-01-29 03:18:40 +00002544
Wang Shilongbcbba5e2014-02-08 23:46:35 +08002545 if (btrfs_root_readonly(root)) {
2546 srcu_read_unlock(&fs_info->subvol_srcu, index);
2547 return 0;
2548 }
2549
Liu Bo38c227d2013-01-29 03:18:40 +00002550 /* step 2: get inode */
2551 key.objectid = backref->inum;
2552 key.type = BTRFS_INODE_ITEM_KEY;
2553 key.offset = 0;
2554
2555 inode = btrfs_iget(fs_info->sb, &key, root, NULL);
2556 if (IS_ERR(inode)) {
2557 srcu_read_unlock(&fs_info->subvol_srcu, index);
2558 return 0;
2559 }
2560
2561 srcu_read_unlock(&fs_info->subvol_srcu, index);
2562
2563 /* step 3: relink backref */
2564 lock_start = backref->file_pos;
2565 lock_end = backref->file_pos + backref->num_bytes - 1;
2566 lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
David Sterbaff13db42015-12-03 14:30:40 +01002567 &cached);
Liu Bo38c227d2013-01-29 03:18:40 +00002568
2569 ordered = btrfs_lookup_first_ordered_extent(inode, lock_end);
2570 if (ordered) {
2571 btrfs_put_ordered_extent(ordered);
2572 goto out_unlock;
2573 }
2574
2575 trans = btrfs_join_transaction(root);
2576 if (IS_ERR(trans)) {
2577 ret = PTR_ERR(trans);
2578 goto out_unlock;
2579 }
2580
2581 key.objectid = backref->inum;
2582 key.type = BTRFS_EXTENT_DATA_KEY;
2583 key.offset = backref->file_pos;
2584
2585 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2586 if (ret < 0) {
2587 goto out_free_path;
2588 } else if (ret > 0) {
2589 ret = 0;
2590 goto out_free_path;
2591 }
2592
2593 extent = btrfs_item_ptr(path->nodes[0], path->slots[0],
2594 struct btrfs_file_extent_item);
2595
2596 if (btrfs_file_extent_generation(path->nodes[0], extent) !=
2597 backref->generation)
2598 goto out_free_path;
2599
2600 btrfs_release_path(path);
2601
2602 start = backref->file_pos;
2603 if (backref->extent_offset < old->extent_offset + old->offset)
2604 start += old->extent_offset + old->offset -
2605 backref->extent_offset;
2606
2607 len = min(backref->extent_offset + backref->num_bytes,
2608 old->extent_offset + old->offset + old->len);
2609 len -= max(backref->extent_offset, old->extent_offset + old->offset);
2610
2611 ret = btrfs_drop_extents(trans, root, inode, start,
2612 start + len, 1);
2613 if (ret)
2614 goto out_free_path;
2615again:
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002616 key.objectid = btrfs_ino(BTRFS_I(inode));
Liu Bo38c227d2013-01-29 03:18:40 +00002617 key.type = BTRFS_EXTENT_DATA_KEY;
2618 key.offset = start;
2619
Liu Boa09a0a72013-03-11 09:20:58 +00002620 path->leave_spinning = 1;
Liu Bo38c227d2013-01-29 03:18:40 +00002621 if (merge) {
2622 struct btrfs_file_extent_item *fi;
2623 u64 extent_len;
2624 struct btrfs_key found_key;
2625
Gui Hecheng3c9665d2014-01-23 13:41:09 +08002626 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
Liu Bo38c227d2013-01-29 03:18:40 +00002627 if (ret < 0)
2628 goto out_free_path;
2629
2630 path->slots[0]--;
2631 leaf = path->nodes[0];
2632 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2633
2634 fi = btrfs_item_ptr(leaf, path->slots[0],
2635 struct btrfs_file_extent_item);
2636 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
2637
Liu Bo116e0022013-08-02 16:30:40 +08002638 if (extent_len + found_key.offset == start &&
2639 relink_is_mergable(leaf, fi, new)) {
Liu Bo38c227d2013-01-29 03:18:40 +00002640 btrfs_set_file_extent_num_bytes(leaf, fi,
2641 extent_len + len);
2642 btrfs_mark_buffer_dirty(leaf);
2643 inode_add_bytes(inode, len);
2644
2645 ret = 1;
2646 goto out_free_path;
2647 } else {
2648 merge = false;
2649 btrfs_release_path(path);
2650 goto again;
2651 }
2652 }
2653
2654 ret = btrfs_insert_empty_item(trans, root, path, &key,
2655 sizeof(*extent));
2656 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002657 btrfs_abort_transaction(trans, ret);
Liu Bo38c227d2013-01-29 03:18:40 +00002658 goto out_free_path;
2659 }
2660
2661 leaf = path->nodes[0];
2662 item = btrfs_item_ptr(leaf, path->slots[0],
2663 struct btrfs_file_extent_item);
2664 btrfs_set_file_extent_disk_bytenr(leaf, item, new->bytenr);
2665 btrfs_set_file_extent_disk_num_bytes(leaf, item, new->disk_len);
2666 btrfs_set_file_extent_offset(leaf, item, start - new->file_pos);
2667 btrfs_set_file_extent_num_bytes(leaf, item, len);
2668 btrfs_set_file_extent_ram_bytes(leaf, item, new->len);
2669 btrfs_set_file_extent_generation(leaf, item, trans->transid);
2670 btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG);
2671 btrfs_set_file_extent_compression(leaf, item, new->compress_type);
2672 btrfs_set_file_extent_encryption(leaf, item, 0);
2673 btrfs_set_file_extent_other_encoding(leaf, item, 0);
2674
2675 btrfs_mark_buffer_dirty(leaf);
2676 inode_add_bytes(inode, len);
Liu Boa09a0a72013-03-11 09:20:58 +00002677 btrfs_release_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002678
Josef Bacik84f7d8e2017-09-29 15:43:49 -04002679 ret = btrfs_inc_extent_ref(trans, root, new->bytenr,
Liu Bo38c227d2013-01-29 03:18:40 +00002680 new->disk_len, 0,
2681 backref->root_id, backref->inum,
Filipe Mananab06c4bf2015-10-23 07:52:54 +01002682 new->file_pos); /* start - extent_offset */
Liu Bo38c227d2013-01-29 03:18:40 +00002683 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002684 btrfs_abort_transaction(trans, ret);
Liu Bo38c227d2013-01-29 03:18:40 +00002685 goto out_free_path;
2686 }
2687
2688 ret = 1;
2689out_free_path:
2690 btrfs_release_path(path);
Liu Boa09a0a72013-03-11 09:20:58 +00002691 path->leave_spinning = 0;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002692 btrfs_end_transaction(trans);
Liu Bo38c227d2013-01-29 03:18:40 +00002693out_unlock:
2694 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
David Sterbae43bbe52017-12-12 21:43:52 +01002695 &cached);
Liu Bo38c227d2013-01-29 03:18:40 +00002696 iput(inode);
2697 return ret;
2698}
2699
Liu Bo6f519562013-10-29 10:45:05 +08002700static void free_sa_defrag_extent(struct new_sa_defrag_extent *new)
2701{
2702 struct old_sa_defrag_extent *old, *tmp;
2703
2704 if (!new)
2705 return;
2706
2707 list_for_each_entry_safe(old, tmp, &new->head, list) {
Liu Bo6f519562013-10-29 10:45:05 +08002708 kfree(old);
2709 }
2710 kfree(new);
2711}
2712
Liu Bo38c227d2013-01-29 03:18:40 +00002713static void relink_file_extents(struct new_sa_defrag_extent *new)
2714{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002715 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002716 struct btrfs_path *path;
Liu Bo38c227d2013-01-29 03:18:40 +00002717 struct sa_defrag_extent_backref *backref;
2718 struct sa_defrag_extent_backref *prev = NULL;
Liu Bo38c227d2013-01-29 03:18:40 +00002719 struct rb_node *node;
2720 int ret;
2721
Liu Bo38c227d2013-01-29 03:18:40 +00002722 path = btrfs_alloc_path();
2723 if (!path)
2724 return;
2725
2726 if (!record_extent_backrefs(path, new)) {
2727 btrfs_free_path(path);
2728 goto out;
2729 }
2730 btrfs_release_path(path);
2731
2732 while (1) {
2733 node = rb_first(&new->root);
2734 if (!node)
2735 break;
2736 rb_erase(node, &new->root);
2737
2738 backref = rb_entry(node, struct sa_defrag_extent_backref, node);
2739
2740 ret = relink_extent_backref(path, prev, backref);
2741 WARN_ON(ret < 0);
2742
2743 kfree(prev);
2744
2745 if (ret == 1)
2746 prev = backref;
2747 else
2748 prev = NULL;
2749 cond_resched();
2750 }
2751 kfree(prev);
2752
2753 btrfs_free_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002754out:
Liu Bo6f519562013-10-29 10:45:05 +08002755 free_sa_defrag_extent(new);
2756
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002757 atomic_dec(&fs_info->defrag_running);
2758 wake_up(&fs_info->transaction_wait);
Liu Bo38c227d2013-01-29 03:18:40 +00002759}
2760
2761static struct new_sa_defrag_extent *
2762record_old_file_extents(struct inode *inode,
2763 struct btrfs_ordered_extent *ordered)
2764{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002765 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002766 struct btrfs_root *root = BTRFS_I(inode)->root;
2767 struct btrfs_path *path;
2768 struct btrfs_key key;
Liu Bo6f519562013-10-29 10:45:05 +08002769 struct old_sa_defrag_extent *old;
Liu Bo38c227d2013-01-29 03:18:40 +00002770 struct new_sa_defrag_extent *new;
2771 int ret;
2772
2773 new = kmalloc(sizeof(*new), GFP_NOFS);
2774 if (!new)
2775 return NULL;
2776
2777 new->inode = inode;
2778 new->file_pos = ordered->file_offset;
2779 new->len = ordered->len;
2780 new->bytenr = ordered->start;
2781 new->disk_len = ordered->disk_len;
2782 new->compress_type = ordered->compress_type;
2783 new->root = RB_ROOT;
2784 INIT_LIST_HEAD(&new->head);
2785
2786 path = btrfs_alloc_path();
2787 if (!path)
2788 goto out_kfree;
2789
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002790 key.objectid = btrfs_ino(BTRFS_I(inode));
Liu Bo38c227d2013-01-29 03:18:40 +00002791 key.type = BTRFS_EXTENT_DATA_KEY;
2792 key.offset = new->file_pos;
2793
2794 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2795 if (ret < 0)
2796 goto out_free_path;
2797 if (ret > 0 && path->slots[0] > 0)
2798 path->slots[0]--;
2799
2800 /* find out all the old extents for the file range */
2801 while (1) {
2802 struct btrfs_file_extent_item *extent;
2803 struct extent_buffer *l;
2804 int slot;
2805 u64 num_bytes;
2806 u64 offset;
2807 u64 end;
2808 u64 disk_bytenr;
2809 u64 extent_offset;
2810
2811 l = path->nodes[0];
2812 slot = path->slots[0];
2813
2814 if (slot >= btrfs_header_nritems(l)) {
2815 ret = btrfs_next_leaf(root, path);
2816 if (ret < 0)
Liu Bo6f519562013-10-29 10:45:05 +08002817 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002818 else if (ret > 0)
2819 break;
2820 continue;
2821 }
2822
2823 btrfs_item_key_to_cpu(l, &key, slot);
2824
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002825 if (key.objectid != btrfs_ino(BTRFS_I(inode)))
Liu Bo38c227d2013-01-29 03:18:40 +00002826 break;
2827 if (key.type != BTRFS_EXTENT_DATA_KEY)
2828 break;
2829 if (key.offset >= new->file_pos + new->len)
2830 break;
2831
2832 extent = btrfs_item_ptr(l, slot, struct btrfs_file_extent_item);
2833
2834 num_bytes = btrfs_file_extent_num_bytes(l, extent);
2835 if (key.offset + num_bytes < new->file_pos)
2836 goto next;
2837
2838 disk_bytenr = btrfs_file_extent_disk_bytenr(l, extent);
2839 if (!disk_bytenr)
2840 goto next;
2841
2842 extent_offset = btrfs_file_extent_offset(l, extent);
2843
2844 old = kmalloc(sizeof(*old), GFP_NOFS);
2845 if (!old)
Liu Bo6f519562013-10-29 10:45:05 +08002846 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002847
2848 offset = max(new->file_pos, key.offset);
2849 end = min(new->file_pos + new->len, key.offset + num_bytes);
2850
2851 old->bytenr = disk_bytenr;
2852 old->extent_offset = extent_offset;
2853 old->offset = offset - key.offset;
2854 old->len = end - offset;
2855 old->new = new;
2856 old->count = 0;
2857 list_add_tail(&old->list, &new->head);
2858next:
2859 path->slots[0]++;
2860 cond_resched();
2861 }
2862
2863 btrfs_free_path(path);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002864 atomic_inc(&fs_info->defrag_running);
Liu Bo38c227d2013-01-29 03:18:40 +00002865
2866 return new;
2867
Liu Bo38c227d2013-01-29 03:18:40 +00002868out_free_path:
2869 btrfs_free_path(path);
2870out_kfree:
Liu Bo6f519562013-10-29 10:45:05 +08002871 free_sa_defrag_extent(new);
Liu Bo38c227d2013-01-29 03:18:40 +00002872 return NULL;
2873}
2874
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002875static void btrfs_release_delalloc_bytes(struct btrfs_fs_info *fs_info,
Miao Xiee570fd22014-06-19 10:42:50 +08002876 u64 start, u64 len)
2877{
2878 struct btrfs_block_group_cache *cache;
2879
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002880 cache = btrfs_lookup_block_group(fs_info, start);
Miao Xiee570fd22014-06-19 10:42:50 +08002881 ASSERT(cache);
2882
2883 spin_lock(&cache->lock);
2884 cache->delalloc_bytes -= len;
2885 spin_unlock(&cache->lock);
2886
2887 btrfs_put_block_group(cache);
2888}
2889
Chris Masond352ac62008-09-29 15:18:18 -04002890/* as ordered data IO finishes, this gets called so we can finish
2891 * an ordered extent if the range of bytes in the file it covers are
2892 * fully written.
2893 */
Josef Bacik5fd02042012-05-02 14:00:54 -04002894static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002895{
Josef Bacik5fd02042012-05-02 14:00:54 -04002896 struct inode *inode = ordered_extent->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002897 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002898 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002899 struct btrfs_trans_handle *trans = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002900 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002901 struct extent_state *cached_state = NULL;
Liu Bo38c227d2013-01-29 03:18:40 +00002902 struct new_sa_defrag_extent *new = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08002903 int compress_type = 0;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002904 int ret = 0;
2905 u64 logical_len = ordered_extent->len;
Li Zefan82d59022011-04-20 10:33:24 +08002906 bool nolock;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002907 bool truncated = false;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002908 bool range_locked = false;
2909 bool clear_new_delalloc_bytes = false;
Josef Bacik49940bd2018-10-11 15:54:21 -04002910 bool clear_reserved_extent = true;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002911
2912 if (!test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
2913 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags) &&
2914 !test_bit(BTRFS_ORDERED_DIRECT, &ordered_extent->flags))
2915 clear_new_delalloc_bytes = true;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002916
Nikolay Borisov70ddc552017-02-20 13:50:35 +02002917 nolock = btrfs_is_free_space_inode(BTRFS_I(inode));
Josef Bacik0cb59c92010-07-02 12:14:14 -04002918
Josef Bacik5fd02042012-05-02 14:00:54 -04002919 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
2920 ret = -EIO;
2921 goto out;
2922 }
2923
Nikolay Borisov7ab79562017-02-20 13:50:57 +02002924 btrfs_free_io_failure_record(BTRFS_I(inode),
2925 ordered_extent->file_offset,
2926 ordered_extent->file_offset +
2927 ordered_extent->len - 1);
Miao Xief6124962014-09-12 18:44:04 +08002928
Josef Bacik77cef2e2013-08-29 13:57:21 -04002929 if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) {
2930 truncated = true;
2931 logical_len = ordered_extent->truncated_len;
2932 /* Truncated the entire extent, don't bother adding */
2933 if (!logical_len)
2934 goto out;
2935 }
2936
Yan, Zhengc2167752009-11-12 09:34:21 +00002937 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002938 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
Qu Wenruo94ed9382015-09-08 17:25:56 +08002939
2940 /*
2941 * For mwrite(mmap + memset to write) case, we still reserve
2942 * space for NOCOW range.
2943 * As NOCOW won't cause a new delayed ref, just free the space
2944 */
Qu Wenruobc42bda2017-02-27 15:10:39 +08002945 btrfs_qgroup_free_data(inode, NULL, ordered_extent->file_offset,
Qu Wenruo94ed9382015-09-08 17:25:56 +08002946 ordered_extent->len);
Josef Bacik6c760c02012-11-09 10:53:21 -05002947 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2948 if (nolock)
2949 trans = btrfs_join_transaction_nolock(root);
2950 else
2951 trans = btrfs_join_transaction(root);
2952 if (IS_ERR(trans)) {
2953 ret = PTR_ERR(trans);
2954 trans = NULL;
2955 goto out;
Yan, Zhengc2167752009-11-12 09:34:21 +00002956 }
Josef Bacik69fe2d72017-10-19 14:15:57 -04002957 trans->block_rsv = &BTRFS_I(inode)->block_rsv;
Josef Bacik6c760c02012-11-09 10:53:21 -05002958 ret = btrfs_update_inode_fallback(trans, root, inode);
2959 if (ret) /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04002960 btrfs_abort_transaction(trans, ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00002961 goto out;
2962 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002963
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002964 range_locked = true;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002965 lock_extent_bits(io_tree, ordered_extent->file_offset,
2966 ordered_extent->file_offset + ordered_extent->len - 1,
David Sterbaff13db42015-12-03 14:30:40 +01002967 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002968
Liu Bo38c227d2013-01-29 03:18:40 +00002969 ret = test_range_bit(io_tree, ordered_extent->file_offset,
2970 ordered_extent->file_offset + ordered_extent->len - 1,
Liu Bo452e62b2017-05-26 17:44:23 -06002971 EXTENT_DEFRAG, 0, cached_state);
Liu Bo38c227d2013-01-29 03:18:40 +00002972 if (ret) {
2973 u64 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
Josef Bacik8101c8d2014-01-29 16:05:30 -05002974 if (0 && last_snapshot >= BTRFS_I(inode)->generation)
Liu Bo38c227d2013-01-29 03:18:40 +00002975 /* the inode is shared */
2976 new = record_old_file_extents(inode, ordered_extent);
2977
2978 clear_extent_bit(io_tree, ordered_extent->file_offset,
2979 ordered_extent->file_offset + ordered_extent->len - 1,
David Sterbaae0f1622017-10-31 16:37:52 +01002980 EXTENT_DEFRAG, 0, 0, &cached_state);
Liu Bo38c227d2013-01-29 03:18:40 +00002981 }
2982
Josef Bacik0cb59c92010-07-02 12:14:14 -04002983 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002984 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002985 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002986 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002987 if (IS_ERR(trans)) {
2988 ret = PTR_ERR(trans);
2989 trans = NULL;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002990 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002991 }
Chris Masona79b7d42014-05-22 16:18:52 -07002992
Josef Bacik69fe2d72017-10-19 14:15:57 -04002993 trans->block_rsv = &BTRFS_I(inode)->block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00002994
Chris Masonc8b97812008-10-29 14:49:59 -04002995 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08002996 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04002997 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08002998 BUG_ON(compress_type);
Justin Maggardb430b772017-10-30 15:29:10 -07002999 btrfs_qgroup_free_data(inode, NULL, ordered_extent->file_offset,
3000 ordered_extent->len);
Nikolay Borisov7a6d7062017-02-20 13:50:48 +02003001 ret = btrfs_mark_extent_written(trans, BTRFS_I(inode),
Yan Zhengd899e052008-10-30 14:25:28 -04003002 ordered_extent->file_offset,
3003 ordered_extent->file_offset +
Josef Bacik77cef2e2013-08-29 13:57:21 -04003004 logical_len);
Yan Zhengd899e052008-10-30 14:25:28 -04003005 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003006 BUG_ON(root == fs_info->tree_root);
Yan Zhengd899e052008-10-30 14:25:28 -04003007 ret = insert_reserved_file_extent(trans, inode,
3008 ordered_extent->file_offset,
3009 ordered_extent->start,
3010 ordered_extent->disk_len,
Josef Bacik77cef2e2013-08-29 13:57:21 -04003011 logical_len, logical_len,
Li Zefan261507a02010-12-17 14:21:50 +08003012 compress_type, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04003013 BTRFS_FILE_EXTENT_REG);
Josef Bacik49940bd2018-10-11 15:54:21 -04003014 if (!ret) {
3015 clear_reserved_extent = false;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003016 btrfs_release_delalloc_bytes(fs_info,
Miao Xiee570fd22014-06-19 10:42:50 +08003017 ordered_extent->start,
3018 ordered_extent->disk_len);
Josef Bacik49940bd2018-10-11 15:54:21 -04003019 }
Yan Zhengd899e052008-10-30 14:25:28 -04003020 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04003021 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
3022 ordered_extent->file_offset, ordered_extent->len,
3023 trans->transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003024 if (ret < 0) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003025 btrfs_abort_transaction(trans, ret);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003026 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003027 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00003028
Nikolay Borisovac01f262018-01-08 10:59:43 +02003029 ret = add_pending_csums(trans, inode, &ordered_extent->list);
3030 if (ret) {
3031 btrfs_abort_transaction(trans, ret);
3032 goto out;
3033 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04003034
Josef Bacik6c760c02012-11-09 10:53:21 -05003035 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
3036 ret = btrfs_update_inode_fallback(trans, root, inode);
3037 if (ret) { /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04003038 btrfs_abort_transaction(trans, ret);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003039 goto out;
Josef Bacik1ef30be2011-04-05 19:25:36 -04003040 }
3041 ret = 0;
Yan, Zhengc2167752009-11-12 09:34:21 +00003042out:
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003043 if (range_locked || clear_new_delalloc_bytes) {
3044 unsigned int clear_bits = 0;
3045
3046 if (range_locked)
3047 clear_bits |= EXTENT_LOCKED;
3048 if (clear_new_delalloc_bytes)
3049 clear_bits |= EXTENT_DELALLOC_NEW;
3050 clear_extent_bit(&BTRFS_I(inode)->io_tree,
3051 ordered_extent->file_offset,
3052 ordered_extent->file_offset +
3053 ordered_extent->len - 1,
3054 clear_bits,
3055 (clear_bits & EXTENT_LOCKED) ? 1 : 0,
David Sterbaae0f1622017-10-31 16:37:52 +01003056 0, &cached_state);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003057 }
3058
Miao Xiea698d0752012-09-20 01:51:59 -06003059 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003060 btrfs_end_transaction(trans);
Josef Bacik0cb59c92010-07-02 12:14:14 -04003061
Josef Bacik77cef2e2013-08-29 13:57:21 -04003062 if (ret || truncated) {
3063 u64 start, end;
3064
3065 if (truncated)
3066 start = ordered_extent->file_offset + logical_len;
3067 else
3068 start = ordered_extent->file_offset;
3069 end = ordered_extent->file_offset + ordered_extent->len - 1;
David Sterbaf08dc362017-10-31 17:02:39 +01003070 clear_extent_uptodate(io_tree, start, end, NULL);
Josef Bacik77cef2e2013-08-29 13:57:21 -04003071
3072 /* Drop the cache for the part of the extent we didn't write. */
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02003073 btrfs_drop_extent_cache(BTRFS_I(inode), start, end, 0);
Josef Bacik5fd02042012-05-02 14:00:54 -04003074
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003075 /*
3076 * If the ordered extent had an IOERR or something else went
3077 * wrong we need to return the space for this ordered extent
Josef Bacik77cef2e2013-08-29 13:57:21 -04003078 * back to the allocator. We only free the extent in the
3079 * truncated case if we didn't write out the extent at all.
Josef Bacik49940bd2018-10-11 15:54:21 -04003080 *
3081 * If we made it past insert_reserved_file_extent before we
3082 * errored out then we don't need to do this as the accounting
3083 * has already been done.
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003084 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04003085 if ((ret || !logical_len) &&
Josef Bacik49940bd2018-10-11 15:54:21 -04003086 clear_reserved_extent &&
Josef Bacik77cef2e2013-08-29 13:57:21 -04003087 !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003088 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003089 btrfs_free_reserved_extent(fs_info,
3090 ordered_extent->start,
Miao Xiee570fd22014-06-19 10:42:50 +08003091 ordered_extent->disk_len, 1);
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003092 }
3093
3094
Josef Bacik5fd02042012-05-02 14:00:54 -04003095 /*
Liu Bo8bad3c02012-06-18 12:14:23 +08003096 * This needs to be done to make sure anybody waiting knows we are done
3097 * updating everything for this ordered extent.
Josef Bacik5fd02042012-05-02 14:00:54 -04003098 */
3099 btrfs_remove_ordered_extent(inode, ordered_extent);
3100
Liu Bo38c227d2013-01-29 03:18:40 +00003101 /* for snapshot-aware defrag */
Liu Bo6f519562013-10-29 10:45:05 +08003102 if (new) {
3103 if (ret) {
3104 free_sa_defrag_extent(new);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003105 atomic_dec(&fs_info->defrag_running);
Liu Bo6f519562013-10-29 10:45:05 +08003106 } else {
3107 relink_file_extents(new);
3108 }
3109 }
Liu Bo38c227d2013-01-29 03:18:40 +00003110
Chris Masone6dcd2d2008-07-17 12:53:50 -04003111 /* once for us */
3112 btrfs_put_ordered_extent(ordered_extent);
3113 /* once for the tree */
3114 btrfs_put_ordered_extent(ordered_extent);
3115
Ethan Liene73e81b2018-05-28 13:48:20 +08003116 /* Try to release some metadata so we don't get an OOM but don't wait */
3117 btrfs_btree_balance_dirty_nodelay(fs_info);
3118
Josef Bacik5fd02042012-05-02 14:00:54 -04003119 return ret;
3120}
3121
3122static void finish_ordered_fn(struct btrfs_work *work)
3123{
3124 struct btrfs_ordered_extent *ordered_extent;
3125 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
3126 btrfs_finish_ordered_io(ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003127}
3128
Nikolay Borisovc6297322018-11-08 10:18:08 +02003129void btrfs_writepage_endio_finish_ordered(struct page *page, u64 start,
3130 u64 end, int uptodate)
Chris Mason211f90e2008-07-18 11:56:15 -04003131{
Josef Bacik5fd02042012-05-02 14:00:54 -04003132 struct inode *inode = page->mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003133 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik5fd02042012-05-02 14:00:54 -04003134 struct btrfs_ordered_extent *ordered_extent = NULL;
Liu Bo9e0af232014-08-15 23:36:53 +08003135 struct btrfs_workqueue *wq;
3136 btrfs_work_func_t func;
Josef Bacik5fd02042012-05-02 14:00:54 -04003137
liubo1abe9b82011-03-24 11:18:59 +00003138 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
3139
Chris Mason8b62b722009-09-02 16:53:46 -04003140 ClearPagePrivate2(page);
Josef Bacik5fd02042012-05-02 14:00:54 -04003141 if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
3142 end - start + 1, uptodate))
David Sterbac3988d62017-02-17 15:18:32 +01003143 return;
Josef Bacik5fd02042012-05-02 14:00:54 -04003144
Nikolay Borisov70ddc552017-02-20 13:50:35 +02003145 if (btrfs_is_free_space_inode(BTRFS_I(inode))) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003146 wq = fs_info->endio_freespace_worker;
Liu Bo9e0af232014-08-15 23:36:53 +08003147 func = btrfs_freespace_write_helper;
3148 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003149 wq = fs_info->endio_write_workers;
Liu Bo9e0af232014-08-15 23:36:53 +08003150 func = btrfs_endio_write_helper;
3151 }
Josef Bacik5fd02042012-05-02 14:00:54 -04003152
Liu Bo9e0af232014-08-15 23:36:53 +08003153 btrfs_init_work(&ordered_extent->work, func, finish_ordered_fn, NULL,
3154 NULL);
3155 btrfs_queue_work(wq, &ordered_extent->work);
Chris Mason211f90e2008-07-18 11:56:15 -04003156}
3157
Miao Xiedc380ae2014-09-12 18:43:55 +08003158static int __readpage_endio_check(struct inode *inode,
3159 struct btrfs_io_bio *io_bio,
3160 int icsum, struct page *page,
3161 int pgoff, u64 start, size_t len)
3162{
3163 char *kaddr;
3164 u32 csum_expected;
3165 u32 csum = ~(u32)0;
Miao Xiedc380ae2014-09-12 18:43:55 +08003166
3167 csum_expected = *(((u32 *)io_bio->csum) + icsum);
3168
3169 kaddr = kmap_atomic(page);
3170 csum = btrfs_csum_data(kaddr + pgoff, csum, len);
Domagoj Tršan0b5e3da2016-10-27 08:52:33 +01003171 btrfs_csum_final(csum, (u8 *)&csum);
Miao Xiedc380ae2014-09-12 18:43:55 +08003172 if (csum != csum_expected)
3173 goto zeroit;
3174
3175 kunmap_atomic(kaddr);
3176 return 0;
3177zeroit:
Nikolay Borisov0970a222017-02-20 13:50:53 +02003178 btrfs_print_data_csum_error(BTRFS_I(inode), start, csum, csum_expected,
Qu Wenruo6f6b6432017-02-09 10:45:06 +08003179 io_bio->mirror_num);
Miao Xiedc380ae2014-09-12 18:43:55 +08003180 memset(kaddr + pgoff, 1, len);
3181 flush_dcache_page(page);
3182 kunmap_atomic(kaddr);
Miao Xiedc380ae2014-09-12 18:43:55 +08003183 return -EIO;
3184}
3185
Chris Masond352ac62008-09-29 15:18:18 -04003186/*
Chris Masond352ac62008-09-29 15:18:18 -04003187 * when reads are done, we need to check csums to verify the data is correct
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02003188 * if there's a match, we allow the bio to finish. If not, the code in
3189 * extent_io.c will try to find good copies for us.
Chris Masond352ac62008-09-29 15:18:18 -04003190 */
Miao Xiefacc8a222013-07-25 19:22:34 +08003191static int btrfs_readpage_end_io_hook(struct btrfs_io_bio *io_bio,
3192 u64 phy_offset, struct page *page,
3193 u64 start, u64 end, int mirror)
Chris Mason07157aa2007-08-30 08:50:51 -04003194{
Miao Xie4eee4fa2012-12-21 09:17:45 +00003195 size_t offset = start - page_offset(page);
Chris Mason07157aa2007-08-30 08:50:51 -04003196 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05003197 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masonff79f812007-10-15 16:22:25 -04003198 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -05003199
Chris Masond20f7042008-12-08 16:58:54 -05003200 if (PageChecked(page)) {
3201 ClearPageChecked(page);
Miao Xiedc380ae2014-09-12 18:43:55 +08003202 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003203 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003204
3205 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Miao Xiedc380ae2014-09-12 18:43:55 +08003206 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003207
Yan Zheng17d217f2008-12-12 10:03:38 -05003208 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04003209 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
David Sterba91166212016-04-26 23:54:39 +02003210 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM);
Yan Zheng17d217f2008-12-12 10:03:38 -05003211 return 0;
3212 }
3213
Miao Xiefacc8a222013-07-25 19:22:34 +08003214 phy_offset >>= inode->i_sb->s_blocksize_bits;
Miao Xiedc380ae2014-09-12 18:43:55 +08003215 return __readpage_endio_check(inode, io_bio, phy_offset, page, offset,
3216 start, (size_t)(end - start + 1));
Chris Mason07157aa2007-08-30 08:50:51 -04003217}
Chris Masonb888db22007-08-27 16:49:44 -04003218
Nikolay Borisovc1c3fac2018-01-16 09:31:58 +02003219/*
3220 * btrfs_add_delayed_iput - perform a delayed iput on @inode
3221 *
3222 * @inode: The inode we want to perform iput on
3223 *
3224 * This function uses the generic vfs_inode::i_count to track whether we should
3225 * just decrement it (in case it's > 1) or if this is the last iput then link
3226 * the inode to the delayed iput machinery. Delayed iputs are processed at
3227 * transaction commit time/superblock commit/cleaner kthread.
3228 */
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003229void btrfs_add_delayed_iput(struct inode *inode)
3230{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003231 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
David Sterba8089fe62015-11-19 14:15:51 +01003232 struct btrfs_inode *binode = BTRFS_I(inode);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003233
3234 if (atomic_add_unless(&inode->i_count, -1, 1))
3235 return;
3236
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003237 spin_lock(&fs_info->delayed_iput_lock);
Nikolay Borisovc1c3fac2018-01-16 09:31:58 +02003238 ASSERT(list_empty(&binode->delayed_iput));
3239 list_add_tail(&binode->delayed_iput, &fs_info->delayed_iputs);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003240 spin_unlock(&fs_info->delayed_iput_lock);
3241}
3242
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003243void btrfs_run_delayed_iputs(struct btrfs_fs_info *fs_info)
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003244{
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003245
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003246 spin_lock(&fs_info->delayed_iput_lock);
David Sterba8089fe62015-11-19 14:15:51 +01003247 while (!list_empty(&fs_info->delayed_iputs)) {
3248 struct btrfs_inode *inode;
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003249
David Sterba8089fe62015-11-19 14:15:51 +01003250 inode = list_first_entry(&fs_info->delayed_iputs,
3251 struct btrfs_inode, delayed_iput);
Nikolay Borisovc1c3fac2018-01-16 09:31:58 +02003252 list_del_init(&inode->delayed_iput);
David Sterba8089fe62015-11-19 14:15:51 +01003253 spin_unlock(&fs_info->delayed_iput_lock);
3254 iput(&inode->vfs_inode);
3255 spin_lock(&fs_info->delayed_iput_lock);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003256 }
David Sterba8089fe62015-11-19 14:15:51 +01003257 spin_unlock(&fs_info->delayed_iput_lock);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003258}
3259
Yan, Zhengd68fc572010-05-16 10:49:58 -04003260/*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003261 * This creates an orphan entry for the given inode in case something goes wrong
3262 * in the middle of an unlink.
Josef Bacik7b128762008-07-24 12:17:14 -04003263 */
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003264int btrfs_orphan_add(struct btrfs_trans_handle *trans,
Omar Sandoval27919062018-05-11 13:13:37 -07003265 struct btrfs_inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003266{
Yan, Zhengd68fc572010-05-16 10:49:58 -04003267 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003268
Omar Sandoval27919062018-05-11 13:13:37 -07003269 ret = btrfs_insert_orphan_item(trans, inode->root, btrfs_ino(inode));
3270 if (ret && ret != -EEXIST) {
3271 btrfs_abort_transaction(trans, ret);
3272 return ret;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003273 }
3274
Yan, Zhengd68fc572010-05-16 10:49:58 -04003275 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003276}
3277
3278/*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003279 * We have done the delete so we can go ahead and remove the orphan item for
3280 * this particular inode.
Josef Bacik7b128762008-07-24 12:17:14 -04003281 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00003282static int btrfs_orphan_del(struct btrfs_trans_handle *trans,
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003283 struct btrfs_inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003284{
Omar Sandoval27919062018-05-11 13:13:37 -07003285 return btrfs_del_orphan_item(trans, inode->root, btrfs_ino(inode));
Josef Bacik7b128762008-07-24 12:17:14 -04003286}
3287
3288/*
3289 * this cleans up any orphans that may be left on the list from the last use
3290 * of this root.
3291 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003292int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04003293{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003294 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik7b128762008-07-24 12:17:14 -04003295 struct btrfs_path *path;
3296 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04003297 struct btrfs_key key, found_key;
3298 struct btrfs_trans_handle *trans;
3299 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003300 u64 last_objectid = 0;
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003301 int ret = 0, nr_unlink = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003302
Yan, Zhengd68fc572010-05-16 10:49:58 -04003303 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003304 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00003305
3306 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003307 if (!path) {
3308 ret = -ENOMEM;
3309 goto out;
3310 }
David Sterbae4058b52015-11-27 16:31:35 +01003311 path->reada = READA_BACK;
Josef Bacik7b128762008-07-24 12:17:14 -04003312
3313 key.objectid = BTRFS_ORPHAN_OBJECTID;
David Sterba962a2982014-06-04 18:41:45 +02003314 key.type = BTRFS_ORPHAN_ITEM_KEY;
Josef Bacik7b128762008-07-24 12:17:14 -04003315 key.offset = (u64)-1;
3316
Josef Bacik7b128762008-07-24 12:17:14 -04003317 while (1) {
3318 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003319 if (ret < 0)
3320 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003321
3322 /*
3323 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003324 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04003325 * find the key and see if we have stuff that matches
3326 */
3327 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003328 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003329 if (path->slots[0] == 0)
3330 break;
3331 path->slots[0]--;
3332 }
3333
3334 /* pull out the item */
3335 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04003336 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3337
3338 /* make sure the item matches what we want */
3339 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
3340 break;
David Sterba962a2982014-06-04 18:41:45 +02003341 if (found_key.type != BTRFS_ORPHAN_ITEM_KEY)
Josef Bacik7b128762008-07-24 12:17:14 -04003342 break;
3343
3344 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02003345 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04003346
3347 /*
3348 * this is where we are basically btrfs_lookup, without the
3349 * crossing root thing. we store the inode number in the
3350 * offset of the orphan item.
3351 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003352
3353 if (found_key.offset == last_objectid) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003354 btrfs_err(fs_info,
3355 "Error removing orphan entry, stopping orphan cleanup");
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003356 ret = -EINVAL;
3357 goto out;
3358 }
3359
3360 last_objectid = found_key.offset;
3361
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003362 found_key.objectid = found_key.offset;
3363 found_key.type = BTRFS_INODE_ITEM_KEY;
3364 found_key.offset = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003365 inode = btrfs_iget(fs_info->sb, &found_key, root, NULL);
Rusty Russell8c6ffba2013-07-15 11:20:32 +09303366 ret = PTR_ERR_OR_ZERO(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003367 if (ret && ret != -ENOENT)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003368 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003369
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003370 if (ret == -ENOENT && root == fs_info->tree_root) {
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003371 struct btrfs_root *dead_root;
3372 struct btrfs_fs_info *fs_info = root->fs_info;
3373 int is_dead_root = 0;
3374
3375 /*
3376 * this is an orphan in the tree root. Currently these
3377 * could come from 2 sources:
3378 * a) a snapshot deletion in progress
3379 * b) a free space cache inode
3380 * We need to distinguish those two, as the snapshot
3381 * orphan must not get deleted.
3382 * find_dead_roots already ran before us, so if this
3383 * is a snapshot deletion, we should find the root
3384 * in the dead_roots list
3385 */
3386 spin_lock(&fs_info->trans_lock);
3387 list_for_each_entry(dead_root, &fs_info->dead_roots,
3388 root_list) {
3389 if (dead_root->root_key.objectid ==
3390 found_key.objectid) {
3391 is_dead_root = 1;
3392 break;
3393 }
3394 }
3395 spin_unlock(&fs_info->trans_lock);
3396 if (is_dead_root) {
3397 /* prevent this orphan from being found again */
3398 key.offset = found_key.objectid - 1;
3399 continue;
3400 }
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003401
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003402 }
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003403
Josef Bacika8c9e572011-09-21 16:55:59 -04003404 /*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003405 * If we have an inode with links, there are a couple of
3406 * possibilities. Old kernels (before v3.12) used to create an
3407 * orphan item for truncate indicating that there were possibly
3408 * extent items past i_size that needed to be deleted. In v3.12,
3409 * truncate was changed to update i_size in sync with the extent
3410 * items, but the (useless) orphan item was still created. Since
3411 * v4.18, we don't create the orphan item for truncate at all.
3412 *
3413 * So, this item could mean that we need to do a truncate, but
3414 * only if this filesystem was last used on a pre-v3.12 kernel
3415 * and was not cleanly unmounted. The odds of that are quite
3416 * slim, and it's a pain to do the truncate now, so just delete
3417 * the orphan item.
3418 *
3419 * It's also possible that this orphan item was supposed to be
3420 * deleted but wasn't. The inode number may have been reused,
3421 * but either way, we can delete the orphan item.
Josef Bacika8c9e572011-09-21 16:55:59 -04003422 */
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003423 if (ret == -ENOENT || inode->i_nlink) {
3424 if (!ret)
3425 iput(inode);
Josef Bacika8c9e572011-09-21 16:55:59 -04003426 trans = btrfs_start_transaction(root, 1);
3427 if (IS_ERR(trans)) {
3428 ret = PTR_ERR(trans);
3429 goto out;
3430 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003431 btrfs_debug(fs_info, "auto deleting %Lu",
3432 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003433 ret = btrfs_del_orphan_item(trans, root,
3434 found_key.objectid);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003435 btrfs_end_transaction(trans);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003436 if (ret)
3437 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003438 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003439 }
Josef Bacik7b128762008-07-24 12:17:14 -04003440
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003441 nr_unlink++;
Josef Bacik7b128762008-07-24 12:17:14 -04003442
3443 /* this will do delete_inode and everything for us */
3444 iput(inode);
3445 }
Miao Xie3254c872011-11-10 20:45:05 -05003446 /* release the path since we're done with it */
3447 btrfs_release_path(path);
3448
Yan, Zhengd68fc572010-05-16 10:49:58 -04003449 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
3450
Omar Sandovala575cee2018-05-11 13:13:38 -07003451 if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003452 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003453 if (!IS_ERR(trans))
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003454 btrfs_end_transaction(trans);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003455 }
Josef Bacik7b128762008-07-24 12:17:14 -04003456
3457 if (nr_unlink)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003458 btrfs_debug(fs_info, "unlinked %d orphans", nr_unlink);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003459
3460out:
3461 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003462 btrfs_err(fs_info, "could not do orphan cleanup %d", ret);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003463 btrfs_free_path(path);
3464 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003465}
3466
Chris Masond352ac62008-09-29 15:18:18 -04003467/*
Chris Mason46a53cc2009-04-27 11:47:50 -04003468 * very simple check to peek ahead in the leaf looking for xattrs. If we
3469 * don't find any xattrs, we know there can't be any acls.
3470 *
3471 * slot is the slot the inode is in, objectid is the objectid of the inode
3472 */
3473static noinline int acls_after_inode_item(struct extent_buffer *leaf,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003474 int slot, u64 objectid,
3475 int *first_xattr_slot)
Chris Mason46a53cc2009-04-27 11:47:50 -04003476{
3477 u32 nritems = btrfs_header_nritems(leaf);
3478 struct btrfs_key found_key;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003479 static u64 xattr_access = 0;
3480 static u64 xattr_default = 0;
Chris Mason46a53cc2009-04-27 11:47:50 -04003481 int scanned = 0;
3482
Josef Bacikf23b5a52013-06-19 10:16:26 -04003483 if (!xattr_access) {
Andreas Gruenbacher97d79292015-12-02 14:44:35 +01003484 xattr_access = btrfs_name_hash(XATTR_NAME_POSIX_ACL_ACCESS,
3485 strlen(XATTR_NAME_POSIX_ACL_ACCESS));
3486 xattr_default = btrfs_name_hash(XATTR_NAME_POSIX_ACL_DEFAULT,
3487 strlen(XATTR_NAME_POSIX_ACL_DEFAULT));
Josef Bacikf23b5a52013-06-19 10:16:26 -04003488 }
3489
Chris Mason46a53cc2009-04-27 11:47:50 -04003490 slot++;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003491 *first_xattr_slot = -1;
Chris Mason46a53cc2009-04-27 11:47:50 -04003492 while (slot < nritems) {
3493 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3494
3495 /* we found a different objectid, there must not be acls */
3496 if (found_key.objectid != objectid)
3497 return 0;
3498
3499 /* we found an xattr, assume we've got an acl */
Josef Bacikf23b5a52013-06-19 10:16:26 -04003500 if (found_key.type == BTRFS_XATTR_ITEM_KEY) {
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003501 if (*first_xattr_slot == -1)
3502 *first_xattr_slot = slot;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003503 if (found_key.offset == xattr_access ||
3504 found_key.offset == xattr_default)
3505 return 1;
3506 }
Chris Mason46a53cc2009-04-27 11:47:50 -04003507
3508 /*
3509 * we found a key greater than an xattr key, there can't
3510 * be any acls later on
3511 */
3512 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
3513 return 0;
3514
3515 slot++;
3516 scanned++;
3517
3518 /*
3519 * it goes inode, inode backrefs, xattrs, extents,
3520 * so if there are a ton of hard links to an inode there can
3521 * be a lot of backrefs. Don't waste time searching too hard,
3522 * this is just an optimization
3523 */
3524 if (scanned >= 8)
3525 break;
3526 }
3527 /* we hit the end of the leaf before we found an xattr or
3528 * something larger than an xattr. We have to assume the inode
3529 * has acls
3530 */
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003531 if (*first_xattr_slot == -1)
3532 *first_xattr_slot = slot;
Chris Mason46a53cc2009-04-27 11:47:50 -04003533 return 1;
3534}
3535
3536/*
Chris Masond352ac62008-09-29 15:18:18 -04003537 * read an inode from the btree into the in-memory inode
3538 */
Filipe Manana4222ea72018-10-24 10:13:03 +01003539static int btrfs_read_locked_inode(struct inode *inode,
3540 struct btrfs_path *in_path)
Chris Mason39279cc2007-06-12 06:35:45 -04003541{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003542 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Filipe Manana4222ea72018-10-24 10:13:03 +01003543 struct btrfs_path *path = in_path;
Chris Mason5f39d392007-10-15 16:14:19 -04003544 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003545 struct btrfs_inode_item *inode_item;
3546 struct btrfs_root *root = BTRFS_I(inode)->root;
3547 struct btrfs_key location;
Miao Xie67de1172013-12-26 13:07:06 +08003548 unsigned long ptr;
Chris Mason46a53cc2009-04-27 11:47:50 -04003549 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04003550 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04003551 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003552 bool filled = false;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003553 int first_xattr_slot;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003554
3555 ret = btrfs_fill_inode(inode, &rdev);
3556 if (!ret)
3557 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04003558
Filipe Manana4222ea72018-10-24 10:13:03 +01003559 if (!path) {
3560 path = btrfs_alloc_path();
3561 if (!path)
3562 return -ENOMEM;
3563 }
Mark Fasheh1748f842011-07-12 11:25:31 -07003564
Chris Mason39279cc2007-06-12 06:35:45 -04003565 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05003566
Chris Mason39279cc2007-06-12 06:35:45 -04003567 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Filipe Manana67710892016-06-06 11:51:25 +01003568 if (ret) {
Filipe Manana4222ea72018-10-24 10:13:03 +01003569 if (path != in_path)
3570 btrfs_free_path(path);
Al Virof5b3a412018-07-29 23:04:51 +01003571 return ret;
Filipe Manana67710892016-06-06 11:51:25 +01003572 }
Chris Mason39279cc2007-06-12 06:35:45 -04003573
Chris Mason5f39d392007-10-15 16:14:19 -04003574 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00003575
3576 if (filled)
Miao Xie67de1172013-12-26 13:07:06 +08003577 goto cache_index;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003578
Chris Mason5f39d392007-10-15 16:14:19 -04003579 inode_item = btrfs_item_ptr(leaf, path->slots[0],
3580 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003581 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02003582 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08003583 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
3584 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02003585 btrfs_i_size_write(BTRFS_I(inode), btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04003586
David Sterbaa937b972014-12-12 17:39:12 +01003587 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->atime);
3588 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->atime);
Chris Mason5f39d392007-10-15 16:14:19 -04003589
David Sterbaa937b972014-12-12 17:39:12 +01003590 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->mtime);
3591 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->mtime);
Chris Mason5f39d392007-10-15 16:14:19 -04003592
David Sterbaa937b972014-12-12 17:39:12 +01003593 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->ctime);
3594 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->ctime);
Chris Mason5f39d392007-10-15 16:14:19 -04003595
chandan r9cc97d62012-07-04 12:48:07 +05303596 BTRFS_I(inode)->i_otime.tv_sec =
3597 btrfs_timespec_sec(leaf, &inode_item->otime);
3598 BTRFS_I(inode)->i_otime.tv_nsec =
3599 btrfs_timespec_nsec(leaf, &inode_item->otime);
Chris Mason5f39d392007-10-15 16:14:19 -04003600
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003601 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003602 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003603 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
3604
Jeff Laytonc7f88c42017-12-11 06:35:12 -05003605 inode_set_iversion_queried(inode,
3606 btrfs_inode_sequence(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003607 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04003608 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003609 rdev = btrfs_inode_rdev(leaf, inode_item);
3610
Josef Bacikaec74772008-07-24 12:12:38 -04003611 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05003612 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Miao Xie67de1172013-12-26 13:07:06 +08003613
3614cache_index:
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003615 /*
3616 * If we were modified in the current generation and evicted from memory
3617 * and then re-read we need to do a full sync since we don't have any
3618 * idea about which extents were modified before we were evicted from
3619 * cache.
3620 *
3621 * This is required for both inode re-read from disk and delayed inode
3622 * in delayed_nodes_tree.
3623 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003624 if (BTRFS_I(inode)->last_trans == fs_info->generation)
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003625 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3626 &BTRFS_I(inode)->runtime_flags);
3627
Filipe Mananabde6c242015-07-24 00:00:19 +01003628 /*
3629 * We don't persist the id of the transaction where an unlink operation
3630 * against the inode was last made. So here we assume the inode might
3631 * have been evicted, and therefore the exact value of last_unlink_trans
3632 * lost, and set it to last_trans to avoid metadata inconsistencies
3633 * between the inode and its parent if the inode is fsync'ed and the log
3634 * replayed. For example, in the scenario:
3635 *
3636 * touch mydir/foo
3637 * ln mydir/foo mydir/bar
3638 * sync
3639 * unlink mydir/bar
3640 * echo 2 > /proc/sys/vm/drop_caches # evicts inode
3641 * xfs_io -c fsync mydir/foo
3642 * <power failure>
3643 * mount fs, triggers fsync log replay
3644 *
3645 * We must make sure that when we fsync our inode foo we also log its
3646 * parent inode, otherwise after log replay the parent still has the
3647 * dentry with the "bar" name but our inode foo has a link count of 1
3648 * and doesn't have an inode ref with the name "bar" anymore.
3649 *
3650 * Setting last_unlink_trans to last_trans is a pessimistic approach,
Nicholas D Steeves01327612016-05-19 21:18:45 -04003651 * but it guarantees correctness at the expense of occasional full
Filipe Mananabde6c242015-07-24 00:00:19 +01003652 * transaction commits on fsync if our inode is a directory, or if our
3653 * inode is not a directory, logging its parent unnecessarily.
3654 */
3655 BTRFS_I(inode)->last_unlink_trans = BTRFS_I(inode)->last_trans;
3656
Miao Xie67de1172013-12-26 13:07:06 +08003657 path->slots[0]++;
3658 if (inode->i_nlink != 1 ||
3659 path->slots[0] >= btrfs_header_nritems(leaf))
3660 goto cache_acl;
3661
3662 btrfs_item_key_to_cpu(leaf, &location, path->slots[0]);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003663 if (location.objectid != btrfs_ino(BTRFS_I(inode)))
Miao Xie67de1172013-12-26 13:07:06 +08003664 goto cache_acl;
3665
3666 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3667 if (location.type == BTRFS_INODE_REF_KEY) {
3668 struct btrfs_inode_ref *ref;
3669
3670 ref = (struct btrfs_inode_ref *)ptr;
3671 BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref);
3672 } else if (location.type == BTRFS_INODE_EXTREF_KEY) {
3673 struct btrfs_inode_extref *extref;
3674
3675 extref = (struct btrfs_inode_extref *)ptr;
3676 BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf,
3677 extref);
3678 }
Miao Xie2f7e33d2011-06-23 07:27:13 +00003679cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04003680 /*
3681 * try to precache a NULL acl entry for files that don't have
3682 * any xattrs or acls
3683 */
Li Zefan33345d012011-04-20 10:31:50 +08003684 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
David Sterbaf85b7372017-01-20 14:54:07 +01003685 btrfs_ino(BTRFS_I(inode)), &first_xattr_slot);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003686 if (first_xattr_slot != -1) {
3687 path->slots[0] = first_xattr_slot;
3688 ret = btrfs_load_inode_props(inode, path);
3689 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003690 btrfs_err(fs_info,
David Sterba351fd352014-05-15 16:48:20 +02003691 "error loading props for ino %llu (root %llu): %d",
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003692 btrfs_ino(BTRFS_I(inode)),
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003693 root->root_key.objectid, ret);
3694 }
Filipe Manana4222ea72018-10-24 10:13:03 +01003695 if (path != in_path)
3696 btrfs_free_path(path);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003697
Al Viro72c04902009-06-24 16:58:48 -04003698 if (!maybe_acls)
3699 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04003700
Chris Mason39279cc2007-06-12 06:35:45 -04003701 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04003702 case S_IFREG:
3703 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masond1310b22008-01-24 16:13:08 -05003704 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04003705 inode->i_fop = &btrfs_file_operations;
3706 inode->i_op = &btrfs_file_inode_operations;
3707 break;
3708 case S_IFDIR:
3709 inode->i_fop = &btrfs_dir_file_operations;
Omar Sandoval67ade052017-01-25 17:06:38 -08003710 inode->i_op = &btrfs_dir_inode_operations;
Chris Mason39279cc2007-06-12 06:35:45 -04003711 break;
3712 case S_IFLNK:
3713 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -05003714 inode_nohighmem(inode);
Omar Sandoval4779cc02018-09-24 15:16:55 -07003715 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason39279cc2007-06-12 06:35:45 -04003716 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04003717 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05003718 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04003719 init_special_inode(inode, inode->i_mode, rdev);
3720 break;
Chris Mason39279cc2007-06-12 06:35:45 -04003721 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003722
David Sterba7b6a2212018-03-26 18:40:21 +02003723 btrfs_sync_inode_flags_to_i_flags(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003724 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003725}
3726
Chris Masond352ac62008-09-29 15:18:18 -04003727/*
3728 * given a leaf and an inode, copy the inode fields into the leaf
3729 */
Chris Masone02119d2008-09-05 16:13:11 -04003730static void fill_inode_item(struct btrfs_trans_handle *trans,
3731 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04003732 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04003733 struct inode *inode)
3734{
Liu Bo51fab692012-12-27 09:01:21 +00003735 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003736
Liu Bo51fab692012-12-27 09:01:21 +00003737 btrfs_init_map_token(&token);
Chris Mason5f39d392007-10-15 16:14:19 -04003738
Liu Bo51fab692012-12-27 09:01:21 +00003739 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
3740 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
3741 btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size,
3742 &token);
3743 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3744 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003745
David Sterbaa937b972014-12-12 17:39:12 +01003746 btrfs_set_token_timespec_sec(leaf, &item->atime,
Liu Bo51fab692012-12-27 09:01:21 +00003747 inode->i_atime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003748 btrfs_set_token_timespec_nsec(leaf, &item->atime,
Liu Bo51fab692012-12-27 09:01:21 +00003749 inode->i_atime.tv_nsec, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003750
David Sterbaa937b972014-12-12 17:39:12 +01003751 btrfs_set_token_timespec_sec(leaf, &item->mtime,
Liu Bo51fab692012-12-27 09:01:21 +00003752 inode->i_mtime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003753 btrfs_set_token_timespec_nsec(leaf, &item->mtime,
Liu Bo51fab692012-12-27 09:01:21 +00003754 inode->i_mtime.tv_nsec, &token);
3755
David Sterbaa937b972014-12-12 17:39:12 +01003756 btrfs_set_token_timespec_sec(leaf, &item->ctime,
Liu Bo51fab692012-12-27 09:01:21 +00003757 inode->i_ctime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003758 btrfs_set_token_timespec_nsec(leaf, &item->ctime,
Liu Bo51fab692012-12-27 09:01:21 +00003759 inode->i_ctime.tv_nsec, &token);
3760
chandan r9cc97d62012-07-04 12:48:07 +05303761 btrfs_set_token_timespec_sec(leaf, &item->otime,
3762 BTRFS_I(inode)->i_otime.tv_sec, &token);
3763 btrfs_set_token_timespec_nsec(leaf, &item->otime,
3764 BTRFS_I(inode)->i_otime.tv_nsec, &token);
3765
Liu Bo51fab692012-12-27 09:01:21 +00003766 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3767 &token);
3768 btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation,
3769 &token);
Jeff Laytonc7f88c42017-12-11 06:35:12 -05003770 btrfs_set_token_inode_sequence(leaf, item, inode_peek_iversion(inode),
3771 &token);
Liu Bo51fab692012-12-27 09:01:21 +00003772 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
3773 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3774 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3775 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
Chris Mason39279cc2007-06-12 06:35:45 -04003776}
3777
Chris Masond352ac62008-09-29 15:18:18 -04003778/*
3779 * copy everything in the in-memory inode into the btree.
3780 */
Chris Mason21151332011-11-10 20:39:08 -05003781static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Chris Masond3977122009-01-05 21:25:51 -05003782 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003783{
3784 struct btrfs_inode_item *inode_item;
3785 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003786 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003787 int ret;
3788
3789 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08003790 if (!path)
3791 return -ENOMEM;
3792
Chris Masonb9473432009-03-13 11:00:37 -04003793 path->leave_spinning = 1;
Miao Xie16cdcec2011-04-22 18:12:22 +08003794 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
3795 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003796 if (ret) {
3797 if (ret > 0)
3798 ret = -ENOENT;
3799 goto failed;
3800 }
3801
Chris Mason5f39d392007-10-15 16:14:19 -04003802 leaf = path->nodes[0];
3803 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08003804 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04003805
Chris Masone02119d2008-09-05 16:13:11 -04003806 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04003807 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04003808 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003809 ret = 0;
3810failed:
Chris Mason39279cc2007-06-12 06:35:45 -04003811 btrfs_free_path(path);
3812 return ret;
3813}
3814
Chris Masond352ac62008-09-29 15:18:18 -04003815/*
Chris Mason21151332011-11-10 20:39:08 -05003816 * copy everything in the in-memory inode into the btree.
3817 */
3818noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
3819 struct btrfs_root *root, struct inode *inode)
3820{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003821 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason21151332011-11-10 20:39:08 -05003822 int ret;
3823
3824 /*
3825 * If the inode is a free space inode, we can deadlock during commit
3826 * if we put it into the delayed code.
3827 *
3828 * The data relocation inode should also be directly updated
3829 * without delay
3830 */
Nikolay Borisov70ddc552017-02-20 13:50:35 +02003831 if (!btrfs_is_free_space_inode(BTRFS_I(inode))
Josef Bacik1d52c782014-09-18 11:30:44 -04003832 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003833 && !test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) {
Alexander Block8ea05e32012-07-25 17:35:53 +02003834 btrfs_update_root_times(trans, root);
3835
Chris Mason21151332011-11-10 20:39:08 -05003836 ret = btrfs_delayed_update_inode(trans, root, inode);
3837 if (!ret)
3838 btrfs_set_inode_last_trans(trans, inode);
3839 return ret;
3840 }
3841
3842 return btrfs_update_inode_item(trans, root, inode);
3843}
3844
Josef Bacikbe6aef62012-10-22 15:43:12 -04003845noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
3846 struct btrfs_root *root,
3847 struct inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05003848{
3849 int ret;
3850
3851 ret = btrfs_update_inode(trans, root, inode);
3852 if (ret == -ENOSPC)
3853 return btrfs_update_inode_item(trans, root, inode);
3854 return ret;
3855}
3856
3857/*
Chris Masond352ac62008-09-29 15:18:18 -04003858 * unlink helper that gets used here in inode.c and in the tree logging
3859 * recovery code. It remove a link in a directory with a given name, and
3860 * also drops the back refs in the inode to the directory
3861 */
Al Viro92986792011-03-04 17:14:37 +00003862static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3863 struct btrfs_root *root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003864 struct btrfs_inode *dir,
3865 struct btrfs_inode *inode,
Al Viro92986792011-03-04 17:14:37 +00003866 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04003867{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003868 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04003869 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04003870 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003871 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003872 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04003873 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04003874 u64 index;
Li Zefan33345d012011-04-20 10:31:50 +08003875 u64 ino = btrfs_ino(inode);
3876 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04003877
3878 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04003879 if (!path) {
3880 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00003881 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04003882 }
3883
Chris Masonb9473432009-03-13 11:00:37 -04003884 path->leave_spinning = 1;
Li Zefan33345d012011-04-20 10:31:50 +08003885 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04003886 name, name_len, -1);
Liu Bo3cf50682018-09-12 06:06:26 +08003887 if (IS_ERR_OR_NULL(di)) {
3888 ret = di ? PTR_ERR(di) : -ENOENT;
Chris Mason39279cc2007-06-12 06:35:45 -04003889 goto err;
3890 }
Chris Mason5f39d392007-10-15 16:14:19 -04003891 leaf = path->nodes[0];
3892 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04003893 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04003894 if (ret)
3895 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02003896 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003897
Miao Xie67de1172013-12-26 13:07:06 +08003898 /*
3899 * If we don't have dir index, we have to get it by looking up
3900 * the inode ref, since we get the inode ref, remove it directly,
3901 * it is unnecessary to do delayed deletion.
3902 *
3903 * But if we have dir index, needn't search inode ref to get it.
3904 * Since the inode ref is close to the inode item, it is better
3905 * that we delay to delete it, and just do this deletion when
3906 * we update the inode item.
3907 */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003908 if (inode->dir_index) {
Miao Xie67de1172013-12-26 13:07:06 +08003909 ret = btrfs_delayed_delete_inode_ref(inode);
3910 if (!ret) {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003911 index = inode->dir_index;
Miao Xie67de1172013-12-26 13:07:06 +08003912 goto skip_backref;
3913 }
3914 }
3915
Li Zefan33345d012011-04-20 10:31:50 +08003916 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
3917 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04003918 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003919 btrfs_info(fs_info,
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003920 "failed to delete reference to %.*s, inode %llu parent %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003921 name_len, name, ino, dir_ino);
Jeff Mahoney66642832016-06-10 18:19:25 -04003922 btrfs_abort_transaction(trans, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04003923 goto err;
3924 }
Miao Xie67de1172013-12-26 13:07:06 +08003925skip_backref:
Lu Fengqi9add2942018-08-01 11:32:26 +08003926 ret = btrfs_delete_delayed_dir_index(trans, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003927 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003928 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003929 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003930 }
Chris Mason39279cc2007-06-12 06:35:45 -04003931
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003932 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len, inode,
3933 dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003934 if (ret != 0 && ret != -ENOENT) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003935 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003936 goto err;
3937 }
Chris Masone02119d2008-09-05 16:13:11 -04003938
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003939 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len, dir,
3940 index);
Chris Mason6418c962010-10-30 07:34:24 -04003941 if (ret == -ENOENT)
3942 ret = 0;
Zach Brownd4e39912013-04-02 21:02:16 +00003943 else if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04003944 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003945err:
3946 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003947 if (ret)
3948 goto out;
3949
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02003950 btrfs_i_size_write(dir, dir->vfs_inode.i_size - name_len * 2);
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003951 inode_inc_iversion(&inode->vfs_inode);
3952 inode_inc_iversion(&dir->vfs_inode);
3953 inode->vfs_inode.i_ctime = dir->vfs_inode.i_mtime =
3954 dir->vfs_inode.i_ctime = current_time(&inode->vfs_inode);
3955 ret = btrfs_update_inode(trans, root, &dir->vfs_inode);
Chris Masone02119d2008-09-05 16:13:11 -04003956out:
Chris Mason39279cc2007-06-12 06:35:45 -04003957 return ret;
3958}
3959
Al Viro92986792011-03-04 17:14:37 +00003960int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3961 struct btrfs_root *root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003962 struct btrfs_inode *dir, struct btrfs_inode *inode,
Al Viro92986792011-03-04 17:14:37 +00003963 const char *name, int name_len)
3964{
3965 int ret;
3966 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
3967 if (!ret) {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003968 drop_nlink(&inode->vfs_inode);
3969 ret = btrfs_update_inode(trans, root, &inode->vfs_inode);
Al Viro92986792011-03-04 17:14:37 +00003970 }
3971 return ret;
3972}
Yan, Zhenga22285a2010-05-16 10:48:46 -04003973
3974/*
3975 * helper to start transaction for unlink and rmdir.
3976 *
Josef Bacikd52be812013-05-29 14:54:47 -04003977 * unlink and rmdir are special in btrfs, they do not always free space, so
3978 * if we cannot make our reservations the normal way try and see if there is
3979 * plenty of slack room in the global reserve to migrate, otherwise we cannot
3980 * allow the unlink to occur.
Yan, Zhenga22285a2010-05-16 10:48:46 -04003981 */
Josef Bacikd52be812013-05-29 14:54:47 -04003982static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir)
Yan, Zhenga22285a2010-05-16 10:48:46 -04003983{
Yan, Zhenga22285a2010-05-16 10:48:46 -04003984 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003985
Josef Bacike70bea52011-10-11 14:18:24 -04003986 /*
3987 * 1 for the possible orphan item
3988 * 1 for the dir item
3989 * 1 for the dir index
3990 * 1 for the inode ref
Josef Bacike70bea52011-10-11 14:18:24 -04003991 * 1 for the inode
3992 */
Filipe Manana8eab77f2015-11-13 23:57:16 +00003993 return btrfs_start_transaction_fallback_global_rsv(root, 5, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003994}
3995
Chris Mason39279cc2007-06-12 06:35:45 -04003996static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
3997{
Yan, Zhenga22285a2010-05-16 10:48:46 -04003998 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003999 struct btrfs_trans_handle *trans;
David Howells2b0143b2015-03-17 22:25:59 +00004000 struct inode *inode = d_inode(dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04004001 int ret;
4002
Josef Bacikd52be812013-05-29 14:54:47 -04004003 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004004 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004005 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04004006
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004007 btrfs_record_unlink_dir(trans, BTRFS_I(dir), BTRFS_I(d_inode(dentry)),
4008 0);
Chris Mason12fcfd22009-03-24 10:24:20 -04004009
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004010 ret = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
4011 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
4012 dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00004013 if (ret)
4014 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004015
Yan, Zhenga22285a2010-05-16 10:48:46 -04004016 if (inode->i_nlink == 0) {
Nikolay Borisov73f2e542017-02-20 13:50:59 +02004017 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Tsutomu Itohb5324022011-07-19 07:27:20 +00004018 if (ret)
4019 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004020 }
Josef Bacik7b128762008-07-24 12:17:14 -04004021
Tsutomu Itohb5324022011-07-19 07:27:20 +00004022out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004023 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004024 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04004025 return ret;
4026}
4027
Misono Tomohirof60a2362018-04-18 11:34:52 +09004028static int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
Lu Fengqi401b3b12018-08-01 11:32:30 +08004029 struct inode *dir, u64 objectid,
4030 const char *name, int name_len)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004031{
Lu Fengqi401b3b12018-08-01 11:32:30 +08004032 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004033 struct btrfs_path *path;
4034 struct extent_buffer *leaf;
4035 struct btrfs_dir_item *di;
4036 struct btrfs_key key;
4037 u64 index;
4038 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004039 u64 dir_ino = btrfs_ino(BTRFS_I(dir));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004040
4041 path = btrfs_alloc_path();
4042 if (!path)
4043 return -ENOMEM;
4044
Li Zefan33345d012011-04-20 10:31:50 +08004045 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004046 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004047 if (IS_ERR_OR_NULL(di)) {
Liu Bo3cf50682018-09-12 06:06:26 +08004048 ret = di ? PTR_ERR(di) : -ENOENT;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004049 goto out;
4050 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004051
4052 leaf = path->nodes[0];
4053 btrfs_dir_item_key_to_cpu(leaf, di, &key);
4054 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
4055 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004056 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004057 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004058 goto out;
4059 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004060 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004061
Lu Fengqi3ee1c552018-08-01 11:32:28 +08004062 ret = btrfs_del_root_ref(trans, objectid, root->root_key.objectid,
4063 dir_ino, &index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004064 if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004065 if (ret != -ENOENT) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004066 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004067 goto out;
4068 }
Li Zefan33345d012011-04-20 10:31:50 +08004069 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004070 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004071 if (IS_ERR_OR_NULL(di)) {
4072 if (!di)
4073 ret = -ENOENT;
4074 else
4075 ret = PTR_ERR(di);
Jeff Mahoney66642832016-06-10 18:19:25 -04004076 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004077 goto out;
4078 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004079
4080 leaf = path->nodes[0];
4081 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004082 index = key.offset;
4083 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004084 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004085
Lu Fengqi9add2942018-08-01 11:32:26 +08004086 ret = btrfs_delete_delayed_dir_index(trans, BTRFS_I(dir), index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004087 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004088 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004089 goto out;
4090 }
Miao Xie16cdcec2011-04-22 18:12:22 +08004091
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004092 btrfs_i_size_write(BTRFS_I(dir), dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004093 inode_inc_iversion(dir);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07004094 dir->i_mtime = dir->i_ctime = current_time(dir);
Josef Bacik5a24e842012-08-08 10:12:59 -06004095 ret = btrfs_update_inode_fallback(trans, root, dir);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004096 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004097 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004098out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04004099 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004100 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004101}
4102
Misono Tomohiroec42f162018-04-18 11:34:13 +09004103/*
4104 * Helper to check if the subvolume references other subvolumes or if it's
4105 * default.
4106 */
Misono Tomohirof60a2362018-04-18 11:34:52 +09004107static noinline int may_destroy_subvol(struct btrfs_root *root)
Misono Tomohiroec42f162018-04-18 11:34:13 +09004108{
4109 struct btrfs_fs_info *fs_info = root->fs_info;
4110 struct btrfs_path *path;
4111 struct btrfs_dir_item *di;
4112 struct btrfs_key key;
4113 u64 dir_id;
4114 int ret;
4115
4116 path = btrfs_alloc_path();
4117 if (!path)
4118 return -ENOMEM;
4119
4120 /* Make sure this root isn't set as the default subvol */
4121 dir_id = btrfs_super_root_dir(fs_info->super_copy);
4122 di = btrfs_lookup_dir_item(NULL, fs_info->tree_root, path,
4123 dir_id, "default", 7, 0);
4124 if (di && !IS_ERR(di)) {
4125 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key);
4126 if (key.objectid == root->root_key.objectid) {
4127 ret = -EPERM;
4128 btrfs_err(fs_info,
4129 "deleting default subvolume %llu is not allowed",
4130 key.objectid);
4131 goto out;
4132 }
4133 btrfs_release_path(path);
4134 }
4135
4136 key.objectid = root->root_key.objectid;
4137 key.type = BTRFS_ROOT_REF_KEY;
4138 key.offset = (u64)-1;
4139
4140 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
4141 if (ret < 0)
4142 goto out;
4143 BUG_ON(ret == 0);
4144
4145 ret = 0;
4146 if (path->slots[0] > 0) {
4147 path->slots[0]--;
4148 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
4149 if (key.objectid == root->root_key.objectid &&
4150 key.type == BTRFS_ROOT_REF_KEY)
4151 ret = -ENOTEMPTY;
4152 }
4153out:
4154 btrfs_free_path(path);
4155 return ret;
4156}
4157
Nikolay Borisov20a68002018-04-27 14:36:24 +03004158/* Delete all dentries for inodes belonging to the root */
4159static void btrfs_prune_dentries(struct btrfs_root *root)
4160{
4161 struct btrfs_fs_info *fs_info = root->fs_info;
4162 struct rb_node *node;
4163 struct rb_node *prev;
4164 struct btrfs_inode *entry;
4165 struct inode *inode;
4166 u64 objectid = 0;
4167
4168 if (!test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
4169 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
4170
4171 spin_lock(&root->inode_lock);
4172again:
4173 node = root->inode_tree.rb_node;
4174 prev = NULL;
4175 while (node) {
4176 prev = node;
4177 entry = rb_entry(node, struct btrfs_inode, rb_node);
4178
David Sterba37508512018-06-29 10:56:40 +02004179 if (objectid < btrfs_ino(entry))
Nikolay Borisov20a68002018-04-27 14:36:24 +03004180 node = node->rb_left;
David Sterba37508512018-06-29 10:56:40 +02004181 else if (objectid > btrfs_ino(entry))
Nikolay Borisov20a68002018-04-27 14:36:24 +03004182 node = node->rb_right;
4183 else
4184 break;
4185 }
4186 if (!node) {
4187 while (prev) {
4188 entry = rb_entry(prev, struct btrfs_inode, rb_node);
David Sterba37508512018-06-29 10:56:40 +02004189 if (objectid <= btrfs_ino(entry)) {
Nikolay Borisov20a68002018-04-27 14:36:24 +03004190 node = prev;
4191 break;
4192 }
4193 prev = rb_next(prev);
4194 }
4195 }
4196 while (node) {
4197 entry = rb_entry(node, struct btrfs_inode, rb_node);
David Sterba37508512018-06-29 10:56:40 +02004198 objectid = btrfs_ino(entry) + 1;
Nikolay Borisov20a68002018-04-27 14:36:24 +03004199 inode = igrab(&entry->vfs_inode);
4200 if (inode) {
4201 spin_unlock(&root->inode_lock);
4202 if (atomic_read(&inode->i_count) > 1)
4203 d_prune_aliases(inode);
4204 /*
4205 * btrfs_drop_inode will have it removed from the inode
4206 * cache when its usage count hits zero.
4207 */
4208 iput(inode);
4209 cond_resched();
4210 spin_lock(&root->inode_lock);
4211 goto again;
4212 }
4213
4214 if (cond_resched_lock(&root->inode_lock))
4215 goto again;
4216
4217 node = rb_next(node);
4218 }
4219 spin_unlock(&root->inode_lock);
4220}
4221
Misono Tomohirof60a2362018-04-18 11:34:52 +09004222int btrfs_delete_subvolume(struct inode *dir, struct dentry *dentry)
4223{
4224 struct btrfs_fs_info *fs_info = btrfs_sb(dentry->d_sb);
4225 struct btrfs_root *root = BTRFS_I(dir)->root;
4226 struct inode *inode = d_inode(dentry);
4227 struct btrfs_root *dest = BTRFS_I(inode)->root;
4228 struct btrfs_trans_handle *trans;
4229 struct btrfs_block_rsv block_rsv;
4230 u64 root_flags;
Misono Tomohirof60a2362018-04-18 11:34:52 +09004231 int ret;
4232 int err;
4233
4234 /*
4235 * Don't allow to delete a subvolume with send in progress. This is
4236 * inside the inode lock so the error handling that has to drop the bit
4237 * again is not run concurrently.
4238 */
4239 spin_lock(&dest->root_item_lock);
Lu Fengqia7176f72018-08-04 21:10:53 +08004240 if (dest->send_in_progress) {
Misono Tomohirof60a2362018-04-18 11:34:52 +09004241 spin_unlock(&dest->root_item_lock);
4242 btrfs_warn(fs_info,
4243 "attempt to delete subvolume %llu during send",
4244 dest->root_key.objectid);
4245 return -EPERM;
4246 }
Lu Fengqia7176f72018-08-04 21:10:53 +08004247 root_flags = btrfs_root_flags(&dest->root_item);
4248 btrfs_set_root_flags(&dest->root_item,
4249 root_flags | BTRFS_ROOT_SUBVOL_DEAD);
4250 spin_unlock(&dest->root_item_lock);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004251
4252 down_write(&fs_info->subvol_sem);
4253
4254 err = may_destroy_subvol(dest);
4255 if (err)
4256 goto out_up_write;
4257
4258 btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP);
4259 /*
4260 * One for dir inode,
4261 * two for dir entries,
4262 * two for root ref/backref.
4263 */
Gu JinXiangc4c129d2018-05-30 11:00:38 +08004264 err = btrfs_subvolume_reserve_metadata(root, &block_rsv, 5, true);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004265 if (err)
4266 goto out_up_write;
4267
4268 trans = btrfs_start_transaction(root, 0);
4269 if (IS_ERR(trans)) {
4270 err = PTR_ERR(trans);
4271 goto out_release;
4272 }
4273 trans->block_rsv = &block_rsv;
4274 trans->bytes_reserved = block_rsv.size;
4275
4276 btrfs_record_snapshot_destroy(trans, BTRFS_I(dir));
4277
Lu Fengqi401b3b12018-08-01 11:32:30 +08004278 ret = btrfs_unlink_subvol(trans, dir, dest->root_key.objectid,
4279 dentry->d_name.name, dentry->d_name.len);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004280 if (ret) {
4281 err = ret;
4282 btrfs_abort_transaction(trans, ret);
4283 goto out_end_trans;
4284 }
4285
4286 btrfs_record_root_in_trans(trans, dest);
4287
4288 memset(&dest->root_item.drop_progress, 0,
4289 sizeof(dest->root_item.drop_progress));
4290 dest->root_item.drop_level = 0;
4291 btrfs_set_root_refs(&dest->root_item, 0);
4292
4293 if (!test_and_set_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &dest->state)) {
4294 ret = btrfs_insert_orphan_item(trans,
4295 fs_info->tree_root,
4296 dest->root_key.objectid);
4297 if (ret) {
4298 btrfs_abort_transaction(trans, ret);
4299 err = ret;
4300 goto out_end_trans;
4301 }
4302 }
4303
Lu Fengqid1957792018-05-29 15:01:54 +08004304 ret = btrfs_uuid_tree_remove(trans, dest->root_item.uuid,
Misono Tomohirof60a2362018-04-18 11:34:52 +09004305 BTRFS_UUID_KEY_SUBVOL,
4306 dest->root_key.objectid);
4307 if (ret && ret != -ENOENT) {
4308 btrfs_abort_transaction(trans, ret);
4309 err = ret;
4310 goto out_end_trans;
4311 }
4312 if (!btrfs_is_empty_uuid(dest->root_item.received_uuid)) {
Lu Fengqid1957792018-05-29 15:01:54 +08004313 ret = btrfs_uuid_tree_remove(trans,
Misono Tomohirof60a2362018-04-18 11:34:52 +09004314 dest->root_item.received_uuid,
4315 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
4316 dest->root_key.objectid);
4317 if (ret && ret != -ENOENT) {
4318 btrfs_abort_transaction(trans, ret);
4319 err = ret;
4320 goto out_end_trans;
4321 }
4322 }
4323
4324out_end_trans:
4325 trans->block_rsv = NULL;
4326 trans->bytes_reserved = 0;
4327 ret = btrfs_end_transaction(trans);
4328 if (ret && !err)
4329 err = ret;
4330 inode->i_flags |= S_DEAD;
4331out_release:
4332 btrfs_subvolume_release_metadata(fs_info, &block_rsv);
4333out_up_write:
4334 up_write(&fs_info->subvol_sem);
4335 if (err) {
4336 spin_lock(&dest->root_item_lock);
4337 root_flags = btrfs_root_flags(&dest->root_item);
4338 btrfs_set_root_flags(&dest->root_item,
4339 root_flags & ~BTRFS_ROOT_SUBVOL_DEAD);
4340 spin_unlock(&dest->root_item_lock);
4341 } else {
4342 d_invalidate(dentry);
Nikolay Borisov20a68002018-04-27 14:36:24 +03004343 btrfs_prune_dentries(dest);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004344 ASSERT(dest->send_in_progress == 0);
4345
4346 /* the last ref */
4347 if (dest->ino_cache_inode) {
4348 iput(dest->ino_cache_inode);
4349 dest->ino_cache_inode = NULL;
4350 }
4351 }
4352
4353 return err;
4354}
4355
Chris Mason39279cc2007-06-12 06:35:45 -04004356static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
4357{
David Howells2b0143b2015-03-17 22:25:59 +00004358 struct inode *inode = d_inode(dentry);
Chris Mason1832a6d2007-12-21 16:27:21 -05004359 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004360 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004361 struct btrfs_trans_handle *trans;
Filipe Manana44f714d2016-06-06 16:11:13 +01004362 u64 last_unlink_trans;
Chris Mason39279cc2007-06-12 06:35:45 -04004363
David Sterbab3ae2442012-09-13 16:04:34 -06004364 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Yan134d4512007-10-25 15:49:25 -04004365 return -ENOTEMPTY;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004366 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_FIRST_FREE_OBJECTID)
Misono Tomohiroa79a4642018-04-18 11:35:31 +09004367 return btrfs_delete_subvolume(dir, dentry);
Yan134d4512007-10-25 15:49:25 -04004368
Josef Bacikd52be812013-05-29 14:54:47 -04004369 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004370 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004371 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004372
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004373 if (unlikely(btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Lu Fengqi401b3b12018-08-01 11:32:30 +08004374 err = btrfs_unlink_subvol(trans, dir,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004375 BTRFS_I(inode)->location.objectid,
4376 dentry->d_name.name,
4377 dentry->d_name.len);
4378 goto out;
4379 }
4380
Nikolay Borisov73f2e542017-02-20 13:50:59 +02004381 err = btrfs_orphan_add(trans, BTRFS_I(inode));
Josef Bacik7b128762008-07-24 12:17:14 -04004382 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004383 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004384
Filipe Manana44f714d2016-06-06 16:11:13 +01004385 last_unlink_trans = BTRFS_I(inode)->last_unlink_trans;
4386
Chris Mason39279cc2007-06-12 06:35:45 -04004387 /* now the directory is empty */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004388 err = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
4389 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
4390 dentry->d_name.len);
Filipe Manana44f714d2016-06-06 16:11:13 +01004391 if (!err) {
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004392 btrfs_i_size_write(BTRFS_I(inode), 0);
Filipe Manana44f714d2016-06-06 16:11:13 +01004393 /*
4394 * Propagate the last_unlink_trans value of the deleted dir to
4395 * its parent directory. This is to prevent an unrecoverable
4396 * log tree in the case we do something like this:
4397 * 1) create dir foo
4398 * 2) create snapshot under dir foo
4399 * 3) delete the snapshot
4400 * 4) rmdir foo
4401 * 5) mkdir foo
4402 * 6) fsync foo or some file inside foo
4403 */
4404 if (last_unlink_trans >= trans->transid)
4405 BTRFS_I(dir)->last_unlink_trans = last_unlink_trans;
4406 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004407out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004408 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004409 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39544012007-12-12 14:38:19 -05004410
Chris Mason39279cc2007-06-12 06:35:45 -04004411 return err;
4412}
4413
Chris Mason28f75a02015-02-04 06:59:29 -08004414static int truncate_space_check(struct btrfs_trans_handle *trans,
4415 struct btrfs_root *root,
4416 u64 bytes_deleted)
4417{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004418 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason28f75a02015-02-04 06:59:29 -08004419 int ret;
4420
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004421 /*
4422 * This is only used to apply pressure to the enospc system, we don't
4423 * intend to use this reservation at all.
4424 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004425 bytes_deleted = btrfs_csum_bytes_to_leaves(fs_info, bytes_deleted);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004426 bytes_deleted *= fs_info->nodesize;
4427 ret = btrfs_block_rsv_add(root, &fs_info->trans_block_rsv,
Chris Mason28f75a02015-02-04 06:59:29 -08004428 bytes_deleted, BTRFS_RESERVE_NO_FLUSH);
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004429 if (!ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004430 trace_btrfs_space_reservation(fs_info, "transaction",
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004431 trans->transid,
4432 bytes_deleted, 1);
Chris Mason28f75a02015-02-04 06:59:29 -08004433 trans->bytes_reserved += bytes_deleted;
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004434 }
Chris Mason28f75a02015-02-04 06:59:29 -08004435 return ret;
4436
4437}
4438
Josef Bacikddfae632017-10-19 14:16:02 -04004439/*
4440 * Return this if we need to call truncate_block for the last bit of the
4441 * truncate.
4442 */
4443#define NEED_TRUNCATE_BLOCK 1
Filipe Manana0305cd52015-10-16 12:34:25 +01004444
Chris Mason323ac952008-10-01 19:05:46 -04004445/*
Chris Mason39279cc2007-06-12 06:35:45 -04004446 * this can truncate away extent items, csum items and directory items.
4447 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04004448 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04004449 *
4450 * csum items that cross the new i_size are truncated to the new size
4451 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04004452 *
4453 * min_type is the minimum key type to truncate down to. If set to 0, this
4454 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04004455 */
Yan, Zheng80825102009-11-12 09:35:36 +00004456int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
4457 struct btrfs_root *root,
4458 struct inode *inode,
4459 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004460{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004461 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04004462 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004463 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004464 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00004465 struct btrfs_key key;
4466 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04004467 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04004468 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004469 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004470 u64 item_end = 0;
Filipe Mananac1aa4572015-06-20 18:20:09 +01004471 u64 last_size = new_size;
Yan, Zheng80825102009-11-12 09:35:36 +00004472 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04004473 int found_extent;
4474 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05004475 int pending_del_nr = 0;
4476 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04004477 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00004478 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004479 u64 ino = btrfs_ino(BTRFS_I(inode));
Chris Mason28ed1342014-12-17 09:41:04 -08004480 u64 bytes_deleted = 0;
Thomas Meyer897ca812017-10-07 16:02:21 +02004481 bool be_nice = false;
4482 bool should_throttle = false;
4483 bool should_end = false;
Yan, Zheng80825102009-11-12 09:35:36 +00004484
4485 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04004486
Chris Mason28ed1342014-12-17 09:41:04 -08004487 /*
4488 * for non-free space inodes and ref cows, we want to back off from
4489 * time to time
4490 */
Nikolay Borisov70ddc552017-02-20 13:50:35 +02004491 if (!btrfs_is_free_space_inode(BTRFS_I(inode)) &&
Chris Mason28ed1342014-12-17 09:41:04 -08004492 test_bit(BTRFS_ROOT_REF_COWS, &root->state))
Thomas Meyer897ca812017-10-07 16:02:21 +02004493 be_nice = true;
Chris Mason28ed1342014-12-17 09:41:04 -08004494
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004495 path = btrfs_alloc_path();
4496 if (!path)
4497 return -ENOMEM;
David Sterbae4058b52015-11-27 16:31:35 +01004498 path->reada = READA_BACK;
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004499
Josef Bacik5dc562c2012-08-17 13:14:17 -04004500 /*
4501 * We want to drop from the next block forward in case this new size is
4502 * not block aligned since we will be keeping the last block of the
4503 * extent just the way it is.
4504 */
Miao Xie27cdeb72014-04-02 19:51:05 +08004505 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004506 root == fs_info->tree_root)
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02004507 btrfs_drop_extent_cache(BTRFS_I(inode), ALIGN(new_size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004508 fs_info->sectorsize),
Jeff Mahoneyda170662016-06-15 09:22:56 -04004509 (u64)-1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00004510
Miao Xie16cdcec2011-04-22 18:12:22 +08004511 /*
4512 * This function is also used to drop the items in the log tree before
4513 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
4514 * it is used to drop the loged items. So we shouldn't kill the delayed
4515 * items.
4516 */
4517 if (min_type == 0 && root == BTRFS_I(inode)->root)
Nikolay Borisov4ccb5c72017-01-10 20:35:38 +02004518 btrfs_kill_delayed_inode_items(BTRFS_I(inode));
Miao Xie16cdcec2011-04-22 18:12:22 +08004519
Li Zefan33345d012011-04-20 10:31:50 +08004520 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04004521 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04004522 key.type = (u8)-1;
4523
Chris Mason85e21ba2008-01-29 15:11:36 -05004524search_again:
Chris Mason28ed1342014-12-17 09:41:04 -08004525 /*
4526 * with a 16K leaf size and 128MB extents, you can actually queue
4527 * up a huge file in a single leaf. Most of the time that
4528 * bytes_deleted is > 0, it will be huge by the time we get here
4529 */
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004530 if (be_nice && bytes_deleted > SZ_32M &&
4531 btrfs_should_end_transaction(trans)) {
4532 ret = -EAGAIN;
Yan, Zheng80825102009-11-12 09:35:36 +00004533 goto out;
4534 }
Chris Masond3977122009-01-05 21:25:51 -05004535
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004536 path->leave_spinning = 1;
4537 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
4538 if (ret < 0)
4539 goto out;
4540
Chris Mason85e21ba2008-01-29 15:11:36 -05004541 if (ret > 0) {
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004542 ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -04004543 /* there are no items in the tree for us to truncate, we're
4544 * done
4545 */
Yan, Zheng80825102009-11-12 09:35:36 +00004546 if (path->slots[0] == 0)
4547 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05004548 path->slots[0]--;
4549 }
4550
Chris Masond3977122009-01-05 21:25:51 -05004551 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04004552 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04004553 leaf = path->nodes[0];
4554 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02004555 found_type = found_key.type;
Chris Mason39279cc2007-06-12 06:35:45 -04004556
Li Zefan33345d012011-04-20 10:31:50 +08004557 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04004558 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004559
Chris Mason85e21ba2008-01-29 15:11:36 -05004560 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004561 break;
4562
Chris Mason5f39d392007-10-15 16:14:19 -04004563 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04004564 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04004565 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04004566 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04004567 extent_type = btrfs_file_extent_type(leaf, fi);
4568 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04004569 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04004570 btrfs_file_extent_num_bytes(leaf, fi);
Liu Bo09ed2f12017-03-10 11:09:48 -08004571
4572 trace_btrfs_truncate_show_fi_regular(
4573 BTRFS_I(inode), leaf, fi,
4574 found_key.offset);
Chris Mason179e29e2007-11-01 11:28:41 -04004575 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Qu Wenruoe41ca582018-06-06 15:41:49 +08004576 item_end += btrfs_file_extent_ram_bytes(leaf,
4577 fi);
Liu Bo09ed2f12017-03-10 11:09:48 -08004578
4579 trace_btrfs_truncate_show_fi_inline(
4580 BTRFS_I(inode), leaf, fi, path->slots[0],
4581 found_key.offset);
Chris Mason39279cc2007-06-12 06:35:45 -04004582 }
Yan008630c2007-11-07 13:31:09 -05004583 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04004584 }
Yan, Zheng80825102009-11-12 09:35:36 +00004585 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04004586 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00004587 } else {
Filipe Manana76b42ab2017-02-14 16:56:01 +00004588 if (item_end < new_size)
Yan, Zheng80825102009-11-12 09:35:36 +00004589 break;
4590 if (found_key.offset >= new_size)
4591 del_item = 1;
4592 else
4593 del_item = 0;
4594 }
Chris Mason39279cc2007-06-12 06:35:45 -04004595 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004596 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04004597 if (found_type != BTRFS_EXTENT_DATA_KEY)
4598 goto delete;
4599
4600 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04004601 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04004602 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05004603 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04004604 u64 orig_num_bytes =
4605 btrfs_file_extent_num_bytes(leaf, fi);
Qu Wenruofda28322013-02-26 08:10:22 +00004606 extent_num_bytes = ALIGN(new_size -
4607 found_key.offset,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004608 fs_info->sectorsize);
Chris Masondb945352007-10-15 16:15:53 -04004609 btrfs_set_file_extent_num_bytes(leaf, fi,
4610 extent_num_bytes);
4611 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05004612 extent_num_bytes);
Miao Xie27cdeb72014-04-02 19:51:05 +08004613 if (test_bit(BTRFS_ROOT_REF_COWS,
4614 &root->state) &&
4615 extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004616 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04004617 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004618 } else {
Chris Masondb945352007-10-15 16:15:53 -04004619 extent_num_bytes =
4620 btrfs_file_extent_disk_num_bytes(leaf,
4621 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004622 extent_offset = found_key.offset -
4623 btrfs_file_extent_offset(leaf, fi);
4624
Chris Mason39279cc2007-06-12 06:35:45 -04004625 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05004626 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004627 if (extent_start != 0) {
4628 found_extent = 1;
Miao Xie27cdeb72014-04-02 19:51:05 +08004629 if (test_bit(BTRFS_ROOT_REF_COWS,
4630 &root->state))
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004631 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04004632 }
4633 }
Chris Mason90692182008-02-08 13:49:28 -05004634 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04004635 /*
4636 * we can't truncate inline items that have had
4637 * special encodings
4638 */
4639 if (!del_item &&
Chris Masonc8b97812008-10-29 14:49:59 -04004640 btrfs_file_extent_encryption(leaf, fi) == 0 &&
Josef Bacikddfae632017-10-19 14:16:02 -04004641 btrfs_file_extent_other_encoding(leaf, fi) == 0 &&
4642 btrfs_file_extent_compression(leaf, fi) == 0) {
4643 u32 size = (u32)(new_size - found_key.offset);
Chris Mason514ac8a2014-01-03 21:07:00 -08004644
Josef Bacikddfae632017-10-19 14:16:02 -04004645 btrfs_set_file_extent_ram_bytes(leaf, fi, size);
4646 size = btrfs_file_extent_calc_inline_size(size);
4647 btrfs_truncate_item(root->fs_info, path, size, 1);
4648 } else if (!del_item) {
Chris Mason514ac8a2014-01-03 21:07:00 -08004649 /*
Josef Bacikddfae632017-10-19 14:16:02 -04004650 * We have to bail so the last_size is set to
4651 * just before this extent.
Chris Mason514ac8a2014-01-03 21:07:00 -08004652 */
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004653 ret = NEED_TRUNCATE_BLOCK;
Josef Bacikddfae632017-10-19 14:16:02 -04004654 break;
Chris Mason90692182008-02-08 13:49:28 -05004655 }
Josef Bacikddfae632017-10-19 14:16:02 -04004656
4657 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state))
4658 inode_sub_bytes(inode, item_end + 1 - new_size);
Chris Mason39279cc2007-06-12 06:35:45 -04004659 }
Chris Mason179e29e2007-11-01 11:28:41 -04004660delete:
Josef Bacikddfae632017-10-19 14:16:02 -04004661 if (del_item)
4662 last_size = found_key.offset;
4663 else
4664 last_size = new_size;
Chris Mason39279cc2007-06-12 06:35:45 -04004665 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05004666 if (!pending_del_nr) {
4667 /* no pending yet, add ourselves */
4668 pending_del_slot = path->slots[0];
4669 pending_del_nr = 1;
4670 } else if (pending_del_nr &&
4671 path->slots[0] + 1 == pending_del_slot) {
4672 /* hop on the pending chunk */
4673 pending_del_nr++;
4674 pending_del_slot = path->slots[0];
4675 } else {
Chris Masond3977122009-01-05 21:25:51 -05004676 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05004677 }
Chris Mason39279cc2007-06-12 06:35:45 -04004678 } else {
4679 break;
4680 }
Thomas Meyer897ca812017-10-07 16:02:21 +02004681 should_throttle = false;
Chris Mason28f75a02015-02-04 06:59:29 -08004682
Miao Xie27cdeb72014-04-02 19:51:05 +08004683 if (found_extent &&
4684 (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004685 root == fs_info->tree_root)) {
Chris Masonb9473432009-03-13 11:00:37 -04004686 btrfs_set_path_blocking(path);
Chris Mason28ed1342014-12-17 09:41:04 -08004687 bytes_deleted += extent_num_bytes;
Josef Bacik84f7d8e2017-09-29 15:43:49 -04004688 ret = btrfs_free_extent(trans, root, extent_start,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004689 extent_num_bytes, 0,
4690 btrfs_header_owner(leaf),
Filipe Mananab06c4bf2015-10-23 07:52:54 +01004691 ino, extent_offset);
Omar Sandoval05522102018-05-11 13:13:31 -07004692 if (ret) {
4693 btrfs_abort_transaction(trans, ret);
4694 break;
4695 }
Lu Fengqi7c861622018-10-11 13:40:36 +08004696 if (btrfs_should_throttle_delayed_refs(trans))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004697 btrfs_async_run_delayed_refs(fs_info,
Wang Xiaoguangdd4b8572016-10-18 15:56:13 +08004698 trans->delayed_ref_updates * 2,
4699 trans->transid, 0);
Chris Mason28f75a02015-02-04 06:59:29 -08004700 if (be_nice) {
4701 if (truncate_space_check(trans, root,
4702 extent_num_bytes)) {
Thomas Meyer897ca812017-10-07 16:02:21 +02004703 should_end = true;
Chris Mason28f75a02015-02-04 06:59:29 -08004704 }
Lu Fengqi7c861622018-10-11 13:40:36 +08004705 if (btrfs_should_throttle_delayed_refs(trans))
Thomas Meyer897ca812017-10-07 16:02:21 +02004706 should_throttle = true;
Chris Mason28f75a02015-02-04 06:59:29 -08004707 }
Chris Mason39279cc2007-06-12 06:35:45 -04004708 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004709
Yan, Zheng80825102009-11-12 09:35:36 +00004710 if (found_type == BTRFS_INODE_ITEM_KEY)
4711 break;
4712
4713 if (path->slots[0] == 0 ||
Josef Bacik12621332015-02-03 07:50:16 -08004714 path->slots[0] != pending_del_slot ||
Chris Mason28f75a02015-02-04 06:59:29 -08004715 should_throttle || should_end) {
Yan, Zheng80825102009-11-12 09:35:36 +00004716 if (pending_del_nr) {
4717 ret = btrfs_del_items(trans, root, path,
4718 pending_del_slot,
4719 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004720 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004721 btrfs_abort_transaction(trans, ret);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004722 break;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004723 }
Yan, Zheng80825102009-11-12 09:35:36 +00004724 pending_del_nr = 0;
4725 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004726 btrfs_release_path(path);
Chris Mason28f75a02015-02-04 06:59:29 -08004727 if (should_throttle) {
Josef Bacik12621332015-02-03 07:50:16 -08004728 unsigned long updates = trans->delayed_ref_updates;
4729 if (updates) {
4730 trans->delayed_ref_updates = 0;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004731 ret = btrfs_run_delayed_refs(trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004732 updates * 2);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004733 if (ret)
4734 break;
Josef Bacik12621332015-02-03 07:50:16 -08004735 }
4736 }
Chris Mason28f75a02015-02-04 06:59:29 -08004737 /*
4738 * if we failed to refill our space rsv, bail out
4739 * and let the transaction restart
4740 */
4741 if (should_end) {
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004742 ret = -EAGAIN;
4743 break;
Chris Mason28f75a02015-02-04 06:59:29 -08004744 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004745 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00004746 } else {
4747 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05004748 }
Chris Mason39279cc2007-06-12 06:35:45 -04004749 }
Yan, Zheng80825102009-11-12 09:35:36 +00004750out:
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004751 if (ret >= 0 && pending_del_nr) {
4752 int err;
4753
4754 err = btrfs_del_items(trans, root, path, pending_del_slot,
Chris Mason85e21ba2008-01-29 15:11:36 -05004755 pending_del_nr);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004756 if (err) {
4757 btrfs_abort_transaction(trans, err);
4758 ret = err;
4759 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004760 }
Filipe Manana76b42ab2017-02-14 16:56:01 +00004761 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
4762 ASSERT(last_size >= new_size);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004763 if (!ret && last_size > new_size)
Filipe Manana76b42ab2017-02-14 16:56:01 +00004764 last_size = new_size;
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004765 btrfs_ordered_update_i_size(inode, last_size, NULL);
Filipe Manana76b42ab2017-02-14 16:56:01 +00004766 }
Chris Mason28ed1342014-12-17 09:41:04 -08004767
Chris Mason39279cc2007-06-12 06:35:45 -04004768 btrfs_free_path(path);
Chris Mason28ed1342014-12-17 09:41:04 -08004769
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004770 if (be_nice && bytes_deleted > SZ_32M && (ret >= 0 || ret == -EAGAIN)) {
Chris Mason28ed1342014-12-17 09:41:04 -08004771 unsigned long updates = trans->delayed_ref_updates;
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004772 int err;
4773
Chris Mason28ed1342014-12-17 09:41:04 -08004774 if (updates) {
4775 trans->delayed_ref_updates = 0;
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004776 err = btrfs_run_delayed_refs(trans, updates * 2);
4777 if (err)
4778 ret = err;
Chris Mason28ed1342014-12-17 09:41:04 -08004779 }
4780 }
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004781 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04004782}
4783
Chris Masona52d9a82007-08-27 16:49:44 -04004784/*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304785 * btrfs_truncate_block - read, zero a chunk and write a block
Josef Bacik2aaa6652012-08-29 14:27:18 -04004786 * @inode - inode that we're zeroing
4787 * @from - the offset to start zeroing
4788 * @len - the length to zero, 0 to zero the entire range respective to the
4789 * offset
4790 * @front - zero up to the offset instead of from the offset on
4791 *
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304792 * This will find the block for the "from" offset and cow the block and zero the
Josef Bacik2aaa6652012-08-29 14:27:18 -04004793 * part we want to zero. This is used with truncate and hole punching.
Chris Masona52d9a82007-08-27 16:49:44 -04004794 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304795int btrfs_truncate_block(struct inode *inode, loff_t from, loff_t len,
Josef Bacik2aaa6652012-08-29 14:27:18 -04004796 int front)
Chris Masona52d9a82007-08-27 16:49:44 -04004797{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004798 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004799 struct address_space *mapping = inode->i_mapping;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004800 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4801 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004802 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08004803 struct extent_changeset *data_reserved = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004804 char *kaddr;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004805 u32 blocksize = fs_info->sectorsize;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004806 pgoff_t index = from >> PAGE_SHIFT;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304807 unsigned offset = from & (blocksize - 1);
Chris Masona52d9a82007-08-27 16:49:44 -04004808 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004809 gfp_t mask = btrfs_alloc_write_mask(mapping);
Chris Masona52d9a82007-08-27 16:49:44 -04004810 int ret = 0;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304811 u64 block_start;
4812 u64 block_end;
Chris Masona52d9a82007-08-27 16:49:44 -04004813
Nikolay Borisovb03ebd92018-01-18 14:47:06 +02004814 if (IS_ALIGNED(offset, blocksize) &&
4815 (!len || IS_ALIGNED(len, blocksize)))
Chris Masona52d9a82007-08-27 16:49:44 -04004816 goto out;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304817
Josef Bacik8b62f872017-10-19 14:15:55 -04004818 block_start = round_down(from, blocksize);
4819 block_end = block_start + blocksize - 1;
4820
Qu Wenruo364ecf32017-02-27 15:10:38 +08004821 ret = btrfs_delalloc_reserve_space(inode, &data_reserved,
Josef Bacik8b62f872017-10-19 14:15:55 -04004822 block_start, blocksize);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004823 if (ret)
4824 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004825
Chris Mason211c17f2008-05-15 09:13:45 -04004826again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004827 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004828 if (!page) {
Qu Wenruobc42bda2017-02-27 15:10:39 +08004829 btrfs_delalloc_release_space(inode, data_reserved,
Qu Wenruo43b18592017-12-12 15:34:32 +08004830 block_start, blocksize, true);
4831 btrfs_delalloc_release_extents(BTRFS_I(inode), blocksize, true);
Miao Xieac6a2b32012-12-05 10:56:13 +00004832 ret = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -04004833 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04004834 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004835
Chris Masona52d9a82007-08-27 16:49:44 -04004836 if (!PageUptodate(page)) {
4837 ret = btrfs_readpage(NULL, page);
4838 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004839 if (page->mapping != mapping) {
4840 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004841 put_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004842 goto again;
4843 }
Chris Masona52d9a82007-08-27 16:49:44 -04004844 if (!PageUptodate(page)) {
4845 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04004846 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04004847 }
4848 }
Chris Mason211c17f2008-05-15 09:13:45 -04004849 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004850
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304851 lock_extent_bits(io_tree, block_start, block_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004852 set_page_extent_mapped(page);
4853
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304854 ordered = btrfs_lookup_ordered_extent(inode, block_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004855 if (ordered) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304856 unlock_extent_cached(io_tree, block_start, block_end,
David Sterbae43bbe52017-12-12 21:43:52 +01004857 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004858 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004859 put_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04004860 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004861 btrfs_put_ordered_extent(ordered);
4862 goto again;
4863 }
4864
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304865 clear_extent_bit(&BTRFS_I(inode)->io_tree, block_start, block_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06004866 EXTENT_DIRTY | EXTENT_DELALLOC |
4867 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
David Sterbaae0f1622017-10-31 16:37:52 +01004868 0, 0, &cached_state);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004869
Filipe Mananae3b8a482017-11-04 00:16:59 +00004870 ret = btrfs_set_extent_delalloc(inode, block_start, block_end, 0,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08004871 &cached_state, 0);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004872 if (ret) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304873 unlock_extent_cached(io_tree, block_start, block_end,
David Sterbae43bbe52017-12-12 21:43:52 +01004874 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004875 goto out_unlock;
4876 }
4877
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304878 if (offset != blocksize) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04004879 if (!len)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304880 len = blocksize - offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004881 kaddr = kmap(page);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004882 if (front)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304883 memset(kaddr + (block_start - page_offset(page)),
4884 0, offset);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004885 else
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304886 memset(kaddr + (block_start - page_offset(page)) + offset,
4887 0, len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004888 flush_dcache_page(page);
4889 kunmap(page);
4890 }
Chris Mason247e7432008-07-17 12:53:51 -04004891 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004892 set_page_dirty(page);
David Sterbae43bbe52017-12-12 21:43:52 +01004893 unlock_extent_cached(io_tree, block_start, block_end, &cached_state);
Chris Mason39279cc2007-06-12 06:35:45 -04004894
Chris Mason89642222008-07-24 09:41:53 -04004895out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04004896 if (ret)
Qu Wenruobc42bda2017-02-27 15:10:39 +08004897 btrfs_delalloc_release_space(inode, data_reserved, block_start,
Qu Wenruo43b18592017-12-12 15:34:32 +08004898 blocksize, true);
4899 btrfs_delalloc_release_extents(BTRFS_I(inode), blocksize, (ret != 0));
Chris Mason39279cc2007-06-12 06:35:45 -04004900 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004901 put_page(page);
Chris Mason39279cc2007-06-12 06:35:45 -04004902out:
Qu Wenruo364ecf32017-02-27 15:10:38 +08004903 extent_changeset_free(data_reserved);
Chris Mason39279cc2007-06-12 06:35:45 -04004904 return ret;
4905}
4906
Josef Bacik16e75492013-10-22 12:18:51 -04004907static int maybe_insert_hole(struct btrfs_root *root, struct inode *inode,
4908 u64 offset, u64 len)
4909{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004910 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik16e75492013-10-22 12:18:51 -04004911 struct btrfs_trans_handle *trans;
4912 int ret;
4913
4914 /*
4915 * Still need to make sure the inode looks like it's been updated so
4916 * that any holes get logged if we fsync.
4917 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004918 if (btrfs_fs_incompat(fs_info, NO_HOLES)) {
4919 BTRFS_I(inode)->last_trans = fs_info->generation;
Josef Bacik16e75492013-10-22 12:18:51 -04004920 BTRFS_I(inode)->last_sub_trans = root->log_transid;
4921 BTRFS_I(inode)->last_log_commit = root->last_log_commit;
4922 return 0;
4923 }
4924
4925 /*
4926 * 1 - for the one we're dropping
4927 * 1 - for the one we're adding
4928 * 1 - for updating the inode.
4929 */
4930 trans = btrfs_start_transaction(root, 3);
4931 if (IS_ERR(trans))
4932 return PTR_ERR(trans);
4933
4934 ret = btrfs_drop_extents(trans, root, inode, offset, offset + len, 1);
4935 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004936 btrfs_abort_transaction(trans, ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004937 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04004938 return ret;
4939 }
4940
David Sterbaf85b7372017-01-20 14:54:07 +01004941 ret = btrfs_insert_file_extent(trans, root, btrfs_ino(BTRFS_I(inode)),
4942 offset, 0, 0, len, 0, len, 0, 0, 0);
Josef Bacik16e75492013-10-22 12:18:51 -04004943 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004944 btrfs_abort_transaction(trans, ret);
Josef Bacik16e75492013-10-22 12:18:51 -04004945 else
4946 btrfs_update_inode(trans, root, inode);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004947 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04004948 return ret;
4949}
4950
Josef Bacik695a0d02011-03-04 15:46:53 -05004951/*
4952 * This function puts in dummy file extents for the area we're creating a hole
4953 * for. So if we are truncating this file to a larger size we need to insert
4954 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
4955 * the range between oldsize and size
4956 */
Josef Bacika41ad392011-01-31 15:30:16 -05004957int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04004958{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004959 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Yan Zheng9036c102008-10-30 14:19:41 -04004960 struct btrfs_root *root = BTRFS_I(inode)->root;
4961 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004962 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004963 struct extent_state *cached_state = NULL;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004964 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004965 u64 hole_start = ALIGN(oldsize, fs_info->sectorsize);
4966 u64 block_end = ALIGN(size, fs_info->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04004967 u64 last_byte;
4968 u64 cur_offset;
4969 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04004970 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04004971
Josef Bacika71754f2013-06-17 17:14:39 -04004972 /*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304973 * If our size started in the middle of a block we need to zero out the
4974 * rest of the block before we expand the i_size, otherwise we could
Josef Bacika71754f2013-06-17 17:14:39 -04004975 * expose stale data.
4976 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304977 err = btrfs_truncate_block(inode, oldsize, 0, 0);
Josef Bacika71754f2013-06-17 17:14:39 -04004978 if (err)
4979 return err;
4980
Yan Zheng9036c102008-10-30 14:19:41 -04004981 if (size <= hole_start)
4982 return 0;
4983
Yan Zheng9036c102008-10-30 14:19:41 -04004984 while (1) {
4985 struct btrfs_ordered_extent *ordered;
Miao Xiefa7c1492013-09-26 13:15:27 +08004986
David Sterbaff13db42015-12-03 14:30:40 +01004987 lock_extent_bits(io_tree, hole_start, block_end - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004988 &cached_state);
Nikolay Borisova776c6f2017-02-20 13:50:49 +02004989 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), hole_start,
Miao Xiefa7c1492013-09-26 13:15:27 +08004990 block_end - hole_start);
Yan Zheng9036c102008-10-30 14:19:41 -04004991 if (!ordered)
4992 break;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004993 unlock_extent_cached(io_tree, hole_start, block_end - 1,
David Sterbae43bbe52017-12-12 21:43:52 +01004994 &cached_state);
Miao Xiefa7c1492013-09-26 13:15:27 +08004995 btrfs_start_ordered_extent(inode, ordered, 1);
Yan Zheng9036c102008-10-30 14:19:41 -04004996 btrfs_put_ordered_extent(ordered);
4997 }
4998
Yan Zheng9036c102008-10-30 14:19:41 -04004999 cur_offset = hole_start;
5000 while (1) {
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02005001 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, cur_offset,
Yan Zheng9036c102008-10-30 14:19:41 -04005002 block_end - cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005003 if (IS_ERR(em)) {
5004 err = PTR_ERR(em);
Zach Brownf2767952013-01-08 19:37:58 +00005005 em = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005006 break;
5007 }
Yan Zheng9036c102008-10-30 14:19:41 -04005008 last_byte = min(extent_map_end(em), block_end);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005009 last_byte = ALIGN(last_byte, fs_info->sectorsize);
Yan, Zheng80825102009-11-12 09:35:36 +00005010 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04005011 struct extent_map *hole_em;
Yan Zheng9036c102008-10-30 14:19:41 -04005012 hole_size = last_byte - cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00005013
Josef Bacik16e75492013-10-22 12:18:51 -04005014 err = maybe_insert_hole(root, inode, cur_offset,
5015 hole_size);
5016 if (err)
Chris Mason771ed682008-11-06 22:02:51 -05005017 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02005018 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04005019 cur_offset + hole_size - 1, 0);
5020 hole_em = alloc_extent_map();
5021 if (!hole_em) {
5022 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
5023 &BTRFS_I(inode)->runtime_flags);
5024 goto next;
5025 }
5026 hole_em->start = cur_offset;
5027 hole_em->len = hole_size;
5028 hole_em->orig_start = cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00005029
Josef Bacik5dc562c2012-08-17 13:14:17 -04005030 hole_em->block_start = EXTENT_MAP_HOLE;
5031 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05005032 hole_em->orig_block_len = 0;
Josef Bacikcc95bef2013-04-04 14:31:27 -04005033 hole_em->ram_bytes = hole_size;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005034 hole_em->bdev = fs_info->fs_devices->latest_bdev;
Josef Bacik5dc562c2012-08-17 13:14:17 -04005035 hole_em->compress_type = BTRFS_COMPRESS_NONE;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005036 hole_em->generation = fs_info->generation;
Josef Bacik5dc562c2012-08-17 13:14:17 -04005037
5038 while (1) {
5039 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04005040 err = add_extent_mapping(em_tree, hole_em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04005041 write_unlock(&em_tree->lock);
5042 if (err != -EEXIST)
5043 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02005044 btrfs_drop_extent_cache(BTRFS_I(inode),
5045 cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04005046 cur_offset +
5047 hole_size - 1, 0);
5048 }
5049 free_extent_map(hole_em);
Yan Zheng9036c102008-10-30 14:19:41 -04005050 }
Josef Bacik16e75492013-10-22 12:18:51 -04005051next:
Yan Zheng9036c102008-10-30 14:19:41 -04005052 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04005053 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04005054 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00005055 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04005056 break;
5057 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04005058 free_extent_map(em);
David Sterbae43bbe52017-12-12 21:43:52 +01005059 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state);
Yan Zheng9036c102008-10-30 14:19:41 -04005060 return err;
5061}
5062
Eric Sandeen3972f262013-01-12 02:57:22 +00005063static int btrfs_setsize(struct inode *inode, struct iattr *attr)
Yan, Zheng80825102009-11-12 09:35:36 +00005064{
Miao Xief4a2f4c2011-12-14 20:12:01 -05005065 struct btrfs_root *root = BTRFS_I(inode)->root;
5066 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05005067 loff_t oldsize = i_size_read(inode);
Eric Sandeen3972f262013-01-12 02:57:22 +00005068 loff_t newsize = attr->ia_size;
5069 int mask = attr->ia_valid;
Yan, Zheng80825102009-11-12 09:35:36 +00005070 int ret;
5071
Eric Sandeen3972f262013-01-12 02:57:22 +00005072 /*
5073 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
5074 * special case where we need to update the times despite not having
5075 * these flags set. For all other operations the VFS set these flags
5076 * explicitly if it wants a timestamp update.
5077 */
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08005078 if (newsize != oldsize) {
5079 inode_inc_iversion(inode);
5080 if (!(mask & (ATTR_CTIME | ATTR_MTIME)))
5081 inode->i_ctime = inode->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07005082 current_time(inode);
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08005083 }
Eric Sandeen3972f262013-01-12 02:57:22 +00005084
Josef Bacika41ad392011-01-31 15:30:16 -05005085 if (newsize > oldsize) {
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005086 /*
David Sterbaea14b57f2017-06-22 02:19:11 +02005087 * Don't do an expanding truncate while snapshotting is ongoing.
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005088 * This is to ensure the snapshot captures a fully consistent
5089 * state of this file - if the snapshot captures this expanding
5090 * truncation, it must capture all writes that happened before
5091 * this truncation.
5092 */
Zhao Lei0bc19f902016-01-06 18:56:36 +08005093 btrfs_wait_for_snapshot_creation(root);
Josef Bacika41ad392011-01-31 15:30:16 -05005094 ret = btrfs_cont_expand(inode, oldsize, newsize);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005095 if (ret) {
David Sterbaea14b57f2017-06-22 02:19:11 +02005096 btrfs_end_write_no_snapshotting(root);
Yan, Zheng80825102009-11-12 09:35:36 +00005097 return ret;
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005098 }
Yan, Zheng80825102009-11-12 09:35:36 +00005099
Miao Xief4a2f4c2011-12-14 20:12:01 -05005100 trans = btrfs_start_transaction(root, 1);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005101 if (IS_ERR(trans)) {
David Sterbaea14b57f2017-06-22 02:19:11 +02005102 btrfs_end_write_no_snapshotting(root);
Miao Xief4a2f4c2011-12-14 20:12:01 -05005103 return PTR_ERR(trans);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005104 }
Miao Xief4a2f4c2011-12-14 20:12:01 -05005105
5106 i_size_write(inode, newsize);
5107 btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
Chandan Rajendra27772b62016-01-21 15:56:03 +05305108 pagecache_isize_extended(inode, oldsize, newsize);
Miao Xief4a2f4c2011-12-14 20:12:01 -05005109 ret = btrfs_update_inode(trans, root, inode);
David Sterbaea14b57f2017-06-22 02:19:11 +02005110 btrfs_end_write_no_snapshotting(root);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005111 btrfs_end_transaction(trans);
Josef Bacika41ad392011-01-31 15:30:16 -05005112 } else {
Yan, Zheng80825102009-11-12 09:35:36 +00005113
Josef Bacika41ad392011-01-31 15:30:16 -05005114 /*
5115 * We're truncating a file that used to have good data down to
5116 * zero. Make sure it gets into the ordered flush list so that
5117 * any new writes get down to disk quickly.
5118 */
5119 if (newsize == 0)
Josef Bacik72ac3c02012-05-23 14:13:11 -04005120 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
5121 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00005122
Josef Bacika41ad392011-01-31 15:30:16 -05005123 truncate_setsize(inode, newsize);
Miao Xie2e60a512013-02-08 07:01:08 +00005124
5125 /* Disable nonlocked read DIO to avoid the end less truncate */
Nikolay Borisovabcefb12017-02-20 13:51:10 +02005126 btrfs_inode_block_unlocked_dio(BTRFS_I(inode));
Miao Xie2e60a512013-02-08 07:01:08 +00005127 inode_dio_wait(inode);
Nikolay Borisov0b581702017-02-20 13:51:11 +02005128 btrfs_inode_resume_unlocked_dio(BTRFS_I(inode));
Miao Xie2e60a512013-02-08 07:01:08 +00005129
Filipe Manana213e8c52018-02-06 20:40:31 +00005130 ret = btrfs_truncate(inode, newsize == oldsize);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005131 if (ret && inode->i_nlink) {
5132 int err;
5133
5134 /*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07005135 * Truncate failed, so fix up the in-memory size. We
5136 * adjusted disk_i_size down as we removed extents, so
5137 * wait for disk_i_size to be stable and then update the
5138 * in-memory size to match.
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005139 */
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07005140 err = btrfs_wait_ordered_range(inode, 0, (u64)-1);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005141 if (err)
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07005142 return err;
5143 i_size_write(inode, BTRFS_I(inode)->disk_i_size);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005144 }
Yan, Zheng80825102009-11-12 09:35:36 +00005145 }
5146
Josef Bacika41ad392011-01-31 15:30:16 -05005147 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00005148}
5149
Chris Mason39279cc2007-06-12 06:35:45 -04005150static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
5151{
David Howells2b0143b2015-03-17 22:25:59 +00005152 struct inode *inode = d_inode(dentry);
Li Zefanb83cc962010-12-20 16:04:08 +08005153 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005154 int err;
5155
Li Zefanb83cc962010-12-20 16:04:08 +08005156 if (btrfs_root_readonly(root))
5157 return -EROFS;
5158
Jan Kara31051c82016-05-26 16:55:18 +02005159 err = setattr_prepare(dentry, attr);
Chris Mason39279cc2007-06-12 06:35:45 -04005160 if (err)
5161 return err;
5162
Chris Mason5a3f23d2009-03-31 13:27:11 -04005163 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Eric Sandeen3972f262013-01-12 02:57:22 +00005164 err = btrfs_setsize(inode, attr);
Yan, Zheng80825102009-11-12 09:35:36 +00005165 if (err)
5166 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005167 }
Yan Zheng9036c102008-10-30 14:19:41 -04005168
Christoph Hellwig10257742010-06-04 11:30:02 +02005169 if (attr->ia_valid) {
5170 setattr_copy(inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005171 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005172 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04005173
Josef Bacik22c44fe2011-11-30 10:45:38 -05005174 if (!err && attr->ia_valid & ATTR_MODE)
Christoph Hellwig996a7102013-12-20 05:16:43 -08005175 err = posix_acl_chmod(inode, inode->i_mode);
Christoph Hellwig10257742010-06-04 11:30:02 +02005176 }
5177
Chris Mason39279cc2007-06-12 06:35:45 -04005178 return err;
5179}
Chris Mason61295eb2008-01-14 16:24:38 -05005180
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005181/*
5182 * While truncating the inode pages during eviction, we get the VFS calling
5183 * btrfs_invalidatepage() against each page of the inode. This is slow because
5184 * the calls to btrfs_invalidatepage() result in a huge amount of calls to
5185 * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting
5186 * extent_state structures over and over, wasting lots of time.
5187 *
5188 * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all
5189 * those expensive operations on a per page basis and do only the ordered io
5190 * finishing, while we release here the extent_map and extent_state structures,
5191 * without the excessive merging and splitting.
5192 */
5193static void evict_inode_truncate_pages(struct inode *inode)
5194{
5195 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
5196 struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree;
5197 struct rb_node *node;
5198
5199 ASSERT(inode->i_state & I_FREEING);
Johannes Weiner91b0abe2014-04-03 14:47:49 -07005200 truncate_inode_pages_final(&inode->i_data);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005201
5202 write_lock(&map_tree->lock);
Liu Bo07e1ce02018-08-23 03:51:52 +08005203 while (!RB_EMPTY_ROOT(&map_tree->map.rb_root)) {
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005204 struct extent_map *em;
5205
Liu Bo07e1ce02018-08-23 03:51:52 +08005206 node = rb_first_cached(&map_tree->map);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005207 em = rb_entry(node, struct extent_map, rb_node);
Wang Shilong180589e2013-12-14 15:27:31 +08005208 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
5209 clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005210 remove_extent_mapping(map_tree, em);
5211 free_extent_map(em);
Filipe Manana7064dd52014-08-08 02:47:05 +01005212 if (need_resched()) {
5213 write_unlock(&map_tree->lock);
5214 cond_resched();
5215 write_lock(&map_tree->lock);
5216 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005217 }
5218 write_unlock(&map_tree->lock);
5219
Filipe Manana6ca07092015-05-26 00:55:42 +01005220 /*
5221 * Keep looping until we have no more ranges in the io tree.
5222 * We can have ongoing bios started by readpages (called from readahead)
Filipe Manana9c6429d2015-06-10 12:55:41 +01005223 * that have their endio callback (extent_io.c:end_bio_extent_readpage)
5224 * still in progress (unlocked the pages in the bio but did not yet
5225 * unlocked the ranges in the io tree). Therefore this means some
Filipe Manana6ca07092015-05-26 00:55:42 +01005226 * ranges can still be locked and eviction started because before
5227 * submitting those bios, which are executed by a separate task (work
5228 * queue kthread), inode references (inode->i_count) were not taken
5229 * (which would be dropped in the end io callback of each bio).
5230 * Therefore here we effectively end up waiting for those bios and
5231 * anyone else holding locked ranges without having bumped the inode's
5232 * reference count - if we don't do it, when they access the inode's
5233 * io_tree to unlock a range it may be too late, leading to an
5234 * use-after-free issue.
5235 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005236 spin_lock(&io_tree->lock);
5237 while (!RB_EMPTY_ROOT(&io_tree->state)) {
5238 struct extent_state *state;
5239 struct extent_state *cached_state = NULL;
Filipe Manana6ca07092015-05-26 00:55:42 +01005240 u64 start;
5241 u64 end;
Filipe Manana421f0922018-10-12 13:02:48 +01005242 unsigned state_flags;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005243
5244 node = rb_first(&io_tree->state);
5245 state = rb_entry(node, struct extent_state, rb_node);
Filipe Manana6ca07092015-05-26 00:55:42 +01005246 start = state->start;
5247 end = state->end;
Filipe Manana421f0922018-10-12 13:02:48 +01005248 state_flags = state->state;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005249 spin_unlock(&io_tree->lock);
5250
David Sterbaff13db42015-12-03 14:30:40 +01005251 lock_extent_bits(io_tree, start, end, &cached_state);
Qu Wenruob9d0b382015-09-29 10:35:16 +08005252
5253 /*
5254 * If still has DELALLOC flag, the extent didn't reach disk,
5255 * and its reserved space won't be freed by delayed_ref.
5256 * So we need to free its reserved space here.
5257 * (Refer to comment in btrfs_invalidatepage, case 2)
5258 *
5259 * Note, end is the bytenr of last byte, so we need + 1 here.
5260 */
Filipe Manana421f0922018-10-12 13:02:48 +01005261 if (state_flags & EXTENT_DELALLOC)
Qu Wenruobc42bda2017-02-27 15:10:39 +08005262 btrfs_qgroup_free_data(inode, NULL, start, end - start + 1);
Qu Wenruob9d0b382015-09-29 10:35:16 +08005263
Filipe Manana6ca07092015-05-26 00:55:42 +01005264 clear_extent_bit(io_tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005265 EXTENT_LOCKED | EXTENT_DIRTY |
5266 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
David Sterbaae0f1622017-10-31 16:37:52 +01005267 EXTENT_DEFRAG, 1, 1, &cached_state);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005268
Filipe Manana7064dd52014-08-08 02:47:05 +01005269 cond_resched();
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005270 spin_lock(&io_tree->lock);
5271 }
5272 spin_unlock(&io_tree->lock);
5273}
5274
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005275static struct btrfs_trans_handle *evict_refill_and_join(struct btrfs_root *root,
Josef Bacikad80cf52018-09-28 07:18:19 -04005276 struct btrfs_block_rsv *rsv)
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005277{
5278 struct btrfs_fs_info *fs_info = root->fs_info;
5279 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
5280 int failures = 0;
5281
5282 for (;;) {
5283 struct btrfs_trans_handle *trans;
5284 int ret;
5285
Josef Bacikad80cf52018-09-28 07:18:19 -04005286 ret = btrfs_block_rsv_refill(root, rsv, rsv->size,
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005287 BTRFS_RESERVE_FLUSH_LIMIT);
5288
5289 if (ret && ++failures > 2) {
5290 btrfs_warn(fs_info,
5291 "could not allocate space for a delete; will truncate on mount");
5292 return ERR_PTR(-ENOSPC);
5293 }
5294
5295 trans = btrfs_join_transaction(root);
5296 if (IS_ERR(trans) || !ret)
5297 return trans;
5298
5299 /*
5300 * Try to steal from the global reserve if there is space for
5301 * it.
5302 */
Lu Fengqiaf9b8a02018-10-11 13:40:35 +08005303 if (!btrfs_check_space_for_delayed_refs(trans) &&
Josef Bacikad80cf52018-09-28 07:18:19 -04005304 !btrfs_block_rsv_migrate(global_rsv, rsv, rsv->size, false))
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005305 return trans;
5306
5307 /* If not, commit and try again. */
5308 ret = btrfs_commit_transaction(trans);
5309 if (ret)
5310 return ERR_PTR(ret);
5311 }
5312}
5313
Al Virobd555972010-06-07 11:35:40 -04005314void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005315{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005316 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04005317 struct btrfs_trans_handle *trans;
5318 struct btrfs_root *root = BTRFS_I(inode)->root;
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005319 struct btrfs_block_rsv *rsv;
Chris Mason39279cc2007-06-12 06:35:45 -04005320 int ret;
5321
liubo1abe9b82011-03-24 11:18:59 +00005322 trace_btrfs_inode_evict(inode);
5323
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005324 if (!root) {
Liu Boe8f1bc12018-01-25 11:02:53 -07005325 clear_inode(inode);
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005326 return;
5327 }
5328
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005329 evict_inode_truncate_pages(inode);
5330
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005331 if (inode->i_nlink &&
5332 ((btrfs_root_refs(&root->root_item) != 0 &&
5333 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) ||
Nikolay Borisov70ddc552017-02-20 13:50:35 +02005334 btrfs_is_free_space_inode(BTRFS_I(inode))))
Al Virobd555972010-06-07 11:35:40 -04005335 goto no_delete;
5336
Omar Sandoval27919062018-05-11 13:13:37 -07005337 if (is_bad_inode(inode))
Chris Mason39279cc2007-06-12 06:35:45 -04005338 goto no_delete;
Chris Mason5f39d392007-10-15 16:14:19 -04005339
Nikolay Borisov7ab79562017-02-20 13:50:57 +02005340 btrfs_free_io_failure_record(BTRFS_I(inode), 0, (u64)-1);
Miao Xief6124962014-09-12 18:44:04 +08005341
Omar Sandoval7b40b692018-05-11 13:13:33 -07005342 if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags))
Yan, Zhengc71bf092009-11-12 09:34:40 +00005343 goto no_delete;
Yan, Zhengc71bf092009-11-12 09:34:40 +00005344
Yan, Zheng76dda932009-09-21 16:00:26 -04005345 if (inode->i_nlink > 0) {
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005346 BUG_ON(btrfs_root_refs(&root->root_item) != 0 &&
5347 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID);
Yan, Zheng76dda932009-09-21 16:00:26 -04005348 goto no_delete;
5349 }
5350
Nikolay Borisovaa790212017-01-10 20:35:40 +02005351 ret = btrfs_commit_inode_delayed_inode(BTRFS_I(inode));
Omar Sandoval27919062018-05-11 13:13:37 -07005352 if (ret)
Miao Xie0e8c36a2012-12-19 06:59:51 +00005353 goto no_delete;
Miao Xie0e8c36a2012-12-19 06:59:51 +00005354
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005355 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Omar Sandoval27919062018-05-11 13:13:37 -07005356 if (!rsv)
Josef Bacik4289a662011-08-05 13:22:24 -04005357 goto no_delete;
Josef Bacikad80cf52018-09-28 07:18:19 -04005358 rsv->size = btrfs_calc_trunc_metadata_size(fs_info, 1);
Josef Bacikca7e70f2012-08-27 17:48:15 -04005359 rsv->failfast = 1;
Josef Bacik4289a662011-08-05 13:22:24 -04005360
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02005361 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Mason5f39d392007-10-15 16:14:19 -04005362
Yan, Zheng80825102009-11-12 09:35:36 +00005363 while (1) {
Josef Bacikad80cf52018-09-28 07:18:19 -04005364 trans = evict_refill_and_join(root, rsv);
Omar Sandoval27919062018-05-11 13:13:37 -07005365 if (IS_ERR(trans))
5366 goto free_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04005367
5368 trans->block_rsv = rsv;
5369
Yan, Zhengd68fc572010-05-16 10:49:58 -04005370 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Omar Sandoval27919062018-05-11 13:13:37 -07005371 trans->block_rsv = &fs_info->trans_block_rsv;
5372 btrfs_end_transaction(trans);
5373 btrfs_btree_balance_dirty(fs_info);
5374 if (ret && ret != -ENOSPC && ret != -EAGAIN)
5375 goto free_rsv;
5376 else if (!ret)
Yan, Zheng80825102009-11-12 09:35:36 +00005377 break;
Josef Bacik7b128762008-07-24 12:17:14 -04005378 }
5379
Josef Bacik4ef31a42013-08-13 14:10:08 -04005380 /*
Omar Sandoval27919062018-05-11 13:13:37 -07005381 * Errors here aren't a big deal, it just means we leave orphan items in
5382 * the tree. They will be cleaned up on the next mount. If the inode
5383 * number gets reused, cleanup deletes the orphan item without doing
5384 * anything, and unlink reuses the existing orphan item.
5385 *
5386 * If it turns out that we are dropping too many of these, we might want
5387 * to add a mechanism for retrying these after a commit.
Josef Bacik4ef31a42013-08-13 14:10:08 -04005388 */
Josef Bacikad80cf52018-09-28 07:18:19 -04005389 trans = evict_refill_and_join(root, rsv);
Omar Sandoval27919062018-05-11 13:13:37 -07005390 if (!IS_ERR(trans)) {
5391 trans->block_rsv = rsv;
5392 btrfs_orphan_del(trans, BTRFS_I(inode));
5393 trans->block_rsv = &fs_info->trans_block_rsv;
5394 btrfs_end_transaction(trans);
5395 }
Chris Mason85e21ba2008-01-29 15:11:36 -05005396
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005397 if (!(root == fs_info->tree_root ||
Li Zefan581bb052011-04-20 10:06:11 +08005398 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005399 btrfs_return_ino(root, btrfs_ino(BTRFS_I(inode)));
Li Zefan581bb052011-04-20 10:06:11 +08005400
Omar Sandoval27919062018-05-11 13:13:37 -07005401free_rsv:
5402 btrfs_free_block_rsv(fs_info, rsv);
Chris Mason39279cc2007-06-12 06:35:45 -04005403no_delete:
Omar Sandoval27919062018-05-11 13:13:37 -07005404 /*
5405 * If we didn't successfully delete, the orphan item will still be in
5406 * the tree and we'll retry on the next mount. Again, we might also want
5407 * to retry these periodically in the future.
5408 */
Nikolay Borisovf48d1cf2017-01-10 20:35:39 +02005409 btrfs_remove_delayed_node(BTRFS_I(inode));
Jan Karadbd57682012-05-03 14:48:02 +02005410 clear_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005411}
5412
5413/*
5414 * this returns the key found in the dir entry in the location pointer.
Su Yue005d6712018-03-05 17:13:37 +08005415 * If no dir entries were found, returns -ENOENT.
5416 * If found a corrupted location in dir entry, returns -EUCLEAN.
Chris Mason39279cc2007-06-12 06:35:45 -04005417 */
5418static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
5419 struct btrfs_key *location)
5420{
5421 const char *name = dentry->d_name.name;
5422 int namelen = dentry->d_name.len;
5423 struct btrfs_dir_item *di;
5424 struct btrfs_path *path;
5425 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04005426 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005427
5428 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005429 if (!path)
5430 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05005431
David Sterbaf85b7372017-01-20 14:54:07 +01005432 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(BTRFS_I(dir)),
5433 name, namelen, 0);
Liu Bo3cf50682018-09-12 06:06:26 +08005434 if (IS_ERR_OR_NULL(di)) {
5435 ret = di ? PTR_ERR(di) : -ENOENT;
Su Yue005d6712018-03-05 17:13:37 +08005436 goto out;
5437 }
Chris Masond3977122009-01-05 21:25:51 -05005438
Chris Mason5f39d392007-10-15 16:14:19 -04005439 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Liu Bo56a0e702017-10-30 11:14:38 -06005440 if (location->type != BTRFS_INODE_ITEM_KEY &&
5441 location->type != BTRFS_ROOT_ITEM_KEY) {
Su Yue005d6712018-03-05 17:13:37 +08005442 ret = -EUCLEAN;
Liu Bo56a0e702017-10-30 11:14:38 -06005443 btrfs_warn(root->fs_info,
5444"%s gets something invalid in DIR_ITEM (name %s, directory ino %llu, location(%llu %u %llu))",
5445 __func__, name, btrfs_ino(BTRFS_I(dir)),
5446 location->objectid, location->type, location->offset);
Liu Bo56a0e702017-10-30 11:14:38 -06005447 }
Chris Mason39279cc2007-06-12 06:35:45 -04005448out:
Chris Mason39279cc2007-06-12 06:35:45 -04005449 btrfs_free_path(path);
5450 return ret;
5451}
5452
5453/*
5454 * when we hit a tree root in a directory, the btrfs part of the inode
5455 * needs to be changed to reflect the root directory of the tree root. This
5456 * is kind of like crossing a mount point.
5457 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005458static int fixup_tree_root_location(struct btrfs_fs_info *fs_info,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005459 struct inode *dir,
5460 struct dentry *dentry,
5461 struct btrfs_key *location,
5462 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04005463{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005464 struct btrfs_path *path;
5465 struct btrfs_root *new_root;
5466 struct btrfs_root_ref *ref;
5467 struct extent_buffer *leaf;
David Sterba1d4c08e2015-01-02 19:36:14 +01005468 struct btrfs_key key;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005469 int ret;
5470 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005471
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005472 path = btrfs_alloc_path();
5473 if (!path) {
5474 err = -ENOMEM;
5475 goto out;
5476 }
Chris Mason39279cc2007-06-12 06:35:45 -04005477
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005478 err = -ENOENT;
David Sterba1d4c08e2015-01-02 19:36:14 +01005479 key.objectid = BTRFS_I(dir)->root->root_key.objectid;
5480 key.type = BTRFS_ROOT_REF_KEY;
5481 key.offset = location->objectid;
5482
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005483 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005484 if (ret) {
5485 if (ret < 0)
5486 err = ret;
5487 goto out;
5488 }
Chris Mason39279cc2007-06-12 06:35:45 -04005489
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005490 leaf = path->nodes[0];
5491 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005492 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(BTRFS_I(dir)) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005493 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
5494 goto out;
5495
5496 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
5497 (unsigned long)(ref + 1),
5498 dentry->d_name.len);
5499 if (ret)
5500 goto out;
5501
David Sterbab3b4aa72011-04-21 01:20:15 +02005502 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005503
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005504 new_root = btrfs_read_fs_root_no_name(fs_info, location);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005505 if (IS_ERR(new_root)) {
5506 err = PTR_ERR(new_root);
5507 goto out;
5508 }
5509
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005510 *sub_root = new_root;
5511 location->objectid = btrfs_root_dirid(&new_root->root_item);
5512 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04005513 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005514 err = 0;
5515out:
5516 btrfs_free_path(path);
5517 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005518}
5519
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005520static void inode_tree_add(struct inode *inode)
5521{
5522 struct btrfs_root *root = BTRFS_I(inode)->root;
5523 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005524 struct rb_node **p;
5525 struct rb_node *parent;
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005526 struct rb_node *new = &BTRFS_I(inode)->rb_node;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005527 u64 ino = btrfs_ino(BTRFS_I(inode));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005528
Al Viro1d3382cb2010-10-23 15:19:20 -04005529 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005530 return;
Miao Xiee1409ce2013-05-15 07:48:16 +00005531 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005532 spin_lock(&root->inode_lock);
Miao Xiee1409ce2013-05-15 07:48:16 +00005533 p = &root->inode_tree.rb_node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005534 while (*p) {
5535 parent = *p;
5536 entry = rb_entry(parent, struct btrfs_inode, rb_node);
5537
David Sterba37508512018-06-29 10:56:40 +02005538 if (ino < btrfs_ino(entry))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005539 p = &parent->rb_left;
David Sterba37508512018-06-29 10:56:40 +02005540 else if (ino > btrfs_ino(entry))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005541 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005542 else {
5543 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04005544 (I_WILL_FREE | I_FREEING)));
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005545 rb_replace_node(parent, new, &root->inode_tree);
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005546 RB_CLEAR_NODE(parent);
5547 spin_unlock(&root->inode_lock);
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005548 return;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005549 }
5550 }
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005551 rb_link_node(new, parent, p);
5552 rb_insert_color(new, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005553 spin_unlock(&root->inode_lock);
5554}
5555
5556static void inode_tree_del(struct inode *inode)
5557{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005558 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005559 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04005560 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005561
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005562 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005563 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005564 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005565 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04005566 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005567 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005568 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04005569
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005570 if (empty && btrfs_root_refs(&root->root_item) == 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005571 synchronize_srcu(&fs_info->subvol_srcu);
Yan, Zheng76dda932009-09-21 16:00:26 -04005572 spin_lock(&root->inode_lock);
5573 empty = RB_EMPTY_ROOT(&root->inode_tree);
5574 spin_unlock(&root->inode_lock);
5575 if (empty)
5576 btrfs_add_dead_root(root);
5577 }
5578}
5579
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005580
Chris Masone02119d2008-09-05 16:13:11 -04005581static int btrfs_init_locked_inode(struct inode *inode, void *p)
5582{
5583 struct btrfs_iget_args *args = p;
Chris Mason90d3e592014-01-09 17:28:00 -08005584 inode->i_ino = args->location->objectid;
5585 memcpy(&BTRFS_I(inode)->location, args->location,
5586 sizeof(*args->location));
Chris Masone02119d2008-09-05 16:13:11 -04005587 BTRFS_I(inode)->root = args->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005588 return 0;
5589}
5590
5591static int btrfs_find_actor(struct inode *inode, void *opaque)
5592{
5593 struct btrfs_iget_args *args = opaque;
Chris Mason90d3e592014-01-09 17:28:00 -08005594 return args->location->objectid == BTRFS_I(inode)->location.objectid &&
Chris Masond3977122009-01-05 21:25:51 -05005595 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005596}
5597
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005598static struct inode *btrfs_iget_locked(struct super_block *s,
Chris Mason90d3e592014-01-09 17:28:00 -08005599 struct btrfs_key *location,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005600 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04005601{
5602 struct inode *inode;
5603 struct btrfs_iget_args args;
Chris Mason90d3e592014-01-09 17:28:00 -08005604 unsigned long hashval = btrfs_inode_hash(location->objectid, root);
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005605
Chris Mason90d3e592014-01-09 17:28:00 -08005606 args.location = location;
Chris Mason39279cc2007-06-12 06:35:45 -04005607 args.root = root;
5608
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005609 inode = iget5_locked(s, hashval, btrfs_find_actor,
Chris Mason39279cc2007-06-12 06:35:45 -04005610 btrfs_init_locked_inode,
5611 (void *)&args);
5612 return inode;
5613}
5614
Balaji Rao1a54ef82008-07-21 02:01:04 +05305615/* Get an inode object given its location and corresponding root.
5616 * Returns in *is_new if the inode was read from disk
5617 */
Filipe Manana4222ea72018-10-24 10:13:03 +01005618struct inode *btrfs_iget_path(struct super_block *s, struct btrfs_key *location,
5619 struct btrfs_root *root, int *new,
5620 struct btrfs_path *path)
Balaji Rao1a54ef82008-07-21 02:01:04 +05305621{
5622 struct inode *inode;
5623
Chris Mason90d3e592014-01-09 17:28:00 -08005624 inode = btrfs_iget_locked(s, location, root);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305625 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005626 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305627
5628 if (inode->i_state & I_NEW) {
Filipe Manana67710892016-06-06 11:51:25 +01005629 int ret;
5630
Filipe Manana4222ea72018-10-24 10:13:03 +01005631 ret = btrfs_read_locked_inode(inode, path);
Al Viro9bc2cef2018-07-29 23:04:50 +01005632 if (!ret) {
Mark Fasheh1748f842011-07-12 11:25:31 -07005633 inode_tree_add(inode);
5634 unlock_new_inode(inode);
5635 if (new)
5636 *new = 1;
5637 } else {
Al Virof5b3a412018-07-29 23:04:51 +01005638 iget_failed(inode);
5639 /*
5640 * ret > 0 can come from btrfs_search_slot called by
5641 * btrfs_read_locked_inode, this means the inode item
5642 * was not found.
5643 */
5644 if (ret > 0)
5645 ret = -ENOENT;
5646 inode = ERR_PTR(ret);
Mark Fasheh1748f842011-07-12 11:25:31 -07005647 }
5648 }
5649
Balaji Rao1a54ef82008-07-21 02:01:04 +05305650 return inode;
5651}
5652
Filipe Manana4222ea72018-10-24 10:13:03 +01005653struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
5654 struct btrfs_root *root, int *new)
5655{
5656 return btrfs_iget_path(s, location, root, new, NULL);
5657}
5658
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005659static struct inode *new_simple_dir(struct super_block *s,
5660 struct btrfs_key *key,
5661 struct btrfs_root *root)
5662{
5663 struct inode *inode = new_inode(s);
5664
5665 if (!inode)
5666 return ERR_PTR(-ENOMEM);
5667
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005668 BTRFS_I(inode)->root = root;
5669 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04005670 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005671
5672 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Li Zefan848cce02012-02-21 17:04:28 +08005673 inode->i_op = &btrfs_dir_ro_inode_operations;
Omar Sandoval1fdf4192017-01-25 17:06:39 -08005674 inode->i_opflags &= ~IOP_XATTR;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005675 inode->i_fop = &simple_dir_operations;
5676 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
Deepa Dinamanic2050a42016-09-14 07:48:06 -07005677 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05305678 inode->i_atime = inode->i_mtime;
5679 inode->i_ctime = inode->i_mtime;
Arnd Bergmannd3c6be6f2018-06-21 18:04:06 +02005680 BTRFS_I(inode)->i_otime = inode->i_mtime;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005681
5682 return inode;
5683}
5684
Chris Mason3de45862008-11-17 21:02:50 -05005685struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04005686{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005687 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masond3977122009-01-05 21:25:51 -05005688 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005689 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005690 struct btrfs_root *sub_root = root;
5691 struct btrfs_key location;
Yan, Zheng76dda932009-09-21 16:00:26 -04005692 int index;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005693 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005694
5695 if (dentry->d_name.len > BTRFS_NAME_LEN)
5696 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04005697
Jeff Layton39e3c952012-11-28 11:30:53 -05005698 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason39279cc2007-06-12 06:35:45 -04005699 if (ret < 0)
5700 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04005701
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005702 if (location.type == BTRFS_INODE_ITEM_KEY) {
Josef Bacik73f73412009-12-04 17:38:27 +00005703 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005704 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04005705 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005706
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005707 index = srcu_read_lock(&fs_info->subvol_srcu);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005708 ret = fixup_tree_root_location(fs_info, dir, dentry,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005709 &location, &sub_root);
5710 if (ret < 0) {
5711 if (ret != -ENOENT)
5712 inode = ERR_PTR(ret);
5713 else
5714 inode = new_simple_dir(dir->i_sb, &location, sub_root);
5715 } else {
Josef Bacik73f73412009-12-04 17:38:27 +00005716 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04005717 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005718 srcu_read_unlock(&fs_info->subvol_srcu, index);
Yan, Zheng76dda932009-09-21 16:00:26 -04005719
Julia Lawall34d19ba2011-01-24 19:55:19 +00005720 if (!IS_ERR(inode) && root != sub_root) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005721 down_read(&fs_info->cleanup_work_sem);
David Howellsbc98a422017-07-17 08:45:34 +01005722 if (!sb_rdonly(inode->i_sb))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005723 ret = btrfs_orphan_cleanup(sub_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005724 up_read(&fs_info->cleanup_work_sem);
Josef Bacik01cd3362013-06-03 21:39:49 -04005725 if (ret) {
5726 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005727 inode = ERR_PTR(ret);
Josef Bacik01cd3362013-06-03 21:39:49 -04005728 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00005729 }
5730
Chris Mason3de45862008-11-17 21:02:50 -05005731 return inode;
5732}
5733
Nick Pigginfe15ce42011-01-07 17:49:23 +11005734static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04005735{
5736 struct btrfs_root *root;
David Howells2b0143b2015-03-17 22:25:59 +00005737 struct inode *inode = d_inode(dentry);
Yan, Zheng76dda932009-09-21 16:00:26 -04005738
Li Zefan848cce02012-02-21 17:04:28 +08005739 if (!inode && !IS_ROOT(dentry))
David Howells2b0143b2015-03-17 22:25:59 +00005740 inode = d_inode(dentry->d_parent);
Yan, Zheng76dda932009-09-21 16:00:26 -04005741
Li Zefan848cce02012-02-21 17:04:28 +08005742 if (inode) {
5743 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04005744 if (btrfs_root_refs(&root->root_item) == 0)
5745 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08005746
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005747 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Li Zefan848cce02012-02-21 17:04:28 +08005748 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04005749 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005750 return 0;
5751}
5752
Chris Mason3de45862008-11-17 21:02:50 -05005753static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04005754 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05005755{
Al Viro3837d202018-10-10 16:38:27 -04005756 struct inode *inode = btrfs_lookup_dentry(dir, dentry);
Josef Bacika66e7cc2011-09-18 10:34:03 -04005757
Al Viro3837d202018-10-10 16:38:27 -04005758 if (inode == ERR_PTR(-ENOENT))
5759 inode = NULL;
Al Viro41d28bc2014-10-12 22:24:21 -04005760 return d_splice_alias(inode, dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04005761}
5762
Miao Xie16cdcec2011-04-22 18:12:22 +08005763unsigned char btrfs_filetype_table[] = {
Chris Mason39279cc2007-06-12 06:35:45 -04005764 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
5765};
5766
Josef Bacik23b5ec72017-07-24 15:14:25 -04005767/*
5768 * All this infrastructure exists because dir_emit can fault, and we are holding
5769 * the tree lock when doing readdir. For now just allocate a buffer and copy
5770 * our information into that, and then dir_emit from the buffer. This is
5771 * similar to what NFS does, only we don't keep the buffer around in pagecache
5772 * because I'm afraid I'll mess that up. Long term we need to make filldir do
5773 * copy_to_user_inatomic so we don't have to worry about page faulting under the
5774 * tree lock.
5775 */
5776static int btrfs_opendir(struct inode *inode, struct file *file)
5777{
5778 struct btrfs_file_private *private;
5779
5780 private = kzalloc(sizeof(struct btrfs_file_private), GFP_KERNEL);
5781 if (!private)
5782 return -ENOMEM;
5783 private->filldir_buf = kzalloc(PAGE_SIZE, GFP_KERNEL);
5784 if (!private->filldir_buf) {
5785 kfree(private);
5786 return -ENOMEM;
5787 }
5788 file->private_data = private;
5789 return 0;
5790}
5791
5792struct dir_entry {
5793 u64 ino;
5794 u64 offset;
5795 unsigned type;
5796 int name_len;
5797};
5798
5799static int btrfs_filldir(void *addr, int entries, struct dir_context *ctx)
5800{
5801 while (entries--) {
5802 struct dir_entry *entry = addr;
5803 char *name = (char *)(entry + 1);
5804
David Sterba92d32172018-04-16 21:10:14 +02005805 ctx->pos = get_unaligned(&entry->offset);
5806 if (!dir_emit(ctx, name, get_unaligned(&entry->name_len),
5807 get_unaligned(&entry->ino),
5808 get_unaligned(&entry->type)))
Josef Bacik23b5ec72017-07-24 15:14:25 -04005809 return 1;
David Sterba92d32172018-04-16 21:10:14 +02005810 addr += sizeof(struct dir_entry) +
5811 get_unaligned(&entry->name_len);
Josef Bacik23b5ec72017-07-24 15:14:25 -04005812 ctx->pos++;
5813 }
5814 return 0;
5815}
5816
Al Viro9cdda8d2013-05-22 16:48:09 -04005817static int btrfs_real_readdir(struct file *file, struct dir_context *ctx)
Chris Mason39279cc2007-06-12 06:35:45 -04005818{
Al Viro9cdda8d2013-05-22 16:48:09 -04005819 struct inode *inode = file_inode(file);
Chris Mason39279cc2007-06-12 06:35:45 -04005820 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005821 struct btrfs_file_private *private = file->private_data;
Chris Mason39279cc2007-06-12 06:35:45 -04005822 struct btrfs_dir_item *di;
5823 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04005824 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04005825 struct btrfs_path *path;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005826 void *addr;
Miao Xie16cdcec2011-04-22 18:12:22 +08005827 struct list_head ins_list;
5828 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04005829 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04005830 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04005831 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04005832 char *name_ptr;
5833 int name_len;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005834 int entries = 0;
5835 int total_len = 0;
Omar Sandoval02dbfc92016-05-20 13:50:33 -07005836 bool put = false;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005837 struct btrfs_key location;
Chris Mason5f39d392007-10-15 16:14:19 -04005838
Al Viro9cdda8d2013-05-22 16:48:09 -04005839 if (!dir_emit_dots(file, ctx))
5840 return 0;
5841
David Woodhouse49593bf2008-08-17 17:08:36 +01005842 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08005843 if (!path)
5844 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04005845
Josef Bacik23b5ec72017-07-24 15:14:25 -04005846 addr = private->filldir_buf;
David Sterbae4058b52015-11-27 16:31:35 +01005847 path->reada = READA_FORWARD;
David Woodhouse49593bf2008-08-17 17:08:36 +01005848
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005849 INIT_LIST_HEAD(&ins_list);
5850 INIT_LIST_HEAD(&del_list);
5851 put = btrfs_readdir_get_delayed_items(inode, &ins_list, &del_list);
Miao Xie16cdcec2011-04-22 18:12:22 +08005852
Josef Bacik23b5ec72017-07-24 15:14:25 -04005853again:
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005854 key.type = BTRFS_DIR_INDEX_KEY;
Al Viro9cdda8d2013-05-22 16:48:09 -04005855 key.offset = ctx->pos;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005856 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason5f39d392007-10-15 16:14:19 -04005857
Chris Mason39279cc2007-06-12 06:35:45 -04005858 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5859 if (ret < 0)
5860 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01005861
5862 while (1) {
Josef Bacik23b5ec72017-07-24 15:14:25 -04005863 struct dir_entry *entry;
5864
Chris Mason5f39d392007-10-15 16:14:19 -04005865 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04005866 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08005867 if (slot >= btrfs_header_nritems(leaf)) {
5868 ret = btrfs_next_leaf(root, path);
5869 if (ret < 0)
5870 goto err;
5871 else if (ret > 0)
5872 break;
5873 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04005874 }
Chris Mason3de45862008-11-17 21:02:50 -05005875
Chris Mason5f39d392007-10-15 16:14:19 -04005876 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5877
5878 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04005879 break;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005880 if (found_key.type != BTRFS_DIR_INDEX_KEY)
Chris Mason39279cc2007-06-12 06:35:45 -04005881 break;
Al Viro9cdda8d2013-05-22 16:48:09 -04005882 if (found_key.offset < ctx->pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08005883 goto next;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005884 if (btrfs_should_delete_dir_index(&del_list, found_key.offset))
Miao Xie16cdcec2011-04-22 18:12:22 +08005885 goto next;
Chris Mason39279cc2007-06-12 06:35:45 -04005886 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005887 name_len = btrfs_dir_name_len(leaf, di);
Josef Bacik23b5ec72017-07-24 15:14:25 -04005888 if ((total_len + sizeof(struct dir_entry) + name_len) >=
5889 PAGE_SIZE) {
5890 btrfs_release_path(path);
5891 ret = btrfs_filldir(private->filldir_buf, entries, ctx);
5892 if (ret)
5893 goto nopos;
5894 addr = private->filldir_buf;
5895 entries = 0;
5896 total_len = 0;
5897 goto again;
Chris Mason39279cc2007-06-12 06:35:45 -04005898 }
Josef Bacik23b5ec72017-07-24 15:14:25 -04005899
5900 entry = addr;
David Sterba92d32172018-04-16 21:10:14 +02005901 put_unaligned(name_len, &entry->name_len);
Josef Bacik23b5ec72017-07-24 15:14:25 -04005902 name_ptr = (char *)(entry + 1);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005903 read_extent_buffer(leaf, name_ptr, (unsigned long)(di + 1),
5904 name_len);
David Sterba92d32172018-04-16 21:10:14 +02005905 put_unaligned(btrfs_filetype_table[btrfs_dir_type(leaf, di)],
5906 &entry->type);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005907 btrfs_dir_item_key_to_cpu(leaf, di, &location);
David Sterba92d32172018-04-16 21:10:14 +02005908 put_unaligned(location.objectid, &entry->ino);
5909 put_unaligned(found_key.offset, &entry->offset);
Josef Bacik23b5ec72017-07-24 15:14:25 -04005910 entries++;
5911 addr += sizeof(struct dir_entry) + name_len;
5912 total_len += sizeof(struct dir_entry) + name_len;
Li Zefanb9e03af2011-03-23 10:43:58 +08005913next:
5914 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04005915 }
Josef Bacik23b5ec72017-07-24 15:14:25 -04005916 btrfs_release_path(path);
5917
5918 ret = btrfs_filldir(private->filldir_buf, entries, ctx);
5919 if (ret)
5920 goto nopos;
David Woodhouse49593bf2008-08-17 17:08:36 +01005921
Jeff Mahoneyd2fbb2b2016-11-05 13:26:35 -04005922 ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005923 if (ret)
David Sterbabc4ef752015-11-13 13:44:28 +01005924 goto nopos;
5925
Zach Browndb62efb2013-07-11 16:19:42 -07005926 /*
5927 * Stop new entries from being returned after we return the last
5928 * entry.
5929 *
5930 * New directory entries are assigned a strictly increasing
5931 * offset. This means that new entries created during readdir
5932 * are *guaranteed* to be seen in the future by that readdir.
5933 * This has broken buggy programs which operate on names as
5934 * they're returned by readdir. Until we re-use freed offsets
5935 * we have this hack to stop new entries from being returned
5936 * under the assumption that they'll never reach this huge
5937 * offset.
5938 *
5939 * This is being careful not to overflow 32bit loff_t unless the
5940 * last entry requires it because doing so has broken 32bit apps
5941 * in the past.
5942 */
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005943 if (ctx->pos >= INT_MAX)
5944 ctx->pos = LLONG_MAX;
5945 else
5946 ctx->pos = INT_MAX;
Chris Mason39279cc2007-06-12 06:35:45 -04005947nopos:
5948 ret = 0;
5949err:
Omar Sandoval02dbfc92016-05-20 13:50:33 -07005950 if (put)
5951 btrfs_readdir_put_delayed_items(inode, &ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04005952 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005953 return ret;
5954}
5955
Chris Mason39279cc2007-06-12 06:35:45 -04005956/*
Chris Mason54aa1f42007-06-22 14:16:25 -04005957 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04005958 * inode changes. But, it is most likely to find the inode in cache.
5959 * FIXME, needs more benchmarking...there are no reasons other than performance
5960 * to keep or drop this code.
5961 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00005962static int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005963{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005964 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04005965 struct btrfs_root *root = BTRFS_I(inode)->root;
5966 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005967 int ret;
5968
Josef Bacik72ac3c02012-05-23 14:13:11 -04005969 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05005970 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005971
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005972 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005973 if (IS_ERR(trans))
5974 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005975
5976 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005977 if (ret && ret == -ENOSPC) {
5978 /* whoops, lets try again with the full transaction */
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005979 btrfs_end_transaction(trans);
Chris Mason94b60442010-05-26 11:02:00 -04005980 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005981 if (IS_ERR(trans))
5982 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005983
Chris Mason94b60442010-05-26 11:02:00 -04005984 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005985 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005986 btrfs_end_transaction(trans);
Miao Xie16cdcec2011-04-22 18:12:22 +08005987 if (BTRFS_I(inode)->delayed_node)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005988 btrfs_balance_delayed_items(fs_info);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005989
5990 return ret;
5991}
5992
5993/*
5994 * This is a copy of file_update_time. We need this so we can return error on
5995 * ENOSPC for updating the inode in the case of file write and mmap writes.
5996 */
Deepa Dinamani95582b02018-05-08 19:36:02 -07005997static int btrfs_update_time(struct inode *inode, struct timespec64 *now,
Josef Bacike41f9412012-03-26 09:46:47 -04005998 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05005999{
Alexander Block2bc5565282012-06-15 09:49:33 +02006000 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Layton3a8c7232017-12-11 06:35:24 -05006001 bool dirty = flags & ~S_VERSION;
Alexander Block2bc5565282012-06-15 09:49:33 +02006002
6003 if (btrfs_root_readonly(root))
6004 return -EROFS;
6005
Josef Bacike41f9412012-03-26 09:46:47 -04006006 if (flags & S_VERSION)
Jeff Layton3a8c7232017-12-11 06:35:24 -05006007 dirty |= inode_maybe_inc_iversion(inode, dirty);
Josef Bacike41f9412012-03-26 09:46:47 -04006008 if (flags & S_CTIME)
6009 inode->i_ctime = *now;
6010 if (flags & S_MTIME)
6011 inode->i_mtime = *now;
6012 if (flags & S_ATIME)
6013 inode->i_atime = *now;
Jeff Layton3a8c7232017-12-11 06:35:24 -05006014 return dirty ? btrfs_dirty_inode(inode) : 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006015}
6016
Chris Masond352ac62008-09-29 15:18:18 -04006017/*
6018 * find the highest existing sequence number in a directory
6019 * and then set the in-memory index_cnt variable to reflect
6020 * free sequence numbers
6021 */
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006022static int btrfs_set_inode_index_count(struct btrfs_inode *inode)
Josef Bacikaec74772008-07-24 12:12:38 -04006023{
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006024 struct btrfs_root *root = inode->root;
Josef Bacikaec74772008-07-24 12:12:38 -04006025 struct btrfs_key key, found_key;
6026 struct btrfs_path *path;
6027 struct extent_buffer *leaf;
6028 int ret;
6029
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006030 key.objectid = btrfs_ino(inode);
David Sterba962a2982014-06-04 18:41:45 +02006031 key.type = BTRFS_DIR_INDEX_KEY;
Josef Bacikaec74772008-07-24 12:12:38 -04006032 key.offset = (u64)-1;
6033
6034 path = btrfs_alloc_path();
6035 if (!path)
6036 return -ENOMEM;
6037
6038 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
6039 if (ret < 0)
6040 goto out;
6041 /* FIXME: we should be able to handle this */
6042 if (ret == 0)
6043 goto out;
6044 ret = 0;
6045
6046 /*
6047 * MAGIC NUMBER EXPLANATION:
6048 * since we search a directory based on f_pos we have to start at 2
6049 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
6050 * else has to start at 2
6051 */
6052 if (path->slots[0] == 0) {
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006053 inode->index_cnt = 2;
Josef Bacikaec74772008-07-24 12:12:38 -04006054 goto out;
6055 }
6056
6057 path->slots[0]--;
6058
6059 leaf = path->nodes[0];
6060 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6061
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006062 if (found_key.objectid != btrfs_ino(inode) ||
David Sterba962a2982014-06-04 18:41:45 +02006063 found_key.type != BTRFS_DIR_INDEX_KEY) {
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006064 inode->index_cnt = 2;
Josef Bacikaec74772008-07-24 12:12:38 -04006065 goto out;
6066 }
6067
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006068 inode->index_cnt = found_key.offset + 1;
Josef Bacikaec74772008-07-24 12:12:38 -04006069out:
6070 btrfs_free_path(path);
6071 return ret;
6072}
6073
Chris Masond352ac62008-09-29 15:18:18 -04006074/*
6075 * helper to find a free sequence number in a given directory. This current
6076 * code is very simple, later versions will do smarter things in the btree
6077 */
Nikolay Borisov877574e2017-02-20 13:50:33 +02006078int btrfs_set_inode_index(struct btrfs_inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04006079{
6080 int ret = 0;
6081
Nikolay Borisov877574e2017-02-20 13:50:33 +02006082 if (dir->index_cnt == (u64)-1) {
6083 ret = btrfs_inode_delayed_dir_index_count(dir);
Miao Xie16cdcec2011-04-22 18:12:22 +08006084 if (ret) {
6085 ret = btrfs_set_inode_index_count(dir);
6086 if (ret)
6087 return ret;
6088 }
Josef Bacikaec74772008-07-24 12:12:38 -04006089 }
6090
Nikolay Borisov877574e2017-02-20 13:50:33 +02006091 *index = dir->index_cnt;
6092 dir->index_cnt++;
Josef Bacikaec74772008-07-24 12:12:38 -04006093
6094 return ret;
6095}
6096
Chris Masonb0d5d102014-09-08 13:08:51 -07006097static int btrfs_insert_inode_locked(struct inode *inode)
6098{
6099 struct btrfs_iget_args args;
6100 args.location = &BTRFS_I(inode)->location;
6101 args.root = BTRFS_I(inode)->root;
6102
6103 return insert_inode_locked4(inode,
6104 btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root),
6105 btrfs_find_actor, &args);
6106}
6107
Anand Jain19aee8d2017-07-18 17:37:05 +08006108/*
6109 * Inherit flags from the parent inode.
6110 *
6111 * Currently only the compression flags and the cow flags are inherited.
6112 */
6113static void btrfs_inherit_iflags(struct inode *inode, struct inode *dir)
6114{
6115 unsigned int flags;
6116
6117 if (!dir)
6118 return;
6119
6120 flags = BTRFS_I(dir)->flags;
6121
6122 if (flags & BTRFS_INODE_NOCOMPRESS) {
6123 BTRFS_I(inode)->flags &= ~BTRFS_INODE_COMPRESS;
6124 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
6125 } else if (flags & BTRFS_INODE_COMPRESS) {
6126 BTRFS_I(inode)->flags &= ~BTRFS_INODE_NOCOMPRESS;
6127 BTRFS_I(inode)->flags |= BTRFS_INODE_COMPRESS;
6128 }
6129
6130 if (flags & BTRFS_INODE_NODATACOW) {
6131 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
6132 if (S_ISREG(inode->i_mode))
6133 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
6134 }
6135
David Sterba7b6a2212018-03-26 18:40:21 +02006136 btrfs_sync_inode_flags_to_i_flags(inode);
Anand Jain19aee8d2017-07-18 17:37:05 +08006137}
6138
Chris Mason39279cc2007-06-12 06:35:45 -04006139static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
6140 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04006141 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05006142 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04006143 u64 ref_objectid, u64 objectid,
6144 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04006145{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006146 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04006147 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006148 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04006149 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04006150 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05006151 struct btrfs_inode_ref *ref;
6152 struct btrfs_key key[2];
6153 u32 sizes[2];
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006154 int nitems = name ? 2 : 1;
Chris Mason9c583092008-01-29 15:15:18 -05006155 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04006156 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006157
Chris Mason5f39d392007-10-15 16:14:19 -04006158 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07006159 if (!path)
6160 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04006161
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006162 inode = new_inode(fs_info->sb);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006163 if (!inode) {
6164 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04006165 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006166 }
Chris Mason39279cc2007-06-12 06:35:45 -04006167
Li Zefan581bb052011-04-20 10:06:11 +08006168 /*
Filipe Manana5762b5c2014-08-01 00:10:32 +01006169 * O_TMPFILE, set link count to 0, so that after this point,
6170 * we fill in an inode item with the correct link count.
6171 */
6172 if (!name)
6173 set_nlink(inode, 0);
6174
6175 /*
Li Zefan581bb052011-04-20 10:06:11 +08006176 * we have to initialize this early, so we can reclaim the inode
6177 * number if we fail afterwards in this function.
6178 */
6179 inode->i_ino = objectid;
6180
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006181 if (dir && name) {
liubo1abe9b82011-03-24 11:18:59 +00006182 trace_btrfs_inode_request(dir);
6183
Nikolay Borisov877574e2017-02-20 13:50:33 +02006184 ret = btrfs_set_inode_index(BTRFS_I(dir), index);
Shen Feng09771432009-04-02 16:46:06 -04006185 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006186 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04006187 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04006188 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04006189 }
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006190 } else if (dir) {
6191 *index = 0;
Josef Bacikaec74772008-07-24 12:12:38 -04006192 }
6193 /*
6194 * index_cnt is ignored for everything but a dir,
Su Yuedf6703e2018-01-12 11:08:02 +08006195 * btrfs_set_inode_index_count has an explanation for the magic
Josef Bacikaec74772008-07-24 12:12:38 -04006196 * number
6197 */
6198 BTRFS_I(inode)->index_cnt = 2;
Miao Xie67de1172013-12-26 13:07:06 +08006199 BTRFS_I(inode)->dir_index = *index;
Chris Mason39279cc2007-06-12 06:35:45 -04006200 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04006201 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00006202 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db22007-08-27 16:49:44 -04006203
Josef Bacik5dc562c2012-08-17 13:14:17 -04006204 /*
6205 * We could have gotten an inode number from somebody who was fsynced
6206 * and then removed in this same transaction, so let's just set full
6207 * sync since it will be a full sync anyway and this will blow away the
6208 * old info in the log.
6209 */
6210 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
6211
Chris Mason9c583092008-01-29 15:15:18 -05006212 key[0].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006213 key[0].type = BTRFS_INODE_ITEM_KEY;
Chris Mason9c583092008-01-29 15:15:18 -05006214 key[0].offset = 0;
6215
Chris Mason9c583092008-01-29 15:15:18 -05006216 sizes[0] = sizeof(struct btrfs_inode_item);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006217
6218 if (name) {
6219 /*
6220 * Start new inodes with an inode_ref. This is slightly more
6221 * efficient for small numbers of hard links since they will
6222 * be packed into one item. Extended refs will kick in if we
6223 * add more hard links than can fit in the ref item.
6224 */
6225 key[1].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006226 key[1].type = BTRFS_INODE_REF_KEY;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006227 key[1].offset = ref_objectid;
6228
6229 sizes[1] = name_len + sizeof(*ref);
6230 }
Chris Mason9c583092008-01-29 15:15:18 -05006231
Chris Masonb0d5d102014-09-08 13:08:51 -07006232 location = &BTRFS_I(inode)->location;
6233 location->objectid = objectid;
6234 location->offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02006235 location->type = BTRFS_INODE_ITEM_KEY;
Chris Masonb0d5d102014-09-08 13:08:51 -07006236
6237 ret = btrfs_insert_inode_locked(inode);
Al Viro32955c52018-05-16 12:20:05 -04006238 if (ret < 0) {
6239 iput(inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006240 goto fail;
Al Viro32955c52018-05-16 12:20:05 -04006241 }
Chris Masonb0d5d102014-09-08 13:08:51 -07006242
Chris Masonb9473432009-03-13 11:00:37 -04006243 path->leave_spinning = 1;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006244 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems);
Chris Mason9c583092008-01-29 15:15:18 -05006245 if (ret != 0)
Chris Masonb0d5d102014-09-08 13:08:51 -07006246 goto fail_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04006247
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03006248 inode_init_owner(inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04006249 inode_set_bytes(inode, 0);
chandan r9cc97d62012-07-04 12:48:07 +05306250
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006251 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05306252 inode->i_atime = inode->i_mtime;
6253 inode->i_ctime = inode->i_mtime;
Arnd Bergmannd3c6be6f2018-06-21 18:04:06 +02006254 BTRFS_I(inode)->i_otime = inode->i_mtime;
chandan r9cc97d62012-07-04 12:48:07 +05306255
Chris Mason5f39d392007-10-15 16:14:19 -04006256 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
6257 struct btrfs_inode_item);
David Sterbab159fa22016-11-08 18:09:03 +01006258 memzero_extent_buffer(path->nodes[0], (unsigned long)inode_item,
Li Zefan293f7e02012-07-10 00:58:58 -06006259 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04006260 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05006261
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006262 if (name) {
6263 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
6264 struct btrfs_inode_ref);
6265 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
6266 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
6267 ptr = (unsigned long)(ref + 1);
6268 write_extent_buffer(path->nodes[0], name, ptr, name_len);
6269 }
Chris Mason9c583092008-01-29 15:15:18 -05006270
Chris Mason5f39d392007-10-15 16:14:19 -04006271 btrfs_mark_buffer_dirty(path->nodes[0]);
6272 btrfs_free_path(path);
6273
Christoph Hellwig6cbff002009-04-17 10:37:41 +02006274 btrfs_inherit_iflags(inode, dir);
6275
Al Viro569254b2011-07-24 17:08:40 -04006276 if (S_ISREG(mode)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006277 if (btrfs_test_opt(fs_info, NODATASUM))
Chris Mason94272162009-07-02 12:26:06 -04006278 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006279 if (btrfs_test_opt(fs_info, NODATACOW))
Josef Bacikf2bdf9a2013-02-21 15:28:28 -05006280 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
6281 BTRFS_INODE_NODATASUM;
Chris Mason94272162009-07-02 12:26:06 -04006282 }
6283
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006284 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00006285
6286 trace_btrfs_inode_new(inode);
Chris Mason1973f0f2011-06-24 13:13:29 -04006287 btrfs_set_inode_last_trans(trans, inode);
liubo1abe9b82011-03-24 11:18:59 +00006288
Alexander Block8ea05e32012-07-25 17:35:53 +02006289 btrfs_update_root_times(trans, root);
6290
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006291 ret = btrfs_inode_inherit_props(trans, inode, dir);
6292 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006293 btrfs_err(fs_info,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006294 "error inheriting props for ino %llu (root %llu): %d",
David Sterbaf85b7372017-01-20 14:54:07 +01006295 btrfs_ino(BTRFS_I(inode)), root->root_key.objectid, ret);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006296
Chris Mason39279cc2007-06-12 06:35:45 -04006297 return inode;
Chris Masonb0d5d102014-09-08 13:08:51 -07006298
6299fail_unlock:
Al Viro32955c52018-05-16 12:20:05 -04006300 discard_new_inode(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006301fail:
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006302 if (dir && name)
Josef Bacikaec74772008-07-24 12:12:38 -04006303 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04006304 btrfs_free_path(path);
6305 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006306}
6307
6308static inline u8 btrfs_inode_type(struct inode *inode)
6309{
6310 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
6311}
6312
Chris Masond352ac62008-09-29 15:18:18 -04006313/*
6314 * utility function to add 'inode' into 'parent_inode' with
6315 * a give name and a given sequence number.
6316 * if 'add_backref' is true, also insert a backref from the
6317 * inode to the parent directory.
6318 */
Chris Masone02119d2008-09-05 16:13:11 -04006319int btrfs_add_link(struct btrfs_trans_handle *trans,
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006320 struct btrfs_inode *parent_inode, struct btrfs_inode *inode,
Chris Masone02119d2008-09-05 16:13:11 -04006321 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006322{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006323 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006324 struct btrfs_key key;
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006325 struct btrfs_root *root = parent_inode->root;
6326 u64 ino = btrfs_ino(inode);
6327 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006328
Li Zefan33345d012011-04-20 10:31:50 +08006329 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006330 memcpy(&key, &inode->root->root_key, sizeof(key));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006331 } else {
Li Zefan33345d012011-04-20 10:31:50 +08006332 key.objectid = ino;
David Sterba962a2982014-06-04 18:41:45 +02006333 key.type = BTRFS_INODE_ITEM_KEY;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006334 key.offset = 0;
6335 }
Chris Mason39279cc2007-06-12 06:35:45 -04006336
Li Zefan33345d012011-04-20 10:31:50 +08006337 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Lu Fengqi6025c192018-08-01 11:32:29 +08006338 ret = btrfs_add_root_ref(trans, key.objectid,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006339 root->root_key.objectid, parent_ino,
6340 index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006341 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08006342 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
6343 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006344 }
6345
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006346 /* Nothing to clean up yet */
6347 if (ret)
6348 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006349
Lu Fengqi684572d2018-08-04 21:10:57 +08006350 ret = btrfs_insert_dir_item(trans, name, name_len, parent_inode, &key,
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006351 btrfs_inode_type(&inode->vfs_inode), index);
Chris Mason9c520572012-12-17 14:26:57 -05006352 if (ret == -EEXIST || ret == -EOVERFLOW)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006353 goto fail_dir_item;
6354 else if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04006355 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006356 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006357 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006358
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006359 btrfs_i_size_write(parent_inode, parent_inode->vfs_inode.i_size +
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006360 name_len * 2);
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006361 inode_inc_iversion(&parent_inode->vfs_inode);
6362 parent_inode->vfs_inode.i_mtime = parent_inode->vfs_inode.i_ctime =
6363 current_time(&parent_inode->vfs_inode);
6364 ret = btrfs_update_inode(trans, root, &parent_inode->vfs_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006365 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04006366 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006367 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05006368
6369fail_dir_item:
6370 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
6371 u64 local_index;
6372 int err;
Lu Fengqi3ee1c552018-08-01 11:32:28 +08006373 err = btrfs_del_root_ref(trans, key.objectid,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006374 root->root_key.objectid, parent_ino,
6375 &local_index, name, name_len);
Chris Masonfe66a052012-02-20 08:40:56 -05006376
6377 } else if (add_backref) {
6378 u64 local_index;
6379 int err;
6380
6381 err = btrfs_del_inode_ref(trans, root, name, name_len,
6382 ino, parent_ino, &local_index);
6383 }
6384 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006385}
6386
6387static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006388 struct btrfs_inode *dir, struct dentry *dentry,
6389 struct btrfs_inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006390{
Josef Bacika1b075d2010-11-19 20:36:11 +00006391 int err = btrfs_add_link(trans, dir, inode,
6392 dentry->d_name.name, dentry->d_name.len,
6393 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006394 if (err > 0)
6395 err = -EEXIST;
6396 return err;
6397}
6398
Josef Bacik618e21d2007-07-11 10:18:17 -04006399static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04006400 umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04006401{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006402 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Josef Bacik618e21d2007-07-11 10:18:17 -04006403 struct btrfs_trans_handle *trans;
6404 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006405 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04006406 int err;
Josef Bacik618e21d2007-07-11 10:18:17 -04006407 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006408 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04006409
Josef Bacik9ed74f22009-09-11 16:12:44 -04006410 /*
6411 * 2 for inode item and ref
6412 * 2 for dir items
6413 * 1 for xattr if selinux is on
6414 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006415 trans = btrfs_start_transaction(root, 5);
6416 if (IS_ERR(trans))
6417 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05006418
Li Zefan581bb052011-04-20 10:06:11 +08006419 err = btrfs_find_free_ino(root, &objectid);
6420 if (err)
6421 goto out_unlock;
6422
Josef Bacikaec74772008-07-24 12:12:38 -04006423 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006424 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6425 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006426 if (IS_ERR(inode)) {
6427 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -04006428 inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04006429 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006430 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006431
Casey Schauflerad19db72011-12-15 10:09:07 -05006432 /*
6433 * If the active LSM wants to access the inode during
6434 * d_instantiate it needs these. Smack checks to see
6435 * if the filesystem supports xattrs by looking at the
6436 * ops vector.
6437 */
Casey Schauflerad19db72011-12-15 10:09:07 -05006438 inode->i_op = &btrfs_special_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006439 init_special_inode(inode, inode->i_mode, rdev);
6440
6441 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik618e21d2007-07-11 10:18:17 -04006442 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006443 goto out_unlock;
Chris Masonb0d5d102014-09-08 13:08:51 -07006444
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006445 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6446 0, index);
Al Viro32955c52018-05-16 12:20:05 -04006447 if (err)
6448 goto out_unlock;
6449
6450 btrfs_update_inode(trans, root, inode);
6451 d_instantiate_new(dentry, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006452
Josef Bacik618e21d2007-07-11 10:18:17 -04006453out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006454 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006455 btrfs_btree_balance_dirty(fs_info);
Al Viro32955c52018-05-16 12:20:05 -04006456 if (err && inode) {
Josef Bacik618e21d2007-07-11 10:18:17 -04006457 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -04006458 discard_new_inode(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006459 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006460 return err;
6461}
6462
Chris Mason39279cc2007-06-12 06:35:45 -04006463static int btrfs_create(struct inode *dir, struct dentry *dentry,
Al Viroebfc3b42012-06-10 18:05:36 -04006464 umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -04006465{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006466 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04006467 struct btrfs_trans_handle *trans;
6468 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006469 struct inode *inode = NULL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006470 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04006471 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006472 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006473
Josef Bacik9ed74f22009-09-11 16:12:44 -04006474 /*
6475 * 2 for inode item and ref
6476 * 2 for dir items
6477 * 1 for xattr if selinux is on
6478 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006479 trans = btrfs_start_transaction(root, 5);
6480 if (IS_ERR(trans))
6481 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006482
Li Zefan581bb052011-04-20 10:06:11 +08006483 err = btrfs_find_free_ino(root, &objectid);
6484 if (err)
6485 goto out_unlock;
6486
Josef Bacikaec74772008-07-24 12:12:38 -04006487 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006488 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6489 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006490 if (IS_ERR(inode)) {
6491 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -04006492 inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006493 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006494 }
Casey Schauflerad19db72011-12-15 10:09:07 -05006495 /*
6496 * If the active LSM wants to access the inode during
6497 * d_instantiate it needs these. Smack checks to see
6498 * if the filesystem supports xattrs by looking at the
6499 * ops vector.
6500 */
6501 inode->i_fop = &btrfs_file_operations;
6502 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006503 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006504
6505 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
6506 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006507 goto out_unlock;
Chris Masonb0d5d102014-09-08 13:08:51 -07006508
6509 err = btrfs_update_inode(trans, root, inode);
6510 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006511 goto out_unlock;
Casey Schauflerad19db72011-12-15 10:09:07 -05006512
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006513 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6514 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006515 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006516 goto out_unlock;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006517
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006518 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Al Viro1e2e5472018-05-04 08:23:01 -04006519 d_instantiate_new(dentry, inode);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006520
Chris Mason39279cc2007-06-12 06:35:45 -04006521out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006522 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -04006523 if (err && inode) {
Chris Mason39279cc2007-06-12 06:35:45 -04006524 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -04006525 discard_new_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006526 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006527 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006528 return err;
6529}
6530
6531static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
6532 struct dentry *dentry)
6533{
Filipe Manana271dba452016-01-05 16:24:05 +00006534 struct btrfs_trans_handle *trans = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006535 struct btrfs_root *root = BTRFS_I(dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00006536 struct inode *inode = d_inode(old_dentry);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006537 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason00e4e6b2008-08-05 11:18:09 -04006538 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04006539 int err;
6540 int drop_inode = 0;
6541
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006542 /* do not allow sys_link's with other subvols of the same device */
Misono Tomohiro4fd786e2018-08-06 14:25:24 +09006543 if (root->root_key.objectid != BTRFS_I(inode)->root->root_key.objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00006544 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006545
Mark Fashehf1863732012-08-08 11:32:27 -07006546 if (inode->i_nlink >= BTRFS_LINK_MAX)
Al Viroc055e992011-03-04 17:15:18 +00006547 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04006548
Nikolay Borisov877574e2017-02-20 13:50:33 +02006549 err = btrfs_set_inode_index(BTRFS_I(dir), &index);
Josef Bacikaec74772008-07-24 12:12:38 -04006550 if (err)
6551 goto fail;
6552
Yan, Zhenga22285a2010-05-16 10:48:46 -04006553 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00006554 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04006555 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00006556 * 1 item for parent inode
Omar Sandoval399b0bb2018-05-11 13:13:40 -07006557 * 1 item for orphan item deletion if O_TMPFILE
Yan, Zhenga22285a2010-05-16 10:48:46 -04006558 */
Omar Sandoval399b0bb2018-05-11 13:13:40 -07006559 trans = btrfs_start_transaction(root, inode->i_nlink ? 5 : 6);
Yan, Zhenga22285a2010-05-16 10:48:46 -04006560 if (IS_ERR(trans)) {
6561 err = PTR_ERR(trans);
Filipe Manana271dba452016-01-05 16:24:05 +00006562 trans = NULL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006563 goto fail;
6564 }
Chris Mason5f39d392007-10-15 16:14:19 -04006565
Miao Xie67de1172013-12-26 13:07:06 +08006566 /* There are several dir indexes for this inode, clear the cache. */
6567 BTRFS_I(inode)->dir_index = 0ULL;
Zach Brown8b558c52013-10-16 12:10:34 -07006568 inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04006569 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006570 inode->i_ctime = current_time(inode);
Al Viro7de9c6ee2010-10-23 11:11:40 -04006571 ihold(inode);
Josef Bacike9976152012-10-11 15:53:56 -04006572 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
Josef Bacikaec74772008-07-24 12:12:38 -04006573
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006574 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6575 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04006576
Yan, Zhenga5719522009-09-24 09:17:31 -04006577 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006578 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04006579 } else {
Al Viro10d9f302011-07-16 23:09:10 -04006580 struct dentry *parent = dentry->d_parent;
Filipe Mananad4682ba2018-06-11 19:24:28 +01006581 int ret;
6582
Yan, Zhenga5719522009-09-24 09:17:31 -04006583 err = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006584 if (err)
6585 goto fail;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006586 if (inode->i_nlink == 1) {
6587 /*
6588 * If new hard link count is 1, it's a file created
6589 * with open(2) O_TMPFILE flag.
6590 */
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02006591 err = btrfs_orphan_del(trans, BTRFS_I(inode));
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006592 if (err)
6593 goto fail;
6594 }
Al Viro08c422c2011-12-23 07:58:13 -05006595 d_instantiate(dentry, inode);
Filipe Mananad4682ba2018-06-11 19:24:28 +01006596 ret = btrfs_log_new_name(trans, BTRFS_I(inode), NULL, parent,
6597 true, NULL);
6598 if (ret == BTRFS_NEED_TRANS_COMMIT) {
6599 err = btrfs_commit_transaction(trans);
6600 trans = NULL;
6601 }
Yan, Zhenga5719522009-09-24 09:17:31 -04006602 }
Chris Mason39279cc2007-06-12 06:35:45 -04006603
Chris Mason1832a6d2007-12-21 16:27:21 -05006604fail:
Filipe Manana271dba452016-01-05 16:24:05 +00006605 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006606 btrfs_end_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006607 if (drop_inode) {
6608 inode_dec_link_count(inode);
6609 iput(inode);
6610 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006611 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006612 return err;
6613}
6614
Al Viro18bb1db2011-07-26 01:41:39 -04006615static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04006616{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006617 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masonb9d86662008-05-02 16:13:49 -04006618 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006619 struct btrfs_trans_handle *trans;
6620 struct btrfs_root *root = BTRFS_I(dir)->root;
6621 int err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04006622 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006623 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006624
Josef Bacik9ed74f22009-09-11 16:12:44 -04006625 /*
6626 * 2 items for inode and ref
6627 * 2 items for dir items
6628 * 1 for xattr if selinux is on
6629 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006630 trans = btrfs_start_transaction(root, 5);
6631 if (IS_ERR(trans))
6632 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006633
Li Zefan581bb052011-04-20 10:06:11 +08006634 err = btrfs_find_free_ino(root, &objectid);
6635 if (err)
6636 goto out_fail;
6637
Josef Bacikaec74772008-07-24 12:12:38 -04006638 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006639 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6640 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04006641 if (IS_ERR(inode)) {
6642 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -04006643 inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006644 goto out_fail;
6645 }
Chris Mason5f39d392007-10-15 16:14:19 -04006646
Chris Masonb0d5d102014-09-08 13:08:51 -07006647 /* these must be set before we unlock the inode */
6648 inode->i_op = &btrfs_dir_inode_operations;
6649 inode->i_fop = &btrfs_dir_file_operations;
Josef Bacik33268ea2008-07-24 12:16:36 -04006650
Eric Paris2a7dba32011-02-01 11:05:39 -05006651 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04006652 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006653 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04006654
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02006655 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Mason39279cc2007-06-12 06:35:45 -04006656 err = btrfs_update_inode(trans, root, inode);
6657 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006658 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04006659
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006660 err = btrfs_add_link(trans, BTRFS_I(dir), BTRFS_I(inode),
6661 dentry->d_name.name,
6662 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006663 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006664 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04006665
Al Viro1e2e5472018-05-04 08:23:01 -04006666 d_instantiate_new(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006667
6668out_fail:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006669 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -04006670 if (err && inode) {
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006671 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -04006672 discard_new_inode(inode);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006673 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006674 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006675 return err;
6676}
6677
Chris Masonc8b97812008-10-29 14:49:59 -04006678static noinline int uncompress_inline(struct btrfs_path *path,
Byongho Leee40da0e2015-05-19 23:46:45 +09006679 struct page *page,
Chris Masonc8b97812008-10-29 14:49:59 -04006680 size_t pg_offset, u64 extent_offset,
6681 struct btrfs_file_extent_item *item)
6682{
6683 int ret;
6684 struct extent_buffer *leaf = path->nodes[0];
6685 char *tmp;
6686 size_t max_size;
6687 unsigned long inline_size;
6688 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08006689 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04006690
6691 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08006692 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04006693 max_size = btrfs_file_extent_ram_bytes(leaf, item);
6694 inline_size = btrfs_file_extent_inline_item_len(leaf,
Ross Kirkdd3cc162013-09-16 15:58:09 +01006695 btrfs_item_nr(path->slots[0]));
Chris Masonc8b97812008-10-29 14:49:59 -04006696 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04006697 if (!tmp)
6698 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04006699 ptr = btrfs_file_extent_inline_start(item);
6700
6701 read_extent_buffer(leaf, tmp, ptr, inline_size);
6702
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006703 max_size = min_t(unsigned long, PAGE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08006704 ret = btrfs_decompress(compress_type, tmp, page,
6705 extent_offset, inline_size, max_size);
Zygo Blaxelle1699d22017-03-10 16:45:44 -05006706
6707 /*
6708 * decompression code contains a memset to fill in any space between the end
6709 * of the uncompressed data and the end of max_size in case the decompressed
6710 * data ends up shorter than ram_bytes. That doesn't cover the hole between
6711 * the end of an inline extent and the beginning of the next block, so we
6712 * cover that region here.
6713 */
6714
6715 if (max_size + pg_offset < PAGE_SIZE) {
6716 char *map = kmap(page);
6717 memset(map + pg_offset + max_size, 0, PAGE_SIZE - max_size - pg_offset);
6718 kunmap(page);
6719 }
Chris Masonc8b97812008-10-29 14:49:59 -04006720 kfree(tmp);
Zach Brown166ae5a2014-05-09 17:15:10 -04006721 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -04006722}
6723
Chris Masond352ac62008-09-29 15:18:18 -04006724/*
6725 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05006726 * the ugly parts come from merging extents from the disk with the in-ram
6727 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04006728 * where the in-ram extents might be locked pending data=ordered completion.
6729 *
6730 * This also copies inline extents directly into the page.
6731 */
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006732struct extent_map *btrfs_get_extent(struct btrfs_inode *inode,
Liu Bode2c6612018-08-25 13:47:59 +08006733 struct page *page,
6734 size_t pg_offset, u64 start, u64 len,
6735 int create)
Chris Masona52d9a82007-08-27 16:49:44 -04006736{
David Sterba3ffbd682018-06-29 10:56:42 +02006737 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Chris Masona52d9a82007-08-27 16:49:44 -04006738 int ret;
6739 int err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006740 u64 extent_start = 0;
6741 u64 extent_end = 0;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006742 u64 objectid = btrfs_ino(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04006743 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04006744 struct btrfs_path *path = NULL;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006745 struct btrfs_root *root = inode->root;
Chris Masona52d9a82007-08-27 16:49:44 -04006746 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04006747 struct extent_buffer *leaf;
6748 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04006749 struct extent_map *em = NULL;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006750 struct extent_map_tree *em_tree = &inode->extent_tree;
6751 struct extent_io_tree *io_tree = &inode->io_tree;
Filipe Manana7ffbb592014-06-09 03:48:05 +01006752 const bool new_inline = !page || create;
Chris Masona52d9a82007-08-27 16:49:44 -04006753
Chris Mason890871b2009-09-02 16:24:52 -04006754 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006755 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04006756 if (em)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006757 em->bdev = fs_info->fs_devices->latest_bdev;
Chris Mason890871b2009-09-02 16:24:52 -04006758 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006759
Chris Masona52d9a82007-08-27 16:49:44 -04006760 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04006761 if (em->start > start || em->start + em->len <= start)
6762 free_extent_map(em);
6763 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05006764 free_extent_map(em);
6765 else
6766 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006767 }
David Sterba172ddd62011-04-21 00:48:27 +02006768 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04006769 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05006770 err = -ENOMEM;
6771 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006772 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006773 em->bdev = fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05006774 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05006775 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05006776 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04006777 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04006778
Liu Bobee6ec82018-08-17 05:05:28 +08006779 path = btrfs_alloc_path();
Chris Masonf4219502008-07-22 11:18:09 -04006780 if (!path) {
Liu Bobee6ec82018-08-17 05:05:28 +08006781 err = -ENOMEM;
6782 goto out;
Chris Masonf4219502008-07-22 11:18:09 -04006783 }
6784
Liu Bobee6ec82018-08-17 05:05:28 +08006785 /* Chances are we'll be called again, so go ahead and do readahead */
6786 path->reada = READA_FORWARD;
6787
Liu Boe49aabd2018-08-25 13:47:09 +08006788 /*
6789 * Unless we're going to uncompress the inline extent, no sleep would
6790 * happen.
6791 */
6792 path->leave_spinning = 1;
6793
Nikolay Borisov5c9a7022017-12-01 11:19:40 +02006794 ret = btrfs_lookup_file_extent(NULL, root, path, objectid, start, 0);
Chris Masona52d9a82007-08-27 16:49:44 -04006795 if (ret < 0) {
6796 err = ret;
6797 goto out;
6798 }
6799
6800 if (ret != 0) {
6801 if (path->slots[0] == 0)
6802 goto not_found;
6803 path->slots[0]--;
6804 }
6805
Chris Mason5f39d392007-10-15 16:14:19 -04006806 leaf = path->nodes[0];
6807 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04006808 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04006809 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04006810 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02006811 found_type = found_key.type;
Chris Mason5f39d392007-10-15 16:14:19 -04006812 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04006813 found_type != BTRFS_EXTENT_DATA_KEY) {
Josef Bacik25a50342013-10-14 12:08:38 -04006814 /*
6815 * If we backup past the first extent we want to move forward
6816 * and see if there is an extent in front of us, otherwise we'll
6817 * say there is a hole for our whole search range which can
6818 * cause problems.
6819 */
6820 extent_end = start;
6821 goto next;
Chris Masona52d9a82007-08-27 16:49:44 -04006822 }
6823
Chris Mason5f39d392007-10-15 16:14:19 -04006824 found_type = btrfs_file_extent_type(leaf, item);
6825 extent_start = found_key.offset;
Yan Zhengd899e052008-10-30 14:25:28 -04006826 if (found_type == BTRFS_FILE_EXTENT_REG ||
6827 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006828 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04006829 btrfs_file_extent_num_bytes(leaf, item);
Liu Bo09ed2f12017-03-10 11:09:48 -08006830
6831 trace_btrfs_get_extent_show_fi_regular(inode, leaf, item,
6832 extent_start);
Yan Zheng9036c102008-10-30 14:19:41 -04006833 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
6834 size_t size;
Qu Wenruoe41ca582018-06-06 15:41:49 +08006835
6836 size = btrfs_file_extent_ram_bytes(leaf, item);
Jeff Mahoneyda170662016-06-15 09:22:56 -04006837 extent_end = ALIGN(extent_start + size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006838 fs_info->sectorsize);
Liu Bo09ed2f12017-03-10 11:09:48 -08006839
6840 trace_btrfs_get_extent_show_fi_inline(inode, leaf, item,
6841 path->slots[0],
6842 extent_start);
Yan Zheng9036c102008-10-30 14:19:41 -04006843 }
Josef Bacik25a50342013-10-14 12:08:38 -04006844next:
Yan Zheng9036c102008-10-30 14:19:41 -04006845 if (start >= extent_end) {
6846 path->slots[0]++;
6847 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
6848 ret = btrfs_next_leaf(root, path);
6849 if (ret < 0) {
6850 err = ret;
6851 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006852 }
Yan Zheng9036c102008-10-30 14:19:41 -04006853 if (ret > 0)
6854 goto not_found;
6855 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04006856 }
Yan Zheng9036c102008-10-30 14:19:41 -04006857 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6858 if (found_key.objectid != objectid ||
6859 found_key.type != BTRFS_EXTENT_DATA_KEY)
6860 goto not_found;
6861 if (start + len <= found_key.offset)
6862 goto not_found;
Wang Shilonge2eca692014-07-17 11:44:14 +08006863 if (start > found_key.offset)
6864 goto next;
Yan Zheng9036c102008-10-30 14:19:41 -04006865 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006866 em->orig_start = start;
Yan Zheng9036c102008-10-30 14:19:41 -04006867 em->len = found_key.offset - start;
6868 goto not_found_em;
6869 }
6870
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006871 btrfs_extent_item_to_extent_map(inode, path, item,
Nikolay Borisov9cdc5122017-02-20 13:51:02 +02006872 new_inline, em);
Filipe Manana7ffbb592014-06-09 03:48:05 +01006873
Yan Zhengd899e052008-10-30 14:25:28 -04006874 if (found_type == BTRFS_FILE_EXTENT_REG ||
6875 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006876 goto insert;
6877 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04006878 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04006879 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04006880 size_t size;
6881 size_t extent_offset;
6882 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04006883
Filipe Manana7ffbb592014-06-09 03:48:05 +01006884 if (new_inline)
Yan689f9342007-10-29 11:41:07 -04006885 goto out;
Yan689f9342007-10-29 11:41:07 -04006886
Qu Wenruoe41ca582018-06-06 15:41:49 +08006887 size = btrfs_file_extent_ram_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04006888 extent_offset = page_offset(page) + pg_offset - extent_start;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006889 copy_size = min_t(u64, PAGE_SIZE - pg_offset,
6890 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04006891 em->start = extent_start + extent_offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006892 em->len = ALIGN(copy_size, fs_info->sectorsize);
Josef Bacikb4939682012-12-03 10:31:19 -05006893 em->orig_block_len = em->len;
Josef Bacik70c8a912012-10-11 16:54:30 -04006894 em->orig_start = em->start;
Yan689f9342007-10-29 11:41:07 -04006895 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Liu Boe49aabd2018-08-25 13:47:09 +08006896
6897 btrfs_set_path_blocking(path);
Edmund Nadolskibf46f522017-11-20 13:24:49 -07006898 if (!PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08006899 if (btrfs_file_extent_compression(leaf, item) !=
6900 BTRFS_COMPRESS_NONE) {
Byongho Leee40da0e2015-05-19 23:46:45 +09006901 ret = uncompress_inline(path, page, pg_offset,
Chris Masonc8b97812008-10-29 14:49:59 -04006902 extent_offset, item);
Zach Brown166ae5a2014-05-09 17:15:10 -04006903 if (ret) {
6904 err = ret;
6905 goto out;
6906 }
Chris Masonc8b97812008-10-29 14:49:59 -04006907 } else {
6908 map = kmap(page);
6909 read_extent_buffer(leaf, map + pg_offset, ptr,
6910 copy_size);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006911 if (pg_offset + copy_size < PAGE_SIZE) {
Chris Mason93c82d52009-09-11 12:36:29 -04006912 memset(map + pg_offset + copy_size, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006913 PAGE_SIZE - pg_offset -
Chris Mason93c82d52009-09-11 12:36:29 -04006914 copy_size);
6915 }
Chris Masonc8b97812008-10-29 14:49:59 -04006916 kunmap(page);
6917 }
Chris Mason179e29e2007-11-01 11:28:41 -04006918 flush_dcache_page(page);
Chris Masona52d9a82007-08-27 16:49:44 -04006919 }
Chris Masond1310b22008-01-24 16:13:08 -05006920 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00006921 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04006922 goto insert;
Chris Masona52d9a82007-08-27 16:49:44 -04006923 }
6924not_found:
6925 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006926 em->orig_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05006927 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04006928not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04006929 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04006930insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02006931 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05006932 if (em->start > start || extent_map_end(em) <= start) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006933 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04006934 "bad extent! em: [%llu %llu] passed [%llu %llu]",
6935 em->start, em->len, start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04006936 err = -EIO;
6937 goto out;
6938 }
Chris Masond1310b22008-01-24 16:13:08 -05006939
6940 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04006941 write_lock(&em_tree->lock);
David Sterbaf46b24c2018-04-03 21:45:57 +02006942 err = btrfs_add_extent_mapping(fs_info, em_tree, &em, start, len);
Chris Mason890871b2009-09-02 16:24:52 -04006943 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04006944out:
Liu Boc6414282018-08-23 07:36:17 +08006945 btrfs_free_path(path);
liubo1abe9b82011-03-24 11:18:59 +00006946
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006947 trace_btrfs_get_extent(root, inode, em);
liubo1abe9b82011-03-24 11:18:59 +00006948
Chris Masona52d9a82007-08-27 16:49:44 -04006949 if (err) {
6950 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04006951 return ERR_PTR(err);
6952 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006953 BUG_ON(!em); /* Error is always set */
Chris Masona52d9a82007-08-27 16:49:44 -04006954 return em;
6955}
6956
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006957struct extent_map *btrfs_get_extent_fiemap(struct btrfs_inode *inode,
6958 struct page *page,
6959 size_t pg_offset, u64 start, u64 len,
6960 int create)
Chris Masonec29ed52011-02-23 16:23:20 -05006961{
6962 struct extent_map *em;
6963 struct extent_map *hole_em = NULL;
6964 u64 range_start = start;
6965 u64 end;
6966 u64 found;
6967 u64 found_end;
6968 int err = 0;
6969
6970 em = btrfs_get_extent(inode, page, pg_offset, start, len, create);
6971 if (IS_ERR(em))
6972 return em;
Dan Carpenter99862772017-04-11 11:57:15 +03006973 /*
6974 * If our em maps to:
6975 * - a hole or
6976 * - a pre-alloc extent,
6977 * there might actually be delalloc bytes behind it.
6978 */
6979 if (em->block_start != EXTENT_MAP_HOLE &&
6980 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
6981 return em;
6982 else
6983 hole_em = em;
Chris Masonec29ed52011-02-23 16:23:20 -05006984
6985 /* check to see if we've wrapped (len == -1 or similar) */
6986 end = start + len;
6987 if (end < start)
6988 end = (u64)-1;
6989 else
6990 end -= 1;
6991
6992 em = NULL;
6993
6994 /* ok, we didn't find anything, lets look for delalloc */
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006995 found = count_range_bits(&inode->io_tree, &range_start,
Chris Masonec29ed52011-02-23 16:23:20 -05006996 end, len, EXTENT_DELALLOC, 1);
6997 found_end = range_start + found;
6998 if (found_end < range_start)
6999 found_end = (u64)-1;
7000
7001 /*
7002 * we didn't find anything useful, return
7003 * the original results from get_extent()
7004 */
7005 if (range_start > end || found_end <= start) {
7006 em = hole_em;
7007 hole_em = NULL;
7008 goto out;
7009 }
7010
7011 /* adjust the range_start to make sure it doesn't
7012 * go backwards from the start they passed in
7013 */
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05307014 range_start = max(start, range_start);
Chris Masonec29ed52011-02-23 16:23:20 -05007015 found = found_end - range_start;
7016
7017 if (found > 0) {
7018 u64 hole_start = start;
7019 u64 hole_len = len;
7020
David Sterba172ddd62011-04-21 00:48:27 +02007021 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05007022 if (!em) {
7023 err = -ENOMEM;
7024 goto out;
7025 }
7026 /*
7027 * when btrfs_get_extent can't find anything it
7028 * returns one huge hole
7029 *
7030 * make sure what it found really fits our range, and
7031 * adjust to make sure it is based on the start from
7032 * the caller
7033 */
7034 if (hole_em) {
7035 u64 calc_end = extent_map_end(hole_em);
7036
7037 if (calc_end <= start || (hole_em->start > end)) {
7038 free_extent_map(hole_em);
7039 hole_em = NULL;
7040 } else {
7041 hole_start = max(hole_em->start, start);
7042 hole_len = calc_end - hole_start;
7043 }
7044 }
7045 em->bdev = NULL;
7046 if (hole_em && range_start > hole_start) {
7047 /* our hole starts before our delalloc, so we
7048 * have to return just the parts of the hole
7049 * that go until the delalloc starts
7050 */
7051 em->len = min(hole_len,
7052 range_start - hole_start);
7053 em->start = hole_start;
7054 em->orig_start = hole_start;
7055 /*
7056 * don't adjust block start at all,
7057 * it is fixed at EXTENT_MAP_HOLE
7058 */
7059 em->block_start = hole_em->block_start;
7060 em->block_len = hole_len;
Liu Bof9e4fb52013-01-07 10:10:12 +00007061 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
7062 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonec29ed52011-02-23 16:23:20 -05007063 } else {
7064 em->start = range_start;
7065 em->len = found;
7066 em->orig_start = range_start;
7067 em->block_start = EXTENT_MAP_DELALLOC;
7068 em->block_len = found;
7069 }
Nikolay Borisovbf8d32b2017-12-01 11:19:43 +02007070 } else {
Chris Masonec29ed52011-02-23 16:23:20 -05007071 return hole_em;
7072 }
7073out:
7074
7075 free_extent_map(hole_em);
7076 if (err) {
7077 free_extent_map(em);
7078 return ERR_PTR(err);
7079 }
7080 return em;
7081}
7082
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007083static struct extent_map *btrfs_create_dio_extent(struct inode *inode,
7084 const u64 start,
7085 const u64 len,
7086 const u64 orig_start,
7087 const u64 block_start,
7088 const u64 block_len,
7089 const u64 orig_block_len,
7090 const u64 ram_bytes,
7091 const int type)
7092{
7093 struct extent_map *em = NULL;
7094 int ret;
7095
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007096 if (type != BTRFS_ORDERED_NOCOW) {
Liu Bo6f9994d2017-01-31 07:50:22 -08007097 em = create_io_em(inode, start, len, orig_start,
7098 block_start, block_len, orig_block_len,
7099 ram_bytes,
7100 BTRFS_COMPRESS_NONE, /* compress_type */
7101 type);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007102 if (IS_ERR(em))
7103 goto out;
7104 }
7105 ret = btrfs_add_ordered_extent_dio(inode, start, block_start,
7106 len, block_len, type);
7107 if (ret) {
7108 if (em) {
7109 free_extent_map(em);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02007110 btrfs_drop_extent_cache(BTRFS_I(inode), start,
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007111 start + len - 1, 0);
7112 }
7113 em = ERR_PTR(ret);
7114 }
7115 out:
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007116
7117 return em;
7118}
7119
Josef Bacik4b46fce2010-05-23 11:00:55 -04007120static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
7121 u64 start, u64 len)
7122{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007123 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007124 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik70c8a912012-10-11 16:54:30 -04007125 struct extent_map *em;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007126 struct btrfs_key ins;
7127 u64 alloc_hint;
7128 int ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007129
Josef Bacik4b46fce2010-05-23 11:00:55 -04007130 alloc_hint = get_extent_allocation_hint(inode, start, len);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007131 ret = btrfs_reserve_extent(root, len, len, fs_info->sectorsize,
Jeff Mahoneyda170662016-06-15 09:22:56 -04007132 0, alloc_hint, &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007133 if (ret)
7134 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007135
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007136 em = btrfs_create_dio_extent(inode, start, ins.offset, start,
7137 ins.objectid, ins.offset, ins.offset,
Liu Bo6288d6e2017-02-21 12:12:58 -08007138 ins.offset, BTRFS_ORDERED_REGULAR);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007139 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007140 if (IS_ERR(em))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007141 btrfs_free_reserved_extent(fs_info, ins.objectid,
7142 ins.offset, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007143
Josef Bacik4b46fce2010-05-23 11:00:55 -04007144 return em;
7145}
7146
Chris Mason46bfbb52010-05-26 11:04:10 -04007147/*
7148 * returns 1 when the nocow is safe, < 1 on error, 0 if the
7149 * block must be cow'd
7150 */
Josef Bacik00361582013-08-14 14:02:47 -04007151noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
Josef Bacik7ee9e442013-06-21 16:37:03 -04007152 u64 *orig_start, u64 *orig_block_len,
7153 u64 *ram_bytes)
Chris Mason46bfbb52010-05-26 11:04:10 -04007154{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007155 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason46bfbb52010-05-26 11:04:10 -04007156 struct btrfs_path *path;
7157 int ret;
7158 struct extent_buffer *leaf;
7159 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xie7b2b7082014-02-27 13:58:05 +08007160 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason46bfbb52010-05-26 11:04:10 -04007161 struct btrfs_file_extent_item *fi;
7162 struct btrfs_key key;
7163 u64 disk_bytenr;
7164 u64 backref_offset;
7165 u64 extent_end;
7166 u64 num_bytes;
7167 int slot;
7168 int found_type;
Josef Bacik7ee9e442013-06-21 16:37:03 -04007169 bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW);
Miao Xiee77751a2013-12-27 21:11:50 +08007170
Chris Mason46bfbb52010-05-26 11:04:10 -04007171 path = btrfs_alloc_path();
7172 if (!path)
7173 return -ENOMEM;
7174
David Sterbaf85b7372017-01-20 14:54:07 +01007175 ret = btrfs_lookup_file_extent(NULL, root, path,
7176 btrfs_ino(BTRFS_I(inode)), offset, 0);
Chris Mason46bfbb52010-05-26 11:04:10 -04007177 if (ret < 0)
7178 goto out;
7179
7180 slot = path->slots[0];
7181 if (ret == 1) {
7182 if (slot == 0) {
7183 /* can't find the item, must cow */
7184 ret = 0;
7185 goto out;
7186 }
7187 slot--;
7188 }
7189 ret = 0;
7190 leaf = path->nodes[0];
7191 btrfs_item_key_to_cpu(leaf, &key, slot);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02007192 if (key.objectid != btrfs_ino(BTRFS_I(inode)) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04007193 key.type != BTRFS_EXTENT_DATA_KEY) {
7194 /* not our file or wrong item type, must cow */
7195 goto out;
7196 }
7197
7198 if (key.offset > offset) {
7199 /* Wrong offset, must cow */
7200 goto out;
7201 }
7202
7203 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
7204 found_type = btrfs_file_extent_type(leaf, fi);
7205 if (found_type != BTRFS_FILE_EXTENT_REG &&
7206 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
7207 /* not a regular extent, must cow */
7208 goto out;
7209 }
Josef Bacik7ee9e442013-06-21 16:37:03 -04007210
7211 if (!nocow && found_type == BTRFS_FILE_EXTENT_REG)
7212 goto out;
7213
Miao Xiee77751a2013-12-27 21:11:50 +08007214 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
7215 if (extent_end <= offset)
7216 goto out;
7217
Chris Mason46bfbb52010-05-26 11:04:10 -04007218 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Josef Bacik7ee9e442013-06-21 16:37:03 -04007219 if (disk_bytenr == 0)
7220 goto out;
7221
7222 if (btrfs_file_extent_compression(leaf, fi) ||
7223 btrfs_file_extent_encryption(leaf, fi) ||
7224 btrfs_file_extent_other_encoding(leaf, fi))
7225 goto out;
7226
Ethan Lien78d42952018-05-17 14:58:29 +08007227 /*
7228 * Do the same check as in btrfs_cross_ref_exist but without the
7229 * unnecessary search.
7230 */
7231 if (btrfs_file_extent_generation(leaf, fi) <=
7232 btrfs_root_last_snapshot(&root->root_item))
7233 goto out;
7234
Chris Mason46bfbb52010-05-26 11:04:10 -04007235 backref_offset = btrfs_file_extent_offset(leaf, fi);
7236
Josef Bacik7ee9e442013-06-21 16:37:03 -04007237 if (orig_start) {
7238 *orig_start = key.offset - backref_offset;
7239 *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
7240 *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
7241 }
Josef Bacikeb384b52013-04-24 16:32:55 -04007242
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007243 if (btrfs_extent_readonly(fs_info, disk_bytenr))
Chris Mason46bfbb52010-05-26 11:04:10 -04007244 goto out;
Miao Xie7b2b7082014-02-27 13:58:05 +08007245
7246 num_bytes = min(offset + *len, extent_end) - offset;
7247 if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) {
7248 u64 range_end;
7249
Jeff Mahoneyda170662016-06-15 09:22:56 -04007250 range_end = round_up(offset + num_bytes,
7251 root->fs_info->sectorsize) - 1;
Miao Xie7b2b7082014-02-27 13:58:05 +08007252 ret = test_range_bit(io_tree, offset, range_end,
7253 EXTENT_DELALLOC, 0, NULL);
7254 if (ret) {
7255 ret = -EAGAIN;
7256 goto out;
7257 }
7258 }
7259
Josef Bacik1bda19e2013-10-18 12:10:36 -04007260 btrfs_release_path(path);
Chris Mason46bfbb52010-05-26 11:04:10 -04007261
7262 /*
7263 * look for other files referencing this extent, if we
7264 * find any we must cow
7265 */
Josef Bacik00361582013-08-14 14:02:47 -04007266
Liu Boe4c3b2d2017-01-30 12:25:28 -08007267 ret = btrfs_cross_ref_exist(root, btrfs_ino(BTRFS_I(inode)),
Josef Bacik00361582013-08-14 14:02:47 -04007268 key.offset - backref_offset, disk_bytenr);
Josef Bacik00361582013-08-14 14:02:47 -04007269 if (ret) {
7270 ret = 0;
7271 goto out;
7272 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007273
7274 /*
7275 * adjust disk_bytenr and num_bytes to cover just the bytes
7276 * in this extent we are about to write. If there
7277 * are any csums in that range we have to cow in order
7278 * to keep the csums correct
7279 */
7280 disk_bytenr += backref_offset;
7281 disk_bytenr += offset - key.offset;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007282 if (csum_exist_in_range(fs_info, disk_bytenr, num_bytes))
7283 goto out;
Chris Mason46bfbb52010-05-26 11:04:10 -04007284 /*
7285 * all of the above have passed, it is safe to overwrite this extent
7286 * without cow
7287 */
Josef Bacikeb384b52013-04-24 16:32:55 -04007288 *len = num_bytes;
Chris Mason46bfbb52010-05-26 11:04:10 -04007289 ret = 1;
7290out:
7291 btrfs_free_path(path);
7292 return ret;
7293}
7294
Josef Bacikeb838e72012-07-31 16:28:48 -04007295static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
7296 struct extent_state **cached_state, int writing)
7297{
7298 struct btrfs_ordered_extent *ordered;
7299 int ret = 0;
7300
7301 while (1) {
7302 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbaff13db42015-12-03 14:30:40 +01007303 cached_state);
Josef Bacikeb838e72012-07-31 16:28:48 -04007304 /*
7305 * We're concerned with the entire range that we're going to be
Nicholas D Steeves01327612016-05-19 21:18:45 -04007306 * doing DIO to, so we need to make sure there's no ordered
Josef Bacikeb838e72012-07-31 16:28:48 -04007307 * extents in this range.
7308 */
Nikolay Borisova776c6f2017-02-20 13:50:49 +02007309 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), lockstart,
Josef Bacikeb838e72012-07-31 16:28:48 -04007310 lockend - lockstart + 1);
7311
7312 /*
7313 * We need to make sure there are no buffered pages in this
7314 * range either, we could have raced between the invalidate in
7315 * generic_file_direct_write and locking the extent. The
7316 * invalidate needs to happen so that reads after a write do not
7317 * get stale data.
7318 */
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007319 if (!ordered &&
David Sterba051c98e2018-03-07 15:33:22 +01007320 (!writing || !filemap_range_has_page(inode->i_mapping,
7321 lockstart, lockend)))
Josef Bacikeb838e72012-07-31 16:28:48 -04007322 break;
7323
7324 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbae43bbe52017-12-12 21:43:52 +01007325 cached_state);
Josef Bacikeb838e72012-07-31 16:28:48 -04007326
7327 if (ordered) {
Filipe Mananaade77022016-02-18 14:28:55 +00007328 /*
7329 * If we are doing a DIO read and the ordered extent we
7330 * found is for a buffered write, we can not wait for it
7331 * to complete and retry, because if we do so we can
7332 * deadlock with concurrent buffered writes on page
7333 * locks. This happens only if our DIO read covers more
7334 * than one extent map, if at this point has already
7335 * created an ordered extent for a previous extent map
7336 * and locked its range in the inode's io tree, and a
7337 * concurrent write against that previous extent map's
7338 * range and this range started (we unlock the ranges
7339 * in the io tree only when the bios complete and
7340 * buffered writes always lock pages before attempting
7341 * to lock range in the io tree).
7342 */
7343 if (writing ||
7344 test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags))
7345 btrfs_start_ordered_extent(inode, ordered, 1);
7346 else
7347 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007348 btrfs_put_ordered_extent(ordered);
7349 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007350 /*
Filipe Mananab850ae12015-12-08 16:23:16 +00007351 * We could trigger writeback for this range (and wait
7352 * for it to complete) and then invalidate the pages for
7353 * this range (through invalidate_inode_pages2_range()),
7354 * but that can lead us to a deadlock with a concurrent
7355 * call to readpages() (a buffered read or a defrag call
7356 * triggered a readahead) on a page lock due to an
7357 * ordered dio extent we created before but did not have
7358 * yet a corresponding bio submitted (whence it can not
7359 * complete), which makes readpages() wait for that
7360 * ordered extent to complete while holding a lock on
7361 * that page.
Josef Bacikeb838e72012-07-31 16:28:48 -04007362 */
Filipe Mananab850ae12015-12-08 16:23:16 +00007363 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007364 }
7365
Filipe Mananaade77022016-02-18 14:28:55 +00007366 if (ret)
7367 break;
7368
Josef Bacikeb838e72012-07-31 16:28:48 -04007369 cond_resched();
7370 }
7371
7372 return ret;
7373}
7374
Liu Bo6f9994d2017-01-31 07:50:22 -08007375/* The callers of this must take lock_extent() */
7376static struct extent_map *create_io_em(struct inode *inode, u64 start, u64 len,
7377 u64 orig_start, u64 block_start,
7378 u64 block_len, u64 orig_block_len,
7379 u64 ram_bytes, int compress_type,
7380 int type)
Josef Bacik69ffb542012-09-11 15:40:07 -04007381{
7382 struct extent_map_tree *em_tree;
7383 struct extent_map *em;
7384 struct btrfs_root *root = BTRFS_I(inode)->root;
7385 int ret;
7386
Liu Bo6f9994d2017-01-31 07:50:22 -08007387 ASSERT(type == BTRFS_ORDERED_PREALLOC ||
7388 type == BTRFS_ORDERED_COMPRESSED ||
7389 type == BTRFS_ORDERED_NOCOW ||
Liu Bo1af4a0a2017-02-13 15:35:09 -08007390 type == BTRFS_ORDERED_REGULAR);
Liu Bo6f9994d2017-01-31 07:50:22 -08007391
Josef Bacik69ffb542012-09-11 15:40:07 -04007392 em_tree = &BTRFS_I(inode)->extent_tree;
7393 em = alloc_extent_map();
7394 if (!em)
7395 return ERR_PTR(-ENOMEM);
7396
7397 em->start = start;
7398 em->orig_start = orig_start;
7399 em->len = len;
7400 em->block_len = block_len;
7401 em->block_start = block_start;
7402 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacikb4939682012-12-03 10:31:19 -05007403 em->orig_block_len = orig_block_len;
Josef Bacikcc95bef2013-04-04 14:31:27 -04007404 em->ram_bytes = ram_bytes;
Josef Bacik70c8a912012-10-11 16:54:30 -04007405 em->generation = -1;
Josef Bacik69ffb542012-09-11 15:40:07 -04007406 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Liu Bo1af4a0a2017-02-13 15:35:09 -08007407 if (type == BTRFS_ORDERED_PREALLOC) {
Josef Bacikb11e2342012-12-03 10:58:15 -05007408 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Liu Bo1af4a0a2017-02-13 15:35:09 -08007409 } else if (type == BTRFS_ORDERED_COMPRESSED) {
Liu Bo6f9994d2017-01-31 07:50:22 -08007410 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
7411 em->compress_type = compress_type;
7412 }
Josef Bacik69ffb542012-09-11 15:40:07 -04007413
7414 do {
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02007415 btrfs_drop_extent_cache(BTRFS_I(inode), em->start,
Josef Bacik69ffb542012-09-11 15:40:07 -04007416 em->start + em->len - 1, 0);
7417 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04007418 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik69ffb542012-09-11 15:40:07 -04007419 write_unlock(&em_tree->lock);
Liu Bo6f9994d2017-01-31 07:50:22 -08007420 /*
7421 * The caller has taken lock_extent(), who could race with us
7422 * to add em?
7423 */
Josef Bacik69ffb542012-09-11 15:40:07 -04007424 } while (ret == -EEXIST);
7425
7426 if (ret) {
7427 free_extent_map(em);
7428 return ERR_PTR(ret);
7429 }
7430
Liu Bo6f9994d2017-01-31 07:50:22 -08007431 /* em got 2 refs now, callers needs to do free_extent_map once. */
Josef Bacik69ffb542012-09-11 15:40:07 -04007432 return em;
7433}
7434
Nikolay Borisov1c8d0172018-05-02 15:19:32 +03007435
7436static int btrfs_get_blocks_direct_read(struct extent_map *em,
7437 struct buffer_head *bh_result,
7438 struct inode *inode,
7439 u64 start, u64 len)
7440{
7441 if (em->block_start == EXTENT_MAP_HOLE ||
7442 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7443 return -ENOENT;
7444
7445 len = min(len, em->len - (start - em->start));
7446
7447 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
7448 inode->i_blkbits;
7449 bh_result->b_size = len;
7450 bh_result->b_bdev = em->bdev;
7451 set_buffer_mapped(bh_result);
7452
7453 return 0;
7454}
7455
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007456static int btrfs_get_blocks_direct_write(struct extent_map **map,
7457 struct buffer_head *bh_result,
7458 struct inode *inode,
7459 struct btrfs_dio_data *dio_data,
7460 u64 start, u64 len)
7461{
7462 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
7463 struct extent_map *em = *map;
7464 int ret = 0;
7465
7466 /*
7467 * We don't allocate a new extent in the following cases
7468 *
7469 * 1) The inode is marked as NODATACOW. In this case we'll just use the
7470 * existing extent.
7471 * 2) The extent is marked as PREALLOC. We're good to go here and can
7472 * just use the extent.
7473 *
7474 */
7475 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
7476 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
7477 em->block_start != EXTENT_MAP_HOLE)) {
7478 int type;
7479 u64 block_start, orig_start, orig_block_len, ram_bytes;
7480
7481 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7482 type = BTRFS_ORDERED_PREALLOC;
7483 else
7484 type = BTRFS_ORDERED_NOCOW;
7485 len = min(len, em->len - (start - em->start));
7486 block_start = em->block_start + (start - em->start);
7487
7488 if (can_nocow_extent(inode, start, &len, &orig_start,
7489 &orig_block_len, &ram_bytes) == 1 &&
7490 btrfs_inc_nocow_writers(fs_info, block_start)) {
7491 struct extent_map *em2;
7492
7493 em2 = btrfs_create_dio_extent(inode, start, len,
7494 orig_start, block_start,
7495 len, orig_block_len,
7496 ram_bytes, type);
7497 btrfs_dec_nocow_writers(fs_info, block_start);
7498 if (type == BTRFS_ORDERED_PREALLOC) {
7499 free_extent_map(em);
7500 *map = em = em2;
7501 }
7502
7503 if (em2 && IS_ERR(em2)) {
7504 ret = PTR_ERR(em2);
7505 goto out;
7506 }
7507 /*
7508 * For inode marked NODATACOW or extent marked PREALLOC,
7509 * use the existing or preallocated extent, so does not
7510 * need to adjust btrfs_space_info's bytes_may_use.
7511 */
7512 btrfs_free_reserved_data_space_noquota(inode, start,
7513 len);
7514 goto skip_cow;
7515 }
7516 }
7517
7518 /* this will cow the extent */
7519 len = bh_result->b_size;
7520 free_extent_map(em);
7521 *map = em = btrfs_new_extent_direct(inode, start, len);
7522 if (IS_ERR(em)) {
7523 ret = PTR_ERR(em);
7524 goto out;
7525 }
7526
7527 len = min(len, em->len - (start - em->start));
7528
7529skip_cow:
7530 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
7531 inode->i_blkbits;
7532 bh_result->b_size = len;
7533 bh_result->b_bdev = em->bdev;
7534 set_buffer_mapped(bh_result);
7535
7536 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7537 set_buffer_new(bh_result);
7538
7539 /*
7540 * Need to update the i_size under the extent lock so buffered
7541 * readers will get the updated i_size when we unlock.
7542 */
7543 if (!dio_data->overwrite && start + len > i_size_read(inode))
7544 i_size_write(inode, start + len);
7545
7546 WARN_ON(dio_data->reserve < len);
7547 dio_data->reserve -= len;
7548 dio_data->unsubmitted_oe_range_end = start + len;
7549 current->journal_info = dio_data;
7550out:
7551 return ret;
7552}
7553
Josef Bacik4b46fce2010-05-23 11:00:55 -04007554static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
7555 struct buffer_head *bh_result, int create)
7556{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007557 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007558 struct extent_map *em;
Josef Bacikeb838e72012-07-31 16:28:48 -04007559 struct extent_state *cached_state = NULL;
chandan50745b02015-08-28 21:10:13 +05307560 struct btrfs_dio_data *dio_data = NULL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007561 u64 start = iblock << inode->i_blkbits;
Josef Bacikeb838e72012-07-31 16:28:48 -04007562 u64 lockstart, lockend;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007563 u64 len = bh_result->b_size;
Josef Bacikeb838e72012-07-31 16:28:48 -04007564 int unlock_bits = EXTENT_LOCKED;
Miao Xie09348562013-02-07 10:12:07 +00007565 int ret = 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007566
Miao Xie172a5042013-02-21 02:48:22 -07007567 if (create)
Josef Bacik32667892015-02-11 15:08:58 -05007568 unlock_bits |= EXTENT_DIRTY;
Miao Xie172a5042013-02-21 02:48:22 -07007569 else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007570 len = min_t(u64, len, fs_info->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04007571
Josef Bacikc3298612012-08-03 16:49:19 -04007572 lockstart = start;
7573 lockend = start + len - 1;
7574
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007575 if (current->journal_info) {
7576 /*
7577 * Need to pull our outstanding extents and set journal_info to NULL so
Nicholas D Steeves01327612016-05-19 21:18:45 -04007578 * that anything that needs to check if there's a transaction doesn't get
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007579 * confused.
7580 */
chandan50745b02015-08-28 21:10:13 +05307581 dio_data = current->journal_info;
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007582 current->journal_info = NULL;
7583 }
7584
Josef Bacikeb838e72012-07-31 16:28:48 -04007585 /*
7586 * If this errors out it's because we couldn't invalidate pagecache for
7587 * this range and we need to fallback to buffered.
7588 */
Filipe Manana9c9464c2015-11-04 09:52:04 +00007589 if (lock_extent_direct(inode, lockstart, lockend, &cached_state,
7590 create)) {
7591 ret = -ENOTBLK;
7592 goto err;
7593 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007594
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007595 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len, 0);
Josef Bacikeb838e72012-07-31 16:28:48 -04007596 if (IS_ERR(em)) {
7597 ret = PTR_ERR(em);
7598 goto unlock_err;
7599 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007600
7601 /*
7602 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
7603 * io. INLINE is special, and we could probably kludge it in here, but
7604 * it's still buffered so for safety lets just fall back to the generic
7605 * buffered path.
7606 *
7607 * For COMPRESSED we _have_ to read the entire extent in so we can
7608 * decompress it, so there will be buffering required no matter what we
7609 * do, so go ahead and fallback to buffered.
7610 *
Nicholas D Steeves01327612016-05-19 21:18:45 -04007611 * We return -ENOTBLK because that's what makes DIO go ahead and go back
Josef Bacik4b46fce2010-05-23 11:00:55 -04007612 * to buffered IO. Don't blame me, this is the price we pay for using
7613 * the generic code.
7614 */
7615 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
7616 em->block_start == EXTENT_MAP_INLINE) {
7617 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007618 ret = -ENOTBLK;
7619 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007620 }
7621
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007622 if (create) {
7623 ret = btrfs_get_blocks_direct_write(&em, bh_result, inode,
7624 dio_data, start, len);
7625 if (ret < 0)
7626 goto unlock_err;
7627
7628 /* clear and unlock the entire range */
7629 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7630 unlock_bits, 1, 0, &cached_state);
7631 } else {
Nikolay Borisov1c8d0172018-05-02 15:19:32 +03007632 ret = btrfs_get_blocks_direct_read(em, bh_result, inode,
7633 start, len);
7634 /* Can be negative only if we read from a hole */
7635 if (ret < 0) {
7636 ret = 0;
7637 free_extent_map(em);
7638 goto unlock_err;
7639 }
7640 /*
7641 * We need to unlock only the end area that we aren't using.
7642 * The rest is going to be unlocked by the endio routine.
7643 */
7644 lockstart = start + bh_result->b_size;
7645 if (lockstart < lockend) {
7646 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
7647 lockend, unlock_bits, 1, 0,
7648 &cached_state);
7649 } else {
7650 free_extent_state(cached_state);
7651 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007652 }
7653
Josef Bacik4b46fce2010-05-23 11:00:55 -04007654 free_extent_map(em);
7655
7656 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007657
7658unlock_err:
Josef Bacikeb838e72012-07-31 16:28:48 -04007659 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbaae0f1622017-10-31 16:37:52 +01007660 unlock_bits, 1, 0, &cached_state);
Filipe Manana9c9464c2015-11-04 09:52:04 +00007661err:
chandan50745b02015-08-28 21:10:13 +05307662 if (dio_data)
7663 current->journal_info = dio_data;
Josef Bacikeb838e72012-07-31 16:28:48 -04007664 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007665}
7666
Omar Sandoval58efbc92017-08-22 23:45:59 -07007667static inline blk_status_t submit_dio_repair_bio(struct inode *inode,
7668 struct bio *bio,
7669 int mirror_num)
Miao Xie8b110e32014-09-12 18:44:03 +08007670{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007671 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Omar Sandoval58efbc92017-08-22 23:45:59 -07007672 blk_status_t ret;
Miao Xie8b110e32014-09-12 18:44:03 +08007673
Mike Christie37226b22016-06-05 14:31:52 -05007674 BUG_ON(bio_op(bio) == REQ_OP_WRITE);
Miao Xie8b110e32014-09-12 18:44:03 +08007675
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007676 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DIO_REPAIR);
Miao Xie8b110e32014-09-12 18:44:03 +08007677 if (ret)
Nikolay Borisovea057f62017-12-13 10:25:38 +02007678 return ret;
Miao Xie8b110e32014-09-12 18:44:03 +08007679
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007680 ret = btrfs_map_bio(fs_info, bio, mirror_num, 0);
Nikolay Borisovea057f62017-12-13 10:25:38 +02007681
Miao Xie8b110e32014-09-12 18:44:03 +08007682 return ret;
7683}
7684
7685static int btrfs_check_dio_repairable(struct inode *inode,
7686 struct bio *failed_bio,
7687 struct io_failure_record *failrec,
7688 int failed_mirror)
7689{
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007690 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xie8b110e32014-09-12 18:44:03 +08007691 int num_copies;
7692
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007693 num_copies = btrfs_num_copies(fs_info, failrec->logical, failrec->len);
Miao Xie8b110e32014-09-12 18:44:03 +08007694 if (num_copies == 1) {
7695 /*
7696 * we only have a single copy of the data, so don't bother with
7697 * all the retry and error correction code that follows. no
7698 * matter what the error is, it is very likely to persist.
7699 */
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007700 btrfs_debug(fs_info,
7701 "Check DIO Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d",
7702 num_copies, failrec->this_mirror, failed_mirror);
Miao Xie8b110e32014-09-12 18:44:03 +08007703 return 0;
7704 }
7705
7706 failrec->failed_mirror = failed_mirror;
7707 failrec->this_mirror++;
7708 if (failrec->this_mirror == failed_mirror)
7709 failrec->this_mirror++;
7710
7711 if (failrec->this_mirror > num_copies) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007712 btrfs_debug(fs_info,
7713 "Check DIO Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d",
7714 num_copies, failrec->this_mirror, failed_mirror);
Miao Xie8b110e32014-09-12 18:44:03 +08007715 return 0;
7716 }
7717
7718 return 1;
7719}
7720
Omar Sandoval58efbc92017-08-22 23:45:59 -07007721static blk_status_t dio_read_error(struct inode *inode, struct bio *failed_bio,
7722 struct page *page, unsigned int pgoff,
7723 u64 start, u64 end, int failed_mirror,
7724 bio_end_io_t *repair_endio, void *repair_arg)
Miao Xie8b110e32014-09-12 18:44:03 +08007725{
7726 struct io_failure_record *failrec;
Josef Bacik7870d082017-05-05 11:57:15 -04007727 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
7728 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
Miao Xie8b110e32014-09-12 18:44:03 +08007729 struct bio *bio;
7730 int isector;
David Sterbaf1c77c52017-06-06 19:03:49 +02007731 unsigned int read_mode = 0;
Liu Bo17347ce2017-05-15 15:33:27 -07007732 int segs;
Miao Xie8b110e32014-09-12 18:44:03 +08007733 int ret;
Omar Sandoval58efbc92017-08-22 23:45:59 -07007734 blk_status_t status;
Ming Leic16a8ac2017-12-18 20:22:12 +08007735 struct bio_vec bvec;
Miao Xie8b110e32014-09-12 18:44:03 +08007736
Mike Christie37226b22016-06-05 14:31:52 -05007737 BUG_ON(bio_op(failed_bio) == REQ_OP_WRITE);
Miao Xie8b110e32014-09-12 18:44:03 +08007738
7739 ret = btrfs_get_io_failure_record(inode, start, end, &failrec);
7740 if (ret)
Omar Sandoval58efbc92017-08-22 23:45:59 -07007741 return errno_to_blk_status(ret);
Miao Xie8b110e32014-09-12 18:44:03 +08007742
7743 ret = btrfs_check_dio_repairable(inode, failed_bio, failrec,
7744 failed_mirror);
7745 if (!ret) {
Josef Bacik7870d082017-05-05 11:57:15 -04007746 free_io_failure(failure_tree, io_tree, failrec);
Omar Sandoval58efbc92017-08-22 23:45:59 -07007747 return BLK_STS_IOERR;
Miao Xie8b110e32014-09-12 18:44:03 +08007748 }
7749
Liu Bo17347ce2017-05-15 15:33:27 -07007750 segs = bio_segments(failed_bio);
Ming Leic16a8ac2017-12-18 20:22:12 +08007751 bio_get_first_bvec(failed_bio, &bvec);
Liu Bo17347ce2017-05-15 15:33:27 -07007752 if (segs > 1 ||
Ming Leic16a8ac2017-12-18 20:22:12 +08007753 (bvec.bv_len > btrfs_inode_sectorsize(inode)))
Christoph Hellwig70fd7612016-11-01 07:40:10 -06007754 read_mode |= REQ_FAILFAST_DEV;
Miao Xie8b110e32014-09-12 18:44:03 +08007755
7756 isector = start - btrfs_io_bio(failed_bio)->logical;
7757 isector >>= inode->i_sb->s_blocksize_bits;
7758 bio = btrfs_create_repair_bio(inode, failed_bio, failrec, page,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307759 pgoff, isector, repair_endio, repair_arg);
David Sterbaebcc3262018-06-29 10:56:53 +02007760 bio->bi_opf = REQ_OP_READ | read_mode;
Miao Xie8b110e32014-09-12 18:44:03 +08007761
7762 btrfs_debug(BTRFS_I(inode)->root->fs_info,
David Sterba913e1532017-07-13 15:32:18 +02007763 "repair DIO read error: submitting new dio read[%#x] to this_mirror=%d, in_validation=%d",
Miao Xie8b110e32014-09-12 18:44:03 +08007764 read_mode, failrec->this_mirror, failrec->in_validation);
7765
Omar Sandoval58efbc92017-08-22 23:45:59 -07007766 status = submit_dio_repair_bio(inode, bio, failrec->this_mirror);
7767 if (status) {
Josef Bacik7870d082017-05-05 11:57:15 -04007768 free_io_failure(failure_tree, io_tree, failrec);
Miao Xie8b110e32014-09-12 18:44:03 +08007769 bio_put(bio);
7770 }
7771
Omar Sandoval58efbc92017-08-22 23:45:59 -07007772 return status;
Miao Xie8b110e32014-09-12 18:44:03 +08007773}
7774
7775struct btrfs_retry_complete {
7776 struct completion done;
7777 struct inode *inode;
7778 u64 start;
7779 int uptodate;
7780};
7781
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007782static void btrfs_retry_endio_nocsum(struct bio *bio)
Miao Xie8b110e32014-09-12 18:44:03 +08007783{
7784 struct btrfs_retry_complete *done = bio->bi_private;
Josef Bacik7870d082017-05-05 11:57:15 -04007785 struct inode *inode = done->inode;
Miao Xie8b110e32014-09-12 18:44:03 +08007786 struct bio_vec *bvec;
Josef Bacik7870d082017-05-05 11:57:15 -04007787 struct extent_io_tree *io_tree, *failure_tree;
Miao Xie8b110e32014-09-12 18:44:03 +08007788 int i;
7789
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02007790 if (bio->bi_status)
Miao Xie8b110e32014-09-12 18:44:03 +08007791 goto end;
7792
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307793 ASSERT(bio->bi_vcnt == 1);
Josef Bacik7870d082017-05-05 11:57:15 -04007794 io_tree = &BTRFS_I(inode)->io_tree;
7795 failure_tree = &BTRFS_I(inode)->io_failure_tree;
Ming Lei263663c2017-12-18 20:22:04 +08007796 ASSERT(bio_first_bvec_all(bio)->bv_len == btrfs_inode_sectorsize(inode));
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307797
Miao Xie8b110e32014-09-12 18:44:03 +08007798 done->uptodate = 1;
David Sterbac09abff2017-07-13 18:10:07 +02007799 ASSERT(!bio_flagged(bio, BIO_CLONED));
Miao Xie8b110e32014-09-12 18:44:03 +08007800 bio_for_each_segment_all(bvec, bio, i)
Josef Bacik7870d082017-05-05 11:57:15 -04007801 clean_io_failure(BTRFS_I(inode)->root->fs_info, failure_tree,
7802 io_tree, done->start, bvec->bv_page,
7803 btrfs_ino(BTRFS_I(inode)), 0);
Miao Xie8b110e32014-09-12 18:44:03 +08007804end:
7805 complete(&done->done);
7806 bio_put(bio);
7807}
7808
Omar Sandoval58efbc92017-08-22 23:45:59 -07007809static blk_status_t __btrfs_correct_data_nocsum(struct inode *inode,
7810 struct btrfs_io_bio *io_bio)
Josef Bacik4b46fce2010-05-23 11:00:55 -04007811{
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307812 struct btrfs_fs_info *fs_info;
Liu Bo17347ce2017-05-15 15:33:27 -07007813 struct bio_vec bvec;
7814 struct bvec_iter iter;
Miao Xie8b110e32014-09-12 18:44:03 +08007815 struct btrfs_retry_complete done;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007816 u64 start;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307817 unsigned int pgoff;
7818 u32 sectorsize;
7819 int nr_sectors;
Omar Sandoval58efbc92017-08-22 23:45:59 -07007820 blk_status_t ret;
7821 blk_status_t err = BLK_STS_OK;
Miao Xiedc380ae2014-09-12 18:43:55 +08007822
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307823 fs_info = BTRFS_I(inode)->root->fs_info;
Jeff Mahoneyda170662016-06-15 09:22:56 -04007824 sectorsize = fs_info->sectorsize;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307825
Miao Xiec1dc0892014-09-12 18:43:56 +08007826 start = io_bio->logical;
Miao Xie8b110e32014-09-12 18:44:03 +08007827 done.inode = inode;
Liu Bo17347ce2017-05-15 15:33:27 -07007828 io_bio->bio.bi_iter = io_bio->iter;
Miao Xie8b110e32014-09-12 18:44:03 +08007829
Liu Bo17347ce2017-05-15 15:33:27 -07007830 bio_for_each_segment(bvec, &io_bio->bio, iter) {
7831 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec.bv_len);
7832 pgoff = bvec.bv_offset;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307833
7834next_block_or_try_again:
Miao Xie8b110e32014-09-12 18:44:03 +08007835 done.uptodate = 0;
7836 done.start = start;
7837 init_completion(&done.done);
7838
Liu Bo17347ce2017-05-15 15:33:27 -07007839 ret = dio_read_error(inode, &io_bio->bio, bvec.bv_page,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307840 pgoff, start, start + sectorsize - 1,
7841 io_bio->mirror_num,
7842 btrfs_retry_endio_nocsum, &done);
Liu Bo629ebf42017-05-15 17:20:07 -07007843 if (ret) {
7844 err = ret;
7845 goto next;
7846 }
Miao Xie8b110e32014-09-12 18:44:03 +08007847
David Sterba9c17f6c2017-07-19 19:26:45 +02007848 wait_for_completion_io(&done.done);
Miao Xie8b110e32014-09-12 18:44:03 +08007849
7850 if (!done.uptodate) {
7851 /* We might have another mirror, so try again */
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307852 goto next_block_or_try_again;
Miao Xie8b110e32014-09-12 18:44:03 +08007853 }
7854
Liu Bo629ebf42017-05-15 17:20:07 -07007855next:
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307856 start += sectorsize;
7857
Liu Bo97bf5a52017-04-07 13:11:10 -07007858 nr_sectors--;
7859 if (nr_sectors) {
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307860 pgoff += sectorsize;
Liu Bo97bf5a52017-04-07 13:11:10 -07007861 ASSERT(pgoff < PAGE_SIZE);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307862 goto next_block_or_try_again;
7863 }
Miao Xie8b110e32014-09-12 18:44:03 +08007864 }
7865
Liu Bo629ebf42017-05-15 17:20:07 -07007866 return err;
Miao Xie8b110e32014-09-12 18:44:03 +08007867}
7868
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007869static void btrfs_retry_endio(struct bio *bio)
Miao Xie8b110e32014-09-12 18:44:03 +08007870{
7871 struct btrfs_retry_complete *done = bio->bi_private;
7872 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
Josef Bacik7870d082017-05-05 11:57:15 -04007873 struct extent_io_tree *io_tree, *failure_tree;
7874 struct inode *inode = done->inode;
Miao Xie8b110e32014-09-12 18:44:03 +08007875 struct bio_vec *bvec;
7876 int uptodate;
7877 int ret;
7878 int i;
7879
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02007880 if (bio->bi_status)
Miao Xie8b110e32014-09-12 18:44:03 +08007881 goto end;
7882
7883 uptodate = 1;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307884
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307885 ASSERT(bio->bi_vcnt == 1);
Ming Lei263663c2017-12-18 20:22:04 +08007886 ASSERT(bio_first_bvec_all(bio)->bv_len == btrfs_inode_sectorsize(done->inode));
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307887
Josef Bacik7870d082017-05-05 11:57:15 -04007888 io_tree = &BTRFS_I(inode)->io_tree;
7889 failure_tree = &BTRFS_I(inode)->io_failure_tree;
7890
David Sterbac09abff2017-07-13 18:10:07 +02007891 ASSERT(!bio_flagged(bio, BIO_CLONED));
Miao Xie8b110e32014-09-12 18:44:03 +08007892 bio_for_each_segment_all(bvec, bio, i) {
Josef Bacik7870d082017-05-05 11:57:15 -04007893 ret = __readpage_endio_check(inode, io_bio, i, bvec->bv_page,
7894 bvec->bv_offset, done->start,
7895 bvec->bv_len);
Miao Xie8b110e32014-09-12 18:44:03 +08007896 if (!ret)
Josef Bacik7870d082017-05-05 11:57:15 -04007897 clean_io_failure(BTRFS_I(inode)->root->fs_info,
7898 failure_tree, io_tree, done->start,
7899 bvec->bv_page,
7900 btrfs_ino(BTRFS_I(inode)),
7901 bvec->bv_offset);
Miao Xie8b110e32014-09-12 18:44:03 +08007902 else
7903 uptodate = 0;
7904 }
7905
7906 done->uptodate = uptodate;
7907end:
7908 complete(&done->done);
7909 bio_put(bio);
7910}
7911
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02007912static blk_status_t __btrfs_subio_endio_read(struct inode *inode,
7913 struct btrfs_io_bio *io_bio, blk_status_t err)
Miao Xie8b110e32014-09-12 18:44:03 +08007914{
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307915 struct btrfs_fs_info *fs_info;
Liu Bo17347ce2017-05-15 15:33:27 -07007916 struct bio_vec bvec;
7917 struct bvec_iter iter;
Miao Xie8b110e32014-09-12 18:44:03 +08007918 struct btrfs_retry_complete done;
7919 u64 start;
7920 u64 offset = 0;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307921 u32 sectorsize;
7922 int nr_sectors;
7923 unsigned int pgoff;
7924 int csum_pos;
Liu Boef7cdac12017-04-13 18:11:48 -07007925 bool uptodate = (err == 0);
Miao Xie8b110e32014-09-12 18:44:03 +08007926 int ret;
Omar Sandoval58efbc92017-08-22 23:45:59 -07007927 blk_status_t status;
Miao Xie8b110e32014-09-12 18:44:03 +08007928
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307929 fs_info = BTRFS_I(inode)->root->fs_info;
Jeff Mahoneyda170662016-06-15 09:22:56 -04007930 sectorsize = fs_info->sectorsize;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307931
Omar Sandoval58efbc92017-08-22 23:45:59 -07007932 err = BLK_STS_OK;
Miao Xie8b110e32014-09-12 18:44:03 +08007933 start = io_bio->logical;
7934 done.inode = inode;
Liu Bo17347ce2017-05-15 15:33:27 -07007935 io_bio->bio.bi_iter = io_bio->iter;
Miao Xie8b110e32014-09-12 18:44:03 +08007936
Liu Bo17347ce2017-05-15 15:33:27 -07007937 bio_for_each_segment(bvec, &io_bio->bio, iter) {
7938 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec.bv_len);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307939
Liu Bo17347ce2017-05-15 15:33:27 -07007940 pgoff = bvec.bv_offset;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307941next_block:
Liu Boef7cdac12017-04-13 18:11:48 -07007942 if (uptodate) {
7943 csum_pos = BTRFS_BYTES_TO_BLKS(fs_info, offset);
7944 ret = __readpage_endio_check(inode, io_bio, csum_pos,
7945 bvec.bv_page, pgoff, start, sectorsize);
7946 if (likely(!ret))
7947 goto next;
7948 }
Miao Xie8b110e32014-09-12 18:44:03 +08007949try_again:
7950 done.uptodate = 0;
7951 done.start = start;
7952 init_completion(&done.done);
7953
Omar Sandoval58efbc92017-08-22 23:45:59 -07007954 status = dio_read_error(inode, &io_bio->bio, bvec.bv_page,
7955 pgoff, start, start + sectorsize - 1,
7956 io_bio->mirror_num, btrfs_retry_endio,
7957 &done);
7958 if (status) {
7959 err = status;
Miao Xie8b110e32014-09-12 18:44:03 +08007960 goto next;
7961 }
7962
David Sterba9c17f6c2017-07-19 19:26:45 +02007963 wait_for_completion_io(&done.done);
Miao Xie8b110e32014-09-12 18:44:03 +08007964
7965 if (!done.uptodate) {
7966 /* We might have another mirror, so try again */
7967 goto try_again;
7968 }
7969next:
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307970 offset += sectorsize;
7971 start += sectorsize;
7972
7973 ASSERT(nr_sectors);
7974
Liu Bo97bf5a52017-04-07 13:11:10 -07007975 nr_sectors--;
7976 if (nr_sectors) {
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307977 pgoff += sectorsize;
Liu Bo97bf5a52017-04-07 13:11:10 -07007978 ASSERT(pgoff < PAGE_SIZE);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307979 goto next_block;
7980 }
Kent Overstreet2c30c712013-11-07 12:20:26 -08007981 }
Miao Xiec1dc0892014-09-12 18:43:56 +08007982
7983 return err;
7984}
7985
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02007986static blk_status_t btrfs_subio_endio_read(struct inode *inode,
7987 struct btrfs_io_bio *io_bio, blk_status_t err)
Miao Xie8b110e32014-09-12 18:44:03 +08007988{
7989 bool skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
7990
7991 if (skip_csum) {
7992 if (unlikely(err))
7993 return __btrfs_correct_data_nocsum(inode, io_bio);
7994 else
Omar Sandoval58efbc92017-08-22 23:45:59 -07007995 return BLK_STS_OK;
Miao Xie8b110e32014-09-12 18:44:03 +08007996 } else {
7997 return __btrfs_subio_endio_read(inode, io_bio, err);
7998 }
7999}
8000
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008001static void btrfs_endio_direct_read(struct bio *bio)
Miao Xiec1dc0892014-09-12 18:43:56 +08008002{
8003 struct btrfs_dio_private *dip = bio->bi_private;
8004 struct inode *inode = dip->inode;
8005 struct bio *dio_bio;
8006 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008007 blk_status_t err = bio->bi_status;
Miao Xiec1dc0892014-09-12 18:43:56 +08008008
Liu Bo99c4e3b92017-09-15 15:06:51 -06008009 if (dip->flags & BTRFS_DIO_ORIG_BIO_SUBMITTED)
Miao Xie8b110e32014-09-12 18:44:03 +08008010 err = btrfs_subio_endio_read(inode, io_bio, err);
Miao Xiec1dc0892014-09-12 18:43:56 +08008011
Josef Bacik4b46fce2010-05-23 11:00:55 -04008012 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01008013 dip->logical_offset + dip->bytes - 1);
Chris Mason9be33952013-05-17 18:30:14 -04008014 dio_bio = dip->dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008015
Josef Bacik4b46fce2010-05-23 11:00:55 -04008016 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04008017
Liu Bo99c4e3b92017-09-15 15:06:51 -06008018 dio_bio->bi_status = err;
Christoph Hellwig40553512017-06-03 09:37:58 +02008019 dio_end_io(dio_bio);
Miao Xie23ea8e52014-09-12 18:43:54 +08008020
8021 if (io_bio->end_io)
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008022 io_bio->end_io(io_bio, blk_status_to_errno(err));
Chris Mason9be33952013-05-17 18:30:14 -04008023 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008024}
8025
Qu Wenruo524272602017-03-08 10:25:52 +08008026static void __endio_write_update_ordered(struct inode *inode,
8027 const u64 offset, const u64 bytes,
8028 const bool uptodate)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008029{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008030 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008031 struct btrfs_ordered_extent *ordered = NULL;
Qu Wenruo524272602017-03-08 10:25:52 +08008032 struct btrfs_workqueue *wq;
8033 btrfs_work_func_t func;
Filipe Manana14543772015-11-24 16:23:54 +00008034 u64 ordered_offset = offset;
8035 u64 ordered_bytes = bytes;
Naohiro Aota67c003f2017-09-01 17:59:07 +09008036 u64 last_offset;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008037
Qu Wenruo524272602017-03-08 10:25:52 +08008038 if (btrfs_is_free_space_inode(BTRFS_I(inode))) {
8039 wq = fs_info->endio_freespace_worker;
8040 func = btrfs_freespace_write_helper;
8041 } else {
8042 wq = fs_info->endio_write_workers;
8043 func = btrfs_endio_write_helper;
8044 }
8045
Nikolay Borisovb25f0d02018-04-11 11:21:17 +03008046 while (ordered_offset < offset + bytes) {
8047 last_offset = ordered_offset;
8048 if (btrfs_dec_test_first_ordered_pending(inode, &ordered,
8049 &ordered_offset,
8050 ordered_bytes,
8051 uptodate)) {
8052 btrfs_init_work(&ordered->work, func,
8053 finish_ordered_fn,
8054 NULL, NULL);
8055 btrfs_queue_work(wq, &ordered->work);
8056 }
8057 /*
8058 * If btrfs_dec_test_ordered_pending does not find any ordered
8059 * extent in the range, we can exit.
8060 */
8061 if (ordered_offset == last_offset)
8062 return;
8063 /*
8064 * Our bio might span multiple ordered extents. In this case
8065 * we keep goin until we have accounted the whole dio.
8066 */
8067 if (ordered_offset < offset + bytes) {
8068 ordered_bytes = offset + bytes - ordered_offset;
8069 ordered = NULL;
8070 }
Chris Mason163cf092010-11-28 19:56:33 -05008071 }
Filipe Manana14543772015-11-24 16:23:54 +00008072}
8073
8074static void btrfs_endio_direct_write(struct bio *bio)
8075{
8076 struct btrfs_dio_private *dip = bio->bi_private;
8077 struct bio *dio_bio = dip->dio_bio;
8078
Qu Wenruo524272602017-03-08 10:25:52 +08008079 __endio_write_update_ordered(dip->inode, dip->logical_offset,
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008080 dip->bytes, !bio->bi_status);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008081
Josef Bacik4b46fce2010-05-23 11:00:55 -04008082 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04008083
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008084 dio_bio->bi_status = bio->bi_status;
Christoph Hellwig40553512017-06-03 09:37:58 +02008085 dio_end_io(dio_bio);
Chris Mason9be33952013-05-17 18:30:14 -04008086 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008087}
8088
David Sterbad0ee3932018-03-08 14:35:48 +01008089static blk_status_t btrfs_submit_bio_start_direct_io(void *private_data,
David Sterbad0779292018-03-08 13:47:33 +01008090 struct bio *bio, u64 offset)
Chris Masoneaf25d92010-05-25 09:48:28 -04008091{
Josef Bacikc6100a42017-05-05 11:57:13 -04008092 struct inode *inode = private_data;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008093 blk_status_t ret;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008094 ret = btrfs_csum_one_bio(inode, bio, offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008095 BUG_ON(ret); /* -ENOMEM */
Chris Masoneaf25d92010-05-25 09:48:28 -04008096 return 0;
8097}
8098
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008099static void btrfs_end_dio_bio(struct bio *bio)
Miao Xiee65e1532010-11-22 03:04:43 +00008100{
8101 struct btrfs_dio_private *dip = bio->bi_private;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008102 blk_status_t err = bio->bi_status;
Miao Xiee65e1532010-11-22 03:04:43 +00008103
Miao Xie8b110e32014-09-12 18:44:03 +08008104 if (err)
8105 btrfs_warn(BTRFS_I(dip->inode)->root->fs_info,
Mike Christie6296b962016-06-05 14:32:21 -05008106 "direct IO failed ino %llu rw %d,%u sector %#Lx len %u err no %d",
David Sterbaf85b7372017-01-20 14:54:07 +01008107 btrfs_ino(BTRFS_I(dip->inode)), bio_op(bio),
8108 bio->bi_opf,
Miao Xie8b110e32014-09-12 18:44:03 +08008109 (unsigned long long)bio->bi_iter.bi_sector,
8110 bio->bi_iter.bi_size, err);
8111
8112 if (dip->subio_endio)
8113 err = dip->subio_endio(dip->inode, btrfs_io_bio(bio), err);
Miao Xiec1dc0892014-09-12 18:43:56 +08008114
8115 if (err) {
Miao Xiee65e1532010-11-22 03:04:43 +00008116 /*
Nikolay Borisovde224b72018-02-14 10:53:36 +02008117 * We want to perceive the errors flag being set before
8118 * decrementing the reference count. We don't need a barrier
8119 * since atomic operations with a return value are fully
8120 * ordered as per atomic_t.txt
Miao Xiee65e1532010-11-22 03:04:43 +00008121 */
Nikolay Borisovde224b72018-02-14 10:53:36 +02008122 dip->errors = 1;
Miao Xiee65e1532010-11-22 03:04:43 +00008123 }
8124
8125 /* if there are more bios still pending for this dio, just exit */
8126 if (!atomic_dec_and_test(&dip->pending_bios))
8127 goto out;
8128
Chris Mason9be33952013-05-17 18:30:14 -04008129 if (dip->errors) {
Miao Xiee65e1532010-11-22 03:04:43 +00008130 bio_io_error(dip->orig_bio);
Chris Mason9be33952013-05-17 18:30:14 -04008131 } else {
Anand Jain2dbe0c72017-10-14 08:35:56 +08008132 dip->dio_bio->bi_status = BLK_STS_OK;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008133 bio_endio(dip->orig_bio);
Miao Xiee65e1532010-11-22 03:04:43 +00008134 }
8135out:
8136 bio_put(bio);
8137}
8138
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008139static inline blk_status_t btrfs_lookup_and_bind_dio_csum(struct inode *inode,
Miao Xiec1dc0892014-09-12 18:43:56 +08008140 struct btrfs_dio_private *dip,
8141 struct bio *bio,
8142 u64 file_offset)
8143{
8144 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
8145 struct btrfs_io_bio *orig_io_bio = btrfs_io_bio(dip->orig_bio);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008146 blk_status_t ret;
Miao Xiec1dc0892014-09-12 18:43:56 +08008147
8148 /*
8149 * We load all the csum data we need when we submit
8150 * the first bio to reduce the csum tree search and
8151 * contention.
8152 */
8153 if (dip->logical_offset == file_offset) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008154 ret = btrfs_lookup_bio_sums_dio(inode, dip->orig_bio,
Miao Xiec1dc0892014-09-12 18:43:56 +08008155 file_offset);
8156 if (ret)
8157 return ret;
8158 }
8159
8160 if (bio == dip->orig_bio)
8161 return 0;
8162
8163 file_offset -= dip->logical_offset;
8164 file_offset >>= inode->i_sb->s_blocksize_bits;
8165 io_bio->csum = (u8 *)(((u32 *)orig_io_bio->csum) + file_offset);
8166
8167 return 0;
8168}
8169
David Sterbad0ee3932018-03-08 14:35:48 +01008170static inline blk_status_t btrfs_submit_dio_bio(struct bio *bio,
8171 struct inode *inode, u64 file_offset, int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00008172{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008173 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xiefacc8a222013-07-25 19:22:34 +08008174 struct btrfs_dio_private *dip = bio->bi_private;
Mike Christie37226b22016-06-05 14:31:52 -05008175 bool write = bio_op(bio) == REQ_OP_WRITE;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008176 blk_status_t ret;
Miao Xiee65e1532010-11-22 03:04:43 +00008177
Liu Bo4c274bc2017-11-01 17:19:27 -06008178 /* Check btrfs_submit_bio_hook() for rules about async submit. */
Josef Bacikb812ce22012-11-16 13:56:32 -05008179 if (async_submit)
8180 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
8181
Josef Bacik5fd02042012-05-02 14:00:54 -04008182 if (!write) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008183 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DATA);
Josef Bacik5fd02042012-05-02 14:00:54 -04008184 if (ret)
8185 goto err;
8186 }
Miao Xiee65e1532010-11-22 03:04:43 +00008187
Nikolay Borisove6961ca2017-08-03 15:44:58 +03008188 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Josef Bacik1ae39932011-04-06 14:41:34 -04008189 goto map;
8190
8191 if (write && async_submit) {
Josef Bacikc6100a42017-05-05 11:57:13 -04008192 ret = btrfs_wq_submit_bio(fs_info, bio, 0, 0,
8193 file_offset, inode,
David Sterbae288c082018-07-18 17:36:24 +02008194 btrfs_submit_bio_start_direct_io);
Miao Xiee65e1532010-11-22 03:04:43 +00008195 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04008196 } else if (write) {
8197 /*
8198 * If we aren't doing async submit, calculate the csum of the
8199 * bio now.
8200 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008201 ret = btrfs_csum_one_bio(inode, bio, file_offset, 1);
Josef Bacik1ae39932011-04-06 14:41:34 -04008202 if (ret)
8203 goto err;
Miao Xie23ea8e52014-09-12 18:43:54 +08008204 } else {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008205 ret = btrfs_lookup_and_bind_dio_csum(inode, dip, bio,
Miao Xiec1dc0892014-09-12 18:43:56 +08008206 file_offset);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00008207 if (ret)
8208 goto err;
8209 }
Josef Bacik1ae39932011-04-06 14:41:34 -04008210map:
Liu Bo9b4a9b22017-08-18 11:54:02 -06008211 ret = btrfs_map_bio(fs_info, bio, 0, 0);
Miao Xiee65e1532010-11-22 03:04:43 +00008212err:
Miao Xiee65e1532010-11-22 03:04:43 +00008213 return ret;
8214}
8215
Nikolay Borisove6961ca2017-08-03 15:44:58 +03008216static int btrfs_submit_direct_hook(struct btrfs_dio_private *dip)
Miao Xiee65e1532010-11-22 03:04:43 +00008217{
8218 struct inode *inode = dip->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008219 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xiee65e1532010-11-22 03:04:43 +00008220 struct bio *bio;
8221 struct bio *orig_bio = dip->orig_bio;
Kent Overstreet4f024f32013-10-11 15:44:27 -07008222 u64 start_sector = orig_bio->bi_iter.bi_sector;
Miao Xiee65e1532010-11-22 03:04:43 +00008223 u64 file_offset = dip->logical_offset;
Miao Xiee65e1532010-11-22 03:04:43 +00008224 u64 map_length;
Josef Bacik1ae39932011-04-06 14:41:34 -04008225 int async_submit = 0;
Liu Bo725130b2017-05-16 09:51:39 -07008226 u64 submit_len;
8227 int clone_offset = 0;
8228 int clone_len;
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05308229 int ret;
Omar Sandoval58efbc92017-08-22 23:45:59 -07008230 blk_status_t status;
Miao Xiee65e1532010-11-22 03:04:43 +00008231
Kent Overstreet4f024f32013-10-11 15:44:27 -07008232 map_length = orig_bio->bi_iter.bi_size;
Liu Bo725130b2017-05-16 09:51:39 -07008233 submit_len = map_length;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008234 ret = btrfs_map_block(fs_info, btrfs_op(orig_bio), start_sector << 9,
8235 &map_length, NULL, 0);
Miao Xie7a5c3c92014-06-17 18:58:59 +08008236 if (ret)
Miao Xiee65e1532010-11-22 03:04:43 +00008237 return -EIO;
Miao Xiefacc8a222013-07-25 19:22:34 +08008238
Liu Bo725130b2017-05-16 09:51:39 -07008239 if (map_length >= submit_len) {
Josef Bacik02f57c72011-04-06 14:25:44 -04008240 bio = orig_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08008241 dip->flags |= BTRFS_DIO_ORIG_BIO_SUBMITTED;
Josef Bacik02f57c72011-04-06 14:25:44 -04008242 goto submit;
8243 }
8244
David Woodhouse53b381b2013-01-29 18:40:14 -05008245 /* async crcs make it difficult to collect full stripe writes. */
Jeff Mahoney1b868262017-05-17 11:38:35 -04008246 if (btrfs_data_alloc_profile(fs_info) & BTRFS_BLOCK_GROUP_RAID56_MASK)
David Woodhouse53b381b2013-01-29 18:40:14 -05008247 async_submit = 0;
8248 else
8249 async_submit = 1;
8250
Liu Bo725130b2017-05-16 09:51:39 -07008251 /* bio split */
8252 ASSERT(map_length <= INT_MAX);
Josef Bacik02f57c72011-04-06 14:25:44 -04008253 atomic_inc(&dip->pending_bios);
Arnd Bergmann3c91ee62017-05-18 15:33:29 +02008254 do {
Liu Bo725130b2017-05-16 09:51:39 -07008255 clone_len = min_t(int, submit_len, map_length);
Josef Bacik02f57c72011-04-06 14:25:44 -04008256
Liu Bo725130b2017-05-16 09:51:39 -07008257 /*
8258 * This will never fail as it's passing GPF_NOFS and
8259 * the allocation is backed by btrfs_bioset.
8260 */
Liu Boe4770942017-05-16 10:57:14 -07008261 bio = btrfs_bio_clone_partial(orig_bio, clone_offset,
Liu Bo725130b2017-05-16 09:51:39 -07008262 clone_len);
8263 bio->bi_private = dip;
8264 bio->bi_end_io = btrfs_end_dio_bio;
8265 btrfs_io_bio(bio)->logical = file_offset;
Miao Xiee65e1532010-11-22 03:04:43 +00008266
Liu Bo725130b2017-05-16 09:51:39 -07008267 ASSERT(submit_len >= clone_len);
8268 submit_len -= clone_len;
8269 if (submit_len == 0)
8270 break;
Miao Xiee65e1532010-11-22 03:04:43 +00008271
Liu Bo725130b2017-05-16 09:51:39 -07008272 /*
8273 * Increase the count before we submit the bio so we know
8274 * the end IO handler won't happen before we increase the
8275 * count. Otherwise, the dip might get freed before we're
8276 * done setting it up.
8277 */
8278 atomic_inc(&dip->pending_bios);
Miao Xiee65e1532010-11-22 03:04:43 +00008279
David Sterbad0ee3932018-03-08 14:35:48 +01008280 status = btrfs_submit_dio_bio(bio, inode, file_offset,
Omar Sandoval58efbc92017-08-22 23:45:59 -07008281 async_submit);
8282 if (status) {
Liu Bo725130b2017-05-16 09:51:39 -07008283 bio_put(bio);
8284 atomic_dec(&dip->pending_bios);
8285 goto out_err;
Miao Xiee65e1532010-11-22 03:04:43 +00008286 }
Liu Bo725130b2017-05-16 09:51:39 -07008287
8288 clone_offset += clone_len;
8289 start_sector += clone_len >> 9;
8290 file_offset += clone_len;
8291
8292 map_length = submit_len;
8293 ret = btrfs_map_block(fs_info, btrfs_op(orig_bio),
8294 start_sector << 9, &map_length, NULL, 0);
8295 if (ret)
8296 goto out_err;
Arnd Bergmann3c91ee62017-05-18 15:33:29 +02008297 } while (submit_len > 0);
Miao Xiee65e1532010-11-22 03:04:43 +00008298
Josef Bacik02f57c72011-04-06 14:25:44 -04008299submit:
David Sterbad0ee3932018-03-08 14:35:48 +01008300 status = btrfs_submit_dio_bio(bio, inode, file_offset, async_submit);
Omar Sandoval58efbc92017-08-22 23:45:59 -07008301 if (!status)
Miao Xiee65e1532010-11-22 03:04:43 +00008302 return 0;
8303
8304 bio_put(bio);
8305out_err:
8306 dip->errors = 1;
8307 /*
Nikolay Borisovde224b72018-02-14 10:53:36 +02008308 * Before atomic variable goto zero, we must make sure dip->errors is
8309 * perceived to be set. This ordering is ensured by the fact that an
8310 * atomic operations with a return value are fully ordered as per
8311 * atomic_t.txt
Miao Xiee65e1532010-11-22 03:04:43 +00008312 */
Miao Xiee65e1532010-11-22 03:04:43 +00008313 if (atomic_dec_and_test(&dip->pending_bios))
8314 bio_io_error(dip->orig_bio);
8315
8316 /* bio_end_io() will handle error, so we needn't return it */
8317 return 0;
8318}
8319
Mike Christie8a4c1e42016-06-05 14:31:50 -05008320static void btrfs_submit_direct(struct bio *dio_bio, struct inode *inode,
8321 loff_t file_offset)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008322{
Filipe Manana61de7182015-07-01 12:13:10 +01008323 struct btrfs_dio_private *dip = NULL;
Liu Bo3892ac92017-04-17 15:00:28 -07008324 struct bio *bio = NULL;
8325 struct btrfs_io_bio *io_bio;
Mike Christie8a4c1e42016-06-05 14:31:50 -05008326 bool write = (bio_op(dio_bio) == REQ_OP_WRITE);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008327 int ret = 0;
8328
David Sterba8b6c1d52017-06-02 17:48:13 +02008329 bio = btrfs_bio_clone(dio_bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008330
Miao Xiec1dc0892014-09-12 18:43:56 +08008331 dip = kzalloc(sizeof(*dip), GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04008332 if (!dip) {
8333 ret = -ENOMEM;
Filipe Manana61de7182015-07-01 12:13:10 +01008334 goto free_ordered;
Chris Mason9be33952013-05-17 18:30:14 -04008335 }
8336
8337 dip->private = dio_bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008338 dip->inode = inode;
8339 dip->logical_offset = file_offset;
Kent Overstreet4f024f32013-10-11 15:44:27 -07008340 dip->bytes = dio_bio->bi_iter.bi_size;
8341 dip->disk_bytenr = (u64)dio_bio->bi_iter.bi_sector << 9;
Liu Bo3892ac92017-04-17 15:00:28 -07008342 bio->bi_private = dip;
8343 dip->orig_bio = bio;
Chris Mason9be33952013-05-17 18:30:14 -04008344 dip->dio_bio = dio_bio;
Miao Xiee65e1532010-11-22 03:04:43 +00008345 atomic_set(&dip->pending_bios, 0);
Liu Bo3892ac92017-04-17 15:00:28 -07008346 io_bio = btrfs_io_bio(bio);
8347 io_bio->logical = file_offset;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008348
Miao Xiec1dc0892014-09-12 18:43:56 +08008349 if (write) {
Liu Bo3892ac92017-04-17 15:00:28 -07008350 bio->bi_end_io = btrfs_endio_direct_write;
Miao Xiec1dc0892014-09-12 18:43:56 +08008351 } else {
Liu Bo3892ac92017-04-17 15:00:28 -07008352 bio->bi_end_io = btrfs_endio_direct_read;
Miao Xiec1dc0892014-09-12 18:43:56 +08008353 dip->subio_endio = btrfs_subio_endio_read;
8354 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04008355
Filipe Mananaf28a4922015-12-08 19:23:20 +00008356 /*
8357 * Reset the range for unsubmitted ordered extents (to a 0 length range)
8358 * even if we fail to submit a bio, because in such case we do the
8359 * corresponding error handling below and it must not be done a second
8360 * time by btrfs_direct_IO().
8361 */
8362 if (write) {
8363 struct btrfs_dio_data *dio_data = current->journal_info;
8364
8365 dio_data->unsubmitted_oe_range_end = dip->logical_offset +
8366 dip->bytes;
8367 dio_data->unsubmitted_oe_range_start =
8368 dio_data->unsubmitted_oe_range_end;
8369 }
8370
Nikolay Borisove6961ca2017-08-03 15:44:58 +03008371 ret = btrfs_submit_direct_hook(dip);
Miao Xiee65e1532010-11-22 03:04:43 +00008372 if (!ret)
Chris Masoneaf25d92010-05-25 09:48:28 -04008373 return;
Chris Mason9be33952013-05-17 18:30:14 -04008374
Liu Bo3892ac92017-04-17 15:00:28 -07008375 if (io_bio->end_io)
8376 io_bio->end_io(io_bio, ret);
Chris Mason9be33952013-05-17 18:30:14 -04008377
Josef Bacik4b46fce2010-05-23 11:00:55 -04008378free_ordered:
8379 /*
Filipe Manana61de7182015-07-01 12:13:10 +01008380 * If we arrived here it means either we failed to submit the dip
8381 * or we either failed to clone the dio_bio or failed to allocate the
8382 * dip. If we cloned the dio_bio and allocated the dip, we can just
8383 * call bio_endio against our io_bio so that we get proper resource
8384 * cleanup if we fail to submit the dip, otherwise, we must do the
8385 * same as btrfs_endio_direct_[write|read] because we can't call these
8386 * callbacks - they require an allocated dip and a clone of dio_bio.
Josef Bacik4b46fce2010-05-23 11:00:55 -04008387 */
Liu Bo3892ac92017-04-17 15:00:28 -07008388 if (bio && dip) {
Guoqing Jiang054ec2f2017-06-02 16:08:50 +08008389 bio_io_error(bio);
Filipe Manana61de7182015-07-01 12:13:10 +01008390 /*
Liu Bo3892ac92017-04-17 15:00:28 -07008391 * The end io callbacks free our dip, do the final put on bio
Filipe Manana61de7182015-07-01 12:13:10 +01008392 * and all the cleanup and final put for dio_bio (through
8393 * dio_end_io()).
8394 */
8395 dip = NULL;
Liu Bo3892ac92017-04-17 15:00:28 -07008396 bio = NULL;
Filipe Manana61de7182015-07-01 12:13:10 +01008397 } else {
Filipe Manana14543772015-11-24 16:23:54 +00008398 if (write)
Qu Wenruo524272602017-03-08 10:25:52 +08008399 __endio_write_update_ordered(inode,
Filipe Manana14543772015-11-24 16:23:54 +00008400 file_offset,
8401 dio_bio->bi_iter.bi_size,
Qu Wenruo524272602017-03-08 10:25:52 +08008402 false);
Filipe Manana14543772015-11-24 16:23:54 +00008403 else
Filipe Manana61de7182015-07-01 12:13:10 +01008404 unlock_extent(&BTRFS_I(inode)->io_tree, file_offset,
8405 file_offset + dio_bio->bi_iter.bi_size - 1);
Filipe Manana14543772015-11-24 16:23:54 +00008406
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008407 dio_bio->bi_status = BLK_STS_IOERR;
Filipe Manana61de7182015-07-01 12:13:10 +01008408 /*
8409 * Releases and cleans up our dio_bio, no need to bio_put()
8410 * nor bio_endio()/bio_io_error() against dio_bio.
8411 */
Christoph Hellwig40553512017-06-03 09:37:58 +02008412 dio_end_io(dio_bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008413 }
Liu Bo3892ac92017-04-17 15:00:28 -07008414 if (bio)
8415 bio_put(bio);
Filipe Manana61de7182015-07-01 12:13:10 +01008416 kfree(dip);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008417}
8418
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008419static ssize_t check_direct_IO(struct btrfs_fs_info *fs_info,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008420 const struct iov_iter *iter, loff_t offset)
Chris Mason5a5f79b2010-05-26 21:33:37 -04008421{
8422 int seg;
Josef Bacika1b75f72011-04-08 15:51:18 +00008423 int i;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008424 unsigned int blocksize_mask = fs_info->sectorsize - 1;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008425 ssize_t retval = -EINVAL;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008426
8427 if (offset & blocksize_mask)
8428 goto out;
8429
Al Viro28060d52014-03-22 05:15:17 -04008430 if (iov_iter_alignment(iter) & blocksize_mask)
8431 goto out;
Josef Bacika1b75f72011-04-08 15:51:18 +00008432
Al Viro28060d52014-03-22 05:15:17 -04008433 /* If this is a write we don't need to check anymore */
Al Virocd27e452016-10-10 13:39:05 -04008434 if (iov_iter_rw(iter) != READ || !iter_is_iovec(iter))
Al Viro28060d52014-03-22 05:15:17 -04008435 return 0;
8436 /*
8437 * Check to make sure we don't have duplicate iov_base's in this
8438 * iovec, if so return EINVAL, otherwise we'll get csum errors
8439 * when reading back.
8440 */
8441 for (seg = 0; seg < iter->nr_segs; seg++) {
8442 for (i = seg + 1; i < iter->nr_segs; i++) {
8443 if (iter->iov[seg].iov_base == iter->iov[i].iov_base)
Josef Bacika1b75f72011-04-08 15:51:18 +00008444 goto out;
8445 }
Chris Mason5a5f79b2010-05-26 21:33:37 -04008446 }
8447 retval = 0;
8448out:
8449 return retval;
8450}
Josef Bacikeb838e72012-07-31 16:28:48 -04008451
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008452static ssize_t btrfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
Chris Mason16432982008-04-10 10:23:21 -04008453{
Josef Bacik4b46fce2010-05-23 11:00:55 -04008454 struct file *file = iocb->ki_filp;
8455 struct inode *inode = file->f_mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008456 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
chandan50745b02015-08-28 21:10:13 +05308457 struct btrfs_dio_data dio_data = { 0 };
Qu Wenruo364ecf32017-02-27 15:10:38 +08008458 struct extent_changeset *data_reserved = NULL;
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008459 loff_t offset = iocb->ki_pos;
Miao Xie09348562013-02-07 10:12:07 +00008460 size_t count = 0;
Miao Xie2e60a512013-02-08 07:01:08 +00008461 int flags = 0;
Miao Xie38851cc2013-02-08 07:04:11 +00008462 bool wakeup = true;
8463 bool relock = false;
Miao Xie09348562013-02-07 10:12:07 +00008464 ssize_t ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008465
Nikolay Borisov8c70c9f2017-08-21 12:43:46 +03008466 if (check_direct_IO(fs_info, iter, offset))
Chris Mason5a5f79b2010-05-26 21:33:37 -04008467 return 0;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008468
Jens Axboefe0f07d2015-04-15 17:05:48 -06008469 inode_dio_begin(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008470
Josef Bacik0e267c42013-07-02 10:38:02 -04008471 /*
Miao Xie41bd9ca2014-03-06 13:54:57 +08008472 * The generic stuff only does filemap_write_and_wait_range, which
8473 * isn't enough if we've written compressed pages to this area, so
8474 * we need to flush the dirty pages again to make absolutely sure
8475 * that any outstanding dirty pages are on disk.
Josef Bacik0e267c42013-07-02 10:38:02 -04008476 */
Al Viroa6cbcd42014-03-04 22:38:00 -05008477 count = iov_iter_count(iter);
Miao Xie41bd9ca2014-03-06 13:54:57 +08008478 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
8479 &BTRFS_I(inode)->runtime_flags))
Wang Shilong9a025a02014-07-17 11:44:13 +08008480 filemap_fdatawrite_range(inode->i_mapping, offset,
8481 offset + count - 1);
Josef Bacik0e267c42013-07-02 10:38:02 -04008482
Omar Sandoval6f673762015-03-16 04:33:52 -07008483 if (iov_iter_rw(iter) == WRITE) {
Miao Xie38851cc2013-02-08 07:04:11 +00008484 /*
8485 * If the write DIO is beyond the EOF, we need update
8486 * the isize, but it is protected by i_mutex. So we can
8487 * not unlock the i_mutex at this case.
8488 */
8489 if (offset + count <= inode->i_size) {
Liu Bo4aaedfb2016-12-14 22:36:05 -08008490 dio_data.overwrite = 1;
Al Viro59551022016-01-22 15:40:57 -05008491 inode_unlock(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008492 relock = true;
Goldwyn Rodriguesedf064e2017-06-20 07:05:49 -05008493 } else if (iocb->ki_flags & IOCB_NOWAIT) {
8494 ret = -EAGAIN;
8495 goto out;
Miao Xie38851cc2013-02-08 07:04:11 +00008496 }
Qu Wenruo364ecf32017-02-27 15:10:38 +08008497 ret = btrfs_delalloc_reserve_space(inode, &data_reserved,
8498 offset, count);
Miao Xie09348562013-02-07 10:12:07 +00008499 if (ret)
Miao Xie38851cc2013-02-08 07:04:11 +00008500 goto out;
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008501
8502 /*
8503 * We need to know how many extents we reserved so that we can
8504 * do the accounting properly if we go over the number we
8505 * originally calculated. Abuse current->journal_info for this.
8506 */
Jeff Mahoneyda170662016-06-15 09:22:56 -04008507 dio_data.reserve = round_up(count,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008508 fs_info->sectorsize);
Filipe Mananaf28a4922015-12-08 19:23:20 +00008509 dio_data.unsubmitted_oe_range_start = (u64)offset;
8510 dio_data.unsubmitted_oe_range_end = (u64)offset;
chandan50745b02015-08-28 21:10:13 +05308511 current->journal_info = &dio_data;
Chandan Rajendra97dcdea2016-12-23 15:00:18 +05308512 down_read(&BTRFS_I(inode)->dio_sem);
David Sterbaee39b432014-09-30 01:33:33 +02008513 } else if (test_bit(BTRFS_INODE_READDIO_NEED_LOCK,
8514 &BTRFS_I(inode)->runtime_flags)) {
Jens Axboefe0f07d2015-04-15 17:05:48 -06008515 inode_dio_end(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008516 flags = DIO_LOCKING | DIO_SKIP_HOLES;
8517 wakeup = false;
Miao Xie09348562013-02-07 10:12:07 +00008518 }
8519
Omar Sandoval17f8c842015-03-16 04:33:50 -07008520 ret = __blockdev_direct_IO(iocb, inode,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008521 fs_info->fs_devices->latest_bdev,
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008522 iter, btrfs_get_blocks_direct, NULL,
Omar Sandoval17f8c842015-03-16 04:33:50 -07008523 btrfs_submit_direct, flags);
Omar Sandoval6f673762015-03-16 04:33:52 -07008524 if (iov_iter_rw(iter) == WRITE) {
Chandan Rajendra97dcdea2016-12-23 15:00:18 +05308525 up_read(&BTRFS_I(inode)->dio_sem);
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008526 current->journal_info = NULL;
Liu Boddba1bf2015-06-17 16:59:58 +08008527 if (ret < 0 && ret != -EIOCBQUEUED) {
chandan50745b02015-08-28 21:10:13 +05308528 if (dio_data.reserve)
Qu Wenruobc42bda2017-02-27 15:10:39 +08008529 btrfs_delalloc_release_space(inode, data_reserved,
Qu Wenruo43b18592017-12-12 15:34:32 +08008530 offset, dio_data.reserve, true);
Filipe Mananaf28a4922015-12-08 19:23:20 +00008531 /*
8532 * On error we might have left some ordered extents
8533 * without submitting corresponding bios for them, so
8534 * cleanup them up to avoid other tasks getting them
8535 * and waiting for them to complete forever.
8536 */
8537 if (dio_data.unsubmitted_oe_range_start <
8538 dio_data.unsubmitted_oe_range_end)
Qu Wenruo524272602017-03-08 10:25:52 +08008539 __endio_write_update_ordered(inode,
Filipe Mananaf28a4922015-12-08 19:23:20 +00008540 dio_data.unsubmitted_oe_range_start,
8541 dio_data.unsubmitted_oe_range_end -
8542 dio_data.unsubmitted_oe_range_start,
Qu Wenruo524272602017-03-08 10:25:52 +08008543 false);
Liu Boddba1bf2015-06-17 16:59:58 +08008544 } else if (ret >= 0 && (size_t)ret < count)
Qu Wenruobc42bda2017-02-27 15:10:39 +08008545 btrfs_delalloc_release_space(inode, data_reserved,
Qu Wenruo43b18592017-12-12 15:34:32 +08008546 offset, count - (size_t)ret, true);
8547 btrfs_delalloc_release_extents(BTRFS_I(inode), count, false);
Miao Xie09348562013-02-07 10:12:07 +00008548 }
Miao Xie38851cc2013-02-08 07:04:11 +00008549out:
Miao Xie2e60a512013-02-08 07:01:08 +00008550 if (wakeup)
Jens Axboefe0f07d2015-04-15 17:05:48 -06008551 inode_dio_end(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008552 if (relock)
Al Viro59551022016-01-22 15:40:57 -05008553 inode_lock(inode);
Miao Xie09348562013-02-07 10:12:07 +00008554
Qu Wenruo364ecf32017-02-27 15:10:38 +08008555 extent_changeset_free(data_reserved);
Miao Xie09348562013-02-07 10:12:07 +00008556 return ret;
Chris Mason16432982008-04-10 10:23:21 -04008557}
8558
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008559#define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
8560
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008561static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
8562 __u64 start, __u64 len)
8563{
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008564 int ret;
8565
8566 ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS);
8567 if (ret)
8568 return ret;
8569
David Sterba2135fb92017-06-23 04:09:57 +02008570 return extent_fiemap(inode, fieinfo, start, len);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008571}
8572
Chris Mason9ebefb182007-06-15 13:50:00 -04008573int btrfs_readpage(struct file *file, struct page *page)
8574{
Chris Masond1310b22008-01-24 16:13:08 -05008575 struct extent_io_tree *tree;
8576 tree = &BTRFS_I(page->mapping->host)->io_tree;
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02008577 return extent_read_full_page(tree, page, btrfs_get_extent, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04008578}
Chris Mason1832a6d2007-12-21 16:27:21 -05008579
Chris Mason39279cc2007-06-12 06:35:45 -04008580static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
8581{
Josef Bacikbe7bd732015-10-22 15:05:09 -04008582 struct inode *inode = page->mapping->host;
8583 int ret;
Chris Masonb888db22007-08-27 16:49:44 -04008584
8585 if (current->flags & PF_MEMALLOC) {
8586 redirty_page_for_writepage(wbc, page);
8587 unlock_page(page);
8588 return 0;
8589 }
Josef Bacikbe7bd732015-10-22 15:05:09 -04008590
8591 /*
8592 * If we are under memory pressure we will call this directly from the
8593 * VM, we need to make sure we have the inode referenced for the ordered
8594 * extent. If not just return like we didn't do anything.
8595 */
8596 if (!igrab(inode)) {
8597 redirty_page_for_writepage(wbc, page);
8598 return AOP_WRITEPAGE_ACTIVATE;
8599 }
Nikolay Borisov0a9b0e52017-12-08 15:55:59 +02008600 ret = extent_write_full_page(page, wbc);
Josef Bacikbe7bd732015-10-22 15:05:09 -04008601 btrfs_add_delayed_iput(inode);
8602 return ret;
Chris Masona52d9a82007-08-27 16:49:44 -04008603}
Chris Mason39279cc2007-06-12 06:35:45 -04008604
Eric Sandeen48a3b632013-04-25 20:41:01 +00008605static int btrfs_writepages(struct address_space *mapping,
8606 struct writeback_control *wbc)
Chris Masonb293f02e2007-11-01 19:45:34 -04008607{
Nikolay Borisov8ae225a2018-04-19 10:46:38 +03008608 return extent_writepages(mapping, wbc);
Chris Masonb293f02e2007-11-01 19:45:34 -04008609}
8610
Chris Mason3ab2fb52007-11-08 10:59:22 -05008611static int
8612btrfs_readpages(struct file *file, struct address_space *mapping,
8613 struct list_head *pages, unsigned nr_pages)
8614{
Nikolay Borisov2a3ff0a2018-04-19 10:46:36 +03008615 return extent_readpages(mapping, pages, nr_pages);
Chris Mason3ab2fb52007-11-08 10:59:22 -05008616}
Nikolay Borisov2a3ff0a2018-04-19 10:46:36 +03008617
Chris Masone6dcd2d2008-07-17 12:53:50 -04008618static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04008619{
Nikolay Borisov477a30b2018-04-19 10:46:34 +03008620 int ret = try_release_extent_mapping(page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04008621 if (ret == 1) {
8622 ClearPagePrivate(page);
8623 set_page_private(page, 0);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008624 put_page(page);
Chris Masona52d9a82007-08-27 16:49:44 -04008625 }
8626 return ret;
8627}
Chris Mason39279cc2007-06-12 06:35:45 -04008628
Chris Masone6dcd2d2008-07-17 12:53:50 -04008629static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
8630{
Chris Mason98509cf2008-09-11 15:51:43 -04008631 if (PageWriteback(page) || PageDirty(page))
8632 return 0;
Michal Hocko3ba7ab22017-01-09 15:39:02 +01008633 return __btrfs_releasepage(page, gfp_flags);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008634}
8635
Lukas Czernerd47992f2013-05-21 23:17:23 -04008636static void btrfs_invalidatepage(struct page *page, unsigned int offset,
8637 unsigned int length)
Chris Masona52d9a82007-08-27 16:49:44 -04008638{
Josef Bacik5fd02042012-05-02 14:00:54 -04008639 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05008640 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008641 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008642 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008643 u64 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008644 u64 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308645 u64 start;
8646 u64 end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008647 int inode_evicting = inode->i_state & I_FREEING;
Chris Masona52d9a82007-08-27 16:49:44 -04008648
Chris Mason8b62b722009-09-02 16:53:46 -04008649 /*
8650 * we have the page locked, so new writeback can't start,
8651 * and the dirty bit won't be cleared while we are here.
8652 *
8653 * Wait for IO on this page so that we can safely clear
8654 * the PagePrivate2 bit and do ordered accounting
8655 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008656 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04008657
Josef Bacik5fd02042012-05-02 14:00:54 -04008658 tree = &BTRFS_I(inode)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008659 if (offset) {
8660 btrfs_releasepage(page, GFP_NOFS);
8661 return;
8662 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008663
8664 if (!inode_evicting)
David Sterbaff13db42015-12-03 14:30:40 +01008665 lock_extent_bits(tree, page_start, page_end, &cached_state);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308666again:
8667 start = page_start;
Nikolay Borisova776c6f2017-02-20 13:50:49 +02008668 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), start,
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308669 page_end - start + 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008670 if (ordered) {
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308671 end = min(page_end, ordered->file_offset + ordered->len - 1);
Chris Masoneb84ae02008-07-17 13:53:27 -04008672 /*
8673 * IO on this page will never be started, so we need
8674 * to account for any ordered extents now
8675 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008676 if (!inode_evicting)
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308677 clear_extent_bit(tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008678 EXTENT_DIRTY | EXTENT_DELALLOC |
Filipe Mananaa7e3b972017-04-03 10:45:46 +01008679 EXTENT_DELALLOC_NEW |
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008680 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
David Sterbaae0f1622017-10-31 16:37:52 +01008681 EXTENT_DEFRAG, 1, 0, &cached_state);
Chris Mason8b62b722009-09-02 16:53:46 -04008682 /*
8683 * whoever cleared the private bit is responsible
8684 * for the finish_ordered_io
8685 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04008686 if (TestClearPagePrivate2(page)) {
8687 struct btrfs_ordered_inode_tree *tree;
8688 u64 new_len;
8689
8690 tree = &BTRFS_I(inode)->ordered_tree;
8691
8692 spin_lock_irq(&tree->lock);
8693 set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308694 new_len = start - ordered->file_offset;
Josef Bacik77cef2e2013-08-29 13:57:21 -04008695 if (new_len < ordered->truncated_len)
8696 ordered->truncated_len = new_len;
8697 spin_unlock_irq(&tree->lock);
8698
8699 if (btrfs_dec_test_ordered_pending(inode, &ordered,
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308700 start,
8701 end - start + 1, 1))
Josef Bacik77cef2e2013-08-29 13:57:21 -04008702 btrfs_finish_ordered_io(ordered);
Chris Mason8b62b722009-09-02 16:53:46 -04008703 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008704 btrfs_put_ordered_extent(ordered);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008705 if (!inode_evicting) {
8706 cached_state = NULL;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308707 lock_extent_bits(tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008708 &cached_state);
8709 }
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308710
8711 start = end + 1;
8712 if (start < page_end)
8713 goto again;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008714 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008715
Qu Wenruob9d0b382015-09-29 10:35:16 +08008716 /*
8717 * Qgroup reserved space handler
8718 * Page here will be either
8719 * 1) Already written to disk
8720 * In this case, its reserved space is released from data rsv map
8721 * and will be freed by delayed_ref handler finally.
8722 * So even we call qgroup_free_data(), it won't decrease reserved
8723 * space.
8724 * 2) Not written to disk
Goldwyn Rodrigues0b34c262016-09-30 10:40:52 -05008725 * This means the reserved space should be freed here. However,
8726 * if a truncate invalidates the page (by clearing PageDirty)
8727 * and the page is accounted for while allocating extent
8728 * in btrfs_check_data_free_space() we let delayed_ref to
8729 * free the entire extent.
Qu Wenruob9d0b382015-09-29 10:35:16 +08008730 */
Goldwyn Rodrigues0b34c262016-09-30 10:40:52 -05008731 if (PageDirty(page))
Qu Wenruobc42bda2017-02-27 15:10:39 +08008732 btrfs_qgroup_free_data(inode, NULL, page_start, PAGE_SIZE);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008733 if (!inode_evicting) {
8734 clear_extent_bit(tree, page_start, page_end,
8735 EXTENT_LOCKED | EXTENT_DIRTY |
Filipe Mananaa7e3b972017-04-03 10:45:46 +01008736 EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
8737 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1,
David Sterbaae0f1622017-10-31 16:37:52 +01008738 &cached_state);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008739
8740 __btrfs_releasepage(page, GFP_NOFS);
8741 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008742
Chris Mason4a096752008-07-21 10:29:44 -04008743 ClearPageChecked(page);
Chris Mason9ad6b7bc2008-04-18 16:11:30 -04008744 if (PagePrivate(page)) {
Chris Mason9ad6b7bc2008-04-18 16:11:30 -04008745 ClearPagePrivate(page);
8746 set_page_private(page, 0);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008747 put_page(page);
Chris Mason9ad6b7bc2008-04-18 16:11:30 -04008748 }
Chris Mason39279cc2007-06-12 06:35:45 -04008749}
8750
Chris Mason9ebefb182007-06-15 13:50:00 -04008751/*
8752 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
8753 * called from a page fault handler when a page is first dirtied. Hence we must
8754 * be careful to check for EOF conditions here. We set the page up correctly
8755 * for a written page which means we get ENOSPC checking when writing into
8756 * holes and correct delalloc and unwritten extent mapping on filesystems that
8757 * support these features.
8758 *
8759 * We are not allowed to take the i_mutex here so we have to play games to
8760 * protect against truncate races as the page could now be beyond EOF. Because
Omar Sandovald1342aa2018-05-11 13:13:29 -07008761 * truncate_setsize() writes the inode size before removing pages, once we have
8762 * the page lock we can determine safely if the page is beyond EOF. If it is not
Chris Mason9ebefb182007-06-15 13:50:00 -04008763 * beyond EOF, then the page is guaranteed safe against truncation until we
8764 * unlock the page.
8765 */
Souptick Joardera528a242018-06-06 19:54:44 +05308766vm_fault_t btrfs_page_mkwrite(struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04008767{
Nick Pigginc2ec1752009-03-31 15:23:21 -07008768 struct page *page = vmf->page;
Dave Jiang11bac802017-02-24 14:56:41 -08008769 struct inode *inode = file_inode(vmf->vma->vm_file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008770 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008771 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
8772 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008773 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08008774 struct extent_changeset *data_reserved = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008775 char *kaddr;
8776 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04008777 loff_t size;
Souptick Joardera528a242018-06-06 19:54:44 +05308778 vm_fault_t ret;
8779 int ret2;
Chris Mason9998eb72012-01-25 13:47:40 -05008780 int reserved = 0;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308781 u64 reserved_space;
Chris Masona52d9a82007-08-27 16:49:44 -04008782 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008783 u64 page_end;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308784 u64 end;
8785
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008786 reserved_space = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04008787
Jan Karab2b5ef52012-06-12 16:20:45 +02008788 sb_start_pagefault(inode->i_sb);
Qu Wenruodf480632015-09-08 17:25:54 +08008789 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008790 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308791 end = page_end;
Qu Wenruodf480632015-09-08 17:25:54 +08008792
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308793 /*
8794 * Reserving delalloc space after obtaining the page lock can lead to
8795 * deadlock. For example, if a dirty page is locked by this function
8796 * and the call to btrfs_delalloc_reserve_space() ends up triggering
8797 * dirty page write out, then the btrfs_writepage() function could
8798 * end up waiting indefinitely to get a lock on the page currently
8799 * being processed by btrfs_page_mkwrite() function.
8800 */
Souptick Joardera528a242018-06-06 19:54:44 +05308801 ret2 = btrfs_delalloc_reserve_space(inode, &data_reserved, page_start,
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308802 reserved_space);
Souptick Joardera528a242018-06-06 19:54:44 +05308803 if (!ret2) {
8804 ret2 = file_update_time(vmf->vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05008805 reserved = 1;
8806 }
Souptick Joardera528a242018-06-06 19:54:44 +05308807 if (ret2) {
8808 ret = vmf_error(ret2);
Chris Mason9998eb72012-01-25 13:47:40 -05008809 if (reserved)
8810 goto out;
8811 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07008812 }
Chris Mason1832a6d2007-12-21 16:27:21 -05008813
Nick Piggin56a76f82009-03-31 15:23:23 -07008814 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008815again:
Chris Mason9ebefb182007-06-15 13:50:00 -04008816 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04008817 size = i_size_read(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04008818
Chris Mason9ebefb182007-06-15 13:50:00 -04008819 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04008820 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04008821 /* page got truncated out from underneath us */
8822 goto out_unlock;
8823 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008824 wait_on_page_writeback(page);
8825
David Sterbaff13db42015-12-03 14:30:40 +01008826 lock_extent_bits(io_tree, page_start, page_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008827 set_page_extent_mapped(page);
8828
Chris Masoneb84ae02008-07-17 13:53:27 -04008829 /*
8830 * we can't set the delalloc bits if there are pending ordered
8831 * extents. Drop our locks and wait for them to finish
8832 */
Nikolay Borisova776c6f2017-02-20 13:50:49 +02008833 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start,
8834 PAGE_SIZE);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008835 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00008836 unlock_extent_cached(io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01008837 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008838 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04008839 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008840 btrfs_put_ordered_extent(ordered);
8841 goto again;
8842 }
8843
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008844 if (page->index == ((size - 1) >> PAGE_SHIFT)) {
Jeff Mahoneyda170662016-06-15 09:22:56 -04008845 reserved_space = round_up(size - page_start,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008846 fs_info->sectorsize);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008847 if (reserved_space < PAGE_SIZE) {
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308848 end = page_start + reserved_space - 1;
Qu Wenruobc42bda2017-02-27 15:10:39 +08008849 btrfs_delalloc_release_space(inode, data_reserved,
Qu Wenruo43b18592017-12-12 15:34:32 +08008850 page_start, PAGE_SIZE - reserved_space,
8851 true);
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308852 }
8853 }
8854
Josef Bacikfbf19082009-10-01 17:10:23 -04008855 /*
Liu Bo54160342016-12-13 12:15:19 -08008856 * page_mkwrite gets called when the page is firstly dirtied after it's
8857 * faulted in, but write(2) could also dirty a page and set delalloc
8858 * bits, thus in this case for space account reason, we still need to
8859 * clear any delalloc bits within this page range since we have to
8860 * reserve data&meta space before lock_page() (see above comments).
Josef Bacikfbf19082009-10-01 17:10:23 -04008861 */
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308862 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06008863 EXTENT_DIRTY | EXTENT_DELALLOC |
8864 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
David Sterbaae0f1622017-10-31 16:37:52 +01008865 0, 0, &cached_state);
Josef Bacikfbf19082009-10-01 17:10:23 -04008866
Souptick Joardera528a242018-06-06 19:54:44 +05308867 ret2 = btrfs_set_extent_delalloc(inode, page_start, end, 0,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08008868 &cached_state, 0);
Souptick Joardera528a242018-06-06 19:54:44 +05308869 if (ret2) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00008870 unlock_extent_cached(io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01008871 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04008872 ret = VM_FAULT_SIGBUS;
8873 goto out_unlock;
8874 }
Souptick Joardera528a242018-06-06 19:54:44 +05308875 ret2 = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04008876
8877 /* page is wholly or partially inside EOF */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008878 if (page_start + PAGE_SIZE > size)
8879 zero_start = size & ~PAGE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04008880 else
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008881 zero_start = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04008882
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008883 if (zero_start != PAGE_SIZE) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04008884 kaddr = kmap(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008885 memset(kaddr + zero_start, 0, PAGE_SIZE - zero_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008886 flush_dcache_page(page);
8887 kunmap(page);
8888 }
Chris Mason247e7432008-07-17 12:53:51 -04008889 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008890 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04008891 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04008892
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008893 BTRFS_I(inode)->last_trans = fs_info->generation;
Chris Mason257c62e2009-10-13 13:21:08 -04008894 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06008895 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
Chris Mason257c62e2009-10-13 13:21:08 -04008896
David Sterbae43bbe52017-12-12 21:43:52 +01008897 unlock_extent_cached(io_tree, page_start, page_end, &cached_state);
Chris Mason9ebefb182007-06-15 13:50:00 -04008898
Souptick Joardera528a242018-06-06 19:54:44 +05308899 if (!ret2) {
Qu Wenruo43b18592017-12-12 15:34:32 +08008900 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE, true);
Jan Karab2b5ef52012-06-12 16:20:45 +02008901 sb_end_pagefault(inode->i_sb);
Qu Wenruo364ecf32017-02-27 15:10:38 +08008902 extent_changeset_free(data_reserved);
Chris Mason50a9b212009-09-11 12:33:12 -04008903 return VM_FAULT_LOCKED;
Jan Karab2b5ef52012-06-12 16:20:45 +02008904 }
Chris Mason717beb92018-06-25 10:03:41 -07008905
8906out_unlock:
Chris Mason9ebefb182007-06-15 13:50:00 -04008907 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05008908out:
Qu Wenruo43b18592017-12-12 15:34:32 +08008909 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE, (ret != 0));
Qu Wenruobc42bda2017-02-27 15:10:39 +08008910 btrfs_delalloc_release_space(inode, data_reserved, page_start,
Qu Wenruo43b18592017-12-12 15:34:32 +08008911 reserved_space, (ret != 0));
Chris Mason9998eb72012-01-25 13:47:40 -05008912out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02008913 sb_end_pagefault(inode->i_sb);
Qu Wenruo364ecf32017-02-27 15:10:38 +08008914 extent_changeset_free(data_reserved);
Chris Mason9ebefb182007-06-15 13:50:00 -04008915 return ret;
8916}
8917
Filipe Manana213e8c52018-02-06 20:40:31 +00008918static int btrfs_truncate(struct inode *inode, bool skip_writeback)
Chris Mason39279cc2007-06-12 06:35:45 -04008919{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008920 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04008921 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04008922 struct btrfs_block_rsv *rsv;
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008923 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04008924 struct btrfs_trans_handle *trans;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008925 u64 mask = fs_info->sectorsize - 1;
8926 u64 min_size = btrfs_calc_trunc_metadata_size(fs_info, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04008927
Filipe Manana213e8c52018-02-06 20:40:31 +00008928 if (!skip_writeback) {
8929 ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask),
8930 (u64)-1);
8931 if (ret)
8932 return ret;
8933 }
Chris Mason39279cc2007-06-12 06:35:45 -04008934
Josef Bacikfcb80c22011-05-03 10:40:22 -04008935 /*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008936 * Yes ladies and gentlemen, this is indeed ugly. We have a couple of
8937 * things going on here:
Josef Bacikfcb80c22011-05-03 10:40:22 -04008938 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008939 * 1) We need to reserve space to update our inode.
Josef Bacikfcb80c22011-05-03 10:40:22 -04008940 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008941 * 2) We need to have something to cache all the space that is going to
Josef Bacikfcb80c22011-05-03 10:40:22 -04008942 * be free'd up by the truncate operation, but also have some slack
8943 * space reserved in case it uses space during the truncate (thank you
8944 * very much snapshotting).
8945 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008946 * And we need these to be separate. The fact is we can use a lot of
Josef Bacikfcb80c22011-05-03 10:40:22 -04008947 * space doing the truncate, and we have no earthly idea how much space
Nicholas D Steeves01327612016-05-19 21:18:45 -04008948 * we will use, so we need the truncate reservation to be separate so it
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008949 * doesn't end up using space reserved for updating the inode. We also
8950 * need to be able to stop the transaction and start a new one, which
8951 * means we need to be able to update the inode several times, and we
8952 * have no idea of knowing how many times that will be, so we can't just
8953 * reserve 1 item for the entirety of the operation, so that has to be
8954 * done separately as well.
Josef Bacikfcb80c22011-05-03 10:40:22 -04008955 *
8956 * So that leaves us with
8957 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008958 * 1) rsv - for the truncate reservation, which we will steal from the
Josef Bacikfcb80c22011-05-03 10:40:22 -04008959 * transaction reservation.
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008960 * 2) fs_info->trans_block_rsv - this will have 1 items worth left for
Josef Bacikfcb80c22011-05-03 10:40:22 -04008961 * updating the inode.
8962 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008963 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008964 if (!rsv)
8965 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04008966 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04008967 rsv->failfast = 1;
Josef Bacikf0cd8462011-03-04 14:37:08 -05008968
Josef Bacik907cbce2011-08-08 13:46:15 -04008969 /*
Josef Bacik07127182011-08-19 10:29:59 -04008970 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04008971 * 1 for updating the inode.
8972 */
Josef Bacikf3fe8202013-01-07 17:03:21 -05008973 trans = btrfs_start_transaction(root, 2);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008974 if (IS_ERR(trans)) {
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008975 ret = PTR_ERR(trans);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008976 goto out;
8977 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05008978
Josef Bacik907cbce2011-08-08 13:46:15 -04008979 /* Migrate the slack space for the truncate to our reserve */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008980 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, rsv,
Lu Fengqi3a584172018-08-04 21:10:55 +08008981 min_size, false);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008982 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05008983
Chris Mason5a3f23d2009-03-31 13:27:11 -04008984 /*
Josef Bacik5dc562c2012-08-17 13:14:17 -04008985 * So if we truncate and then write and fsync we normally would just
8986 * write the extents that changed, which is a problem if we need to
8987 * first truncate that entire inode. So set this flag so we write out
8988 * all of the extents in the inode to the sync log so we're completely
8989 * safe.
8990 */
8991 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008992 trans->block_rsv = rsv;
Josef Bacik5dc562c2012-08-17 13:14:17 -04008993
Yan, Zheng80825102009-11-12 09:35:36 +00008994 while (1) {
8995 ret = btrfs_truncate_inode_items(trans, root, inode,
8996 inode->i_size,
8997 BTRFS_EXTENT_DATA_KEY);
Josef Bacikddfae632017-10-19 14:16:02 -04008998 trans->block_rsv = &fs_info->trans_block_rsv;
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008999 if (ret != -ENOSPC && ret != -EAGAIN)
Yan, Zheng80825102009-11-12 09:35:36 +00009000 break;
Chris Mason39279cc2007-06-12 06:35:45 -04009001
Yan, Zheng80825102009-11-12 09:35:36 +00009002 ret = btrfs_update_inode(trans, root, inode);
Omar Sandovalad7e1a72018-05-22 09:59:50 -07009003 if (ret)
Josef Bacik3893e332011-01-31 16:03:11 -05009004 break;
Josef Bacikca7e70f2012-08-27 17:48:15 -04009005
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009006 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009007 btrfs_btree_balance_dirty(fs_info);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009008
9009 trans = btrfs_start_transaction(root, 2);
9010 if (IS_ERR(trans)) {
Omar Sandovalad7e1a72018-05-22 09:59:50 -07009011 ret = PTR_ERR(trans);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009012 trans = NULL;
9013 break;
9014 }
9015
Wang Xiaoguang47b5d642016-09-07 20:17:38 +08009016 btrfs_block_rsv_release(fs_info, rsv, -1);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009017 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv,
Lu Fengqi3a584172018-08-04 21:10:55 +08009018 rsv, min_size, false);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009019 BUG_ON(ret); /* shouldn't happen */
9020 trans->block_rsv = rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00009021 }
9022
Josef Bacikddfae632017-10-19 14:16:02 -04009023 /*
9024 * We can't call btrfs_truncate_block inside a trans handle as we could
9025 * deadlock with freeze, if we got NEED_TRUNCATE_BLOCK then we know
9026 * we've truncated everything except the last little bit, and can do
9027 * btrfs_truncate_block and then update the disk_i_size.
9028 */
9029 if (ret == NEED_TRUNCATE_BLOCK) {
9030 btrfs_end_transaction(trans);
9031 btrfs_btree_balance_dirty(fs_info);
9032
9033 ret = btrfs_truncate_block(inode, inode->i_size, 0, 0);
9034 if (ret)
9035 goto out;
9036 trans = btrfs_start_transaction(root, 1);
9037 if (IS_ERR(trans)) {
9038 ret = PTR_ERR(trans);
9039 goto out;
9040 }
9041 btrfs_ordered_update_i_size(inode, inode->i_size, NULL);
9042 }
9043
Chris Mason917c16b2011-11-08 14:49:59 -05009044 if (trans) {
Omar Sandovalad7e1a72018-05-22 09:59:50 -07009045 int ret2;
Josef Bacik7b128762008-07-24 12:17:14 -04009046
Omar Sandovalad7e1a72018-05-22 09:59:50 -07009047 trans->block_rsv = &fs_info->trans_block_rsv;
9048 ret2 = btrfs_update_inode(trans, root, inode);
9049 if (ret2 && !ret)
9050 ret = ret2;
9051
9052 ret2 = btrfs_end_transaction(trans);
9053 if (ret2 && !ret)
9054 ret = ret2;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009055 btrfs_btree_balance_dirty(fs_info);
Chris Mason917c16b2011-11-08 14:49:59 -05009056 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04009057out:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009058 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009059
Omar Sandovalad7e1a72018-05-22 09:59:50 -07009060 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04009061}
9062
Sven Wegener3b963622008-06-09 21:57:42 -04009063/*
Chris Masond352ac62008-09-29 15:18:18 -04009064 * create a new subvolume directory/inode (helper for the ioctl).
9065 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05009066int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009067 struct btrfs_root *new_root,
9068 struct btrfs_root *parent_root,
9069 u64 new_dirid)
Chris Mason39279cc2007-06-12 06:35:45 -04009070{
Chris Mason39279cc2007-06-12 06:35:45 -04009071 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04009072 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04009073 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009074
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01009075 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
9076 new_dirid, new_dirid,
9077 S_IFDIR | (~current_umask() & S_IRWXUGO),
9078 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04009079 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04009080 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009081 inode->i_op = &btrfs_dir_inode_operations;
9082 inode->i_fop = &btrfs_dir_file_operations;
9083
Miklos Szeredibfe86842011-10-28 14:13:29 +02009084 set_nlink(inode, 1);
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02009085 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Masonb0d5d102014-09-08 13:08:51 -07009086 unlock_new_inode(inode);
Sven Wegener3b963622008-06-09 21:57:42 -04009087
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009088 err = btrfs_subvol_inherit_props(trans, new_root, parent_root);
9089 if (err)
9090 btrfs_err(new_root->fs_info,
David Sterba351fd352014-05-15 16:48:20 +02009091 "error inheriting subvolume %llu properties: %d",
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009092 new_root->root_key.objectid, err);
9093
Yan, Zheng76dda932009-09-21 16:00:26 -04009094 err = btrfs_update_inode(trans, new_root, inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04009095
Yan, Zheng76dda932009-09-21 16:00:26 -04009096 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07009097 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04009098}
9099
Chris Mason39279cc2007-06-12 06:35:45 -04009100struct inode *btrfs_alloc_inode(struct super_block *sb)
9101{
Josef Bacik69fe2d72017-10-19 14:15:57 -04009102 struct btrfs_fs_info *fs_info = btrfs_sb(sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009103 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009104 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009105
David Sterba712e36c2017-10-31 17:08:27 +01009106 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_KERNEL);
Chris Mason39279cc2007-06-12 06:35:45 -04009107 if (!ei)
9108 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009109
9110 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009111 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04009112 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04009113 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04009114 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009115 ei->delalloc_bytes = 0;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01009116 ei->new_delalloc_bytes = 0;
Wang Shilong47059d92014-07-03 18:22:07 +08009117 ei->defrag_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009118 ei->disk_i_size = 0;
9119 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04009120 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009121 ei->index_cnt = (u64)-1;
Miao Xie67de1172013-12-26 13:07:06 +08009122 ei->dir_index = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009123 ei->last_unlink_trans = 0;
Liu Bo46d8bc32012-08-29 01:07:55 -06009124 ei->last_log_commit = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009125
Josef Bacik9e0baf62011-07-15 15:16:44 +00009126 spin_lock_init(&ei->lock);
9127 ei->outstanding_extents = 0;
Josef Bacik69fe2d72017-10-19 14:15:57 -04009128 if (sb->s_magic != BTRFS_TEST_MAGIC)
9129 btrfs_init_metadata_block_rsv(fs_info, &ei->block_rsv,
9130 BTRFS_BLOCK_RSV_DELALLOC);
Josef Bacik72ac3c02012-05-23 14:13:11 -04009131 ei->runtime_flags = 0;
David Sterbab52aa8c2017-07-17 19:17:20 +02009132 ei->prop_compress = BTRFS_COMPRESS_NONE;
David Sterbaeec63c62017-07-17 19:41:31 +02009133 ei->defrag_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009134
Miao Xie16cdcec2011-04-22 18:12:22 +08009135 ei->delayed_node = NULL;
9136
chandan r9cc97d62012-07-04 12:48:07 +05309137 ei->i_otime.tv_sec = 0;
9138 ei->i_otime.tv_nsec = 0;
9139
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009140 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02009141 extent_map_tree_init(&ei->extent_tree);
Josef Bacikc6100a42017-05-05 11:57:13 -04009142 extent_io_tree_init(&ei->io_tree, inode);
9143 extent_io_tree_init(&ei->io_failure_tree, inode);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04009144 ei->io_tree.track_uptodate = 1;
9145 ei->io_failure_tree.track_uptodate = 1;
Josef Bacikb812ce22012-11-16 13:56:32 -05009146 atomic_set(&ei->sync_writers, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009147 mutex_init(&ei->log_mutex);
Josef Bacikf2486792012-01-13 12:09:22 -05009148 mutex_init(&ei->delalloc_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009149 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009150 INIT_LIST_HEAD(&ei->delalloc_inodes);
David Sterba8089fe62015-11-19 14:15:51 +01009151 INIT_LIST_HEAD(&ei->delayed_iput);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009152 RB_CLEAR_NODE(&ei->rb_node);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01009153 init_rwsem(&ei->dio_sem);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009154
9155 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009156}
9157
Josef Bacikaaedb552013-10-11 14:44:09 -04009158#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
9159void btrfs_test_destroy_inode(struct inode *inode)
9160{
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02009161 btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0);
Josef Bacikaaedb552013-10-11 14:44:09 -04009162 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
9163}
9164#endif
9165
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009166static void btrfs_i_callback(struct rcu_head *head)
9167{
9168 struct inode *inode = container_of(head, struct inode, i_rcu);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009169 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
9170}
9171
Chris Mason39279cc2007-06-12 06:35:45 -04009172void btrfs_destroy_inode(struct inode *inode)
9173{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009174 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009175 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009176 struct btrfs_root *root = BTRFS_I(inode)->root;
9177
Al Virob3d9b7a2012-06-09 13:51:19 -04009178 WARN_ON(!hlist_empty(&inode->i_dentry));
Chris Mason39279cc2007-06-12 06:35:45 -04009179 WARN_ON(inode->i_data.nrpages);
Josef Bacik69fe2d72017-10-19 14:15:57 -04009180 WARN_ON(BTRFS_I(inode)->block_rsv.reserved);
9181 WARN_ON(BTRFS_I(inode)->block_rsv.size);
Josef Bacik9e0baf62011-07-15 15:16:44 +00009182 WARN_ON(BTRFS_I(inode)->outstanding_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04009183 WARN_ON(BTRFS_I(inode)->delalloc_bytes);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01009184 WARN_ON(BTRFS_I(inode)->new_delalloc_bytes);
Josef Bacik7709cde2011-08-04 10:25:02 -04009185 WARN_ON(BTRFS_I(inode)->csum_bytes);
Wang Shilong47059d92014-07-03 18:22:07 +08009186 WARN_ON(BTRFS_I(inode)->defrag_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04009187
Chris Mason5a3f23d2009-03-31 13:27:11 -04009188 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05009189 * This can happen where we create an inode, but somebody else also
9190 * created the same inode and we need to destroy the one we already
9191 * created.
9192 */
9193 if (!root)
9194 goto free;
9195
Chris Masond3977122009-01-05 21:25:51 -05009196 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04009197 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
9198 if (!ordered)
9199 break;
9200 else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009201 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04009202 "found ordered extent %llu %llu on inode cleanup",
9203 ordered->file_offset, ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009204 btrfs_remove_ordered_extent(inode, ordered);
9205 btrfs_put_ordered_extent(ordered);
9206 btrfs_put_ordered_extent(ordered);
9207 }
9208 }
Qu Wenruo56fa9d02015-10-13 09:53:10 +08009209 btrfs_qgroup_check_reserved_leak(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04009210 inode_tree_del(inode);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02009211 btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0);
Josef Bacika6dbd422009-11-11 15:53:34 -05009212free:
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009213 call_rcu(&inode->i_rcu, btrfs_i_callback);
Chris Mason39279cc2007-06-12 06:35:45 -04009214}
9215
Al Viro45321ac2010-06-07 13:43:19 -04009216int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04009217{
9218 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04009219
Naohiro Aota6379ef92013-06-06 09:56:34 +00009220 if (root == NULL)
9221 return 1;
9222
Liu Bofa6ac872013-02-20 14:10:23 +00009223 /* the snap/subvol tree is on deleting */
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02009224 if (btrfs_root_refs(&root->root_item) == 0)
Al Viro45321ac2010-06-07 13:43:19 -04009225 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04009226 else
Al Viro45321ac2010-06-07 13:43:19 -04009227 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04009228}
9229
Sven Wegener0ee0fda2008-07-30 16:54:26 -04009230static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04009231{
9232 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
9233
9234 inode_init_once(&ei->vfs_inode);
9235}
9236
David Sterbae67c7182018-02-19 17:24:18 +01009237void __cold btrfs_destroy_cachep(void)
Chris Mason39279cc2007-06-12 06:35:45 -04009238{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10009239 /*
9240 * Make sure all delayed rcu free inodes are flushed before we
9241 * destroy cache.
9242 */
9243 rcu_barrier();
Kinglong Mee5598e902016-01-29 21:36:35 +08009244 kmem_cache_destroy(btrfs_inode_cachep);
9245 kmem_cache_destroy(btrfs_trans_handle_cachep);
Kinglong Mee5598e902016-01-29 21:36:35 +08009246 kmem_cache_destroy(btrfs_path_cachep);
9247 kmem_cache_destroy(btrfs_free_space_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04009248}
9249
Liu Bof5c29bd2017-11-02 17:21:50 -06009250int __init btrfs_init_cachep(void)
Chris Mason39279cc2007-06-12 06:35:45 -04009251{
David Sterba837e1972012-09-07 03:00:48 -06009252 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009253 sizeof(struct btrfs_inode), 0,
Vladimir Davydov5d097052016-01-14 15:18:21 -08009254 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD | SLAB_ACCOUNT,
9255 init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04009256 if (!btrfs_inode_cachep)
9257 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009258
David Sterba837e1972012-09-07 03:00:48 -06009259 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009260 sizeof(struct btrfs_trans_handle), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009261 SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009262 if (!btrfs_trans_handle_cachep)
9263 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009264
David Sterba837e1972012-09-07 03:00:48 -06009265 btrfs_path_cachep = kmem_cache_create("btrfs_path",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009266 sizeof(struct btrfs_path), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009267 SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009268 if (!btrfs_path_cachep)
9269 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009270
David Sterba837e1972012-09-07 03:00:48 -06009271 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
Josef Bacikdc89e982011-01-28 17:05:48 -05009272 sizeof(struct btrfs_free_space), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009273 SLAB_MEM_SPREAD, NULL);
Josef Bacikdc89e982011-01-28 17:05:48 -05009274 if (!btrfs_free_space_cachep)
9275 goto fail;
9276
Chris Mason39279cc2007-06-12 06:35:45 -04009277 return 0;
9278fail:
9279 btrfs_destroy_cachep();
9280 return -ENOMEM;
9281}
9282
David Howellsa528d352017-01-31 16:46:22 +00009283static int btrfs_getattr(const struct path *path, struct kstat *stat,
9284 u32 request_mask, unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04009285{
Miao Xiedf0af1a2013-01-29 10:11:59 +00009286 u64 delalloc_bytes;
David Howellsa528d352017-01-31 16:46:22 +00009287 struct inode *inode = d_inode(path->dentry);
David Sterbafadc0d82011-11-20 07:33:38 -05009288 u32 blocksize = inode->i_sb->s_blocksize;
Yonghong Song04a87e32017-05-12 15:07:43 -07009289 u32 bi_flags = BTRFS_I(inode)->flags;
9290
9291 stat->result_mask |= STATX_BTIME;
9292 stat->btime.tv_sec = BTRFS_I(inode)->i_otime.tv_sec;
9293 stat->btime.tv_nsec = BTRFS_I(inode)->i_otime.tv_nsec;
9294 if (bi_flags & BTRFS_INODE_APPEND)
9295 stat->attributes |= STATX_ATTR_APPEND;
9296 if (bi_flags & BTRFS_INODE_COMPRESS)
9297 stat->attributes |= STATX_ATTR_COMPRESSED;
9298 if (bi_flags & BTRFS_INODE_IMMUTABLE)
9299 stat->attributes |= STATX_ATTR_IMMUTABLE;
9300 if (bi_flags & BTRFS_INODE_NODUMP)
9301 stat->attributes |= STATX_ATTR_NODUMP;
9302
9303 stat->attributes_mask |= (STATX_ATTR_APPEND |
9304 STATX_ATTR_COMPRESSED |
9305 STATX_ATTR_IMMUTABLE |
9306 STATX_ATTR_NODUMP);
David Sterbafadc0d82011-11-20 07:33:38 -05009307
Chris Mason39279cc2007-06-12 06:35:45 -04009308 generic_fillattr(inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04009309 stat->dev = BTRFS_I(inode)->root->anon_dev;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009310
9311 spin_lock(&BTRFS_I(inode)->lock);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01009312 delalloc_bytes = BTRFS_I(inode)->new_delalloc_bytes;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009313 spin_unlock(&BTRFS_I(inode)->lock);
David Sterbafadc0d82011-11-20 07:33:38 -05009314 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
Miao Xiedf0af1a2013-01-29 10:11:59 +00009315 ALIGN(delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04009316 return 0;
9317}
9318
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009319static int btrfs_rename_exchange(struct inode *old_dir,
9320 struct dentry *old_dentry,
9321 struct inode *new_dir,
9322 struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04009323{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009324 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009325 struct btrfs_trans_handle *trans;
9326 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009327 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009328 struct inode *new_inode = new_dentry->d_inode;
9329 struct inode *old_inode = old_dentry->d_inode;
Deepa Dinamani95582b02018-05-08 19:36:02 -07009330 struct timespec64 ctime = current_time(old_inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009331 struct dentry *parent;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009332 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
9333 u64 new_ino = btrfs_ino(BTRFS_I(new_inode));
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009334 u64 old_idx = 0;
9335 u64 new_idx = 0;
9336 u64 root_objectid;
9337 int ret;
Filipe Manana86e8aa02016-05-05 02:02:27 +01009338 bool root_log_pinned = false;
9339 bool dest_log_pinned = false;
Filipe Mananad4682ba2018-06-11 19:24:28 +01009340 struct btrfs_log_ctx ctx_root;
9341 struct btrfs_log_ctx ctx_dest;
9342 bool sync_log_root = false;
9343 bool sync_log_dest = false;
9344 bool commit_transaction = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009345
9346 /* we only allow rename subvolume link between subvolumes */
9347 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
9348 return -EXDEV;
9349
Filipe Mananad4682ba2018-06-11 19:24:28 +01009350 btrfs_init_log_ctx(&ctx_root, old_inode);
9351 btrfs_init_log_ctx(&ctx_dest, new_inode);
9352
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009353 /* close the race window with snapshot create/destroy ioctl */
9354 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009355 down_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009356 if (new_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009357 down_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009358
9359 /*
9360 * We want to reserve the absolute worst case amount of items. So if
9361 * both inodes are subvols and we need to unlink them then that would
9362 * require 4 item modifications, but if they are both normal inodes it
9363 * would require 5 item modifications, so we'll assume their normal
9364 * inodes. So 5 * 2 is 10, plus 2 for the new links, so 12 total items
9365 * should cover the worst case number of items we'll modify.
9366 */
9367 trans = btrfs_start_transaction(root, 12);
9368 if (IS_ERR(trans)) {
9369 ret = PTR_ERR(trans);
9370 goto out_notrans;
9371 }
9372
9373 /*
9374 * We need to find a free sequence number both in the source and
9375 * in the destination directory for the exchange.
9376 */
Nikolay Borisov877574e2017-02-20 13:50:33 +02009377 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009378 if (ret)
9379 goto out_fail;
Nikolay Borisov877574e2017-02-20 13:50:33 +02009380 ret = btrfs_set_inode_index(BTRFS_I(old_dir), &new_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009381 if (ret)
9382 goto out_fail;
9383
9384 BTRFS_I(old_inode)->dir_index = 0ULL;
9385 BTRFS_I(new_inode)->dir_index = 0ULL;
9386
9387 /* Reference for the source. */
9388 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
9389 /* force full log commit if subvolume involved. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009390 btrfs_set_log_full_commit(fs_info, trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009391 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01009392 btrfs_pin_log_trans(root);
9393 root_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009394 ret = btrfs_insert_inode_ref(trans, dest,
9395 new_dentry->d_name.name,
9396 new_dentry->d_name.len,
9397 old_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01009398 btrfs_ino(BTRFS_I(new_dir)),
9399 old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009400 if (ret)
9401 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009402 }
9403
9404 /* And now for the dest. */
9405 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
9406 /* force full log commit if subvolume involved. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009407 btrfs_set_log_full_commit(fs_info, trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009408 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01009409 btrfs_pin_log_trans(dest);
9410 dest_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009411 ret = btrfs_insert_inode_ref(trans, root,
9412 old_dentry->d_name.name,
9413 old_dentry->d_name.len,
9414 new_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01009415 btrfs_ino(BTRFS_I(old_dir)),
9416 new_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009417 if (ret)
9418 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009419 }
9420
9421 /* Update inode version and ctime/mtime. */
9422 inode_inc_iversion(old_dir);
9423 inode_inc_iversion(new_dir);
9424 inode_inc_iversion(old_inode);
9425 inode_inc_iversion(new_inode);
9426 old_dir->i_ctime = old_dir->i_mtime = ctime;
9427 new_dir->i_ctime = new_dir->i_mtime = ctime;
9428 old_inode->i_ctime = ctime;
9429 new_inode->i_ctime = ctime;
9430
9431 if (old_dentry->d_parent != new_dentry->d_parent) {
David Sterbaf85b7372017-01-20 14:54:07 +01009432 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
9433 BTRFS_I(old_inode), 1);
9434 btrfs_record_unlink_dir(trans, BTRFS_I(new_dir),
9435 BTRFS_I(new_inode), 1);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009436 }
9437
9438 /* src is a subvolume */
9439 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
9440 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
Lu Fengqi401b3b12018-08-01 11:32:30 +08009441 ret = btrfs_unlink_subvol(trans, old_dir, root_objectid,
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009442 old_dentry->d_name.name,
9443 old_dentry->d_name.len);
9444 } else { /* src is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009445 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
9446 BTRFS_I(old_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009447 old_dentry->d_name.name,
9448 old_dentry->d_name.len);
9449 if (!ret)
9450 ret = btrfs_update_inode(trans, root, old_inode);
9451 }
9452 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009453 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009454 goto out_fail;
9455 }
9456
9457 /* dest is a subvolume */
9458 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
9459 root_objectid = BTRFS_I(new_inode)->root->root_key.objectid;
Lu Fengqi401b3b12018-08-01 11:32:30 +08009460 ret = btrfs_unlink_subvol(trans, new_dir, root_objectid,
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009461 new_dentry->d_name.name,
9462 new_dentry->d_name.len);
9463 } else { /* dest is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009464 ret = __btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
9465 BTRFS_I(new_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009466 new_dentry->d_name.name,
9467 new_dentry->d_name.len);
9468 if (!ret)
9469 ret = btrfs_update_inode(trans, dest, new_inode);
9470 }
9471 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009472 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009473 goto out_fail;
9474 }
9475
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009476 ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009477 new_dentry->d_name.name,
9478 new_dentry->d_name.len, 0, old_idx);
9479 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009480 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009481 goto out_fail;
9482 }
9483
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009484 ret = btrfs_add_link(trans, BTRFS_I(old_dir), BTRFS_I(new_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009485 old_dentry->d_name.name,
9486 old_dentry->d_name.len, 0, new_idx);
9487 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009488 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009489 goto out_fail;
9490 }
9491
9492 if (old_inode->i_nlink == 1)
9493 BTRFS_I(old_inode)->dir_index = old_idx;
9494 if (new_inode->i_nlink == 1)
9495 BTRFS_I(new_inode)->dir_index = new_idx;
9496
Filipe Manana86e8aa02016-05-05 02:02:27 +01009497 if (root_log_pinned) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009498 parent = new_dentry->d_parent;
Filipe Mananad4682ba2018-06-11 19:24:28 +01009499 ret = btrfs_log_new_name(trans, BTRFS_I(old_inode),
9500 BTRFS_I(old_dir), parent,
9501 false, &ctx_root);
9502 if (ret == BTRFS_NEED_LOG_SYNC)
9503 sync_log_root = true;
9504 else if (ret == BTRFS_NEED_TRANS_COMMIT)
9505 commit_transaction = true;
9506 ret = 0;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009507 btrfs_end_log_trans(root);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009508 root_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009509 }
Filipe Manana86e8aa02016-05-05 02:02:27 +01009510 if (dest_log_pinned) {
Filipe Mananad4682ba2018-06-11 19:24:28 +01009511 if (!commit_transaction) {
9512 parent = old_dentry->d_parent;
9513 ret = btrfs_log_new_name(trans, BTRFS_I(new_inode),
9514 BTRFS_I(new_dir), parent,
9515 false, &ctx_dest);
9516 if (ret == BTRFS_NEED_LOG_SYNC)
9517 sync_log_dest = true;
9518 else if (ret == BTRFS_NEED_TRANS_COMMIT)
9519 commit_transaction = true;
9520 ret = 0;
9521 }
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009522 btrfs_end_log_trans(dest);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009523 dest_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009524 }
9525out_fail:
Filipe Manana86e8aa02016-05-05 02:02:27 +01009526 /*
9527 * If we have pinned a log and an error happened, we unpin tasks
9528 * trying to sync the log and force them to fallback to a transaction
9529 * commit if the log currently contains any of the inodes involved in
9530 * this rename operation (to ensure we do not persist a log with an
9531 * inconsistent state for any of these inodes or leading to any
9532 * inconsistencies when replayed). If the transaction was aborted, the
9533 * abortion reason is propagated to userspace when attempting to commit
9534 * the transaction. If the log does not contain any of these inodes, we
9535 * allow the tasks to sync it.
9536 */
9537 if (ret && (root_log_pinned || dest_log_pinned)) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009538 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
9539 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
9540 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana86e8aa02016-05-05 02:02:27 +01009541 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009542 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009543 btrfs_set_log_full_commit(fs_info, trans);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009544
9545 if (root_log_pinned) {
9546 btrfs_end_log_trans(root);
9547 root_log_pinned = false;
9548 }
9549 if (dest_log_pinned) {
9550 btrfs_end_log_trans(dest);
9551 dest_log_pinned = false;
9552 }
9553 }
Filipe Mananad4682ba2018-06-11 19:24:28 +01009554 if (!ret && sync_log_root && !commit_transaction) {
9555 ret = btrfs_sync_log(trans, BTRFS_I(old_inode)->root,
9556 &ctx_root);
9557 if (ret)
9558 commit_transaction = true;
9559 }
9560 if (!ret && sync_log_dest && !commit_transaction) {
9561 ret = btrfs_sync_log(trans, BTRFS_I(new_inode)->root,
9562 &ctx_dest);
9563 if (ret)
9564 commit_transaction = true;
9565 }
9566 if (commit_transaction) {
9567 ret = btrfs_commit_transaction(trans);
9568 } else {
9569 int ret2;
9570
9571 ret2 = btrfs_end_transaction(trans);
9572 ret = ret ? ret : ret2;
9573 }
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009574out_notrans:
9575 if (new_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009576 up_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009577 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009578 up_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009579
9580 return ret;
9581}
9582
9583static int btrfs_whiteout_for_rename(struct btrfs_trans_handle *trans,
9584 struct btrfs_root *root,
9585 struct inode *dir,
9586 struct dentry *dentry)
9587{
9588 int ret;
9589 struct inode *inode;
9590 u64 objectid;
9591 u64 index;
9592
9593 ret = btrfs_find_free_ino(root, &objectid);
9594 if (ret)
9595 return ret;
9596
9597 inode = btrfs_new_inode(trans, root, dir,
9598 dentry->d_name.name,
9599 dentry->d_name.len,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009600 btrfs_ino(BTRFS_I(dir)),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009601 objectid,
9602 S_IFCHR | WHITEOUT_MODE,
9603 &index);
9604
9605 if (IS_ERR(inode)) {
9606 ret = PTR_ERR(inode);
9607 return ret;
9608 }
9609
9610 inode->i_op = &btrfs_special_inode_operations;
9611 init_special_inode(inode, inode->i_mode,
9612 WHITEOUT_DEV);
9613
9614 ret = btrfs_init_inode_security(trans, inode, dir,
9615 &dentry->d_name);
9616 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009617 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009618
Nikolay Borisovcef415a2017-02-20 13:51:09 +02009619 ret = btrfs_add_nondir(trans, BTRFS_I(dir), dentry,
9620 BTRFS_I(inode), 0, index);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009621 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009622 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009623
9624 ret = btrfs_update_inode(trans, root, inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009625out:
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009626 unlock_new_inode(inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009627 if (ret)
9628 inode_dec_link_count(inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009629 iput(inode);
9630
Filipe Mananac9901612016-05-05 01:41:57 +01009631 return ret;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009632}
9633
Chris Mason39279cc2007-06-12 06:35:45 -04009634static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009635 struct inode *new_dir, struct dentry *new_dentry,
9636 unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04009637{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009638 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009639 struct btrfs_trans_handle *trans;
Filipe Manana5062af32016-05-05 10:26:26 +01009640 unsigned int trans_num_items;
Chris Mason39279cc2007-06-12 06:35:45 -04009641 struct btrfs_root *root = BTRFS_I(old_dir)->root;
9642 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00009643 struct inode *new_inode = d_inode(new_dentry);
9644 struct inode *old_inode = d_inode(old_dentry);
Chris Mason00e4e6b2008-08-05 11:18:09 -04009645 u64 index = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009646 u64 root_objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04009647 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009648 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009649 bool log_pinned = false;
Filipe Mananad4682ba2018-06-11 19:24:28 +01009650 struct btrfs_log_ctx ctx;
9651 bool sync_log = false;
9652 bool commit_transaction = false;
Chris Mason39279cc2007-06-12 06:35:45 -04009653
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009654 if (btrfs_ino(BTRFS_I(new_dir)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04009655 return -EPERM;
9656
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009657 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08009658 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05009659 return -EXDEV;
9660
Li Zefan33345d012011-04-20 10:31:50 +08009661 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009662 (new_inode && btrfs_ino(BTRFS_I(new_inode)) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009663 return -ENOTEMPTY;
9664
Chris Mason39279cc2007-06-12 06:35:45 -04009665 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009666 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04009667 return -ENOTEMPTY;
Chris Mason9c520572012-12-17 14:26:57 -05009668
9669
9670 /* check for collisions, even if the name isn't there */
Josef Bacik4871c152013-10-09 12:24:04 -04009671 ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino,
Chris Mason9c520572012-12-17 14:26:57 -05009672 new_dentry->d_name.name,
9673 new_dentry->d_name.len);
9674
9675 if (ret) {
9676 if (ret == -EEXIST) {
9677 /* we shouldn't get
9678 * eexist without a new_inode */
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05309679 if (WARN_ON(!new_inode)) {
Chris Mason9c520572012-12-17 14:26:57 -05009680 return ret;
9681 }
9682 } else {
9683 /* maybe -EOVERFLOW */
9684 return ret;
9685 }
9686 }
9687 ret = 0;
9688
Chris Mason5a3f23d2009-03-31 13:27:11 -04009689 /*
Chris Mason8d875f92014-08-12 10:47:42 -07009690 * we're using rename to replace one file with another. Start IO on it
9691 * now so we don't add too much work to the end of the transaction
Chris Mason5a3f23d2009-03-31 13:27:11 -04009692 */
Chris Mason8d875f92014-08-12 10:47:42 -07009693 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size)
Chris Mason5a3f23d2009-03-31 13:27:11 -04009694 filemap_flush(old_inode->i_mapping);
9695
Yan, Zheng76dda932009-09-21 16:00:26 -04009696 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08009697 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009698 down_read(&fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04009699 /*
9700 * We want to reserve the absolute worst case amount of items. So if
9701 * both inodes are subvols and we need to unlink them then that would
9702 * require 4 item modifications, but if they are both normal inodes it
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009703 * would require 5 item modifications, so we'll assume they are normal
Yan, Zhenga22285a2010-05-16 10:48:46 -04009704 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
9705 * should cover the worst case number of items we'll modify.
Filipe Manana5062af32016-05-05 10:26:26 +01009706 * If our rename has the whiteout flag, we need more 5 units for the
9707 * new inode (1 inode item, 1 inode ref, 2 dir items and 1 xattr item
9708 * when selinux is enabled).
Yan, Zhenga22285a2010-05-16 10:48:46 -04009709 */
Filipe Manana5062af32016-05-05 10:26:26 +01009710 trans_num_items = 11;
9711 if (flags & RENAME_WHITEOUT)
9712 trans_num_items += 5;
9713 trans = btrfs_start_transaction(root, trans_num_items);
Johann Lombardib44c59a2011-03-31 13:23:47 +00009714 if (IS_ERR(trans)) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009715 ret = PTR_ERR(trans);
9716 goto out_notrans;
9717 }
Chris Mason5f39d392007-10-15 16:14:19 -04009718
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009719 if (dest != root)
9720 btrfs_record_root_in_trans(trans, dest);
9721
Nikolay Borisov877574e2017-02-20 13:50:33 +02009722 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009723 if (ret)
9724 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009725
Miao Xie67de1172013-12-26 13:07:06 +08009726 BTRFS_I(old_inode)->dir_index = 0ULL;
Li Zefan33345d012011-04-20 10:31:50 +08009727 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009728 /* force full log commit if subvolume involved. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009729 btrfs_set_log_full_commit(fs_info, trans);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009730 } else {
Filipe Mananac4aba952016-04-29 13:14:42 +01009731 btrfs_pin_log_trans(root);
9732 log_pinned = true;
Yan, Zhenga5719522009-09-24 09:17:31 -04009733 ret = btrfs_insert_inode_ref(trans, dest,
9734 new_dentry->d_name.name,
9735 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08009736 old_ino,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009737 btrfs_ino(BTRFS_I(new_dir)), index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009738 if (ret)
9739 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009740 }
Chris Mason5a3f23d2009-03-31 13:27:11 -04009741
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009742 inode_inc_iversion(old_dir);
9743 inode_inc_iversion(new_dir);
9744 inode_inc_iversion(old_inode);
Deepa Dinamani04b285f2016-02-06 23:57:21 -08009745 old_dir->i_ctime = old_dir->i_mtime =
9746 new_dir->i_ctime = new_dir->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009747 old_inode->i_ctime = current_time(old_dir);
Chris Mason5f39d392007-10-15 16:14:19 -04009748
Chris Mason12fcfd22009-03-24 10:24:20 -04009749 if (old_dentry->d_parent != new_dentry->d_parent)
David Sterbaf85b7372017-01-20 14:54:07 +01009750 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
9751 BTRFS_I(old_inode), 1);
Chris Mason12fcfd22009-03-24 10:24:20 -04009752
Li Zefan33345d012011-04-20 10:31:50 +08009753 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009754 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
Lu Fengqi401b3b12018-08-01 11:32:30 +08009755 ret = btrfs_unlink_subvol(trans, old_dir, root_objectid,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009756 old_dentry->d_name.name,
9757 old_dentry->d_name.len);
9758 } else {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009759 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
9760 BTRFS_I(d_inode(old_dentry)),
Al Viro92986792011-03-04 17:14:37 +00009761 old_dentry->d_name.name,
9762 old_dentry->d_name.len);
9763 if (!ret)
9764 ret = btrfs_update_inode(trans, root, old_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009765 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009766 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009767 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009768 goto out_fail;
9769 }
Chris Mason39279cc2007-06-12 06:35:45 -04009770
9771 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009772 inode_inc_iversion(new_inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009773 new_inode->i_ctime = current_time(new_inode);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009774 if (unlikely(btrfs_ino(BTRFS_I(new_inode)) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009775 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
9776 root_objectid = BTRFS_I(new_inode)->location.objectid;
Lu Fengqi401b3b12018-08-01 11:32:30 +08009777 ret = btrfs_unlink_subvol(trans, new_dir, root_objectid,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009778 new_dentry->d_name.name,
9779 new_dentry->d_name.len);
9780 BUG_ON(new_inode->i_nlink == 0);
9781 } else {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009782 ret = btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
9783 BTRFS_I(d_inode(new_dentry)),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009784 new_dentry->d_name.name,
9785 new_dentry->d_name.len);
9786 }
Josef Bacik4ef31a42013-08-13 14:10:08 -04009787 if (!ret && new_inode->i_nlink == 0)
Nikolay Borisov73f2e542017-02-20 13:50:59 +02009788 ret = btrfs_orphan_add(trans,
9789 BTRFS_I(d_inode(new_dentry)));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009790 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009791 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009792 goto out_fail;
9793 }
Chris Mason39279cc2007-06-12 06:35:45 -04009794 }
Josef Bacikaec74772008-07-24 12:12:38 -04009795
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009796 ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009797 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04009798 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009799 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009800 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009801 goto out_fail;
9802 }
Chris Mason39279cc2007-06-12 06:35:45 -04009803
Miao Xie67de1172013-12-26 13:07:06 +08009804 if (old_inode->i_nlink == 1)
9805 BTRFS_I(old_inode)->dir_index = index;
9806
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009807 if (log_pinned) {
Al Viro10d9f302011-07-16 23:09:10 -04009808 struct dentry *parent = new_dentry->d_parent;
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009809
Filipe Mananad4682ba2018-06-11 19:24:28 +01009810 btrfs_init_log_ctx(&ctx, old_inode);
9811 ret = btrfs_log_new_name(trans, BTRFS_I(old_inode),
9812 BTRFS_I(old_dir), parent,
9813 false, &ctx);
9814 if (ret == BTRFS_NEED_LOG_SYNC)
9815 sync_log = true;
9816 else if (ret == BTRFS_NEED_TRANS_COMMIT)
9817 commit_transaction = true;
9818 ret = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009819 btrfs_end_log_trans(root);
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009820 log_pinned = false;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009821 }
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009822
9823 if (flags & RENAME_WHITEOUT) {
9824 ret = btrfs_whiteout_for_rename(trans, root, old_dir,
9825 old_dentry);
9826
9827 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009828 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009829 goto out_fail;
9830 }
Chris Mason12fcfd22009-03-24 10:24:20 -04009831 }
Chris Mason39279cc2007-06-12 06:35:45 -04009832out_fail:
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009833 /*
9834 * If we have pinned the log and an error happened, we unpin tasks
9835 * trying to sync the log and force them to fallback to a transaction
9836 * commit if the log currently contains any of the inodes involved in
9837 * this rename operation (to ensure we do not persist a log with an
9838 * inconsistent state for any of these inodes or leading to any
9839 * inconsistencies when replayed). If the transaction was aborted, the
9840 * abortion reason is propagated to userspace when attempting to commit
9841 * the transaction. If the log does not contain any of these inodes, we
9842 * allow the tasks to sync it.
9843 */
9844 if (ret && log_pinned) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009845 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
9846 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
9847 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009848 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009849 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009850 btrfs_set_log_full_commit(fs_info, trans);
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009851
9852 btrfs_end_log_trans(root);
9853 log_pinned = false;
9854 }
Filipe Mananad4682ba2018-06-11 19:24:28 +01009855 if (!ret && sync_log) {
9856 ret = btrfs_sync_log(trans, BTRFS_I(old_inode)->root, &ctx);
9857 if (ret)
9858 commit_transaction = true;
9859 }
9860 if (commit_transaction) {
9861 ret = btrfs_commit_transaction(trans);
9862 } else {
9863 int ret2;
9864
9865 ret2 = btrfs_end_transaction(trans);
9866 ret = ret ? ret : ret2;
9867 }
Johann Lombardib44c59a2011-03-31 13:23:47 +00009868out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +08009869 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009870 up_read(&fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009871
Chris Mason39279cc2007-06-12 06:35:45 -04009872 return ret;
9873}
9874
Miklos Szeredi80ace852014-07-23 15:15:32 +02009875static int btrfs_rename2(struct inode *old_dir, struct dentry *old_dentry,
9876 struct inode *new_dir, struct dentry *new_dentry,
9877 unsigned int flags)
9878{
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009879 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
Miklos Szeredi80ace852014-07-23 15:15:32 +02009880 return -EINVAL;
9881
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009882 if (flags & RENAME_EXCHANGE)
9883 return btrfs_rename_exchange(old_dir, old_dentry, new_dir,
9884 new_dentry);
9885
9886 return btrfs_rename(old_dir, old_dentry, new_dir, new_dentry, flags);
Miklos Szeredi80ace852014-07-23 15:15:32 +02009887}
9888
Nikolay Borisov3a2f8c02018-04-24 17:23:59 +03009889struct btrfs_delalloc_work {
9890 struct inode *inode;
9891 struct completion completion;
9892 struct list_head list;
9893 struct btrfs_work work;
9894};
9895
Miao Xie8ccf6f192012-10-25 09:28:04 +00009896static void btrfs_run_delalloc_work(struct btrfs_work *work)
9897{
9898 struct btrfs_delalloc_work *delalloc_work;
Josef Bacik9f23e282013-10-28 15:03:41 -04009899 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009900
9901 delalloc_work = container_of(work, struct btrfs_delalloc_work,
9902 work);
Josef Bacik9f23e282013-10-28 15:03:41 -04009903 inode = delalloc_work->inode;
David Sterba30424602015-11-27 19:27:11 +01009904 filemap_flush(inode->i_mapping);
9905 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
9906 &BTRFS_I(inode)->runtime_flags))
Josef Bacik9f23e282013-10-28 15:03:41 -04009907 filemap_flush(inode->i_mapping);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009908
Nikolay Borisov076da912018-04-23 10:54:16 +03009909 iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009910 complete(&delalloc_work->completion);
9911}
9912
Nikolay Borisov3a2f8c02018-04-24 17:23:59 +03009913static struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode)
Miao Xie8ccf6f192012-10-25 09:28:04 +00009914{
9915 struct btrfs_delalloc_work *work;
9916
David Sterba100d5702015-12-08 14:39:32 +01009917 work = kmalloc(sizeof(*work), GFP_NOFS);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009918 if (!work)
9919 return NULL;
9920
9921 init_completion(&work->completion);
9922 INIT_LIST_HEAD(&work->list);
9923 work->inode = inode;
Liu Bo9e0af232014-08-15 23:36:53 +08009924 WARN_ON_ONCE(!inode);
9925 btrfs_init_work(&work->work, btrfs_flush_delalloc_helper,
9926 btrfs_run_delalloc_work, NULL, NULL);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009927
9928 return work;
9929}
9930
Chris Masond352ac62008-09-29 15:18:18 -04009931/*
9932 * some fairly slow code that needs optimization. This walks the list
9933 * of all the inodes with pending delalloc and forces them to disk.
9934 */
Ethan Lien3cd24c62018-11-01 14:49:03 +08009935static int start_delalloc_inodes(struct btrfs_root *root, int nr, bool snapshot)
Chris Masonea8c2812008-08-04 23:17:27 -04009936{
Chris Masonea8c2812008-08-04 23:17:27 -04009937 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009938 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009939 struct btrfs_delalloc_work *work, *next;
9940 struct list_head works;
Miao Xie1eafa6c2013-01-22 10:49:00 +00009941 struct list_head splice;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009942 int ret = 0;
Chris Masonea8c2812008-08-04 23:17:27 -04009943
Miao Xie8ccf6f192012-10-25 09:28:04 +00009944 INIT_LIST_HEAD(&works);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009945 INIT_LIST_HEAD(&splice);
Miao Xie63607cc2013-01-22 10:50:35 +00009946
Miao Xie573bfb72014-03-06 13:55:03 +08009947 mutex_lock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009948 spin_lock(&root->delalloc_lock);
9949 list_splice_init(&root->delalloc_inodes, &splice);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009950 while (!list_empty(&splice)) {
9951 binode = list_entry(splice.next, struct btrfs_inode,
Chris Masonea8c2812008-08-04 23:17:27 -04009952 delalloc_inodes);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009953
Miao Xieeb73c1b2013-05-15 07:48:22 +00009954 list_move_tail(&binode->delalloc_inodes,
9955 &root->delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009956 inode = igrab(&binode->vfs_inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00009957 if (!inode) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00009958 cond_resched_lock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009959 continue;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009960 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00009961 spin_unlock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009962
Ethan Lien3cd24c62018-11-01 14:49:03 +08009963 if (snapshot)
9964 set_bit(BTRFS_INODE_SNAPSHOT_FLUSH,
9965 &binode->runtime_flags);
Nikolay Borisov076da912018-04-23 10:54:16 +03009966 work = btrfs_alloc_delalloc_work(inode);
David Sterba5d99a9982014-09-29 19:20:37 +02009967 if (!work) {
Nikolay Borisov4fbb5142018-04-23 10:54:15 +03009968 iput(inode);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009969 ret = -ENOMEM;
Wang Shilonga1ecaab2014-04-02 19:53:32 +08009970 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009971 }
Miao Xie1eafa6c2013-01-22 10:49:00 +00009972 list_add_tail(&work->list, &works);
Qu Wenruoa44903a2014-02-28 10:46:09 +08009973 btrfs_queue_work(root->fs_info->flush_workers,
9974 &work->work);
Miao Xie6c255e62014-03-06 13:55:01 +08009975 ret++;
9976 if (nr != -1 && ret >= nr)
Wang Shilonga1ecaab2014-04-02 19:53:32 +08009977 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009978 cond_resched();
Miao Xieeb73c1b2013-05-15 07:48:22 +00009979 spin_lock(&root->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04009980 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00009981 spin_unlock(&root->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -04009982
Wang Shilonga1ecaab2014-04-02 19:53:32 +08009983out:
Miao Xie1eafa6c2013-01-22 10:49:00 +00009984 list_for_each_entry_safe(work, next, &works, list) {
9985 list_del_init(&work->list);
Nikolay Borisov40012f92018-04-19 10:46:39 +03009986 wait_for_completion(&work->completion);
9987 kfree(work);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009988 }
9989
Nikolay Borisov81f1d392018-04-19 10:46:37 +03009990 if (!list_empty(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00009991 spin_lock(&root->delalloc_lock);
9992 list_splice_tail(&splice, &root->delalloc_inodes);
9993 spin_unlock(&root->delalloc_lock);
9994 }
Miao Xie573bfb72014-03-06 13:55:03 +08009995 mutex_unlock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009996 return ret;
9997}
9998
Ethan Lien3cd24c62018-11-01 14:49:03 +08009999int btrfs_start_delalloc_snapshot(struct btrfs_root *root)
Miao Xieeb73c1b2013-05-15 07:48:22 +000010000{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010001 struct btrfs_fs_info *fs_info = root->fs_info;
Miao Xieeb73c1b2013-05-15 07:48:22 +000010002 int ret;
10003
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010004 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +000010005 return -EROFS;
10006
Ethan Lien3cd24c62018-11-01 14:49:03 +080010007 ret = start_delalloc_inodes(root, -1, true);
Miao Xie6c255e62014-03-06 13:55:01 +080010008 if (ret > 0)
10009 ret = 0;
Miao Xieeb73c1b2013-05-15 07:48:22 +000010010 return ret;
10011}
10012
Nikolay Borisov82b3e532018-04-23 10:54:13 +030010013int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, int nr)
Miao Xieeb73c1b2013-05-15 07:48:22 +000010014{
10015 struct btrfs_root *root;
10016 struct list_head splice;
10017 int ret;
10018
Wang Shilong2c21b4d2014-01-14 19:42:20 +080010019 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +000010020 return -EROFS;
10021
10022 INIT_LIST_HEAD(&splice);
10023
Miao Xie573bfb72014-03-06 13:55:03 +080010024 mutex_lock(&fs_info->delalloc_root_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010025 spin_lock(&fs_info->delalloc_root_lock);
10026 list_splice_init(&fs_info->delalloc_roots, &splice);
Miao Xie6c255e62014-03-06 13:55:01 +080010027 while (!list_empty(&splice) && nr) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010028 root = list_first_entry(&splice, struct btrfs_root,
10029 delalloc_root);
10030 root = btrfs_grab_fs_root(root);
10031 BUG_ON(!root);
10032 list_move_tail(&root->delalloc_root,
10033 &fs_info->delalloc_roots);
10034 spin_unlock(&fs_info->delalloc_root_lock);
10035
Ethan Lien3cd24c62018-11-01 14:49:03 +080010036 ret = start_delalloc_inodes(root, nr, false);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010037 btrfs_put_fs_root(root);
Miao Xie6c255e62014-03-06 13:55:01 +080010038 if (ret < 0)
Miao Xieeb73c1b2013-05-15 07:48:22 +000010039 goto out;
10040
Miao Xie6c255e62014-03-06 13:55:01 +080010041 if (nr != -1) {
10042 nr -= ret;
10043 WARN_ON(nr < 0);
10044 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010045 spin_lock(&fs_info->delalloc_root_lock);
10046 }
10047 spin_unlock(&fs_info->delalloc_root_lock);
10048
Miao Xie6c255e62014-03-06 13:55:01 +080010049 ret = 0;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010050out:
Nikolay Borisov81f1d392018-04-19 10:46:37 +030010051 if (!list_empty(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010052 spin_lock(&fs_info->delalloc_root_lock);
10053 list_splice_tail(&splice, &fs_info->delalloc_roots);
10054 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010055 }
Miao Xie573bfb72014-03-06 13:55:03 +080010056 mutex_unlock(&fs_info->delalloc_root_mutex);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010057 return ret;
Chris Masonea8c2812008-08-04 23:17:27 -040010058}
10059
Chris Mason39279cc2007-06-12 06:35:45 -040010060static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
10061 const char *symname)
10062{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010063 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -040010064 struct btrfs_trans_handle *trans;
10065 struct btrfs_root *root = BTRFS_I(dir)->root;
10066 struct btrfs_path *path;
10067 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -050010068 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -040010069 int err;
Chris Mason39279cc2007-06-12 06:35:45 -040010070 u64 objectid;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +053010071 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -040010072 int name_len;
10073 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -040010074 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -040010075 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -040010076 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -040010077
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +010010078 name_len = strlen(symname);
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010079 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info))
Chris Mason39279cc2007-06-12 06:35:45 -040010080 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -050010081
Josef Bacik9ed74f22009-09-11 16:12:44 -040010082 /*
10083 * 2 items for inode item and ref
10084 * 2 items for dir items
Filipe Manana9269d122015-12-31 18:16:29 +000010085 * 1 item for updating parent inode item
10086 * 1 item for the inline extent item
Josef Bacik9ed74f22009-09-11 16:12:44 -040010087 * 1 item for xattr if selinux is on
10088 */
Filipe Manana9269d122015-12-31 18:16:29 +000010089 trans = btrfs_start_transaction(root, 7);
Yan, Zhenga22285a2010-05-16 10:48:46 -040010090 if (IS_ERR(trans))
10091 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -050010092
Li Zefan581bb052011-04-20 10:06:11 +080010093 err = btrfs_find_free_ino(root, &objectid);
10094 if (err)
10095 goto out_unlock;
10096
Josef Bacikaec74772008-07-24 12:12:38 -040010097 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +010010098 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)),
10099 objectid, S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -040010100 if (IS_ERR(inode)) {
10101 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -040010102 inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -040010103 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -040010104 }
Chris Mason39279cc2007-06-12 06:35:45 -040010105
Casey Schauflerad19db72011-12-15 10:09:07 -050010106 /*
10107 * If the active LSM wants to access the inode during
10108 * d_instantiate it needs these. Smack checks to see
10109 * if the filesystem supports xattrs by looking at the
10110 * ops vector.
10111 */
10112 inode->i_fop = &btrfs_file_operations;
10113 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -070010114 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -070010115 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
10116
10117 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
10118 if (err)
Al Viro32955c52018-05-16 12:20:05 -040010119 goto out_unlock;
Casey Schauflerad19db72011-12-15 10:09:07 -050010120
Chris Mason39279cc2007-06-12 06:35:45 -040010121 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -070010122 if (!path) {
10123 err = -ENOMEM;
Al Viro32955c52018-05-16 12:20:05 -040010124 goto out_unlock;
Mark Fashehd8926bb2011-07-13 10:38:47 -070010125 }
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +020010126 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason39279cc2007-06-12 06:35:45 -040010127 key.offset = 0;
David Sterba962a2982014-06-04 18:41:45 +020010128 key.type = BTRFS_EXTENT_DATA_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -040010129 datasize = btrfs_file_extent_calc_inline_size(name_len);
10130 err = btrfs_insert_empty_item(trans, root, path, &key,
10131 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -040010132 if (err) {
Julia Lawallb0839162011-05-14 07:10:51 +000010133 btrfs_free_path(path);
Al Viro32955c52018-05-16 12:20:05 -040010134 goto out_unlock;
Chris Mason54aa1f42007-06-22 14:16:25 -040010135 }
Chris Mason5f39d392007-10-15 16:14:19 -040010136 leaf = path->nodes[0];
10137 ei = btrfs_item_ptr(leaf, path->slots[0],
10138 struct btrfs_file_extent_item);
10139 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
10140 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -040010141 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -040010142 btrfs_set_file_extent_encryption(leaf, ei, 0);
10143 btrfs_set_file_extent_compression(leaf, ei, 0);
10144 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
10145 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
10146
Chris Mason39279cc2007-06-12 06:35:45 -040010147 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -040010148 write_extent_buffer(leaf, symname, ptr, name_len);
10149 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -040010150 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -040010151
Chris Mason39279cc2007-06-12 06:35:45 -040010152 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -050010153 inode_nohighmem(inode);
Omar Sandoval4779cc02018-09-24 15:16:55 -070010154 inode->i_mapping->a_ops = &btrfs_aops;
Yan Zhengd899e052008-10-30 14:25:28 -040010155 inode_set_bytes(inode, name_len);
Nikolay Borisov6ef06d22017-02-20 13:50:34 +020010156 btrfs_i_size_write(BTRFS_I(inode), name_len);
Chris Mason54aa1f42007-06-22 14:16:25 -040010157 err = btrfs_update_inode(trans, root, inode);
Filipe Mananad50866d2015-12-31 18:08:24 +000010158 /*
10159 * Last step, add directory indexes for our symlink inode. This is the
10160 * last step to avoid extra cleanup of these indexes if an error happens
10161 * elsewhere above.
10162 */
10163 if (!err)
Nikolay Borisovcef415a2017-02-20 13:51:09 +020010164 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry,
10165 BTRFS_I(inode), 0, index);
Al Viro32955c52018-05-16 12:20:05 -040010166 if (err)
10167 goto out_unlock;
Chris Masonb0d5d102014-09-08 13:08:51 -070010168
Al Viro1e2e5472018-05-04 08:23:01 -040010169 d_instantiate_new(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010170
10171out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010172 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -040010173 if (err && inode) {
Chris Mason39279cc2007-06-12 06:35:45 -040010174 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -040010175 discard_new_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010176 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010177 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -040010178 return err;
10179}
Chris Mason16432982008-04-10 10:23:21 -040010180
Josef Bacik0af3d002010-06-21 14:48:16 -040010181static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
10182 u64 start, u64 num_bytes, u64 min_size,
10183 loff_t actual_len, u64 *alloc_hint,
10184 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -040010185{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010186 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik5dc562c2012-08-17 13:14:17 -040010187 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
10188 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -040010189 struct btrfs_root *root = BTRFS_I(inode)->root;
10190 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -040010191 u64 cur_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +000010192 u64 i_size;
Chris Mason154ea282013-03-05 11:11:26 -050010193 u64 cur_bytes;
Josef Bacik0b670dc2015-09-23 17:11:16 -040010194 u64 last_alloc = (u64)-1;
Yan Zhengd899e052008-10-30 14:25:28 -040010195 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -040010196 bool own_trans = true;
Wang Xiaoguang18513092016-07-25 15:51:40 +080010197 u64 end = start + num_bytes - 1;
Yan Zhengd899e052008-10-30 14:25:28 -040010198
Josef Bacik0af3d002010-06-21 14:48:16 -040010199 if (trans)
10200 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -040010201 while (num_bytes > 0) {
Josef Bacik0af3d002010-06-21 14:48:16 -040010202 if (own_trans) {
10203 trans = btrfs_start_transaction(root, 3);
10204 if (IS_ERR(trans)) {
10205 ret = PTR_ERR(trans);
10206 break;
10207 }
Yan Zhengd899e052008-10-30 14:25:28 -040010208 }
Yan, Zheng5a303d52009-11-12 09:34:52 +000010209
Byongho Leeee221842015-12-15 01:42:10 +090010210 cur_bytes = min_t(u64, num_bytes, SZ_256M);
Chris Mason154ea282013-03-05 11:11:26 -050010211 cur_bytes = max(cur_bytes, min_size);
Josef Bacik0b670dc2015-09-23 17:11:16 -040010212 /*
10213 * If we are severely fragmented we could end up with really
10214 * small allocations, so if the allocator is returning small
10215 * chunks lets make its job easier by only searching for those
10216 * sized chunks.
10217 */
10218 cur_bytes = min(cur_bytes, last_alloc);
Wang Xiaoguang18513092016-07-25 15:51:40 +080010219 ret = btrfs_reserve_extent(root, cur_bytes, cur_bytes,
10220 min_size, 0, *alloc_hint, &ins, 1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010221 if (ret) {
Josef Bacik0af3d002010-06-21 14:48:16 -040010222 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010223 btrfs_end_transaction(trans);
Yan, Zhenga22285a2010-05-16 10:48:46 -040010224 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +000010225 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010226 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010227
Josef Bacik0b670dc2015-09-23 17:11:16 -040010228 last_alloc = ins.offset;
Yan Zhengd899e052008-10-30 14:25:28 -040010229 ret = insert_reserved_file_extent(trans, inode,
10230 cur_offset, ins.objectid,
10231 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +000010232 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -040010233 BTRFS_FILE_EXTENT_PREALLOC);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010234 if (ret) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010235 btrfs_free_reserved_extent(fs_info, ins.objectid,
Miao Xiee570fd22014-06-19 10:42:50 +080010236 ins.offset, 0);
Jeff Mahoney66642832016-06-10 18:19:25 -040010237 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010238 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010239 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010240 break;
10241 }
Dongsheng Yang31193212014-12-12 16:44:35 +080010242
Nikolay Borisovdcdbc052017-02-20 13:50:45 +020010243 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Chris Masona1ed8352009-09-11 12:27:37 -040010244 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010245
Josef Bacik5dc562c2012-08-17 13:14:17 -040010246 em = alloc_extent_map();
10247 if (!em) {
10248 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
10249 &BTRFS_I(inode)->runtime_flags);
10250 goto next;
10251 }
10252
10253 em->start = cur_offset;
10254 em->orig_start = cur_offset;
10255 em->len = ins.offset;
10256 em->block_start = ins.objectid;
10257 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -050010258 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -040010259 em->ram_bytes = ins.offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010260 em->bdev = fs_info->fs_devices->latest_bdev;
Josef Bacik5dc562c2012-08-17 13:14:17 -040010261 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
10262 em->generation = trans->transid;
10263
10264 while (1) {
10265 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -040010266 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -040010267 write_unlock(&em_tree->lock);
10268 if (ret != -EEXIST)
10269 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +020010270 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -040010271 cur_offset + ins.offset - 1,
10272 0);
10273 }
10274 free_extent_map(em);
10275next:
Yan Zhengd899e052008-10-30 14:25:28 -040010276 num_bytes -= ins.offset;
10277 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -040010278 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +000010279
Josef Bacik0c4d2d92012-04-05 15:03:02 -040010280 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -070010281 inode->i_ctime = current_time(inode);
Christoph Hellwig6cbff002009-04-17 10:37:41 +020010282 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -040010283 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -040010284 (actual_len > inode->i_size) &&
10285 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +000010286 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +000010287 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +000010288 else
Josef Bacik55a61d12010-11-22 18:50:32 +000010289 i_size = cur_offset;
10290 i_size_write(inode, i_size);
10291 btrfs_ordered_update_i_size(inode, i_size, NULL);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010292 }
10293
Yan Zhengd899e052008-10-30 14:25:28 -040010294 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010295
10296 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -040010297 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010298 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010299 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010300 break;
10301 }
Yan Zhengd899e052008-10-30 14:25:28 -040010302
Josef Bacik0af3d002010-06-21 14:48:16 -040010303 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010304 btrfs_end_transaction(trans);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010305 }
Wang Xiaoguang18513092016-07-25 15:51:40 +080010306 if (cur_offset < end)
Qu Wenruobc42bda2017-02-27 15:10:39 +080010307 btrfs_free_reserved_data_space(inode, NULL, cur_offset,
Wang Xiaoguang18513092016-07-25 15:51:40 +080010308 end - cur_offset + 1);
Yan Zhengd899e052008-10-30 14:25:28 -040010309 return ret;
10310}
10311
Josef Bacik0af3d002010-06-21 14:48:16 -040010312int btrfs_prealloc_file_range(struct inode *inode, int mode,
10313 u64 start, u64 num_bytes, u64 min_size,
10314 loff_t actual_len, u64 *alloc_hint)
10315{
10316 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
10317 min_size, actual_len, alloc_hint,
10318 NULL);
10319}
10320
10321int btrfs_prealloc_file_range_trans(struct inode *inode,
10322 struct btrfs_trans_handle *trans, int mode,
10323 u64 start, u64 num_bytes, u64 min_size,
10324 loff_t actual_len, u64 *alloc_hint)
10325{
10326 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
10327 min_size, actual_len, alloc_hint, trans);
10328}
10329
Chris Masone6dcd2d2008-07-17 12:53:50 -040010330static int btrfs_set_page_dirty(struct page *page)
10331{
Chris Masone6dcd2d2008-07-17 12:53:50 -040010332 return __set_page_dirty_nobuffers(page);
10333}
10334
Al Viro10556cb22011-06-20 19:28:19 -040010335static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -050010336{
Li Zefanb83cc962010-12-20 16:04:08 +080010337 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +000010338 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +080010339
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +000010340 if (mask & MAY_WRITE &&
10341 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
10342 if (btrfs_root_readonly(root))
10343 return -EROFS;
10344 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
10345 return -EACCES;
10346 }
Al Viro2830ba72011-06-20 19:16:29 -040010347 return generic_permission(inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -050010348}
Chris Mason39279cc2007-06-12 06:35:45 -040010349
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010350static int btrfs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
10351{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010352 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010353 struct btrfs_trans_handle *trans;
10354 struct btrfs_root *root = BTRFS_I(dir)->root;
10355 struct inode *inode = NULL;
10356 u64 objectid;
10357 u64 index;
10358 int ret = 0;
10359
10360 /*
10361 * 5 units required for adding orphan entry
10362 */
10363 trans = btrfs_start_transaction(root, 5);
10364 if (IS_ERR(trans))
10365 return PTR_ERR(trans);
10366
10367 ret = btrfs_find_free_ino(root, &objectid);
10368 if (ret)
10369 goto out;
10370
10371 inode = btrfs_new_inode(trans, root, dir, NULL, 0,
David Sterbaf85b7372017-01-20 14:54:07 +010010372 btrfs_ino(BTRFS_I(dir)), objectid, mode, &index);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010373 if (IS_ERR(inode)) {
10374 ret = PTR_ERR(inode);
10375 inode = NULL;
10376 goto out;
10377 }
10378
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010379 inode->i_fop = &btrfs_file_operations;
10380 inode->i_op = &btrfs_file_inode_operations;
10381
10382 inode->i_mapping->a_ops = &btrfs_aops;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010383 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
10384
Chris Masonb0d5d102014-09-08 13:08:51 -070010385 ret = btrfs_init_inode_security(trans, inode, dir, NULL);
10386 if (ret)
Al Viro32955c52018-05-16 12:20:05 -040010387 goto out;
Chris Masonb0d5d102014-09-08 13:08:51 -070010388
10389 ret = btrfs_update_inode(trans, root, inode);
10390 if (ret)
Al Viro32955c52018-05-16 12:20:05 -040010391 goto out;
Nikolay Borisov73f2e542017-02-20 13:50:59 +020010392 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010393 if (ret)
Al Viro32955c52018-05-16 12:20:05 -040010394 goto out;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010395
Filipe Manana5762b5c2014-08-01 00:10:32 +010010396 /*
10397 * We set number of links to 0 in btrfs_new_inode(), and here we set
10398 * it to 1 because d_tmpfile() will issue a warning if the count is 0,
10399 * through:
10400 *
10401 * d_tmpfile() -> inode_dec_link_count() -> drop_nlink()
10402 */
10403 set_nlink(inode, 1);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010404 d_tmpfile(dentry, inode);
Al Viro32955c52018-05-16 12:20:05 -040010405 unlock_new_inode(inode);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010406 mark_inode_dirty(inode);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010407out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010408 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -040010409 if (ret && inode)
10410 discard_new_inode(inode);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010411 btrfs_btree_balance_dirty(fs_info);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010412 return ret;
10413}
10414
David Sterba20a7db82017-02-17 16:24:29 +010010415__attribute__((const))
Liu Bo9d0d1c82017-03-24 15:04:50 -070010416static int btrfs_readpage_io_failed_hook(struct page *page, int failed_mirror)
David Sterba20a7db82017-02-17 16:24:29 +010010417{
Liu Bo9d0d1c82017-03-24 15:04:50 -070010418 return -EAGAIN;
David Sterba20a7db82017-02-17 16:24:29 +010010419}
10420
David Sterba5cdc84b2018-07-18 20:32:52 +020010421void btrfs_set_range_writeback(struct extent_io_tree *tree, u64 start, u64 end)
Josef Bacikc6100a42017-05-05 11:57:13 -040010422{
David Sterba5cdc84b2018-07-18 20:32:52 +020010423 struct inode *inode = tree->private_data;
Josef Bacikc6100a42017-05-05 11:57:13 -040010424 unsigned long index = start >> PAGE_SHIFT;
10425 unsigned long end_index = end >> PAGE_SHIFT;
10426 struct page *page;
10427
10428 while (index <= end_index) {
10429 page = find_get_page(inode->i_mapping, index);
10430 ASSERT(page); /* Pages should be in the extent_io_tree */
10431 set_page_writeback(page);
10432 put_page(page);
10433 index++;
10434 }
10435}
10436
Omar Sandovaled46ff32016-11-03 10:28:14 -070010437#ifdef CONFIG_SWAP
10438/*
10439 * Add an entry indicating a block group or device which is pinned by a
10440 * swapfile. Returns 0 on success, 1 if there is already an entry for it, or a
10441 * negative errno on failure.
10442 */
10443static int btrfs_add_swapfile_pin(struct inode *inode, void *ptr,
10444 bool is_block_group)
10445{
10446 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
10447 struct btrfs_swapfile_pin *sp, *entry;
10448 struct rb_node **p;
10449 struct rb_node *parent = NULL;
10450
10451 sp = kmalloc(sizeof(*sp), GFP_NOFS);
10452 if (!sp)
10453 return -ENOMEM;
10454 sp->ptr = ptr;
10455 sp->inode = inode;
10456 sp->is_block_group = is_block_group;
10457
10458 spin_lock(&fs_info->swapfile_pins_lock);
10459 p = &fs_info->swapfile_pins.rb_node;
10460 while (*p) {
10461 parent = *p;
10462 entry = rb_entry(parent, struct btrfs_swapfile_pin, node);
10463 if (sp->ptr < entry->ptr ||
10464 (sp->ptr == entry->ptr && sp->inode < entry->inode)) {
10465 p = &(*p)->rb_left;
10466 } else if (sp->ptr > entry->ptr ||
10467 (sp->ptr == entry->ptr && sp->inode > entry->inode)) {
10468 p = &(*p)->rb_right;
10469 } else {
10470 spin_unlock(&fs_info->swapfile_pins_lock);
10471 kfree(sp);
10472 return 1;
10473 }
10474 }
10475 rb_link_node(&sp->node, parent, p);
10476 rb_insert_color(&sp->node, &fs_info->swapfile_pins);
10477 spin_unlock(&fs_info->swapfile_pins_lock);
10478 return 0;
10479}
10480
10481/* Free all of the entries pinned by this swapfile. */
10482static void btrfs_free_swapfile_pins(struct inode *inode)
10483{
10484 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
10485 struct btrfs_swapfile_pin *sp;
10486 struct rb_node *node, *next;
10487
10488 spin_lock(&fs_info->swapfile_pins_lock);
10489 node = rb_first(&fs_info->swapfile_pins);
10490 while (node) {
10491 next = rb_next(node);
10492 sp = rb_entry(node, struct btrfs_swapfile_pin, node);
10493 if (sp->inode == inode) {
10494 rb_erase(&sp->node, &fs_info->swapfile_pins);
10495 if (sp->is_block_group)
10496 btrfs_put_block_group(sp->ptr);
10497 kfree(sp);
10498 }
10499 node = next;
10500 }
10501 spin_unlock(&fs_info->swapfile_pins_lock);
10502}
10503
10504struct btrfs_swap_info {
10505 u64 start;
10506 u64 block_start;
10507 u64 block_len;
10508 u64 lowest_ppage;
10509 u64 highest_ppage;
10510 unsigned long nr_pages;
10511 int nr_extents;
10512};
10513
10514static int btrfs_add_swap_extent(struct swap_info_struct *sis,
10515 struct btrfs_swap_info *bsi)
10516{
10517 unsigned long nr_pages;
10518 u64 first_ppage, first_ppage_reported, next_ppage;
10519 int ret;
10520
10521 first_ppage = ALIGN(bsi->block_start, PAGE_SIZE) >> PAGE_SHIFT;
10522 next_ppage = ALIGN_DOWN(bsi->block_start + bsi->block_len,
10523 PAGE_SIZE) >> PAGE_SHIFT;
10524
10525 if (first_ppage >= next_ppage)
10526 return 0;
10527 nr_pages = next_ppage - first_ppage;
10528
10529 first_ppage_reported = first_ppage;
10530 if (bsi->start == 0)
10531 first_ppage_reported++;
10532 if (bsi->lowest_ppage > first_ppage_reported)
10533 bsi->lowest_ppage = first_ppage_reported;
10534 if (bsi->highest_ppage < (next_ppage - 1))
10535 bsi->highest_ppage = next_ppage - 1;
10536
10537 ret = add_swap_extent(sis, bsi->nr_pages, nr_pages, first_ppage);
10538 if (ret < 0)
10539 return ret;
10540 bsi->nr_extents += ret;
10541 bsi->nr_pages += nr_pages;
10542 return 0;
10543}
10544
10545static void btrfs_swap_deactivate(struct file *file)
10546{
10547 struct inode *inode = file_inode(file);
10548
10549 btrfs_free_swapfile_pins(inode);
10550 atomic_dec(&BTRFS_I(inode)->root->nr_swapfiles);
10551}
10552
10553static int btrfs_swap_activate(struct swap_info_struct *sis, struct file *file,
10554 sector_t *span)
10555{
10556 struct inode *inode = file_inode(file);
10557 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
10558 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
10559 struct extent_state *cached_state = NULL;
10560 struct extent_map *em = NULL;
10561 struct btrfs_device *device = NULL;
10562 struct btrfs_swap_info bsi = {
10563 .lowest_ppage = (sector_t)-1ULL,
10564 };
10565 int ret = 0;
10566 u64 isize;
10567 u64 start;
10568
10569 /*
10570 * If the swap file was just created, make sure delalloc is done. If the
10571 * file changes again after this, the user is doing something stupid and
10572 * we don't really care.
10573 */
10574 ret = btrfs_wait_ordered_range(inode, 0, (u64)-1);
10575 if (ret)
10576 return ret;
10577
10578 /*
10579 * The inode is locked, so these flags won't change after we check them.
10580 */
10581 if (BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS) {
10582 btrfs_warn(fs_info, "swapfile must not be compressed");
10583 return -EINVAL;
10584 }
10585 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW)) {
10586 btrfs_warn(fs_info, "swapfile must not be copy-on-write");
10587 return -EINVAL;
10588 }
10589 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
10590 btrfs_warn(fs_info, "swapfile must not be checksummed");
10591 return -EINVAL;
10592 }
10593
10594 /*
10595 * Balance or device remove/replace/resize can move stuff around from
10596 * under us. The EXCL_OP flag makes sure they aren't running/won't run
10597 * concurrently while we are mapping the swap extents, and
10598 * fs_info->swapfile_pins prevents them from running while the swap file
10599 * is active and moving the extents. Note that this also prevents a
10600 * concurrent device add which isn't actually necessary, but it's not
10601 * really worth the trouble to allow it.
10602 */
10603 if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) {
10604 btrfs_warn(fs_info,
10605 "cannot activate swapfile while exclusive operation is running");
10606 return -EBUSY;
10607 }
10608 /*
10609 * Snapshots can create extents which require COW even if NODATACOW is
10610 * set. We use this counter to prevent snapshots. We must increment it
10611 * before walking the extents because we don't want a concurrent
10612 * snapshot to run after we've already checked the extents.
10613 */
10614 atomic_inc(&BTRFS_I(inode)->root->nr_swapfiles);
10615
10616 isize = ALIGN_DOWN(inode->i_size, fs_info->sectorsize);
10617
10618 lock_extent_bits(io_tree, 0, isize - 1, &cached_state);
10619 start = 0;
10620 while (start < isize) {
10621 u64 logical_block_start, physical_block_start;
10622 struct btrfs_block_group_cache *bg;
10623 u64 len = isize - start;
10624
10625 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len, 0);
10626 if (IS_ERR(em)) {
10627 ret = PTR_ERR(em);
10628 goto out;
10629 }
10630
10631 if (em->block_start == EXTENT_MAP_HOLE) {
10632 btrfs_warn(fs_info, "swapfile must not have holes");
10633 ret = -EINVAL;
10634 goto out;
10635 }
10636 if (em->block_start == EXTENT_MAP_INLINE) {
10637 /*
10638 * It's unlikely we'll ever actually find ourselves
10639 * here, as a file small enough to fit inline won't be
10640 * big enough to store more than the swap header, but in
10641 * case something changes in the future, let's catch it
10642 * here rather than later.
10643 */
10644 btrfs_warn(fs_info, "swapfile must not be inline");
10645 ret = -EINVAL;
10646 goto out;
10647 }
10648 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
10649 btrfs_warn(fs_info, "swapfile must not be compressed");
10650 ret = -EINVAL;
10651 goto out;
10652 }
10653
10654 logical_block_start = em->block_start + (start - em->start);
10655 len = min(len, em->len - (start - em->start));
10656 free_extent_map(em);
10657 em = NULL;
10658
10659 ret = can_nocow_extent(inode, start, &len, NULL, NULL, NULL);
10660 if (ret < 0) {
10661 goto out;
10662 } else if (ret) {
10663 ret = 0;
10664 } else {
10665 btrfs_warn(fs_info,
10666 "swapfile must not be copy-on-write");
10667 ret = -EINVAL;
10668 goto out;
10669 }
10670
10671 em = btrfs_get_chunk_map(fs_info, logical_block_start, len);
10672 if (IS_ERR(em)) {
10673 ret = PTR_ERR(em);
10674 goto out;
10675 }
10676
10677 if (em->map_lookup->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
10678 btrfs_warn(fs_info,
10679 "swapfile must have single data profile");
10680 ret = -EINVAL;
10681 goto out;
10682 }
10683
10684 if (device == NULL) {
10685 device = em->map_lookup->stripes[0].dev;
10686 ret = btrfs_add_swapfile_pin(inode, device, false);
10687 if (ret == 1)
10688 ret = 0;
10689 else if (ret)
10690 goto out;
10691 } else if (device != em->map_lookup->stripes[0].dev) {
10692 btrfs_warn(fs_info, "swapfile must be on one device");
10693 ret = -EINVAL;
10694 goto out;
10695 }
10696
10697 physical_block_start = (em->map_lookup->stripes[0].physical +
10698 (logical_block_start - em->start));
10699 len = min(len, em->len - (logical_block_start - em->start));
10700 free_extent_map(em);
10701 em = NULL;
10702
10703 bg = btrfs_lookup_block_group(fs_info, logical_block_start);
10704 if (!bg) {
10705 btrfs_warn(fs_info,
10706 "could not find block group containing swapfile");
10707 ret = -EINVAL;
10708 goto out;
10709 }
10710
10711 ret = btrfs_add_swapfile_pin(inode, bg, true);
10712 if (ret) {
10713 btrfs_put_block_group(bg);
10714 if (ret == 1)
10715 ret = 0;
10716 else
10717 goto out;
10718 }
10719
10720 if (bsi.block_len &&
10721 bsi.block_start + bsi.block_len == physical_block_start) {
10722 bsi.block_len += len;
10723 } else {
10724 if (bsi.block_len) {
10725 ret = btrfs_add_swap_extent(sis, &bsi);
10726 if (ret)
10727 goto out;
10728 }
10729 bsi.start = start;
10730 bsi.block_start = physical_block_start;
10731 bsi.block_len = len;
10732 }
10733
10734 start += len;
10735 }
10736
10737 if (bsi.block_len)
10738 ret = btrfs_add_swap_extent(sis, &bsi);
10739
10740out:
10741 if (!IS_ERR_OR_NULL(em))
10742 free_extent_map(em);
10743
10744 unlock_extent_cached(io_tree, 0, isize - 1, &cached_state);
10745
10746 if (ret)
10747 btrfs_swap_deactivate(file);
10748
10749 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
10750
10751 if (ret)
10752 return ret;
10753
10754 if (device)
10755 sis->bdev = device->bdev;
10756 *span = bsi.highest_ppage - bsi.lowest_ppage + 1;
10757 sis->max = bsi.nr_pages;
10758 sis->pages = bsi.nr_pages - 1;
10759 sis->highest_bit = bsi.nr_pages - 1;
10760 return bsi.nr_extents;
10761}
10762#else
10763static void btrfs_swap_deactivate(struct file *file)
10764{
10765}
10766
10767static int btrfs_swap_activate(struct swap_info_struct *sis, struct file *file,
10768 sector_t *span)
10769{
10770 return -EOPNOTSUPP;
10771}
10772#endif
10773
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010774static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -050010775 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -040010776 .lookup = btrfs_lookup,
10777 .create = btrfs_create,
10778 .unlink = btrfs_unlink,
10779 .link = btrfs_link,
10780 .mkdir = btrfs_mkdir,
10781 .rmdir = btrfs_rmdir,
Miklos Szeredi2773bf02016-09-27 11:03:58 +020010782 .rename = btrfs_rename2,
Chris Mason39279cc2007-06-12 06:35:45 -040010783 .symlink = btrfs_symlink,
10784 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -040010785 .mknod = btrfs_mknod,
Josef Bacik5103e942007-11-16 11:45:54 -050010786 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010787 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010788 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010789 .set_acl = btrfs_set_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070010790 .update_time = btrfs_update_time,
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010791 .tmpfile = btrfs_tmpfile,
Chris Mason39279cc2007-06-12 06:35:45 -040010792};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010793static const struct inode_operations btrfs_dir_ro_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010794 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -050010795 .permission = btrfs_permission,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070010796 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010797};
Yan, Zheng76dda932009-09-21 16:00:26 -040010798
Alexey Dobriyan828c0952009-10-01 15:43:56 -070010799static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010800 .llseek = generic_file_llseek,
10801 .read = generic_read_dir,
Omar Sandoval02dbfc92016-05-20 13:50:33 -070010802 .iterate_shared = btrfs_real_readdir,
Josef Bacik23b5ec72017-07-24 15:14:25 -040010803 .open = btrfs_opendir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -040010804 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010805#ifdef CONFIG_COMPAT
Luke Dashjr4c63c242015-10-29 08:22:21 +000010806 .compat_ioctl = btrfs_compat_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010807#endif
Sage Weil6bf13c02008-06-10 10:07:39 -040010808 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -040010809 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -040010810};
10811
David Sterba20e55062015-11-19 11:42:28 +010010812static const struct extent_io_ops btrfs_extent_io_ops = {
David Sterba4d53ddd2017-02-17 15:27:44 +010010813 /* mandatory callbacks */
Chris Mason065631f2008-02-20 12:07:25 -050010814 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -040010815 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
Liu Bo9d0d1c82017-03-24 15:04:50 -070010816 .readpage_io_failed_hook = btrfs_readpage_io_failed_hook,
Chris Mason07157aa2007-08-30 08:50:51 -040010817};
10818
Chris Mason35054392009-01-21 13:11:13 -050010819/*
10820 * btrfs doesn't support the bmap operation because swapfiles
10821 * use bmap to make a mapping of extents in the file. They assume
10822 * these extents won't change over the life of the file and they
10823 * use the bmap result to do IO directly to the drive.
10824 *
10825 * the btrfs bmap call would return logical addresses that aren't
10826 * suitable for IO and they also will change frequently as COW
10827 * operations happen. So, swapfile + btrfs == corruption.
10828 *
10829 * For now we're avoiding this by dropping bmap.
10830 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -070010831static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -040010832 .readpage = btrfs_readpage,
10833 .writepage = btrfs_writepage,
Chris Masonb293f02e2007-11-01 19:45:34 -040010834 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -050010835 .readpages = btrfs_readpages,
Chris Mason16432982008-04-10 10:23:21 -040010836 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -040010837 .invalidatepage = btrfs_invalidatepage,
10838 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -040010839 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +020010840 .error_remove_page = generic_error_remove_page,
Omar Sandovaled46ff32016-11-03 10:28:14 -070010841 .swap_activate = btrfs_swap_activate,
10842 .swap_deactivate = btrfs_swap_deactivate,
Chris Mason39279cc2007-06-12 06:35:45 -040010843};
10844
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010845static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010846 .getattr = btrfs_getattr,
10847 .setattr = btrfs_setattr,
Josef Bacik5103e942007-11-16 11:45:54 -050010848 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010849 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -050010850 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010851 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010852 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010853 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010854};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010855static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -040010856 .getattr = btrfs_getattr,
10857 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010858 .permission = btrfs_permission,
Josef Bacik33268ea2008-07-24 12:16:36 -040010859 .listxattr = btrfs_listxattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010860 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010861 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010862 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -040010863};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010864static const struct inode_operations btrfs_symlink_inode_operations = {
Al Viro6b255392015-11-17 10:20:54 -050010865 .get_link = page_get_link,
Li Zefanf2095612010-11-19 02:05:24 +000010866 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -050010867 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010868 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -050010869 .listxattr = btrfs_listxattr,
Josef Bacike41f9412012-03-26 09:46:47 -040010870 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010871};
Yan, Zheng76dda932009-09-21 16:00:26 -040010872
Alexey Dobriyan82d339d2009-10-09 09:54:36 -040010873const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -040010874 .d_delete = btrfs_dentry_delete,
10875};