blob: b4129d9072eccb27595c61af31a26607f1e0f400 [file] [log] [blame]
David Sterbac1d7c512018-04-03 19:23:33 +02001// SPDX-License-Identifier: GPL-2.0
Chris Mason6cbd5572007-06-12 09:07:21 -04002/*
3 * Copyright (C) 2007 Oracle. All rights reserved.
Chris Mason6cbd5572007-06-12 09:07:21 -04004 */
5
Chris Mason8f18cf12008-04-25 16:53:30 -04006#include <linux/kernel.h>
Chris Mason065631f2008-02-20 12:07:25 -05007#include <linux/bio.h>
Chris Mason39279cc2007-06-12 06:35:45 -04008#include <linux/buffer_head.h>
Sage Weilf2eb0a22008-05-02 14:43:14 -04009#include <linux/file.h>
Chris Mason39279cc2007-06-12 06:35:45 -040010#include <linux/fs.h>
11#include <linux/pagemap.h>
12#include <linux/highmem.h>
13#include <linux/time.h>
14#include <linux/init.h>
15#include <linux/string.h>
Chris Mason39279cc2007-06-12 06:35:45 -040016#include <linux/backing-dev.h>
Chris Mason39279cc2007-06-12 06:35:45 -040017#include <linux/writeback.h>
Chris Mason39279cc2007-06-12 06:35:45 -040018#include <linux/compat.h>
Josef Bacik5103e942007-11-16 11:45:54 -050019#include <linux/xattr.h>
Josef Bacik33268ea2008-07-24 12:16:36 -040020#include <linux/posix_acl.h>
Yan Zhengd899e052008-10-30 14:25:28 -040021#include <linux/falloc.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090022#include <linux/slab.h>
David Sterba7a36dde2011-05-06 15:33:15 +020023#include <linux/ratelimit.h>
Filipe Brandenburger55e301f2013-01-29 06:04:50 +000024#include <linux/btrfs.h>
David Woodhouse53b381b2013-01-29 18:40:14 -050025#include <linux/blkdev.h>
Josef Bacikf23b5a52013-06-19 10:16:26 -040026#include <linux/posix_acl_xattr.h>
Christoph Hellwige2e40f22015-02-22 08:58:50 -080027#include <linux/uio.h>
Josef Bacik69fe2d72017-10-19 14:15:57 -040028#include <linux/magic.h>
Jeff Laytonae5e1652018-01-29 06:41:30 -050029#include <linux/iversion.h>
Omar Sandovaled46ff32016-11-03 10:28:14 -070030#include <linux/swap.h>
David Sterba92d32172018-04-16 21:10:14 +020031#include <asm/unaligned.h>
Chris Mason39279cc2007-06-12 06:35:45 -040032#include "ctree.h"
33#include "disk-io.h"
34#include "transaction.h"
35#include "btrfs_inode.h"
Chris Mason39279cc2007-06-12 06:35:45 -040036#include "print-tree.h"
Chris Masone6dcd2d2008-07-17 12:53:50 -040037#include "ordered-data.h"
Christoph Hellwig95819c02008-08-28 06:21:17 -040038#include "xattr.h"
Chris Masone02119d2008-09-05 16:13:11 -040039#include "tree-log.h"
Jan Schmidt4a54c8c2011-07-22 15:41:52 +020040#include "volumes.h"
Chris Masonc8b97812008-10-29 14:49:59 -040041#include "compression.h"
Chris Masonb4ce94d2009-02-04 09:25:08 -050042#include "locking.h"
Josef Bacikdc89e982011-01-28 17:05:48 -050043#include "free-space-cache.h"
Li Zefan581bb052011-04-20 10:06:11 +080044#include "inode-map.h"
Liu Bo38c227d2013-01-29 03:18:40 +000045#include "backref.h"
Filipe David Borba Manana63541922014-01-07 11:47:46 +000046#include "props.h"
Dongsheng Yang31193212014-12-12 16:44:35 +080047#include "qgroup.h"
Wang Xiaoguangdda32452016-07-11 11:05:29 +080048#include "dedupe.h"
Chris Mason39279cc2007-06-12 06:35:45 -040049
50struct btrfs_iget_args {
Chris Mason90d3e592014-01-09 17:28:00 -080051 struct btrfs_key *location;
Chris Mason39279cc2007-06-12 06:35:45 -040052 struct btrfs_root *root;
53};
54
Filipe Mananaf28a4922015-12-08 19:23:20 +000055struct btrfs_dio_data {
Filipe Mananaf28a4922015-12-08 19:23:20 +000056 u64 reserve;
57 u64 unsubmitted_oe_range_start;
58 u64 unsubmitted_oe_range_end;
Liu Bo4aaedfb2016-12-14 22:36:05 -080059 int overwrite;
Filipe Mananaf28a4922015-12-08 19:23:20 +000060};
61
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070062static const struct inode_operations btrfs_dir_inode_operations;
63static const struct inode_operations btrfs_symlink_inode_operations;
64static const struct inode_operations btrfs_dir_ro_inode_operations;
65static const struct inode_operations btrfs_special_inode_operations;
66static const struct inode_operations btrfs_file_inode_operations;
Alexey Dobriyan7f094102009-09-21 17:01:10 -070067static const struct address_space_operations btrfs_aops;
Alexey Dobriyan828c0952009-10-01 15:43:56 -070068static const struct file_operations btrfs_dir_file_operations;
David Sterba20e55062015-11-19 11:42:28 +010069static const struct extent_io_ops btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -040070
71static struct kmem_cache *btrfs_inode_cachep;
72struct kmem_cache *btrfs_trans_handle_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040073struct kmem_cache *btrfs_path_cachep;
Josef Bacikdc89e982011-01-28 17:05:48 -050074struct kmem_cache *btrfs_free_space_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040075
76#define S_SHIFT 12
David Sterba4d4ab6d2015-11-19 11:42:31 +010077static const unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
Chris Mason39279cc2007-06-12 06:35:45 -040078 [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
79 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
80 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
81 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
82 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
83 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
84 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
85};
86
Eric Sandeen3972f262013-01-12 02:57:22 +000087static int btrfs_setsize(struct inode *inode, struct iattr *attr);
Filipe Manana213e8c52018-02-06 20:40:31 +000088static int btrfs_truncate(struct inode *inode, bool skip_writeback);
Josef Bacik5fd02042012-05-02 14:00:54 -040089static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent);
Chris Mason771ed682008-11-06 22:02:51 -050090static noinline int cow_file_range(struct inode *inode,
91 struct page *locked_page,
Wang Xiaoguangdda32452016-07-11 11:05:29 +080092 u64 start, u64 end, u64 delalloc_end,
93 int *page_started, unsigned long *nr_written,
94 int unlock, struct btrfs_dedupe_hash *hash);
Liu Bo6f9994d2017-01-31 07:50:22 -080095static struct extent_map *create_io_em(struct inode *inode, u64 start, u64 len,
96 u64 orig_start, u64 block_start,
97 u64 block_len, u64 orig_block_len,
98 u64 ram_bytes, int compress_type,
99 int type);
Josef Bacik7b128762008-07-24 12:17:14 -0400100
Qu Wenruo524272602017-03-08 10:25:52 +0800101static void __endio_write_update_ordered(struct inode *inode,
102 const u64 offset, const u64 bytes,
103 const bool uptodate);
104
105/*
106 * Cleanup all submitted ordered extents in specified range to handle errors
107 * from the fill_dellaloc() callback.
108 *
109 * NOTE: caller must ensure that when an error happens, it can not call
110 * extent_clear_unlock_delalloc() to clear both the bits EXTENT_DO_ACCOUNTING
111 * and EXTENT_DELALLOC simultaneously, because that causes the reserved metadata
112 * to be released, which we want to happen only when finishing the ordered
Nikolay Borisov5eaad972018-11-01 14:09:46 +0200113 * extent (btrfs_finish_ordered_io()). Also note that the caller of
114 * btrfs_run_delalloc_range already does proper cleanup for the first page of
Qu Wenruo524272602017-03-08 10:25:52 +0800115 * the range, that is, it invokes the callback writepage_end_io_hook() for the
116 * range of the first page.
117 */
118static inline void btrfs_cleanup_ordered_extents(struct inode *inode,
119 const u64 offset,
120 const u64 bytes)
121{
Naohiro Aota63d71450c2017-09-01 17:58:47 +0900122 unsigned long index = offset >> PAGE_SHIFT;
123 unsigned long end_index = (offset + bytes - 1) >> PAGE_SHIFT;
124 struct page *page;
125
126 while (index <= end_index) {
127 page = find_get_page(inode->i_mapping, index);
128 index++;
129 if (!page)
130 continue;
131 ClearPagePrivate2(page);
132 put_page(page);
133 }
Qu Wenruo524272602017-03-08 10:25:52 +0800134 return __endio_write_update_ordered(inode, offset + PAGE_SIZE,
135 bytes - PAGE_SIZE, false);
136}
137
Eric Sandeen48a3b632013-04-25 20:41:01 +0000138static int btrfs_dirty_inode(struct inode *inode);
Chris Mason39279cc2007-06-12 06:35:45 -0400139
Josef Bacik6a3891c2015-03-16 17:38:52 -0400140#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
141void btrfs_test_inode_set_ops(struct inode *inode)
142{
143 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
144}
145#endif
146
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000147static int btrfs_init_inode_security(struct btrfs_trans_handle *trans,
Eric Paris2a7dba32011-02-01 11:05:39 -0500148 struct inode *inode, struct inode *dir,
149 const struct qstr *qstr)
Jim Owens0279b4c2009-02-04 09:29:13 -0500150{
151 int err;
152
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000153 err = btrfs_init_acl(trans, inode, dir);
Jim Owens0279b4c2009-02-04 09:29:13 -0500154 if (!err)
Eric Paris2a7dba32011-02-01 11:05:39 -0500155 err = btrfs_xattr_security_init(trans, inode, dir, qstr);
Jim Owens0279b4c2009-02-04 09:29:13 -0500156 return err;
157}
158
Chris Masond352ac62008-09-29 15:18:18 -0400159/*
Chris Masonc8b97812008-10-29 14:49:59 -0400160 * this does all the hard work for inserting an inline extent into
161 * the btree. The caller should have done a btrfs_drop_extents so that
162 * no overlapping inline items exist in the btree
163 */
Chris Mason40f76582014-05-21 13:35:51 -0700164static int insert_inline_extent(struct btrfs_trans_handle *trans,
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000165 struct btrfs_path *path, int extent_inserted,
Chris Masonc8b97812008-10-29 14:49:59 -0400166 struct btrfs_root *root, struct inode *inode,
167 u64 start, size_t size, size_t compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000168 int compress_type,
Chris Masonc8b97812008-10-29 14:49:59 -0400169 struct page **compressed_pages)
170{
Chris Masonc8b97812008-10-29 14:49:59 -0400171 struct extent_buffer *leaf;
172 struct page *page = NULL;
173 char *kaddr;
174 unsigned long ptr;
175 struct btrfs_file_extent_item *ei;
Chris Masonc8b97812008-10-29 14:49:59 -0400176 int ret;
177 size_t cur_size = size;
Chris Masonc8b97812008-10-29 14:49:59 -0400178 unsigned long offset;
Chris Masonc8b97812008-10-29 14:49:59 -0400179
Li Zefanfe3f5662011-03-28 08:30:38 +0000180 if (compressed_size && compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400181 cur_size = compressed_size;
Chris Masonc8b97812008-10-29 14:49:59 -0400182
Chris Masonc8b97812008-10-29 14:49:59 -0400183 inode_add_bytes(inode, size);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000184
185 if (!extent_inserted) {
186 struct btrfs_key key;
187 size_t datasize;
188
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +0200189 key.objectid = btrfs_ino(BTRFS_I(inode));
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000190 key.offset = start;
David Sterba962a2982014-06-04 18:41:45 +0200191 key.type = BTRFS_EXTENT_DATA_KEY;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000192
193 datasize = btrfs_file_extent_calc_inline_size(cur_size);
194 path->leave_spinning = 1;
195 ret = btrfs_insert_empty_item(trans, root, path, &key,
196 datasize);
David Sterba79b4f4c2017-06-15 19:09:51 +0200197 if (ret)
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000198 goto fail;
Chris Masonc8b97812008-10-29 14:49:59 -0400199 }
200 leaf = path->nodes[0];
201 ei = btrfs_item_ptr(leaf, path->slots[0],
202 struct btrfs_file_extent_item);
203 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
204 btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
205 btrfs_set_file_extent_encryption(leaf, ei, 0);
206 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
207 btrfs_set_file_extent_ram_bytes(leaf, ei, size);
208 ptr = btrfs_file_extent_inline_start(ei);
209
Li Zefan261507a02010-12-17 14:21:50 +0800210 if (compress_type != BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -0400211 struct page *cpage;
212 int i = 0;
Chris Masond3977122009-01-05 21:25:51 -0500213 while (compressed_size > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -0400214 cpage = compressed_pages[i];
Chris Mason5b050f02008-11-11 09:34:41 -0500215 cur_size = min_t(unsigned long, compressed_size,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300216 PAGE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400217
Cong Wang7ac687d2011-11-25 23:14:28 +0800218 kaddr = kmap_atomic(cpage);
Chris Masonc8b97812008-10-29 14:49:59 -0400219 write_extent_buffer(leaf, kaddr, ptr, cur_size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800220 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400221
222 i++;
223 ptr += cur_size;
224 compressed_size -= cur_size;
225 }
226 btrfs_set_file_extent_compression(leaf, ei,
Li Zefan261507a02010-12-17 14:21:50 +0800227 compress_type);
Chris Masonc8b97812008-10-29 14:49:59 -0400228 } else {
229 page = find_get_page(inode->i_mapping,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300230 start >> PAGE_SHIFT);
Chris Masonc8b97812008-10-29 14:49:59 -0400231 btrfs_set_file_extent_compression(leaf, ei, 0);
Cong Wang7ac687d2011-11-25 23:14:28 +0800232 kaddr = kmap_atomic(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300233 offset = start & (PAGE_SIZE - 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400234 write_extent_buffer(leaf, kaddr + offset, ptr, size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800235 kunmap_atomic(kaddr);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300236 put_page(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400237 }
238 btrfs_mark_buffer_dirty(leaf);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000239 btrfs_release_path(path);
Chris Masonc8b97812008-10-29 14:49:59 -0400240
Yan, Zhengc2167752009-11-12 09:34:21 +0000241 /*
242 * we're an inline extent, so nobody can
243 * extend the file past i_size without locking
244 * a page we already have locked.
245 *
246 * We must do any isize and inode updates
247 * before we unlock the pages. Otherwise we
248 * could end up racing with unlink.
249 */
Chris Masonc8b97812008-10-29 14:49:59 -0400250 BTRFS_I(inode)->disk_i_size = inode->i_size;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100251 ret = btrfs_update_inode(trans, root, inode);
Yan, Zhengc2167752009-11-12 09:34:21 +0000252
Chris Masonc8b97812008-10-29 14:49:59 -0400253fail:
David Sterba79b4f4c2017-06-15 19:09:51 +0200254 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400255}
256
257
258/*
259 * conditionally insert an inline extent into the file. This
260 * does the checks required to make sure the data is small enough
261 * to fit as an inline extent.
262 */
Nikolay Borisovd02c0e22018-03-02 09:43:15 +0200263static noinline int cow_file_range_inline(struct inode *inode, u64 start,
Josef Bacik00361582013-08-14 14:02:47 -0400264 u64 end, size_t compressed_size,
265 int compress_type,
266 struct page **compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400267{
Nikolay Borisovd02c0e22018-03-02 09:43:15 +0200268 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400269 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik00361582013-08-14 14:02:47 -0400270 struct btrfs_trans_handle *trans;
Chris Masonc8b97812008-10-29 14:49:59 -0400271 u64 isize = i_size_read(inode);
272 u64 actual_end = min(end + 1, isize);
273 u64 inline_len = actual_end - start;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400274 u64 aligned_end = ALIGN(end, fs_info->sectorsize);
Chris Masonc8b97812008-10-29 14:49:59 -0400275 u64 data_len = inline_len;
276 int ret;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000277 struct btrfs_path *path;
278 int extent_inserted = 0;
279 u32 extent_item_size;
Chris Masonc8b97812008-10-29 14:49:59 -0400280
281 if (compressed_size)
282 data_len = compressed_size;
283
284 if (start > 0 ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400285 actual_end > fs_info->sectorsize ||
286 data_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info) ||
Chris Masonc8b97812008-10-29 14:49:59 -0400287 (!compressed_size &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400288 (actual_end & (fs_info->sectorsize - 1)) == 0) ||
Chris Masonc8b97812008-10-29 14:49:59 -0400289 end + 1 < isize ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400290 data_len > fs_info->max_inline) {
Chris Masonc8b97812008-10-29 14:49:59 -0400291 return 1;
292 }
293
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000294 path = btrfs_alloc_path();
295 if (!path)
296 return -ENOMEM;
297
Josef Bacik00361582013-08-14 14:02:47 -0400298 trans = btrfs_join_transaction(root);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000299 if (IS_ERR(trans)) {
300 btrfs_free_path(path);
Josef Bacik00361582013-08-14 14:02:47 -0400301 return PTR_ERR(trans);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000302 }
Josef Bacik69fe2d72017-10-19 14:15:57 -0400303 trans->block_rsv = &BTRFS_I(inode)->block_rsv;
Josef Bacik00361582013-08-14 14:02:47 -0400304
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000305 if (compressed_size && compressed_pages)
306 extent_item_size = btrfs_file_extent_calc_inline_size(
307 compressed_size);
308 else
309 extent_item_size = btrfs_file_extent_calc_inline_size(
310 inline_len);
311
312 ret = __btrfs_drop_extents(trans, root, inode, path,
313 start, aligned_end, NULL,
314 1, 1, extent_item_size, &extent_inserted);
Josef Bacik00361582013-08-14 14:02:47 -0400315 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -0400316 btrfs_abort_transaction(trans, ret);
Josef Bacik00361582013-08-14 14:02:47 -0400317 goto out;
318 }
Chris Masonc8b97812008-10-29 14:49:59 -0400319
320 if (isize > actual_end)
321 inline_len = min_t(u64, isize, actual_end);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000322 ret = insert_inline_extent(trans, path, extent_inserted,
323 root, inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -0400324 inline_len, compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000325 compress_type, compressed_pages);
Josef Bacik2adcac12012-05-23 16:10:14 -0400326 if (ret && ret != -ENOSPC) {
Jeff Mahoney66642832016-06-10 18:19:25 -0400327 btrfs_abort_transaction(trans, ret);
Josef Bacik00361582013-08-14 14:02:47 -0400328 goto out;
Josef Bacik2adcac12012-05-23 16:10:14 -0400329 } else if (ret == -ENOSPC) {
Josef Bacik00361582013-08-14 14:02:47 -0400330 ret = 1;
331 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100332 }
Josef Bacik2adcac12012-05-23 16:10:14 -0400333
Josef Bacikbdc20e62013-02-28 13:23:38 -0500334 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +0200335 btrfs_drop_extent_cache(BTRFS_I(inode), start, aligned_end - 1, 0);
Josef Bacik00361582013-08-14 14:02:47 -0400336out:
Qu Wenruo94ed9382015-09-08 17:25:56 +0800337 /*
338 * Don't forget to free the reserved space, as for inlined extent
339 * it won't count as data extent, free them directly here.
340 * And at reserve time, it's always aligned to page size, so
341 * just free one page here.
342 */
Qu Wenruobc42bda2017-02-27 15:10:39 +0800343 btrfs_qgroup_free_data(inode, NULL, 0, PAGE_SIZE);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000344 btrfs_free_path(path);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -0400345 btrfs_end_transaction(trans);
Josef Bacik00361582013-08-14 14:02:47 -0400346 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400347}
348
Chris Mason771ed682008-11-06 22:02:51 -0500349struct async_extent {
350 u64 start;
351 u64 ram_size;
352 u64 compressed_size;
353 struct page **pages;
354 unsigned long nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800355 int compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500356 struct list_head list;
357};
358
359struct async_cow {
360 struct inode *inode;
David Sterba600b6cf2018-11-22 17:16:44 +0100361 struct btrfs_fs_info *fs_info;
Chris Mason771ed682008-11-06 22:02:51 -0500362 struct page *locked_page;
363 u64 start;
364 u64 end;
Liu Bof82b7352017-10-23 23:18:16 -0600365 unsigned int write_flags;
Chris Mason771ed682008-11-06 22:02:51 -0500366 struct list_head extents;
367 struct btrfs_work work;
368};
369
370static noinline int add_async_extent(struct async_cow *cow,
371 u64 start, u64 ram_size,
372 u64 compressed_size,
373 struct page **pages,
Li Zefan261507a02010-12-17 14:21:50 +0800374 unsigned long nr_pages,
375 int compress_type)
Chris Mason771ed682008-11-06 22:02:51 -0500376{
377 struct async_extent *async_extent;
378
379 async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100380 BUG_ON(!async_extent); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -0500381 async_extent->start = start;
382 async_extent->ram_size = ram_size;
383 async_extent->compressed_size = compressed_size;
384 async_extent->pages = pages;
385 async_extent->nr_pages = nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800386 async_extent->compress_type = compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500387 list_add_tail(&async_extent->list, &cow->extents);
388 return 0;
389}
390
Timofey Titovetsc2fcdcd2017-07-17 16:52:58 +0300391static inline int inode_need_compress(struct inode *inode, u64 start, u64 end)
Wang Shilongf79707b2014-07-17 11:44:09 +0800392{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400393 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Wang Shilongf79707b2014-07-17 11:44:09 +0800394
395 /* force compress */
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400396 if (btrfs_test_opt(fs_info, FORCE_COMPRESS))
Wang Shilongf79707b2014-07-17 11:44:09 +0800397 return 1;
David Sterbaeec63c62017-07-17 19:41:31 +0200398 /* defrag ioctl */
399 if (BTRFS_I(inode)->defrag_compress)
400 return 1;
Wang Shilongf79707b2014-07-17 11:44:09 +0800401 /* bad compression ratios */
402 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS)
403 return 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400404 if (btrfs_test_opt(fs_info, COMPRESS) ||
Wang Shilongf79707b2014-07-17 11:44:09 +0800405 BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS ||
David Sterbab52aa8c2017-07-17 19:17:20 +0200406 BTRFS_I(inode)->prop_compress)
Timofey Titovetsc2fcdcd2017-07-17 16:52:58 +0300407 return btrfs_compress_heuristic(inode, start, end);
Wang Shilongf79707b2014-07-17 11:44:09 +0800408 return 0;
409}
410
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200411static inline void inode_should_defrag(struct btrfs_inode *inode,
Anand Jain26d30f82016-12-19 19:09:06 +0800412 u64 start, u64 end, u64 num_bytes, u64 small_write)
413{
414 /* If this is a small write inside eof, kick off a defrag */
415 if (num_bytes < small_write &&
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200416 (start > 0 || end + 1 < inode->disk_i_size))
Anand Jain26d30f82016-12-19 19:09:06 +0800417 btrfs_add_inode_defrag(NULL, inode);
418}
419
Chris Masonc8b97812008-10-29 14:49:59 -0400420/*
Chris Mason771ed682008-11-06 22:02:51 -0500421 * we create compressed extents in two phases. The first
422 * phase compresses a range of pages that have already been
423 * locked (both pages and state bits are locked).
Chris Masonc8b97812008-10-29 14:49:59 -0400424 *
Chris Mason771ed682008-11-06 22:02:51 -0500425 * This is done inside an ordered work queue, and the compression
426 * is spread across many cpus. The actual IO submission is step
427 * two, and the ordered work queue takes care of making sure that
428 * happens in the same order things were put onto the queue by
429 * writepages and friends.
Chris Masonc8b97812008-10-29 14:49:59 -0400430 *
Chris Mason771ed682008-11-06 22:02:51 -0500431 * If this code finds it can't get good compression, it puts an
432 * entry onto the work queue to write the uncompressed bytes. This
433 * makes sure that both compressed inodes and uncompressed inodes
Artem Bityutskiyb2570312012-07-25 18:12:06 +0300434 * are written in the same order that the flusher thread sent them
435 * down.
Chris Masond352ac62008-09-29 15:18:18 -0400436 */
Filipe Mananac44f6492014-10-09 21:15:44 +0100437static noinline void compress_file_range(struct inode *inode,
Chris Mason771ed682008-11-06 22:02:51 -0500438 struct page *locked_page,
439 u64 start, u64 end,
440 struct async_cow *async_cow,
441 int *num_added)
Chris Masonb888db22007-08-27 16:49:44 -0400442{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400443 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400444 u64 blocksize = fs_info->sectorsize;
Chris Masonc8b97812008-10-29 14:49:59 -0400445 u64 actual_end;
Chris Mason42dc7ba2008-12-15 11:44:56 -0500446 u64 isize = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400447 int ret = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400448 struct page **pages = NULL;
449 unsigned long nr_pages;
Chris Masonc8b97812008-10-29 14:49:59 -0400450 unsigned long total_compressed = 0;
451 unsigned long total_in = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400452 int i;
453 int will_compress;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400454 int compress_type = fs_info->compress_type;
Chris Mason4adaa612013-03-26 13:07:00 -0400455 int redirty = 0;
Chris Masonb888db22007-08-27 16:49:44 -0400456
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200457 inode_should_defrag(BTRFS_I(inode), start, end, end - start + 1,
458 SZ_16K);
Chris Mason4cb53002011-05-24 15:35:30 -0400459
Chris Mason42dc7ba2008-12-15 11:44:56 -0500460 actual_end = min_t(u64, isize, end + 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400461again:
462 will_compress = 0;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300463 nr_pages = (end >> PAGE_SHIFT) - (start >> PAGE_SHIFT) + 1;
David Sterba069eac72017-02-14 19:36:54 +0100464 BUILD_BUG_ON((BTRFS_MAX_COMPRESSED % PAGE_SIZE) != 0);
465 nr_pages = min_t(unsigned long, nr_pages,
466 BTRFS_MAX_COMPRESSED / PAGE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400467
Chris Masonf03d9301f2009-02-04 09:31:06 -0500468 /*
469 * we don't want to send crud past the end of i_size through
470 * compression, that's just a waste of CPU time. So, if the
471 * end of the file is before the start of our current
472 * requested range of bytes, we bail out to the uncompressed
473 * cleanup code that can deal with all of this.
474 *
475 * It isn't really the fastest way to fix things, but this is a
476 * very uncommon corner.
477 */
478 if (actual_end <= start)
479 goto cleanup_and_bail_uncompressed;
480
Chris Masonc8b97812008-10-29 14:49:59 -0400481 total_compressed = actual_end - start;
482
Shilong Wang4bcbb332014-10-07 18:44:35 -0400483 /*
484 * skip compression for a small file range(<=blocksize) that
Nicholas D Steeves01327612016-05-19 21:18:45 -0400485 * isn't an inline extent, since it doesn't save disk space at all.
Shilong Wang4bcbb332014-10-07 18:44:35 -0400486 */
487 if (total_compressed <= blocksize &&
488 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
489 goto cleanup_and_bail_uncompressed;
490
David Sterba069eac72017-02-14 19:36:54 +0100491 total_compressed = min_t(unsigned long, total_compressed,
492 BTRFS_MAX_UNCOMPRESSED);
Chris Masonc8b97812008-10-29 14:49:59 -0400493 total_in = 0;
494 ret = 0;
Chris Masondb945352007-10-15 16:15:53 -0400495
Chris Mason771ed682008-11-06 22:02:51 -0500496 /*
497 * we do compression for mount -o compress and when the
498 * inode has not been flagged as nocompress. This flag can
499 * change at any time if we discover bad compression ratios.
Chris Masonc8b97812008-10-29 14:49:59 -0400500 */
Timofey Titovetsc2fcdcd2017-07-17 16:52:58 +0300501 if (inode_need_compress(inode, start, end)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400502 WARN_ON(pages);
David Sterba31e818f2015-02-20 18:00:26 +0100503 pages = kcalloc(nr_pages, sizeof(struct page *), GFP_NOFS);
Li Zefan560f7d72011-09-08 10:22:01 +0800504 if (!pages) {
505 /* just bail out to the uncompressed code */
Filipe Manana3527a012018-10-13 00:37:25 +0100506 nr_pages = 0;
Li Zefan560f7d72011-09-08 10:22:01 +0800507 goto cont;
508 }
Chris Mason179e29e2007-11-01 11:28:41 -0400509
David Sterbaeec63c62017-07-17 19:41:31 +0200510 if (BTRFS_I(inode)->defrag_compress)
511 compress_type = BTRFS_I(inode)->defrag_compress;
512 else if (BTRFS_I(inode)->prop_compress)
David Sterbab52aa8c2017-07-17 19:17:20 +0200513 compress_type = BTRFS_I(inode)->prop_compress;
Li Zefan261507a02010-12-17 14:21:50 +0800514
Chris Mason4adaa612013-03-26 13:07:00 -0400515 /*
516 * we need to call clear_page_dirty_for_io on each
517 * page in the range. Otherwise applications with the file
518 * mmap'd can wander in and change the page contents while
519 * we are compressing them.
520 *
521 * If the compression fails for any reason, we set the pages
522 * dirty again later on.
Timofey Titovetse9679de2017-10-24 01:29:48 +0300523 *
524 * Note that the remaining part is redirtied, the start pointer
525 * has moved, the end is the original one.
Chris Mason4adaa612013-03-26 13:07:00 -0400526 */
Timofey Titovetse9679de2017-10-24 01:29:48 +0300527 if (!redirty) {
528 extent_range_clear_dirty_for_io(inode, start, end);
529 redirty = 1;
530 }
David Sterbaf51d2b52017-09-15 17:36:57 +0200531
532 /* Compression level is applied here and only here */
533 ret = btrfs_compress_pages(
534 compress_type | (fs_info->compress_level << 4),
Li Zefan261507a02010-12-17 14:21:50 +0800535 inode->i_mapping, start,
David Sterba38c31462017-02-14 19:04:07 +0100536 pages,
David Sterba4d3a8002017-02-14 19:04:07 +0100537 &nr_pages,
Li Zefan261507a02010-12-17 14:21:50 +0800538 &total_in,
David Sterbae5d74902017-02-14 19:45:05 +0100539 &total_compressed);
Chris Masonc8b97812008-10-29 14:49:59 -0400540
541 if (!ret) {
542 unsigned long offset = total_compressed &
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300543 (PAGE_SIZE - 1);
David Sterba4d3a8002017-02-14 19:04:07 +0100544 struct page *page = pages[nr_pages - 1];
Chris Masonc8b97812008-10-29 14:49:59 -0400545 char *kaddr;
546
547 /* zero the tail end of the last page, we might be
548 * sending it down to disk
549 */
550 if (offset) {
Cong Wang7ac687d2011-11-25 23:14:28 +0800551 kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400552 memset(kaddr + offset, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300553 PAGE_SIZE - offset);
Cong Wang7ac687d2011-11-25 23:14:28 +0800554 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400555 }
556 will_compress = 1;
557 }
558 }
Li Zefan560f7d72011-09-08 10:22:01 +0800559cont:
Chris Masonc8b97812008-10-29 14:49:59 -0400560 if (start == 0) {
561 /* lets try to make an inline extent */
Timofey Titovets6018ba02017-09-15 01:57:26 +0300562 if (ret || total_in < actual_end) {
Chris Masonc8b97812008-10-29 14:49:59 -0400563 /* we didn't compress the entire range, try
Chris Mason771ed682008-11-06 22:02:51 -0500564 * to make an uncompressed inline extent.
Chris Masonc8b97812008-10-29 14:49:59 -0400565 */
Nikolay Borisovd02c0e22018-03-02 09:43:15 +0200566 ret = cow_file_range_inline(inode, start, end, 0,
567 BTRFS_COMPRESS_NONE, NULL);
Chris Masonc8b97812008-10-29 14:49:59 -0400568 } else {
Chris Mason771ed682008-11-06 22:02:51 -0500569 /* try making a compressed inline extent */
Nikolay Borisovd02c0e22018-03-02 09:43:15 +0200570 ret = cow_file_range_inline(inode, start, end,
Li Zefanfe3f5662011-03-28 08:30:38 +0000571 total_compressed,
572 compress_type, pages);
Chris Masonc8b97812008-10-29 14:49:59 -0400573 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100574 if (ret <= 0) {
Josef Bacik151a41b2013-07-29 13:22:24 -0400575 unsigned long clear_flags = EXTENT_DELALLOC |
Josef Bacik8b62f872017-10-19 14:15:55 -0400576 EXTENT_DELALLOC_NEW | EXTENT_DEFRAG |
577 EXTENT_DO_ACCOUNTING;
Filipe Mananae6eb4312014-10-10 10:45:12 +0100578 unsigned long page_error_op;
579
Filipe Mananae6eb4312014-10-10 10:45:12 +0100580 page_error_op = ret < 0 ? PAGE_SET_ERROR : 0;
Josef Bacik151a41b2013-07-29 13:22:24 -0400581
Chris Mason771ed682008-11-06 22:02:51 -0500582 /*
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100583 * inline extent creation worked or returned error,
584 * we don't need to create any more async work items.
585 * Unlock and free up our temp pages.
Josef Bacik8b62f872017-10-19 14:15:55 -0400586 *
587 * We use DO_ACCOUNTING here because we need the
588 * delalloc_release_metadata to be done _after_ we drop
589 * our outstanding extent for clearing delalloc for this
590 * range.
Chris Mason771ed682008-11-06 22:02:51 -0500591 */
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800592 extent_clear_unlock_delalloc(inode, start, end, end,
593 NULL, clear_flags,
594 PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -0400595 PAGE_CLEAR_DIRTY |
596 PAGE_SET_WRITEBACK |
Filipe Mananae6eb4312014-10-10 10:45:12 +0100597 page_error_op |
Josef Bacikc2790a22013-07-29 11:20:47 -0400598 PAGE_END_WRITEBACK);
Chris Masonc8b97812008-10-29 14:49:59 -0400599 goto free_pages_out;
600 }
601 }
602
603 if (will_compress) {
604 /*
605 * we aren't doing an inline extent round the compressed size
606 * up to a block size boundary so the allocator does sane
607 * things
608 */
Qu Wenruofda28322013-02-26 08:10:22 +0000609 total_compressed = ALIGN(total_compressed, blocksize);
Chris Masonc8b97812008-10-29 14:49:59 -0400610
611 /*
612 * one last check to make sure the compression is really a
Timofey Titovets170607e2017-06-06 14:41:15 +0300613 * win, compare the page count read with the blocks on disk,
614 * compression must free at least one sector size
Chris Masonc8b97812008-10-29 14:49:59 -0400615 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300616 total_in = ALIGN(total_in, PAGE_SIZE);
Timofey Titovets170607e2017-06-06 14:41:15 +0300617 if (total_compressed + blocksize <= total_in) {
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700618 *num_added += 1;
619
620 /*
621 * The async work queues will take care of doing actual
622 * allocation on disk for these compressed pages, and
623 * will submit them to the elevator.
624 */
Timofey Titovets11708622017-10-03 18:06:01 +0300625 add_async_extent(async_cow, start, total_in,
David Sterba4d3a8002017-02-14 19:04:07 +0100626 total_compressed, pages, nr_pages,
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700627 compress_type);
628
Timofey Titovets11708622017-10-03 18:06:01 +0300629 if (start + total_in < end) {
630 start += total_in;
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700631 pages = NULL;
632 cond_resched();
633 goto again;
634 }
635 return;
Chris Masonc8b97812008-10-29 14:49:59 -0400636 }
637 }
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700638 if (pages) {
Chris Masonc8b97812008-10-29 14:49:59 -0400639 /*
640 * the compression code ran but failed to make things smaller,
641 * free any pages it allocated and our page pointer array
642 */
David Sterba4d3a8002017-02-14 19:04:07 +0100643 for (i = 0; i < nr_pages; i++) {
Chris Mason70b99e62008-10-31 12:46:39 -0400644 WARN_ON(pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300645 put_page(pages[i]);
Chris Masonc8b97812008-10-29 14:49:59 -0400646 }
647 kfree(pages);
648 pages = NULL;
649 total_compressed = 0;
David Sterba4d3a8002017-02-14 19:04:07 +0100650 nr_pages = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400651
652 /* flag the file so we don't compress in the future */
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400653 if (!btrfs_test_opt(fs_info, FORCE_COMPRESS) &&
David Sterbab52aa8c2017-07-17 19:17:20 +0200654 !(BTRFS_I(inode)->prop_compress)) {
Chris Masona555f812010-01-28 16:18:15 -0500655 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
Chris Mason1e701a32010-03-11 09:42:04 -0500656 }
Chris Masonc8b97812008-10-29 14:49:59 -0400657 }
Chris Masonf03d9301f2009-02-04 09:31:06 -0500658cleanup_and_bail_uncompressed:
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700659 /*
660 * No compression, but we still need to write the pages in the file
661 * we've been given so far. redirty the locked page if it corresponds
662 * to our extent and set things up for the async work queue to run
663 * cow_file_range to do the normal delalloc dance.
664 */
665 if (page_offset(locked_page) >= start &&
666 page_offset(locked_page) <= end)
667 __set_page_dirty_nobuffers(locked_page);
668 /* unlocked later on in the async handlers */
669
670 if (redirty)
671 extent_range_redirty_for_io(inode, start, end);
672 add_async_extent(async_cow, start, end - start + 1, 0, NULL, 0,
673 BTRFS_COMPRESS_NONE);
674 *num_added += 1;
Chris Mason771ed682008-11-06 22:02:51 -0500675
Filipe Mananac44f6492014-10-09 21:15:44 +0100676 return;
Chris Mason771ed682008-11-06 22:02:51 -0500677
678free_pages_out:
David Sterba4d3a8002017-02-14 19:04:07 +0100679 for (i = 0; i < nr_pages; i++) {
Chris Mason771ed682008-11-06 22:02:51 -0500680 WARN_ON(pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300681 put_page(pages[i]);
Chris Mason771ed682008-11-06 22:02:51 -0500682 }
Chris Masond3977122009-01-05 21:25:51 -0500683 kfree(pages);
Chris Mason771ed682008-11-06 22:02:51 -0500684}
Chris Mason771ed682008-11-06 22:02:51 -0500685
Filipe Manana40ae8372014-10-06 22:14:24 +0100686static void free_async_extent_pages(struct async_extent *async_extent)
687{
688 int i;
689
690 if (!async_extent->pages)
691 return;
692
693 for (i = 0; i < async_extent->nr_pages; i++) {
694 WARN_ON(async_extent->pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300695 put_page(async_extent->pages[i]);
Filipe Manana40ae8372014-10-06 22:14:24 +0100696 }
697 kfree(async_extent->pages);
698 async_extent->nr_pages = 0;
699 async_extent->pages = NULL;
Chris Mason771ed682008-11-06 22:02:51 -0500700}
701
702/*
703 * phase two of compressed writeback. This is the ordered portion
704 * of the code, which only gets called in the order the work was
705 * queued. We walk all the async extents created by compress_file_range
706 * and send them down to the disk.
707 */
Filipe Mananadec8f172014-10-06 22:14:26 +0100708static noinline void submit_compressed_extents(struct inode *inode,
Chris Mason771ed682008-11-06 22:02:51 -0500709 struct async_cow *async_cow)
710{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400711 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason771ed682008-11-06 22:02:51 -0500712 struct async_extent *async_extent;
713 u64 alloc_hint = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500714 struct btrfs_key ins;
715 struct extent_map *em;
716 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason771ed682008-11-06 22:02:51 -0500717 struct extent_io_tree *io_tree;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500718 int ret = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500719
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500720again:
Chris Masond3977122009-01-05 21:25:51 -0500721 while (!list_empty(&async_cow->extents)) {
Chris Mason771ed682008-11-06 22:02:51 -0500722 async_extent = list_entry(async_cow->extents.next,
723 struct async_extent, list);
724 list_del(&async_extent->list);
725
726 io_tree = &BTRFS_I(inode)->io_tree;
727
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500728retry:
Chris Mason771ed682008-11-06 22:02:51 -0500729 /* did the compression code fall back to uncompressed IO? */
730 if (!async_extent->pages) {
731 int page_started = 0;
732 unsigned long nr_written = 0;
733
734 lock_extent(io_tree, async_extent->start,
Josef Bacik2ac55d42010-02-03 19:33:23 +0000735 async_extent->start +
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100736 async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500737
738 /* allocate blocks */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500739 ret = cow_file_range(inode, async_cow->locked_page,
740 async_extent->start,
741 async_extent->start +
742 async_extent->ram_size - 1,
Wang Xiaoguangdda32452016-07-11 11:05:29 +0800743 async_extent->start +
744 async_extent->ram_size - 1,
745 &page_started, &nr_written, 0,
746 NULL);
Chris Mason771ed682008-11-06 22:02:51 -0500747
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100748 /* JDM XXX */
749
Chris Mason771ed682008-11-06 22:02:51 -0500750 /*
751 * if page_started, cow_file_range inserted an
752 * inline extent and took care of all the unlocking
753 * and IO for us. Otherwise, we need to submit
754 * all those pages down to the drive.
755 */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500756 if (!page_started && !ret)
Nikolay Borisov5e3ee232017-12-08 15:55:58 +0200757 extent_write_locked_range(inode,
758 async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500759 async_extent->start +
Chris Mason771ed682008-11-06 22:02:51 -0500760 async_extent->ram_size - 1,
Chris Mason771ed682008-11-06 22:02:51 -0500761 WB_SYNC_ALL);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500762 else if (ret)
763 unlock_page(async_cow->locked_page);
Chris Mason771ed682008-11-06 22:02:51 -0500764 kfree(async_extent);
765 cond_resched();
766 continue;
767 }
768
769 lock_extent(io_tree, async_extent->start,
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100770 async_extent->start + async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500771
Wang Xiaoguang18513092016-07-25 15:51:40 +0800772 ret = btrfs_reserve_extent(root, async_extent->ram_size,
Chris Mason771ed682008-11-06 22:02:51 -0500773 async_extent->compressed_size,
774 async_extent->compressed_size,
Miao Xiee570fd22014-06-19 10:42:50 +0800775 0, alloc_hint, &ins, 1, 1);
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500776 if (ret) {
Filipe Manana40ae8372014-10-06 22:14:24 +0100777 free_async_extent_pages(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500778
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400779 if (ret == -ENOSPC) {
780 unlock_extent(io_tree, async_extent->start,
781 async_extent->start +
782 async_extent->ram_size - 1);
Liu Boce620032014-07-24 22:48:05 +0800783
784 /*
785 * we need to redirty the pages if we decide to
786 * fallback to uncompressed IO, otherwise we
787 * will not submit these pages down to lower
788 * layers.
789 */
790 extent_range_redirty_for_io(inode,
791 async_extent->start,
792 async_extent->start +
793 async_extent->ram_size - 1);
794
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100795 goto retry;
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400796 }
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500797 goto out_free;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500798 }
Yan, Zhengc2167752009-11-12 09:34:21 +0000799 /*
800 * here we're doing allocation and writeback of the
801 * compressed pages
802 */
Liu Bo6f9994d2017-01-31 07:50:22 -0800803 em = create_io_em(inode, async_extent->start,
804 async_extent->ram_size, /* len */
805 async_extent->start, /* orig_start */
806 ins.objectid, /* block_start */
807 ins.offset, /* block_len */
808 ins.offset, /* orig_block_len */
809 async_extent->ram_size, /* ram_bytes */
810 async_extent->compress_type,
811 BTRFS_ORDERED_COMPRESSED);
812 if (IS_ERR(em))
813 /* ret value is not necessary due to void function */
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500814 goto out_free_reserve;
Liu Bo6f9994d2017-01-31 07:50:22 -0800815 free_extent_map(em);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500816
Li Zefan261507a02010-12-17 14:21:50 +0800817 ret = btrfs_add_ordered_extent_compress(inode,
818 async_extent->start,
819 ins.objectid,
820 async_extent->ram_size,
821 ins.offset,
822 BTRFS_ORDERED_COMPRESSED,
823 async_extent->compress_type);
Filipe Mananad9f85962014-08-25 10:43:00 +0100824 if (ret) {
Nikolay Borisovdcdbc052017-02-20 13:50:45 +0200825 btrfs_drop_extent_cache(BTRFS_I(inode),
826 async_extent->start,
Filipe Mananad9f85962014-08-25 10:43:00 +0100827 async_extent->start +
828 async_extent->ram_size - 1, 0);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500829 goto out_free_reserve;
Filipe Mananad9f85962014-08-25 10:43:00 +0100830 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400831 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Chris Mason771ed682008-11-06 22:02:51 -0500832
Chris Mason771ed682008-11-06 22:02:51 -0500833 /*
834 * clear dirty, set writeback and unlock the pages.
835 */
Josef Bacikc2790a22013-07-29 11:20:47 -0400836 extent_clear_unlock_delalloc(inode, async_extent->start,
Chris Masona791e352009-10-08 11:27:10 -0400837 async_extent->start +
838 async_extent->ram_size - 1,
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800839 async_extent->start +
840 async_extent->ram_size - 1,
Josef Bacik151a41b2013-07-29 13:22:24 -0400841 NULL, EXTENT_LOCKED | EXTENT_DELALLOC,
842 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -0400843 PAGE_SET_WRITEBACK);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200844 if (btrfs_submit_compressed_write(inode,
Chris Masond3977122009-01-05 21:25:51 -0500845 async_extent->start,
846 async_extent->ram_size,
847 ins.objectid,
848 ins.offset, async_extent->pages,
Liu Bof82b7352017-10-23 23:18:16 -0600849 async_extent->nr_pages,
850 async_cow->write_flags)) {
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100851 struct page *p = async_extent->pages[0];
852 const u64 start = async_extent->start;
853 const u64 end = start + async_extent->ram_size - 1;
854
855 p->mapping = inode->i_mapping;
Nikolay Borisovc6297322018-11-08 10:18:08 +0200856 btrfs_writepage_endio_finish_ordered(p, start, end, 0);
Nikolay Borisov7087a9d2018-11-01 14:09:48 +0200857
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100858 p->mapping = NULL;
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800859 extent_clear_unlock_delalloc(inode, start, end, end,
860 NULL, 0,
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100861 PAGE_END_WRITEBACK |
862 PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +0100863 free_async_extent_pages(async_extent);
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100864 }
Chris Mason771ed682008-11-06 22:02:51 -0500865 alloc_hint = ins.objectid + ins.offset;
866 kfree(async_extent);
867 cond_resched();
868 }
Filipe Mananadec8f172014-10-06 22:14:26 +0100869 return;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500870out_free_reserve:
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400871 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400872 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100873out_free:
Josef Bacikc2790a22013-07-29 11:20:47 -0400874 extent_clear_unlock_delalloc(inode, async_extent->start,
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500875 async_extent->start +
876 async_extent->ram_size - 1,
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800877 async_extent->start +
878 async_extent->ram_size - 1,
Josef Bacikc2790a22013-07-29 11:20:47 -0400879 NULL, EXTENT_LOCKED | EXTENT_DELALLOC |
Filipe Mananaa7e3b972017-04-03 10:45:46 +0100880 EXTENT_DELALLOC_NEW |
Josef Bacik151a41b2013-07-29 13:22:24 -0400881 EXTENT_DEFRAG | EXTENT_DO_ACCOUNTING,
882 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Filipe Manana704de492014-10-06 22:14:22 +0100883 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK |
884 PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +0100885 free_async_extent_pages(async_extent);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100886 kfree(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500887 goto again;
Chris Mason771ed682008-11-06 22:02:51 -0500888}
889
Josef Bacik4b46fce2010-05-23 11:00:55 -0400890static u64 get_extent_allocation_hint(struct inode *inode, u64 start,
891 u64 num_bytes)
892{
893 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
894 struct extent_map *em;
895 u64 alloc_hint = 0;
896
897 read_lock(&em_tree->lock);
898 em = search_extent_mapping(em_tree, start, num_bytes);
899 if (em) {
900 /*
901 * if block start isn't an actual block number then find the
902 * first block in this inode and use that as a hint. If that
903 * block is also bogus then just don't worry about it.
904 */
905 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
906 free_extent_map(em);
907 em = search_extent_mapping(em_tree, 0, 0);
908 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
909 alloc_hint = em->block_start;
910 if (em)
911 free_extent_map(em);
912 } else {
913 alloc_hint = em->block_start;
914 free_extent_map(em);
915 }
916 }
917 read_unlock(&em_tree->lock);
918
919 return alloc_hint;
920}
921
Chris Mason771ed682008-11-06 22:02:51 -0500922/*
923 * when extent_io.c finds a delayed allocation range in the file,
924 * the call backs end up in this code. The basic idea is to
925 * allocate extents on disk for the range, and create ordered data structs
926 * in ram to track those extents.
927 *
928 * locked_page is the page that writepage had locked already. We use
929 * it to make sure we don't do extra locks or unlocks.
930 *
931 * *page_started is set to one if we unlock locked_page and do everything
932 * required to start IO on it. It may be clean and already done with
933 * IO when we return.
934 */
Josef Bacik00361582013-08-14 14:02:47 -0400935static noinline int cow_file_range(struct inode *inode,
936 struct page *locked_page,
Wang Xiaoguangdda32452016-07-11 11:05:29 +0800937 u64 start, u64 end, u64 delalloc_end,
938 int *page_started, unsigned long *nr_written,
939 int unlock, struct btrfs_dedupe_hash *hash)
Chris Mason771ed682008-11-06 22:02:51 -0500940{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400941 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik00361582013-08-14 14:02:47 -0400942 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason771ed682008-11-06 22:02:51 -0500943 u64 alloc_hint = 0;
944 u64 num_bytes;
945 unsigned long ram_size;
Filipe Mananaa315e682017-03-06 23:04:20 +0000946 u64 cur_alloc_size = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400947 u64 blocksize = fs_info->sectorsize;
Chris Mason771ed682008-11-06 22:02:51 -0500948 struct btrfs_key ins;
949 struct extent_map *em;
Filipe Mananaa315e682017-03-06 23:04:20 +0000950 unsigned clear_bits;
951 unsigned long page_ops;
952 bool extent_reserved = false;
Chris Mason771ed682008-11-06 22:02:51 -0500953 int ret = 0;
954
Nikolay Borisov70ddc552017-02-20 13:50:35 +0200955 if (btrfs_is_free_space_inode(BTRFS_I(inode))) {
Josef Bacik02ecd2c2013-10-25 16:19:08 -0400956 WARN_ON_ONCE(1);
Josef Bacik29bce2f2014-02-07 12:21:23 -0500957 ret = -EINVAL;
958 goto out_unlock;
Josef Bacik02ecd2c2013-10-25 16:19:08 -0400959 }
Chris Mason771ed682008-11-06 22:02:51 -0500960
Qu Wenruofda28322013-02-26 08:10:22 +0000961 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Mason771ed682008-11-06 22:02:51 -0500962 num_bytes = max(blocksize, num_bytes);
Anand Jain566b1762018-02-15 18:07:59 +0800963 ASSERT(num_bytes <= btrfs_super_total_bytes(fs_info->super_copy));
Chris Mason771ed682008-11-06 22:02:51 -0500964
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200965 inode_should_defrag(BTRFS_I(inode), start, end, num_bytes, SZ_64K);
Chris Mason4cb53002011-05-24 15:35:30 -0400966
Chris Mason771ed682008-11-06 22:02:51 -0500967 if (start == 0) {
968 /* lets try to make an inline extent */
Nikolay Borisovd02c0e22018-03-02 09:43:15 +0200969 ret = cow_file_range_inline(inode, start, end, 0,
970 BTRFS_COMPRESS_NONE, NULL);
Chris Mason771ed682008-11-06 22:02:51 -0500971 if (ret == 0) {
Josef Bacik8b62f872017-10-19 14:15:55 -0400972 /*
973 * We use DO_ACCOUNTING here because we need the
974 * delalloc_release_metadata to be run _after_ we drop
975 * our outstanding extent for clearing delalloc for this
976 * range.
977 */
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800978 extent_clear_unlock_delalloc(inode, start, end,
979 delalloc_end, NULL,
Josef Bacikc2790a22013-07-29 11:20:47 -0400980 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik8b62f872017-10-19 14:15:55 -0400981 EXTENT_DELALLOC_NEW | EXTENT_DEFRAG |
982 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -0400983 PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
984 PAGE_END_WRITEBACK);
Chris Mason771ed682008-11-06 22:02:51 -0500985 *nr_written = *nr_written +
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300986 (end - start + PAGE_SIZE) / PAGE_SIZE;
Chris Mason771ed682008-11-06 22:02:51 -0500987 *page_started = 1;
Chris Mason771ed682008-11-06 22:02:51 -0500988 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100989 } else if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100990 goto out_unlock;
Chris Mason771ed682008-11-06 22:02:51 -0500991 }
992 }
Chris Masonc8b97812008-10-29 14:49:59 -0400993
Josef Bacik4b46fce2010-05-23 11:00:55 -0400994 alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +0200995 btrfs_drop_extent_cache(BTRFS_I(inode), start,
996 start + num_bytes - 1, 0);
Chris Mason3b951512008-04-17 11:29:12 -0400997
Anand Jain3752d222018-02-15 12:29:38 +0800998 while (num_bytes > 0) {
999 cur_alloc_size = num_bytes;
Wang Xiaoguang18513092016-07-25 15:51:40 +08001000 ret = btrfs_reserve_extent(root, cur_alloc_size, cur_alloc_size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001001 fs_info->sectorsize, 0, alloc_hint,
Miao Xiee570fd22014-06-19 10:42:50 +08001002 &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04001003 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001004 goto out_unlock;
Filipe Mananaa315e682017-03-06 23:04:20 +00001005 cur_alloc_size = ins.offset;
1006 extent_reserved = true;
Chris Masond3977122009-01-05 21:25:51 -05001007
Chris Mason771ed682008-11-06 22:02:51 -05001008 ram_size = ins.offset;
Liu Bo6f9994d2017-01-31 07:50:22 -08001009 em = create_io_em(inode, start, ins.offset, /* len */
1010 start, /* orig_start */
1011 ins.objectid, /* block_start */
1012 ins.offset, /* block_len */
1013 ins.offset, /* orig_block_len */
1014 ram_size, /* ram_bytes */
1015 BTRFS_COMPRESS_NONE, /* compress_type */
Liu Bo1af4a0a2017-02-13 15:35:09 -08001016 BTRFS_ORDERED_REGULAR /* type */);
Su Yue090a127a2018-05-30 16:48:56 +08001017 if (IS_ERR(em)) {
1018 ret = PTR_ERR(em);
Liu Boace68ba2013-04-22 10:53:47 +00001019 goto out_reserve;
Su Yue090a127a2018-05-30 16:48:56 +08001020 }
Liu Bo6f9994d2017-01-31 07:50:22 -08001021 free_extent_map(em);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001022
Chris Masone6dcd2d2008-07-17 12:53:50 -04001023 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
Chris Mason771ed682008-11-06 22:02:51 -05001024 ram_size, cur_alloc_size, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001025 if (ret)
Filipe Mananad9f85962014-08-25 10:43:00 +01001026 goto out_drop_extent_cache;
Chris Masonc8b97812008-10-29 14:49:59 -04001027
Yan Zheng17d217f2008-12-12 10:03:38 -05001028 if (root->root_key.objectid ==
1029 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1030 ret = btrfs_reloc_clone_csums(inode, start,
1031 cur_alloc_size);
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001032 /*
1033 * Only drop cache here, and process as normal.
1034 *
1035 * We must not allow extent_clear_unlock_delalloc()
1036 * at out_unlock label to free meta of this ordered
1037 * extent, as its meta should be freed by
1038 * btrfs_finish_ordered_io().
1039 *
1040 * So we must continue until @start is increased to
1041 * skip current ordered extent.
1042 */
Josef Bacik00361582013-08-14 14:02:47 -04001043 if (ret)
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001044 btrfs_drop_extent_cache(BTRFS_I(inode), start,
1045 start + ram_size - 1, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001046 }
1047
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001048 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana9cfa3e32016-04-26 15:39:32 +01001049
Chris Masonc8b97812008-10-29 14:49:59 -04001050 /* we're not doing compressed IO, don't unlock the first
1051 * page (which the caller expects to stay locked), don't
1052 * clear any dirty bits and don't set any writeback bits
Chris Mason8b62b722009-09-02 16:53:46 -04001053 *
1054 * Do set the Private2 bit so we know this page was properly
1055 * setup for writepage
Chris Masonc8b97812008-10-29 14:49:59 -04001056 */
Filipe Mananaa315e682017-03-06 23:04:20 +00001057 page_ops = unlock ? PAGE_UNLOCK : 0;
1058 page_ops |= PAGE_SET_PRIVATE2;
Chris Masona791e352009-10-08 11:27:10 -04001059
Josef Bacikc2790a22013-07-29 11:20:47 -04001060 extent_clear_unlock_delalloc(inode, start,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001061 start + ram_size - 1,
1062 delalloc_end, locked_page,
Josef Bacikc2790a22013-07-29 11:20:47 -04001063 EXTENT_LOCKED | EXTENT_DELALLOC,
Filipe Mananaa315e682017-03-06 23:04:20 +00001064 page_ops);
Anand Jain3752d222018-02-15 12:29:38 +08001065 if (num_bytes < cur_alloc_size)
1066 num_bytes = 0;
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001067 else
Anand Jain3752d222018-02-15 12:29:38 +08001068 num_bytes -= cur_alloc_size;
Chris Masonc59f8952007-12-17 20:14:04 -05001069 alloc_hint = ins.objectid + ins.offset;
1070 start += cur_alloc_size;
Filipe Mananaa315e682017-03-06 23:04:20 +00001071 extent_reserved = false;
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001072
1073 /*
1074 * btrfs_reloc_clone_csums() error, since start is increased
1075 * extent_clear_unlock_delalloc() at out_unlock label won't
1076 * free metadata of current ordered extent, we're OK to exit.
1077 */
1078 if (ret)
1079 goto out_unlock;
Chris Masonb888db22007-08-27 16:49:44 -04001080 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001081out:
Chris Masonbe20aa92007-12-17 20:14:01 -05001082 return ret;
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001083
Filipe Mananad9f85962014-08-25 10:43:00 +01001084out_drop_extent_cache:
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02001085 btrfs_drop_extent_cache(BTRFS_I(inode), start, start + ram_size - 1, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001086out_reserve:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001087 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001088 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001089out_unlock:
Filipe Mananaa7e3b972017-04-03 10:45:46 +01001090 clear_bits = EXTENT_LOCKED | EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
1091 EXTENT_DEFRAG | EXTENT_CLEAR_META_RESV;
Filipe Mananaa315e682017-03-06 23:04:20 +00001092 page_ops = PAGE_UNLOCK | PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
1093 PAGE_END_WRITEBACK;
1094 /*
1095 * If we reserved an extent for our delalloc range (or a subrange) and
1096 * failed to create the respective ordered extent, then it means that
1097 * when we reserved the extent we decremented the extent's size from
1098 * the data space_info's bytes_may_use counter and incremented the
1099 * space_info's bytes_reserved counter by the same amount. We must make
1100 * sure extent_clear_unlock_delalloc() does not try to decrement again
1101 * the data space_info's bytes_may_use counter, therefore we do not pass
1102 * it the flag EXTENT_CLEAR_DATA_RESV.
1103 */
1104 if (extent_reserved) {
1105 extent_clear_unlock_delalloc(inode, start,
1106 start + cur_alloc_size,
1107 start + cur_alloc_size,
1108 locked_page,
1109 clear_bits,
1110 page_ops);
1111 start += cur_alloc_size;
1112 if (start >= end)
1113 goto out;
1114 }
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001115 extent_clear_unlock_delalloc(inode, start, end, delalloc_end,
1116 locked_page,
Filipe Mananaa315e682017-03-06 23:04:20 +00001117 clear_bits | EXTENT_CLEAR_DATA_RESV,
1118 page_ops);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001119 goto out;
Chris Mason771ed682008-11-06 22:02:51 -05001120}
Chris Masonc8b97812008-10-29 14:49:59 -04001121
Chris Mason771ed682008-11-06 22:02:51 -05001122/*
1123 * work queue call back to started compression on a file and pages
1124 */
1125static noinline void async_cow_start(struct btrfs_work *work)
1126{
1127 struct async_cow *async_cow;
1128 int num_added = 0;
1129 async_cow = container_of(work, struct async_cow, work);
1130
1131 compress_file_range(async_cow->inode, async_cow->locked_page,
1132 async_cow->start, async_cow->end, async_cow,
1133 &num_added);
Josef Bacik8180ef82012-06-08 15:16:12 -04001134 if (num_added == 0) {
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001135 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001136 async_cow->inode = NULL;
Josef Bacik8180ef82012-06-08 15:16:12 -04001137 }
Chris Mason771ed682008-11-06 22:02:51 -05001138}
1139
1140/*
1141 * work queue call back to submit previously compressed pages
1142 */
1143static noinline void async_cow_submit(struct btrfs_work *work)
1144{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001145 struct btrfs_fs_info *fs_info;
Chris Mason771ed682008-11-06 22:02:51 -05001146 struct async_cow *async_cow;
Chris Mason771ed682008-11-06 22:02:51 -05001147 unsigned long nr_pages;
1148
1149 async_cow = container_of(work, struct async_cow, work);
1150
David Sterba600b6cf2018-11-22 17:16:44 +01001151 fs_info = async_cow->fs_info;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001152 nr_pages = (async_cow->end - async_cow->start + PAGE_SIZE) >>
1153 PAGE_SHIFT;
Chris Mason771ed682008-11-06 22:02:51 -05001154
David Sterba093258e2018-02-26 16:15:17 +01001155 /* atomic_sub_return implies a barrier */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001156 if (atomic_sub_return(nr_pages, &fs_info->async_delalloc_pages) <
David Sterba093258e2018-02-26 16:15:17 +01001157 5 * SZ_1M)
1158 cond_wake_up_nomb(&fs_info->async_submit_wait);
Chris Mason771ed682008-11-06 22:02:51 -05001159
Chris Masond3977122009-01-05 21:25:51 -05001160 if (async_cow->inode)
Chris Mason771ed682008-11-06 22:02:51 -05001161 submit_compressed_extents(async_cow->inode, async_cow);
Chris Mason771ed682008-11-06 22:02:51 -05001162}
Chris Masonc8b97812008-10-29 14:49:59 -04001163
Chris Mason771ed682008-11-06 22:02:51 -05001164static noinline void async_cow_free(struct btrfs_work *work)
1165{
1166 struct async_cow *async_cow;
1167 async_cow = container_of(work, struct async_cow, work);
Josef Bacik8180ef82012-06-08 15:16:12 -04001168 if (async_cow->inode)
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001169 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001170 kfree(async_cow);
1171}
1172
1173static int cow_file_range_async(struct inode *inode, struct page *locked_page,
1174 u64 start, u64 end, int *page_started,
Liu Bof82b7352017-10-23 23:18:16 -06001175 unsigned long *nr_written,
1176 unsigned int write_flags)
Chris Mason771ed682008-11-06 22:02:51 -05001177{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001178 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason771ed682008-11-06 22:02:51 -05001179 struct async_cow *async_cow;
Chris Mason771ed682008-11-06 22:02:51 -05001180 unsigned long nr_pages;
1181 u64 cur_end;
Chris Mason771ed682008-11-06 22:02:51 -05001182
Chris Masona3429ab2009-10-08 12:30:20 -04001183 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED,
David Sterbaae0f1622017-10-31 16:37:52 +01001184 1, 0, NULL);
Chris Masond3977122009-01-05 21:25:51 -05001185 while (start < end) {
Chris Mason771ed682008-11-06 22:02:51 -05001186 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001187 BUG_ON(!async_cow); /* -ENOMEM */
Josef Bacik8180ef82012-06-08 15:16:12 -04001188 async_cow->inode = igrab(inode);
David Sterba600b6cf2018-11-22 17:16:44 +01001189 async_cow->fs_info = fs_info;
Chris Mason771ed682008-11-06 22:02:51 -05001190 async_cow->locked_page = locked_page;
1191 async_cow->start = start;
Liu Bof82b7352017-10-23 23:18:16 -06001192 async_cow->write_flags = write_flags;
Chris Mason771ed682008-11-06 22:02:51 -05001193
Wang Shilongf79707b2014-07-17 11:44:09 +08001194 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001195 !btrfs_test_opt(fs_info, FORCE_COMPRESS))
Chris Mason771ed682008-11-06 22:02:51 -05001196 cur_end = end;
1197 else
Byongho Leeee221842015-12-15 01:42:10 +09001198 cur_end = min(end, start + SZ_512K - 1);
Chris Mason771ed682008-11-06 22:02:51 -05001199
1200 async_cow->end = cur_end;
1201 INIT_LIST_HEAD(&async_cow->extents);
1202
Liu Bo9e0af232014-08-15 23:36:53 +08001203 btrfs_init_work(&async_cow->work,
1204 btrfs_delalloc_helper,
1205 async_cow_start, async_cow_submit,
1206 async_cow_free);
Chris Mason771ed682008-11-06 22:02:51 -05001207
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001208 nr_pages = (cur_end - start + PAGE_SIZE) >>
1209 PAGE_SHIFT;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001210 atomic_add(nr_pages, &fs_info->async_delalloc_pages);
Chris Mason771ed682008-11-06 22:02:51 -05001211
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001212 btrfs_queue_work(fs_info->delalloc_workers, &async_cow->work);
Chris Mason771ed682008-11-06 22:02:51 -05001213
Chris Mason771ed682008-11-06 22:02:51 -05001214 *nr_written += nr_pages;
1215 start = cur_end + 1;
1216 }
1217 *page_started = 1;
1218 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001219}
1220
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001221static noinline int csum_exist_in_range(struct btrfs_fs_info *fs_info,
Yan Zheng17d217f2008-12-12 10:03:38 -05001222 u64 bytenr, u64 num_bytes)
1223{
1224 int ret;
1225 struct btrfs_ordered_sum *sums;
1226 LIST_HEAD(list);
1227
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001228 ret = btrfs_lookup_csums_range(fs_info->csum_root, bytenr,
Arne Jansena2de7332011-03-08 14:14:00 +01001229 bytenr + num_bytes - 1, &list, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001230 if (ret == 0 && list_empty(&list))
1231 return 0;
1232
1233 while (!list_empty(&list)) {
1234 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1235 list_del(&sums->list);
1236 kfree(sums);
1237 }
Liu Bo58113752018-01-31 17:09:13 -07001238 if (ret < 0)
1239 return ret;
Yan Zheng17d217f2008-12-12 10:03:38 -05001240 return 1;
1241}
1242
Chris Masond352ac62008-09-29 15:18:18 -04001243/*
1244 * when nowcow writeback call back. This checks for snapshots or COW copies
1245 * of the extents that exist in the file, and COWs the file as required.
1246 *
1247 * If no cow copies or snapshots exist, we write directly to the existing
1248 * blocks on disk
1249 */
Chris Mason7f366cf2009-03-12 20:12:45 -04001250static noinline int run_delalloc_nocow(struct inode *inode,
1251 struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001252 u64 start, u64 end, int *page_started, int force,
1253 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001254{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001255 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masonbe20aa92007-12-17 20:14:01 -05001256 struct btrfs_root *root = BTRFS_I(inode)->root;
1257 struct extent_buffer *leaf;
Chris Masonbe20aa92007-12-17 20:14:01 -05001258 struct btrfs_path *path;
Yan Zheng80ff3852008-10-30 14:20:02 -04001259 struct btrfs_file_extent_item *fi;
Chris Masonbe20aa92007-12-17 20:14:01 -05001260 struct btrfs_key found_key;
Liu Bo6f9994d2017-01-31 07:50:22 -08001261 struct extent_map *em;
Yan Zheng80ff3852008-10-30 14:20:02 -04001262 u64 cow_start;
1263 u64 cur_offset;
1264 u64 extent_end;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001265 u64 extent_offset;
Yan Zheng80ff3852008-10-30 14:20:02 -04001266 u64 disk_bytenr;
1267 u64 num_bytes;
Josef Bacikb4939682012-12-03 10:31:19 -05001268 u64 disk_num_bytes;
Josef Bacikcc95bef2013-04-04 14:31:27 -04001269 u64 ram_bytes;
Yan Zheng80ff3852008-10-30 14:20:02 -04001270 int extent_type;
Robbie Ko8ecebf4d2018-08-06 10:30:30 +08001271 int ret;
Yan Zhengd899e052008-10-30 14:25:28 -04001272 int type;
Yan Zheng80ff3852008-10-30 14:20:02 -04001273 int nocow;
1274 int check_prev = 1;
Li Zefan82d59022011-04-20 10:33:24 +08001275 bool nolock;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02001276 u64 ino = btrfs_ino(BTRFS_I(inode));
Chris Masonbe20aa92007-12-17 20:14:01 -05001277
1278 path = btrfs_alloc_path();
Josef Bacik17ca04a2012-05-31 15:58:55 -04001279 if (!path) {
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001280 extent_clear_unlock_delalloc(inode, start, end, end,
1281 locked_page,
Josef Bacikc2790a22013-07-29 11:20:47 -04001282 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -04001283 EXTENT_DO_ACCOUNTING |
1284 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001285 PAGE_CLEAR_DIRTY |
1286 PAGE_SET_WRITEBACK |
1287 PAGE_END_WRITEBACK);
Mark Fashehd8926bb2011-07-13 10:38:47 -07001288 return -ENOMEM;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001289 }
Li Zefan82d59022011-04-20 10:33:24 +08001290
Nikolay Borisov70ddc552017-02-20 13:50:35 +02001291 nolock = btrfs_is_free_space_inode(BTRFS_I(inode));
Li Zefan82d59022011-04-20 10:33:24 +08001292
Yan Zheng80ff3852008-10-30 14:20:02 -04001293 cow_start = (u64)-1;
1294 cur_offset = start;
1295 while (1) {
Liu Boe4c3b2d2017-01-30 12:25:28 -08001296 ret = btrfs_lookup_file_extent(NULL, root, path, ino,
Yan Zheng80ff3852008-10-30 14:20:02 -04001297 cur_offset, 0);
Josef Bacikd788a342013-10-25 16:55:08 -04001298 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001299 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001300 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1301 leaf = path->nodes[0];
1302 btrfs_item_key_to_cpu(leaf, &found_key,
1303 path->slots[0] - 1);
Li Zefan33345d012011-04-20 10:31:50 +08001304 if (found_key.objectid == ino &&
Yan Zheng80ff3852008-10-30 14:20:02 -04001305 found_key.type == BTRFS_EXTENT_DATA_KEY)
1306 path->slots[0]--;
1307 }
1308 check_prev = 0;
1309next_slot:
1310 leaf = path->nodes[0];
1311 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1312 ret = btrfs_next_leaf(root, path);
Liu Boe8916692018-01-25 11:02:50 -07001313 if (ret < 0) {
1314 if (cow_start != (u64)-1)
1315 cur_offset = cow_start;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001316 goto error;
Liu Boe8916692018-01-25 11:02:50 -07001317 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001318 if (ret > 0)
1319 break;
1320 leaf = path->nodes[0];
1321 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001322
Yan Zheng80ff3852008-10-30 14:20:02 -04001323 nocow = 0;
1324 disk_bytenr = 0;
Yan Zheng17d217f2008-12-12 10:03:38 -05001325 num_bytes = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001326 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001327
Filipe Manana1d512cb2015-11-09 00:33:58 +00001328 if (found_key.objectid > ino)
1329 break;
1330 if (WARN_ON_ONCE(found_key.objectid < ino) ||
1331 found_key.type < BTRFS_EXTENT_DATA_KEY) {
1332 path->slots[0]++;
1333 goto next_slot;
1334 }
1335 if (found_key.type > BTRFS_EXTENT_DATA_KEY ||
Yan Zheng80ff3852008-10-30 14:20:02 -04001336 found_key.offset > end)
1337 break;
Chris Masonbe20aa92007-12-17 20:14:01 -05001338
Yan Zheng80ff3852008-10-30 14:20:02 -04001339 if (found_key.offset > cur_offset) {
1340 extent_end = found_key.offset;
Chris Masone9061e22009-10-09 09:57:45 -04001341 extent_type = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001342 goto out_check;
1343 }
Chris Masonc31f8832008-01-08 15:46:31 -05001344
Yan Zheng80ff3852008-10-30 14:20:02 -04001345 fi = btrfs_item_ptr(leaf, path->slots[0],
1346 struct btrfs_file_extent_item);
1347 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001348
Josef Bacikcc95bef2013-04-04 14:31:27 -04001349 ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
Yan Zhengd899e052008-10-30 14:25:28 -04001350 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1351 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001352 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001353 extent_offset = btrfs_file_extent_offset(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001354 extent_end = found_key.offset +
1355 btrfs_file_extent_num_bytes(leaf, fi);
Josef Bacikb4939682012-12-03 10:31:19 -05001356 disk_num_bytes =
1357 btrfs_file_extent_disk_num_bytes(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001358 if (extent_end <= start) {
1359 path->slots[0]++;
1360 goto next_slot;
1361 }
Yan Zheng17d217f2008-12-12 10:03:38 -05001362 if (disk_bytenr == 0)
1363 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001364 if (btrfs_file_extent_compression(leaf, fi) ||
1365 btrfs_file_extent_encryption(leaf, fi) ||
1366 btrfs_file_extent_other_encoding(leaf, fi))
1367 goto out_check;
Ethan Lien78d42952018-05-17 14:58:29 +08001368 /*
1369 * Do the same check as in btrfs_cross_ref_exist but
1370 * without the unnecessary search.
1371 */
1372 if (btrfs_file_extent_generation(leaf, fi) <=
1373 btrfs_root_last_snapshot(&root->root_item))
1374 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001375 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1376 goto out_check;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001377 if (btrfs_extent_readonly(fs_info, disk_bytenr))
Yan Zheng80ff3852008-10-30 14:20:02 -04001378 goto out_check;
Liu Bo58113752018-01-31 17:09:13 -07001379 ret = btrfs_cross_ref_exist(root, ino,
1380 found_key.offset -
1381 extent_offset, disk_bytenr);
1382 if (ret) {
1383 /*
1384 * ret could be -EIO if the above fails to read
1385 * metadata.
1386 */
1387 if (ret < 0) {
1388 if (cow_start != (u64)-1)
1389 cur_offset = cow_start;
1390 goto error;
1391 }
1392
1393 WARN_ON_ONCE(nolock);
Yan Zheng17d217f2008-12-12 10:03:38 -05001394 goto out_check;
Liu Bo58113752018-01-31 17:09:13 -07001395 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001396 disk_bytenr += extent_offset;
Yan Zheng17d217f2008-12-12 10:03:38 -05001397 disk_bytenr += cur_offset - found_key.offset;
1398 num_bytes = min(end + 1, extent_end) - cur_offset;
1399 /*
Wang Shilonge9894fd2014-03-27 11:12:25 +08001400 * if there are pending snapshots for this root,
1401 * we fall into common COW way.
1402 */
Robbie Ko8ecebf4d2018-08-06 10:30:30 +08001403 if (!nolock && atomic_read(&root->snapshot_force_cow))
1404 goto out_check;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001405 /*
Yan Zheng17d217f2008-12-12 10:03:38 -05001406 * force cow if csum exists in the range.
1407 * this ensure that csum for a given extent are
1408 * either valid or do not exist.
1409 */
Liu Bo58113752018-01-31 17:09:13 -07001410 ret = csum_exist_in_range(fs_info, disk_bytenr,
1411 num_bytes);
1412 if (ret) {
Liu Bo58113752018-01-31 17:09:13 -07001413 /*
1414 * ret could be -EIO if the above fails to read
1415 * metadata.
1416 */
1417 if (ret < 0) {
1418 if (cow_start != (u64)-1)
1419 cur_offset = cow_start;
1420 goto error;
1421 }
1422 WARN_ON_ONCE(nolock);
Yan Zheng17d217f2008-12-12 10:03:38 -05001423 goto out_check;
Robbie Ko91e1f562016-10-07 10:01:29 +08001424 }
Robbie Ko8ecebf4d2018-08-06 10:30:30 +08001425 if (!btrfs_inc_nocow_writers(fs_info, disk_bytenr))
Filipe Mananaf78c4362016-05-09 13:15:41 +01001426 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001427 nocow = 1;
1428 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1429 extent_end = found_key.offset +
Qu Wenruoe41ca582018-06-06 15:41:49 +08001430 btrfs_file_extent_ram_bytes(leaf, fi);
Jeff Mahoneyda170662016-06-15 09:22:56 -04001431 extent_end = ALIGN(extent_end,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001432 fs_info->sectorsize);
Yan Zheng80ff3852008-10-30 14:20:02 -04001433 } else {
1434 BUG_ON(1);
1435 }
1436out_check:
1437 if (extent_end <= start) {
1438 path->slots[0]++;
Filipe Mananaf78c4362016-05-09 13:15:41 +01001439 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001440 btrfs_dec_nocow_writers(fs_info, disk_bytenr);
Yan Zheng80ff3852008-10-30 14:20:02 -04001441 goto next_slot;
1442 }
1443 if (!nocow) {
1444 if (cow_start == (u64)-1)
1445 cow_start = cur_offset;
1446 cur_offset = extent_end;
1447 if (cur_offset > end)
1448 break;
1449 path->slots[0]++;
1450 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001451 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001452
David Sterbab3b4aa72011-04-21 01:20:15 +02001453 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001454 if (cow_start != (u64)-1) {
Josef Bacik00361582013-08-14 14:02:47 -04001455 ret = cow_file_range(inode, locked_page,
1456 cow_start, found_key.offset - 1,
Wang Xiaoguangdda32452016-07-11 11:05:29 +08001457 end, page_started, nr_written, 1,
1458 NULL);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001459 if (ret) {
Filipe Mananaf78c4362016-05-09 13:15:41 +01001460 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001461 btrfs_dec_nocow_writers(fs_info,
Filipe Mananaf78c4362016-05-09 13:15:41 +01001462 disk_bytenr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001463 goto error;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001464 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001465 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001466 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001467
Yan Zhengd899e052008-10-30 14:25:28 -04001468 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Liu Bo6f9994d2017-01-31 07:50:22 -08001469 u64 orig_start = found_key.offset - extent_offset;
1470
1471 em = create_io_em(inode, cur_offset, num_bytes,
1472 orig_start,
1473 disk_bytenr, /* block_start */
1474 num_bytes, /* block_len */
1475 disk_num_bytes, /* orig_block_len */
1476 ram_bytes, BTRFS_COMPRESS_NONE,
1477 BTRFS_ORDERED_PREALLOC);
1478 if (IS_ERR(em)) {
Liu Bo6f9994d2017-01-31 07:50:22 -08001479 if (nocow)
1480 btrfs_dec_nocow_writers(fs_info,
1481 disk_bytenr);
1482 ret = PTR_ERR(em);
1483 goto error;
Yan Zhengd899e052008-10-30 14:25:28 -04001484 }
Liu Bo6f9994d2017-01-31 07:50:22 -08001485 free_extent_map(em);
1486 }
1487
1488 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zhengd899e052008-10-30 14:25:28 -04001489 type = BTRFS_ORDERED_PREALLOC;
1490 } else {
1491 type = BTRFS_ORDERED_NOCOW;
1492 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001493
1494 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
Yan Zhengd899e052008-10-30 14:25:28 -04001495 num_bytes, num_bytes, type);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001496 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001497 btrfs_dec_nocow_writers(fs_info, disk_bytenr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001498 BUG_ON(ret); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -05001499
Yan, Zhengefa56462010-05-16 10:49:59 -04001500 if (root->root_key.objectid ==
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001501 BTRFS_DATA_RELOC_TREE_OBJECTID)
1502 /*
1503 * Error handled later, as we must prevent
1504 * extent_clear_unlock_delalloc() in error handler
1505 * from freeing metadata of created ordered extent.
1506 */
Yan, Zhengefa56462010-05-16 10:49:59 -04001507 ret = btrfs_reloc_clone_csums(inode, cur_offset,
1508 num_bytes);
Yan, Zhengefa56462010-05-16 10:49:59 -04001509
Josef Bacikc2790a22013-07-29 11:20:47 -04001510 extent_clear_unlock_delalloc(inode, cur_offset,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001511 cur_offset + num_bytes - 1, end,
Josef Bacikc2790a22013-07-29 11:20:47 -04001512 locked_page, EXTENT_LOCKED |
Wang Xiaoguang18513092016-07-25 15:51:40 +08001513 EXTENT_DELALLOC |
1514 EXTENT_CLEAR_DATA_RESV,
1515 PAGE_UNLOCK | PAGE_SET_PRIVATE2);
1516
Yan Zheng80ff3852008-10-30 14:20:02 -04001517 cur_offset = extent_end;
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001518
1519 /*
1520 * btrfs_reloc_clone_csums() error, now we're OK to call error
1521 * handler, as metadata for created ordered extent will only
1522 * be freed by btrfs_finish_ordered_io().
1523 */
1524 if (ret)
1525 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001526 if (cur_offset > end)
1527 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001528 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001529 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001530
Robbie Ko506481b2018-10-30 18:04:04 +08001531 if (cur_offset <= end && cow_start == (u64)-1)
Yan Zheng80ff3852008-10-30 14:20:02 -04001532 cow_start = cur_offset;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001533
Yan Zheng80ff3852008-10-30 14:20:02 -04001534 if (cow_start != (u64)-1) {
Robbie Ko506481b2018-10-30 18:04:04 +08001535 cur_offset = end;
Wang Xiaoguangdda32452016-07-11 11:05:29 +08001536 ret = cow_file_range(inode, locked_page, cow_start, end, end,
1537 page_started, nr_written, 1, NULL);
Josef Bacikd788a342013-10-25 16:55:08 -04001538 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001539 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001540 }
1541
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001542error:
Josef Bacik17ca04a2012-05-31 15:58:55 -04001543 if (ret && cur_offset < end)
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001544 extent_clear_unlock_delalloc(inode, cur_offset, end, end,
Josef Bacikc2790a22013-07-29 11:20:47 -04001545 locked_page, EXTENT_LOCKED |
Josef Bacik151a41b2013-07-29 13:22:24 -04001546 EXTENT_DELALLOC | EXTENT_DEFRAG |
1547 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
1548 PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -04001549 PAGE_SET_WRITEBACK |
1550 PAGE_END_WRITEBACK);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001551 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001552 return ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001553}
1554
Wang Shilong47059d92014-07-03 18:22:07 +08001555static inline int need_force_cow(struct inode *inode, u64 start, u64 end)
1556{
1557
1558 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
1559 !(BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC))
1560 return 0;
1561
1562 /*
1563 * @defrag_bytes is a hint value, no spinlock held here,
1564 * if is not zero, it means the file is defragging.
1565 * Force cow if given extent needs to be defragged.
1566 */
1567 if (BTRFS_I(inode)->defrag_bytes &&
1568 test_range_bit(&BTRFS_I(inode)->io_tree, start, end,
1569 EXTENT_DEFRAG, 0, NULL))
1570 return 1;
1571
1572 return 0;
1573}
1574
Chris Masond352ac62008-09-29 15:18:18 -04001575/*
Nikolay Borisov5eaad972018-11-01 14:09:46 +02001576 * Function to process delayed allocation (create CoW) for ranges which are
1577 * being touched for the first time.
Chris Masond352ac62008-09-29 15:18:18 -04001578 */
Nikolay Borisov5eaad972018-11-01 14:09:46 +02001579int btrfs_run_delalloc_range(void *private_data, struct page *locked_page,
1580 u64 start, u64 end, int *page_started, unsigned long *nr_written,
1581 struct writeback_control *wbc)
Chris Masonbe20aa92007-12-17 20:14:01 -05001582{
Josef Bacikc6100a42017-05-05 11:57:13 -04001583 struct inode *inode = private_data;
Chris Masonbe20aa92007-12-17 20:14:01 -05001584 int ret;
Wang Shilong47059d92014-07-03 18:22:07 +08001585 int force_cow = need_force_cow(inode, start, end);
Liu Bof82b7352017-10-23 23:18:16 -06001586 unsigned int write_flags = wbc_to_write_flags(wbc);
Chris Masona2135012008-06-25 16:01:30 -04001587
Wang Shilong47059d92014-07-03 18:22:07 +08001588 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW && !force_cow) {
Chris Masonc8b97812008-10-29 14:49:59 -04001589 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001590 page_started, 1, nr_written);
Wang Shilong47059d92014-07-03 18:22:07 +08001591 } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC && !force_cow) {
Yan Zhengd899e052008-10-30 14:25:28 -04001592 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001593 page_started, 0, nr_written);
Timofey Titovetsc2fcdcd2017-07-17 16:52:58 +03001594 } else if (!inode_need_compress(inode, start, end)) {
Wang Xiaoguangdda32452016-07-11 11:05:29 +08001595 ret = cow_file_range(inode, locked_page, start, end, end,
1596 page_started, nr_written, 1, NULL);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001597 } else {
1598 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1599 &BTRFS_I(inode)->runtime_flags);
Chris Mason771ed682008-11-06 22:02:51 -05001600 ret = cow_file_range_async(inode, locked_page, start, end,
Liu Bof82b7352017-10-23 23:18:16 -06001601 page_started, nr_written,
1602 write_flags);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001603 }
Qu Wenruo524272602017-03-08 10:25:52 +08001604 if (ret)
1605 btrfs_cleanup_ordered_extents(inode, start, end - start + 1);
Chris Masonb888db22007-08-27 16:49:44 -04001606 return ret;
1607}
1608
Nikolay Borisovabbb55f2018-11-01 14:09:53 +02001609void btrfs_split_delalloc_extent(struct inode *inode,
1610 struct extent_state *orig, u64 split)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001611{
Josef Bacikdcab6a32015-02-11 15:08:59 -05001612 u64 size;
1613
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001614 /* not delalloc, ignore it */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001615 if (!(orig->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001616 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001617
Josef Bacikdcab6a32015-02-11 15:08:59 -05001618 size = orig->end - orig->start + 1;
1619 if (size > BTRFS_MAX_EXTENT_SIZE) {
David Sterba823bb202017-01-04 11:09:51 +01001620 u32 num_extents;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001621 u64 new_size;
1622
1623 /*
Nikolay Borisov5c848192018-11-01 14:09:52 +02001624 * See the explanation in btrfs_merge_delalloc_extent, the same
Josef Bacikba117212015-03-13 15:01:24 -04001625 * applies here, just in reverse.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001626 */
1627 new_size = orig->end - split + 1;
David Sterba823bb202017-01-04 11:09:51 +01001628 num_extents = count_max_extents(new_size);
Josef Bacikba117212015-03-13 15:01:24 -04001629 new_size = split - orig->start;
David Sterba823bb202017-01-04 11:09:51 +01001630 num_extents += count_max_extents(new_size);
1631 if (count_max_extents(size) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001632 return;
1633 }
1634
Josef Bacik9e0baf62011-07-15 15:16:44 +00001635 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04001636 btrfs_mod_outstanding_extents(BTRFS_I(inode), 1);
Josef Bacik9e0baf62011-07-15 15:16:44 +00001637 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001638}
1639
1640/*
Nikolay Borisov5c848192018-11-01 14:09:52 +02001641 * Handle merged delayed allocation extents so we can keep track of new extents
1642 * that are just merged onto old extents, such as when we are doing sequential
1643 * writes, so we can properly account for the metadata space we'll need.
Josef Bacik9ed74f22009-09-11 16:12:44 -04001644 */
Nikolay Borisov5c848192018-11-01 14:09:52 +02001645void btrfs_merge_delalloc_extent(struct inode *inode, struct extent_state *new,
1646 struct extent_state *other)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001647{
Josef Bacikdcab6a32015-02-11 15:08:59 -05001648 u64 new_size, old_size;
David Sterba823bb202017-01-04 11:09:51 +01001649 u32 num_extents;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001650
Josef Bacik9ed74f22009-09-11 16:12:44 -04001651 /* not delalloc, ignore it */
1652 if (!(other->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001653 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001654
Josef Bacik8461a3d2015-03-13 15:12:08 -04001655 if (new->start > other->start)
1656 new_size = new->end - other->start + 1;
1657 else
1658 new_size = other->end - new->start + 1;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001659
1660 /* we're not bigger than the max, unreserve the space and go */
1661 if (new_size <= BTRFS_MAX_EXTENT_SIZE) {
1662 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04001663 btrfs_mod_outstanding_extents(BTRFS_I(inode), -1);
Josef Bacikdcab6a32015-02-11 15:08:59 -05001664 spin_unlock(&BTRFS_I(inode)->lock);
1665 return;
1666 }
1667
1668 /*
Josef Bacikba117212015-03-13 15:01:24 -04001669 * We have to add up either side to figure out how many extents were
1670 * accounted for before we merged into one big extent. If the number of
1671 * extents we accounted for is <= the amount we need for the new range
1672 * then we can return, otherwise drop. Think of it like this
1673 *
1674 * [ 4k][MAX_SIZE]
1675 *
1676 * So we've grown the extent by a MAX_SIZE extent, this would mean we
1677 * need 2 outstanding extents, on one side we have 1 and the other side
1678 * we have 1 so they are == and we can return. But in this case
1679 *
1680 * [MAX_SIZE+4k][MAX_SIZE+4k]
1681 *
1682 * Each range on their own accounts for 2 extents, but merged together
1683 * they are only 3 extents worth of accounting, so we need to drop in
1684 * this case.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001685 */
Josef Bacikba117212015-03-13 15:01:24 -04001686 old_size = other->end - other->start + 1;
David Sterba823bb202017-01-04 11:09:51 +01001687 num_extents = count_max_extents(old_size);
Josef Bacikba117212015-03-13 15:01:24 -04001688 old_size = new->end - new->start + 1;
David Sterba823bb202017-01-04 11:09:51 +01001689 num_extents += count_max_extents(old_size);
1690 if (count_max_extents(new_size) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001691 return;
1692
Josef Bacik9e0baf62011-07-15 15:16:44 +00001693 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04001694 btrfs_mod_outstanding_extents(BTRFS_I(inode), -1);
Josef Bacik9e0baf62011-07-15 15:16:44 +00001695 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001696}
1697
Miao Xieeb73c1b2013-05-15 07:48:22 +00001698static void btrfs_add_delalloc_inodes(struct btrfs_root *root,
1699 struct inode *inode)
1700{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001701 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1702
Miao Xieeb73c1b2013-05-15 07:48:22 +00001703 spin_lock(&root->delalloc_lock);
1704 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1705 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1706 &root->delalloc_inodes);
1707 set_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1708 &BTRFS_I(inode)->runtime_flags);
1709 root->nr_delalloc_inodes++;
1710 if (root->nr_delalloc_inodes == 1) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001711 spin_lock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001712 BUG_ON(!list_empty(&root->delalloc_root));
1713 list_add_tail(&root->delalloc_root,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001714 &fs_info->delalloc_roots);
1715 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001716 }
1717 }
1718 spin_unlock(&root->delalloc_lock);
1719}
1720
Nikolay Borisov2b877332018-04-27 12:21:51 +03001721
1722void __btrfs_del_delalloc_inode(struct btrfs_root *root,
1723 struct btrfs_inode *inode)
Miao Xieeb73c1b2013-05-15 07:48:22 +00001724{
David Sterba3ffbd682018-06-29 10:56:42 +02001725 struct btrfs_fs_info *fs_info = root->fs_info;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001726
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001727 if (!list_empty(&inode->delalloc_inodes)) {
1728 list_del_init(&inode->delalloc_inodes);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001729 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001730 &inode->runtime_flags);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001731 root->nr_delalloc_inodes--;
1732 if (!root->nr_delalloc_inodes) {
Nikolay Borisov7c8a0d32018-04-27 12:21:52 +03001733 ASSERT(list_empty(&root->delalloc_inodes));
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001734 spin_lock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001735 BUG_ON(list_empty(&root->delalloc_root));
1736 list_del_init(&root->delalloc_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001737 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001738 }
1739 }
Nikolay Borisov2b877332018-04-27 12:21:51 +03001740}
1741
1742static void btrfs_del_delalloc_inode(struct btrfs_root *root,
1743 struct btrfs_inode *inode)
1744{
1745 spin_lock(&root->delalloc_lock);
1746 __btrfs_del_delalloc_inode(root, inode);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001747 spin_unlock(&root->delalloc_lock);
1748}
1749
Chris Masond352ac62008-09-29 15:18:18 -04001750/*
Nikolay Borisove06a1fc2018-11-01 14:09:50 +02001751 * Properly track delayed allocation bytes in the inode and to maintain the
1752 * list of inodes that have pending delalloc work to be done.
Chris Masond352ac62008-09-29 15:18:18 -04001753 */
Nikolay Borisove06a1fc2018-11-01 14:09:50 +02001754void btrfs_set_delalloc_extent(struct inode *inode, struct extent_state *state,
1755 unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001756{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001757 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1758
Wang Shilong47059d92014-07-03 18:22:07 +08001759 if ((*bits & EXTENT_DEFRAG) && !(*bits & EXTENT_DELALLOC))
1760 WARN_ON(1);
Chris Mason75eff682008-12-15 15:54:40 -05001761 /*
1762 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001763 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001764 * bit, which is only set or cleared with irqs on
1765 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001766 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001767 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001768 u64 len = state->end + 1 - state->start;
Josef Bacik8b62f872017-10-19 14:15:55 -04001769 u32 num_extents = count_max_extents(len);
Nikolay Borisov70ddc552017-02-20 13:50:35 +02001770 bool do_list = !btrfs_is_free_space_inode(BTRFS_I(inode));
Josef Bacik9ed74f22009-09-11 16:12:44 -04001771
Josef Bacik8b62f872017-10-19 14:15:55 -04001772 spin_lock(&BTRFS_I(inode)->lock);
1773 btrfs_mod_outstanding_extents(BTRFS_I(inode), num_extents);
1774 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik287a0ab2010-03-19 18:07:23 +00001775
Josef Bacik6a3891c2015-03-16 17:38:52 -04001776 /* For sanity tests */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001777 if (btrfs_is_testing(fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04001778 return;
1779
Nikolay Borisov104b4e52017-06-20 21:01:20 +03001780 percpu_counter_add_batch(&fs_info->delalloc_bytes, len,
1781 fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001782 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001783 BTRFS_I(inode)->delalloc_bytes += len;
Wang Shilong47059d92014-07-03 18:22:07 +08001784 if (*bits & EXTENT_DEFRAG)
1785 BTRFS_I(inode)->defrag_bytes += len;
Miao Xiedf0af1a2013-01-29 10:11:59 +00001786 if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00001787 &BTRFS_I(inode)->runtime_flags))
1788 btrfs_add_delalloc_inodes(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001789 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001790 }
Filipe Mananaa7e3b972017-04-03 10:45:46 +01001791
1792 if (!(state->state & EXTENT_DELALLOC_NEW) &&
1793 (*bits & EXTENT_DELALLOC_NEW)) {
1794 spin_lock(&BTRFS_I(inode)->lock);
1795 BTRFS_I(inode)->new_delalloc_bytes += state->end + 1 -
1796 state->start;
1797 spin_unlock(&BTRFS_I(inode)->lock);
1798 }
Chris Mason291d6732008-01-29 15:55:23 -05001799}
1800
Chris Masond352ac62008-09-29 15:18:18 -04001801/*
Nikolay Borisova36bb5f2018-11-01 14:09:51 +02001802 * Once a range is no longer delalloc this function ensures that proper
1803 * accounting happens.
Chris Masond352ac62008-09-29 15:18:18 -04001804 */
Nikolay Borisova36bb5f2018-11-01 14:09:51 +02001805void btrfs_clear_delalloc_extent(struct inode *vfs_inode,
1806 struct extent_state *state, unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001807{
Nikolay Borisova36bb5f2018-11-01 14:09:51 +02001808 struct btrfs_inode *inode = BTRFS_I(vfs_inode);
1809 struct btrfs_fs_info *fs_info = btrfs_sb(vfs_inode->i_sb);
Wang Shilong47059d92014-07-03 18:22:07 +08001810 u64 len = state->end + 1 - state->start;
David Sterba823bb202017-01-04 11:09:51 +01001811 u32 num_extents = count_max_extents(len);
Wang Shilong47059d92014-07-03 18:22:07 +08001812
Filipe Manana4a4b9642017-07-27 19:52:55 +01001813 if ((state->state & EXTENT_DEFRAG) && (*bits & EXTENT_DEFRAG)) {
1814 spin_lock(&inode->lock);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001815 inode->defrag_bytes -= len;
Filipe Manana4a4b9642017-07-27 19:52:55 +01001816 spin_unlock(&inode->lock);
1817 }
Wang Shilong47059d92014-07-03 18:22:07 +08001818
Chris Mason75eff682008-12-15 15:54:40 -05001819 /*
1820 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001821 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001822 * bit, which is only set or cleared with irqs on
1823 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001824 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001825 struct btrfs_root *root = inode->root;
Liu Bo83eea1f2012-07-10 05:28:39 -06001826 bool do_list = !btrfs_is_free_space_inode(inode);
Chris Masonbcbfce82008-04-22 13:26:47 -04001827
Josef Bacik8b62f872017-10-19 14:15:55 -04001828 spin_lock(&inode->lock);
1829 btrfs_mod_outstanding_extents(inode, -num_extents);
1830 spin_unlock(&inode->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001831
Josef Bacikb6d08f02013-09-27 14:57:43 -04001832 /*
1833 * We don't reserve metadata space for space cache inodes so we
1834 * don't need to call dellalloc_release_metadata if there is an
1835 * error.
1836 */
Filipe Mananaa315e682017-03-06 23:04:20 +00001837 if (*bits & EXTENT_CLEAR_META_RESV &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001838 root != fs_info->tree_root)
Qu Wenruo43b18592017-12-12 15:34:32 +08001839 btrfs_delalloc_release_metadata(inode, len, false);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001840
Josef Bacik6a3891c2015-03-16 17:38:52 -04001841 /* For sanity tests. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001842 if (btrfs_is_testing(fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04001843 return;
1844
Filipe Mananaa315e682017-03-06 23:04:20 +00001845 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID &&
1846 do_list && !(state->state & EXTENT_NORESERVE) &&
1847 (*bits & EXTENT_CLEAR_DATA_RESV))
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001848 btrfs_free_reserved_data_space_noquota(
1849 &inode->vfs_inode,
Qu Wenruo51773be2015-10-08 18:19:37 +08001850 state->start, len);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001851
Nikolay Borisov104b4e52017-06-20 21:01:20 +03001852 percpu_counter_add_batch(&fs_info->delalloc_bytes, -len,
1853 fs_info->delalloc_batch);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001854 spin_lock(&inode->lock);
1855 inode->delalloc_bytes -= len;
1856 if (do_list && inode->delalloc_bytes == 0 &&
Miao Xiedf0af1a2013-01-29 10:11:59 +00001857 test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001858 &inode->runtime_flags))
Miao Xieeb73c1b2013-05-15 07:48:22 +00001859 btrfs_del_delalloc_inode(root, inode);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001860 spin_unlock(&inode->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001861 }
Filipe Mananaa7e3b972017-04-03 10:45:46 +01001862
1863 if ((state->state & EXTENT_DELALLOC_NEW) &&
1864 (*bits & EXTENT_DELALLOC_NEW)) {
1865 spin_lock(&inode->lock);
1866 ASSERT(inode->new_delalloc_bytes >= len);
1867 inode->new_delalloc_bytes -= len;
1868 spin_unlock(&inode->lock);
1869 }
Chris Mason291d6732008-01-29 15:55:23 -05001870}
1871
Chris Masond352ac62008-09-29 15:18:18 -04001872/*
Nikolay Borisovda12fe52018-11-27 20:57:58 +02001873 * btrfs_bio_fits_in_stripe - Checks whether the size of the given bio will fit
1874 * in a chunk's stripe. This function ensures that bios do not span a
1875 * stripe/chunk
Liu Bo6f034ec2016-06-22 18:31:49 -07001876 *
Nikolay Borisovda12fe52018-11-27 20:57:58 +02001877 * @page - The page we are about to add to the bio
1878 * @size - size we want to add to the bio
1879 * @bio - bio we want to ensure is smaller than a stripe
1880 * @bio_flags - flags of the bio
1881 *
1882 * return 1 if page cannot be added to the bio
1883 * return 0 if page can be added to the bio
Liu Bo6f034ec2016-06-22 18:31:49 -07001884 * return error otherwise
Chris Masond352ac62008-09-29 15:18:18 -04001885 */
Nikolay Borisovda12fe52018-11-27 20:57:58 +02001886int btrfs_bio_fits_in_stripe(struct page *page, size_t size, struct bio *bio,
1887 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04001888{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001889 struct inode *inode = page->mapping->host;
1890 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001891 u64 logical = (u64)bio->bi_iter.bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04001892 u64 length = 0;
1893 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04001894 int ret;
1895
Chris Mason771ed682008-11-06 22:02:51 -05001896 if (bio_flags & EXTENT_BIO_COMPRESSED)
1897 return 0;
1898
Kent Overstreet4f024f32013-10-11 15:44:27 -07001899 length = bio->bi_iter.bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04001900 map_length = length;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001901 ret = btrfs_map_block(fs_info, btrfs_op(bio), logical, &map_length,
1902 NULL, 0);
Liu Bo6f034ec2016-06-22 18:31:49 -07001903 if (ret < 0)
1904 return ret;
Chris Masond3977122009-01-05 21:25:51 -05001905 if (map_length < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04001906 return 1;
Jeff Mahoney3444a972011-10-03 23:23:13 -04001907 return 0;
Chris Mason239b14b2008-03-24 15:02:07 -04001908}
1909
Chris Masond352ac62008-09-29 15:18:18 -04001910/*
1911 * in order to insert checksums into the metadata in large chunks,
1912 * we wait until bio submission time. All the pages in the bio are
1913 * checksummed and sums are attached onto the ordered extent record.
1914 *
1915 * At IO completion time the cums attached on the ordered extent record
1916 * are inserted into the btree
1917 */
David Sterbad0ee3932018-03-08 14:35:48 +01001918static blk_status_t btrfs_submit_bio_start(void *private_data, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001919 u64 bio_offset)
Chris Mason065631f2008-02-20 12:07:25 -05001920{
Josef Bacikc6100a42017-05-05 11:57:13 -04001921 struct inode *inode = private_data;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001922 blk_status_t ret = 0;
Chris Masone0156402008-04-16 11:15:20 -04001923
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001924 ret = btrfs_csum_one_bio(inode, bio, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001925 BUG_ON(ret); /* -ENOMEM */
Chris Mason4a69a412008-11-06 22:03:00 -05001926 return 0;
1927}
Chris Masone0156402008-04-16 11:15:20 -04001928
Chris Mason4a69a412008-11-06 22:03:00 -05001929/*
Chris Masoncad321a2008-12-17 14:51:42 -05001930 * extent_io.c submission hook. This does the right thing for csum calculation
Liu Bo4c274bc2017-11-01 17:19:27 -06001931 * on write, or reading the csums from the tree before a read.
1932 *
1933 * Rules about async/sync submit,
1934 * a) read: sync submit
1935 *
1936 * b) write without checksum: sync submit
1937 *
1938 * c) write with checksum:
1939 * c-1) if bio is issued by fsync: sync submit
1940 * (sync_writers != 0)
1941 *
1942 * c-2) if root is reloc root: sync submit
1943 * (only in case of buffered IO)
1944 *
1945 * c-3) otherwise: async submit
Chris Masond352ac62008-09-29 15:18:18 -04001946 */
Linus Torvalds8c27cb32017-07-05 16:41:23 -07001947static blk_status_t btrfs_submit_bio_hook(void *private_data, struct bio *bio,
Josef Bacikc6100a42017-05-05 11:57:13 -04001948 int mirror_num, unsigned long bio_flags,
1949 u64 bio_offset)
Chris Mason44b8bd72008-04-16 11:14:51 -04001950{
Josef Bacikc6100a42017-05-05 11:57:13 -04001951 struct inode *inode = private_data;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001952 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason44b8bd72008-04-16 11:14:51 -04001953 struct btrfs_root *root = BTRFS_I(inode)->root;
Chandan Rajendra0d51e282015-07-27 15:26:43 +05301954 enum btrfs_wq_endio_type metadata = BTRFS_WQ_ENDIO_DATA;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001955 blk_status_t ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001956 int skip_sum;
Josef Bacikb812ce22012-11-16 13:56:32 -05001957 int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
Chris Mason44b8bd72008-04-16 11:14:51 -04001958
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001959 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masoncad321a2008-12-17 14:51:42 -05001960
Nikolay Borisov70ddc552017-02-20 13:50:35 +02001961 if (btrfs_is_free_space_inode(BTRFS_I(inode)))
Chandan Rajendra0d51e282015-07-27 15:26:43 +05301962 metadata = BTRFS_WQ_ENDIO_FREE_SPACE;
Jeff Mahoney04173412011-10-03 23:23:12 -04001963
Mike Christie37226b22016-06-05 14:31:52 -05001964 if (bio_op(bio) != REQ_OP_WRITE) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001965 ret = btrfs_bio_wq_end_io(fs_info, bio, metadata);
Josef Bacik5fd02042012-05-02 14:00:54 -04001966 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001967 goto out;
Josef Bacik5fd02042012-05-02 14:00:54 -04001968
Chris Masond20f7042008-12-08 16:58:54 -05001969 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Stefan Behrens61891922012-11-05 18:51:52 +01001970 ret = btrfs_submit_compressed_read(inode, bio,
1971 mirror_num,
1972 bio_flags);
1973 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001974 } else if (!skip_sum) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001975 ret = btrfs_lookup_bio_sums(inode, bio, NULL);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001976 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001977 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001978 }
Chris Mason4d1b5fb2008-08-20 09:44:52 -04001979 goto mapit;
Josef Bacikb812ce22012-11-16 13:56:32 -05001980 } else if (async && !skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05001981 /* csum items have already been cloned */
1982 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1983 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001984 /* we're doing a write, do the async checksumming */
Josef Bacikc6100a42017-05-05 11:57:13 -04001985 ret = btrfs_wq_submit_bio(fs_info, bio, mirror_num, bio_flags,
1986 bio_offset, inode,
David Sterbae288c082018-07-18 17:36:24 +02001987 btrfs_submit_bio_start);
Stefan Behrens61891922012-11-05 18:51:52 +01001988 goto out;
Josef Bacikb812ce22012-11-16 13:56:32 -05001989 } else if (!skip_sum) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001990 ret = btrfs_csum_one_bio(inode, bio, 0, 0);
Josef Bacikb812ce22012-11-16 13:56:32 -05001991 if (ret)
1992 goto out;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001993 }
1994
Chris Mason0b86a832008-03-24 15:01:56 -04001995mapit:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001996 ret = btrfs_map_bio(fs_info, bio, mirror_num, 0);
Stefan Behrens61891922012-11-05 18:51:52 +01001997
1998out:
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001999 if (ret) {
2000 bio->bi_status = ret;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002001 bio_endio(bio);
2002 }
Stefan Behrens61891922012-11-05 18:51:52 +01002003 return ret;
Chris Mason065631f2008-02-20 12:07:25 -05002004}
Chris Mason6885f302008-02-20 16:11:05 -05002005
Chris Masond352ac62008-09-29 15:18:18 -04002006/*
2007 * given a list of ordered sums record them in the inode. This happens
2008 * at IO completion time based on sums calculated at bio submission time.
2009 */
Chris Masonba1da2f2008-07-17 12:54:15 -04002010static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
David Sterbadf9f6282017-02-10 19:35:37 +01002011 struct inode *inode, struct list_head *list)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002012{
Chris Masone6dcd2d2008-07-17 12:53:50 -04002013 struct btrfs_ordered_sum *sum;
Nikolay Borisovac01f262018-01-08 10:59:43 +02002014 int ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002015
Qinghuang Fengc6e30872009-01-21 10:59:08 -05002016 list_for_each_entry(sum, list, list) {
David Sterba7c2871a2017-11-08 01:07:43 +01002017 trans->adding_csums = true;
Nikolay Borisovac01f262018-01-08 10:59:43 +02002018 ret = btrfs_csum_file_blocks(trans,
Chris Masond20f7042008-12-08 16:58:54 -05002019 BTRFS_I(inode)->root->fs_info->csum_root, sum);
David Sterba7c2871a2017-11-08 01:07:43 +01002020 trans->adding_csums = false;
Nikolay Borisovac01f262018-01-08 10:59:43 +02002021 if (ret)
2022 return ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002023 }
2024 return 0;
2025}
2026
Josef Bacik2ac55d42010-02-03 19:33:23 +00002027int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
Filipe Mananae3b8a482017-11-04 00:16:59 +00002028 unsigned int extra_bits,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08002029 struct extent_state **cached_state, int dedupe)
Chris Masonea8c2812008-08-04 23:17:27 -04002030{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002031 WARN_ON((end & (PAGE_SIZE - 1)) == 0);
Chris Masonea8c2812008-08-04 23:17:27 -04002032 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
Filipe Mananae3b8a482017-11-04 00:16:59 +00002033 extra_bits, cached_state);
Chris Masonea8c2812008-08-04 23:17:27 -04002034}
2035
Chris Masond352ac62008-09-29 15:18:18 -04002036/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04002037struct btrfs_writepage_fixup {
2038 struct page *page;
2039 struct btrfs_work work;
2040};
2041
Christoph Hellwigb2950862008-12-02 09:54:17 -05002042static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04002043{
2044 struct btrfs_writepage_fixup *fixup;
2045 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002046 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08002047 struct extent_changeset *data_reserved = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04002048 struct page *page;
2049 struct inode *inode;
2050 u64 page_start;
2051 u64 page_end;
Jeff Mahoney87826df2012-02-15 16:23:57 +01002052 int ret;
Chris Mason247e7432008-07-17 12:53:51 -04002053
2054 fixup = container_of(work, struct btrfs_writepage_fixup, work);
2055 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -04002056again:
Chris Mason247e7432008-07-17 12:53:51 -04002057 lock_page(page);
2058 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
2059 ClearPageChecked(page);
2060 goto out_page;
2061 }
2062
2063 inode = page->mapping->host;
2064 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002065 page_end = page_offset(page) + PAGE_SIZE - 1;
Chris Mason247e7432008-07-17 12:53:51 -04002066
David Sterbaff13db42015-12-03 14:30:40 +01002067 lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002068 &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04002069
2070 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04002071 if (PagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04002072 goto out;
Chris Mason4a096752008-07-21 10:29:44 -04002073
Nikolay Borisova776c6f2017-02-20 13:50:49 +02002074 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002075 PAGE_SIZE);
Chris Mason4a096752008-07-21 10:29:44 -04002076 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00002077 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
David Sterbae43bbe52017-12-12 21:43:52 +01002078 page_end, &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04002079 unlock_page(page);
2080 btrfs_start_ordered_extent(inode, ordered, 1);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002081 btrfs_put_ordered_extent(ordered);
Chris Mason4a096752008-07-21 10:29:44 -04002082 goto again;
2083 }
Chris Mason247e7432008-07-17 12:53:51 -04002084
Qu Wenruo364ecf32017-02-27 15:10:38 +08002085 ret = btrfs_delalloc_reserve_space(inode, &data_reserved, page_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002086 PAGE_SIZE);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002087 if (ret) {
2088 mapping_set_error(page->mapping, ret);
2089 end_extent_writepage(page, ret, page_start, page_end);
2090 ClearPageChecked(page);
2091 goto out;
2092 }
2093
Nikolay Borisovf3038ee2017-12-05 09:29:19 +02002094 ret = btrfs_set_extent_delalloc(inode, page_start, page_end, 0,
2095 &cached_state, 0);
2096 if (ret) {
2097 mapping_set_error(page->mapping, ret);
2098 end_extent_writepage(page, ret, page_start, page_end);
2099 ClearPageChecked(page);
2100 goto out;
2101 }
2102
Chris Mason247e7432008-07-17 12:53:51 -04002103 ClearPageChecked(page);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002104 set_page_dirty(page);
Qu Wenruo43b18592017-12-12 15:34:32 +08002105 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE, false);
Chris Mason247e7432008-07-17 12:53:51 -04002106out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00002107 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01002108 &cached_state);
Chris Mason247e7432008-07-17 12:53:51 -04002109out_page:
2110 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002111 put_page(page);
Miao Xieb897abe2011-01-26 16:19:22 +08002112 kfree(fixup);
Qu Wenruo364ecf32017-02-27 15:10:38 +08002113 extent_changeset_free(data_reserved);
Chris Mason247e7432008-07-17 12:53:51 -04002114}
2115
2116/*
2117 * There are a few paths in the higher layers of the kernel that directly
2118 * set the page dirty bit without asking the filesystem if it is a
2119 * good idea. This causes problems because we want to make sure COW
2120 * properly happens and the data=ordered rules are followed.
2121 *
Chris Masonc8b97812008-10-29 14:49:59 -04002122 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04002123 * hasn't been properly setup for IO. We kick off an async process
2124 * to fix it up. The async helper will wait for ordered extents, set
2125 * the delalloc bit and make it safe to write the page.
2126 */
Nikolay Borisovd75855b2018-11-01 14:09:47 +02002127int btrfs_writepage_cow_fixup(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04002128{
2129 struct inode *inode = page->mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002130 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason247e7432008-07-17 12:53:51 -04002131 struct btrfs_writepage_fixup *fixup;
Chris Mason247e7432008-07-17 12:53:51 -04002132
Chris Mason8b62b722009-09-02 16:53:46 -04002133 /* this page is properly in the ordered list */
2134 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04002135 return 0;
2136
2137 if (PageChecked(page))
2138 return -EAGAIN;
2139
2140 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
2141 if (!fixup)
2142 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04002143
Chris Mason247e7432008-07-17 12:53:51 -04002144 SetPageChecked(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002145 get_page(page);
Liu Bo9e0af232014-08-15 23:36:53 +08002146 btrfs_init_work(&fixup->work, btrfs_fixup_helper,
2147 btrfs_writepage_fixup_worker, NULL, NULL);
Chris Mason247e7432008-07-17 12:53:51 -04002148 fixup->page = page;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002149 btrfs_queue_work(fs_info->fixup_workers, &fixup->work);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002150 return -EBUSY;
Chris Mason247e7432008-07-17 12:53:51 -04002151}
2152
Yan Zhengd899e052008-10-30 14:25:28 -04002153static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
2154 struct inode *inode, u64 file_pos,
2155 u64 disk_bytenr, u64 disk_num_bytes,
2156 u64 num_bytes, u64 ram_bytes,
2157 u8 compression, u8 encryption,
2158 u16 other_encoding, int extent_type)
2159{
2160 struct btrfs_root *root = BTRFS_I(inode)->root;
2161 struct btrfs_file_extent_item *fi;
2162 struct btrfs_path *path;
2163 struct extent_buffer *leaf;
2164 struct btrfs_key ins;
Qu Wenruoa12b8772017-02-27 15:10:37 +08002165 u64 qg_released;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002166 int extent_inserted = 0;
Yan Zhengd899e052008-10-30 14:25:28 -04002167 int ret;
2168
2169 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07002170 if (!path)
2171 return -ENOMEM;
Yan Zhengd899e052008-10-30 14:25:28 -04002172
Chris Masona1ed8352009-09-11 12:27:37 -04002173 /*
2174 * we may be replacing one extent in the tree with another.
2175 * The new extent is pinned in the extent map, and we don't want
2176 * to drop it from the cache until it is completely in the btree.
2177 *
2178 * So, tell btrfs_drop_extents to leave this extent in the cache.
2179 * the caller is expected to unpin it and allow it to be merged
2180 * with the others.
2181 */
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002182 ret = __btrfs_drop_extents(trans, root, inode, path, file_pos,
2183 file_pos + num_bytes, NULL, 0,
2184 1, sizeof(*fi), &extent_inserted);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002185 if (ret)
2186 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04002187
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002188 if (!extent_inserted) {
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002189 ins.objectid = btrfs_ino(BTRFS_I(inode));
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002190 ins.offset = file_pos;
2191 ins.type = BTRFS_EXTENT_DATA_KEY;
2192
2193 path->leave_spinning = 1;
2194 ret = btrfs_insert_empty_item(trans, root, path, &ins,
2195 sizeof(*fi));
2196 if (ret)
2197 goto out;
2198 }
Yan Zhengd899e052008-10-30 14:25:28 -04002199 leaf = path->nodes[0];
2200 fi = btrfs_item_ptr(leaf, path->slots[0],
2201 struct btrfs_file_extent_item);
2202 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
2203 btrfs_set_file_extent_type(leaf, fi, extent_type);
2204 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
2205 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
2206 btrfs_set_file_extent_offset(leaf, fi, 0);
2207 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
2208 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
2209 btrfs_set_file_extent_compression(leaf, fi, compression);
2210 btrfs_set_file_extent_encryption(leaf, fi, encryption);
2211 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
Chris Masonb9473432009-03-13 11:00:37 -04002212
Yan Zhengd899e052008-10-30 14:25:28 -04002213 btrfs_mark_buffer_dirty(leaf);
Josef Bacikce195332012-09-25 15:26:16 -04002214 btrfs_release_path(path);
Yan Zhengd899e052008-10-30 14:25:28 -04002215
2216 inode_add_bytes(inode, num_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -04002217
2218 ins.objectid = disk_bytenr;
2219 ins.offset = disk_num_bytes;
2220 ins.type = BTRFS_EXTENT_ITEM_KEY;
Qu Wenruoa12b8772017-02-27 15:10:37 +08002221
Qu Wenruo297d7502015-09-08 17:08:37 +08002222 /*
Qu Wenruo5846a3c2015-10-26 14:11:18 +08002223 * Release the reserved range from inode dirty range map, as it is
2224 * already moved into delayed_ref_head
Qu Wenruo297d7502015-09-08 17:08:37 +08002225 */
Qu Wenruoa12b8772017-02-27 15:10:37 +08002226 ret = btrfs_qgroup_release_data(inode, file_pos, ram_bytes);
2227 if (ret < 0)
2228 goto out;
2229 qg_released = ret;
Josef Bacik84f7d8e2017-09-29 15:43:49 -04002230 ret = btrfs_alloc_reserved_file_extent(trans, root,
2231 btrfs_ino(BTRFS_I(inode)),
2232 file_pos, qg_released, &ins);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002233out:
Yan Zhengd899e052008-10-30 14:25:28 -04002234 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04002235
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002236 return ret;
Yan Zhengd899e052008-10-30 14:25:28 -04002237}
2238
Liu Bo38c227d2013-01-29 03:18:40 +00002239/* snapshot-aware defrag */
2240struct sa_defrag_extent_backref {
2241 struct rb_node node;
2242 struct old_sa_defrag_extent *old;
2243 u64 root_id;
2244 u64 inum;
2245 u64 file_pos;
2246 u64 extent_offset;
2247 u64 num_bytes;
2248 u64 generation;
2249};
2250
2251struct old_sa_defrag_extent {
2252 struct list_head list;
2253 struct new_sa_defrag_extent *new;
2254
2255 u64 extent_offset;
2256 u64 bytenr;
2257 u64 offset;
2258 u64 len;
2259 int count;
2260};
2261
2262struct new_sa_defrag_extent {
2263 struct rb_root root;
2264 struct list_head head;
2265 struct btrfs_path *path;
2266 struct inode *inode;
2267 u64 file_pos;
2268 u64 len;
2269 u64 bytenr;
2270 u64 disk_len;
2271 u8 compress_type;
2272};
2273
2274static int backref_comp(struct sa_defrag_extent_backref *b1,
2275 struct sa_defrag_extent_backref *b2)
2276{
2277 if (b1->root_id < b2->root_id)
2278 return -1;
2279 else if (b1->root_id > b2->root_id)
2280 return 1;
2281
2282 if (b1->inum < b2->inum)
2283 return -1;
2284 else if (b1->inum > b2->inum)
2285 return 1;
2286
2287 if (b1->file_pos < b2->file_pos)
2288 return -1;
2289 else if (b1->file_pos > b2->file_pos)
2290 return 1;
2291
2292 /*
2293 * [------------------------------] ===> (a range of space)
2294 * |<--->| |<---->| =============> (fs/file tree A)
2295 * |<---------------------------->| ===> (fs/file tree B)
2296 *
2297 * A range of space can refer to two file extents in one tree while
2298 * refer to only one file extent in another tree.
2299 *
2300 * So we may process a disk offset more than one time(two extents in A)
2301 * and locate at the same extent(one extent in B), then insert two same
2302 * backrefs(both refer to the extent in B).
2303 */
2304 return 0;
2305}
2306
2307static void backref_insert(struct rb_root *root,
2308 struct sa_defrag_extent_backref *backref)
2309{
2310 struct rb_node **p = &root->rb_node;
2311 struct rb_node *parent = NULL;
2312 struct sa_defrag_extent_backref *entry;
2313 int ret;
2314
2315 while (*p) {
2316 parent = *p;
2317 entry = rb_entry(parent, struct sa_defrag_extent_backref, node);
2318
2319 ret = backref_comp(backref, entry);
2320 if (ret < 0)
2321 p = &(*p)->rb_left;
2322 else
2323 p = &(*p)->rb_right;
2324 }
2325
2326 rb_link_node(&backref->node, parent, p);
2327 rb_insert_color(&backref->node, root);
2328}
2329
2330/*
2331 * Note the backref might has changed, and in this case we just return 0.
2332 */
2333static noinline int record_one_backref(u64 inum, u64 offset, u64 root_id,
2334 void *ctx)
2335{
2336 struct btrfs_file_extent_item *extent;
Liu Bo38c227d2013-01-29 03:18:40 +00002337 struct old_sa_defrag_extent *old = ctx;
2338 struct new_sa_defrag_extent *new = old->new;
2339 struct btrfs_path *path = new->path;
2340 struct btrfs_key key;
2341 struct btrfs_root *root;
2342 struct sa_defrag_extent_backref *backref;
2343 struct extent_buffer *leaf;
2344 struct inode *inode = new->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002345 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002346 int slot;
2347 int ret;
2348 u64 extent_offset;
2349 u64 num_bytes;
2350
2351 if (BTRFS_I(inode)->root->root_key.objectid == root_id &&
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002352 inum == btrfs_ino(BTRFS_I(inode)))
Liu Bo38c227d2013-01-29 03:18:40 +00002353 return 0;
2354
2355 key.objectid = root_id;
2356 key.type = BTRFS_ROOT_ITEM_KEY;
2357 key.offset = (u64)-1;
2358
Liu Bo38c227d2013-01-29 03:18:40 +00002359 root = btrfs_read_fs_root_no_name(fs_info, &key);
2360 if (IS_ERR(root)) {
2361 if (PTR_ERR(root) == -ENOENT)
2362 return 0;
2363 WARN_ON(1);
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002364 btrfs_debug(fs_info, "inum=%llu, offset=%llu, root_id=%llu",
Liu Bo38c227d2013-01-29 03:18:40 +00002365 inum, offset, root_id);
2366 return PTR_ERR(root);
2367 }
2368
2369 key.objectid = inum;
2370 key.type = BTRFS_EXTENT_DATA_KEY;
2371 if (offset > (u64)-1 << 32)
2372 key.offset = 0;
2373 else
2374 key.offset = offset;
2375
2376 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05302377 if (WARN_ON(ret < 0))
Liu Bo38c227d2013-01-29 03:18:40 +00002378 return ret;
Josef Bacik50f13192013-07-22 12:50:37 -04002379 ret = 0;
Liu Bo38c227d2013-01-29 03:18:40 +00002380
2381 while (1) {
2382 cond_resched();
2383
2384 leaf = path->nodes[0];
2385 slot = path->slots[0];
2386
2387 if (slot >= btrfs_header_nritems(leaf)) {
2388 ret = btrfs_next_leaf(root, path);
2389 if (ret < 0) {
2390 goto out;
2391 } else if (ret > 0) {
2392 ret = 0;
2393 goto out;
2394 }
2395 continue;
2396 }
2397
2398 path->slots[0]++;
2399
2400 btrfs_item_key_to_cpu(leaf, &key, slot);
2401
2402 if (key.objectid > inum)
2403 goto out;
2404
2405 if (key.objectid < inum || key.type != BTRFS_EXTENT_DATA_KEY)
2406 continue;
2407
2408 extent = btrfs_item_ptr(leaf, slot,
2409 struct btrfs_file_extent_item);
2410
2411 if (btrfs_file_extent_disk_bytenr(leaf, extent) != old->bytenr)
2412 continue;
2413
Liu Boe68afa42013-07-01 22:13:26 +08002414 /*
2415 * 'offset' refers to the exact key.offset,
2416 * NOT the 'offset' field in btrfs_extent_data_ref, ie.
2417 * (key.offset - extent_offset).
2418 */
2419 if (key.offset != offset)
Liu Bo38c227d2013-01-29 03:18:40 +00002420 continue;
2421
Liu Boe68afa42013-07-01 22:13:26 +08002422 extent_offset = btrfs_file_extent_offset(leaf, extent);
Liu Bo38c227d2013-01-29 03:18:40 +00002423 num_bytes = btrfs_file_extent_num_bytes(leaf, extent);
Liu Boe68afa42013-07-01 22:13:26 +08002424
Liu Bo38c227d2013-01-29 03:18:40 +00002425 if (extent_offset >= old->extent_offset + old->offset +
2426 old->len || extent_offset + num_bytes <=
2427 old->extent_offset + old->offset)
2428 continue;
Liu Bo38c227d2013-01-29 03:18:40 +00002429 break;
2430 }
2431
2432 backref = kmalloc(sizeof(*backref), GFP_NOFS);
2433 if (!backref) {
2434 ret = -ENOENT;
2435 goto out;
2436 }
2437
2438 backref->root_id = root_id;
2439 backref->inum = inum;
Liu Boe68afa42013-07-01 22:13:26 +08002440 backref->file_pos = offset;
Liu Bo38c227d2013-01-29 03:18:40 +00002441 backref->num_bytes = num_bytes;
2442 backref->extent_offset = extent_offset;
2443 backref->generation = btrfs_file_extent_generation(leaf, extent);
2444 backref->old = old;
2445 backref_insert(&new->root, backref);
2446 old->count++;
2447out:
2448 btrfs_release_path(path);
2449 WARN_ON(ret);
2450 return ret;
2451}
2452
2453static noinline bool record_extent_backrefs(struct btrfs_path *path,
2454 struct new_sa_defrag_extent *new)
2455{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002456 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002457 struct old_sa_defrag_extent *old, *tmp;
2458 int ret;
2459
2460 new->path = path;
2461
2462 list_for_each_entry_safe(old, tmp, &new->head, list) {
Liu Boe68afa42013-07-01 22:13:26 +08002463 ret = iterate_inodes_from_logical(old->bytenr +
2464 old->extent_offset, fs_info,
Liu Bo38c227d2013-01-29 03:18:40 +00002465 path, record_one_backref,
Zygo Blaxellc995ab32017-09-22 13:58:45 -04002466 old, false);
Josef Bacik4724b102013-11-05 11:11:40 -05002467 if (ret < 0 && ret != -ENOENT)
2468 return false;
Liu Bo38c227d2013-01-29 03:18:40 +00002469
2470 /* no backref to be processed for this extent */
2471 if (!old->count) {
2472 list_del(&old->list);
2473 kfree(old);
2474 }
2475 }
2476
2477 if (list_empty(&new->head))
2478 return false;
2479
2480 return true;
2481}
2482
2483static int relink_is_mergable(struct extent_buffer *leaf,
2484 struct btrfs_file_extent_item *fi,
Liu Bo116e0022013-08-02 16:30:40 +08002485 struct new_sa_defrag_extent *new)
Liu Bo38c227d2013-01-29 03:18:40 +00002486{
Liu Bo116e0022013-08-02 16:30:40 +08002487 if (btrfs_file_extent_disk_bytenr(leaf, fi) != new->bytenr)
Liu Bo38c227d2013-01-29 03:18:40 +00002488 return 0;
2489
2490 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG)
2491 return 0;
2492
Liu Bo116e0022013-08-02 16:30:40 +08002493 if (btrfs_file_extent_compression(leaf, fi) != new->compress_type)
2494 return 0;
2495
2496 if (btrfs_file_extent_encryption(leaf, fi) ||
Liu Bo38c227d2013-01-29 03:18:40 +00002497 btrfs_file_extent_other_encoding(leaf, fi))
2498 return 0;
2499
2500 return 1;
2501}
2502
2503/*
2504 * Note the backref might has changed, and in this case we just return 0.
2505 */
2506static noinline int relink_extent_backref(struct btrfs_path *path,
2507 struct sa_defrag_extent_backref *prev,
2508 struct sa_defrag_extent_backref *backref)
2509{
2510 struct btrfs_file_extent_item *extent;
2511 struct btrfs_file_extent_item *item;
2512 struct btrfs_ordered_extent *ordered;
2513 struct btrfs_trans_handle *trans;
Liu Bo38c227d2013-01-29 03:18:40 +00002514 struct btrfs_root *root;
2515 struct btrfs_key key;
2516 struct extent_buffer *leaf;
2517 struct old_sa_defrag_extent *old = backref->old;
2518 struct new_sa_defrag_extent *new = old->new;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002519 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002520 struct inode *inode;
2521 struct extent_state *cached = NULL;
2522 int ret = 0;
2523 u64 start;
2524 u64 len;
2525 u64 lock_start;
2526 u64 lock_end;
2527 bool merge = false;
2528 int index;
2529
2530 if (prev && prev->root_id == backref->root_id &&
2531 prev->inum == backref->inum &&
2532 prev->file_pos + prev->num_bytes == backref->file_pos)
2533 merge = true;
2534
2535 /* step 1: get root */
2536 key.objectid = backref->root_id;
2537 key.type = BTRFS_ROOT_ITEM_KEY;
2538 key.offset = (u64)-1;
2539
Liu Bo38c227d2013-01-29 03:18:40 +00002540 index = srcu_read_lock(&fs_info->subvol_srcu);
2541
2542 root = btrfs_read_fs_root_no_name(fs_info, &key);
2543 if (IS_ERR(root)) {
2544 srcu_read_unlock(&fs_info->subvol_srcu, index);
2545 if (PTR_ERR(root) == -ENOENT)
2546 return 0;
2547 return PTR_ERR(root);
2548 }
Liu Bo38c227d2013-01-29 03:18:40 +00002549
Wang Shilongbcbba5e2014-02-08 23:46:35 +08002550 if (btrfs_root_readonly(root)) {
2551 srcu_read_unlock(&fs_info->subvol_srcu, index);
2552 return 0;
2553 }
2554
Liu Bo38c227d2013-01-29 03:18:40 +00002555 /* step 2: get inode */
2556 key.objectid = backref->inum;
2557 key.type = BTRFS_INODE_ITEM_KEY;
2558 key.offset = 0;
2559
2560 inode = btrfs_iget(fs_info->sb, &key, root, NULL);
2561 if (IS_ERR(inode)) {
2562 srcu_read_unlock(&fs_info->subvol_srcu, index);
2563 return 0;
2564 }
2565
2566 srcu_read_unlock(&fs_info->subvol_srcu, index);
2567
2568 /* step 3: relink backref */
2569 lock_start = backref->file_pos;
2570 lock_end = backref->file_pos + backref->num_bytes - 1;
2571 lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
David Sterbaff13db42015-12-03 14:30:40 +01002572 &cached);
Liu Bo38c227d2013-01-29 03:18:40 +00002573
2574 ordered = btrfs_lookup_first_ordered_extent(inode, lock_end);
2575 if (ordered) {
2576 btrfs_put_ordered_extent(ordered);
2577 goto out_unlock;
2578 }
2579
2580 trans = btrfs_join_transaction(root);
2581 if (IS_ERR(trans)) {
2582 ret = PTR_ERR(trans);
2583 goto out_unlock;
2584 }
2585
2586 key.objectid = backref->inum;
2587 key.type = BTRFS_EXTENT_DATA_KEY;
2588 key.offset = backref->file_pos;
2589
2590 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2591 if (ret < 0) {
2592 goto out_free_path;
2593 } else if (ret > 0) {
2594 ret = 0;
2595 goto out_free_path;
2596 }
2597
2598 extent = btrfs_item_ptr(path->nodes[0], path->slots[0],
2599 struct btrfs_file_extent_item);
2600
2601 if (btrfs_file_extent_generation(path->nodes[0], extent) !=
2602 backref->generation)
2603 goto out_free_path;
2604
2605 btrfs_release_path(path);
2606
2607 start = backref->file_pos;
2608 if (backref->extent_offset < old->extent_offset + old->offset)
2609 start += old->extent_offset + old->offset -
2610 backref->extent_offset;
2611
2612 len = min(backref->extent_offset + backref->num_bytes,
2613 old->extent_offset + old->offset + old->len);
2614 len -= max(backref->extent_offset, old->extent_offset + old->offset);
2615
2616 ret = btrfs_drop_extents(trans, root, inode, start,
2617 start + len, 1);
2618 if (ret)
2619 goto out_free_path;
2620again:
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002621 key.objectid = btrfs_ino(BTRFS_I(inode));
Liu Bo38c227d2013-01-29 03:18:40 +00002622 key.type = BTRFS_EXTENT_DATA_KEY;
2623 key.offset = start;
2624
Liu Boa09a0a72013-03-11 09:20:58 +00002625 path->leave_spinning = 1;
Liu Bo38c227d2013-01-29 03:18:40 +00002626 if (merge) {
2627 struct btrfs_file_extent_item *fi;
2628 u64 extent_len;
2629 struct btrfs_key found_key;
2630
Gui Hecheng3c9665d2014-01-23 13:41:09 +08002631 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
Liu Bo38c227d2013-01-29 03:18:40 +00002632 if (ret < 0)
2633 goto out_free_path;
2634
2635 path->slots[0]--;
2636 leaf = path->nodes[0];
2637 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2638
2639 fi = btrfs_item_ptr(leaf, path->slots[0],
2640 struct btrfs_file_extent_item);
2641 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
2642
Liu Bo116e0022013-08-02 16:30:40 +08002643 if (extent_len + found_key.offset == start &&
2644 relink_is_mergable(leaf, fi, new)) {
Liu Bo38c227d2013-01-29 03:18:40 +00002645 btrfs_set_file_extent_num_bytes(leaf, fi,
2646 extent_len + len);
2647 btrfs_mark_buffer_dirty(leaf);
2648 inode_add_bytes(inode, len);
2649
2650 ret = 1;
2651 goto out_free_path;
2652 } else {
2653 merge = false;
2654 btrfs_release_path(path);
2655 goto again;
2656 }
2657 }
2658
2659 ret = btrfs_insert_empty_item(trans, root, path, &key,
2660 sizeof(*extent));
2661 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002662 btrfs_abort_transaction(trans, ret);
Liu Bo38c227d2013-01-29 03:18:40 +00002663 goto out_free_path;
2664 }
2665
2666 leaf = path->nodes[0];
2667 item = btrfs_item_ptr(leaf, path->slots[0],
2668 struct btrfs_file_extent_item);
2669 btrfs_set_file_extent_disk_bytenr(leaf, item, new->bytenr);
2670 btrfs_set_file_extent_disk_num_bytes(leaf, item, new->disk_len);
2671 btrfs_set_file_extent_offset(leaf, item, start - new->file_pos);
2672 btrfs_set_file_extent_num_bytes(leaf, item, len);
2673 btrfs_set_file_extent_ram_bytes(leaf, item, new->len);
2674 btrfs_set_file_extent_generation(leaf, item, trans->transid);
2675 btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG);
2676 btrfs_set_file_extent_compression(leaf, item, new->compress_type);
2677 btrfs_set_file_extent_encryption(leaf, item, 0);
2678 btrfs_set_file_extent_other_encoding(leaf, item, 0);
2679
2680 btrfs_mark_buffer_dirty(leaf);
2681 inode_add_bytes(inode, len);
Liu Boa09a0a72013-03-11 09:20:58 +00002682 btrfs_release_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002683
Josef Bacik84f7d8e2017-09-29 15:43:49 -04002684 ret = btrfs_inc_extent_ref(trans, root, new->bytenr,
Liu Bo38c227d2013-01-29 03:18:40 +00002685 new->disk_len, 0,
2686 backref->root_id, backref->inum,
Filipe Mananab06c4bf2015-10-23 07:52:54 +01002687 new->file_pos); /* start - extent_offset */
Liu Bo38c227d2013-01-29 03:18:40 +00002688 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002689 btrfs_abort_transaction(trans, ret);
Liu Bo38c227d2013-01-29 03:18:40 +00002690 goto out_free_path;
2691 }
2692
2693 ret = 1;
2694out_free_path:
2695 btrfs_release_path(path);
Liu Boa09a0a72013-03-11 09:20:58 +00002696 path->leave_spinning = 0;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002697 btrfs_end_transaction(trans);
Liu Bo38c227d2013-01-29 03:18:40 +00002698out_unlock:
2699 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
David Sterbae43bbe52017-12-12 21:43:52 +01002700 &cached);
Liu Bo38c227d2013-01-29 03:18:40 +00002701 iput(inode);
2702 return ret;
2703}
2704
Liu Bo6f519562013-10-29 10:45:05 +08002705static void free_sa_defrag_extent(struct new_sa_defrag_extent *new)
2706{
2707 struct old_sa_defrag_extent *old, *tmp;
2708
2709 if (!new)
2710 return;
2711
2712 list_for_each_entry_safe(old, tmp, &new->head, list) {
Liu Bo6f519562013-10-29 10:45:05 +08002713 kfree(old);
2714 }
2715 kfree(new);
2716}
2717
Liu Bo38c227d2013-01-29 03:18:40 +00002718static void relink_file_extents(struct new_sa_defrag_extent *new)
2719{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002720 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002721 struct btrfs_path *path;
Liu Bo38c227d2013-01-29 03:18:40 +00002722 struct sa_defrag_extent_backref *backref;
2723 struct sa_defrag_extent_backref *prev = NULL;
Liu Bo38c227d2013-01-29 03:18:40 +00002724 struct rb_node *node;
2725 int ret;
2726
Liu Bo38c227d2013-01-29 03:18:40 +00002727 path = btrfs_alloc_path();
2728 if (!path)
2729 return;
2730
2731 if (!record_extent_backrefs(path, new)) {
2732 btrfs_free_path(path);
2733 goto out;
2734 }
2735 btrfs_release_path(path);
2736
2737 while (1) {
2738 node = rb_first(&new->root);
2739 if (!node)
2740 break;
2741 rb_erase(node, &new->root);
2742
2743 backref = rb_entry(node, struct sa_defrag_extent_backref, node);
2744
2745 ret = relink_extent_backref(path, prev, backref);
2746 WARN_ON(ret < 0);
2747
2748 kfree(prev);
2749
2750 if (ret == 1)
2751 prev = backref;
2752 else
2753 prev = NULL;
2754 cond_resched();
2755 }
2756 kfree(prev);
2757
2758 btrfs_free_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002759out:
Liu Bo6f519562013-10-29 10:45:05 +08002760 free_sa_defrag_extent(new);
2761
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002762 atomic_dec(&fs_info->defrag_running);
2763 wake_up(&fs_info->transaction_wait);
Liu Bo38c227d2013-01-29 03:18:40 +00002764}
2765
2766static struct new_sa_defrag_extent *
2767record_old_file_extents(struct inode *inode,
2768 struct btrfs_ordered_extent *ordered)
2769{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002770 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002771 struct btrfs_root *root = BTRFS_I(inode)->root;
2772 struct btrfs_path *path;
2773 struct btrfs_key key;
Liu Bo6f519562013-10-29 10:45:05 +08002774 struct old_sa_defrag_extent *old;
Liu Bo38c227d2013-01-29 03:18:40 +00002775 struct new_sa_defrag_extent *new;
2776 int ret;
2777
2778 new = kmalloc(sizeof(*new), GFP_NOFS);
2779 if (!new)
2780 return NULL;
2781
2782 new->inode = inode;
2783 new->file_pos = ordered->file_offset;
2784 new->len = ordered->len;
2785 new->bytenr = ordered->start;
2786 new->disk_len = ordered->disk_len;
2787 new->compress_type = ordered->compress_type;
2788 new->root = RB_ROOT;
2789 INIT_LIST_HEAD(&new->head);
2790
2791 path = btrfs_alloc_path();
2792 if (!path)
2793 goto out_kfree;
2794
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002795 key.objectid = btrfs_ino(BTRFS_I(inode));
Liu Bo38c227d2013-01-29 03:18:40 +00002796 key.type = BTRFS_EXTENT_DATA_KEY;
2797 key.offset = new->file_pos;
2798
2799 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2800 if (ret < 0)
2801 goto out_free_path;
2802 if (ret > 0 && path->slots[0] > 0)
2803 path->slots[0]--;
2804
2805 /* find out all the old extents for the file range */
2806 while (1) {
2807 struct btrfs_file_extent_item *extent;
2808 struct extent_buffer *l;
2809 int slot;
2810 u64 num_bytes;
2811 u64 offset;
2812 u64 end;
2813 u64 disk_bytenr;
2814 u64 extent_offset;
2815
2816 l = path->nodes[0];
2817 slot = path->slots[0];
2818
2819 if (slot >= btrfs_header_nritems(l)) {
2820 ret = btrfs_next_leaf(root, path);
2821 if (ret < 0)
Liu Bo6f519562013-10-29 10:45:05 +08002822 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002823 else if (ret > 0)
2824 break;
2825 continue;
2826 }
2827
2828 btrfs_item_key_to_cpu(l, &key, slot);
2829
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002830 if (key.objectid != btrfs_ino(BTRFS_I(inode)))
Liu Bo38c227d2013-01-29 03:18:40 +00002831 break;
2832 if (key.type != BTRFS_EXTENT_DATA_KEY)
2833 break;
2834 if (key.offset >= new->file_pos + new->len)
2835 break;
2836
2837 extent = btrfs_item_ptr(l, slot, struct btrfs_file_extent_item);
2838
2839 num_bytes = btrfs_file_extent_num_bytes(l, extent);
2840 if (key.offset + num_bytes < new->file_pos)
2841 goto next;
2842
2843 disk_bytenr = btrfs_file_extent_disk_bytenr(l, extent);
2844 if (!disk_bytenr)
2845 goto next;
2846
2847 extent_offset = btrfs_file_extent_offset(l, extent);
2848
2849 old = kmalloc(sizeof(*old), GFP_NOFS);
2850 if (!old)
Liu Bo6f519562013-10-29 10:45:05 +08002851 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002852
2853 offset = max(new->file_pos, key.offset);
2854 end = min(new->file_pos + new->len, key.offset + num_bytes);
2855
2856 old->bytenr = disk_bytenr;
2857 old->extent_offset = extent_offset;
2858 old->offset = offset - key.offset;
2859 old->len = end - offset;
2860 old->new = new;
2861 old->count = 0;
2862 list_add_tail(&old->list, &new->head);
2863next:
2864 path->slots[0]++;
2865 cond_resched();
2866 }
2867
2868 btrfs_free_path(path);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002869 atomic_inc(&fs_info->defrag_running);
Liu Bo38c227d2013-01-29 03:18:40 +00002870
2871 return new;
2872
Liu Bo38c227d2013-01-29 03:18:40 +00002873out_free_path:
2874 btrfs_free_path(path);
2875out_kfree:
Liu Bo6f519562013-10-29 10:45:05 +08002876 free_sa_defrag_extent(new);
Liu Bo38c227d2013-01-29 03:18:40 +00002877 return NULL;
2878}
2879
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002880static void btrfs_release_delalloc_bytes(struct btrfs_fs_info *fs_info,
Miao Xiee570fd22014-06-19 10:42:50 +08002881 u64 start, u64 len)
2882{
2883 struct btrfs_block_group_cache *cache;
2884
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002885 cache = btrfs_lookup_block_group(fs_info, start);
Miao Xiee570fd22014-06-19 10:42:50 +08002886 ASSERT(cache);
2887
2888 spin_lock(&cache->lock);
2889 cache->delalloc_bytes -= len;
2890 spin_unlock(&cache->lock);
2891
2892 btrfs_put_block_group(cache);
2893}
2894
Chris Masond352ac62008-09-29 15:18:18 -04002895/* as ordered data IO finishes, this gets called so we can finish
2896 * an ordered extent if the range of bytes in the file it covers are
2897 * fully written.
2898 */
Josef Bacik5fd02042012-05-02 14:00:54 -04002899static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002900{
Josef Bacik5fd02042012-05-02 14:00:54 -04002901 struct inode *inode = ordered_extent->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002902 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002903 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002904 struct btrfs_trans_handle *trans = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002905 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002906 struct extent_state *cached_state = NULL;
Liu Bo38c227d2013-01-29 03:18:40 +00002907 struct new_sa_defrag_extent *new = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08002908 int compress_type = 0;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002909 int ret = 0;
2910 u64 logical_len = ordered_extent->len;
Li Zefan82d59022011-04-20 10:33:24 +08002911 bool nolock;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002912 bool truncated = false;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002913 bool range_locked = false;
2914 bool clear_new_delalloc_bytes = false;
Josef Bacik49940bd2018-10-11 15:54:21 -04002915 bool clear_reserved_extent = true;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002916
2917 if (!test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
2918 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags) &&
2919 !test_bit(BTRFS_ORDERED_DIRECT, &ordered_extent->flags))
2920 clear_new_delalloc_bytes = true;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002921
Nikolay Borisov70ddc552017-02-20 13:50:35 +02002922 nolock = btrfs_is_free_space_inode(BTRFS_I(inode));
Josef Bacik0cb59c92010-07-02 12:14:14 -04002923
Josef Bacik5fd02042012-05-02 14:00:54 -04002924 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
2925 ret = -EIO;
2926 goto out;
2927 }
2928
Nikolay Borisov7ab79562017-02-20 13:50:57 +02002929 btrfs_free_io_failure_record(BTRFS_I(inode),
2930 ordered_extent->file_offset,
2931 ordered_extent->file_offset +
2932 ordered_extent->len - 1);
Miao Xief6124962014-09-12 18:44:04 +08002933
Josef Bacik77cef2e2013-08-29 13:57:21 -04002934 if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) {
2935 truncated = true;
2936 logical_len = ordered_extent->truncated_len;
2937 /* Truncated the entire extent, don't bother adding */
2938 if (!logical_len)
2939 goto out;
2940 }
2941
Yan, Zhengc2167752009-11-12 09:34:21 +00002942 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002943 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
Qu Wenruo94ed9382015-09-08 17:25:56 +08002944
2945 /*
2946 * For mwrite(mmap + memset to write) case, we still reserve
2947 * space for NOCOW range.
2948 * As NOCOW won't cause a new delayed ref, just free the space
2949 */
Qu Wenruobc42bda2017-02-27 15:10:39 +08002950 btrfs_qgroup_free_data(inode, NULL, ordered_extent->file_offset,
Qu Wenruo94ed9382015-09-08 17:25:56 +08002951 ordered_extent->len);
Josef Bacik6c760c02012-11-09 10:53:21 -05002952 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2953 if (nolock)
2954 trans = btrfs_join_transaction_nolock(root);
2955 else
2956 trans = btrfs_join_transaction(root);
2957 if (IS_ERR(trans)) {
2958 ret = PTR_ERR(trans);
2959 trans = NULL;
2960 goto out;
Yan, Zhengc2167752009-11-12 09:34:21 +00002961 }
Josef Bacik69fe2d72017-10-19 14:15:57 -04002962 trans->block_rsv = &BTRFS_I(inode)->block_rsv;
Josef Bacik6c760c02012-11-09 10:53:21 -05002963 ret = btrfs_update_inode_fallback(trans, root, inode);
2964 if (ret) /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04002965 btrfs_abort_transaction(trans, ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00002966 goto out;
2967 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002968
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002969 range_locked = true;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002970 lock_extent_bits(io_tree, ordered_extent->file_offset,
2971 ordered_extent->file_offset + ordered_extent->len - 1,
David Sterbaff13db42015-12-03 14:30:40 +01002972 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002973
Liu Bo38c227d2013-01-29 03:18:40 +00002974 ret = test_range_bit(io_tree, ordered_extent->file_offset,
2975 ordered_extent->file_offset + ordered_extent->len - 1,
Liu Bo452e62b2017-05-26 17:44:23 -06002976 EXTENT_DEFRAG, 0, cached_state);
Liu Bo38c227d2013-01-29 03:18:40 +00002977 if (ret) {
2978 u64 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
Josef Bacik8101c8d2014-01-29 16:05:30 -05002979 if (0 && last_snapshot >= BTRFS_I(inode)->generation)
Liu Bo38c227d2013-01-29 03:18:40 +00002980 /* the inode is shared */
2981 new = record_old_file_extents(inode, ordered_extent);
2982
2983 clear_extent_bit(io_tree, ordered_extent->file_offset,
2984 ordered_extent->file_offset + ordered_extent->len - 1,
David Sterbaae0f1622017-10-31 16:37:52 +01002985 EXTENT_DEFRAG, 0, 0, &cached_state);
Liu Bo38c227d2013-01-29 03:18:40 +00002986 }
2987
Josef Bacik0cb59c92010-07-02 12:14:14 -04002988 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002989 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002990 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002991 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002992 if (IS_ERR(trans)) {
2993 ret = PTR_ERR(trans);
2994 trans = NULL;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002995 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002996 }
Chris Masona79b7d42014-05-22 16:18:52 -07002997
Josef Bacik69fe2d72017-10-19 14:15:57 -04002998 trans->block_rsv = &BTRFS_I(inode)->block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00002999
Chris Masonc8b97812008-10-29 14:49:59 -04003000 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08003001 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04003002 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08003003 BUG_ON(compress_type);
Justin Maggardb430b772017-10-30 15:29:10 -07003004 btrfs_qgroup_free_data(inode, NULL, ordered_extent->file_offset,
3005 ordered_extent->len);
Nikolay Borisov7a6d7062017-02-20 13:50:48 +02003006 ret = btrfs_mark_extent_written(trans, BTRFS_I(inode),
Yan Zhengd899e052008-10-30 14:25:28 -04003007 ordered_extent->file_offset,
3008 ordered_extent->file_offset +
Josef Bacik77cef2e2013-08-29 13:57:21 -04003009 logical_len);
Yan Zhengd899e052008-10-30 14:25:28 -04003010 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003011 BUG_ON(root == fs_info->tree_root);
Yan Zhengd899e052008-10-30 14:25:28 -04003012 ret = insert_reserved_file_extent(trans, inode,
3013 ordered_extent->file_offset,
3014 ordered_extent->start,
3015 ordered_extent->disk_len,
Josef Bacik77cef2e2013-08-29 13:57:21 -04003016 logical_len, logical_len,
Li Zefan261507a02010-12-17 14:21:50 +08003017 compress_type, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04003018 BTRFS_FILE_EXTENT_REG);
Josef Bacik49940bd2018-10-11 15:54:21 -04003019 if (!ret) {
3020 clear_reserved_extent = false;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003021 btrfs_release_delalloc_bytes(fs_info,
Miao Xiee570fd22014-06-19 10:42:50 +08003022 ordered_extent->start,
3023 ordered_extent->disk_len);
Josef Bacik49940bd2018-10-11 15:54:21 -04003024 }
Yan Zhengd899e052008-10-30 14:25:28 -04003025 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04003026 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
3027 ordered_extent->file_offset, ordered_extent->len,
3028 trans->transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003029 if (ret < 0) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003030 btrfs_abort_transaction(trans, ret);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003031 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003032 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00003033
Nikolay Borisovac01f262018-01-08 10:59:43 +02003034 ret = add_pending_csums(trans, inode, &ordered_extent->list);
3035 if (ret) {
3036 btrfs_abort_transaction(trans, ret);
3037 goto out;
3038 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04003039
Josef Bacik6c760c02012-11-09 10:53:21 -05003040 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
3041 ret = btrfs_update_inode_fallback(trans, root, inode);
3042 if (ret) { /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04003043 btrfs_abort_transaction(trans, ret);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003044 goto out;
Josef Bacik1ef30be2011-04-05 19:25:36 -04003045 }
3046 ret = 0;
Yan, Zhengc2167752009-11-12 09:34:21 +00003047out:
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003048 if (range_locked || clear_new_delalloc_bytes) {
3049 unsigned int clear_bits = 0;
3050
3051 if (range_locked)
3052 clear_bits |= EXTENT_LOCKED;
3053 if (clear_new_delalloc_bytes)
3054 clear_bits |= EXTENT_DELALLOC_NEW;
3055 clear_extent_bit(&BTRFS_I(inode)->io_tree,
3056 ordered_extent->file_offset,
3057 ordered_extent->file_offset +
3058 ordered_extent->len - 1,
3059 clear_bits,
3060 (clear_bits & EXTENT_LOCKED) ? 1 : 0,
David Sterbaae0f1622017-10-31 16:37:52 +01003061 0, &cached_state);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003062 }
3063
Miao Xiea698d0752012-09-20 01:51:59 -06003064 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003065 btrfs_end_transaction(trans);
Josef Bacik0cb59c92010-07-02 12:14:14 -04003066
Josef Bacik77cef2e2013-08-29 13:57:21 -04003067 if (ret || truncated) {
3068 u64 start, end;
3069
3070 if (truncated)
3071 start = ordered_extent->file_offset + logical_len;
3072 else
3073 start = ordered_extent->file_offset;
3074 end = ordered_extent->file_offset + ordered_extent->len - 1;
David Sterbaf08dc362017-10-31 17:02:39 +01003075 clear_extent_uptodate(io_tree, start, end, NULL);
Josef Bacik77cef2e2013-08-29 13:57:21 -04003076
3077 /* Drop the cache for the part of the extent we didn't write. */
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02003078 btrfs_drop_extent_cache(BTRFS_I(inode), start, end, 0);
Josef Bacik5fd02042012-05-02 14:00:54 -04003079
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003080 /*
3081 * If the ordered extent had an IOERR or something else went
3082 * wrong we need to return the space for this ordered extent
Josef Bacik77cef2e2013-08-29 13:57:21 -04003083 * back to the allocator. We only free the extent in the
3084 * truncated case if we didn't write out the extent at all.
Josef Bacik49940bd2018-10-11 15:54:21 -04003085 *
3086 * If we made it past insert_reserved_file_extent before we
3087 * errored out then we don't need to do this as the accounting
3088 * has already been done.
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003089 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04003090 if ((ret || !logical_len) &&
Josef Bacik49940bd2018-10-11 15:54:21 -04003091 clear_reserved_extent &&
Josef Bacik77cef2e2013-08-29 13:57:21 -04003092 !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003093 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003094 btrfs_free_reserved_extent(fs_info,
3095 ordered_extent->start,
Miao Xiee570fd22014-06-19 10:42:50 +08003096 ordered_extent->disk_len, 1);
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003097 }
3098
3099
Josef Bacik5fd02042012-05-02 14:00:54 -04003100 /*
Liu Bo8bad3c02012-06-18 12:14:23 +08003101 * This needs to be done to make sure anybody waiting knows we are done
3102 * updating everything for this ordered extent.
Josef Bacik5fd02042012-05-02 14:00:54 -04003103 */
3104 btrfs_remove_ordered_extent(inode, ordered_extent);
3105
Liu Bo38c227d2013-01-29 03:18:40 +00003106 /* for snapshot-aware defrag */
Liu Bo6f519562013-10-29 10:45:05 +08003107 if (new) {
3108 if (ret) {
3109 free_sa_defrag_extent(new);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003110 atomic_dec(&fs_info->defrag_running);
Liu Bo6f519562013-10-29 10:45:05 +08003111 } else {
3112 relink_file_extents(new);
3113 }
3114 }
Liu Bo38c227d2013-01-29 03:18:40 +00003115
Chris Masone6dcd2d2008-07-17 12:53:50 -04003116 /* once for us */
3117 btrfs_put_ordered_extent(ordered_extent);
3118 /* once for the tree */
3119 btrfs_put_ordered_extent(ordered_extent);
3120
Ethan Liene73e81b2018-05-28 13:48:20 +08003121 /* Try to release some metadata so we don't get an OOM but don't wait */
3122 btrfs_btree_balance_dirty_nodelay(fs_info);
3123
Josef Bacik5fd02042012-05-02 14:00:54 -04003124 return ret;
3125}
3126
3127static void finish_ordered_fn(struct btrfs_work *work)
3128{
3129 struct btrfs_ordered_extent *ordered_extent;
3130 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
3131 btrfs_finish_ordered_io(ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003132}
3133
Nikolay Borisovc6297322018-11-08 10:18:08 +02003134void btrfs_writepage_endio_finish_ordered(struct page *page, u64 start,
3135 u64 end, int uptodate)
Chris Mason211f90e2008-07-18 11:56:15 -04003136{
Josef Bacik5fd02042012-05-02 14:00:54 -04003137 struct inode *inode = page->mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003138 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik5fd02042012-05-02 14:00:54 -04003139 struct btrfs_ordered_extent *ordered_extent = NULL;
Liu Bo9e0af232014-08-15 23:36:53 +08003140 struct btrfs_workqueue *wq;
3141 btrfs_work_func_t func;
Josef Bacik5fd02042012-05-02 14:00:54 -04003142
liubo1abe9b82011-03-24 11:18:59 +00003143 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
3144
Chris Mason8b62b722009-09-02 16:53:46 -04003145 ClearPagePrivate2(page);
Josef Bacik5fd02042012-05-02 14:00:54 -04003146 if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
3147 end - start + 1, uptodate))
David Sterbac3988d62017-02-17 15:18:32 +01003148 return;
Josef Bacik5fd02042012-05-02 14:00:54 -04003149
Nikolay Borisov70ddc552017-02-20 13:50:35 +02003150 if (btrfs_is_free_space_inode(BTRFS_I(inode))) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003151 wq = fs_info->endio_freespace_worker;
Liu Bo9e0af232014-08-15 23:36:53 +08003152 func = btrfs_freespace_write_helper;
3153 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003154 wq = fs_info->endio_write_workers;
Liu Bo9e0af232014-08-15 23:36:53 +08003155 func = btrfs_endio_write_helper;
3156 }
Josef Bacik5fd02042012-05-02 14:00:54 -04003157
Liu Bo9e0af232014-08-15 23:36:53 +08003158 btrfs_init_work(&ordered_extent->work, func, finish_ordered_fn, NULL,
3159 NULL);
3160 btrfs_queue_work(wq, &ordered_extent->work);
Chris Mason211f90e2008-07-18 11:56:15 -04003161}
3162
Miao Xiedc380ae2014-09-12 18:43:55 +08003163static int __readpage_endio_check(struct inode *inode,
3164 struct btrfs_io_bio *io_bio,
3165 int icsum, struct page *page,
3166 int pgoff, u64 start, size_t len)
3167{
3168 char *kaddr;
3169 u32 csum_expected;
3170 u32 csum = ~(u32)0;
Miao Xiedc380ae2014-09-12 18:43:55 +08003171
3172 csum_expected = *(((u32 *)io_bio->csum) + icsum);
3173
3174 kaddr = kmap_atomic(page);
3175 csum = btrfs_csum_data(kaddr + pgoff, csum, len);
Domagoj Tršan0b5e3da2016-10-27 08:52:33 +01003176 btrfs_csum_final(csum, (u8 *)&csum);
Miao Xiedc380ae2014-09-12 18:43:55 +08003177 if (csum != csum_expected)
3178 goto zeroit;
3179
3180 kunmap_atomic(kaddr);
3181 return 0;
3182zeroit:
Nikolay Borisov0970a222017-02-20 13:50:53 +02003183 btrfs_print_data_csum_error(BTRFS_I(inode), start, csum, csum_expected,
Qu Wenruo6f6b6432017-02-09 10:45:06 +08003184 io_bio->mirror_num);
Miao Xiedc380ae2014-09-12 18:43:55 +08003185 memset(kaddr + pgoff, 1, len);
3186 flush_dcache_page(page);
3187 kunmap_atomic(kaddr);
Miao Xiedc380ae2014-09-12 18:43:55 +08003188 return -EIO;
3189}
3190
Chris Masond352ac62008-09-29 15:18:18 -04003191/*
Chris Masond352ac62008-09-29 15:18:18 -04003192 * when reads are done, we need to check csums to verify the data is correct
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02003193 * if there's a match, we allow the bio to finish. If not, the code in
3194 * extent_io.c will try to find good copies for us.
Chris Masond352ac62008-09-29 15:18:18 -04003195 */
Miao Xiefacc8a222013-07-25 19:22:34 +08003196static int btrfs_readpage_end_io_hook(struct btrfs_io_bio *io_bio,
3197 u64 phy_offset, struct page *page,
3198 u64 start, u64 end, int mirror)
Chris Mason07157aa2007-08-30 08:50:51 -04003199{
Miao Xie4eee4fa2012-12-21 09:17:45 +00003200 size_t offset = start - page_offset(page);
Chris Mason07157aa2007-08-30 08:50:51 -04003201 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05003202 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masonff79f812007-10-15 16:22:25 -04003203 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -05003204
Chris Masond20f7042008-12-08 16:58:54 -05003205 if (PageChecked(page)) {
3206 ClearPageChecked(page);
Miao Xiedc380ae2014-09-12 18:43:55 +08003207 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003208 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003209
3210 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Miao Xiedc380ae2014-09-12 18:43:55 +08003211 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003212
Yan Zheng17d217f2008-12-12 10:03:38 -05003213 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04003214 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
David Sterba91166212016-04-26 23:54:39 +02003215 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM);
Yan Zheng17d217f2008-12-12 10:03:38 -05003216 return 0;
3217 }
3218
Miao Xiefacc8a222013-07-25 19:22:34 +08003219 phy_offset >>= inode->i_sb->s_blocksize_bits;
Miao Xiedc380ae2014-09-12 18:43:55 +08003220 return __readpage_endio_check(inode, io_bio, phy_offset, page, offset,
3221 start, (size_t)(end - start + 1));
Chris Mason07157aa2007-08-30 08:50:51 -04003222}
Chris Masonb888db22007-08-27 16:49:44 -04003223
Nikolay Borisovc1c3fac2018-01-16 09:31:58 +02003224/*
3225 * btrfs_add_delayed_iput - perform a delayed iput on @inode
3226 *
3227 * @inode: The inode we want to perform iput on
3228 *
3229 * This function uses the generic vfs_inode::i_count to track whether we should
3230 * just decrement it (in case it's > 1) or if this is the last iput then link
3231 * the inode to the delayed iput machinery. Delayed iputs are processed at
3232 * transaction commit time/superblock commit/cleaner kthread.
3233 */
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003234void btrfs_add_delayed_iput(struct inode *inode)
3235{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003236 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
David Sterba8089fe62015-11-19 14:15:51 +01003237 struct btrfs_inode *binode = BTRFS_I(inode);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003238
3239 if (atomic_add_unless(&inode->i_count, -1, 1))
3240 return;
3241
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003242 spin_lock(&fs_info->delayed_iput_lock);
Nikolay Borisovc1c3fac2018-01-16 09:31:58 +02003243 ASSERT(list_empty(&binode->delayed_iput));
3244 list_add_tail(&binode->delayed_iput, &fs_info->delayed_iputs);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003245 spin_unlock(&fs_info->delayed_iput_lock);
3246}
3247
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003248void btrfs_run_delayed_iputs(struct btrfs_fs_info *fs_info)
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003249{
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003250
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003251 spin_lock(&fs_info->delayed_iput_lock);
David Sterba8089fe62015-11-19 14:15:51 +01003252 while (!list_empty(&fs_info->delayed_iputs)) {
3253 struct btrfs_inode *inode;
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003254
David Sterba8089fe62015-11-19 14:15:51 +01003255 inode = list_first_entry(&fs_info->delayed_iputs,
3256 struct btrfs_inode, delayed_iput);
Nikolay Borisovc1c3fac2018-01-16 09:31:58 +02003257 list_del_init(&inode->delayed_iput);
David Sterba8089fe62015-11-19 14:15:51 +01003258 spin_unlock(&fs_info->delayed_iput_lock);
3259 iput(&inode->vfs_inode);
3260 spin_lock(&fs_info->delayed_iput_lock);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003261 }
David Sterba8089fe62015-11-19 14:15:51 +01003262 spin_unlock(&fs_info->delayed_iput_lock);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003263}
3264
Yan, Zhengd68fc572010-05-16 10:49:58 -04003265/*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003266 * This creates an orphan entry for the given inode in case something goes wrong
3267 * in the middle of an unlink.
Josef Bacik7b128762008-07-24 12:17:14 -04003268 */
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003269int btrfs_orphan_add(struct btrfs_trans_handle *trans,
Omar Sandoval27919062018-05-11 13:13:37 -07003270 struct btrfs_inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003271{
Yan, Zhengd68fc572010-05-16 10:49:58 -04003272 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003273
Omar Sandoval27919062018-05-11 13:13:37 -07003274 ret = btrfs_insert_orphan_item(trans, inode->root, btrfs_ino(inode));
3275 if (ret && ret != -EEXIST) {
3276 btrfs_abort_transaction(trans, ret);
3277 return ret;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003278 }
3279
Yan, Zhengd68fc572010-05-16 10:49:58 -04003280 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003281}
3282
3283/*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003284 * We have done the delete so we can go ahead and remove the orphan item for
3285 * this particular inode.
Josef Bacik7b128762008-07-24 12:17:14 -04003286 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00003287static int btrfs_orphan_del(struct btrfs_trans_handle *trans,
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003288 struct btrfs_inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003289{
Omar Sandoval27919062018-05-11 13:13:37 -07003290 return btrfs_del_orphan_item(trans, inode->root, btrfs_ino(inode));
Josef Bacik7b128762008-07-24 12:17:14 -04003291}
3292
3293/*
3294 * this cleans up any orphans that may be left on the list from the last use
3295 * of this root.
3296 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003297int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04003298{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003299 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik7b128762008-07-24 12:17:14 -04003300 struct btrfs_path *path;
3301 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04003302 struct btrfs_key key, found_key;
3303 struct btrfs_trans_handle *trans;
3304 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003305 u64 last_objectid = 0;
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003306 int ret = 0, nr_unlink = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003307
Yan, Zhengd68fc572010-05-16 10:49:58 -04003308 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003309 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00003310
3311 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003312 if (!path) {
3313 ret = -ENOMEM;
3314 goto out;
3315 }
David Sterbae4058b52015-11-27 16:31:35 +01003316 path->reada = READA_BACK;
Josef Bacik7b128762008-07-24 12:17:14 -04003317
3318 key.objectid = BTRFS_ORPHAN_OBJECTID;
David Sterba962a2982014-06-04 18:41:45 +02003319 key.type = BTRFS_ORPHAN_ITEM_KEY;
Josef Bacik7b128762008-07-24 12:17:14 -04003320 key.offset = (u64)-1;
3321
Josef Bacik7b128762008-07-24 12:17:14 -04003322 while (1) {
3323 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003324 if (ret < 0)
3325 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003326
3327 /*
3328 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003329 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04003330 * find the key and see if we have stuff that matches
3331 */
3332 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003333 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003334 if (path->slots[0] == 0)
3335 break;
3336 path->slots[0]--;
3337 }
3338
3339 /* pull out the item */
3340 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04003341 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3342
3343 /* make sure the item matches what we want */
3344 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
3345 break;
David Sterba962a2982014-06-04 18:41:45 +02003346 if (found_key.type != BTRFS_ORPHAN_ITEM_KEY)
Josef Bacik7b128762008-07-24 12:17:14 -04003347 break;
3348
3349 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02003350 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04003351
3352 /*
3353 * this is where we are basically btrfs_lookup, without the
3354 * crossing root thing. we store the inode number in the
3355 * offset of the orphan item.
3356 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003357
3358 if (found_key.offset == last_objectid) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003359 btrfs_err(fs_info,
3360 "Error removing orphan entry, stopping orphan cleanup");
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003361 ret = -EINVAL;
3362 goto out;
3363 }
3364
3365 last_objectid = found_key.offset;
3366
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003367 found_key.objectid = found_key.offset;
3368 found_key.type = BTRFS_INODE_ITEM_KEY;
3369 found_key.offset = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003370 inode = btrfs_iget(fs_info->sb, &found_key, root, NULL);
Rusty Russell8c6ffba2013-07-15 11:20:32 +09303371 ret = PTR_ERR_OR_ZERO(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003372 if (ret && ret != -ENOENT)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003373 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003374
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003375 if (ret == -ENOENT && root == fs_info->tree_root) {
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003376 struct btrfs_root *dead_root;
3377 struct btrfs_fs_info *fs_info = root->fs_info;
3378 int is_dead_root = 0;
3379
3380 /*
3381 * this is an orphan in the tree root. Currently these
3382 * could come from 2 sources:
3383 * a) a snapshot deletion in progress
3384 * b) a free space cache inode
3385 * We need to distinguish those two, as the snapshot
3386 * orphan must not get deleted.
3387 * find_dead_roots already ran before us, so if this
3388 * is a snapshot deletion, we should find the root
3389 * in the dead_roots list
3390 */
3391 spin_lock(&fs_info->trans_lock);
3392 list_for_each_entry(dead_root, &fs_info->dead_roots,
3393 root_list) {
3394 if (dead_root->root_key.objectid ==
3395 found_key.objectid) {
3396 is_dead_root = 1;
3397 break;
3398 }
3399 }
3400 spin_unlock(&fs_info->trans_lock);
3401 if (is_dead_root) {
3402 /* prevent this orphan from being found again */
3403 key.offset = found_key.objectid - 1;
3404 continue;
3405 }
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003406
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003407 }
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003408
Josef Bacika8c9e572011-09-21 16:55:59 -04003409 /*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003410 * If we have an inode with links, there are a couple of
3411 * possibilities. Old kernels (before v3.12) used to create an
3412 * orphan item for truncate indicating that there were possibly
3413 * extent items past i_size that needed to be deleted. In v3.12,
3414 * truncate was changed to update i_size in sync with the extent
3415 * items, but the (useless) orphan item was still created. Since
3416 * v4.18, we don't create the orphan item for truncate at all.
3417 *
3418 * So, this item could mean that we need to do a truncate, but
3419 * only if this filesystem was last used on a pre-v3.12 kernel
3420 * and was not cleanly unmounted. The odds of that are quite
3421 * slim, and it's a pain to do the truncate now, so just delete
3422 * the orphan item.
3423 *
3424 * It's also possible that this orphan item was supposed to be
3425 * deleted but wasn't. The inode number may have been reused,
3426 * but either way, we can delete the orphan item.
Josef Bacika8c9e572011-09-21 16:55:59 -04003427 */
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003428 if (ret == -ENOENT || inode->i_nlink) {
3429 if (!ret)
3430 iput(inode);
Josef Bacika8c9e572011-09-21 16:55:59 -04003431 trans = btrfs_start_transaction(root, 1);
3432 if (IS_ERR(trans)) {
3433 ret = PTR_ERR(trans);
3434 goto out;
3435 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003436 btrfs_debug(fs_info, "auto deleting %Lu",
3437 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003438 ret = btrfs_del_orphan_item(trans, root,
3439 found_key.objectid);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003440 btrfs_end_transaction(trans);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003441 if (ret)
3442 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003443 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003444 }
Josef Bacik7b128762008-07-24 12:17:14 -04003445
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003446 nr_unlink++;
Josef Bacik7b128762008-07-24 12:17:14 -04003447
3448 /* this will do delete_inode and everything for us */
3449 iput(inode);
3450 }
Miao Xie3254c872011-11-10 20:45:05 -05003451 /* release the path since we're done with it */
3452 btrfs_release_path(path);
3453
Yan, Zhengd68fc572010-05-16 10:49:58 -04003454 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
3455
Omar Sandovala575cee2018-05-11 13:13:38 -07003456 if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003457 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003458 if (!IS_ERR(trans))
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003459 btrfs_end_transaction(trans);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003460 }
Josef Bacik7b128762008-07-24 12:17:14 -04003461
3462 if (nr_unlink)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003463 btrfs_debug(fs_info, "unlinked %d orphans", nr_unlink);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003464
3465out:
3466 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003467 btrfs_err(fs_info, "could not do orphan cleanup %d", ret);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003468 btrfs_free_path(path);
3469 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003470}
3471
Chris Masond352ac62008-09-29 15:18:18 -04003472/*
Chris Mason46a53cc2009-04-27 11:47:50 -04003473 * very simple check to peek ahead in the leaf looking for xattrs. If we
3474 * don't find any xattrs, we know there can't be any acls.
3475 *
3476 * slot is the slot the inode is in, objectid is the objectid of the inode
3477 */
3478static noinline int acls_after_inode_item(struct extent_buffer *leaf,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003479 int slot, u64 objectid,
3480 int *first_xattr_slot)
Chris Mason46a53cc2009-04-27 11:47:50 -04003481{
3482 u32 nritems = btrfs_header_nritems(leaf);
3483 struct btrfs_key found_key;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003484 static u64 xattr_access = 0;
3485 static u64 xattr_default = 0;
Chris Mason46a53cc2009-04-27 11:47:50 -04003486 int scanned = 0;
3487
Josef Bacikf23b5a52013-06-19 10:16:26 -04003488 if (!xattr_access) {
Andreas Gruenbacher97d79292015-12-02 14:44:35 +01003489 xattr_access = btrfs_name_hash(XATTR_NAME_POSIX_ACL_ACCESS,
3490 strlen(XATTR_NAME_POSIX_ACL_ACCESS));
3491 xattr_default = btrfs_name_hash(XATTR_NAME_POSIX_ACL_DEFAULT,
3492 strlen(XATTR_NAME_POSIX_ACL_DEFAULT));
Josef Bacikf23b5a52013-06-19 10:16:26 -04003493 }
3494
Chris Mason46a53cc2009-04-27 11:47:50 -04003495 slot++;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003496 *first_xattr_slot = -1;
Chris Mason46a53cc2009-04-27 11:47:50 -04003497 while (slot < nritems) {
3498 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3499
3500 /* we found a different objectid, there must not be acls */
3501 if (found_key.objectid != objectid)
3502 return 0;
3503
3504 /* we found an xattr, assume we've got an acl */
Josef Bacikf23b5a52013-06-19 10:16:26 -04003505 if (found_key.type == BTRFS_XATTR_ITEM_KEY) {
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003506 if (*first_xattr_slot == -1)
3507 *first_xattr_slot = slot;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003508 if (found_key.offset == xattr_access ||
3509 found_key.offset == xattr_default)
3510 return 1;
3511 }
Chris Mason46a53cc2009-04-27 11:47:50 -04003512
3513 /*
3514 * we found a key greater than an xattr key, there can't
3515 * be any acls later on
3516 */
3517 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
3518 return 0;
3519
3520 slot++;
3521 scanned++;
3522
3523 /*
3524 * it goes inode, inode backrefs, xattrs, extents,
3525 * so if there are a ton of hard links to an inode there can
3526 * be a lot of backrefs. Don't waste time searching too hard,
3527 * this is just an optimization
3528 */
3529 if (scanned >= 8)
3530 break;
3531 }
3532 /* we hit the end of the leaf before we found an xattr or
3533 * something larger than an xattr. We have to assume the inode
3534 * has acls
3535 */
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003536 if (*first_xattr_slot == -1)
3537 *first_xattr_slot = slot;
Chris Mason46a53cc2009-04-27 11:47:50 -04003538 return 1;
3539}
3540
3541/*
Chris Masond352ac62008-09-29 15:18:18 -04003542 * read an inode from the btree into the in-memory inode
3543 */
Filipe Manana4222ea72018-10-24 10:13:03 +01003544static int btrfs_read_locked_inode(struct inode *inode,
3545 struct btrfs_path *in_path)
Chris Mason39279cc2007-06-12 06:35:45 -04003546{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003547 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Filipe Manana4222ea72018-10-24 10:13:03 +01003548 struct btrfs_path *path = in_path;
Chris Mason5f39d392007-10-15 16:14:19 -04003549 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003550 struct btrfs_inode_item *inode_item;
3551 struct btrfs_root *root = BTRFS_I(inode)->root;
3552 struct btrfs_key location;
Miao Xie67de1172013-12-26 13:07:06 +08003553 unsigned long ptr;
Chris Mason46a53cc2009-04-27 11:47:50 -04003554 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04003555 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04003556 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003557 bool filled = false;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003558 int first_xattr_slot;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003559
3560 ret = btrfs_fill_inode(inode, &rdev);
3561 if (!ret)
3562 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04003563
Filipe Manana4222ea72018-10-24 10:13:03 +01003564 if (!path) {
3565 path = btrfs_alloc_path();
3566 if (!path)
3567 return -ENOMEM;
3568 }
Mark Fasheh1748f842011-07-12 11:25:31 -07003569
Chris Mason39279cc2007-06-12 06:35:45 -04003570 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05003571
Chris Mason39279cc2007-06-12 06:35:45 -04003572 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Filipe Manana67710892016-06-06 11:51:25 +01003573 if (ret) {
Filipe Manana4222ea72018-10-24 10:13:03 +01003574 if (path != in_path)
3575 btrfs_free_path(path);
Al Virof5b3a412018-07-29 23:04:51 +01003576 return ret;
Filipe Manana67710892016-06-06 11:51:25 +01003577 }
Chris Mason39279cc2007-06-12 06:35:45 -04003578
Chris Mason5f39d392007-10-15 16:14:19 -04003579 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00003580
3581 if (filled)
Miao Xie67de1172013-12-26 13:07:06 +08003582 goto cache_index;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003583
Chris Mason5f39d392007-10-15 16:14:19 -04003584 inode_item = btrfs_item_ptr(leaf, path->slots[0],
3585 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003586 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02003587 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08003588 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
3589 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02003590 btrfs_i_size_write(BTRFS_I(inode), btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04003591
David Sterbaa937b972014-12-12 17:39:12 +01003592 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->atime);
3593 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->atime);
Chris Mason5f39d392007-10-15 16:14:19 -04003594
David Sterbaa937b972014-12-12 17:39:12 +01003595 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->mtime);
3596 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->mtime);
Chris Mason5f39d392007-10-15 16:14:19 -04003597
David Sterbaa937b972014-12-12 17:39:12 +01003598 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->ctime);
3599 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->ctime);
Chris Mason5f39d392007-10-15 16:14:19 -04003600
chandan r9cc97d62012-07-04 12:48:07 +05303601 BTRFS_I(inode)->i_otime.tv_sec =
3602 btrfs_timespec_sec(leaf, &inode_item->otime);
3603 BTRFS_I(inode)->i_otime.tv_nsec =
3604 btrfs_timespec_nsec(leaf, &inode_item->otime);
Chris Mason5f39d392007-10-15 16:14:19 -04003605
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003606 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003607 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003608 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
3609
Jeff Laytonc7f88c42017-12-11 06:35:12 -05003610 inode_set_iversion_queried(inode,
3611 btrfs_inode_sequence(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003612 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04003613 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003614 rdev = btrfs_inode_rdev(leaf, inode_item);
3615
Josef Bacikaec74772008-07-24 12:12:38 -04003616 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05003617 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Miao Xie67de1172013-12-26 13:07:06 +08003618
3619cache_index:
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003620 /*
3621 * If we were modified in the current generation and evicted from memory
3622 * and then re-read we need to do a full sync since we don't have any
3623 * idea about which extents were modified before we were evicted from
3624 * cache.
3625 *
3626 * This is required for both inode re-read from disk and delayed inode
3627 * in delayed_nodes_tree.
3628 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003629 if (BTRFS_I(inode)->last_trans == fs_info->generation)
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003630 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3631 &BTRFS_I(inode)->runtime_flags);
3632
Filipe Mananabde6c242015-07-24 00:00:19 +01003633 /*
3634 * We don't persist the id of the transaction where an unlink operation
3635 * against the inode was last made. So here we assume the inode might
3636 * have been evicted, and therefore the exact value of last_unlink_trans
3637 * lost, and set it to last_trans to avoid metadata inconsistencies
3638 * between the inode and its parent if the inode is fsync'ed and the log
3639 * replayed. For example, in the scenario:
3640 *
3641 * touch mydir/foo
3642 * ln mydir/foo mydir/bar
3643 * sync
3644 * unlink mydir/bar
3645 * echo 2 > /proc/sys/vm/drop_caches # evicts inode
3646 * xfs_io -c fsync mydir/foo
3647 * <power failure>
3648 * mount fs, triggers fsync log replay
3649 *
3650 * We must make sure that when we fsync our inode foo we also log its
3651 * parent inode, otherwise after log replay the parent still has the
3652 * dentry with the "bar" name but our inode foo has a link count of 1
3653 * and doesn't have an inode ref with the name "bar" anymore.
3654 *
3655 * Setting last_unlink_trans to last_trans is a pessimistic approach,
Nicholas D Steeves01327612016-05-19 21:18:45 -04003656 * but it guarantees correctness at the expense of occasional full
Filipe Mananabde6c242015-07-24 00:00:19 +01003657 * transaction commits on fsync if our inode is a directory, or if our
3658 * inode is not a directory, logging its parent unnecessarily.
3659 */
3660 BTRFS_I(inode)->last_unlink_trans = BTRFS_I(inode)->last_trans;
Filipe Manana41bd6062018-11-28 14:54:28 +00003661 /*
3662 * Similar reasoning for last_link_trans, needs to be set otherwise
3663 * for a case like the following:
3664 *
3665 * mkdir A
3666 * touch foo
3667 * ln foo A/bar
3668 * echo 2 > /proc/sys/vm/drop_caches
3669 * fsync foo
3670 * <power failure>
3671 *
3672 * Would result in link bar and directory A not existing after the power
3673 * failure.
3674 */
3675 BTRFS_I(inode)->last_link_trans = BTRFS_I(inode)->last_trans;
Filipe Mananabde6c242015-07-24 00:00:19 +01003676
Miao Xie67de1172013-12-26 13:07:06 +08003677 path->slots[0]++;
3678 if (inode->i_nlink != 1 ||
3679 path->slots[0] >= btrfs_header_nritems(leaf))
3680 goto cache_acl;
3681
3682 btrfs_item_key_to_cpu(leaf, &location, path->slots[0]);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003683 if (location.objectid != btrfs_ino(BTRFS_I(inode)))
Miao Xie67de1172013-12-26 13:07:06 +08003684 goto cache_acl;
3685
3686 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3687 if (location.type == BTRFS_INODE_REF_KEY) {
3688 struct btrfs_inode_ref *ref;
3689
3690 ref = (struct btrfs_inode_ref *)ptr;
3691 BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref);
3692 } else if (location.type == BTRFS_INODE_EXTREF_KEY) {
3693 struct btrfs_inode_extref *extref;
3694
3695 extref = (struct btrfs_inode_extref *)ptr;
3696 BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf,
3697 extref);
3698 }
Miao Xie2f7e33d2011-06-23 07:27:13 +00003699cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04003700 /*
3701 * try to precache a NULL acl entry for files that don't have
3702 * any xattrs or acls
3703 */
Li Zefan33345d012011-04-20 10:31:50 +08003704 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
David Sterbaf85b7372017-01-20 14:54:07 +01003705 btrfs_ino(BTRFS_I(inode)), &first_xattr_slot);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003706 if (first_xattr_slot != -1) {
3707 path->slots[0] = first_xattr_slot;
3708 ret = btrfs_load_inode_props(inode, path);
3709 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003710 btrfs_err(fs_info,
David Sterba351fd352014-05-15 16:48:20 +02003711 "error loading props for ino %llu (root %llu): %d",
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003712 btrfs_ino(BTRFS_I(inode)),
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003713 root->root_key.objectid, ret);
3714 }
Filipe Manana4222ea72018-10-24 10:13:03 +01003715 if (path != in_path)
3716 btrfs_free_path(path);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003717
Al Viro72c04902009-06-24 16:58:48 -04003718 if (!maybe_acls)
3719 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04003720
Chris Mason39279cc2007-06-12 06:35:45 -04003721 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04003722 case S_IFREG:
3723 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masond1310b22008-01-24 16:13:08 -05003724 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04003725 inode->i_fop = &btrfs_file_operations;
3726 inode->i_op = &btrfs_file_inode_operations;
3727 break;
3728 case S_IFDIR:
3729 inode->i_fop = &btrfs_dir_file_operations;
Omar Sandoval67ade052017-01-25 17:06:38 -08003730 inode->i_op = &btrfs_dir_inode_operations;
Chris Mason39279cc2007-06-12 06:35:45 -04003731 break;
3732 case S_IFLNK:
3733 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -05003734 inode_nohighmem(inode);
Omar Sandoval4779cc02018-09-24 15:16:55 -07003735 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason39279cc2007-06-12 06:35:45 -04003736 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04003737 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05003738 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04003739 init_special_inode(inode, inode->i_mode, rdev);
3740 break;
Chris Mason39279cc2007-06-12 06:35:45 -04003741 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003742
David Sterba7b6a2212018-03-26 18:40:21 +02003743 btrfs_sync_inode_flags_to_i_flags(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003744 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003745}
3746
Chris Masond352ac62008-09-29 15:18:18 -04003747/*
3748 * given a leaf and an inode, copy the inode fields into the leaf
3749 */
Chris Masone02119d2008-09-05 16:13:11 -04003750static void fill_inode_item(struct btrfs_trans_handle *trans,
3751 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04003752 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04003753 struct inode *inode)
3754{
Liu Bo51fab692012-12-27 09:01:21 +00003755 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003756
Liu Bo51fab692012-12-27 09:01:21 +00003757 btrfs_init_map_token(&token);
Chris Mason5f39d392007-10-15 16:14:19 -04003758
Liu Bo51fab692012-12-27 09:01:21 +00003759 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
3760 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
3761 btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size,
3762 &token);
3763 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3764 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003765
David Sterbaa937b972014-12-12 17:39:12 +01003766 btrfs_set_token_timespec_sec(leaf, &item->atime,
Liu Bo51fab692012-12-27 09:01:21 +00003767 inode->i_atime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003768 btrfs_set_token_timespec_nsec(leaf, &item->atime,
Liu Bo51fab692012-12-27 09:01:21 +00003769 inode->i_atime.tv_nsec, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003770
David Sterbaa937b972014-12-12 17:39:12 +01003771 btrfs_set_token_timespec_sec(leaf, &item->mtime,
Liu Bo51fab692012-12-27 09:01:21 +00003772 inode->i_mtime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003773 btrfs_set_token_timespec_nsec(leaf, &item->mtime,
Liu Bo51fab692012-12-27 09:01:21 +00003774 inode->i_mtime.tv_nsec, &token);
3775
David Sterbaa937b972014-12-12 17:39:12 +01003776 btrfs_set_token_timespec_sec(leaf, &item->ctime,
Liu Bo51fab692012-12-27 09:01:21 +00003777 inode->i_ctime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003778 btrfs_set_token_timespec_nsec(leaf, &item->ctime,
Liu Bo51fab692012-12-27 09:01:21 +00003779 inode->i_ctime.tv_nsec, &token);
3780
chandan r9cc97d62012-07-04 12:48:07 +05303781 btrfs_set_token_timespec_sec(leaf, &item->otime,
3782 BTRFS_I(inode)->i_otime.tv_sec, &token);
3783 btrfs_set_token_timespec_nsec(leaf, &item->otime,
3784 BTRFS_I(inode)->i_otime.tv_nsec, &token);
3785
Liu Bo51fab692012-12-27 09:01:21 +00003786 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3787 &token);
3788 btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation,
3789 &token);
Jeff Laytonc7f88c42017-12-11 06:35:12 -05003790 btrfs_set_token_inode_sequence(leaf, item, inode_peek_iversion(inode),
3791 &token);
Liu Bo51fab692012-12-27 09:01:21 +00003792 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
3793 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3794 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3795 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
Chris Mason39279cc2007-06-12 06:35:45 -04003796}
3797
Chris Masond352ac62008-09-29 15:18:18 -04003798/*
3799 * copy everything in the in-memory inode into the btree.
3800 */
Chris Mason21151332011-11-10 20:39:08 -05003801static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Chris Masond3977122009-01-05 21:25:51 -05003802 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003803{
3804 struct btrfs_inode_item *inode_item;
3805 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003806 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003807 int ret;
3808
3809 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08003810 if (!path)
3811 return -ENOMEM;
3812
Chris Masonb9473432009-03-13 11:00:37 -04003813 path->leave_spinning = 1;
Miao Xie16cdcec2011-04-22 18:12:22 +08003814 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
3815 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003816 if (ret) {
3817 if (ret > 0)
3818 ret = -ENOENT;
3819 goto failed;
3820 }
3821
Chris Mason5f39d392007-10-15 16:14:19 -04003822 leaf = path->nodes[0];
3823 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08003824 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04003825
Chris Masone02119d2008-09-05 16:13:11 -04003826 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04003827 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04003828 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003829 ret = 0;
3830failed:
Chris Mason39279cc2007-06-12 06:35:45 -04003831 btrfs_free_path(path);
3832 return ret;
3833}
3834
Chris Masond352ac62008-09-29 15:18:18 -04003835/*
Chris Mason21151332011-11-10 20:39:08 -05003836 * copy everything in the in-memory inode into the btree.
3837 */
3838noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
3839 struct btrfs_root *root, struct inode *inode)
3840{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003841 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason21151332011-11-10 20:39:08 -05003842 int ret;
3843
3844 /*
3845 * If the inode is a free space inode, we can deadlock during commit
3846 * if we put it into the delayed code.
3847 *
3848 * The data relocation inode should also be directly updated
3849 * without delay
3850 */
Nikolay Borisov70ddc552017-02-20 13:50:35 +02003851 if (!btrfs_is_free_space_inode(BTRFS_I(inode))
Josef Bacik1d52c782014-09-18 11:30:44 -04003852 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003853 && !test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) {
Alexander Block8ea05e32012-07-25 17:35:53 +02003854 btrfs_update_root_times(trans, root);
3855
Chris Mason21151332011-11-10 20:39:08 -05003856 ret = btrfs_delayed_update_inode(trans, root, inode);
3857 if (!ret)
3858 btrfs_set_inode_last_trans(trans, inode);
3859 return ret;
3860 }
3861
3862 return btrfs_update_inode_item(trans, root, inode);
3863}
3864
Josef Bacikbe6aef62012-10-22 15:43:12 -04003865noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
3866 struct btrfs_root *root,
3867 struct inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05003868{
3869 int ret;
3870
3871 ret = btrfs_update_inode(trans, root, inode);
3872 if (ret == -ENOSPC)
3873 return btrfs_update_inode_item(trans, root, inode);
3874 return ret;
3875}
3876
3877/*
Chris Masond352ac62008-09-29 15:18:18 -04003878 * unlink helper that gets used here in inode.c and in the tree logging
3879 * recovery code. It remove a link in a directory with a given name, and
3880 * also drops the back refs in the inode to the directory
3881 */
Al Viro92986792011-03-04 17:14:37 +00003882static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3883 struct btrfs_root *root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003884 struct btrfs_inode *dir,
3885 struct btrfs_inode *inode,
Al Viro92986792011-03-04 17:14:37 +00003886 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04003887{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003888 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04003889 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04003890 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003891 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003892 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04003893 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04003894 u64 index;
Li Zefan33345d012011-04-20 10:31:50 +08003895 u64 ino = btrfs_ino(inode);
3896 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04003897
3898 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04003899 if (!path) {
3900 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00003901 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04003902 }
3903
Chris Masonb9473432009-03-13 11:00:37 -04003904 path->leave_spinning = 1;
Li Zefan33345d012011-04-20 10:31:50 +08003905 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04003906 name, name_len, -1);
Liu Bo3cf50682018-09-12 06:06:26 +08003907 if (IS_ERR_OR_NULL(di)) {
3908 ret = di ? PTR_ERR(di) : -ENOENT;
Chris Mason39279cc2007-06-12 06:35:45 -04003909 goto err;
3910 }
Chris Mason5f39d392007-10-15 16:14:19 -04003911 leaf = path->nodes[0];
3912 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04003913 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04003914 if (ret)
3915 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02003916 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003917
Miao Xie67de1172013-12-26 13:07:06 +08003918 /*
3919 * If we don't have dir index, we have to get it by looking up
3920 * the inode ref, since we get the inode ref, remove it directly,
3921 * it is unnecessary to do delayed deletion.
3922 *
3923 * But if we have dir index, needn't search inode ref to get it.
3924 * Since the inode ref is close to the inode item, it is better
3925 * that we delay to delete it, and just do this deletion when
3926 * we update the inode item.
3927 */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003928 if (inode->dir_index) {
Miao Xie67de1172013-12-26 13:07:06 +08003929 ret = btrfs_delayed_delete_inode_ref(inode);
3930 if (!ret) {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003931 index = inode->dir_index;
Miao Xie67de1172013-12-26 13:07:06 +08003932 goto skip_backref;
3933 }
3934 }
3935
Li Zefan33345d012011-04-20 10:31:50 +08003936 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
3937 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04003938 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003939 btrfs_info(fs_info,
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003940 "failed to delete reference to %.*s, inode %llu parent %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003941 name_len, name, ino, dir_ino);
Jeff Mahoney66642832016-06-10 18:19:25 -04003942 btrfs_abort_transaction(trans, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04003943 goto err;
3944 }
Miao Xie67de1172013-12-26 13:07:06 +08003945skip_backref:
Lu Fengqi9add2942018-08-01 11:32:26 +08003946 ret = btrfs_delete_delayed_dir_index(trans, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003947 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003948 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003949 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003950 }
Chris Mason39279cc2007-06-12 06:35:45 -04003951
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003952 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len, inode,
3953 dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003954 if (ret != 0 && ret != -ENOENT) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003955 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003956 goto err;
3957 }
Chris Masone02119d2008-09-05 16:13:11 -04003958
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003959 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len, dir,
3960 index);
Chris Mason6418c962010-10-30 07:34:24 -04003961 if (ret == -ENOENT)
3962 ret = 0;
Zach Brownd4e39912013-04-02 21:02:16 +00003963 else if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04003964 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003965err:
3966 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003967 if (ret)
3968 goto out;
3969
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02003970 btrfs_i_size_write(dir, dir->vfs_inode.i_size - name_len * 2);
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003971 inode_inc_iversion(&inode->vfs_inode);
3972 inode_inc_iversion(&dir->vfs_inode);
3973 inode->vfs_inode.i_ctime = dir->vfs_inode.i_mtime =
3974 dir->vfs_inode.i_ctime = current_time(&inode->vfs_inode);
3975 ret = btrfs_update_inode(trans, root, &dir->vfs_inode);
Chris Masone02119d2008-09-05 16:13:11 -04003976out:
Chris Mason39279cc2007-06-12 06:35:45 -04003977 return ret;
3978}
3979
Al Viro92986792011-03-04 17:14:37 +00003980int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3981 struct btrfs_root *root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003982 struct btrfs_inode *dir, struct btrfs_inode *inode,
Al Viro92986792011-03-04 17:14:37 +00003983 const char *name, int name_len)
3984{
3985 int ret;
3986 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
3987 if (!ret) {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003988 drop_nlink(&inode->vfs_inode);
3989 ret = btrfs_update_inode(trans, root, &inode->vfs_inode);
Al Viro92986792011-03-04 17:14:37 +00003990 }
3991 return ret;
3992}
Yan, Zhenga22285a2010-05-16 10:48:46 -04003993
3994/*
3995 * helper to start transaction for unlink and rmdir.
3996 *
Josef Bacikd52be812013-05-29 14:54:47 -04003997 * unlink and rmdir are special in btrfs, they do not always free space, so
3998 * if we cannot make our reservations the normal way try and see if there is
3999 * plenty of slack room in the global reserve to migrate, otherwise we cannot
4000 * allow the unlink to occur.
Yan, Zhenga22285a2010-05-16 10:48:46 -04004001 */
Josef Bacikd52be812013-05-29 14:54:47 -04004002static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir)
Yan, Zhenga22285a2010-05-16 10:48:46 -04004003{
Yan, Zhenga22285a2010-05-16 10:48:46 -04004004 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004005
Josef Bacike70bea52011-10-11 14:18:24 -04004006 /*
4007 * 1 for the possible orphan item
4008 * 1 for the dir item
4009 * 1 for the dir index
4010 * 1 for the inode ref
Josef Bacike70bea52011-10-11 14:18:24 -04004011 * 1 for the inode
4012 */
Filipe Manana8eab77f2015-11-13 23:57:16 +00004013 return btrfs_start_transaction_fallback_global_rsv(root, 5, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004014}
4015
Chris Mason39279cc2007-06-12 06:35:45 -04004016static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
4017{
Yan, Zhenga22285a2010-05-16 10:48:46 -04004018 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004019 struct btrfs_trans_handle *trans;
David Howells2b0143b2015-03-17 22:25:59 +00004020 struct inode *inode = d_inode(dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04004021 int ret;
4022
Josef Bacikd52be812013-05-29 14:54:47 -04004023 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004024 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004025 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04004026
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004027 btrfs_record_unlink_dir(trans, BTRFS_I(dir), BTRFS_I(d_inode(dentry)),
4028 0);
Chris Mason12fcfd22009-03-24 10:24:20 -04004029
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004030 ret = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
4031 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
4032 dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00004033 if (ret)
4034 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004035
Yan, Zhenga22285a2010-05-16 10:48:46 -04004036 if (inode->i_nlink == 0) {
Nikolay Borisov73f2e542017-02-20 13:50:59 +02004037 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Tsutomu Itohb5324022011-07-19 07:27:20 +00004038 if (ret)
4039 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004040 }
Josef Bacik7b128762008-07-24 12:17:14 -04004041
Tsutomu Itohb5324022011-07-19 07:27:20 +00004042out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004043 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004044 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04004045 return ret;
4046}
4047
Misono Tomohirof60a2362018-04-18 11:34:52 +09004048static int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
Lu Fengqi401b3b12018-08-01 11:32:30 +08004049 struct inode *dir, u64 objectid,
4050 const char *name, int name_len)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004051{
Lu Fengqi401b3b12018-08-01 11:32:30 +08004052 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004053 struct btrfs_path *path;
4054 struct extent_buffer *leaf;
4055 struct btrfs_dir_item *di;
4056 struct btrfs_key key;
4057 u64 index;
4058 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004059 u64 dir_ino = btrfs_ino(BTRFS_I(dir));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004060
4061 path = btrfs_alloc_path();
4062 if (!path)
4063 return -ENOMEM;
4064
Li Zefan33345d012011-04-20 10:31:50 +08004065 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004066 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004067 if (IS_ERR_OR_NULL(di)) {
Liu Bo3cf50682018-09-12 06:06:26 +08004068 ret = di ? PTR_ERR(di) : -ENOENT;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004069 goto out;
4070 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004071
4072 leaf = path->nodes[0];
4073 btrfs_dir_item_key_to_cpu(leaf, di, &key);
4074 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
4075 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004076 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004077 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004078 goto out;
4079 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004080 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004081
Lu Fengqi3ee1c552018-08-01 11:32:28 +08004082 ret = btrfs_del_root_ref(trans, objectid, root->root_key.objectid,
4083 dir_ino, &index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004084 if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004085 if (ret != -ENOENT) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004086 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004087 goto out;
4088 }
Li Zefan33345d012011-04-20 10:31:50 +08004089 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004090 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004091 if (IS_ERR_OR_NULL(di)) {
4092 if (!di)
4093 ret = -ENOENT;
4094 else
4095 ret = PTR_ERR(di);
Jeff Mahoney66642832016-06-10 18:19:25 -04004096 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004097 goto out;
4098 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004099
4100 leaf = path->nodes[0];
4101 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004102 index = key.offset;
4103 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004104 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004105
Lu Fengqi9add2942018-08-01 11:32:26 +08004106 ret = btrfs_delete_delayed_dir_index(trans, BTRFS_I(dir), index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004107 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004108 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004109 goto out;
4110 }
Miao Xie16cdcec2011-04-22 18:12:22 +08004111
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004112 btrfs_i_size_write(BTRFS_I(dir), dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004113 inode_inc_iversion(dir);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07004114 dir->i_mtime = dir->i_ctime = current_time(dir);
Josef Bacik5a24e842012-08-08 10:12:59 -06004115 ret = btrfs_update_inode_fallback(trans, root, dir);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004116 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004117 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004118out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04004119 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004120 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004121}
4122
Misono Tomohiroec42f162018-04-18 11:34:13 +09004123/*
4124 * Helper to check if the subvolume references other subvolumes or if it's
4125 * default.
4126 */
Misono Tomohirof60a2362018-04-18 11:34:52 +09004127static noinline int may_destroy_subvol(struct btrfs_root *root)
Misono Tomohiroec42f162018-04-18 11:34:13 +09004128{
4129 struct btrfs_fs_info *fs_info = root->fs_info;
4130 struct btrfs_path *path;
4131 struct btrfs_dir_item *di;
4132 struct btrfs_key key;
4133 u64 dir_id;
4134 int ret;
4135
4136 path = btrfs_alloc_path();
4137 if (!path)
4138 return -ENOMEM;
4139
4140 /* Make sure this root isn't set as the default subvol */
4141 dir_id = btrfs_super_root_dir(fs_info->super_copy);
4142 di = btrfs_lookup_dir_item(NULL, fs_info->tree_root, path,
4143 dir_id, "default", 7, 0);
4144 if (di && !IS_ERR(di)) {
4145 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key);
4146 if (key.objectid == root->root_key.objectid) {
4147 ret = -EPERM;
4148 btrfs_err(fs_info,
4149 "deleting default subvolume %llu is not allowed",
4150 key.objectid);
4151 goto out;
4152 }
4153 btrfs_release_path(path);
4154 }
4155
4156 key.objectid = root->root_key.objectid;
4157 key.type = BTRFS_ROOT_REF_KEY;
4158 key.offset = (u64)-1;
4159
4160 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
4161 if (ret < 0)
4162 goto out;
4163 BUG_ON(ret == 0);
4164
4165 ret = 0;
4166 if (path->slots[0] > 0) {
4167 path->slots[0]--;
4168 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
4169 if (key.objectid == root->root_key.objectid &&
4170 key.type == BTRFS_ROOT_REF_KEY)
4171 ret = -ENOTEMPTY;
4172 }
4173out:
4174 btrfs_free_path(path);
4175 return ret;
4176}
4177
Nikolay Borisov20a68002018-04-27 14:36:24 +03004178/* Delete all dentries for inodes belonging to the root */
4179static void btrfs_prune_dentries(struct btrfs_root *root)
4180{
4181 struct btrfs_fs_info *fs_info = root->fs_info;
4182 struct rb_node *node;
4183 struct rb_node *prev;
4184 struct btrfs_inode *entry;
4185 struct inode *inode;
4186 u64 objectid = 0;
4187
4188 if (!test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
4189 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
4190
4191 spin_lock(&root->inode_lock);
4192again:
4193 node = root->inode_tree.rb_node;
4194 prev = NULL;
4195 while (node) {
4196 prev = node;
4197 entry = rb_entry(node, struct btrfs_inode, rb_node);
4198
David Sterba37508512018-06-29 10:56:40 +02004199 if (objectid < btrfs_ino(entry))
Nikolay Borisov20a68002018-04-27 14:36:24 +03004200 node = node->rb_left;
David Sterba37508512018-06-29 10:56:40 +02004201 else if (objectid > btrfs_ino(entry))
Nikolay Borisov20a68002018-04-27 14:36:24 +03004202 node = node->rb_right;
4203 else
4204 break;
4205 }
4206 if (!node) {
4207 while (prev) {
4208 entry = rb_entry(prev, struct btrfs_inode, rb_node);
David Sterba37508512018-06-29 10:56:40 +02004209 if (objectid <= btrfs_ino(entry)) {
Nikolay Borisov20a68002018-04-27 14:36:24 +03004210 node = prev;
4211 break;
4212 }
4213 prev = rb_next(prev);
4214 }
4215 }
4216 while (node) {
4217 entry = rb_entry(node, struct btrfs_inode, rb_node);
David Sterba37508512018-06-29 10:56:40 +02004218 objectid = btrfs_ino(entry) + 1;
Nikolay Borisov20a68002018-04-27 14:36:24 +03004219 inode = igrab(&entry->vfs_inode);
4220 if (inode) {
4221 spin_unlock(&root->inode_lock);
4222 if (atomic_read(&inode->i_count) > 1)
4223 d_prune_aliases(inode);
4224 /*
4225 * btrfs_drop_inode will have it removed from the inode
4226 * cache when its usage count hits zero.
4227 */
4228 iput(inode);
4229 cond_resched();
4230 spin_lock(&root->inode_lock);
4231 goto again;
4232 }
4233
4234 if (cond_resched_lock(&root->inode_lock))
4235 goto again;
4236
4237 node = rb_next(node);
4238 }
4239 spin_unlock(&root->inode_lock);
4240}
4241
Misono Tomohirof60a2362018-04-18 11:34:52 +09004242int btrfs_delete_subvolume(struct inode *dir, struct dentry *dentry)
4243{
4244 struct btrfs_fs_info *fs_info = btrfs_sb(dentry->d_sb);
4245 struct btrfs_root *root = BTRFS_I(dir)->root;
4246 struct inode *inode = d_inode(dentry);
4247 struct btrfs_root *dest = BTRFS_I(inode)->root;
4248 struct btrfs_trans_handle *trans;
4249 struct btrfs_block_rsv block_rsv;
4250 u64 root_flags;
Misono Tomohirof60a2362018-04-18 11:34:52 +09004251 int ret;
4252 int err;
4253
4254 /*
4255 * Don't allow to delete a subvolume with send in progress. This is
4256 * inside the inode lock so the error handling that has to drop the bit
4257 * again is not run concurrently.
4258 */
4259 spin_lock(&dest->root_item_lock);
Lu Fengqia7176f72018-08-04 21:10:53 +08004260 if (dest->send_in_progress) {
Misono Tomohirof60a2362018-04-18 11:34:52 +09004261 spin_unlock(&dest->root_item_lock);
4262 btrfs_warn(fs_info,
4263 "attempt to delete subvolume %llu during send",
4264 dest->root_key.objectid);
4265 return -EPERM;
4266 }
Lu Fengqia7176f72018-08-04 21:10:53 +08004267 root_flags = btrfs_root_flags(&dest->root_item);
4268 btrfs_set_root_flags(&dest->root_item,
4269 root_flags | BTRFS_ROOT_SUBVOL_DEAD);
4270 spin_unlock(&dest->root_item_lock);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004271
4272 down_write(&fs_info->subvol_sem);
4273
4274 err = may_destroy_subvol(dest);
4275 if (err)
4276 goto out_up_write;
4277
4278 btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP);
4279 /*
4280 * One for dir inode,
4281 * two for dir entries,
4282 * two for root ref/backref.
4283 */
Gu JinXiangc4c129d2018-05-30 11:00:38 +08004284 err = btrfs_subvolume_reserve_metadata(root, &block_rsv, 5, true);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004285 if (err)
4286 goto out_up_write;
4287
4288 trans = btrfs_start_transaction(root, 0);
4289 if (IS_ERR(trans)) {
4290 err = PTR_ERR(trans);
4291 goto out_release;
4292 }
4293 trans->block_rsv = &block_rsv;
4294 trans->bytes_reserved = block_rsv.size;
4295
4296 btrfs_record_snapshot_destroy(trans, BTRFS_I(dir));
4297
Lu Fengqi401b3b12018-08-01 11:32:30 +08004298 ret = btrfs_unlink_subvol(trans, dir, dest->root_key.objectid,
4299 dentry->d_name.name, dentry->d_name.len);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004300 if (ret) {
4301 err = ret;
4302 btrfs_abort_transaction(trans, ret);
4303 goto out_end_trans;
4304 }
4305
4306 btrfs_record_root_in_trans(trans, dest);
4307
4308 memset(&dest->root_item.drop_progress, 0,
4309 sizeof(dest->root_item.drop_progress));
4310 dest->root_item.drop_level = 0;
4311 btrfs_set_root_refs(&dest->root_item, 0);
4312
4313 if (!test_and_set_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &dest->state)) {
4314 ret = btrfs_insert_orphan_item(trans,
4315 fs_info->tree_root,
4316 dest->root_key.objectid);
4317 if (ret) {
4318 btrfs_abort_transaction(trans, ret);
4319 err = ret;
4320 goto out_end_trans;
4321 }
4322 }
4323
Lu Fengqid1957792018-05-29 15:01:54 +08004324 ret = btrfs_uuid_tree_remove(trans, dest->root_item.uuid,
Misono Tomohirof60a2362018-04-18 11:34:52 +09004325 BTRFS_UUID_KEY_SUBVOL,
4326 dest->root_key.objectid);
4327 if (ret && ret != -ENOENT) {
4328 btrfs_abort_transaction(trans, ret);
4329 err = ret;
4330 goto out_end_trans;
4331 }
4332 if (!btrfs_is_empty_uuid(dest->root_item.received_uuid)) {
Lu Fengqid1957792018-05-29 15:01:54 +08004333 ret = btrfs_uuid_tree_remove(trans,
Misono Tomohirof60a2362018-04-18 11:34:52 +09004334 dest->root_item.received_uuid,
4335 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
4336 dest->root_key.objectid);
4337 if (ret && ret != -ENOENT) {
4338 btrfs_abort_transaction(trans, ret);
4339 err = ret;
4340 goto out_end_trans;
4341 }
4342 }
4343
4344out_end_trans:
4345 trans->block_rsv = NULL;
4346 trans->bytes_reserved = 0;
4347 ret = btrfs_end_transaction(trans);
4348 if (ret && !err)
4349 err = ret;
4350 inode->i_flags |= S_DEAD;
4351out_release:
4352 btrfs_subvolume_release_metadata(fs_info, &block_rsv);
4353out_up_write:
4354 up_write(&fs_info->subvol_sem);
4355 if (err) {
4356 spin_lock(&dest->root_item_lock);
4357 root_flags = btrfs_root_flags(&dest->root_item);
4358 btrfs_set_root_flags(&dest->root_item,
4359 root_flags & ~BTRFS_ROOT_SUBVOL_DEAD);
4360 spin_unlock(&dest->root_item_lock);
4361 } else {
4362 d_invalidate(dentry);
Nikolay Borisov20a68002018-04-27 14:36:24 +03004363 btrfs_prune_dentries(dest);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004364 ASSERT(dest->send_in_progress == 0);
4365
4366 /* the last ref */
4367 if (dest->ino_cache_inode) {
4368 iput(dest->ino_cache_inode);
4369 dest->ino_cache_inode = NULL;
4370 }
4371 }
4372
4373 return err;
4374}
4375
Chris Mason39279cc2007-06-12 06:35:45 -04004376static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
4377{
David Howells2b0143b2015-03-17 22:25:59 +00004378 struct inode *inode = d_inode(dentry);
Chris Mason1832a6d2007-12-21 16:27:21 -05004379 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004380 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004381 struct btrfs_trans_handle *trans;
Filipe Manana44f714d2016-06-06 16:11:13 +01004382 u64 last_unlink_trans;
Chris Mason39279cc2007-06-12 06:35:45 -04004383
David Sterbab3ae2442012-09-13 16:04:34 -06004384 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Yan134d4512007-10-25 15:49:25 -04004385 return -ENOTEMPTY;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004386 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_FIRST_FREE_OBJECTID)
Misono Tomohiroa79a4642018-04-18 11:35:31 +09004387 return btrfs_delete_subvolume(dir, dentry);
Yan134d4512007-10-25 15:49:25 -04004388
Josef Bacikd52be812013-05-29 14:54:47 -04004389 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004390 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004391 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004392
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004393 if (unlikely(btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Lu Fengqi401b3b12018-08-01 11:32:30 +08004394 err = btrfs_unlink_subvol(trans, dir,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004395 BTRFS_I(inode)->location.objectid,
4396 dentry->d_name.name,
4397 dentry->d_name.len);
4398 goto out;
4399 }
4400
Nikolay Borisov73f2e542017-02-20 13:50:59 +02004401 err = btrfs_orphan_add(trans, BTRFS_I(inode));
Josef Bacik7b128762008-07-24 12:17:14 -04004402 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004403 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004404
Filipe Manana44f714d2016-06-06 16:11:13 +01004405 last_unlink_trans = BTRFS_I(inode)->last_unlink_trans;
4406
Chris Mason39279cc2007-06-12 06:35:45 -04004407 /* now the directory is empty */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004408 err = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
4409 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
4410 dentry->d_name.len);
Filipe Manana44f714d2016-06-06 16:11:13 +01004411 if (!err) {
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004412 btrfs_i_size_write(BTRFS_I(inode), 0);
Filipe Manana44f714d2016-06-06 16:11:13 +01004413 /*
4414 * Propagate the last_unlink_trans value of the deleted dir to
4415 * its parent directory. This is to prevent an unrecoverable
4416 * log tree in the case we do something like this:
4417 * 1) create dir foo
4418 * 2) create snapshot under dir foo
4419 * 3) delete the snapshot
4420 * 4) rmdir foo
4421 * 5) mkdir foo
4422 * 6) fsync foo or some file inside foo
4423 */
4424 if (last_unlink_trans >= trans->transid)
4425 BTRFS_I(dir)->last_unlink_trans = last_unlink_trans;
4426 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004427out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004428 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004429 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39544012007-12-12 14:38:19 -05004430
Chris Mason39279cc2007-06-12 06:35:45 -04004431 return err;
4432}
4433
Chris Mason28f75a02015-02-04 06:59:29 -08004434static int truncate_space_check(struct btrfs_trans_handle *trans,
4435 struct btrfs_root *root,
4436 u64 bytes_deleted)
4437{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004438 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason28f75a02015-02-04 06:59:29 -08004439 int ret;
4440
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004441 /*
4442 * This is only used to apply pressure to the enospc system, we don't
4443 * intend to use this reservation at all.
4444 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004445 bytes_deleted = btrfs_csum_bytes_to_leaves(fs_info, bytes_deleted);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004446 bytes_deleted *= fs_info->nodesize;
4447 ret = btrfs_block_rsv_add(root, &fs_info->trans_block_rsv,
Chris Mason28f75a02015-02-04 06:59:29 -08004448 bytes_deleted, BTRFS_RESERVE_NO_FLUSH);
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004449 if (!ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004450 trace_btrfs_space_reservation(fs_info, "transaction",
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004451 trans->transid,
4452 bytes_deleted, 1);
Chris Mason28f75a02015-02-04 06:59:29 -08004453 trans->bytes_reserved += bytes_deleted;
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004454 }
Chris Mason28f75a02015-02-04 06:59:29 -08004455 return ret;
4456
4457}
4458
Josef Bacikddfae632017-10-19 14:16:02 -04004459/*
4460 * Return this if we need to call truncate_block for the last bit of the
4461 * truncate.
4462 */
4463#define NEED_TRUNCATE_BLOCK 1
Filipe Manana0305cd52015-10-16 12:34:25 +01004464
Chris Mason323ac952008-10-01 19:05:46 -04004465/*
Chris Mason39279cc2007-06-12 06:35:45 -04004466 * this can truncate away extent items, csum items and directory items.
4467 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04004468 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04004469 *
4470 * csum items that cross the new i_size are truncated to the new size
4471 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04004472 *
4473 * min_type is the minimum key type to truncate down to. If set to 0, this
4474 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04004475 */
Yan, Zheng80825102009-11-12 09:35:36 +00004476int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
4477 struct btrfs_root *root,
4478 struct inode *inode,
4479 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004480{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004481 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04004482 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004483 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004484 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00004485 struct btrfs_key key;
4486 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04004487 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04004488 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004489 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004490 u64 item_end = 0;
Filipe Mananac1aa4572015-06-20 18:20:09 +01004491 u64 last_size = new_size;
Yan, Zheng80825102009-11-12 09:35:36 +00004492 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04004493 int found_extent;
4494 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05004495 int pending_del_nr = 0;
4496 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04004497 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00004498 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004499 u64 ino = btrfs_ino(BTRFS_I(inode));
Chris Mason28ed1342014-12-17 09:41:04 -08004500 u64 bytes_deleted = 0;
Thomas Meyer897ca812017-10-07 16:02:21 +02004501 bool be_nice = false;
4502 bool should_throttle = false;
4503 bool should_end = false;
Yan, Zheng80825102009-11-12 09:35:36 +00004504
4505 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04004506
Chris Mason28ed1342014-12-17 09:41:04 -08004507 /*
4508 * for non-free space inodes and ref cows, we want to back off from
4509 * time to time
4510 */
Nikolay Borisov70ddc552017-02-20 13:50:35 +02004511 if (!btrfs_is_free_space_inode(BTRFS_I(inode)) &&
Chris Mason28ed1342014-12-17 09:41:04 -08004512 test_bit(BTRFS_ROOT_REF_COWS, &root->state))
Thomas Meyer897ca812017-10-07 16:02:21 +02004513 be_nice = true;
Chris Mason28ed1342014-12-17 09:41:04 -08004514
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004515 path = btrfs_alloc_path();
4516 if (!path)
4517 return -ENOMEM;
David Sterbae4058b52015-11-27 16:31:35 +01004518 path->reada = READA_BACK;
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004519
Josef Bacik5dc562c2012-08-17 13:14:17 -04004520 /*
4521 * We want to drop from the next block forward in case this new size is
4522 * not block aligned since we will be keeping the last block of the
4523 * extent just the way it is.
4524 */
Miao Xie27cdeb72014-04-02 19:51:05 +08004525 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004526 root == fs_info->tree_root)
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02004527 btrfs_drop_extent_cache(BTRFS_I(inode), ALIGN(new_size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004528 fs_info->sectorsize),
Jeff Mahoneyda170662016-06-15 09:22:56 -04004529 (u64)-1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00004530
Miao Xie16cdcec2011-04-22 18:12:22 +08004531 /*
4532 * This function is also used to drop the items in the log tree before
4533 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
4534 * it is used to drop the loged items. So we shouldn't kill the delayed
4535 * items.
4536 */
4537 if (min_type == 0 && root == BTRFS_I(inode)->root)
Nikolay Borisov4ccb5c72017-01-10 20:35:38 +02004538 btrfs_kill_delayed_inode_items(BTRFS_I(inode));
Miao Xie16cdcec2011-04-22 18:12:22 +08004539
Li Zefan33345d012011-04-20 10:31:50 +08004540 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04004541 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04004542 key.type = (u8)-1;
4543
Chris Mason85e21ba2008-01-29 15:11:36 -05004544search_again:
Chris Mason28ed1342014-12-17 09:41:04 -08004545 /*
4546 * with a 16K leaf size and 128MB extents, you can actually queue
4547 * up a huge file in a single leaf. Most of the time that
4548 * bytes_deleted is > 0, it will be huge by the time we get here
4549 */
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004550 if (be_nice && bytes_deleted > SZ_32M &&
4551 btrfs_should_end_transaction(trans)) {
4552 ret = -EAGAIN;
Yan, Zheng80825102009-11-12 09:35:36 +00004553 goto out;
4554 }
Chris Masond3977122009-01-05 21:25:51 -05004555
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004556 path->leave_spinning = 1;
4557 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
4558 if (ret < 0)
4559 goto out;
4560
Chris Mason85e21ba2008-01-29 15:11:36 -05004561 if (ret > 0) {
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004562 ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -04004563 /* there are no items in the tree for us to truncate, we're
4564 * done
4565 */
Yan, Zheng80825102009-11-12 09:35:36 +00004566 if (path->slots[0] == 0)
4567 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05004568 path->slots[0]--;
4569 }
4570
Chris Masond3977122009-01-05 21:25:51 -05004571 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04004572 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04004573 leaf = path->nodes[0];
4574 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02004575 found_type = found_key.type;
Chris Mason39279cc2007-06-12 06:35:45 -04004576
Li Zefan33345d012011-04-20 10:31:50 +08004577 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04004578 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004579
Chris Mason85e21ba2008-01-29 15:11:36 -05004580 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004581 break;
4582
Chris Mason5f39d392007-10-15 16:14:19 -04004583 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04004584 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04004585 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04004586 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04004587 extent_type = btrfs_file_extent_type(leaf, fi);
4588 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04004589 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04004590 btrfs_file_extent_num_bytes(leaf, fi);
Liu Bo09ed2f12017-03-10 11:09:48 -08004591
4592 trace_btrfs_truncate_show_fi_regular(
4593 BTRFS_I(inode), leaf, fi,
4594 found_key.offset);
Chris Mason179e29e2007-11-01 11:28:41 -04004595 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Qu Wenruoe41ca582018-06-06 15:41:49 +08004596 item_end += btrfs_file_extent_ram_bytes(leaf,
4597 fi);
Liu Bo09ed2f12017-03-10 11:09:48 -08004598
4599 trace_btrfs_truncate_show_fi_inline(
4600 BTRFS_I(inode), leaf, fi, path->slots[0],
4601 found_key.offset);
Chris Mason39279cc2007-06-12 06:35:45 -04004602 }
Yan008630c2007-11-07 13:31:09 -05004603 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04004604 }
Yan, Zheng80825102009-11-12 09:35:36 +00004605 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04004606 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00004607 } else {
Filipe Manana76b42ab2017-02-14 16:56:01 +00004608 if (item_end < new_size)
Yan, Zheng80825102009-11-12 09:35:36 +00004609 break;
4610 if (found_key.offset >= new_size)
4611 del_item = 1;
4612 else
4613 del_item = 0;
4614 }
Chris Mason39279cc2007-06-12 06:35:45 -04004615 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004616 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04004617 if (found_type != BTRFS_EXTENT_DATA_KEY)
4618 goto delete;
4619
4620 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04004621 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04004622 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05004623 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04004624 u64 orig_num_bytes =
4625 btrfs_file_extent_num_bytes(leaf, fi);
Qu Wenruofda28322013-02-26 08:10:22 +00004626 extent_num_bytes = ALIGN(new_size -
4627 found_key.offset,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004628 fs_info->sectorsize);
Chris Masondb945352007-10-15 16:15:53 -04004629 btrfs_set_file_extent_num_bytes(leaf, fi,
4630 extent_num_bytes);
4631 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05004632 extent_num_bytes);
Miao Xie27cdeb72014-04-02 19:51:05 +08004633 if (test_bit(BTRFS_ROOT_REF_COWS,
4634 &root->state) &&
4635 extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004636 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04004637 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004638 } else {
Chris Masondb945352007-10-15 16:15:53 -04004639 extent_num_bytes =
4640 btrfs_file_extent_disk_num_bytes(leaf,
4641 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004642 extent_offset = found_key.offset -
4643 btrfs_file_extent_offset(leaf, fi);
4644
Chris Mason39279cc2007-06-12 06:35:45 -04004645 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05004646 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004647 if (extent_start != 0) {
4648 found_extent = 1;
Miao Xie27cdeb72014-04-02 19:51:05 +08004649 if (test_bit(BTRFS_ROOT_REF_COWS,
4650 &root->state))
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004651 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04004652 }
4653 }
Chris Mason90692182008-02-08 13:49:28 -05004654 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04004655 /*
4656 * we can't truncate inline items that have had
4657 * special encodings
4658 */
4659 if (!del_item &&
Chris Masonc8b97812008-10-29 14:49:59 -04004660 btrfs_file_extent_encryption(leaf, fi) == 0 &&
Josef Bacikddfae632017-10-19 14:16:02 -04004661 btrfs_file_extent_other_encoding(leaf, fi) == 0 &&
4662 btrfs_file_extent_compression(leaf, fi) == 0) {
4663 u32 size = (u32)(new_size - found_key.offset);
Chris Mason514ac8a2014-01-03 21:07:00 -08004664
Josef Bacikddfae632017-10-19 14:16:02 -04004665 btrfs_set_file_extent_ram_bytes(leaf, fi, size);
4666 size = btrfs_file_extent_calc_inline_size(size);
4667 btrfs_truncate_item(root->fs_info, path, size, 1);
4668 } else if (!del_item) {
Chris Mason514ac8a2014-01-03 21:07:00 -08004669 /*
Josef Bacikddfae632017-10-19 14:16:02 -04004670 * We have to bail so the last_size is set to
4671 * just before this extent.
Chris Mason514ac8a2014-01-03 21:07:00 -08004672 */
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004673 ret = NEED_TRUNCATE_BLOCK;
Josef Bacikddfae632017-10-19 14:16:02 -04004674 break;
Chris Mason90692182008-02-08 13:49:28 -05004675 }
Josef Bacikddfae632017-10-19 14:16:02 -04004676
4677 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state))
4678 inode_sub_bytes(inode, item_end + 1 - new_size);
Chris Mason39279cc2007-06-12 06:35:45 -04004679 }
Chris Mason179e29e2007-11-01 11:28:41 -04004680delete:
Josef Bacikddfae632017-10-19 14:16:02 -04004681 if (del_item)
4682 last_size = found_key.offset;
4683 else
4684 last_size = new_size;
Chris Mason39279cc2007-06-12 06:35:45 -04004685 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05004686 if (!pending_del_nr) {
4687 /* no pending yet, add ourselves */
4688 pending_del_slot = path->slots[0];
4689 pending_del_nr = 1;
4690 } else if (pending_del_nr &&
4691 path->slots[0] + 1 == pending_del_slot) {
4692 /* hop on the pending chunk */
4693 pending_del_nr++;
4694 pending_del_slot = path->slots[0];
4695 } else {
Chris Masond3977122009-01-05 21:25:51 -05004696 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05004697 }
Chris Mason39279cc2007-06-12 06:35:45 -04004698 } else {
4699 break;
4700 }
Thomas Meyer897ca812017-10-07 16:02:21 +02004701 should_throttle = false;
Chris Mason28f75a02015-02-04 06:59:29 -08004702
Miao Xie27cdeb72014-04-02 19:51:05 +08004703 if (found_extent &&
4704 (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004705 root == fs_info->tree_root)) {
Chris Masonb9473432009-03-13 11:00:37 -04004706 btrfs_set_path_blocking(path);
Chris Mason28ed1342014-12-17 09:41:04 -08004707 bytes_deleted += extent_num_bytes;
Josef Bacik84f7d8e2017-09-29 15:43:49 -04004708 ret = btrfs_free_extent(trans, root, extent_start,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004709 extent_num_bytes, 0,
4710 btrfs_header_owner(leaf),
Filipe Mananab06c4bf2015-10-23 07:52:54 +01004711 ino, extent_offset);
Omar Sandoval05522102018-05-11 13:13:31 -07004712 if (ret) {
4713 btrfs_abort_transaction(trans, ret);
4714 break;
4715 }
Lu Fengqi7c861622018-10-11 13:40:36 +08004716 if (btrfs_should_throttle_delayed_refs(trans))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004717 btrfs_async_run_delayed_refs(fs_info,
Wang Xiaoguangdd4b8572016-10-18 15:56:13 +08004718 trans->delayed_ref_updates * 2,
4719 trans->transid, 0);
Chris Mason28f75a02015-02-04 06:59:29 -08004720 if (be_nice) {
4721 if (truncate_space_check(trans, root,
4722 extent_num_bytes)) {
Thomas Meyer897ca812017-10-07 16:02:21 +02004723 should_end = true;
Chris Mason28f75a02015-02-04 06:59:29 -08004724 }
Lu Fengqi7c861622018-10-11 13:40:36 +08004725 if (btrfs_should_throttle_delayed_refs(trans))
Thomas Meyer897ca812017-10-07 16:02:21 +02004726 should_throttle = true;
Chris Mason28f75a02015-02-04 06:59:29 -08004727 }
Chris Mason39279cc2007-06-12 06:35:45 -04004728 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004729
Yan, Zheng80825102009-11-12 09:35:36 +00004730 if (found_type == BTRFS_INODE_ITEM_KEY)
4731 break;
4732
4733 if (path->slots[0] == 0 ||
Josef Bacik12621332015-02-03 07:50:16 -08004734 path->slots[0] != pending_del_slot ||
Chris Mason28f75a02015-02-04 06:59:29 -08004735 should_throttle || should_end) {
Yan, Zheng80825102009-11-12 09:35:36 +00004736 if (pending_del_nr) {
4737 ret = btrfs_del_items(trans, root, path,
4738 pending_del_slot,
4739 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004740 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004741 btrfs_abort_transaction(trans, ret);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004742 break;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004743 }
Yan, Zheng80825102009-11-12 09:35:36 +00004744 pending_del_nr = 0;
4745 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004746 btrfs_release_path(path);
Chris Mason28f75a02015-02-04 06:59:29 -08004747 if (should_throttle) {
Josef Bacik12621332015-02-03 07:50:16 -08004748 unsigned long updates = trans->delayed_ref_updates;
4749 if (updates) {
4750 trans->delayed_ref_updates = 0;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004751 ret = btrfs_run_delayed_refs(trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004752 updates * 2);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004753 if (ret)
4754 break;
Josef Bacik12621332015-02-03 07:50:16 -08004755 }
4756 }
Chris Mason28f75a02015-02-04 06:59:29 -08004757 /*
4758 * if we failed to refill our space rsv, bail out
4759 * and let the transaction restart
4760 */
4761 if (should_end) {
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004762 ret = -EAGAIN;
4763 break;
Chris Mason28f75a02015-02-04 06:59:29 -08004764 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004765 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00004766 } else {
4767 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05004768 }
Chris Mason39279cc2007-06-12 06:35:45 -04004769 }
Yan, Zheng80825102009-11-12 09:35:36 +00004770out:
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004771 if (ret >= 0 && pending_del_nr) {
4772 int err;
4773
4774 err = btrfs_del_items(trans, root, path, pending_del_slot,
Chris Mason85e21ba2008-01-29 15:11:36 -05004775 pending_del_nr);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004776 if (err) {
4777 btrfs_abort_transaction(trans, err);
4778 ret = err;
4779 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004780 }
Filipe Manana76b42ab2017-02-14 16:56:01 +00004781 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
4782 ASSERT(last_size >= new_size);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004783 if (!ret && last_size > new_size)
Filipe Manana76b42ab2017-02-14 16:56:01 +00004784 last_size = new_size;
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004785 btrfs_ordered_update_i_size(inode, last_size, NULL);
Filipe Manana76b42ab2017-02-14 16:56:01 +00004786 }
Chris Mason28ed1342014-12-17 09:41:04 -08004787
Chris Mason39279cc2007-06-12 06:35:45 -04004788 btrfs_free_path(path);
Chris Mason28ed1342014-12-17 09:41:04 -08004789
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004790 if (be_nice && bytes_deleted > SZ_32M && (ret >= 0 || ret == -EAGAIN)) {
Chris Mason28ed1342014-12-17 09:41:04 -08004791 unsigned long updates = trans->delayed_ref_updates;
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004792 int err;
4793
Chris Mason28ed1342014-12-17 09:41:04 -08004794 if (updates) {
4795 trans->delayed_ref_updates = 0;
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004796 err = btrfs_run_delayed_refs(trans, updates * 2);
4797 if (err)
4798 ret = err;
Chris Mason28ed1342014-12-17 09:41:04 -08004799 }
4800 }
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004801 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04004802}
4803
Chris Masona52d9a82007-08-27 16:49:44 -04004804/*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304805 * btrfs_truncate_block - read, zero a chunk and write a block
Josef Bacik2aaa6652012-08-29 14:27:18 -04004806 * @inode - inode that we're zeroing
4807 * @from - the offset to start zeroing
4808 * @len - the length to zero, 0 to zero the entire range respective to the
4809 * offset
4810 * @front - zero up to the offset instead of from the offset on
4811 *
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304812 * This will find the block for the "from" offset and cow the block and zero the
Josef Bacik2aaa6652012-08-29 14:27:18 -04004813 * part we want to zero. This is used with truncate and hole punching.
Chris Masona52d9a82007-08-27 16:49:44 -04004814 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304815int btrfs_truncate_block(struct inode *inode, loff_t from, loff_t len,
Josef Bacik2aaa6652012-08-29 14:27:18 -04004816 int front)
Chris Masona52d9a82007-08-27 16:49:44 -04004817{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004818 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004819 struct address_space *mapping = inode->i_mapping;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004820 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4821 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004822 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08004823 struct extent_changeset *data_reserved = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004824 char *kaddr;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004825 u32 blocksize = fs_info->sectorsize;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004826 pgoff_t index = from >> PAGE_SHIFT;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304827 unsigned offset = from & (blocksize - 1);
Chris Masona52d9a82007-08-27 16:49:44 -04004828 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004829 gfp_t mask = btrfs_alloc_write_mask(mapping);
Chris Masona52d9a82007-08-27 16:49:44 -04004830 int ret = 0;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304831 u64 block_start;
4832 u64 block_end;
Chris Masona52d9a82007-08-27 16:49:44 -04004833
Nikolay Borisovb03ebd92018-01-18 14:47:06 +02004834 if (IS_ALIGNED(offset, blocksize) &&
4835 (!len || IS_ALIGNED(len, blocksize)))
Chris Masona52d9a82007-08-27 16:49:44 -04004836 goto out;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304837
Josef Bacik8b62f872017-10-19 14:15:55 -04004838 block_start = round_down(from, blocksize);
4839 block_end = block_start + blocksize - 1;
4840
Qu Wenruo364ecf32017-02-27 15:10:38 +08004841 ret = btrfs_delalloc_reserve_space(inode, &data_reserved,
Josef Bacik8b62f872017-10-19 14:15:55 -04004842 block_start, blocksize);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004843 if (ret)
4844 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004845
Chris Mason211c17f2008-05-15 09:13:45 -04004846again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004847 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004848 if (!page) {
Qu Wenruobc42bda2017-02-27 15:10:39 +08004849 btrfs_delalloc_release_space(inode, data_reserved,
Qu Wenruo43b18592017-12-12 15:34:32 +08004850 block_start, blocksize, true);
4851 btrfs_delalloc_release_extents(BTRFS_I(inode), blocksize, true);
Miao Xieac6a2b32012-12-05 10:56:13 +00004852 ret = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -04004853 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04004854 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004855
Chris Masona52d9a82007-08-27 16:49:44 -04004856 if (!PageUptodate(page)) {
4857 ret = btrfs_readpage(NULL, page);
4858 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004859 if (page->mapping != mapping) {
4860 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004861 put_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004862 goto again;
4863 }
Chris Masona52d9a82007-08-27 16:49:44 -04004864 if (!PageUptodate(page)) {
4865 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04004866 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04004867 }
4868 }
Chris Mason211c17f2008-05-15 09:13:45 -04004869 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004870
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304871 lock_extent_bits(io_tree, block_start, block_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004872 set_page_extent_mapped(page);
4873
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304874 ordered = btrfs_lookup_ordered_extent(inode, block_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004875 if (ordered) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304876 unlock_extent_cached(io_tree, block_start, block_end,
David Sterbae43bbe52017-12-12 21:43:52 +01004877 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004878 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004879 put_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04004880 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004881 btrfs_put_ordered_extent(ordered);
4882 goto again;
4883 }
4884
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304885 clear_extent_bit(&BTRFS_I(inode)->io_tree, block_start, block_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06004886 EXTENT_DIRTY | EXTENT_DELALLOC |
4887 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
David Sterbaae0f1622017-10-31 16:37:52 +01004888 0, 0, &cached_state);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004889
Filipe Mananae3b8a482017-11-04 00:16:59 +00004890 ret = btrfs_set_extent_delalloc(inode, block_start, block_end, 0,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08004891 &cached_state, 0);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004892 if (ret) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304893 unlock_extent_cached(io_tree, block_start, block_end,
David Sterbae43bbe52017-12-12 21:43:52 +01004894 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004895 goto out_unlock;
4896 }
4897
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304898 if (offset != blocksize) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04004899 if (!len)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304900 len = blocksize - offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004901 kaddr = kmap(page);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004902 if (front)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304903 memset(kaddr + (block_start - page_offset(page)),
4904 0, offset);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004905 else
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304906 memset(kaddr + (block_start - page_offset(page)) + offset,
4907 0, len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004908 flush_dcache_page(page);
4909 kunmap(page);
4910 }
Chris Mason247e7432008-07-17 12:53:51 -04004911 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004912 set_page_dirty(page);
David Sterbae43bbe52017-12-12 21:43:52 +01004913 unlock_extent_cached(io_tree, block_start, block_end, &cached_state);
Chris Mason39279cc2007-06-12 06:35:45 -04004914
Chris Mason89642222008-07-24 09:41:53 -04004915out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04004916 if (ret)
Qu Wenruobc42bda2017-02-27 15:10:39 +08004917 btrfs_delalloc_release_space(inode, data_reserved, block_start,
Qu Wenruo43b18592017-12-12 15:34:32 +08004918 blocksize, true);
4919 btrfs_delalloc_release_extents(BTRFS_I(inode), blocksize, (ret != 0));
Chris Mason39279cc2007-06-12 06:35:45 -04004920 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004921 put_page(page);
Chris Mason39279cc2007-06-12 06:35:45 -04004922out:
Qu Wenruo364ecf32017-02-27 15:10:38 +08004923 extent_changeset_free(data_reserved);
Chris Mason39279cc2007-06-12 06:35:45 -04004924 return ret;
4925}
4926
Josef Bacik16e75492013-10-22 12:18:51 -04004927static int maybe_insert_hole(struct btrfs_root *root, struct inode *inode,
4928 u64 offset, u64 len)
4929{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004930 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik16e75492013-10-22 12:18:51 -04004931 struct btrfs_trans_handle *trans;
4932 int ret;
4933
4934 /*
4935 * Still need to make sure the inode looks like it's been updated so
4936 * that any holes get logged if we fsync.
4937 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004938 if (btrfs_fs_incompat(fs_info, NO_HOLES)) {
4939 BTRFS_I(inode)->last_trans = fs_info->generation;
Josef Bacik16e75492013-10-22 12:18:51 -04004940 BTRFS_I(inode)->last_sub_trans = root->log_transid;
4941 BTRFS_I(inode)->last_log_commit = root->last_log_commit;
4942 return 0;
4943 }
4944
4945 /*
4946 * 1 - for the one we're dropping
4947 * 1 - for the one we're adding
4948 * 1 - for updating the inode.
4949 */
4950 trans = btrfs_start_transaction(root, 3);
4951 if (IS_ERR(trans))
4952 return PTR_ERR(trans);
4953
4954 ret = btrfs_drop_extents(trans, root, inode, offset, offset + len, 1);
4955 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004956 btrfs_abort_transaction(trans, ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004957 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04004958 return ret;
4959 }
4960
David Sterbaf85b7372017-01-20 14:54:07 +01004961 ret = btrfs_insert_file_extent(trans, root, btrfs_ino(BTRFS_I(inode)),
4962 offset, 0, 0, len, 0, len, 0, 0, 0);
Josef Bacik16e75492013-10-22 12:18:51 -04004963 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004964 btrfs_abort_transaction(trans, ret);
Josef Bacik16e75492013-10-22 12:18:51 -04004965 else
4966 btrfs_update_inode(trans, root, inode);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004967 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04004968 return ret;
4969}
4970
Josef Bacik695a0d02011-03-04 15:46:53 -05004971/*
4972 * This function puts in dummy file extents for the area we're creating a hole
4973 * for. So if we are truncating this file to a larger size we need to insert
4974 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
4975 * the range between oldsize and size
4976 */
Josef Bacika41ad392011-01-31 15:30:16 -05004977int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04004978{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004979 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Yan Zheng9036c102008-10-30 14:19:41 -04004980 struct btrfs_root *root = BTRFS_I(inode)->root;
4981 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004982 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004983 struct extent_state *cached_state = NULL;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004984 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004985 u64 hole_start = ALIGN(oldsize, fs_info->sectorsize);
4986 u64 block_end = ALIGN(size, fs_info->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04004987 u64 last_byte;
4988 u64 cur_offset;
4989 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04004990 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04004991
Josef Bacika71754f2013-06-17 17:14:39 -04004992 /*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304993 * If our size started in the middle of a block we need to zero out the
4994 * rest of the block before we expand the i_size, otherwise we could
Josef Bacika71754f2013-06-17 17:14:39 -04004995 * expose stale data.
4996 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304997 err = btrfs_truncate_block(inode, oldsize, 0, 0);
Josef Bacika71754f2013-06-17 17:14:39 -04004998 if (err)
4999 return err;
5000
Yan Zheng9036c102008-10-30 14:19:41 -04005001 if (size <= hole_start)
5002 return 0;
5003
Yan Zheng9036c102008-10-30 14:19:41 -04005004 while (1) {
5005 struct btrfs_ordered_extent *ordered;
Miao Xiefa7c1492013-09-26 13:15:27 +08005006
David Sterbaff13db42015-12-03 14:30:40 +01005007 lock_extent_bits(io_tree, hole_start, block_end - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01005008 &cached_state);
Nikolay Borisova776c6f2017-02-20 13:50:49 +02005009 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), hole_start,
Miao Xiefa7c1492013-09-26 13:15:27 +08005010 block_end - hole_start);
Yan Zheng9036c102008-10-30 14:19:41 -04005011 if (!ordered)
5012 break;
Josef Bacik2ac55d42010-02-03 19:33:23 +00005013 unlock_extent_cached(io_tree, hole_start, block_end - 1,
David Sterbae43bbe52017-12-12 21:43:52 +01005014 &cached_state);
Miao Xiefa7c1492013-09-26 13:15:27 +08005015 btrfs_start_ordered_extent(inode, ordered, 1);
Yan Zheng9036c102008-10-30 14:19:41 -04005016 btrfs_put_ordered_extent(ordered);
5017 }
5018
Yan Zheng9036c102008-10-30 14:19:41 -04005019 cur_offset = hole_start;
5020 while (1) {
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02005021 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, cur_offset,
Yan Zheng9036c102008-10-30 14:19:41 -04005022 block_end - cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005023 if (IS_ERR(em)) {
5024 err = PTR_ERR(em);
Zach Brownf2767952013-01-08 19:37:58 +00005025 em = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005026 break;
5027 }
Yan Zheng9036c102008-10-30 14:19:41 -04005028 last_byte = min(extent_map_end(em), block_end);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005029 last_byte = ALIGN(last_byte, fs_info->sectorsize);
Yan, Zheng80825102009-11-12 09:35:36 +00005030 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04005031 struct extent_map *hole_em;
Yan Zheng9036c102008-10-30 14:19:41 -04005032 hole_size = last_byte - cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00005033
Josef Bacik16e75492013-10-22 12:18:51 -04005034 err = maybe_insert_hole(root, inode, cur_offset,
5035 hole_size);
5036 if (err)
Chris Mason771ed682008-11-06 22:02:51 -05005037 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02005038 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04005039 cur_offset + hole_size - 1, 0);
5040 hole_em = alloc_extent_map();
5041 if (!hole_em) {
5042 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
5043 &BTRFS_I(inode)->runtime_flags);
5044 goto next;
5045 }
5046 hole_em->start = cur_offset;
5047 hole_em->len = hole_size;
5048 hole_em->orig_start = cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00005049
Josef Bacik5dc562c2012-08-17 13:14:17 -04005050 hole_em->block_start = EXTENT_MAP_HOLE;
5051 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05005052 hole_em->orig_block_len = 0;
Josef Bacikcc95bef2013-04-04 14:31:27 -04005053 hole_em->ram_bytes = hole_size;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005054 hole_em->bdev = fs_info->fs_devices->latest_bdev;
Josef Bacik5dc562c2012-08-17 13:14:17 -04005055 hole_em->compress_type = BTRFS_COMPRESS_NONE;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005056 hole_em->generation = fs_info->generation;
Josef Bacik5dc562c2012-08-17 13:14:17 -04005057
5058 while (1) {
5059 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04005060 err = add_extent_mapping(em_tree, hole_em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04005061 write_unlock(&em_tree->lock);
5062 if (err != -EEXIST)
5063 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02005064 btrfs_drop_extent_cache(BTRFS_I(inode),
5065 cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04005066 cur_offset +
5067 hole_size - 1, 0);
5068 }
5069 free_extent_map(hole_em);
Yan Zheng9036c102008-10-30 14:19:41 -04005070 }
Josef Bacik16e75492013-10-22 12:18:51 -04005071next:
Yan Zheng9036c102008-10-30 14:19:41 -04005072 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04005073 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04005074 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00005075 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04005076 break;
5077 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04005078 free_extent_map(em);
David Sterbae43bbe52017-12-12 21:43:52 +01005079 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state);
Yan Zheng9036c102008-10-30 14:19:41 -04005080 return err;
5081}
5082
Eric Sandeen3972f262013-01-12 02:57:22 +00005083static int btrfs_setsize(struct inode *inode, struct iattr *attr)
Yan, Zheng80825102009-11-12 09:35:36 +00005084{
Miao Xief4a2f4c2011-12-14 20:12:01 -05005085 struct btrfs_root *root = BTRFS_I(inode)->root;
5086 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05005087 loff_t oldsize = i_size_read(inode);
Eric Sandeen3972f262013-01-12 02:57:22 +00005088 loff_t newsize = attr->ia_size;
5089 int mask = attr->ia_valid;
Yan, Zheng80825102009-11-12 09:35:36 +00005090 int ret;
5091
Eric Sandeen3972f262013-01-12 02:57:22 +00005092 /*
5093 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
5094 * special case where we need to update the times despite not having
5095 * these flags set. For all other operations the VFS set these flags
5096 * explicitly if it wants a timestamp update.
5097 */
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08005098 if (newsize != oldsize) {
5099 inode_inc_iversion(inode);
5100 if (!(mask & (ATTR_CTIME | ATTR_MTIME)))
5101 inode->i_ctime = inode->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07005102 current_time(inode);
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08005103 }
Eric Sandeen3972f262013-01-12 02:57:22 +00005104
Josef Bacika41ad392011-01-31 15:30:16 -05005105 if (newsize > oldsize) {
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005106 /*
David Sterbaea14b57f2017-06-22 02:19:11 +02005107 * Don't do an expanding truncate while snapshotting is ongoing.
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005108 * This is to ensure the snapshot captures a fully consistent
5109 * state of this file - if the snapshot captures this expanding
5110 * truncation, it must capture all writes that happened before
5111 * this truncation.
5112 */
Zhao Lei0bc19f902016-01-06 18:56:36 +08005113 btrfs_wait_for_snapshot_creation(root);
Josef Bacika41ad392011-01-31 15:30:16 -05005114 ret = btrfs_cont_expand(inode, oldsize, newsize);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005115 if (ret) {
David Sterbaea14b57f2017-06-22 02:19:11 +02005116 btrfs_end_write_no_snapshotting(root);
Yan, Zheng80825102009-11-12 09:35:36 +00005117 return ret;
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005118 }
Yan, Zheng80825102009-11-12 09:35:36 +00005119
Miao Xief4a2f4c2011-12-14 20:12:01 -05005120 trans = btrfs_start_transaction(root, 1);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005121 if (IS_ERR(trans)) {
David Sterbaea14b57f2017-06-22 02:19:11 +02005122 btrfs_end_write_no_snapshotting(root);
Miao Xief4a2f4c2011-12-14 20:12:01 -05005123 return PTR_ERR(trans);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005124 }
Miao Xief4a2f4c2011-12-14 20:12:01 -05005125
5126 i_size_write(inode, newsize);
5127 btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
Chandan Rajendra27772b62016-01-21 15:56:03 +05305128 pagecache_isize_extended(inode, oldsize, newsize);
Miao Xief4a2f4c2011-12-14 20:12:01 -05005129 ret = btrfs_update_inode(trans, root, inode);
David Sterbaea14b57f2017-06-22 02:19:11 +02005130 btrfs_end_write_no_snapshotting(root);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005131 btrfs_end_transaction(trans);
Josef Bacika41ad392011-01-31 15:30:16 -05005132 } else {
Yan, Zheng80825102009-11-12 09:35:36 +00005133
Josef Bacika41ad392011-01-31 15:30:16 -05005134 /*
5135 * We're truncating a file that used to have good data down to
5136 * zero. Make sure it gets into the ordered flush list so that
5137 * any new writes get down to disk quickly.
5138 */
5139 if (newsize == 0)
Josef Bacik72ac3c02012-05-23 14:13:11 -04005140 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
5141 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00005142
Josef Bacika41ad392011-01-31 15:30:16 -05005143 truncate_setsize(inode, newsize);
Miao Xie2e60a512013-02-08 07:01:08 +00005144
5145 /* Disable nonlocked read DIO to avoid the end less truncate */
Nikolay Borisovabcefb12017-02-20 13:51:10 +02005146 btrfs_inode_block_unlocked_dio(BTRFS_I(inode));
Miao Xie2e60a512013-02-08 07:01:08 +00005147 inode_dio_wait(inode);
Nikolay Borisov0b581702017-02-20 13:51:11 +02005148 btrfs_inode_resume_unlocked_dio(BTRFS_I(inode));
Miao Xie2e60a512013-02-08 07:01:08 +00005149
Filipe Manana213e8c52018-02-06 20:40:31 +00005150 ret = btrfs_truncate(inode, newsize == oldsize);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005151 if (ret && inode->i_nlink) {
5152 int err;
5153
5154 /*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07005155 * Truncate failed, so fix up the in-memory size. We
5156 * adjusted disk_i_size down as we removed extents, so
5157 * wait for disk_i_size to be stable and then update the
5158 * in-memory size to match.
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005159 */
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07005160 err = btrfs_wait_ordered_range(inode, 0, (u64)-1);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005161 if (err)
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07005162 return err;
5163 i_size_write(inode, BTRFS_I(inode)->disk_i_size);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005164 }
Yan, Zheng80825102009-11-12 09:35:36 +00005165 }
5166
Josef Bacika41ad392011-01-31 15:30:16 -05005167 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00005168}
5169
Chris Mason39279cc2007-06-12 06:35:45 -04005170static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
5171{
David Howells2b0143b2015-03-17 22:25:59 +00005172 struct inode *inode = d_inode(dentry);
Li Zefanb83cc962010-12-20 16:04:08 +08005173 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005174 int err;
5175
Li Zefanb83cc962010-12-20 16:04:08 +08005176 if (btrfs_root_readonly(root))
5177 return -EROFS;
5178
Jan Kara31051c82016-05-26 16:55:18 +02005179 err = setattr_prepare(dentry, attr);
Chris Mason39279cc2007-06-12 06:35:45 -04005180 if (err)
5181 return err;
5182
Chris Mason5a3f23d2009-03-31 13:27:11 -04005183 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Eric Sandeen3972f262013-01-12 02:57:22 +00005184 err = btrfs_setsize(inode, attr);
Yan, Zheng80825102009-11-12 09:35:36 +00005185 if (err)
5186 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005187 }
Yan Zheng9036c102008-10-30 14:19:41 -04005188
Christoph Hellwig10257742010-06-04 11:30:02 +02005189 if (attr->ia_valid) {
5190 setattr_copy(inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005191 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005192 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04005193
Josef Bacik22c44fe2011-11-30 10:45:38 -05005194 if (!err && attr->ia_valid & ATTR_MODE)
Christoph Hellwig996a7102013-12-20 05:16:43 -08005195 err = posix_acl_chmod(inode, inode->i_mode);
Christoph Hellwig10257742010-06-04 11:30:02 +02005196 }
5197
Chris Mason39279cc2007-06-12 06:35:45 -04005198 return err;
5199}
Chris Mason61295eb2008-01-14 16:24:38 -05005200
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005201/*
5202 * While truncating the inode pages during eviction, we get the VFS calling
5203 * btrfs_invalidatepage() against each page of the inode. This is slow because
5204 * the calls to btrfs_invalidatepage() result in a huge amount of calls to
5205 * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting
5206 * extent_state structures over and over, wasting lots of time.
5207 *
5208 * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all
5209 * those expensive operations on a per page basis and do only the ordered io
5210 * finishing, while we release here the extent_map and extent_state structures,
5211 * without the excessive merging and splitting.
5212 */
5213static void evict_inode_truncate_pages(struct inode *inode)
5214{
5215 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
5216 struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree;
5217 struct rb_node *node;
5218
5219 ASSERT(inode->i_state & I_FREEING);
Johannes Weiner91b0abe2014-04-03 14:47:49 -07005220 truncate_inode_pages_final(&inode->i_data);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005221
5222 write_lock(&map_tree->lock);
Liu Bo07e1ce02018-08-23 03:51:52 +08005223 while (!RB_EMPTY_ROOT(&map_tree->map.rb_root)) {
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005224 struct extent_map *em;
5225
Liu Bo07e1ce02018-08-23 03:51:52 +08005226 node = rb_first_cached(&map_tree->map);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005227 em = rb_entry(node, struct extent_map, rb_node);
Wang Shilong180589e2013-12-14 15:27:31 +08005228 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
5229 clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005230 remove_extent_mapping(map_tree, em);
5231 free_extent_map(em);
Filipe Manana7064dd52014-08-08 02:47:05 +01005232 if (need_resched()) {
5233 write_unlock(&map_tree->lock);
5234 cond_resched();
5235 write_lock(&map_tree->lock);
5236 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005237 }
5238 write_unlock(&map_tree->lock);
5239
Filipe Manana6ca07092015-05-26 00:55:42 +01005240 /*
5241 * Keep looping until we have no more ranges in the io tree.
5242 * We can have ongoing bios started by readpages (called from readahead)
Filipe Manana9c6429d2015-06-10 12:55:41 +01005243 * that have their endio callback (extent_io.c:end_bio_extent_readpage)
5244 * still in progress (unlocked the pages in the bio but did not yet
5245 * unlocked the ranges in the io tree). Therefore this means some
Filipe Manana6ca07092015-05-26 00:55:42 +01005246 * ranges can still be locked and eviction started because before
5247 * submitting those bios, which are executed by a separate task (work
5248 * queue kthread), inode references (inode->i_count) were not taken
5249 * (which would be dropped in the end io callback of each bio).
5250 * Therefore here we effectively end up waiting for those bios and
5251 * anyone else holding locked ranges without having bumped the inode's
5252 * reference count - if we don't do it, when they access the inode's
5253 * io_tree to unlock a range it may be too late, leading to an
5254 * use-after-free issue.
5255 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005256 spin_lock(&io_tree->lock);
5257 while (!RB_EMPTY_ROOT(&io_tree->state)) {
5258 struct extent_state *state;
5259 struct extent_state *cached_state = NULL;
Filipe Manana6ca07092015-05-26 00:55:42 +01005260 u64 start;
5261 u64 end;
Filipe Manana421f0922018-10-12 13:02:48 +01005262 unsigned state_flags;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005263
5264 node = rb_first(&io_tree->state);
5265 state = rb_entry(node, struct extent_state, rb_node);
Filipe Manana6ca07092015-05-26 00:55:42 +01005266 start = state->start;
5267 end = state->end;
Filipe Manana421f0922018-10-12 13:02:48 +01005268 state_flags = state->state;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005269 spin_unlock(&io_tree->lock);
5270
David Sterbaff13db42015-12-03 14:30:40 +01005271 lock_extent_bits(io_tree, start, end, &cached_state);
Qu Wenruob9d0b382015-09-29 10:35:16 +08005272
5273 /*
5274 * If still has DELALLOC flag, the extent didn't reach disk,
5275 * and its reserved space won't be freed by delayed_ref.
5276 * So we need to free its reserved space here.
5277 * (Refer to comment in btrfs_invalidatepage, case 2)
5278 *
5279 * Note, end is the bytenr of last byte, so we need + 1 here.
5280 */
Filipe Manana421f0922018-10-12 13:02:48 +01005281 if (state_flags & EXTENT_DELALLOC)
Qu Wenruobc42bda2017-02-27 15:10:39 +08005282 btrfs_qgroup_free_data(inode, NULL, start, end - start + 1);
Qu Wenruob9d0b382015-09-29 10:35:16 +08005283
Filipe Manana6ca07092015-05-26 00:55:42 +01005284 clear_extent_bit(io_tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005285 EXTENT_LOCKED | EXTENT_DIRTY |
5286 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
David Sterbaae0f1622017-10-31 16:37:52 +01005287 EXTENT_DEFRAG, 1, 1, &cached_state);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005288
Filipe Manana7064dd52014-08-08 02:47:05 +01005289 cond_resched();
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005290 spin_lock(&io_tree->lock);
5291 }
5292 spin_unlock(&io_tree->lock);
5293}
5294
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005295static struct btrfs_trans_handle *evict_refill_and_join(struct btrfs_root *root,
Josef Bacikad80cf52018-09-28 07:18:19 -04005296 struct btrfs_block_rsv *rsv)
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005297{
5298 struct btrfs_fs_info *fs_info = root->fs_info;
5299 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
5300 int failures = 0;
5301
5302 for (;;) {
5303 struct btrfs_trans_handle *trans;
5304 int ret;
5305
Josef Bacikad80cf52018-09-28 07:18:19 -04005306 ret = btrfs_block_rsv_refill(root, rsv, rsv->size,
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005307 BTRFS_RESERVE_FLUSH_LIMIT);
5308
5309 if (ret && ++failures > 2) {
5310 btrfs_warn(fs_info,
5311 "could not allocate space for a delete; will truncate on mount");
5312 return ERR_PTR(-ENOSPC);
5313 }
5314
5315 trans = btrfs_join_transaction(root);
5316 if (IS_ERR(trans) || !ret)
5317 return trans;
5318
5319 /*
5320 * Try to steal from the global reserve if there is space for
5321 * it.
5322 */
Lu Fengqiaf9b8a02018-10-11 13:40:35 +08005323 if (!btrfs_check_space_for_delayed_refs(trans) &&
Josef Bacikad80cf52018-09-28 07:18:19 -04005324 !btrfs_block_rsv_migrate(global_rsv, rsv, rsv->size, false))
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005325 return trans;
5326
5327 /* If not, commit and try again. */
5328 ret = btrfs_commit_transaction(trans);
5329 if (ret)
5330 return ERR_PTR(ret);
5331 }
5332}
5333
Al Virobd555972010-06-07 11:35:40 -04005334void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005335{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005336 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04005337 struct btrfs_trans_handle *trans;
5338 struct btrfs_root *root = BTRFS_I(inode)->root;
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005339 struct btrfs_block_rsv *rsv;
Chris Mason39279cc2007-06-12 06:35:45 -04005340 int ret;
5341
liubo1abe9b82011-03-24 11:18:59 +00005342 trace_btrfs_inode_evict(inode);
5343
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005344 if (!root) {
Liu Boe8f1bc12018-01-25 11:02:53 -07005345 clear_inode(inode);
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005346 return;
5347 }
5348
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005349 evict_inode_truncate_pages(inode);
5350
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005351 if (inode->i_nlink &&
5352 ((btrfs_root_refs(&root->root_item) != 0 &&
5353 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) ||
Nikolay Borisov70ddc552017-02-20 13:50:35 +02005354 btrfs_is_free_space_inode(BTRFS_I(inode))))
Al Virobd555972010-06-07 11:35:40 -04005355 goto no_delete;
5356
Omar Sandoval27919062018-05-11 13:13:37 -07005357 if (is_bad_inode(inode))
Chris Mason39279cc2007-06-12 06:35:45 -04005358 goto no_delete;
Chris Mason5f39d392007-10-15 16:14:19 -04005359
Nikolay Borisov7ab79562017-02-20 13:50:57 +02005360 btrfs_free_io_failure_record(BTRFS_I(inode), 0, (u64)-1);
Miao Xief6124962014-09-12 18:44:04 +08005361
Omar Sandoval7b40b692018-05-11 13:13:33 -07005362 if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags))
Yan, Zhengc71bf092009-11-12 09:34:40 +00005363 goto no_delete;
Yan, Zhengc71bf092009-11-12 09:34:40 +00005364
Yan, Zheng76dda932009-09-21 16:00:26 -04005365 if (inode->i_nlink > 0) {
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005366 BUG_ON(btrfs_root_refs(&root->root_item) != 0 &&
5367 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID);
Yan, Zheng76dda932009-09-21 16:00:26 -04005368 goto no_delete;
5369 }
5370
Nikolay Borisovaa790212017-01-10 20:35:40 +02005371 ret = btrfs_commit_inode_delayed_inode(BTRFS_I(inode));
Omar Sandoval27919062018-05-11 13:13:37 -07005372 if (ret)
Miao Xie0e8c36a2012-12-19 06:59:51 +00005373 goto no_delete;
Miao Xie0e8c36a2012-12-19 06:59:51 +00005374
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005375 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Omar Sandoval27919062018-05-11 13:13:37 -07005376 if (!rsv)
Josef Bacik4289a662011-08-05 13:22:24 -04005377 goto no_delete;
Josef Bacikad80cf52018-09-28 07:18:19 -04005378 rsv->size = btrfs_calc_trunc_metadata_size(fs_info, 1);
Josef Bacikca7e70f2012-08-27 17:48:15 -04005379 rsv->failfast = 1;
Josef Bacik4289a662011-08-05 13:22:24 -04005380
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02005381 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Mason5f39d392007-10-15 16:14:19 -04005382
Yan, Zheng80825102009-11-12 09:35:36 +00005383 while (1) {
Josef Bacikad80cf52018-09-28 07:18:19 -04005384 trans = evict_refill_and_join(root, rsv);
Omar Sandoval27919062018-05-11 13:13:37 -07005385 if (IS_ERR(trans))
5386 goto free_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04005387
5388 trans->block_rsv = rsv;
5389
Yan, Zhengd68fc572010-05-16 10:49:58 -04005390 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Omar Sandoval27919062018-05-11 13:13:37 -07005391 trans->block_rsv = &fs_info->trans_block_rsv;
5392 btrfs_end_transaction(trans);
5393 btrfs_btree_balance_dirty(fs_info);
5394 if (ret && ret != -ENOSPC && ret != -EAGAIN)
5395 goto free_rsv;
5396 else if (!ret)
Yan, Zheng80825102009-11-12 09:35:36 +00005397 break;
Josef Bacik7b128762008-07-24 12:17:14 -04005398 }
5399
Josef Bacik4ef31a42013-08-13 14:10:08 -04005400 /*
Omar Sandoval27919062018-05-11 13:13:37 -07005401 * Errors here aren't a big deal, it just means we leave orphan items in
5402 * the tree. They will be cleaned up on the next mount. If the inode
5403 * number gets reused, cleanup deletes the orphan item without doing
5404 * anything, and unlink reuses the existing orphan item.
5405 *
5406 * If it turns out that we are dropping too many of these, we might want
5407 * to add a mechanism for retrying these after a commit.
Josef Bacik4ef31a42013-08-13 14:10:08 -04005408 */
Josef Bacikad80cf52018-09-28 07:18:19 -04005409 trans = evict_refill_and_join(root, rsv);
Omar Sandoval27919062018-05-11 13:13:37 -07005410 if (!IS_ERR(trans)) {
5411 trans->block_rsv = rsv;
5412 btrfs_orphan_del(trans, BTRFS_I(inode));
5413 trans->block_rsv = &fs_info->trans_block_rsv;
5414 btrfs_end_transaction(trans);
5415 }
Chris Mason85e21ba2008-01-29 15:11:36 -05005416
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005417 if (!(root == fs_info->tree_root ||
Li Zefan581bb052011-04-20 10:06:11 +08005418 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005419 btrfs_return_ino(root, btrfs_ino(BTRFS_I(inode)));
Li Zefan581bb052011-04-20 10:06:11 +08005420
Omar Sandoval27919062018-05-11 13:13:37 -07005421free_rsv:
5422 btrfs_free_block_rsv(fs_info, rsv);
Chris Mason39279cc2007-06-12 06:35:45 -04005423no_delete:
Omar Sandoval27919062018-05-11 13:13:37 -07005424 /*
5425 * If we didn't successfully delete, the orphan item will still be in
5426 * the tree and we'll retry on the next mount. Again, we might also want
5427 * to retry these periodically in the future.
5428 */
Nikolay Borisovf48d1cf2017-01-10 20:35:39 +02005429 btrfs_remove_delayed_node(BTRFS_I(inode));
Jan Karadbd57682012-05-03 14:48:02 +02005430 clear_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005431}
5432
5433/*
5434 * this returns the key found in the dir entry in the location pointer.
Su Yue005d6712018-03-05 17:13:37 +08005435 * If no dir entries were found, returns -ENOENT.
5436 * If found a corrupted location in dir entry, returns -EUCLEAN.
Chris Mason39279cc2007-06-12 06:35:45 -04005437 */
5438static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
5439 struct btrfs_key *location)
5440{
5441 const char *name = dentry->d_name.name;
5442 int namelen = dentry->d_name.len;
5443 struct btrfs_dir_item *di;
5444 struct btrfs_path *path;
5445 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04005446 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005447
5448 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005449 if (!path)
5450 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05005451
David Sterbaf85b7372017-01-20 14:54:07 +01005452 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(BTRFS_I(dir)),
5453 name, namelen, 0);
Liu Bo3cf50682018-09-12 06:06:26 +08005454 if (IS_ERR_OR_NULL(di)) {
5455 ret = di ? PTR_ERR(di) : -ENOENT;
Su Yue005d6712018-03-05 17:13:37 +08005456 goto out;
5457 }
Chris Masond3977122009-01-05 21:25:51 -05005458
Chris Mason5f39d392007-10-15 16:14:19 -04005459 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Liu Bo56a0e702017-10-30 11:14:38 -06005460 if (location->type != BTRFS_INODE_ITEM_KEY &&
5461 location->type != BTRFS_ROOT_ITEM_KEY) {
Su Yue005d6712018-03-05 17:13:37 +08005462 ret = -EUCLEAN;
Liu Bo56a0e702017-10-30 11:14:38 -06005463 btrfs_warn(root->fs_info,
5464"%s gets something invalid in DIR_ITEM (name %s, directory ino %llu, location(%llu %u %llu))",
5465 __func__, name, btrfs_ino(BTRFS_I(dir)),
5466 location->objectid, location->type, location->offset);
Liu Bo56a0e702017-10-30 11:14:38 -06005467 }
Chris Mason39279cc2007-06-12 06:35:45 -04005468out:
Chris Mason39279cc2007-06-12 06:35:45 -04005469 btrfs_free_path(path);
5470 return ret;
5471}
5472
5473/*
5474 * when we hit a tree root in a directory, the btrfs part of the inode
5475 * needs to be changed to reflect the root directory of the tree root. This
5476 * is kind of like crossing a mount point.
5477 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005478static int fixup_tree_root_location(struct btrfs_fs_info *fs_info,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005479 struct inode *dir,
5480 struct dentry *dentry,
5481 struct btrfs_key *location,
5482 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04005483{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005484 struct btrfs_path *path;
5485 struct btrfs_root *new_root;
5486 struct btrfs_root_ref *ref;
5487 struct extent_buffer *leaf;
David Sterba1d4c08e2015-01-02 19:36:14 +01005488 struct btrfs_key key;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005489 int ret;
5490 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005491
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005492 path = btrfs_alloc_path();
5493 if (!path) {
5494 err = -ENOMEM;
5495 goto out;
5496 }
Chris Mason39279cc2007-06-12 06:35:45 -04005497
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005498 err = -ENOENT;
David Sterba1d4c08e2015-01-02 19:36:14 +01005499 key.objectid = BTRFS_I(dir)->root->root_key.objectid;
5500 key.type = BTRFS_ROOT_REF_KEY;
5501 key.offset = location->objectid;
5502
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005503 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005504 if (ret) {
5505 if (ret < 0)
5506 err = ret;
5507 goto out;
5508 }
Chris Mason39279cc2007-06-12 06:35:45 -04005509
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005510 leaf = path->nodes[0];
5511 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005512 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(BTRFS_I(dir)) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005513 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
5514 goto out;
5515
5516 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
5517 (unsigned long)(ref + 1),
5518 dentry->d_name.len);
5519 if (ret)
5520 goto out;
5521
David Sterbab3b4aa72011-04-21 01:20:15 +02005522 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005523
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005524 new_root = btrfs_read_fs_root_no_name(fs_info, location);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005525 if (IS_ERR(new_root)) {
5526 err = PTR_ERR(new_root);
5527 goto out;
5528 }
5529
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005530 *sub_root = new_root;
5531 location->objectid = btrfs_root_dirid(&new_root->root_item);
5532 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04005533 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005534 err = 0;
5535out:
5536 btrfs_free_path(path);
5537 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005538}
5539
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005540static void inode_tree_add(struct inode *inode)
5541{
5542 struct btrfs_root *root = BTRFS_I(inode)->root;
5543 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005544 struct rb_node **p;
5545 struct rb_node *parent;
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005546 struct rb_node *new = &BTRFS_I(inode)->rb_node;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005547 u64 ino = btrfs_ino(BTRFS_I(inode));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005548
Al Viro1d3382cb2010-10-23 15:19:20 -04005549 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005550 return;
Miao Xiee1409ce2013-05-15 07:48:16 +00005551 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005552 spin_lock(&root->inode_lock);
Miao Xiee1409ce2013-05-15 07:48:16 +00005553 p = &root->inode_tree.rb_node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005554 while (*p) {
5555 parent = *p;
5556 entry = rb_entry(parent, struct btrfs_inode, rb_node);
5557
David Sterba37508512018-06-29 10:56:40 +02005558 if (ino < btrfs_ino(entry))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005559 p = &parent->rb_left;
David Sterba37508512018-06-29 10:56:40 +02005560 else if (ino > btrfs_ino(entry))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005561 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005562 else {
5563 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04005564 (I_WILL_FREE | I_FREEING)));
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005565 rb_replace_node(parent, new, &root->inode_tree);
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005566 RB_CLEAR_NODE(parent);
5567 spin_unlock(&root->inode_lock);
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005568 return;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005569 }
5570 }
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005571 rb_link_node(new, parent, p);
5572 rb_insert_color(new, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005573 spin_unlock(&root->inode_lock);
5574}
5575
5576static void inode_tree_del(struct inode *inode)
5577{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005578 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005579 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04005580 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005581
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005582 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005583 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005584 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005585 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04005586 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005587 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005588 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04005589
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005590 if (empty && btrfs_root_refs(&root->root_item) == 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005591 synchronize_srcu(&fs_info->subvol_srcu);
Yan, Zheng76dda932009-09-21 16:00:26 -04005592 spin_lock(&root->inode_lock);
5593 empty = RB_EMPTY_ROOT(&root->inode_tree);
5594 spin_unlock(&root->inode_lock);
5595 if (empty)
5596 btrfs_add_dead_root(root);
5597 }
5598}
5599
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005600
Chris Masone02119d2008-09-05 16:13:11 -04005601static int btrfs_init_locked_inode(struct inode *inode, void *p)
5602{
5603 struct btrfs_iget_args *args = p;
Chris Mason90d3e592014-01-09 17:28:00 -08005604 inode->i_ino = args->location->objectid;
5605 memcpy(&BTRFS_I(inode)->location, args->location,
5606 sizeof(*args->location));
Chris Masone02119d2008-09-05 16:13:11 -04005607 BTRFS_I(inode)->root = args->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005608 return 0;
5609}
5610
5611static int btrfs_find_actor(struct inode *inode, void *opaque)
5612{
5613 struct btrfs_iget_args *args = opaque;
Chris Mason90d3e592014-01-09 17:28:00 -08005614 return args->location->objectid == BTRFS_I(inode)->location.objectid &&
Chris Masond3977122009-01-05 21:25:51 -05005615 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005616}
5617
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005618static struct inode *btrfs_iget_locked(struct super_block *s,
Chris Mason90d3e592014-01-09 17:28:00 -08005619 struct btrfs_key *location,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005620 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04005621{
5622 struct inode *inode;
5623 struct btrfs_iget_args args;
Chris Mason90d3e592014-01-09 17:28:00 -08005624 unsigned long hashval = btrfs_inode_hash(location->objectid, root);
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005625
Chris Mason90d3e592014-01-09 17:28:00 -08005626 args.location = location;
Chris Mason39279cc2007-06-12 06:35:45 -04005627 args.root = root;
5628
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005629 inode = iget5_locked(s, hashval, btrfs_find_actor,
Chris Mason39279cc2007-06-12 06:35:45 -04005630 btrfs_init_locked_inode,
5631 (void *)&args);
5632 return inode;
5633}
5634
Balaji Rao1a54ef82008-07-21 02:01:04 +05305635/* Get an inode object given its location and corresponding root.
5636 * Returns in *is_new if the inode was read from disk
5637 */
Filipe Manana4222ea72018-10-24 10:13:03 +01005638struct inode *btrfs_iget_path(struct super_block *s, struct btrfs_key *location,
5639 struct btrfs_root *root, int *new,
5640 struct btrfs_path *path)
Balaji Rao1a54ef82008-07-21 02:01:04 +05305641{
5642 struct inode *inode;
5643
Chris Mason90d3e592014-01-09 17:28:00 -08005644 inode = btrfs_iget_locked(s, location, root);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305645 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005646 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305647
5648 if (inode->i_state & I_NEW) {
Filipe Manana67710892016-06-06 11:51:25 +01005649 int ret;
5650
Filipe Manana4222ea72018-10-24 10:13:03 +01005651 ret = btrfs_read_locked_inode(inode, path);
Al Viro9bc2cef2018-07-29 23:04:50 +01005652 if (!ret) {
Mark Fasheh1748f842011-07-12 11:25:31 -07005653 inode_tree_add(inode);
5654 unlock_new_inode(inode);
5655 if (new)
5656 *new = 1;
5657 } else {
Al Virof5b3a412018-07-29 23:04:51 +01005658 iget_failed(inode);
5659 /*
5660 * ret > 0 can come from btrfs_search_slot called by
5661 * btrfs_read_locked_inode, this means the inode item
5662 * was not found.
5663 */
5664 if (ret > 0)
5665 ret = -ENOENT;
5666 inode = ERR_PTR(ret);
Mark Fasheh1748f842011-07-12 11:25:31 -07005667 }
5668 }
5669
Balaji Rao1a54ef82008-07-21 02:01:04 +05305670 return inode;
5671}
5672
Filipe Manana4222ea72018-10-24 10:13:03 +01005673struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
5674 struct btrfs_root *root, int *new)
5675{
5676 return btrfs_iget_path(s, location, root, new, NULL);
5677}
5678
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005679static struct inode *new_simple_dir(struct super_block *s,
5680 struct btrfs_key *key,
5681 struct btrfs_root *root)
5682{
5683 struct inode *inode = new_inode(s);
5684
5685 if (!inode)
5686 return ERR_PTR(-ENOMEM);
5687
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005688 BTRFS_I(inode)->root = root;
5689 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04005690 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005691
5692 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Li Zefan848cce02012-02-21 17:04:28 +08005693 inode->i_op = &btrfs_dir_ro_inode_operations;
Omar Sandoval1fdf4192017-01-25 17:06:39 -08005694 inode->i_opflags &= ~IOP_XATTR;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005695 inode->i_fop = &simple_dir_operations;
5696 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
Deepa Dinamanic2050a42016-09-14 07:48:06 -07005697 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05305698 inode->i_atime = inode->i_mtime;
5699 inode->i_ctime = inode->i_mtime;
Arnd Bergmannd3c6be6f2018-06-21 18:04:06 +02005700 BTRFS_I(inode)->i_otime = inode->i_mtime;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005701
5702 return inode;
5703}
5704
Chris Mason3de45862008-11-17 21:02:50 -05005705struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04005706{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005707 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masond3977122009-01-05 21:25:51 -05005708 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005709 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005710 struct btrfs_root *sub_root = root;
5711 struct btrfs_key location;
Yan, Zheng76dda932009-09-21 16:00:26 -04005712 int index;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005713 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005714
5715 if (dentry->d_name.len > BTRFS_NAME_LEN)
5716 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04005717
Jeff Layton39e3c952012-11-28 11:30:53 -05005718 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason39279cc2007-06-12 06:35:45 -04005719 if (ret < 0)
5720 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04005721
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005722 if (location.type == BTRFS_INODE_ITEM_KEY) {
Josef Bacik73f73412009-12-04 17:38:27 +00005723 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005724 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04005725 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005726
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005727 index = srcu_read_lock(&fs_info->subvol_srcu);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005728 ret = fixup_tree_root_location(fs_info, dir, dentry,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005729 &location, &sub_root);
5730 if (ret < 0) {
5731 if (ret != -ENOENT)
5732 inode = ERR_PTR(ret);
5733 else
5734 inode = new_simple_dir(dir->i_sb, &location, sub_root);
5735 } else {
Josef Bacik73f73412009-12-04 17:38:27 +00005736 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04005737 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005738 srcu_read_unlock(&fs_info->subvol_srcu, index);
Yan, Zheng76dda932009-09-21 16:00:26 -04005739
Julia Lawall34d19ba2011-01-24 19:55:19 +00005740 if (!IS_ERR(inode) && root != sub_root) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005741 down_read(&fs_info->cleanup_work_sem);
David Howellsbc98a422017-07-17 08:45:34 +01005742 if (!sb_rdonly(inode->i_sb))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005743 ret = btrfs_orphan_cleanup(sub_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005744 up_read(&fs_info->cleanup_work_sem);
Josef Bacik01cd3362013-06-03 21:39:49 -04005745 if (ret) {
5746 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005747 inode = ERR_PTR(ret);
Josef Bacik01cd3362013-06-03 21:39:49 -04005748 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00005749 }
5750
Chris Mason3de45862008-11-17 21:02:50 -05005751 return inode;
5752}
5753
Nick Pigginfe15ce42011-01-07 17:49:23 +11005754static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04005755{
5756 struct btrfs_root *root;
David Howells2b0143b2015-03-17 22:25:59 +00005757 struct inode *inode = d_inode(dentry);
Yan, Zheng76dda932009-09-21 16:00:26 -04005758
Li Zefan848cce02012-02-21 17:04:28 +08005759 if (!inode && !IS_ROOT(dentry))
David Howells2b0143b2015-03-17 22:25:59 +00005760 inode = d_inode(dentry->d_parent);
Yan, Zheng76dda932009-09-21 16:00:26 -04005761
Li Zefan848cce02012-02-21 17:04:28 +08005762 if (inode) {
5763 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04005764 if (btrfs_root_refs(&root->root_item) == 0)
5765 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08005766
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005767 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Li Zefan848cce02012-02-21 17:04:28 +08005768 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04005769 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005770 return 0;
5771}
5772
Chris Mason3de45862008-11-17 21:02:50 -05005773static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04005774 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05005775{
Al Viro3837d202018-10-10 16:38:27 -04005776 struct inode *inode = btrfs_lookup_dentry(dir, dentry);
Josef Bacika66e7cc2011-09-18 10:34:03 -04005777
Al Viro3837d202018-10-10 16:38:27 -04005778 if (inode == ERR_PTR(-ENOENT))
5779 inode = NULL;
Al Viro41d28bc2014-10-12 22:24:21 -04005780 return d_splice_alias(inode, dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04005781}
5782
Miao Xie16cdcec2011-04-22 18:12:22 +08005783unsigned char btrfs_filetype_table[] = {
Chris Mason39279cc2007-06-12 06:35:45 -04005784 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
5785};
5786
Josef Bacik23b5ec72017-07-24 15:14:25 -04005787/*
5788 * All this infrastructure exists because dir_emit can fault, and we are holding
5789 * the tree lock when doing readdir. For now just allocate a buffer and copy
5790 * our information into that, and then dir_emit from the buffer. This is
5791 * similar to what NFS does, only we don't keep the buffer around in pagecache
5792 * because I'm afraid I'll mess that up. Long term we need to make filldir do
5793 * copy_to_user_inatomic so we don't have to worry about page faulting under the
5794 * tree lock.
5795 */
5796static int btrfs_opendir(struct inode *inode, struct file *file)
5797{
5798 struct btrfs_file_private *private;
5799
5800 private = kzalloc(sizeof(struct btrfs_file_private), GFP_KERNEL);
5801 if (!private)
5802 return -ENOMEM;
5803 private->filldir_buf = kzalloc(PAGE_SIZE, GFP_KERNEL);
5804 if (!private->filldir_buf) {
5805 kfree(private);
5806 return -ENOMEM;
5807 }
5808 file->private_data = private;
5809 return 0;
5810}
5811
5812struct dir_entry {
5813 u64 ino;
5814 u64 offset;
5815 unsigned type;
5816 int name_len;
5817};
5818
5819static int btrfs_filldir(void *addr, int entries, struct dir_context *ctx)
5820{
5821 while (entries--) {
5822 struct dir_entry *entry = addr;
5823 char *name = (char *)(entry + 1);
5824
David Sterba92d32172018-04-16 21:10:14 +02005825 ctx->pos = get_unaligned(&entry->offset);
5826 if (!dir_emit(ctx, name, get_unaligned(&entry->name_len),
5827 get_unaligned(&entry->ino),
5828 get_unaligned(&entry->type)))
Josef Bacik23b5ec72017-07-24 15:14:25 -04005829 return 1;
David Sterba92d32172018-04-16 21:10:14 +02005830 addr += sizeof(struct dir_entry) +
5831 get_unaligned(&entry->name_len);
Josef Bacik23b5ec72017-07-24 15:14:25 -04005832 ctx->pos++;
5833 }
5834 return 0;
5835}
5836
Al Viro9cdda8d2013-05-22 16:48:09 -04005837static int btrfs_real_readdir(struct file *file, struct dir_context *ctx)
Chris Mason39279cc2007-06-12 06:35:45 -04005838{
Al Viro9cdda8d2013-05-22 16:48:09 -04005839 struct inode *inode = file_inode(file);
Chris Mason39279cc2007-06-12 06:35:45 -04005840 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005841 struct btrfs_file_private *private = file->private_data;
Chris Mason39279cc2007-06-12 06:35:45 -04005842 struct btrfs_dir_item *di;
5843 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04005844 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04005845 struct btrfs_path *path;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005846 void *addr;
Miao Xie16cdcec2011-04-22 18:12:22 +08005847 struct list_head ins_list;
5848 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04005849 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04005850 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04005851 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04005852 char *name_ptr;
5853 int name_len;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005854 int entries = 0;
5855 int total_len = 0;
Omar Sandoval02dbfc92016-05-20 13:50:33 -07005856 bool put = false;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005857 struct btrfs_key location;
Chris Mason5f39d392007-10-15 16:14:19 -04005858
Al Viro9cdda8d2013-05-22 16:48:09 -04005859 if (!dir_emit_dots(file, ctx))
5860 return 0;
5861
David Woodhouse49593bf2008-08-17 17:08:36 +01005862 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08005863 if (!path)
5864 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04005865
Josef Bacik23b5ec72017-07-24 15:14:25 -04005866 addr = private->filldir_buf;
David Sterbae4058b52015-11-27 16:31:35 +01005867 path->reada = READA_FORWARD;
David Woodhouse49593bf2008-08-17 17:08:36 +01005868
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005869 INIT_LIST_HEAD(&ins_list);
5870 INIT_LIST_HEAD(&del_list);
5871 put = btrfs_readdir_get_delayed_items(inode, &ins_list, &del_list);
Miao Xie16cdcec2011-04-22 18:12:22 +08005872
Josef Bacik23b5ec72017-07-24 15:14:25 -04005873again:
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005874 key.type = BTRFS_DIR_INDEX_KEY;
Al Viro9cdda8d2013-05-22 16:48:09 -04005875 key.offset = ctx->pos;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005876 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason5f39d392007-10-15 16:14:19 -04005877
Chris Mason39279cc2007-06-12 06:35:45 -04005878 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5879 if (ret < 0)
5880 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01005881
5882 while (1) {
Josef Bacik23b5ec72017-07-24 15:14:25 -04005883 struct dir_entry *entry;
5884
Chris Mason5f39d392007-10-15 16:14:19 -04005885 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04005886 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08005887 if (slot >= btrfs_header_nritems(leaf)) {
5888 ret = btrfs_next_leaf(root, path);
5889 if (ret < 0)
5890 goto err;
5891 else if (ret > 0)
5892 break;
5893 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04005894 }
Chris Mason3de45862008-11-17 21:02:50 -05005895
Chris Mason5f39d392007-10-15 16:14:19 -04005896 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5897
5898 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04005899 break;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005900 if (found_key.type != BTRFS_DIR_INDEX_KEY)
Chris Mason39279cc2007-06-12 06:35:45 -04005901 break;
Al Viro9cdda8d2013-05-22 16:48:09 -04005902 if (found_key.offset < ctx->pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08005903 goto next;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005904 if (btrfs_should_delete_dir_index(&del_list, found_key.offset))
Miao Xie16cdcec2011-04-22 18:12:22 +08005905 goto next;
Chris Mason39279cc2007-06-12 06:35:45 -04005906 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005907 name_len = btrfs_dir_name_len(leaf, di);
Josef Bacik23b5ec72017-07-24 15:14:25 -04005908 if ((total_len + sizeof(struct dir_entry) + name_len) >=
5909 PAGE_SIZE) {
5910 btrfs_release_path(path);
5911 ret = btrfs_filldir(private->filldir_buf, entries, ctx);
5912 if (ret)
5913 goto nopos;
5914 addr = private->filldir_buf;
5915 entries = 0;
5916 total_len = 0;
5917 goto again;
Chris Mason39279cc2007-06-12 06:35:45 -04005918 }
Josef Bacik23b5ec72017-07-24 15:14:25 -04005919
5920 entry = addr;
David Sterba92d32172018-04-16 21:10:14 +02005921 put_unaligned(name_len, &entry->name_len);
Josef Bacik23b5ec72017-07-24 15:14:25 -04005922 name_ptr = (char *)(entry + 1);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005923 read_extent_buffer(leaf, name_ptr, (unsigned long)(di + 1),
5924 name_len);
David Sterba92d32172018-04-16 21:10:14 +02005925 put_unaligned(btrfs_filetype_table[btrfs_dir_type(leaf, di)],
5926 &entry->type);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005927 btrfs_dir_item_key_to_cpu(leaf, di, &location);
David Sterba92d32172018-04-16 21:10:14 +02005928 put_unaligned(location.objectid, &entry->ino);
5929 put_unaligned(found_key.offset, &entry->offset);
Josef Bacik23b5ec72017-07-24 15:14:25 -04005930 entries++;
5931 addr += sizeof(struct dir_entry) + name_len;
5932 total_len += sizeof(struct dir_entry) + name_len;
Li Zefanb9e03af2011-03-23 10:43:58 +08005933next:
5934 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04005935 }
Josef Bacik23b5ec72017-07-24 15:14:25 -04005936 btrfs_release_path(path);
5937
5938 ret = btrfs_filldir(private->filldir_buf, entries, ctx);
5939 if (ret)
5940 goto nopos;
David Woodhouse49593bf2008-08-17 17:08:36 +01005941
Jeff Mahoneyd2fbb2b2016-11-05 13:26:35 -04005942 ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005943 if (ret)
David Sterbabc4ef752015-11-13 13:44:28 +01005944 goto nopos;
5945
Zach Browndb62efb2013-07-11 16:19:42 -07005946 /*
5947 * Stop new entries from being returned after we return the last
5948 * entry.
5949 *
5950 * New directory entries are assigned a strictly increasing
5951 * offset. This means that new entries created during readdir
5952 * are *guaranteed* to be seen in the future by that readdir.
5953 * This has broken buggy programs which operate on names as
5954 * they're returned by readdir. Until we re-use freed offsets
5955 * we have this hack to stop new entries from being returned
5956 * under the assumption that they'll never reach this huge
5957 * offset.
5958 *
5959 * This is being careful not to overflow 32bit loff_t unless the
5960 * last entry requires it because doing so has broken 32bit apps
5961 * in the past.
5962 */
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005963 if (ctx->pos >= INT_MAX)
5964 ctx->pos = LLONG_MAX;
5965 else
5966 ctx->pos = INT_MAX;
Chris Mason39279cc2007-06-12 06:35:45 -04005967nopos:
5968 ret = 0;
5969err:
Omar Sandoval02dbfc92016-05-20 13:50:33 -07005970 if (put)
5971 btrfs_readdir_put_delayed_items(inode, &ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04005972 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005973 return ret;
5974}
5975
Chris Mason39279cc2007-06-12 06:35:45 -04005976/*
Chris Mason54aa1f42007-06-22 14:16:25 -04005977 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04005978 * inode changes. But, it is most likely to find the inode in cache.
5979 * FIXME, needs more benchmarking...there are no reasons other than performance
5980 * to keep or drop this code.
5981 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00005982static int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005983{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005984 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04005985 struct btrfs_root *root = BTRFS_I(inode)->root;
5986 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005987 int ret;
5988
Josef Bacik72ac3c02012-05-23 14:13:11 -04005989 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05005990 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005991
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005992 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005993 if (IS_ERR(trans))
5994 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005995
5996 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005997 if (ret && ret == -ENOSPC) {
5998 /* whoops, lets try again with the full transaction */
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005999 btrfs_end_transaction(trans);
Chris Mason94b60442010-05-26 11:02:00 -04006000 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006001 if (IS_ERR(trans))
6002 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006003
Chris Mason94b60442010-05-26 11:02:00 -04006004 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04006005 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006006 btrfs_end_transaction(trans);
Miao Xie16cdcec2011-04-22 18:12:22 +08006007 if (BTRFS_I(inode)->delayed_node)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006008 btrfs_balance_delayed_items(fs_info);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006009
6010 return ret;
6011}
6012
6013/*
6014 * This is a copy of file_update_time. We need this so we can return error on
6015 * ENOSPC for updating the inode in the case of file write and mmap writes.
6016 */
Deepa Dinamani95582b02018-05-08 19:36:02 -07006017static int btrfs_update_time(struct inode *inode, struct timespec64 *now,
Josef Bacike41f9412012-03-26 09:46:47 -04006018 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05006019{
Alexander Block2bc5565282012-06-15 09:49:33 +02006020 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Layton3a8c7232017-12-11 06:35:24 -05006021 bool dirty = flags & ~S_VERSION;
Alexander Block2bc5565282012-06-15 09:49:33 +02006022
6023 if (btrfs_root_readonly(root))
6024 return -EROFS;
6025
Josef Bacike41f9412012-03-26 09:46:47 -04006026 if (flags & S_VERSION)
Jeff Layton3a8c7232017-12-11 06:35:24 -05006027 dirty |= inode_maybe_inc_iversion(inode, dirty);
Josef Bacike41f9412012-03-26 09:46:47 -04006028 if (flags & S_CTIME)
6029 inode->i_ctime = *now;
6030 if (flags & S_MTIME)
6031 inode->i_mtime = *now;
6032 if (flags & S_ATIME)
6033 inode->i_atime = *now;
Jeff Layton3a8c7232017-12-11 06:35:24 -05006034 return dirty ? btrfs_dirty_inode(inode) : 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006035}
6036
Chris Masond352ac62008-09-29 15:18:18 -04006037/*
6038 * find the highest existing sequence number in a directory
6039 * and then set the in-memory index_cnt variable to reflect
6040 * free sequence numbers
6041 */
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006042static int btrfs_set_inode_index_count(struct btrfs_inode *inode)
Josef Bacikaec74772008-07-24 12:12:38 -04006043{
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006044 struct btrfs_root *root = inode->root;
Josef Bacikaec74772008-07-24 12:12:38 -04006045 struct btrfs_key key, found_key;
6046 struct btrfs_path *path;
6047 struct extent_buffer *leaf;
6048 int ret;
6049
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006050 key.objectid = btrfs_ino(inode);
David Sterba962a2982014-06-04 18:41:45 +02006051 key.type = BTRFS_DIR_INDEX_KEY;
Josef Bacikaec74772008-07-24 12:12:38 -04006052 key.offset = (u64)-1;
6053
6054 path = btrfs_alloc_path();
6055 if (!path)
6056 return -ENOMEM;
6057
6058 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
6059 if (ret < 0)
6060 goto out;
6061 /* FIXME: we should be able to handle this */
6062 if (ret == 0)
6063 goto out;
6064 ret = 0;
6065
6066 /*
6067 * MAGIC NUMBER EXPLANATION:
6068 * since we search a directory based on f_pos we have to start at 2
6069 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
6070 * else has to start at 2
6071 */
6072 if (path->slots[0] == 0) {
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006073 inode->index_cnt = 2;
Josef Bacikaec74772008-07-24 12:12:38 -04006074 goto out;
6075 }
6076
6077 path->slots[0]--;
6078
6079 leaf = path->nodes[0];
6080 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6081
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006082 if (found_key.objectid != btrfs_ino(inode) ||
David Sterba962a2982014-06-04 18:41:45 +02006083 found_key.type != BTRFS_DIR_INDEX_KEY) {
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006084 inode->index_cnt = 2;
Josef Bacikaec74772008-07-24 12:12:38 -04006085 goto out;
6086 }
6087
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006088 inode->index_cnt = found_key.offset + 1;
Josef Bacikaec74772008-07-24 12:12:38 -04006089out:
6090 btrfs_free_path(path);
6091 return ret;
6092}
6093
Chris Masond352ac62008-09-29 15:18:18 -04006094/*
6095 * helper to find a free sequence number in a given directory. This current
6096 * code is very simple, later versions will do smarter things in the btree
6097 */
Nikolay Borisov877574e2017-02-20 13:50:33 +02006098int btrfs_set_inode_index(struct btrfs_inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04006099{
6100 int ret = 0;
6101
Nikolay Borisov877574e2017-02-20 13:50:33 +02006102 if (dir->index_cnt == (u64)-1) {
6103 ret = btrfs_inode_delayed_dir_index_count(dir);
Miao Xie16cdcec2011-04-22 18:12:22 +08006104 if (ret) {
6105 ret = btrfs_set_inode_index_count(dir);
6106 if (ret)
6107 return ret;
6108 }
Josef Bacikaec74772008-07-24 12:12:38 -04006109 }
6110
Nikolay Borisov877574e2017-02-20 13:50:33 +02006111 *index = dir->index_cnt;
6112 dir->index_cnt++;
Josef Bacikaec74772008-07-24 12:12:38 -04006113
6114 return ret;
6115}
6116
Chris Masonb0d5d102014-09-08 13:08:51 -07006117static int btrfs_insert_inode_locked(struct inode *inode)
6118{
6119 struct btrfs_iget_args args;
6120 args.location = &BTRFS_I(inode)->location;
6121 args.root = BTRFS_I(inode)->root;
6122
6123 return insert_inode_locked4(inode,
6124 btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root),
6125 btrfs_find_actor, &args);
6126}
6127
Anand Jain19aee8d2017-07-18 17:37:05 +08006128/*
6129 * Inherit flags from the parent inode.
6130 *
6131 * Currently only the compression flags and the cow flags are inherited.
6132 */
6133static void btrfs_inherit_iflags(struct inode *inode, struct inode *dir)
6134{
6135 unsigned int flags;
6136
6137 if (!dir)
6138 return;
6139
6140 flags = BTRFS_I(dir)->flags;
6141
6142 if (flags & BTRFS_INODE_NOCOMPRESS) {
6143 BTRFS_I(inode)->flags &= ~BTRFS_INODE_COMPRESS;
6144 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
6145 } else if (flags & BTRFS_INODE_COMPRESS) {
6146 BTRFS_I(inode)->flags &= ~BTRFS_INODE_NOCOMPRESS;
6147 BTRFS_I(inode)->flags |= BTRFS_INODE_COMPRESS;
6148 }
6149
6150 if (flags & BTRFS_INODE_NODATACOW) {
6151 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
6152 if (S_ISREG(inode->i_mode))
6153 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
6154 }
6155
David Sterba7b6a2212018-03-26 18:40:21 +02006156 btrfs_sync_inode_flags_to_i_flags(inode);
Anand Jain19aee8d2017-07-18 17:37:05 +08006157}
6158
Chris Mason39279cc2007-06-12 06:35:45 -04006159static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
6160 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04006161 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05006162 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04006163 u64 ref_objectid, u64 objectid,
6164 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04006165{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006166 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04006167 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006168 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04006169 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04006170 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05006171 struct btrfs_inode_ref *ref;
6172 struct btrfs_key key[2];
6173 u32 sizes[2];
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006174 int nitems = name ? 2 : 1;
Chris Mason9c583092008-01-29 15:15:18 -05006175 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04006176 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006177
Chris Mason5f39d392007-10-15 16:14:19 -04006178 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07006179 if (!path)
6180 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04006181
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006182 inode = new_inode(fs_info->sb);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006183 if (!inode) {
6184 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04006185 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006186 }
Chris Mason39279cc2007-06-12 06:35:45 -04006187
Li Zefan581bb052011-04-20 10:06:11 +08006188 /*
Filipe Manana5762b5c2014-08-01 00:10:32 +01006189 * O_TMPFILE, set link count to 0, so that after this point,
6190 * we fill in an inode item with the correct link count.
6191 */
6192 if (!name)
6193 set_nlink(inode, 0);
6194
6195 /*
Li Zefan581bb052011-04-20 10:06:11 +08006196 * we have to initialize this early, so we can reclaim the inode
6197 * number if we fail afterwards in this function.
6198 */
6199 inode->i_ino = objectid;
6200
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006201 if (dir && name) {
liubo1abe9b82011-03-24 11:18:59 +00006202 trace_btrfs_inode_request(dir);
6203
Nikolay Borisov877574e2017-02-20 13:50:33 +02006204 ret = btrfs_set_inode_index(BTRFS_I(dir), index);
Shen Feng09771432009-04-02 16:46:06 -04006205 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006206 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04006207 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04006208 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04006209 }
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006210 } else if (dir) {
6211 *index = 0;
Josef Bacikaec74772008-07-24 12:12:38 -04006212 }
6213 /*
6214 * index_cnt is ignored for everything but a dir,
Su Yuedf6703e2018-01-12 11:08:02 +08006215 * btrfs_set_inode_index_count has an explanation for the magic
Josef Bacikaec74772008-07-24 12:12:38 -04006216 * number
6217 */
6218 BTRFS_I(inode)->index_cnt = 2;
Miao Xie67de1172013-12-26 13:07:06 +08006219 BTRFS_I(inode)->dir_index = *index;
Chris Mason39279cc2007-06-12 06:35:45 -04006220 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04006221 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00006222 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db22007-08-27 16:49:44 -04006223
Josef Bacik5dc562c2012-08-17 13:14:17 -04006224 /*
6225 * We could have gotten an inode number from somebody who was fsynced
6226 * and then removed in this same transaction, so let's just set full
6227 * sync since it will be a full sync anyway and this will blow away the
6228 * old info in the log.
6229 */
6230 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
6231
Chris Mason9c583092008-01-29 15:15:18 -05006232 key[0].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006233 key[0].type = BTRFS_INODE_ITEM_KEY;
Chris Mason9c583092008-01-29 15:15:18 -05006234 key[0].offset = 0;
6235
Chris Mason9c583092008-01-29 15:15:18 -05006236 sizes[0] = sizeof(struct btrfs_inode_item);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006237
6238 if (name) {
6239 /*
6240 * Start new inodes with an inode_ref. This is slightly more
6241 * efficient for small numbers of hard links since they will
6242 * be packed into one item. Extended refs will kick in if we
6243 * add more hard links than can fit in the ref item.
6244 */
6245 key[1].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006246 key[1].type = BTRFS_INODE_REF_KEY;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006247 key[1].offset = ref_objectid;
6248
6249 sizes[1] = name_len + sizeof(*ref);
6250 }
Chris Mason9c583092008-01-29 15:15:18 -05006251
Chris Masonb0d5d102014-09-08 13:08:51 -07006252 location = &BTRFS_I(inode)->location;
6253 location->objectid = objectid;
6254 location->offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02006255 location->type = BTRFS_INODE_ITEM_KEY;
Chris Masonb0d5d102014-09-08 13:08:51 -07006256
6257 ret = btrfs_insert_inode_locked(inode);
Al Viro32955c52018-05-16 12:20:05 -04006258 if (ret < 0) {
6259 iput(inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006260 goto fail;
Al Viro32955c52018-05-16 12:20:05 -04006261 }
Chris Masonb0d5d102014-09-08 13:08:51 -07006262
Chris Masonb9473432009-03-13 11:00:37 -04006263 path->leave_spinning = 1;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006264 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems);
Chris Mason9c583092008-01-29 15:15:18 -05006265 if (ret != 0)
Chris Masonb0d5d102014-09-08 13:08:51 -07006266 goto fail_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04006267
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03006268 inode_init_owner(inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04006269 inode_set_bytes(inode, 0);
chandan r9cc97d62012-07-04 12:48:07 +05306270
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006271 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05306272 inode->i_atime = inode->i_mtime;
6273 inode->i_ctime = inode->i_mtime;
Arnd Bergmannd3c6be6f2018-06-21 18:04:06 +02006274 BTRFS_I(inode)->i_otime = inode->i_mtime;
chandan r9cc97d62012-07-04 12:48:07 +05306275
Chris Mason5f39d392007-10-15 16:14:19 -04006276 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
6277 struct btrfs_inode_item);
David Sterbab159fa22016-11-08 18:09:03 +01006278 memzero_extent_buffer(path->nodes[0], (unsigned long)inode_item,
Li Zefan293f7e02012-07-10 00:58:58 -06006279 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04006280 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05006281
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006282 if (name) {
6283 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
6284 struct btrfs_inode_ref);
6285 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
6286 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
6287 ptr = (unsigned long)(ref + 1);
6288 write_extent_buffer(path->nodes[0], name, ptr, name_len);
6289 }
Chris Mason9c583092008-01-29 15:15:18 -05006290
Chris Mason5f39d392007-10-15 16:14:19 -04006291 btrfs_mark_buffer_dirty(path->nodes[0]);
6292 btrfs_free_path(path);
6293
Christoph Hellwig6cbff002009-04-17 10:37:41 +02006294 btrfs_inherit_iflags(inode, dir);
6295
Al Viro569254b2011-07-24 17:08:40 -04006296 if (S_ISREG(mode)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006297 if (btrfs_test_opt(fs_info, NODATASUM))
Chris Mason94272162009-07-02 12:26:06 -04006298 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006299 if (btrfs_test_opt(fs_info, NODATACOW))
Josef Bacikf2bdf9a2013-02-21 15:28:28 -05006300 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
6301 BTRFS_INODE_NODATASUM;
Chris Mason94272162009-07-02 12:26:06 -04006302 }
6303
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006304 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00006305
6306 trace_btrfs_inode_new(inode);
Chris Mason1973f0f2011-06-24 13:13:29 -04006307 btrfs_set_inode_last_trans(trans, inode);
liubo1abe9b82011-03-24 11:18:59 +00006308
Alexander Block8ea05e32012-07-25 17:35:53 +02006309 btrfs_update_root_times(trans, root);
6310
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006311 ret = btrfs_inode_inherit_props(trans, inode, dir);
6312 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006313 btrfs_err(fs_info,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006314 "error inheriting props for ino %llu (root %llu): %d",
David Sterbaf85b7372017-01-20 14:54:07 +01006315 btrfs_ino(BTRFS_I(inode)), root->root_key.objectid, ret);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006316
Chris Mason39279cc2007-06-12 06:35:45 -04006317 return inode;
Chris Masonb0d5d102014-09-08 13:08:51 -07006318
6319fail_unlock:
Al Viro32955c52018-05-16 12:20:05 -04006320 discard_new_inode(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006321fail:
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006322 if (dir && name)
Josef Bacikaec74772008-07-24 12:12:38 -04006323 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04006324 btrfs_free_path(path);
6325 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006326}
6327
6328static inline u8 btrfs_inode_type(struct inode *inode)
6329{
6330 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
6331}
6332
Chris Masond352ac62008-09-29 15:18:18 -04006333/*
6334 * utility function to add 'inode' into 'parent_inode' with
6335 * a give name and a given sequence number.
6336 * if 'add_backref' is true, also insert a backref from the
6337 * inode to the parent directory.
6338 */
Chris Masone02119d2008-09-05 16:13:11 -04006339int btrfs_add_link(struct btrfs_trans_handle *trans,
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006340 struct btrfs_inode *parent_inode, struct btrfs_inode *inode,
Chris Masone02119d2008-09-05 16:13:11 -04006341 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006342{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006343 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006344 struct btrfs_key key;
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006345 struct btrfs_root *root = parent_inode->root;
6346 u64 ino = btrfs_ino(inode);
6347 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006348
Li Zefan33345d012011-04-20 10:31:50 +08006349 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006350 memcpy(&key, &inode->root->root_key, sizeof(key));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006351 } else {
Li Zefan33345d012011-04-20 10:31:50 +08006352 key.objectid = ino;
David Sterba962a2982014-06-04 18:41:45 +02006353 key.type = BTRFS_INODE_ITEM_KEY;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006354 key.offset = 0;
6355 }
Chris Mason39279cc2007-06-12 06:35:45 -04006356
Li Zefan33345d012011-04-20 10:31:50 +08006357 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Lu Fengqi6025c192018-08-01 11:32:29 +08006358 ret = btrfs_add_root_ref(trans, key.objectid,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006359 root->root_key.objectid, parent_ino,
6360 index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006361 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08006362 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
6363 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006364 }
6365
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006366 /* Nothing to clean up yet */
6367 if (ret)
6368 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006369
Lu Fengqi684572d2018-08-04 21:10:57 +08006370 ret = btrfs_insert_dir_item(trans, name, name_len, parent_inode, &key,
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006371 btrfs_inode_type(&inode->vfs_inode), index);
Chris Mason9c520572012-12-17 14:26:57 -05006372 if (ret == -EEXIST || ret == -EOVERFLOW)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006373 goto fail_dir_item;
6374 else if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04006375 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006376 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006377 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006378
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006379 btrfs_i_size_write(parent_inode, parent_inode->vfs_inode.i_size +
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006380 name_len * 2);
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006381 inode_inc_iversion(&parent_inode->vfs_inode);
6382 parent_inode->vfs_inode.i_mtime = parent_inode->vfs_inode.i_ctime =
6383 current_time(&parent_inode->vfs_inode);
6384 ret = btrfs_update_inode(trans, root, &parent_inode->vfs_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006385 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04006386 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006387 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05006388
6389fail_dir_item:
6390 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
6391 u64 local_index;
6392 int err;
Lu Fengqi3ee1c552018-08-01 11:32:28 +08006393 err = btrfs_del_root_ref(trans, key.objectid,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006394 root->root_key.objectid, parent_ino,
6395 &local_index, name, name_len);
Chris Masonfe66a052012-02-20 08:40:56 -05006396
6397 } else if (add_backref) {
6398 u64 local_index;
6399 int err;
6400
6401 err = btrfs_del_inode_ref(trans, root, name, name_len,
6402 ino, parent_ino, &local_index);
6403 }
6404 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006405}
6406
6407static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006408 struct btrfs_inode *dir, struct dentry *dentry,
6409 struct btrfs_inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006410{
Josef Bacika1b075d2010-11-19 20:36:11 +00006411 int err = btrfs_add_link(trans, dir, inode,
6412 dentry->d_name.name, dentry->d_name.len,
6413 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006414 if (err > 0)
6415 err = -EEXIST;
6416 return err;
6417}
6418
Josef Bacik618e21d2007-07-11 10:18:17 -04006419static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04006420 umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04006421{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006422 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Josef Bacik618e21d2007-07-11 10:18:17 -04006423 struct btrfs_trans_handle *trans;
6424 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006425 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04006426 int err;
Josef Bacik618e21d2007-07-11 10:18:17 -04006427 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006428 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04006429
Josef Bacik9ed74f22009-09-11 16:12:44 -04006430 /*
6431 * 2 for inode item and ref
6432 * 2 for dir items
6433 * 1 for xattr if selinux is on
6434 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006435 trans = btrfs_start_transaction(root, 5);
6436 if (IS_ERR(trans))
6437 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05006438
Li Zefan581bb052011-04-20 10:06:11 +08006439 err = btrfs_find_free_ino(root, &objectid);
6440 if (err)
6441 goto out_unlock;
6442
Josef Bacikaec74772008-07-24 12:12:38 -04006443 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006444 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6445 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006446 if (IS_ERR(inode)) {
6447 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -04006448 inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04006449 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006450 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006451
Casey Schauflerad19db72011-12-15 10:09:07 -05006452 /*
6453 * If the active LSM wants to access the inode during
6454 * d_instantiate it needs these. Smack checks to see
6455 * if the filesystem supports xattrs by looking at the
6456 * ops vector.
6457 */
Casey Schauflerad19db72011-12-15 10:09:07 -05006458 inode->i_op = &btrfs_special_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006459 init_special_inode(inode, inode->i_mode, rdev);
6460
6461 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik618e21d2007-07-11 10:18:17 -04006462 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006463 goto out_unlock;
Chris Masonb0d5d102014-09-08 13:08:51 -07006464
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006465 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6466 0, index);
Al Viro32955c52018-05-16 12:20:05 -04006467 if (err)
6468 goto out_unlock;
6469
6470 btrfs_update_inode(trans, root, inode);
6471 d_instantiate_new(dentry, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006472
Josef Bacik618e21d2007-07-11 10:18:17 -04006473out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006474 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006475 btrfs_btree_balance_dirty(fs_info);
Al Viro32955c52018-05-16 12:20:05 -04006476 if (err && inode) {
Josef Bacik618e21d2007-07-11 10:18:17 -04006477 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -04006478 discard_new_inode(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006479 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006480 return err;
6481}
6482
Chris Mason39279cc2007-06-12 06:35:45 -04006483static int btrfs_create(struct inode *dir, struct dentry *dentry,
Al Viroebfc3b42012-06-10 18:05:36 -04006484 umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -04006485{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006486 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04006487 struct btrfs_trans_handle *trans;
6488 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006489 struct inode *inode = NULL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006490 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04006491 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006492 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006493
Josef Bacik9ed74f22009-09-11 16:12:44 -04006494 /*
6495 * 2 for inode item and ref
6496 * 2 for dir items
6497 * 1 for xattr if selinux is on
6498 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006499 trans = btrfs_start_transaction(root, 5);
6500 if (IS_ERR(trans))
6501 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006502
Li Zefan581bb052011-04-20 10:06:11 +08006503 err = btrfs_find_free_ino(root, &objectid);
6504 if (err)
6505 goto out_unlock;
6506
Josef Bacikaec74772008-07-24 12:12:38 -04006507 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006508 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6509 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006510 if (IS_ERR(inode)) {
6511 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -04006512 inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006513 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006514 }
Casey Schauflerad19db72011-12-15 10:09:07 -05006515 /*
6516 * If the active LSM wants to access the inode during
6517 * d_instantiate it needs these. Smack checks to see
6518 * if the filesystem supports xattrs by looking at the
6519 * ops vector.
6520 */
6521 inode->i_fop = &btrfs_file_operations;
6522 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006523 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006524
6525 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
6526 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006527 goto out_unlock;
Chris Masonb0d5d102014-09-08 13:08:51 -07006528
6529 err = btrfs_update_inode(trans, root, inode);
6530 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006531 goto out_unlock;
Casey Schauflerad19db72011-12-15 10:09:07 -05006532
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006533 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6534 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006535 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006536 goto out_unlock;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006537
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006538 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Al Viro1e2e5472018-05-04 08:23:01 -04006539 d_instantiate_new(dentry, inode);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006540
Chris Mason39279cc2007-06-12 06:35:45 -04006541out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006542 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -04006543 if (err && inode) {
Chris Mason39279cc2007-06-12 06:35:45 -04006544 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -04006545 discard_new_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006546 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006547 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006548 return err;
6549}
6550
6551static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
6552 struct dentry *dentry)
6553{
Filipe Manana271dba452016-01-05 16:24:05 +00006554 struct btrfs_trans_handle *trans = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006555 struct btrfs_root *root = BTRFS_I(dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00006556 struct inode *inode = d_inode(old_dentry);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006557 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason00e4e6b2008-08-05 11:18:09 -04006558 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04006559 int err;
6560 int drop_inode = 0;
6561
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006562 /* do not allow sys_link's with other subvols of the same device */
Misono Tomohiro4fd786e2018-08-06 14:25:24 +09006563 if (root->root_key.objectid != BTRFS_I(inode)->root->root_key.objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00006564 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006565
Mark Fashehf1863732012-08-08 11:32:27 -07006566 if (inode->i_nlink >= BTRFS_LINK_MAX)
Al Viroc055e992011-03-04 17:15:18 +00006567 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04006568
Nikolay Borisov877574e2017-02-20 13:50:33 +02006569 err = btrfs_set_inode_index(BTRFS_I(dir), &index);
Josef Bacikaec74772008-07-24 12:12:38 -04006570 if (err)
6571 goto fail;
6572
Yan, Zhenga22285a2010-05-16 10:48:46 -04006573 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00006574 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04006575 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00006576 * 1 item for parent inode
Omar Sandoval399b0bb2018-05-11 13:13:40 -07006577 * 1 item for orphan item deletion if O_TMPFILE
Yan, Zhenga22285a2010-05-16 10:48:46 -04006578 */
Omar Sandoval399b0bb2018-05-11 13:13:40 -07006579 trans = btrfs_start_transaction(root, inode->i_nlink ? 5 : 6);
Yan, Zhenga22285a2010-05-16 10:48:46 -04006580 if (IS_ERR(trans)) {
6581 err = PTR_ERR(trans);
Filipe Manana271dba452016-01-05 16:24:05 +00006582 trans = NULL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006583 goto fail;
6584 }
Chris Mason5f39d392007-10-15 16:14:19 -04006585
Miao Xie67de1172013-12-26 13:07:06 +08006586 /* There are several dir indexes for this inode, clear the cache. */
6587 BTRFS_I(inode)->dir_index = 0ULL;
Zach Brown8b558c52013-10-16 12:10:34 -07006588 inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04006589 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006590 inode->i_ctime = current_time(inode);
Al Viro7de9c6ee2010-10-23 11:11:40 -04006591 ihold(inode);
Josef Bacike9976152012-10-11 15:53:56 -04006592 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
Josef Bacikaec74772008-07-24 12:12:38 -04006593
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006594 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6595 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04006596
Yan, Zhenga5719522009-09-24 09:17:31 -04006597 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006598 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04006599 } else {
Al Viro10d9f302011-07-16 23:09:10 -04006600 struct dentry *parent = dentry->d_parent;
Filipe Mananad4682ba2018-06-11 19:24:28 +01006601 int ret;
6602
Yan, Zhenga5719522009-09-24 09:17:31 -04006603 err = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006604 if (err)
6605 goto fail;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006606 if (inode->i_nlink == 1) {
6607 /*
6608 * If new hard link count is 1, it's a file created
6609 * with open(2) O_TMPFILE flag.
6610 */
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02006611 err = btrfs_orphan_del(trans, BTRFS_I(inode));
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006612 if (err)
6613 goto fail;
6614 }
Filipe Manana41bd6062018-11-28 14:54:28 +00006615 BTRFS_I(inode)->last_link_trans = trans->transid;
Al Viro08c422c2011-12-23 07:58:13 -05006616 d_instantiate(dentry, inode);
Filipe Mananad4682ba2018-06-11 19:24:28 +01006617 ret = btrfs_log_new_name(trans, BTRFS_I(inode), NULL, parent,
6618 true, NULL);
6619 if (ret == BTRFS_NEED_TRANS_COMMIT) {
6620 err = btrfs_commit_transaction(trans);
6621 trans = NULL;
6622 }
Yan, Zhenga5719522009-09-24 09:17:31 -04006623 }
Chris Mason39279cc2007-06-12 06:35:45 -04006624
Chris Mason1832a6d2007-12-21 16:27:21 -05006625fail:
Filipe Manana271dba452016-01-05 16:24:05 +00006626 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006627 btrfs_end_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006628 if (drop_inode) {
6629 inode_dec_link_count(inode);
6630 iput(inode);
6631 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006632 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006633 return err;
6634}
6635
Al Viro18bb1db2011-07-26 01:41:39 -04006636static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04006637{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006638 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masonb9d86662008-05-02 16:13:49 -04006639 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006640 struct btrfs_trans_handle *trans;
6641 struct btrfs_root *root = BTRFS_I(dir)->root;
6642 int err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04006643 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006644 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006645
Josef Bacik9ed74f22009-09-11 16:12:44 -04006646 /*
6647 * 2 items for inode and ref
6648 * 2 items for dir items
6649 * 1 for xattr if selinux is on
6650 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006651 trans = btrfs_start_transaction(root, 5);
6652 if (IS_ERR(trans))
6653 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006654
Li Zefan581bb052011-04-20 10:06:11 +08006655 err = btrfs_find_free_ino(root, &objectid);
6656 if (err)
6657 goto out_fail;
6658
Josef Bacikaec74772008-07-24 12:12:38 -04006659 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006660 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6661 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04006662 if (IS_ERR(inode)) {
6663 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -04006664 inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006665 goto out_fail;
6666 }
Chris Mason5f39d392007-10-15 16:14:19 -04006667
Chris Masonb0d5d102014-09-08 13:08:51 -07006668 /* these must be set before we unlock the inode */
6669 inode->i_op = &btrfs_dir_inode_operations;
6670 inode->i_fop = &btrfs_dir_file_operations;
Josef Bacik33268ea2008-07-24 12:16:36 -04006671
Eric Paris2a7dba32011-02-01 11:05:39 -05006672 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04006673 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006674 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04006675
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02006676 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Mason39279cc2007-06-12 06:35:45 -04006677 err = btrfs_update_inode(trans, root, inode);
6678 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006679 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04006680
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006681 err = btrfs_add_link(trans, BTRFS_I(dir), BTRFS_I(inode),
6682 dentry->d_name.name,
6683 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006684 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006685 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04006686
Al Viro1e2e5472018-05-04 08:23:01 -04006687 d_instantiate_new(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006688
6689out_fail:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006690 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -04006691 if (err && inode) {
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006692 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -04006693 discard_new_inode(inode);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006694 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006695 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006696 return err;
6697}
6698
Chris Masonc8b97812008-10-29 14:49:59 -04006699static noinline int uncompress_inline(struct btrfs_path *path,
Byongho Leee40da0e2015-05-19 23:46:45 +09006700 struct page *page,
Chris Masonc8b97812008-10-29 14:49:59 -04006701 size_t pg_offset, u64 extent_offset,
6702 struct btrfs_file_extent_item *item)
6703{
6704 int ret;
6705 struct extent_buffer *leaf = path->nodes[0];
6706 char *tmp;
6707 size_t max_size;
6708 unsigned long inline_size;
6709 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08006710 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04006711
6712 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08006713 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04006714 max_size = btrfs_file_extent_ram_bytes(leaf, item);
6715 inline_size = btrfs_file_extent_inline_item_len(leaf,
Ross Kirkdd3cc162013-09-16 15:58:09 +01006716 btrfs_item_nr(path->slots[0]));
Chris Masonc8b97812008-10-29 14:49:59 -04006717 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04006718 if (!tmp)
6719 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04006720 ptr = btrfs_file_extent_inline_start(item);
6721
6722 read_extent_buffer(leaf, tmp, ptr, inline_size);
6723
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006724 max_size = min_t(unsigned long, PAGE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08006725 ret = btrfs_decompress(compress_type, tmp, page,
6726 extent_offset, inline_size, max_size);
Zygo Blaxelle1699d22017-03-10 16:45:44 -05006727
6728 /*
6729 * decompression code contains a memset to fill in any space between the end
6730 * of the uncompressed data and the end of max_size in case the decompressed
6731 * data ends up shorter than ram_bytes. That doesn't cover the hole between
6732 * the end of an inline extent and the beginning of the next block, so we
6733 * cover that region here.
6734 */
6735
6736 if (max_size + pg_offset < PAGE_SIZE) {
6737 char *map = kmap(page);
6738 memset(map + pg_offset + max_size, 0, PAGE_SIZE - max_size - pg_offset);
6739 kunmap(page);
6740 }
Chris Masonc8b97812008-10-29 14:49:59 -04006741 kfree(tmp);
Zach Brown166ae5a2014-05-09 17:15:10 -04006742 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -04006743}
6744
Chris Masond352ac62008-09-29 15:18:18 -04006745/*
6746 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05006747 * the ugly parts come from merging extents from the disk with the in-ram
6748 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04006749 * where the in-ram extents might be locked pending data=ordered completion.
6750 *
6751 * This also copies inline extents directly into the page.
6752 */
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006753struct extent_map *btrfs_get_extent(struct btrfs_inode *inode,
Liu Bode2c6612018-08-25 13:47:59 +08006754 struct page *page,
6755 size_t pg_offset, u64 start, u64 len,
6756 int create)
Chris Masona52d9a82007-08-27 16:49:44 -04006757{
David Sterba3ffbd682018-06-29 10:56:42 +02006758 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Chris Masona52d9a82007-08-27 16:49:44 -04006759 int ret;
6760 int err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006761 u64 extent_start = 0;
6762 u64 extent_end = 0;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006763 u64 objectid = btrfs_ino(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04006764 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04006765 struct btrfs_path *path = NULL;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006766 struct btrfs_root *root = inode->root;
Chris Masona52d9a82007-08-27 16:49:44 -04006767 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04006768 struct extent_buffer *leaf;
6769 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04006770 struct extent_map *em = NULL;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006771 struct extent_map_tree *em_tree = &inode->extent_tree;
6772 struct extent_io_tree *io_tree = &inode->io_tree;
Filipe Manana7ffbb592014-06-09 03:48:05 +01006773 const bool new_inline = !page || create;
Chris Masona52d9a82007-08-27 16:49:44 -04006774
Chris Mason890871b2009-09-02 16:24:52 -04006775 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006776 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04006777 if (em)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006778 em->bdev = fs_info->fs_devices->latest_bdev;
Chris Mason890871b2009-09-02 16:24:52 -04006779 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006780
Chris Masona52d9a82007-08-27 16:49:44 -04006781 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04006782 if (em->start > start || em->start + em->len <= start)
6783 free_extent_map(em);
6784 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05006785 free_extent_map(em);
6786 else
6787 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006788 }
David Sterba172ddd62011-04-21 00:48:27 +02006789 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04006790 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05006791 err = -ENOMEM;
6792 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006793 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006794 em->bdev = fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05006795 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05006796 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05006797 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04006798 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04006799
Liu Bobee6ec82018-08-17 05:05:28 +08006800 path = btrfs_alloc_path();
Chris Masonf4219502008-07-22 11:18:09 -04006801 if (!path) {
Liu Bobee6ec82018-08-17 05:05:28 +08006802 err = -ENOMEM;
6803 goto out;
Chris Masonf4219502008-07-22 11:18:09 -04006804 }
6805
Liu Bobee6ec82018-08-17 05:05:28 +08006806 /* Chances are we'll be called again, so go ahead and do readahead */
6807 path->reada = READA_FORWARD;
6808
Liu Boe49aabd2018-08-25 13:47:09 +08006809 /*
6810 * Unless we're going to uncompress the inline extent, no sleep would
6811 * happen.
6812 */
6813 path->leave_spinning = 1;
6814
Nikolay Borisov5c9a7022017-12-01 11:19:40 +02006815 ret = btrfs_lookup_file_extent(NULL, root, path, objectid, start, 0);
Chris Masona52d9a82007-08-27 16:49:44 -04006816 if (ret < 0) {
6817 err = ret;
6818 goto out;
6819 }
6820
6821 if (ret != 0) {
6822 if (path->slots[0] == 0)
6823 goto not_found;
6824 path->slots[0]--;
6825 }
6826
Chris Mason5f39d392007-10-15 16:14:19 -04006827 leaf = path->nodes[0];
6828 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04006829 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04006830 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04006831 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02006832 found_type = found_key.type;
Chris Mason5f39d392007-10-15 16:14:19 -04006833 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04006834 found_type != BTRFS_EXTENT_DATA_KEY) {
Josef Bacik25a50342013-10-14 12:08:38 -04006835 /*
6836 * If we backup past the first extent we want to move forward
6837 * and see if there is an extent in front of us, otherwise we'll
6838 * say there is a hole for our whole search range which can
6839 * cause problems.
6840 */
6841 extent_end = start;
6842 goto next;
Chris Masona52d9a82007-08-27 16:49:44 -04006843 }
6844
Chris Mason5f39d392007-10-15 16:14:19 -04006845 found_type = btrfs_file_extent_type(leaf, item);
6846 extent_start = found_key.offset;
Yan Zhengd899e052008-10-30 14:25:28 -04006847 if (found_type == BTRFS_FILE_EXTENT_REG ||
6848 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006849 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04006850 btrfs_file_extent_num_bytes(leaf, item);
Liu Bo09ed2f12017-03-10 11:09:48 -08006851
6852 trace_btrfs_get_extent_show_fi_regular(inode, leaf, item,
6853 extent_start);
Yan Zheng9036c102008-10-30 14:19:41 -04006854 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
6855 size_t size;
Qu Wenruoe41ca582018-06-06 15:41:49 +08006856
6857 size = btrfs_file_extent_ram_bytes(leaf, item);
Jeff Mahoneyda170662016-06-15 09:22:56 -04006858 extent_end = ALIGN(extent_start + size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006859 fs_info->sectorsize);
Liu Bo09ed2f12017-03-10 11:09:48 -08006860
6861 trace_btrfs_get_extent_show_fi_inline(inode, leaf, item,
6862 path->slots[0],
6863 extent_start);
Yan Zheng9036c102008-10-30 14:19:41 -04006864 }
Josef Bacik25a50342013-10-14 12:08:38 -04006865next:
Yan Zheng9036c102008-10-30 14:19:41 -04006866 if (start >= extent_end) {
6867 path->slots[0]++;
6868 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
6869 ret = btrfs_next_leaf(root, path);
6870 if (ret < 0) {
6871 err = ret;
6872 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006873 }
Yan Zheng9036c102008-10-30 14:19:41 -04006874 if (ret > 0)
6875 goto not_found;
6876 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04006877 }
Yan Zheng9036c102008-10-30 14:19:41 -04006878 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6879 if (found_key.objectid != objectid ||
6880 found_key.type != BTRFS_EXTENT_DATA_KEY)
6881 goto not_found;
6882 if (start + len <= found_key.offset)
6883 goto not_found;
Wang Shilonge2eca692014-07-17 11:44:14 +08006884 if (start > found_key.offset)
6885 goto next;
Yan Zheng9036c102008-10-30 14:19:41 -04006886 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006887 em->orig_start = start;
Yan Zheng9036c102008-10-30 14:19:41 -04006888 em->len = found_key.offset - start;
6889 goto not_found_em;
6890 }
6891
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006892 btrfs_extent_item_to_extent_map(inode, path, item,
Nikolay Borisov9cdc5122017-02-20 13:51:02 +02006893 new_inline, em);
Filipe Manana7ffbb592014-06-09 03:48:05 +01006894
Yan Zhengd899e052008-10-30 14:25:28 -04006895 if (found_type == BTRFS_FILE_EXTENT_REG ||
6896 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006897 goto insert;
6898 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04006899 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04006900 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04006901 size_t size;
6902 size_t extent_offset;
6903 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04006904
Filipe Manana7ffbb592014-06-09 03:48:05 +01006905 if (new_inline)
Yan689f9342007-10-29 11:41:07 -04006906 goto out;
Yan689f9342007-10-29 11:41:07 -04006907
Qu Wenruoe41ca582018-06-06 15:41:49 +08006908 size = btrfs_file_extent_ram_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04006909 extent_offset = page_offset(page) + pg_offset - extent_start;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006910 copy_size = min_t(u64, PAGE_SIZE - pg_offset,
6911 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04006912 em->start = extent_start + extent_offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006913 em->len = ALIGN(copy_size, fs_info->sectorsize);
Josef Bacikb4939682012-12-03 10:31:19 -05006914 em->orig_block_len = em->len;
Josef Bacik70c8a912012-10-11 16:54:30 -04006915 em->orig_start = em->start;
Yan689f9342007-10-29 11:41:07 -04006916 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Liu Boe49aabd2018-08-25 13:47:09 +08006917
6918 btrfs_set_path_blocking(path);
Edmund Nadolskibf46f522017-11-20 13:24:49 -07006919 if (!PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08006920 if (btrfs_file_extent_compression(leaf, item) !=
6921 BTRFS_COMPRESS_NONE) {
Byongho Leee40da0e2015-05-19 23:46:45 +09006922 ret = uncompress_inline(path, page, pg_offset,
Chris Masonc8b97812008-10-29 14:49:59 -04006923 extent_offset, item);
Zach Brown166ae5a2014-05-09 17:15:10 -04006924 if (ret) {
6925 err = ret;
6926 goto out;
6927 }
Chris Masonc8b97812008-10-29 14:49:59 -04006928 } else {
6929 map = kmap(page);
6930 read_extent_buffer(leaf, map + pg_offset, ptr,
6931 copy_size);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006932 if (pg_offset + copy_size < PAGE_SIZE) {
Chris Mason93c82d52009-09-11 12:36:29 -04006933 memset(map + pg_offset + copy_size, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006934 PAGE_SIZE - pg_offset -
Chris Mason93c82d52009-09-11 12:36:29 -04006935 copy_size);
6936 }
Chris Masonc8b97812008-10-29 14:49:59 -04006937 kunmap(page);
6938 }
Chris Mason179e29e2007-11-01 11:28:41 -04006939 flush_dcache_page(page);
Chris Masona52d9a82007-08-27 16:49:44 -04006940 }
Chris Masond1310b22008-01-24 16:13:08 -05006941 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00006942 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04006943 goto insert;
Chris Masona52d9a82007-08-27 16:49:44 -04006944 }
6945not_found:
6946 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006947 em->orig_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05006948 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04006949not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04006950 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04006951insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02006952 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05006953 if (em->start > start || extent_map_end(em) <= start) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006954 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04006955 "bad extent! em: [%llu %llu] passed [%llu %llu]",
6956 em->start, em->len, start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04006957 err = -EIO;
6958 goto out;
6959 }
Chris Masond1310b22008-01-24 16:13:08 -05006960
6961 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04006962 write_lock(&em_tree->lock);
David Sterbaf46b24c2018-04-03 21:45:57 +02006963 err = btrfs_add_extent_mapping(fs_info, em_tree, &em, start, len);
Chris Mason890871b2009-09-02 16:24:52 -04006964 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04006965out:
Liu Boc6414282018-08-23 07:36:17 +08006966 btrfs_free_path(path);
liubo1abe9b82011-03-24 11:18:59 +00006967
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006968 trace_btrfs_get_extent(root, inode, em);
liubo1abe9b82011-03-24 11:18:59 +00006969
Chris Masona52d9a82007-08-27 16:49:44 -04006970 if (err) {
6971 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04006972 return ERR_PTR(err);
6973 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006974 BUG_ON(!em); /* Error is always set */
Chris Masona52d9a82007-08-27 16:49:44 -04006975 return em;
6976}
6977
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006978struct extent_map *btrfs_get_extent_fiemap(struct btrfs_inode *inode,
6979 struct page *page,
6980 size_t pg_offset, u64 start, u64 len,
6981 int create)
Chris Masonec29ed52011-02-23 16:23:20 -05006982{
6983 struct extent_map *em;
6984 struct extent_map *hole_em = NULL;
6985 u64 range_start = start;
6986 u64 end;
6987 u64 found;
6988 u64 found_end;
6989 int err = 0;
6990
6991 em = btrfs_get_extent(inode, page, pg_offset, start, len, create);
6992 if (IS_ERR(em))
6993 return em;
Dan Carpenter99862772017-04-11 11:57:15 +03006994 /*
6995 * If our em maps to:
6996 * - a hole or
6997 * - a pre-alloc extent,
6998 * there might actually be delalloc bytes behind it.
6999 */
7000 if (em->block_start != EXTENT_MAP_HOLE &&
7001 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7002 return em;
7003 else
7004 hole_em = em;
Chris Masonec29ed52011-02-23 16:23:20 -05007005
7006 /* check to see if we've wrapped (len == -1 or similar) */
7007 end = start + len;
7008 if (end < start)
7009 end = (u64)-1;
7010 else
7011 end -= 1;
7012
7013 em = NULL;
7014
7015 /* ok, we didn't find anything, lets look for delalloc */
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007016 found = count_range_bits(&inode->io_tree, &range_start,
Chris Masonec29ed52011-02-23 16:23:20 -05007017 end, len, EXTENT_DELALLOC, 1);
7018 found_end = range_start + found;
7019 if (found_end < range_start)
7020 found_end = (u64)-1;
7021
7022 /*
7023 * we didn't find anything useful, return
7024 * the original results from get_extent()
7025 */
7026 if (range_start > end || found_end <= start) {
7027 em = hole_em;
7028 hole_em = NULL;
7029 goto out;
7030 }
7031
7032 /* adjust the range_start to make sure it doesn't
7033 * go backwards from the start they passed in
7034 */
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05307035 range_start = max(start, range_start);
Chris Masonec29ed52011-02-23 16:23:20 -05007036 found = found_end - range_start;
7037
7038 if (found > 0) {
7039 u64 hole_start = start;
7040 u64 hole_len = len;
7041
David Sterba172ddd62011-04-21 00:48:27 +02007042 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05007043 if (!em) {
7044 err = -ENOMEM;
7045 goto out;
7046 }
7047 /*
7048 * when btrfs_get_extent can't find anything it
7049 * returns one huge hole
7050 *
7051 * make sure what it found really fits our range, and
7052 * adjust to make sure it is based on the start from
7053 * the caller
7054 */
7055 if (hole_em) {
7056 u64 calc_end = extent_map_end(hole_em);
7057
7058 if (calc_end <= start || (hole_em->start > end)) {
7059 free_extent_map(hole_em);
7060 hole_em = NULL;
7061 } else {
7062 hole_start = max(hole_em->start, start);
7063 hole_len = calc_end - hole_start;
7064 }
7065 }
7066 em->bdev = NULL;
7067 if (hole_em && range_start > hole_start) {
7068 /* our hole starts before our delalloc, so we
7069 * have to return just the parts of the hole
7070 * that go until the delalloc starts
7071 */
7072 em->len = min(hole_len,
7073 range_start - hole_start);
7074 em->start = hole_start;
7075 em->orig_start = hole_start;
7076 /*
7077 * don't adjust block start at all,
7078 * it is fixed at EXTENT_MAP_HOLE
7079 */
7080 em->block_start = hole_em->block_start;
7081 em->block_len = hole_len;
Liu Bof9e4fb52013-01-07 10:10:12 +00007082 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
7083 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonec29ed52011-02-23 16:23:20 -05007084 } else {
7085 em->start = range_start;
7086 em->len = found;
7087 em->orig_start = range_start;
7088 em->block_start = EXTENT_MAP_DELALLOC;
7089 em->block_len = found;
7090 }
Nikolay Borisovbf8d32b2017-12-01 11:19:43 +02007091 } else {
Chris Masonec29ed52011-02-23 16:23:20 -05007092 return hole_em;
7093 }
7094out:
7095
7096 free_extent_map(hole_em);
7097 if (err) {
7098 free_extent_map(em);
7099 return ERR_PTR(err);
7100 }
7101 return em;
7102}
7103
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007104static struct extent_map *btrfs_create_dio_extent(struct inode *inode,
7105 const u64 start,
7106 const u64 len,
7107 const u64 orig_start,
7108 const u64 block_start,
7109 const u64 block_len,
7110 const u64 orig_block_len,
7111 const u64 ram_bytes,
7112 const int type)
7113{
7114 struct extent_map *em = NULL;
7115 int ret;
7116
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007117 if (type != BTRFS_ORDERED_NOCOW) {
Liu Bo6f9994d2017-01-31 07:50:22 -08007118 em = create_io_em(inode, start, len, orig_start,
7119 block_start, block_len, orig_block_len,
7120 ram_bytes,
7121 BTRFS_COMPRESS_NONE, /* compress_type */
7122 type);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007123 if (IS_ERR(em))
7124 goto out;
7125 }
7126 ret = btrfs_add_ordered_extent_dio(inode, start, block_start,
7127 len, block_len, type);
7128 if (ret) {
7129 if (em) {
7130 free_extent_map(em);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02007131 btrfs_drop_extent_cache(BTRFS_I(inode), start,
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007132 start + len - 1, 0);
7133 }
7134 em = ERR_PTR(ret);
7135 }
7136 out:
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007137
7138 return em;
7139}
7140
Josef Bacik4b46fce2010-05-23 11:00:55 -04007141static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
7142 u64 start, u64 len)
7143{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007144 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007145 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik70c8a912012-10-11 16:54:30 -04007146 struct extent_map *em;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007147 struct btrfs_key ins;
7148 u64 alloc_hint;
7149 int ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007150
Josef Bacik4b46fce2010-05-23 11:00:55 -04007151 alloc_hint = get_extent_allocation_hint(inode, start, len);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007152 ret = btrfs_reserve_extent(root, len, len, fs_info->sectorsize,
Jeff Mahoneyda170662016-06-15 09:22:56 -04007153 0, alloc_hint, &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007154 if (ret)
7155 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007156
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007157 em = btrfs_create_dio_extent(inode, start, ins.offset, start,
7158 ins.objectid, ins.offset, ins.offset,
Liu Bo6288d6e2017-02-21 12:12:58 -08007159 ins.offset, BTRFS_ORDERED_REGULAR);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007160 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007161 if (IS_ERR(em))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007162 btrfs_free_reserved_extent(fs_info, ins.objectid,
7163 ins.offset, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007164
Josef Bacik4b46fce2010-05-23 11:00:55 -04007165 return em;
7166}
7167
Chris Mason46bfbb52010-05-26 11:04:10 -04007168/*
7169 * returns 1 when the nocow is safe, < 1 on error, 0 if the
7170 * block must be cow'd
7171 */
Josef Bacik00361582013-08-14 14:02:47 -04007172noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
Josef Bacik7ee9e442013-06-21 16:37:03 -04007173 u64 *orig_start, u64 *orig_block_len,
7174 u64 *ram_bytes)
Chris Mason46bfbb52010-05-26 11:04:10 -04007175{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007176 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason46bfbb52010-05-26 11:04:10 -04007177 struct btrfs_path *path;
7178 int ret;
7179 struct extent_buffer *leaf;
7180 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xie7b2b7082014-02-27 13:58:05 +08007181 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason46bfbb52010-05-26 11:04:10 -04007182 struct btrfs_file_extent_item *fi;
7183 struct btrfs_key key;
7184 u64 disk_bytenr;
7185 u64 backref_offset;
7186 u64 extent_end;
7187 u64 num_bytes;
7188 int slot;
7189 int found_type;
Josef Bacik7ee9e442013-06-21 16:37:03 -04007190 bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW);
Miao Xiee77751a2013-12-27 21:11:50 +08007191
Chris Mason46bfbb52010-05-26 11:04:10 -04007192 path = btrfs_alloc_path();
7193 if (!path)
7194 return -ENOMEM;
7195
David Sterbaf85b7372017-01-20 14:54:07 +01007196 ret = btrfs_lookup_file_extent(NULL, root, path,
7197 btrfs_ino(BTRFS_I(inode)), offset, 0);
Chris Mason46bfbb52010-05-26 11:04:10 -04007198 if (ret < 0)
7199 goto out;
7200
7201 slot = path->slots[0];
7202 if (ret == 1) {
7203 if (slot == 0) {
7204 /* can't find the item, must cow */
7205 ret = 0;
7206 goto out;
7207 }
7208 slot--;
7209 }
7210 ret = 0;
7211 leaf = path->nodes[0];
7212 btrfs_item_key_to_cpu(leaf, &key, slot);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02007213 if (key.objectid != btrfs_ino(BTRFS_I(inode)) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04007214 key.type != BTRFS_EXTENT_DATA_KEY) {
7215 /* not our file or wrong item type, must cow */
7216 goto out;
7217 }
7218
7219 if (key.offset > offset) {
7220 /* Wrong offset, must cow */
7221 goto out;
7222 }
7223
7224 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
7225 found_type = btrfs_file_extent_type(leaf, fi);
7226 if (found_type != BTRFS_FILE_EXTENT_REG &&
7227 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
7228 /* not a regular extent, must cow */
7229 goto out;
7230 }
Josef Bacik7ee9e442013-06-21 16:37:03 -04007231
7232 if (!nocow && found_type == BTRFS_FILE_EXTENT_REG)
7233 goto out;
7234
Miao Xiee77751a2013-12-27 21:11:50 +08007235 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
7236 if (extent_end <= offset)
7237 goto out;
7238
Chris Mason46bfbb52010-05-26 11:04:10 -04007239 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Josef Bacik7ee9e442013-06-21 16:37:03 -04007240 if (disk_bytenr == 0)
7241 goto out;
7242
7243 if (btrfs_file_extent_compression(leaf, fi) ||
7244 btrfs_file_extent_encryption(leaf, fi) ||
7245 btrfs_file_extent_other_encoding(leaf, fi))
7246 goto out;
7247
Ethan Lien78d42952018-05-17 14:58:29 +08007248 /*
7249 * Do the same check as in btrfs_cross_ref_exist but without the
7250 * unnecessary search.
7251 */
7252 if (btrfs_file_extent_generation(leaf, fi) <=
7253 btrfs_root_last_snapshot(&root->root_item))
7254 goto out;
7255
Chris Mason46bfbb52010-05-26 11:04:10 -04007256 backref_offset = btrfs_file_extent_offset(leaf, fi);
7257
Josef Bacik7ee9e442013-06-21 16:37:03 -04007258 if (orig_start) {
7259 *orig_start = key.offset - backref_offset;
7260 *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
7261 *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
7262 }
Josef Bacikeb384b52013-04-24 16:32:55 -04007263
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007264 if (btrfs_extent_readonly(fs_info, disk_bytenr))
Chris Mason46bfbb52010-05-26 11:04:10 -04007265 goto out;
Miao Xie7b2b7082014-02-27 13:58:05 +08007266
7267 num_bytes = min(offset + *len, extent_end) - offset;
7268 if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) {
7269 u64 range_end;
7270
Jeff Mahoneyda170662016-06-15 09:22:56 -04007271 range_end = round_up(offset + num_bytes,
7272 root->fs_info->sectorsize) - 1;
Miao Xie7b2b7082014-02-27 13:58:05 +08007273 ret = test_range_bit(io_tree, offset, range_end,
7274 EXTENT_DELALLOC, 0, NULL);
7275 if (ret) {
7276 ret = -EAGAIN;
7277 goto out;
7278 }
7279 }
7280
Josef Bacik1bda19e2013-10-18 12:10:36 -04007281 btrfs_release_path(path);
Chris Mason46bfbb52010-05-26 11:04:10 -04007282
7283 /*
7284 * look for other files referencing this extent, if we
7285 * find any we must cow
7286 */
Josef Bacik00361582013-08-14 14:02:47 -04007287
Liu Boe4c3b2d2017-01-30 12:25:28 -08007288 ret = btrfs_cross_ref_exist(root, btrfs_ino(BTRFS_I(inode)),
Josef Bacik00361582013-08-14 14:02:47 -04007289 key.offset - backref_offset, disk_bytenr);
Josef Bacik00361582013-08-14 14:02:47 -04007290 if (ret) {
7291 ret = 0;
7292 goto out;
7293 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007294
7295 /*
7296 * adjust disk_bytenr and num_bytes to cover just the bytes
7297 * in this extent we are about to write. If there
7298 * are any csums in that range we have to cow in order
7299 * to keep the csums correct
7300 */
7301 disk_bytenr += backref_offset;
7302 disk_bytenr += offset - key.offset;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007303 if (csum_exist_in_range(fs_info, disk_bytenr, num_bytes))
7304 goto out;
Chris Mason46bfbb52010-05-26 11:04:10 -04007305 /*
7306 * all of the above have passed, it is safe to overwrite this extent
7307 * without cow
7308 */
Josef Bacikeb384b52013-04-24 16:32:55 -04007309 *len = num_bytes;
Chris Mason46bfbb52010-05-26 11:04:10 -04007310 ret = 1;
7311out:
7312 btrfs_free_path(path);
7313 return ret;
7314}
7315
Josef Bacikeb838e72012-07-31 16:28:48 -04007316static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
7317 struct extent_state **cached_state, int writing)
7318{
7319 struct btrfs_ordered_extent *ordered;
7320 int ret = 0;
7321
7322 while (1) {
7323 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbaff13db42015-12-03 14:30:40 +01007324 cached_state);
Josef Bacikeb838e72012-07-31 16:28:48 -04007325 /*
7326 * We're concerned with the entire range that we're going to be
Nicholas D Steeves01327612016-05-19 21:18:45 -04007327 * doing DIO to, so we need to make sure there's no ordered
Josef Bacikeb838e72012-07-31 16:28:48 -04007328 * extents in this range.
7329 */
Nikolay Borisova776c6f2017-02-20 13:50:49 +02007330 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), lockstart,
Josef Bacikeb838e72012-07-31 16:28:48 -04007331 lockend - lockstart + 1);
7332
7333 /*
7334 * We need to make sure there are no buffered pages in this
7335 * range either, we could have raced between the invalidate in
7336 * generic_file_direct_write and locking the extent. The
7337 * invalidate needs to happen so that reads after a write do not
7338 * get stale data.
7339 */
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007340 if (!ordered &&
David Sterba051c98e2018-03-07 15:33:22 +01007341 (!writing || !filemap_range_has_page(inode->i_mapping,
7342 lockstart, lockend)))
Josef Bacikeb838e72012-07-31 16:28:48 -04007343 break;
7344
7345 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbae43bbe52017-12-12 21:43:52 +01007346 cached_state);
Josef Bacikeb838e72012-07-31 16:28:48 -04007347
7348 if (ordered) {
Filipe Mananaade77022016-02-18 14:28:55 +00007349 /*
7350 * If we are doing a DIO read and the ordered extent we
7351 * found is for a buffered write, we can not wait for it
7352 * to complete and retry, because if we do so we can
7353 * deadlock with concurrent buffered writes on page
7354 * locks. This happens only if our DIO read covers more
7355 * than one extent map, if at this point has already
7356 * created an ordered extent for a previous extent map
7357 * and locked its range in the inode's io tree, and a
7358 * concurrent write against that previous extent map's
7359 * range and this range started (we unlock the ranges
7360 * in the io tree only when the bios complete and
7361 * buffered writes always lock pages before attempting
7362 * to lock range in the io tree).
7363 */
7364 if (writing ||
7365 test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags))
7366 btrfs_start_ordered_extent(inode, ordered, 1);
7367 else
7368 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007369 btrfs_put_ordered_extent(ordered);
7370 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007371 /*
Filipe Mananab850ae12015-12-08 16:23:16 +00007372 * We could trigger writeback for this range (and wait
7373 * for it to complete) and then invalidate the pages for
7374 * this range (through invalidate_inode_pages2_range()),
7375 * but that can lead us to a deadlock with a concurrent
7376 * call to readpages() (a buffered read or a defrag call
7377 * triggered a readahead) on a page lock due to an
7378 * ordered dio extent we created before but did not have
7379 * yet a corresponding bio submitted (whence it can not
7380 * complete), which makes readpages() wait for that
7381 * ordered extent to complete while holding a lock on
7382 * that page.
Josef Bacikeb838e72012-07-31 16:28:48 -04007383 */
Filipe Mananab850ae12015-12-08 16:23:16 +00007384 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007385 }
7386
Filipe Mananaade77022016-02-18 14:28:55 +00007387 if (ret)
7388 break;
7389
Josef Bacikeb838e72012-07-31 16:28:48 -04007390 cond_resched();
7391 }
7392
7393 return ret;
7394}
7395
Liu Bo6f9994d2017-01-31 07:50:22 -08007396/* The callers of this must take lock_extent() */
7397static struct extent_map *create_io_em(struct inode *inode, u64 start, u64 len,
7398 u64 orig_start, u64 block_start,
7399 u64 block_len, u64 orig_block_len,
7400 u64 ram_bytes, int compress_type,
7401 int type)
Josef Bacik69ffb542012-09-11 15:40:07 -04007402{
7403 struct extent_map_tree *em_tree;
7404 struct extent_map *em;
7405 struct btrfs_root *root = BTRFS_I(inode)->root;
7406 int ret;
7407
Liu Bo6f9994d2017-01-31 07:50:22 -08007408 ASSERT(type == BTRFS_ORDERED_PREALLOC ||
7409 type == BTRFS_ORDERED_COMPRESSED ||
7410 type == BTRFS_ORDERED_NOCOW ||
Liu Bo1af4a0a2017-02-13 15:35:09 -08007411 type == BTRFS_ORDERED_REGULAR);
Liu Bo6f9994d2017-01-31 07:50:22 -08007412
Josef Bacik69ffb542012-09-11 15:40:07 -04007413 em_tree = &BTRFS_I(inode)->extent_tree;
7414 em = alloc_extent_map();
7415 if (!em)
7416 return ERR_PTR(-ENOMEM);
7417
7418 em->start = start;
7419 em->orig_start = orig_start;
7420 em->len = len;
7421 em->block_len = block_len;
7422 em->block_start = block_start;
7423 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacikb4939682012-12-03 10:31:19 -05007424 em->orig_block_len = orig_block_len;
Josef Bacikcc95bef2013-04-04 14:31:27 -04007425 em->ram_bytes = ram_bytes;
Josef Bacik70c8a912012-10-11 16:54:30 -04007426 em->generation = -1;
Josef Bacik69ffb542012-09-11 15:40:07 -04007427 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Liu Bo1af4a0a2017-02-13 15:35:09 -08007428 if (type == BTRFS_ORDERED_PREALLOC) {
Josef Bacikb11e2342012-12-03 10:58:15 -05007429 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Liu Bo1af4a0a2017-02-13 15:35:09 -08007430 } else if (type == BTRFS_ORDERED_COMPRESSED) {
Liu Bo6f9994d2017-01-31 07:50:22 -08007431 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
7432 em->compress_type = compress_type;
7433 }
Josef Bacik69ffb542012-09-11 15:40:07 -04007434
7435 do {
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02007436 btrfs_drop_extent_cache(BTRFS_I(inode), em->start,
Josef Bacik69ffb542012-09-11 15:40:07 -04007437 em->start + em->len - 1, 0);
7438 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04007439 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik69ffb542012-09-11 15:40:07 -04007440 write_unlock(&em_tree->lock);
Liu Bo6f9994d2017-01-31 07:50:22 -08007441 /*
7442 * The caller has taken lock_extent(), who could race with us
7443 * to add em?
7444 */
Josef Bacik69ffb542012-09-11 15:40:07 -04007445 } while (ret == -EEXIST);
7446
7447 if (ret) {
7448 free_extent_map(em);
7449 return ERR_PTR(ret);
7450 }
7451
Liu Bo6f9994d2017-01-31 07:50:22 -08007452 /* em got 2 refs now, callers needs to do free_extent_map once. */
Josef Bacik69ffb542012-09-11 15:40:07 -04007453 return em;
7454}
7455
Nikolay Borisov1c8d0172018-05-02 15:19:32 +03007456
7457static int btrfs_get_blocks_direct_read(struct extent_map *em,
7458 struct buffer_head *bh_result,
7459 struct inode *inode,
7460 u64 start, u64 len)
7461{
7462 if (em->block_start == EXTENT_MAP_HOLE ||
7463 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7464 return -ENOENT;
7465
7466 len = min(len, em->len - (start - em->start));
7467
7468 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
7469 inode->i_blkbits;
7470 bh_result->b_size = len;
7471 bh_result->b_bdev = em->bdev;
7472 set_buffer_mapped(bh_result);
7473
7474 return 0;
7475}
7476
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007477static int btrfs_get_blocks_direct_write(struct extent_map **map,
7478 struct buffer_head *bh_result,
7479 struct inode *inode,
7480 struct btrfs_dio_data *dio_data,
7481 u64 start, u64 len)
7482{
7483 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
7484 struct extent_map *em = *map;
7485 int ret = 0;
7486
7487 /*
7488 * We don't allocate a new extent in the following cases
7489 *
7490 * 1) The inode is marked as NODATACOW. In this case we'll just use the
7491 * existing extent.
7492 * 2) The extent is marked as PREALLOC. We're good to go here and can
7493 * just use the extent.
7494 *
7495 */
7496 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
7497 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
7498 em->block_start != EXTENT_MAP_HOLE)) {
7499 int type;
7500 u64 block_start, orig_start, orig_block_len, ram_bytes;
7501
7502 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7503 type = BTRFS_ORDERED_PREALLOC;
7504 else
7505 type = BTRFS_ORDERED_NOCOW;
7506 len = min(len, em->len - (start - em->start));
7507 block_start = em->block_start + (start - em->start);
7508
7509 if (can_nocow_extent(inode, start, &len, &orig_start,
7510 &orig_block_len, &ram_bytes) == 1 &&
7511 btrfs_inc_nocow_writers(fs_info, block_start)) {
7512 struct extent_map *em2;
7513
7514 em2 = btrfs_create_dio_extent(inode, start, len,
7515 orig_start, block_start,
7516 len, orig_block_len,
7517 ram_bytes, type);
7518 btrfs_dec_nocow_writers(fs_info, block_start);
7519 if (type == BTRFS_ORDERED_PREALLOC) {
7520 free_extent_map(em);
7521 *map = em = em2;
7522 }
7523
7524 if (em2 && IS_ERR(em2)) {
7525 ret = PTR_ERR(em2);
7526 goto out;
7527 }
7528 /*
7529 * For inode marked NODATACOW or extent marked PREALLOC,
7530 * use the existing or preallocated extent, so does not
7531 * need to adjust btrfs_space_info's bytes_may_use.
7532 */
7533 btrfs_free_reserved_data_space_noquota(inode, start,
7534 len);
7535 goto skip_cow;
7536 }
7537 }
7538
7539 /* this will cow the extent */
7540 len = bh_result->b_size;
7541 free_extent_map(em);
7542 *map = em = btrfs_new_extent_direct(inode, start, len);
7543 if (IS_ERR(em)) {
7544 ret = PTR_ERR(em);
7545 goto out;
7546 }
7547
7548 len = min(len, em->len - (start - em->start));
7549
7550skip_cow:
7551 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
7552 inode->i_blkbits;
7553 bh_result->b_size = len;
7554 bh_result->b_bdev = em->bdev;
7555 set_buffer_mapped(bh_result);
7556
7557 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7558 set_buffer_new(bh_result);
7559
7560 /*
7561 * Need to update the i_size under the extent lock so buffered
7562 * readers will get the updated i_size when we unlock.
7563 */
7564 if (!dio_data->overwrite && start + len > i_size_read(inode))
7565 i_size_write(inode, start + len);
7566
7567 WARN_ON(dio_data->reserve < len);
7568 dio_data->reserve -= len;
7569 dio_data->unsubmitted_oe_range_end = start + len;
7570 current->journal_info = dio_data;
7571out:
7572 return ret;
7573}
7574
Josef Bacik4b46fce2010-05-23 11:00:55 -04007575static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
7576 struct buffer_head *bh_result, int create)
7577{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007578 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007579 struct extent_map *em;
Josef Bacikeb838e72012-07-31 16:28:48 -04007580 struct extent_state *cached_state = NULL;
chandan50745b02015-08-28 21:10:13 +05307581 struct btrfs_dio_data *dio_data = NULL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007582 u64 start = iblock << inode->i_blkbits;
Josef Bacikeb838e72012-07-31 16:28:48 -04007583 u64 lockstart, lockend;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007584 u64 len = bh_result->b_size;
Josef Bacikeb838e72012-07-31 16:28:48 -04007585 int unlock_bits = EXTENT_LOCKED;
Miao Xie09348562013-02-07 10:12:07 +00007586 int ret = 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007587
Miao Xie172a5042013-02-21 02:48:22 -07007588 if (create)
Josef Bacik32667892015-02-11 15:08:58 -05007589 unlock_bits |= EXTENT_DIRTY;
Miao Xie172a5042013-02-21 02:48:22 -07007590 else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007591 len = min_t(u64, len, fs_info->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04007592
Josef Bacikc3298612012-08-03 16:49:19 -04007593 lockstart = start;
7594 lockend = start + len - 1;
7595
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007596 if (current->journal_info) {
7597 /*
7598 * Need to pull our outstanding extents and set journal_info to NULL so
Nicholas D Steeves01327612016-05-19 21:18:45 -04007599 * that anything that needs to check if there's a transaction doesn't get
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007600 * confused.
7601 */
chandan50745b02015-08-28 21:10:13 +05307602 dio_data = current->journal_info;
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007603 current->journal_info = NULL;
7604 }
7605
Josef Bacikeb838e72012-07-31 16:28:48 -04007606 /*
7607 * If this errors out it's because we couldn't invalidate pagecache for
7608 * this range and we need to fallback to buffered.
7609 */
Filipe Manana9c9464c2015-11-04 09:52:04 +00007610 if (lock_extent_direct(inode, lockstart, lockend, &cached_state,
7611 create)) {
7612 ret = -ENOTBLK;
7613 goto err;
7614 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007615
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007616 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len, 0);
Josef Bacikeb838e72012-07-31 16:28:48 -04007617 if (IS_ERR(em)) {
7618 ret = PTR_ERR(em);
7619 goto unlock_err;
7620 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007621
7622 /*
7623 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
7624 * io. INLINE is special, and we could probably kludge it in here, but
7625 * it's still buffered so for safety lets just fall back to the generic
7626 * buffered path.
7627 *
7628 * For COMPRESSED we _have_ to read the entire extent in so we can
7629 * decompress it, so there will be buffering required no matter what we
7630 * do, so go ahead and fallback to buffered.
7631 *
Nicholas D Steeves01327612016-05-19 21:18:45 -04007632 * We return -ENOTBLK because that's what makes DIO go ahead and go back
Josef Bacik4b46fce2010-05-23 11:00:55 -04007633 * to buffered IO. Don't blame me, this is the price we pay for using
7634 * the generic code.
7635 */
7636 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
7637 em->block_start == EXTENT_MAP_INLINE) {
7638 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007639 ret = -ENOTBLK;
7640 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007641 }
7642
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007643 if (create) {
7644 ret = btrfs_get_blocks_direct_write(&em, bh_result, inode,
7645 dio_data, start, len);
7646 if (ret < 0)
7647 goto unlock_err;
7648
7649 /* clear and unlock the entire range */
7650 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7651 unlock_bits, 1, 0, &cached_state);
7652 } else {
Nikolay Borisov1c8d0172018-05-02 15:19:32 +03007653 ret = btrfs_get_blocks_direct_read(em, bh_result, inode,
7654 start, len);
7655 /* Can be negative only if we read from a hole */
7656 if (ret < 0) {
7657 ret = 0;
7658 free_extent_map(em);
7659 goto unlock_err;
7660 }
7661 /*
7662 * We need to unlock only the end area that we aren't using.
7663 * The rest is going to be unlocked by the endio routine.
7664 */
7665 lockstart = start + bh_result->b_size;
7666 if (lockstart < lockend) {
7667 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
7668 lockend, unlock_bits, 1, 0,
7669 &cached_state);
7670 } else {
7671 free_extent_state(cached_state);
7672 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007673 }
7674
Josef Bacik4b46fce2010-05-23 11:00:55 -04007675 free_extent_map(em);
7676
7677 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007678
7679unlock_err:
Josef Bacikeb838e72012-07-31 16:28:48 -04007680 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbaae0f1622017-10-31 16:37:52 +01007681 unlock_bits, 1, 0, &cached_state);
Filipe Manana9c9464c2015-11-04 09:52:04 +00007682err:
chandan50745b02015-08-28 21:10:13 +05307683 if (dio_data)
7684 current->journal_info = dio_data;
Josef Bacikeb838e72012-07-31 16:28:48 -04007685 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007686}
7687
Omar Sandoval58efbc92017-08-22 23:45:59 -07007688static inline blk_status_t submit_dio_repair_bio(struct inode *inode,
7689 struct bio *bio,
7690 int mirror_num)
Miao Xie8b110e32014-09-12 18:44:03 +08007691{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007692 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Omar Sandoval58efbc92017-08-22 23:45:59 -07007693 blk_status_t ret;
Miao Xie8b110e32014-09-12 18:44:03 +08007694
Mike Christie37226b22016-06-05 14:31:52 -05007695 BUG_ON(bio_op(bio) == REQ_OP_WRITE);
Miao Xie8b110e32014-09-12 18:44:03 +08007696
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007697 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DIO_REPAIR);
Miao Xie8b110e32014-09-12 18:44:03 +08007698 if (ret)
Nikolay Borisovea057f62017-12-13 10:25:38 +02007699 return ret;
Miao Xie8b110e32014-09-12 18:44:03 +08007700
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007701 ret = btrfs_map_bio(fs_info, bio, mirror_num, 0);
Nikolay Borisovea057f62017-12-13 10:25:38 +02007702
Miao Xie8b110e32014-09-12 18:44:03 +08007703 return ret;
7704}
7705
7706static int btrfs_check_dio_repairable(struct inode *inode,
7707 struct bio *failed_bio,
7708 struct io_failure_record *failrec,
7709 int failed_mirror)
7710{
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007711 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xie8b110e32014-09-12 18:44:03 +08007712 int num_copies;
7713
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007714 num_copies = btrfs_num_copies(fs_info, failrec->logical, failrec->len);
Miao Xie8b110e32014-09-12 18:44:03 +08007715 if (num_copies == 1) {
7716 /*
7717 * we only have a single copy of the data, so don't bother with
7718 * all the retry and error correction code that follows. no
7719 * matter what the error is, it is very likely to persist.
7720 */
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007721 btrfs_debug(fs_info,
7722 "Check DIO Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d",
7723 num_copies, failrec->this_mirror, failed_mirror);
Miao Xie8b110e32014-09-12 18:44:03 +08007724 return 0;
7725 }
7726
7727 failrec->failed_mirror = failed_mirror;
7728 failrec->this_mirror++;
7729 if (failrec->this_mirror == failed_mirror)
7730 failrec->this_mirror++;
7731
7732 if (failrec->this_mirror > num_copies) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007733 btrfs_debug(fs_info,
7734 "Check DIO Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d",
7735 num_copies, failrec->this_mirror, failed_mirror);
Miao Xie8b110e32014-09-12 18:44:03 +08007736 return 0;
7737 }
7738
7739 return 1;
7740}
7741
Omar Sandoval58efbc92017-08-22 23:45:59 -07007742static blk_status_t dio_read_error(struct inode *inode, struct bio *failed_bio,
7743 struct page *page, unsigned int pgoff,
7744 u64 start, u64 end, int failed_mirror,
7745 bio_end_io_t *repair_endio, void *repair_arg)
Miao Xie8b110e32014-09-12 18:44:03 +08007746{
7747 struct io_failure_record *failrec;
Josef Bacik7870d082017-05-05 11:57:15 -04007748 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
7749 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
Miao Xie8b110e32014-09-12 18:44:03 +08007750 struct bio *bio;
7751 int isector;
David Sterbaf1c77c52017-06-06 19:03:49 +02007752 unsigned int read_mode = 0;
Liu Bo17347ce2017-05-15 15:33:27 -07007753 int segs;
Miao Xie8b110e32014-09-12 18:44:03 +08007754 int ret;
Omar Sandoval58efbc92017-08-22 23:45:59 -07007755 blk_status_t status;
Ming Leic16a8ac2017-12-18 20:22:12 +08007756 struct bio_vec bvec;
Miao Xie8b110e32014-09-12 18:44:03 +08007757
Mike Christie37226b22016-06-05 14:31:52 -05007758 BUG_ON(bio_op(failed_bio) == REQ_OP_WRITE);
Miao Xie8b110e32014-09-12 18:44:03 +08007759
7760 ret = btrfs_get_io_failure_record(inode, start, end, &failrec);
7761 if (ret)
Omar Sandoval58efbc92017-08-22 23:45:59 -07007762 return errno_to_blk_status(ret);
Miao Xie8b110e32014-09-12 18:44:03 +08007763
7764 ret = btrfs_check_dio_repairable(inode, failed_bio, failrec,
7765 failed_mirror);
7766 if (!ret) {
Josef Bacik7870d082017-05-05 11:57:15 -04007767 free_io_failure(failure_tree, io_tree, failrec);
Omar Sandoval58efbc92017-08-22 23:45:59 -07007768 return BLK_STS_IOERR;
Miao Xie8b110e32014-09-12 18:44:03 +08007769 }
7770
Liu Bo17347ce2017-05-15 15:33:27 -07007771 segs = bio_segments(failed_bio);
Ming Leic16a8ac2017-12-18 20:22:12 +08007772 bio_get_first_bvec(failed_bio, &bvec);
Liu Bo17347ce2017-05-15 15:33:27 -07007773 if (segs > 1 ||
Ming Leic16a8ac2017-12-18 20:22:12 +08007774 (bvec.bv_len > btrfs_inode_sectorsize(inode)))
Christoph Hellwig70fd7612016-11-01 07:40:10 -06007775 read_mode |= REQ_FAILFAST_DEV;
Miao Xie8b110e32014-09-12 18:44:03 +08007776
7777 isector = start - btrfs_io_bio(failed_bio)->logical;
7778 isector >>= inode->i_sb->s_blocksize_bits;
7779 bio = btrfs_create_repair_bio(inode, failed_bio, failrec, page,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307780 pgoff, isector, repair_endio, repair_arg);
David Sterbaebcc3262018-06-29 10:56:53 +02007781 bio->bi_opf = REQ_OP_READ | read_mode;
Miao Xie8b110e32014-09-12 18:44:03 +08007782
7783 btrfs_debug(BTRFS_I(inode)->root->fs_info,
David Sterba913e1532017-07-13 15:32:18 +02007784 "repair DIO read error: submitting new dio read[%#x] to this_mirror=%d, in_validation=%d",
Miao Xie8b110e32014-09-12 18:44:03 +08007785 read_mode, failrec->this_mirror, failrec->in_validation);
7786
Omar Sandoval58efbc92017-08-22 23:45:59 -07007787 status = submit_dio_repair_bio(inode, bio, failrec->this_mirror);
7788 if (status) {
Josef Bacik7870d082017-05-05 11:57:15 -04007789 free_io_failure(failure_tree, io_tree, failrec);
Miao Xie8b110e32014-09-12 18:44:03 +08007790 bio_put(bio);
7791 }
7792
Omar Sandoval58efbc92017-08-22 23:45:59 -07007793 return status;
Miao Xie8b110e32014-09-12 18:44:03 +08007794}
7795
7796struct btrfs_retry_complete {
7797 struct completion done;
7798 struct inode *inode;
7799 u64 start;
7800 int uptodate;
7801};
7802
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007803static void btrfs_retry_endio_nocsum(struct bio *bio)
Miao Xie8b110e32014-09-12 18:44:03 +08007804{
7805 struct btrfs_retry_complete *done = bio->bi_private;
Josef Bacik7870d082017-05-05 11:57:15 -04007806 struct inode *inode = done->inode;
Miao Xie8b110e32014-09-12 18:44:03 +08007807 struct bio_vec *bvec;
Josef Bacik7870d082017-05-05 11:57:15 -04007808 struct extent_io_tree *io_tree, *failure_tree;
Miao Xie8b110e32014-09-12 18:44:03 +08007809 int i;
7810
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02007811 if (bio->bi_status)
Miao Xie8b110e32014-09-12 18:44:03 +08007812 goto end;
7813
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307814 ASSERT(bio->bi_vcnt == 1);
Josef Bacik7870d082017-05-05 11:57:15 -04007815 io_tree = &BTRFS_I(inode)->io_tree;
7816 failure_tree = &BTRFS_I(inode)->io_failure_tree;
Ming Lei263663c2017-12-18 20:22:04 +08007817 ASSERT(bio_first_bvec_all(bio)->bv_len == btrfs_inode_sectorsize(inode));
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307818
Miao Xie8b110e32014-09-12 18:44:03 +08007819 done->uptodate = 1;
David Sterbac09abff2017-07-13 18:10:07 +02007820 ASSERT(!bio_flagged(bio, BIO_CLONED));
Miao Xie8b110e32014-09-12 18:44:03 +08007821 bio_for_each_segment_all(bvec, bio, i)
Josef Bacik7870d082017-05-05 11:57:15 -04007822 clean_io_failure(BTRFS_I(inode)->root->fs_info, failure_tree,
7823 io_tree, done->start, bvec->bv_page,
7824 btrfs_ino(BTRFS_I(inode)), 0);
Miao Xie8b110e32014-09-12 18:44:03 +08007825end:
7826 complete(&done->done);
7827 bio_put(bio);
7828}
7829
Omar Sandoval58efbc92017-08-22 23:45:59 -07007830static blk_status_t __btrfs_correct_data_nocsum(struct inode *inode,
7831 struct btrfs_io_bio *io_bio)
Josef Bacik4b46fce2010-05-23 11:00:55 -04007832{
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307833 struct btrfs_fs_info *fs_info;
Liu Bo17347ce2017-05-15 15:33:27 -07007834 struct bio_vec bvec;
7835 struct bvec_iter iter;
Miao Xie8b110e32014-09-12 18:44:03 +08007836 struct btrfs_retry_complete done;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007837 u64 start;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307838 unsigned int pgoff;
7839 u32 sectorsize;
7840 int nr_sectors;
Omar Sandoval58efbc92017-08-22 23:45:59 -07007841 blk_status_t ret;
7842 blk_status_t err = BLK_STS_OK;
Miao Xiedc380ae2014-09-12 18:43:55 +08007843
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307844 fs_info = BTRFS_I(inode)->root->fs_info;
Jeff Mahoneyda170662016-06-15 09:22:56 -04007845 sectorsize = fs_info->sectorsize;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307846
Miao Xiec1dc0892014-09-12 18:43:56 +08007847 start = io_bio->logical;
Miao Xie8b110e32014-09-12 18:44:03 +08007848 done.inode = inode;
Liu Bo17347ce2017-05-15 15:33:27 -07007849 io_bio->bio.bi_iter = io_bio->iter;
Miao Xie8b110e32014-09-12 18:44:03 +08007850
Liu Bo17347ce2017-05-15 15:33:27 -07007851 bio_for_each_segment(bvec, &io_bio->bio, iter) {
7852 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec.bv_len);
7853 pgoff = bvec.bv_offset;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307854
7855next_block_or_try_again:
Miao Xie8b110e32014-09-12 18:44:03 +08007856 done.uptodate = 0;
7857 done.start = start;
7858 init_completion(&done.done);
7859
Liu Bo17347ce2017-05-15 15:33:27 -07007860 ret = dio_read_error(inode, &io_bio->bio, bvec.bv_page,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307861 pgoff, start, start + sectorsize - 1,
7862 io_bio->mirror_num,
7863 btrfs_retry_endio_nocsum, &done);
Liu Bo629ebf42017-05-15 17:20:07 -07007864 if (ret) {
7865 err = ret;
7866 goto next;
7867 }
Miao Xie8b110e32014-09-12 18:44:03 +08007868
David Sterba9c17f6c2017-07-19 19:26:45 +02007869 wait_for_completion_io(&done.done);
Miao Xie8b110e32014-09-12 18:44:03 +08007870
7871 if (!done.uptodate) {
7872 /* We might have another mirror, so try again */
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307873 goto next_block_or_try_again;
Miao Xie8b110e32014-09-12 18:44:03 +08007874 }
7875
Liu Bo629ebf42017-05-15 17:20:07 -07007876next:
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307877 start += sectorsize;
7878
Liu Bo97bf5a52017-04-07 13:11:10 -07007879 nr_sectors--;
7880 if (nr_sectors) {
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307881 pgoff += sectorsize;
Liu Bo97bf5a52017-04-07 13:11:10 -07007882 ASSERT(pgoff < PAGE_SIZE);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307883 goto next_block_or_try_again;
7884 }
Miao Xie8b110e32014-09-12 18:44:03 +08007885 }
7886
Liu Bo629ebf42017-05-15 17:20:07 -07007887 return err;
Miao Xie8b110e32014-09-12 18:44:03 +08007888}
7889
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007890static void btrfs_retry_endio(struct bio *bio)
Miao Xie8b110e32014-09-12 18:44:03 +08007891{
7892 struct btrfs_retry_complete *done = bio->bi_private;
7893 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
Josef Bacik7870d082017-05-05 11:57:15 -04007894 struct extent_io_tree *io_tree, *failure_tree;
7895 struct inode *inode = done->inode;
Miao Xie8b110e32014-09-12 18:44:03 +08007896 struct bio_vec *bvec;
7897 int uptodate;
7898 int ret;
7899 int i;
7900
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02007901 if (bio->bi_status)
Miao Xie8b110e32014-09-12 18:44:03 +08007902 goto end;
7903
7904 uptodate = 1;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307905
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307906 ASSERT(bio->bi_vcnt == 1);
Ming Lei263663c2017-12-18 20:22:04 +08007907 ASSERT(bio_first_bvec_all(bio)->bv_len == btrfs_inode_sectorsize(done->inode));
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307908
Josef Bacik7870d082017-05-05 11:57:15 -04007909 io_tree = &BTRFS_I(inode)->io_tree;
7910 failure_tree = &BTRFS_I(inode)->io_failure_tree;
7911
David Sterbac09abff2017-07-13 18:10:07 +02007912 ASSERT(!bio_flagged(bio, BIO_CLONED));
Miao Xie8b110e32014-09-12 18:44:03 +08007913 bio_for_each_segment_all(bvec, bio, i) {
Josef Bacik7870d082017-05-05 11:57:15 -04007914 ret = __readpage_endio_check(inode, io_bio, i, bvec->bv_page,
7915 bvec->bv_offset, done->start,
7916 bvec->bv_len);
Miao Xie8b110e32014-09-12 18:44:03 +08007917 if (!ret)
Josef Bacik7870d082017-05-05 11:57:15 -04007918 clean_io_failure(BTRFS_I(inode)->root->fs_info,
7919 failure_tree, io_tree, done->start,
7920 bvec->bv_page,
7921 btrfs_ino(BTRFS_I(inode)),
7922 bvec->bv_offset);
Miao Xie8b110e32014-09-12 18:44:03 +08007923 else
7924 uptodate = 0;
7925 }
7926
7927 done->uptodate = uptodate;
7928end:
7929 complete(&done->done);
7930 bio_put(bio);
7931}
7932
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02007933static blk_status_t __btrfs_subio_endio_read(struct inode *inode,
7934 struct btrfs_io_bio *io_bio, blk_status_t err)
Miao Xie8b110e32014-09-12 18:44:03 +08007935{
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307936 struct btrfs_fs_info *fs_info;
Liu Bo17347ce2017-05-15 15:33:27 -07007937 struct bio_vec bvec;
7938 struct bvec_iter iter;
Miao Xie8b110e32014-09-12 18:44:03 +08007939 struct btrfs_retry_complete done;
7940 u64 start;
7941 u64 offset = 0;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307942 u32 sectorsize;
7943 int nr_sectors;
7944 unsigned int pgoff;
7945 int csum_pos;
Liu Boef7cdac12017-04-13 18:11:48 -07007946 bool uptodate = (err == 0);
Miao Xie8b110e32014-09-12 18:44:03 +08007947 int ret;
Omar Sandoval58efbc92017-08-22 23:45:59 -07007948 blk_status_t status;
Miao Xie8b110e32014-09-12 18:44:03 +08007949
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307950 fs_info = BTRFS_I(inode)->root->fs_info;
Jeff Mahoneyda170662016-06-15 09:22:56 -04007951 sectorsize = fs_info->sectorsize;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307952
Omar Sandoval58efbc92017-08-22 23:45:59 -07007953 err = BLK_STS_OK;
Miao Xie8b110e32014-09-12 18:44:03 +08007954 start = io_bio->logical;
7955 done.inode = inode;
Liu Bo17347ce2017-05-15 15:33:27 -07007956 io_bio->bio.bi_iter = io_bio->iter;
Miao Xie8b110e32014-09-12 18:44:03 +08007957
Liu Bo17347ce2017-05-15 15:33:27 -07007958 bio_for_each_segment(bvec, &io_bio->bio, iter) {
7959 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec.bv_len);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307960
Liu Bo17347ce2017-05-15 15:33:27 -07007961 pgoff = bvec.bv_offset;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307962next_block:
Liu Boef7cdac12017-04-13 18:11:48 -07007963 if (uptodate) {
7964 csum_pos = BTRFS_BYTES_TO_BLKS(fs_info, offset);
7965 ret = __readpage_endio_check(inode, io_bio, csum_pos,
7966 bvec.bv_page, pgoff, start, sectorsize);
7967 if (likely(!ret))
7968 goto next;
7969 }
Miao Xie8b110e32014-09-12 18:44:03 +08007970try_again:
7971 done.uptodate = 0;
7972 done.start = start;
7973 init_completion(&done.done);
7974
Omar Sandoval58efbc92017-08-22 23:45:59 -07007975 status = dio_read_error(inode, &io_bio->bio, bvec.bv_page,
7976 pgoff, start, start + sectorsize - 1,
7977 io_bio->mirror_num, btrfs_retry_endio,
7978 &done);
7979 if (status) {
7980 err = status;
Miao Xie8b110e32014-09-12 18:44:03 +08007981 goto next;
7982 }
7983
David Sterba9c17f6c2017-07-19 19:26:45 +02007984 wait_for_completion_io(&done.done);
Miao Xie8b110e32014-09-12 18:44:03 +08007985
7986 if (!done.uptodate) {
7987 /* We might have another mirror, so try again */
7988 goto try_again;
7989 }
7990next:
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307991 offset += sectorsize;
7992 start += sectorsize;
7993
7994 ASSERT(nr_sectors);
7995
Liu Bo97bf5a52017-04-07 13:11:10 -07007996 nr_sectors--;
7997 if (nr_sectors) {
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307998 pgoff += sectorsize;
Liu Bo97bf5a52017-04-07 13:11:10 -07007999 ASSERT(pgoff < PAGE_SIZE);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308000 goto next_block;
8001 }
Kent Overstreet2c30c712013-11-07 12:20:26 -08008002 }
Miao Xiec1dc0892014-09-12 18:43:56 +08008003
8004 return err;
8005}
8006
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008007static blk_status_t btrfs_subio_endio_read(struct inode *inode,
8008 struct btrfs_io_bio *io_bio, blk_status_t err)
Miao Xie8b110e32014-09-12 18:44:03 +08008009{
8010 bool skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
8011
8012 if (skip_csum) {
8013 if (unlikely(err))
8014 return __btrfs_correct_data_nocsum(inode, io_bio);
8015 else
Omar Sandoval58efbc92017-08-22 23:45:59 -07008016 return BLK_STS_OK;
Miao Xie8b110e32014-09-12 18:44:03 +08008017 } else {
8018 return __btrfs_subio_endio_read(inode, io_bio, err);
8019 }
8020}
8021
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008022static void btrfs_endio_direct_read(struct bio *bio)
Miao Xiec1dc0892014-09-12 18:43:56 +08008023{
8024 struct btrfs_dio_private *dip = bio->bi_private;
8025 struct inode *inode = dip->inode;
8026 struct bio *dio_bio;
8027 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008028 blk_status_t err = bio->bi_status;
Miao Xiec1dc0892014-09-12 18:43:56 +08008029
Liu Bo99c4e3b92017-09-15 15:06:51 -06008030 if (dip->flags & BTRFS_DIO_ORIG_BIO_SUBMITTED)
Miao Xie8b110e32014-09-12 18:44:03 +08008031 err = btrfs_subio_endio_read(inode, io_bio, err);
Miao Xiec1dc0892014-09-12 18:43:56 +08008032
Josef Bacik4b46fce2010-05-23 11:00:55 -04008033 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01008034 dip->logical_offset + dip->bytes - 1);
Chris Mason9be33952013-05-17 18:30:14 -04008035 dio_bio = dip->dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008036
Josef Bacik4b46fce2010-05-23 11:00:55 -04008037 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04008038
Liu Bo99c4e3b92017-09-15 15:06:51 -06008039 dio_bio->bi_status = err;
Christoph Hellwig40553512017-06-03 09:37:58 +02008040 dio_end_io(dio_bio);
David Sterbab3a0dd52018-11-22 17:16:49 +01008041 btrfs_io_bio_free_csum(io_bio);
Chris Mason9be33952013-05-17 18:30:14 -04008042 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008043}
8044
Qu Wenruo524272602017-03-08 10:25:52 +08008045static void __endio_write_update_ordered(struct inode *inode,
8046 const u64 offset, const u64 bytes,
8047 const bool uptodate)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008048{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008049 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008050 struct btrfs_ordered_extent *ordered = NULL;
Qu Wenruo524272602017-03-08 10:25:52 +08008051 struct btrfs_workqueue *wq;
8052 btrfs_work_func_t func;
Filipe Manana14543772015-11-24 16:23:54 +00008053 u64 ordered_offset = offset;
8054 u64 ordered_bytes = bytes;
Naohiro Aota67c003f2017-09-01 17:59:07 +09008055 u64 last_offset;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008056
Qu Wenruo524272602017-03-08 10:25:52 +08008057 if (btrfs_is_free_space_inode(BTRFS_I(inode))) {
8058 wq = fs_info->endio_freespace_worker;
8059 func = btrfs_freespace_write_helper;
8060 } else {
8061 wq = fs_info->endio_write_workers;
8062 func = btrfs_endio_write_helper;
8063 }
8064
Nikolay Borisovb25f0d02018-04-11 11:21:17 +03008065 while (ordered_offset < offset + bytes) {
8066 last_offset = ordered_offset;
8067 if (btrfs_dec_test_first_ordered_pending(inode, &ordered,
8068 &ordered_offset,
8069 ordered_bytes,
8070 uptodate)) {
8071 btrfs_init_work(&ordered->work, func,
8072 finish_ordered_fn,
8073 NULL, NULL);
8074 btrfs_queue_work(wq, &ordered->work);
8075 }
8076 /*
8077 * If btrfs_dec_test_ordered_pending does not find any ordered
8078 * extent in the range, we can exit.
8079 */
8080 if (ordered_offset == last_offset)
8081 return;
8082 /*
8083 * Our bio might span multiple ordered extents. In this case
8084 * we keep goin until we have accounted the whole dio.
8085 */
8086 if (ordered_offset < offset + bytes) {
8087 ordered_bytes = offset + bytes - ordered_offset;
8088 ordered = NULL;
8089 }
Chris Mason163cf092010-11-28 19:56:33 -05008090 }
Filipe Manana14543772015-11-24 16:23:54 +00008091}
8092
8093static void btrfs_endio_direct_write(struct bio *bio)
8094{
8095 struct btrfs_dio_private *dip = bio->bi_private;
8096 struct bio *dio_bio = dip->dio_bio;
8097
Qu Wenruo524272602017-03-08 10:25:52 +08008098 __endio_write_update_ordered(dip->inode, dip->logical_offset,
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008099 dip->bytes, !bio->bi_status);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008100
Josef Bacik4b46fce2010-05-23 11:00:55 -04008101 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04008102
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008103 dio_bio->bi_status = bio->bi_status;
Christoph Hellwig40553512017-06-03 09:37:58 +02008104 dio_end_io(dio_bio);
Chris Mason9be33952013-05-17 18:30:14 -04008105 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008106}
8107
David Sterbad0ee3932018-03-08 14:35:48 +01008108static blk_status_t btrfs_submit_bio_start_direct_io(void *private_data,
David Sterbad0779292018-03-08 13:47:33 +01008109 struct bio *bio, u64 offset)
Chris Masoneaf25d92010-05-25 09:48:28 -04008110{
Josef Bacikc6100a42017-05-05 11:57:13 -04008111 struct inode *inode = private_data;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008112 blk_status_t ret;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008113 ret = btrfs_csum_one_bio(inode, bio, offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008114 BUG_ON(ret); /* -ENOMEM */
Chris Masoneaf25d92010-05-25 09:48:28 -04008115 return 0;
8116}
8117
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008118static void btrfs_end_dio_bio(struct bio *bio)
Miao Xiee65e1532010-11-22 03:04:43 +00008119{
8120 struct btrfs_dio_private *dip = bio->bi_private;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008121 blk_status_t err = bio->bi_status;
Miao Xiee65e1532010-11-22 03:04:43 +00008122
Miao Xie8b110e32014-09-12 18:44:03 +08008123 if (err)
8124 btrfs_warn(BTRFS_I(dip->inode)->root->fs_info,
Mike Christie6296b962016-06-05 14:32:21 -05008125 "direct IO failed ino %llu rw %d,%u sector %#Lx len %u err no %d",
David Sterbaf85b7372017-01-20 14:54:07 +01008126 btrfs_ino(BTRFS_I(dip->inode)), bio_op(bio),
8127 bio->bi_opf,
Miao Xie8b110e32014-09-12 18:44:03 +08008128 (unsigned long long)bio->bi_iter.bi_sector,
8129 bio->bi_iter.bi_size, err);
8130
8131 if (dip->subio_endio)
8132 err = dip->subio_endio(dip->inode, btrfs_io_bio(bio), err);
Miao Xiec1dc0892014-09-12 18:43:56 +08008133
8134 if (err) {
Miao Xiee65e1532010-11-22 03:04:43 +00008135 /*
Nikolay Borisovde224b72018-02-14 10:53:36 +02008136 * We want to perceive the errors flag being set before
8137 * decrementing the reference count. We don't need a barrier
8138 * since atomic operations with a return value are fully
8139 * ordered as per atomic_t.txt
Miao Xiee65e1532010-11-22 03:04:43 +00008140 */
Nikolay Borisovde224b72018-02-14 10:53:36 +02008141 dip->errors = 1;
Miao Xiee65e1532010-11-22 03:04:43 +00008142 }
8143
8144 /* if there are more bios still pending for this dio, just exit */
8145 if (!atomic_dec_and_test(&dip->pending_bios))
8146 goto out;
8147
Chris Mason9be33952013-05-17 18:30:14 -04008148 if (dip->errors) {
Miao Xiee65e1532010-11-22 03:04:43 +00008149 bio_io_error(dip->orig_bio);
Chris Mason9be33952013-05-17 18:30:14 -04008150 } else {
Anand Jain2dbe0c72017-10-14 08:35:56 +08008151 dip->dio_bio->bi_status = BLK_STS_OK;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008152 bio_endio(dip->orig_bio);
Miao Xiee65e1532010-11-22 03:04:43 +00008153 }
8154out:
8155 bio_put(bio);
8156}
8157
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008158static inline blk_status_t btrfs_lookup_and_bind_dio_csum(struct inode *inode,
Miao Xiec1dc0892014-09-12 18:43:56 +08008159 struct btrfs_dio_private *dip,
8160 struct bio *bio,
8161 u64 file_offset)
8162{
8163 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
8164 struct btrfs_io_bio *orig_io_bio = btrfs_io_bio(dip->orig_bio);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008165 blk_status_t ret;
Miao Xiec1dc0892014-09-12 18:43:56 +08008166
8167 /*
8168 * We load all the csum data we need when we submit
8169 * the first bio to reduce the csum tree search and
8170 * contention.
8171 */
8172 if (dip->logical_offset == file_offset) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008173 ret = btrfs_lookup_bio_sums_dio(inode, dip->orig_bio,
Miao Xiec1dc0892014-09-12 18:43:56 +08008174 file_offset);
8175 if (ret)
8176 return ret;
8177 }
8178
8179 if (bio == dip->orig_bio)
8180 return 0;
8181
8182 file_offset -= dip->logical_offset;
8183 file_offset >>= inode->i_sb->s_blocksize_bits;
8184 io_bio->csum = (u8 *)(((u32 *)orig_io_bio->csum) + file_offset);
8185
8186 return 0;
8187}
8188
David Sterbad0ee3932018-03-08 14:35:48 +01008189static inline blk_status_t btrfs_submit_dio_bio(struct bio *bio,
8190 struct inode *inode, u64 file_offset, int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00008191{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008192 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xiefacc8a222013-07-25 19:22:34 +08008193 struct btrfs_dio_private *dip = bio->bi_private;
Mike Christie37226b22016-06-05 14:31:52 -05008194 bool write = bio_op(bio) == REQ_OP_WRITE;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008195 blk_status_t ret;
Miao Xiee65e1532010-11-22 03:04:43 +00008196
Liu Bo4c274bc2017-11-01 17:19:27 -06008197 /* Check btrfs_submit_bio_hook() for rules about async submit. */
Josef Bacikb812ce22012-11-16 13:56:32 -05008198 if (async_submit)
8199 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
8200
Josef Bacik5fd02042012-05-02 14:00:54 -04008201 if (!write) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008202 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DATA);
Josef Bacik5fd02042012-05-02 14:00:54 -04008203 if (ret)
8204 goto err;
8205 }
Miao Xiee65e1532010-11-22 03:04:43 +00008206
Nikolay Borisove6961ca2017-08-03 15:44:58 +03008207 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Josef Bacik1ae39932011-04-06 14:41:34 -04008208 goto map;
8209
8210 if (write && async_submit) {
Josef Bacikc6100a42017-05-05 11:57:13 -04008211 ret = btrfs_wq_submit_bio(fs_info, bio, 0, 0,
8212 file_offset, inode,
David Sterbae288c082018-07-18 17:36:24 +02008213 btrfs_submit_bio_start_direct_io);
Miao Xiee65e1532010-11-22 03:04:43 +00008214 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04008215 } else if (write) {
8216 /*
8217 * If we aren't doing async submit, calculate the csum of the
8218 * bio now.
8219 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008220 ret = btrfs_csum_one_bio(inode, bio, file_offset, 1);
Josef Bacik1ae39932011-04-06 14:41:34 -04008221 if (ret)
8222 goto err;
Miao Xie23ea8e52014-09-12 18:43:54 +08008223 } else {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008224 ret = btrfs_lookup_and_bind_dio_csum(inode, dip, bio,
Miao Xiec1dc0892014-09-12 18:43:56 +08008225 file_offset);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00008226 if (ret)
8227 goto err;
8228 }
Josef Bacik1ae39932011-04-06 14:41:34 -04008229map:
Liu Bo9b4a9b22017-08-18 11:54:02 -06008230 ret = btrfs_map_bio(fs_info, bio, 0, 0);
Miao Xiee65e1532010-11-22 03:04:43 +00008231err:
Miao Xiee65e1532010-11-22 03:04:43 +00008232 return ret;
8233}
8234
Nikolay Borisove6961ca2017-08-03 15:44:58 +03008235static int btrfs_submit_direct_hook(struct btrfs_dio_private *dip)
Miao Xiee65e1532010-11-22 03:04:43 +00008236{
8237 struct inode *inode = dip->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008238 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xiee65e1532010-11-22 03:04:43 +00008239 struct bio *bio;
8240 struct bio *orig_bio = dip->orig_bio;
Kent Overstreet4f024f32013-10-11 15:44:27 -07008241 u64 start_sector = orig_bio->bi_iter.bi_sector;
Miao Xiee65e1532010-11-22 03:04:43 +00008242 u64 file_offset = dip->logical_offset;
Miao Xiee65e1532010-11-22 03:04:43 +00008243 u64 map_length;
Josef Bacik1ae39932011-04-06 14:41:34 -04008244 int async_submit = 0;
Liu Bo725130b2017-05-16 09:51:39 -07008245 u64 submit_len;
8246 int clone_offset = 0;
8247 int clone_len;
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05308248 int ret;
Omar Sandoval58efbc92017-08-22 23:45:59 -07008249 blk_status_t status;
Miao Xiee65e1532010-11-22 03:04:43 +00008250
Kent Overstreet4f024f32013-10-11 15:44:27 -07008251 map_length = orig_bio->bi_iter.bi_size;
Liu Bo725130b2017-05-16 09:51:39 -07008252 submit_len = map_length;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008253 ret = btrfs_map_block(fs_info, btrfs_op(orig_bio), start_sector << 9,
8254 &map_length, NULL, 0);
Miao Xie7a5c3c92014-06-17 18:58:59 +08008255 if (ret)
Miao Xiee65e1532010-11-22 03:04:43 +00008256 return -EIO;
Miao Xiefacc8a222013-07-25 19:22:34 +08008257
Liu Bo725130b2017-05-16 09:51:39 -07008258 if (map_length >= submit_len) {
Josef Bacik02f57c72011-04-06 14:25:44 -04008259 bio = orig_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08008260 dip->flags |= BTRFS_DIO_ORIG_BIO_SUBMITTED;
Josef Bacik02f57c72011-04-06 14:25:44 -04008261 goto submit;
8262 }
8263
David Woodhouse53b381b2013-01-29 18:40:14 -05008264 /* async crcs make it difficult to collect full stripe writes. */
Jeff Mahoney1b868262017-05-17 11:38:35 -04008265 if (btrfs_data_alloc_profile(fs_info) & BTRFS_BLOCK_GROUP_RAID56_MASK)
David Woodhouse53b381b2013-01-29 18:40:14 -05008266 async_submit = 0;
8267 else
8268 async_submit = 1;
8269
Liu Bo725130b2017-05-16 09:51:39 -07008270 /* bio split */
8271 ASSERT(map_length <= INT_MAX);
Josef Bacik02f57c72011-04-06 14:25:44 -04008272 atomic_inc(&dip->pending_bios);
Arnd Bergmann3c91ee62017-05-18 15:33:29 +02008273 do {
Liu Bo725130b2017-05-16 09:51:39 -07008274 clone_len = min_t(int, submit_len, map_length);
Josef Bacik02f57c72011-04-06 14:25:44 -04008275
Liu Bo725130b2017-05-16 09:51:39 -07008276 /*
8277 * This will never fail as it's passing GPF_NOFS and
8278 * the allocation is backed by btrfs_bioset.
8279 */
Liu Boe4770942017-05-16 10:57:14 -07008280 bio = btrfs_bio_clone_partial(orig_bio, clone_offset,
Liu Bo725130b2017-05-16 09:51:39 -07008281 clone_len);
8282 bio->bi_private = dip;
8283 bio->bi_end_io = btrfs_end_dio_bio;
8284 btrfs_io_bio(bio)->logical = file_offset;
Miao Xiee65e1532010-11-22 03:04:43 +00008285
Liu Bo725130b2017-05-16 09:51:39 -07008286 ASSERT(submit_len >= clone_len);
8287 submit_len -= clone_len;
8288 if (submit_len == 0)
8289 break;
Miao Xiee65e1532010-11-22 03:04:43 +00008290
Liu Bo725130b2017-05-16 09:51:39 -07008291 /*
8292 * Increase the count before we submit the bio so we know
8293 * the end IO handler won't happen before we increase the
8294 * count. Otherwise, the dip might get freed before we're
8295 * done setting it up.
8296 */
8297 atomic_inc(&dip->pending_bios);
Miao Xiee65e1532010-11-22 03:04:43 +00008298
David Sterbad0ee3932018-03-08 14:35:48 +01008299 status = btrfs_submit_dio_bio(bio, inode, file_offset,
Omar Sandoval58efbc92017-08-22 23:45:59 -07008300 async_submit);
8301 if (status) {
Liu Bo725130b2017-05-16 09:51:39 -07008302 bio_put(bio);
8303 atomic_dec(&dip->pending_bios);
8304 goto out_err;
Miao Xiee65e1532010-11-22 03:04:43 +00008305 }
Liu Bo725130b2017-05-16 09:51:39 -07008306
8307 clone_offset += clone_len;
8308 start_sector += clone_len >> 9;
8309 file_offset += clone_len;
8310
8311 map_length = submit_len;
8312 ret = btrfs_map_block(fs_info, btrfs_op(orig_bio),
8313 start_sector << 9, &map_length, NULL, 0);
8314 if (ret)
8315 goto out_err;
Arnd Bergmann3c91ee62017-05-18 15:33:29 +02008316 } while (submit_len > 0);
Miao Xiee65e1532010-11-22 03:04:43 +00008317
Josef Bacik02f57c72011-04-06 14:25:44 -04008318submit:
David Sterbad0ee3932018-03-08 14:35:48 +01008319 status = btrfs_submit_dio_bio(bio, inode, file_offset, async_submit);
Omar Sandoval58efbc92017-08-22 23:45:59 -07008320 if (!status)
Miao Xiee65e1532010-11-22 03:04:43 +00008321 return 0;
8322
8323 bio_put(bio);
8324out_err:
8325 dip->errors = 1;
8326 /*
Nikolay Borisovde224b72018-02-14 10:53:36 +02008327 * Before atomic variable goto zero, we must make sure dip->errors is
8328 * perceived to be set. This ordering is ensured by the fact that an
8329 * atomic operations with a return value are fully ordered as per
8330 * atomic_t.txt
Miao Xiee65e1532010-11-22 03:04:43 +00008331 */
Miao Xiee65e1532010-11-22 03:04:43 +00008332 if (atomic_dec_and_test(&dip->pending_bios))
8333 bio_io_error(dip->orig_bio);
8334
8335 /* bio_end_io() will handle error, so we needn't return it */
8336 return 0;
8337}
8338
Mike Christie8a4c1e42016-06-05 14:31:50 -05008339static void btrfs_submit_direct(struct bio *dio_bio, struct inode *inode,
8340 loff_t file_offset)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008341{
Filipe Manana61de7182015-07-01 12:13:10 +01008342 struct btrfs_dio_private *dip = NULL;
Liu Bo3892ac92017-04-17 15:00:28 -07008343 struct bio *bio = NULL;
8344 struct btrfs_io_bio *io_bio;
Mike Christie8a4c1e42016-06-05 14:31:50 -05008345 bool write = (bio_op(dio_bio) == REQ_OP_WRITE);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008346 int ret = 0;
8347
David Sterba8b6c1d52017-06-02 17:48:13 +02008348 bio = btrfs_bio_clone(dio_bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008349
Miao Xiec1dc0892014-09-12 18:43:56 +08008350 dip = kzalloc(sizeof(*dip), GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04008351 if (!dip) {
8352 ret = -ENOMEM;
Filipe Manana61de7182015-07-01 12:13:10 +01008353 goto free_ordered;
Chris Mason9be33952013-05-17 18:30:14 -04008354 }
8355
8356 dip->private = dio_bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008357 dip->inode = inode;
8358 dip->logical_offset = file_offset;
Kent Overstreet4f024f32013-10-11 15:44:27 -07008359 dip->bytes = dio_bio->bi_iter.bi_size;
8360 dip->disk_bytenr = (u64)dio_bio->bi_iter.bi_sector << 9;
Liu Bo3892ac92017-04-17 15:00:28 -07008361 bio->bi_private = dip;
8362 dip->orig_bio = bio;
Chris Mason9be33952013-05-17 18:30:14 -04008363 dip->dio_bio = dio_bio;
Miao Xiee65e1532010-11-22 03:04:43 +00008364 atomic_set(&dip->pending_bios, 0);
Liu Bo3892ac92017-04-17 15:00:28 -07008365 io_bio = btrfs_io_bio(bio);
8366 io_bio->logical = file_offset;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008367
Miao Xiec1dc0892014-09-12 18:43:56 +08008368 if (write) {
Liu Bo3892ac92017-04-17 15:00:28 -07008369 bio->bi_end_io = btrfs_endio_direct_write;
Miao Xiec1dc0892014-09-12 18:43:56 +08008370 } else {
Liu Bo3892ac92017-04-17 15:00:28 -07008371 bio->bi_end_io = btrfs_endio_direct_read;
Miao Xiec1dc0892014-09-12 18:43:56 +08008372 dip->subio_endio = btrfs_subio_endio_read;
8373 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04008374
Filipe Mananaf28a4922015-12-08 19:23:20 +00008375 /*
8376 * Reset the range for unsubmitted ordered extents (to a 0 length range)
8377 * even if we fail to submit a bio, because in such case we do the
8378 * corresponding error handling below and it must not be done a second
8379 * time by btrfs_direct_IO().
8380 */
8381 if (write) {
8382 struct btrfs_dio_data *dio_data = current->journal_info;
8383
8384 dio_data->unsubmitted_oe_range_end = dip->logical_offset +
8385 dip->bytes;
8386 dio_data->unsubmitted_oe_range_start =
8387 dio_data->unsubmitted_oe_range_end;
8388 }
8389
Nikolay Borisove6961ca2017-08-03 15:44:58 +03008390 ret = btrfs_submit_direct_hook(dip);
Miao Xiee65e1532010-11-22 03:04:43 +00008391 if (!ret)
Chris Masoneaf25d92010-05-25 09:48:28 -04008392 return;
Chris Mason9be33952013-05-17 18:30:14 -04008393
David Sterbab3a0dd52018-11-22 17:16:49 +01008394 btrfs_io_bio_free_csum(io_bio);
Chris Mason9be33952013-05-17 18:30:14 -04008395
Josef Bacik4b46fce2010-05-23 11:00:55 -04008396free_ordered:
8397 /*
Filipe Manana61de7182015-07-01 12:13:10 +01008398 * If we arrived here it means either we failed to submit the dip
8399 * or we either failed to clone the dio_bio or failed to allocate the
8400 * dip. If we cloned the dio_bio and allocated the dip, we can just
8401 * call bio_endio against our io_bio so that we get proper resource
8402 * cleanup if we fail to submit the dip, otherwise, we must do the
8403 * same as btrfs_endio_direct_[write|read] because we can't call these
8404 * callbacks - they require an allocated dip and a clone of dio_bio.
Josef Bacik4b46fce2010-05-23 11:00:55 -04008405 */
Liu Bo3892ac92017-04-17 15:00:28 -07008406 if (bio && dip) {
Guoqing Jiang054ec2f2017-06-02 16:08:50 +08008407 bio_io_error(bio);
Filipe Manana61de7182015-07-01 12:13:10 +01008408 /*
Liu Bo3892ac92017-04-17 15:00:28 -07008409 * The end io callbacks free our dip, do the final put on bio
Filipe Manana61de7182015-07-01 12:13:10 +01008410 * and all the cleanup and final put for dio_bio (through
8411 * dio_end_io()).
8412 */
8413 dip = NULL;
Liu Bo3892ac92017-04-17 15:00:28 -07008414 bio = NULL;
Filipe Manana61de7182015-07-01 12:13:10 +01008415 } else {
Filipe Manana14543772015-11-24 16:23:54 +00008416 if (write)
Qu Wenruo524272602017-03-08 10:25:52 +08008417 __endio_write_update_ordered(inode,
Filipe Manana14543772015-11-24 16:23:54 +00008418 file_offset,
8419 dio_bio->bi_iter.bi_size,
Qu Wenruo524272602017-03-08 10:25:52 +08008420 false);
Filipe Manana14543772015-11-24 16:23:54 +00008421 else
Filipe Manana61de7182015-07-01 12:13:10 +01008422 unlock_extent(&BTRFS_I(inode)->io_tree, file_offset,
8423 file_offset + dio_bio->bi_iter.bi_size - 1);
Filipe Manana14543772015-11-24 16:23:54 +00008424
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008425 dio_bio->bi_status = BLK_STS_IOERR;
Filipe Manana61de7182015-07-01 12:13:10 +01008426 /*
8427 * Releases and cleans up our dio_bio, no need to bio_put()
8428 * nor bio_endio()/bio_io_error() against dio_bio.
8429 */
Christoph Hellwig40553512017-06-03 09:37:58 +02008430 dio_end_io(dio_bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008431 }
Liu Bo3892ac92017-04-17 15:00:28 -07008432 if (bio)
8433 bio_put(bio);
Filipe Manana61de7182015-07-01 12:13:10 +01008434 kfree(dip);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008435}
8436
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008437static ssize_t check_direct_IO(struct btrfs_fs_info *fs_info,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008438 const struct iov_iter *iter, loff_t offset)
Chris Mason5a5f79b2010-05-26 21:33:37 -04008439{
8440 int seg;
Josef Bacika1b75f72011-04-08 15:51:18 +00008441 int i;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008442 unsigned int blocksize_mask = fs_info->sectorsize - 1;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008443 ssize_t retval = -EINVAL;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008444
8445 if (offset & blocksize_mask)
8446 goto out;
8447
Al Viro28060d52014-03-22 05:15:17 -04008448 if (iov_iter_alignment(iter) & blocksize_mask)
8449 goto out;
Josef Bacika1b75f72011-04-08 15:51:18 +00008450
Al Viro28060d52014-03-22 05:15:17 -04008451 /* If this is a write we don't need to check anymore */
Al Virocd27e452016-10-10 13:39:05 -04008452 if (iov_iter_rw(iter) != READ || !iter_is_iovec(iter))
Al Viro28060d52014-03-22 05:15:17 -04008453 return 0;
8454 /*
8455 * Check to make sure we don't have duplicate iov_base's in this
8456 * iovec, if so return EINVAL, otherwise we'll get csum errors
8457 * when reading back.
8458 */
8459 for (seg = 0; seg < iter->nr_segs; seg++) {
8460 for (i = seg + 1; i < iter->nr_segs; i++) {
8461 if (iter->iov[seg].iov_base == iter->iov[i].iov_base)
Josef Bacika1b75f72011-04-08 15:51:18 +00008462 goto out;
8463 }
Chris Mason5a5f79b2010-05-26 21:33:37 -04008464 }
8465 retval = 0;
8466out:
8467 return retval;
8468}
Josef Bacikeb838e72012-07-31 16:28:48 -04008469
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008470static ssize_t btrfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
Chris Mason16432982008-04-10 10:23:21 -04008471{
Josef Bacik4b46fce2010-05-23 11:00:55 -04008472 struct file *file = iocb->ki_filp;
8473 struct inode *inode = file->f_mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008474 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
chandan50745b02015-08-28 21:10:13 +05308475 struct btrfs_dio_data dio_data = { 0 };
Qu Wenruo364ecf32017-02-27 15:10:38 +08008476 struct extent_changeset *data_reserved = NULL;
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008477 loff_t offset = iocb->ki_pos;
Miao Xie09348562013-02-07 10:12:07 +00008478 size_t count = 0;
Miao Xie2e60a512013-02-08 07:01:08 +00008479 int flags = 0;
Miao Xie38851cc2013-02-08 07:04:11 +00008480 bool wakeup = true;
8481 bool relock = false;
Miao Xie09348562013-02-07 10:12:07 +00008482 ssize_t ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008483
Nikolay Borisov8c70c9f2017-08-21 12:43:46 +03008484 if (check_direct_IO(fs_info, iter, offset))
Chris Mason5a5f79b2010-05-26 21:33:37 -04008485 return 0;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008486
Jens Axboefe0f07d2015-04-15 17:05:48 -06008487 inode_dio_begin(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008488
Josef Bacik0e267c42013-07-02 10:38:02 -04008489 /*
Miao Xie41bd9ca2014-03-06 13:54:57 +08008490 * The generic stuff only does filemap_write_and_wait_range, which
8491 * isn't enough if we've written compressed pages to this area, so
8492 * we need to flush the dirty pages again to make absolutely sure
8493 * that any outstanding dirty pages are on disk.
Josef Bacik0e267c42013-07-02 10:38:02 -04008494 */
Al Viroa6cbcd42014-03-04 22:38:00 -05008495 count = iov_iter_count(iter);
Miao Xie41bd9ca2014-03-06 13:54:57 +08008496 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
8497 &BTRFS_I(inode)->runtime_flags))
Wang Shilong9a025a02014-07-17 11:44:13 +08008498 filemap_fdatawrite_range(inode->i_mapping, offset,
8499 offset + count - 1);
Josef Bacik0e267c42013-07-02 10:38:02 -04008500
Omar Sandoval6f673762015-03-16 04:33:52 -07008501 if (iov_iter_rw(iter) == WRITE) {
Miao Xie38851cc2013-02-08 07:04:11 +00008502 /*
8503 * If the write DIO is beyond the EOF, we need update
8504 * the isize, but it is protected by i_mutex. So we can
8505 * not unlock the i_mutex at this case.
8506 */
8507 if (offset + count <= inode->i_size) {
Liu Bo4aaedfb2016-12-14 22:36:05 -08008508 dio_data.overwrite = 1;
Al Viro59551022016-01-22 15:40:57 -05008509 inode_unlock(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008510 relock = true;
Goldwyn Rodriguesedf064e2017-06-20 07:05:49 -05008511 } else if (iocb->ki_flags & IOCB_NOWAIT) {
8512 ret = -EAGAIN;
8513 goto out;
Miao Xie38851cc2013-02-08 07:04:11 +00008514 }
Qu Wenruo364ecf32017-02-27 15:10:38 +08008515 ret = btrfs_delalloc_reserve_space(inode, &data_reserved,
8516 offset, count);
Miao Xie09348562013-02-07 10:12:07 +00008517 if (ret)
Miao Xie38851cc2013-02-08 07:04:11 +00008518 goto out;
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008519
8520 /*
8521 * We need to know how many extents we reserved so that we can
8522 * do the accounting properly if we go over the number we
8523 * originally calculated. Abuse current->journal_info for this.
8524 */
Jeff Mahoneyda170662016-06-15 09:22:56 -04008525 dio_data.reserve = round_up(count,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008526 fs_info->sectorsize);
Filipe Mananaf28a4922015-12-08 19:23:20 +00008527 dio_data.unsubmitted_oe_range_start = (u64)offset;
8528 dio_data.unsubmitted_oe_range_end = (u64)offset;
chandan50745b02015-08-28 21:10:13 +05308529 current->journal_info = &dio_data;
Chandan Rajendra97dcdea2016-12-23 15:00:18 +05308530 down_read(&BTRFS_I(inode)->dio_sem);
David Sterbaee39b432014-09-30 01:33:33 +02008531 } else if (test_bit(BTRFS_INODE_READDIO_NEED_LOCK,
8532 &BTRFS_I(inode)->runtime_flags)) {
Jens Axboefe0f07d2015-04-15 17:05:48 -06008533 inode_dio_end(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008534 flags = DIO_LOCKING | DIO_SKIP_HOLES;
8535 wakeup = false;
Miao Xie09348562013-02-07 10:12:07 +00008536 }
8537
Omar Sandoval17f8c842015-03-16 04:33:50 -07008538 ret = __blockdev_direct_IO(iocb, inode,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008539 fs_info->fs_devices->latest_bdev,
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008540 iter, btrfs_get_blocks_direct, NULL,
Omar Sandoval17f8c842015-03-16 04:33:50 -07008541 btrfs_submit_direct, flags);
Omar Sandoval6f673762015-03-16 04:33:52 -07008542 if (iov_iter_rw(iter) == WRITE) {
Chandan Rajendra97dcdea2016-12-23 15:00:18 +05308543 up_read(&BTRFS_I(inode)->dio_sem);
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008544 current->journal_info = NULL;
Liu Boddba1bf2015-06-17 16:59:58 +08008545 if (ret < 0 && ret != -EIOCBQUEUED) {
chandan50745b02015-08-28 21:10:13 +05308546 if (dio_data.reserve)
Qu Wenruobc42bda2017-02-27 15:10:39 +08008547 btrfs_delalloc_release_space(inode, data_reserved,
Qu Wenruo43b18592017-12-12 15:34:32 +08008548 offset, dio_data.reserve, true);
Filipe Mananaf28a4922015-12-08 19:23:20 +00008549 /*
8550 * On error we might have left some ordered extents
8551 * without submitting corresponding bios for them, so
8552 * cleanup them up to avoid other tasks getting them
8553 * and waiting for them to complete forever.
8554 */
8555 if (dio_data.unsubmitted_oe_range_start <
8556 dio_data.unsubmitted_oe_range_end)
Qu Wenruo524272602017-03-08 10:25:52 +08008557 __endio_write_update_ordered(inode,
Filipe Mananaf28a4922015-12-08 19:23:20 +00008558 dio_data.unsubmitted_oe_range_start,
8559 dio_data.unsubmitted_oe_range_end -
8560 dio_data.unsubmitted_oe_range_start,
Qu Wenruo524272602017-03-08 10:25:52 +08008561 false);
Liu Boddba1bf2015-06-17 16:59:58 +08008562 } else if (ret >= 0 && (size_t)ret < count)
Qu Wenruobc42bda2017-02-27 15:10:39 +08008563 btrfs_delalloc_release_space(inode, data_reserved,
Qu Wenruo43b18592017-12-12 15:34:32 +08008564 offset, count - (size_t)ret, true);
8565 btrfs_delalloc_release_extents(BTRFS_I(inode), count, false);
Miao Xie09348562013-02-07 10:12:07 +00008566 }
Miao Xie38851cc2013-02-08 07:04:11 +00008567out:
Miao Xie2e60a512013-02-08 07:01:08 +00008568 if (wakeup)
Jens Axboefe0f07d2015-04-15 17:05:48 -06008569 inode_dio_end(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008570 if (relock)
Al Viro59551022016-01-22 15:40:57 -05008571 inode_lock(inode);
Miao Xie09348562013-02-07 10:12:07 +00008572
Qu Wenruo364ecf32017-02-27 15:10:38 +08008573 extent_changeset_free(data_reserved);
Miao Xie09348562013-02-07 10:12:07 +00008574 return ret;
Chris Mason16432982008-04-10 10:23:21 -04008575}
8576
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008577#define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
8578
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008579static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
8580 __u64 start, __u64 len)
8581{
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008582 int ret;
8583
8584 ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS);
8585 if (ret)
8586 return ret;
8587
David Sterba2135fb92017-06-23 04:09:57 +02008588 return extent_fiemap(inode, fieinfo, start, len);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008589}
8590
Chris Mason9ebefb182007-06-15 13:50:00 -04008591int btrfs_readpage(struct file *file, struct page *page)
8592{
Chris Masond1310b22008-01-24 16:13:08 -05008593 struct extent_io_tree *tree;
8594 tree = &BTRFS_I(page->mapping->host)->io_tree;
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02008595 return extent_read_full_page(tree, page, btrfs_get_extent, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04008596}
Chris Mason1832a6d2007-12-21 16:27:21 -05008597
Chris Mason39279cc2007-06-12 06:35:45 -04008598static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
8599{
Josef Bacikbe7bd732015-10-22 15:05:09 -04008600 struct inode *inode = page->mapping->host;
8601 int ret;
Chris Masonb888db22007-08-27 16:49:44 -04008602
8603 if (current->flags & PF_MEMALLOC) {
8604 redirty_page_for_writepage(wbc, page);
8605 unlock_page(page);
8606 return 0;
8607 }
Josef Bacikbe7bd732015-10-22 15:05:09 -04008608
8609 /*
8610 * If we are under memory pressure we will call this directly from the
8611 * VM, we need to make sure we have the inode referenced for the ordered
8612 * extent. If not just return like we didn't do anything.
8613 */
8614 if (!igrab(inode)) {
8615 redirty_page_for_writepage(wbc, page);
8616 return AOP_WRITEPAGE_ACTIVATE;
8617 }
Nikolay Borisov0a9b0e52017-12-08 15:55:59 +02008618 ret = extent_write_full_page(page, wbc);
Josef Bacikbe7bd732015-10-22 15:05:09 -04008619 btrfs_add_delayed_iput(inode);
8620 return ret;
Chris Masona52d9a82007-08-27 16:49:44 -04008621}
Chris Mason39279cc2007-06-12 06:35:45 -04008622
Eric Sandeen48a3b632013-04-25 20:41:01 +00008623static int btrfs_writepages(struct address_space *mapping,
8624 struct writeback_control *wbc)
Chris Masonb293f02e2007-11-01 19:45:34 -04008625{
Nikolay Borisov8ae225a2018-04-19 10:46:38 +03008626 return extent_writepages(mapping, wbc);
Chris Masonb293f02e2007-11-01 19:45:34 -04008627}
8628
Chris Mason3ab2fb52007-11-08 10:59:22 -05008629static int
8630btrfs_readpages(struct file *file, struct address_space *mapping,
8631 struct list_head *pages, unsigned nr_pages)
8632{
Nikolay Borisov2a3ff0a2018-04-19 10:46:36 +03008633 return extent_readpages(mapping, pages, nr_pages);
Chris Mason3ab2fb52007-11-08 10:59:22 -05008634}
Nikolay Borisov2a3ff0a2018-04-19 10:46:36 +03008635
Chris Masone6dcd2d2008-07-17 12:53:50 -04008636static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04008637{
Nikolay Borisov477a30b2018-04-19 10:46:34 +03008638 int ret = try_release_extent_mapping(page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04008639 if (ret == 1) {
8640 ClearPagePrivate(page);
8641 set_page_private(page, 0);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008642 put_page(page);
Chris Masona52d9a82007-08-27 16:49:44 -04008643 }
8644 return ret;
8645}
Chris Mason39279cc2007-06-12 06:35:45 -04008646
Chris Masone6dcd2d2008-07-17 12:53:50 -04008647static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
8648{
Chris Mason98509cf2008-09-11 15:51:43 -04008649 if (PageWriteback(page) || PageDirty(page))
8650 return 0;
Michal Hocko3ba7ab22017-01-09 15:39:02 +01008651 return __btrfs_releasepage(page, gfp_flags);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008652}
8653
Lukas Czernerd47992f2013-05-21 23:17:23 -04008654static void btrfs_invalidatepage(struct page *page, unsigned int offset,
8655 unsigned int length)
Chris Masona52d9a82007-08-27 16:49:44 -04008656{
Josef Bacik5fd02042012-05-02 14:00:54 -04008657 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05008658 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008659 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008660 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008661 u64 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008662 u64 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308663 u64 start;
8664 u64 end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008665 int inode_evicting = inode->i_state & I_FREEING;
Chris Masona52d9a82007-08-27 16:49:44 -04008666
Chris Mason8b62b722009-09-02 16:53:46 -04008667 /*
8668 * we have the page locked, so new writeback can't start,
8669 * and the dirty bit won't be cleared while we are here.
8670 *
8671 * Wait for IO on this page so that we can safely clear
8672 * the PagePrivate2 bit and do ordered accounting
8673 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008674 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04008675
Josef Bacik5fd02042012-05-02 14:00:54 -04008676 tree = &BTRFS_I(inode)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008677 if (offset) {
8678 btrfs_releasepage(page, GFP_NOFS);
8679 return;
8680 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008681
8682 if (!inode_evicting)
David Sterbaff13db42015-12-03 14:30:40 +01008683 lock_extent_bits(tree, page_start, page_end, &cached_state);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308684again:
8685 start = page_start;
Nikolay Borisova776c6f2017-02-20 13:50:49 +02008686 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), start,
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308687 page_end - start + 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008688 if (ordered) {
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308689 end = min(page_end, ordered->file_offset + ordered->len - 1);
Chris Masoneb84ae02008-07-17 13:53:27 -04008690 /*
8691 * IO on this page will never be started, so we need
8692 * to account for any ordered extents now
8693 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008694 if (!inode_evicting)
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308695 clear_extent_bit(tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008696 EXTENT_DIRTY | EXTENT_DELALLOC |
Filipe Mananaa7e3b972017-04-03 10:45:46 +01008697 EXTENT_DELALLOC_NEW |
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008698 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
David Sterbaae0f1622017-10-31 16:37:52 +01008699 EXTENT_DEFRAG, 1, 0, &cached_state);
Chris Mason8b62b722009-09-02 16:53:46 -04008700 /*
8701 * whoever cleared the private bit is responsible
8702 * for the finish_ordered_io
8703 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04008704 if (TestClearPagePrivate2(page)) {
8705 struct btrfs_ordered_inode_tree *tree;
8706 u64 new_len;
8707
8708 tree = &BTRFS_I(inode)->ordered_tree;
8709
8710 spin_lock_irq(&tree->lock);
8711 set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308712 new_len = start - ordered->file_offset;
Josef Bacik77cef2e2013-08-29 13:57:21 -04008713 if (new_len < ordered->truncated_len)
8714 ordered->truncated_len = new_len;
8715 spin_unlock_irq(&tree->lock);
8716
8717 if (btrfs_dec_test_ordered_pending(inode, &ordered,
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308718 start,
8719 end - start + 1, 1))
Josef Bacik77cef2e2013-08-29 13:57:21 -04008720 btrfs_finish_ordered_io(ordered);
Chris Mason8b62b722009-09-02 16:53:46 -04008721 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008722 btrfs_put_ordered_extent(ordered);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008723 if (!inode_evicting) {
8724 cached_state = NULL;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308725 lock_extent_bits(tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008726 &cached_state);
8727 }
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308728
8729 start = end + 1;
8730 if (start < page_end)
8731 goto again;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008732 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008733
Qu Wenruob9d0b382015-09-29 10:35:16 +08008734 /*
8735 * Qgroup reserved space handler
8736 * Page here will be either
8737 * 1) Already written to disk
8738 * In this case, its reserved space is released from data rsv map
8739 * and will be freed by delayed_ref handler finally.
8740 * So even we call qgroup_free_data(), it won't decrease reserved
8741 * space.
8742 * 2) Not written to disk
Goldwyn Rodrigues0b34c262016-09-30 10:40:52 -05008743 * This means the reserved space should be freed here. However,
8744 * if a truncate invalidates the page (by clearing PageDirty)
8745 * and the page is accounted for while allocating extent
8746 * in btrfs_check_data_free_space() we let delayed_ref to
8747 * free the entire extent.
Qu Wenruob9d0b382015-09-29 10:35:16 +08008748 */
Goldwyn Rodrigues0b34c262016-09-30 10:40:52 -05008749 if (PageDirty(page))
Qu Wenruobc42bda2017-02-27 15:10:39 +08008750 btrfs_qgroup_free_data(inode, NULL, page_start, PAGE_SIZE);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008751 if (!inode_evicting) {
8752 clear_extent_bit(tree, page_start, page_end,
8753 EXTENT_LOCKED | EXTENT_DIRTY |
Filipe Mananaa7e3b972017-04-03 10:45:46 +01008754 EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
8755 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1,
David Sterbaae0f1622017-10-31 16:37:52 +01008756 &cached_state);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008757
8758 __btrfs_releasepage(page, GFP_NOFS);
8759 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008760
Chris Mason4a096752008-07-21 10:29:44 -04008761 ClearPageChecked(page);
Chris Mason9ad6b7bc2008-04-18 16:11:30 -04008762 if (PagePrivate(page)) {
Chris Mason9ad6b7bc2008-04-18 16:11:30 -04008763 ClearPagePrivate(page);
8764 set_page_private(page, 0);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008765 put_page(page);
Chris Mason9ad6b7bc2008-04-18 16:11:30 -04008766 }
Chris Mason39279cc2007-06-12 06:35:45 -04008767}
8768
Chris Mason9ebefb182007-06-15 13:50:00 -04008769/*
8770 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
8771 * called from a page fault handler when a page is first dirtied. Hence we must
8772 * be careful to check for EOF conditions here. We set the page up correctly
8773 * for a written page which means we get ENOSPC checking when writing into
8774 * holes and correct delalloc and unwritten extent mapping on filesystems that
8775 * support these features.
8776 *
8777 * We are not allowed to take the i_mutex here so we have to play games to
8778 * protect against truncate races as the page could now be beyond EOF. Because
Omar Sandovald1342aa2018-05-11 13:13:29 -07008779 * truncate_setsize() writes the inode size before removing pages, once we have
8780 * the page lock we can determine safely if the page is beyond EOF. If it is not
Chris Mason9ebefb182007-06-15 13:50:00 -04008781 * beyond EOF, then the page is guaranteed safe against truncation until we
8782 * unlock the page.
8783 */
Souptick Joardera528a242018-06-06 19:54:44 +05308784vm_fault_t btrfs_page_mkwrite(struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04008785{
Nick Pigginc2ec1752009-03-31 15:23:21 -07008786 struct page *page = vmf->page;
Dave Jiang11bac802017-02-24 14:56:41 -08008787 struct inode *inode = file_inode(vmf->vma->vm_file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008788 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008789 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
8790 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008791 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08008792 struct extent_changeset *data_reserved = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008793 char *kaddr;
8794 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04008795 loff_t size;
Souptick Joardera528a242018-06-06 19:54:44 +05308796 vm_fault_t ret;
8797 int ret2;
Chris Mason9998eb72012-01-25 13:47:40 -05008798 int reserved = 0;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308799 u64 reserved_space;
Chris Masona52d9a82007-08-27 16:49:44 -04008800 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008801 u64 page_end;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308802 u64 end;
8803
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008804 reserved_space = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04008805
Jan Karab2b5ef52012-06-12 16:20:45 +02008806 sb_start_pagefault(inode->i_sb);
Qu Wenruodf480632015-09-08 17:25:54 +08008807 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008808 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308809 end = page_end;
Qu Wenruodf480632015-09-08 17:25:54 +08008810
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308811 /*
8812 * Reserving delalloc space after obtaining the page lock can lead to
8813 * deadlock. For example, if a dirty page is locked by this function
8814 * and the call to btrfs_delalloc_reserve_space() ends up triggering
8815 * dirty page write out, then the btrfs_writepage() function could
8816 * end up waiting indefinitely to get a lock on the page currently
8817 * being processed by btrfs_page_mkwrite() function.
8818 */
Souptick Joardera528a242018-06-06 19:54:44 +05308819 ret2 = btrfs_delalloc_reserve_space(inode, &data_reserved, page_start,
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308820 reserved_space);
Souptick Joardera528a242018-06-06 19:54:44 +05308821 if (!ret2) {
8822 ret2 = file_update_time(vmf->vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05008823 reserved = 1;
8824 }
Souptick Joardera528a242018-06-06 19:54:44 +05308825 if (ret2) {
8826 ret = vmf_error(ret2);
Chris Mason9998eb72012-01-25 13:47:40 -05008827 if (reserved)
8828 goto out;
8829 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07008830 }
Chris Mason1832a6d2007-12-21 16:27:21 -05008831
Nick Piggin56a76f82009-03-31 15:23:23 -07008832 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008833again:
Chris Mason9ebefb182007-06-15 13:50:00 -04008834 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04008835 size = i_size_read(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04008836
Chris Mason9ebefb182007-06-15 13:50:00 -04008837 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04008838 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04008839 /* page got truncated out from underneath us */
8840 goto out_unlock;
8841 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008842 wait_on_page_writeback(page);
8843
David Sterbaff13db42015-12-03 14:30:40 +01008844 lock_extent_bits(io_tree, page_start, page_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008845 set_page_extent_mapped(page);
8846
Chris Masoneb84ae02008-07-17 13:53:27 -04008847 /*
8848 * we can't set the delalloc bits if there are pending ordered
8849 * extents. Drop our locks and wait for them to finish
8850 */
Nikolay Borisova776c6f2017-02-20 13:50:49 +02008851 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start,
8852 PAGE_SIZE);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008853 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00008854 unlock_extent_cached(io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01008855 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008856 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04008857 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008858 btrfs_put_ordered_extent(ordered);
8859 goto again;
8860 }
8861
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008862 if (page->index == ((size - 1) >> PAGE_SHIFT)) {
Jeff Mahoneyda170662016-06-15 09:22:56 -04008863 reserved_space = round_up(size - page_start,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008864 fs_info->sectorsize);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008865 if (reserved_space < PAGE_SIZE) {
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308866 end = page_start + reserved_space - 1;
Qu Wenruobc42bda2017-02-27 15:10:39 +08008867 btrfs_delalloc_release_space(inode, data_reserved,
Qu Wenruo43b18592017-12-12 15:34:32 +08008868 page_start, PAGE_SIZE - reserved_space,
8869 true);
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308870 }
8871 }
8872
Josef Bacikfbf19082009-10-01 17:10:23 -04008873 /*
Liu Bo54160342016-12-13 12:15:19 -08008874 * page_mkwrite gets called when the page is firstly dirtied after it's
8875 * faulted in, but write(2) could also dirty a page and set delalloc
8876 * bits, thus in this case for space account reason, we still need to
8877 * clear any delalloc bits within this page range since we have to
8878 * reserve data&meta space before lock_page() (see above comments).
Josef Bacikfbf19082009-10-01 17:10:23 -04008879 */
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308880 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06008881 EXTENT_DIRTY | EXTENT_DELALLOC |
8882 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
David Sterbaae0f1622017-10-31 16:37:52 +01008883 0, 0, &cached_state);
Josef Bacikfbf19082009-10-01 17:10:23 -04008884
Souptick Joardera528a242018-06-06 19:54:44 +05308885 ret2 = btrfs_set_extent_delalloc(inode, page_start, end, 0,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08008886 &cached_state, 0);
Souptick Joardera528a242018-06-06 19:54:44 +05308887 if (ret2) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00008888 unlock_extent_cached(io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01008889 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04008890 ret = VM_FAULT_SIGBUS;
8891 goto out_unlock;
8892 }
Souptick Joardera528a242018-06-06 19:54:44 +05308893 ret2 = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04008894
8895 /* page is wholly or partially inside EOF */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008896 if (page_start + PAGE_SIZE > size)
8897 zero_start = size & ~PAGE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04008898 else
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008899 zero_start = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04008900
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008901 if (zero_start != PAGE_SIZE) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04008902 kaddr = kmap(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008903 memset(kaddr + zero_start, 0, PAGE_SIZE - zero_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008904 flush_dcache_page(page);
8905 kunmap(page);
8906 }
Chris Mason247e7432008-07-17 12:53:51 -04008907 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008908 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04008909 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04008910
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008911 BTRFS_I(inode)->last_trans = fs_info->generation;
Chris Mason257c62e2009-10-13 13:21:08 -04008912 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06008913 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
Chris Mason257c62e2009-10-13 13:21:08 -04008914
David Sterbae43bbe52017-12-12 21:43:52 +01008915 unlock_extent_cached(io_tree, page_start, page_end, &cached_state);
Chris Mason9ebefb182007-06-15 13:50:00 -04008916
Souptick Joardera528a242018-06-06 19:54:44 +05308917 if (!ret2) {
Qu Wenruo43b18592017-12-12 15:34:32 +08008918 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE, true);
Jan Karab2b5ef52012-06-12 16:20:45 +02008919 sb_end_pagefault(inode->i_sb);
Qu Wenruo364ecf32017-02-27 15:10:38 +08008920 extent_changeset_free(data_reserved);
Chris Mason50a9b212009-09-11 12:33:12 -04008921 return VM_FAULT_LOCKED;
Jan Karab2b5ef52012-06-12 16:20:45 +02008922 }
Chris Mason717beb92018-06-25 10:03:41 -07008923
8924out_unlock:
Chris Mason9ebefb182007-06-15 13:50:00 -04008925 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05008926out:
Qu Wenruo43b18592017-12-12 15:34:32 +08008927 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE, (ret != 0));
Qu Wenruobc42bda2017-02-27 15:10:39 +08008928 btrfs_delalloc_release_space(inode, data_reserved, page_start,
Qu Wenruo43b18592017-12-12 15:34:32 +08008929 reserved_space, (ret != 0));
Chris Mason9998eb72012-01-25 13:47:40 -05008930out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02008931 sb_end_pagefault(inode->i_sb);
Qu Wenruo364ecf32017-02-27 15:10:38 +08008932 extent_changeset_free(data_reserved);
Chris Mason9ebefb182007-06-15 13:50:00 -04008933 return ret;
8934}
8935
Filipe Manana213e8c52018-02-06 20:40:31 +00008936static int btrfs_truncate(struct inode *inode, bool skip_writeback)
Chris Mason39279cc2007-06-12 06:35:45 -04008937{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008938 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04008939 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04008940 struct btrfs_block_rsv *rsv;
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008941 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04008942 struct btrfs_trans_handle *trans;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008943 u64 mask = fs_info->sectorsize - 1;
8944 u64 min_size = btrfs_calc_trunc_metadata_size(fs_info, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04008945
Filipe Manana213e8c52018-02-06 20:40:31 +00008946 if (!skip_writeback) {
8947 ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask),
8948 (u64)-1);
8949 if (ret)
8950 return ret;
8951 }
Chris Mason39279cc2007-06-12 06:35:45 -04008952
Josef Bacikfcb80c22011-05-03 10:40:22 -04008953 /*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008954 * Yes ladies and gentlemen, this is indeed ugly. We have a couple of
8955 * things going on here:
Josef Bacikfcb80c22011-05-03 10:40:22 -04008956 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008957 * 1) We need to reserve space to update our inode.
Josef Bacikfcb80c22011-05-03 10:40:22 -04008958 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008959 * 2) We need to have something to cache all the space that is going to
Josef Bacikfcb80c22011-05-03 10:40:22 -04008960 * be free'd up by the truncate operation, but also have some slack
8961 * space reserved in case it uses space during the truncate (thank you
8962 * very much snapshotting).
8963 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008964 * And we need these to be separate. The fact is we can use a lot of
Josef Bacikfcb80c22011-05-03 10:40:22 -04008965 * space doing the truncate, and we have no earthly idea how much space
Nicholas D Steeves01327612016-05-19 21:18:45 -04008966 * we will use, so we need the truncate reservation to be separate so it
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008967 * doesn't end up using space reserved for updating the inode. We also
8968 * need to be able to stop the transaction and start a new one, which
8969 * means we need to be able to update the inode several times, and we
8970 * have no idea of knowing how many times that will be, so we can't just
8971 * reserve 1 item for the entirety of the operation, so that has to be
8972 * done separately as well.
Josef Bacikfcb80c22011-05-03 10:40:22 -04008973 *
8974 * So that leaves us with
8975 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008976 * 1) rsv - for the truncate reservation, which we will steal from the
Josef Bacikfcb80c22011-05-03 10:40:22 -04008977 * transaction reservation.
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008978 * 2) fs_info->trans_block_rsv - this will have 1 items worth left for
Josef Bacikfcb80c22011-05-03 10:40:22 -04008979 * updating the inode.
8980 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008981 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008982 if (!rsv)
8983 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04008984 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04008985 rsv->failfast = 1;
Josef Bacikf0cd8462011-03-04 14:37:08 -05008986
Josef Bacik907cbce2011-08-08 13:46:15 -04008987 /*
Josef Bacik07127182011-08-19 10:29:59 -04008988 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04008989 * 1 for updating the inode.
8990 */
Josef Bacikf3fe8202013-01-07 17:03:21 -05008991 trans = btrfs_start_transaction(root, 2);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008992 if (IS_ERR(trans)) {
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008993 ret = PTR_ERR(trans);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008994 goto out;
8995 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05008996
Josef Bacik907cbce2011-08-08 13:46:15 -04008997 /* Migrate the slack space for the truncate to our reserve */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008998 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, rsv,
Lu Fengqi3a584172018-08-04 21:10:55 +08008999 min_size, false);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009000 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05009001
Chris Mason5a3f23d2009-03-31 13:27:11 -04009002 /*
Josef Bacik5dc562c2012-08-17 13:14:17 -04009003 * So if we truncate and then write and fsync we normally would just
9004 * write the extents that changed, which is a problem if we need to
9005 * first truncate that entire inode. So set this flag so we write out
9006 * all of the extents in the inode to the sync log so we're completely
9007 * safe.
9008 */
9009 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009010 trans->block_rsv = rsv;
Josef Bacik5dc562c2012-08-17 13:14:17 -04009011
Yan, Zheng80825102009-11-12 09:35:36 +00009012 while (1) {
9013 ret = btrfs_truncate_inode_items(trans, root, inode,
9014 inode->i_size,
9015 BTRFS_EXTENT_DATA_KEY);
Josef Bacikddfae632017-10-19 14:16:02 -04009016 trans->block_rsv = &fs_info->trans_block_rsv;
Omar Sandovalad7e1a72018-05-22 09:59:50 -07009017 if (ret != -ENOSPC && ret != -EAGAIN)
Yan, Zheng80825102009-11-12 09:35:36 +00009018 break;
Chris Mason39279cc2007-06-12 06:35:45 -04009019
Yan, Zheng80825102009-11-12 09:35:36 +00009020 ret = btrfs_update_inode(trans, root, inode);
Omar Sandovalad7e1a72018-05-22 09:59:50 -07009021 if (ret)
Josef Bacik3893e332011-01-31 16:03:11 -05009022 break;
Josef Bacikca7e70f2012-08-27 17:48:15 -04009023
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009024 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009025 btrfs_btree_balance_dirty(fs_info);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009026
9027 trans = btrfs_start_transaction(root, 2);
9028 if (IS_ERR(trans)) {
Omar Sandovalad7e1a72018-05-22 09:59:50 -07009029 ret = PTR_ERR(trans);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009030 trans = NULL;
9031 break;
9032 }
9033
Wang Xiaoguang47b5d642016-09-07 20:17:38 +08009034 btrfs_block_rsv_release(fs_info, rsv, -1);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009035 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv,
Lu Fengqi3a584172018-08-04 21:10:55 +08009036 rsv, min_size, false);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009037 BUG_ON(ret); /* shouldn't happen */
9038 trans->block_rsv = rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00009039 }
9040
Josef Bacikddfae632017-10-19 14:16:02 -04009041 /*
9042 * We can't call btrfs_truncate_block inside a trans handle as we could
9043 * deadlock with freeze, if we got NEED_TRUNCATE_BLOCK then we know
9044 * we've truncated everything except the last little bit, and can do
9045 * btrfs_truncate_block and then update the disk_i_size.
9046 */
9047 if (ret == NEED_TRUNCATE_BLOCK) {
9048 btrfs_end_transaction(trans);
9049 btrfs_btree_balance_dirty(fs_info);
9050
9051 ret = btrfs_truncate_block(inode, inode->i_size, 0, 0);
9052 if (ret)
9053 goto out;
9054 trans = btrfs_start_transaction(root, 1);
9055 if (IS_ERR(trans)) {
9056 ret = PTR_ERR(trans);
9057 goto out;
9058 }
9059 btrfs_ordered_update_i_size(inode, inode->i_size, NULL);
9060 }
9061
Chris Mason917c16b2011-11-08 14:49:59 -05009062 if (trans) {
Omar Sandovalad7e1a72018-05-22 09:59:50 -07009063 int ret2;
Josef Bacik7b128762008-07-24 12:17:14 -04009064
Omar Sandovalad7e1a72018-05-22 09:59:50 -07009065 trans->block_rsv = &fs_info->trans_block_rsv;
9066 ret2 = btrfs_update_inode(trans, root, inode);
9067 if (ret2 && !ret)
9068 ret = ret2;
9069
9070 ret2 = btrfs_end_transaction(trans);
9071 if (ret2 && !ret)
9072 ret = ret2;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009073 btrfs_btree_balance_dirty(fs_info);
Chris Mason917c16b2011-11-08 14:49:59 -05009074 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04009075out:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009076 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009077
Omar Sandovalad7e1a72018-05-22 09:59:50 -07009078 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04009079}
9080
Sven Wegener3b963622008-06-09 21:57:42 -04009081/*
Chris Masond352ac62008-09-29 15:18:18 -04009082 * create a new subvolume directory/inode (helper for the ioctl).
9083 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05009084int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009085 struct btrfs_root *new_root,
9086 struct btrfs_root *parent_root,
9087 u64 new_dirid)
Chris Mason39279cc2007-06-12 06:35:45 -04009088{
Chris Mason39279cc2007-06-12 06:35:45 -04009089 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04009090 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04009091 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009092
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01009093 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
9094 new_dirid, new_dirid,
9095 S_IFDIR | (~current_umask() & S_IRWXUGO),
9096 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04009097 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04009098 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009099 inode->i_op = &btrfs_dir_inode_operations;
9100 inode->i_fop = &btrfs_dir_file_operations;
9101
Miklos Szeredibfe86842011-10-28 14:13:29 +02009102 set_nlink(inode, 1);
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02009103 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Masonb0d5d102014-09-08 13:08:51 -07009104 unlock_new_inode(inode);
Sven Wegener3b963622008-06-09 21:57:42 -04009105
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009106 err = btrfs_subvol_inherit_props(trans, new_root, parent_root);
9107 if (err)
9108 btrfs_err(new_root->fs_info,
David Sterba351fd352014-05-15 16:48:20 +02009109 "error inheriting subvolume %llu properties: %d",
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009110 new_root->root_key.objectid, err);
9111
Yan, Zheng76dda932009-09-21 16:00:26 -04009112 err = btrfs_update_inode(trans, new_root, inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04009113
Yan, Zheng76dda932009-09-21 16:00:26 -04009114 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07009115 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04009116}
9117
Chris Mason39279cc2007-06-12 06:35:45 -04009118struct inode *btrfs_alloc_inode(struct super_block *sb)
9119{
Josef Bacik69fe2d72017-10-19 14:15:57 -04009120 struct btrfs_fs_info *fs_info = btrfs_sb(sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009121 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009122 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009123
David Sterba712e36c2017-10-31 17:08:27 +01009124 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_KERNEL);
Chris Mason39279cc2007-06-12 06:35:45 -04009125 if (!ei)
9126 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009127
9128 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009129 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04009130 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04009131 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04009132 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009133 ei->delalloc_bytes = 0;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01009134 ei->new_delalloc_bytes = 0;
Wang Shilong47059d92014-07-03 18:22:07 +08009135 ei->defrag_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009136 ei->disk_i_size = 0;
9137 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04009138 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009139 ei->index_cnt = (u64)-1;
Miao Xie67de1172013-12-26 13:07:06 +08009140 ei->dir_index = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009141 ei->last_unlink_trans = 0;
Filipe Manana41bd6062018-11-28 14:54:28 +00009142 ei->last_link_trans = 0;
Liu Bo46d8bc32012-08-29 01:07:55 -06009143 ei->last_log_commit = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009144
Josef Bacik9e0baf62011-07-15 15:16:44 +00009145 spin_lock_init(&ei->lock);
9146 ei->outstanding_extents = 0;
Josef Bacik69fe2d72017-10-19 14:15:57 -04009147 if (sb->s_magic != BTRFS_TEST_MAGIC)
9148 btrfs_init_metadata_block_rsv(fs_info, &ei->block_rsv,
9149 BTRFS_BLOCK_RSV_DELALLOC);
Josef Bacik72ac3c02012-05-23 14:13:11 -04009150 ei->runtime_flags = 0;
David Sterbab52aa8c2017-07-17 19:17:20 +02009151 ei->prop_compress = BTRFS_COMPRESS_NONE;
David Sterbaeec63c62017-07-17 19:41:31 +02009152 ei->defrag_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009153
Miao Xie16cdcec2011-04-22 18:12:22 +08009154 ei->delayed_node = NULL;
9155
chandan r9cc97d62012-07-04 12:48:07 +05309156 ei->i_otime.tv_sec = 0;
9157 ei->i_otime.tv_nsec = 0;
9158
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009159 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02009160 extent_map_tree_init(&ei->extent_tree);
Josef Bacikc6100a42017-05-05 11:57:13 -04009161 extent_io_tree_init(&ei->io_tree, inode);
9162 extent_io_tree_init(&ei->io_failure_tree, inode);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04009163 ei->io_tree.track_uptodate = 1;
9164 ei->io_failure_tree.track_uptodate = 1;
Josef Bacikb812ce22012-11-16 13:56:32 -05009165 atomic_set(&ei->sync_writers, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009166 mutex_init(&ei->log_mutex);
Josef Bacikf2486792012-01-13 12:09:22 -05009167 mutex_init(&ei->delalloc_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009168 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009169 INIT_LIST_HEAD(&ei->delalloc_inodes);
David Sterba8089fe62015-11-19 14:15:51 +01009170 INIT_LIST_HEAD(&ei->delayed_iput);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009171 RB_CLEAR_NODE(&ei->rb_node);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01009172 init_rwsem(&ei->dio_sem);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009173
9174 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009175}
9176
Josef Bacikaaedb552013-10-11 14:44:09 -04009177#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
9178void btrfs_test_destroy_inode(struct inode *inode)
9179{
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02009180 btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0);
Josef Bacikaaedb552013-10-11 14:44:09 -04009181 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
9182}
9183#endif
9184
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009185static void btrfs_i_callback(struct rcu_head *head)
9186{
9187 struct inode *inode = container_of(head, struct inode, i_rcu);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009188 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
9189}
9190
Chris Mason39279cc2007-06-12 06:35:45 -04009191void btrfs_destroy_inode(struct inode *inode)
9192{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009193 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009194 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009195 struct btrfs_root *root = BTRFS_I(inode)->root;
9196
Al Virob3d9b7a2012-06-09 13:51:19 -04009197 WARN_ON(!hlist_empty(&inode->i_dentry));
Chris Mason39279cc2007-06-12 06:35:45 -04009198 WARN_ON(inode->i_data.nrpages);
Josef Bacik69fe2d72017-10-19 14:15:57 -04009199 WARN_ON(BTRFS_I(inode)->block_rsv.reserved);
9200 WARN_ON(BTRFS_I(inode)->block_rsv.size);
Josef Bacik9e0baf62011-07-15 15:16:44 +00009201 WARN_ON(BTRFS_I(inode)->outstanding_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04009202 WARN_ON(BTRFS_I(inode)->delalloc_bytes);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01009203 WARN_ON(BTRFS_I(inode)->new_delalloc_bytes);
Josef Bacik7709cde2011-08-04 10:25:02 -04009204 WARN_ON(BTRFS_I(inode)->csum_bytes);
Wang Shilong47059d92014-07-03 18:22:07 +08009205 WARN_ON(BTRFS_I(inode)->defrag_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04009206
Chris Mason5a3f23d2009-03-31 13:27:11 -04009207 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05009208 * This can happen where we create an inode, but somebody else also
9209 * created the same inode and we need to destroy the one we already
9210 * created.
9211 */
9212 if (!root)
9213 goto free;
9214
Chris Masond3977122009-01-05 21:25:51 -05009215 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04009216 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
9217 if (!ordered)
9218 break;
9219 else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009220 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04009221 "found ordered extent %llu %llu on inode cleanup",
9222 ordered->file_offset, ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009223 btrfs_remove_ordered_extent(inode, ordered);
9224 btrfs_put_ordered_extent(ordered);
9225 btrfs_put_ordered_extent(ordered);
9226 }
9227 }
Qu Wenruo56fa9d02015-10-13 09:53:10 +08009228 btrfs_qgroup_check_reserved_leak(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04009229 inode_tree_del(inode);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02009230 btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0);
Josef Bacika6dbd422009-11-11 15:53:34 -05009231free:
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009232 call_rcu(&inode->i_rcu, btrfs_i_callback);
Chris Mason39279cc2007-06-12 06:35:45 -04009233}
9234
Al Viro45321ac2010-06-07 13:43:19 -04009235int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04009236{
9237 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04009238
Naohiro Aota6379ef92013-06-06 09:56:34 +00009239 if (root == NULL)
9240 return 1;
9241
Liu Bofa6ac872013-02-20 14:10:23 +00009242 /* the snap/subvol tree is on deleting */
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02009243 if (btrfs_root_refs(&root->root_item) == 0)
Al Viro45321ac2010-06-07 13:43:19 -04009244 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04009245 else
Al Viro45321ac2010-06-07 13:43:19 -04009246 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04009247}
9248
Sven Wegener0ee0fda2008-07-30 16:54:26 -04009249static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04009250{
9251 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
9252
9253 inode_init_once(&ei->vfs_inode);
9254}
9255
David Sterbae67c7182018-02-19 17:24:18 +01009256void __cold btrfs_destroy_cachep(void)
Chris Mason39279cc2007-06-12 06:35:45 -04009257{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10009258 /*
9259 * Make sure all delayed rcu free inodes are flushed before we
9260 * destroy cache.
9261 */
9262 rcu_barrier();
Kinglong Mee5598e902016-01-29 21:36:35 +08009263 kmem_cache_destroy(btrfs_inode_cachep);
9264 kmem_cache_destroy(btrfs_trans_handle_cachep);
Kinglong Mee5598e902016-01-29 21:36:35 +08009265 kmem_cache_destroy(btrfs_path_cachep);
9266 kmem_cache_destroy(btrfs_free_space_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04009267}
9268
Liu Bof5c29bd2017-11-02 17:21:50 -06009269int __init btrfs_init_cachep(void)
Chris Mason39279cc2007-06-12 06:35:45 -04009270{
David Sterba837e1972012-09-07 03:00:48 -06009271 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009272 sizeof(struct btrfs_inode), 0,
Vladimir Davydov5d097052016-01-14 15:18:21 -08009273 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD | SLAB_ACCOUNT,
9274 init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04009275 if (!btrfs_inode_cachep)
9276 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009277
David Sterba837e1972012-09-07 03:00:48 -06009278 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009279 sizeof(struct btrfs_trans_handle), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009280 SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009281 if (!btrfs_trans_handle_cachep)
9282 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009283
David Sterba837e1972012-09-07 03:00:48 -06009284 btrfs_path_cachep = kmem_cache_create("btrfs_path",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009285 sizeof(struct btrfs_path), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009286 SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009287 if (!btrfs_path_cachep)
9288 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009289
David Sterba837e1972012-09-07 03:00:48 -06009290 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
Josef Bacikdc89e982011-01-28 17:05:48 -05009291 sizeof(struct btrfs_free_space), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009292 SLAB_MEM_SPREAD, NULL);
Josef Bacikdc89e982011-01-28 17:05:48 -05009293 if (!btrfs_free_space_cachep)
9294 goto fail;
9295
Chris Mason39279cc2007-06-12 06:35:45 -04009296 return 0;
9297fail:
9298 btrfs_destroy_cachep();
9299 return -ENOMEM;
9300}
9301
David Howellsa528d352017-01-31 16:46:22 +00009302static int btrfs_getattr(const struct path *path, struct kstat *stat,
9303 u32 request_mask, unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04009304{
Miao Xiedf0af1a2013-01-29 10:11:59 +00009305 u64 delalloc_bytes;
David Howellsa528d352017-01-31 16:46:22 +00009306 struct inode *inode = d_inode(path->dentry);
David Sterbafadc0d82011-11-20 07:33:38 -05009307 u32 blocksize = inode->i_sb->s_blocksize;
Yonghong Song04a87e32017-05-12 15:07:43 -07009308 u32 bi_flags = BTRFS_I(inode)->flags;
9309
9310 stat->result_mask |= STATX_BTIME;
9311 stat->btime.tv_sec = BTRFS_I(inode)->i_otime.tv_sec;
9312 stat->btime.tv_nsec = BTRFS_I(inode)->i_otime.tv_nsec;
9313 if (bi_flags & BTRFS_INODE_APPEND)
9314 stat->attributes |= STATX_ATTR_APPEND;
9315 if (bi_flags & BTRFS_INODE_COMPRESS)
9316 stat->attributes |= STATX_ATTR_COMPRESSED;
9317 if (bi_flags & BTRFS_INODE_IMMUTABLE)
9318 stat->attributes |= STATX_ATTR_IMMUTABLE;
9319 if (bi_flags & BTRFS_INODE_NODUMP)
9320 stat->attributes |= STATX_ATTR_NODUMP;
9321
9322 stat->attributes_mask |= (STATX_ATTR_APPEND |
9323 STATX_ATTR_COMPRESSED |
9324 STATX_ATTR_IMMUTABLE |
9325 STATX_ATTR_NODUMP);
David Sterbafadc0d82011-11-20 07:33:38 -05009326
Chris Mason39279cc2007-06-12 06:35:45 -04009327 generic_fillattr(inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04009328 stat->dev = BTRFS_I(inode)->root->anon_dev;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009329
9330 spin_lock(&BTRFS_I(inode)->lock);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01009331 delalloc_bytes = BTRFS_I(inode)->new_delalloc_bytes;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009332 spin_unlock(&BTRFS_I(inode)->lock);
David Sterbafadc0d82011-11-20 07:33:38 -05009333 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
Miao Xiedf0af1a2013-01-29 10:11:59 +00009334 ALIGN(delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04009335 return 0;
9336}
9337
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009338static int btrfs_rename_exchange(struct inode *old_dir,
9339 struct dentry *old_dentry,
9340 struct inode *new_dir,
9341 struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04009342{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009343 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009344 struct btrfs_trans_handle *trans;
9345 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009346 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009347 struct inode *new_inode = new_dentry->d_inode;
9348 struct inode *old_inode = old_dentry->d_inode;
Deepa Dinamani95582b02018-05-08 19:36:02 -07009349 struct timespec64 ctime = current_time(old_inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009350 struct dentry *parent;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009351 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
9352 u64 new_ino = btrfs_ino(BTRFS_I(new_inode));
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009353 u64 old_idx = 0;
9354 u64 new_idx = 0;
9355 u64 root_objectid;
9356 int ret;
Filipe Manana86e8aa02016-05-05 02:02:27 +01009357 bool root_log_pinned = false;
9358 bool dest_log_pinned = false;
Filipe Mananad4682ba2018-06-11 19:24:28 +01009359 struct btrfs_log_ctx ctx_root;
9360 struct btrfs_log_ctx ctx_dest;
9361 bool sync_log_root = false;
9362 bool sync_log_dest = false;
9363 bool commit_transaction = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009364
9365 /* we only allow rename subvolume link between subvolumes */
9366 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
9367 return -EXDEV;
9368
Filipe Mananad4682ba2018-06-11 19:24:28 +01009369 btrfs_init_log_ctx(&ctx_root, old_inode);
9370 btrfs_init_log_ctx(&ctx_dest, new_inode);
9371
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009372 /* close the race window with snapshot create/destroy ioctl */
9373 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009374 down_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009375 if (new_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009376 down_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009377
9378 /*
9379 * We want to reserve the absolute worst case amount of items. So if
9380 * both inodes are subvols and we need to unlink them then that would
9381 * require 4 item modifications, but if they are both normal inodes it
9382 * would require 5 item modifications, so we'll assume their normal
9383 * inodes. So 5 * 2 is 10, plus 2 for the new links, so 12 total items
9384 * should cover the worst case number of items we'll modify.
9385 */
9386 trans = btrfs_start_transaction(root, 12);
9387 if (IS_ERR(trans)) {
9388 ret = PTR_ERR(trans);
9389 goto out_notrans;
9390 }
9391
9392 /*
9393 * We need to find a free sequence number both in the source and
9394 * in the destination directory for the exchange.
9395 */
Nikolay Borisov877574e2017-02-20 13:50:33 +02009396 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009397 if (ret)
9398 goto out_fail;
Nikolay Borisov877574e2017-02-20 13:50:33 +02009399 ret = btrfs_set_inode_index(BTRFS_I(old_dir), &new_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009400 if (ret)
9401 goto out_fail;
9402
9403 BTRFS_I(old_inode)->dir_index = 0ULL;
9404 BTRFS_I(new_inode)->dir_index = 0ULL;
9405
9406 /* Reference for the source. */
9407 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
9408 /* force full log commit if subvolume involved. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009409 btrfs_set_log_full_commit(fs_info, trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009410 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01009411 btrfs_pin_log_trans(root);
9412 root_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009413 ret = btrfs_insert_inode_ref(trans, dest,
9414 new_dentry->d_name.name,
9415 new_dentry->d_name.len,
9416 old_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01009417 btrfs_ino(BTRFS_I(new_dir)),
9418 old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009419 if (ret)
9420 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009421 }
9422
9423 /* And now for the dest. */
9424 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
9425 /* force full log commit if subvolume involved. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009426 btrfs_set_log_full_commit(fs_info, trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009427 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01009428 btrfs_pin_log_trans(dest);
9429 dest_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009430 ret = btrfs_insert_inode_ref(trans, root,
9431 old_dentry->d_name.name,
9432 old_dentry->d_name.len,
9433 new_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01009434 btrfs_ino(BTRFS_I(old_dir)),
9435 new_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009436 if (ret)
9437 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009438 }
9439
9440 /* Update inode version and ctime/mtime. */
9441 inode_inc_iversion(old_dir);
9442 inode_inc_iversion(new_dir);
9443 inode_inc_iversion(old_inode);
9444 inode_inc_iversion(new_inode);
9445 old_dir->i_ctime = old_dir->i_mtime = ctime;
9446 new_dir->i_ctime = new_dir->i_mtime = ctime;
9447 old_inode->i_ctime = ctime;
9448 new_inode->i_ctime = ctime;
9449
9450 if (old_dentry->d_parent != new_dentry->d_parent) {
David Sterbaf85b7372017-01-20 14:54:07 +01009451 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
9452 BTRFS_I(old_inode), 1);
9453 btrfs_record_unlink_dir(trans, BTRFS_I(new_dir),
9454 BTRFS_I(new_inode), 1);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009455 }
9456
9457 /* src is a subvolume */
9458 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
9459 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
Lu Fengqi401b3b12018-08-01 11:32:30 +08009460 ret = btrfs_unlink_subvol(trans, old_dir, root_objectid,
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009461 old_dentry->d_name.name,
9462 old_dentry->d_name.len);
9463 } else { /* src is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009464 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
9465 BTRFS_I(old_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009466 old_dentry->d_name.name,
9467 old_dentry->d_name.len);
9468 if (!ret)
9469 ret = btrfs_update_inode(trans, root, old_inode);
9470 }
9471 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009472 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009473 goto out_fail;
9474 }
9475
9476 /* dest is a subvolume */
9477 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
9478 root_objectid = BTRFS_I(new_inode)->root->root_key.objectid;
Lu Fengqi401b3b12018-08-01 11:32:30 +08009479 ret = btrfs_unlink_subvol(trans, new_dir, root_objectid,
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009480 new_dentry->d_name.name,
9481 new_dentry->d_name.len);
9482 } else { /* dest is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009483 ret = __btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
9484 BTRFS_I(new_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009485 new_dentry->d_name.name,
9486 new_dentry->d_name.len);
9487 if (!ret)
9488 ret = btrfs_update_inode(trans, dest, new_inode);
9489 }
9490 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009491 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009492 goto out_fail;
9493 }
9494
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009495 ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009496 new_dentry->d_name.name,
9497 new_dentry->d_name.len, 0, old_idx);
9498 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009499 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009500 goto out_fail;
9501 }
9502
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009503 ret = btrfs_add_link(trans, BTRFS_I(old_dir), BTRFS_I(new_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009504 old_dentry->d_name.name,
9505 old_dentry->d_name.len, 0, new_idx);
9506 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009507 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009508 goto out_fail;
9509 }
9510
9511 if (old_inode->i_nlink == 1)
9512 BTRFS_I(old_inode)->dir_index = old_idx;
9513 if (new_inode->i_nlink == 1)
9514 BTRFS_I(new_inode)->dir_index = new_idx;
9515
Filipe Manana86e8aa02016-05-05 02:02:27 +01009516 if (root_log_pinned) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009517 parent = new_dentry->d_parent;
Filipe Mananad4682ba2018-06-11 19:24:28 +01009518 ret = btrfs_log_new_name(trans, BTRFS_I(old_inode),
9519 BTRFS_I(old_dir), parent,
9520 false, &ctx_root);
9521 if (ret == BTRFS_NEED_LOG_SYNC)
9522 sync_log_root = true;
9523 else if (ret == BTRFS_NEED_TRANS_COMMIT)
9524 commit_transaction = true;
9525 ret = 0;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009526 btrfs_end_log_trans(root);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009527 root_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009528 }
Filipe Manana86e8aa02016-05-05 02:02:27 +01009529 if (dest_log_pinned) {
Filipe Mananad4682ba2018-06-11 19:24:28 +01009530 if (!commit_transaction) {
9531 parent = old_dentry->d_parent;
9532 ret = btrfs_log_new_name(trans, BTRFS_I(new_inode),
9533 BTRFS_I(new_dir), parent,
9534 false, &ctx_dest);
9535 if (ret == BTRFS_NEED_LOG_SYNC)
9536 sync_log_dest = true;
9537 else if (ret == BTRFS_NEED_TRANS_COMMIT)
9538 commit_transaction = true;
9539 ret = 0;
9540 }
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009541 btrfs_end_log_trans(dest);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009542 dest_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009543 }
9544out_fail:
Filipe Manana86e8aa02016-05-05 02:02:27 +01009545 /*
9546 * If we have pinned a log and an error happened, we unpin tasks
9547 * trying to sync the log and force them to fallback to a transaction
9548 * commit if the log currently contains any of the inodes involved in
9549 * this rename operation (to ensure we do not persist a log with an
9550 * inconsistent state for any of these inodes or leading to any
9551 * inconsistencies when replayed). If the transaction was aborted, the
9552 * abortion reason is propagated to userspace when attempting to commit
9553 * the transaction. If the log does not contain any of these inodes, we
9554 * allow the tasks to sync it.
9555 */
9556 if (ret && (root_log_pinned || dest_log_pinned)) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009557 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
9558 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
9559 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana86e8aa02016-05-05 02:02:27 +01009560 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009561 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009562 btrfs_set_log_full_commit(fs_info, trans);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009563
9564 if (root_log_pinned) {
9565 btrfs_end_log_trans(root);
9566 root_log_pinned = false;
9567 }
9568 if (dest_log_pinned) {
9569 btrfs_end_log_trans(dest);
9570 dest_log_pinned = false;
9571 }
9572 }
Filipe Mananad4682ba2018-06-11 19:24:28 +01009573 if (!ret && sync_log_root && !commit_transaction) {
9574 ret = btrfs_sync_log(trans, BTRFS_I(old_inode)->root,
9575 &ctx_root);
9576 if (ret)
9577 commit_transaction = true;
9578 }
9579 if (!ret && sync_log_dest && !commit_transaction) {
9580 ret = btrfs_sync_log(trans, BTRFS_I(new_inode)->root,
9581 &ctx_dest);
9582 if (ret)
9583 commit_transaction = true;
9584 }
9585 if (commit_transaction) {
9586 ret = btrfs_commit_transaction(trans);
9587 } else {
9588 int ret2;
9589
9590 ret2 = btrfs_end_transaction(trans);
9591 ret = ret ? ret : ret2;
9592 }
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009593out_notrans:
9594 if (new_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009595 up_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009596 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009597 up_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009598
9599 return ret;
9600}
9601
9602static int btrfs_whiteout_for_rename(struct btrfs_trans_handle *trans,
9603 struct btrfs_root *root,
9604 struct inode *dir,
9605 struct dentry *dentry)
9606{
9607 int ret;
9608 struct inode *inode;
9609 u64 objectid;
9610 u64 index;
9611
9612 ret = btrfs_find_free_ino(root, &objectid);
9613 if (ret)
9614 return ret;
9615
9616 inode = btrfs_new_inode(trans, root, dir,
9617 dentry->d_name.name,
9618 dentry->d_name.len,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009619 btrfs_ino(BTRFS_I(dir)),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009620 objectid,
9621 S_IFCHR | WHITEOUT_MODE,
9622 &index);
9623
9624 if (IS_ERR(inode)) {
9625 ret = PTR_ERR(inode);
9626 return ret;
9627 }
9628
9629 inode->i_op = &btrfs_special_inode_operations;
9630 init_special_inode(inode, inode->i_mode,
9631 WHITEOUT_DEV);
9632
9633 ret = btrfs_init_inode_security(trans, inode, dir,
9634 &dentry->d_name);
9635 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009636 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009637
Nikolay Borisovcef415a2017-02-20 13:51:09 +02009638 ret = btrfs_add_nondir(trans, BTRFS_I(dir), dentry,
9639 BTRFS_I(inode), 0, index);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009640 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009641 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009642
9643 ret = btrfs_update_inode(trans, root, inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009644out:
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009645 unlock_new_inode(inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009646 if (ret)
9647 inode_dec_link_count(inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009648 iput(inode);
9649
Filipe Mananac9901612016-05-05 01:41:57 +01009650 return ret;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009651}
9652
Chris Mason39279cc2007-06-12 06:35:45 -04009653static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009654 struct inode *new_dir, struct dentry *new_dentry,
9655 unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04009656{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009657 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009658 struct btrfs_trans_handle *trans;
Filipe Manana5062af32016-05-05 10:26:26 +01009659 unsigned int trans_num_items;
Chris Mason39279cc2007-06-12 06:35:45 -04009660 struct btrfs_root *root = BTRFS_I(old_dir)->root;
9661 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00009662 struct inode *new_inode = d_inode(new_dentry);
9663 struct inode *old_inode = d_inode(old_dentry);
Chris Mason00e4e6b2008-08-05 11:18:09 -04009664 u64 index = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009665 u64 root_objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04009666 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009667 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009668 bool log_pinned = false;
Filipe Mananad4682ba2018-06-11 19:24:28 +01009669 struct btrfs_log_ctx ctx;
9670 bool sync_log = false;
9671 bool commit_transaction = false;
Chris Mason39279cc2007-06-12 06:35:45 -04009672
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009673 if (btrfs_ino(BTRFS_I(new_dir)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04009674 return -EPERM;
9675
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009676 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08009677 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05009678 return -EXDEV;
9679
Li Zefan33345d012011-04-20 10:31:50 +08009680 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009681 (new_inode && btrfs_ino(BTRFS_I(new_inode)) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009682 return -ENOTEMPTY;
9683
Chris Mason39279cc2007-06-12 06:35:45 -04009684 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009685 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04009686 return -ENOTEMPTY;
Chris Mason9c520572012-12-17 14:26:57 -05009687
9688
9689 /* check for collisions, even if the name isn't there */
Josef Bacik4871c152013-10-09 12:24:04 -04009690 ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino,
Chris Mason9c520572012-12-17 14:26:57 -05009691 new_dentry->d_name.name,
9692 new_dentry->d_name.len);
9693
9694 if (ret) {
9695 if (ret == -EEXIST) {
9696 /* we shouldn't get
9697 * eexist without a new_inode */
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05309698 if (WARN_ON(!new_inode)) {
Chris Mason9c520572012-12-17 14:26:57 -05009699 return ret;
9700 }
9701 } else {
9702 /* maybe -EOVERFLOW */
9703 return ret;
9704 }
9705 }
9706 ret = 0;
9707
Chris Mason5a3f23d2009-03-31 13:27:11 -04009708 /*
Chris Mason8d875f92014-08-12 10:47:42 -07009709 * we're using rename to replace one file with another. Start IO on it
9710 * now so we don't add too much work to the end of the transaction
Chris Mason5a3f23d2009-03-31 13:27:11 -04009711 */
Chris Mason8d875f92014-08-12 10:47:42 -07009712 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size)
Chris Mason5a3f23d2009-03-31 13:27:11 -04009713 filemap_flush(old_inode->i_mapping);
9714
Yan, Zheng76dda932009-09-21 16:00:26 -04009715 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08009716 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009717 down_read(&fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04009718 /*
9719 * We want to reserve the absolute worst case amount of items. So if
9720 * both inodes are subvols and we need to unlink them then that would
9721 * require 4 item modifications, but if they are both normal inodes it
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009722 * would require 5 item modifications, so we'll assume they are normal
Yan, Zhenga22285a2010-05-16 10:48:46 -04009723 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
9724 * should cover the worst case number of items we'll modify.
Filipe Manana5062af32016-05-05 10:26:26 +01009725 * If our rename has the whiteout flag, we need more 5 units for the
9726 * new inode (1 inode item, 1 inode ref, 2 dir items and 1 xattr item
9727 * when selinux is enabled).
Yan, Zhenga22285a2010-05-16 10:48:46 -04009728 */
Filipe Manana5062af32016-05-05 10:26:26 +01009729 trans_num_items = 11;
9730 if (flags & RENAME_WHITEOUT)
9731 trans_num_items += 5;
9732 trans = btrfs_start_transaction(root, trans_num_items);
Johann Lombardib44c59a2011-03-31 13:23:47 +00009733 if (IS_ERR(trans)) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009734 ret = PTR_ERR(trans);
9735 goto out_notrans;
9736 }
Chris Mason5f39d392007-10-15 16:14:19 -04009737
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009738 if (dest != root)
9739 btrfs_record_root_in_trans(trans, dest);
9740
Nikolay Borisov877574e2017-02-20 13:50:33 +02009741 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009742 if (ret)
9743 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009744
Miao Xie67de1172013-12-26 13:07:06 +08009745 BTRFS_I(old_inode)->dir_index = 0ULL;
Li Zefan33345d012011-04-20 10:31:50 +08009746 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009747 /* force full log commit if subvolume involved. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009748 btrfs_set_log_full_commit(fs_info, trans);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009749 } else {
Filipe Mananac4aba952016-04-29 13:14:42 +01009750 btrfs_pin_log_trans(root);
9751 log_pinned = true;
Yan, Zhenga5719522009-09-24 09:17:31 -04009752 ret = btrfs_insert_inode_ref(trans, dest,
9753 new_dentry->d_name.name,
9754 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08009755 old_ino,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009756 btrfs_ino(BTRFS_I(new_dir)), index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009757 if (ret)
9758 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009759 }
Chris Mason5a3f23d2009-03-31 13:27:11 -04009760
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009761 inode_inc_iversion(old_dir);
9762 inode_inc_iversion(new_dir);
9763 inode_inc_iversion(old_inode);
Deepa Dinamani04b285f2016-02-06 23:57:21 -08009764 old_dir->i_ctime = old_dir->i_mtime =
9765 new_dir->i_ctime = new_dir->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009766 old_inode->i_ctime = current_time(old_dir);
Chris Mason5f39d392007-10-15 16:14:19 -04009767
Chris Mason12fcfd22009-03-24 10:24:20 -04009768 if (old_dentry->d_parent != new_dentry->d_parent)
David Sterbaf85b7372017-01-20 14:54:07 +01009769 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
9770 BTRFS_I(old_inode), 1);
Chris Mason12fcfd22009-03-24 10:24:20 -04009771
Li Zefan33345d012011-04-20 10:31:50 +08009772 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009773 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
Lu Fengqi401b3b12018-08-01 11:32:30 +08009774 ret = btrfs_unlink_subvol(trans, old_dir, root_objectid,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009775 old_dentry->d_name.name,
9776 old_dentry->d_name.len);
9777 } else {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009778 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
9779 BTRFS_I(d_inode(old_dentry)),
Al Viro92986792011-03-04 17:14:37 +00009780 old_dentry->d_name.name,
9781 old_dentry->d_name.len);
9782 if (!ret)
9783 ret = btrfs_update_inode(trans, root, old_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009784 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009785 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009786 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009787 goto out_fail;
9788 }
Chris Mason39279cc2007-06-12 06:35:45 -04009789
9790 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009791 inode_inc_iversion(new_inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009792 new_inode->i_ctime = current_time(new_inode);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009793 if (unlikely(btrfs_ino(BTRFS_I(new_inode)) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009794 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
9795 root_objectid = BTRFS_I(new_inode)->location.objectid;
Lu Fengqi401b3b12018-08-01 11:32:30 +08009796 ret = btrfs_unlink_subvol(trans, new_dir, root_objectid,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009797 new_dentry->d_name.name,
9798 new_dentry->d_name.len);
9799 BUG_ON(new_inode->i_nlink == 0);
9800 } else {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009801 ret = btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
9802 BTRFS_I(d_inode(new_dentry)),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009803 new_dentry->d_name.name,
9804 new_dentry->d_name.len);
9805 }
Josef Bacik4ef31a42013-08-13 14:10:08 -04009806 if (!ret && new_inode->i_nlink == 0)
Nikolay Borisov73f2e542017-02-20 13:50:59 +02009807 ret = btrfs_orphan_add(trans,
9808 BTRFS_I(d_inode(new_dentry)));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009809 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009810 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009811 goto out_fail;
9812 }
Chris Mason39279cc2007-06-12 06:35:45 -04009813 }
Josef Bacikaec74772008-07-24 12:12:38 -04009814
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009815 ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009816 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04009817 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009818 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009819 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009820 goto out_fail;
9821 }
Chris Mason39279cc2007-06-12 06:35:45 -04009822
Miao Xie67de1172013-12-26 13:07:06 +08009823 if (old_inode->i_nlink == 1)
9824 BTRFS_I(old_inode)->dir_index = index;
9825
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009826 if (log_pinned) {
Al Viro10d9f302011-07-16 23:09:10 -04009827 struct dentry *parent = new_dentry->d_parent;
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009828
Filipe Mananad4682ba2018-06-11 19:24:28 +01009829 btrfs_init_log_ctx(&ctx, old_inode);
9830 ret = btrfs_log_new_name(trans, BTRFS_I(old_inode),
9831 BTRFS_I(old_dir), parent,
9832 false, &ctx);
9833 if (ret == BTRFS_NEED_LOG_SYNC)
9834 sync_log = true;
9835 else if (ret == BTRFS_NEED_TRANS_COMMIT)
9836 commit_transaction = true;
9837 ret = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009838 btrfs_end_log_trans(root);
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009839 log_pinned = false;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009840 }
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009841
9842 if (flags & RENAME_WHITEOUT) {
9843 ret = btrfs_whiteout_for_rename(trans, root, old_dir,
9844 old_dentry);
9845
9846 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009847 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009848 goto out_fail;
9849 }
Chris Mason12fcfd22009-03-24 10:24:20 -04009850 }
Chris Mason39279cc2007-06-12 06:35:45 -04009851out_fail:
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009852 /*
9853 * If we have pinned the log and an error happened, we unpin tasks
9854 * trying to sync the log and force them to fallback to a transaction
9855 * commit if the log currently contains any of the inodes involved in
9856 * this rename operation (to ensure we do not persist a log with an
9857 * inconsistent state for any of these inodes or leading to any
9858 * inconsistencies when replayed). If the transaction was aborted, the
9859 * abortion reason is propagated to userspace when attempting to commit
9860 * the transaction. If the log does not contain any of these inodes, we
9861 * allow the tasks to sync it.
9862 */
9863 if (ret && log_pinned) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009864 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
9865 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
9866 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009867 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009868 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009869 btrfs_set_log_full_commit(fs_info, trans);
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009870
9871 btrfs_end_log_trans(root);
9872 log_pinned = false;
9873 }
Filipe Mananad4682ba2018-06-11 19:24:28 +01009874 if (!ret && sync_log) {
9875 ret = btrfs_sync_log(trans, BTRFS_I(old_inode)->root, &ctx);
9876 if (ret)
9877 commit_transaction = true;
9878 }
9879 if (commit_transaction) {
9880 ret = btrfs_commit_transaction(trans);
9881 } else {
9882 int ret2;
9883
9884 ret2 = btrfs_end_transaction(trans);
9885 ret = ret ? ret : ret2;
9886 }
Johann Lombardib44c59a2011-03-31 13:23:47 +00009887out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +08009888 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009889 up_read(&fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009890
Chris Mason39279cc2007-06-12 06:35:45 -04009891 return ret;
9892}
9893
Miklos Szeredi80ace852014-07-23 15:15:32 +02009894static int btrfs_rename2(struct inode *old_dir, struct dentry *old_dentry,
9895 struct inode *new_dir, struct dentry *new_dentry,
9896 unsigned int flags)
9897{
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009898 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
Miklos Szeredi80ace852014-07-23 15:15:32 +02009899 return -EINVAL;
9900
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009901 if (flags & RENAME_EXCHANGE)
9902 return btrfs_rename_exchange(old_dir, old_dentry, new_dir,
9903 new_dentry);
9904
9905 return btrfs_rename(old_dir, old_dentry, new_dir, new_dentry, flags);
Miklos Szeredi80ace852014-07-23 15:15:32 +02009906}
9907
Nikolay Borisov3a2f8c02018-04-24 17:23:59 +03009908struct btrfs_delalloc_work {
9909 struct inode *inode;
9910 struct completion completion;
9911 struct list_head list;
9912 struct btrfs_work work;
9913};
9914
Miao Xie8ccf6f192012-10-25 09:28:04 +00009915static void btrfs_run_delalloc_work(struct btrfs_work *work)
9916{
9917 struct btrfs_delalloc_work *delalloc_work;
Josef Bacik9f23e282013-10-28 15:03:41 -04009918 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009919
9920 delalloc_work = container_of(work, struct btrfs_delalloc_work,
9921 work);
Josef Bacik9f23e282013-10-28 15:03:41 -04009922 inode = delalloc_work->inode;
David Sterba30424602015-11-27 19:27:11 +01009923 filemap_flush(inode->i_mapping);
9924 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
9925 &BTRFS_I(inode)->runtime_flags))
Josef Bacik9f23e282013-10-28 15:03:41 -04009926 filemap_flush(inode->i_mapping);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009927
Nikolay Borisov076da912018-04-23 10:54:16 +03009928 iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009929 complete(&delalloc_work->completion);
9930}
9931
Nikolay Borisov3a2f8c02018-04-24 17:23:59 +03009932static struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode)
Miao Xie8ccf6f192012-10-25 09:28:04 +00009933{
9934 struct btrfs_delalloc_work *work;
9935
David Sterba100d5702015-12-08 14:39:32 +01009936 work = kmalloc(sizeof(*work), GFP_NOFS);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009937 if (!work)
9938 return NULL;
9939
9940 init_completion(&work->completion);
9941 INIT_LIST_HEAD(&work->list);
9942 work->inode = inode;
Liu Bo9e0af232014-08-15 23:36:53 +08009943 WARN_ON_ONCE(!inode);
9944 btrfs_init_work(&work->work, btrfs_flush_delalloc_helper,
9945 btrfs_run_delalloc_work, NULL, NULL);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009946
9947 return work;
9948}
9949
Chris Masond352ac62008-09-29 15:18:18 -04009950/*
9951 * some fairly slow code that needs optimization. This walks the list
9952 * of all the inodes with pending delalloc and forces them to disk.
9953 */
Ethan Lien3cd24c62018-11-01 14:49:03 +08009954static int start_delalloc_inodes(struct btrfs_root *root, int nr, bool snapshot)
Chris Masonea8c2812008-08-04 23:17:27 -04009955{
Chris Masonea8c2812008-08-04 23:17:27 -04009956 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009957 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009958 struct btrfs_delalloc_work *work, *next;
9959 struct list_head works;
Miao Xie1eafa6c2013-01-22 10:49:00 +00009960 struct list_head splice;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009961 int ret = 0;
Chris Masonea8c2812008-08-04 23:17:27 -04009962
Miao Xie8ccf6f192012-10-25 09:28:04 +00009963 INIT_LIST_HEAD(&works);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009964 INIT_LIST_HEAD(&splice);
Miao Xie63607cc2013-01-22 10:50:35 +00009965
Miao Xie573bfb72014-03-06 13:55:03 +08009966 mutex_lock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009967 spin_lock(&root->delalloc_lock);
9968 list_splice_init(&root->delalloc_inodes, &splice);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009969 while (!list_empty(&splice)) {
9970 binode = list_entry(splice.next, struct btrfs_inode,
Chris Masonea8c2812008-08-04 23:17:27 -04009971 delalloc_inodes);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009972
Miao Xieeb73c1b2013-05-15 07:48:22 +00009973 list_move_tail(&binode->delalloc_inodes,
9974 &root->delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009975 inode = igrab(&binode->vfs_inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00009976 if (!inode) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00009977 cond_resched_lock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009978 continue;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009979 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00009980 spin_unlock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009981
Ethan Lien3cd24c62018-11-01 14:49:03 +08009982 if (snapshot)
9983 set_bit(BTRFS_INODE_SNAPSHOT_FLUSH,
9984 &binode->runtime_flags);
Nikolay Borisov076da912018-04-23 10:54:16 +03009985 work = btrfs_alloc_delalloc_work(inode);
David Sterba5d99a9982014-09-29 19:20:37 +02009986 if (!work) {
Nikolay Borisov4fbb5142018-04-23 10:54:15 +03009987 iput(inode);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009988 ret = -ENOMEM;
Wang Shilonga1ecaab2014-04-02 19:53:32 +08009989 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009990 }
Miao Xie1eafa6c2013-01-22 10:49:00 +00009991 list_add_tail(&work->list, &works);
Qu Wenruoa44903a2014-02-28 10:46:09 +08009992 btrfs_queue_work(root->fs_info->flush_workers,
9993 &work->work);
Miao Xie6c255e62014-03-06 13:55:01 +08009994 ret++;
9995 if (nr != -1 && ret >= nr)
Wang Shilonga1ecaab2014-04-02 19:53:32 +08009996 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009997 cond_resched();
Miao Xieeb73c1b2013-05-15 07:48:22 +00009998 spin_lock(&root->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04009999 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010000 spin_unlock(&root->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -040010001
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010002out:
Miao Xie1eafa6c2013-01-22 10:49:00 +000010003 list_for_each_entry_safe(work, next, &works, list) {
10004 list_del_init(&work->list);
Nikolay Borisov40012f92018-04-19 10:46:39 +030010005 wait_for_completion(&work->completion);
10006 kfree(work);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010007 }
10008
Nikolay Borisov81f1d392018-04-19 10:46:37 +030010009 if (!list_empty(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010010 spin_lock(&root->delalloc_lock);
10011 list_splice_tail(&splice, &root->delalloc_inodes);
10012 spin_unlock(&root->delalloc_lock);
10013 }
Miao Xie573bfb72014-03-06 13:55:03 +080010014 mutex_unlock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010015 return ret;
10016}
10017
Ethan Lien3cd24c62018-11-01 14:49:03 +080010018int btrfs_start_delalloc_snapshot(struct btrfs_root *root)
Miao Xieeb73c1b2013-05-15 07:48:22 +000010019{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010020 struct btrfs_fs_info *fs_info = root->fs_info;
Miao Xieeb73c1b2013-05-15 07:48:22 +000010021 int ret;
10022
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010023 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +000010024 return -EROFS;
10025
Ethan Lien3cd24c62018-11-01 14:49:03 +080010026 ret = start_delalloc_inodes(root, -1, true);
Miao Xie6c255e62014-03-06 13:55:01 +080010027 if (ret > 0)
10028 ret = 0;
Miao Xieeb73c1b2013-05-15 07:48:22 +000010029 return ret;
10030}
10031
Nikolay Borisov82b3e532018-04-23 10:54:13 +030010032int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, int nr)
Miao Xieeb73c1b2013-05-15 07:48:22 +000010033{
10034 struct btrfs_root *root;
10035 struct list_head splice;
10036 int ret;
10037
Wang Shilong2c21b4d2014-01-14 19:42:20 +080010038 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +000010039 return -EROFS;
10040
10041 INIT_LIST_HEAD(&splice);
10042
Miao Xie573bfb72014-03-06 13:55:03 +080010043 mutex_lock(&fs_info->delalloc_root_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010044 spin_lock(&fs_info->delalloc_root_lock);
10045 list_splice_init(&fs_info->delalloc_roots, &splice);
Miao Xie6c255e62014-03-06 13:55:01 +080010046 while (!list_empty(&splice) && nr) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010047 root = list_first_entry(&splice, struct btrfs_root,
10048 delalloc_root);
10049 root = btrfs_grab_fs_root(root);
10050 BUG_ON(!root);
10051 list_move_tail(&root->delalloc_root,
10052 &fs_info->delalloc_roots);
10053 spin_unlock(&fs_info->delalloc_root_lock);
10054
Ethan Lien3cd24c62018-11-01 14:49:03 +080010055 ret = start_delalloc_inodes(root, nr, false);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010056 btrfs_put_fs_root(root);
Miao Xie6c255e62014-03-06 13:55:01 +080010057 if (ret < 0)
Miao Xieeb73c1b2013-05-15 07:48:22 +000010058 goto out;
10059
Miao Xie6c255e62014-03-06 13:55:01 +080010060 if (nr != -1) {
10061 nr -= ret;
10062 WARN_ON(nr < 0);
10063 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010064 spin_lock(&fs_info->delalloc_root_lock);
10065 }
10066 spin_unlock(&fs_info->delalloc_root_lock);
10067
Miao Xie6c255e62014-03-06 13:55:01 +080010068 ret = 0;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010069out:
Nikolay Borisov81f1d392018-04-19 10:46:37 +030010070 if (!list_empty(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010071 spin_lock(&fs_info->delalloc_root_lock);
10072 list_splice_tail(&splice, &fs_info->delalloc_roots);
10073 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010074 }
Miao Xie573bfb72014-03-06 13:55:03 +080010075 mutex_unlock(&fs_info->delalloc_root_mutex);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010076 return ret;
Chris Masonea8c2812008-08-04 23:17:27 -040010077}
10078
Chris Mason39279cc2007-06-12 06:35:45 -040010079static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
10080 const char *symname)
10081{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010082 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -040010083 struct btrfs_trans_handle *trans;
10084 struct btrfs_root *root = BTRFS_I(dir)->root;
10085 struct btrfs_path *path;
10086 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -050010087 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -040010088 int err;
Chris Mason39279cc2007-06-12 06:35:45 -040010089 u64 objectid;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +053010090 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -040010091 int name_len;
10092 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -040010093 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -040010094 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -040010095 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -040010096
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +010010097 name_len = strlen(symname);
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010098 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info))
Chris Mason39279cc2007-06-12 06:35:45 -040010099 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -050010100
Josef Bacik9ed74f22009-09-11 16:12:44 -040010101 /*
10102 * 2 items for inode item and ref
10103 * 2 items for dir items
Filipe Manana9269d122015-12-31 18:16:29 +000010104 * 1 item for updating parent inode item
10105 * 1 item for the inline extent item
Josef Bacik9ed74f22009-09-11 16:12:44 -040010106 * 1 item for xattr if selinux is on
10107 */
Filipe Manana9269d122015-12-31 18:16:29 +000010108 trans = btrfs_start_transaction(root, 7);
Yan, Zhenga22285a2010-05-16 10:48:46 -040010109 if (IS_ERR(trans))
10110 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -050010111
Li Zefan581bb052011-04-20 10:06:11 +080010112 err = btrfs_find_free_ino(root, &objectid);
10113 if (err)
10114 goto out_unlock;
10115
Josef Bacikaec74772008-07-24 12:12:38 -040010116 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +010010117 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)),
10118 objectid, S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -040010119 if (IS_ERR(inode)) {
10120 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -040010121 inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -040010122 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -040010123 }
Chris Mason39279cc2007-06-12 06:35:45 -040010124
Casey Schauflerad19db72011-12-15 10:09:07 -050010125 /*
10126 * If the active LSM wants to access the inode during
10127 * d_instantiate it needs these. Smack checks to see
10128 * if the filesystem supports xattrs by looking at the
10129 * ops vector.
10130 */
10131 inode->i_fop = &btrfs_file_operations;
10132 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -070010133 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -070010134 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
10135
10136 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
10137 if (err)
Al Viro32955c52018-05-16 12:20:05 -040010138 goto out_unlock;
Casey Schauflerad19db72011-12-15 10:09:07 -050010139
Chris Mason39279cc2007-06-12 06:35:45 -040010140 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -070010141 if (!path) {
10142 err = -ENOMEM;
Al Viro32955c52018-05-16 12:20:05 -040010143 goto out_unlock;
Mark Fashehd8926bb2011-07-13 10:38:47 -070010144 }
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +020010145 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason39279cc2007-06-12 06:35:45 -040010146 key.offset = 0;
David Sterba962a2982014-06-04 18:41:45 +020010147 key.type = BTRFS_EXTENT_DATA_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -040010148 datasize = btrfs_file_extent_calc_inline_size(name_len);
10149 err = btrfs_insert_empty_item(trans, root, path, &key,
10150 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -040010151 if (err) {
Julia Lawallb0839162011-05-14 07:10:51 +000010152 btrfs_free_path(path);
Al Viro32955c52018-05-16 12:20:05 -040010153 goto out_unlock;
Chris Mason54aa1f42007-06-22 14:16:25 -040010154 }
Chris Mason5f39d392007-10-15 16:14:19 -040010155 leaf = path->nodes[0];
10156 ei = btrfs_item_ptr(leaf, path->slots[0],
10157 struct btrfs_file_extent_item);
10158 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
10159 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -040010160 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -040010161 btrfs_set_file_extent_encryption(leaf, ei, 0);
10162 btrfs_set_file_extent_compression(leaf, ei, 0);
10163 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
10164 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
10165
Chris Mason39279cc2007-06-12 06:35:45 -040010166 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -040010167 write_extent_buffer(leaf, symname, ptr, name_len);
10168 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -040010169 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -040010170
Chris Mason39279cc2007-06-12 06:35:45 -040010171 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -050010172 inode_nohighmem(inode);
Omar Sandoval4779cc02018-09-24 15:16:55 -070010173 inode->i_mapping->a_ops = &btrfs_aops;
Yan Zhengd899e052008-10-30 14:25:28 -040010174 inode_set_bytes(inode, name_len);
Nikolay Borisov6ef06d22017-02-20 13:50:34 +020010175 btrfs_i_size_write(BTRFS_I(inode), name_len);
Chris Mason54aa1f42007-06-22 14:16:25 -040010176 err = btrfs_update_inode(trans, root, inode);
Filipe Mananad50866d2015-12-31 18:08:24 +000010177 /*
10178 * Last step, add directory indexes for our symlink inode. This is the
10179 * last step to avoid extra cleanup of these indexes if an error happens
10180 * elsewhere above.
10181 */
10182 if (!err)
Nikolay Borisovcef415a2017-02-20 13:51:09 +020010183 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry,
10184 BTRFS_I(inode), 0, index);
Al Viro32955c52018-05-16 12:20:05 -040010185 if (err)
10186 goto out_unlock;
Chris Masonb0d5d102014-09-08 13:08:51 -070010187
Al Viro1e2e5472018-05-04 08:23:01 -040010188 d_instantiate_new(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010189
10190out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010191 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -040010192 if (err && inode) {
Chris Mason39279cc2007-06-12 06:35:45 -040010193 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -040010194 discard_new_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010195 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010196 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -040010197 return err;
10198}
Chris Mason16432982008-04-10 10:23:21 -040010199
Josef Bacik0af3d002010-06-21 14:48:16 -040010200static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
10201 u64 start, u64 num_bytes, u64 min_size,
10202 loff_t actual_len, u64 *alloc_hint,
10203 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -040010204{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010205 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik5dc562c2012-08-17 13:14:17 -040010206 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
10207 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -040010208 struct btrfs_root *root = BTRFS_I(inode)->root;
10209 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -040010210 u64 cur_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +000010211 u64 i_size;
Chris Mason154ea282013-03-05 11:11:26 -050010212 u64 cur_bytes;
Josef Bacik0b670dc2015-09-23 17:11:16 -040010213 u64 last_alloc = (u64)-1;
Yan Zhengd899e052008-10-30 14:25:28 -040010214 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -040010215 bool own_trans = true;
Wang Xiaoguang18513092016-07-25 15:51:40 +080010216 u64 end = start + num_bytes - 1;
Yan Zhengd899e052008-10-30 14:25:28 -040010217
Josef Bacik0af3d002010-06-21 14:48:16 -040010218 if (trans)
10219 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -040010220 while (num_bytes > 0) {
Josef Bacik0af3d002010-06-21 14:48:16 -040010221 if (own_trans) {
10222 trans = btrfs_start_transaction(root, 3);
10223 if (IS_ERR(trans)) {
10224 ret = PTR_ERR(trans);
10225 break;
10226 }
Yan Zhengd899e052008-10-30 14:25:28 -040010227 }
Yan, Zheng5a303d52009-11-12 09:34:52 +000010228
Byongho Leeee221842015-12-15 01:42:10 +090010229 cur_bytes = min_t(u64, num_bytes, SZ_256M);
Chris Mason154ea282013-03-05 11:11:26 -050010230 cur_bytes = max(cur_bytes, min_size);
Josef Bacik0b670dc2015-09-23 17:11:16 -040010231 /*
10232 * If we are severely fragmented we could end up with really
10233 * small allocations, so if the allocator is returning small
10234 * chunks lets make its job easier by only searching for those
10235 * sized chunks.
10236 */
10237 cur_bytes = min(cur_bytes, last_alloc);
Wang Xiaoguang18513092016-07-25 15:51:40 +080010238 ret = btrfs_reserve_extent(root, cur_bytes, cur_bytes,
10239 min_size, 0, *alloc_hint, &ins, 1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010240 if (ret) {
Josef Bacik0af3d002010-06-21 14:48:16 -040010241 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010242 btrfs_end_transaction(trans);
Yan, Zhenga22285a2010-05-16 10:48:46 -040010243 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +000010244 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010245 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010246
Josef Bacik0b670dc2015-09-23 17:11:16 -040010247 last_alloc = ins.offset;
Yan Zhengd899e052008-10-30 14:25:28 -040010248 ret = insert_reserved_file_extent(trans, inode,
10249 cur_offset, ins.objectid,
10250 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +000010251 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -040010252 BTRFS_FILE_EXTENT_PREALLOC);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010253 if (ret) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010254 btrfs_free_reserved_extent(fs_info, ins.objectid,
Miao Xiee570fd22014-06-19 10:42:50 +080010255 ins.offset, 0);
Jeff Mahoney66642832016-06-10 18:19:25 -040010256 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010257 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010258 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010259 break;
10260 }
Dongsheng Yang31193212014-12-12 16:44:35 +080010261
Nikolay Borisovdcdbc052017-02-20 13:50:45 +020010262 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Chris Masona1ed8352009-09-11 12:27:37 -040010263 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010264
Josef Bacik5dc562c2012-08-17 13:14:17 -040010265 em = alloc_extent_map();
10266 if (!em) {
10267 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
10268 &BTRFS_I(inode)->runtime_flags);
10269 goto next;
10270 }
10271
10272 em->start = cur_offset;
10273 em->orig_start = cur_offset;
10274 em->len = ins.offset;
10275 em->block_start = ins.objectid;
10276 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -050010277 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -040010278 em->ram_bytes = ins.offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010279 em->bdev = fs_info->fs_devices->latest_bdev;
Josef Bacik5dc562c2012-08-17 13:14:17 -040010280 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
10281 em->generation = trans->transid;
10282
10283 while (1) {
10284 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -040010285 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -040010286 write_unlock(&em_tree->lock);
10287 if (ret != -EEXIST)
10288 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +020010289 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -040010290 cur_offset + ins.offset - 1,
10291 0);
10292 }
10293 free_extent_map(em);
10294next:
Yan Zhengd899e052008-10-30 14:25:28 -040010295 num_bytes -= ins.offset;
10296 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -040010297 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +000010298
Josef Bacik0c4d2d92012-04-05 15:03:02 -040010299 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -070010300 inode->i_ctime = current_time(inode);
Christoph Hellwig6cbff002009-04-17 10:37:41 +020010301 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -040010302 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -040010303 (actual_len > inode->i_size) &&
10304 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +000010305 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +000010306 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +000010307 else
Josef Bacik55a61d12010-11-22 18:50:32 +000010308 i_size = cur_offset;
10309 i_size_write(inode, i_size);
10310 btrfs_ordered_update_i_size(inode, i_size, NULL);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010311 }
10312
Yan Zhengd899e052008-10-30 14:25:28 -040010313 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010314
10315 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -040010316 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010317 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010318 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010319 break;
10320 }
Yan Zhengd899e052008-10-30 14:25:28 -040010321
Josef Bacik0af3d002010-06-21 14:48:16 -040010322 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010323 btrfs_end_transaction(trans);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010324 }
Wang Xiaoguang18513092016-07-25 15:51:40 +080010325 if (cur_offset < end)
Qu Wenruobc42bda2017-02-27 15:10:39 +080010326 btrfs_free_reserved_data_space(inode, NULL, cur_offset,
Wang Xiaoguang18513092016-07-25 15:51:40 +080010327 end - cur_offset + 1);
Yan Zhengd899e052008-10-30 14:25:28 -040010328 return ret;
10329}
10330
Josef Bacik0af3d002010-06-21 14:48:16 -040010331int btrfs_prealloc_file_range(struct inode *inode, int mode,
10332 u64 start, u64 num_bytes, u64 min_size,
10333 loff_t actual_len, u64 *alloc_hint)
10334{
10335 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
10336 min_size, actual_len, alloc_hint,
10337 NULL);
10338}
10339
10340int btrfs_prealloc_file_range_trans(struct inode *inode,
10341 struct btrfs_trans_handle *trans, 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, trans);
10347}
10348
Chris Masone6dcd2d2008-07-17 12:53:50 -040010349static int btrfs_set_page_dirty(struct page *page)
10350{
Chris Masone6dcd2d2008-07-17 12:53:50 -040010351 return __set_page_dirty_nobuffers(page);
10352}
10353
Al Viro10556cb22011-06-20 19:28:19 -040010354static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -050010355{
Li Zefanb83cc962010-12-20 16:04:08 +080010356 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +000010357 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +080010358
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +000010359 if (mask & MAY_WRITE &&
10360 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
10361 if (btrfs_root_readonly(root))
10362 return -EROFS;
10363 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
10364 return -EACCES;
10365 }
Al Viro2830ba72011-06-20 19:16:29 -040010366 return generic_permission(inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -050010367}
Chris Mason39279cc2007-06-12 06:35:45 -040010368
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010369static int btrfs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
10370{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010371 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010372 struct btrfs_trans_handle *trans;
10373 struct btrfs_root *root = BTRFS_I(dir)->root;
10374 struct inode *inode = NULL;
10375 u64 objectid;
10376 u64 index;
10377 int ret = 0;
10378
10379 /*
10380 * 5 units required for adding orphan entry
10381 */
10382 trans = btrfs_start_transaction(root, 5);
10383 if (IS_ERR(trans))
10384 return PTR_ERR(trans);
10385
10386 ret = btrfs_find_free_ino(root, &objectid);
10387 if (ret)
10388 goto out;
10389
10390 inode = btrfs_new_inode(trans, root, dir, NULL, 0,
David Sterbaf85b7372017-01-20 14:54:07 +010010391 btrfs_ino(BTRFS_I(dir)), objectid, mode, &index);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010392 if (IS_ERR(inode)) {
10393 ret = PTR_ERR(inode);
10394 inode = NULL;
10395 goto out;
10396 }
10397
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010398 inode->i_fop = &btrfs_file_operations;
10399 inode->i_op = &btrfs_file_inode_operations;
10400
10401 inode->i_mapping->a_ops = &btrfs_aops;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010402 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
10403
Chris Masonb0d5d102014-09-08 13:08:51 -070010404 ret = btrfs_init_inode_security(trans, inode, dir, NULL);
10405 if (ret)
Al Viro32955c52018-05-16 12:20:05 -040010406 goto out;
Chris Masonb0d5d102014-09-08 13:08:51 -070010407
10408 ret = btrfs_update_inode(trans, root, inode);
10409 if (ret)
Al Viro32955c52018-05-16 12:20:05 -040010410 goto out;
Nikolay Borisov73f2e542017-02-20 13:50:59 +020010411 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010412 if (ret)
Al Viro32955c52018-05-16 12:20:05 -040010413 goto out;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010414
Filipe Manana5762b5c2014-08-01 00:10:32 +010010415 /*
10416 * We set number of links to 0 in btrfs_new_inode(), and here we set
10417 * it to 1 because d_tmpfile() will issue a warning if the count is 0,
10418 * through:
10419 *
10420 * d_tmpfile() -> inode_dec_link_count() -> drop_nlink()
10421 */
10422 set_nlink(inode, 1);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010423 d_tmpfile(dentry, inode);
Al Viro32955c52018-05-16 12:20:05 -040010424 unlock_new_inode(inode);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010425 mark_inode_dirty(inode);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010426out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010427 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -040010428 if (ret && inode)
10429 discard_new_inode(inode);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010430 btrfs_btree_balance_dirty(fs_info);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010431 return ret;
10432}
10433
David Sterba5cdc84b2018-07-18 20:32:52 +020010434void btrfs_set_range_writeback(struct extent_io_tree *tree, u64 start, u64 end)
Josef Bacikc6100a42017-05-05 11:57:13 -040010435{
David Sterba5cdc84b2018-07-18 20:32:52 +020010436 struct inode *inode = tree->private_data;
Josef Bacikc6100a42017-05-05 11:57:13 -040010437 unsigned long index = start >> PAGE_SHIFT;
10438 unsigned long end_index = end >> PAGE_SHIFT;
10439 struct page *page;
10440
10441 while (index <= end_index) {
10442 page = find_get_page(inode->i_mapping, index);
10443 ASSERT(page); /* Pages should be in the extent_io_tree */
10444 set_page_writeback(page);
10445 put_page(page);
10446 index++;
10447 }
10448}
10449
Omar Sandovaled46ff32016-11-03 10:28:14 -070010450#ifdef CONFIG_SWAP
10451/*
10452 * Add an entry indicating a block group or device which is pinned by a
10453 * swapfile. Returns 0 on success, 1 if there is already an entry for it, or a
10454 * negative errno on failure.
10455 */
10456static int btrfs_add_swapfile_pin(struct inode *inode, void *ptr,
10457 bool is_block_group)
10458{
10459 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
10460 struct btrfs_swapfile_pin *sp, *entry;
10461 struct rb_node **p;
10462 struct rb_node *parent = NULL;
10463
10464 sp = kmalloc(sizeof(*sp), GFP_NOFS);
10465 if (!sp)
10466 return -ENOMEM;
10467 sp->ptr = ptr;
10468 sp->inode = inode;
10469 sp->is_block_group = is_block_group;
10470
10471 spin_lock(&fs_info->swapfile_pins_lock);
10472 p = &fs_info->swapfile_pins.rb_node;
10473 while (*p) {
10474 parent = *p;
10475 entry = rb_entry(parent, struct btrfs_swapfile_pin, node);
10476 if (sp->ptr < entry->ptr ||
10477 (sp->ptr == entry->ptr && sp->inode < entry->inode)) {
10478 p = &(*p)->rb_left;
10479 } else if (sp->ptr > entry->ptr ||
10480 (sp->ptr == entry->ptr && sp->inode > entry->inode)) {
10481 p = &(*p)->rb_right;
10482 } else {
10483 spin_unlock(&fs_info->swapfile_pins_lock);
10484 kfree(sp);
10485 return 1;
10486 }
10487 }
10488 rb_link_node(&sp->node, parent, p);
10489 rb_insert_color(&sp->node, &fs_info->swapfile_pins);
10490 spin_unlock(&fs_info->swapfile_pins_lock);
10491 return 0;
10492}
10493
10494/* Free all of the entries pinned by this swapfile. */
10495static void btrfs_free_swapfile_pins(struct inode *inode)
10496{
10497 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
10498 struct btrfs_swapfile_pin *sp;
10499 struct rb_node *node, *next;
10500
10501 spin_lock(&fs_info->swapfile_pins_lock);
10502 node = rb_first(&fs_info->swapfile_pins);
10503 while (node) {
10504 next = rb_next(node);
10505 sp = rb_entry(node, struct btrfs_swapfile_pin, node);
10506 if (sp->inode == inode) {
10507 rb_erase(&sp->node, &fs_info->swapfile_pins);
10508 if (sp->is_block_group)
10509 btrfs_put_block_group(sp->ptr);
10510 kfree(sp);
10511 }
10512 node = next;
10513 }
10514 spin_unlock(&fs_info->swapfile_pins_lock);
10515}
10516
10517struct btrfs_swap_info {
10518 u64 start;
10519 u64 block_start;
10520 u64 block_len;
10521 u64 lowest_ppage;
10522 u64 highest_ppage;
10523 unsigned long nr_pages;
10524 int nr_extents;
10525};
10526
10527static int btrfs_add_swap_extent(struct swap_info_struct *sis,
10528 struct btrfs_swap_info *bsi)
10529{
10530 unsigned long nr_pages;
10531 u64 first_ppage, first_ppage_reported, next_ppage;
10532 int ret;
10533
10534 first_ppage = ALIGN(bsi->block_start, PAGE_SIZE) >> PAGE_SHIFT;
10535 next_ppage = ALIGN_DOWN(bsi->block_start + bsi->block_len,
10536 PAGE_SIZE) >> PAGE_SHIFT;
10537
10538 if (first_ppage >= next_ppage)
10539 return 0;
10540 nr_pages = next_ppage - first_ppage;
10541
10542 first_ppage_reported = first_ppage;
10543 if (bsi->start == 0)
10544 first_ppage_reported++;
10545 if (bsi->lowest_ppage > first_ppage_reported)
10546 bsi->lowest_ppage = first_ppage_reported;
10547 if (bsi->highest_ppage < (next_ppage - 1))
10548 bsi->highest_ppage = next_ppage - 1;
10549
10550 ret = add_swap_extent(sis, bsi->nr_pages, nr_pages, first_ppage);
10551 if (ret < 0)
10552 return ret;
10553 bsi->nr_extents += ret;
10554 bsi->nr_pages += nr_pages;
10555 return 0;
10556}
10557
10558static void btrfs_swap_deactivate(struct file *file)
10559{
10560 struct inode *inode = file_inode(file);
10561
10562 btrfs_free_swapfile_pins(inode);
10563 atomic_dec(&BTRFS_I(inode)->root->nr_swapfiles);
10564}
10565
10566static int btrfs_swap_activate(struct swap_info_struct *sis, struct file *file,
10567 sector_t *span)
10568{
10569 struct inode *inode = file_inode(file);
10570 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
10571 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
10572 struct extent_state *cached_state = NULL;
10573 struct extent_map *em = NULL;
10574 struct btrfs_device *device = NULL;
10575 struct btrfs_swap_info bsi = {
10576 .lowest_ppage = (sector_t)-1ULL,
10577 };
10578 int ret = 0;
10579 u64 isize;
10580 u64 start;
10581
10582 /*
10583 * If the swap file was just created, make sure delalloc is done. If the
10584 * file changes again after this, the user is doing something stupid and
10585 * we don't really care.
10586 */
10587 ret = btrfs_wait_ordered_range(inode, 0, (u64)-1);
10588 if (ret)
10589 return ret;
10590
10591 /*
10592 * The inode is locked, so these flags won't change after we check them.
10593 */
10594 if (BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS) {
10595 btrfs_warn(fs_info, "swapfile must not be compressed");
10596 return -EINVAL;
10597 }
10598 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW)) {
10599 btrfs_warn(fs_info, "swapfile must not be copy-on-write");
10600 return -EINVAL;
10601 }
10602 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
10603 btrfs_warn(fs_info, "swapfile must not be checksummed");
10604 return -EINVAL;
10605 }
10606
10607 /*
10608 * Balance or device remove/replace/resize can move stuff around from
10609 * under us. The EXCL_OP flag makes sure they aren't running/won't run
10610 * concurrently while we are mapping the swap extents, and
10611 * fs_info->swapfile_pins prevents them from running while the swap file
10612 * is active and moving the extents. Note that this also prevents a
10613 * concurrent device add which isn't actually necessary, but it's not
10614 * really worth the trouble to allow it.
10615 */
10616 if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) {
10617 btrfs_warn(fs_info,
10618 "cannot activate swapfile while exclusive operation is running");
10619 return -EBUSY;
10620 }
10621 /*
10622 * Snapshots can create extents which require COW even if NODATACOW is
10623 * set. We use this counter to prevent snapshots. We must increment it
10624 * before walking the extents because we don't want a concurrent
10625 * snapshot to run after we've already checked the extents.
10626 */
10627 atomic_inc(&BTRFS_I(inode)->root->nr_swapfiles);
10628
10629 isize = ALIGN_DOWN(inode->i_size, fs_info->sectorsize);
10630
10631 lock_extent_bits(io_tree, 0, isize - 1, &cached_state);
10632 start = 0;
10633 while (start < isize) {
10634 u64 logical_block_start, physical_block_start;
10635 struct btrfs_block_group_cache *bg;
10636 u64 len = isize - start;
10637
10638 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len, 0);
10639 if (IS_ERR(em)) {
10640 ret = PTR_ERR(em);
10641 goto out;
10642 }
10643
10644 if (em->block_start == EXTENT_MAP_HOLE) {
10645 btrfs_warn(fs_info, "swapfile must not have holes");
10646 ret = -EINVAL;
10647 goto out;
10648 }
10649 if (em->block_start == EXTENT_MAP_INLINE) {
10650 /*
10651 * It's unlikely we'll ever actually find ourselves
10652 * here, as a file small enough to fit inline won't be
10653 * big enough to store more than the swap header, but in
10654 * case something changes in the future, let's catch it
10655 * here rather than later.
10656 */
10657 btrfs_warn(fs_info, "swapfile must not be inline");
10658 ret = -EINVAL;
10659 goto out;
10660 }
10661 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
10662 btrfs_warn(fs_info, "swapfile must not be compressed");
10663 ret = -EINVAL;
10664 goto out;
10665 }
10666
10667 logical_block_start = em->block_start + (start - em->start);
10668 len = min(len, em->len - (start - em->start));
10669 free_extent_map(em);
10670 em = NULL;
10671
10672 ret = can_nocow_extent(inode, start, &len, NULL, NULL, NULL);
10673 if (ret < 0) {
10674 goto out;
10675 } else if (ret) {
10676 ret = 0;
10677 } else {
10678 btrfs_warn(fs_info,
10679 "swapfile must not be copy-on-write");
10680 ret = -EINVAL;
10681 goto out;
10682 }
10683
10684 em = btrfs_get_chunk_map(fs_info, logical_block_start, len);
10685 if (IS_ERR(em)) {
10686 ret = PTR_ERR(em);
10687 goto out;
10688 }
10689
10690 if (em->map_lookup->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
10691 btrfs_warn(fs_info,
10692 "swapfile must have single data profile");
10693 ret = -EINVAL;
10694 goto out;
10695 }
10696
10697 if (device == NULL) {
10698 device = em->map_lookup->stripes[0].dev;
10699 ret = btrfs_add_swapfile_pin(inode, device, false);
10700 if (ret == 1)
10701 ret = 0;
10702 else if (ret)
10703 goto out;
10704 } else if (device != em->map_lookup->stripes[0].dev) {
10705 btrfs_warn(fs_info, "swapfile must be on one device");
10706 ret = -EINVAL;
10707 goto out;
10708 }
10709
10710 physical_block_start = (em->map_lookup->stripes[0].physical +
10711 (logical_block_start - em->start));
10712 len = min(len, em->len - (logical_block_start - em->start));
10713 free_extent_map(em);
10714 em = NULL;
10715
10716 bg = btrfs_lookup_block_group(fs_info, logical_block_start);
10717 if (!bg) {
10718 btrfs_warn(fs_info,
10719 "could not find block group containing swapfile");
10720 ret = -EINVAL;
10721 goto out;
10722 }
10723
10724 ret = btrfs_add_swapfile_pin(inode, bg, true);
10725 if (ret) {
10726 btrfs_put_block_group(bg);
10727 if (ret == 1)
10728 ret = 0;
10729 else
10730 goto out;
10731 }
10732
10733 if (bsi.block_len &&
10734 bsi.block_start + bsi.block_len == physical_block_start) {
10735 bsi.block_len += len;
10736 } else {
10737 if (bsi.block_len) {
10738 ret = btrfs_add_swap_extent(sis, &bsi);
10739 if (ret)
10740 goto out;
10741 }
10742 bsi.start = start;
10743 bsi.block_start = physical_block_start;
10744 bsi.block_len = len;
10745 }
10746
10747 start += len;
10748 }
10749
10750 if (bsi.block_len)
10751 ret = btrfs_add_swap_extent(sis, &bsi);
10752
10753out:
10754 if (!IS_ERR_OR_NULL(em))
10755 free_extent_map(em);
10756
10757 unlock_extent_cached(io_tree, 0, isize - 1, &cached_state);
10758
10759 if (ret)
10760 btrfs_swap_deactivate(file);
10761
10762 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
10763
10764 if (ret)
10765 return ret;
10766
10767 if (device)
10768 sis->bdev = device->bdev;
10769 *span = bsi.highest_ppage - bsi.lowest_ppage + 1;
10770 sis->max = bsi.nr_pages;
10771 sis->pages = bsi.nr_pages - 1;
10772 sis->highest_bit = bsi.nr_pages - 1;
10773 return bsi.nr_extents;
10774}
10775#else
10776static void btrfs_swap_deactivate(struct file *file)
10777{
10778}
10779
10780static int btrfs_swap_activate(struct swap_info_struct *sis, struct file *file,
10781 sector_t *span)
10782{
10783 return -EOPNOTSUPP;
10784}
10785#endif
10786
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010787static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -050010788 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -040010789 .lookup = btrfs_lookup,
10790 .create = btrfs_create,
10791 .unlink = btrfs_unlink,
10792 .link = btrfs_link,
10793 .mkdir = btrfs_mkdir,
10794 .rmdir = btrfs_rmdir,
Miklos Szeredi2773bf02016-09-27 11:03:58 +020010795 .rename = btrfs_rename2,
Chris Mason39279cc2007-06-12 06:35:45 -040010796 .symlink = btrfs_symlink,
10797 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -040010798 .mknod = btrfs_mknod,
Josef Bacik5103e942007-11-16 11:45:54 -050010799 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010800 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010801 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010802 .set_acl = btrfs_set_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070010803 .update_time = btrfs_update_time,
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010804 .tmpfile = btrfs_tmpfile,
Chris Mason39279cc2007-06-12 06:35:45 -040010805};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010806static const struct inode_operations btrfs_dir_ro_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010807 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -050010808 .permission = btrfs_permission,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070010809 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010810};
Yan, Zheng76dda932009-09-21 16:00:26 -040010811
Alexey Dobriyan828c0952009-10-01 15:43:56 -070010812static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010813 .llseek = generic_file_llseek,
10814 .read = generic_read_dir,
Omar Sandoval02dbfc92016-05-20 13:50:33 -070010815 .iterate_shared = btrfs_real_readdir,
Josef Bacik23b5ec72017-07-24 15:14:25 -040010816 .open = btrfs_opendir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -040010817 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010818#ifdef CONFIG_COMPAT
Luke Dashjr4c63c242015-10-29 08:22:21 +000010819 .compat_ioctl = btrfs_compat_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010820#endif
Sage Weil6bf13c02008-06-10 10:07:39 -040010821 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -040010822 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -040010823};
10824
David Sterba20e55062015-11-19 11:42:28 +010010825static const struct extent_io_ops btrfs_extent_io_ops = {
David Sterba4d53ddd2017-02-17 15:27:44 +010010826 /* mandatory callbacks */
Chris Mason065631f2008-02-20 12:07:25 -050010827 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -040010828 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
10829};
10830
Chris Mason35054392009-01-21 13:11:13 -050010831/*
10832 * btrfs doesn't support the bmap operation because swapfiles
10833 * use bmap to make a mapping of extents in the file. They assume
10834 * these extents won't change over the life of the file and they
10835 * use the bmap result to do IO directly to the drive.
10836 *
10837 * the btrfs bmap call would return logical addresses that aren't
10838 * suitable for IO and they also will change frequently as COW
10839 * operations happen. So, swapfile + btrfs == corruption.
10840 *
10841 * For now we're avoiding this by dropping bmap.
10842 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -070010843static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -040010844 .readpage = btrfs_readpage,
10845 .writepage = btrfs_writepage,
Chris Masonb293f02e2007-11-01 19:45:34 -040010846 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -050010847 .readpages = btrfs_readpages,
Chris Mason16432982008-04-10 10:23:21 -040010848 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -040010849 .invalidatepage = btrfs_invalidatepage,
10850 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -040010851 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +020010852 .error_remove_page = generic_error_remove_page,
Omar Sandovaled46ff32016-11-03 10:28:14 -070010853 .swap_activate = btrfs_swap_activate,
10854 .swap_deactivate = btrfs_swap_deactivate,
Chris Mason39279cc2007-06-12 06:35:45 -040010855};
10856
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010857static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010858 .getattr = btrfs_getattr,
10859 .setattr = btrfs_setattr,
Josef Bacik5103e942007-11-16 11:45:54 -050010860 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010861 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -050010862 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010863 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010864 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010865 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010866};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010867static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -040010868 .getattr = btrfs_getattr,
10869 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010870 .permission = btrfs_permission,
Josef Bacik33268ea2008-07-24 12:16:36 -040010871 .listxattr = btrfs_listxattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010872 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010873 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010874 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -040010875};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010876static const struct inode_operations btrfs_symlink_inode_operations = {
Al Viro6b255392015-11-17 10:20:54 -050010877 .get_link = page_get_link,
Li Zefanf2095612010-11-19 02:05:24 +000010878 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -050010879 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010880 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -050010881 .listxattr = btrfs_listxattr,
Josef Bacike41f9412012-03-26 09:46:47 -040010882 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010883};
Yan, Zheng76dda932009-09-21 16:00:26 -040010884
Alexey Dobriyan82d339d2009-10-09 09:54:36 -040010885const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -040010886 .d_delete = btrfs_dentry_delete,
10887};