blob: 2585200cb43a5604cc41a060ee926f7d42834c0a [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;
361 struct btrfs_root *root;
362 struct page *locked_page;
363 u64 start;
364 u64 end;
Liu Bof82b7352017-10-23 23:18:16 -0600365 unsigned int write_flags;
Chris Mason771ed682008-11-06 22:02:51 -0500366 struct list_head extents;
367 struct btrfs_work work;
368};
369
370static noinline int add_async_extent(struct async_cow *cow,
371 u64 start, u64 ram_size,
372 u64 compressed_size,
373 struct page **pages,
Li Zefan261507a02010-12-17 14:21:50 +0800374 unsigned long nr_pages,
375 int compress_type)
Chris Mason771ed682008-11-06 22:02:51 -0500376{
377 struct async_extent *async_extent;
378
379 async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100380 BUG_ON(!async_extent); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -0500381 async_extent->start = start;
382 async_extent->ram_size = ram_size;
383 async_extent->compressed_size = compressed_size;
384 async_extent->pages = pages;
385 async_extent->nr_pages = nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800386 async_extent->compress_type = compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500387 list_add_tail(&async_extent->list, &cow->extents);
388 return 0;
389}
390
Timofey Titovetsc2fcdcd2017-07-17 16:52:58 +0300391static inline int inode_need_compress(struct inode *inode, u64 start, u64 end)
Wang Shilongf79707b2014-07-17 11:44:09 +0800392{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400393 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Wang Shilongf79707b2014-07-17 11:44:09 +0800394
395 /* force compress */
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400396 if (btrfs_test_opt(fs_info, FORCE_COMPRESS))
Wang Shilongf79707b2014-07-17 11:44:09 +0800397 return 1;
David Sterbaeec63c62017-07-17 19:41:31 +0200398 /* defrag ioctl */
399 if (BTRFS_I(inode)->defrag_compress)
400 return 1;
Wang Shilongf79707b2014-07-17 11:44:09 +0800401 /* bad compression ratios */
402 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS)
403 return 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400404 if (btrfs_test_opt(fs_info, COMPRESS) ||
Wang Shilongf79707b2014-07-17 11:44:09 +0800405 BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS ||
David Sterbab52aa8c2017-07-17 19:17:20 +0200406 BTRFS_I(inode)->prop_compress)
Timofey Titovetsc2fcdcd2017-07-17 16:52:58 +0300407 return btrfs_compress_heuristic(inode, start, end);
Wang Shilongf79707b2014-07-17 11:44:09 +0800408 return 0;
409}
410
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200411static inline void inode_should_defrag(struct btrfs_inode *inode,
Anand Jain26d30f82016-12-19 19:09:06 +0800412 u64 start, u64 end, u64 num_bytes, u64 small_write)
413{
414 /* If this is a small write inside eof, kick off a defrag */
415 if (num_bytes < small_write &&
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200416 (start > 0 || end + 1 < inode->disk_i_size))
Anand Jain26d30f82016-12-19 19:09:06 +0800417 btrfs_add_inode_defrag(NULL, inode);
418}
419
Chris Masonc8b97812008-10-29 14:49:59 -0400420/*
Chris Mason771ed682008-11-06 22:02:51 -0500421 * we create compressed extents in two phases. The first
422 * phase compresses a range of pages that have already been
423 * locked (both pages and state bits are locked).
Chris Masonc8b97812008-10-29 14:49:59 -0400424 *
Chris Mason771ed682008-11-06 22:02:51 -0500425 * This is done inside an ordered work queue, and the compression
426 * is spread across many cpus. The actual IO submission is step
427 * two, and the ordered work queue takes care of making sure that
428 * happens in the same order things were put onto the queue by
429 * writepages and friends.
Chris Masonc8b97812008-10-29 14:49:59 -0400430 *
Chris Mason771ed682008-11-06 22:02:51 -0500431 * If this code finds it can't get good compression, it puts an
432 * entry onto the work queue to write the uncompressed bytes. This
433 * makes sure that both compressed inodes and uncompressed inodes
Artem Bityutskiyb2570312012-07-25 18:12:06 +0300434 * are written in the same order that the flusher thread sent them
435 * down.
Chris Masond352ac62008-09-29 15:18:18 -0400436 */
Filipe Mananac44f6492014-10-09 21:15:44 +0100437static noinline void compress_file_range(struct inode *inode,
Chris Mason771ed682008-11-06 22:02:51 -0500438 struct page *locked_page,
439 u64 start, u64 end,
440 struct async_cow *async_cow,
441 int *num_added)
Chris Masonb888db2b2007-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 Masonb888db2b2007-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 Masonb888db2b2007-08-27 16:49:44 -04001080 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001081out:
Chris Masonbe20aa92007-12-17 20:14:01 -05001082 return ret;
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001083
Filipe Mananad9f85962014-08-25 10:43:00 +01001084out_drop_extent_cache:
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02001085 btrfs_drop_extent_cache(BTRFS_I(inode), start, start + ram_size - 1, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001086out_reserve:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001087 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001088 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001089out_unlock:
Filipe Mananaa7e3b972017-04-03 10:45:46 +01001090 clear_bits = EXTENT_LOCKED | EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
1091 EXTENT_DEFRAG | EXTENT_CLEAR_META_RESV;
Filipe Mananaa315e682017-03-06 23:04:20 +00001092 page_ops = PAGE_UNLOCK | PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
1093 PAGE_END_WRITEBACK;
1094 /*
1095 * If we reserved an extent for our delalloc range (or a subrange) and
1096 * failed to create the respective ordered extent, then it means that
1097 * when we reserved the extent we decremented the extent's size from
1098 * the data space_info's bytes_may_use counter and incremented the
1099 * space_info's bytes_reserved counter by the same amount. We must make
1100 * sure extent_clear_unlock_delalloc() does not try to decrement again
1101 * the data space_info's bytes_may_use counter, therefore we do not pass
1102 * it the flag EXTENT_CLEAR_DATA_RESV.
1103 */
1104 if (extent_reserved) {
1105 extent_clear_unlock_delalloc(inode, start,
1106 start + cur_alloc_size,
1107 start + cur_alloc_size,
1108 locked_page,
1109 clear_bits,
1110 page_ops);
1111 start += cur_alloc_size;
1112 if (start >= end)
1113 goto out;
1114 }
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001115 extent_clear_unlock_delalloc(inode, start, end, delalloc_end,
1116 locked_page,
Filipe Mananaa315e682017-03-06 23:04:20 +00001117 clear_bits | EXTENT_CLEAR_DATA_RESV,
1118 page_ops);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001119 goto out;
Chris Mason771ed682008-11-06 22:02:51 -05001120}
Chris Masonc8b97812008-10-29 14:49:59 -04001121
Chris Mason771ed682008-11-06 22:02:51 -05001122/*
1123 * work queue call back to started compression on a file and pages
1124 */
1125static noinline void async_cow_start(struct btrfs_work *work)
1126{
1127 struct async_cow *async_cow;
1128 int num_added = 0;
1129 async_cow = container_of(work, struct async_cow, work);
1130
1131 compress_file_range(async_cow->inode, async_cow->locked_page,
1132 async_cow->start, async_cow->end, async_cow,
1133 &num_added);
Josef Bacik8180ef82012-06-08 15:16:12 -04001134 if (num_added == 0) {
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001135 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001136 async_cow->inode = NULL;
Josef Bacik8180ef82012-06-08 15:16:12 -04001137 }
Chris Mason771ed682008-11-06 22:02:51 -05001138}
1139
1140/*
1141 * work queue call back to submit previously compressed pages
1142 */
1143static noinline void async_cow_submit(struct btrfs_work *work)
1144{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001145 struct btrfs_fs_info *fs_info;
Chris Mason771ed682008-11-06 22:02:51 -05001146 struct async_cow *async_cow;
1147 struct btrfs_root *root;
1148 unsigned long nr_pages;
1149
1150 async_cow = container_of(work, struct async_cow, work);
1151
1152 root = async_cow->root;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001153 fs_info = root->fs_info;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001154 nr_pages = (async_cow->end - async_cow->start + PAGE_SIZE) >>
1155 PAGE_SHIFT;
Chris Mason771ed682008-11-06 22:02:51 -05001156
David Sterba093258e2018-02-26 16:15:17 +01001157 /* atomic_sub_return implies a barrier */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001158 if (atomic_sub_return(nr_pages, &fs_info->async_delalloc_pages) <
David Sterba093258e2018-02-26 16:15:17 +01001159 5 * SZ_1M)
1160 cond_wake_up_nomb(&fs_info->async_submit_wait);
Chris Mason771ed682008-11-06 22:02:51 -05001161
Chris Masond3977122009-01-05 21:25:51 -05001162 if (async_cow->inode)
Chris Mason771ed682008-11-06 22:02:51 -05001163 submit_compressed_extents(async_cow->inode, async_cow);
Chris Mason771ed682008-11-06 22:02:51 -05001164}
Chris Masonc8b97812008-10-29 14:49:59 -04001165
Chris Mason771ed682008-11-06 22:02:51 -05001166static noinline void async_cow_free(struct btrfs_work *work)
1167{
1168 struct async_cow *async_cow;
1169 async_cow = container_of(work, struct async_cow, work);
Josef Bacik8180ef82012-06-08 15:16:12 -04001170 if (async_cow->inode)
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001171 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001172 kfree(async_cow);
1173}
1174
1175static int cow_file_range_async(struct inode *inode, struct page *locked_page,
1176 u64 start, u64 end, int *page_started,
Liu Bof82b7352017-10-23 23:18:16 -06001177 unsigned long *nr_written,
1178 unsigned int write_flags)
Chris Mason771ed682008-11-06 22:02:51 -05001179{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001180 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason771ed682008-11-06 22:02:51 -05001181 struct async_cow *async_cow;
1182 struct btrfs_root *root = BTRFS_I(inode)->root;
1183 unsigned long nr_pages;
1184 u64 cur_end;
Chris Mason771ed682008-11-06 22:02:51 -05001185
Chris Masona3429ab2009-10-08 12:30:20 -04001186 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED,
David Sterbaae0f1622017-10-31 16:37:52 +01001187 1, 0, NULL);
Chris Masond3977122009-01-05 21:25:51 -05001188 while (start < end) {
Chris Mason771ed682008-11-06 22:02:51 -05001189 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001190 BUG_ON(!async_cow); /* -ENOMEM */
Josef Bacik8180ef82012-06-08 15:16:12 -04001191 async_cow->inode = igrab(inode);
Chris Mason771ed682008-11-06 22:02:51 -05001192 async_cow->root = root;
1193 async_cow->locked_page = locked_page;
1194 async_cow->start = start;
Liu Bof82b7352017-10-23 23:18:16 -06001195 async_cow->write_flags = write_flags;
Chris Mason771ed682008-11-06 22:02:51 -05001196
Wang Shilongf79707b2014-07-17 11:44:09 +08001197 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001198 !btrfs_test_opt(fs_info, FORCE_COMPRESS))
Chris Mason771ed682008-11-06 22:02:51 -05001199 cur_end = end;
1200 else
Byongho Leeee221842015-12-15 01:42:10 +09001201 cur_end = min(end, start + SZ_512K - 1);
Chris Mason771ed682008-11-06 22:02:51 -05001202
1203 async_cow->end = cur_end;
1204 INIT_LIST_HEAD(&async_cow->extents);
1205
Liu Bo9e0af232014-08-15 23:36:53 +08001206 btrfs_init_work(&async_cow->work,
1207 btrfs_delalloc_helper,
1208 async_cow_start, async_cow_submit,
1209 async_cow_free);
Chris Mason771ed682008-11-06 22:02:51 -05001210
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001211 nr_pages = (cur_end - start + PAGE_SIZE) >>
1212 PAGE_SHIFT;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001213 atomic_add(nr_pages, &fs_info->async_delalloc_pages);
Chris Mason771ed682008-11-06 22:02:51 -05001214
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001215 btrfs_queue_work(fs_info->delalloc_workers, &async_cow->work);
Chris Mason771ed682008-11-06 22:02:51 -05001216
Chris Mason771ed682008-11-06 22:02:51 -05001217 *nr_written += nr_pages;
1218 start = cur_end + 1;
1219 }
1220 *page_started = 1;
1221 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001222}
1223
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001224static noinline int csum_exist_in_range(struct btrfs_fs_info *fs_info,
Yan Zheng17d217f2008-12-12 10:03:38 -05001225 u64 bytenr, u64 num_bytes)
1226{
1227 int ret;
1228 struct btrfs_ordered_sum *sums;
1229 LIST_HEAD(list);
1230
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001231 ret = btrfs_lookup_csums_range(fs_info->csum_root, bytenr,
Arne Jansena2de7332011-03-08 14:14:00 +01001232 bytenr + num_bytes - 1, &list, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001233 if (ret == 0 && list_empty(&list))
1234 return 0;
1235
1236 while (!list_empty(&list)) {
1237 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1238 list_del(&sums->list);
1239 kfree(sums);
1240 }
Liu Bo58113752018-01-31 17:09:13 -07001241 if (ret < 0)
1242 return ret;
Yan Zheng17d217f2008-12-12 10:03:38 -05001243 return 1;
1244}
1245
Chris Masond352ac62008-09-29 15:18:18 -04001246/*
1247 * when nowcow writeback call back. This checks for snapshots or COW copies
1248 * of the extents that exist in the file, and COWs the file as required.
1249 *
1250 * If no cow copies or snapshots exist, we write directly to the existing
1251 * blocks on disk
1252 */
Chris Mason7f366cf2009-03-12 20:12:45 -04001253static noinline int run_delalloc_nocow(struct inode *inode,
1254 struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001255 u64 start, u64 end, int *page_started, int force,
1256 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001257{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001258 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masonbe20aa92007-12-17 20:14:01 -05001259 struct btrfs_root *root = BTRFS_I(inode)->root;
1260 struct extent_buffer *leaf;
Chris Masonbe20aa92007-12-17 20:14:01 -05001261 struct btrfs_path *path;
Yan Zheng80ff3852008-10-30 14:20:02 -04001262 struct btrfs_file_extent_item *fi;
Chris Masonbe20aa92007-12-17 20:14:01 -05001263 struct btrfs_key found_key;
Liu Bo6f9994d2017-01-31 07:50:22 -08001264 struct extent_map *em;
Yan Zheng80ff3852008-10-30 14:20:02 -04001265 u64 cow_start;
1266 u64 cur_offset;
1267 u64 extent_end;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001268 u64 extent_offset;
Yan Zheng80ff3852008-10-30 14:20:02 -04001269 u64 disk_bytenr;
1270 u64 num_bytes;
Josef Bacikb4939682012-12-03 10:31:19 -05001271 u64 disk_num_bytes;
Josef Bacikcc95bef2013-04-04 14:31:27 -04001272 u64 ram_bytes;
Yan Zheng80ff3852008-10-30 14:20:02 -04001273 int extent_type;
Robbie Ko8ecebf4d2018-08-06 10:30:30 +08001274 int ret;
Yan Zhengd899e052008-10-30 14:25:28 -04001275 int type;
Yan Zheng80ff3852008-10-30 14:20:02 -04001276 int nocow;
1277 int check_prev = 1;
Li Zefan82d59022011-04-20 10:33:24 +08001278 bool nolock;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02001279 u64 ino = btrfs_ino(BTRFS_I(inode));
Chris Masonbe20aa92007-12-17 20:14:01 -05001280
1281 path = btrfs_alloc_path();
Josef Bacik17ca04a2012-05-31 15:58:55 -04001282 if (!path) {
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001283 extent_clear_unlock_delalloc(inode, start, end, end,
1284 locked_page,
Josef Bacikc2790a22013-07-29 11:20:47 -04001285 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -04001286 EXTENT_DO_ACCOUNTING |
1287 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001288 PAGE_CLEAR_DIRTY |
1289 PAGE_SET_WRITEBACK |
1290 PAGE_END_WRITEBACK);
Mark Fashehd8926bb2011-07-13 10:38:47 -07001291 return -ENOMEM;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001292 }
Li Zefan82d59022011-04-20 10:33:24 +08001293
Nikolay Borisov70ddc552017-02-20 13:50:35 +02001294 nolock = btrfs_is_free_space_inode(BTRFS_I(inode));
Li Zefan82d59022011-04-20 10:33:24 +08001295
Yan Zheng80ff3852008-10-30 14:20:02 -04001296 cow_start = (u64)-1;
1297 cur_offset = start;
1298 while (1) {
Liu Boe4c3b2d2017-01-30 12:25:28 -08001299 ret = btrfs_lookup_file_extent(NULL, root, path, ino,
Yan Zheng80ff3852008-10-30 14:20:02 -04001300 cur_offset, 0);
Josef Bacikd788a342013-10-25 16:55:08 -04001301 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001302 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001303 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1304 leaf = path->nodes[0];
1305 btrfs_item_key_to_cpu(leaf, &found_key,
1306 path->slots[0] - 1);
Li Zefan33345d012011-04-20 10:31:50 +08001307 if (found_key.objectid == ino &&
Yan Zheng80ff3852008-10-30 14:20:02 -04001308 found_key.type == BTRFS_EXTENT_DATA_KEY)
1309 path->slots[0]--;
1310 }
1311 check_prev = 0;
1312next_slot:
1313 leaf = path->nodes[0];
1314 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1315 ret = btrfs_next_leaf(root, path);
Liu Boe8916692018-01-25 11:02:50 -07001316 if (ret < 0) {
1317 if (cow_start != (u64)-1)
1318 cur_offset = cow_start;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001319 goto error;
Liu Boe8916692018-01-25 11:02:50 -07001320 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001321 if (ret > 0)
1322 break;
1323 leaf = path->nodes[0];
1324 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001325
Yan Zheng80ff3852008-10-30 14:20:02 -04001326 nocow = 0;
1327 disk_bytenr = 0;
Yan Zheng17d217f2008-12-12 10:03:38 -05001328 num_bytes = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001329 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001330
Filipe Manana1d512cb2015-11-09 00:33:58 +00001331 if (found_key.objectid > ino)
1332 break;
1333 if (WARN_ON_ONCE(found_key.objectid < ino) ||
1334 found_key.type < BTRFS_EXTENT_DATA_KEY) {
1335 path->slots[0]++;
1336 goto next_slot;
1337 }
1338 if (found_key.type > BTRFS_EXTENT_DATA_KEY ||
Yan Zheng80ff3852008-10-30 14:20:02 -04001339 found_key.offset > end)
1340 break;
Chris Masonbe20aa92007-12-17 20:14:01 -05001341
Yan Zheng80ff3852008-10-30 14:20:02 -04001342 if (found_key.offset > cur_offset) {
1343 extent_end = found_key.offset;
Chris Masone9061e22009-10-09 09:57:45 -04001344 extent_type = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001345 goto out_check;
1346 }
Chris Masonc31f8832008-01-08 15:46:31 -05001347
Yan Zheng80ff3852008-10-30 14:20:02 -04001348 fi = btrfs_item_ptr(leaf, path->slots[0],
1349 struct btrfs_file_extent_item);
1350 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001351
Josef Bacikcc95bef2013-04-04 14:31:27 -04001352 ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
Yan Zhengd899e052008-10-30 14:25:28 -04001353 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1354 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001355 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001356 extent_offset = btrfs_file_extent_offset(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001357 extent_end = found_key.offset +
1358 btrfs_file_extent_num_bytes(leaf, fi);
Josef Bacikb4939682012-12-03 10:31:19 -05001359 disk_num_bytes =
1360 btrfs_file_extent_disk_num_bytes(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001361 if (extent_end <= start) {
1362 path->slots[0]++;
1363 goto next_slot;
1364 }
Yan Zheng17d217f2008-12-12 10:03:38 -05001365 if (disk_bytenr == 0)
1366 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001367 if (btrfs_file_extent_compression(leaf, fi) ||
1368 btrfs_file_extent_encryption(leaf, fi) ||
1369 btrfs_file_extent_other_encoding(leaf, fi))
1370 goto out_check;
Ethan Lien78d42952018-05-17 14:58:29 +08001371 /*
1372 * Do the same check as in btrfs_cross_ref_exist but
1373 * without the unnecessary search.
1374 */
1375 if (btrfs_file_extent_generation(leaf, fi) <=
1376 btrfs_root_last_snapshot(&root->root_item))
1377 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001378 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1379 goto out_check;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001380 if (btrfs_extent_readonly(fs_info, disk_bytenr))
Yan Zheng80ff3852008-10-30 14:20:02 -04001381 goto out_check;
Liu Bo58113752018-01-31 17:09:13 -07001382 ret = btrfs_cross_ref_exist(root, ino,
1383 found_key.offset -
1384 extent_offset, disk_bytenr);
1385 if (ret) {
1386 /*
1387 * ret could be -EIO if the above fails to read
1388 * metadata.
1389 */
1390 if (ret < 0) {
1391 if (cow_start != (u64)-1)
1392 cur_offset = cow_start;
1393 goto error;
1394 }
1395
1396 WARN_ON_ONCE(nolock);
Yan Zheng17d217f2008-12-12 10:03:38 -05001397 goto out_check;
Liu Bo58113752018-01-31 17:09:13 -07001398 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001399 disk_bytenr += extent_offset;
Yan Zheng17d217f2008-12-12 10:03:38 -05001400 disk_bytenr += cur_offset - found_key.offset;
1401 num_bytes = min(end + 1, extent_end) - cur_offset;
1402 /*
Wang Shilonge9894fd2014-03-27 11:12:25 +08001403 * if there are pending snapshots for this root,
1404 * we fall into common COW way.
1405 */
Robbie Ko8ecebf4d2018-08-06 10:30:30 +08001406 if (!nolock && atomic_read(&root->snapshot_force_cow))
1407 goto out_check;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001408 /*
Yan Zheng17d217f2008-12-12 10:03:38 -05001409 * force cow if csum exists in the range.
1410 * this ensure that csum for a given extent are
1411 * either valid or do not exist.
1412 */
Liu Bo58113752018-01-31 17:09:13 -07001413 ret = csum_exist_in_range(fs_info, disk_bytenr,
1414 num_bytes);
1415 if (ret) {
Liu Bo58113752018-01-31 17:09:13 -07001416 /*
1417 * ret could be -EIO if the above fails to read
1418 * metadata.
1419 */
1420 if (ret < 0) {
1421 if (cow_start != (u64)-1)
1422 cur_offset = cow_start;
1423 goto error;
1424 }
1425 WARN_ON_ONCE(nolock);
Yan Zheng17d217f2008-12-12 10:03:38 -05001426 goto out_check;
Robbie Ko91e1f562016-10-07 10:01:29 +08001427 }
Robbie Ko8ecebf4d2018-08-06 10:30:30 +08001428 if (!btrfs_inc_nocow_writers(fs_info, disk_bytenr))
Filipe Mananaf78c4362016-05-09 13:15:41 +01001429 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001430 nocow = 1;
1431 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1432 extent_end = found_key.offset +
Qu Wenruoe41ca582018-06-06 15:41:49 +08001433 btrfs_file_extent_ram_bytes(leaf, fi);
Jeff Mahoneyda170662016-06-15 09:22:56 -04001434 extent_end = ALIGN(extent_end,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001435 fs_info->sectorsize);
Yan Zheng80ff3852008-10-30 14:20:02 -04001436 } else {
1437 BUG_ON(1);
1438 }
1439out_check:
1440 if (extent_end <= start) {
1441 path->slots[0]++;
Filipe Mananaf78c4362016-05-09 13:15:41 +01001442 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001443 btrfs_dec_nocow_writers(fs_info, disk_bytenr);
Yan Zheng80ff3852008-10-30 14:20:02 -04001444 goto next_slot;
1445 }
1446 if (!nocow) {
1447 if (cow_start == (u64)-1)
1448 cow_start = cur_offset;
1449 cur_offset = extent_end;
1450 if (cur_offset > end)
1451 break;
1452 path->slots[0]++;
1453 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001454 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001455
David Sterbab3b4aa72011-04-21 01:20:15 +02001456 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001457 if (cow_start != (u64)-1) {
Josef Bacik00361582013-08-14 14:02:47 -04001458 ret = cow_file_range(inode, locked_page,
1459 cow_start, found_key.offset - 1,
Wang Xiaoguangdda32452016-07-11 11:05:29 +08001460 end, page_started, nr_written, 1,
1461 NULL);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001462 if (ret) {
Filipe Mananaf78c4362016-05-09 13:15:41 +01001463 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001464 btrfs_dec_nocow_writers(fs_info,
Filipe Mananaf78c4362016-05-09 13:15:41 +01001465 disk_bytenr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001466 goto error;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001467 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001468 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001469 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001470
Yan Zhengd899e052008-10-30 14:25:28 -04001471 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Liu Bo6f9994d2017-01-31 07:50:22 -08001472 u64 orig_start = found_key.offset - extent_offset;
1473
1474 em = create_io_em(inode, cur_offset, num_bytes,
1475 orig_start,
1476 disk_bytenr, /* block_start */
1477 num_bytes, /* block_len */
1478 disk_num_bytes, /* orig_block_len */
1479 ram_bytes, BTRFS_COMPRESS_NONE,
1480 BTRFS_ORDERED_PREALLOC);
1481 if (IS_ERR(em)) {
Liu Bo6f9994d2017-01-31 07:50:22 -08001482 if (nocow)
1483 btrfs_dec_nocow_writers(fs_info,
1484 disk_bytenr);
1485 ret = PTR_ERR(em);
1486 goto error;
Yan Zhengd899e052008-10-30 14:25:28 -04001487 }
Liu Bo6f9994d2017-01-31 07:50:22 -08001488 free_extent_map(em);
1489 }
1490
1491 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zhengd899e052008-10-30 14:25:28 -04001492 type = BTRFS_ORDERED_PREALLOC;
1493 } else {
1494 type = BTRFS_ORDERED_NOCOW;
1495 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001496
1497 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
Yan Zhengd899e052008-10-30 14:25:28 -04001498 num_bytes, num_bytes, type);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001499 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001500 btrfs_dec_nocow_writers(fs_info, disk_bytenr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001501 BUG_ON(ret); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -05001502
Yan, Zhengefa56462010-05-16 10:49:59 -04001503 if (root->root_key.objectid ==
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001504 BTRFS_DATA_RELOC_TREE_OBJECTID)
1505 /*
1506 * Error handled later, as we must prevent
1507 * extent_clear_unlock_delalloc() in error handler
1508 * from freeing metadata of created ordered extent.
1509 */
Yan, Zhengefa56462010-05-16 10:49:59 -04001510 ret = btrfs_reloc_clone_csums(inode, cur_offset,
1511 num_bytes);
Yan, Zhengefa56462010-05-16 10:49:59 -04001512
Josef Bacikc2790a22013-07-29 11:20:47 -04001513 extent_clear_unlock_delalloc(inode, cur_offset,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001514 cur_offset + num_bytes - 1, end,
Josef Bacikc2790a22013-07-29 11:20:47 -04001515 locked_page, EXTENT_LOCKED |
Wang Xiaoguang18513092016-07-25 15:51:40 +08001516 EXTENT_DELALLOC |
1517 EXTENT_CLEAR_DATA_RESV,
1518 PAGE_UNLOCK | PAGE_SET_PRIVATE2);
1519
Yan Zheng80ff3852008-10-30 14:20:02 -04001520 cur_offset = extent_end;
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001521
1522 /*
1523 * btrfs_reloc_clone_csums() error, now we're OK to call error
1524 * handler, as metadata for created ordered extent will only
1525 * be freed by btrfs_finish_ordered_io().
1526 */
1527 if (ret)
1528 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001529 if (cur_offset > end)
1530 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001531 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001532 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001533
Robbie Ko506481b2018-10-30 18:04:04 +08001534 if (cur_offset <= end && cow_start == (u64)-1)
Yan Zheng80ff3852008-10-30 14:20:02 -04001535 cow_start = cur_offset;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001536
Yan Zheng80ff3852008-10-30 14:20:02 -04001537 if (cow_start != (u64)-1) {
Robbie Ko506481b2018-10-30 18:04:04 +08001538 cur_offset = end;
Wang Xiaoguangdda32452016-07-11 11:05:29 +08001539 ret = cow_file_range(inode, locked_page, cow_start, end, end,
1540 page_started, nr_written, 1, NULL);
Josef Bacikd788a342013-10-25 16:55:08 -04001541 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001542 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001543 }
1544
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001545error:
Josef Bacik17ca04a2012-05-31 15:58:55 -04001546 if (ret && cur_offset < end)
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001547 extent_clear_unlock_delalloc(inode, cur_offset, end, end,
Josef Bacikc2790a22013-07-29 11:20:47 -04001548 locked_page, EXTENT_LOCKED |
Josef Bacik151a41b2013-07-29 13:22:24 -04001549 EXTENT_DELALLOC | EXTENT_DEFRAG |
1550 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
1551 PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -04001552 PAGE_SET_WRITEBACK |
1553 PAGE_END_WRITEBACK);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001554 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001555 return ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001556}
1557
Wang Shilong47059d92014-07-03 18:22:07 +08001558static inline int need_force_cow(struct inode *inode, u64 start, u64 end)
1559{
1560
1561 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
1562 !(BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC))
1563 return 0;
1564
1565 /*
1566 * @defrag_bytes is a hint value, no spinlock held here,
1567 * if is not zero, it means the file is defragging.
1568 * Force cow if given extent needs to be defragged.
1569 */
1570 if (BTRFS_I(inode)->defrag_bytes &&
1571 test_range_bit(&BTRFS_I(inode)->io_tree, start, end,
1572 EXTENT_DEFRAG, 0, NULL))
1573 return 1;
1574
1575 return 0;
1576}
1577
Chris Masond352ac62008-09-29 15:18:18 -04001578/*
Nikolay Borisov5eaad972018-11-01 14:09:46 +02001579 * Function to process delayed allocation (create CoW) for ranges which are
1580 * being touched for the first time.
Chris Masond352ac62008-09-29 15:18:18 -04001581 */
Nikolay Borisov5eaad972018-11-01 14:09:46 +02001582int btrfs_run_delalloc_range(void *private_data, struct page *locked_page,
1583 u64 start, u64 end, int *page_started, unsigned long *nr_written,
1584 struct writeback_control *wbc)
Chris Masonbe20aa92007-12-17 20:14:01 -05001585{
Josef Bacikc6100a42017-05-05 11:57:13 -04001586 struct inode *inode = private_data;
Chris Masonbe20aa92007-12-17 20:14:01 -05001587 int ret;
Wang Shilong47059d92014-07-03 18:22:07 +08001588 int force_cow = need_force_cow(inode, start, end);
Liu Bof82b7352017-10-23 23:18:16 -06001589 unsigned int write_flags = wbc_to_write_flags(wbc);
Chris Masona2135012008-06-25 16:01:30 -04001590
Wang Shilong47059d92014-07-03 18:22:07 +08001591 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW && !force_cow) {
Chris Masonc8b97812008-10-29 14:49:59 -04001592 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001593 page_started, 1, nr_written);
Wang Shilong47059d92014-07-03 18:22:07 +08001594 } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC && !force_cow) {
Yan Zhengd899e052008-10-30 14:25:28 -04001595 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001596 page_started, 0, nr_written);
Timofey Titovetsc2fcdcd2017-07-17 16:52:58 +03001597 } else if (!inode_need_compress(inode, start, end)) {
Wang Xiaoguangdda32452016-07-11 11:05:29 +08001598 ret = cow_file_range(inode, locked_page, start, end, end,
1599 page_started, nr_written, 1, NULL);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001600 } else {
1601 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1602 &BTRFS_I(inode)->runtime_flags);
Chris Mason771ed682008-11-06 22:02:51 -05001603 ret = cow_file_range_async(inode, locked_page, start, end,
Liu Bof82b7352017-10-23 23:18:16 -06001604 page_started, nr_written,
1605 write_flags);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001606 }
Qu Wenruo524272602017-03-08 10:25:52 +08001607 if (ret)
1608 btrfs_cleanup_ordered_extents(inode, start, end - start + 1);
Chris Masonb888db2b2007-08-27 16:49:44 -04001609 return ret;
1610}
1611
Nikolay Borisovabbb55f2018-11-01 14:09:53 +02001612void btrfs_split_delalloc_extent(struct inode *inode,
1613 struct extent_state *orig, u64 split)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001614{
Josef Bacikdcab6a32015-02-11 15:08:59 -05001615 u64 size;
1616
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001617 /* not delalloc, ignore it */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001618 if (!(orig->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001619 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001620
Josef Bacikdcab6a32015-02-11 15:08:59 -05001621 size = orig->end - orig->start + 1;
1622 if (size > BTRFS_MAX_EXTENT_SIZE) {
David Sterba823bb202017-01-04 11:09:51 +01001623 u32 num_extents;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001624 u64 new_size;
1625
1626 /*
Nikolay Borisov5c848192018-11-01 14:09:52 +02001627 * See the explanation in btrfs_merge_delalloc_extent, the same
Josef Bacikba117212015-03-13 15:01:24 -04001628 * applies here, just in reverse.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001629 */
1630 new_size = orig->end - split + 1;
David Sterba823bb202017-01-04 11:09:51 +01001631 num_extents = count_max_extents(new_size);
Josef Bacikba117212015-03-13 15:01:24 -04001632 new_size = split - orig->start;
David Sterba823bb202017-01-04 11:09:51 +01001633 num_extents += count_max_extents(new_size);
1634 if (count_max_extents(size) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001635 return;
1636 }
1637
Josef Bacik9e0baf62011-07-15 15:16:44 +00001638 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04001639 btrfs_mod_outstanding_extents(BTRFS_I(inode), 1);
Josef Bacik9e0baf62011-07-15 15:16:44 +00001640 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001641}
1642
1643/*
Nikolay Borisov5c848192018-11-01 14:09:52 +02001644 * Handle merged delayed allocation extents so we can keep track of new extents
1645 * that are just merged onto old extents, such as when we are doing sequential
1646 * writes, so we can properly account for the metadata space we'll need.
Josef Bacik9ed74f22009-09-11 16:12:44 -04001647 */
Nikolay Borisov5c848192018-11-01 14:09:52 +02001648void btrfs_merge_delalloc_extent(struct inode *inode, struct extent_state *new,
1649 struct extent_state *other)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001650{
Josef Bacikdcab6a32015-02-11 15:08:59 -05001651 u64 new_size, old_size;
David Sterba823bb202017-01-04 11:09:51 +01001652 u32 num_extents;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001653
Josef Bacik9ed74f22009-09-11 16:12:44 -04001654 /* not delalloc, ignore it */
1655 if (!(other->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001656 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001657
Josef Bacik8461a3d2015-03-13 15:12:08 -04001658 if (new->start > other->start)
1659 new_size = new->end - other->start + 1;
1660 else
1661 new_size = other->end - new->start + 1;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001662
1663 /* we're not bigger than the max, unreserve the space and go */
1664 if (new_size <= BTRFS_MAX_EXTENT_SIZE) {
1665 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04001666 btrfs_mod_outstanding_extents(BTRFS_I(inode), -1);
Josef Bacikdcab6a32015-02-11 15:08:59 -05001667 spin_unlock(&BTRFS_I(inode)->lock);
1668 return;
1669 }
1670
1671 /*
Josef Bacikba117212015-03-13 15:01:24 -04001672 * We have to add up either side to figure out how many extents were
1673 * accounted for before we merged into one big extent. If the number of
1674 * extents we accounted for is <= the amount we need for the new range
1675 * then we can return, otherwise drop. Think of it like this
1676 *
1677 * [ 4k][MAX_SIZE]
1678 *
1679 * So we've grown the extent by a MAX_SIZE extent, this would mean we
1680 * need 2 outstanding extents, on one side we have 1 and the other side
1681 * we have 1 so they are == and we can return. But in this case
1682 *
1683 * [MAX_SIZE+4k][MAX_SIZE+4k]
1684 *
1685 * Each range on their own accounts for 2 extents, but merged together
1686 * they are only 3 extents worth of accounting, so we need to drop in
1687 * this case.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001688 */
Josef Bacikba117212015-03-13 15:01:24 -04001689 old_size = other->end - other->start + 1;
David Sterba823bb202017-01-04 11:09:51 +01001690 num_extents = count_max_extents(old_size);
Josef Bacikba117212015-03-13 15:01:24 -04001691 old_size = new->end - new->start + 1;
David Sterba823bb202017-01-04 11:09:51 +01001692 num_extents += count_max_extents(old_size);
1693 if (count_max_extents(new_size) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001694 return;
1695
Josef Bacik9e0baf62011-07-15 15:16:44 +00001696 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04001697 btrfs_mod_outstanding_extents(BTRFS_I(inode), -1);
Josef Bacik9e0baf62011-07-15 15:16:44 +00001698 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001699}
1700
Miao Xieeb73c1b2013-05-15 07:48:22 +00001701static void btrfs_add_delalloc_inodes(struct btrfs_root *root,
1702 struct inode *inode)
1703{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001704 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1705
Miao Xieeb73c1b2013-05-15 07:48:22 +00001706 spin_lock(&root->delalloc_lock);
1707 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1708 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1709 &root->delalloc_inodes);
1710 set_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1711 &BTRFS_I(inode)->runtime_flags);
1712 root->nr_delalloc_inodes++;
1713 if (root->nr_delalloc_inodes == 1) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001714 spin_lock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001715 BUG_ON(!list_empty(&root->delalloc_root));
1716 list_add_tail(&root->delalloc_root,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001717 &fs_info->delalloc_roots);
1718 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001719 }
1720 }
1721 spin_unlock(&root->delalloc_lock);
1722}
1723
Nikolay Borisov2b877332018-04-27 12:21:51 +03001724
1725void __btrfs_del_delalloc_inode(struct btrfs_root *root,
1726 struct btrfs_inode *inode)
Miao Xieeb73c1b2013-05-15 07:48:22 +00001727{
David Sterba3ffbd682018-06-29 10:56:42 +02001728 struct btrfs_fs_info *fs_info = root->fs_info;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001729
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001730 if (!list_empty(&inode->delalloc_inodes)) {
1731 list_del_init(&inode->delalloc_inodes);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001732 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001733 &inode->runtime_flags);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001734 root->nr_delalloc_inodes--;
1735 if (!root->nr_delalloc_inodes) {
Nikolay Borisov7c8a0d32018-04-27 12:21:52 +03001736 ASSERT(list_empty(&root->delalloc_inodes));
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001737 spin_lock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001738 BUG_ON(list_empty(&root->delalloc_root));
1739 list_del_init(&root->delalloc_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001740 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001741 }
1742 }
Nikolay Borisov2b877332018-04-27 12:21:51 +03001743}
1744
1745static void btrfs_del_delalloc_inode(struct btrfs_root *root,
1746 struct btrfs_inode *inode)
1747{
1748 spin_lock(&root->delalloc_lock);
1749 __btrfs_del_delalloc_inode(root, inode);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001750 spin_unlock(&root->delalloc_lock);
1751}
1752
Chris Masond352ac62008-09-29 15:18:18 -04001753/*
Nikolay Borisove06a1fc2018-11-01 14:09:50 +02001754 * Properly track delayed allocation bytes in the inode and to maintain the
1755 * list of inodes that have pending delalloc work to be done.
Chris Masond352ac62008-09-29 15:18:18 -04001756 */
Nikolay Borisove06a1fc2018-11-01 14:09:50 +02001757void btrfs_set_delalloc_extent(struct inode *inode, struct extent_state *state,
1758 unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001759{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001760 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1761
Wang Shilong47059d92014-07-03 18:22:07 +08001762 if ((*bits & EXTENT_DEFRAG) && !(*bits & EXTENT_DELALLOC))
1763 WARN_ON(1);
Chris Mason75eff682008-12-15 15:54:40 -05001764 /*
1765 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001766 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001767 * bit, which is only set or cleared with irqs on
1768 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001769 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001770 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001771 u64 len = state->end + 1 - state->start;
Josef Bacik8b62f872017-10-19 14:15:55 -04001772 u32 num_extents = count_max_extents(len);
Nikolay Borisov70ddc552017-02-20 13:50:35 +02001773 bool do_list = !btrfs_is_free_space_inode(BTRFS_I(inode));
Josef Bacik9ed74f22009-09-11 16:12:44 -04001774
Josef Bacik8b62f872017-10-19 14:15:55 -04001775 spin_lock(&BTRFS_I(inode)->lock);
1776 btrfs_mod_outstanding_extents(BTRFS_I(inode), num_extents);
1777 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik287a0ab2010-03-19 18:07:23 +00001778
Josef Bacik6a3891c2015-03-16 17:38:52 -04001779 /* For sanity tests */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001780 if (btrfs_is_testing(fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04001781 return;
1782
Nikolay Borisov104b4e52017-06-20 21:01:20 +03001783 percpu_counter_add_batch(&fs_info->delalloc_bytes, len,
1784 fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001785 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001786 BTRFS_I(inode)->delalloc_bytes += len;
Wang Shilong47059d92014-07-03 18:22:07 +08001787 if (*bits & EXTENT_DEFRAG)
1788 BTRFS_I(inode)->defrag_bytes += len;
Miao Xiedf0af1a2013-01-29 10:11:59 +00001789 if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00001790 &BTRFS_I(inode)->runtime_flags))
1791 btrfs_add_delalloc_inodes(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001792 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001793 }
Filipe Mananaa7e3b972017-04-03 10:45:46 +01001794
1795 if (!(state->state & EXTENT_DELALLOC_NEW) &&
1796 (*bits & EXTENT_DELALLOC_NEW)) {
1797 spin_lock(&BTRFS_I(inode)->lock);
1798 BTRFS_I(inode)->new_delalloc_bytes += state->end + 1 -
1799 state->start;
1800 spin_unlock(&BTRFS_I(inode)->lock);
1801 }
Chris Mason291d6732008-01-29 15:55:23 -05001802}
1803
Chris Masond352ac62008-09-29 15:18:18 -04001804/*
Nikolay Borisova36bb5f2018-11-01 14:09:51 +02001805 * Once a range is no longer delalloc this function ensures that proper
1806 * accounting happens.
Chris Masond352ac62008-09-29 15:18:18 -04001807 */
Nikolay Borisova36bb5f2018-11-01 14:09:51 +02001808void btrfs_clear_delalloc_extent(struct inode *vfs_inode,
1809 struct extent_state *state, unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001810{
Nikolay Borisova36bb5f2018-11-01 14:09:51 +02001811 struct btrfs_inode *inode = BTRFS_I(vfs_inode);
1812 struct btrfs_fs_info *fs_info = btrfs_sb(vfs_inode->i_sb);
Wang Shilong47059d92014-07-03 18:22:07 +08001813 u64 len = state->end + 1 - state->start;
David Sterba823bb202017-01-04 11:09:51 +01001814 u32 num_extents = count_max_extents(len);
Wang Shilong47059d92014-07-03 18:22:07 +08001815
Filipe Manana4a4b9642017-07-27 19:52:55 +01001816 if ((state->state & EXTENT_DEFRAG) && (*bits & EXTENT_DEFRAG)) {
1817 spin_lock(&inode->lock);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001818 inode->defrag_bytes -= len;
Filipe Manana4a4b9642017-07-27 19:52:55 +01001819 spin_unlock(&inode->lock);
1820 }
Wang Shilong47059d92014-07-03 18:22:07 +08001821
Chris Mason75eff682008-12-15 15:54:40 -05001822 /*
1823 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001824 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001825 * bit, which is only set or cleared with irqs on
1826 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001827 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001828 struct btrfs_root *root = inode->root;
Liu Bo83eea1f2012-07-10 05:28:39 -06001829 bool do_list = !btrfs_is_free_space_inode(inode);
Chris Masonbcbfce82008-04-22 13:26:47 -04001830
Josef Bacik8b62f872017-10-19 14:15:55 -04001831 spin_lock(&inode->lock);
1832 btrfs_mod_outstanding_extents(inode, -num_extents);
1833 spin_unlock(&inode->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001834
Josef Bacikb6d08f02013-09-27 14:57:43 -04001835 /*
1836 * We don't reserve metadata space for space cache inodes so we
1837 * don't need to call dellalloc_release_metadata if there is an
1838 * error.
1839 */
Filipe Mananaa315e682017-03-06 23:04:20 +00001840 if (*bits & EXTENT_CLEAR_META_RESV &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001841 root != fs_info->tree_root)
Qu Wenruo43b18592017-12-12 15:34:32 +08001842 btrfs_delalloc_release_metadata(inode, len, false);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001843
Josef Bacik6a3891c2015-03-16 17:38:52 -04001844 /* For sanity tests. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001845 if (btrfs_is_testing(fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04001846 return;
1847
Filipe Mananaa315e682017-03-06 23:04:20 +00001848 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID &&
1849 do_list && !(state->state & EXTENT_NORESERVE) &&
1850 (*bits & EXTENT_CLEAR_DATA_RESV))
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001851 btrfs_free_reserved_data_space_noquota(
1852 &inode->vfs_inode,
Qu Wenruo51773be2015-10-08 18:19:37 +08001853 state->start, len);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001854
Nikolay Borisov104b4e52017-06-20 21:01:20 +03001855 percpu_counter_add_batch(&fs_info->delalloc_bytes, -len,
1856 fs_info->delalloc_batch);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001857 spin_lock(&inode->lock);
1858 inode->delalloc_bytes -= len;
1859 if (do_list && inode->delalloc_bytes == 0 &&
Miao Xiedf0af1a2013-01-29 10:11:59 +00001860 test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001861 &inode->runtime_flags))
Miao Xieeb73c1b2013-05-15 07:48:22 +00001862 btrfs_del_delalloc_inode(root, inode);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001863 spin_unlock(&inode->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001864 }
Filipe Mananaa7e3b972017-04-03 10:45:46 +01001865
1866 if ((state->state & EXTENT_DELALLOC_NEW) &&
1867 (*bits & EXTENT_DELALLOC_NEW)) {
1868 spin_lock(&inode->lock);
1869 ASSERT(inode->new_delalloc_bytes >= len);
1870 inode->new_delalloc_bytes -= len;
1871 spin_unlock(&inode->lock);
1872 }
Chris Mason291d6732008-01-29 15:55:23 -05001873}
1874
Chris Masond352ac62008-09-29 15:18:18 -04001875/*
David Sterba00032d32018-07-18 19:28:09 +02001876 * Merge bio hook, this must check the chunk tree to make sure we don't create
1877 * bios that span stripes or chunks
Liu Bo6f034ec2016-06-22 18:31:49 -07001878 *
1879 * return 1 if page cannot be merged to bio
1880 * return 0 if page can be merged to bio
1881 * return error otherwise
Chris Masond352ac62008-09-29 15:18:18 -04001882 */
Mike Christie81a75f672016-06-05 14:31:54 -05001883int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
Chris Masonc8b97812008-10-29 14:49:59 -04001884 size_t size, struct bio *bio,
1885 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04001886{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001887 struct inode *inode = page->mapping->host;
1888 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001889 u64 logical = (u64)bio->bi_iter.bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04001890 u64 length = 0;
1891 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04001892 int ret;
1893
Chris Mason771ed682008-11-06 22:02:51 -05001894 if (bio_flags & EXTENT_BIO_COMPRESSED)
1895 return 0;
1896
Kent Overstreet4f024f32013-10-11 15:44:27 -07001897 length = bio->bi_iter.bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04001898 map_length = length;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001899 ret = btrfs_map_block(fs_info, btrfs_op(bio), logical, &map_length,
1900 NULL, 0);
Liu Bo6f034ec2016-06-22 18:31:49 -07001901 if (ret < 0)
1902 return ret;
Chris Masond3977122009-01-05 21:25:51 -05001903 if (map_length < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04001904 return 1;
Jeff Mahoney3444a972011-10-03 23:23:13 -04001905 return 0;
Chris Mason239b14b2008-03-24 15:02:07 -04001906}
1907
Chris Masond352ac62008-09-29 15:18:18 -04001908/*
1909 * in order to insert checksums into the metadata in large chunks,
1910 * we wait until bio submission time. All the pages in the bio are
1911 * checksummed and sums are attached onto the ordered extent record.
1912 *
1913 * At IO completion time the cums attached on the ordered extent record
1914 * are inserted into the btree
1915 */
David Sterbad0ee3932018-03-08 14:35:48 +01001916static blk_status_t btrfs_submit_bio_start(void *private_data, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001917 u64 bio_offset)
Chris Mason065631f2008-02-20 12:07:25 -05001918{
Josef Bacikc6100a42017-05-05 11:57:13 -04001919 struct inode *inode = private_data;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001920 blk_status_t ret = 0;
Chris Masone0156402008-04-16 11:15:20 -04001921
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001922 ret = btrfs_csum_one_bio(inode, bio, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001923 BUG_ON(ret); /* -ENOMEM */
Chris Mason4a69a412008-11-06 22:03:00 -05001924 return 0;
1925}
Chris Masone0156402008-04-16 11:15:20 -04001926
Chris Mason4a69a412008-11-06 22:03:00 -05001927/*
1928 * in order to insert checksums into the metadata in large chunks,
1929 * we wait until bio submission time. All the pages in the bio are
1930 * checksummed and sums are attached onto the ordered extent record.
1931 *
1932 * At IO completion time the cums attached on the ordered extent record
1933 * are inserted into the btree
1934 */
David Sterbae288c082018-07-18 17:36:24 +02001935blk_status_t btrfs_submit_bio_done(void *private_data, struct bio *bio,
David Sterba6c553432018-03-08 13:47:33 +01001936 int mirror_num)
Chris Mason4a69a412008-11-06 22:03:00 -05001937{
Josef Bacikc6100a42017-05-05 11:57:13 -04001938 struct inode *inode = private_data;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001939 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001940 blk_status_t ret;
Stefan Behrens61891922012-11-05 18:51:52 +01001941
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001942 ret = btrfs_map_bio(fs_info, bio, mirror_num, 1);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001943 if (ret) {
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001944 bio->bi_status = ret;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001945 bio_endio(bio);
1946 }
Stefan Behrens61891922012-11-05 18:51:52 +01001947 return ret;
Chris Mason44b8bd72008-04-16 11:14:51 -04001948}
1949
Chris Masond352ac62008-09-29 15:18:18 -04001950/*
Chris Masoncad321a2008-12-17 14:51:42 -05001951 * extent_io.c submission hook. This does the right thing for csum calculation
Liu Bo4c274bc2017-11-01 17:19:27 -06001952 * on write, or reading the csums from the tree before a read.
1953 *
1954 * Rules about async/sync submit,
1955 * a) read: sync submit
1956 *
1957 * b) write without checksum: sync submit
1958 *
1959 * c) write with checksum:
1960 * c-1) if bio is issued by fsync: sync submit
1961 * (sync_writers != 0)
1962 *
1963 * c-2) if root is reloc root: sync submit
1964 * (only in case of buffered IO)
1965 *
1966 * c-3) otherwise: async submit
Chris Masond352ac62008-09-29 15:18:18 -04001967 */
Linus Torvalds8c27cb32017-07-05 16:41:23 -07001968static blk_status_t btrfs_submit_bio_hook(void *private_data, struct bio *bio,
Josef Bacikc6100a42017-05-05 11:57:13 -04001969 int mirror_num, unsigned long bio_flags,
1970 u64 bio_offset)
Chris Mason44b8bd72008-04-16 11:14:51 -04001971{
Josef Bacikc6100a42017-05-05 11:57:13 -04001972 struct inode *inode = private_data;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001973 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason44b8bd72008-04-16 11:14:51 -04001974 struct btrfs_root *root = BTRFS_I(inode)->root;
Chandan Rajendra0d51e282015-07-27 15:26:43 +05301975 enum btrfs_wq_endio_type metadata = BTRFS_WQ_ENDIO_DATA;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001976 blk_status_t ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001977 int skip_sum;
Josef Bacikb812ce22012-11-16 13:56:32 -05001978 int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
Chris Mason44b8bd72008-04-16 11:14:51 -04001979
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001980 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masoncad321a2008-12-17 14:51:42 -05001981
Nikolay Borisov70ddc552017-02-20 13:50:35 +02001982 if (btrfs_is_free_space_inode(BTRFS_I(inode)))
Chandan Rajendra0d51e282015-07-27 15:26:43 +05301983 metadata = BTRFS_WQ_ENDIO_FREE_SPACE;
Jeff Mahoney04173412011-10-03 23:23:12 -04001984
Mike Christie37226b22016-06-05 14:31:52 -05001985 if (bio_op(bio) != REQ_OP_WRITE) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001986 ret = btrfs_bio_wq_end_io(fs_info, bio, metadata);
Josef Bacik5fd02042012-05-02 14:00:54 -04001987 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001988 goto out;
Josef Bacik5fd02042012-05-02 14:00:54 -04001989
Chris Masond20f7042008-12-08 16:58:54 -05001990 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Stefan Behrens61891922012-11-05 18:51:52 +01001991 ret = btrfs_submit_compressed_read(inode, bio,
1992 mirror_num,
1993 bio_flags);
1994 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001995 } else if (!skip_sum) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001996 ret = btrfs_lookup_bio_sums(inode, bio, NULL);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001997 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001998 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001999 }
Chris Mason4d1b5fb2008-08-20 09:44:52 -04002000 goto mapit;
Josef Bacikb812ce22012-11-16 13:56:32 -05002001 } else if (async && !skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05002002 /* csum items have already been cloned */
2003 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
2004 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04002005 /* we're doing a write, do the async checksumming */
Josef Bacikc6100a42017-05-05 11:57:13 -04002006 ret = btrfs_wq_submit_bio(fs_info, bio, mirror_num, bio_flags,
2007 bio_offset, inode,
David Sterbae288c082018-07-18 17:36:24 +02002008 btrfs_submit_bio_start);
Stefan Behrens61891922012-11-05 18:51:52 +01002009 goto out;
Josef Bacikb812ce22012-11-16 13:56:32 -05002010 } else if (!skip_sum) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002011 ret = btrfs_csum_one_bio(inode, bio, 0, 0);
Josef Bacikb812ce22012-11-16 13:56:32 -05002012 if (ret)
2013 goto out;
Chris Mason19b9bdb2008-10-30 14:23:13 -04002014 }
2015
Chris Mason0b86a832008-03-24 15:01:56 -04002016mapit:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002017 ret = btrfs_map_bio(fs_info, bio, mirror_num, 0);
Stefan Behrens61891922012-11-05 18:51:52 +01002018
2019out:
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002020 if (ret) {
2021 bio->bi_status = ret;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002022 bio_endio(bio);
2023 }
Stefan Behrens61891922012-11-05 18:51:52 +01002024 return ret;
Chris Mason065631f2008-02-20 12:07:25 -05002025}
Chris Mason6885f302008-02-20 16:11:05 -05002026
Chris Masond352ac62008-09-29 15:18:18 -04002027/*
2028 * given a list of ordered sums record them in the inode. This happens
2029 * at IO completion time based on sums calculated at bio submission time.
2030 */
Chris Masonba1da2f2008-07-17 12:54:15 -04002031static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
David Sterbadf9f6282017-02-10 19:35:37 +01002032 struct inode *inode, struct list_head *list)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002033{
Chris Masone6dcd2d2008-07-17 12:53:50 -04002034 struct btrfs_ordered_sum *sum;
Nikolay Borisovac01f262018-01-08 10:59:43 +02002035 int ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002036
Qinghuang Fengc6e30872009-01-21 10:59:08 -05002037 list_for_each_entry(sum, list, list) {
David Sterba7c2871a2017-11-08 01:07:43 +01002038 trans->adding_csums = true;
Nikolay Borisovac01f262018-01-08 10:59:43 +02002039 ret = btrfs_csum_file_blocks(trans,
Chris Masond20f7042008-12-08 16:58:54 -05002040 BTRFS_I(inode)->root->fs_info->csum_root, sum);
David Sterba7c2871a2017-11-08 01:07:43 +01002041 trans->adding_csums = false;
Nikolay Borisovac01f262018-01-08 10:59:43 +02002042 if (ret)
2043 return ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002044 }
2045 return 0;
2046}
2047
Josef Bacik2ac55d42010-02-03 19:33:23 +00002048int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
Filipe Mananae3b8a482017-11-04 00:16:59 +00002049 unsigned int extra_bits,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08002050 struct extent_state **cached_state, int dedupe)
Chris Masonea8c2812008-08-04 23:17:27 -04002051{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002052 WARN_ON((end & (PAGE_SIZE - 1)) == 0);
Chris Masonea8c2812008-08-04 23:17:27 -04002053 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
Filipe Mananae3b8a482017-11-04 00:16:59 +00002054 extra_bits, cached_state);
Chris Masonea8c2812008-08-04 23:17:27 -04002055}
2056
Chris Masond352ac62008-09-29 15:18:18 -04002057/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04002058struct btrfs_writepage_fixup {
2059 struct page *page;
2060 struct btrfs_work work;
2061};
2062
Christoph Hellwigb2950862008-12-02 09:54:17 -05002063static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04002064{
2065 struct btrfs_writepage_fixup *fixup;
2066 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002067 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08002068 struct extent_changeset *data_reserved = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04002069 struct page *page;
2070 struct inode *inode;
2071 u64 page_start;
2072 u64 page_end;
Jeff Mahoney87826df2012-02-15 16:23:57 +01002073 int ret;
Chris Mason247e7432008-07-17 12:53:51 -04002074
2075 fixup = container_of(work, struct btrfs_writepage_fixup, work);
2076 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -04002077again:
Chris Mason247e7432008-07-17 12:53:51 -04002078 lock_page(page);
2079 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
2080 ClearPageChecked(page);
2081 goto out_page;
2082 }
2083
2084 inode = page->mapping->host;
2085 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002086 page_end = page_offset(page) + PAGE_SIZE - 1;
Chris Mason247e7432008-07-17 12:53:51 -04002087
David Sterbaff13db42015-12-03 14:30:40 +01002088 lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002089 &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04002090
2091 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04002092 if (PagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04002093 goto out;
Chris Mason4a096752008-07-21 10:29:44 -04002094
Nikolay Borisova776c6f2017-02-20 13:50:49 +02002095 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002096 PAGE_SIZE);
Chris Mason4a096752008-07-21 10:29:44 -04002097 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00002098 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
David Sterbae43bbe52017-12-12 21:43:52 +01002099 page_end, &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04002100 unlock_page(page);
2101 btrfs_start_ordered_extent(inode, ordered, 1);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002102 btrfs_put_ordered_extent(ordered);
Chris Mason4a096752008-07-21 10:29:44 -04002103 goto again;
2104 }
Chris Mason247e7432008-07-17 12:53:51 -04002105
Qu Wenruo364ecf32017-02-27 15:10:38 +08002106 ret = btrfs_delalloc_reserve_space(inode, &data_reserved, page_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002107 PAGE_SIZE);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002108 if (ret) {
2109 mapping_set_error(page->mapping, ret);
2110 end_extent_writepage(page, ret, page_start, page_end);
2111 ClearPageChecked(page);
2112 goto out;
2113 }
2114
Nikolay Borisovf3038ee2017-12-05 09:29:19 +02002115 ret = btrfs_set_extent_delalloc(inode, page_start, page_end, 0,
2116 &cached_state, 0);
2117 if (ret) {
2118 mapping_set_error(page->mapping, ret);
2119 end_extent_writepage(page, ret, page_start, page_end);
2120 ClearPageChecked(page);
2121 goto out;
2122 }
2123
Chris Mason247e7432008-07-17 12:53:51 -04002124 ClearPageChecked(page);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002125 set_page_dirty(page);
Qu Wenruo43b18592017-12-12 15:34:32 +08002126 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE, false);
Chris Mason247e7432008-07-17 12:53:51 -04002127out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00002128 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01002129 &cached_state);
Chris Mason247e7432008-07-17 12:53:51 -04002130out_page:
2131 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002132 put_page(page);
Miao Xieb897abe2011-01-26 16:19:22 +08002133 kfree(fixup);
Qu Wenruo364ecf32017-02-27 15:10:38 +08002134 extent_changeset_free(data_reserved);
Chris Mason247e7432008-07-17 12:53:51 -04002135}
2136
2137/*
2138 * There are a few paths in the higher layers of the kernel that directly
2139 * set the page dirty bit without asking the filesystem if it is a
2140 * good idea. This causes problems because we want to make sure COW
2141 * properly happens and the data=ordered rules are followed.
2142 *
Chris Masonc8b97812008-10-29 14:49:59 -04002143 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04002144 * hasn't been properly setup for IO. We kick off an async process
2145 * to fix it up. The async helper will wait for ordered extents, set
2146 * the delalloc bit and make it safe to write the page.
2147 */
Nikolay Borisovd75855b2018-11-01 14:09:47 +02002148int btrfs_writepage_cow_fixup(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04002149{
2150 struct inode *inode = page->mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002151 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason247e7432008-07-17 12:53:51 -04002152 struct btrfs_writepage_fixup *fixup;
Chris Mason247e7432008-07-17 12:53:51 -04002153
Chris Mason8b62b722009-09-02 16:53:46 -04002154 /* this page is properly in the ordered list */
2155 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04002156 return 0;
2157
2158 if (PageChecked(page))
2159 return -EAGAIN;
2160
2161 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
2162 if (!fixup)
2163 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04002164
Chris Mason247e7432008-07-17 12:53:51 -04002165 SetPageChecked(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002166 get_page(page);
Liu Bo9e0af232014-08-15 23:36:53 +08002167 btrfs_init_work(&fixup->work, btrfs_fixup_helper,
2168 btrfs_writepage_fixup_worker, NULL, NULL);
Chris Mason247e7432008-07-17 12:53:51 -04002169 fixup->page = page;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002170 btrfs_queue_work(fs_info->fixup_workers, &fixup->work);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002171 return -EBUSY;
Chris Mason247e7432008-07-17 12:53:51 -04002172}
2173
Yan Zhengd899e052008-10-30 14:25:28 -04002174static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
2175 struct inode *inode, u64 file_pos,
2176 u64 disk_bytenr, u64 disk_num_bytes,
2177 u64 num_bytes, u64 ram_bytes,
2178 u8 compression, u8 encryption,
2179 u16 other_encoding, int extent_type)
2180{
2181 struct btrfs_root *root = BTRFS_I(inode)->root;
2182 struct btrfs_file_extent_item *fi;
2183 struct btrfs_path *path;
2184 struct extent_buffer *leaf;
2185 struct btrfs_key ins;
Qu Wenruoa12b8772017-02-27 15:10:37 +08002186 u64 qg_released;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002187 int extent_inserted = 0;
Yan Zhengd899e052008-10-30 14:25:28 -04002188 int ret;
2189
2190 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07002191 if (!path)
2192 return -ENOMEM;
Yan Zhengd899e052008-10-30 14:25:28 -04002193
Chris Masona1ed8352009-09-11 12:27:37 -04002194 /*
2195 * we may be replacing one extent in the tree with another.
2196 * The new extent is pinned in the extent map, and we don't want
2197 * to drop it from the cache until it is completely in the btree.
2198 *
2199 * So, tell btrfs_drop_extents to leave this extent in the cache.
2200 * the caller is expected to unpin it and allow it to be merged
2201 * with the others.
2202 */
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002203 ret = __btrfs_drop_extents(trans, root, inode, path, file_pos,
2204 file_pos + num_bytes, NULL, 0,
2205 1, sizeof(*fi), &extent_inserted);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002206 if (ret)
2207 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04002208
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002209 if (!extent_inserted) {
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002210 ins.objectid = btrfs_ino(BTRFS_I(inode));
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002211 ins.offset = file_pos;
2212 ins.type = BTRFS_EXTENT_DATA_KEY;
2213
2214 path->leave_spinning = 1;
2215 ret = btrfs_insert_empty_item(trans, root, path, &ins,
2216 sizeof(*fi));
2217 if (ret)
2218 goto out;
2219 }
Yan Zhengd899e052008-10-30 14:25:28 -04002220 leaf = path->nodes[0];
2221 fi = btrfs_item_ptr(leaf, path->slots[0],
2222 struct btrfs_file_extent_item);
2223 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
2224 btrfs_set_file_extent_type(leaf, fi, extent_type);
2225 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
2226 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
2227 btrfs_set_file_extent_offset(leaf, fi, 0);
2228 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
2229 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
2230 btrfs_set_file_extent_compression(leaf, fi, compression);
2231 btrfs_set_file_extent_encryption(leaf, fi, encryption);
2232 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
Chris Masonb9473432009-03-13 11:00:37 -04002233
Yan Zhengd899e052008-10-30 14:25:28 -04002234 btrfs_mark_buffer_dirty(leaf);
Josef Bacikce195332012-09-25 15:26:16 -04002235 btrfs_release_path(path);
Yan Zhengd899e052008-10-30 14:25:28 -04002236
2237 inode_add_bytes(inode, num_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -04002238
2239 ins.objectid = disk_bytenr;
2240 ins.offset = disk_num_bytes;
2241 ins.type = BTRFS_EXTENT_ITEM_KEY;
Qu Wenruoa12b8772017-02-27 15:10:37 +08002242
Qu Wenruo297d7502015-09-08 17:08:37 +08002243 /*
Qu Wenruo5846a3c2015-10-26 14:11:18 +08002244 * Release the reserved range from inode dirty range map, as it is
2245 * already moved into delayed_ref_head
Qu Wenruo297d7502015-09-08 17:08:37 +08002246 */
Qu Wenruoa12b8772017-02-27 15:10:37 +08002247 ret = btrfs_qgroup_release_data(inode, file_pos, ram_bytes);
2248 if (ret < 0)
2249 goto out;
2250 qg_released = ret;
Josef Bacik84f7d8e2017-09-29 15:43:49 -04002251 ret = btrfs_alloc_reserved_file_extent(trans, root,
2252 btrfs_ino(BTRFS_I(inode)),
2253 file_pos, qg_released, &ins);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002254out:
Yan Zhengd899e052008-10-30 14:25:28 -04002255 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04002256
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002257 return ret;
Yan Zhengd899e052008-10-30 14:25:28 -04002258}
2259
Liu Bo38c227d2013-01-29 03:18:40 +00002260/* snapshot-aware defrag */
2261struct sa_defrag_extent_backref {
2262 struct rb_node node;
2263 struct old_sa_defrag_extent *old;
2264 u64 root_id;
2265 u64 inum;
2266 u64 file_pos;
2267 u64 extent_offset;
2268 u64 num_bytes;
2269 u64 generation;
2270};
2271
2272struct old_sa_defrag_extent {
2273 struct list_head list;
2274 struct new_sa_defrag_extent *new;
2275
2276 u64 extent_offset;
2277 u64 bytenr;
2278 u64 offset;
2279 u64 len;
2280 int count;
2281};
2282
2283struct new_sa_defrag_extent {
2284 struct rb_root root;
2285 struct list_head head;
2286 struct btrfs_path *path;
2287 struct inode *inode;
2288 u64 file_pos;
2289 u64 len;
2290 u64 bytenr;
2291 u64 disk_len;
2292 u8 compress_type;
2293};
2294
2295static int backref_comp(struct sa_defrag_extent_backref *b1,
2296 struct sa_defrag_extent_backref *b2)
2297{
2298 if (b1->root_id < b2->root_id)
2299 return -1;
2300 else if (b1->root_id > b2->root_id)
2301 return 1;
2302
2303 if (b1->inum < b2->inum)
2304 return -1;
2305 else if (b1->inum > b2->inum)
2306 return 1;
2307
2308 if (b1->file_pos < b2->file_pos)
2309 return -1;
2310 else if (b1->file_pos > b2->file_pos)
2311 return 1;
2312
2313 /*
2314 * [------------------------------] ===> (a range of space)
2315 * |<--->| |<---->| =============> (fs/file tree A)
2316 * |<---------------------------->| ===> (fs/file tree B)
2317 *
2318 * A range of space can refer to two file extents in one tree while
2319 * refer to only one file extent in another tree.
2320 *
2321 * So we may process a disk offset more than one time(two extents in A)
2322 * and locate at the same extent(one extent in B), then insert two same
2323 * backrefs(both refer to the extent in B).
2324 */
2325 return 0;
2326}
2327
2328static void backref_insert(struct rb_root *root,
2329 struct sa_defrag_extent_backref *backref)
2330{
2331 struct rb_node **p = &root->rb_node;
2332 struct rb_node *parent = NULL;
2333 struct sa_defrag_extent_backref *entry;
2334 int ret;
2335
2336 while (*p) {
2337 parent = *p;
2338 entry = rb_entry(parent, struct sa_defrag_extent_backref, node);
2339
2340 ret = backref_comp(backref, entry);
2341 if (ret < 0)
2342 p = &(*p)->rb_left;
2343 else
2344 p = &(*p)->rb_right;
2345 }
2346
2347 rb_link_node(&backref->node, parent, p);
2348 rb_insert_color(&backref->node, root);
2349}
2350
2351/*
2352 * Note the backref might has changed, and in this case we just return 0.
2353 */
2354static noinline int record_one_backref(u64 inum, u64 offset, u64 root_id,
2355 void *ctx)
2356{
2357 struct btrfs_file_extent_item *extent;
Liu Bo38c227d2013-01-29 03:18:40 +00002358 struct old_sa_defrag_extent *old = ctx;
2359 struct new_sa_defrag_extent *new = old->new;
2360 struct btrfs_path *path = new->path;
2361 struct btrfs_key key;
2362 struct btrfs_root *root;
2363 struct sa_defrag_extent_backref *backref;
2364 struct extent_buffer *leaf;
2365 struct inode *inode = new->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002366 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002367 int slot;
2368 int ret;
2369 u64 extent_offset;
2370 u64 num_bytes;
2371
2372 if (BTRFS_I(inode)->root->root_key.objectid == root_id &&
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002373 inum == btrfs_ino(BTRFS_I(inode)))
Liu Bo38c227d2013-01-29 03:18:40 +00002374 return 0;
2375
2376 key.objectid = root_id;
2377 key.type = BTRFS_ROOT_ITEM_KEY;
2378 key.offset = (u64)-1;
2379
Liu Bo38c227d2013-01-29 03:18:40 +00002380 root = btrfs_read_fs_root_no_name(fs_info, &key);
2381 if (IS_ERR(root)) {
2382 if (PTR_ERR(root) == -ENOENT)
2383 return 0;
2384 WARN_ON(1);
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002385 btrfs_debug(fs_info, "inum=%llu, offset=%llu, root_id=%llu",
Liu Bo38c227d2013-01-29 03:18:40 +00002386 inum, offset, root_id);
2387 return PTR_ERR(root);
2388 }
2389
2390 key.objectid = inum;
2391 key.type = BTRFS_EXTENT_DATA_KEY;
2392 if (offset > (u64)-1 << 32)
2393 key.offset = 0;
2394 else
2395 key.offset = offset;
2396
2397 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05302398 if (WARN_ON(ret < 0))
Liu Bo38c227d2013-01-29 03:18:40 +00002399 return ret;
Josef Bacik50f13192013-07-22 12:50:37 -04002400 ret = 0;
Liu Bo38c227d2013-01-29 03:18:40 +00002401
2402 while (1) {
2403 cond_resched();
2404
2405 leaf = path->nodes[0];
2406 slot = path->slots[0];
2407
2408 if (slot >= btrfs_header_nritems(leaf)) {
2409 ret = btrfs_next_leaf(root, path);
2410 if (ret < 0) {
2411 goto out;
2412 } else if (ret > 0) {
2413 ret = 0;
2414 goto out;
2415 }
2416 continue;
2417 }
2418
2419 path->slots[0]++;
2420
2421 btrfs_item_key_to_cpu(leaf, &key, slot);
2422
2423 if (key.objectid > inum)
2424 goto out;
2425
2426 if (key.objectid < inum || key.type != BTRFS_EXTENT_DATA_KEY)
2427 continue;
2428
2429 extent = btrfs_item_ptr(leaf, slot,
2430 struct btrfs_file_extent_item);
2431
2432 if (btrfs_file_extent_disk_bytenr(leaf, extent) != old->bytenr)
2433 continue;
2434
Liu Boe68afa42013-07-01 22:13:26 +08002435 /*
2436 * 'offset' refers to the exact key.offset,
2437 * NOT the 'offset' field in btrfs_extent_data_ref, ie.
2438 * (key.offset - extent_offset).
2439 */
2440 if (key.offset != offset)
Liu Bo38c227d2013-01-29 03:18:40 +00002441 continue;
2442
Liu Boe68afa42013-07-01 22:13:26 +08002443 extent_offset = btrfs_file_extent_offset(leaf, extent);
Liu Bo38c227d2013-01-29 03:18:40 +00002444 num_bytes = btrfs_file_extent_num_bytes(leaf, extent);
Liu Boe68afa42013-07-01 22:13:26 +08002445
Liu Bo38c227d2013-01-29 03:18:40 +00002446 if (extent_offset >= old->extent_offset + old->offset +
2447 old->len || extent_offset + num_bytes <=
2448 old->extent_offset + old->offset)
2449 continue;
Liu Bo38c227d2013-01-29 03:18:40 +00002450 break;
2451 }
2452
2453 backref = kmalloc(sizeof(*backref), GFP_NOFS);
2454 if (!backref) {
2455 ret = -ENOENT;
2456 goto out;
2457 }
2458
2459 backref->root_id = root_id;
2460 backref->inum = inum;
Liu Boe68afa42013-07-01 22:13:26 +08002461 backref->file_pos = offset;
Liu Bo38c227d2013-01-29 03:18:40 +00002462 backref->num_bytes = num_bytes;
2463 backref->extent_offset = extent_offset;
2464 backref->generation = btrfs_file_extent_generation(leaf, extent);
2465 backref->old = old;
2466 backref_insert(&new->root, backref);
2467 old->count++;
2468out:
2469 btrfs_release_path(path);
2470 WARN_ON(ret);
2471 return ret;
2472}
2473
2474static noinline bool record_extent_backrefs(struct btrfs_path *path,
2475 struct new_sa_defrag_extent *new)
2476{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002477 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002478 struct old_sa_defrag_extent *old, *tmp;
2479 int ret;
2480
2481 new->path = path;
2482
2483 list_for_each_entry_safe(old, tmp, &new->head, list) {
Liu Boe68afa42013-07-01 22:13:26 +08002484 ret = iterate_inodes_from_logical(old->bytenr +
2485 old->extent_offset, fs_info,
Liu Bo38c227d2013-01-29 03:18:40 +00002486 path, record_one_backref,
Zygo Blaxellc995ab32017-09-22 13:58:45 -04002487 old, false);
Josef Bacik4724b102013-11-05 11:11:40 -05002488 if (ret < 0 && ret != -ENOENT)
2489 return false;
Liu Bo38c227d2013-01-29 03:18:40 +00002490
2491 /* no backref to be processed for this extent */
2492 if (!old->count) {
2493 list_del(&old->list);
2494 kfree(old);
2495 }
2496 }
2497
2498 if (list_empty(&new->head))
2499 return false;
2500
2501 return true;
2502}
2503
2504static int relink_is_mergable(struct extent_buffer *leaf,
2505 struct btrfs_file_extent_item *fi,
Liu Bo116e0022013-08-02 16:30:40 +08002506 struct new_sa_defrag_extent *new)
Liu Bo38c227d2013-01-29 03:18:40 +00002507{
Liu Bo116e0022013-08-02 16:30:40 +08002508 if (btrfs_file_extent_disk_bytenr(leaf, fi) != new->bytenr)
Liu Bo38c227d2013-01-29 03:18:40 +00002509 return 0;
2510
2511 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG)
2512 return 0;
2513
Liu Bo116e0022013-08-02 16:30:40 +08002514 if (btrfs_file_extent_compression(leaf, fi) != new->compress_type)
2515 return 0;
2516
2517 if (btrfs_file_extent_encryption(leaf, fi) ||
Liu Bo38c227d2013-01-29 03:18:40 +00002518 btrfs_file_extent_other_encoding(leaf, fi))
2519 return 0;
2520
2521 return 1;
2522}
2523
2524/*
2525 * Note the backref might has changed, and in this case we just return 0.
2526 */
2527static noinline int relink_extent_backref(struct btrfs_path *path,
2528 struct sa_defrag_extent_backref *prev,
2529 struct sa_defrag_extent_backref *backref)
2530{
2531 struct btrfs_file_extent_item *extent;
2532 struct btrfs_file_extent_item *item;
2533 struct btrfs_ordered_extent *ordered;
2534 struct btrfs_trans_handle *trans;
Liu Bo38c227d2013-01-29 03:18:40 +00002535 struct btrfs_root *root;
2536 struct btrfs_key key;
2537 struct extent_buffer *leaf;
2538 struct old_sa_defrag_extent *old = backref->old;
2539 struct new_sa_defrag_extent *new = old->new;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002540 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002541 struct inode *inode;
2542 struct extent_state *cached = NULL;
2543 int ret = 0;
2544 u64 start;
2545 u64 len;
2546 u64 lock_start;
2547 u64 lock_end;
2548 bool merge = false;
2549 int index;
2550
2551 if (prev && prev->root_id == backref->root_id &&
2552 prev->inum == backref->inum &&
2553 prev->file_pos + prev->num_bytes == backref->file_pos)
2554 merge = true;
2555
2556 /* step 1: get root */
2557 key.objectid = backref->root_id;
2558 key.type = BTRFS_ROOT_ITEM_KEY;
2559 key.offset = (u64)-1;
2560
Liu Bo38c227d2013-01-29 03:18:40 +00002561 index = srcu_read_lock(&fs_info->subvol_srcu);
2562
2563 root = btrfs_read_fs_root_no_name(fs_info, &key);
2564 if (IS_ERR(root)) {
2565 srcu_read_unlock(&fs_info->subvol_srcu, index);
2566 if (PTR_ERR(root) == -ENOENT)
2567 return 0;
2568 return PTR_ERR(root);
2569 }
Liu Bo38c227d2013-01-29 03:18:40 +00002570
Wang Shilongbcbba5e2014-02-08 23:46:35 +08002571 if (btrfs_root_readonly(root)) {
2572 srcu_read_unlock(&fs_info->subvol_srcu, index);
2573 return 0;
2574 }
2575
Liu Bo38c227d2013-01-29 03:18:40 +00002576 /* step 2: get inode */
2577 key.objectid = backref->inum;
2578 key.type = BTRFS_INODE_ITEM_KEY;
2579 key.offset = 0;
2580
2581 inode = btrfs_iget(fs_info->sb, &key, root, NULL);
2582 if (IS_ERR(inode)) {
2583 srcu_read_unlock(&fs_info->subvol_srcu, index);
2584 return 0;
2585 }
2586
2587 srcu_read_unlock(&fs_info->subvol_srcu, index);
2588
2589 /* step 3: relink backref */
2590 lock_start = backref->file_pos;
2591 lock_end = backref->file_pos + backref->num_bytes - 1;
2592 lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
David Sterbaff13db42015-12-03 14:30:40 +01002593 &cached);
Liu Bo38c227d2013-01-29 03:18:40 +00002594
2595 ordered = btrfs_lookup_first_ordered_extent(inode, lock_end);
2596 if (ordered) {
2597 btrfs_put_ordered_extent(ordered);
2598 goto out_unlock;
2599 }
2600
2601 trans = btrfs_join_transaction(root);
2602 if (IS_ERR(trans)) {
2603 ret = PTR_ERR(trans);
2604 goto out_unlock;
2605 }
2606
2607 key.objectid = backref->inum;
2608 key.type = BTRFS_EXTENT_DATA_KEY;
2609 key.offset = backref->file_pos;
2610
2611 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2612 if (ret < 0) {
2613 goto out_free_path;
2614 } else if (ret > 0) {
2615 ret = 0;
2616 goto out_free_path;
2617 }
2618
2619 extent = btrfs_item_ptr(path->nodes[0], path->slots[0],
2620 struct btrfs_file_extent_item);
2621
2622 if (btrfs_file_extent_generation(path->nodes[0], extent) !=
2623 backref->generation)
2624 goto out_free_path;
2625
2626 btrfs_release_path(path);
2627
2628 start = backref->file_pos;
2629 if (backref->extent_offset < old->extent_offset + old->offset)
2630 start += old->extent_offset + old->offset -
2631 backref->extent_offset;
2632
2633 len = min(backref->extent_offset + backref->num_bytes,
2634 old->extent_offset + old->offset + old->len);
2635 len -= max(backref->extent_offset, old->extent_offset + old->offset);
2636
2637 ret = btrfs_drop_extents(trans, root, inode, start,
2638 start + len, 1);
2639 if (ret)
2640 goto out_free_path;
2641again:
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002642 key.objectid = btrfs_ino(BTRFS_I(inode));
Liu Bo38c227d2013-01-29 03:18:40 +00002643 key.type = BTRFS_EXTENT_DATA_KEY;
2644 key.offset = start;
2645
Liu Boa09a0a72013-03-11 09:20:58 +00002646 path->leave_spinning = 1;
Liu Bo38c227d2013-01-29 03:18:40 +00002647 if (merge) {
2648 struct btrfs_file_extent_item *fi;
2649 u64 extent_len;
2650 struct btrfs_key found_key;
2651
Gui Hecheng3c9665d2014-01-23 13:41:09 +08002652 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
Liu Bo38c227d2013-01-29 03:18:40 +00002653 if (ret < 0)
2654 goto out_free_path;
2655
2656 path->slots[0]--;
2657 leaf = path->nodes[0];
2658 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2659
2660 fi = btrfs_item_ptr(leaf, path->slots[0],
2661 struct btrfs_file_extent_item);
2662 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
2663
Liu Bo116e0022013-08-02 16:30:40 +08002664 if (extent_len + found_key.offset == start &&
2665 relink_is_mergable(leaf, fi, new)) {
Liu Bo38c227d2013-01-29 03:18:40 +00002666 btrfs_set_file_extent_num_bytes(leaf, fi,
2667 extent_len + len);
2668 btrfs_mark_buffer_dirty(leaf);
2669 inode_add_bytes(inode, len);
2670
2671 ret = 1;
2672 goto out_free_path;
2673 } else {
2674 merge = false;
2675 btrfs_release_path(path);
2676 goto again;
2677 }
2678 }
2679
2680 ret = btrfs_insert_empty_item(trans, root, path, &key,
2681 sizeof(*extent));
2682 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002683 btrfs_abort_transaction(trans, ret);
Liu Bo38c227d2013-01-29 03:18:40 +00002684 goto out_free_path;
2685 }
2686
2687 leaf = path->nodes[0];
2688 item = btrfs_item_ptr(leaf, path->slots[0],
2689 struct btrfs_file_extent_item);
2690 btrfs_set_file_extent_disk_bytenr(leaf, item, new->bytenr);
2691 btrfs_set_file_extent_disk_num_bytes(leaf, item, new->disk_len);
2692 btrfs_set_file_extent_offset(leaf, item, start - new->file_pos);
2693 btrfs_set_file_extent_num_bytes(leaf, item, len);
2694 btrfs_set_file_extent_ram_bytes(leaf, item, new->len);
2695 btrfs_set_file_extent_generation(leaf, item, trans->transid);
2696 btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG);
2697 btrfs_set_file_extent_compression(leaf, item, new->compress_type);
2698 btrfs_set_file_extent_encryption(leaf, item, 0);
2699 btrfs_set_file_extent_other_encoding(leaf, item, 0);
2700
2701 btrfs_mark_buffer_dirty(leaf);
2702 inode_add_bytes(inode, len);
Liu Boa09a0a72013-03-11 09:20:58 +00002703 btrfs_release_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002704
Josef Bacik84f7d8e2017-09-29 15:43:49 -04002705 ret = btrfs_inc_extent_ref(trans, root, new->bytenr,
Liu Bo38c227d2013-01-29 03:18:40 +00002706 new->disk_len, 0,
2707 backref->root_id, backref->inum,
Filipe Mananab06c4bf2015-10-23 07:52:54 +01002708 new->file_pos); /* start - extent_offset */
Liu Bo38c227d2013-01-29 03:18:40 +00002709 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002710 btrfs_abort_transaction(trans, ret);
Liu Bo38c227d2013-01-29 03:18:40 +00002711 goto out_free_path;
2712 }
2713
2714 ret = 1;
2715out_free_path:
2716 btrfs_release_path(path);
Liu Boa09a0a72013-03-11 09:20:58 +00002717 path->leave_spinning = 0;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002718 btrfs_end_transaction(trans);
Liu Bo38c227d2013-01-29 03:18:40 +00002719out_unlock:
2720 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
David Sterbae43bbe52017-12-12 21:43:52 +01002721 &cached);
Liu Bo38c227d2013-01-29 03:18:40 +00002722 iput(inode);
2723 return ret;
2724}
2725
Liu Bo6f519562013-10-29 10:45:05 +08002726static void free_sa_defrag_extent(struct new_sa_defrag_extent *new)
2727{
2728 struct old_sa_defrag_extent *old, *tmp;
2729
2730 if (!new)
2731 return;
2732
2733 list_for_each_entry_safe(old, tmp, &new->head, list) {
Liu Bo6f519562013-10-29 10:45:05 +08002734 kfree(old);
2735 }
2736 kfree(new);
2737}
2738
Liu Bo38c227d2013-01-29 03:18:40 +00002739static void relink_file_extents(struct new_sa_defrag_extent *new)
2740{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002741 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002742 struct btrfs_path *path;
Liu Bo38c227d2013-01-29 03:18:40 +00002743 struct sa_defrag_extent_backref *backref;
2744 struct sa_defrag_extent_backref *prev = NULL;
Liu Bo38c227d2013-01-29 03:18:40 +00002745 struct rb_node *node;
2746 int ret;
2747
Liu Bo38c227d2013-01-29 03:18:40 +00002748 path = btrfs_alloc_path();
2749 if (!path)
2750 return;
2751
2752 if (!record_extent_backrefs(path, new)) {
2753 btrfs_free_path(path);
2754 goto out;
2755 }
2756 btrfs_release_path(path);
2757
2758 while (1) {
2759 node = rb_first(&new->root);
2760 if (!node)
2761 break;
2762 rb_erase(node, &new->root);
2763
2764 backref = rb_entry(node, struct sa_defrag_extent_backref, node);
2765
2766 ret = relink_extent_backref(path, prev, backref);
2767 WARN_ON(ret < 0);
2768
2769 kfree(prev);
2770
2771 if (ret == 1)
2772 prev = backref;
2773 else
2774 prev = NULL;
2775 cond_resched();
2776 }
2777 kfree(prev);
2778
2779 btrfs_free_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002780out:
Liu Bo6f519562013-10-29 10:45:05 +08002781 free_sa_defrag_extent(new);
2782
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002783 atomic_dec(&fs_info->defrag_running);
2784 wake_up(&fs_info->transaction_wait);
Liu Bo38c227d2013-01-29 03:18:40 +00002785}
2786
2787static struct new_sa_defrag_extent *
2788record_old_file_extents(struct inode *inode,
2789 struct btrfs_ordered_extent *ordered)
2790{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002791 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002792 struct btrfs_root *root = BTRFS_I(inode)->root;
2793 struct btrfs_path *path;
2794 struct btrfs_key key;
Liu Bo6f519562013-10-29 10:45:05 +08002795 struct old_sa_defrag_extent *old;
Liu Bo38c227d2013-01-29 03:18:40 +00002796 struct new_sa_defrag_extent *new;
2797 int ret;
2798
2799 new = kmalloc(sizeof(*new), GFP_NOFS);
2800 if (!new)
2801 return NULL;
2802
2803 new->inode = inode;
2804 new->file_pos = ordered->file_offset;
2805 new->len = ordered->len;
2806 new->bytenr = ordered->start;
2807 new->disk_len = ordered->disk_len;
2808 new->compress_type = ordered->compress_type;
2809 new->root = RB_ROOT;
2810 INIT_LIST_HEAD(&new->head);
2811
2812 path = btrfs_alloc_path();
2813 if (!path)
2814 goto out_kfree;
2815
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002816 key.objectid = btrfs_ino(BTRFS_I(inode));
Liu Bo38c227d2013-01-29 03:18:40 +00002817 key.type = BTRFS_EXTENT_DATA_KEY;
2818 key.offset = new->file_pos;
2819
2820 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2821 if (ret < 0)
2822 goto out_free_path;
2823 if (ret > 0 && path->slots[0] > 0)
2824 path->slots[0]--;
2825
2826 /* find out all the old extents for the file range */
2827 while (1) {
2828 struct btrfs_file_extent_item *extent;
2829 struct extent_buffer *l;
2830 int slot;
2831 u64 num_bytes;
2832 u64 offset;
2833 u64 end;
2834 u64 disk_bytenr;
2835 u64 extent_offset;
2836
2837 l = path->nodes[0];
2838 slot = path->slots[0];
2839
2840 if (slot >= btrfs_header_nritems(l)) {
2841 ret = btrfs_next_leaf(root, path);
2842 if (ret < 0)
Liu Bo6f519562013-10-29 10:45:05 +08002843 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002844 else if (ret > 0)
2845 break;
2846 continue;
2847 }
2848
2849 btrfs_item_key_to_cpu(l, &key, slot);
2850
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002851 if (key.objectid != btrfs_ino(BTRFS_I(inode)))
Liu Bo38c227d2013-01-29 03:18:40 +00002852 break;
2853 if (key.type != BTRFS_EXTENT_DATA_KEY)
2854 break;
2855 if (key.offset >= new->file_pos + new->len)
2856 break;
2857
2858 extent = btrfs_item_ptr(l, slot, struct btrfs_file_extent_item);
2859
2860 num_bytes = btrfs_file_extent_num_bytes(l, extent);
2861 if (key.offset + num_bytes < new->file_pos)
2862 goto next;
2863
2864 disk_bytenr = btrfs_file_extent_disk_bytenr(l, extent);
2865 if (!disk_bytenr)
2866 goto next;
2867
2868 extent_offset = btrfs_file_extent_offset(l, extent);
2869
2870 old = kmalloc(sizeof(*old), GFP_NOFS);
2871 if (!old)
Liu Bo6f519562013-10-29 10:45:05 +08002872 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002873
2874 offset = max(new->file_pos, key.offset);
2875 end = min(new->file_pos + new->len, key.offset + num_bytes);
2876
2877 old->bytenr = disk_bytenr;
2878 old->extent_offset = extent_offset;
2879 old->offset = offset - key.offset;
2880 old->len = end - offset;
2881 old->new = new;
2882 old->count = 0;
2883 list_add_tail(&old->list, &new->head);
2884next:
2885 path->slots[0]++;
2886 cond_resched();
2887 }
2888
2889 btrfs_free_path(path);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002890 atomic_inc(&fs_info->defrag_running);
Liu Bo38c227d2013-01-29 03:18:40 +00002891
2892 return new;
2893
Liu Bo38c227d2013-01-29 03:18:40 +00002894out_free_path:
2895 btrfs_free_path(path);
2896out_kfree:
Liu Bo6f519562013-10-29 10:45:05 +08002897 free_sa_defrag_extent(new);
Liu Bo38c227d2013-01-29 03:18:40 +00002898 return NULL;
2899}
2900
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002901static void btrfs_release_delalloc_bytes(struct btrfs_fs_info *fs_info,
Miao Xiee570fd22014-06-19 10:42:50 +08002902 u64 start, u64 len)
2903{
2904 struct btrfs_block_group_cache *cache;
2905
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002906 cache = btrfs_lookup_block_group(fs_info, start);
Miao Xiee570fd22014-06-19 10:42:50 +08002907 ASSERT(cache);
2908
2909 spin_lock(&cache->lock);
2910 cache->delalloc_bytes -= len;
2911 spin_unlock(&cache->lock);
2912
2913 btrfs_put_block_group(cache);
2914}
2915
Chris Masond352ac62008-09-29 15:18:18 -04002916/* as ordered data IO finishes, this gets called so we can finish
2917 * an ordered extent if the range of bytes in the file it covers are
2918 * fully written.
2919 */
Josef Bacik5fd02042012-05-02 14:00:54 -04002920static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002921{
Josef Bacik5fd02042012-05-02 14:00:54 -04002922 struct inode *inode = ordered_extent->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002923 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002924 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002925 struct btrfs_trans_handle *trans = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002926 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002927 struct extent_state *cached_state = NULL;
Liu Bo38c227d2013-01-29 03:18:40 +00002928 struct new_sa_defrag_extent *new = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08002929 int compress_type = 0;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002930 int ret = 0;
2931 u64 logical_len = ordered_extent->len;
Li Zefan82d59022011-04-20 10:33:24 +08002932 bool nolock;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002933 bool truncated = false;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002934 bool range_locked = false;
2935 bool clear_new_delalloc_bytes = false;
Josef Bacik49940bd2018-10-11 15:54:21 -04002936 bool clear_reserved_extent = true;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002937
2938 if (!test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
2939 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags) &&
2940 !test_bit(BTRFS_ORDERED_DIRECT, &ordered_extent->flags))
2941 clear_new_delalloc_bytes = true;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002942
Nikolay Borisov70ddc552017-02-20 13:50:35 +02002943 nolock = btrfs_is_free_space_inode(BTRFS_I(inode));
Josef Bacik0cb59c92010-07-02 12:14:14 -04002944
Josef Bacik5fd02042012-05-02 14:00:54 -04002945 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
2946 ret = -EIO;
2947 goto out;
2948 }
2949
Nikolay Borisov7ab79562017-02-20 13:50:57 +02002950 btrfs_free_io_failure_record(BTRFS_I(inode),
2951 ordered_extent->file_offset,
2952 ordered_extent->file_offset +
2953 ordered_extent->len - 1);
Miao Xief6124962014-09-12 18:44:04 +08002954
Josef Bacik77cef2e2013-08-29 13:57:21 -04002955 if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) {
2956 truncated = true;
2957 logical_len = ordered_extent->truncated_len;
2958 /* Truncated the entire extent, don't bother adding */
2959 if (!logical_len)
2960 goto out;
2961 }
2962
Yan, Zhengc2167752009-11-12 09:34:21 +00002963 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002964 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
Qu Wenruo94ed9382015-09-08 17:25:56 +08002965
2966 /*
2967 * For mwrite(mmap + memset to write) case, we still reserve
2968 * space for NOCOW range.
2969 * As NOCOW won't cause a new delayed ref, just free the space
2970 */
Qu Wenruobc42bda2017-02-27 15:10:39 +08002971 btrfs_qgroup_free_data(inode, NULL, ordered_extent->file_offset,
Qu Wenruo94ed9382015-09-08 17:25:56 +08002972 ordered_extent->len);
Josef Bacik6c760c02012-11-09 10:53:21 -05002973 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2974 if (nolock)
2975 trans = btrfs_join_transaction_nolock(root);
2976 else
2977 trans = btrfs_join_transaction(root);
2978 if (IS_ERR(trans)) {
2979 ret = PTR_ERR(trans);
2980 trans = NULL;
2981 goto out;
Yan, Zhengc2167752009-11-12 09:34:21 +00002982 }
Josef Bacik69fe2d72017-10-19 14:15:57 -04002983 trans->block_rsv = &BTRFS_I(inode)->block_rsv;
Josef Bacik6c760c02012-11-09 10:53:21 -05002984 ret = btrfs_update_inode_fallback(trans, root, inode);
2985 if (ret) /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04002986 btrfs_abort_transaction(trans, ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00002987 goto out;
2988 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002989
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002990 range_locked = true;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002991 lock_extent_bits(io_tree, ordered_extent->file_offset,
2992 ordered_extent->file_offset + ordered_extent->len - 1,
David Sterbaff13db42015-12-03 14:30:40 +01002993 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002994
Liu Bo38c227d2013-01-29 03:18:40 +00002995 ret = test_range_bit(io_tree, ordered_extent->file_offset,
2996 ordered_extent->file_offset + ordered_extent->len - 1,
Liu Bo452e62b2017-05-26 17:44:23 -06002997 EXTENT_DEFRAG, 0, cached_state);
Liu Bo38c227d2013-01-29 03:18:40 +00002998 if (ret) {
2999 u64 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
Josef Bacik8101c8d2014-01-29 16:05:30 -05003000 if (0 && last_snapshot >= BTRFS_I(inode)->generation)
Liu Bo38c227d2013-01-29 03:18:40 +00003001 /* the inode is shared */
3002 new = record_old_file_extents(inode, ordered_extent);
3003
3004 clear_extent_bit(io_tree, ordered_extent->file_offset,
3005 ordered_extent->file_offset + ordered_extent->len - 1,
David Sterbaae0f1622017-10-31 16:37:52 +01003006 EXTENT_DEFRAG, 0, 0, &cached_state);
Liu Bo38c227d2013-01-29 03:18:40 +00003007 }
3008
Josef Bacik0cb59c92010-07-02 12:14:14 -04003009 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003010 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04003011 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003012 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003013 if (IS_ERR(trans)) {
3014 ret = PTR_ERR(trans);
3015 trans = NULL;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003016 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003017 }
Chris Masona79b7d42014-05-22 16:18:52 -07003018
Josef Bacik69fe2d72017-10-19 14:15:57 -04003019 trans->block_rsv = &BTRFS_I(inode)->block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00003020
Chris Masonc8b97812008-10-29 14:49:59 -04003021 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08003022 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04003023 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08003024 BUG_ON(compress_type);
Justin Maggardb430b772017-10-30 15:29:10 -07003025 btrfs_qgroup_free_data(inode, NULL, ordered_extent->file_offset,
3026 ordered_extent->len);
Nikolay Borisov7a6d7062017-02-20 13:50:48 +02003027 ret = btrfs_mark_extent_written(trans, BTRFS_I(inode),
Yan Zhengd899e052008-10-30 14:25:28 -04003028 ordered_extent->file_offset,
3029 ordered_extent->file_offset +
Josef Bacik77cef2e2013-08-29 13:57:21 -04003030 logical_len);
Yan Zhengd899e052008-10-30 14:25:28 -04003031 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003032 BUG_ON(root == fs_info->tree_root);
Yan Zhengd899e052008-10-30 14:25:28 -04003033 ret = insert_reserved_file_extent(trans, inode,
3034 ordered_extent->file_offset,
3035 ordered_extent->start,
3036 ordered_extent->disk_len,
Josef Bacik77cef2e2013-08-29 13:57:21 -04003037 logical_len, logical_len,
Li Zefan261507a02010-12-17 14:21:50 +08003038 compress_type, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04003039 BTRFS_FILE_EXTENT_REG);
Josef Bacik49940bd2018-10-11 15:54:21 -04003040 if (!ret) {
3041 clear_reserved_extent = false;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003042 btrfs_release_delalloc_bytes(fs_info,
Miao Xiee570fd22014-06-19 10:42:50 +08003043 ordered_extent->start,
3044 ordered_extent->disk_len);
Josef Bacik49940bd2018-10-11 15:54:21 -04003045 }
Yan Zhengd899e052008-10-30 14:25:28 -04003046 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04003047 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
3048 ordered_extent->file_offset, ordered_extent->len,
3049 trans->transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003050 if (ret < 0) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003051 btrfs_abort_transaction(trans, ret);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003052 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003053 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00003054
Nikolay Borisovac01f262018-01-08 10:59:43 +02003055 ret = add_pending_csums(trans, inode, &ordered_extent->list);
3056 if (ret) {
3057 btrfs_abort_transaction(trans, ret);
3058 goto out;
3059 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04003060
Josef Bacik6c760c02012-11-09 10:53:21 -05003061 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
3062 ret = btrfs_update_inode_fallback(trans, root, inode);
3063 if (ret) { /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04003064 btrfs_abort_transaction(trans, ret);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003065 goto out;
Josef Bacik1ef30be2011-04-05 19:25:36 -04003066 }
3067 ret = 0;
Yan, Zhengc2167752009-11-12 09:34:21 +00003068out:
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003069 if (range_locked || clear_new_delalloc_bytes) {
3070 unsigned int clear_bits = 0;
3071
3072 if (range_locked)
3073 clear_bits |= EXTENT_LOCKED;
3074 if (clear_new_delalloc_bytes)
3075 clear_bits |= EXTENT_DELALLOC_NEW;
3076 clear_extent_bit(&BTRFS_I(inode)->io_tree,
3077 ordered_extent->file_offset,
3078 ordered_extent->file_offset +
3079 ordered_extent->len - 1,
3080 clear_bits,
3081 (clear_bits & EXTENT_LOCKED) ? 1 : 0,
David Sterbaae0f1622017-10-31 16:37:52 +01003082 0, &cached_state);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003083 }
3084
Miao Xiea698d0752012-09-20 01:51:59 -06003085 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003086 btrfs_end_transaction(trans);
Josef Bacik0cb59c92010-07-02 12:14:14 -04003087
Josef Bacik77cef2e2013-08-29 13:57:21 -04003088 if (ret || truncated) {
3089 u64 start, end;
3090
3091 if (truncated)
3092 start = ordered_extent->file_offset + logical_len;
3093 else
3094 start = ordered_extent->file_offset;
3095 end = ordered_extent->file_offset + ordered_extent->len - 1;
David Sterbaf08dc362017-10-31 17:02:39 +01003096 clear_extent_uptodate(io_tree, start, end, NULL);
Josef Bacik77cef2e2013-08-29 13:57:21 -04003097
3098 /* Drop the cache for the part of the extent we didn't write. */
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02003099 btrfs_drop_extent_cache(BTRFS_I(inode), start, end, 0);
Josef Bacik5fd02042012-05-02 14:00:54 -04003100
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003101 /*
3102 * If the ordered extent had an IOERR or something else went
3103 * wrong we need to return the space for this ordered extent
Josef Bacik77cef2e2013-08-29 13:57:21 -04003104 * back to the allocator. We only free the extent in the
3105 * truncated case if we didn't write out the extent at all.
Josef Bacik49940bd2018-10-11 15:54:21 -04003106 *
3107 * If we made it past insert_reserved_file_extent before we
3108 * errored out then we don't need to do this as the accounting
3109 * has already been done.
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003110 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04003111 if ((ret || !logical_len) &&
Josef Bacik49940bd2018-10-11 15:54:21 -04003112 clear_reserved_extent &&
Josef Bacik77cef2e2013-08-29 13:57:21 -04003113 !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003114 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003115 btrfs_free_reserved_extent(fs_info,
3116 ordered_extent->start,
Miao Xiee570fd22014-06-19 10:42:50 +08003117 ordered_extent->disk_len, 1);
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003118 }
3119
3120
Josef Bacik5fd02042012-05-02 14:00:54 -04003121 /*
Liu Bo8bad3c02012-06-18 12:14:23 +08003122 * This needs to be done to make sure anybody waiting knows we are done
3123 * updating everything for this ordered extent.
Josef Bacik5fd02042012-05-02 14:00:54 -04003124 */
3125 btrfs_remove_ordered_extent(inode, ordered_extent);
3126
Liu Bo38c227d2013-01-29 03:18:40 +00003127 /* for snapshot-aware defrag */
Liu Bo6f519562013-10-29 10:45:05 +08003128 if (new) {
3129 if (ret) {
3130 free_sa_defrag_extent(new);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003131 atomic_dec(&fs_info->defrag_running);
Liu Bo6f519562013-10-29 10:45:05 +08003132 } else {
3133 relink_file_extents(new);
3134 }
3135 }
Liu Bo38c227d2013-01-29 03:18:40 +00003136
Chris Masone6dcd2d2008-07-17 12:53:50 -04003137 /* once for us */
3138 btrfs_put_ordered_extent(ordered_extent);
3139 /* once for the tree */
3140 btrfs_put_ordered_extent(ordered_extent);
3141
Ethan Liene73e81b2018-05-28 13:48:20 +08003142 /* Try to release some metadata so we don't get an OOM but don't wait */
3143 btrfs_btree_balance_dirty_nodelay(fs_info);
3144
Josef Bacik5fd02042012-05-02 14:00:54 -04003145 return ret;
3146}
3147
3148static void finish_ordered_fn(struct btrfs_work *work)
3149{
3150 struct btrfs_ordered_extent *ordered_extent;
3151 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
3152 btrfs_finish_ordered_io(ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003153}
3154
Nikolay Borisovc6297322018-11-08 10:18:08 +02003155void btrfs_writepage_endio_finish_ordered(struct page *page, u64 start,
3156 u64 end, int uptodate)
Chris Mason211f90e2008-07-18 11:56:15 -04003157{
Josef Bacik5fd02042012-05-02 14:00:54 -04003158 struct inode *inode = page->mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003159 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik5fd02042012-05-02 14:00:54 -04003160 struct btrfs_ordered_extent *ordered_extent = NULL;
Liu Bo9e0af232014-08-15 23:36:53 +08003161 struct btrfs_workqueue *wq;
3162 btrfs_work_func_t func;
Josef Bacik5fd02042012-05-02 14:00:54 -04003163
liubo1abe9b82011-03-24 11:18:59 +00003164 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
3165
Chris Mason8b62b722009-09-02 16:53:46 -04003166 ClearPagePrivate2(page);
Josef Bacik5fd02042012-05-02 14:00:54 -04003167 if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
3168 end - start + 1, uptodate))
David Sterbac3988d62017-02-17 15:18:32 +01003169 return;
Josef Bacik5fd02042012-05-02 14:00:54 -04003170
Nikolay Borisov70ddc552017-02-20 13:50:35 +02003171 if (btrfs_is_free_space_inode(BTRFS_I(inode))) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003172 wq = fs_info->endio_freespace_worker;
Liu Bo9e0af232014-08-15 23:36:53 +08003173 func = btrfs_freespace_write_helper;
3174 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003175 wq = fs_info->endio_write_workers;
Liu Bo9e0af232014-08-15 23:36:53 +08003176 func = btrfs_endio_write_helper;
3177 }
Josef Bacik5fd02042012-05-02 14:00:54 -04003178
Liu Bo9e0af232014-08-15 23:36:53 +08003179 btrfs_init_work(&ordered_extent->work, func, finish_ordered_fn, NULL,
3180 NULL);
3181 btrfs_queue_work(wq, &ordered_extent->work);
Chris Mason211f90e2008-07-18 11:56:15 -04003182}
3183
Miao Xiedc380ae2014-09-12 18:43:55 +08003184static int __readpage_endio_check(struct inode *inode,
3185 struct btrfs_io_bio *io_bio,
3186 int icsum, struct page *page,
3187 int pgoff, u64 start, size_t len)
3188{
3189 char *kaddr;
3190 u32 csum_expected;
3191 u32 csum = ~(u32)0;
Miao Xiedc380ae2014-09-12 18:43:55 +08003192
3193 csum_expected = *(((u32 *)io_bio->csum) + icsum);
3194
3195 kaddr = kmap_atomic(page);
3196 csum = btrfs_csum_data(kaddr + pgoff, csum, len);
Domagoj Tršan0b5e3da2016-10-27 08:52:33 +01003197 btrfs_csum_final(csum, (u8 *)&csum);
Miao Xiedc380ae2014-09-12 18:43:55 +08003198 if (csum != csum_expected)
3199 goto zeroit;
3200
3201 kunmap_atomic(kaddr);
3202 return 0;
3203zeroit:
Nikolay Borisov0970a222017-02-20 13:50:53 +02003204 btrfs_print_data_csum_error(BTRFS_I(inode), start, csum, csum_expected,
Qu Wenruo6f6b6432017-02-09 10:45:06 +08003205 io_bio->mirror_num);
Miao Xiedc380ae2014-09-12 18:43:55 +08003206 memset(kaddr + pgoff, 1, len);
3207 flush_dcache_page(page);
3208 kunmap_atomic(kaddr);
Miao Xiedc380ae2014-09-12 18:43:55 +08003209 return -EIO;
3210}
3211
Chris Masond352ac62008-09-29 15:18:18 -04003212/*
Chris Masond352ac62008-09-29 15:18:18 -04003213 * when reads are done, we need to check csums to verify the data is correct
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02003214 * if there's a match, we allow the bio to finish. If not, the code in
3215 * extent_io.c will try to find good copies for us.
Chris Masond352ac62008-09-29 15:18:18 -04003216 */
Miao Xiefacc8a222013-07-25 19:22:34 +08003217static int btrfs_readpage_end_io_hook(struct btrfs_io_bio *io_bio,
3218 u64 phy_offset, struct page *page,
3219 u64 start, u64 end, int mirror)
Chris Mason07157aa2007-08-30 08:50:51 -04003220{
Miao Xie4eee4fa2012-12-21 09:17:45 +00003221 size_t offset = start - page_offset(page);
Chris Mason07157aa2007-08-30 08:50:51 -04003222 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05003223 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masonff79f812007-10-15 16:22:25 -04003224 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -05003225
Chris Masond20f7042008-12-08 16:58:54 -05003226 if (PageChecked(page)) {
3227 ClearPageChecked(page);
Miao Xiedc380ae2014-09-12 18:43:55 +08003228 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003229 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003230
3231 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Miao Xiedc380ae2014-09-12 18:43:55 +08003232 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003233
Yan Zheng17d217f2008-12-12 10:03:38 -05003234 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04003235 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
David Sterba91166212016-04-26 23:54:39 +02003236 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM);
Yan Zheng17d217f2008-12-12 10:03:38 -05003237 return 0;
3238 }
3239
Miao Xiefacc8a222013-07-25 19:22:34 +08003240 phy_offset >>= inode->i_sb->s_blocksize_bits;
Miao Xiedc380ae2014-09-12 18:43:55 +08003241 return __readpage_endio_check(inode, io_bio, phy_offset, page, offset,
3242 start, (size_t)(end - start + 1));
Chris Mason07157aa2007-08-30 08:50:51 -04003243}
Chris Masonb888db2b2007-08-27 16:49:44 -04003244
Nikolay Borisovc1c3fac2018-01-16 09:31:58 +02003245/*
3246 * btrfs_add_delayed_iput - perform a delayed iput on @inode
3247 *
3248 * @inode: The inode we want to perform iput on
3249 *
3250 * This function uses the generic vfs_inode::i_count to track whether we should
3251 * just decrement it (in case it's > 1) or if this is the last iput then link
3252 * the inode to the delayed iput machinery. Delayed iputs are processed at
3253 * transaction commit time/superblock commit/cleaner kthread.
3254 */
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003255void btrfs_add_delayed_iput(struct inode *inode)
3256{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003257 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
David Sterba8089fe62015-11-19 14:15:51 +01003258 struct btrfs_inode *binode = BTRFS_I(inode);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003259
3260 if (atomic_add_unless(&inode->i_count, -1, 1))
3261 return;
3262
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003263 spin_lock(&fs_info->delayed_iput_lock);
Nikolay Borisovc1c3fac2018-01-16 09:31:58 +02003264 ASSERT(list_empty(&binode->delayed_iput));
3265 list_add_tail(&binode->delayed_iput, &fs_info->delayed_iputs);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003266 spin_unlock(&fs_info->delayed_iput_lock);
3267}
3268
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003269void btrfs_run_delayed_iputs(struct btrfs_fs_info *fs_info)
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003270{
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003271
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003272 spin_lock(&fs_info->delayed_iput_lock);
David Sterba8089fe62015-11-19 14:15:51 +01003273 while (!list_empty(&fs_info->delayed_iputs)) {
3274 struct btrfs_inode *inode;
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003275
David Sterba8089fe62015-11-19 14:15:51 +01003276 inode = list_first_entry(&fs_info->delayed_iputs,
3277 struct btrfs_inode, delayed_iput);
Nikolay Borisovc1c3fac2018-01-16 09:31:58 +02003278 list_del_init(&inode->delayed_iput);
David Sterba8089fe62015-11-19 14:15:51 +01003279 spin_unlock(&fs_info->delayed_iput_lock);
3280 iput(&inode->vfs_inode);
3281 spin_lock(&fs_info->delayed_iput_lock);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003282 }
David Sterba8089fe62015-11-19 14:15:51 +01003283 spin_unlock(&fs_info->delayed_iput_lock);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003284}
3285
Yan, Zhengd68fc572010-05-16 10:49:58 -04003286/*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003287 * This creates an orphan entry for the given inode in case something goes wrong
3288 * in the middle of an unlink.
Josef Bacik7b128762008-07-24 12:17:14 -04003289 */
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003290int btrfs_orphan_add(struct btrfs_trans_handle *trans,
Omar Sandoval27919062018-05-11 13:13:37 -07003291 struct btrfs_inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003292{
Yan, Zhengd68fc572010-05-16 10:49:58 -04003293 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003294
Omar Sandoval27919062018-05-11 13:13:37 -07003295 ret = btrfs_insert_orphan_item(trans, inode->root, btrfs_ino(inode));
3296 if (ret && ret != -EEXIST) {
3297 btrfs_abort_transaction(trans, ret);
3298 return ret;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003299 }
3300
Yan, Zhengd68fc572010-05-16 10:49:58 -04003301 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003302}
3303
3304/*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003305 * We have done the delete so we can go ahead and remove the orphan item for
3306 * this particular inode.
Josef Bacik7b128762008-07-24 12:17:14 -04003307 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00003308static int btrfs_orphan_del(struct btrfs_trans_handle *trans,
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003309 struct btrfs_inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003310{
Omar Sandoval27919062018-05-11 13:13:37 -07003311 return btrfs_del_orphan_item(trans, inode->root, btrfs_ino(inode));
Josef Bacik7b128762008-07-24 12:17:14 -04003312}
3313
3314/*
3315 * this cleans up any orphans that may be left on the list from the last use
3316 * of this root.
3317 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003318int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04003319{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003320 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik7b128762008-07-24 12:17:14 -04003321 struct btrfs_path *path;
3322 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04003323 struct btrfs_key key, found_key;
3324 struct btrfs_trans_handle *trans;
3325 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003326 u64 last_objectid = 0;
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003327 int ret = 0, nr_unlink = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003328
Yan, Zhengd68fc572010-05-16 10:49:58 -04003329 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003330 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00003331
3332 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003333 if (!path) {
3334 ret = -ENOMEM;
3335 goto out;
3336 }
David Sterbae4058b52015-11-27 16:31:35 +01003337 path->reada = READA_BACK;
Josef Bacik7b128762008-07-24 12:17:14 -04003338
3339 key.objectid = BTRFS_ORPHAN_OBJECTID;
David Sterba962a2982014-06-04 18:41:45 +02003340 key.type = BTRFS_ORPHAN_ITEM_KEY;
Josef Bacik7b128762008-07-24 12:17:14 -04003341 key.offset = (u64)-1;
3342
Josef Bacik7b128762008-07-24 12:17:14 -04003343 while (1) {
3344 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003345 if (ret < 0)
3346 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003347
3348 /*
3349 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003350 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04003351 * find the key and see if we have stuff that matches
3352 */
3353 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003354 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003355 if (path->slots[0] == 0)
3356 break;
3357 path->slots[0]--;
3358 }
3359
3360 /* pull out the item */
3361 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04003362 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3363
3364 /* make sure the item matches what we want */
3365 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
3366 break;
David Sterba962a2982014-06-04 18:41:45 +02003367 if (found_key.type != BTRFS_ORPHAN_ITEM_KEY)
Josef Bacik7b128762008-07-24 12:17:14 -04003368 break;
3369
3370 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02003371 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04003372
3373 /*
3374 * this is where we are basically btrfs_lookup, without the
3375 * crossing root thing. we store the inode number in the
3376 * offset of the orphan item.
3377 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003378
3379 if (found_key.offset == last_objectid) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003380 btrfs_err(fs_info,
3381 "Error removing orphan entry, stopping orphan cleanup");
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003382 ret = -EINVAL;
3383 goto out;
3384 }
3385
3386 last_objectid = found_key.offset;
3387
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003388 found_key.objectid = found_key.offset;
3389 found_key.type = BTRFS_INODE_ITEM_KEY;
3390 found_key.offset = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003391 inode = btrfs_iget(fs_info->sb, &found_key, root, NULL);
Rusty Russell8c6ffba2013-07-15 11:20:32 +09303392 ret = PTR_ERR_OR_ZERO(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003393 if (ret && ret != -ENOENT)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003394 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003395
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003396 if (ret == -ENOENT && root == fs_info->tree_root) {
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003397 struct btrfs_root *dead_root;
3398 struct btrfs_fs_info *fs_info = root->fs_info;
3399 int is_dead_root = 0;
3400
3401 /*
3402 * this is an orphan in the tree root. Currently these
3403 * could come from 2 sources:
3404 * a) a snapshot deletion in progress
3405 * b) a free space cache inode
3406 * We need to distinguish those two, as the snapshot
3407 * orphan must not get deleted.
3408 * find_dead_roots already ran before us, so if this
3409 * is a snapshot deletion, we should find the root
3410 * in the dead_roots list
3411 */
3412 spin_lock(&fs_info->trans_lock);
3413 list_for_each_entry(dead_root, &fs_info->dead_roots,
3414 root_list) {
3415 if (dead_root->root_key.objectid ==
3416 found_key.objectid) {
3417 is_dead_root = 1;
3418 break;
3419 }
3420 }
3421 spin_unlock(&fs_info->trans_lock);
3422 if (is_dead_root) {
3423 /* prevent this orphan from being found again */
3424 key.offset = found_key.objectid - 1;
3425 continue;
3426 }
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003427
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003428 }
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003429
Josef Bacika8c9e572011-09-21 16:55:59 -04003430 /*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003431 * If we have an inode with links, there are a couple of
3432 * possibilities. Old kernels (before v3.12) used to create an
3433 * orphan item for truncate indicating that there were possibly
3434 * extent items past i_size that needed to be deleted. In v3.12,
3435 * truncate was changed to update i_size in sync with the extent
3436 * items, but the (useless) orphan item was still created. Since
3437 * v4.18, we don't create the orphan item for truncate at all.
3438 *
3439 * So, this item could mean that we need to do a truncate, but
3440 * only if this filesystem was last used on a pre-v3.12 kernel
3441 * and was not cleanly unmounted. The odds of that are quite
3442 * slim, and it's a pain to do the truncate now, so just delete
3443 * the orphan item.
3444 *
3445 * It's also possible that this orphan item was supposed to be
3446 * deleted but wasn't. The inode number may have been reused,
3447 * but either way, we can delete the orphan item.
Josef Bacika8c9e572011-09-21 16:55:59 -04003448 */
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003449 if (ret == -ENOENT || inode->i_nlink) {
3450 if (!ret)
3451 iput(inode);
Josef Bacika8c9e572011-09-21 16:55:59 -04003452 trans = btrfs_start_transaction(root, 1);
3453 if (IS_ERR(trans)) {
3454 ret = PTR_ERR(trans);
3455 goto out;
3456 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003457 btrfs_debug(fs_info, "auto deleting %Lu",
3458 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003459 ret = btrfs_del_orphan_item(trans, root,
3460 found_key.objectid);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003461 btrfs_end_transaction(trans);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003462 if (ret)
3463 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003464 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003465 }
Josef Bacik7b128762008-07-24 12:17:14 -04003466
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003467 nr_unlink++;
Josef Bacik7b128762008-07-24 12:17:14 -04003468
3469 /* this will do delete_inode and everything for us */
3470 iput(inode);
3471 }
Miao Xie3254c872011-11-10 20:45:05 -05003472 /* release the path since we're done with it */
3473 btrfs_release_path(path);
3474
Yan, Zhengd68fc572010-05-16 10:49:58 -04003475 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
3476
Omar Sandovala575cee2018-05-11 13:13:38 -07003477 if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003478 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003479 if (!IS_ERR(trans))
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003480 btrfs_end_transaction(trans);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003481 }
Josef Bacik7b128762008-07-24 12:17:14 -04003482
3483 if (nr_unlink)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003484 btrfs_debug(fs_info, "unlinked %d orphans", nr_unlink);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003485
3486out:
3487 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003488 btrfs_err(fs_info, "could not do orphan cleanup %d", ret);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003489 btrfs_free_path(path);
3490 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003491}
3492
Chris Masond352ac62008-09-29 15:18:18 -04003493/*
Chris Mason46a53cc2009-04-27 11:47:50 -04003494 * very simple check to peek ahead in the leaf looking for xattrs. If we
3495 * don't find any xattrs, we know there can't be any acls.
3496 *
3497 * slot is the slot the inode is in, objectid is the objectid of the inode
3498 */
3499static noinline int acls_after_inode_item(struct extent_buffer *leaf,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003500 int slot, u64 objectid,
3501 int *first_xattr_slot)
Chris Mason46a53cc2009-04-27 11:47:50 -04003502{
3503 u32 nritems = btrfs_header_nritems(leaf);
3504 struct btrfs_key found_key;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003505 static u64 xattr_access = 0;
3506 static u64 xattr_default = 0;
Chris Mason46a53cc2009-04-27 11:47:50 -04003507 int scanned = 0;
3508
Josef Bacikf23b5a52013-06-19 10:16:26 -04003509 if (!xattr_access) {
Andreas Gruenbacher97d79292015-12-02 14:44:35 +01003510 xattr_access = btrfs_name_hash(XATTR_NAME_POSIX_ACL_ACCESS,
3511 strlen(XATTR_NAME_POSIX_ACL_ACCESS));
3512 xattr_default = btrfs_name_hash(XATTR_NAME_POSIX_ACL_DEFAULT,
3513 strlen(XATTR_NAME_POSIX_ACL_DEFAULT));
Josef Bacikf23b5a52013-06-19 10:16:26 -04003514 }
3515
Chris Mason46a53cc2009-04-27 11:47:50 -04003516 slot++;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003517 *first_xattr_slot = -1;
Chris Mason46a53cc2009-04-27 11:47:50 -04003518 while (slot < nritems) {
3519 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3520
3521 /* we found a different objectid, there must not be acls */
3522 if (found_key.objectid != objectid)
3523 return 0;
3524
3525 /* we found an xattr, assume we've got an acl */
Josef Bacikf23b5a52013-06-19 10:16:26 -04003526 if (found_key.type == BTRFS_XATTR_ITEM_KEY) {
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003527 if (*first_xattr_slot == -1)
3528 *first_xattr_slot = slot;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003529 if (found_key.offset == xattr_access ||
3530 found_key.offset == xattr_default)
3531 return 1;
3532 }
Chris Mason46a53cc2009-04-27 11:47:50 -04003533
3534 /*
3535 * we found a key greater than an xattr key, there can't
3536 * be any acls later on
3537 */
3538 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
3539 return 0;
3540
3541 slot++;
3542 scanned++;
3543
3544 /*
3545 * it goes inode, inode backrefs, xattrs, extents,
3546 * so if there are a ton of hard links to an inode there can
3547 * be a lot of backrefs. Don't waste time searching too hard,
3548 * this is just an optimization
3549 */
3550 if (scanned >= 8)
3551 break;
3552 }
3553 /* we hit the end of the leaf before we found an xattr or
3554 * something larger than an xattr. We have to assume the inode
3555 * has acls
3556 */
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003557 if (*first_xattr_slot == -1)
3558 *first_xattr_slot = slot;
Chris Mason46a53cc2009-04-27 11:47:50 -04003559 return 1;
3560}
3561
3562/*
Chris Masond352ac62008-09-29 15:18:18 -04003563 * read an inode from the btree into the in-memory inode
3564 */
Filipe Manana4222ea72018-10-24 10:13:03 +01003565static int btrfs_read_locked_inode(struct inode *inode,
3566 struct btrfs_path *in_path)
Chris Mason39279cc2007-06-12 06:35:45 -04003567{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003568 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Filipe Manana4222ea72018-10-24 10:13:03 +01003569 struct btrfs_path *path = in_path;
Chris Mason5f39d392007-10-15 16:14:19 -04003570 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003571 struct btrfs_inode_item *inode_item;
3572 struct btrfs_root *root = BTRFS_I(inode)->root;
3573 struct btrfs_key location;
Miao Xie67de1172013-12-26 13:07:06 +08003574 unsigned long ptr;
Chris Mason46a53cc2009-04-27 11:47:50 -04003575 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04003576 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04003577 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003578 bool filled = false;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003579 int first_xattr_slot;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003580
3581 ret = btrfs_fill_inode(inode, &rdev);
3582 if (!ret)
3583 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04003584
Filipe Manana4222ea72018-10-24 10:13:03 +01003585 if (!path) {
3586 path = btrfs_alloc_path();
3587 if (!path)
3588 return -ENOMEM;
3589 }
Mark Fasheh1748f842011-07-12 11:25:31 -07003590
Chris Mason39279cc2007-06-12 06:35:45 -04003591 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05003592
Chris Mason39279cc2007-06-12 06:35:45 -04003593 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Filipe Manana67710892016-06-06 11:51:25 +01003594 if (ret) {
Filipe Manana4222ea72018-10-24 10:13:03 +01003595 if (path != in_path)
3596 btrfs_free_path(path);
Al Virof5b3a412018-07-29 23:04:51 +01003597 return ret;
Filipe Manana67710892016-06-06 11:51:25 +01003598 }
Chris Mason39279cc2007-06-12 06:35:45 -04003599
Chris Mason5f39d392007-10-15 16:14:19 -04003600 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00003601
3602 if (filled)
Miao Xie67de1172013-12-26 13:07:06 +08003603 goto cache_index;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003604
Chris Mason5f39d392007-10-15 16:14:19 -04003605 inode_item = btrfs_item_ptr(leaf, path->slots[0],
3606 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003607 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02003608 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08003609 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
3610 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02003611 btrfs_i_size_write(BTRFS_I(inode), btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04003612
David Sterbaa937b972014-12-12 17:39:12 +01003613 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->atime);
3614 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->atime);
Chris Mason5f39d392007-10-15 16:14:19 -04003615
David Sterbaa937b972014-12-12 17:39:12 +01003616 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->mtime);
3617 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->mtime);
Chris Mason5f39d392007-10-15 16:14:19 -04003618
David Sterbaa937b972014-12-12 17:39:12 +01003619 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->ctime);
3620 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->ctime);
Chris Mason5f39d392007-10-15 16:14:19 -04003621
chandan r9cc97d62012-07-04 12:48:07 +05303622 BTRFS_I(inode)->i_otime.tv_sec =
3623 btrfs_timespec_sec(leaf, &inode_item->otime);
3624 BTRFS_I(inode)->i_otime.tv_nsec =
3625 btrfs_timespec_nsec(leaf, &inode_item->otime);
Chris Mason5f39d392007-10-15 16:14:19 -04003626
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003627 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003628 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003629 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
3630
Jeff Laytonc7f88c42017-12-11 06:35:12 -05003631 inode_set_iversion_queried(inode,
3632 btrfs_inode_sequence(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003633 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04003634 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003635 rdev = btrfs_inode_rdev(leaf, inode_item);
3636
Josef Bacikaec74772008-07-24 12:12:38 -04003637 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05003638 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Miao Xie67de1172013-12-26 13:07:06 +08003639
3640cache_index:
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003641 /*
3642 * If we were modified in the current generation and evicted from memory
3643 * and then re-read we need to do a full sync since we don't have any
3644 * idea about which extents were modified before we were evicted from
3645 * cache.
3646 *
3647 * This is required for both inode re-read from disk and delayed inode
3648 * in delayed_nodes_tree.
3649 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003650 if (BTRFS_I(inode)->last_trans == fs_info->generation)
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003651 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3652 &BTRFS_I(inode)->runtime_flags);
3653
Filipe Mananabde6c242015-07-24 00:00:19 +01003654 /*
3655 * We don't persist the id of the transaction where an unlink operation
3656 * against the inode was last made. So here we assume the inode might
3657 * have been evicted, and therefore the exact value of last_unlink_trans
3658 * lost, and set it to last_trans to avoid metadata inconsistencies
3659 * between the inode and its parent if the inode is fsync'ed and the log
3660 * replayed. For example, in the scenario:
3661 *
3662 * touch mydir/foo
3663 * ln mydir/foo mydir/bar
3664 * sync
3665 * unlink mydir/bar
3666 * echo 2 > /proc/sys/vm/drop_caches # evicts inode
3667 * xfs_io -c fsync mydir/foo
3668 * <power failure>
3669 * mount fs, triggers fsync log replay
3670 *
3671 * We must make sure that when we fsync our inode foo we also log its
3672 * parent inode, otherwise after log replay the parent still has the
3673 * dentry with the "bar" name but our inode foo has a link count of 1
3674 * and doesn't have an inode ref with the name "bar" anymore.
3675 *
3676 * Setting last_unlink_trans to last_trans is a pessimistic approach,
Nicholas D Steeves01327612016-05-19 21:18:45 -04003677 * but it guarantees correctness at the expense of occasional full
Filipe Mananabde6c242015-07-24 00:00:19 +01003678 * transaction commits on fsync if our inode is a directory, or if our
3679 * inode is not a directory, logging its parent unnecessarily.
3680 */
3681 BTRFS_I(inode)->last_unlink_trans = BTRFS_I(inode)->last_trans;
3682
Miao Xie67de1172013-12-26 13:07:06 +08003683 path->slots[0]++;
3684 if (inode->i_nlink != 1 ||
3685 path->slots[0] >= btrfs_header_nritems(leaf))
3686 goto cache_acl;
3687
3688 btrfs_item_key_to_cpu(leaf, &location, path->slots[0]);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003689 if (location.objectid != btrfs_ino(BTRFS_I(inode)))
Miao Xie67de1172013-12-26 13:07:06 +08003690 goto cache_acl;
3691
3692 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3693 if (location.type == BTRFS_INODE_REF_KEY) {
3694 struct btrfs_inode_ref *ref;
3695
3696 ref = (struct btrfs_inode_ref *)ptr;
3697 BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref);
3698 } else if (location.type == BTRFS_INODE_EXTREF_KEY) {
3699 struct btrfs_inode_extref *extref;
3700
3701 extref = (struct btrfs_inode_extref *)ptr;
3702 BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf,
3703 extref);
3704 }
Miao Xie2f7e33d2011-06-23 07:27:13 +00003705cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04003706 /*
3707 * try to precache a NULL acl entry for files that don't have
3708 * any xattrs or acls
3709 */
Li Zefan33345d012011-04-20 10:31:50 +08003710 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
David Sterbaf85b7372017-01-20 14:54:07 +01003711 btrfs_ino(BTRFS_I(inode)), &first_xattr_slot);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003712 if (first_xattr_slot != -1) {
3713 path->slots[0] = first_xattr_slot;
3714 ret = btrfs_load_inode_props(inode, path);
3715 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003716 btrfs_err(fs_info,
David Sterba351fd352014-05-15 16:48:20 +02003717 "error loading props for ino %llu (root %llu): %d",
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003718 btrfs_ino(BTRFS_I(inode)),
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003719 root->root_key.objectid, ret);
3720 }
Filipe Manana4222ea72018-10-24 10:13:03 +01003721 if (path != in_path)
3722 btrfs_free_path(path);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003723
Al Viro72c04902009-06-24 16:58:48 -04003724 if (!maybe_acls)
3725 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04003726
Chris Mason39279cc2007-06-12 06:35:45 -04003727 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04003728 case S_IFREG:
3729 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masond1310b22008-01-24 16:13:08 -05003730 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04003731 inode->i_fop = &btrfs_file_operations;
3732 inode->i_op = &btrfs_file_inode_operations;
3733 break;
3734 case S_IFDIR:
3735 inode->i_fop = &btrfs_dir_file_operations;
Omar Sandoval67ade052017-01-25 17:06:38 -08003736 inode->i_op = &btrfs_dir_inode_operations;
Chris Mason39279cc2007-06-12 06:35:45 -04003737 break;
3738 case S_IFLNK:
3739 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -05003740 inode_nohighmem(inode);
Omar Sandoval4779cc02018-09-24 15:16:55 -07003741 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason39279cc2007-06-12 06:35:45 -04003742 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04003743 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05003744 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04003745 init_special_inode(inode, inode->i_mode, rdev);
3746 break;
Chris Mason39279cc2007-06-12 06:35:45 -04003747 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003748
David Sterba7b6a2212018-03-26 18:40:21 +02003749 btrfs_sync_inode_flags_to_i_flags(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003750 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003751}
3752
Chris Masond352ac62008-09-29 15:18:18 -04003753/*
3754 * given a leaf and an inode, copy the inode fields into the leaf
3755 */
Chris Masone02119d2008-09-05 16:13:11 -04003756static void fill_inode_item(struct btrfs_trans_handle *trans,
3757 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04003758 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04003759 struct inode *inode)
3760{
Liu Bo51fab692012-12-27 09:01:21 +00003761 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003762
Liu Bo51fab692012-12-27 09:01:21 +00003763 btrfs_init_map_token(&token);
Chris Mason5f39d392007-10-15 16:14:19 -04003764
Liu Bo51fab692012-12-27 09:01:21 +00003765 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
3766 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
3767 btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size,
3768 &token);
3769 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3770 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003771
David Sterbaa937b972014-12-12 17:39:12 +01003772 btrfs_set_token_timespec_sec(leaf, &item->atime,
Liu Bo51fab692012-12-27 09:01:21 +00003773 inode->i_atime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003774 btrfs_set_token_timespec_nsec(leaf, &item->atime,
Liu Bo51fab692012-12-27 09:01:21 +00003775 inode->i_atime.tv_nsec, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003776
David Sterbaa937b972014-12-12 17:39:12 +01003777 btrfs_set_token_timespec_sec(leaf, &item->mtime,
Liu Bo51fab692012-12-27 09:01:21 +00003778 inode->i_mtime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003779 btrfs_set_token_timespec_nsec(leaf, &item->mtime,
Liu Bo51fab692012-12-27 09:01:21 +00003780 inode->i_mtime.tv_nsec, &token);
3781
David Sterbaa937b972014-12-12 17:39:12 +01003782 btrfs_set_token_timespec_sec(leaf, &item->ctime,
Liu Bo51fab692012-12-27 09:01:21 +00003783 inode->i_ctime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003784 btrfs_set_token_timespec_nsec(leaf, &item->ctime,
Liu Bo51fab692012-12-27 09:01:21 +00003785 inode->i_ctime.tv_nsec, &token);
3786
chandan r9cc97d62012-07-04 12:48:07 +05303787 btrfs_set_token_timespec_sec(leaf, &item->otime,
3788 BTRFS_I(inode)->i_otime.tv_sec, &token);
3789 btrfs_set_token_timespec_nsec(leaf, &item->otime,
3790 BTRFS_I(inode)->i_otime.tv_nsec, &token);
3791
Liu Bo51fab692012-12-27 09:01:21 +00003792 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3793 &token);
3794 btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation,
3795 &token);
Jeff Laytonc7f88c42017-12-11 06:35:12 -05003796 btrfs_set_token_inode_sequence(leaf, item, inode_peek_iversion(inode),
3797 &token);
Liu Bo51fab692012-12-27 09:01:21 +00003798 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
3799 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3800 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3801 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
Chris Mason39279cc2007-06-12 06:35:45 -04003802}
3803
Chris Masond352ac62008-09-29 15:18:18 -04003804/*
3805 * copy everything in the in-memory inode into the btree.
3806 */
Chris Mason21151332011-11-10 20:39:08 -05003807static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Chris Masond3977122009-01-05 21:25:51 -05003808 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003809{
3810 struct btrfs_inode_item *inode_item;
3811 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003812 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003813 int ret;
3814
3815 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08003816 if (!path)
3817 return -ENOMEM;
3818
Chris Masonb9473432009-03-13 11:00:37 -04003819 path->leave_spinning = 1;
Miao Xie16cdcec2011-04-22 18:12:22 +08003820 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
3821 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003822 if (ret) {
3823 if (ret > 0)
3824 ret = -ENOENT;
3825 goto failed;
3826 }
3827
Chris Mason5f39d392007-10-15 16:14:19 -04003828 leaf = path->nodes[0];
3829 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08003830 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04003831
Chris Masone02119d2008-09-05 16:13:11 -04003832 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04003833 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04003834 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003835 ret = 0;
3836failed:
Chris Mason39279cc2007-06-12 06:35:45 -04003837 btrfs_free_path(path);
3838 return ret;
3839}
3840
Chris Masond352ac62008-09-29 15:18:18 -04003841/*
Chris Mason21151332011-11-10 20:39:08 -05003842 * copy everything in the in-memory inode into the btree.
3843 */
3844noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
3845 struct btrfs_root *root, struct inode *inode)
3846{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003847 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason21151332011-11-10 20:39:08 -05003848 int ret;
3849
3850 /*
3851 * If the inode is a free space inode, we can deadlock during commit
3852 * if we put it into the delayed code.
3853 *
3854 * The data relocation inode should also be directly updated
3855 * without delay
3856 */
Nikolay Borisov70ddc552017-02-20 13:50:35 +02003857 if (!btrfs_is_free_space_inode(BTRFS_I(inode))
Josef Bacik1d52c782014-09-18 11:30:44 -04003858 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003859 && !test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) {
Alexander Block8ea05e32012-07-25 17:35:53 +02003860 btrfs_update_root_times(trans, root);
3861
Chris Mason21151332011-11-10 20:39:08 -05003862 ret = btrfs_delayed_update_inode(trans, root, inode);
3863 if (!ret)
3864 btrfs_set_inode_last_trans(trans, inode);
3865 return ret;
3866 }
3867
3868 return btrfs_update_inode_item(trans, root, inode);
3869}
3870
Josef Bacikbe6aef62012-10-22 15:43:12 -04003871noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
3872 struct btrfs_root *root,
3873 struct inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05003874{
3875 int ret;
3876
3877 ret = btrfs_update_inode(trans, root, inode);
3878 if (ret == -ENOSPC)
3879 return btrfs_update_inode_item(trans, root, inode);
3880 return ret;
3881}
3882
3883/*
Chris Masond352ac62008-09-29 15:18:18 -04003884 * unlink helper that gets used here in inode.c and in the tree logging
3885 * recovery code. It remove a link in a directory with a given name, and
3886 * also drops the back refs in the inode to the directory
3887 */
Al Viro92986792011-03-04 17:14:37 +00003888static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3889 struct btrfs_root *root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003890 struct btrfs_inode *dir,
3891 struct btrfs_inode *inode,
Al Viro92986792011-03-04 17:14:37 +00003892 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04003893{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003894 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04003895 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04003896 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003897 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003898 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04003899 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04003900 u64 index;
Li Zefan33345d012011-04-20 10:31:50 +08003901 u64 ino = btrfs_ino(inode);
3902 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04003903
3904 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04003905 if (!path) {
3906 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00003907 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04003908 }
3909
Chris Masonb9473432009-03-13 11:00:37 -04003910 path->leave_spinning = 1;
Li Zefan33345d012011-04-20 10:31:50 +08003911 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04003912 name, name_len, -1);
Liu Bo3cf50682018-09-12 06:06:26 +08003913 if (IS_ERR_OR_NULL(di)) {
3914 ret = di ? PTR_ERR(di) : -ENOENT;
Chris Mason39279cc2007-06-12 06:35:45 -04003915 goto err;
3916 }
Chris Mason5f39d392007-10-15 16:14:19 -04003917 leaf = path->nodes[0];
3918 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04003919 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04003920 if (ret)
3921 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02003922 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003923
Miao Xie67de1172013-12-26 13:07:06 +08003924 /*
3925 * If we don't have dir index, we have to get it by looking up
3926 * the inode ref, since we get the inode ref, remove it directly,
3927 * it is unnecessary to do delayed deletion.
3928 *
3929 * But if we have dir index, needn't search inode ref to get it.
3930 * Since the inode ref is close to the inode item, it is better
3931 * that we delay to delete it, and just do this deletion when
3932 * we update the inode item.
3933 */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003934 if (inode->dir_index) {
Miao Xie67de1172013-12-26 13:07:06 +08003935 ret = btrfs_delayed_delete_inode_ref(inode);
3936 if (!ret) {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003937 index = inode->dir_index;
Miao Xie67de1172013-12-26 13:07:06 +08003938 goto skip_backref;
3939 }
3940 }
3941
Li Zefan33345d012011-04-20 10:31:50 +08003942 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
3943 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04003944 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003945 btrfs_info(fs_info,
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003946 "failed to delete reference to %.*s, inode %llu parent %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003947 name_len, name, ino, dir_ino);
Jeff Mahoney66642832016-06-10 18:19:25 -04003948 btrfs_abort_transaction(trans, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04003949 goto err;
3950 }
Miao Xie67de1172013-12-26 13:07:06 +08003951skip_backref:
Lu Fengqi9add2942018-08-01 11:32:26 +08003952 ret = btrfs_delete_delayed_dir_index(trans, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003953 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003954 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003955 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003956 }
Chris Mason39279cc2007-06-12 06:35:45 -04003957
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003958 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len, inode,
3959 dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003960 if (ret != 0 && ret != -ENOENT) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003961 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003962 goto err;
3963 }
Chris Masone02119d2008-09-05 16:13:11 -04003964
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003965 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len, dir,
3966 index);
Chris Mason6418c962010-10-30 07:34:24 -04003967 if (ret == -ENOENT)
3968 ret = 0;
Zach Brownd4e39912013-04-02 21:02:16 +00003969 else if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04003970 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003971err:
3972 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003973 if (ret)
3974 goto out;
3975
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02003976 btrfs_i_size_write(dir, dir->vfs_inode.i_size - name_len * 2);
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003977 inode_inc_iversion(&inode->vfs_inode);
3978 inode_inc_iversion(&dir->vfs_inode);
3979 inode->vfs_inode.i_ctime = dir->vfs_inode.i_mtime =
3980 dir->vfs_inode.i_ctime = current_time(&inode->vfs_inode);
3981 ret = btrfs_update_inode(trans, root, &dir->vfs_inode);
Chris Masone02119d2008-09-05 16:13:11 -04003982out:
Chris Mason39279cc2007-06-12 06:35:45 -04003983 return ret;
3984}
3985
Al Viro92986792011-03-04 17:14:37 +00003986int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3987 struct btrfs_root *root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003988 struct btrfs_inode *dir, struct btrfs_inode *inode,
Al Viro92986792011-03-04 17:14:37 +00003989 const char *name, int name_len)
3990{
3991 int ret;
3992 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
3993 if (!ret) {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003994 drop_nlink(&inode->vfs_inode);
3995 ret = btrfs_update_inode(trans, root, &inode->vfs_inode);
Al Viro92986792011-03-04 17:14:37 +00003996 }
3997 return ret;
3998}
Yan, Zhenga22285a2010-05-16 10:48:46 -04003999
4000/*
4001 * helper to start transaction for unlink and rmdir.
4002 *
Josef Bacikd52be812013-05-29 14:54:47 -04004003 * unlink and rmdir are special in btrfs, they do not always free space, so
4004 * if we cannot make our reservations the normal way try and see if there is
4005 * plenty of slack room in the global reserve to migrate, otherwise we cannot
4006 * allow the unlink to occur.
Yan, Zhenga22285a2010-05-16 10:48:46 -04004007 */
Josef Bacikd52be812013-05-29 14:54:47 -04004008static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir)
Yan, Zhenga22285a2010-05-16 10:48:46 -04004009{
Yan, Zhenga22285a2010-05-16 10:48:46 -04004010 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004011
Josef Bacike70bea52011-10-11 14:18:24 -04004012 /*
4013 * 1 for the possible orphan item
4014 * 1 for the dir item
4015 * 1 for the dir index
4016 * 1 for the inode ref
Josef Bacike70bea52011-10-11 14:18:24 -04004017 * 1 for the inode
4018 */
Filipe Manana8eab77f2015-11-13 23:57:16 +00004019 return btrfs_start_transaction_fallback_global_rsv(root, 5, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004020}
4021
Chris Mason39279cc2007-06-12 06:35:45 -04004022static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
4023{
Yan, Zhenga22285a2010-05-16 10:48:46 -04004024 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004025 struct btrfs_trans_handle *trans;
David Howells2b0143b2015-03-17 22:25:59 +00004026 struct inode *inode = d_inode(dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04004027 int ret;
4028
Josef Bacikd52be812013-05-29 14:54:47 -04004029 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004030 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004031 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04004032
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004033 btrfs_record_unlink_dir(trans, BTRFS_I(dir), BTRFS_I(d_inode(dentry)),
4034 0);
Chris Mason12fcfd22009-03-24 10:24:20 -04004035
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004036 ret = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
4037 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
4038 dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00004039 if (ret)
4040 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004041
Yan, Zhenga22285a2010-05-16 10:48:46 -04004042 if (inode->i_nlink == 0) {
Nikolay Borisov73f2e542017-02-20 13:50:59 +02004043 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Tsutomu Itohb5324022011-07-19 07:27:20 +00004044 if (ret)
4045 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004046 }
Josef Bacik7b128762008-07-24 12:17:14 -04004047
Tsutomu Itohb5324022011-07-19 07:27:20 +00004048out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004049 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004050 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04004051 return ret;
4052}
4053
Misono Tomohirof60a2362018-04-18 11:34:52 +09004054static int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
Lu Fengqi401b3b12018-08-01 11:32:30 +08004055 struct inode *dir, u64 objectid,
4056 const char *name, int name_len)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004057{
Lu Fengqi401b3b12018-08-01 11:32:30 +08004058 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004059 struct btrfs_path *path;
4060 struct extent_buffer *leaf;
4061 struct btrfs_dir_item *di;
4062 struct btrfs_key key;
4063 u64 index;
4064 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004065 u64 dir_ino = btrfs_ino(BTRFS_I(dir));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004066
4067 path = btrfs_alloc_path();
4068 if (!path)
4069 return -ENOMEM;
4070
Li Zefan33345d012011-04-20 10:31:50 +08004071 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004072 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004073 if (IS_ERR_OR_NULL(di)) {
Liu Bo3cf50682018-09-12 06:06:26 +08004074 ret = di ? PTR_ERR(di) : -ENOENT;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004075 goto out;
4076 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004077
4078 leaf = path->nodes[0];
4079 btrfs_dir_item_key_to_cpu(leaf, di, &key);
4080 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
4081 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004082 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004083 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004084 goto out;
4085 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004086 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004087
Lu Fengqi3ee1c552018-08-01 11:32:28 +08004088 ret = btrfs_del_root_ref(trans, objectid, root->root_key.objectid,
4089 dir_ino, &index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004090 if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004091 if (ret != -ENOENT) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004092 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004093 goto out;
4094 }
Li Zefan33345d012011-04-20 10:31:50 +08004095 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004096 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004097 if (IS_ERR_OR_NULL(di)) {
4098 if (!di)
4099 ret = -ENOENT;
4100 else
4101 ret = PTR_ERR(di);
Jeff Mahoney66642832016-06-10 18:19:25 -04004102 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004103 goto out;
4104 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004105
4106 leaf = path->nodes[0];
4107 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004108 index = key.offset;
4109 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004110 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004111
Lu Fengqi9add2942018-08-01 11:32:26 +08004112 ret = btrfs_delete_delayed_dir_index(trans, BTRFS_I(dir), index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004113 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004114 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004115 goto out;
4116 }
Miao Xie16cdcec2011-04-22 18:12:22 +08004117
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004118 btrfs_i_size_write(BTRFS_I(dir), dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004119 inode_inc_iversion(dir);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07004120 dir->i_mtime = dir->i_ctime = current_time(dir);
Josef Bacik5a24e842012-08-08 10:12:59 -06004121 ret = btrfs_update_inode_fallback(trans, root, dir);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004122 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004123 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004124out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04004125 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004126 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004127}
4128
Misono Tomohiroec42f162018-04-18 11:34:13 +09004129/*
4130 * Helper to check if the subvolume references other subvolumes or if it's
4131 * default.
4132 */
Misono Tomohirof60a2362018-04-18 11:34:52 +09004133static noinline int may_destroy_subvol(struct btrfs_root *root)
Misono Tomohiroec42f162018-04-18 11:34:13 +09004134{
4135 struct btrfs_fs_info *fs_info = root->fs_info;
4136 struct btrfs_path *path;
4137 struct btrfs_dir_item *di;
4138 struct btrfs_key key;
4139 u64 dir_id;
4140 int ret;
4141
4142 path = btrfs_alloc_path();
4143 if (!path)
4144 return -ENOMEM;
4145
4146 /* Make sure this root isn't set as the default subvol */
4147 dir_id = btrfs_super_root_dir(fs_info->super_copy);
4148 di = btrfs_lookup_dir_item(NULL, fs_info->tree_root, path,
4149 dir_id, "default", 7, 0);
4150 if (di && !IS_ERR(di)) {
4151 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key);
4152 if (key.objectid == root->root_key.objectid) {
4153 ret = -EPERM;
4154 btrfs_err(fs_info,
4155 "deleting default subvolume %llu is not allowed",
4156 key.objectid);
4157 goto out;
4158 }
4159 btrfs_release_path(path);
4160 }
4161
4162 key.objectid = root->root_key.objectid;
4163 key.type = BTRFS_ROOT_REF_KEY;
4164 key.offset = (u64)-1;
4165
4166 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
4167 if (ret < 0)
4168 goto out;
4169 BUG_ON(ret == 0);
4170
4171 ret = 0;
4172 if (path->slots[0] > 0) {
4173 path->slots[0]--;
4174 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
4175 if (key.objectid == root->root_key.objectid &&
4176 key.type == BTRFS_ROOT_REF_KEY)
4177 ret = -ENOTEMPTY;
4178 }
4179out:
4180 btrfs_free_path(path);
4181 return ret;
4182}
4183
Nikolay Borisov20a68002018-04-27 14:36:24 +03004184/* Delete all dentries for inodes belonging to the root */
4185static void btrfs_prune_dentries(struct btrfs_root *root)
4186{
4187 struct btrfs_fs_info *fs_info = root->fs_info;
4188 struct rb_node *node;
4189 struct rb_node *prev;
4190 struct btrfs_inode *entry;
4191 struct inode *inode;
4192 u64 objectid = 0;
4193
4194 if (!test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
4195 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
4196
4197 spin_lock(&root->inode_lock);
4198again:
4199 node = root->inode_tree.rb_node;
4200 prev = NULL;
4201 while (node) {
4202 prev = node;
4203 entry = rb_entry(node, struct btrfs_inode, rb_node);
4204
David Sterba37508512018-06-29 10:56:40 +02004205 if (objectid < btrfs_ino(entry))
Nikolay Borisov20a68002018-04-27 14:36:24 +03004206 node = node->rb_left;
David Sterba37508512018-06-29 10:56:40 +02004207 else if (objectid > btrfs_ino(entry))
Nikolay Borisov20a68002018-04-27 14:36:24 +03004208 node = node->rb_right;
4209 else
4210 break;
4211 }
4212 if (!node) {
4213 while (prev) {
4214 entry = rb_entry(prev, struct btrfs_inode, rb_node);
David Sterba37508512018-06-29 10:56:40 +02004215 if (objectid <= btrfs_ino(entry)) {
Nikolay Borisov20a68002018-04-27 14:36:24 +03004216 node = prev;
4217 break;
4218 }
4219 prev = rb_next(prev);
4220 }
4221 }
4222 while (node) {
4223 entry = rb_entry(node, struct btrfs_inode, rb_node);
David Sterba37508512018-06-29 10:56:40 +02004224 objectid = btrfs_ino(entry) + 1;
Nikolay Borisov20a68002018-04-27 14:36:24 +03004225 inode = igrab(&entry->vfs_inode);
4226 if (inode) {
4227 spin_unlock(&root->inode_lock);
4228 if (atomic_read(&inode->i_count) > 1)
4229 d_prune_aliases(inode);
4230 /*
4231 * btrfs_drop_inode will have it removed from the inode
4232 * cache when its usage count hits zero.
4233 */
4234 iput(inode);
4235 cond_resched();
4236 spin_lock(&root->inode_lock);
4237 goto again;
4238 }
4239
4240 if (cond_resched_lock(&root->inode_lock))
4241 goto again;
4242
4243 node = rb_next(node);
4244 }
4245 spin_unlock(&root->inode_lock);
4246}
4247
Misono Tomohirof60a2362018-04-18 11:34:52 +09004248int btrfs_delete_subvolume(struct inode *dir, struct dentry *dentry)
4249{
4250 struct btrfs_fs_info *fs_info = btrfs_sb(dentry->d_sb);
4251 struct btrfs_root *root = BTRFS_I(dir)->root;
4252 struct inode *inode = d_inode(dentry);
4253 struct btrfs_root *dest = BTRFS_I(inode)->root;
4254 struct btrfs_trans_handle *trans;
4255 struct btrfs_block_rsv block_rsv;
4256 u64 root_flags;
Misono Tomohirof60a2362018-04-18 11:34:52 +09004257 int ret;
4258 int err;
4259
4260 /*
4261 * Don't allow to delete a subvolume with send in progress. This is
4262 * inside the inode lock so the error handling that has to drop the bit
4263 * again is not run concurrently.
4264 */
4265 spin_lock(&dest->root_item_lock);
Lu Fengqia7176f72018-08-04 21:10:53 +08004266 if (dest->send_in_progress) {
Misono Tomohirof60a2362018-04-18 11:34:52 +09004267 spin_unlock(&dest->root_item_lock);
4268 btrfs_warn(fs_info,
4269 "attempt to delete subvolume %llu during send",
4270 dest->root_key.objectid);
4271 return -EPERM;
4272 }
Lu Fengqia7176f72018-08-04 21:10:53 +08004273 root_flags = btrfs_root_flags(&dest->root_item);
4274 btrfs_set_root_flags(&dest->root_item,
4275 root_flags | BTRFS_ROOT_SUBVOL_DEAD);
4276 spin_unlock(&dest->root_item_lock);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004277
4278 down_write(&fs_info->subvol_sem);
4279
4280 err = may_destroy_subvol(dest);
4281 if (err)
4282 goto out_up_write;
4283
4284 btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP);
4285 /*
4286 * One for dir inode,
4287 * two for dir entries,
4288 * two for root ref/backref.
4289 */
Gu JinXiangc4c129d2018-05-30 11:00:38 +08004290 err = btrfs_subvolume_reserve_metadata(root, &block_rsv, 5, true);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004291 if (err)
4292 goto out_up_write;
4293
4294 trans = btrfs_start_transaction(root, 0);
4295 if (IS_ERR(trans)) {
4296 err = PTR_ERR(trans);
4297 goto out_release;
4298 }
4299 trans->block_rsv = &block_rsv;
4300 trans->bytes_reserved = block_rsv.size;
4301
4302 btrfs_record_snapshot_destroy(trans, BTRFS_I(dir));
4303
Lu Fengqi401b3b12018-08-01 11:32:30 +08004304 ret = btrfs_unlink_subvol(trans, dir, dest->root_key.objectid,
4305 dentry->d_name.name, dentry->d_name.len);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004306 if (ret) {
4307 err = ret;
4308 btrfs_abort_transaction(trans, ret);
4309 goto out_end_trans;
4310 }
4311
4312 btrfs_record_root_in_trans(trans, dest);
4313
4314 memset(&dest->root_item.drop_progress, 0,
4315 sizeof(dest->root_item.drop_progress));
4316 dest->root_item.drop_level = 0;
4317 btrfs_set_root_refs(&dest->root_item, 0);
4318
4319 if (!test_and_set_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &dest->state)) {
4320 ret = btrfs_insert_orphan_item(trans,
4321 fs_info->tree_root,
4322 dest->root_key.objectid);
4323 if (ret) {
4324 btrfs_abort_transaction(trans, ret);
4325 err = ret;
4326 goto out_end_trans;
4327 }
4328 }
4329
Lu Fengqid1957792018-05-29 15:01:54 +08004330 ret = btrfs_uuid_tree_remove(trans, dest->root_item.uuid,
Misono Tomohirof60a2362018-04-18 11:34:52 +09004331 BTRFS_UUID_KEY_SUBVOL,
4332 dest->root_key.objectid);
4333 if (ret && ret != -ENOENT) {
4334 btrfs_abort_transaction(trans, ret);
4335 err = ret;
4336 goto out_end_trans;
4337 }
4338 if (!btrfs_is_empty_uuid(dest->root_item.received_uuid)) {
Lu Fengqid1957792018-05-29 15:01:54 +08004339 ret = btrfs_uuid_tree_remove(trans,
Misono Tomohirof60a2362018-04-18 11:34:52 +09004340 dest->root_item.received_uuid,
4341 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
4342 dest->root_key.objectid);
4343 if (ret && ret != -ENOENT) {
4344 btrfs_abort_transaction(trans, ret);
4345 err = ret;
4346 goto out_end_trans;
4347 }
4348 }
4349
4350out_end_trans:
4351 trans->block_rsv = NULL;
4352 trans->bytes_reserved = 0;
4353 ret = btrfs_end_transaction(trans);
4354 if (ret && !err)
4355 err = ret;
4356 inode->i_flags |= S_DEAD;
4357out_release:
4358 btrfs_subvolume_release_metadata(fs_info, &block_rsv);
4359out_up_write:
4360 up_write(&fs_info->subvol_sem);
4361 if (err) {
4362 spin_lock(&dest->root_item_lock);
4363 root_flags = btrfs_root_flags(&dest->root_item);
4364 btrfs_set_root_flags(&dest->root_item,
4365 root_flags & ~BTRFS_ROOT_SUBVOL_DEAD);
4366 spin_unlock(&dest->root_item_lock);
4367 } else {
4368 d_invalidate(dentry);
Nikolay Borisov20a68002018-04-27 14:36:24 +03004369 btrfs_prune_dentries(dest);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004370 ASSERT(dest->send_in_progress == 0);
4371
4372 /* the last ref */
4373 if (dest->ino_cache_inode) {
4374 iput(dest->ino_cache_inode);
4375 dest->ino_cache_inode = NULL;
4376 }
4377 }
4378
4379 return err;
4380}
4381
Chris Mason39279cc2007-06-12 06:35:45 -04004382static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
4383{
David Howells2b0143b2015-03-17 22:25:59 +00004384 struct inode *inode = d_inode(dentry);
Chris Mason1832a6d2007-12-21 16:27:21 -05004385 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004386 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004387 struct btrfs_trans_handle *trans;
Filipe Manana44f714d2016-06-06 16:11:13 +01004388 u64 last_unlink_trans;
Chris Mason39279cc2007-06-12 06:35:45 -04004389
David Sterbab3ae2442012-09-13 16:04:34 -06004390 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Yan134d4512007-10-25 15:49:25 -04004391 return -ENOTEMPTY;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004392 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_FIRST_FREE_OBJECTID)
Misono Tomohiroa79a4642018-04-18 11:35:31 +09004393 return btrfs_delete_subvolume(dir, dentry);
Yan134d4512007-10-25 15:49:25 -04004394
Josef Bacikd52be812013-05-29 14:54:47 -04004395 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004396 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004397 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004398
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004399 if (unlikely(btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Lu Fengqi401b3b12018-08-01 11:32:30 +08004400 err = btrfs_unlink_subvol(trans, dir,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004401 BTRFS_I(inode)->location.objectid,
4402 dentry->d_name.name,
4403 dentry->d_name.len);
4404 goto out;
4405 }
4406
Nikolay Borisov73f2e542017-02-20 13:50:59 +02004407 err = btrfs_orphan_add(trans, BTRFS_I(inode));
Josef Bacik7b128762008-07-24 12:17:14 -04004408 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004409 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004410
Filipe Manana44f714d2016-06-06 16:11:13 +01004411 last_unlink_trans = BTRFS_I(inode)->last_unlink_trans;
4412
Chris Mason39279cc2007-06-12 06:35:45 -04004413 /* now the directory is empty */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004414 err = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
4415 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
4416 dentry->d_name.len);
Filipe Manana44f714d2016-06-06 16:11:13 +01004417 if (!err) {
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004418 btrfs_i_size_write(BTRFS_I(inode), 0);
Filipe Manana44f714d2016-06-06 16:11:13 +01004419 /*
4420 * Propagate the last_unlink_trans value of the deleted dir to
4421 * its parent directory. This is to prevent an unrecoverable
4422 * log tree in the case we do something like this:
4423 * 1) create dir foo
4424 * 2) create snapshot under dir foo
4425 * 3) delete the snapshot
4426 * 4) rmdir foo
4427 * 5) mkdir foo
4428 * 6) fsync foo or some file inside foo
4429 */
4430 if (last_unlink_trans >= trans->transid)
4431 BTRFS_I(dir)->last_unlink_trans = last_unlink_trans;
4432 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004433out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004434 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004435 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39544012007-12-12 14:38:19 -05004436
Chris Mason39279cc2007-06-12 06:35:45 -04004437 return err;
4438}
4439
Chris Mason28f75a02015-02-04 06:59:29 -08004440static int truncate_space_check(struct btrfs_trans_handle *trans,
4441 struct btrfs_root *root,
4442 u64 bytes_deleted)
4443{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004444 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason28f75a02015-02-04 06:59:29 -08004445 int ret;
4446
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004447 /*
4448 * This is only used to apply pressure to the enospc system, we don't
4449 * intend to use this reservation at all.
4450 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004451 bytes_deleted = btrfs_csum_bytes_to_leaves(fs_info, bytes_deleted);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004452 bytes_deleted *= fs_info->nodesize;
4453 ret = btrfs_block_rsv_add(root, &fs_info->trans_block_rsv,
Chris Mason28f75a02015-02-04 06:59:29 -08004454 bytes_deleted, BTRFS_RESERVE_NO_FLUSH);
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004455 if (!ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004456 trace_btrfs_space_reservation(fs_info, "transaction",
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004457 trans->transid,
4458 bytes_deleted, 1);
Chris Mason28f75a02015-02-04 06:59:29 -08004459 trans->bytes_reserved += bytes_deleted;
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004460 }
Chris Mason28f75a02015-02-04 06:59:29 -08004461 return ret;
4462
4463}
4464
Josef Bacikddfae632017-10-19 14:16:02 -04004465/*
4466 * Return this if we need to call truncate_block for the last bit of the
4467 * truncate.
4468 */
4469#define NEED_TRUNCATE_BLOCK 1
Filipe Manana0305cd52015-10-16 12:34:25 +01004470
Chris Mason323ac952008-10-01 19:05:46 -04004471/*
Chris Mason39279cc2007-06-12 06:35:45 -04004472 * this can truncate away extent items, csum items and directory items.
4473 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04004474 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04004475 *
4476 * csum items that cross the new i_size are truncated to the new size
4477 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04004478 *
4479 * min_type is the minimum key type to truncate down to. If set to 0, this
4480 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04004481 */
Yan, Zheng80825102009-11-12 09:35:36 +00004482int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
4483 struct btrfs_root *root,
4484 struct inode *inode,
4485 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004486{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004487 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04004488 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004489 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004490 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00004491 struct btrfs_key key;
4492 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04004493 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04004494 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004495 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004496 u64 item_end = 0;
Filipe Mananac1aa4572015-06-20 18:20:09 +01004497 u64 last_size = new_size;
Yan, Zheng80825102009-11-12 09:35:36 +00004498 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04004499 int found_extent;
4500 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05004501 int pending_del_nr = 0;
4502 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04004503 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00004504 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004505 u64 ino = btrfs_ino(BTRFS_I(inode));
Chris Mason28ed1342014-12-17 09:41:04 -08004506 u64 bytes_deleted = 0;
Thomas Meyer897ca812017-10-07 16:02:21 +02004507 bool be_nice = false;
4508 bool should_throttle = false;
4509 bool should_end = false;
Yan, Zheng80825102009-11-12 09:35:36 +00004510
4511 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04004512
Chris Mason28ed1342014-12-17 09:41:04 -08004513 /*
4514 * for non-free space inodes and ref cows, we want to back off from
4515 * time to time
4516 */
Nikolay Borisov70ddc552017-02-20 13:50:35 +02004517 if (!btrfs_is_free_space_inode(BTRFS_I(inode)) &&
Chris Mason28ed1342014-12-17 09:41:04 -08004518 test_bit(BTRFS_ROOT_REF_COWS, &root->state))
Thomas Meyer897ca812017-10-07 16:02:21 +02004519 be_nice = true;
Chris Mason28ed1342014-12-17 09:41:04 -08004520
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004521 path = btrfs_alloc_path();
4522 if (!path)
4523 return -ENOMEM;
David Sterbae4058b52015-11-27 16:31:35 +01004524 path->reada = READA_BACK;
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004525
Josef Bacik5dc562c2012-08-17 13:14:17 -04004526 /*
4527 * We want to drop from the next block forward in case this new size is
4528 * not block aligned since we will be keeping the last block of the
4529 * extent just the way it is.
4530 */
Miao Xie27cdeb72014-04-02 19:51:05 +08004531 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004532 root == fs_info->tree_root)
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02004533 btrfs_drop_extent_cache(BTRFS_I(inode), ALIGN(new_size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004534 fs_info->sectorsize),
Jeff Mahoneyda170662016-06-15 09:22:56 -04004535 (u64)-1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00004536
Miao Xie16cdcec2011-04-22 18:12:22 +08004537 /*
4538 * This function is also used to drop the items in the log tree before
4539 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
4540 * it is used to drop the loged items. So we shouldn't kill the delayed
4541 * items.
4542 */
4543 if (min_type == 0 && root == BTRFS_I(inode)->root)
Nikolay Borisov4ccb5c72017-01-10 20:35:38 +02004544 btrfs_kill_delayed_inode_items(BTRFS_I(inode));
Miao Xie16cdcec2011-04-22 18:12:22 +08004545
Li Zefan33345d012011-04-20 10:31:50 +08004546 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04004547 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04004548 key.type = (u8)-1;
4549
Chris Mason85e21ba2008-01-29 15:11:36 -05004550search_again:
Chris Mason28ed1342014-12-17 09:41:04 -08004551 /*
4552 * with a 16K leaf size and 128MB extents, you can actually queue
4553 * up a huge file in a single leaf. Most of the time that
4554 * bytes_deleted is > 0, it will be huge by the time we get here
4555 */
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004556 if (be_nice && bytes_deleted > SZ_32M &&
4557 btrfs_should_end_transaction(trans)) {
4558 ret = -EAGAIN;
Yan, Zheng80825102009-11-12 09:35:36 +00004559 goto out;
4560 }
Chris Masond3977122009-01-05 21:25:51 -05004561
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004562 path->leave_spinning = 1;
4563 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
4564 if (ret < 0)
4565 goto out;
4566
Chris Mason85e21ba2008-01-29 15:11:36 -05004567 if (ret > 0) {
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004568 ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -04004569 /* there are no items in the tree for us to truncate, we're
4570 * done
4571 */
Yan, Zheng80825102009-11-12 09:35:36 +00004572 if (path->slots[0] == 0)
4573 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05004574 path->slots[0]--;
4575 }
4576
Chris Masond3977122009-01-05 21:25:51 -05004577 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04004578 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04004579 leaf = path->nodes[0];
4580 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02004581 found_type = found_key.type;
Chris Mason39279cc2007-06-12 06:35:45 -04004582
Li Zefan33345d012011-04-20 10:31:50 +08004583 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04004584 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004585
Chris Mason85e21ba2008-01-29 15:11:36 -05004586 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004587 break;
4588
Chris Mason5f39d392007-10-15 16:14:19 -04004589 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04004590 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04004591 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04004592 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04004593 extent_type = btrfs_file_extent_type(leaf, fi);
4594 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04004595 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04004596 btrfs_file_extent_num_bytes(leaf, fi);
Liu Bo09ed2f12017-03-10 11:09:48 -08004597
4598 trace_btrfs_truncate_show_fi_regular(
4599 BTRFS_I(inode), leaf, fi,
4600 found_key.offset);
Chris Mason179e29e2007-11-01 11:28:41 -04004601 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Qu Wenruoe41ca582018-06-06 15:41:49 +08004602 item_end += btrfs_file_extent_ram_bytes(leaf,
4603 fi);
Liu Bo09ed2f12017-03-10 11:09:48 -08004604
4605 trace_btrfs_truncate_show_fi_inline(
4606 BTRFS_I(inode), leaf, fi, path->slots[0],
4607 found_key.offset);
Chris Mason39279cc2007-06-12 06:35:45 -04004608 }
Yan008630c2007-11-07 13:31:09 -05004609 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04004610 }
Yan, Zheng80825102009-11-12 09:35:36 +00004611 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04004612 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00004613 } else {
Filipe Manana76b42ab2017-02-14 16:56:01 +00004614 if (item_end < new_size)
Yan, Zheng80825102009-11-12 09:35:36 +00004615 break;
4616 if (found_key.offset >= new_size)
4617 del_item = 1;
4618 else
4619 del_item = 0;
4620 }
Chris Mason39279cc2007-06-12 06:35:45 -04004621 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004622 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04004623 if (found_type != BTRFS_EXTENT_DATA_KEY)
4624 goto delete;
4625
4626 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04004627 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04004628 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05004629 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04004630 u64 orig_num_bytes =
4631 btrfs_file_extent_num_bytes(leaf, fi);
Qu Wenruofda28322013-02-26 08:10:22 +00004632 extent_num_bytes = ALIGN(new_size -
4633 found_key.offset,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004634 fs_info->sectorsize);
Chris Masondb945352007-10-15 16:15:53 -04004635 btrfs_set_file_extent_num_bytes(leaf, fi,
4636 extent_num_bytes);
4637 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05004638 extent_num_bytes);
Miao Xie27cdeb72014-04-02 19:51:05 +08004639 if (test_bit(BTRFS_ROOT_REF_COWS,
4640 &root->state) &&
4641 extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004642 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04004643 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004644 } else {
Chris Masondb945352007-10-15 16:15:53 -04004645 extent_num_bytes =
4646 btrfs_file_extent_disk_num_bytes(leaf,
4647 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004648 extent_offset = found_key.offset -
4649 btrfs_file_extent_offset(leaf, fi);
4650
Chris Mason39279cc2007-06-12 06:35:45 -04004651 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05004652 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004653 if (extent_start != 0) {
4654 found_extent = 1;
Miao Xie27cdeb72014-04-02 19:51:05 +08004655 if (test_bit(BTRFS_ROOT_REF_COWS,
4656 &root->state))
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004657 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04004658 }
4659 }
Chris Mason90692182008-02-08 13:49:28 -05004660 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04004661 /*
4662 * we can't truncate inline items that have had
4663 * special encodings
4664 */
4665 if (!del_item &&
Chris Masonc8b97812008-10-29 14:49:59 -04004666 btrfs_file_extent_encryption(leaf, fi) == 0 &&
Josef Bacikddfae632017-10-19 14:16:02 -04004667 btrfs_file_extent_other_encoding(leaf, fi) == 0 &&
4668 btrfs_file_extent_compression(leaf, fi) == 0) {
4669 u32 size = (u32)(new_size - found_key.offset);
Chris Mason514ac8a2014-01-03 21:07:00 -08004670
Josef Bacikddfae632017-10-19 14:16:02 -04004671 btrfs_set_file_extent_ram_bytes(leaf, fi, size);
4672 size = btrfs_file_extent_calc_inline_size(size);
4673 btrfs_truncate_item(root->fs_info, path, size, 1);
4674 } else if (!del_item) {
Chris Mason514ac8a2014-01-03 21:07:00 -08004675 /*
Josef Bacikddfae632017-10-19 14:16:02 -04004676 * We have to bail so the last_size is set to
4677 * just before this extent.
Chris Mason514ac8a2014-01-03 21:07:00 -08004678 */
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004679 ret = NEED_TRUNCATE_BLOCK;
Josef Bacikddfae632017-10-19 14:16:02 -04004680 break;
Chris Mason90692182008-02-08 13:49:28 -05004681 }
Josef Bacikddfae632017-10-19 14:16:02 -04004682
4683 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state))
4684 inode_sub_bytes(inode, item_end + 1 - new_size);
Chris Mason39279cc2007-06-12 06:35:45 -04004685 }
Chris Mason179e29e2007-11-01 11:28:41 -04004686delete:
Josef Bacikddfae632017-10-19 14:16:02 -04004687 if (del_item)
4688 last_size = found_key.offset;
4689 else
4690 last_size = new_size;
Chris Mason39279cc2007-06-12 06:35:45 -04004691 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05004692 if (!pending_del_nr) {
4693 /* no pending yet, add ourselves */
4694 pending_del_slot = path->slots[0];
4695 pending_del_nr = 1;
4696 } else if (pending_del_nr &&
4697 path->slots[0] + 1 == pending_del_slot) {
4698 /* hop on the pending chunk */
4699 pending_del_nr++;
4700 pending_del_slot = path->slots[0];
4701 } else {
Chris Masond3977122009-01-05 21:25:51 -05004702 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05004703 }
Chris Mason39279cc2007-06-12 06:35:45 -04004704 } else {
4705 break;
4706 }
Thomas Meyer897ca812017-10-07 16:02:21 +02004707 should_throttle = false;
Chris Mason28f75a02015-02-04 06:59:29 -08004708
Miao Xie27cdeb72014-04-02 19:51:05 +08004709 if (found_extent &&
4710 (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004711 root == fs_info->tree_root)) {
Chris Masonb9473432009-03-13 11:00:37 -04004712 btrfs_set_path_blocking(path);
Chris Mason28ed1342014-12-17 09:41:04 -08004713 bytes_deleted += extent_num_bytes;
Josef Bacik84f7d8e2017-09-29 15:43:49 -04004714 ret = btrfs_free_extent(trans, root, extent_start,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004715 extent_num_bytes, 0,
4716 btrfs_header_owner(leaf),
Filipe Mananab06c4bf2015-10-23 07:52:54 +01004717 ino, extent_offset);
Omar Sandoval05522102018-05-11 13:13:31 -07004718 if (ret) {
4719 btrfs_abort_transaction(trans, ret);
4720 break;
4721 }
Lu Fengqi7c861622018-10-11 13:40:36 +08004722 if (btrfs_should_throttle_delayed_refs(trans))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004723 btrfs_async_run_delayed_refs(fs_info,
Wang Xiaoguangdd4b8572016-10-18 15:56:13 +08004724 trans->delayed_ref_updates * 2,
4725 trans->transid, 0);
Chris Mason28f75a02015-02-04 06:59:29 -08004726 if (be_nice) {
4727 if (truncate_space_check(trans, root,
4728 extent_num_bytes)) {
Thomas Meyer897ca812017-10-07 16:02:21 +02004729 should_end = true;
Chris Mason28f75a02015-02-04 06:59:29 -08004730 }
Lu Fengqi7c861622018-10-11 13:40:36 +08004731 if (btrfs_should_throttle_delayed_refs(trans))
Thomas Meyer897ca812017-10-07 16:02:21 +02004732 should_throttle = true;
Chris Mason28f75a02015-02-04 06:59:29 -08004733 }
Chris Mason39279cc2007-06-12 06:35:45 -04004734 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004735
Yan, Zheng80825102009-11-12 09:35:36 +00004736 if (found_type == BTRFS_INODE_ITEM_KEY)
4737 break;
4738
4739 if (path->slots[0] == 0 ||
Josef Bacik12621332015-02-03 07:50:16 -08004740 path->slots[0] != pending_del_slot ||
Chris Mason28f75a02015-02-04 06:59:29 -08004741 should_throttle || should_end) {
Yan, Zheng80825102009-11-12 09:35:36 +00004742 if (pending_del_nr) {
4743 ret = btrfs_del_items(trans, root, path,
4744 pending_del_slot,
4745 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004746 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004747 btrfs_abort_transaction(trans, ret);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004748 break;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004749 }
Yan, Zheng80825102009-11-12 09:35:36 +00004750 pending_del_nr = 0;
4751 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004752 btrfs_release_path(path);
Chris Mason28f75a02015-02-04 06:59:29 -08004753 if (should_throttle) {
Josef Bacik12621332015-02-03 07:50:16 -08004754 unsigned long updates = trans->delayed_ref_updates;
4755 if (updates) {
4756 trans->delayed_ref_updates = 0;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004757 ret = btrfs_run_delayed_refs(trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004758 updates * 2);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004759 if (ret)
4760 break;
Josef Bacik12621332015-02-03 07:50:16 -08004761 }
4762 }
Chris Mason28f75a02015-02-04 06:59:29 -08004763 /*
4764 * if we failed to refill our space rsv, bail out
4765 * and let the transaction restart
4766 */
4767 if (should_end) {
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004768 ret = -EAGAIN;
4769 break;
Chris Mason28f75a02015-02-04 06:59:29 -08004770 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004771 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00004772 } else {
4773 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05004774 }
Chris Mason39279cc2007-06-12 06:35:45 -04004775 }
Yan, Zheng80825102009-11-12 09:35:36 +00004776out:
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004777 if (ret >= 0 && pending_del_nr) {
4778 int err;
4779
4780 err = btrfs_del_items(trans, root, path, pending_del_slot,
Chris Mason85e21ba2008-01-29 15:11:36 -05004781 pending_del_nr);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004782 if (err) {
4783 btrfs_abort_transaction(trans, err);
4784 ret = err;
4785 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004786 }
Filipe Manana76b42ab2017-02-14 16:56:01 +00004787 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
4788 ASSERT(last_size >= new_size);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004789 if (!ret && last_size > new_size)
Filipe Manana76b42ab2017-02-14 16:56:01 +00004790 last_size = new_size;
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004791 btrfs_ordered_update_i_size(inode, last_size, NULL);
Filipe Manana76b42ab2017-02-14 16:56:01 +00004792 }
Chris Mason28ed1342014-12-17 09:41:04 -08004793
Chris Mason39279cc2007-06-12 06:35:45 -04004794 btrfs_free_path(path);
Chris Mason28ed1342014-12-17 09:41:04 -08004795
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004796 if (be_nice && bytes_deleted > SZ_32M && (ret >= 0 || ret == -EAGAIN)) {
Chris Mason28ed1342014-12-17 09:41:04 -08004797 unsigned long updates = trans->delayed_ref_updates;
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004798 int err;
4799
Chris Mason28ed1342014-12-17 09:41:04 -08004800 if (updates) {
4801 trans->delayed_ref_updates = 0;
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004802 err = btrfs_run_delayed_refs(trans, updates * 2);
4803 if (err)
4804 ret = err;
Chris Mason28ed1342014-12-17 09:41:04 -08004805 }
4806 }
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004807 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04004808}
4809
Chris Masona52d9a82007-08-27 16:49:44 -04004810/*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304811 * btrfs_truncate_block - read, zero a chunk and write a block
Josef Bacik2aaa6652012-08-29 14:27:18 -04004812 * @inode - inode that we're zeroing
4813 * @from - the offset to start zeroing
4814 * @len - the length to zero, 0 to zero the entire range respective to the
4815 * offset
4816 * @front - zero up to the offset instead of from the offset on
4817 *
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304818 * This will find the block for the "from" offset and cow the block and zero the
Josef Bacik2aaa6652012-08-29 14:27:18 -04004819 * part we want to zero. This is used with truncate and hole punching.
Chris Masona52d9a82007-08-27 16:49:44 -04004820 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304821int btrfs_truncate_block(struct inode *inode, loff_t from, loff_t len,
Josef Bacik2aaa6652012-08-29 14:27:18 -04004822 int front)
Chris Masona52d9a82007-08-27 16:49:44 -04004823{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004824 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004825 struct address_space *mapping = inode->i_mapping;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004826 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4827 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004828 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08004829 struct extent_changeset *data_reserved = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004830 char *kaddr;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004831 u32 blocksize = fs_info->sectorsize;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004832 pgoff_t index = from >> PAGE_SHIFT;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304833 unsigned offset = from & (blocksize - 1);
Chris Masona52d9a82007-08-27 16:49:44 -04004834 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004835 gfp_t mask = btrfs_alloc_write_mask(mapping);
Chris Masona52d9a82007-08-27 16:49:44 -04004836 int ret = 0;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304837 u64 block_start;
4838 u64 block_end;
Chris Masona52d9a82007-08-27 16:49:44 -04004839
Nikolay Borisovb03ebd92018-01-18 14:47:06 +02004840 if (IS_ALIGNED(offset, blocksize) &&
4841 (!len || IS_ALIGNED(len, blocksize)))
Chris Masona52d9a82007-08-27 16:49:44 -04004842 goto out;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304843
Josef Bacik8b62f872017-10-19 14:15:55 -04004844 block_start = round_down(from, blocksize);
4845 block_end = block_start + blocksize - 1;
4846
Qu Wenruo364ecf32017-02-27 15:10:38 +08004847 ret = btrfs_delalloc_reserve_space(inode, &data_reserved,
Josef Bacik8b62f872017-10-19 14:15:55 -04004848 block_start, blocksize);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004849 if (ret)
4850 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004851
Chris Mason211c17f2008-05-15 09:13:45 -04004852again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004853 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004854 if (!page) {
Qu Wenruobc42bda2017-02-27 15:10:39 +08004855 btrfs_delalloc_release_space(inode, data_reserved,
Qu Wenruo43b18592017-12-12 15:34:32 +08004856 block_start, blocksize, true);
4857 btrfs_delalloc_release_extents(BTRFS_I(inode), blocksize, true);
Miao Xieac6a2b32012-12-05 10:56:13 +00004858 ret = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -04004859 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04004860 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004861
Chris Masona52d9a82007-08-27 16:49:44 -04004862 if (!PageUptodate(page)) {
4863 ret = btrfs_readpage(NULL, page);
4864 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004865 if (page->mapping != mapping) {
4866 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004867 put_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004868 goto again;
4869 }
Chris Masona52d9a82007-08-27 16:49:44 -04004870 if (!PageUptodate(page)) {
4871 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04004872 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04004873 }
4874 }
Chris Mason211c17f2008-05-15 09:13:45 -04004875 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004876
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304877 lock_extent_bits(io_tree, block_start, block_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004878 set_page_extent_mapped(page);
4879
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304880 ordered = btrfs_lookup_ordered_extent(inode, block_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004881 if (ordered) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304882 unlock_extent_cached(io_tree, block_start, block_end,
David Sterbae43bbe52017-12-12 21:43:52 +01004883 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004884 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004885 put_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04004886 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004887 btrfs_put_ordered_extent(ordered);
4888 goto again;
4889 }
4890
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304891 clear_extent_bit(&BTRFS_I(inode)->io_tree, block_start, block_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06004892 EXTENT_DIRTY | EXTENT_DELALLOC |
4893 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
David Sterbaae0f1622017-10-31 16:37:52 +01004894 0, 0, &cached_state);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004895
Filipe Mananae3b8a482017-11-04 00:16:59 +00004896 ret = btrfs_set_extent_delalloc(inode, block_start, block_end, 0,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08004897 &cached_state, 0);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004898 if (ret) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304899 unlock_extent_cached(io_tree, block_start, block_end,
David Sterbae43bbe52017-12-12 21:43:52 +01004900 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004901 goto out_unlock;
4902 }
4903
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304904 if (offset != blocksize) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04004905 if (!len)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304906 len = blocksize - offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004907 kaddr = kmap(page);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004908 if (front)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304909 memset(kaddr + (block_start - page_offset(page)),
4910 0, offset);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004911 else
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304912 memset(kaddr + (block_start - page_offset(page)) + offset,
4913 0, len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004914 flush_dcache_page(page);
4915 kunmap(page);
4916 }
Chris Mason247e7432008-07-17 12:53:51 -04004917 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004918 set_page_dirty(page);
David Sterbae43bbe52017-12-12 21:43:52 +01004919 unlock_extent_cached(io_tree, block_start, block_end, &cached_state);
Chris Mason39279cc2007-06-12 06:35:45 -04004920
Chris Mason89642222008-07-24 09:41:53 -04004921out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04004922 if (ret)
Qu Wenruobc42bda2017-02-27 15:10:39 +08004923 btrfs_delalloc_release_space(inode, data_reserved, block_start,
Qu Wenruo43b18592017-12-12 15:34:32 +08004924 blocksize, true);
4925 btrfs_delalloc_release_extents(BTRFS_I(inode), blocksize, (ret != 0));
Chris Mason39279cc2007-06-12 06:35:45 -04004926 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004927 put_page(page);
Chris Mason39279cc2007-06-12 06:35:45 -04004928out:
Qu Wenruo364ecf32017-02-27 15:10:38 +08004929 extent_changeset_free(data_reserved);
Chris Mason39279cc2007-06-12 06:35:45 -04004930 return ret;
4931}
4932
Josef Bacik16e75492013-10-22 12:18:51 -04004933static int maybe_insert_hole(struct btrfs_root *root, struct inode *inode,
4934 u64 offset, u64 len)
4935{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004936 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik16e75492013-10-22 12:18:51 -04004937 struct btrfs_trans_handle *trans;
4938 int ret;
4939
4940 /*
4941 * Still need to make sure the inode looks like it's been updated so
4942 * that any holes get logged if we fsync.
4943 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004944 if (btrfs_fs_incompat(fs_info, NO_HOLES)) {
4945 BTRFS_I(inode)->last_trans = fs_info->generation;
Josef Bacik16e75492013-10-22 12:18:51 -04004946 BTRFS_I(inode)->last_sub_trans = root->log_transid;
4947 BTRFS_I(inode)->last_log_commit = root->last_log_commit;
4948 return 0;
4949 }
4950
4951 /*
4952 * 1 - for the one we're dropping
4953 * 1 - for the one we're adding
4954 * 1 - for updating the inode.
4955 */
4956 trans = btrfs_start_transaction(root, 3);
4957 if (IS_ERR(trans))
4958 return PTR_ERR(trans);
4959
4960 ret = btrfs_drop_extents(trans, root, inode, offset, offset + len, 1);
4961 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004962 btrfs_abort_transaction(trans, ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004963 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04004964 return ret;
4965 }
4966
David Sterbaf85b7372017-01-20 14:54:07 +01004967 ret = btrfs_insert_file_extent(trans, root, btrfs_ino(BTRFS_I(inode)),
4968 offset, 0, 0, len, 0, len, 0, 0, 0);
Josef Bacik16e75492013-10-22 12:18:51 -04004969 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004970 btrfs_abort_transaction(trans, ret);
Josef Bacik16e75492013-10-22 12:18:51 -04004971 else
4972 btrfs_update_inode(trans, root, inode);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004973 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04004974 return ret;
4975}
4976
Josef Bacik695a0d02011-03-04 15:46:53 -05004977/*
4978 * This function puts in dummy file extents for the area we're creating a hole
4979 * for. So if we are truncating this file to a larger size we need to insert
4980 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
4981 * the range between oldsize and size
4982 */
Josef Bacika41ad392011-01-31 15:30:16 -05004983int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04004984{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004985 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Yan Zheng9036c102008-10-30 14:19:41 -04004986 struct btrfs_root *root = BTRFS_I(inode)->root;
4987 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004988 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004989 struct extent_state *cached_state = NULL;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004990 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004991 u64 hole_start = ALIGN(oldsize, fs_info->sectorsize);
4992 u64 block_end = ALIGN(size, fs_info->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04004993 u64 last_byte;
4994 u64 cur_offset;
4995 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04004996 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04004997
Josef Bacika71754f2013-06-17 17:14:39 -04004998 /*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304999 * If our size started in the middle of a block we need to zero out the
5000 * rest of the block before we expand the i_size, otherwise we could
Josef Bacika71754f2013-06-17 17:14:39 -04005001 * expose stale data.
5002 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05305003 err = btrfs_truncate_block(inode, oldsize, 0, 0);
Josef Bacika71754f2013-06-17 17:14:39 -04005004 if (err)
5005 return err;
5006
Yan Zheng9036c102008-10-30 14:19:41 -04005007 if (size <= hole_start)
5008 return 0;
5009
Yan Zheng9036c102008-10-30 14:19:41 -04005010 while (1) {
5011 struct btrfs_ordered_extent *ordered;
Miao Xiefa7c1492013-09-26 13:15:27 +08005012
David Sterbaff13db42015-12-03 14:30:40 +01005013 lock_extent_bits(io_tree, hole_start, block_end - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01005014 &cached_state);
Nikolay Borisova776c6f2017-02-20 13:50:49 +02005015 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), hole_start,
Miao Xiefa7c1492013-09-26 13:15:27 +08005016 block_end - hole_start);
Yan Zheng9036c102008-10-30 14:19:41 -04005017 if (!ordered)
5018 break;
Josef Bacik2ac55d42010-02-03 19:33:23 +00005019 unlock_extent_cached(io_tree, hole_start, block_end - 1,
David Sterbae43bbe52017-12-12 21:43:52 +01005020 &cached_state);
Miao Xiefa7c1492013-09-26 13:15:27 +08005021 btrfs_start_ordered_extent(inode, ordered, 1);
Yan Zheng9036c102008-10-30 14:19:41 -04005022 btrfs_put_ordered_extent(ordered);
5023 }
5024
Yan Zheng9036c102008-10-30 14:19:41 -04005025 cur_offset = hole_start;
5026 while (1) {
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02005027 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, cur_offset,
Yan Zheng9036c102008-10-30 14:19:41 -04005028 block_end - cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005029 if (IS_ERR(em)) {
5030 err = PTR_ERR(em);
Zach Brownf2767952013-01-08 19:37:58 +00005031 em = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005032 break;
5033 }
Yan Zheng9036c102008-10-30 14:19:41 -04005034 last_byte = min(extent_map_end(em), block_end);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005035 last_byte = ALIGN(last_byte, fs_info->sectorsize);
Yan, Zheng80825102009-11-12 09:35:36 +00005036 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04005037 struct extent_map *hole_em;
Yan Zheng9036c102008-10-30 14:19:41 -04005038 hole_size = last_byte - cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00005039
Josef Bacik16e75492013-10-22 12:18:51 -04005040 err = maybe_insert_hole(root, inode, cur_offset,
5041 hole_size);
5042 if (err)
Chris Mason771ed682008-11-06 22:02:51 -05005043 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02005044 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04005045 cur_offset + hole_size - 1, 0);
5046 hole_em = alloc_extent_map();
5047 if (!hole_em) {
5048 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
5049 &BTRFS_I(inode)->runtime_flags);
5050 goto next;
5051 }
5052 hole_em->start = cur_offset;
5053 hole_em->len = hole_size;
5054 hole_em->orig_start = cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00005055
Josef Bacik5dc562c2012-08-17 13:14:17 -04005056 hole_em->block_start = EXTENT_MAP_HOLE;
5057 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05005058 hole_em->orig_block_len = 0;
Josef Bacikcc95bef2013-04-04 14:31:27 -04005059 hole_em->ram_bytes = hole_size;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005060 hole_em->bdev = fs_info->fs_devices->latest_bdev;
Josef Bacik5dc562c2012-08-17 13:14:17 -04005061 hole_em->compress_type = BTRFS_COMPRESS_NONE;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005062 hole_em->generation = fs_info->generation;
Josef Bacik5dc562c2012-08-17 13:14:17 -04005063
5064 while (1) {
5065 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04005066 err = add_extent_mapping(em_tree, hole_em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04005067 write_unlock(&em_tree->lock);
5068 if (err != -EEXIST)
5069 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02005070 btrfs_drop_extent_cache(BTRFS_I(inode),
5071 cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04005072 cur_offset +
5073 hole_size - 1, 0);
5074 }
5075 free_extent_map(hole_em);
Yan Zheng9036c102008-10-30 14:19:41 -04005076 }
Josef Bacik16e75492013-10-22 12:18:51 -04005077next:
Yan Zheng9036c102008-10-30 14:19:41 -04005078 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04005079 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04005080 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00005081 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04005082 break;
5083 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04005084 free_extent_map(em);
David Sterbae43bbe52017-12-12 21:43:52 +01005085 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state);
Yan Zheng9036c102008-10-30 14:19:41 -04005086 return err;
5087}
5088
Eric Sandeen3972f262013-01-12 02:57:22 +00005089static int btrfs_setsize(struct inode *inode, struct iattr *attr)
Yan, Zheng80825102009-11-12 09:35:36 +00005090{
Miao Xief4a2f4c2011-12-14 20:12:01 -05005091 struct btrfs_root *root = BTRFS_I(inode)->root;
5092 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05005093 loff_t oldsize = i_size_read(inode);
Eric Sandeen3972f262013-01-12 02:57:22 +00005094 loff_t newsize = attr->ia_size;
5095 int mask = attr->ia_valid;
Yan, Zheng80825102009-11-12 09:35:36 +00005096 int ret;
5097
Eric Sandeen3972f262013-01-12 02:57:22 +00005098 /*
5099 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
5100 * special case where we need to update the times despite not having
5101 * these flags set. For all other operations the VFS set these flags
5102 * explicitly if it wants a timestamp update.
5103 */
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08005104 if (newsize != oldsize) {
5105 inode_inc_iversion(inode);
5106 if (!(mask & (ATTR_CTIME | ATTR_MTIME)))
5107 inode->i_ctime = inode->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07005108 current_time(inode);
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08005109 }
Eric Sandeen3972f262013-01-12 02:57:22 +00005110
Josef Bacika41ad392011-01-31 15:30:16 -05005111 if (newsize > oldsize) {
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005112 /*
David Sterbaea14b57f2017-06-22 02:19:11 +02005113 * Don't do an expanding truncate while snapshotting is ongoing.
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005114 * This is to ensure the snapshot captures a fully consistent
5115 * state of this file - if the snapshot captures this expanding
5116 * truncation, it must capture all writes that happened before
5117 * this truncation.
5118 */
Zhao Lei0bc19f902016-01-06 18:56:36 +08005119 btrfs_wait_for_snapshot_creation(root);
Josef Bacika41ad392011-01-31 15:30:16 -05005120 ret = btrfs_cont_expand(inode, oldsize, newsize);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005121 if (ret) {
David Sterbaea14b57f2017-06-22 02:19:11 +02005122 btrfs_end_write_no_snapshotting(root);
Yan, Zheng80825102009-11-12 09:35:36 +00005123 return ret;
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005124 }
Yan, Zheng80825102009-11-12 09:35:36 +00005125
Miao Xief4a2f4c2011-12-14 20:12:01 -05005126 trans = btrfs_start_transaction(root, 1);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005127 if (IS_ERR(trans)) {
David Sterbaea14b57f2017-06-22 02:19:11 +02005128 btrfs_end_write_no_snapshotting(root);
Miao Xief4a2f4c2011-12-14 20:12:01 -05005129 return PTR_ERR(trans);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005130 }
Miao Xief4a2f4c2011-12-14 20:12:01 -05005131
5132 i_size_write(inode, newsize);
5133 btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
Chandan Rajendra27772b62016-01-21 15:56:03 +05305134 pagecache_isize_extended(inode, oldsize, newsize);
Miao Xief4a2f4c2011-12-14 20:12:01 -05005135 ret = btrfs_update_inode(trans, root, inode);
David Sterbaea14b57f2017-06-22 02:19:11 +02005136 btrfs_end_write_no_snapshotting(root);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005137 btrfs_end_transaction(trans);
Josef Bacika41ad392011-01-31 15:30:16 -05005138 } else {
Yan, Zheng80825102009-11-12 09:35:36 +00005139
Josef Bacika41ad392011-01-31 15:30:16 -05005140 /*
5141 * We're truncating a file that used to have good data down to
5142 * zero. Make sure it gets into the ordered flush list so that
5143 * any new writes get down to disk quickly.
5144 */
5145 if (newsize == 0)
Josef Bacik72ac3c02012-05-23 14:13:11 -04005146 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
5147 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00005148
Josef Bacika41ad392011-01-31 15:30:16 -05005149 truncate_setsize(inode, newsize);
Miao Xie2e60a512013-02-08 07:01:08 +00005150
5151 /* Disable nonlocked read DIO to avoid the end less truncate */
Nikolay Borisovabcefb12017-02-20 13:51:10 +02005152 btrfs_inode_block_unlocked_dio(BTRFS_I(inode));
Miao Xie2e60a512013-02-08 07:01:08 +00005153 inode_dio_wait(inode);
Nikolay Borisov0b581702017-02-20 13:51:11 +02005154 btrfs_inode_resume_unlocked_dio(BTRFS_I(inode));
Miao Xie2e60a512013-02-08 07:01:08 +00005155
Filipe Manana213e8c52018-02-06 20:40:31 +00005156 ret = btrfs_truncate(inode, newsize == oldsize);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005157 if (ret && inode->i_nlink) {
5158 int err;
5159
5160 /*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07005161 * Truncate failed, so fix up the in-memory size. We
5162 * adjusted disk_i_size down as we removed extents, so
5163 * wait for disk_i_size to be stable and then update the
5164 * in-memory size to match.
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005165 */
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07005166 err = btrfs_wait_ordered_range(inode, 0, (u64)-1);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005167 if (err)
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07005168 return err;
5169 i_size_write(inode, BTRFS_I(inode)->disk_i_size);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005170 }
Yan, Zheng80825102009-11-12 09:35:36 +00005171 }
5172
Josef Bacika41ad392011-01-31 15:30:16 -05005173 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00005174}
5175
Chris Mason39279cc2007-06-12 06:35:45 -04005176static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
5177{
David Howells2b0143b2015-03-17 22:25:59 +00005178 struct inode *inode = d_inode(dentry);
Li Zefanb83cc962010-12-20 16:04:08 +08005179 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005180 int err;
5181
Li Zefanb83cc962010-12-20 16:04:08 +08005182 if (btrfs_root_readonly(root))
5183 return -EROFS;
5184
Jan Kara31051c82016-05-26 16:55:18 +02005185 err = setattr_prepare(dentry, attr);
Chris Mason39279cc2007-06-12 06:35:45 -04005186 if (err)
5187 return err;
5188
Chris Mason5a3f23d2009-03-31 13:27:11 -04005189 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Eric Sandeen3972f262013-01-12 02:57:22 +00005190 err = btrfs_setsize(inode, attr);
Yan, Zheng80825102009-11-12 09:35:36 +00005191 if (err)
5192 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005193 }
Yan Zheng9036c102008-10-30 14:19:41 -04005194
Christoph Hellwig10257742010-06-04 11:30:02 +02005195 if (attr->ia_valid) {
5196 setattr_copy(inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005197 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005198 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04005199
Josef Bacik22c44fe2011-11-30 10:45:38 -05005200 if (!err && attr->ia_valid & ATTR_MODE)
Christoph Hellwig996a7102013-12-20 05:16:43 -08005201 err = posix_acl_chmod(inode, inode->i_mode);
Christoph Hellwig10257742010-06-04 11:30:02 +02005202 }
5203
Chris Mason39279cc2007-06-12 06:35:45 -04005204 return err;
5205}
Chris Mason61295eb2008-01-14 16:24:38 -05005206
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005207/*
5208 * While truncating the inode pages during eviction, we get the VFS calling
5209 * btrfs_invalidatepage() against each page of the inode. This is slow because
5210 * the calls to btrfs_invalidatepage() result in a huge amount of calls to
5211 * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting
5212 * extent_state structures over and over, wasting lots of time.
5213 *
5214 * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all
5215 * those expensive operations on a per page basis and do only the ordered io
5216 * finishing, while we release here the extent_map and extent_state structures,
5217 * without the excessive merging and splitting.
5218 */
5219static void evict_inode_truncate_pages(struct inode *inode)
5220{
5221 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
5222 struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree;
5223 struct rb_node *node;
5224
5225 ASSERT(inode->i_state & I_FREEING);
Johannes Weiner91b0abe2014-04-03 14:47:49 -07005226 truncate_inode_pages_final(&inode->i_data);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005227
5228 write_lock(&map_tree->lock);
Liu Bo07e1ce02018-08-23 03:51:52 +08005229 while (!RB_EMPTY_ROOT(&map_tree->map.rb_root)) {
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005230 struct extent_map *em;
5231
Liu Bo07e1ce02018-08-23 03:51:52 +08005232 node = rb_first_cached(&map_tree->map);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005233 em = rb_entry(node, struct extent_map, rb_node);
Wang Shilong180589e2013-12-14 15:27:31 +08005234 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
5235 clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005236 remove_extent_mapping(map_tree, em);
5237 free_extent_map(em);
Filipe Manana7064dd52014-08-08 02:47:05 +01005238 if (need_resched()) {
5239 write_unlock(&map_tree->lock);
5240 cond_resched();
5241 write_lock(&map_tree->lock);
5242 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005243 }
5244 write_unlock(&map_tree->lock);
5245
Filipe Manana6ca07092015-05-26 00:55:42 +01005246 /*
5247 * Keep looping until we have no more ranges in the io tree.
5248 * We can have ongoing bios started by readpages (called from readahead)
Filipe Manana9c6429d2015-06-10 12:55:41 +01005249 * that have their endio callback (extent_io.c:end_bio_extent_readpage)
5250 * still in progress (unlocked the pages in the bio but did not yet
5251 * unlocked the ranges in the io tree). Therefore this means some
Filipe Manana6ca07092015-05-26 00:55:42 +01005252 * ranges can still be locked and eviction started because before
5253 * submitting those bios, which are executed by a separate task (work
5254 * queue kthread), inode references (inode->i_count) were not taken
5255 * (which would be dropped in the end io callback of each bio).
5256 * Therefore here we effectively end up waiting for those bios and
5257 * anyone else holding locked ranges without having bumped the inode's
5258 * reference count - if we don't do it, when they access the inode's
5259 * io_tree to unlock a range it may be too late, leading to an
5260 * use-after-free issue.
5261 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005262 spin_lock(&io_tree->lock);
5263 while (!RB_EMPTY_ROOT(&io_tree->state)) {
5264 struct extent_state *state;
5265 struct extent_state *cached_state = NULL;
Filipe Manana6ca07092015-05-26 00:55:42 +01005266 u64 start;
5267 u64 end;
Filipe Manana421f0922018-10-12 13:02:48 +01005268 unsigned state_flags;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005269
5270 node = rb_first(&io_tree->state);
5271 state = rb_entry(node, struct extent_state, rb_node);
Filipe Manana6ca07092015-05-26 00:55:42 +01005272 start = state->start;
5273 end = state->end;
Filipe Manana421f0922018-10-12 13:02:48 +01005274 state_flags = state->state;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005275 spin_unlock(&io_tree->lock);
5276
David Sterbaff13db42015-12-03 14:30:40 +01005277 lock_extent_bits(io_tree, start, end, &cached_state);
Qu Wenruob9d0b382015-09-29 10:35:16 +08005278
5279 /*
5280 * If still has DELALLOC flag, the extent didn't reach disk,
5281 * and its reserved space won't be freed by delayed_ref.
5282 * So we need to free its reserved space here.
5283 * (Refer to comment in btrfs_invalidatepage, case 2)
5284 *
5285 * Note, end is the bytenr of last byte, so we need + 1 here.
5286 */
Filipe Manana421f0922018-10-12 13:02:48 +01005287 if (state_flags & EXTENT_DELALLOC)
Qu Wenruobc42bda2017-02-27 15:10:39 +08005288 btrfs_qgroup_free_data(inode, NULL, start, end - start + 1);
Qu Wenruob9d0b382015-09-29 10:35:16 +08005289
Filipe Manana6ca07092015-05-26 00:55:42 +01005290 clear_extent_bit(io_tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005291 EXTENT_LOCKED | EXTENT_DIRTY |
5292 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
David Sterbaae0f1622017-10-31 16:37:52 +01005293 EXTENT_DEFRAG, 1, 1, &cached_state);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005294
Filipe Manana7064dd52014-08-08 02:47:05 +01005295 cond_resched();
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005296 spin_lock(&io_tree->lock);
5297 }
5298 spin_unlock(&io_tree->lock);
5299}
5300
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005301static struct btrfs_trans_handle *evict_refill_and_join(struct btrfs_root *root,
Josef Bacikad80cf52018-09-28 07:18:19 -04005302 struct btrfs_block_rsv *rsv)
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005303{
5304 struct btrfs_fs_info *fs_info = root->fs_info;
5305 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
5306 int failures = 0;
5307
5308 for (;;) {
5309 struct btrfs_trans_handle *trans;
5310 int ret;
5311
Josef Bacikad80cf52018-09-28 07:18:19 -04005312 ret = btrfs_block_rsv_refill(root, rsv, rsv->size,
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005313 BTRFS_RESERVE_FLUSH_LIMIT);
5314
5315 if (ret && ++failures > 2) {
5316 btrfs_warn(fs_info,
5317 "could not allocate space for a delete; will truncate on mount");
5318 return ERR_PTR(-ENOSPC);
5319 }
5320
5321 trans = btrfs_join_transaction(root);
5322 if (IS_ERR(trans) || !ret)
5323 return trans;
5324
5325 /*
5326 * Try to steal from the global reserve if there is space for
5327 * it.
5328 */
Lu Fengqiaf9b8a02018-10-11 13:40:35 +08005329 if (!btrfs_check_space_for_delayed_refs(trans) &&
Josef Bacikad80cf52018-09-28 07:18:19 -04005330 !btrfs_block_rsv_migrate(global_rsv, rsv, rsv->size, false))
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005331 return trans;
5332
5333 /* If not, commit and try again. */
5334 ret = btrfs_commit_transaction(trans);
5335 if (ret)
5336 return ERR_PTR(ret);
5337 }
5338}
5339
Al Virobd555972010-06-07 11:35:40 -04005340void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005341{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005342 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04005343 struct btrfs_trans_handle *trans;
5344 struct btrfs_root *root = BTRFS_I(inode)->root;
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005345 struct btrfs_block_rsv *rsv;
Chris Mason39279cc2007-06-12 06:35:45 -04005346 int ret;
5347
liubo1abe9b82011-03-24 11:18:59 +00005348 trace_btrfs_inode_evict(inode);
5349
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005350 if (!root) {
Liu Boe8f1bc12018-01-25 11:02:53 -07005351 clear_inode(inode);
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005352 return;
5353 }
5354
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005355 evict_inode_truncate_pages(inode);
5356
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005357 if (inode->i_nlink &&
5358 ((btrfs_root_refs(&root->root_item) != 0 &&
5359 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) ||
Nikolay Borisov70ddc552017-02-20 13:50:35 +02005360 btrfs_is_free_space_inode(BTRFS_I(inode))))
Al Virobd555972010-06-07 11:35:40 -04005361 goto no_delete;
5362
Omar Sandoval27919062018-05-11 13:13:37 -07005363 if (is_bad_inode(inode))
Chris Mason39279cc2007-06-12 06:35:45 -04005364 goto no_delete;
Chris Mason5f39d392007-10-15 16:14:19 -04005365
Nikolay Borisov7ab79562017-02-20 13:50:57 +02005366 btrfs_free_io_failure_record(BTRFS_I(inode), 0, (u64)-1);
Miao Xief6124962014-09-12 18:44:04 +08005367
Omar Sandoval7b40b692018-05-11 13:13:33 -07005368 if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags))
Yan, Zhengc71bf092009-11-12 09:34:40 +00005369 goto no_delete;
Yan, Zhengc71bf092009-11-12 09:34:40 +00005370
Yan, Zheng76dda932009-09-21 16:00:26 -04005371 if (inode->i_nlink > 0) {
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005372 BUG_ON(btrfs_root_refs(&root->root_item) != 0 &&
5373 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID);
Yan, Zheng76dda932009-09-21 16:00:26 -04005374 goto no_delete;
5375 }
5376
Nikolay Borisovaa790212017-01-10 20:35:40 +02005377 ret = btrfs_commit_inode_delayed_inode(BTRFS_I(inode));
Omar Sandoval27919062018-05-11 13:13:37 -07005378 if (ret)
Miao Xie0e8c36a2012-12-19 06:59:51 +00005379 goto no_delete;
Miao Xie0e8c36a2012-12-19 06:59:51 +00005380
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005381 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Omar Sandoval27919062018-05-11 13:13:37 -07005382 if (!rsv)
Josef Bacik4289a662011-08-05 13:22:24 -04005383 goto no_delete;
Josef Bacikad80cf52018-09-28 07:18:19 -04005384 rsv->size = btrfs_calc_trunc_metadata_size(fs_info, 1);
Josef Bacikca7e70f2012-08-27 17:48:15 -04005385 rsv->failfast = 1;
Josef Bacik4289a662011-08-05 13:22:24 -04005386
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02005387 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Mason5f39d392007-10-15 16:14:19 -04005388
Yan, Zheng80825102009-11-12 09:35:36 +00005389 while (1) {
Josef Bacikad80cf52018-09-28 07:18:19 -04005390 trans = evict_refill_and_join(root, rsv);
Omar Sandoval27919062018-05-11 13:13:37 -07005391 if (IS_ERR(trans))
5392 goto free_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04005393
5394 trans->block_rsv = rsv;
5395
Yan, Zhengd68fc572010-05-16 10:49:58 -04005396 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Omar Sandoval27919062018-05-11 13:13:37 -07005397 trans->block_rsv = &fs_info->trans_block_rsv;
5398 btrfs_end_transaction(trans);
5399 btrfs_btree_balance_dirty(fs_info);
5400 if (ret && ret != -ENOSPC && ret != -EAGAIN)
5401 goto free_rsv;
5402 else if (!ret)
Yan, Zheng80825102009-11-12 09:35:36 +00005403 break;
Josef Bacik7b128762008-07-24 12:17:14 -04005404 }
5405
Josef Bacik4ef31a42013-08-13 14:10:08 -04005406 /*
Omar Sandoval27919062018-05-11 13:13:37 -07005407 * Errors here aren't a big deal, it just means we leave orphan items in
5408 * the tree. They will be cleaned up on the next mount. If the inode
5409 * number gets reused, cleanup deletes the orphan item without doing
5410 * anything, and unlink reuses the existing orphan item.
5411 *
5412 * If it turns out that we are dropping too many of these, we might want
5413 * to add a mechanism for retrying these after a commit.
Josef Bacik4ef31a42013-08-13 14:10:08 -04005414 */
Josef Bacikad80cf52018-09-28 07:18:19 -04005415 trans = evict_refill_and_join(root, rsv);
Omar Sandoval27919062018-05-11 13:13:37 -07005416 if (!IS_ERR(trans)) {
5417 trans->block_rsv = rsv;
5418 btrfs_orphan_del(trans, BTRFS_I(inode));
5419 trans->block_rsv = &fs_info->trans_block_rsv;
5420 btrfs_end_transaction(trans);
5421 }
Chris Mason85e21ba2008-01-29 15:11:36 -05005422
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005423 if (!(root == fs_info->tree_root ||
Li Zefan581bb052011-04-20 10:06:11 +08005424 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005425 btrfs_return_ino(root, btrfs_ino(BTRFS_I(inode)));
Li Zefan581bb052011-04-20 10:06:11 +08005426
Omar Sandoval27919062018-05-11 13:13:37 -07005427free_rsv:
5428 btrfs_free_block_rsv(fs_info, rsv);
Chris Mason39279cc2007-06-12 06:35:45 -04005429no_delete:
Omar Sandoval27919062018-05-11 13:13:37 -07005430 /*
5431 * If we didn't successfully delete, the orphan item will still be in
5432 * the tree and we'll retry on the next mount. Again, we might also want
5433 * to retry these periodically in the future.
5434 */
Nikolay Borisovf48d1cf2017-01-10 20:35:39 +02005435 btrfs_remove_delayed_node(BTRFS_I(inode));
Jan Karadbd57682012-05-03 14:48:02 +02005436 clear_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005437}
5438
5439/*
5440 * this returns the key found in the dir entry in the location pointer.
Su Yue005d6712018-03-05 17:13:37 +08005441 * If no dir entries were found, returns -ENOENT.
5442 * If found a corrupted location in dir entry, returns -EUCLEAN.
Chris Mason39279cc2007-06-12 06:35:45 -04005443 */
5444static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
5445 struct btrfs_key *location)
5446{
5447 const char *name = dentry->d_name.name;
5448 int namelen = dentry->d_name.len;
5449 struct btrfs_dir_item *di;
5450 struct btrfs_path *path;
5451 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04005452 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005453
5454 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005455 if (!path)
5456 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05005457
David Sterbaf85b7372017-01-20 14:54:07 +01005458 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(BTRFS_I(dir)),
5459 name, namelen, 0);
Liu Bo3cf50682018-09-12 06:06:26 +08005460 if (IS_ERR_OR_NULL(di)) {
5461 ret = di ? PTR_ERR(di) : -ENOENT;
Su Yue005d6712018-03-05 17:13:37 +08005462 goto out;
5463 }
Chris Masond3977122009-01-05 21:25:51 -05005464
Chris Mason5f39d392007-10-15 16:14:19 -04005465 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Liu Bo56a0e702017-10-30 11:14:38 -06005466 if (location->type != BTRFS_INODE_ITEM_KEY &&
5467 location->type != BTRFS_ROOT_ITEM_KEY) {
Su Yue005d6712018-03-05 17:13:37 +08005468 ret = -EUCLEAN;
Liu Bo56a0e702017-10-30 11:14:38 -06005469 btrfs_warn(root->fs_info,
5470"%s gets something invalid in DIR_ITEM (name %s, directory ino %llu, location(%llu %u %llu))",
5471 __func__, name, btrfs_ino(BTRFS_I(dir)),
5472 location->objectid, location->type, location->offset);
Liu Bo56a0e702017-10-30 11:14:38 -06005473 }
Chris Mason39279cc2007-06-12 06:35:45 -04005474out:
Chris Mason39279cc2007-06-12 06:35:45 -04005475 btrfs_free_path(path);
5476 return ret;
5477}
5478
5479/*
5480 * when we hit a tree root in a directory, the btrfs part of the inode
5481 * needs to be changed to reflect the root directory of the tree root. This
5482 * is kind of like crossing a mount point.
5483 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005484static int fixup_tree_root_location(struct btrfs_fs_info *fs_info,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005485 struct inode *dir,
5486 struct dentry *dentry,
5487 struct btrfs_key *location,
5488 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04005489{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005490 struct btrfs_path *path;
5491 struct btrfs_root *new_root;
5492 struct btrfs_root_ref *ref;
5493 struct extent_buffer *leaf;
David Sterba1d4c08e2015-01-02 19:36:14 +01005494 struct btrfs_key key;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005495 int ret;
5496 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005497
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005498 path = btrfs_alloc_path();
5499 if (!path) {
5500 err = -ENOMEM;
5501 goto out;
5502 }
Chris Mason39279cc2007-06-12 06:35:45 -04005503
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005504 err = -ENOENT;
David Sterba1d4c08e2015-01-02 19:36:14 +01005505 key.objectid = BTRFS_I(dir)->root->root_key.objectid;
5506 key.type = BTRFS_ROOT_REF_KEY;
5507 key.offset = location->objectid;
5508
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005509 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005510 if (ret) {
5511 if (ret < 0)
5512 err = ret;
5513 goto out;
5514 }
Chris Mason39279cc2007-06-12 06:35:45 -04005515
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005516 leaf = path->nodes[0];
5517 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005518 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(BTRFS_I(dir)) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005519 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
5520 goto out;
5521
5522 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
5523 (unsigned long)(ref + 1),
5524 dentry->d_name.len);
5525 if (ret)
5526 goto out;
5527
David Sterbab3b4aa72011-04-21 01:20:15 +02005528 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005529
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005530 new_root = btrfs_read_fs_root_no_name(fs_info, location);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005531 if (IS_ERR(new_root)) {
5532 err = PTR_ERR(new_root);
5533 goto out;
5534 }
5535
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005536 *sub_root = new_root;
5537 location->objectid = btrfs_root_dirid(&new_root->root_item);
5538 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04005539 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005540 err = 0;
5541out:
5542 btrfs_free_path(path);
5543 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005544}
5545
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005546static void inode_tree_add(struct inode *inode)
5547{
5548 struct btrfs_root *root = BTRFS_I(inode)->root;
5549 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005550 struct rb_node **p;
5551 struct rb_node *parent;
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005552 struct rb_node *new = &BTRFS_I(inode)->rb_node;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005553 u64 ino = btrfs_ino(BTRFS_I(inode));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005554
Al Viro1d3382cb2010-10-23 15:19:20 -04005555 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005556 return;
Miao Xiee1409ce2013-05-15 07:48:16 +00005557 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005558 spin_lock(&root->inode_lock);
Miao Xiee1409ce2013-05-15 07:48:16 +00005559 p = &root->inode_tree.rb_node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005560 while (*p) {
5561 parent = *p;
5562 entry = rb_entry(parent, struct btrfs_inode, rb_node);
5563
David Sterba37508512018-06-29 10:56:40 +02005564 if (ino < btrfs_ino(entry))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005565 p = &parent->rb_left;
David Sterba37508512018-06-29 10:56:40 +02005566 else if (ino > btrfs_ino(entry))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005567 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005568 else {
5569 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04005570 (I_WILL_FREE | I_FREEING)));
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005571 rb_replace_node(parent, new, &root->inode_tree);
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005572 RB_CLEAR_NODE(parent);
5573 spin_unlock(&root->inode_lock);
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005574 return;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005575 }
5576 }
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005577 rb_link_node(new, parent, p);
5578 rb_insert_color(new, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005579 spin_unlock(&root->inode_lock);
5580}
5581
5582static void inode_tree_del(struct inode *inode)
5583{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005584 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005585 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04005586 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005587
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005588 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005589 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005590 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005591 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04005592 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005593 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005594 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04005595
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005596 if (empty && btrfs_root_refs(&root->root_item) == 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005597 synchronize_srcu(&fs_info->subvol_srcu);
Yan, Zheng76dda932009-09-21 16:00:26 -04005598 spin_lock(&root->inode_lock);
5599 empty = RB_EMPTY_ROOT(&root->inode_tree);
5600 spin_unlock(&root->inode_lock);
5601 if (empty)
5602 btrfs_add_dead_root(root);
5603 }
5604}
5605
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005606
Chris Masone02119d2008-09-05 16:13:11 -04005607static int btrfs_init_locked_inode(struct inode *inode, void *p)
5608{
5609 struct btrfs_iget_args *args = p;
Chris Mason90d3e592014-01-09 17:28:00 -08005610 inode->i_ino = args->location->objectid;
5611 memcpy(&BTRFS_I(inode)->location, args->location,
5612 sizeof(*args->location));
Chris Masone02119d2008-09-05 16:13:11 -04005613 BTRFS_I(inode)->root = args->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005614 return 0;
5615}
5616
5617static int btrfs_find_actor(struct inode *inode, void *opaque)
5618{
5619 struct btrfs_iget_args *args = opaque;
Chris Mason90d3e592014-01-09 17:28:00 -08005620 return args->location->objectid == BTRFS_I(inode)->location.objectid &&
Chris Masond3977122009-01-05 21:25:51 -05005621 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005622}
5623
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005624static struct inode *btrfs_iget_locked(struct super_block *s,
Chris Mason90d3e592014-01-09 17:28:00 -08005625 struct btrfs_key *location,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005626 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04005627{
5628 struct inode *inode;
5629 struct btrfs_iget_args args;
Chris Mason90d3e592014-01-09 17:28:00 -08005630 unsigned long hashval = btrfs_inode_hash(location->objectid, root);
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005631
Chris Mason90d3e592014-01-09 17:28:00 -08005632 args.location = location;
Chris Mason39279cc2007-06-12 06:35:45 -04005633 args.root = root;
5634
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005635 inode = iget5_locked(s, hashval, btrfs_find_actor,
Chris Mason39279cc2007-06-12 06:35:45 -04005636 btrfs_init_locked_inode,
5637 (void *)&args);
5638 return inode;
5639}
5640
Balaji Rao1a54ef82008-07-21 02:01:04 +05305641/* Get an inode object given its location and corresponding root.
5642 * Returns in *is_new if the inode was read from disk
5643 */
Filipe Manana4222ea72018-10-24 10:13:03 +01005644struct inode *btrfs_iget_path(struct super_block *s, struct btrfs_key *location,
5645 struct btrfs_root *root, int *new,
5646 struct btrfs_path *path)
Balaji Rao1a54ef82008-07-21 02:01:04 +05305647{
5648 struct inode *inode;
5649
Chris Mason90d3e592014-01-09 17:28:00 -08005650 inode = btrfs_iget_locked(s, location, root);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305651 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005652 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305653
5654 if (inode->i_state & I_NEW) {
Filipe Manana67710892016-06-06 11:51:25 +01005655 int ret;
5656
Filipe Manana4222ea72018-10-24 10:13:03 +01005657 ret = btrfs_read_locked_inode(inode, path);
Al Viro9bc2cef2018-07-29 23:04:50 +01005658 if (!ret) {
Mark Fasheh1748f842011-07-12 11:25:31 -07005659 inode_tree_add(inode);
5660 unlock_new_inode(inode);
5661 if (new)
5662 *new = 1;
5663 } else {
Al Virof5b3a412018-07-29 23:04:51 +01005664 iget_failed(inode);
5665 /*
5666 * ret > 0 can come from btrfs_search_slot called by
5667 * btrfs_read_locked_inode, this means the inode item
5668 * was not found.
5669 */
5670 if (ret > 0)
5671 ret = -ENOENT;
5672 inode = ERR_PTR(ret);
Mark Fasheh1748f842011-07-12 11:25:31 -07005673 }
5674 }
5675
Balaji Rao1a54ef82008-07-21 02:01:04 +05305676 return inode;
5677}
5678
Filipe Manana4222ea72018-10-24 10:13:03 +01005679struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
5680 struct btrfs_root *root, int *new)
5681{
5682 return btrfs_iget_path(s, location, root, new, NULL);
5683}
5684
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005685static struct inode *new_simple_dir(struct super_block *s,
5686 struct btrfs_key *key,
5687 struct btrfs_root *root)
5688{
5689 struct inode *inode = new_inode(s);
5690
5691 if (!inode)
5692 return ERR_PTR(-ENOMEM);
5693
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005694 BTRFS_I(inode)->root = root;
5695 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04005696 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005697
5698 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Li Zefan848cce02012-02-21 17:04:28 +08005699 inode->i_op = &btrfs_dir_ro_inode_operations;
Omar Sandoval1fdf4192017-01-25 17:06:39 -08005700 inode->i_opflags &= ~IOP_XATTR;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005701 inode->i_fop = &simple_dir_operations;
5702 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
Deepa Dinamanic2050a42016-09-14 07:48:06 -07005703 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05305704 inode->i_atime = inode->i_mtime;
5705 inode->i_ctime = inode->i_mtime;
Arnd Bergmannd3c6be6f2018-06-21 18:04:06 +02005706 BTRFS_I(inode)->i_otime = inode->i_mtime;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005707
5708 return inode;
5709}
5710
Chris Mason3de45862008-11-17 21:02:50 -05005711struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04005712{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005713 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masond3977122009-01-05 21:25:51 -05005714 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005715 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005716 struct btrfs_root *sub_root = root;
5717 struct btrfs_key location;
Yan, Zheng76dda932009-09-21 16:00:26 -04005718 int index;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005719 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005720
5721 if (dentry->d_name.len > BTRFS_NAME_LEN)
5722 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04005723
Jeff Layton39e3c952012-11-28 11:30:53 -05005724 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason39279cc2007-06-12 06:35:45 -04005725 if (ret < 0)
5726 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04005727
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005728 if (location.type == BTRFS_INODE_ITEM_KEY) {
Josef Bacik73f73412009-12-04 17:38:27 +00005729 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005730 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04005731 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005732
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005733 index = srcu_read_lock(&fs_info->subvol_srcu);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005734 ret = fixup_tree_root_location(fs_info, dir, dentry,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005735 &location, &sub_root);
5736 if (ret < 0) {
5737 if (ret != -ENOENT)
5738 inode = ERR_PTR(ret);
5739 else
5740 inode = new_simple_dir(dir->i_sb, &location, sub_root);
5741 } else {
Josef Bacik73f73412009-12-04 17:38:27 +00005742 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04005743 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005744 srcu_read_unlock(&fs_info->subvol_srcu, index);
Yan, Zheng76dda932009-09-21 16:00:26 -04005745
Julia Lawall34d19ba2011-01-24 19:55:19 +00005746 if (!IS_ERR(inode) && root != sub_root) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005747 down_read(&fs_info->cleanup_work_sem);
David Howellsbc98a422017-07-17 08:45:34 +01005748 if (!sb_rdonly(inode->i_sb))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005749 ret = btrfs_orphan_cleanup(sub_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005750 up_read(&fs_info->cleanup_work_sem);
Josef Bacik01cd3362013-06-03 21:39:49 -04005751 if (ret) {
5752 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005753 inode = ERR_PTR(ret);
Josef Bacik01cd3362013-06-03 21:39:49 -04005754 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00005755 }
5756
Chris Mason3de45862008-11-17 21:02:50 -05005757 return inode;
5758}
5759
Nick Pigginfe15ce42011-01-07 17:49:23 +11005760static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04005761{
5762 struct btrfs_root *root;
David Howells2b0143b2015-03-17 22:25:59 +00005763 struct inode *inode = d_inode(dentry);
Yan, Zheng76dda932009-09-21 16:00:26 -04005764
Li Zefan848cce02012-02-21 17:04:28 +08005765 if (!inode && !IS_ROOT(dentry))
David Howells2b0143b2015-03-17 22:25:59 +00005766 inode = d_inode(dentry->d_parent);
Yan, Zheng76dda932009-09-21 16:00:26 -04005767
Li Zefan848cce02012-02-21 17:04:28 +08005768 if (inode) {
5769 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04005770 if (btrfs_root_refs(&root->root_item) == 0)
5771 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08005772
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005773 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Li Zefan848cce02012-02-21 17:04:28 +08005774 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04005775 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005776 return 0;
5777}
5778
Chris Mason3de45862008-11-17 21:02:50 -05005779static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04005780 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05005781{
Al Viro3837d202018-10-10 16:38:27 -04005782 struct inode *inode = btrfs_lookup_dentry(dir, dentry);
Josef Bacika66e7cc2011-09-18 10:34:03 -04005783
Al Viro3837d202018-10-10 16:38:27 -04005784 if (inode == ERR_PTR(-ENOENT))
5785 inode = NULL;
Al Viro41d28bc2014-10-12 22:24:21 -04005786 return d_splice_alias(inode, dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04005787}
5788
Miao Xie16cdcec2011-04-22 18:12:22 +08005789unsigned char btrfs_filetype_table[] = {
Chris Mason39279cc2007-06-12 06:35:45 -04005790 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
5791};
5792
Josef Bacik23b5ec72017-07-24 15:14:25 -04005793/*
5794 * All this infrastructure exists because dir_emit can fault, and we are holding
5795 * the tree lock when doing readdir. For now just allocate a buffer and copy
5796 * our information into that, and then dir_emit from the buffer. This is
5797 * similar to what NFS does, only we don't keep the buffer around in pagecache
5798 * because I'm afraid I'll mess that up. Long term we need to make filldir do
5799 * copy_to_user_inatomic so we don't have to worry about page faulting under the
5800 * tree lock.
5801 */
5802static int btrfs_opendir(struct inode *inode, struct file *file)
5803{
5804 struct btrfs_file_private *private;
5805
5806 private = kzalloc(sizeof(struct btrfs_file_private), GFP_KERNEL);
5807 if (!private)
5808 return -ENOMEM;
5809 private->filldir_buf = kzalloc(PAGE_SIZE, GFP_KERNEL);
5810 if (!private->filldir_buf) {
5811 kfree(private);
5812 return -ENOMEM;
5813 }
5814 file->private_data = private;
5815 return 0;
5816}
5817
5818struct dir_entry {
5819 u64 ino;
5820 u64 offset;
5821 unsigned type;
5822 int name_len;
5823};
5824
5825static int btrfs_filldir(void *addr, int entries, struct dir_context *ctx)
5826{
5827 while (entries--) {
5828 struct dir_entry *entry = addr;
5829 char *name = (char *)(entry + 1);
5830
David Sterba92d32172018-04-16 21:10:14 +02005831 ctx->pos = get_unaligned(&entry->offset);
5832 if (!dir_emit(ctx, name, get_unaligned(&entry->name_len),
5833 get_unaligned(&entry->ino),
5834 get_unaligned(&entry->type)))
Josef Bacik23b5ec72017-07-24 15:14:25 -04005835 return 1;
David Sterba92d32172018-04-16 21:10:14 +02005836 addr += sizeof(struct dir_entry) +
5837 get_unaligned(&entry->name_len);
Josef Bacik23b5ec72017-07-24 15:14:25 -04005838 ctx->pos++;
5839 }
5840 return 0;
5841}
5842
Al Viro9cdda8d2013-05-22 16:48:09 -04005843static int btrfs_real_readdir(struct file *file, struct dir_context *ctx)
Chris Mason39279cc2007-06-12 06:35:45 -04005844{
Al Viro9cdda8d2013-05-22 16:48:09 -04005845 struct inode *inode = file_inode(file);
Chris Mason39279cc2007-06-12 06:35:45 -04005846 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005847 struct btrfs_file_private *private = file->private_data;
Chris Mason39279cc2007-06-12 06:35:45 -04005848 struct btrfs_dir_item *di;
5849 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04005850 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04005851 struct btrfs_path *path;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005852 void *addr;
Miao Xie16cdcec2011-04-22 18:12:22 +08005853 struct list_head ins_list;
5854 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04005855 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04005856 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04005857 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04005858 char *name_ptr;
5859 int name_len;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005860 int entries = 0;
5861 int total_len = 0;
Omar Sandoval02dbfc92016-05-20 13:50:33 -07005862 bool put = false;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005863 struct btrfs_key location;
Chris Mason5f39d392007-10-15 16:14:19 -04005864
Al Viro9cdda8d2013-05-22 16:48:09 -04005865 if (!dir_emit_dots(file, ctx))
5866 return 0;
5867
David Woodhouse49593bf2008-08-17 17:08:36 +01005868 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08005869 if (!path)
5870 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04005871
Josef Bacik23b5ec72017-07-24 15:14:25 -04005872 addr = private->filldir_buf;
David Sterbae4058b52015-11-27 16:31:35 +01005873 path->reada = READA_FORWARD;
David Woodhouse49593bf2008-08-17 17:08:36 +01005874
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005875 INIT_LIST_HEAD(&ins_list);
5876 INIT_LIST_HEAD(&del_list);
5877 put = btrfs_readdir_get_delayed_items(inode, &ins_list, &del_list);
Miao Xie16cdcec2011-04-22 18:12:22 +08005878
Josef Bacik23b5ec72017-07-24 15:14:25 -04005879again:
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005880 key.type = BTRFS_DIR_INDEX_KEY;
Al Viro9cdda8d2013-05-22 16:48:09 -04005881 key.offset = ctx->pos;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005882 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason5f39d392007-10-15 16:14:19 -04005883
Chris Mason39279cc2007-06-12 06:35:45 -04005884 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5885 if (ret < 0)
5886 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01005887
5888 while (1) {
Josef Bacik23b5ec72017-07-24 15:14:25 -04005889 struct dir_entry *entry;
5890
Chris Mason5f39d392007-10-15 16:14:19 -04005891 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04005892 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08005893 if (slot >= btrfs_header_nritems(leaf)) {
5894 ret = btrfs_next_leaf(root, path);
5895 if (ret < 0)
5896 goto err;
5897 else if (ret > 0)
5898 break;
5899 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04005900 }
Chris Mason3de45862008-11-17 21:02:50 -05005901
Chris Mason5f39d392007-10-15 16:14:19 -04005902 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5903
5904 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04005905 break;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005906 if (found_key.type != BTRFS_DIR_INDEX_KEY)
Chris Mason39279cc2007-06-12 06:35:45 -04005907 break;
Al Viro9cdda8d2013-05-22 16:48:09 -04005908 if (found_key.offset < ctx->pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08005909 goto next;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005910 if (btrfs_should_delete_dir_index(&del_list, found_key.offset))
Miao Xie16cdcec2011-04-22 18:12:22 +08005911 goto next;
Chris Mason39279cc2007-06-12 06:35:45 -04005912 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005913 name_len = btrfs_dir_name_len(leaf, di);
Josef Bacik23b5ec72017-07-24 15:14:25 -04005914 if ((total_len + sizeof(struct dir_entry) + name_len) >=
5915 PAGE_SIZE) {
5916 btrfs_release_path(path);
5917 ret = btrfs_filldir(private->filldir_buf, entries, ctx);
5918 if (ret)
5919 goto nopos;
5920 addr = private->filldir_buf;
5921 entries = 0;
5922 total_len = 0;
5923 goto again;
Chris Mason39279cc2007-06-12 06:35:45 -04005924 }
Josef Bacik23b5ec72017-07-24 15:14:25 -04005925
5926 entry = addr;
David Sterba92d32172018-04-16 21:10:14 +02005927 put_unaligned(name_len, &entry->name_len);
Josef Bacik23b5ec72017-07-24 15:14:25 -04005928 name_ptr = (char *)(entry + 1);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005929 read_extent_buffer(leaf, name_ptr, (unsigned long)(di + 1),
5930 name_len);
David Sterba92d32172018-04-16 21:10:14 +02005931 put_unaligned(btrfs_filetype_table[btrfs_dir_type(leaf, di)],
5932 &entry->type);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005933 btrfs_dir_item_key_to_cpu(leaf, di, &location);
David Sterba92d32172018-04-16 21:10:14 +02005934 put_unaligned(location.objectid, &entry->ino);
5935 put_unaligned(found_key.offset, &entry->offset);
Josef Bacik23b5ec72017-07-24 15:14:25 -04005936 entries++;
5937 addr += sizeof(struct dir_entry) + name_len;
5938 total_len += sizeof(struct dir_entry) + name_len;
Li Zefanb9e03af2011-03-23 10:43:58 +08005939next:
5940 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04005941 }
Josef Bacik23b5ec72017-07-24 15:14:25 -04005942 btrfs_release_path(path);
5943
5944 ret = btrfs_filldir(private->filldir_buf, entries, ctx);
5945 if (ret)
5946 goto nopos;
David Woodhouse49593bf2008-08-17 17:08:36 +01005947
Jeff Mahoneyd2fbb2b2016-11-05 13:26:35 -04005948 ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005949 if (ret)
David Sterbabc4ef752015-11-13 13:44:28 +01005950 goto nopos;
5951
Zach Browndb62efb2013-07-11 16:19:42 -07005952 /*
5953 * Stop new entries from being returned after we return the last
5954 * entry.
5955 *
5956 * New directory entries are assigned a strictly increasing
5957 * offset. This means that new entries created during readdir
5958 * are *guaranteed* to be seen in the future by that readdir.
5959 * This has broken buggy programs which operate on names as
5960 * they're returned by readdir. Until we re-use freed offsets
5961 * we have this hack to stop new entries from being returned
5962 * under the assumption that they'll never reach this huge
5963 * offset.
5964 *
5965 * This is being careful not to overflow 32bit loff_t unless the
5966 * last entry requires it because doing so has broken 32bit apps
5967 * in the past.
5968 */
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005969 if (ctx->pos >= INT_MAX)
5970 ctx->pos = LLONG_MAX;
5971 else
5972 ctx->pos = INT_MAX;
Chris Mason39279cc2007-06-12 06:35:45 -04005973nopos:
5974 ret = 0;
5975err:
Omar Sandoval02dbfc92016-05-20 13:50:33 -07005976 if (put)
5977 btrfs_readdir_put_delayed_items(inode, &ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04005978 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005979 return ret;
5980}
5981
Chris Mason39279cc2007-06-12 06:35:45 -04005982/*
Chris Mason54aa1f42007-06-22 14:16:25 -04005983 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04005984 * inode changes. But, it is most likely to find the inode in cache.
5985 * FIXME, needs more benchmarking...there are no reasons other than performance
5986 * to keep or drop this code.
5987 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00005988static int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005989{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005990 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04005991 struct btrfs_root *root = BTRFS_I(inode)->root;
5992 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005993 int ret;
5994
Josef Bacik72ac3c02012-05-23 14:13:11 -04005995 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05005996 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005997
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005998 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005999 if (IS_ERR(trans))
6000 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006001
6002 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04006003 if (ret && ret == -ENOSPC) {
6004 /* whoops, lets try again with the full transaction */
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006005 btrfs_end_transaction(trans);
Chris Mason94b60442010-05-26 11:02:00 -04006006 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006007 if (IS_ERR(trans))
6008 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006009
Chris Mason94b60442010-05-26 11:02:00 -04006010 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04006011 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006012 btrfs_end_transaction(trans);
Miao Xie16cdcec2011-04-22 18:12:22 +08006013 if (BTRFS_I(inode)->delayed_node)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006014 btrfs_balance_delayed_items(fs_info);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006015
6016 return ret;
6017}
6018
6019/*
6020 * This is a copy of file_update_time. We need this so we can return error on
6021 * ENOSPC for updating the inode in the case of file write and mmap writes.
6022 */
Deepa Dinamani95582b02018-05-08 19:36:02 -07006023static int btrfs_update_time(struct inode *inode, struct timespec64 *now,
Josef Bacike41f9412012-03-26 09:46:47 -04006024 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05006025{
Alexander Block2bc5565282012-06-15 09:49:33 +02006026 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Layton3a8c7232017-12-11 06:35:24 -05006027 bool dirty = flags & ~S_VERSION;
Alexander Block2bc5565282012-06-15 09:49:33 +02006028
6029 if (btrfs_root_readonly(root))
6030 return -EROFS;
6031
Josef Bacike41f9412012-03-26 09:46:47 -04006032 if (flags & S_VERSION)
Jeff Layton3a8c7232017-12-11 06:35:24 -05006033 dirty |= inode_maybe_inc_iversion(inode, dirty);
Josef Bacike41f9412012-03-26 09:46:47 -04006034 if (flags & S_CTIME)
6035 inode->i_ctime = *now;
6036 if (flags & S_MTIME)
6037 inode->i_mtime = *now;
6038 if (flags & S_ATIME)
6039 inode->i_atime = *now;
Jeff Layton3a8c7232017-12-11 06:35:24 -05006040 return dirty ? btrfs_dirty_inode(inode) : 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006041}
6042
Chris Masond352ac62008-09-29 15:18:18 -04006043/*
6044 * find the highest existing sequence number in a directory
6045 * and then set the in-memory index_cnt variable to reflect
6046 * free sequence numbers
6047 */
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006048static int btrfs_set_inode_index_count(struct btrfs_inode *inode)
Josef Bacikaec74772008-07-24 12:12:38 -04006049{
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006050 struct btrfs_root *root = inode->root;
Josef Bacikaec74772008-07-24 12:12:38 -04006051 struct btrfs_key key, found_key;
6052 struct btrfs_path *path;
6053 struct extent_buffer *leaf;
6054 int ret;
6055
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006056 key.objectid = btrfs_ino(inode);
David Sterba962a2982014-06-04 18:41:45 +02006057 key.type = BTRFS_DIR_INDEX_KEY;
Josef Bacikaec74772008-07-24 12:12:38 -04006058 key.offset = (u64)-1;
6059
6060 path = btrfs_alloc_path();
6061 if (!path)
6062 return -ENOMEM;
6063
6064 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
6065 if (ret < 0)
6066 goto out;
6067 /* FIXME: we should be able to handle this */
6068 if (ret == 0)
6069 goto out;
6070 ret = 0;
6071
6072 /*
6073 * MAGIC NUMBER EXPLANATION:
6074 * since we search a directory based on f_pos we have to start at 2
6075 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
6076 * else has to start at 2
6077 */
6078 if (path->slots[0] == 0) {
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006079 inode->index_cnt = 2;
Josef Bacikaec74772008-07-24 12:12:38 -04006080 goto out;
6081 }
6082
6083 path->slots[0]--;
6084
6085 leaf = path->nodes[0];
6086 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6087
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006088 if (found_key.objectid != btrfs_ino(inode) ||
David Sterba962a2982014-06-04 18:41:45 +02006089 found_key.type != BTRFS_DIR_INDEX_KEY) {
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006090 inode->index_cnt = 2;
Josef Bacikaec74772008-07-24 12:12:38 -04006091 goto out;
6092 }
6093
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006094 inode->index_cnt = found_key.offset + 1;
Josef Bacikaec74772008-07-24 12:12:38 -04006095out:
6096 btrfs_free_path(path);
6097 return ret;
6098}
6099
Chris Masond352ac62008-09-29 15:18:18 -04006100/*
6101 * helper to find a free sequence number in a given directory. This current
6102 * code is very simple, later versions will do smarter things in the btree
6103 */
Nikolay Borisov877574e2017-02-20 13:50:33 +02006104int btrfs_set_inode_index(struct btrfs_inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04006105{
6106 int ret = 0;
6107
Nikolay Borisov877574e2017-02-20 13:50:33 +02006108 if (dir->index_cnt == (u64)-1) {
6109 ret = btrfs_inode_delayed_dir_index_count(dir);
Miao Xie16cdcec2011-04-22 18:12:22 +08006110 if (ret) {
6111 ret = btrfs_set_inode_index_count(dir);
6112 if (ret)
6113 return ret;
6114 }
Josef Bacikaec74772008-07-24 12:12:38 -04006115 }
6116
Nikolay Borisov877574e2017-02-20 13:50:33 +02006117 *index = dir->index_cnt;
6118 dir->index_cnt++;
Josef Bacikaec74772008-07-24 12:12:38 -04006119
6120 return ret;
6121}
6122
Chris Masonb0d5d102014-09-08 13:08:51 -07006123static int btrfs_insert_inode_locked(struct inode *inode)
6124{
6125 struct btrfs_iget_args args;
6126 args.location = &BTRFS_I(inode)->location;
6127 args.root = BTRFS_I(inode)->root;
6128
6129 return insert_inode_locked4(inode,
6130 btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root),
6131 btrfs_find_actor, &args);
6132}
6133
Anand Jain19aee8d2017-07-18 17:37:05 +08006134/*
6135 * Inherit flags from the parent inode.
6136 *
6137 * Currently only the compression flags and the cow flags are inherited.
6138 */
6139static void btrfs_inherit_iflags(struct inode *inode, struct inode *dir)
6140{
6141 unsigned int flags;
6142
6143 if (!dir)
6144 return;
6145
6146 flags = BTRFS_I(dir)->flags;
6147
6148 if (flags & BTRFS_INODE_NOCOMPRESS) {
6149 BTRFS_I(inode)->flags &= ~BTRFS_INODE_COMPRESS;
6150 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
6151 } else if (flags & BTRFS_INODE_COMPRESS) {
6152 BTRFS_I(inode)->flags &= ~BTRFS_INODE_NOCOMPRESS;
6153 BTRFS_I(inode)->flags |= BTRFS_INODE_COMPRESS;
6154 }
6155
6156 if (flags & BTRFS_INODE_NODATACOW) {
6157 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
6158 if (S_ISREG(inode->i_mode))
6159 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
6160 }
6161
David Sterba7b6a2212018-03-26 18:40:21 +02006162 btrfs_sync_inode_flags_to_i_flags(inode);
Anand Jain19aee8d2017-07-18 17:37:05 +08006163}
6164
Chris Mason39279cc2007-06-12 06:35:45 -04006165static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
6166 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04006167 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05006168 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04006169 u64 ref_objectid, u64 objectid,
6170 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04006171{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006172 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04006173 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006174 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04006175 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04006176 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05006177 struct btrfs_inode_ref *ref;
6178 struct btrfs_key key[2];
6179 u32 sizes[2];
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006180 int nitems = name ? 2 : 1;
Chris Mason9c583092008-01-29 15:15:18 -05006181 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04006182 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006183
Chris Mason5f39d392007-10-15 16:14:19 -04006184 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07006185 if (!path)
6186 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04006187
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006188 inode = new_inode(fs_info->sb);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006189 if (!inode) {
6190 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04006191 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006192 }
Chris Mason39279cc2007-06-12 06:35:45 -04006193
Li Zefan581bb052011-04-20 10:06:11 +08006194 /*
Filipe Manana5762b5c2014-08-01 00:10:32 +01006195 * O_TMPFILE, set link count to 0, so that after this point,
6196 * we fill in an inode item with the correct link count.
6197 */
6198 if (!name)
6199 set_nlink(inode, 0);
6200
6201 /*
Li Zefan581bb052011-04-20 10:06:11 +08006202 * we have to initialize this early, so we can reclaim the inode
6203 * number if we fail afterwards in this function.
6204 */
6205 inode->i_ino = objectid;
6206
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006207 if (dir && name) {
liubo1abe9b82011-03-24 11:18:59 +00006208 trace_btrfs_inode_request(dir);
6209
Nikolay Borisov877574e2017-02-20 13:50:33 +02006210 ret = btrfs_set_inode_index(BTRFS_I(dir), index);
Shen Feng09771432009-04-02 16:46:06 -04006211 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006212 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04006213 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04006214 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04006215 }
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006216 } else if (dir) {
6217 *index = 0;
Josef Bacikaec74772008-07-24 12:12:38 -04006218 }
6219 /*
6220 * index_cnt is ignored for everything but a dir,
Su Yuedf6703e2018-01-12 11:08:02 +08006221 * btrfs_set_inode_index_count has an explanation for the magic
Josef Bacikaec74772008-07-24 12:12:38 -04006222 * number
6223 */
6224 BTRFS_I(inode)->index_cnt = 2;
Miao Xie67de1172013-12-26 13:07:06 +08006225 BTRFS_I(inode)->dir_index = *index;
Chris Mason39279cc2007-06-12 06:35:45 -04006226 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04006227 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00006228 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db2b2007-08-27 16:49:44 -04006229
Josef Bacik5dc562c2012-08-17 13:14:17 -04006230 /*
6231 * We could have gotten an inode number from somebody who was fsynced
6232 * and then removed in this same transaction, so let's just set full
6233 * sync since it will be a full sync anyway and this will blow away the
6234 * old info in the log.
6235 */
6236 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
6237
Chris Mason9c583092008-01-29 15:15:18 -05006238 key[0].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006239 key[0].type = BTRFS_INODE_ITEM_KEY;
Chris Mason9c583092008-01-29 15:15:18 -05006240 key[0].offset = 0;
6241
Chris Mason9c583092008-01-29 15:15:18 -05006242 sizes[0] = sizeof(struct btrfs_inode_item);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006243
6244 if (name) {
6245 /*
6246 * Start new inodes with an inode_ref. This is slightly more
6247 * efficient for small numbers of hard links since they will
6248 * be packed into one item. Extended refs will kick in if we
6249 * add more hard links than can fit in the ref item.
6250 */
6251 key[1].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006252 key[1].type = BTRFS_INODE_REF_KEY;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006253 key[1].offset = ref_objectid;
6254
6255 sizes[1] = name_len + sizeof(*ref);
6256 }
Chris Mason9c583092008-01-29 15:15:18 -05006257
Chris Masonb0d5d102014-09-08 13:08:51 -07006258 location = &BTRFS_I(inode)->location;
6259 location->objectid = objectid;
6260 location->offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02006261 location->type = BTRFS_INODE_ITEM_KEY;
Chris Masonb0d5d102014-09-08 13:08:51 -07006262
6263 ret = btrfs_insert_inode_locked(inode);
Al Viro32955c52018-05-16 12:20:05 -04006264 if (ret < 0) {
6265 iput(inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006266 goto fail;
Al Viro32955c52018-05-16 12:20:05 -04006267 }
Chris Masonb0d5d102014-09-08 13:08:51 -07006268
Chris Masonb9473432009-03-13 11:00:37 -04006269 path->leave_spinning = 1;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006270 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems);
Chris Mason9c583092008-01-29 15:15:18 -05006271 if (ret != 0)
Chris Masonb0d5d102014-09-08 13:08:51 -07006272 goto fail_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04006273
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03006274 inode_init_owner(inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04006275 inode_set_bytes(inode, 0);
chandan r9cc97d62012-07-04 12:48:07 +05306276
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006277 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05306278 inode->i_atime = inode->i_mtime;
6279 inode->i_ctime = inode->i_mtime;
Arnd Bergmannd3c6be6f2018-06-21 18:04:06 +02006280 BTRFS_I(inode)->i_otime = inode->i_mtime;
chandan r9cc97d62012-07-04 12:48:07 +05306281
Chris Mason5f39d392007-10-15 16:14:19 -04006282 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
6283 struct btrfs_inode_item);
David Sterbab159fa22016-11-08 18:09:03 +01006284 memzero_extent_buffer(path->nodes[0], (unsigned long)inode_item,
Li Zefan293f7e02012-07-10 00:58:58 -06006285 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04006286 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05006287
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006288 if (name) {
6289 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
6290 struct btrfs_inode_ref);
6291 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
6292 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
6293 ptr = (unsigned long)(ref + 1);
6294 write_extent_buffer(path->nodes[0], name, ptr, name_len);
6295 }
Chris Mason9c583092008-01-29 15:15:18 -05006296
Chris Mason5f39d392007-10-15 16:14:19 -04006297 btrfs_mark_buffer_dirty(path->nodes[0]);
6298 btrfs_free_path(path);
6299
Christoph Hellwig6cbff002009-04-17 10:37:41 +02006300 btrfs_inherit_iflags(inode, dir);
6301
Al Viro569254b2011-07-24 17:08:40 -04006302 if (S_ISREG(mode)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006303 if (btrfs_test_opt(fs_info, NODATASUM))
Chris Mason94272162009-07-02 12:26:06 -04006304 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006305 if (btrfs_test_opt(fs_info, NODATACOW))
Josef Bacikf2bdf9a2013-02-21 15:28:28 -05006306 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
6307 BTRFS_INODE_NODATASUM;
Chris Mason94272162009-07-02 12:26:06 -04006308 }
6309
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006310 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00006311
6312 trace_btrfs_inode_new(inode);
Chris Mason1973f0f2011-06-24 13:13:29 -04006313 btrfs_set_inode_last_trans(trans, inode);
liubo1abe9b82011-03-24 11:18:59 +00006314
Alexander Block8ea05e32012-07-25 17:35:53 +02006315 btrfs_update_root_times(trans, root);
6316
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006317 ret = btrfs_inode_inherit_props(trans, inode, dir);
6318 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006319 btrfs_err(fs_info,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006320 "error inheriting props for ino %llu (root %llu): %d",
David Sterbaf85b7372017-01-20 14:54:07 +01006321 btrfs_ino(BTRFS_I(inode)), root->root_key.objectid, ret);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006322
Chris Mason39279cc2007-06-12 06:35:45 -04006323 return inode;
Chris Masonb0d5d102014-09-08 13:08:51 -07006324
6325fail_unlock:
Al Viro32955c52018-05-16 12:20:05 -04006326 discard_new_inode(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006327fail:
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006328 if (dir && name)
Josef Bacikaec74772008-07-24 12:12:38 -04006329 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04006330 btrfs_free_path(path);
6331 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006332}
6333
6334static inline u8 btrfs_inode_type(struct inode *inode)
6335{
6336 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
6337}
6338
Chris Masond352ac62008-09-29 15:18:18 -04006339/*
6340 * utility function to add 'inode' into 'parent_inode' with
6341 * a give name and a given sequence number.
6342 * if 'add_backref' is true, also insert a backref from the
6343 * inode to the parent directory.
6344 */
Chris Masone02119d2008-09-05 16:13:11 -04006345int btrfs_add_link(struct btrfs_trans_handle *trans,
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006346 struct btrfs_inode *parent_inode, struct btrfs_inode *inode,
Chris Masone02119d2008-09-05 16:13:11 -04006347 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006348{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006349 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006350 struct btrfs_key key;
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006351 struct btrfs_root *root = parent_inode->root;
6352 u64 ino = btrfs_ino(inode);
6353 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006354
Li Zefan33345d012011-04-20 10:31:50 +08006355 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006356 memcpy(&key, &inode->root->root_key, sizeof(key));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006357 } else {
Li Zefan33345d012011-04-20 10:31:50 +08006358 key.objectid = ino;
David Sterba962a2982014-06-04 18:41:45 +02006359 key.type = BTRFS_INODE_ITEM_KEY;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006360 key.offset = 0;
6361 }
Chris Mason39279cc2007-06-12 06:35:45 -04006362
Li Zefan33345d012011-04-20 10:31:50 +08006363 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Lu Fengqi6025c192018-08-01 11:32:29 +08006364 ret = btrfs_add_root_ref(trans, key.objectid,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006365 root->root_key.objectid, parent_ino,
6366 index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006367 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08006368 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
6369 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006370 }
6371
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006372 /* Nothing to clean up yet */
6373 if (ret)
6374 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006375
Lu Fengqi684572d2018-08-04 21:10:57 +08006376 ret = btrfs_insert_dir_item(trans, name, name_len, parent_inode, &key,
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006377 btrfs_inode_type(&inode->vfs_inode), index);
Chris Mason9c520572012-12-17 14:26:57 -05006378 if (ret == -EEXIST || ret == -EOVERFLOW)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006379 goto fail_dir_item;
6380 else if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04006381 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006382 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006383 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006384
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006385 btrfs_i_size_write(parent_inode, parent_inode->vfs_inode.i_size +
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006386 name_len * 2);
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006387 inode_inc_iversion(&parent_inode->vfs_inode);
6388 parent_inode->vfs_inode.i_mtime = parent_inode->vfs_inode.i_ctime =
6389 current_time(&parent_inode->vfs_inode);
6390 ret = btrfs_update_inode(trans, root, &parent_inode->vfs_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006391 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04006392 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006393 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05006394
6395fail_dir_item:
6396 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
6397 u64 local_index;
6398 int err;
Lu Fengqi3ee1c552018-08-01 11:32:28 +08006399 err = btrfs_del_root_ref(trans, key.objectid,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006400 root->root_key.objectid, parent_ino,
6401 &local_index, name, name_len);
Chris Masonfe66a052012-02-20 08:40:56 -05006402
6403 } else if (add_backref) {
6404 u64 local_index;
6405 int err;
6406
6407 err = btrfs_del_inode_ref(trans, root, name, name_len,
6408 ino, parent_ino, &local_index);
6409 }
6410 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006411}
6412
6413static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006414 struct btrfs_inode *dir, struct dentry *dentry,
6415 struct btrfs_inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006416{
Josef Bacika1b075d2010-11-19 20:36:11 +00006417 int err = btrfs_add_link(trans, dir, inode,
6418 dentry->d_name.name, dentry->d_name.len,
6419 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006420 if (err > 0)
6421 err = -EEXIST;
6422 return err;
6423}
6424
Josef Bacik618e21d2007-07-11 10:18:17 -04006425static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04006426 umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04006427{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006428 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Josef Bacik618e21d2007-07-11 10:18:17 -04006429 struct btrfs_trans_handle *trans;
6430 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006431 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04006432 int err;
Josef Bacik618e21d2007-07-11 10:18:17 -04006433 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006434 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04006435
Josef Bacik9ed74f22009-09-11 16:12:44 -04006436 /*
6437 * 2 for inode item and ref
6438 * 2 for dir items
6439 * 1 for xattr if selinux is on
6440 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006441 trans = btrfs_start_transaction(root, 5);
6442 if (IS_ERR(trans))
6443 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05006444
Li Zefan581bb052011-04-20 10:06:11 +08006445 err = btrfs_find_free_ino(root, &objectid);
6446 if (err)
6447 goto out_unlock;
6448
Josef Bacikaec74772008-07-24 12:12:38 -04006449 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006450 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6451 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006452 if (IS_ERR(inode)) {
6453 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -04006454 inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04006455 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006456 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006457
Casey Schauflerad19db72011-12-15 10:09:07 -05006458 /*
6459 * If the active LSM wants to access the inode during
6460 * d_instantiate it needs these. Smack checks to see
6461 * if the filesystem supports xattrs by looking at the
6462 * ops vector.
6463 */
Casey Schauflerad19db72011-12-15 10:09:07 -05006464 inode->i_op = &btrfs_special_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006465 init_special_inode(inode, inode->i_mode, rdev);
6466
6467 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik618e21d2007-07-11 10:18:17 -04006468 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006469 goto out_unlock;
Chris Masonb0d5d102014-09-08 13:08:51 -07006470
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006471 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6472 0, index);
Al Viro32955c52018-05-16 12:20:05 -04006473 if (err)
6474 goto out_unlock;
6475
6476 btrfs_update_inode(trans, root, inode);
6477 d_instantiate_new(dentry, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006478
Josef Bacik618e21d2007-07-11 10:18:17 -04006479out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006480 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006481 btrfs_btree_balance_dirty(fs_info);
Al Viro32955c52018-05-16 12:20:05 -04006482 if (err && inode) {
Josef Bacik618e21d2007-07-11 10:18:17 -04006483 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -04006484 discard_new_inode(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006485 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006486 return err;
6487}
6488
Chris Mason39279cc2007-06-12 06:35:45 -04006489static int btrfs_create(struct inode *dir, struct dentry *dentry,
Al Viroebfc3b42012-06-10 18:05:36 -04006490 umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -04006491{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006492 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04006493 struct btrfs_trans_handle *trans;
6494 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006495 struct inode *inode = NULL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006496 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04006497 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006498 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006499
Josef Bacik9ed74f22009-09-11 16:12:44 -04006500 /*
6501 * 2 for inode item and ref
6502 * 2 for dir items
6503 * 1 for xattr if selinux is on
6504 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006505 trans = btrfs_start_transaction(root, 5);
6506 if (IS_ERR(trans))
6507 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006508
Li Zefan581bb052011-04-20 10:06:11 +08006509 err = btrfs_find_free_ino(root, &objectid);
6510 if (err)
6511 goto out_unlock;
6512
Josef Bacikaec74772008-07-24 12:12:38 -04006513 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006514 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6515 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006516 if (IS_ERR(inode)) {
6517 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -04006518 inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006519 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006520 }
Casey Schauflerad19db72011-12-15 10:09:07 -05006521 /*
6522 * If the active LSM wants to access the inode during
6523 * d_instantiate it needs these. Smack checks to see
6524 * if the filesystem supports xattrs by looking at the
6525 * ops vector.
6526 */
6527 inode->i_fop = &btrfs_file_operations;
6528 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006529 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006530
6531 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
6532 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006533 goto out_unlock;
Chris Masonb0d5d102014-09-08 13:08:51 -07006534
6535 err = btrfs_update_inode(trans, root, inode);
6536 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006537 goto out_unlock;
Casey Schauflerad19db72011-12-15 10:09:07 -05006538
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006539 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6540 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006541 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006542 goto out_unlock;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006543
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006544 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Al Viro1e2e5472018-05-04 08:23:01 -04006545 d_instantiate_new(dentry, inode);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006546
Chris Mason39279cc2007-06-12 06:35:45 -04006547out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006548 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -04006549 if (err && inode) {
Chris Mason39279cc2007-06-12 06:35:45 -04006550 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -04006551 discard_new_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006552 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006553 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006554 return err;
6555}
6556
6557static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
6558 struct dentry *dentry)
6559{
Filipe Manana271dba452016-01-05 16:24:05 +00006560 struct btrfs_trans_handle *trans = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006561 struct btrfs_root *root = BTRFS_I(dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00006562 struct inode *inode = d_inode(old_dentry);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006563 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason00e4e6b2008-08-05 11:18:09 -04006564 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04006565 int err;
6566 int drop_inode = 0;
6567
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006568 /* do not allow sys_link's with other subvols of the same device */
Misono Tomohiro4fd786e2018-08-06 14:25:24 +09006569 if (root->root_key.objectid != BTRFS_I(inode)->root->root_key.objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00006570 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006571
Mark Fashehf1863732012-08-08 11:32:27 -07006572 if (inode->i_nlink >= BTRFS_LINK_MAX)
Al Viroc055e992011-03-04 17:15:18 +00006573 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04006574
Nikolay Borisov877574e2017-02-20 13:50:33 +02006575 err = btrfs_set_inode_index(BTRFS_I(dir), &index);
Josef Bacikaec74772008-07-24 12:12:38 -04006576 if (err)
6577 goto fail;
6578
Yan, Zhenga22285a2010-05-16 10:48:46 -04006579 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00006580 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04006581 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00006582 * 1 item for parent inode
Omar Sandoval399b0bb2018-05-11 13:13:40 -07006583 * 1 item for orphan item deletion if O_TMPFILE
Yan, Zhenga22285a2010-05-16 10:48:46 -04006584 */
Omar Sandoval399b0bb2018-05-11 13:13:40 -07006585 trans = btrfs_start_transaction(root, inode->i_nlink ? 5 : 6);
Yan, Zhenga22285a2010-05-16 10:48:46 -04006586 if (IS_ERR(trans)) {
6587 err = PTR_ERR(trans);
Filipe Manana271dba452016-01-05 16:24:05 +00006588 trans = NULL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006589 goto fail;
6590 }
Chris Mason5f39d392007-10-15 16:14:19 -04006591
Miao Xie67de1172013-12-26 13:07:06 +08006592 /* There are several dir indexes for this inode, clear the cache. */
6593 BTRFS_I(inode)->dir_index = 0ULL;
Zach Brown8b558c52013-10-16 12:10:34 -07006594 inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04006595 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006596 inode->i_ctime = current_time(inode);
Al Viro7de9c6ee2010-10-23 11:11:40 -04006597 ihold(inode);
Josef Bacike9976152012-10-11 15:53:56 -04006598 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
Josef Bacikaec74772008-07-24 12:12:38 -04006599
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006600 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6601 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04006602
Yan, Zhenga5719522009-09-24 09:17:31 -04006603 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006604 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04006605 } else {
Al Viro10d9f302011-07-16 23:09:10 -04006606 struct dentry *parent = dentry->d_parent;
Filipe Mananad4682ba2018-06-11 19:24:28 +01006607 int ret;
6608
Yan, Zhenga5719522009-09-24 09:17:31 -04006609 err = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006610 if (err)
6611 goto fail;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006612 if (inode->i_nlink == 1) {
6613 /*
6614 * If new hard link count is 1, it's a file created
6615 * with open(2) O_TMPFILE flag.
6616 */
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02006617 err = btrfs_orphan_del(trans, BTRFS_I(inode));
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006618 if (err)
6619 goto fail;
6620 }
Al Viro08c422c2011-12-23 07:58:13 -05006621 d_instantiate(dentry, inode);
Filipe Mananad4682ba2018-06-11 19:24:28 +01006622 ret = btrfs_log_new_name(trans, BTRFS_I(inode), NULL, parent,
6623 true, NULL);
6624 if (ret == BTRFS_NEED_TRANS_COMMIT) {
6625 err = btrfs_commit_transaction(trans);
6626 trans = NULL;
6627 }
Yan, Zhenga5719522009-09-24 09:17:31 -04006628 }
Chris Mason39279cc2007-06-12 06:35:45 -04006629
Chris Mason1832a6d2007-12-21 16:27:21 -05006630fail:
Filipe Manana271dba452016-01-05 16:24:05 +00006631 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006632 btrfs_end_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006633 if (drop_inode) {
6634 inode_dec_link_count(inode);
6635 iput(inode);
6636 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006637 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006638 return err;
6639}
6640
Al Viro18bb1db2011-07-26 01:41:39 -04006641static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04006642{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006643 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masonb9d86662008-05-02 16:13:49 -04006644 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006645 struct btrfs_trans_handle *trans;
6646 struct btrfs_root *root = BTRFS_I(dir)->root;
6647 int err = 0;
6648 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04006649 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006650 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006651
Josef Bacik9ed74f22009-09-11 16:12:44 -04006652 /*
6653 * 2 items for inode and ref
6654 * 2 items for dir items
6655 * 1 for xattr if selinux is on
6656 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006657 trans = btrfs_start_transaction(root, 5);
6658 if (IS_ERR(trans))
6659 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006660
Li Zefan581bb052011-04-20 10:06:11 +08006661 err = btrfs_find_free_ino(root, &objectid);
6662 if (err)
6663 goto out_fail;
6664
Josef Bacikaec74772008-07-24 12:12:38 -04006665 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006666 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6667 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04006668 if (IS_ERR(inode)) {
6669 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -04006670 inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006671 goto out_fail;
6672 }
Chris Mason5f39d392007-10-15 16:14:19 -04006673
Chris Mason39279cc2007-06-12 06:35:45 -04006674 drop_on_err = 1;
Chris Masonb0d5d102014-09-08 13:08:51 -07006675 /* these must be set before we unlock the inode */
6676 inode->i_op = &btrfs_dir_inode_operations;
6677 inode->i_fop = &btrfs_dir_file_operations;
Josef Bacik33268ea2008-07-24 12:16:36 -04006678
Eric Paris2a7dba32011-02-01 11:05:39 -05006679 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04006680 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006681 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04006682
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02006683 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Mason39279cc2007-06-12 06:35:45 -04006684 err = btrfs_update_inode(trans, root, inode);
6685 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006686 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04006687
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006688 err = btrfs_add_link(trans, BTRFS_I(dir), BTRFS_I(inode),
6689 dentry->d_name.name,
6690 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006691 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006692 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04006693
Al Viro1e2e5472018-05-04 08:23:01 -04006694 d_instantiate_new(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006695 drop_on_err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006696
6697out_fail:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006698 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -04006699 if (err && inode) {
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006700 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -04006701 discard_new_inode(inode);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006702 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006703 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006704 return err;
6705}
6706
Chris Masonc8b97812008-10-29 14:49:59 -04006707static noinline int uncompress_inline(struct btrfs_path *path,
Byongho Leee40da0e2015-05-19 23:46:45 +09006708 struct page *page,
Chris Masonc8b97812008-10-29 14:49:59 -04006709 size_t pg_offset, u64 extent_offset,
6710 struct btrfs_file_extent_item *item)
6711{
6712 int ret;
6713 struct extent_buffer *leaf = path->nodes[0];
6714 char *tmp;
6715 size_t max_size;
6716 unsigned long inline_size;
6717 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08006718 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04006719
6720 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08006721 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04006722 max_size = btrfs_file_extent_ram_bytes(leaf, item);
6723 inline_size = btrfs_file_extent_inline_item_len(leaf,
Ross Kirkdd3cc162013-09-16 15:58:09 +01006724 btrfs_item_nr(path->slots[0]));
Chris Masonc8b97812008-10-29 14:49:59 -04006725 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04006726 if (!tmp)
6727 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04006728 ptr = btrfs_file_extent_inline_start(item);
6729
6730 read_extent_buffer(leaf, tmp, ptr, inline_size);
6731
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006732 max_size = min_t(unsigned long, PAGE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08006733 ret = btrfs_decompress(compress_type, tmp, page,
6734 extent_offset, inline_size, max_size);
Zygo Blaxelle1699d22017-03-10 16:45:44 -05006735
6736 /*
6737 * decompression code contains a memset to fill in any space between the end
6738 * of the uncompressed data and the end of max_size in case the decompressed
6739 * data ends up shorter than ram_bytes. That doesn't cover the hole between
6740 * the end of an inline extent and the beginning of the next block, so we
6741 * cover that region here.
6742 */
6743
6744 if (max_size + pg_offset < PAGE_SIZE) {
6745 char *map = kmap(page);
6746 memset(map + pg_offset + max_size, 0, PAGE_SIZE - max_size - pg_offset);
6747 kunmap(page);
6748 }
Chris Masonc8b97812008-10-29 14:49:59 -04006749 kfree(tmp);
Zach Brown166ae5a2014-05-09 17:15:10 -04006750 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -04006751}
6752
Chris Masond352ac62008-09-29 15:18:18 -04006753/*
6754 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05006755 * the ugly parts come from merging extents from the disk with the in-ram
6756 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04006757 * where the in-ram extents might be locked pending data=ordered completion.
6758 *
6759 * This also copies inline extents directly into the page.
6760 */
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006761struct extent_map *btrfs_get_extent(struct btrfs_inode *inode,
Liu Bode2c6612018-08-25 13:47:59 +08006762 struct page *page,
6763 size_t pg_offset, u64 start, u64 len,
6764 int create)
Chris Masona52d9a82007-08-27 16:49:44 -04006765{
David Sterba3ffbd682018-06-29 10:56:42 +02006766 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Chris Masona52d9a82007-08-27 16:49:44 -04006767 int ret;
6768 int err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006769 u64 extent_start = 0;
6770 u64 extent_end = 0;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006771 u64 objectid = btrfs_ino(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04006772 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04006773 struct btrfs_path *path = NULL;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006774 struct btrfs_root *root = inode->root;
Chris Masona52d9a82007-08-27 16:49:44 -04006775 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04006776 struct extent_buffer *leaf;
6777 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04006778 struct extent_map *em = NULL;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006779 struct extent_map_tree *em_tree = &inode->extent_tree;
6780 struct extent_io_tree *io_tree = &inode->io_tree;
Filipe Manana7ffbb592014-06-09 03:48:05 +01006781 const bool new_inline = !page || create;
Chris Masona52d9a82007-08-27 16:49:44 -04006782
Chris Mason890871b2009-09-02 16:24:52 -04006783 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006784 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04006785 if (em)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006786 em->bdev = fs_info->fs_devices->latest_bdev;
Chris Mason890871b2009-09-02 16:24:52 -04006787 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006788
Chris Masona52d9a82007-08-27 16:49:44 -04006789 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04006790 if (em->start > start || em->start + em->len <= start)
6791 free_extent_map(em);
6792 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05006793 free_extent_map(em);
6794 else
6795 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006796 }
David Sterba172ddd62011-04-21 00:48:27 +02006797 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04006798 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05006799 err = -ENOMEM;
6800 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006801 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006802 em->bdev = fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05006803 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05006804 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05006805 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04006806 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04006807
Liu Bobee6ec82018-08-17 05:05:28 +08006808 path = btrfs_alloc_path();
Chris Masonf4219502008-07-22 11:18:09 -04006809 if (!path) {
Liu Bobee6ec82018-08-17 05:05:28 +08006810 err = -ENOMEM;
6811 goto out;
Chris Masonf4219502008-07-22 11:18:09 -04006812 }
6813
Liu Bobee6ec82018-08-17 05:05:28 +08006814 /* Chances are we'll be called again, so go ahead and do readahead */
6815 path->reada = READA_FORWARD;
6816
Liu Boe49aabd2018-08-25 13:47:09 +08006817 /*
6818 * Unless we're going to uncompress the inline extent, no sleep would
6819 * happen.
6820 */
6821 path->leave_spinning = 1;
6822
Nikolay Borisov5c9a7022017-12-01 11:19:40 +02006823 ret = btrfs_lookup_file_extent(NULL, root, path, objectid, start, 0);
Chris Masona52d9a82007-08-27 16:49:44 -04006824 if (ret < 0) {
6825 err = ret;
6826 goto out;
6827 }
6828
6829 if (ret != 0) {
6830 if (path->slots[0] == 0)
6831 goto not_found;
6832 path->slots[0]--;
6833 }
6834
Chris Mason5f39d392007-10-15 16:14:19 -04006835 leaf = path->nodes[0];
6836 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04006837 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04006838 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04006839 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02006840 found_type = found_key.type;
Chris Mason5f39d392007-10-15 16:14:19 -04006841 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04006842 found_type != BTRFS_EXTENT_DATA_KEY) {
Josef Bacik25a50342013-10-14 12:08:38 -04006843 /*
6844 * If we backup past the first extent we want to move forward
6845 * and see if there is an extent in front of us, otherwise we'll
6846 * say there is a hole for our whole search range which can
6847 * cause problems.
6848 */
6849 extent_end = start;
6850 goto next;
Chris Masona52d9a82007-08-27 16:49:44 -04006851 }
6852
Chris Mason5f39d392007-10-15 16:14:19 -04006853 found_type = btrfs_file_extent_type(leaf, item);
6854 extent_start = found_key.offset;
Yan Zhengd899e052008-10-30 14:25:28 -04006855 if (found_type == BTRFS_FILE_EXTENT_REG ||
6856 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006857 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04006858 btrfs_file_extent_num_bytes(leaf, item);
Liu Bo09ed2f12017-03-10 11:09:48 -08006859
6860 trace_btrfs_get_extent_show_fi_regular(inode, leaf, item,
6861 extent_start);
Yan Zheng9036c102008-10-30 14:19:41 -04006862 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
6863 size_t size;
Qu Wenruoe41ca582018-06-06 15:41:49 +08006864
6865 size = btrfs_file_extent_ram_bytes(leaf, item);
Jeff Mahoneyda170662016-06-15 09:22:56 -04006866 extent_end = ALIGN(extent_start + size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006867 fs_info->sectorsize);
Liu Bo09ed2f12017-03-10 11:09:48 -08006868
6869 trace_btrfs_get_extent_show_fi_inline(inode, leaf, item,
6870 path->slots[0],
6871 extent_start);
Yan Zheng9036c102008-10-30 14:19:41 -04006872 }
Josef Bacik25a50342013-10-14 12:08:38 -04006873next:
Yan Zheng9036c102008-10-30 14:19:41 -04006874 if (start >= extent_end) {
6875 path->slots[0]++;
6876 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
6877 ret = btrfs_next_leaf(root, path);
6878 if (ret < 0) {
6879 err = ret;
6880 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006881 }
Yan Zheng9036c102008-10-30 14:19:41 -04006882 if (ret > 0)
6883 goto not_found;
6884 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04006885 }
Yan Zheng9036c102008-10-30 14:19:41 -04006886 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6887 if (found_key.objectid != objectid ||
6888 found_key.type != BTRFS_EXTENT_DATA_KEY)
6889 goto not_found;
6890 if (start + len <= found_key.offset)
6891 goto not_found;
Wang Shilonge2eca692014-07-17 11:44:14 +08006892 if (start > found_key.offset)
6893 goto next;
Yan Zheng9036c102008-10-30 14:19:41 -04006894 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006895 em->orig_start = start;
Yan Zheng9036c102008-10-30 14:19:41 -04006896 em->len = found_key.offset - start;
6897 goto not_found_em;
6898 }
6899
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006900 btrfs_extent_item_to_extent_map(inode, path, item,
Nikolay Borisov9cdc5122017-02-20 13:51:02 +02006901 new_inline, em);
Filipe Manana7ffbb592014-06-09 03:48:05 +01006902
Yan Zhengd899e052008-10-30 14:25:28 -04006903 if (found_type == BTRFS_FILE_EXTENT_REG ||
6904 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006905 goto insert;
6906 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04006907 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04006908 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04006909 size_t size;
6910 size_t extent_offset;
6911 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04006912
Filipe Manana7ffbb592014-06-09 03:48:05 +01006913 if (new_inline)
Yan689f9342007-10-29 11:41:07 -04006914 goto out;
Yan689f9342007-10-29 11:41:07 -04006915
Qu Wenruoe41ca582018-06-06 15:41:49 +08006916 size = btrfs_file_extent_ram_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04006917 extent_offset = page_offset(page) + pg_offset - extent_start;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006918 copy_size = min_t(u64, PAGE_SIZE - pg_offset,
6919 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04006920 em->start = extent_start + extent_offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006921 em->len = ALIGN(copy_size, fs_info->sectorsize);
Josef Bacikb4939682012-12-03 10:31:19 -05006922 em->orig_block_len = em->len;
Josef Bacik70c8a912012-10-11 16:54:30 -04006923 em->orig_start = em->start;
Yan689f9342007-10-29 11:41:07 -04006924 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Liu Boe49aabd2018-08-25 13:47:09 +08006925
6926 btrfs_set_path_blocking(path);
Edmund Nadolskibf46f522017-11-20 13:24:49 -07006927 if (!PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08006928 if (btrfs_file_extent_compression(leaf, item) !=
6929 BTRFS_COMPRESS_NONE) {
Byongho Leee40da0e2015-05-19 23:46:45 +09006930 ret = uncompress_inline(path, page, pg_offset,
Chris Masonc8b97812008-10-29 14:49:59 -04006931 extent_offset, item);
Zach Brown166ae5a2014-05-09 17:15:10 -04006932 if (ret) {
6933 err = ret;
6934 goto out;
6935 }
Chris Masonc8b97812008-10-29 14:49:59 -04006936 } else {
6937 map = kmap(page);
6938 read_extent_buffer(leaf, map + pg_offset, ptr,
6939 copy_size);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006940 if (pg_offset + copy_size < PAGE_SIZE) {
Chris Mason93c82d52009-09-11 12:36:29 -04006941 memset(map + pg_offset + copy_size, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006942 PAGE_SIZE - pg_offset -
Chris Mason93c82d52009-09-11 12:36:29 -04006943 copy_size);
6944 }
Chris Masonc8b97812008-10-29 14:49:59 -04006945 kunmap(page);
6946 }
Chris Mason179e29e2007-11-01 11:28:41 -04006947 flush_dcache_page(page);
Chris Masona52d9a82007-08-27 16:49:44 -04006948 }
Chris Masond1310b22008-01-24 16:13:08 -05006949 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00006950 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04006951 goto insert;
Chris Masona52d9a82007-08-27 16:49:44 -04006952 }
6953not_found:
6954 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006955 em->orig_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05006956 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04006957not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04006958 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04006959insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02006960 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05006961 if (em->start > start || extent_map_end(em) <= start) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006962 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04006963 "bad extent! em: [%llu %llu] passed [%llu %llu]",
6964 em->start, em->len, start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04006965 err = -EIO;
6966 goto out;
6967 }
Chris Masond1310b22008-01-24 16:13:08 -05006968
6969 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04006970 write_lock(&em_tree->lock);
David Sterbaf46b24c2018-04-03 21:45:57 +02006971 err = btrfs_add_extent_mapping(fs_info, em_tree, &em, start, len);
Chris Mason890871b2009-09-02 16:24:52 -04006972 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04006973out:
Liu Boc6414282018-08-23 07:36:17 +08006974 btrfs_free_path(path);
liubo1abe9b82011-03-24 11:18:59 +00006975
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006976 trace_btrfs_get_extent(root, inode, em);
liubo1abe9b82011-03-24 11:18:59 +00006977
Chris Masona52d9a82007-08-27 16:49:44 -04006978 if (err) {
6979 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04006980 return ERR_PTR(err);
6981 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006982 BUG_ON(!em); /* Error is always set */
Chris Masona52d9a82007-08-27 16:49:44 -04006983 return em;
6984}
6985
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006986struct extent_map *btrfs_get_extent_fiemap(struct btrfs_inode *inode,
6987 struct page *page,
6988 size_t pg_offset, u64 start, u64 len,
6989 int create)
Chris Masonec29ed52011-02-23 16:23:20 -05006990{
6991 struct extent_map *em;
6992 struct extent_map *hole_em = NULL;
6993 u64 range_start = start;
6994 u64 end;
6995 u64 found;
6996 u64 found_end;
6997 int err = 0;
6998
6999 em = btrfs_get_extent(inode, page, pg_offset, start, len, create);
7000 if (IS_ERR(em))
7001 return em;
Dan Carpenter99862772017-04-11 11:57:15 +03007002 /*
7003 * If our em maps to:
7004 * - a hole or
7005 * - a pre-alloc extent,
7006 * there might actually be delalloc bytes behind it.
7007 */
7008 if (em->block_start != EXTENT_MAP_HOLE &&
7009 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7010 return em;
7011 else
7012 hole_em = em;
Chris Masonec29ed52011-02-23 16:23:20 -05007013
7014 /* check to see if we've wrapped (len == -1 or similar) */
7015 end = start + len;
7016 if (end < start)
7017 end = (u64)-1;
7018 else
7019 end -= 1;
7020
7021 em = NULL;
7022
7023 /* ok, we didn't find anything, lets look for delalloc */
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007024 found = count_range_bits(&inode->io_tree, &range_start,
Chris Masonec29ed52011-02-23 16:23:20 -05007025 end, len, EXTENT_DELALLOC, 1);
7026 found_end = range_start + found;
7027 if (found_end < range_start)
7028 found_end = (u64)-1;
7029
7030 /*
7031 * we didn't find anything useful, return
7032 * the original results from get_extent()
7033 */
7034 if (range_start > end || found_end <= start) {
7035 em = hole_em;
7036 hole_em = NULL;
7037 goto out;
7038 }
7039
7040 /* adjust the range_start to make sure it doesn't
7041 * go backwards from the start they passed in
7042 */
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05307043 range_start = max(start, range_start);
Chris Masonec29ed52011-02-23 16:23:20 -05007044 found = found_end - range_start;
7045
7046 if (found > 0) {
7047 u64 hole_start = start;
7048 u64 hole_len = len;
7049
David Sterba172ddd62011-04-21 00:48:27 +02007050 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05007051 if (!em) {
7052 err = -ENOMEM;
7053 goto out;
7054 }
7055 /*
7056 * when btrfs_get_extent can't find anything it
7057 * returns one huge hole
7058 *
7059 * make sure what it found really fits our range, and
7060 * adjust to make sure it is based on the start from
7061 * the caller
7062 */
7063 if (hole_em) {
7064 u64 calc_end = extent_map_end(hole_em);
7065
7066 if (calc_end <= start || (hole_em->start > end)) {
7067 free_extent_map(hole_em);
7068 hole_em = NULL;
7069 } else {
7070 hole_start = max(hole_em->start, start);
7071 hole_len = calc_end - hole_start;
7072 }
7073 }
7074 em->bdev = NULL;
7075 if (hole_em && range_start > hole_start) {
7076 /* our hole starts before our delalloc, so we
7077 * have to return just the parts of the hole
7078 * that go until the delalloc starts
7079 */
7080 em->len = min(hole_len,
7081 range_start - hole_start);
7082 em->start = hole_start;
7083 em->orig_start = hole_start;
7084 /*
7085 * don't adjust block start at all,
7086 * it is fixed at EXTENT_MAP_HOLE
7087 */
7088 em->block_start = hole_em->block_start;
7089 em->block_len = hole_len;
Liu Bof9e4fb52013-01-07 10:10:12 +00007090 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
7091 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonec29ed52011-02-23 16:23:20 -05007092 } else {
7093 em->start = range_start;
7094 em->len = found;
7095 em->orig_start = range_start;
7096 em->block_start = EXTENT_MAP_DELALLOC;
7097 em->block_len = found;
7098 }
Nikolay Borisovbf8d32b2017-12-01 11:19:43 +02007099 } else {
Chris Masonec29ed52011-02-23 16:23:20 -05007100 return hole_em;
7101 }
7102out:
7103
7104 free_extent_map(hole_em);
7105 if (err) {
7106 free_extent_map(em);
7107 return ERR_PTR(err);
7108 }
7109 return em;
7110}
7111
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007112static struct extent_map *btrfs_create_dio_extent(struct inode *inode,
7113 const u64 start,
7114 const u64 len,
7115 const u64 orig_start,
7116 const u64 block_start,
7117 const u64 block_len,
7118 const u64 orig_block_len,
7119 const u64 ram_bytes,
7120 const int type)
7121{
7122 struct extent_map *em = NULL;
7123 int ret;
7124
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007125 if (type != BTRFS_ORDERED_NOCOW) {
Liu Bo6f9994d2017-01-31 07:50:22 -08007126 em = create_io_em(inode, start, len, orig_start,
7127 block_start, block_len, orig_block_len,
7128 ram_bytes,
7129 BTRFS_COMPRESS_NONE, /* compress_type */
7130 type);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007131 if (IS_ERR(em))
7132 goto out;
7133 }
7134 ret = btrfs_add_ordered_extent_dio(inode, start, block_start,
7135 len, block_len, type);
7136 if (ret) {
7137 if (em) {
7138 free_extent_map(em);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02007139 btrfs_drop_extent_cache(BTRFS_I(inode), start,
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007140 start + len - 1, 0);
7141 }
7142 em = ERR_PTR(ret);
7143 }
7144 out:
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007145
7146 return em;
7147}
7148
Josef Bacik4b46fce2010-05-23 11:00:55 -04007149static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
7150 u64 start, u64 len)
7151{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007152 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007153 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik70c8a912012-10-11 16:54:30 -04007154 struct extent_map *em;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007155 struct btrfs_key ins;
7156 u64 alloc_hint;
7157 int ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007158
Josef Bacik4b46fce2010-05-23 11:00:55 -04007159 alloc_hint = get_extent_allocation_hint(inode, start, len);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007160 ret = btrfs_reserve_extent(root, len, len, fs_info->sectorsize,
Jeff Mahoneyda170662016-06-15 09:22:56 -04007161 0, alloc_hint, &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007162 if (ret)
7163 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007164
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007165 em = btrfs_create_dio_extent(inode, start, ins.offset, start,
7166 ins.objectid, ins.offset, ins.offset,
Liu Bo6288d6e2017-02-21 12:12:58 -08007167 ins.offset, BTRFS_ORDERED_REGULAR);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007168 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007169 if (IS_ERR(em))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007170 btrfs_free_reserved_extent(fs_info, ins.objectid,
7171 ins.offset, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007172
Josef Bacik4b46fce2010-05-23 11:00:55 -04007173 return em;
7174}
7175
Chris Mason46bfbb52010-05-26 11:04:10 -04007176/*
7177 * returns 1 when the nocow is safe, < 1 on error, 0 if the
7178 * block must be cow'd
7179 */
Josef Bacik00361582013-08-14 14:02:47 -04007180noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
Josef Bacik7ee9e442013-06-21 16:37:03 -04007181 u64 *orig_start, u64 *orig_block_len,
7182 u64 *ram_bytes)
Chris Mason46bfbb52010-05-26 11:04:10 -04007183{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007184 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason46bfbb52010-05-26 11:04:10 -04007185 struct btrfs_path *path;
7186 int ret;
7187 struct extent_buffer *leaf;
7188 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xie7b2b7082014-02-27 13:58:05 +08007189 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason46bfbb52010-05-26 11:04:10 -04007190 struct btrfs_file_extent_item *fi;
7191 struct btrfs_key key;
7192 u64 disk_bytenr;
7193 u64 backref_offset;
7194 u64 extent_end;
7195 u64 num_bytes;
7196 int slot;
7197 int found_type;
Josef Bacik7ee9e442013-06-21 16:37:03 -04007198 bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW);
Miao Xiee77751a2013-12-27 21:11:50 +08007199
Chris Mason46bfbb52010-05-26 11:04:10 -04007200 path = btrfs_alloc_path();
7201 if (!path)
7202 return -ENOMEM;
7203
David Sterbaf85b7372017-01-20 14:54:07 +01007204 ret = btrfs_lookup_file_extent(NULL, root, path,
7205 btrfs_ino(BTRFS_I(inode)), offset, 0);
Chris Mason46bfbb52010-05-26 11:04:10 -04007206 if (ret < 0)
7207 goto out;
7208
7209 slot = path->slots[0];
7210 if (ret == 1) {
7211 if (slot == 0) {
7212 /* can't find the item, must cow */
7213 ret = 0;
7214 goto out;
7215 }
7216 slot--;
7217 }
7218 ret = 0;
7219 leaf = path->nodes[0];
7220 btrfs_item_key_to_cpu(leaf, &key, slot);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02007221 if (key.objectid != btrfs_ino(BTRFS_I(inode)) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04007222 key.type != BTRFS_EXTENT_DATA_KEY) {
7223 /* not our file or wrong item type, must cow */
7224 goto out;
7225 }
7226
7227 if (key.offset > offset) {
7228 /* Wrong offset, must cow */
7229 goto out;
7230 }
7231
7232 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
7233 found_type = btrfs_file_extent_type(leaf, fi);
7234 if (found_type != BTRFS_FILE_EXTENT_REG &&
7235 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
7236 /* not a regular extent, must cow */
7237 goto out;
7238 }
Josef Bacik7ee9e442013-06-21 16:37:03 -04007239
7240 if (!nocow && found_type == BTRFS_FILE_EXTENT_REG)
7241 goto out;
7242
Miao Xiee77751a2013-12-27 21:11:50 +08007243 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
7244 if (extent_end <= offset)
7245 goto out;
7246
Chris Mason46bfbb52010-05-26 11:04:10 -04007247 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Josef Bacik7ee9e442013-06-21 16:37:03 -04007248 if (disk_bytenr == 0)
7249 goto out;
7250
7251 if (btrfs_file_extent_compression(leaf, fi) ||
7252 btrfs_file_extent_encryption(leaf, fi) ||
7253 btrfs_file_extent_other_encoding(leaf, fi))
7254 goto out;
7255
Ethan Lien78d42952018-05-17 14:58:29 +08007256 /*
7257 * Do the same check as in btrfs_cross_ref_exist but without the
7258 * unnecessary search.
7259 */
7260 if (btrfs_file_extent_generation(leaf, fi) <=
7261 btrfs_root_last_snapshot(&root->root_item))
7262 goto out;
7263
Chris Mason46bfbb52010-05-26 11:04:10 -04007264 backref_offset = btrfs_file_extent_offset(leaf, fi);
7265
Josef Bacik7ee9e442013-06-21 16:37:03 -04007266 if (orig_start) {
7267 *orig_start = key.offset - backref_offset;
7268 *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
7269 *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
7270 }
Josef Bacikeb384b52013-04-24 16:32:55 -04007271
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007272 if (btrfs_extent_readonly(fs_info, disk_bytenr))
Chris Mason46bfbb52010-05-26 11:04:10 -04007273 goto out;
Miao Xie7b2b7082014-02-27 13:58:05 +08007274
7275 num_bytes = min(offset + *len, extent_end) - offset;
7276 if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) {
7277 u64 range_end;
7278
Jeff Mahoneyda170662016-06-15 09:22:56 -04007279 range_end = round_up(offset + num_bytes,
7280 root->fs_info->sectorsize) - 1;
Miao Xie7b2b7082014-02-27 13:58:05 +08007281 ret = test_range_bit(io_tree, offset, range_end,
7282 EXTENT_DELALLOC, 0, NULL);
7283 if (ret) {
7284 ret = -EAGAIN;
7285 goto out;
7286 }
7287 }
7288
Josef Bacik1bda19e2013-10-18 12:10:36 -04007289 btrfs_release_path(path);
Chris Mason46bfbb52010-05-26 11:04:10 -04007290
7291 /*
7292 * look for other files referencing this extent, if we
7293 * find any we must cow
7294 */
Josef Bacik00361582013-08-14 14:02:47 -04007295
Liu Boe4c3b2d2017-01-30 12:25:28 -08007296 ret = btrfs_cross_ref_exist(root, btrfs_ino(BTRFS_I(inode)),
Josef Bacik00361582013-08-14 14:02:47 -04007297 key.offset - backref_offset, disk_bytenr);
Josef Bacik00361582013-08-14 14:02:47 -04007298 if (ret) {
7299 ret = 0;
7300 goto out;
7301 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007302
7303 /*
7304 * adjust disk_bytenr and num_bytes to cover just the bytes
7305 * in this extent we are about to write. If there
7306 * are any csums in that range we have to cow in order
7307 * to keep the csums correct
7308 */
7309 disk_bytenr += backref_offset;
7310 disk_bytenr += offset - key.offset;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007311 if (csum_exist_in_range(fs_info, disk_bytenr, num_bytes))
7312 goto out;
Chris Mason46bfbb52010-05-26 11:04:10 -04007313 /*
7314 * all of the above have passed, it is safe to overwrite this extent
7315 * without cow
7316 */
Josef Bacikeb384b52013-04-24 16:32:55 -04007317 *len = num_bytes;
Chris Mason46bfbb52010-05-26 11:04:10 -04007318 ret = 1;
7319out:
7320 btrfs_free_path(path);
7321 return ret;
7322}
7323
Josef Bacikeb838e72012-07-31 16:28:48 -04007324static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
7325 struct extent_state **cached_state, int writing)
7326{
7327 struct btrfs_ordered_extent *ordered;
7328 int ret = 0;
7329
7330 while (1) {
7331 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbaff13db42015-12-03 14:30:40 +01007332 cached_state);
Josef Bacikeb838e72012-07-31 16:28:48 -04007333 /*
7334 * We're concerned with the entire range that we're going to be
Nicholas D Steeves01327612016-05-19 21:18:45 -04007335 * doing DIO to, so we need to make sure there's no ordered
Josef Bacikeb838e72012-07-31 16:28:48 -04007336 * extents in this range.
7337 */
Nikolay Borisova776c6f2017-02-20 13:50:49 +02007338 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), lockstart,
Josef Bacikeb838e72012-07-31 16:28:48 -04007339 lockend - lockstart + 1);
7340
7341 /*
7342 * We need to make sure there are no buffered pages in this
7343 * range either, we could have raced between the invalidate in
7344 * generic_file_direct_write and locking the extent. The
7345 * invalidate needs to happen so that reads after a write do not
7346 * get stale data.
7347 */
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007348 if (!ordered &&
David Sterba051c98e2018-03-07 15:33:22 +01007349 (!writing || !filemap_range_has_page(inode->i_mapping,
7350 lockstart, lockend)))
Josef Bacikeb838e72012-07-31 16:28:48 -04007351 break;
7352
7353 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbae43bbe52017-12-12 21:43:52 +01007354 cached_state);
Josef Bacikeb838e72012-07-31 16:28:48 -04007355
7356 if (ordered) {
Filipe Mananaade77022016-02-18 14:28:55 +00007357 /*
7358 * If we are doing a DIO read and the ordered extent we
7359 * found is for a buffered write, we can not wait for it
7360 * to complete and retry, because if we do so we can
7361 * deadlock with concurrent buffered writes on page
7362 * locks. This happens only if our DIO read covers more
7363 * than one extent map, if at this point has already
7364 * created an ordered extent for a previous extent map
7365 * and locked its range in the inode's io tree, and a
7366 * concurrent write against that previous extent map's
7367 * range and this range started (we unlock the ranges
7368 * in the io tree only when the bios complete and
7369 * buffered writes always lock pages before attempting
7370 * to lock range in the io tree).
7371 */
7372 if (writing ||
7373 test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags))
7374 btrfs_start_ordered_extent(inode, ordered, 1);
7375 else
7376 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007377 btrfs_put_ordered_extent(ordered);
7378 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007379 /*
Filipe Mananab850ae12015-12-08 16:23:16 +00007380 * We could trigger writeback for this range (and wait
7381 * for it to complete) and then invalidate the pages for
7382 * this range (through invalidate_inode_pages2_range()),
7383 * but that can lead us to a deadlock with a concurrent
7384 * call to readpages() (a buffered read or a defrag call
7385 * triggered a readahead) on a page lock due to an
7386 * ordered dio extent we created before but did not have
7387 * yet a corresponding bio submitted (whence it can not
7388 * complete), which makes readpages() wait for that
7389 * ordered extent to complete while holding a lock on
7390 * that page.
Josef Bacikeb838e72012-07-31 16:28:48 -04007391 */
Filipe Mananab850ae12015-12-08 16:23:16 +00007392 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007393 }
7394
Filipe Mananaade77022016-02-18 14:28:55 +00007395 if (ret)
7396 break;
7397
Josef Bacikeb838e72012-07-31 16:28:48 -04007398 cond_resched();
7399 }
7400
7401 return ret;
7402}
7403
Liu Bo6f9994d2017-01-31 07:50:22 -08007404/* The callers of this must take lock_extent() */
7405static struct extent_map *create_io_em(struct inode *inode, u64 start, u64 len,
7406 u64 orig_start, u64 block_start,
7407 u64 block_len, u64 orig_block_len,
7408 u64 ram_bytes, int compress_type,
7409 int type)
Josef Bacik69ffb542012-09-11 15:40:07 -04007410{
7411 struct extent_map_tree *em_tree;
7412 struct extent_map *em;
7413 struct btrfs_root *root = BTRFS_I(inode)->root;
7414 int ret;
7415
Liu Bo6f9994d2017-01-31 07:50:22 -08007416 ASSERT(type == BTRFS_ORDERED_PREALLOC ||
7417 type == BTRFS_ORDERED_COMPRESSED ||
7418 type == BTRFS_ORDERED_NOCOW ||
Liu Bo1af4a0a2017-02-13 15:35:09 -08007419 type == BTRFS_ORDERED_REGULAR);
Liu Bo6f9994d2017-01-31 07:50:22 -08007420
Josef Bacik69ffb542012-09-11 15:40:07 -04007421 em_tree = &BTRFS_I(inode)->extent_tree;
7422 em = alloc_extent_map();
7423 if (!em)
7424 return ERR_PTR(-ENOMEM);
7425
7426 em->start = start;
7427 em->orig_start = orig_start;
7428 em->len = len;
7429 em->block_len = block_len;
7430 em->block_start = block_start;
7431 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacikb4939682012-12-03 10:31:19 -05007432 em->orig_block_len = orig_block_len;
Josef Bacikcc95bef2013-04-04 14:31:27 -04007433 em->ram_bytes = ram_bytes;
Josef Bacik70c8a912012-10-11 16:54:30 -04007434 em->generation = -1;
Josef Bacik69ffb542012-09-11 15:40:07 -04007435 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Liu Bo1af4a0a2017-02-13 15:35:09 -08007436 if (type == BTRFS_ORDERED_PREALLOC) {
Josef Bacikb11e2342012-12-03 10:58:15 -05007437 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Liu Bo1af4a0a2017-02-13 15:35:09 -08007438 } else if (type == BTRFS_ORDERED_COMPRESSED) {
Liu Bo6f9994d2017-01-31 07:50:22 -08007439 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
7440 em->compress_type = compress_type;
7441 }
Josef Bacik69ffb542012-09-11 15:40:07 -04007442
7443 do {
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02007444 btrfs_drop_extent_cache(BTRFS_I(inode), em->start,
Josef Bacik69ffb542012-09-11 15:40:07 -04007445 em->start + em->len - 1, 0);
7446 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04007447 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik69ffb542012-09-11 15:40:07 -04007448 write_unlock(&em_tree->lock);
Liu Bo6f9994d2017-01-31 07:50:22 -08007449 /*
7450 * The caller has taken lock_extent(), who could race with us
7451 * to add em?
7452 */
Josef Bacik69ffb542012-09-11 15:40:07 -04007453 } while (ret == -EEXIST);
7454
7455 if (ret) {
7456 free_extent_map(em);
7457 return ERR_PTR(ret);
7458 }
7459
Liu Bo6f9994d2017-01-31 07:50:22 -08007460 /* em got 2 refs now, callers needs to do free_extent_map once. */
Josef Bacik69ffb542012-09-11 15:40:07 -04007461 return em;
7462}
7463
Nikolay Borisov1c8d0172018-05-02 15:19:32 +03007464
7465static int btrfs_get_blocks_direct_read(struct extent_map *em,
7466 struct buffer_head *bh_result,
7467 struct inode *inode,
7468 u64 start, u64 len)
7469{
7470 if (em->block_start == EXTENT_MAP_HOLE ||
7471 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7472 return -ENOENT;
7473
7474 len = min(len, em->len - (start - em->start));
7475
7476 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
7477 inode->i_blkbits;
7478 bh_result->b_size = len;
7479 bh_result->b_bdev = em->bdev;
7480 set_buffer_mapped(bh_result);
7481
7482 return 0;
7483}
7484
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007485static int btrfs_get_blocks_direct_write(struct extent_map **map,
7486 struct buffer_head *bh_result,
7487 struct inode *inode,
7488 struct btrfs_dio_data *dio_data,
7489 u64 start, u64 len)
7490{
7491 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
7492 struct extent_map *em = *map;
7493 int ret = 0;
7494
7495 /*
7496 * We don't allocate a new extent in the following cases
7497 *
7498 * 1) The inode is marked as NODATACOW. In this case we'll just use the
7499 * existing extent.
7500 * 2) The extent is marked as PREALLOC. We're good to go here and can
7501 * just use the extent.
7502 *
7503 */
7504 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
7505 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
7506 em->block_start != EXTENT_MAP_HOLE)) {
7507 int type;
7508 u64 block_start, orig_start, orig_block_len, ram_bytes;
7509
7510 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7511 type = BTRFS_ORDERED_PREALLOC;
7512 else
7513 type = BTRFS_ORDERED_NOCOW;
7514 len = min(len, em->len - (start - em->start));
7515 block_start = em->block_start + (start - em->start);
7516
7517 if (can_nocow_extent(inode, start, &len, &orig_start,
7518 &orig_block_len, &ram_bytes) == 1 &&
7519 btrfs_inc_nocow_writers(fs_info, block_start)) {
7520 struct extent_map *em2;
7521
7522 em2 = btrfs_create_dio_extent(inode, start, len,
7523 orig_start, block_start,
7524 len, orig_block_len,
7525 ram_bytes, type);
7526 btrfs_dec_nocow_writers(fs_info, block_start);
7527 if (type == BTRFS_ORDERED_PREALLOC) {
7528 free_extent_map(em);
7529 *map = em = em2;
7530 }
7531
7532 if (em2 && IS_ERR(em2)) {
7533 ret = PTR_ERR(em2);
7534 goto out;
7535 }
7536 /*
7537 * For inode marked NODATACOW or extent marked PREALLOC,
7538 * use the existing or preallocated extent, so does not
7539 * need to adjust btrfs_space_info's bytes_may_use.
7540 */
7541 btrfs_free_reserved_data_space_noquota(inode, start,
7542 len);
7543 goto skip_cow;
7544 }
7545 }
7546
7547 /* this will cow the extent */
7548 len = bh_result->b_size;
7549 free_extent_map(em);
7550 *map = em = btrfs_new_extent_direct(inode, start, len);
7551 if (IS_ERR(em)) {
7552 ret = PTR_ERR(em);
7553 goto out;
7554 }
7555
7556 len = min(len, em->len - (start - em->start));
7557
7558skip_cow:
7559 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
7560 inode->i_blkbits;
7561 bh_result->b_size = len;
7562 bh_result->b_bdev = em->bdev;
7563 set_buffer_mapped(bh_result);
7564
7565 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7566 set_buffer_new(bh_result);
7567
7568 /*
7569 * Need to update the i_size under the extent lock so buffered
7570 * readers will get the updated i_size when we unlock.
7571 */
7572 if (!dio_data->overwrite && start + len > i_size_read(inode))
7573 i_size_write(inode, start + len);
7574
7575 WARN_ON(dio_data->reserve < len);
7576 dio_data->reserve -= len;
7577 dio_data->unsubmitted_oe_range_end = start + len;
7578 current->journal_info = dio_data;
7579out:
7580 return ret;
7581}
7582
Josef Bacik4b46fce2010-05-23 11:00:55 -04007583static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
7584 struct buffer_head *bh_result, int create)
7585{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007586 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007587 struct extent_map *em;
Josef Bacikeb838e72012-07-31 16:28:48 -04007588 struct extent_state *cached_state = NULL;
chandan50745b02015-08-28 21:10:13 +05307589 struct btrfs_dio_data *dio_data = NULL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007590 u64 start = iblock << inode->i_blkbits;
Josef Bacikeb838e72012-07-31 16:28:48 -04007591 u64 lockstart, lockend;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007592 u64 len = bh_result->b_size;
Josef Bacikeb838e72012-07-31 16:28:48 -04007593 int unlock_bits = EXTENT_LOCKED;
Miao Xie09348562013-02-07 10:12:07 +00007594 int ret = 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007595
Miao Xie172a5042013-02-21 02:48:22 -07007596 if (create)
Josef Bacik32667892015-02-11 15:08:58 -05007597 unlock_bits |= EXTENT_DIRTY;
Miao Xie172a5042013-02-21 02:48:22 -07007598 else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007599 len = min_t(u64, len, fs_info->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04007600
Josef Bacikc3298612012-08-03 16:49:19 -04007601 lockstart = start;
7602 lockend = start + len - 1;
7603
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007604 if (current->journal_info) {
7605 /*
7606 * Need to pull our outstanding extents and set journal_info to NULL so
Nicholas D Steeves01327612016-05-19 21:18:45 -04007607 * that anything that needs to check if there's a transaction doesn't get
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007608 * confused.
7609 */
chandan50745b02015-08-28 21:10:13 +05307610 dio_data = current->journal_info;
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007611 current->journal_info = NULL;
7612 }
7613
Josef Bacikeb838e72012-07-31 16:28:48 -04007614 /*
7615 * If this errors out it's because we couldn't invalidate pagecache for
7616 * this range and we need to fallback to buffered.
7617 */
Filipe Manana9c9464c2015-11-04 09:52:04 +00007618 if (lock_extent_direct(inode, lockstart, lockend, &cached_state,
7619 create)) {
7620 ret = -ENOTBLK;
7621 goto err;
7622 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007623
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007624 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len, 0);
Josef Bacikeb838e72012-07-31 16:28:48 -04007625 if (IS_ERR(em)) {
7626 ret = PTR_ERR(em);
7627 goto unlock_err;
7628 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007629
7630 /*
7631 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
7632 * io. INLINE is special, and we could probably kludge it in here, but
7633 * it's still buffered so for safety lets just fall back to the generic
7634 * buffered path.
7635 *
7636 * For COMPRESSED we _have_ to read the entire extent in so we can
7637 * decompress it, so there will be buffering required no matter what we
7638 * do, so go ahead and fallback to buffered.
7639 *
Nicholas D Steeves01327612016-05-19 21:18:45 -04007640 * We return -ENOTBLK because that's what makes DIO go ahead and go back
Josef Bacik4b46fce2010-05-23 11:00:55 -04007641 * to buffered IO. Don't blame me, this is the price we pay for using
7642 * the generic code.
7643 */
7644 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
7645 em->block_start == EXTENT_MAP_INLINE) {
7646 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007647 ret = -ENOTBLK;
7648 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007649 }
7650
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007651 if (create) {
7652 ret = btrfs_get_blocks_direct_write(&em, bh_result, inode,
7653 dio_data, start, len);
7654 if (ret < 0)
7655 goto unlock_err;
7656
7657 /* clear and unlock the entire range */
7658 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7659 unlock_bits, 1, 0, &cached_state);
7660 } else {
Nikolay Borisov1c8d0172018-05-02 15:19:32 +03007661 ret = btrfs_get_blocks_direct_read(em, bh_result, inode,
7662 start, len);
7663 /* Can be negative only if we read from a hole */
7664 if (ret < 0) {
7665 ret = 0;
7666 free_extent_map(em);
7667 goto unlock_err;
7668 }
7669 /*
7670 * We need to unlock only the end area that we aren't using.
7671 * The rest is going to be unlocked by the endio routine.
7672 */
7673 lockstart = start + bh_result->b_size;
7674 if (lockstart < lockend) {
7675 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
7676 lockend, unlock_bits, 1, 0,
7677 &cached_state);
7678 } else {
7679 free_extent_state(cached_state);
7680 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007681 }
7682
Josef Bacik4b46fce2010-05-23 11:00:55 -04007683 free_extent_map(em);
7684
7685 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007686
7687unlock_err:
Josef Bacikeb838e72012-07-31 16:28:48 -04007688 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbaae0f1622017-10-31 16:37:52 +01007689 unlock_bits, 1, 0, &cached_state);
Filipe Manana9c9464c2015-11-04 09:52:04 +00007690err:
chandan50745b02015-08-28 21:10:13 +05307691 if (dio_data)
7692 current->journal_info = dio_data;
Josef Bacikeb838e72012-07-31 16:28:48 -04007693 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007694}
7695
Omar Sandoval58efbc92017-08-22 23:45:59 -07007696static inline blk_status_t submit_dio_repair_bio(struct inode *inode,
7697 struct bio *bio,
7698 int mirror_num)
Miao Xie8b110e32014-09-12 18:44:03 +08007699{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007700 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Omar Sandoval58efbc92017-08-22 23:45:59 -07007701 blk_status_t ret;
Miao Xie8b110e32014-09-12 18:44:03 +08007702
Mike Christie37226b22016-06-05 14:31:52 -05007703 BUG_ON(bio_op(bio) == REQ_OP_WRITE);
Miao Xie8b110e32014-09-12 18:44:03 +08007704
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007705 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DIO_REPAIR);
Miao Xie8b110e32014-09-12 18:44:03 +08007706 if (ret)
Nikolay Borisovea057f62017-12-13 10:25:38 +02007707 return ret;
Miao Xie8b110e32014-09-12 18:44:03 +08007708
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007709 ret = btrfs_map_bio(fs_info, bio, mirror_num, 0);
Nikolay Borisovea057f62017-12-13 10:25:38 +02007710
Miao Xie8b110e32014-09-12 18:44:03 +08007711 return ret;
7712}
7713
7714static int btrfs_check_dio_repairable(struct inode *inode,
7715 struct bio *failed_bio,
7716 struct io_failure_record *failrec,
7717 int failed_mirror)
7718{
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007719 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xie8b110e32014-09-12 18:44:03 +08007720 int num_copies;
7721
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007722 num_copies = btrfs_num_copies(fs_info, failrec->logical, failrec->len);
Miao Xie8b110e32014-09-12 18:44:03 +08007723 if (num_copies == 1) {
7724 /*
7725 * we only have a single copy of the data, so don't bother with
7726 * all the retry and error correction code that follows. no
7727 * matter what the error is, it is very likely to persist.
7728 */
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007729 btrfs_debug(fs_info,
7730 "Check DIO Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d",
7731 num_copies, failrec->this_mirror, failed_mirror);
Miao Xie8b110e32014-09-12 18:44:03 +08007732 return 0;
7733 }
7734
7735 failrec->failed_mirror = failed_mirror;
7736 failrec->this_mirror++;
7737 if (failrec->this_mirror == failed_mirror)
7738 failrec->this_mirror++;
7739
7740 if (failrec->this_mirror > num_copies) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007741 btrfs_debug(fs_info,
7742 "Check DIO Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d",
7743 num_copies, failrec->this_mirror, failed_mirror);
Miao Xie8b110e32014-09-12 18:44:03 +08007744 return 0;
7745 }
7746
7747 return 1;
7748}
7749
Omar Sandoval58efbc92017-08-22 23:45:59 -07007750static blk_status_t dio_read_error(struct inode *inode, struct bio *failed_bio,
7751 struct page *page, unsigned int pgoff,
7752 u64 start, u64 end, int failed_mirror,
7753 bio_end_io_t *repair_endio, void *repair_arg)
Miao Xie8b110e32014-09-12 18:44:03 +08007754{
7755 struct io_failure_record *failrec;
Josef Bacik7870d082017-05-05 11:57:15 -04007756 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
7757 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
Miao Xie8b110e32014-09-12 18:44:03 +08007758 struct bio *bio;
7759 int isector;
David Sterbaf1c77c52017-06-06 19:03:49 +02007760 unsigned int read_mode = 0;
Liu Bo17347ce2017-05-15 15:33:27 -07007761 int segs;
Miao Xie8b110e32014-09-12 18:44:03 +08007762 int ret;
Omar Sandoval58efbc92017-08-22 23:45:59 -07007763 blk_status_t status;
Ming Leic16a8ac2017-12-18 20:22:12 +08007764 struct bio_vec bvec;
Miao Xie8b110e32014-09-12 18:44:03 +08007765
Mike Christie37226b22016-06-05 14:31:52 -05007766 BUG_ON(bio_op(failed_bio) == REQ_OP_WRITE);
Miao Xie8b110e32014-09-12 18:44:03 +08007767
7768 ret = btrfs_get_io_failure_record(inode, start, end, &failrec);
7769 if (ret)
Omar Sandoval58efbc92017-08-22 23:45:59 -07007770 return errno_to_blk_status(ret);
Miao Xie8b110e32014-09-12 18:44:03 +08007771
7772 ret = btrfs_check_dio_repairable(inode, failed_bio, failrec,
7773 failed_mirror);
7774 if (!ret) {
Josef Bacik7870d082017-05-05 11:57:15 -04007775 free_io_failure(failure_tree, io_tree, failrec);
Omar Sandoval58efbc92017-08-22 23:45:59 -07007776 return BLK_STS_IOERR;
Miao Xie8b110e32014-09-12 18:44:03 +08007777 }
7778
Liu Bo17347ce2017-05-15 15:33:27 -07007779 segs = bio_segments(failed_bio);
Ming Leic16a8ac2017-12-18 20:22:12 +08007780 bio_get_first_bvec(failed_bio, &bvec);
Liu Bo17347ce2017-05-15 15:33:27 -07007781 if (segs > 1 ||
Ming Leic16a8ac2017-12-18 20:22:12 +08007782 (bvec.bv_len > btrfs_inode_sectorsize(inode)))
Christoph Hellwig70fd7612016-11-01 07:40:10 -06007783 read_mode |= REQ_FAILFAST_DEV;
Miao Xie8b110e32014-09-12 18:44:03 +08007784
7785 isector = start - btrfs_io_bio(failed_bio)->logical;
7786 isector >>= inode->i_sb->s_blocksize_bits;
7787 bio = btrfs_create_repair_bio(inode, failed_bio, failrec, page,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307788 pgoff, isector, repair_endio, repair_arg);
David Sterbaebcc3262018-06-29 10:56:53 +02007789 bio->bi_opf = REQ_OP_READ | read_mode;
Miao Xie8b110e32014-09-12 18:44:03 +08007790
7791 btrfs_debug(BTRFS_I(inode)->root->fs_info,
David Sterba913e1532017-07-13 15:32:18 +02007792 "repair DIO read error: submitting new dio read[%#x] to this_mirror=%d, in_validation=%d",
Miao Xie8b110e32014-09-12 18:44:03 +08007793 read_mode, failrec->this_mirror, failrec->in_validation);
7794
Omar Sandoval58efbc92017-08-22 23:45:59 -07007795 status = submit_dio_repair_bio(inode, bio, failrec->this_mirror);
7796 if (status) {
Josef Bacik7870d082017-05-05 11:57:15 -04007797 free_io_failure(failure_tree, io_tree, failrec);
Miao Xie8b110e32014-09-12 18:44:03 +08007798 bio_put(bio);
7799 }
7800
Omar Sandoval58efbc92017-08-22 23:45:59 -07007801 return status;
Miao Xie8b110e32014-09-12 18:44:03 +08007802}
7803
7804struct btrfs_retry_complete {
7805 struct completion done;
7806 struct inode *inode;
7807 u64 start;
7808 int uptodate;
7809};
7810
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007811static void btrfs_retry_endio_nocsum(struct bio *bio)
Miao Xie8b110e32014-09-12 18:44:03 +08007812{
7813 struct btrfs_retry_complete *done = bio->bi_private;
Josef Bacik7870d082017-05-05 11:57:15 -04007814 struct inode *inode = done->inode;
Miao Xie8b110e32014-09-12 18:44:03 +08007815 struct bio_vec *bvec;
Josef Bacik7870d082017-05-05 11:57:15 -04007816 struct extent_io_tree *io_tree, *failure_tree;
Miao Xie8b110e32014-09-12 18:44:03 +08007817 int i;
7818
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02007819 if (bio->bi_status)
Miao Xie8b110e32014-09-12 18:44:03 +08007820 goto end;
7821
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307822 ASSERT(bio->bi_vcnt == 1);
Josef Bacik7870d082017-05-05 11:57:15 -04007823 io_tree = &BTRFS_I(inode)->io_tree;
7824 failure_tree = &BTRFS_I(inode)->io_failure_tree;
Ming Lei263663c2017-12-18 20:22:04 +08007825 ASSERT(bio_first_bvec_all(bio)->bv_len == btrfs_inode_sectorsize(inode));
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307826
Miao Xie8b110e32014-09-12 18:44:03 +08007827 done->uptodate = 1;
David Sterbac09abff2017-07-13 18:10:07 +02007828 ASSERT(!bio_flagged(bio, BIO_CLONED));
Miao Xie8b110e32014-09-12 18:44:03 +08007829 bio_for_each_segment_all(bvec, bio, i)
Josef Bacik7870d082017-05-05 11:57:15 -04007830 clean_io_failure(BTRFS_I(inode)->root->fs_info, failure_tree,
7831 io_tree, done->start, bvec->bv_page,
7832 btrfs_ino(BTRFS_I(inode)), 0);
Miao Xie8b110e32014-09-12 18:44:03 +08007833end:
7834 complete(&done->done);
7835 bio_put(bio);
7836}
7837
Omar Sandoval58efbc92017-08-22 23:45:59 -07007838static blk_status_t __btrfs_correct_data_nocsum(struct inode *inode,
7839 struct btrfs_io_bio *io_bio)
Josef Bacik4b46fce2010-05-23 11:00:55 -04007840{
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307841 struct btrfs_fs_info *fs_info;
Liu Bo17347ce2017-05-15 15:33:27 -07007842 struct bio_vec bvec;
7843 struct bvec_iter iter;
Miao Xie8b110e32014-09-12 18:44:03 +08007844 struct btrfs_retry_complete done;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007845 u64 start;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307846 unsigned int pgoff;
7847 u32 sectorsize;
7848 int nr_sectors;
Omar Sandoval58efbc92017-08-22 23:45:59 -07007849 blk_status_t ret;
7850 blk_status_t err = BLK_STS_OK;
Miao Xiedc380ae2014-09-12 18:43:55 +08007851
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307852 fs_info = BTRFS_I(inode)->root->fs_info;
Jeff Mahoneyda170662016-06-15 09:22:56 -04007853 sectorsize = fs_info->sectorsize;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307854
Miao Xiec1dc0892014-09-12 18:43:56 +08007855 start = io_bio->logical;
Miao Xie8b110e32014-09-12 18:44:03 +08007856 done.inode = inode;
Liu Bo17347ce2017-05-15 15:33:27 -07007857 io_bio->bio.bi_iter = io_bio->iter;
Miao Xie8b110e32014-09-12 18:44:03 +08007858
Liu Bo17347ce2017-05-15 15:33:27 -07007859 bio_for_each_segment(bvec, &io_bio->bio, iter) {
7860 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec.bv_len);
7861 pgoff = bvec.bv_offset;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307862
7863next_block_or_try_again:
Miao Xie8b110e32014-09-12 18:44:03 +08007864 done.uptodate = 0;
7865 done.start = start;
7866 init_completion(&done.done);
7867
Liu Bo17347ce2017-05-15 15:33:27 -07007868 ret = dio_read_error(inode, &io_bio->bio, bvec.bv_page,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307869 pgoff, start, start + sectorsize - 1,
7870 io_bio->mirror_num,
7871 btrfs_retry_endio_nocsum, &done);
Liu Bo629ebf42017-05-15 17:20:07 -07007872 if (ret) {
7873 err = ret;
7874 goto next;
7875 }
Miao Xie8b110e32014-09-12 18:44:03 +08007876
David Sterba9c17f6c2017-07-19 19:26:45 +02007877 wait_for_completion_io(&done.done);
Miao Xie8b110e32014-09-12 18:44:03 +08007878
7879 if (!done.uptodate) {
7880 /* We might have another mirror, so try again */
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307881 goto next_block_or_try_again;
Miao Xie8b110e32014-09-12 18:44:03 +08007882 }
7883
Liu Bo629ebf42017-05-15 17:20:07 -07007884next:
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307885 start += sectorsize;
7886
Liu Bo97bf5a52017-04-07 13:11:10 -07007887 nr_sectors--;
7888 if (nr_sectors) {
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307889 pgoff += sectorsize;
Liu Bo97bf5a52017-04-07 13:11:10 -07007890 ASSERT(pgoff < PAGE_SIZE);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307891 goto next_block_or_try_again;
7892 }
Miao Xie8b110e32014-09-12 18:44:03 +08007893 }
7894
Liu Bo629ebf42017-05-15 17:20:07 -07007895 return err;
Miao Xie8b110e32014-09-12 18:44:03 +08007896}
7897
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007898static void btrfs_retry_endio(struct bio *bio)
Miao Xie8b110e32014-09-12 18:44:03 +08007899{
7900 struct btrfs_retry_complete *done = bio->bi_private;
7901 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
Josef Bacik7870d082017-05-05 11:57:15 -04007902 struct extent_io_tree *io_tree, *failure_tree;
7903 struct inode *inode = done->inode;
Miao Xie8b110e32014-09-12 18:44:03 +08007904 struct bio_vec *bvec;
7905 int uptodate;
7906 int ret;
7907 int i;
7908
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02007909 if (bio->bi_status)
Miao Xie8b110e32014-09-12 18:44:03 +08007910 goto end;
7911
7912 uptodate = 1;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307913
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307914 ASSERT(bio->bi_vcnt == 1);
Ming Lei263663c2017-12-18 20:22:04 +08007915 ASSERT(bio_first_bvec_all(bio)->bv_len == btrfs_inode_sectorsize(done->inode));
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307916
Josef Bacik7870d082017-05-05 11:57:15 -04007917 io_tree = &BTRFS_I(inode)->io_tree;
7918 failure_tree = &BTRFS_I(inode)->io_failure_tree;
7919
David Sterbac09abff2017-07-13 18:10:07 +02007920 ASSERT(!bio_flagged(bio, BIO_CLONED));
Miao Xie8b110e32014-09-12 18:44:03 +08007921 bio_for_each_segment_all(bvec, bio, i) {
Josef Bacik7870d082017-05-05 11:57:15 -04007922 ret = __readpage_endio_check(inode, io_bio, i, bvec->bv_page,
7923 bvec->bv_offset, done->start,
7924 bvec->bv_len);
Miao Xie8b110e32014-09-12 18:44:03 +08007925 if (!ret)
Josef Bacik7870d082017-05-05 11:57:15 -04007926 clean_io_failure(BTRFS_I(inode)->root->fs_info,
7927 failure_tree, io_tree, done->start,
7928 bvec->bv_page,
7929 btrfs_ino(BTRFS_I(inode)),
7930 bvec->bv_offset);
Miao Xie8b110e32014-09-12 18:44:03 +08007931 else
7932 uptodate = 0;
7933 }
7934
7935 done->uptodate = uptodate;
7936end:
7937 complete(&done->done);
7938 bio_put(bio);
7939}
7940
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02007941static blk_status_t __btrfs_subio_endio_read(struct inode *inode,
7942 struct btrfs_io_bio *io_bio, blk_status_t err)
Miao Xie8b110e32014-09-12 18:44:03 +08007943{
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307944 struct btrfs_fs_info *fs_info;
Liu Bo17347ce2017-05-15 15:33:27 -07007945 struct bio_vec bvec;
7946 struct bvec_iter iter;
Miao Xie8b110e32014-09-12 18:44:03 +08007947 struct btrfs_retry_complete done;
7948 u64 start;
7949 u64 offset = 0;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307950 u32 sectorsize;
7951 int nr_sectors;
7952 unsigned int pgoff;
7953 int csum_pos;
Liu Boef7cdac12017-04-13 18:11:48 -07007954 bool uptodate = (err == 0);
Miao Xie8b110e32014-09-12 18:44:03 +08007955 int ret;
Omar Sandoval58efbc92017-08-22 23:45:59 -07007956 blk_status_t status;
Miao Xie8b110e32014-09-12 18:44:03 +08007957
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307958 fs_info = BTRFS_I(inode)->root->fs_info;
Jeff Mahoneyda170662016-06-15 09:22:56 -04007959 sectorsize = fs_info->sectorsize;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307960
Omar Sandoval58efbc92017-08-22 23:45:59 -07007961 err = BLK_STS_OK;
Miao Xie8b110e32014-09-12 18:44:03 +08007962 start = io_bio->logical;
7963 done.inode = inode;
Liu Bo17347ce2017-05-15 15:33:27 -07007964 io_bio->bio.bi_iter = io_bio->iter;
Miao Xie8b110e32014-09-12 18:44:03 +08007965
Liu Bo17347ce2017-05-15 15:33:27 -07007966 bio_for_each_segment(bvec, &io_bio->bio, iter) {
7967 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec.bv_len);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307968
Liu Bo17347ce2017-05-15 15:33:27 -07007969 pgoff = bvec.bv_offset;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307970next_block:
Liu Boef7cdac12017-04-13 18:11:48 -07007971 if (uptodate) {
7972 csum_pos = BTRFS_BYTES_TO_BLKS(fs_info, offset);
7973 ret = __readpage_endio_check(inode, io_bio, csum_pos,
7974 bvec.bv_page, pgoff, start, sectorsize);
7975 if (likely(!ret))
7976 goto next;
7977 }
Miao Xie8b110e32014-09-12 18:44:03 +08007978try_again:
7979 done.uptodate = 0;
7980 done.start = start;
7981 init_completion(&done.done);
7982
Omar Sandoval58efbc92017-08-22 23:45:59 -07007983 status = dio_read_error(inode, &io_bio->bio, bvec.bv_page,
7984 pgoff, start, start + sectorsize - 1,
7985 io_bio->mirror_num, btrfs_retry_endio,
7986 &done);
7987 if (status) {
7988 err = status;
Miao Xie8b110e32014-09-12 18:44:03 +08007989 goto next;
7990 }
7991
David Sterba9c17f6c2017-07-19 19:26:45 +02007992 wait_for_completion_io(&done.done);
Miao Xie8b110e32014-09-12 18:44:03 +08007993
7994 if (!done.uptodate) {
7995 /* We might have another mirror, so try again */
7996 goto try_again;
7997 }
7998next:
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307999 offset += sectorsize;
8000 start += sectorsize;
8001
8002 ASSERT(nr_sectors);
8003
Liu Bo97bf5a52017-04-07 13:11:10 -07008004 nr_sectors--;
8005 if (nr_sectors) {
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308006 pgoff += sectorsize;
Liu Bo97bf5a52017-04-07 13:11:10 -07008007 ASSERT(pgoff < PAGE_SIZE);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308008 goto next_block;
8009 }
Kent Overstreet2c30c712013-11-07 12:20:26 -08008010 }
Miao Xiec1dc0892014-09-12 18:43:56 +08008011
8012 return err;
8013}
8014
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008015static blk_status_t btrfs_subio_endio_read(struct inode *inode,
8016 struct btrfs_io_bio *io_bio, blk_status_t err)
Miao Xie8b110e32014-09-12 18:44:03 +08008017{
8018 bool skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
8019
8020 if (skip_csum) {
8021 if (unlikely(err))
8022 return __btrfs_correct_data_nocsum(inode, io_bio);
8023 else
Omar Sandoval58efbc92017-08-22 23:45:59 -07008024 return BLK_STS_OK;
Miao Xie8b110e32014-09-12 18:44:03 +08008025 } else {
8026 return __btrfs_subio_endio_read(inode, io_bio, err);
8027 }
8028}
8029
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008030static void btrfs_endio_direct_read(struct bio *bio)
Miao Xiec1dc0892014-09-12 18:43:56 +08008031{
8032 struct btrfs_dio_private *dip = bio->bi_private;
8033 struct inode *inode = dip->inode;
8034 struct bio *dio_bio;
8035 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008036 blk_status_t err = bio->bi_status;
Miao Xiec1dc0892014-09-12 18:43:56 +08008037
Liu Bo99c4e3b2017-09-15 15:06:51 -06008038 if (dip->flags & BTRFS_DIO_ORIG_BIO_SUBMITTED)
Miao Xie8b110e32014-09-12 18:44:03 +08008039 err = btrfs_subio_endio_read(inode, io_bio, err);
Miao Xiec1dc0892014-09-12 18:43:56 +08008040
Josef Bacik4b46fce2010-05-23 11:00:55 -04008041 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01008042 dip->logical_offset + dip->bytes - 1);
Chris Mason9be33952013-05-17 18:30:14 -04008043 dio_bio = dip->dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008044
Josef Bacik4b46fce2010-05-23 11:00:55 -04008045 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04008046
Liu Bo99c4e3b2017-09-15 15:06:51 -06008047 dio_bio->bi_status = err;
Christoph Hellwig40553512017-06-03 09:37:58 +02008048 dio_end_io(dio_bio);
Miao Xie23ea8e52014-09-12 18:43:54 +08008049
8050 if (io_bio->end_io)
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008051 io_bio->end_io(io_bio, blk_status_to_errno(err));
Chris Mason9be33952013-05-17 18:30:14 -04008052 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008053}
8054
Qu Wenruo524272602017-03-08 10:25:52 +08008055static void __endio_write_update_ordered(struct inode *inode,
8056 const u64 offset, const u64 bytes,
8057 const bool uptodate)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008058{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008059 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008060 struct btrfs_ordered_extent *ordered = NULL;
Qu Wenruo524272602017-03-08 10:25:52 +08008061 struct btrfs_workqueue *wq;
8062 btrfs_work_func_t func;
Filipe Manana14543772015-11-24 16:23:54 +00008063 u64 ordered_offset = offset;
8064 u64 ordered_bytes = bytes;
Naohiro Aota67c003f2017-09-01 17:59:07 +09008065 u64 last_offset;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008066
Qu Wenruo524272602017-03-08 10:25:52 +08008067 if (btrfs_is_free_space_inode(BTRFS_I(inode))) {
8068 wq = fs_info->endio_freespace_worker;
8069 func = btrfs_freespace_write_helper;
8070 } else {
8071 wq = fs_info->endio_write_workers;
8072 func = btrfs_endio_write_helper;
8073 }
8074
Nikolay Borisovb25f0d02018-04-11 11:21:17 +03008075 while (ordered_offset < offset + bytes) {
8076 last_offset = ordered_offset;
8077 if (btrfs_dec_test_first_ordered_pending(inode, &ordered,
8078 &ordered_offset,
8079 ordered_bytes,
8080 uptodate)) {
8081 btrfs_init_work(&ordered->work, func,
8082 finish_ordered_fn,
8083 NULL, NULL);
8084 btrfs_queue_work(wq, &ordered->work);
8085 }
8086 /*
8087 * If btrfs_dec_test_ordered_pending does not find any ordered
8088 * extent in the range, we can exit.
8089 */
8090 if (ordered_offset == last_offset)
8091 return;
8092 /*
8093 * Our bio might span multiple ordered extents. In this case
8094 * we keep goin until we have accounted the whole dio.
8095 */
8096 if (ordered_offset < offset + bytes) {
8097 ordered_bytes = offset + bytes - ordered_offset;
8098 ordered = NULL;
8099 }
Chris Mason163cf092010-11-28 19:56:33 -05008100 }
Filipe Manana14543772015-11-24 16:23:54 +00008101}
8102
8103static void btrfs_endio_direct_write(struct bio *bio)
8104{
8105 struct btrfs_dio_private *dip = bio->bi_private;
8106 struct bio *dio_bio = dip->dio_bio;
8107
Qu Wenruo524272602017-03-08 10:25:52 +08008108 __endio_write_update_ordered(dip->inode, dip->logical_offset,
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008109 dip->bytes, !bio->bi_status);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008110
Josef Bacik4b46fce2010-05-23 11:00:55 -04008111 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04008112
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008113 dio_bio->bi_status = bio->bi_status;
Christoph Hellwig40553512017-06-03 09:37:58 +02008114 dio_end_io(dio_bio);
Chris Mason9be33952013-05-17 18:30:14 -04008115 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008116}
8117
David Sterbad0ee3932018-03-08 14:35:48 +01008118static blk_status_t btrfs_submit_bio_start_direct_io(void *private_data,
David Sterbad0779292018-03-08 13:47:33 +01008119 struct bio *bio, u64 offset)
Chris Masoneaf25d92010-05-25 09:48:28 -04008120{
Josef Bacikc6100a42017-05-05 11:57:13 -04008121 struct inode *inode = private_data;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008122 blk_status_t ret;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008123 ret = btrfs_csum_one_bio(inode, bio, offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008124 BUG_ON(ret); /* -ENOMEM */
Chris Masoneaf25d92010-05-25 09:48:28 -04008125 return 0;
8126}
8127
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008128static void btrfs_end_dio_bio(struct bio *bio)
Miao Xiee65e1532010-11-22 03:04:43 +00008129{
8130 struct btrfs_dio_private *dip = bio->bi_private;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008131 blk_status_t err = bio->bi_status;
Miao Xiee65e1532010-11-22 03:04:43 +00008132
Miao Xie8b110e32014-09-12 18:44:03 +08008133 if (err)
8134 btrfs_warn(BTRFS_I(dip->inode)->root->fs_info,
Mike Christie6296b962016-06-05 14:32:21 -05008135 "direct IO failed ino %llu rw %d,%u sector %#Lx len %u err no %d",
David Sterbaf85b7372017-01-20 14:54:07 +01008136 btrfs_ino(BTRFS_I(dip->inode)), bio_op(bio),
8137 bio->bi_opf,
Miao Xie8b110e32014-09-12 18:44:03 +08008138 (unsigned long long)bio->bi_iter.bi_sector,
8139 bio->bi_iter.bi_size, err);
8140
8141 if (dip->subio_endio)
8142 err = dip->subio_endio(dip->inode, btrfs_io_bio(bio), err);
Miao Xiec1dc0892014-09-12 18:43:56 +08008143
8144 if (err) {
Miao Xiee65e1532010-11-22 03:04:43 +00008145 /*
Nikolay Borisovde224b72018-02-14 10:53:36 +02008146 * We want to perceive the errors flag being set before
8147 * decrementing the reference count. We don't need a barrier
8148 * since atomic operations with a return value are fully
8149 * ordered as per atomic_t.txt
Miao Xiee65e1532010-11-22 03:04:43 +00008150 */
Nikolay Borisovde224b72018-02-14 10:53:36 +02008151 dip->errors = 1;
Miao Xiee65e1532010-11-22 03:04:43 +00008152 }
8153
8154 /* if there are more bios still pending for this dio, just exit */
8155 if (!atomic_dec_and_test(&dip->pending_bios))
8156 goto out;
8157
Chris Mason9be33952013-05-17 18:30:14 -04008158 if (dip->errors) {
Miao Xiee65e1532010-11-22 03:04:43 +00008159 bio_io_error(dip->orig_bio);
Chris Mason9be33952013-05-17 18:30:14 -04008160 } else {
Anand Jain2dbe0c72017-10-14 08:35:56 +08008161 dip->dio_bio->bi_status = BLK_STS_OK;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008162 bio_endio(dip->orig_bio);
Miao Xiee65e1532010-11-22 03:04:43 +00008163 }
8164out:
8165 bio_put(bio);
8166}
8167
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008168static inline blk_status_t btrfs_lookup_and_bind_dio_csum(struct inode *inode,
Miao Xiec1dc0892014-09-12 18:43:56 +08008169 struct btrfs_dio_private *dip,
8170 struct bio *bio,
8171 u64 file_offset)
8172{
8173 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
8174 struct btrfs_io_bio *orig_io_bio = btrfs_io_bio(dip->orig_bio);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008175 blk_status_t ret;
Miao Xiec1dc0892014-09-12 18:43:56 +08008176
8177 /*
8178 * We load all the csum data we need when we submit
8179 * the first bio to reduce the csum tree search and
8180 * contention.
8181 */
8182 if (dip->logical_offset == file_offset) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008183 ret = btrfs_lookup_bio_sums_dio(inode, dip->orig_bio,
Miao Xiec1dc0892014-09-12 18:43:56 +08008184 file_offset);
8185 if (ret)
8186 return ret;
8187 }
8188
8189 if (bio == dip->orig_bio)
8190 return 0;
8191
8192 file_offset -= dip->logical_offset;
8193 file_offset >>= inode->i_sb->s_blocksize_bits;
8194 io_bio->csum = (u8 *)(((u32 *)orig_io_bio->csum) + file_offset);
8195
8196 return 0;
8197}
8198
David Sterbad0ee3932018-03-08 14:35:48 +01008199static inline blk_status_t btrfs_submit_dio_bio(struct bio *bio,
8200 struct inode *inode, u64 file_offset, int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00008201{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008202 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xiefacc8a222013-07-25 19:22:34 +08008203 struct btrfs_dio_private *dip = bio->bi_private;
Mike Christie37226b22016-06-05 14:31:52 -05008204 bool write = bio_op(bio) == REQ_OP_WRITE;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008205 blk_status_t ret;
Miao Xiee65e1532010-11-22 03:04:43 +00008206
Liu Bo4c274bc2017-11-01 17:19:27 -06008207 /* Check btrfs_submit_bio_hook() for rules about async submit. */
Josef Bacikb812ce22012-11-16 13:56:32 -05008208 if (async_submit)
8209 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
8210
Josef Bacik5fd02042012-05-02 14:00:54 -04008211 if (!write) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008212 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DATA);
Josef Bacik5fd02042012-05-02 14:00:54 -04008213 if (ret)
8214 goto err;
8215 }
Miao Xiee65e1532010-11-22 03:04:43 +00008216
Nikolay Borisove6961ca2017-08-03 15:44:58 +03008217 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Josef Bacik1ae39932011-04-06 14:41:34 -04008218 goto map;
8219
8220 if (write && async_submit) {
Josef Bacikc6100a42017-05-05 11:57:13 -04008221 ret = btrfs_wq_submit_bio(fs_info, bio, 0, 0,
8222 file_offset, inode,
David Sterbae288c082018-07-18 17:36:24 +02008223 btrfs_submit_bio_start_direct_io);
Miao Xiee65e1532010-11-22 03:04:43 +00008224 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04008225 } else if (write) {
8226 /*
8227 * If we aren't doing async submit, calculate the csum of the
8228 * bio now.
8229 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008230 ret = btrfs_csum_one_bio(inode, bio, file_offset, 1);
Josef Bacik1ae39932011-04-06 14:41:34 -04008231 if (ret)
8232 goto err;
Miao Xie23ea8e52014-09-12 18:43:54 +08008233 } else {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008234 ret = btrfs_lookup_and_bind_dio_csum(inode, dip, bio,
Miao Xiec1dc0892014-09-12 18:43:56 +08008235 file_offset);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00008236 if (ret)
8237 goto err;
8238 }
Josef Bacik1ae39932011-04-06 14:41:34 -04008239map:
Liu Bo9b4a9b22017-08-18 11:54:02 -06008240 ret = btrfs_map_bio(fs_info, bio, 0, 0);
Miao Xiee65e1532010-11-22 03:04:43 +00008241err:
Miao Xiee65e1532010-11-22 03:04:43 +00008242 return ret;
8243}
8244
Nikolay Borisove6961ca2017-08-03 15:44:58 +03008245static int btrfs_submit_direct_hook(struct btrfs_dio_private *dip)
Miao Xiee65e1532010-11-22 03:04:43 +00008246{
8247 struct inode *inode = dip->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008248 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xiee65e1532010-11-22 03:04:43 +00008249 struct bio *bio;
8250 struct bio *orig_bio = dip->orig_bio;
Kent Overstreet4f024f32013-10-11 15:44:27 -07008251 u64 start_sector = orig_bio->bi_iter.bi_sector;
Miao Xiee65e1532010-11-22 03:04:43 +00008252 u64 file_offset = dip->logical_offset;
Miao Xiee65e1532010-11-22 03:04:43 +00008253 u64 map_length;
Josef Bacik1ae39932011-04-06 14:41:34 -04008254 int async_submit = 0;
Liu Bo725130b2017-05-16 09:51:39 -07008255 u64 submit_len;
8256 int clone_offset = 0;
8257 int clone_len;
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05308258 int ret;
Omar Sandoval58efbc92017-08-22 23:45:59 -07008259 blk_status_t status;
Miao Xiee65e1532010-11-22 03:04:43 +00008260
Kent Overstreet4f024f32013-10-11 15:44:27 -07008261 map_length = orig_bio->bi_iter.bi_size;
Liu Bo725130b2017-05-16 09:51:39 -07008262 submit_len = map_length;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008263 ret = btrfs_map_block(fs_info, btrfs_op(orig_bio), start_sector << 9,
8264 &map_length, NULL, 0);
Miao Xie7a5c3c92014-06-17 18:58:59 +08008265 if (ret)
Miao Xiee65e1532010-11-22 03:04:43 +00008266 return -EIO;
Miao Xiefacc8a222013-07-25 19:22:34 +08008267
Liu Bo725130b2017-05-16 09:51:39 -07008268 if (map_length >= submit_len) {
Josef Bacik02f57c72011-04-06 14:25:44 -04008269 bio = orig_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08008270 dip->flags |= BTRFS_DIO_ORIG_BIO_SUBMITTED;
Josef Bacik02f57c72011-04-06 14:25:44 -04008271 goto submit;
8272 }
8273
David Woodhouse53b381b2013-01-29 18:40:14 -05008274 /* async crcs make it difficult to collect full stripe writes. */
Jeff Mahoney1b868262017-05-17 11:38:35 -04008275 if (btrfs_data_alloc_profile(fs_info) & BTRFS_BLOCK_GROUP_RAID56_MASK)
David Woodhouse53b381b2013-01-29 18:40:14 -05008276 async_submit = 0;
8277 else
8278 async_submit = 1;
8279
Liu Bo725130b2017-05-16 09:51:39 -07008280 /* bio split */
8281 ASSERT(map_length <= INT_MAX);
Josef Bacik02f57c72011-04-06 14:25:44 -04008282 atomic_inc(&dip->pending_bios);
Arnd Bergmann3c91ee62017-05-18 15:33:29 +02008283 do {
Liu Bo725130b2017-05-16 09:51:39 -07008284 clone_len = min_t(int, submit_len, map_length);
Josef Bacik02f57c72011-04-06 14:25:44 -04008285
Liu Bo725130b2017-05-16 09:51:39 -07008286 /*
8287 * This will never fail as it's passing GPF_NOFS and
8288 * the allocation is backed by btrfs_bioset.
8289 */
Liu Boe4770942017-05-16 10:57:14 -07008290 bio = btrfs_bio_clone_partial(orig_bio, clone_offset,
Liu Bo725130b2017-05-16 09:51:39 -07008291 clone_len);
8292 bio->bi_private = dip;
8293 bio->bi_end_io = btrfs_end_dio_bio;
8294 btrfs_io_bio(bio)->logical = file_offset;
Miao Xiee65e1532010-11-22 03:04:43 +00008295
Liu Bo725130b2017-05-16 09:51:39 -07008296 ASSERT(submit_len >= clone_len);
8297 submit_len -= clone_len;
8298 if (submit_len == 0)
8299 break;
Miao Xiee65e1532010-11-22 03:04:43 +00008300
Liu Bo725130b2017-05-16 09:51:39 -07008301 /*
8302 * Increase the count before we submit the bio so we know
8303 * the end IO handler won't happen before we increase the
8304 * count. Otherwise, the dip might get freed before we're
8305 * done setting it up.
8306 */
8307 atomic_inc(&dip->pending_bios);
Miao Xiee65e1532010-11-22 03:04:43 +00008308
David Sterbad0ee3932018-03-08 14:35:48 +01008309 status = btrfs_submit_dio_bio(bio, inode, file_offset,
Omar Sandoval58efbc92017-08-22 23:45:59 -07008310 async_submit);
8311 if (status) {
Liu Bo725130b2017-05-16 09:51:39 -07008312 bio_put(bio);
8313 atomic_dec(&dip->pending_bios);
8314 goto out_err;
Miao Xiee65e1532010-11-22 03:04:43 +00008315 }
Liu Bo725130b2017-05-16 09:51:39 -07008316
8317 clone_offset += clone_len;
8318 start_sector += clone_len >> 9;
8319 file_offset += clone_len;
8320
8321 map_length = submit_len;
8322 ret = btrfs_map_block(fs_info, btrfs_op(orig_bio),
8323 start_sector << 9, &map_length, NULL, 0);
8324 if (ret)
8325 goto out_err;
Arnd Bergmann3c91ee62017-05-18 15:33:29 +02008326 } while (submit_len > 0);
Miao Xiee65e1532010-11-22 03:04:43 +00008327
Josef Bacik02f57c72011-04-06 14:25:44 -04008328submit:
David Sterbad0ee3932018-03-08 14:35:48 +01008329 status = btrfs_submit_dio_bio(bio, inode, file_offset, async_submit);
Omar Sandoval58efbc92017-08-22 23:45:59 -07008330 if (!status)
Miao Xiee65e1532010-11-22 03:04:43 +00008331 return 0;
8332
8333 bio_put(bio);
8334out_err:
8335 dip->errors = 1;
8336 /*
Nikolay Borisovde224b72018-02-14 10:53:36 +02008337 * Before atomic variable goto zero, we must make sure dip->errors is
8338 * perceived to be set. This ordering is ensured by the fact that an
8339 * atomic operations with a return value are fully ordered as per
8340 * atomic_t.txt
Miao Xiee65e1532010-11-22 03:04:43 +00008341 */
Miao Xiee65e1532010-11-22 03:04:43 +00008342 if (atomic_dec_and_test(&dip->pending_bios))
8343 bio_io_error(dip->orig_bio);
8344
8345 /* bio_end_io() will handle error, so we needn't return it */
8346 return 0;
8347}
8348
Mike Christie8a4c1e42016-06-05 14:31:50 -05008349static void btrfs_submit_direct(struct bio *dio_bio, struct inode *inode,
8350 loff_t file_offset)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008351{
Filipe Manana61de7182015-07-01 12:13:10 +01008352 struct btrfs_dio_private *dip = NULL;
Liu Bo3892ac92017-04-17 15:00:28 -07008353 struct bio *bio = NULL;
8354 struct btrfs_io_bio *io_bio;
Mike Christie8a4c1e42016-06-05 14:31:50 -05008355 bool write = (bio_op(dio_bio) == REQ_OP_WRITE);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008356 int ret = 0;
8357
David Sterba8b6c1d52017-06-02 17:48:13 +02008358 bio = btrfs_bio_clone(dio_bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008359
Miao Xiec1dc0892014-09-12 18:43:56 +08008360 dip = kzalloc(sizeof(*dip), GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04008361 if (!dip) {
8362 ret = -ENOMEM;
Filipe Manana61de7182015-07-01 12:13:10 +01008363 goto free_ordered;
Chris Mason9be33952013-05-17 18:30:14 -04008364 }
8365
8366 dip->private = dio_bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008367 dip->inode = inode;
8368 dip->logical_offset = file_offset;
Kent Overstreet4f024f32013-10-11 15:44:27 -07008369 dip->bytes = dio_bio->bi_iter.bi_size;
8370 dip->disk_bytenr = (u64)dio_bio->bi_iter.bi_sector << 9;
Liu Bo3892ac92017-04-17 15:00:28 -07008371 bio->bi_private = dip;
8372 dip->orig_bio = bio;
Chris Mason9be33952013-05-17 18:30:14 -04008373 dip->dio_bio = dio_bio;
Miao Xiee65e1532010-11-22 03:04:43 +00008374 atomic_set(&dip->pending_bios, 0);
Liu Bo3892ac92017-04-17 15:00:28 -07008375 io_bio = btrfs_io_bio(bio);
8376 io_bio->logical = file_offset;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008377
Miao Xiec1dc0892014-09-12 18:43:56 +08008378 if (write) {
Liu Bo3892ac92017-04-17 15:00:28 -07008379 bio->bi_end_io = btrfs_endio_direct_write;
Miao Xiec1dc0892014-09-12 18:43:56 +08008380 } else {
Liu Bo3892ac92017-04-17 15:00:28 -07008381 bio->bi_end_io = btrfs_endio_direct_read;
Miao Xiec1dc0892014-09-12 18:43:56 +08008382 dip->subio_endio = btrfs_subio_endio_read;
8383 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04008384
Filipe Mananaf28a4922015-12-08 19:23:20 +00008385 /*
8386 * Reset the range for unsubmitted ordered extents (to a 0 length range)
8387 * even if we fail to submit a bio, because in such case we do the
8388 * corresponding error handling below and it must not be done a second
8389 * time by btrfs_direct_IO().
8390 */
8391 if (write) {
8392 struct btrfs_dio_data *dio_data = current->journal_info;
8393
8394 dio_data->unsubmitted_oe_range_end = dip->logical_offset +
8395 dip->bytes;
8396 dio_data->unsubmitted_oe_range_start =
8397 dio_data->unsubmitted_oe_range_end;
8398 }
8399
Nikolay Borisove6961ca2017-08-03 15:44:58 +03008400 ret = btrfs_submit_direct_hook(dip);
Miao Xiee65e1532010-11-22 03:04:43 +00008401 if (!ret)
Chris Masoneaf25d92010-05-25 09:48:28 -04008402 return;
Chris Mason9be33952013-05-17 18:30:14 -04008403
Liu Bo3892ac92017-04-17 15:00:28 -07008404 if (io_bio->end_io)
8405 io_bio->end_io(io_bio, ret);
Chris Mason9be33952013-05-17 18:30:14 -04008406
Josef Bacik4b46fce2010-05-23 11:00:55 -04008407free_ordered:
8408 /*
Filipe Manana61de7182015-07-01 12:13:10 +01008409 * If we arrived here it means either we failed to submit the dip
8410 * or we either failed to clone the dio_bio or failed to allocate the
8411 * dip. If we cloned the dio_bio and allocated the dip, we can just
8412 * call bio_endio against our io_bio so that we get proper resource
8413 * cleanup if we fail to submit the dip, otherwise, we must do the
8414 * same as btrfs_endio_direct_[write|read] because we can't call these
8415 * callbacks - they require an allocated dip and a clone of dio_bio.
Josef Bacik4b46fce2010-05-23 11:00:55 -04008416 */
Liu Bo3892ac92017-04-17 15:00:28 -07008417 if (bio && dip) {
Guoqing Jiang054ec2f2017-06-02 16:08:50 +08008418 bio_io_error(bio);
Filipe Manana61de7182015-07-01 12:13:10 +01008419 /*
Liu Bo3892ac92017-04-17 15:00:28 -07008420 * The end io callbacks free our dip, do the final put on bio
Filipe Manana61de7182015-07-01 12:13:10 +01008421 * and all the cleanup and final put for dio_bio (through
8422 * dio_end_io()).
8423 */
8424 dip = NULL;
Liu Bo3892ac92017-04-17 15:00:28 -07008425 bio = NULL;
Filipe Manana61de7182015-07-01 12:13:10 +01008426 } else {
Filipe Manana14543772015-11-24 16:23:54 +00008427 if (write)
Qu Wenruo524272602017-03-08 10:25:52 +08008428 __endio_write_update_ordered(inode,
Filipe Manana14543772015-11-24 16:23:54 +00008429 file_offset,
8430 dio_bio->bi_iter.bi_size,
Qu Wenruo524272602017-03-08 10:25:52 +08008431 false);
Filipe Manana14543772015-11-24 16:23:54 +00008432 else
Filipe Manana61de7182015-07-01 12:13:10 +01008433 unlock_extent(&BTRFS_I(inode)->io_tree, file_offset,
8434 file_offset + dio_bio->bi_iter.bi_size - 1);
Filipe Manana14543772015-11-24 16:23:54 +00008435
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008436 dio_bio->bi_status = BLK_STS_IOERR;
Filipe Manana61de7182015-07-01 12:13:10 +01008437 /*
8438 * Releases and cleans up our dio_bio, no need to bio_put()
8439 * nor bio_endio()/bio_io_error() against dio_bio.
8440 */
Christoph Hellwig40553512017-06-03 09:37:58 +02008441 dio_end_io(dio_bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008442 }
Liu Bo3892ac92017-04-17 15:00:28 -07008443 if (bio)
8444 bio_put(bio);
Filipe Manana61de7182015-07-01 12:13:10 +01008445 kfree(dip);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008446}
8447
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008448static ssize_t check_direct_IO(struct btrfs_fs_info *fs_info,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008449 const struct iov_iter *iter, loff_t offset)
Chris Mason5a5f79b2010-05-26 21:33:37 -04008450{
8451 int seg;
Josef Bacika1b75f72011-04-08 15:51:18 +00008452 int i;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008453 unsigned int blocksize_mask = fs_info->sectorsize - 1;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008454 ssize_t retval = -EINVAL;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008455
8456 if (offset & blocksize_mask)
8457 goto out;
8458
Al Viro28060d52014-03-22 05:15:17 -04008459 if (iov_iter_alignment(iter) & blocksize_mask)
8460 goto out;
Josef Bacika1b75f72011-04-08 15:51:18 +00008461
Al Viro28060d52014-03-22 05:15:17 -04008462 /* If this is a write we don't need to check anymore */
Al Virocd27e452016-10-10 13:39:05 -04008463 if (iov_iter_rw(iter) != READ || !iter_is_iovec(iter))
Al Viro28060d52014-03-22 05:15:17 -04008464 return 0;
8465 /*
8466 * Check to make sure we don't have duplicate iov_base's in this
8467 * iovec, if so return EINVAL, otherwise we'll get csum errors
8468 * when reading back.
8469 */
8470 for (seg = 0; seg < iter->nr_segs; seg++) {
8471 for (i = seg + 1; i < iter->nr_segs; i++) {
8472 if (iter->iov[seg].iov_base == iter->iov[i].iov_base)
Josef Bacika1b75f72011-04-08 15:51:18 +00008473 goto out;
8474 }
Chris Mason5a5f79b2010-05-26 21:33:37 -04008475 }
8476 retval = 0;
8477out:
8478 return retval;
8479}
Josef Bacikeb838e72012-07-31 16:28:48 -04008480
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008481static ssize_t btrfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
Chris Mason16432982008-04-10 10:23:21 -04008482{
Josef Bacik4b46fce2010-05-23 11:00:55 -04008483 struct file *file = iocb->ki_filp;
8484 struct inode *inode = file->f_mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008485 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
chandan50745b02015-08-28 21:10:13 +05308486 struct btrfs_dio_data dio_data = { 0 };
Qu Wenruo364ecf32017-02-27 15:10:38 +08008487 struct extent_changeset *data_reserved = NULL;
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008488 loff_t offset = iocb->ki_pos;
Miao Xie09348562013-02-07 10:12:07 +00008489 size_t count = 0;
Miao Xie2e60a512013-02-08 07:01:08 +00008490 int flags = 0;
Miao Xie38851cc2013-02-08 07:04:11 +00008491 bool wakeup = true;
8492 bool relock = false;
Miao Xie09348562013-02-07 10:12:07 +00008493 ssize_t ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008494
Nikolay Borisov8c70c9f2017-08-21 12:43:46 +03008495 if (check_direct_IO(fs_info, iter, offset))
Chris Mason5a5f79b2010-05-26 21:33:37 -04008496 return 0;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008497
Jens Axboefe0f07d2015-04-15 17:05:48 -06008498 inode_dio_begin(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008499
Josef Bacik0e267c42013-07-02 10:38:02 -04008500 /*
Miao Xie41bd9ca2014-03-06 13:54:57 +08008501 * The generic stuff only does filemap_write_and_wait_range, which
8502 * isn't enough if we've written compressed pages to this area, so
8503 * we need to flush the dirty pages again to make absolutely sure
8504 * that any outstanding dirty pages are on disk.
Josef Bacik0e267c42013-07-02 10:38:02 -04008505 */
Al Viroa6cbcd42014-03-04 22:38:00 -05008506 count = iov_iter_count(iter);
Miao Xie41bd9ca2014-03-06 13:54:57 +08008507 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
8508 &BTRFS_I(inode)->runtime_flags))
Wang Shilong9a025a02014-07-17 11:44:13 +08008509 filemap_fdatawrite_range(inode->i_mapping, offset,
8510 offset + count - 1);
Josef Bacik0e267c42013-07-02 10:38:02 -04008511
Omar Sandoval6f673762015-03-16 04:33:52 -07008512 if (iov_iter_rw(iter) == WRITE) {
Miao Xie38851cc2013-02-08 07:04:11 +00008513 /*
8514 * If the write DIO is beyond the EOF, we need update
8515 * the isize, but it is protected by i_mutex. So we can
8516 * not unlock the i_mutex at this case.
8517 */
8518 if (offset + count <= inode->i_size) {
Liu Bo4aaedfb2016-12-14 22:36:05 -08008519 dio_data.overwrite = 1;
Al Viro59551022016-01-22 15:40:57 -05008520 inode_unlock(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008521 relock = true;
Goldwyn Rodriguesedf064e2017-06-20 07:05:49 -05008522 } else if (iocb->ki_flags & IOCB_NOWAIT) {
8523 ret = -EAGAIN;
8524 goto out;
Miao Xie38851cc2013-02-08 07:04:11 +00008525 }
Qu Wenruo364ecf32017-02-27 15:10:38 +08008526 ret = btrfs_delalloc_reserve_space(inode, &data_reserved,
8527 offset, count);
Miao Xie09348562013-02-07 10:12:07 +00008528 if (ret)
Miao Xie38851cc2013-02-08 07:04:11 +00008529 goto out;
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008530
8531 /*
8532 * We need to know how many extents we reserved so that we can
8533 * do the accounting properly if we go over the number we
8534 * originally calculated. Abuse current->journal_info for this.
8535 */
Jeff Mahoneyda170662016-06-15 09:22:56 -04008536 dio_data.reserve = round_up(count,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008537 fs_info->sectorsize);
Filipe Mananaf28a4922015-12-08 19:23:20 +00008538 dio_data.unsubmitted_oe_range_start = (u64)offset;
8539 dio_data.unsubmitted_oe_range_end = (u64)offset;
chandan50745b02015-08-28 21:10:13 +05308540 current->journal_info = &dio_data;
Chandan Rajendra97dcdea2016-12-23 15:00:18 +05308541 down_read(&BTRFS_I(inode)->dio_sem);
David Sterbaee39b432014-09-30 01:33:33 +02008542 } else if (test_bit(BTRFS_INODE_READDIO_NEED_LOCK,
8543 &BTRFS_I(inode)->runtime_flags)) {
Jens Axboefe0f07d2015-04-15 17:05:48 -06008544 inode_dio_end(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008545 flags = DIO_LOCKING | DIO_SKIP_HOLES;
8546 wakeup = false;
Miao Xie09348562013-02-07 10:12:07 +00008547 }
8548
Omar Sandoval17f8c842015-03-16 04:33:50 -07008549 ret = __blockdev_direct_IO(iocb, inode,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008550 fs_info->fs_devices->latest_bdev,
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008551 iter, btrfs_get_blocks_direct, NULL,
Omar Sandoval17f8c842015-03-16 04:33:50 -07008552 btrfs_submit_direct, flags);
Omar Sandoval6f673762015-03-16 04:33:52 -07008553 if (iov_iter_rw(iter) == WRITE) {
Chandan Rajendra97dcdea2016-12-23 15:00:18 +05308554 up_read(&BTRFS_I(inode)->dio_sem);
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008555 current->journal_info = NULL;
Liu Boddba1bf2015-06-17 16:59:58 +08008556 if (ret < 0 && ret != -EIOCBQUEUED) {
chandan50745b02015-08-28 21:10:13 +05308557 if (dio_data.reserve)
Qu Wenruobc42bda2017-02-27 15:10:39 +08008558 btrfs_delalloc_release_space(inode, data_reserved,
Qu Wenruo43b18592017-12-12 15:34:32 +08008559 offset, dio_data.reserve, true);
Filipe Mananaf28a4922015-12-08 19:23:20 +00008560 /*
8561 * On error we might have left some ordered extents
8562 * without submitting corresponding bios for them, so
8563 * cleanup them up to avoid other tasks getting them
8564 * and waiting for them to complete forever.
8565 */
8566 if (dio_data.unsubmitted_oe_range_start <
8567 dio_data.unsubmitted_oe_range_end)
Qu Wenruo524272602017-03-08 10:25:52 +08008568 __endio_write_update_ordered(inode,
Filipe Mananaf28a4922015-12-08 19:23:20 +00008569 dio_data.unsubmitted_oe_range_start,
8570 dio_data.unsubmitted_oe_range_end -
8571 dio_data.unsubmitted_oe_range_start,
Qu Wenruo524272602017-03-08 10:25:52 +08008572 false);
Liu Boddba1bf2015-06-17 16:59:58 +08008573 } else if (ret >= 0 && (size_t)ret < count)
Qu Wenruobc42bda2017-02-27 15:10:39 +08008574 btrfs_delalloc_release_space(inode, data_reserved,
Qu Wenruo43b18592017-12-12 15:34:32 +08008575 offset, count - (size_t)ret, true);
8576 btrfs_delalloc_release_extents(BTRFS_I(inode), count, false);
Miao Xie09348562013-02-07 10:12:07 +00008577 }
Miao Xie38851cc2013-02-08 07:04:11 +00008578out:
Miao Xie2e60a512013-02-08 07:01:08 +00008579 if (wakeup)
Jens Axboefe0f07d2015-04-15 17:05:48 -06008580 inode_dio_end(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008581 if (relock)
Al Viro59551022016-01-22 15:40:57 -05008582 inode_lock(inode);
Miao Xie09348562013-02-07 10:12:07 +00008583
Qu Wenruo364ecf32017-02-27 15:10:38 +08008584 extent_changeset_free(data_reserved);
Miao Xie09348562013-02-07 10:12:07 +00008585 return ret;
Chris Mason16432982008-04-10 10:23:21 -04008586}
8587
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008588#define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
8589
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008590static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
8591 __u64 start, __u64 len)
8592{
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008593 int ret;
8594
8595 ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS);
8596 if (ret)
8597 return ret;
8598
David Sterba2135fb92017-06-23 04:09:57 +02008599 return extent_fiemap(inode, fieinfo, start, len);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008600}
8601
Chris Mason9ebefb182007-06-15 13:50:00 -04008602int btrfs_readpage(struct file *file, struct page *page)
8603{
Chris Masond1310b22008-01-24 16:13:08 -05008604 struct extent_io_tree *tree;
8605 tree = &BTRFS_I(page->mapping->host)->io_tree;
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02008606 return extent_read_full_page(tree, page, btrfs_get_extent, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04008607}
Chris Mason1832a6d2007-12-21 16:27:21 -05008608
Chris Mason39279cc2007-06-12 06:35:45 -04008609static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
8610{
Josef Bacikbe7bd732015-10-22 15:05:09 -04008611 struct inode *inode = page->mapping->host;
8612 int ret;
Chris Masonb888db2b2007-08-27 16:49:44 -04008613
8614 if (current->flags & PF_MEMALLOC) {
8615 redirty_page_for_writepage(wbc, page);
8616 unlock_page(page);
8617 return 0;
8618 }
Josef Bacikbe7bd732015-10-22 15:05:09 -04008619
8620 /*
8621 * If we are under memory pressure we will call this directly from the
8622 * VM, we need to make sure we have the inode referenced for the ordered
8623 * extent. If not just return like we didn't do anything.
8624 */
8625 if (!igrab(inode)) {
8626 redirty_page_for_writepage(wbc, page);
8627 return AOP_WRITEPAGE_ACTIVATE;
8628 }
Nikolay Borisov0a9b0e52017-12-08 15:55:59 +02008629 ret = extent_write_full_page(page, wbc);
Josef Bacikbe7bd732015-10-22 15:05:09 -04008630 btrfs_add_delayed_iput(inode);
8631 return ret;
Chris Masona52d9a82007-08-27 16:49:44 -04008632}
Chris Mason39279cc2007-06-12 06:35:45 -04008633
Eric Sandeen48a3b632013-04-25 20:41:01 +00008634static int btrfs_writepages(struct address_space *mapping,
8635 struct writeback_control *wbc)
Chris Masonb293f02e2007-11-01 19:45:34 -04008636{
Nikolay Borisov8ae225a2018-04-19 10:46:38 +03008637 return extent_writepages(mapping, wbc);
Chris Masonb293f02e2007-11-01 19:45:34 -04008638}
8639
Chris Mason3ab2fb52007-11-08 10:59:22 -05008640static int
8641btrfs_readpages(struct file *file, struct address_space *mapping,
8642 struct list_head *pages, unsigned nr_pages)
8643{
Nikolay Borisov2a3ff0a2018-04-19 10:46:36 +03008644 return extent_readpages(mapping, pages, nr_pages);
Chris Mason3ab2fb52007-11-08 10:59:22 -05008645}
Nikolay Borisov2a3ff0a2018-04-19 10:46:36 +03008646
Chris Masone6dcd2d2008-07-17 12:53:50 -04008647static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04008648{
Nikolay Borisov477a30b2018-04-19 10:46:34 +03008649 int ret = try_release_extent_mapping(page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04008650 if (ret == 1) {
8651 ClearPagePrivate(page);
8652 set_page_private(page, 0);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008653 put_page(page);
Chris Masona52d9a82007-08-27 16:49:44 -04008654 }
8655 return ret;
8656}
Chris Mason39279cc2007-06-12 06:35:45 -04008657
Chris Masone6dcd2d2008-07-17 12:53:50 -04008658static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
8659{
Chris Mason98509cf2008-09-11 15:51:43 -04008660 if (PageWriteback(page) || PageDirty(page))
8661 return 0;
Michal Hocko3ba7ab22017-01-09 15:39:02 +01008662 return __btrfs_releasepage(page, gfp_flags);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008663}
8664
Lukas Czernerd47992f2013-05-21 23:17:23 -04008665static void btrfs_invalidatepage(struct page *page, unsigned int offset,
8666 unsigned int length)
Chris Masona52d9a82007-08-27 16:49:44 -04008667{
Josef Bacik5fd02042012-05-02 14:00:54 -04008668 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05008669 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008670 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008671 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008672 u64 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008673 u64 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308674 u64 start;
8675 u64 end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008676 int inode_evicting = inode->i_state & I_FREEING;
Chris Masona52d9a82007-08-27 16:49:44 -04008677
Chris Mason8b62b722009-09-02 16:53:46 -04008678 /*
8679 * we have the page locked, so new writeback can't start,
8680 * and the dirty bit won't be cleared while we are here.
8681 *
8682 * Wait for IO on this page so that we can safely clear
8683 * the PagePrivate2 bit and do ordered accounting
8684 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008685 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04008686
Josef Bacik5fd02042012-05-02 14:00:54 -04008687 tree = &BTRFS_I(inode)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008688 if (offset) {
8689 btrfs_releasepage(page, GFP_NOFS);
8690 return;
8691 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008692
8693 if (!inode_evicting)
David Sterbaff13db42015-12-03 14:30:40 +01008694 lock_extent_bits(tree, page_start, page_end, &cached_state);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308695again:
8696 start = page_start;
Nikolay Borisova776c6f2017-02-20 13:50:49 +02008697 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), start,
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308698 page_end - start + 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008699 if (ordered) {
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308700 end = min(page_end, ordered->file_offset + ordered->len - 1);
Chris Masoneb84ae02008-07-17 13:53:27 -04008701 /*
8702 * IO on this page will never be started, so we need
8703 * to account for any ordered extents now
8704 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008705 if (!inode_evicting)
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308706 clear_extent_bit(tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008707 EXTENT_DIRTY | EXTENT_DELALLOC |
Filipe Mananaa7e3b972017-04-03 10:45:46 +01008708 EXTENT_DELALLOC_NEW |
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008709 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
David Sterbaae0f1622017-10-31 16:37:52 +01008710 EXTENT_DEFRAG, 1, 0, &cached_state);
Chris Mason8b62b722009-09-02 16:53:46 -04008711 /*
8712 * whoever cleared the private bit is responsible
8713 * for the finish_ordered_io
8714 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04008715 if (TestClearPagePrivate2(page)) {
8716 struct btrfs_ordered_inode_tree *tree;
8717 u64 new_len;
8718
8719 tree = &BTRFS_I(inode)->ordered_tree;
8720
8721 spin_lock_irq(&tree->lock);
8722 set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308723 new_len = start - ordered->file_offset;
Josef Bacik77cef2e2013-08-29 13:57:21 -04008724 if (new_len < ordered->truncated_len)
8725 ordered->truncated_len = new_len;
8726 spin_unlock_irq(&tree->lock);
8727
8728 if (btrfs_dec_test_ordered_pending(inode, &ordered,
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308729 start,
8730 end - start + 1, 1))
Josef Bacik77cef2e2013-08-29 13:57:21 -04008731 btrfs_finish_ordered_io(ordered);
Chris Mason8b62b722009-09-02 16:53:46 -04008732 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008733 btrfs_put_ordered_extent(ordered);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008734 if (!inode_evicting) {
8735 cached_state = NULL;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308736 lock_extent_bits(tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008737 &cached_state);
8738 }
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308739
8740 start = end + 1;
8741 if (start < page_end)
8742 goto again;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008743 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008744
Qu Wenruob9d0b382015-09-29 10:35:16 +08008745 /*
8746 * Qgroup reserved space handler
8747 * Page here will be either
8748 * 1) Already written to disk
8749 * In this case, its reserved space is released from data rsv map
8750 * and will be freed by delayed_ref handler finally.
8751 * So even we call qgroup_free_data(), it won't decrease reserved
8752 * space.
8753 * 2) Not written to disk
Goldwyn Rodrigues0b34c262016-09-30 10:40:52 -05008754 * This means the reserved space should be freed here. However,
8755 * if a truncate invalidates the page (by clearing PageDirty)
8756 * and the page is accounted for while allocating extent
8757 * in btrfs_check_data_free_space() we let delayed_ref to
8758 * free the entire extent.
Qu Wenruob9d0b382015-09-29 10:35:16 +08008759 */
Goldwyn Rodrigues0b34c262016-09-30 10:40:52 -05008760 if (PageDirty(page))
Qu Wenruobc42bda2017-02-27 15:10:39 +08008761 btrfs_qgroup_free_data(inode, NULL, page_start, PAGE_SIZE);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008762 if (!inode_evicting) {
8763 clear_extent_bit(tree, page_start, page_end,
8764 EXTENT_LOCKED | EXTENT_DIRTY |
Filipe Mananaa7e3b972017-04-03 10:45:46 +01008765 EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
8766 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1,
David Sterbaae0f1622017-10-31 16:37:52 +01008767 &cached_state);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008768
8769 __btrfs_releasepage(page, GFP_NOFS);
8770 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008771
Chris Mason4a096752008-07-21 10:29:44 -04008772 ClearPageChecked(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04008773 if (PagePrivate(page)) {
Chris Mason9ad6b7b2008-04-18 16:11:30 -04008774 ClearPagePrivate(page);
8775 set_page_private(page, 0);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008776 put_page(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04008777 }
Chris Mason39279cc2007-06-12 06:35:45 -04008778}
8779
Chris Mason9ebefb182007-06-15 13:50:00 -04008780/*
8781 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
8782 * called from a page fault handler when a page is first dirtied. Hence we must
8783 * be careful to check for EOF conditions here. We set the page up correctly
8784 * for a written page which means we get ENOSPC checking when writing into
8785 * holes and correct delalloc and unwritten extent mapping on filesystems that
8786 * support these features.
8787 *
8788 * We are not allowed to take the i_mutex here so we have to play games to
8789 * protect against truncate races as the page could now be beyond EOF. Because
Omar Sandovald1342aa2018-05-11 13:13:29 -07008790 * truncate_setsize() writes the inode size before removing pages, once we have
8791 * the page lock we can determine safely if the page is beyond EOF. If it is not
Chris Mason9ebefb182007-06-15 13:50:00 -04008792 * beyond EOF, then the page is guaranteed safe against truncation until we
8793 * unlock the page.
8794 */
Souptick Joardera528a242018-06-06 19:54:44 +05308795vm_fault_t btrfs_page_mkwrite(struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04008796{
Nick Pigginc2ec1752009-03-31 15:23:21 -07008797 struct page *page = vmf->page;
Dave Jiang11bac802017-02-24 14:56:41 -08008798 struct inode *inode = file_inode(vmf->vma->vm_file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008799 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008800 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
8801 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008802 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08008803 struct extent_changeset *data_reserved = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008804 char *kaddr;
8805 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04008806 loff_t size;
Souptick Joardera528a242018-06-06 19:54:44 +05308807 vm_fault_t ret;
8808 int ret2;
Chris Mason9998eb72012-01-25 13:47:40 -05008809 int reserved = 0;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308810 u64 reserved_space;
Chris Masona52d9a82007-08-27 16:49:44 -04008811 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008812 u64 page_end;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308813 u64 end;
8814
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008815 reserved_space = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04008816
Jan Karab2b5ef52012-06-12 16:20:45 +02008817 sb_start_pagefault(inode->i_sb);
Qu Wenruodf480632015-09-08 17:25:54 +08008818 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008819 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308820 end = page_end;
Qu Wenruodf480632015-09-08 17:25:54 +08008821
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308822 /*
8823 * Reserving delalloc space after obtaining the page lock can lead to
8824 * deadlock. For example, if a dirty page is locked by this function
8825 * and the call to btrfs_delalloc_reserve_space() ends up triggering
8826 * dirty page write out, then the btrfs_writepage() function could
8827 * end up waiting indefinitely to get a lock on the page currently
8828 * being processed by btrfs_page_mkwrite() function.
8829 */
Souptick Joardera528a242018-06-06 19:54:44 +05308830 ret2 = btrfs_delalloc_reserve_space(inode, &data_reserved, page_start,
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308831 reserved_space);
Souptick Joardera528a242018-06-06 19:54:44 +05308832 if (!ret2) {
8833 ret2 = file_update_time(vmf->vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05008834 reserved = 1;
8835 }
Souptick Joardera528a242018-06-06 19:54:44 +05308836 if (ret2) {
8837 ret = vmf_error(ret2);
Chris Mason9998eb72012-01-25 13:47:40 -05008838 if (reserved)
8839 goto out;
8840 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07008841 }
Chris Mason1832a6d2007-12-21 16:27:21 -05008842
Nick Piggin56a76f82009-03-31 15:23:23 -07008843 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008844again:
Chris Mason9ebefb182007-06-15 13:50:00 -04008845 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04008846 size = i_size_read(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04008847
Chris Mason9ebefb182007-06-15 13:50:00 -04008848 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04008849 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04008850 /* page got truncated out from underneath us */
8851 goto out_unlock;
8852 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008853 wait_on_page_writeback(page);
8854
David Sterbaff13db42015-12-03 14:30:40 +01008855 lock_extent_bits(io_tree, page_start, page_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008856 set_page_extent_mapped(page);
8857
Chris Masoneb84ae02008-07-17 13:53:27 -04008858 /*
8859 * we can't set the delalloc bits if there are pending ordered
8860 * extents. Drop our locks and wait for them to finish
8861 */
Nikolay Borisova776c6f2017-02-20 13:50:49 +02008862 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start,
8863 PAGE_SIZE);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008864 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00008865 unlock_extent_cached(io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01008866 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008867 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04008868 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008869 btrfs_put_ordered_extent(ordered);
8870 goto again;
8871 }
8872
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008873 if (page->index == ((size - 1) >> PAGE_SHIFT)) {
Jeff Mahoneyda170662016-06-15 09:22:56 -04008874 reserved_space = round_up(size - page_start,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008875 fs_info->sectorsize);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008876 if (reserved_space < PAGE_SIZE) {
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308877 end = page_start + reserved_space - 1;
Qu Wenruobc42bda2017-02-27 15:10:39 +08008878 btrfs_delalloc_release_space(inode, data_reserved,
Qu Wenruo43b18592017-12-12 15:34:32 +08008879 page_start, PAGE_SIZE - reserved_space,
8880 true);
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308881 }
8882 }
8883
Josef Bacikfbf19082009-10-01 17:10:23 -04008884 /*
Liu Bo54160342016-12-13 12:15:19 -08008885 * page_mkwrite gets called when the page is firstly dirtied after it's
8886 * faulted in, but write(2) could also dirty a page and set delalloc
8887 * bits, thus in this case for space account reason, we still need to
8888 * clear any delalloc bits within this page range since we have to
8889 * reserve data&meta space before lock_page() (see above comments).
Josef Bacikfbf19082009-10-01 17:10:23 -04008890 */
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308891 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06008892 EXTENT_DIRTY | EXTENT_DELALLOC |
8893 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
David Sterbaae0f1622017-10-31 16:37:52 +01008894 0, 0, &cached_state);
Josef Bacikfbf19082009-10-01 17:10:23 -04008895
Souptick Joardera528a242018-06-06 19:54:44 +05308896 ret2 = btrfs_set_extent_delalloc(inode, page_start, end, 0,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08008897 &cached_state, 0);
Souptick Joardera528a242018-06-06 19:54:44 +05308898 if (ret2) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00008899 unlock_extent_cached(io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01008900 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04008901 ret = VM_FAULT_SIGBUS;
8902 goto out_unlock;
8903 }
Souptick Joardera528a242018-06-06 19:54:44 +05308904 ret2 = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04008905
8906 /* page is wholly or partially inside EOF */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008907 if (page_start + PAGE_SIZE > size)
8908 zero_start = size & ~PAGE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04008909 else
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008910 zero_start = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04008911
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008912 if (zero_start != PAGE_SIZE) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04008913 kaddr = kmap(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008914 memset(kaddr + zero_start, 0, PAGE_SIZE - zero_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008915 flush_dcache_page(page);
8916 kunmap(page);
8917 }
Chris Mason247e7432008-07-17 12:53:51 -04008918 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008919 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04008920 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04008921
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008922 BTRFS_I(inode)->last_trans = fs_info->generation;
Chris Mason257c62e2009-10-13 13:21:08 -04008923 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06008924 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
Chris Mason257c62e2009-10-13 13:21:08 -04008925
David Sterbae43bbe52017-12-12 21:43:52 +01008926 unlock_extent_cached(io_tree, page_start, page_end, &cached_state);
Chris Mason9ebefb182007-06-15 13:50:00 -04008927
Souptick Joardera528a242018-06-06 19:54:44 +05308928 if (!ret2) {
Qu Wenruo43b18592017-12-12 15:34:32 +08008929 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE, true);
Jan Karab2b5ef52012-06-12 16:20:45 +02008930 sb_end_pagefault(inode->i_sb);
Qu Wenruo364ecf32017-02-27 15:10:38 +08008931 extent_changeset_free(data_reserved);
Chris Mason50a9b212009-09-11 12:33:12 -04008932 return VM_FAULT_LOCKED;
Jan Karab2b5ef52012-06-12 16:20:45 +02008933 }
Chris Mason717beb92018-06-25 10:03:41 -07008934
8935out_unlock:
Chris Mason9ebefb182007-06-15 13:50:00 -04008936 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05008937out:
Qu Wenruo43b18592017-12-12 15:34:32 +08008938 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE, (ret != 0));
Qu Wenruobc42bda2017-02-27 15:10:39 +08008939 btrfs_delalloc_release_space(inode, data_reserved, page_start,
Qu Wenruo43b18592017-12-12 15:34:32 +08008940 reserved_space, (ret != 0));
Chris Mason9998eb72012-01-25 13:47:40 -05008941out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02008942 sb_end_pagefault(inode->i_sb);
Qu Wenruo364ecf32017-02-27 15:10:38 +08008943 extent_changeset_free(data_reserved);
Chris Mason9ebefb182007-06-15 13:50:00 -04008944 return ret;
8945}
8946
Filipe Manana213e8c52018-02-06 20:40:31 +00008947static int btrfs_truncate(struct inode *inode, bool skip_writeback)
Chris Mason39279cc2007-06-12 06:35:45 -04008948{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008949 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04008950 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04008951 struct btrfs_block_rsv *rsv;
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008952 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04008953 struct btrfs_trans_handle *trans;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008954 u64 mask = fs_info->sectorsize - 1;
8955 u64 min_size = btrfs_calc_trunc_metadata_size(fs_info, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04008956
Filipe Manana213e8c52018-02-06 20:40:31 +00008957 if (!skip_writeback) {
8958 ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask),
8959 (u64)-1);
8960 if (ret)
8961 return ret;
8962 }
Chris Mason39279cc2007-06-12 06:35:45 -04008963
Josef Bacikfcb80c22011-05-03 10:40:22 -04008964 /*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008965 * Yes ladies and gentlemen, this is indeed ugly. We have a couple of
8966 * things going on here:
Josef Bacikfcb80c22011-05-03 10:40:22 -04008967 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008968 * 1) We need to reserve space to update our inode.
Josef Bacikfcb80c22011-05-03 10:40:22 -04008969 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008970 * 2) We need to have something to cache all the space that is going to
Josef Bacikfcb80c22011-05-03 10:40:22 -04008971 * be free'd up by the truncate operation, but also have some slack
8972 * space reserved in case it uses space during the truncate (thank you
8973 * very much snapshotting).
8974 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008975 * And we need these to be separate. The fact is we can use a lot of
Josef Bacikfcb80c22011-05-03 10:40:22 -04008976 * space doing the truncate, and we have no earthly idea how much space
Nicholas D Steeves01327612016-05-19 21:18:45 -04008977 * we will use, so we need the truncate reservation to be separate so it
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008978 * doesn't end up using space reserved for updating the inode. We also
8979 * need to be able to stop the transaction and start a new one, which
8980 * means we need to be able to update the inode several times, and we
8981 * have no idea of knowing how many times that will be, so we can't just
8982 * reserve 1 item for the entirety of the operation, so that has to be
8983 * done separately as well.
Josef Bacikfcb80c22011-05-03 10:40:22 -04008984 *
8985 * So that leaves us with
8986 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008987 * 1) rsv - for the truncate reservation, which we will steal from the
Josef Bacikfcb80c22011-05-03 10:40:22 -04008988 * transaction reservation.
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008989 * 2) fs_info->trans_block_rsv - this will have 1 items worth left for
Josef Bacikfcb80c22011-05-03 10:40:22 -04008990 * updating the inode.
8991 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008992 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008993 if (!rsv)
8994 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04008995 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04008996 rsv->failfast = 1;
Josef Bacikf0cd8462011-03-04 14:37:08 -05008997
Josef Bacik907cbce2011-08-08 13:46:15 -04008998 /*
Josef Bacik07127182011-08-19 10:29:59 -04008999 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04009000 * 1 for updating the inode.
9001 */
Josef Bacikf3fe8202013-01-07 17:03:21 -05009002 trans = btrfs_start_transaction(root, 2);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009003 if (IS_ERR(trans)) {
Omar Sandovalad7e1a72018-05-22 09:59:50 -07009004 ret = PTR_ERR(trans);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009005 goto out;
9006 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05009007
Josef Bacik907cbce2011-08-08 13:46:15 -04009008 /* Migrate the slack space for the truncate to our reserve */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009009 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, rsv,
Lu Fengqi3a584172018-08-04 21:10:55 +08009010 min_size, false);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009011 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05009012
Chris Mason5a3f23d2009-03-31 13:27:11 -04009013 /*
Josef Bacik5dc562c2012-08-17 13:14:17 -04009014 * So if we truncate and then write and fsync we normally would just
9015 * write the extents that changed, which is a problem if we need to
9016 * first truncate that entire inode. So set this flag so we write out
9017 * all of the extents in the inode to the sync log so we're completely
9018 * safe.
9019 */
9020 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009021 trans->block_rsv = rsv;
Josef Bacik5dc562c2012-08-17 13:14:17 -04009022
Yan, Zheng80825102009-11-12 09:35:36 +00009023 while (1) {
9024 ret = btrfs_truncate_inode_items(trans, root, inode,
9025 inode->i_size,
9026 BTRFS_EXTENT_DATA_KEY);
Josef Bacikddfae632017-10-19 14:16:02 -04009027 trans->block_rsv = &fs_info->trans_block_rsv;
Omar Sandovalad7e1a72018-05-22 09:59:50 -07009028 if (ret != -ENOSPC && ret != -EAGAIN)
Yan, Zheng80825102009-11-12 09:35:36 +00009029 break;
Chris Mason39279cc2007-06-12 06:35:45 -04009030
Yan, Zheng80825102009-11-12 09:35:36 +00009031 ret = btrfs_update_inode(trans, root, inode);
Omar Sandovalad7e1a72018-05-22 09:59:50 -07009032 if (ret)
Josef Bacik3893e332011-01-31 16:03:11 -05009033 break;
Josef Bacikca7e70f2012-08-27 17:48:15 -04009034
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009035 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009036 btrfs_btree_balance_dirty(fs_info);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009037
9038 trans = btrfs_start_transaction(root, 2);
9039 if (IS_ERR(trans)) {
Omar Sandovalad7e1a72018-05-22 09:59:50 -07009040 ret = PTR_ERR(trans);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009041 trans = NULL;
9042 break;
9043 }
9044
Wang Xiaoguang47b5d642016-09-07 20:17:38 +08009045 btrfs_block_rsv_release(fs_info, rsv, -1);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009046 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv,
Lu Fengqi3a584172018-08-04 21:10:55 +08009047 rsv, min_size, false);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009048 BUG_ON(ret); /* shouldn't happen */
9049 trans->block_rsv = rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00009050 }
9051
Josef Bacikddfae632017-10-19 14:16:02 -04009052 /*
9053 * We can't call btrfs_truncate_block inside a trans handle as we could
9054 * deadlock with freeze, if we got NEED_TRUNCATE_BLOCK then we know
9055 * we've truncated everything except the last little bit, and can do
9056 * btrfs_truncate_block and then update the disk_i_size.
9057 */
9058 if (ret == NEED_TRUNCATE_BLOCK) {
9059 btrfs_end_transaction(trans);
9060 btrfs_btree_balance_dirty(fs_info);
9061
9062 ret = btrfs_truncate_block(inode, inode->i_size, 0, 0);
9063 if (ret)
9064 goto out;
9065 trans = btrfs_start_transaction(root, 1);
9066 if (IS_ERR(trans)) {
9067 ret = PTR_ERR(trans);
9068 goto out;
9069 }
9070 btrfs_ordered_update_i_size(inode, inode->i_size, NULL);
9071 }
9072
Chris Mason917c16b2011-11-08 14:49:59 -05009073 if (trans) {
Omar Sandovalad7e1a72018-05-22 09:59:50 -07009074 int ret2;
Josef Bacik7b128762008-07-24 12:17:14 -04009075
Omar Sandovalad7e1a72018-05-22 09:59:50 -07009076 trans->block_rsv = &fs_info->trans_block_rsv;
9077 ret2 = btrfs_update_inode(trans, root, inode);
9078 if (ret2 && !ret)
9079 ret = ret2;
9080
9081 ret2 = btrfs_end_transaction(trans);
9082 if (ret2 && !ret)
9083 ret = ret2;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009084 btrfs_btree_balance_dirty(fs_info);
Chris Mason917c16b2011-11-08 14:49:59 -05009085 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04009086out:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009087 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009088
Omar Sandovalad7e1a72018-05-22 09:59:50 -07009089 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04009090}
9091
Sven Wegener3b963622008-06-09 21:57:42 -04009092/*
Chris Masond352ac62008-09-29 15:18:18 -04009093 * create a new subvolume directory/inode (helper for the ioctl).
9094 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05009095int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009096 struct btrfs_root *new_root,
9097 struct btrfs_root *parent_root,
9098 u64 new_dirid)
Chris Mason39279cc2007-06-12 06:35:45 -04009099{
Chris Mason39279cc2007-06-12 06:35:45 -04009100 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04009101 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04009102 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009103
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01009104 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
9105 new_dirid, new_dirid,
9106 S_IFDIR | (~current_umask() & S_IRWXUGO),
9107 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04009108 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04009109 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009110 inode->i_op = &btrfs_dir_inode_operations;
9111 inode->i_fop = &btrfs_dir_file_operations;
9112
Miklos Szeredibfe86842011-10-28 14:13:29 +02009113 set_nlink(inode, 1);
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02009114 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Masonb0d5d102014-09-08 13:08:51 -07009115 unlock_new_inode(inode);
Sven Wegener3b963622008-06-09 21:57:42 -04009116
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009117 err = btrfs_subvol_inherit_props(trans, new_root, parent_root);
9118 if (err)
9119 btrfs_err(new_root->fs_info,
David Sterba351fd352014-05-15 16:48:20 +02009120 "error inheriting subvolume %llu properties: %d",
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009121 new_root->root_key.objectid, err);
9122
Yan, Zheng76dda932009-09-21 16:00:26 -04009123 err = btrfs_update_inode(trans, new_root, inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04009124
Yan, Zheng76dda932009-09-21 16:00:26 -04009125 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07009126 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04009127}
9128
Chris Mason39279cc2007-06-12 06:35:45 -04009129struct inode *btrfs_alloc_inode(struct super_block *sb)
9130{
Josef Bacik69fe2d72017-10-19 14:15:57 -04009131 struct btrfs_fs_info *fs_info = btrfs_sb(sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009132 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009133 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009134
David Sterba712e36c2017-10-31 17:08:27 +01009135 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_KERNEL);
Chris Mason39279cc2007-06-12 06:35:45 -04009136 if (!ei)
9137 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009138
9139 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009140 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04009141 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04009142 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04009143 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009144 ei->delalloc_bytes = 0;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01009145 ei->new_delalloc_bytes = 0;
Wang Shilong47059d92014-07-03 18:22:07 +08009146 ei->defrag_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009147 ei->disk_i_size = 0;
9148 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04009149 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009150 ei->index_cnt = (u64)-1;
Miao Xie67de1172013-12-26 13:07:06 +08009151 ei->dir_index = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009152 ei->last_unlink_trans = 0;
Liu Bo46d8bc32012-08-29 01:07:55 -06009153 ei->last_log_commit = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009154
Josef Bacik9e0baf62011-07-15 15:16:44 +00009155 spin_lock_init(&ei->lock);
9156 ei->outstanding_extents = 0;
Josef Bacik69fe2d72017-10-19 14:15:57 -04009157 if (sb->s_magic != BTRFS_TEST_MAGIC)
9158 btrfs_init_metadata_block_rsv(fs_info, &ei->block_rsv,
9159 BTRFS_BLOCK_RSV_DELALLOC);
Josef Bacik72ac3c02012-05-23 14:13:11 -04009160 ei->runtime_flags = 0;
David Sterbab52aa8c2017-07-17 19:17:20 +02009161 ei->prop_compress = BTRFS_COMPRESS_NONE;
David Sterbaeec63c62017-07-17 19:41:31 +02009162 ei->defrag_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009163
Miao Xie16cdcec2011-04-22 18:12:22 +08009164 ei->delayed_node = NULL;
9165
chandan r9cc97d62012-07-04 12:48:07 +05309166 ei->i_otime.tv_sec = 0;
9167 ei->i_otime.tv_nsec = 0;
9168
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009169 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02009170 extent_map_tree_init(&ei->extent_tree);
Josef Bacikc6100a42017-05-05 11:57:13 -04009171 extent_io_tree_init(&ei->io_tree, inode);
9172 extent_io_tree_init(&ei->io_failure_tree, inode);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04009173 ei->io_tree.track_uptodate = 1;
9174 ei->io_failure_tree.track_uptodate = 1;
Josef Bacikb812ce22012-11-16 13:56:32 -05009175 atomic_set(&ei->sync_writers, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009176 mutex_init(&ei->log_mutex);
Josef Bacikf2486792012-01-13 12:09:22 -05009177 mutex_init(&ei->delalloc_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009178 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009179 INIT_LIST_HEAD(&ei->delalloc_inodes);
David Sterba8089fe62015-11-19 14:15:51 +01009180 INIT_LIST_HEAD(&ei->delayed_iput);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009181 RB_CLEAR_NODE(&ei->rb_node);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01009182 init_rwsem(&ei->dio_sem);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009183
9184 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009185}
9186
Josef Bacikaaedb552013-10-11 14:44:09 -04009187#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
9188void btrfs_test_destroy_inode(struct inode *inode)
9189{
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02009190 btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0);
Josef Bacikaaedb552013-10-11 14:44:09 -04009191 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
9192}
9193#endif
9194
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009195static void btrfs_i_callback(struct rcu_head *head)
9196{
9197 struct inode *inode = container_of(head, struct inode, i_rcu);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009198 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
9199}
9200
Chris Mason39279cc2007-06-12 06:35:45 -04009201void btrfs_destroy_inode(struct inode *inode)
9202{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009203 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009204 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009205 struct btrfs_root *root = BTRFS_I(inode)->root;
9206
Al Virob3d9b7a2012-06-09 13:51:19 -04009207 WARN_ON(!hlist_empty(&inode->i_dentry));
Chris Mason39279cc2007-06-12 06:35:45 -04009208 WARN_ON(inode->i_data.nrpages);
Josef Bacik69fe2d72017-10-19 14:15:57 -04009209 WARN_ON(BTRFS_I(inode)->block_rsv.reserved);
9210 WARN_ON(BTRFS_I(inode)->block_rsv.size);
Josef Bacik9e0baf62011-07-15 15:16:44 +00009211 WARN_ON(BTRFS_I(inode)->outstanding_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04009212 WARN_ON(BTRFS_I(inode)->delalloc_bytes);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01009213 WARN_ON(BTRFS_I(inode)->new_delalloc_bytes);
Josef Bacik7709cde2011-08-04 10:25:02 -04009214 WARN_ON(BTRFS_I(inode)->csum_bytes);
Wang Shilong47059d92014-07-03 18:22:07 +08009215 WARN_ON(BTRFS_I(inode)->defrag_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04009216
Chris Mason5a3f23d2009-03-31 13:27:11 -04009217 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05009218 * This can happen where we create an inode, but somebody else also
9219 * created the same inode and we need to destroy the one we already
9220 * created.
9221 */
9222 if (!root)
9223 goto free;
9224
Chris Masond3977122009-01-05 21:25:51 -05009225 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04009226 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
9227 if (!ordered)
9228 break;
9229 else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009230 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04009231 "found ordered extent %llu %llu on inode cleanup",
9232 ordered->file_offset, ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009233 btrfs_remove_ordered_extent(inode, ordered);
9234 btrfs_put_ordered_extent(ordered);
9235 btrfs_put_ordered_extent(ordered);
9236 }
9237 }
Qu Wenruo56fa9d02015-10-13 09:53:10 +08009238 btrfs_qgroup_check_reserved_leak(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04009239 inode_tree_del(inode);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02009240 btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0);
Josef Bacika6dbd422009-11-11 15:53:34 -05009241free:
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009242 call_rcu(&inode->i_rcu, btrfs_i_callback);
Chris Mason39279cc2007-06-12 06:35:45 -04009243}
9244
Al Viro45321ac2010-06-07 13:43:19 -04009245int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04009246{
9247 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04009248
Naohiro Aota6379ef92013-06-06 09:56:34 +00009249 if (root == NULL)
9250 return 1;
9251
Liu Bofa6ac872013-02-20 14:10:23 +00009252 /* the snap/subvol tree is on deleting */
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02009253 if (btrfs_root_refs(&root->root_item) == 0)
Al Viro45321ac2010-06-07 13:43:19 -04009254 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04009255 else
Al Viro45321ac2010-06-07 13:43:19 -04009256 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04009257}
9258
Sven Wegener0ee0fda2008-07-30 16:54:26 -04009259static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04009260{
9261 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
9262
9263 inode_init_once(&ei->vfs_inode);
9264}
9265
David Sterbae67c7182018-02-19 17:24:18 +01009266void __cold btrfs_destroy_cachep(void)
Chris Mason39279cc2007-06-12 06:35:45 -04009267{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10009268 /*
9269 * Make sure all delayed rcu free inodes are flushed before we
9270 * destroy cache.
9271 */
9272 rcu_barrier();
Kinglong Mee5598e902016-01-29 21:36:35 +08009273 kmem_cache_destroy(btrfs_inode_cachep);
9274 kmem_cache_destroy(btrfs_trans_handle_cachep);
Kinglong Mee5598e902016-01-29 21:36:35 +08009275 kmem_cache_destroy(btrfs_path_cachep);
9276 kmem_cache_destroy(btrfs_free_space_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04009277}
9278
Liu Bof5c29bd2017-11-02 17:21:50 -06009279int __init btrfs_init_cachep(void)
Chris Mason39279cc2007-06-12 06:35:45 -04009280{
David Sterba837e1972012-09-07 03:00:48 -06009281 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009282 sizeof(struct btrfs_inode), 0,
Vladimir Davydov5d097052016-01-14 15:18:21 -08009283 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD | SLAB_ACCOUNT,
9284 init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04009285 if (!btrfs_inode_cachep)
9286 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009287
David Sterba837e1972012-09-07 03:00:48 -06009288 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009289 sizeof(struct btrfs_trans_handle), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009290 SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009291 if (!btrfs_trans_handle_cachep)
9292 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009293
David Sterba837e1972012-09-07 03:00:48 -06009294 btrfs_path_cachep = kmem_cache_create("btrfs_path",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009295 sizeof(struct btrfs_path), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009296 SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009297 if (!btrfs_path_cachep)
9298 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009299
David Sterba837e1972012-09-07 03:00:48 -06009300 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
Josef Bacikdc89e982011-01-28 17:05:48 -05009301 sizeof(struct btrfs_free_space), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009302 SLAB_MEM_SPREAD, NULL);
Josef Bacikdc89e982011-01-28 17:05:48 -05009303 if (!btrfs_free_space_cachep)
9304 goto fail;
9305
Chris Mason39279cc2007-06-12 06:35:45 -04009306 return 0;
9307fail:
9308 btrfs_destroy_cachep();
9309 return -ENOMEM;
9310}
9311
David Howellsa528d352017-01-31 16:46:22 +00009312static int btrfs_getattr(const struct path *path, struct kstat *stat,
9313 u32 request_mask, unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04009314{
Miao Xiedf0af1a2013-01-29 10:11:59 +00009315 u64 delalloc_bytes;
David Howellsa528d352017-01-31 16:46:22 +00009316 struct inode *inode = d_inode(path->dentry);
David Sterbafadc0d82011-11-20 07:33:38 -05009317 u32 blocksize = inode->i_sb->s_blocksize;
Yonghong Song04a87e32017-05-12 15:07:43 -07009318 u32 bi_flags = BTRFS_I(inode)->flags;
9319
9320 stat->result_mask |= STATX_BTIME;
9321 stat->btime.tv_sec = BTRFS_I(inode)->i_otime.tv_sec;
9322 stat->btime.tv_nsec = BTRFS_I(inode)->i_otime.tv_nsec;
9323 if (bi_flags & BTRFS_INODE_APPEND)
9324 stat->attributes |= STATX_ATTR_APPEND;
9325 if (bi_flags & BTRFS_INODE_COMPRESS)
9326 stat->attributes |= STATX_ATTR_COMPRESSED;
9327 if (bi_flags & BTRFS_INODE_IMMUTABLE)
9328 stat->attributes |= STATX_ATTR_IMMUTABLE;
9329 if (bi_flags & BTRFS_INODE_NODUMP)
9330 stat->attributes |= STATX_ATTR_NODUMP;
9331
9332 stat->attributes_mask |= (STATX_ATTR_APPEND |
9333 STATX_ATTR_COMPRESSED |
9334 STATX_ATTR_IMMUTABLE |
9335 STATX_ATTR_NODUMP);
David Sterbafadc0d82011-11-20 07:33:38 -05009336
Chris Mason39279cc2007-06-12 06:35:45 -04009337 generic_fillattr(inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04009338 stat->dev = BTRFS_I(inode)->root->anon_dev;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009339
9340 spin_lock(&BTRFS_I(inode)->lock);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01009341 delalloc_bytes = BTRFS_I(inode)->new_delalloc_bytes;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009342 spin_unlock(&BTRFS_I(inode)->lock);
David Sterbafadc0d82011-11-20 07:33:38 -05009343 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
Miao Xiedf0af1a2013-01-29 10:11:59 +00009344 ALIGN(delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04009345 return 0;
9346}
9347
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009348static int btrfs_rename_exchange(struct inode *old_dir,
9349 struct dentry *old_dentry,
9350 struct inode *new_dir,
9351 struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04009352{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009353 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009354 struct btrfs_trans_handle *trans;
9355 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009356 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009357 struct inode *new_inode = new_dentry->d_inode;
9358 struct inode *old_inode = old_dentry->d_inode;
Deepa Dinamani95582b02018-05-08 19:36:02 -07009359 struct timespec64 ctime = current_time(old_inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009360 struct dentry *parent;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009361 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
9362 u64 new_ino = btrfs_ino(BTRFS_I(new_inode));
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009363 u64 old_idx = 0;
9364 u64 new_idx = 0;
9365 u64 root_objectid;
9366 int ret;
Filipe Manana86e8aa02016-05-05 02:02:27 +01009367 bool root_log_pinned = false;
9368 bool dest_log_pinned = false;
Filipe Mananad4682ba2018-06-11 19:24:28 +01009369 struct btrfs_log_ctx ctx_root;
9370 struct btrfs_log_ctx ctx_dest;
9371 bool sync_log_root = false;
9372 bool sync_log_dest = false;
9373 bool commit_transaction = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009374
9375 /* we only allow rename subvolume link between subvolumes */
9376 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
9377 return -EXDEV;
9378
Filipe Mananad4682ba2018-06-11 19:24:28 +01009379 btrfs_init_log_ctx(&ctx_root, old_inode);
9380 btrfs_init_log_ctx(&ctx_dest, new_inode);
9381
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009382 /* close the race window with snapshot create/destroy ioctl */
9383 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009384 down_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009385 if (new_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009386 down_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009387
9388 /*
9389 * We want to reserve the absolute worst case amount of items. So if
9390 * both inodes are subvols and we need to unlink them then that would
9391 * require 4 item modifications, but if they are both normal inodes it
9392 * would require 5 item modifications, so we'll assume their normal
9393 * inodes. So 5 * 2 is 10, plus 2 for the new links, so 12 total items
9394 * should cover the worst case number of items we'll modify.
9395 */
9396 trans = btrfs_start_transaction(root, 12);
9397 if (IS_ERR(trans)) {
9398 ret = PTR_ERR(trans);
9399 goto out_notrans;
9400 }
9401
9402 /*
9403 * We need to find a free sequence number both in the source and
9404 * in the destination directory for the exchange.
9405 */
Nikolay Borisov877574e2017-02-20 13:50:33 +02009406 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009407 if (ret)
9408 goto out_fail;
Nikolay Borisov877574e2017-02-20 13:50:33 +02009409 ret = btrfs_set_inode_index(BTRFS_I(old_dir), &new_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009410 if (ret)
9411 goto out_fail;
9412
9413 BTRFS_I(old_inode)->dir_index = 0ULL;
9414 BTRFS_I(new_inode)->dir_index = 0ULL;
9415
9416 /* Reference for the source. */
9417 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
9418 /* force full log commit if subvolume involved. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009419 btrfs_set_log_full_commit(fs_info, trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009420 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01009421 btrfs_pin_log_trans(root);
9422 root_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009423 ret = btrfs_insert_inode_ref(trans, dest,
9424 new_dentry->d_name.name,
9425 new_dentry->d_name.len,
9426 old_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01009427 btrfs_ino(BTRFS_I(new_dir)),
9428 old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009429 if (ret)
9430 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009431 }
9432
9433 /* And now for the dest. */
9434 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
9435 /* force full log commit if subvolume involved. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009436 btrfs_set_log_full_commit(fs_info, trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009437 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01009438 btrfs_pin_log_trans(dest);
9439 dest_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009440 ret = btrfs_insert_inode_ref(trans, root,
9441 old_dentry->d_name.name,
9442 old_dentry->d_name.len,
9443 new_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01009444 btrfs_ino(BTRFS_I(old_dir)),
9445 new_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009446 if (ret)
9447 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009448 }
9449
9450 /* Update inode version and ctime/mtime. */
9451 inode_inc_iversion(old_dir);
9452 inode_inc_iversion(new_dir);
9453 inode_inc_iversion(old_inode);
9454 inode_inc_iversion(new_inode);
9455 old_dir->i_ctime = old_dir->i_mtime = ctime;
9456 new_dir->i_ctime = new_dir->i_mtime = ctime;
9457 old_inode->i_ctime = ctime;
9458 new_inode->i_ctime = ctime;
9459
9460 if (old_dentry->d_parent != new_dentry->d_parent) {
David Sterbaf85b7372017-01-20 14:54:07 +01009461 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
9462 BTRFS_I(old_inode), 1);
9463 btrfs_record_unlink_dir(trans, BTRFS_I(new_dir),
9464 BTRFS_I(new_inode), 1);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009465 }
9466
9467 /* src is a subvolume */
9468 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
9469 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
Lu Fengqi401b3b12018-08-01 11:32:30 +08009470 ret = btrfs_unlink_subvol(trans, old_dir, root_objectid,
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009471 old_dentry->d_name.name,
9472 old_dentry->d_name.len);
9473 } else { /* src is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009474 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
9475 BTRFS_I(old_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009476 old_dentry->d_name.name,
9477 old_dentry->d_name.len);
9478 if (!ret)
9479 ret = btrfs_update_inode(trans, root, old_inode);
9480 }
9481 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009482 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009483 goto out_fail;
9484 }
9485
9486 /* dest is a subvolume */
9487 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
9488 root_objectid = BTRFS_I(new_inode)->root->root_key.objectid;
Lu Fengqi401b3b12018-08-01 11:32:30 +08009489 ret = btrfs_unlink_subvol(trans, new_dir, root_objectid,
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009490 new_dentry->d_name.name,
9491 new_dentry->d_name.len);
9492 } else { /* dest is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009493 ret = __btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
9494 BTRFS_I(new_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009495 new_dentry->d_name.name,
9496 new_dentry->d_name.len);
9497 if (!ret)
9498 ret = btrfs_update_inode(trans, dest, new_inode);
9499 }
9500 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009501 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009502 goto out_fail;
9503 }
9504
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009505 ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009506 new_dentry->d_name.name,
9507 new_dentry->d_name.len, 0, old_idx);
9508 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009509 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009510 goto out_fail;
9511 }
9512
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009513 ret = btrfs_add_link(trans, BTRFS_I(old_dir), BTRFS_I(new_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009514 old_dentry->d_name.name,
9515 old_dentry->d_name.len, 0, new_idx);
9516 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009517 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009518 goto out_fail;
9519 }
9520
9521 if (old_inode->i_nlink == 1)
9522 BTRFS_I(old_inode)->dir_index = old_idx;
9523 if (new_inode->i_nlink == 1)
9524 BTRFS_I(new_inode)->dir_index = new_idx;
9525
Filipe Manana86e8aa02016-05-05 02:02:27 +01009526 if (root_log_pinned) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009527 parent = new_dentry->d_parent;
Filipe Mananad4682ba2018-06-11 19:24:28 +01009528 ret = btrfs_log_new_name(trans, BTRFS_I(old_inode),
9529 BTRFS_I(old_dir), parent,
9530 false, &ctx_root);
9531 if (ret == BTRFS_NEED_LOG_SYNC)
9532 sync_log_root = true;
9533 else if (ret == BTRFS_NEED_TRANS_COMMIT)
9534 commit_transaction = true;
9535 ret = 0;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009536 btrfs_end_log_trans(root);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009537 root_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009538 }
Filipe Manana86e8aa02016-05-05 02:02:27 +01009539 if (dest_log_pinned) {
Filipe Mananad4682ba2018-06-11 19:24:28 +01009540 if (!commit_transaction) {
9541 parent = old_dentry->d_parent;
9542 ret = btrfs_log_new_name(trans, BTRFS_I(new_inode),
9543 BTRFS_I(new_dir), parent,
9544 false, &ctx_dest);
9545 if (ret == BTRFS_NEED_LOG_SYNC)
9546 sync_log_dest = true;
9547 else if (ret == BTRFS_NEED_TRANS_COMMIT)
9548 commit_transaction = true;
9549 ret = 0;
9550 }
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009551 btrfs_end_log_trans(dest);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009552 dest_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009553 }
9554out_fail:
Filipe Manana86e8aa02016-05-05 02:02:27 +01009555 /*
9556 * If we have pinned a log and an error happened, we unpin tasks
9557 * trying to sync the log and force them to fallback to a transaction
9558 * commit if the log currently contains any of the inodes involved in
9559 * this rename operation (to ensure we do not persist a log with an
9560 * inconsistent state for any of these inodes or leading to any
9561 * inconsistencies when replayed). If the transaction was aborted, the
9562 * abortion reason is propagated to userspace when attempting to commit
9563 * the transaction. If the log does not contain any of these inodes, we
9564 * allow the tasks to sync it.
9565 */
9566 if (ret && (root_log_pinned || dest_log_pinned)) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009567 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
9568 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
9569 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana86e8aa02016-05-05 02:02:27 +01009570 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009571 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009572 btrfs_set_log_full_commit(fs_info, trans);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009573
9574 if (root_log_pinned) {
9575 btrfs_end_log_trans(root);
9576 root_log_pinned = false;
9577 }
9578 if (dest_log_pinned) {
9579 btrfs_end_log_trans(dest);
9580 dest_log_pinned = false;
9581 }
9582 }
Filipe Mananad4682ba2018-06-11 19:24:28 +01009583 if (!ret && sync_log_root && !commit_transaction) {
9584 ret = btrfs_sync_log(trans, BTRFS_I(old_inode)->root,
9585 &ctx_root);
9586 if (ret)
9587 commit_transaction = true;
9588 }
9589 if (!ret && sync_log_dest && !commit_transaction) {
9590 ret = btrfs_sync_log(trans, BTRFS_I(new_inode)->root,
9591 &ctx_dest);
9592 if (ret)
9593 commit_transaction = true;
9594 }
9595 if (commit_transaction) {
9596 ret = btrfs_commit_transaction(trans);
9597 } else {
9598 int ret2;
9599
9600 ret2 = btrfs_end_transaction(trans);
9601 ret = ret ? ret : ret2;
9602 }
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009603out_notrans:
9604 if (new_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009605 up_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009606 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009607 up_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009608
9609 return ret;
9610}
9611
9612static int btrfs_whiteout_for_rename(struct btrfs_trans_handle *trans,
9613 struct btrfs_root *root,
9614 struct inode *dir,
9615 struct dentry *dentry)
9616{
9617 int ret;
9618 struct inode *inode;
9619 u64 objectid;
9620 u64 index;
9621
9622 ret = btrfs_find_free_ino(root, &objectid);
9623 if (ret)
9624 return ret;
9625
9626 inode = btrfs_new_inode(trans, root, dir,
9627 dentry->d_name.name,
9628 dentry->d_name.len,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009629 btrfs_ino(BTRFS_I(dir)),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009630 objectid,
9631 S_IFCHR | WHITEOUT_MODE,
9632 &index);
9633
9634 if (IS_ERR(inode)) {
9635 ret = PTR_ERR(inode);
9636 return ret;
9637 }
9638
9639 inode->i_op = &btrfs_special_inode_operations;
9640 init_special_inode(inode, inode->i_mode,
9641 WHITEOUT_DEV);
9642
9643 ret = btrfs_init_inode_security(trans, inode, dir,
9644 &dentry->d_name);
9645 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009646 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009647
Nikolay Borisovcef415a2017-02-20 13:51:09 +02009648 ret = btrfs_add_nondir(trans, BTRFS_I(dir), dentry,
9649 BTRFS_I(inode), 0, index);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009650 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009651 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009652
9653 ret = btrfs_update_inode(trans, root, inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009654out:
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009655 unlock_new_inode(inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009656 if (ret)
9657 inode_dec_link_count(inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009658 iput(inode);
9659
Filipe Mananac9901612016-05-05 01:41:57 +01009660 return ret;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009661}
9662
Chris Mason39279cc2007-06-12 06:35:45 -04009663static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009664 struct inode *new_dir, struct dentry *new_dentry,
9665 unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04009666{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009667 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009668 struct btrfs_trans_handle *trans;
Filipe Manana5062af32016-05-05 10:26:26 +01009669 unsigned int trans_num_items;
Chris Mason39279cc2007-06-12 06:35:45 -04009670 struct btrfs_root *root = BTRFS_I(old_dir)->root;
9671 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00009672 struct inode *new_inode = d_inode(new_dentry);
9673 struct inode *old_inode = d_inode(old_dentry);
Chris Mason00e4e6b2008-08-05 11:18:09 -04009674 u64 index = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009675 u64 root_objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04009676 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009677 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009678 bool log_pinned = false;
Filipe Mananad4682ba2018-06-11 19:24:28 +01009679 struct btrfs_log_ctx ctx;
9680 bool sync_log = false;
9681 bool commit_transaction = false;
Chris Mason39279cc2007-06-12 06:35:45 -04009682
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009683 if (btrfs_ino(BTRFS_I(new_dir)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04009684 return -EPERM;
9685
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009686 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08009687 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05009688 return -EXDEV;
9689
Li Zefan33345d012011-04-20 10:31:50 +08009690 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009691 (new_inode && btrfs_ino(BTRFS_I(new_inode)) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009692 return -ENOTEMPTY;
9693
Chris Mason39279cc2007-06-12 06:35:45 -04009694 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009695 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04009696 return -ENOTEMPTY;
Chris Mason9c520572012-12-17 14:26:57 -05009697
9698
9699 /* check for collisions, even if the name isn't there */
Josef Bacik4871c152013-10-09 12:24:04 -04009700 ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino,
Chris Mason9c520572012-12-17 14:26:57 -05009701 new_dentry->d_name.name,
9702 new_dentry->d_name.len);
9703
9704 if (ret) {
9705 if (ret == -EEXIST) {
9706 /* we shouldn't get
9707 * eexist without a new_inode */
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05309708 if (WARN_ON(!new_inode)) {
Chris Mason9c520572012-12-17 14:26:57 -05009709 return ret;
9710 }
9711 } else {
9712 /* maybe -EOVERFLOW */
9713 return ret;
9714 }
9715 }
9716 ret = 0;
9717
Chris Mason5a3f23d2009-03-31 13:27:11 -04009718 /*
Chris Mason8d875f92014-08-12 10:47:42 -07009719 * we're using rename to replace one file with another. Start IO on it
9720 * now so we don't add too much work to the end of the transaction
Chris Mason5a3f23d2009-03-31 13:27:11 -04009721 */
Chris Mason8d875f92014-08-12 10:47:42 -07009722 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size)
Chris Mason5a3f23d2009-03-31 13:27:11 -04009723 filemap_flush(old_inode->i_mapping);
9724
Yan, Zheng76dda932009-09-21 16:00:26 -04009725 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08009726 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009727 down_read(&fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04009728 /*
9729 * We want to reserve the absolute worst case amount of items. So if
9730 * both inodes are subvols and we need to unlink them then that would
9731 * require 4 item modifications, but if they are both normal inodes it
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009732 * would require 5 item modifications, so we'll assume they are normal
Yan, Zhenga22285a2010-05-16 10:48:46 -04009733 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
9734 * should cover the worst case number of items we'll modify.
Filipe Manana5062af32016-05-05 10:26:26 +01009735 * If our rename has the whiteout flag, we need more 5 units for the
9736 * new inode (1 inode item, 1 inode ref, 2 dir items and 1 xattr item
9737 * when selinux is enabled).
Yan, Zhenga22285a2010-05-16 10:48:46 -04009738 */
Filipe Manana5062af32016-05-05 10:26:26 +01009739 trans_num_items = 11;
9740 if (flags & RENAME_WHITEOUT)
9741 trans_num_items += 5;
9742 trans = btrfs_start_transaction(root, trans_num_items);
Johann Lombardib44c59a2011-03-31 13:23:47 +00009743 if (IS_ERR(trans)) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009744 ret = PTR_ERR(trans);
9745 goto out_notrans;
9746 }
Chris Mason5f39d392007-10-15 16:14:19 -04009747
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009748 if (dest != root)
9749 btrfs_record_root_in_trans(trans, dest);
9750
Nikolay Borisov877574e2017-02-20 13:50:33 +02009751 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009752 if (ret)
9753 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009754
Miao Xie67de1172013-12-26 13:07:06 +08009755 BTRFS_I(old_inode)->dir_index = 0ULL;
Li Zefan33345d012011-04-20 10:31:50 +08009756 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009757 /* force full log commit if subvolume involved. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009758 btrfs_set_log_full_commit(fs_info, trans);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009759 } else {
Filipe Mananac4aba952016-04-29 13:14:42 +01009760 btrfs_pin_log_trans(root);
9761 log_pinned = true;
Yan, Zhenga5719522009-09-24 09:17:31 -04009762 ret = btrfs_insert_inode_ref(trans, dest,
9763 new_dentry->d_name.name,
9764 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08009765 old_ino,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009766 btrfs_ino(BTRFS_I(new_dir)), index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009767 if (ret)
9768 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009769 }
Chris Mason5a3f23d2009-03-31 13:27:11 -04009770
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009771 inode_inc_iversion(old_dir);
9772 inode_inc_iversion(new_dir);
9773 inode_inc_iversion(old_inode);
Deepa Dinamani04b285f2016-02-06 23:57:21 -08009774 old_dir->i_ctime = old_dir->i_mtime =
9775 new_dir->i_ctime = new_dir->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009776 old_inode->i_ctime = current_time(old_dir);
Chris Mason5f39d392007-10-15 16:14:19 -04009777
Chris Mason12fcfd22009-03-24 10:24:20 -04009778 if (old_dentry->d_parent != new_dentry->d_parent)
David Sterbaf85b7372017-01-20 14:54:07 +01009779 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
9780 BTRFS_I(old_inode), 1);
Chris Mason12fcfd22009-03-24 10:24:20 -04009781
Li Zefan33345d012011-04-20 10:31:50 +08009782 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009783 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
Lu Fengqi401b3b12018-08-01 11:32:30 +08009784 ret = btrfs_unlink_subvol(trans, old_dir, root_objectid,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009785 old_dentry->d_name.name,
9786 old_dentry->d_name.len);
9787 } else {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009788 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
9789 BTRFS_I(d_inode(old_dentry)),
Al Viro92986792011-03-04 17:14:37 +00009790 old_dentry->d_name.name,
9791 old_dentry->d_name.len);
9792 if (!ret)
9793 ret = btrfs_update_inode(trans, root, old_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009794 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009795 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009796 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009797 goto out_fail;
9798 }
Chris Mason39279cc2007-06-12 06:35:45 -04009799
9800 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009801 inode_inc_iversion(new_inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009802 new_inode->i_ctime = current_time(new_inode);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009803 if (unlikely(btrfs_ino(BTRFS_I(new_inode)) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009804 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
9805 root_objectid = BTRFS_I(new_inode)->location.objectid;
Lu Fengqi401b3b12018-08-01 11:32:30 +08009806 ret = btrfs_unlink_subvol(trans, new_dir, root_objectid,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009807 new_dentry->d_name.name,
9808 new_dentry->d_name.len);
9809 BUG_ON(new_inode->i_nlink == 0);
9810 } else {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009811 ret = btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
9812 BTRFS_I(d_inode(new_dentry)),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009813 new_dentry->d_name.name,
9814 new_dentry->d_name.len);
9815 }
Josef Bacik4ef31a42013-08-13 14:10:08 -04009816 if (!ret && new_inode->i_nlink == 0)
Nikolay Borisov73f2e542017-02-20 13:50:59 +02009817 ret = btrfs_orphan_add(trans,
9818 BTRFS_I(d_inode(new_dentry)));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009819 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009820 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009821 goto out_fail;
9822 }
Chris Mason39279cc2007-06-12 06:35:45 -04009823 }
Josef Bacikaec74772008-07-24 12:12:38 -04009824
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009825 ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009826 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04009827 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009828 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009829 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009830 goto out_fail;
9831 }
Chris Mason39279cc2007-06-12 06:35:45 -04009832
Miao Xie67de1172013-12-26 13:07:06 +08009833 if (old_inode->i_nlink == 1)
9834 BTRFS_I(old_inode)->dir_index = index;
9835
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009836 if (log_pinned) {
Al Viro10d9f302011-07-16 23:09:10 -04009837 struct dentry *parent = new_dentry->d_parent;
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009838
Filipe Mananad4682ba2018-06-11 19:24:28 +01009839 btrfs_init_log_ctx(&ctx, old_inode);
9840 ret = btrfs_log_new_name(trans, BTRFS_I(old_inode),
9841 BTRFS_I(old_dir), parent,
9842 false, &ctx);
9843 if (ret == BTRFS_NEED_LOG_SYNC)
9844 sync_log = true;
9845 else if (ret == BTRFS_NEED_TRANS_COMMIT)
9846 commit_transaction = true;
9847 ret = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009848 btrfs_end_log_trans(root);
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009849 log_pinned = false;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009850 }
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009851
9852 if (flags & RENAME_WHITEOUT) {
9853 ret = btrfs_whiteout_for_rename(trans, root, old_dir,
9854 old_dentry);
9855
9856 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009857 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009858 goto out_fail;
9859 }
Chris Mason12fcfd22009-03-24 10:24:20 -04009860 }
Chris Mason39279cc2007-06-12 06:35:45 -04009861out_fail:
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009862 /*
9863 * If we have pinned the log and an error happened, we unpin tasks
9864 * trying to sync the log and force them to fallback to a transaction
9865 * commit if the log currently contains any of the inodes involved in
9866 * this rename operation (to ensure we do not persist a log with an
9867 * inconsistent state for any of these inodes or leading to any
9868 * inconsistencies when replayed). If the transaction was aborted, the
9869 * abortion reason is propagated to userspace when attempting to commit
9870 * the transaction. If the log does not contain any of these inodes, we
9871 * allow the tasks to sync it.
9872 */
9873 if (ret && log_pinned) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009874 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
9875 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
9876 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009877 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009878 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009879 btrfs_set_log_full_commit(fs_info, trans);
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009880
9881 btrfs_end_log_trans(root);
9882 log_pinned = false;
9883 }
Filipe Mananad4682ba2018-06-11 19:24:28 +01009884 if (!ret && sync_log) {
9885 ret = btrfs_sync_log(trans, BTRFS_I(old_inode)->root, &ctx);
9886 if (ret)
9887 commit_transaction = true;
9888 }
9889 if (commit_transaction) {
9890 ret = btrfs_commit_transaction(trans);
9891 } else {
9892 int ret2;
9893
9894 ret2 = btrfs_end_transaction(trans);
9895 ret = ret ? ret : ret2;
9896 }
Johann Lombardib44c59a2011-03-31 13:23:47 +00009897out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +08009898 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009899 up_read(&fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009900
Chris Mason39279cc2007-06-12 06:35:45 -04009901 return ret;
9902}
9903
Miklos Szeredi80ace852014-07-23 15:15:32 +02009904static int btrfs_rename2(struct inode *old_dir, struct dentry *old_dentry,
9905 struct inode *new_dir, struct dentry *new_dentry,
9906 unsigned int flags)
9907{
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009908 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
Miklos Szeredi80ace852014-07-23 15:15:32 +02009909 return -EINVAL;
9910
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009911 if (flags & RENAME_EXCHANGE)
9912 return btrfs_rename_exchange(old_dir, old_dentry, new_dir,
9913 new_dentry);
9914
9915 return btrfs_rename(old_dir, old_dentry, new_dir, new_dentry, flags);
Miklos Szeredi80ace852014-07-23 15:15:32 +02009916}
9917
Nikolay Borisov3a2f8c02018-04-24 17:23:59 +03009918struct btrfs_delalloc_work {
9919 struct inode *inode;
9920 struct completion completion;
9921 struct list_head list;
9922 struct btrfs_work work;
9923};
9924
Miao Xie8ccf6f192012-10-25 09:28:04 +00009925static void btrfs_run_delalloc_work(struct btrfs_work *work)
9926{
9927 struct btrfs_delalloc_work *delalloc_work;
Josef Bacik9f23e282013-10-28 15:03:41 -04009928 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009929
9930 delalloc_work = container_of(work, struct btrfs_delalloc_work,
9931 work);
Josef Bacik9f23e282013-10-28 15:03:41 -04009932 inode = delalloc_work->inode;
David Sterba30424602015-11-27 19:27:11 +01009933 filemap_flush(inode->i_mapping);
9934 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
9935 &BTRFS_I(inode)->runtime_flags))
Josef Bacik9f23e282013-10-28 15:03:41 -04009936 filemap_flush(inode->i_mapping);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009937
Nikolay Borisov076da912018-04-23 10:54:16 +03009938 iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009939 complete(&delalloc_work->completion);
9940}
9941
Nikolay Borisov3a2f8c02018-04-24 17:23:59 +03009942static struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode)
Miao Xie8ccf6f192012-10-25 09:28:04 +00009943{
9944 struct btrfs_delalloc_work *work;
9945
David Sterba100d5702015-12-08 14:39:32 +01009946 work = kmalloc(sizeof(*work), GFP_NOFS);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009947 if (!work)
9948 return NULL;
9949
9950 init_completion(&work->completion);
9951 INIT_LIST_HEAD(&work->list);
9952 work->inode = inode;
Liu Bo9e0af232014-08-15 23:36:53 +08009953 WARN_ON_ONCE(!inode);
9954 btrfs_init_work(&work->work, btrfs_flush_delalloc_helper,
9955 btrfs_run_delalloc_work, NULL, NULL);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009956
9957 return work;
9958}
9959
Chris Masond352ac62008-09-29 15:18:18 -04009960/*
9961 * some fairly slow code that needs optimization. This walks the list
9962 * of all the inodes with pending delalloc and forces them to disk.
9963 */
Ethan Lien3cd24c62018-11-01 14:49:03 +08009964static int start_delalloc_inodes(struct btrfs_root *root, int nr, bool snapshot)
Chris Masonea8c2812008-08-04 23:17:27 -04009965{
Chris Masonea8c2812008-08-04 23:17:27 -04009966 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009967 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009968 struct btrfs_delalloc_work *work, *next;
9969 struct list_head works;
Miao Xie1eafa6c2013-01-22 10:49:00 +00009970 struct list_head splice;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009971 int ret = 0;
Chris Masonea8c2812008-08-04 23:17:27 -04009972
Miao Xie8ccf6f192012-10-25 09:28:04 +00009973 INIT_LIST_HEAD(&works);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009974 INIT_LIST_HEAD(&splice);
Miao Xie63607cc2013-01-22 10:50:35 +00009975
Miao Xie573bfb72014-03-06 13:55:03 +08009976 mutex_lock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009977 spin_lock(&root->delalloc_lock);
9978 list_splice_init(&root->delalloc_inodes, &splice);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009979 while (!list_empty(&splice)) {
9980 binode = list_entry(splice.next, struct btrfs_inode,
Chris Masonea8c2812008-08-04 23:17:27 -04009981 delalloc_inodes);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009982
Miao Xieeb73c1b2013-05-15 07:48:22 +00009983 list_move_tail(&binode->delalloc_inodes,
9984 &root->delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009985 inode = igrab(&binode->vfs_inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00009986 if (!inode) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00009987 cond_resched_lock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009988 continue;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009989 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00009990 spin_unlock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009991
Ethan Lien3cd24c62018-11-01 14:49:03 +08009992 if (snapshot)
9993 set_bit(BTRFS_INODE_SNAPSHOT_FLUSH,
9994 &binode->runtime_flags);
Nikolay Borisov076da912018-04-23 10:54:16 +03009995 work = btrfs_alloc_delalloc_work(inode);
David Sterba5d99a9982014-09-29 19:20:37 +02009996 if (!work) {
Nikolay Borisov4fbb5142018-04-23 10:54:15 +03009997 iput(inode);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009998 ret = -ENOMEM;
Wang Shilonga1ecaab2014-04-02 19:53:32 +08009999 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010000 }
Miao Xie1eafa6c2013-01-22 10:49:00 +000010001 list_add_tail(&work->list, &works);
Qu Wenruoa44903a2014-02-28 10:46:09 +080010002 btrfs_queue_work(root->fs_info->flush_workers,
10003 &work->work);
Miao Xie6c255e62014-03-06 13:55:01 +080010004 ret++;
10005 if (nr != -1 && ret >= nr)
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010006 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010007 cond_resched();
Miao Xieeb73c1b2013-05-15 07:48:22 +000010008 spin_lock(&root->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -040010009 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010010 spin_unlock(&root->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -040010011
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010012out:
Miao Xie1eafa6c2013-01-22 10:49:00 +000010013 list_for_each_entry_safe(work, next, &works, list) {
10014 list_del_init(&work->list);
Nikolay Borisov40012f92018-04-19 10:46:39 +030010015 wait_for_completion(&work->completion);
10016 kfree(work);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010017 }
10018
Nikolay Borisov81f1d392018-04-19 10:46:37 +030010019 if (!list_empty(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010020 spin_lock(&root->delalloc_lock);
10021 list_splice_tail(&splice, &root->delalloc_inodes);
10022 spin_unlock(&root->delalloc_lock);
10023 }
Miao Xie573bfb72014-03-06 13:55:03 +080010024 mutex_unlock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010025 return ret;
10026}
10027
Ethan Lien3cd24c62018-11-01 14:49:03 +080010028int btrfs_start_delalloc_snapshot(struct btrfs_root *root)
Miao Xieeb73c1b2013-05-15 07:48:22 +000010029{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010030 struct btrfs_fs_info *fs_info = root->fs_info;
Miao Xieeb73c1b2013-05-15 07:48:22 +000010031 int ret;
10032
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010033 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +000010034 return -EROFS;
10035
Ethan Lien3cd24c62018-11-01 14:49:03 +080010036 ret = start_delalloc_inodes(root, -1, true);
Miao Xie6c255e62014-03-06 13:55:01 +080010037 if (ret > 0)
10038 ret = 0;
Miao Xieeb73c1b2013-05-15 07:48:22 +000010039 return ret;
10040}
10041
Nikolay Borisov82b3e532018-04-23 10:54:13 +030010042int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, int nr)
Miao Xieeb73c1b2013-05-15 07:48:22 +000010043{
10044 struct btrfs_root *root;
10045 struct list_head splice;
10046 int ret;
10047
Wang Shilong2c21b4d2014-01-14 19:42:20 +080010048 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +000010049 return -EROFS;
10050
10051 INIT_LIST_HEAD(&splice);
10052
Miao Xie573bfb72014-03-06 13:55:03 +080010053 mutex_lock(&fs_info->delalloc_root_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010054 spin_lock(&fs_info->delalloc_root_lock);
10055 list_splice_init(&fs_info->delalloc_roots, &splice);
Miao Xie6c255e62014-03-06 13:55:01 +080010056 while (!list_empty(&splice) && nr) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010057 root = list_first_entry(&splice, struct btrfs_root,
10058 delalloc_root);
10059 root = btrfs_grab_fs_root(root);
10060 BUG_ON(!root);
10061 list_move_tail(&root->delalloc_root,
10062 &fs_info->delalloc_roots);
10063 spin_unlock(&fs_info->delalloc_root_lock);
10064
Ethan Lien3cd24c62018-11-01 14:49:03 +080010065 ret = start_delalloc_inodes(root, nr, false);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010066 btrfs_put_fs_root(root);
Miao Xie6c255e62014-03-06 13:55:01 +080010067 if (ret < 0)
Miao Xieeb73c1b2013-05-15 07:48:22 +000010068 goto out;
10069
Miao Xie6c255e62014-03-06 13:55:01 +080010070 if (nr != -1) {
10071 nr -= ret;
10072 WARN_ON(nr < 0);
10073 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010074 spin_lock(&fs_info->delalloc_root_lock);
10075 }
10076 spin_unlock(&fs_info->delalloc_root_lock);
10077
Miao Xie6c255e62014-03-06 13:55:01 +080010078 ret = 0;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010079out:
Nikolay Borisov81f1d392018-04-19 10:46:37 +030010080 if (!list_empty(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010081 spin_lock(&fs_info->delalloc_root_lock);
10082 list_splice_tail(&splice, &fs_info->delalloc_roots);
10083 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010084 }
Miao Xie573bfb72014-03-06 13:55:03 +080010085 mutex_unlock(&fs_info->delalloc_root_mutex);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010086 return ret;
Chris Masonea8c2812008-08-04 23:17:27 -040010087}
10088
Chris Mason39279cc2007-06-12 06:35:45 -040010089static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
10090 const char *symname)
10091{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010092 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -040010093 struct btrfs_trans_handle *trans;
10094 struct btrfs_root *root = BTRFS_I(dir)->root;
10095 struct btrfs_path *path;
10096 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -050010097 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -040010098 int err;
Chris Mason39279cc2007-06-12 06:35:45 -040010099 u64 objectid;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +053010100 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -040010101 int name_len;
10102 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -040010103 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -040010104 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -040010105 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -040010106
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +010010107 name_len = strlen(symname);
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010108 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info))
Chris Mason39279cc2007-06-12 06:35:45 -040010109 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -050010110
Josef Bacik9ed74f22009-09-11 16:12:44 -040010111 /*
10112 * 2 items for inode item and ref
10113 * 2 items for dir items
Filipe Manana9269d122015-12-31 18:16:29 +000010114 * 1 item for updating parent inode item
10115 * 1 item for the inline extent item
Josef Bacik9ed74f22009-09-11 16:12:44 -040010116 * 1 item for xattr if selinux is on
10117 */
Filipe Manana9269d122015-12-31 18:16:29 +000010118 trans = btrfs_start_transaction(root, 7);
Yan, Zhenga22285a2010-05-16 10:48:46 -040010119 if (IS_ERR(trans))
10120 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -050010121
Li Zefan581bb052011-04-20 10:06:11 +080010122 err = btrfs_find_free_ino(root, &objectid);
10123 if (err)
10124 goto out_unlock;
10125
Josef Bacikaec74772008-07-24 12:12:38 -040010126 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +010010127 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)),
10128 objectid, S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -040010129 if (IS_ERR(inode)) {
10130 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -040010131 inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -040010132 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -040010133 }
Chris Mason39279cc2007-06-12 06:35:45 -040010134
Casey Schauflerad19db72011-12-15 10:09:07 -050010135 /*
10136 * If the active LSM wants to access the inode during
10137 * d_instantiate it needs these. Smack checks to see
10138 * if the filesystem supports xattrs by looking at the
10139 * ops vector.
10140 */
10141 inode->i_fop = &btrfs_file_operations;
10142 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -070010143 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -070010144 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
10145
10146 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
10147 if (err)
Al Viro32955c52018-05-16 12:20:05 -040010148 goto out_unlock;
Casey Schauflerad19db72011-12-15 10:09:07 -050010149
Chris Mason39279cc2007-06-12 06:35:45 -040010150 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -070010151 if (!path) {
10152 err = -ENOMEM;
Al Viro32955c52018-05-16 12:20:05 -040010153 goto out_unlock;
Mark Fashehd8926bb2011-07-13 10:38:47 -070010154 }
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +020010155 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason39279cc2007-06-12 06:35:45 -040010156 key.offset = 0;
David Sterba962a2982014-06-04 18:41:45 +020010157 key.type = BTRFS_EXTENT_DATA_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -040010158 datasize = btrfs_file_extent_calc_inline_size(name_len);
10159 err = btrfs_insert_empty_item(trans, root, path, &key,
10160 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -040010161 if (err) {
Julia Lawallb0839162011-05-14 07:10:51 +000010162 btrfs_free_path(path);
Al Viro32955c52018-05-16 12:20:05 -040010163 goto out_unlock;
Chris Mason54aa1f42007-06-22 14:16:25 -040010164 }
Chris Mason5f39d392007-10-15 16:14:19 -040010165 leaf = path->nodes[0];
10166 ei = btrfs_item_ptr(leaf, path->slots[0],
10167 struct btrfs_file_extent_item);
10168 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
10169 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -040010170 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -040010171 btrfs_set_file_extent_encryption(leaf, ei, 0);
10172 btrfs_set_file_extent_compression(leaf, ei, 0);
10173 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
10174 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
10175
Chris Mason39279cc2007-06-12 06:35:45 -040010176 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -040010177 write_extent_buffer(leaf, symname, ptr, name_len);
10178 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -040010179 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -040010180
Chris Mason39279cc2007-06-12 06:35:45 -040010181 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -050010182 inode_nohighmem(inode);
Omar Sandoval4779cc02018-09-24 15:16:55 -070010183 inode->i_mapping->a_ops = &btrfs_aops;
Yan Zhengd899e052008-10-30 14:25:28 -040010184 inode_set_bytes(inode, name_len);
Nikolay Borisov6ef06d22017-02-20 13:50:34 +020010185 btrfs_i_size_write(BTRFS_I(inode), name_len);
Chris Mason54aa1f42007-06-22 14:16:25 -040010186 err = btrfs_update_inode(trans, root, inode);
Filipe Mananad50866d2015-12-31 18:08:24 +000010187 /*
10188 * Last step, add directory indexes for our symlink inode. This is the
10189 * last step to avoid extra cleanup of these indexes if an error happens
10190 * elsewhere above.
10191 */
10192 if (!err)
Nikolay Borisovcef415a2017-02-20 13:51:09 +020010193 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry,
10194 BTRFS_I(inode), 0, index);
Al Viro32955c52018-05-16 12:20:05 -040010195 if (err)
10196 goto out_unlock;
Chris Masonb0d5d102014-09-08 13:08:51 -070010197
Al Viro1e2e5472018-05-04 08:23:01 -040010198 d_instantiate_new(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010199
10200out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010201 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -040010202 if (err && inode) {
Chris Mason39279cc2007-06-12 06:35:45 -040010203 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -040010204 discard_new_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010205 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010206 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -040010207 return err;
10208}
Chris Mason16432982008-04-10 10:23:21 -040010209
Josef Bacik0af3d002010-06-21 14:48:16 -040010210static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
10211 u64 start, u64 num_bytes, u64 min_size,
10212 loff_t actual_len, u64 *alloc_hint,
10213 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -040010214{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010215 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik5dc562c2012-08-17 13:14:17 -040010216 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
10217 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -040010218 struct btrfs_root *root = BTRFS_I(inode)->root;
10219 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -040010220 u64 cur_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +000010221 u64 i_size;
Chris Mason154ea282013-03-05 11:11:26 -050010222 u64 cur_bytes;
Josef Bacik0b670dc2015-09-23 17:11:16 -040010223 u64 last_alloc = (u64)-1;
Yan Zhengd899e052008-10-30 14:25:28 -040010224 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -040010225 bool own_trans = true;
Wang Xiaoguang18513092016-07-25 15:51:40 +080010226 u64 end = start + num_bytes - 1;
Yan Zhengd899e052008-10-30 14:25:28 -040010227
Josef Bacik0af3d002010-06-21 14:48:16 -040010228 if (trans)
10229 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -040010230 while (num_bytes > 0) {
Josef Bacik0af3d002010-06-21 14:48:16 -040010231 if (own_trans) {
10232 trans = btrfs_start_transaction(root, 3);
10233 if (IS_ERR(trans)) {
10234 ret = PTR_ERR(trans);
10235 break;
10236 }
Yan Zhengd899e052008-10-30 14:25:28 -040010237 }
Yan, Zheng5a303d52009-11-12 09:34:52 +000010238
Byongho Leeee221842015-12-15 01:42:10 +090010239 cur_bytes = min_t(u64, num_bytes, SZ_256M);
Chris Mason154ea282013-03-05 11:11:26 -050010240 cur_bytes = max(cur_bytes, min_size);
Josef Bacik0b670dc2015-09-23 17:11:16 -040010241 /*
10242 * If we are severely fragmented we could end up with really
10243 * small allocations, so if the allocator is returning small
10244 * chunks lets make its job easier by only searching for those
10245 * sized chunks.
10246 */
10247 cur_bytes = min(cur_bytes, last_alloc);
Wang Xiaoguang18513092016-07-25 15:51:40 +080010248 ret = btrfs_reserve_extent(root, cur_bytes, cur_bytes,
10249 min_size, 0, *alloc_hint, &ins, 1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010250 if (ret) {
Josef Bacik0af3d002010-06-21 14:48:16 -040010251 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010252 btrfs_end_transaction(trans);
Yan, Zhenga22285a2010-05-16 10:48:46 -040010253 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +000010254 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010255 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010256
Josef Bacik0b670dc2015-09-23 17:11:16 -040010257 last_alloc = ins.offset;
Yan Zhengd899e052008-10-30 14:25:28 -040010258 ret = insert_reserved_file_extent(trans, inode,
10259 cur_offset, ins.objectid,
10260 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +000010261 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -040010262 BTRFS_FILE_EXTENT_PREALLOC);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010263 if (ret) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010264 btrfs_free_reserved_extent(fs_info, ins.objectid,
Miao Xiee570fd22014-06-19 10:42:50 +080010265 ins.offset, 0);
Jeff Mahoney66642832016-06-10 18:19:25 -040010266 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010267 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010268 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010269 break;
10270 }
Dongsheng Yang31193212014-12-12 16:44:35 +080010271
Nikolay Borisovdcdbc052017-02-20 13:50:45 +020010272 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Chris Masona1ed8352009-09-11 12:27:37 -040010273 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010274
Josef Bacik5dc562c2012-08-17 13:14:17 -040010275 em = alloc_extent_map();
10276 if (!em) {
10277 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
10278 &BTRFS_I(inode)->runtime_flags);
10279 goto next;
10280 }
10281
10282 em->start = cur_offset;
10283 em->orig_start = cur_offset;
10284 em->len = ins.offset;
10285 em->block_start = ins.objectid;
10286 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -050010287 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -040010288 em->ram_bytes = ins.offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010289 em->bdev = fs_info->fs_devices->latest_bdev;
Josef Bacik5dc562c2012-08-17 13:14:17 -040010290 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
10291 em->generation = trans->transid;
10292
10293 while (1) {
10294 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -040010295 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -040010296 write_unlock(&em_tree->lock);
10297 if (ret != -EEXIST)
10298 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +020010299 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -040010300 cur_offset + ins.offset - 1,
10301 0);
10302 }
10303 free_extent_map(em);
10304next:
Yan Zhengd899e052008-10-30 14:25:28 -040010305 num_bytes -= ins.offset;
10306 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -040010307 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +000010308
Josef Bacik0c4d2d92012-04-05 15:03:02 -040010309 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -070010310 inode->i_ctime = current_time(inode);
Christoph Hellwig6cbff002009-04-17 10:37:41 +020010311 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -040010312 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -040010313 (actual_len > inode->i_size) &&
10314 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +000010315 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +000010316 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +000010317 else
Josef Bacik55a61d12010-11-22 18:50:32 +000010318 i_size = cur_offset;
10319 i_size_write(inode, i_size);
10320 btrfs_ordered_update_i_size(inode, i_size, NULL);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010321 }
10322
Yan Zhengd899e052008-10-30 14:25:28 -040010323 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010324
10325 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -040010326 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010327 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010328 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010329 break;
10330 }
Yan Zhengd899e052008-10-30 14:25:28 -040010331
Josef Bacik0af3d002010-06-21 14:48:16 -040010332 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010333 btrfs_end_transaction(trans);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010334 }
Wang Xiaoguang18513092016-07-25 15:51:40 +080010335 if (cur_offset < end)
Qu Wenruobc42bda2017-02-27 15:10:39 +080010336 btrfs_free_reserved_data_space(inode, NULL, cur_offset,
Wang Xiaoguang18513092016-07-25 15:51:40 +080010337 end - cur_offset + 1);
Yan Zhengd899e052008-10-30 14:25:28 -040010338 return ret;
10339}
10340
Josef Bacik0af3d002010-06-21 14:48:16 -040010341int btrfs_prealloc_file_range(struct inode *inode, int mode,
10342 u64 start, u64 num_bytes, u64 min_size,
10343 loff_t actual_len, u64 *alloc_hint)
10344{
10345 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
10346 min_size, actual_len, alloc_hint,
10347 NULL);
10348}
10349
10350int btrfs_prealloc_file_range_trans(struct inode *inode,
10351 struct btrfs_trans_handle *trans, int mode,
10352 u64 start, u64 num_bytes, u64 min_size,
10353 loff_t actual_len, u64 *alloc_hint)
10354{
10355 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
10356 min_size, actual_len, alloc_hint, trans);
10357}
10358
Chris Masone6dcd2d2008-07-17 12:53:50 -040010359static int btrfs_set_page_dirty(struct page *page)
10360{
Chris Masone6dcd2d2008-07-17 12:53:50 -040010361 return __set_page_dirty_nobuffers(page);
10362}
10363
Al Viro10556cb2011-06-20 19:28:19 -040010364static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -050010365{
Li Zefanb83cc962010-12-20 16:04:08 +080010366 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +000010367 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +080010368
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +000010369 if (mask & MAY_WRITE &&
10370 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
10371 if (btrfs_root_readonly(root))
10372 return -EROFS;
10373 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
10374 return -EACCES;
10375 }
Al Viro2830ba72011-06-20 19:16:29 -040010376 return generic_permission(inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -050010377}
Chris Mason39279cc2007-06-12 06:35:45 -040010378
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010379static int btrfs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
10380{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010381 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010382 struct btrfs_trans_handle *trans;
10383 struct btrfs_root *root = BTRFS_I(dir)->root;
10384 struct inode *inode = NULL;
10385 u64 objectid;
10386 u64 index;
10387 int ret = 0;
10388
10389 /*
10390 * 5 units required for adding orphan entry
10391 */
10392 trans = btrfs_start_transaction(root, 5);
10393 if (IS_ERR(trans))
10394 return PTR_ERR(trans);
10395
10396 ret = btrfs_find_free_ino(root, &objectid);
10397 if (ret)
10398 goto out;
10399
10400 inode = btrfs_new_inode(trans, root, dir, NULL, 0,
David Sterbaf85b7372017-01-20 14:54:07 +010010401 btrfs_ino(BTRFS_I(dir)), objectid, mode, &index);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010402 if (IS_ERR(inode)) {
10403 ret = PTR_ERR(inode);
10404 inode = NULL;
10405 goto out;
10406 }
10407
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010408 inode->i_fop = &btrfs_file_operations;
10409 inode->i_op = &btrfs_file_inode_operations;
10410
10411 inode->i_mapping->a_ops = &btrfs_aops;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010412 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
10413
Chris Masonb0d5d102014-09-08 13:08:51 -070010414 ret = btrfs_init_inode_security(trans, inode, dir, NULL);
10415 if (ret)
Al Viro32955c52018-05-16 12:20:05 -040010416 goto out;
Chris Masonb0d5d102014-09-08 13:08:51 -070010417
10418 ret = btrfs_update_inode(trans, root, inode);
10419 if (ret)
Al Viro32955c52018-05-16 12:20:05 -040010420 goto out;
Nikolay Borisov73f2e542017-02-20 13:50:59 +020010421 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010422 if (ret)
Al Viro32955c52018-05-16 12:20:05 -040010423 goto out;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010424
Filipe Manana5762b5c2014-08-01 00:10:32 +010010425 /*
10426 * We set number of links to 0 in btrfs_new_inode(), and here we set
10427 * it to 1 because d_tmpfile() will issue a warning if the count is 0,
10428 * through:
10429 *
10430 * d_tmpfile() -> inode_dec_link_count() -> drop_nlink()
10431 */
10432 set_nlink(inode, 1);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010433 d_tmpfile(dentry, inode);
Al Viro32955c52018-05-16 12:20:05 -040010434 unlock_new_inode(inode);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010435 mark_inode_dirty(inode);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010436out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010437 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -040010438 if (ret && inode)
10439 discard_new_inode(inode);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010440 btrfs_btree_balance_dirty(fs_info);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010441 return ret;
10442}
10443
David Sterba20a7db82017-02-17 16:24:29 +010010444__attribute__((const))
Liu Bo9d0d1c82017-03-24 15:04:50 -070010445static int btrfs_readpage_io_failed_hook(struct page *page, int failed_mirror)
David Sterba20a7db82017-02-17 16:24:29 +010010446{
Liu Bo9d0d1c82017-03-24 15:04:50 -070010447 return -EAGAIN;
David Sterba20a7db82017-02-17 16:24:29 +010010448}
10449
David Sterba5cdc84b2018-07-18 20:32:52 +020010450void btrfs_set_range_writeback(struct extent_io_tree *tree, u64 start, u64 end)
Josef Bacikc6100a42017-05-05 11:57:13 -040010451{
David Sterba5cdc84b2018-07-18 20:32:52 +020010452 struct inode *inode = tree->private_data;
Josef Bacikc6100a42017-05-05 11:57:13 -040010453 unsigned long index = start >> PAGE_SHIFT;
10454 unsigned long end_index = end >> PAGE_SHIFT;
10455 struct page *page;
10456
10457 while (index <= end_index) {
10458 page = find_get_page(inode->i_mapping, index);
10459 ASSERT(page); /* Pages should be in the extent_io_tree */
10460 set_page_writeback(page);
10461 put_page(page);
10462 index++;
10463 }
10464}
10465
Omar Sandovaled46ff32016-11-03 10:28:14 -070010466#ifdef CONFIG_SWAP
10467/*
10468 * Add an entry indicating a block group or device which is pinned by a
10469 * swapfile. Returns 0 on success, 1 if there is already an entry for it, or a
10470 * negative errno on failure.
10471 */
10472static int btrfs_add_swapfile_pin(struct inode *inode, void *ptr,
10473 bool is_block_group)
10474{
10475 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
10476 struct btrfs_swapfile_pin *sp, *entry;
10477 struct rb_node **p;
10478 struct rb_node *parent = NULL;
10479
10480 sp = kmalloc(sizeof(*sp), GFP_NOFS);
10481 if (!sp)
10482 return -ENOMEM;
10483 sp->ptr = ptr;
10484 sp->inode = inode;
10485 sp->is_block_group = is_block_group;
10486
10487 spin_lock(&fs_info->swapfile_pins_lock);
10488 p = &fs_info->swapfile_pins.rb_node;
10489 while (*p) {
10490 parent = *p;
10491 entry = rb_entry(parent, struct btrfs_swapfile_pin, node);
10492 if (sp->ptr < entry->ptr ||
10493 (sp->ptr == entry->ptr && sp->inode < entry->inode)) {
10494 p = &(*p)->rb_left;
10495 } else if (sp->ptr > entry->ptr ||
10496 (sp->ptr == entry->ptr && sp->inode > entry->inode)) {
10497 p = &(*p)->rb_right;
10498 } else {
10499 spin_unlock(&fs_info->swapfile_pins_lock);
10500 kfree(sp);
10501 return 1;
10502 }
10503 }
10504 rb_link_node(&sp->node, parent, p);
10505 rb_insert_color(&sp->node, &fs_info->swapfile_pins);
10506 spin_unlock(&fs_info->swapfile_pins_lock);
10507 return 0;
10508}
10509
10510/* Free all of the entries pinned by this swapfile. */
10511static void btrfs_free_swapfile_pins(struct inode *inode)
10512{
10513 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
10514 struct btrfs_swapfile_pin *sp;
10515 struct rb_node *node, *next;
10516
10517 spin_lock(&fs_info->swapfile_pins_lock);
10518 node = rb_first(&fs_info->swapfile_pins);
10519 while (node) {
10520 next = rb_next(node);
10521 sp = rb_entry(node, struct btrfs_swapfile_pin, node);
10522 if (sp->inode == inode) {
10523 rb_erase(&sp->node, &fs_info->swapfile_pins);
10524 if (sp->is_block_group)
10525 btrfs_put_block_group(sp->ptr);
10526 kfree(sp);
10527 }
10528 node = next;
10529 }
10530 spin_unlock(&fs_info->swapfile_pins_lock);
10531}
10532
10533struct btrfs_swap_info {
10534 u64 start;
10535 u64 block_start;
10536 u64 block_len;
10537 u64 lowest_ppage;
10538 u64 highest_ppage;
10539 unsigned long nr_pages;
10540 int nr_extents;
10541};
10542
10543static int btrfs_add_swap_extent(struct swap_info_struct *sis,
10544 struct btrfs_swap_info *bsi)
10545{
10546 unsigned long nr_pages;
10547 u64 first_ppage, first_ppage_reported, next_ppage;
10548 int ret;
10549
10550 first_ppage = ALIGN(bsi->block_start, PAGE_SIZE) >> PAGE_SHIFT;
10551 next_ppage = ALIGN_DOWN(bsi->block_start + bsi->block_len,
10552 PAGE_SIZE) >> PAGE_SHIFT;
10553
10554 if (first_ppage >= next_ppage)
10555 return 0;
10556 nr_pages = next_ppage - first_ppage;
10557
10558 first_ppage_reported = first_ppage;
10559 if (bsi->start == 0)
10560 first_ppage_reported++;
10561 if (bsi->lowest_ppage > first_ppage_reported)
10562 bsi->lowest_ppage = first_ppage_reported;
10563 if (bsi->highest_ppage < (next_ppage - 1))
10564 bsi->highest_ppage = next_ppage - 1;
10565
10566 ret = add_swap_extent(sis, bsi->nr_pages, nr_pages, first_ppage);
10567 if (ret < 0)
10568 return ret;
10569 bsi->nr_extents += ret;
10570 bsi->nr_pages += nr_pages;
10571 return 0;
10572}
10573
10574static void btrfs_swap_deactivate(struct file *file)
10575{
10576 struct inode *inode = file_inode(file);
10577
10578 btrfs_free_swapfile_pins(inode);
10579 atomic_dec(&BTRFS_I(inode)->root->nr_swapfiles);
10580}
10581
10582static int btrfs_swap_activate(struct swap_info_struct *sis, struct file *file,
10583 sector_t *span)
10584{
10585 struct inode *inode = file_inode(file);
10586 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
10587 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
10588 struct extent_state *cached_state = NULL;
10589 struct extent_map *em = NULL;
10590 struct btrfs_device *device = NULL;
10591 struct btrfs_swap_info bsi = {
10592 .lowest_ppage = (sector_t)-1ULL,
10593 };
10594 int ret = 0;
10595 u64 isize;
10596 u64 start;
10597
10598 /*
10599 * If the swap file was just created, make sure delalloc is done. If the
10600 * file changes again after this, the user is doing something stupid and
10601 * we don't really care.
10602 */
10603 ret = btrfs_wait_ordered_range(inode, 0, (u64)-1);
10604 if (ret)
10605 return ret;
10606
10607 /*
10608 * The inode is locked, so these flags won't change after we check them.
10609 */
10610 if (BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS) {
10611 btrfs_warn(fs_info, "swapfile must not be compressed");
10612 return -EINVAL;
10613 }
10614 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW)) {
10615 btrfs_warn(fs_info, "swapfile must not be copy-on-write");
10616 return -EINVAL;
10617 }
10618 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
10619 btrfs_warn(fs_info, "swapfile must not be checksummed");
10620 return -EINVAL;
10621 }
10622
10623 /*
10624 * Balance or device remove/replace/resize can move stuff around from
10625 * under us. The EXCL_OP flag makes sure they aren't running/won't run
10626 * concurrently while we are mapping the swap extents, and
10627 * fs_info->swapfile_pins prevents them from running while the swap file
10628 * is active and moving the extents. Note that this also prevents a
10629 * concurrent device add which isn't actually necessary, but it's not
10630 * really worth the trouble to allow it.
10631 */
10632 if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) {
10633 btrfs_warn(fs_info,
10634 "cannot activate swapfile while exclusive operation is running");
10635 return -EBUSY;
10636 }
10637 /*
10638 * Snapshots can create extents which require COW even if NODATACOW is
10639 * set. We use this counter to prevent snapshots. We must increment it
10640 * before walking the extents because we don't want a concurrent
10641 * snapshot to run after we've already checked the extents.
10642 */
10643 atomic_inc(&BTRFS_I(inode)->root->nr_swapfiles);
10644
10645 isize = ALIGN_DOWN(inode->i_size, fs_info->sectorsize);
10646
10647 lock_extent_bits(io_tree, 0, isize - 1, &cached_state);
10648 start = 0;
10649 while (start < isize) {
10650 u64 logical_block_start, physical_block_start;
10651 struct btrfs_block_group_cache *bg;
10652 u64 len = isize - start;
10653
10654 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len, 0);
10655 if (IS_ERR(em)) {
10656 ret = PTR_ERR(em);
10657 goto out;
10658 }
10659
10660 if (em->block_start == EXTENT_MAP_HOLE) {
10661 btrfs_warn(fs_info, "swapfile must not have holes");
10662 ret = -EINVAL;
10663 goto out;
10664 }
10665 if (em->block_start == EXTENT_MAP_INLINE) {
10666 /*
10667 * It's unlikely we'll ever actually find ourselves
10668 * here, as a file small enough to fit inline won't be
10669 * big enough to store more than the swap header, but in
10670 * case something changes in the future, let's catch it
10671 * here rather than later.
10672 */
10673 btrfs_warn(fs_info, "swapfile must not be inline");
10674 ret = -EINVAL;
10675 goto out;
10676 }
10677 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
10678 btrfs_warn(fs_info, "swapfile must not be compressed");
10679 ret = -EINVAL;
10680 goto out;
10681 }
10682
10683 logical_block_start = em->block_start + (start - em->start);
10684 len = min(len, em->len - (start - em->start));
10685 free_extent_map(em);
10686 em = NULL;
10687
10688 ret = can_nocow_extent(inode, start, &len, NULL, NULL, NULL);
10689 if (ret < 0) {
10690 goto out;
10691 } else if (ret) {
10692 ret = 0;
10693 } else {
10694 btrfs_warn(fs_info,
10695 "swapfile must not be copy-on-write");
10696 ret = -EINVAL;
10697 goto out;
10698 }
10699
10700 em = btrfs_get_chunk_map(fs_info, logical_block_start, len);
10701 if (IS_ERR(em)) {
10702 ret = PTR_ERR(em);
10703 goto out;
10704 }
10705
10706 if (em->map_lookup->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
10707 btrfs_warn(fs_info,
10708 "swapfile must have single data profile");
10709 ret = -EINVAL;
10710 goto out;
10711 }
10712
10713 if (device == NULL) {
10714 device = em->map_lookup->stripes[0].dev;
10715 ret = btrfs_add_swapfile_pin(inode, device, false);
10716 if (ret == 1)
10717 ret = 0;
10718 else if (ret)
10719 goto out;
10720 } else if (device != em->map_lookup->stripes[0].dev) {
10721 btrfs_warn(fs_info, "swapfile must be on one device");
10722 ret = -EINVAL;
10723 goto out;
10724 }
10725
10726 physical_block_start = (em->map_lookup->stripes[0].physical +
10727 (logical_block_start - em->start));
10728 len = min(len, em->len - (logical_block_start - em->start));
10729 free_extent_map(em);
10730 em = NULL;
10731
10732 bg = btrfs_lookup_block_group(fs_info, logical_block_start);
10733 if (!bg) {
10734 btrfs_warn(fs_info,
10735 "could not find block group containing swapfile");
10736 ret = -EINVAL;
10737 goto out;
10738 }
10739
10740 ret = btrfs_add_swapfile_pin(inode, bg, true);
10741 if (ret) {
10742 btrfs_put_block_group(bg);
10743 if (ret == 1)
10744 ret = 0;
10745 else
10746 goto out;
10747 }
10748
10749 if (bsi.block_len &&
10750 bsi.block_start + bsi.block_len == physical_block_start) {
10751 bsi.block_len += len;
10752 } else {
10753 if (bsi.block_len) {
10754 ret = btrfs_add_swap_extent(sis, &bsi);
10755 if (ret)
10756 goto out;
10757 }
10758 bsi.start = start;
10759 bsi.block_start = physical_block_start;
10760 bsi.block_len = len;
10761 }
10762
10763 start += len;
10764 }
10765
10766 if (bsi.block_len)
10767 ret = btrfs_add_swap_extent(sis, &bsi);
10768
10769out:
10770 if (!IS_ERR_OR_NULL(em))
10771 free_extent_map(em);
10772
10773 unlock_extent_cached(io_tree, 0, isize - 1, &cached_state);
10774
10775 if (ret)
10776 btrfs_swap_deactivate(file);
10777
10778 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
10779
10780 if (ret)
10781 return ret;
10782
10783 if (device)
10784 sis->bdev = device->bdev;
10785 *span = bsi.highest_ppage - bsi.lowest_ppage + 1;
10786 sis->max = bsi.nr_pages;
10787 sis->pages = bsi.nr_pages - 1;
10788 sis->highest_bit = bsi.nr_pages - 1;
10789 return bsi.nr_extents;
10790}
10791#else
10792static void btrfs_swap_deactivate(struct file *file)
10793{
10794}
10795
10796static int btrfs_swap_activate(struct swap_info_struct *sis, struct file *file,
10797 sector_t *span)
10798{
10799 return -EOPNOTSUPP;
10800}
10801#endif
10802
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010803static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -050010804 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -040010805 .lookup = btrfs_lookup,
10806 .create = btrfs_create,
10807 .unlink = btrfs_unlink,
10808 .link = btrfs_link,
10809 .mkdir = btrfs_mkdir,
10810 .rmdir = btrfs_rmdir,
Miklos Szeredi2773bf02016-09-27 11:03:58 +020010811 .rename = btrfs_rename2,
Chris Mason39279cc2007-06-12 06:35:45 -040010812 .symlink = btrfs_symlink,
10813 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -040010814 .mknod = btrfs_mknod,
Josef Bacik5103e942007-11-16 11:45:54 -050010815 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010816 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010817 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010818 .set_acl = btrfs_set_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070010819 .update_time = btrfs_update_time,
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010820 .tmpfile = btrfs_tmpfile,
Chris Mason39279cc2007-06-12 06:35:45 -040010821};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010822static const struct inode_operations btrfs_dir_ro_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010823 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -050010824 .permission = btrfs_permission,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070010825 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010826};
Yan, Zheng76dda932009-09-21 16:00:26 -040010827
Alexey Dobriyan828c0952009-10-01 15:43:56 -070010828static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010829 .llseek = generic_file_llseek,
10830 .read = generic_read_dir,
Omar Sandoval02dbfc92016-05-20 13:50:33 -070010831 .iterate_shared = btrfs_real_readdir,
Josef Bacik23b5ec72017-07-24 15:14:25 -040010832 .open = btrfs_opendir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -040010833 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010834#ifdef CONFIG_COMPAT
Luke Dashjr4c63c242015-10-29 08:22:21 +000010835 .compat_ioctl = btrfs_compat_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010836#endif
Sage Weil6bf13c02008-06-10 10:07:39 -040010837 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -040010838 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -040010839};
10840
David Sterba20e55062015-11-19 11:42:28 +010010841static const struct extent_io_ops btrfs_extent_io_ops = {
David Sterba4d53ddd2017-02-17 15:27:44 +010010842 /* mandatory callbacks */
Chris Mason065631f2008-02-20 12:07:25 -050010843 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -040010844 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
Liu Bo9d0d1c82017-03-24 15:04:50 -070010845 .readpage_io_failed_hook = btrfs_readpage_io_failed_hook,
Chris Mason07157aa2007-08-30 08:50:51 -040010846};
10847
Chris Mason35054392009-01-21 13:11:13 -050010848/*
10849 * btrfs doesn't support the bmap operation because swapfiles
10850 * use bmap to make a mapping of extents in the file. They assume
10851 * these extents won't change over the life of the file and they
10852 * use the bmap result to do IO directly to the drive.
10853 *
10854 * the btrfs bmap call would return logical addresses that aren't
10855 * suitable for IO and they also will change frequently as COW
10856 * operations happen. So, swapfile + btrfs == corruption.
10857 *
10858 * For now we're avoiding this by dropping bmap.
10859 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -070010860static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -040010861 .readpage = btrfs_readpage,
10862 .writepage = btrfs_writepage,
Chris Masonb293f02e2007-11-01 19:45:34 -040010863 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -050010864 .readpages = btrfs_readpages,
Chris Mason16432982008-04-10 10:23:21 -040010865 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -040010866 .invalidatepage = btrfs_invalidatepage,
10867 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -040010868 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +020010869 .error_remove_page = generic_error_remove_page,
Omar Sandovaled46ff32016-11-03 10:28:14 -070010870 .swap_activate = btrfs_swap_activate,
10871 .swap_deactivate = btrfs_swap_deactivate,
Chris Mason39279cc2007-06-12 06:35:45 -040010872};
10873
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010874static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010875 .getattr = btrfs_getattr,
10876 .setattr = btrfs_setattr,
Josef Bacik5103e942007-11-16 11:45:54 -050010877 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010878 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -050010879 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010880 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010881 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010882 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010883};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010884static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -040010885 .getattr = btrfs_getattr,
10886 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010887 .permission = btrfs_permission,
Josef Bacik33268ea2008-07-24 12:16:36 -040010888 .listxattr = btrfs_listxattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010889 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010890 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010891 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -040010892};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010893static const struct inode_operations btrfs_symlink_inode_operations = {
Al Viro6b255392015-11-17 10:20:54 -050010894 .get_link = page_get_link,
Li Zefanf2095612010-11-19 02:05:24 +000010895 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -050010896 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010897 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -050010898 .listxattr = btrfs_listxattr,
Josef Bacike41f9412012-03-26 09:46:47 -040010899 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010900};
Yan, Zheng76dda932009-09-21 16:00:26 -040010901
Alexey Dobriyan82d339d2009-10-09 09:54:36 -040010902const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -040010903 .d_delete = btrfs_dentry_delete,
10904};