blob: fb237ffbda1a92d0049b927b12ee31cf663495ff [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
Eric Sandeen3972f262013-01-12 02:57:22 +000076static int btrfs_setsize(struct inode *inode, struct iattr *attr);
Filipe Manana213e8c52018-02-06 20:40:31 +000077static int btrfs_truncate(struct inode *inode, bool skip_writeback);
Josef Bacik5fd02042012-05-02 14:00:54 -040078static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent);
Chris Mason771ed682008-11-06 22:02:51 -050079static noinline int cow_file_range(struct inode *inode,
80 struct page *locked_page,
Wang Xiaoguangdda32452016-07-11 11:05:29 +080081 u64 start, u64 end, u64 delalloc_end,
82 int *page_started, unsigned long *nr_written,
83 int unlock, struct btrfs_dedupe_hash *hash);
Liu Bo6f9994d2017-01-31 07:50:22 -080084static struct extent_map *create_io_em(struct inode *inode, u64 start, u64 len,
85 u64 orig_start, u64 block_start,
86 u64 block_len, u64 orig_block_len,
87 u64 ram_bytes, int compress_type,
88 int type);
Josef Bacik7b128762008-07-24 12:17:14 -040089
Qu Wenruo524272602017-03-08 10:25:52 +080090static void __endio_write_update_ordered(struct inode *inode,
91 const u64 offset, const u64 bytes,
92 const bool uptodate);
93
94/*
95 * Cleanup all submitted ordered extents in specified range to handle errors
Andrea Gelmini52042d82018-11-28 12:05:13 +010096 * from the btrfs_run_delalloc_range() callback.
Qu Wenruo524272602017-03-08 10:25:52 +080097 *
98 * NOTE: caller must ensure that when an error happens, it can not call
99 * extent_clear_unlock_delalloc() to clear both the bits EXTENT_DO_ACCOUNTING
100 * and EXTENT_DELALLOC simultaneously, because that causes the reserved metadata
101 * to be released, which we want to happen only when finishing the ordered
Nikolay Borisovd1051d62018-11-21 17:10:52 +0200102 * extent (btrfs_finish_ordered_io()).
Qu Wenruo524272602017-03-08 10:25:52 +0800103 */
104static inline void btrfs_cleanup_ordered_extents(struct inode *inode,
Nikolay Borisovd1051d62018-11-21 17:10:52 +0200105 struct page *locked_page,
106 u64 offset, u64 bytes)
Qu Wenruo524272602017-03-08 10:25:52 +0800107{
Naohiro Aota63d71450c2017-09-01 17:58:47 +0900108 unsigned long index = offset >> PAGE_SHIFT;
109 unsigned long end_index = (offset + bytes - 1) >> PAGE_SHIFT;
Nikolay Borisovd1051d62018-11-21 17:10:52 +0200110 u64 page_start = page_offset(locked_page);
111 u64 page_end = page_start + PAGE_SIZE - 1;
112
Naohiro Aota63d71450c2017-09-01 17:58:47 +0900113 struct page *page;
114
115 while (index <= end_index) {
116 page = find_get_page(inode->i_mapping, index);
117 index++;
118 if (!page)
119 continue;
120 ClearPagePrivate2(page);
121 put_page(page);
122 }
Nikolay Borisovd1051d62018-11-21 17:10:52 +0200123
124 /*
125 * In case this page belongs to the delalloc range being instantiated
126 * then skip it, since the first page of a range is going to be
127 * properly cleaned up by the caller of run_delalloc_range
128 */
129 if (page_start >= offset && page_end <= (offset + bytes - 1)) {
130 offset += PAGE_SIZE;
131 bytes -= PAGE_SIZE;
132 }
133
134 return __endio_write_update_ordered(inode, offset, bytes, false);
Qu Wenruo524272602017-03-08 10:25:52 +0800135}
136
Eric Sandeen48a3b632013-04-25 20:41:01 +0000137static int btrfs_dirty_inode(struct inode *inode);
Chris Mason39279cc2007-06-12 06:35:45 -0400138
Josef Bacik6a3891c2015-03-16 17:38:52 -0400139#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
140void btrfs_test_inode_set_ops(struct inode *inode)
141{
142 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
143}
144#endif
145
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000146static int btrfs_init_inode_security(struct btrfs_trans_handle *trans,
Eric Paris2a7dba32011-02-01 11:05:39 -0500147 struct inode *inode, struct inode *dir,
148 const struct qstr *qstr)
Jim Owens0279b4c2009-02-04 09:29:13 -0500149{
150 int err;
151
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000152 err = btrfs_init_acl(trans, inode, dir);
Jim Owens0279b4c2009-02-04 09:29:13 -0500153 if (!err)
Eric Paris2a7dba32011-02-01 11:05:39 -0500154 err = btrfs_xattr_security_init(trans, inode, dir, qstr);
Jim Owens0279b4c2009-02-04 09:29:13 -0500155 return err;
156}
157
Chris Masond352ac62008-09-29 15:18:18 -0400158/*
Chris Masonc8b97812008-10-29 14:49:59 -0400159 * this does all the hard work for inserting an inline extent into
160 * the btree. The caller should have done a btrfs_drop_extents so that
161 * no overlapping inline items exist in the btree
162 */
Chris Mason40f76582014-05-21 13:35:51 -0700163static int insert_inline_extent(struct btrfs_trans_handle *trans,
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000164 struct btrfs_path *path, int extent_inserted,
Chris Masonc8b97812008-10-29 14:49:59 -0400165 struct btrfs_root *root, struct inode *inode,
166 u64 start, size_t size, size_t compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000167 int compress_type,
Chris Masonc8b97812008-10-29 14:49:59 -0400168 struct page **compressed_pages)
169{
Chris Masonc8b97812008-10-29 14:49:59 -0400170 struct extent_buffer *leaf;
171 struct page *page = NULL;
172 char *kaddr;
173 unsigned long ptr;
174 struct btrfs_file_extent_item *ei;
Chris Masonc8b97812008-10-29 14:49:59 -0400175 int ret;
176 size_t cur_size = size;
Chris Masonc8b97812008-10-29 14:49:59 -0400177 unsigned long offset;
Chris Masonc8b97812008-10-29 14:49:59 -0400178
Li Zefanfe3f5662011-03-28 08:30:38 +0000179 if (compressed_size && compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400180 cur_size = compressed_size;
Chris Masonc8b97812008-10-29 14:49:59 -0400181
Chris Masonc8b97812008-10-29 14:49:59 -0400182 inode_add_bytes(inode, size);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000183
184 if (!extent_inserted) {
185 struct btrfs_key key;
186 size_t datasize;
187
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +0200188 key.objectid = btrfs_ino(BTRFS_I(inode));
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000189 key.offset = start;
David Sterba962a2982014-06-04 18:41:45 +0200190 key.type = BTRFS_EXTENT_DATA_KEY;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000191
192 datasize = btrfs_file_extent_calc_inline_size(cur_size);
193 path->leave_spinning = 1;
194 ret = btrfs_insert_empty_item(trans, root, path, &key,
195 datasize);
David Sterba79b4f4c2017-06-15 19:09:51 +0200196 if (ret)
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000197 goto fail;
Chris Masonc8b97812008-10-29 14:49:59 -0400198 }
199 leaf = path->nodes[0];
200 ei = btrfs_item_ptr(leaf, path->slots[0],
201 struct btrfs_file_extent_item);
202 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
203 btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
204 btrfs_set_file_extent_encryption(leaf, ei, 0);
205 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
206 btrfs_set_file_extent_ram_bytes(leaf, ei, size);
207 ptr = btrfs_file_extent_inline_start(ei);
208
Li Zefan261507a02010-12-17 14:21:50 +0800209 if (compress_type != BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -0400210 struct page *cpage;
211 int i = 0;
Chris Masond3977122009-01-05 21:25:51 -0500212 while (compressed_size > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -0400213 cpage = compressed_pages[i];
Chris Mason5b050f02008-11-11 09:34:41 -0500214 cur_size = min_t(unsigned long, compressed_size,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300215 PAGE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400216
Cong Wang7ac687d2011-11-25 23:14:28 +0800217 kaddr = kmap_atomic(cpage);
Chris Masonc8b97812008-10-29 14:49:59 -0400218 write_extent_buffer(leaf, kaddr, ptr, cur_size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800219 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400220
221 i++;
222 ptr += cur_size;
223 compressed_size -= cur_size;
224 }
225 btrfs_set_file_extent_compression(leaf, ei,
Li Zefan261507a02010-12-17 14:21:50 +0800226 compress_type);
Chris Masonc8b97812008-10-29 14:49:59 -0400227 } else {
228 page = find_get_page(inode->i_mapping,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300229 start >> PAGE_SHIFT);
Chris Masonc8b97812008-10-29 14:49:59 -0400230 btrfs_set_file_extent_compression(leaf, ei, 0);
Cong Wang7ac687d2011-11-25 23:14:28 +0800231 kaddr = kmap_atomic(page);
Johannes Thumshirn70730172018-12-05 15:23:03 +0100232 offset = offset_in_page(start);
Chris Masonc8b97812008-10-29 14:49:59 -0400233 write_extent_buffer(leaf, kaddr + offset, ptr, size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800234 kunmap_atomic(kaddr);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300235 put_page(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400236 }
237 btrfs_mark_buffer_dirty(leaf);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000238 btrfs_release_path(path);
Chris Masonc8b97812008-10-29 14:49:59 -0400239
Yan, Zhengc2167752009-11-12 09:34:21 +0000240 /*
241 * we're an inline extent, so nobody can
242 * extend the file past i_size without locking
243 * a page we already have locked.
244 *
245 * We must do any isize and inode updates
246 * before we unlock the pages. Otherwise we
247 * could end up racing with unlink.
248 */
Chris Masonc8b97812008-10-29 14:49:59 -0400249 BTRFS_I(inode)->disk_i_size = inode->i_size;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100250 ret = btrfs_update_inode(trans, root, inode);
Yan, Zhengc2167752009-11-12 09:34:21 +0000251
Chris Masonc8b97812008-10-29 14:49:59 -0400252fail:
David Sterba79b4f4c2017-06-15 19:09:51 +0200253 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400254}
255
256
257/*
258 * conditionally insert an inline extent into the file. This
259 * does the checks required to make sure the data is small enough
260 * to fit as an inline extent.
261 */
Nikolay Borisovd02c0e22018-03-02 09:43:15 +0200262static noinline int cow_file_range_inline(struct inode *inode, u64 start,
Josef Bacik00361582013-08-14 14:02:47 -0400263 u64 end, size_t compressed_size,
264 int compress_type,
265 struct page **compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400266{
Nikolay Borisovd02c0e22018-03-02 09:43:15 +0200267 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400268 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik00361582013-08-14 14:02:47 -0400269 struct btrfs_trans_handle *trans;
Chris Masonc8b97812008-10-29 14:49:59 -0400270 u64 isize = i_size_read(inode);
271 u64 actual_end = min(end + 1, isize);
272 u64 inline_len = actual_end - start;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400273 u64 aligned_end = ALIGN(end, fs_info->sectorsize);
Chris Masonc8b97812008-10-29 14:49:59 -0400274 u64 data_len = inline_len;
275 int ret;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000276 struct btrfs_path *path;
277 int extent_inserted = 0;
278 u32 extent_item_size;
Chris Masonc8b97812008-10-29 14:49:59 -0400279
280 if (compressed_size)
281 data_len = compressed_size;
282
283 if (start > 0 ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400284 actual_end > fs_info->sectorsize ||
285 data_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info) ||
Chris Masonc8b97812008-10-29 14:49:59 -0400286 (!compressed_size &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400287 (actual_end & (fs_info->sectorsize - 1)) == 0) ||
Chris Masonc8b97812008-10-29 14:49:59 -0400288 end + 1 < isize ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400289 data_len > fs_info->max_inline) {
Chris Masonc8b97812008-10-29 14:49:59 -0400290 return 1;
291 }
292
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000293 path = btrfs_alloc_path();
294 if (!path)
295 return -ENOMEM;
296
Josef Bacik00361582013-08-14 14:02:47 -0400297 trans = btrfs_join_transaction(root);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000298 if (IS_ERR(trans)) {
299 btrfs_free_path(path);
Josef Bacik00361582013-08-14 14:02:47 -0400300 return PTR_ERR(trans);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000301 }
Josef Bacik69fe2d72017-10-19 14:15:57 -0400302 trans->block_rsv = &BTRFS_I(inode)->block_rsv;
Josef Bacik00361582013-08-14 14:02:47 -0400303
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000304 if (compressed_size && compressed_pages)
305 extent_item_size = btrfs_file_extent_calc_inline_size(
306 compressed_size);
307 else
308 extent_item_size = btrfs_file_extent_calc_inline_size(
309 inline_len);
310
311 ret = __btrfs_drop_extents(trans, root, inode, path,
312 start, aligned_end, NULL,
313 1, 1, extent_item_size, &extent_inserted);
Josef Bacik00361582013-08-14 14:02:47 -0400314 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -0400315 btrfs_abort_transaction(trans, ret);
Josef Bacik00361582013-08-14 14:02:47 -0400316 goto out;
317 }
Chris Masonc8b97812008-10-29 14:49:59 -0400318
319 if (isize > actual_end)
320 inline_len = min_t(u64, isize, actual_end);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000321 ret = insert_inline_extent(trans, path, extent_inserted,
322 root, inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -0400323 inline_len, compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000324 compress_type, compressed_pages);
Josef Bacik2adcac12012-05-23 16:10:14 -0400325 if (ret && ret != -ENOSPC) {
Jeff Mahoney66642832016-06-10 18:19:25 -0400326 btrfs_abort_transaction(trans, ret);
Josef Bacik00361582013-08-14 14:02:47 -0400327 goto out;
Josef Bacik2adcac12012-05-23 16:10:14 -0400328 } else if (ret == -ENOSPC) {
Josef Bacik00361582013-08-14 14:02:47 -0400329 ret = 1;
330 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100331 }
Josef Bacik2adcac12012-05-23 16:10:14 -0400332
Josef Bacikbdc20e62013-02-28 13:23:38 -0500333 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +0200334 btrfs_drop_extent_cache(BTRFS_I(inode), start, aligned_end - 1, 0);
Josef Bacik00361582013-08-14 14:02:47 -0400335out:
Qu Wenruo94ed9382015-09-08 17:25:56 +0800336 /*
337 * Don't forget to free the reserved space, as for inlined extent
338 * it won't count as data extent, free them directly here.
339 * And at reserve time, it's always aligned to page size, so
340 * just free one page here.
341 */
Qu Wenruobc42bda2017-02-27 15:10:39 +0800342 btrfs_qgroup_free_data(inode, NULL, 0, PAGE_SIZE);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000343 btrfs_free_path(path);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -0400344 btrfs_end_transaction(trans);
Josef Bacik00361582013-08-14 14:02:47 -0400345 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400346}
347
Chris Mason771ed682008-11-06 22:02:51 -0500348struct async_extent {
349 u64 start;
350 u64 ram_size;
351 u64 compressed_size;
352 struct page **pages;
353 unsigned long nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800354 int compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500355 struct list_head list;
356};
357
358struct async_cow {
359 struct inode *inode;
David Sterba600b6cf2018-11-22 17:16:44 +0100360 struct btrfs_fs_info *fs_info;
Chris Mason771ed682008-11-06 22:02:51 -0500361 struct page *locked_page;
362 u64 start;
363 u64 end;
Liu Bof82b7352017-10-23 23:18:16 -0600364 unsigned int write_flags;
Chris Mason771ed682008-11-06 22:02:51 -0500365 struct list_head extents;
366 struct btrfs_work work;
367};
368
369static noinline int add_async_extent(struct async_cow *cow,
370 u64 start, u64 ram_size,
371 u64 compressed_size,
372 struct page **pages,
Li Zefan261507a02010-12-17 14:21:50 +0800373 unsigned long nr_pages,
374 int compress_type)
Chris Mason771ed682008-11-06 22:02:51 -0500375{
376 struct async_extent *async_extent;
377
378 async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100379 BUG_ON(!async_extent); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -0500380 async_extent->start = start;
381 async_extent->ram_size = ram_size;
382 async_extent->compressed_size = compressed_size;
383 async_extent->pages = pages;
384 async_extent->nr_pages = nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800385 async_extent->compress_type = compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500386 list_add_tail(&async_extent->list, &cow->extents);
387 return 0;
388}
389
Timofey Titovetsc2fcdcd2017-07-17 16:52:58 +0300390static inline int inode_need_compress(struct inode *inode, u64 start, u64 end)
Wang Shilongf79707b2014-07-17 11:44:09 +0800391{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400392 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Wang Shilongf79707b2014-07-17 11:44:09 +0800393
394 /* force compress */
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400395 if (btrfs_test_opt(fs_info, FORCE_COMPRESS))
Wang Shilongf79707b2014-07-17 11:44:09 +0800396 return 1;
David Sterbaeec63c62017-07-17 19:41:31 +0200397 /* defrag ioctl */
398 if (BTRFS_I(inode)->defrag_compress)
399 return 1;
Wang Shilongf79707b2014-07-17 11:44:09 +0800400 /* bad compression ratios */
401 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS)
402 return 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400403 if (btrfs_test_opt(fs_info, COMPRESS) ||
Wang Shilongf79707b2014-07-17 11:44:09 +0800404 BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS ||
David Sterbab52aa8c2017-07-17 19:17:20 +0200405 BTRFS_I(inode)->prop_compress)
Timofey Titovetsc2fcdcd2017-07-17 16:52:58 +0300406 return btrfs_compress_heuristic(inode, start, end);
Wang Shilongf79707b2014-07-17 11:44:09 +0800407 return 0;
408}
409
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200410static inline void inode_should_defrag(struct btrfs_inode *inode,
Anand Jain26d30f82016-12-19 19:09:06 +0800411 u64 start, u64 end, u64 num_bytes, u64 small_write)
412{
413 /* If this is a small write inside eof, kick off a defrag */
414 if (num_bytes < small_write &&
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200415 (start > 0 || end + 1 < inode->disk_i_size))
Anand Jain26d30f82016-12-19 19:09:06 +0800416 btrfs_add_inode_defrag(NULL, inode);
417}
418
Chris Masonc8b97812008-10-29 14:49:59 -0400419/*
Chris Mason771ed682008-11-06 22:02:51 -0500420 * we create compressed extents in two phases. The first
421 * phase compresses a range of pages that have already been
422 * locked (both pages and state bits are locked).
Chris Masonc8b97812008-10-29 14:49:59 -0400423 *
Chris Mason771ed682008-11-06 22:02:51 -0500424 * This is done inside an ordered work queue, and the compression
425 * is spread across many cpus. The actual IO submission is step
426 * two, and the ordered work queue takes care of making sure that
427 * happens in the same order things were put onto the queue by
428 * writepages and friends.
Chris Masonc8b97812008-10-29 14:49:59 -0400429 *
Chris Mason771ed682008-11-06 22:02:51 -0500430 * If this code finds it can't get good compression, it puts an
431 * entry onto the work queue to write the uncompressed bytes. This
432 * makes sure that both compressed inodes and uncompressed inodes
Artem Bityutskiyb2570312012-07-25 18:12:06 +0300433 * are written in the same order that the flusher thread sent them
434 * down.
Chris Masond352ac62008-09-29 15:18:18 -0400435 */
Filipe Mananac44f6492014-10-09 21:15:44 +0100436static noinline void compress_file_range(struct inode *inode,
Chris Mason771ed682008-11-06 22:02:51 -0500437 struct page *locked_page,
438 u64 start, u64 end,
439 struct async_cow *async_cow,
440 int *num_added)
Chris Masonb888db22007-08-27 16:49:44 -0400441{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400442 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400443 u64 blocksize = fs_info->sectorsize;
Chris Masonc8b97812008-10-29 14:49:59 -0400444 u64 actual_end;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400445 int ret = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400446 struct page **pages = NULL;
447 unsigned long nr_pages;
Chris Masonc8b97812008-10-29 14:49:59 -0400448 unsigned long total_compressed = 0;
449 unsigned long total_in = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400450 int i;
451 int will_compress;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400452 int compress_type = fs_info->compress_type;
Chris Mason4adaa612013-03-26 13:07:00 -0400453 int redirty = 0;
Chris Masonb888db22007-08-27 16:49:44 -0400454
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200455 inode_should_defrag(BTRFS_I(inode), start, end, end - start + 1,
456 SZ_16K);
Chris Mason4cb53002011-05-24 15:35:30 -0400457
Nikolay Borisov62b37622019-01-03 10:50:00 +0200458 actual_end = min_t(u64, i_size_read(inode), end + 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400459again:
460 will_compress = 0;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300461 nr_pages = (end >> PAGE_SHIFT) - (start >> PAGE_SHIFT) + 1;
David Sterba069eac72017-02-14 19:36:54 +0100462 BUILD_BUG_ON((BTRFS_MAX_COMPRESSED % PAGE_SIZE) != 0);
463 nr_pages = min_t(unsigned long, nr_pages,
464 BTRFS_MAX_COMPRESSED / PAGE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400465
Chris Masonf03d9301f2009-02-04 09:31:06 -0500466 /*
467 * we don't want to send crud past the end of i_size through
468 * compression, that's just a waste of CPU time. So, if the
469 * end of the file is before the start of our current
470 * requested range of bytes, we bail out to the uncompressed
471 * cleanup code that can deal with all of this.
472 *
473 * It isn't really the fastest way to fix things, but this is a
474 * very uncommon corner.
475 */
476 if (actual_end <= start)
477 goto cleanup_and_bail_uncompressed;
478
Chris Masonc8b97812008-10-29 14:49:59 -0400479 total_compressed = actual_end - start;
480
Shilong Wang4bcbb332014-10-07 18:44:35 -0400481 /*
482 * skip compression for a small file range(<=blocksize) that
Nicholas D Steeves01327612016-05-19 21:18:45 -0400483 * isn't an inline extent, since it doesn't save disk space at all.
Shilong Wang4bcbb332014-10-07 18:44:35 -0400484 */
485 if (total_compressed <= blocksize &&
486 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
487 goto cleanup_and_bail_uncompressed;
488
David Sterba069eac72017-02-14 19:36:54 +0100489 total_compressed = min_t(unsigned long, total_compressed,
490 BTRFS_MAX_UNCOMPRESSED);
Chris Masonc8b97812008-10-29 14:49:59 -0400491 total_in = 0;
492 ret = 0;
Chris Masondb945352007-10-15 16:15:53 -0400493
Chris Mason771ed682008-11-06 22:02:51 -0500494 /*
495 * we do compression for mount -o compress and when the
496 * inode has not been flagged as nocompress. This flag can
497 * change at any time if we discover bad compression ratios.
Chris Masonc8b97812008-10-29 14:49:59 -0400498 */
Timofey Titovetsc2fcdcd2017-07-17 16:52:58 +0300499 if (inode_need_compress(inode, start, end)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400500 WARN_ON(pages);
David Sterba31e818f2015-02-20 18:00:26 +0100501 pages = kcalloc(nr_pages, sizeof(struct page *), GFP_NOFS);
Li Zefan560f7d72011-09-08 10:22:01 +0800502 if (!pages) {
503 /* just bail out to the uncompressed code */
Filipe Manana3527a012018-10-13 00:37:25 +0100504 nr_pages = 0;
Li Zefan560f7d72011-09-08 10:22:01 +0800505 goto cont;
506 }
Chris Mason179e29e2007-11-01 11:28:41 -0400507
David Sterbaeec63c62017-07-17 19:41:31 +0200508 if (BTRFS_I(inode)->defrag_compress)
509 compress_type = BTRFS_I(inode)->defrag_compress;
510 else if (BTRFS_I(inode)->prop_compress)
David Sterbab52aa8c2017-07-17 19:17:20 +0200511 compress_type = BTRFS_I(inode)->prop_compress;
Li Zefan261507a02010-12-17 14:21:50 +0800512
Chris Mason4adaa612013-03-26 13:07:00 -0400513 /*
514 * we need to call clear_page_dirty_for_io on each
515 * page in the range. Otherwise applications with the file
516 * mmap'd can wander in and change the page contents while
517 * we are compressing them.
518 *
519 * If the compression fails for any reason, we set the pages
520 * dirty again later on.
Timofey Titovetse9679de2017-10-24 01:29:48 +0300521 *
522 * Note that the remaining part is redirtied, the start pointer
523 * has moved, the end is the original one.
Chris Mason4adaa612013-03-26 13:07:00 -0400524 */
Timofey Titovetse9679de2017-10-24 01:29:48 +0300525 if (!redirty) {
526 extent_range_clear_dirty_for_io(inode, start, end);
527 redirty = 1;
528 }
David Sterbaf51d2b52017-09-15 17:36:57 +0200529
530 /* Compression level is applied here and only here */
531 ret = btrfs_compress_pages(
532 compress_type | (fs_info->compress_level << 4),
Li Zefan261507a02010-12-17 14:21:50 +0800533 inode->i_mapping, start,
David Sterba38c31462017-02-14 19:04:07 +0100534 pages,
David Sterba4d3a8002017-02-14 19:04:07 +0100535 &nr_pages,
Li Zefan261507a02010-12-17 14:21:50 +0800536 &total_in,
David Sterbae5d74902017-02-14 19:45:05 +0100537 &total_compressed);
Chris Masonc8b97812008-10-29 14:49:59 -0400538
539 if (!ret) {
Johannes Thumshirn70730172018-12-05 15:23:03 +0100540 unsigned long offset = offset_in_page(total_compressed);
David Sterba4d3a8002017-02-14 19:04:07 +0100541 struct page *page = pages[nr_pages - 1];
Chris Masonc8b97812008-10-29 14:49:59 -0400542 char *kaddr;
543
544 /* zero the tail end of the last page, we might be
545 * sending it down to disk
546 */
547 if (offset) {
Cong Wang7ac687d2011-11-25 23:14:28 +0800548 kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400549 memset(kaddr + offset, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300550 PAGE_SIZE - offset);
Cong Wang7ac687d2011-11-25 23:14:28 +0800551 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400552 }
553 will_compress = 1;
554 }
555 }
Li Zefan560f7d72011-09-08 10:22:01 +0800556cont:
Chris Masonc8b97812008-10-29 14:49:59 -0400557 if (start == 0) {
558 /* lets try to make an inline extent */
Timofey Titovets6018ba02017-09-15 01:57:26 +0300559 if (ret || total_in < actual_end) {
Chris Masonc8b97812008-10-29 14:49:59 -0400560 /* we didn't compress the entire range, try
Chris Mason771ed682008-11-06 22:02:51 -0500561 * to make an uncompressed inline extent.
Chris Masonc8b97812008-10-29 14:49:59 -0400562 */
Nikolay Borisovd02c0e22018-03-02 09:43:15 +0200563 ret = cow_file_range_inline(inode, start, end, 0,
564 BTRFS_COMPRESS_NONE, NULL);
Chris Masonc8b97812008-10-29 14:49:59 -0400565 } else {
Chris Mason771ed682008-11-06 22:02:51 -0500566 /* try making a compressed inline extent */
Nikolay Borisovd02c0e22018-03-02 09:43:15 +0200567 ret = cow_file_range_inline(inode, start, end,
Li Zefanfe3f5662011-03-28 08:30:38 +0000568 total_compressed,
569 compress_type, pages);
Chris Masonc8b97812008-10-29 14:49:59 -0400570 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100571 if (ret <= 0) {
Josef Bacik151a41b2013-07-29 13:22:24 -0400572 unsigned long clear_flags = EXTENT_DELALLOC |
Josef Bacik8b62f872017-10-19 14:15:55 -0400573 EXTENT_DELALLOC_NEW | EXTENT_DEFRAG |
574 EXTENT_DO_ACCOUNTING;
Filipe Mananae6eb4312014-10-10 10:45:12 +0100575 unsigned long page_error_op;
576
Filipe Mananae6eb4312014-10-10 10:45:12 +0100577 page_error_op = ret < 0 ? PAGE_SET_ERROR : 0;
Josef Bacik151a41b2013-07-29 13:22:24 -0400578
Chris Mason771ed682008-11-06 22:02:51 -0500579 /*
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100580 * inline extent creation worked or returned error,
581 * we don't need to create any more async work items.
582 * Unlock and free up our temp pages.
Josef Bacik8b62f872017-10-19 14:15:55 -0400583 *
584 * We use DO_ACCOUNTING here because we need the
585 * delalloc_release_metadata to be done _after_ we drop
586 * our outstanding extent for clearing delalloc for this
587 * range.
Chris Mason771ed682008-11-06 22:02:51 -0500588 */
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800589 extent_clear_unlock_delalloc(inode, start, end, end,
590 NULL, clear_flags,
591 PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -0400592 PAGE_CLEAR_DIRTY |
593 PAGE_SET_WRITEBACK |
Filipe Mananae6eb4312014-10-10 10:45:12 +0100594 page_error_op |
Josef Bacikc2790a22013-07-29 11:20:47 -0400595 PAGE_END_WRITEBACK);
Chris Masonc8b97812008-10-29 14:49:59 -0400596 goto free_pages_out;
597 }
598 }
599
600 if (will_compress) {
601 /*
602 * we aren't doing an inline extent round the compressed size
603 * up to a block size boundary so the allocator does sane
604 * things
605 */
Qu Wenruofda28322013-02-26 08:10:22 +0000606 total_compressed = ALIGN(total_compressed, blocksize);
Chris Masonc8b97812008-10-29 14:49:59 -0400607
608 /*
609 * one last check to make sure the compression is really a
Timofey Titovets170607e2017-06-06 14:41:15 +0300610 * win, compare the page count read with the blocks on disk,
611 * compression must free at least one sector size
Chris Masonc8b97812008-10-29 14:49:59 -0400612 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300613 total_in = ALIGN(total_in, PAGE_SIZE);
Timofey Titovets170607e2017-06-06 14:41:15 +0300614 if (total_compressed + blocksize <= total_in) {
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700615 *num_added += 1;
616
617 /*
618 * The async work queues will take care of doing actual
619 * allocation on disk for these compressed pages, and
620 * will submit them to the elevator.
621 */
Timofey Titovets11708622017-10-03 18:06:01 +0300622 add_async_extent(async_cow, start, total_in,
David Sterba4d3a8002017-02-14 19:04:07 +0100623 total_compressed, pages, nr_pages,
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700624 compress_type);
625
Timofey Titovets11708622017-10-03 18:06:01 +0300626 if (start + total_in < end) {
627 start += total_in;
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700628 pages = NULL;
629 cond_resched();
630 goto again;
631 }
632 return;
Chris Masonc8b97812008-10-29 14:49:59 -0400633 }
634 }
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700635 if (pages) {
Chris Masonc8b97812008-10-29 14:49:59 -0400636 /*
637 * the compression code ran but failed to make things smaller,
638 * free any pages it allocated and our page pointer array
639 */
David Sterba4d3a8002017-02-14 19:04:07 +0100640 for (i = 0; i < nr_pages; i++) {
Chris Mason70b99e62008-10-31 12:46:39 -0400641 WARN_ON(pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300642 put_page(pages[i]);
Chris Masonc8b97812008-10-29 14:49:59 -0400643 }
644 kfree(pages);
645 pages = NULL;
646 total_compressed = 0;
David Sterba4d3a8002017-02-14 19:04:07 +0100647 nr_pages = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400648
649 /* flag the file so we don't compress in the future */
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400650 if (!btrfs_test_opt(fs_info, FORCE_COMPRESS) &&
David Sterbab52aa8c2017-07-17 19:17:20 +0200651 !(BTRFS_I(inode)->prop_compress)) {
Chris Masona555f812010-01-28 16:18:15 -0500652 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
Chris Mason1e701a32010-03-11 09:42:04 -0500653 }
Chris Masonc8b97812008-10-29 14:49:59 -0400654 }
Chris Masonf03d9301f2009-02-04 09:31:06 -0500655cleanup_and_bail_uncompressed:
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700656 /*
657 * No compression, but we still need to write the pages in the file
658 * we've been given so far. redirty the locked page if it corresponds
659 * to our extent and set things up for the async work queue to run
660 * cow_file_range to do the normal delalloc dance.
661 */
662 if (page_offset(locked_page) >= start &&
663 page_offset(locked_page) <= end)
664 __set_page_dirty_nobuffers(locked_page);
665 /* unlocked later on in the async handlers */
666
667 if (redirty)
668 extent_range_redirty_for_io(inode, start, end);
669 add_async_extent(async_cow, start, end - start + 1, 0, NULL, 0,
670 BTRFS_COMPRESS_NONE);
671 *num_added += 1;
Chris Mason771ed682008-11-06 22:02:51 -0500672
Filipe Mananac44f6492014-10-09 21:15:44 +0100673 return;
Chris Mason771ed682008-11-06 22:02:51 -0500674
675free_pages_out:
David Sterba4d3a8002017-02-14 19:04:07 +0100676 for (i = 0; i < nr_pages; i++) {
Chris Mason771ed682008-11-06 22:02:51 -0500677 WARN_ON(pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300678 put_page(pages[i]);
Chris Mason771ed682008-11-06 22:02:51 -0500679 }
Chris Masond3977122009-01-05 21:25:51 -0500680 kfree(pages);
Chris Mason771ed682008-11-06 22:02:51 -0500681}
Chris Mason771ed682008-11-06 22:02:51 -0500682
Filipe Manana40ae8372014-10-06 22:14:24 +0100683static void free_async_extent_pages(struct async_extent *async_extent)
684{
685 int i;
686
687 if (!async_extent->pages)
688 return;
689
690 for (i = 0; i < async_extent->nr_pages; i++) {
691 WARN_ON(async_extent->pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300692 put_page(async_extent->pages[i]);
Filipe Manana40ae8372014-10-06 22:14:24 +0100693 }
694 kfree(async_extent->pages);
695 async_extent->nr_pages = 0;
696 async_extent->pages = NULL;
Chris Mason771ed682008-11-06 22:02:51 -0500697}
698
699/*
700 * phase two of compressed writeback. This is the ordered portion
701 * of the code, which only gets called in the order the work was
702 * queued. We walk all the async extents created by compress_file_range
703 * and send them down to the disk.
704 */
Nikolay Borisov532425f2019-01-03 10:49:59 +0200705static noinline void submit_compressed_extents(struct async_cow *async_cow)
Chris Mason771ed682008-11-06 22:02:51 -0500706{
Nikolay Borisov532425f2019-01-03 10:49:59 +0200707 struct inode *inode = async_cow->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400708 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason771ed682008-11-06 22:02:51 -0500709 struct async_extent *async_extent;
710 u64 alloc_hint = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500711 struct btrfs_key ins;
712 struct extent_map *em;
713 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason771ed682008-11-06 22:02:51 -0500714 struct extent_io_tree *io_tree;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500715 int ret = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500716
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500717again:
Chris Masond3977122009-01-05 21:25:51 -0500718 while (!list_empty(&async_cow->extents)) {
Chris Mason771ed682008-11-06 22:02:51 -0500719 async_extent = list_entry(async_cow->extents.next,
720 struct async_extent, list);
721 list_del(&async_extent->list);
722
723 io_tree = &BTRFS_I(inode)->io_tree;
724
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500725retry:
Chris Mason771ed682008-11-06 22:02:51 -0500726 /* did the compression code fall back to uncompressed IO? */
727 if (!async_extent->pages) {
728 int page_started = 0;
729 unsigned long nr_written = 0;
730
731 lock_extent(io_tree, async_extent->start,
Josef Bacik2ac55d42010-02-03 19:33:23 +0000732 async_extent->start +
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100733 async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500734
735 /* allocate blocks */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500736 ret = cow_file_range(inode, async_cow->locked_page,
737 async_extent->start,
738 async_extent->start +
739 async_extent->ram_size - 1,
Wang Xiaoguangdda32452016-07-11 11:05:29 +0800740 async_extent->start +
741 async_extent->ram_size - 1,
742 &page_started, &nr_written, 0,
743 NULL);
Chris Mason771ed682008-11-06 22:02:51 -0500744
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100745 /* JDM XXX */
746
Chris Mason771ed682008-11-06 22:02:51 -0500747 /*
748 * if page_started, cow_file_range inserted an
749 * inline extent and took care of all the unlocking
750 * and IO for us. Otherwise, we need to submit
751 * all those pages down to the drive.
752 */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500753 if (!page_started && !ret)
Nikolay Borisov5e3ee232017-12-08 15:55:58 +0200754 extent_write_locked_range(inode,
755 async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500756 async_extent->start +
Chris Mason771ed682008-11-06 22:02:51 -0500757 async_extent->ram_size - 1,
Chris Mason771ed682008-11-06 22:02:51 -0500758 WB_SYNC_ALL);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500759 else if (ret)
760 unlock_page(async_cow->locked_page);
Chris Mason771ed682008-11-06 22:02:51 -0500761 kfree(async_extent);
762 cond_resched();
763 continue;
764 }
765
766 lock_extent(io_tree, async_extent->start,
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100767 async_extent->start + async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500768
Wang Xiaoguang18513092016-07-25 15:51:40 +0800769 ret = btrfs_reserve_extent(root, async_extent->ram_size,
Chris Mason771ed682008-11-06 22:02:51 -0500770 async_extent->compressed_size,
771 async_extent->compressed_size,
Miao Xiee570fd22014-06-19 10:42:50 +0800772 0, alloc_hint, &ins, 1, 1);
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500773 if (ret) {
Filipe Manana40ae8372014-10-06 22:14:24 +0100774 free_async_extent_pages(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500775
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400776 if (ret == -ENOSPC) {
777 unlock_extent(io_tree, async_extent->start,
778 async_extent->start +
779 async_extent->ram_size - 1);
Liu Boce620032014-07-24 22:48:05 +0800780
781 /*
782 * we need to redirty the pages if we decide to
783 * fallback to uncompressed IO, otherwise we
784 * will not submit these pages down to lower
785 * layers.
786 */
787 extent_range_redirty_for_io(inode,
788 async_extent->start,
789 async_extent->start +
790 async_extent->ram_size - 1);
791
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100792 goto retry;
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400793 }
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500794 goto out_free;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500795 }
Yan, Zhengc2167752009-11-12 09:34:21 +0000796 /*
797 * here we're doing allocation and writeback of the
798 * compressed pages
799 */
Liu Bo6f9994d2017-01-31 07:50:22 -0800800 em = create_io_em(inode, async_extent->start,
801 async_extent->ram_size, /* len */
802 async_extent->start, /* orig_start */
803 ins.objectid, /* block_start */
804 ins.offset, /* block_len */
805 ins.offset, /* orig_block_len */
806 async_extent->ram_size, /* ram_bytes */
807 async_extent->compress_type,
808 BTRFS_ORDERED_COMPRESSED);
809 if (IS_ERR(em))
810 /* ret value is not necessary due to void function */
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500811 goto out_free_reserve;
Liu Bo6f9994d2017-01-31 07:50:22 -0800812 free_extent_map(em);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500813
Li Zefan261507a02010-12-17 14:21:50 +0800814 ret = btrfs_add_ordered_extent_compress(inode,
815 async_extent->start,
816 ins.objectid,
817 async_extent->ram_size,
818 ins.offset,
819 BTRFS_ORDERED_COMPRESSED,
820 async_extent->compress_type);
Filipe Mananad9f85962014-08-25 10:43:00 +0100821 if (ret) {
Nikolay Borisovdcdbc052017-02-20 13:50:45 +0200822 btrfs_drop_extent_cache(BTRFS_I(inode),
823 async_extent->start,
Filipe Mananad9f85962014-08-25 10:43:00 +0100824 async_extent->start +
825 async_extent->ram_size - 1, 0);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500826 goto out_free_reserve;
Filipe Mananad9f85962014-08-25 10:43:00 +0100827 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400828 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Chris Mason771ed682008-11-06 22:02:51 -0500829
Chris Mason771ed682008-11-06 22:02:51 -0500830 /*
831 * clear dirty, set writeback and unlock the pages.
832 */
Josef Bacikc2790a22013-07-29 11:20:47 -0400833 extent_clear_unlock_delalloc(inode, async_extent->start,
Chris Masona791e352009-10-08 11:27:10 -0400834 async_extent->start +
835 async_extent->ram_size - 1,
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800836 async_extent->start +
837 async_extent->ram_size - 1,
Josef Bacik151a41b2013-07-29 13:22:24 -0400838 NULL, EXTENT_LOCKED | EXTENT_DELALLOC,
839 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -0400840 PAGE_SET_WRITEBACK);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200841 if (btrfs_submit_compressed_write(inode,
Chris Masond3977122009-01-05 21:25:51 -0500842 async_extent->start,
843 async_extent->ram_size,
844 ins.objectid,
845 ins.offset, async_extent->pages,
Liu Bof82b7352017-10-23 23:18:16 -0600846 async_extent->nr_pages,
847 async_cow->write_flags)) {
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100848 struct page *p = async_extent->pages[0];
849 const u64 start = async_extent->start;
850 const u64 end = start + async_extent->ram_size - 1;
851
852 p->mapping = inode->i_mapping;
Nikolay Borisovc6297322018-11-08 10:18:08 +0200853 btrfs_writepage_endio_finish_ordered(p, start, end, 0);
Nikolay Borisov7087a9d2018-11-01 14:09:48 +0200854
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100855 p->mapping = NULL;
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800856 extent_clear_unlock_delalloc(inode, start, end, end,
857 NULL, 0,
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100858 PAGE_END_WRITEBACK |
859 PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +0100860 free_async_extent_pages(async_extent);
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100861 }
Chris Mason771ed682008-11-06 22:02:51 -0500862 alloc_hint = ins.objectid + ins.offset;
863 kfree(async_extent);
864 cond_resched();
865 }
Filipe Mananadec8f172014-10-06 22:14:26 +0100866 return;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500867out_free_reserve:
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400868 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400869 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100870out_free:
Josef Bacikc2790a22013-07-29 11:20:47 -0400871 extent_clear_unlock_delalloc(inode, async_extent->start,
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500872 async_extent->start +
873 async_extent->ram_size - 1,
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800874 async_extent->start +
875 async_extent->ram_size - 1,
Josef Bacikc2790a22013-07-29 11:20:47 -0400876 NULL, EXTENT_LOCKED | EXTENT_DELALLOC |
Filipe Mananaa7e3b972017-04-03 10:45:46 +0100877 EXTENT_DELALLOC_NEW |
Josef Bacik151a41b2013-07-29 13:22:24 -0400878 EXTENT_DEFRAG | EXTENT_DO_ACCOUNTING,
879 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Filipe Manana704de492014-10-06 22:14:22 +0100880 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK |
881 PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +0100882 free_async_extent_pages(async_extent);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100883 kfree(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500884 goto again;
Chris Mason771ed682008-11-06 22:02:51 -0500885}
886
Josef Bacik4b46fce2010-05-23 11:00:55 -0400887static u64 get_extent_allocation_hint(struct inode *inode, u64 start,
888 u64 num_bytes)
889{
890 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
891 struct extent_map *em;
892 u64 alloc_hint = 0;
893
894 read_lock(&em_tree->lock);
895 em = search_extent_mapping(em_tree, start, num_bytes);
896 if (em) {
897 /*
898 * if block start isn't an actual block number then find the
899 * first block in this inode and use that as a hint. If that
900 * block is also bogus then just don't worry about it.
901 */
902 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
903 free_extent_map(em);
904 em = search_extent_mapping(em_tree, 0, 0);
905 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
906 alloc_hint = em->block_start;
907 if (em)
908 free_extent_map(em);
909 } else {
910 alloc_hint = em->block_start;
911 free_extent_map(em);
912 }
913 }
914 read_unlock(&em_tree->lock);
915
916 return alloc_hint;
917}
918
Chris Mason771ed682008-11-06 22:02:51 -0500919/*
920 * when extent_io.c finds a delayed allocation range in the file,
921 * the call backs end up in this code. The basic idea is to
922 * allocate extents on disk for the range, and create ordered data structs
923 * in ram to track those extents.
924 *
925 * locked_page is the page that writepage had locked already. We use
926 * it to make sure we don't do extra locks or unlocks.
927 *
928 * *page_started is set to one if we unlock locked_page and do everything
929 * required to start IO on it. It may be clean and already done with
930 * IO when we return.
931 */
Josef Bacik00361582013-08-14 14:02:47 -0400932static noinline int cow_file_range(struct inode *inode,
933 struct page *locked_page,
Wang Xiaoguangdda32452016-07-11 11:05:29 +0800934 u64 start, u64 end, u64 delalloc_end,
935 int *page_started, unsigned long *nr_written,
936 int unlock, struct btrfs_dedupe_hash *hash)
Chris Mason771ed682008-11-06 22:02:51 -0500937{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400938 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik00361582013-08-14 14:02:47 -0400939 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason771ed682008-11-06 22:02:51 -0500940 u64 alloc_hint = 0;
941 u64 num_bytes;
942 unsigned long ram_size;
Filipe Mananaa315e682017-03-06 23:04:20 +0000943 u64 cur_alloc_size = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400944 u64 blocksize = fs_info->sectorsize;
Chris Mason771ed682008-11-06 22:02:51 -0500945 struct btrfs_key ins;
946 struct extent_map *em;
Filipe Mananaa315e682017-03-06 23:04:20 +0000947 unsigned clear_bits;
948 unsigned long page_ops;
949 bool extent_reserved = false;
Chris Mason771ed682008-11-06 22:02:51 -0500950 int ret = 0;
951
Nikolay Borisov70ddc552017-02-20 13:50:35 +0200952 if (btrfs_is_free_space_inode(BTRFS_I(inode))) {
Josef Bacik02ecd2c2013-10-25 16:19:08 -0400953 WARN_ON_ONCE(1);
Josef Bacik29bce2f2014-02-07 12:21:23 -0500954 ret = -EINVAL;
955 goto out_unlock;
Josef Bacik02ecd2c2013-10-25 16:19:08 -0400956 }
Chris Mason771ed682008-11-06 22:02:51 -0500957
Qu Wenruofda28322013-02-26 08:10:22 +0000958 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Mason771ed682008-11-06 22:02:51 -0500959 num_bytes = max(blocksize, num_bytes);
Anand Jain566b1762018-02-15 18:07:59 +0800960 ASSERT(num_bytes <= btrfs_super_total_bytes(fs_info->super_copy));
Chris Mason771ed682008-11-06 22:02:51 -0500961
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200962 inode_should_defrag(BTRFS_I(inode), start, end, num_bytes, SZ_64K);
Chris Mason4cb53002011-05-24 15:35:30 -0400963
Chris Mason771ed682008-11-06 22:02:51 -0500964 if (start == 0) {
965 /* lets try to make an inline extent */
Nikolay Borisovd02c0e22018-03-02 09:43:15 +0200966 ret = cow_file_range_inline(inode, start, end, 0,
967 BTRFS_COMPRESS_NONE, NULL);
Chris Mason771ed682008-11-06 22:02:51 -0500968 if (ret == 0) {
Josef Bacik8b62f872017-10-19 14:15:55 -0400969 /*
970 * We use DO_ACCOUNTING here because we need the
971 * delalloc_release_metadata to be run _after_ we drop
972 * our outstanding extent for clearing delalloc for this
973 * range.
974 */
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800975 extent_clear_unlock_delalloc(inode, start, end,
976 delalloc_end, NULL,
Josef Bacikc2790a22013-07-29 11:20:47 -0400977 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik8b62f872017-10-19 14:15:55 -0400978 EXTENT_DELALLOC_NEW | EXTENT_DEFRAG |
979 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -0400980 PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
981 PAGE_END_WRITEBACK);
Chris Mason771ed682008-11-06 22:02:51 -0500982 *nr_written = *nr_written +
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300983 (end - start + PAGE_SIZE) / PAGE_SIZE;
Chris Mason771ed682008-11-06 22:02:51 -0500984 *page_started = 1;
Chris Mason771ed682008-11-06 22:02:51 -0500985 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100986 } else if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100987 goto out_unlock;
Chris Mason771ed682008-11-06 22:02:51 -0500988 }
989 }
Chris Masonc8b97812008-10-29 14:49:59 -0400990
Josef Bacik4b46fce2010-05-23 11:00:55 -0400991 alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +0200992 btrfs_drop_extent_cache(BTRFS_I(inode), start,
993 start + num_bytes - 1, 0);
Chris Mason3b951512008-04-17 11:29:12 -0400994
Anand Jain3752d222018-02-15 12:29:38 +0800995 while (num_bytes > 0) {
996 cur_alloc_size = num_bytes;
Wang Xiaoguang18513092016-07-25 15:51:40 +0800997 ret = btrfs_reserve_extent(root, cur_alloc_size, cur_alloc_size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400998 fs_info->sectorsize, 0, alloc_hint,
Miao Xiee570fd22014-06-19 10:42:50 +0800999 &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04001000 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001001 goto out_unlock;
Filipe Mananaa315e682017-03-06 23:04:20 +00001002 cur_alloc_size = ins.offset;
1003 extent_reserved = true;
Chris Masond3977122009-01-05 21:25:51 -05001004
Chris Mason771ed682008-11-06 22:02:51 -05001005 ram_size = ins.offset;
Liu Bo6f9994d2017-01-31 07:50:22 -08001006 em = create_io_em(inode, start, ins.offset, /* len */
1007 start, /* orig_start */
1008 ins.objectid, /* block_start */
1009 ins.offset, /* block_len */
1010 ins.offset, /* orig_block_len */
1011 ram_size, /* ram_bytes */
1012 BTRFS_COMPRESS_NONE, /* compress_type */
Liu Bo1af4a0a2017-02-13 15:35:09 -08001013 BTRFS_ORDERED_REGULAR /* type */);
Su Yue090a127a2018-05-30 16:48:56 +08001014 if (IS_ERR(em)) {
1015 ret = PTR_ERR(em);
Liu Boace68ba2013-04-22 10:53:47 +00001016 goto out_reserve;
Su Yue090a127a2018-05-30 16:48:56 +08001017 }
Liu Bo6f9994d2017-01-31 07:50:22 -08001018 free_extent_map(em);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001019
Chris Masone6dcd2d2008-07-17 12:53:50 -04001020 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
Chris Mason771ed682008-11-06 22:02:51 -05001021 ram_size, cur_alloc_size, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001022 if (ret)
Filipe Mananad9f85962014-08-25 10:43:00 +01001023 goto out_drop_extent_cache;
Chris Masonc8b97812008-10-29 14:49:59 -04001024
Yan Zheng17d217f2008-12-12 10:03:38 -05001025 if (root->root_key.objectid ==
1026 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1027 ret = btrfs_reloc_clone_csums(inode, start,
1028 cur_alloc_size);
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001029 /*
1030 * Only drop cache here, and process as normal.
1031 *
1032 * We must not allow extent_clear_unlock_delalloc()
1033 * at out_unlock label to free meta of this ordered
1034 * extent, as its meta should be freed by
1035 * btrfs_finish_ordered_io().
1036 *
1037 * So we must continue until @start is increased to
1038 * skip current ordered extent.
1039 */
Josef Bacik00361582013-08-14 14:02:47 -04001040 if (ret)
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001041 btrfs_drop_extent_cache(BTRFS_I(inode), start,
1042 start + ram_size - 1, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001043 }
1044
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001045 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana9cfa3e32016-04-26 15:39:32 +01001046
Chris Masonc8b97812008-10-29 14:49:59 -04001047 /* we're not doing compressed IO, don't unlock the first
1048 * page (which the caller expects to stay locked), don't
1049 * clear any dirty bits and don't set any writeback bits
Chris Mason8b62b722009-09-02 16:53:46 -04001050 *
1051 * Do set the Private2 bit so we know this page was properly
1052 * setup for writepage
Chris Masonc8b97812008-10-29 14:49:59 -04001053 */
Filipe Mananaa315e682017-03-06 23:04:20 +00001054 page_ops = unlock ? PAGE_UNLOCK : 0;
1055 page_ops |= PAGE_SET_PRIVATE2;
Chris Masona791e352009-10-08 11:27:10 -04001056
Josef Bacikc2790a22013-07-29 11:20:47 -04001057 extent_clear_unlock_delalloc(inode, start,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001058 start + ram_size - 1,
1059 delalloc_end, locked_page,
Josef Bacikc2790a22013-07-29 11:20:47 -04001060 EXTENT_LOCKED | EXTENT_DELALLOC,
Filipe Mananaa315e682017-03-06 23:04:20 +00001061 page_ops);
Anand Jain3752d222018-02-15 12:29:38 +08001062 if (num_bytes < cur_alloc_size)
1063 num_bytes = 0;
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001064 else
Anand Jain3752d222018-02-15 12:29:38 +08001065 num_bytes -= cur_alloc_size;
Chris Masonc59f8952007-12-17 20:14:04 -05001066 alloc_hint = ins.objectid + ins.offset;
1067 start += cur_alloc_size;
Filipe Mananaa315e682017-03-06 23:04:20 +00001068 extent_reserved = false;
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001069
1070 /*
1071 * btrfs_reloc_clone_csums() error, since start is increased
1072 * extent_clear_unlock_delalloc() at out_unlock label won't
1073 * free metadata of current ordered extent, we're OK to exit.
1074 */
1075 if (ret)
1076 goto out_unlock;
Chris Masonb888db22007-08-27 16:49:44 -04001077 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001078out:
Chris Masonbe20aa92007-12-17 20:14:01 -05001079 return ret;
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001080
Filipe Mananad9f85962014-08-25 10:43:00 +01001081out_drop_extent_cache:
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02001082 btrfs_drop_extent_cache(BTRFS_I(inode), start, start + ram_size - 1, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001083out_reserve:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001084 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001085 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001086out_unlock:
Filipe Mananaa7e3b972017-04-03 10:45:46 +01001087 clear_bits = EXTENT_LOCKED | EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
1088 EXTENT_DEFRAG | EXTENT_CLEAR_META_RESV;
Filipe Mananaa315e682017-03-06 23:04:20 +00001089 page_ops = PAGE_UNLOCK | PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
1090 PAGE_END_WRITEBACK;
1091 /*
1092 * If we reserved an extent for our delalloc range (or a subrange) and
1093 * failed to create the respective ordered extent, then it means that
1094 * when we reserved the extent we decremented the extent's size from
1095 * the data space_info's bytes_may_use counter and incremented the
1096 * space_info's bytes_reserved counter by the same amount. We must make
1097 * sure extent_clear_unlock_delalloc() does not try to decrement again
1098 * the data space_info's bytes_may_use counter, therefore we do not pass
1099 * it the flag EXTENT_CLEAR_DATA_RESV.
1100 */
1101 if (extent_reserved) {
1102 extent_clear_unlock_delalloc(inode, start,
1103 start + cur_alloc_size,
1104 start + cur_alloc_size,
1105 locked_page,
1106 clear_bits,
1107 page_ops);
1108 start += cur_alloc_size;
1109 if (start >= end)
1110 goto out;
1111 }
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001112 extent_clear_unlock_delalloc(inode, start, end, delalloc_end,
1113 locked_page,
Filipe Mananaa315e682017-03-06 23:04:20 +00001114 clear_bits | EXTENT_CLEAR_DATA_RESV,
1115 page_ops);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001116 goto out;
Chris Mason771ed682008-11-06 22:02:51 -05001117}
Chris Masonc8b97812008-10-29 14:49:59 -04001118
Chris Mason771ed682008-11-06 22:02:51 -05001119/*
1120 * work queue call back to started compression on a file and pages
1121 */
1122static noinline void async_cow_start(struct btrfs_work *work)
1123{
1124 struct async_cow *async_cow;
1125 int num_added = 0;
1126 async_cow = container_of(work, struct async_cow, work);
1127
1128 compress_file_range(async_cow->inode, async_cow->locked_page,
1129 async_cow->start, async_cow->end, async_cow,
1130 &num_added);
Josef Bacik8180ef82012-06-08 15:16:12 -04001131 if (num_added == 0) {
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001132 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001133 async_cow->inode = NULL;
Josef Bacik8180ef82012-06-08 15:16:12 -04001134 }
Chris Mason771ed682008-11-06 22:02:51 -05001135}
1136
1137/*
1138 * work queue call back to submit previously compressed pages
1139 */
1140static noinline void async_cow_submit(struct btrfs_work *work)
1141{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001142 struct btrfs_fs_info *fs_info;
Chris Mason771ed682008-11-06 22:02:51 -05001143 struct async_cow *async_cow;
Chris Mason771ed682008-11-06 22:02:51 -05001144 unsigned long nr_pages;
1145
1146 async_cow = container_of(work, struct async_cow, work);
1147
David Sterba600b6cf2018-11-22 17:16:44 +01001148 fs_info = async_cow->fs_info;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001149 nr_pages = (async_cow->end - async_cow->start + PAGE_SIZE) >>
1150 PAGE_SHIFT;
Chris Mason771ed682008-11-06 22:02:51 -05001151
David Sterba093258e2018-02-26 16:15:17 +01001152 /* atomic_sub_return implies a barrier */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001153 if (atomic_sub_return(nr_pages, &fs_info->async_delalloc_pages) <
David Sterba093258e2018-02-26 16:15:17 +01001154 5 * SZ_1M)
1155 cond_wake_up_nomb(&fs_info->async_submit_wait);
Chris Mason771ed682008-11-06 22:02:51 -05001156
Nikolay Borisov4546d172019-01-03 10:50:03 +02001157 /*
1158 * ->inode could be NULL if async_cow_start has failed to compress,
1159 * in which case we don't have anything to submit, yet we need to
1160 * always adjust ->async_delalloc_pages as its paired with the init
1161 * happening in cow_file_range_async
1162 */
Chris Masond3977122009-01-05 21:25:51 -05001163 if (async_cow->inode)
Nikolay Borisov532425f2019-01-03 10:49:59 +02001164 submit_compressed_extents(async_cow);
Chris Mason771ed682008-11-06 22:02:51 -05001165}
Chris Masonc8b97812008-10-29 14:49:59 -04001166
Chris Mason771ed682008-11-06 22:02:51 -05001167static noinline void async_cow_free(struct btrfs_work *work)
1168{
1169 struct async_cow *async_cow;
1170 async_cow = container_of(work, struct async_cow, work);
Josef Bacik8180ef82012-06-08 15:16:12 -04001171 if (async_cow->inode)
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001172 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001173 kfree(async_cow);
1174}
1175
1176static int cow_file_range_async(struct inode *inode, struct page *locked_page,
1177 u64 start, u64 end, int *page_started,
Liu Bof82b7352017-10-23 23:18:16 -06001178 unsigned long *nr_written,
1179 unsigned int write_flags)
Chris Mason771ed682008-11-06 22:02:51 -05001180{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001181 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason771ed682008-11-06 22:02:51 -05001182 struct async_cow *async_cow;
Chris Mason771ed682008-11-06 22:02:51 -05001183 unsigned long nr_pages;
1184 u64 cur_end;
Chris Mason771ed682008-11-06 22:02:51 -05001185
Chris Masona3429ab2009-10-08 12:30:20 -04001186 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED,
David Sterbaae0f1622017-10-31 16:37:52 +01001187 1, 0, NULL);
Chris Masond3977122009-01-05 21:25:51 -05001188 while (start < end) {
Chris Mason771ed682008-11-06 22:02:51 -05001189 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001190 BUG_ON(!async_cow); /* -ENOMEM */
Nikolay Borisovbd4691a2019-01-03 10:50:01 +02001191 /*
1192 * igrab is called higher up in the call chain, take only the
1193 * lightweight reference for the callback lifetime
1194 */
1195 ihold(inode);
1196 async_cow->inode = inode;
David Sterba600b6cf2018-11-22 17:16:44 +01001197 async_cow->fs_info = fs_info;
Chris Mason771ed682008-11-06 22:02:51 -05001198 async_cow->locked_page = locked_page;
1199 async_cow->start = start;
Liu Bof82b7352017-10-23 23:18:16 -06001200 async_cow->write_flags = write_flags;
Chris Mason771ed682008-11-06 22:02:51 -05001201
Wang Shilongf79707b2014-07-17 11:44:09 +08001202 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001203 !btrfs_test_opt(fs_info, FORCE_COMPRESS))
Chris Mason771ed682008-11-06 22:02:51 -05001204 cur_end = end;
1205 else
Byongho Leeee221842015-12-15 01:42:10 +09001206 cur_end = min(end, start + SZ_512K - 1);
Chris Mason771ed682008-11-06 22:02:51 -05001207
1208 async_cow->end = cur_end;
1209 INIT_LIST_HEAD(&async_cow->extents);
1210
Liu Bo9e0af232014-08-15 23:36:53 +08001211 btrfs_init_work(&async_cow->work,
1212 btrfs_delalloc_helper,
1213 async_cow_start, async_cow_submit,
1214 async_cow_free);
Chris Mason771ed682008-11-06 22:02:51 -05001215
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001216 nr_pages = (cur_end - start + PAGE_SIZE) >>
1217 PAGE_SHIFT;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001218 atomic_add(nr_pages, &fs_info->async_delalloc_pages);
Chris Mason771ed682008-11-06 22:02:51 -05001219
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001220 btrfs_queue_work(fs_info->delalloc_workers, &async_cow->work);
Chris Mason771ed682008-11-06 22:02:51 -05001221
Chris Mason771ed682008-11-06 22:02:51 -05001222 *nr_written += nr_pages;
1223 start = cur_end + 1;
1224 }
1225 *page_started = 1;
1226 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001227}
1228
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001229static noinline int csum_exist_in_range(struct btrfs_fs_info *fs_info,
Yan Zheng17d217f2008-12-12 10:03:38 -05001230 u64 bytenr, u64 num_bytes)
1231{
1232 int ret;
1233 struct btrfs_ordered_sum *sums;
1234 LIST_HEAD(list);
1235
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001236 ret = btrfs_lookup_csums_range(fs_info->csum_root, bytenr,
Arne Jansena2de7332011-03-08 14:14:00 +01001237 bytenr + num_bytes - 1, &list, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001238 if (ret == 0 && list_empty(&list))
1239 return 0;
1240
1241 while (!list_empty(&list)) {
1242 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1243 list_del(&sums->list);
1244 kfree(sums);
1245 }
Liu Bo58113752018-01-31 17:09:13 -07001246 if (ret < 0)
1247 return ret;
Yan Zheng17d217f2008-12-12 10:03:38 -05001248 return 1;
1249}
1250
Chris Masond352ac62008-09-29 15:18:18 -04001251/*
1252 * when nowcow writeback call back. This checks for snapshots or COW copies
1253 * of the extents that exist in the file, and COWs the file as required.
1254 *
1255 * If no cow copies or snapshots exist, we write directly to the existing
1256 * blocks on disk
1257 */
Chris Mason7f366cf2009-03-12 20:12:45 -04001258static noinline int run_delalloc_nocow(struct inode *inode,
1259 struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001260 u64 start, u64 end, int *page_started, int force,
1261 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001262{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001263 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masonbe20aa92007-12-17 20:14:01 -05001264 struct btrfs_root *root = BTRFS_I(inode)->root;
1265 struct extent_buffer *leaf;
Chris Masonbe20aa92007-12-17 20:14:01 -05001266 struct btrfs_path *path;
Yan Zheng80ff3852008-10-30 14:20:02 -04001267 struct btrfs_file_extent_item *fi;
Chris Masonbe20aa92007-12-17 20:14:01 -05001268 struct btrfs_key found_key;
Liu Bo6f9994d2017-01-31 07:50:22 -08001269 struct extent_map *em;
Yan Zheng80ff3852008-10-30 14:20:02 -04001270 u64 cow_start;
1271 u64 cur_offset;
1272 u64 extent_end;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001273 u64 extent_offset;
Yan Zheng80ff3852008-10-30 14:20:02 -04001274 u64 disk_bytenr;
1275 u64 num_bytes;
Josef Bacikb4939682012-12-03 10:31:19 -05001276 u64 disk_num_bytes;
Josef Bacikcc95bef2013-04-04 14:31:27 -04001277 u64 ram_bytes;
Yan Zheng80ff3852008-10-30 14:20:02 -04001278 int extent_type;
Robbie Ko8ecebf4d2018-08-06 10:30:30 +08001279 int ret;
Yan Zhengd899e052008-10-30 14:25:28 -04001280 int type;
Yan Zheng80ff3852008-10-30 14:20:02 -04001281 int nocow;
1282 int check_prev = 1;
Li Zefan82d59022011-04-20 10:33:24 +08001283 bool nolock;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02001284 u64 ino = btrfs_ino(BTRFS_I(inode));
Chris Masonbe20aa92007-12-17 20:14:01 -05001285
1286 path = btrfs_alloc_path();
Josef Bacik17ca04a2012-05-31 15:58:55 -04001287 if (!path) {
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001288 extent_clear_unlock_delalloc(inode, start, end, end,
1289 locked_page,
Josef Bacikc2790a22013-07-29 11:20:47 -04001290 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -04001291 EXTENT_DO_ACCOUNTING |
1292 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001293 PAGE_CLEAR_DIRTY |
1294 PAGE_SET_WRITEBACK |
1295 PAGE_END_WRITEBACK);
Mark Fashehd8926bb2011-07-13 10:38:47 -07001296 return -ENOMEM;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001297 }
Li Zefan82d59022011-04-20 10:33:24 +08001298
Nikolay Borisov70ddc552017-02-20 13:50:35 +02001299 nolock = btrfs_is_free_space_inode(BTRFS_I(inode));
Li Zefan82d59022011-04-20 10:33:24 +08001300
Yan Zheng80ff3852008-10-30 14:20:02 -04001301 cow_start = (u64)-1;
1302 cur_offset = start;
1303 while (1) {
Liu Boe4c3b2d2017-01-30 12:25:28 -08001304 ret = btrfs_lookup_file_extent(NULL, root, path, ino,
Yan Zheng80ff3852008-10-30 14:20:02 -04001305 cur_offset, 0);
Josef Bacikd788a342013-10-25 16:55:08 -04001306 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001307 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001308 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1309 leaf = path->nodes[0];
1310 btrfs_item_key_to_cpu(leaf, &found_key,
1311 path->slots[0] - 1);
Li Zefan33345d012011-04-20 10:31:50 +08001312 if (found_key.objectid == ino &&
Yan Zheng80ff3852008-10-30 14:20:02 -04001313 found_key.type == BTRFS_EXTENT_DATA_KEY)
1314 path->slots[0]--;
1315 }
1316 check_prev = 0;
1317next_slot:
1318 leaf = path->nodes[0];
1319 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1320 ret = btrfs_next_leaf(root, path);
Liu Boe8916692018-01-25 11:02:50 -07001321 if (ret < 0) {
1322 if (cow_start != (u64)-1)
1323 cur_offset = cow_start;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001324 goto error;
Liu Boe8916692018-01-25 11:02:50 -07001325 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001326 if (ret > 0)
1327 break;
1328 leaf = path->nodes[0];
1329 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001330
Yan Zheng80ff3852008-10-30 14:20:02 -04001331 nocow = 0;
1332 disk_bytenr = 0;
Yan Zheng17d217f2008-12-12 10:03:38 -05001333 num_bytes = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001334 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001335
Filipe Manana1d512cb2015-11-09 00:33:58 +00001336 if (found_key.objectid > ino)
1337 break;
1338 if (WARN_ON_ONCE(found_key.objectid < ino) ||
1339 found_key.type < BTRFS_EXTENT_DATA_KEY) {
1340 path->slots[0]++;
1341 goto next_slot;
1342 }
1343 if (found_key.type > BTRFS_EXTENT_DATA_KEY ||
Yan Zheng80ff3852008-10-30 14:20:02 -04001344 found_key.offset > end)
1345 break;
Chris Masonbe20aa92007-12-17 20:14:01 -05001346
Yan Zheng80ff3852008-10-30 14:20:02 -04001347 if (found_key.offset > cur_offset) {
1348 extent_end = found_key.offset;
Chris Masone9061e22009-10-09 09:57:45 -04001349 extent_type = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001350 goto out_check;
1351 }
Chris Masonc31f8832008-01-08 15:46:31 -05001352
Yan Zheng80ff3852008-10-30 14:20:02 -04001353 fi = btrfs_item_ptr(leaf, path->slots[0],
1354 struct btrfs_file_extent_item);
1355 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001356
Josef Bacikcc95bef2013-04-04 14:31:27 -04001357 ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
Yan Zhengd899e052008-10-30 14:25:28 -04001358 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1359 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001360 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001361 extent_offset = btrfs_file_extent_offset(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001362 extent_end = found_key.offset +
1363 btrfs_file_extent_num_bytes(leaf, fi);
Josef Bacikb4939682012-12-03 10:31:19 -05001364 disk_num_bytes =
1365 btrfs_file_extent_disk_num_bytes(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001366 if (extent_end <= start) {
1367 path->slots[0]++;
1368 goto next_slot;
1369 }
Yan Zheng17d217f2008-12-12 10:03:38 -05001370 if (disk_bytenr == 0)
1371 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001372 if (btrfs_file_extent_compression(leaf, fi) ||
1373 btrfs_file_extent_encryption(leaf, fi) ||
1374 btrfs_file_extent_other_encoding(leaf, fi))
1375 goto out_check;
Ethan Lien78d42952018-05-17 14:58:29 +08001376 /*
1377 * Do the same check as in btrfs_cross_ref_exist but
1378 * without the unnecessary search.
1379 */
Lu Fengqi27a7ff52018-11-29 17:31:32 +08001380 if (!nolock &&
1381 btrfs_file_extent_generation(leaf, fi) <=
Ethan Lien78d42952018-05-17 14:58:29 +08001382 btrfs_root_last_snapshot(&root->root_item))
1383 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001384 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1385 goto out_check;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001386 if (btrfs_extent_readonly(fs_info, disk_bytenr))
Yan Zheng80ff3852008-10-30 14:20:02 -04001387 goto out_check;
Liu Bo58113752018-01-31 17:09:13 -07001388 ret = btrfs_cross_ref_exist(root, ino,
1389 found_key.offset -
1390 extent_offset, disk_bytenr);
1391 if (ret) {
1392 /*
1393 * ret could be -EIO if the above fails to read
1394 * metadata.
1395 */
1396 if (ret < 0) {
1397 if (cow_start != (u64)-1)
1398 cur_offset = cow_start;
1399 goto error;
1400 }
1401
1402 WARN_ON_ONCE(nolock);
Yan Zheng17d217f2008-12-12 10:03:38 -05001403 goto out_check;
Liu Bo58113752018-01-31 17:09:13 -07001404 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001405 disk_bytenr += extent_offset;
Yan Zheng17d217f2008-12-12 10:03:38 -05001406 disk_bytenr += cur_offset - found_key.offset;
1407 num_bytes = min(end + 1, extent_end) - cur_offset;
1408 /*
Wang Shilonge9894fd2014-03-27 11:12:25 +08001409 * if there are pending snapshots for this root,
1410 * we fall into common COW way.
1411 */
Robbie Ko8ecebf4d2018-08-06 10:30:30 +08001412 if (!nolock && atomic_read(&root->snapshot_force_cow))
1413 goto out_check;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001414 /*
Yan Zheng17d217f2008-12-12 10:03:38 -05001415 * force cow if csum exists in the range.
1416 * this ensure that csum for a given extent are
1417 * either valid or do not exist.
1418 */
Liu Bo58113752018-01-31 17:09:13 -07001419 ret = csum_exist_in_range(fs_info, disk_bytenr,
1420 num_bytes);
1421 if (ret) {
Liu Bo58113752018-01-31 17:09:13 -07001422 /*
1423 * ret could be -EIO if the above fails to read
1424 * metadata.
1425 */
1426 if (ret < 0) {
1427 if (cow_start != (u64)-1)
1428 cur_offset = cow_start;
1429 goto error;
1430 }
1431 WARN_ON_ONCE(nolock);
Yan Zheng17d217f2008-12-12 10:03:38 -05001432 goto out_check;
Robbie Ko91e1f562016-10-07 10:01:29 +08001433 }
Robbie Ko8ecebf4d2018-08-06 10:30:30 +08001434 if (!btrfs_inc_nocow_writers(fs_info, disk_bytenr))
Filipe Mananaf78c4362016-05-09 13:15:41 +01001435 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001436 nocow = 1;
1437 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1438 extent_end = found_key.offset +
Qu Wenruoe41ca582018-06-06 15:41:49 +08001439 btrfs_file_extent_ram_bytes(leaf, fi);
Jeff Mahoneyda170662016-06-15 09:22:56 -04001440 extent_end = ALIGN(extent_end,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001441 fs_info->sectorsize);
Yan Zheng80ff3852008-10-30 14:20:02 -04001442 } else {
Arnd Bergmann290342f2019-03-25 14:02:25 +01001443 BUG();
Yan Zheng80ff3852008-10-30 14:20:02 -04001444 }
1445out_check:
1446 if (extent_end <= start) {
1447 path->slots[0]++;
Filipe Mananaf78c4362016-05-09 13:15:41 +01001448 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001449 btrfs_dec_nocow_writers(fs_info, disk_bytenr);
Yan Zheng80ff3852008-10-30 14:20:02 -04001450 goto next_slot;
1451 }
1452 if (!nocow) {
1453 if (cow_start == (u64)-1)
1454 cow_start = cur_offset;
1455 cur_offset = extent_end;
1456 if (cur_offset > end)
1457 break;
1458 path->slots[0]++;
1459 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001460 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001461
David Sterbab3b4aa72011-04-21 01:20:15 +02001462 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001463 if (cow_start != (u64)-1) {
Josef Bacik00361582013-08-14 14:02:47 -04001464 ret = cow_file_range(inode, locked_page,
1465 cow_start, found_key.offset - 1,
Wang Xiaoguangdda32452016-07-11 11:05:29 +08001466 end, page_started, nr_written, 1,
1467 NULL);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001468 if (ret) {
Filipe Mananaf78c4362016-05-09 13:15:41 +01001469 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001470 btrfs_dec_nocow_writers(fs_info,
Filipe Mananaf78c4362016-05-09 13:15:41 +01001471 disk_bytenr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001472 goto error;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001473 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001474 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001475 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001476
Yan Zhengd899e052008-10-30 14:25:28 -04001477 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Liu Bo6f9994d2017-01-31 07:50:22 -08001478 u64 orig_start = found_key.offset - extent_offset;
1479
1480 em = create_io_em(inode, cur_offset, num_bytes,
1481 orig_start,
1482 disk_bytenr, /* block_start */
1483 num_bytes, /* block_len */
1484 disk_num_bytes, /* orig_block_len */
1485 ram_bytes, BTRFS_COMPRESS_NONE,
1486 BTRFS_ORDERED_PREALLOC);
1487 if (IS_ERR(em)) {
Liu Bo6f9994d2017-01-31 07:50:22 -08001488 if (nocow)
1489 btrfs_dec_nocow_writers(fs_info,
1490 disk_bytenr);
1491 ret = PTR_ERR(em);
1492 goto error;
Yan Zhengd899e052008-10-30 14:25:28 -04001493 }
Liu Bo6f9994d2017-01-31 07:50:22 -08001494 free_extent_map(em);
1495 }
1496
1497 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zhengd899e052008-10-30 14:25:28 -04001498 type = BTRFS_ORDERED_PREALLOC;
1499 } else {
1500 type = BTRFS_ORDERED_NOCOW;
1501 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001502
1503 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
Yan Zhengd899e052008-10-30 14:25:28 -04001504 num_bytes, num_bytes, type);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001505 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001506 btrfs_dec_nocow_writers(fs_info, disk_bytenr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001507 BUG_ON(ret); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -05001508
Yan, Zhengefa56462010-05-16 10:49:59 -04001509 if (root->root_key.objectid ==
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001510 BTRFS_DATA_RELOC_TREE_OBJECTID)
1511 /*
1512 * Error handled later, as we must prevent
1513 * extent_clear_unlock_delalloc() in error handler
1514 * from freeing metadata of created ordered extent.
1515 */
Yan, Zhengefa56462010-05-16 10:49:59 -04001516 ret = btrfs_reloc_clone_csums(inode, cur_offset,
1517 num_bytes);
Yan, Zhengefa56462010-05-16 10:49:59 -04001518
Josef Bacikc2790a22013-07-29 11:20:47 -04001519 extent_clear_unlock_delalloc(inode, cur_offset,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001520 cur_offset + num_bytes - 1, end,
Josef Bacikc2790a22013-07-29 11:20:47 -04001521 locked_page, EXTENT_LOCKED |
Wang Xiaoguang18513092016-07-25 15:51:40 +08001522 EXTENT_DELALLOC |
1523 EXTENT_CLEAR_DATA_RESV,
1524 PAGE_UNLOCK | PAGE_SET_PRIVATE2);
1525
Yan Zheng80ff3852008-10-30 14:20:02 -04001526 cur_offset = extent_end;
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001527
1528 /*
1529 * btrfs_reloc_clone_csums() error, now we're OK to call error
1530 * handler, as metadata for created ordered extent will only
1531 * be freed by btrfs_finish_ordered_io().
1532 */
1533 if (ret)
1534 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001535 if (cur_offset > end)
1536 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001537 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001538 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001539
Robbie Ko506481b2018-10-30 18:04:04 +08001540 if (cur_offset <= end && cow_start == (u64)-1)
Yan Zheng80ff3852008-10-30 14:20:02 -04001541 cow_start = cur_offset;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001542
Yan Zheng80ff3852008-10-30 14:20:02 -04001543 if (cow_start != (u64)-1) {
Robbie Ko506481b2018-10-30 18:04:04 +08001544 cur_offset = end;
Wang Xiaoguangdda32452016-07-11 11:05:29 +08001545 ret = cow_file_range(inode, locked_page, cow_start, end, end,
1546 page_started, nr_written, 1, NULL);
Josef Bacikd788a342013-10-25 16:55:08 -04001547 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001548 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001549 }
1550
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001551error:
Josef Bacik17ca04a2012-05-31 15:58:55 -04001552 if (ret && cur_offset < end)
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001553 extent_clear_unlock_delalloc(inode, cur_offset, end, end,
Josef Bacikc2790a22013-07-29 11:20:47 -04001554 locked_page, EXTENT_LOCKED |
Josef Bacik151a41b2013-07-29 13:22:24 -04001555 EXTENT_DELALLOC | EXTENT_DEFRAG |
1556 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
1557 PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -04001558 PAGE_SET_WRITEBACK |
1559 PAGE_END_WRITEBACK);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001560 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001561 return ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001562}
1563
Wang Shilong47059d92014-07-03 18:22:07 +08001564static inline int need_force_cow(struct inode *inode, u64 start, u64 end)
1565{
1566
1567 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
1568 !(BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC))
1569 return 0;
1570
1571 /*
1572 * @defrag_bytes is a hint value, no spinlock held here,
1573 * if is not zero, it means the file is defragging.
1574 * Force cow if given extent needs to be defragged.
1575 */
1576 if (BTRFS_I(inode)->defrag_bytes &&
1577 test_range_bit(&BTRFS_I(inode)->io_tree, start, end,
1578 EXTENT_DEFRAG, 0, NULL))
1579 return 1;
1580
1581 return 0;
1582}
1583
Chris Masond352ac62008-09-29 15:18:18 -04001584/*
Nikolay Borisov5eaad972018-11-01 14:09:46 +02001585 * Function to process delayed allocation (create CoW) for ranges which are
1586 * being touched for the first time.
Chris Masond352ac62008-09-29 15:18:18 -04001587 */
Nikolay Borisovbc9a8bf2018-12-19 09:50:20 +02001588int btrfs_run_delalloc_range(struct inode *inode, struct page *locked_page,
Nikolay Borisov5eaad972018-11-01 14:09:46 +02001589 u64 start, u64 end, int *page_started, unsigned long *nr_written,
1590 struct writeback_control *wbc)
Chris Masonbe20aa92007-12-17 20:14:01 -05001591{
Chris Masonbe20aa92007-12-17 20:14:01 -05001592 int ret;
Wang Shilong47059d92014-07-03 18:22:07 +08001593 int force_cow = need_force_cow(inode, start, end);
Liu Bof82b7352017-10-23 23:18:16 -06001594 unsigned int write_flags = wbc_to_write_flags(wbc);
Chris Masona2135012008-06-25 16:01:30 -04001595
Wang Shilong47059d92014-07-03 18:22:07 +08001596 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW && !force_cow) {
Chris Masonc8b97812008-10-29 14:49:59 -04001597 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001598 page_started, 1, nr_written);
Wang Shilong47059d92014-07-03 18:22:07 +08001599 } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC && !force_cow) {
Yan Zhengd899e052008-10-30 14:25:28 -04001600 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001601 page_started, 0, nr_written);
Timofey Titovetsc2fcdcd2017-07-17 16:52:58 +03001602 } else if (!inode_need_compress(inode, start, end)) {
Wang Xiaoguangdda32452016-07-11 11:05:29 +08001603 ret = cow_file_range(inode, locked_page, start, end, end,
1604 page_started, nr_written, 1, NULL);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001605 } else {
1606 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1607 &BTRFS_I(inode)->runtime_flags);
Chris Mason771ed682008-11-06 22:02:51 -05001608 ret = cow_file_range_async(inode, locked_page, start, end,
Liu Bof82b7352017-10-23 23:18:16 -06001609 page_started, nr_written,
1610 write_flags);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001611 }
Qu Wenruo524272602017-03-08 10:25:52 +08001612 if (ret)
Nikolay Borisovd1051d62018-11-21 17:10:52 +02001613 btrfs_cleanup_ordered_extents(inode, locked_page, start,
1614 end - start + 1);
Chris Masonb888db22007-08-27 16:49:44 -04001615 return ret;
1616}
1617
Nikolay Borisovabbb55f2018-11-01 14:09:53 +02001618void btrfs_split_delalloc_extent(struct inode *inode,
1619 struct extent_state *orig, u64 split)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001620{
Josef Bacikdcab6a32015-02-11 15:08:59 -05001621 u64 size;
1622
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001623 /* not delalloc, ignore it */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001624 if (!(orig->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001625 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001626
Josef Bacikdcab6a32015-02-11 15:08:59 -05001627 size = orig->end - orig->start + 1;
1628 if (size > BTRFS_MAX_EXTENT_SIZE) {
David Sterba823bb202017-01-04 11:09:51 +01001629 u32 num_extents;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001630 u64 new_size;
1631
1632 /*
Nikolay Borisov5c848192018-11-01 14:09:52 +02001633 * See the explanation in btrfs_merge_delalloc_extent, the same
Josef Bacikba117212015-03-13 15:01:24 -04001634 * applies here, just in reverse.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001635 */
1636 new_size = orig->end - split + 1;
David Sterba823bb202017-01-04 11:09:51 +01001637 num_extents = count_max_extents(new_size);
Josef Bacikba117212015-03-13 15:01:24 -04001638 new_size = split - orig->start;
David Sterba823bb202017-01-04 11:09:51 +01001639 num_extents += count_max_extents(new_size);
1640 if (count_max_extents(size) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001641 return;
1642 }
1643
Josef Bacik9e0baf62011-07-15 15:16:44 +00001644 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04001645 btrfs_mod_outstanding_extents(BTRFS_I(inode), 1);
Josef Bacik9e0baf62011-07-15 15:16:44 +00001646 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001647}
1648
1649/*
Nikolay Borisov5c848192018-11-01 14:09:52 +02001650 * Handle merged delayed allocation extents so we can keep track of new extents
1651 * that are just merged onto old extents, such as when we are doing sequential
1652 * writes, so we can properly account for the metadata space we'll need.
Josef Bacik9ed74f22009-09-11 16:12:44 -04001653 */
Nikolay Borisov5c848192018-11-01 14:09:52 +02001654void btrfs_merge_delalloc_extent(struct inode *inode, struct extent_state *new,
1655 struct extent_state *other)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001656{
Josef Bacikdcab6a32015-02-11 15:08:59 -05001657 u64 new_size, old_size;
David Sterba823bb202017-01-04 11:09:51 +01001658 u32 num_extents;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001659
Josef Bacik9ed74f22009-09-11 16:12:44 -04001660 /* not delalloc, ignore it */
1661 if (!(other->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001662 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001663
Josef Bacik8461a3d2015-03-13 15:12:08 -04001664 if (new->start > other->start)
1665 new_size = new->end - other->start + 1;
1666 else
1667 new_size = other->end - new->start + 1;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001668
1669 /* we're not bigger than the max, unreserve the space and go */
1670 if (new_size <= BTRFS_MAX_EXTENT_SIZE) {
1671 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04001672 btrfs_mod_outstanding_extents(BTRFS_I(inode), -1);
Josef Bacikdcab6a32015-02-11 15:08:59 -05001673 spin_unlock(&BTRFS_I(inode)->lock);
1674 return;
1675 }
1676
1677 /*
Josef Bacikba117212015-03-13 15:01:24 -04001678 * We have to add up either side to figure out how many extents were
1679 * accounted for before we merged into one big extent. If the number of
1680 * extents we accounted for is <= the amount we need for the new range
1681 * then we can return, otherwise drop. Think of it like this
1682 *
1683 * [ 4k][MAX_SIZE]
1684 *
1685 * So we've grown the extent by a MAX_SIZE extent, this would mean we
1686 * need 2 outstanding extents, on one side we have 1 and the other side
1687 * we have 1 so they are == and we can return. But in this case
1688 *
1689 * [MAX_SIZE+4k][MAX_SIZE+4k]
1690 *
1691 * Each range on their own accounts for 2 extents, but merged together
1692 * they are only 3 extents worth of accounting, so we need to drop in
1693 * this case.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001694 */
Josef Bacikba117212015-03-13 15:01:24 -04001695 old_size = other->end - other->start + 1;
David Sterba823bb202017-01-04 11:09:51 +01001696 num_extents = count_max_extents(old_size);
Josef Bacikba117212015-03-13 15:01:24 -04001697 old_size = new->end - new->start + 1;
David Sterba823bb202017-01-04 11:09:51 +01001698 num_extents += count_max_extents(old_size);
1699 if (count_max_extents(new_size) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001700 return;
1701
Josef Bacik9e0baf62011-07-15 15:16:44 +00001702 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04001703 btrfs_mod_outstanding_extents(BTRFS_I(inode), -1);
Josef Bacik9e0baf62011-07-15 15:16:44 +00001704 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001705}
1706
Miao Xieeb73c1b2013-05-15 07:48:22 +00001707static void btrfs_add_delalloc_inodes(struct btrfs_root *root,
1708 struct inode *inode)
1709{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001710 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1711
Miao Xieeb73c1b2013-05-15 07:48:22 +00001712 spin_lock(&root->delalloc_lock);
1713 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1714 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1715 &root->delalloc_inodes);
1716 set_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1717 &BTRFS_I(inode)->runtime_flags);
1718 root->nr_delalloc_inodes++;
1719 if (root->nr_delalloc_inodes == 1) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001720 spin_lock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001721 BUG_ON(!list_empty(&root->delalloc_root));
1722 list_add_tail(&root->delalloc_root,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001723 &fs_info->delalloc_roots);
1724 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001725 }
1726 }
1727 spin_unlock(&root->delalloc_lock);
1728}
1729
Nikolay Borisov2b877332018-04-27 12:21:51 +03001730
1731void __btrfs_del_delalloc_inode(struct btrfs_root *root,
1732 struct btrfs_inode *inode)
Miao Xieeb73c1b2013-05-15 07:48:22 +00001733{
David Sterba3ffbd682018-06-29 10:56:42 +02001734 struct btrfs_fs_info *fs_info = root->fs_info;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001735
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001736 if (!list_empty(&inode->delalloc_inodes)) {
1737 list_del_init(&inode->delalloc_inodes);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001738 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001739 &inode->runtime_flags);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001740 root->nr_delalloc_inodes--;
1741 if (!root->nr_delalloc_inodes) {
Nikolay Borisov7c8a0d32018-04-27 12:21:52 +03001742 ASSERT(list_empty(&root->delalloc_inodes));
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001743 spin_lock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001744 BUG_ON(list_empty(&root->delalloc_root));
1745 list_del_init(&root->delalloc_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001746 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001747 }
1748 }
Nikolay Borisov2b877332018-04-27 12:21:51 +03001749}
1750
1751static void btrfs_del_delalloc_inode(struct btrfs_root *root,
1752 struct btrfs_inode *inode)
1753{
1754 spin_lock(&root->delalloc_lock);
1755 __btrfs_del_delalloc_inode(root, inode);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001756 spin_unlock(&root->delalloc_lock);
1757}
1758
Chris Masond352ac62008-09-29 15:18:18 -04001759/*
Nikolay Borisove06a1fc2018-11-01 14:09:50 +02001760 * Properly track delayed allocation bytes in the inode and to maintain the
1761 * list of inodes that have pending delalloc work to be done.
Chris Masond352ac62008-09-29 15:18:18 -04001762 */
Nikolay Borisove06a1fc2018-11-01 14:09:50 +02001763void btrfs_set_delalloc_extent(struct inode *inode, struct extent_state *state,
1764 unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001765{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001766 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1767
Wang Shilong47059d92014-07-03 18:22:07 +08001768 if ((*bits & EXTENT_DEFRAG) && !(*bits & EXTENT_DELALLOC))
1769 WARN_ON(1);
Chris Mason75eff682008-12-15 15:54:40 -05001770 /*
1771 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001772 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001773 * bit, which is only set or cleared with irqs on
1774 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001775 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001776 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001777 u64 len = state->end + 1 - state->start;
Josef Bacik8b62f872017-10-19 14:15:55 -04001778 u32 num_extents = count_max_extents(len);
Nikolay Borisov70ddc552017-02-20 13:50:35 +02001779 bool do_list = !btrfs_is_free_space_inode(BTRFS_I(inode));
Josef Bacik9ed74f22009-09-11 16:12:44 -04001780
Josef Bacik8b62f872017-10-19 14:15:55 -04001781 spin_lock(&BTRFS_I(inode)->lock);
1782 btrfs_mod_outstanding_extents(BTRFS_I(inode), num_extents);
1783 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik287a0ab2010-03-19 18:07:23 +00001784
Josef Bacik6a3891c2015-03-16 17:38:52 -04001785 /* For sanity tests */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001786 if (btrfs_is_testing(fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04001787 return;
1788
Nikolay Borisov104b4e52017-06-20 21:01:20 +03001789 percpu_counter_add_batch(&fs_info->delalloc_bytes, len,
1790 fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001791 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001792 BTRFS_I(inode)->delalloc_bytes += len;
Wang Shilong47059d92014-07-03 18:22:07 +08001793 if (*bits & EXTENT_DEFRAG)
1794 BTRFS_I(inode)->defrag_bytes += len;
Miao Xiedf0af1a2013-01-29 10:11:59 +00001795 if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00001796 &BTRFS_I(inode)->runtime_flags))
1797 btrfs_add_delalloc_inodes(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001798 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001799 }
Filipe Mananaa7e3b972017-04-03 10:45:46 +01001800
1801 if (!(state->state & EXTENT_DELALLOC_NEW) &&
1802 (*bits & EXTENT_DELALLOC_NEW)) {
1803 spin_lock(&BTRFS_I(inode)->lock);
1804 BTRFS_I(inode)->new_delalloc_bytes += state->end + 1 -
1805 state->start;
1806 spin_unlock(&BTRFS_I(inode)->lock);
1807 }
Chris Mason291d6732008-01-29 15:55:23 -05001808}
1809
Chris Masond352ac62008-09-29 15:18:18 -04001810/*
Nikolay Borisova36bb5f2018-11-01 14:09:51 +02001811 * Once a range is no longer delalloc this function ensures that proper
1812 * accounting happens.
Chris Masond352ac62008-09-29 15:18:18 -04001813 */
Nikolay Borisova36bb5f2018-11-01 14:09:51 +02001814void btrfs_clear_delalloc_extent(struct inode *vfs_inode,
1815 struct extent_state *state, unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001816{
Nikolay Borisova36bb5f2018-11-01 14:09:51 +02001817 struct btrfs_inode *inode = BTRFS_I(vfs_inode);
1818 struct btrfs_fs_info *fs_info = btrfs_sb(vfs_inode->i_sb);
Wang Shilong47059d92014-07-03 18:22:07 +08001819 u64 len = state->end + 1 - state->start;
David Sterba823bb202017-01-04 11:09:51 +01001820 u32 num_extents = count_max_extents(len);
Wang Shilong47059d92014-07-03 18:22:07 +08001821
Filipe Manana4a4b9642017-07-27 19:52:55 +01001822 if ((state->state & EXTENT_DEFRAG) && (*bits & EXTENT_DEFRAG)) {
1823 spin_lock(&inode->lock);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001824 inode->defrag_bytes -= len;
Filipe Manana4a4b9642017-07-27 19:52:55 +01001825 spin_unlock(&inode->lock);
1826 }
Wang Shilong47059d92014-07-03 18:22:07 +08001827
Chris Mason75eff682008-12-15 15:54:40 -05001828 /*
1829 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001830 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001831 * bit, which is only set or cleared with irqs on
1832 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001833 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001834 struct btrfs_root *root = inode->root;
Liu Bo83eea1f2012-07-10 05:28:39 -06001835 bool do_list = !btrfs_is_free_space_inode(inode);
Chris Masonbcbfce82008-04-22 13:26:47 -04001836
Josef Bacik8b62f872017-10-19 14:15:55 -04001837 spin_lock(&inode->lock);
1838 btrfs_mod_outstanding_extents(inode, -num_extents);
1839 spin_unlock(&inode->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001840
Josef Bacikb6d08f02013-09-27 14:57:43 -04001841 /*
1842 * We don't reserve metadata space for space cache inodes so we
Andrea Gelmini52042d82018-11-28 12:05:13 +01001843 * don't need to call delalloc_release_metadata if there is an
Josef Bacikb6d08f02013-09-27 14:57:43 -04001844 * error.
1845 */
Filipe Mananaa315e682017-03-06 23:04:20 +00001846 if (*bits & EXTENT_CLEAR_META_RESV &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001847 root != fs_info->tree_root)
Qu Wenruo43b18592017-12-12 15:34:32 +08001848 btrfs_delalloc_release_metadata(inode, len, false);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001849
Josef Bacik6a3891c2015-03-16 17:38:52 -04001850 /* For sanity tests. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001851 if (btrfs_is_testing(fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04001852 return;
1853
Filipe Mananaa315e682017-03-06 23:04:20 +00001854 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID &&
1855 do_list && !(state->state & EXTENT_NORESERVE) &&
1856 (*bits & EXTENT_CLEAR_DATA_RESV))
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001857 btrfs_free_reserved_data_space_noquota(
1858 &inode->vfs_inode,
Qu Wenruo51773be2015-10-08 18:19:37 +08001859 state->start, len);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001860
Nikolay Borisov104b4e52017-06-20 21:01:20 +03001861 percpu_counter_add_batch(&fs_info->delalloc_bytes, -len,
1862 fs_info->delalloc_batch);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001863 spin_lock(&inode->lock);
1864 inode->delalloc_bytes -= len;
1865 if (do_list && inode->delalloc_bytes == 0 &&
Miao Xiedf0af1a2013-01-29 10:11:59 +00001866 test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001867 &inode->runtime_flags))
Miao Xieeb73c1b2013-05-15 07:48:22 +00001868 btrfs_del_delalloc_inode(root, inode);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001869 spin_unlock(&inode->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001870 }
Filipe Mananaa7e3b972017-04-03 10:45:46 +01001871
1872 if ((state->state & EXTENT_DELALLOC_NEW) &&
1873 (*bits & EXTENT_DELALLOC_NEW)) {
1874 spin_lock(&inode->lock);
1875 ASSERT(inode->new_delalloc_bytes >= len);
1876 inode->new_delalloc_bytes -= len;
1877 spin_unlock(&inode->lock);
1878 }
Chris Mason291d6732008-01-29 15:55:23 -05001879}
1880
Chris Masond352ac62008-09-29 15:18:18 -04001881/*
Nikolay Borisovda12fe52018-11-27 20:57:58 +02001882 * btrfs_bio_fits_in_stripe - Checks whether the size of the given bio will fit
1883 * in a chunk's stripe. This function ensures that bios do not span a
1884 * stripe/chunk
Liu Bo6f034ec2016-06-22 18:31:49 -07001885 *
Nikolay Borisovda12fe52018-11-27 20:57:58 +02001886 * @page - The page we are about to add to the bio
1887 * @size - size we want to add to the bio
1888 * @bio - bio we want to ensure is smaller than a stripe
1889 * @bio_flags - flags of the bio
1890 *
1891 * return 1 if page cannot be added to the bio
1892 * return 0 if page can be added to the bio
Liu Bo6f034ec2016-06-22 18:31:49 -07001893 * return error otherwise
Chris Masond352ac62008-09-29 15:18:18 -04001894 */
Nikolay Borisovda12fe52018-11-27 20:57:58 +02001895int btrfs_bio_fits_in_stripe(struct page *page, size_t size, struct bio *bio,
1896 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04001897{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001898 struct inode *inode = page->mapping->host;
1899 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001900 u64 logical = (u64)bio->bi_iter.bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04001901 u64 length = 0;
1902 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04001903 int ret;
1904
Chris Mason771ed682008-11-06 22:02:51 -05001905 if (bio_flags & EXTENT_BIO_COMPRESSED)
1906 return 0;
1907
Kent Overstreet4f024f32013-10-11 15:44:27 -07001908 length = bio->bi_iter.bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04001909 map_length = length;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001910 ret = btrfs_map_block(fs_info, btrfs_op(bio), logical, &map_length,
1911 NULL, 0);
Liu Bo6f034ec2016-06-22 18:31:49 -07001912 if (ret < 0)
1913 return ret;
Chris Masond3977122009-01-05 21:25:51 -05001914 if (map_length < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04001915 return 1;
Jeff Mahoney3444a972011-10-03 23:23:13 -04001916 return 0;
Chris Mason239b14b2008-03-24 15:02:07 -04001917}
1918
Chris Masond352ac62008-09-29 15:18:18 -04001919/*
1920 * in order to insert checksums into the metadata in large chunks,
1921 * we wait until bio submission time. All the pages in the bio are
1922 * checksummed and sums are attached onto the ordered extent record.
1923 *
1924 * At IO completion time the cums attached on the ordered extent record
1925 * are inserted into the btree
1926 */
David Sterbad0ee3932018-03-08 14:35:48 +01001927static blk_status_t btrfs_submit_bio_start(void *private_data, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001928 u64 bio_offset)
Chris Mason065631f2008-02-20 12:07:25 -05001929{
Josef Bacikc6100a42017-05-05 11:57:13 -04001930 struct inode *inode = private_data;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001931 blk_status_t ret = 0;
Chris Masone0156402008-04-16 11:15:20 -04001932
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001933 ret = btrfs_csum_one_bio(inode, bio, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001934 BUG_ON(ret); /* -ENOMEM */
Chris Mason4a69a412008-11-06 22:03:00 -05001935 return 0;
1936}
Chris Masone0156402008-04-16 11:15:20 -04001937
Chris Mason4a69a412008-11-06 22:03:00 -05001938/*
Chris Masoncad321a2008-12-17 14:51:42 -05001939 * extent_io.c submission hook. This does the right thing for csum calculation
Liu Bo4c274bc2017-11-01 17:19:27 -06001940 * on write, or reading the csums from the tree before a read.
1941 *
1942 * Rules about async/sync submit,
1943 * a) read: sync submit
1944 *
1945 * b) write without checksum: sync submit
1946 *
1947 * c) write with checksum:
1948 * c-1) if bio is issued by fsync: sync submit
1949 * (sync_writers != 0)
1950 *
1951 * c-2) if root is reloc root: sync submit
1952 * (only in case of buffered IO)
1953 *
1954 * c-3) otherwise: async submit
Chris Masond352ac62008-09-29 15:18:18 -04001955 */
Nikolay Borisova56b1c72019-04-10 17:24:39 +03001956static blk_status_t btrfs_submit_bio_hook(struct inode *inode, struct bio *bio,
Nikolay Borisov50489a52019-04-10 19:46:04 +03001957 int mirror_num,
1958 unsigned long bio_flags)
1959
Chris Mason44b8bd72008-04-16 11:14:51 -04001960{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001961 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason44b8bd72008-04-16 11:14:51 -04001962 struct btrfs_root *root = BTRFS_I(inode)->root;
Chandan Rajendra0d51e282015-07-27 15:26:43 +05301963 enum btrfs_wq_endio_type metadata = BTRFS_WQ_ENDIO_DATA;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001964 blk_status_t ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001965 int skip_sum;
Josef Bacikb812ce22012-11-16 13:56:32 -05001966 int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
Chris Mason44b8bd72008-04-16 11:14:51 -04001967
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001968 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masoncad321a2008-12-17 14:51:42 -05001969
Nikolay Borisov70ddc552017-02-20 13:50:35 +02001970 if (btrfs_is_free_space_inode(BTRFS_I(inode)))
Chandan Rajendra0d51e282015-07-27 15:26:43 +05301971 metadata = BTRFS_WQ_ENDIO_FREE_SPACE;
Jeff Mahoney04173412011-10-03 23:23:12 -04001972
Mike Christie37226b22016-06-05 14:31:52 -05001973 if (bio_op(bio) != REQ_OP_WRITE) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001974 ret = btrfs_bio_wq_end_io(fs_info, bio, metadata);
Josef Bacik5fd02042012-05-02 14:00:54 -04001975 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001976 goto out;
Josef Bacik5fd02042012-05-02 14:00:54 -04001977
Chris Masond20f7042008-12-08 16:58:54 -05001978 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Stefan Behrens61891922012-11-05 18:51:52 +01001979 ret = btrfs_submit_compressed_read(inode, bio,
1980 mirror_num,
1981 bio_flags);
1982 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001983 } else if (!skip_sum) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001984 ret = btrfs_lookup_bio_sums(inode, bio, NULL);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001985 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001986 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001987 }
Chris Mason4d1b5fb2008-08-20 09:44:52 -04001988 goto mapit;
Josef Bacikb812ce22012-11-16 13:56:32 -05001989 } else if (async && !skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05001990 /* csum items have already been cloned */
1991 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1992 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001993 /* we're doing a write, do the async checksumming */
Josef Bacikc6100a42017-05-05 11:57:13 -04001994 ret = btrfs_wq_submit_bio(fs_info, bio, mirror_num, bio_flags,
Nikolay Borisove7681162019-04-10 17:24:41 +03001995 0, inode, btrfs_submit_bio_start);
Stefan Behrens61891922012-11-05 18:51:52 +01001996 goto out;
Josef Bacikb812ce22012-11-16 13:56:32 -05001997 } else if (!skip_sum) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001998 ret = btrfs_csum_one_bio(inode, bio, 0, 0);
Josef Bacikb812ce22012-11-16 13:56:32 -05001999 if (ret)
2000 goto out;
Chris Mason19b9bdb2008-10-30 14:23:13 -04002001 }
2002
Chris Mason0b86a832008-03-24 15:01:56 -04002003mapit:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002004 ret = btrfs_map_bio(fs_info, bio, mirror_num, 0);
Stefan Behrens61891922012-11-05 18:51:52 +01002005
2006out:
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002007 if (ret) {
2008 bio->bi_status = ret;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002009 bio_endio(bio);
2010 }
Stefan Behrens61891922012-11-05 18:51:52 +01002011 return ret;
Chris Mason065631f2008-02-20 12:07:25 -05002012}
Chris Mason6885f302008-02-20 16:11:05 -05002013
Chris Masond352ac62008-09-29 15:18:18 -04002014/*
2015 * given a list of ordered sums record them in the inode. This happens
2016 * at IO completion time based on sums calculated at bio submission time.
2017 */
Chris Masonba1da2f2008-07-17 12:54:15 -04002018static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
David Sterbadf9f6282017-02-10 19:35:37 +01002019 struct inode *inode, struct list_head *list)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002020{
Chris Masone6dcd2d2008-07-17 12:53:50 -04002021 struct btrfs_ordered_sum *sum;
Nikolay Borisovac01f262018-01-08 10:59:43 +02002022 int ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002023
Qinghuang Fengc6e30872009-01-21 10:59:08 -05002024 list_for_each_entry(sum, list, list) {
David Sterba7c2871a2017-11-08 01:07:43 +01002025 trans->adding_csums = true;
Nikolay Borisovac01f262018-01-08 10:59:43 +02002026 ret = btrfs_csum_file_blocks(trans,
Chris Masond20f7042008-12-08 16:58:54 -05002027 BTRFS_I(inode)->root->fs_info->csum_root, sum);
David Sterba7c2871a2017-11-08 01:07:43 +01002028 trans->adding_csums = false;
Nikolay Borisovac01f262018-01-08 10:59:43 +02002029 if (ret)
2030 return ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002031 }
2032 return 0;
2033}
2034
Josef Bacik2ac55d42010-02-03 19:33:23 +00002035int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
Filipe Mananae3b8a482017-11-04 00:16:59 +00002036 unsigned int extra_bits,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08002037 struct extent_state **cached_state, int dedupe)
Chris Masonea8c2812008-08-04 23:17:27 -04002038{
Johannes Thumshirnfdb1e122018-12-05 15:23:04 +01002039 WARN_ON(PAGE_ALIGNED(end));
Chris Masonea8c2812008-08-04 23:17:27 -04002040 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
Filipe Mananae3b8a482017-11-04 00:16:59 +00002041 extra_bits, cached_state);
Chris Masonea8c2812008-08-04 23:17:27 -04002042}
2043
Chris Masond352ac62008-09-29 15:18:18 -04002044/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04002045struct btrfs_writepage_fixup {
2046 struct page *page;
2047 struct btrfs_work work;
2048};
2049
Christoph Hellwigb2950862008-12-02 09:54:17 -05002050static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04002051{
2052 struct btrfs_writepage_fixup *fixup;
2053 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002054 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08002055 struct extent_changeset *data_reserved = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04002056 struct page *page;
2057 struct inode *inode;
2058 u64 page_start;
2059 u64 page_end;
Jeff Mahoney87826df2012-02-15 16:23:57 +01002060 int ret;
Chris Mason247e7432008-07-17 12:53:51 -04002061
2062 fixup = container_of(work, struct btrfs_writepage_fixup, work);
2063 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -04002064again:
Chris Mason247e7432008-07-17 12:53:51 -04002065 lock_page(page);
2066 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
2067 ClearPageChecked(page);
2068 goto out_page;
2069 }
2070
2071 inode = page->mapping->host;
2072 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002073 page_end = page_offset(page) + PAGE_SIZE - 1;
Chris Mason247e7432008-07-17 12:53:51 -04002074
David Sterbaff13db42015-12-03 14:30:40 +01002075 lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002076 &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04002077
2078 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04002079 if (PagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04002080 goto out;
Chris Mason4a096752008-07-21 10:29:44 -04002081
Nikolay Borisova776c6f2017-02-20 13:50:49 +02002082 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002083 PAGE_SIZE);
Chris Mason4a096752008-07-21 10:29:44 -04002084 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00002085 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
David Sterbae43bbe52017-12-12 21:43:52 +01002086 page_end, &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04002087 unlock_page(page);
2088 btrfs_start_ordered_extent(inode, ordered, 1);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002089 btrfs_put_ordered_extent(ordered);
Chris Mason4a096752008-07-21 10:29:44 -04002090 goto again;
2091 }
Chris Mason247e7432008-07-17 12:53:51 -04002092
Qu Wenruo364ecf32017-02-27 15:10:38 +08002093 ret = btrfs_delalloc_reserve_space(inode, &data_reserved, page_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002094 PAGE_SIZE);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002095 if (ret) {
2096 mapping_set_error(page->mapping, ret);
2097 end_extent_writepage(page, ret, page_start, page_end);
2098 ClearPageChecked(page);
2099 goto out;
2100 }
2101
Nikolay Borisovf3038ee2017-12-05 09:29:19 +02002102 ret = btrfs_set_extent_delalloc(inode, page_start, page_end, 0,
2103 &cached_state, 0);
2104 if (ret) {
2105 mapping_set_error(page->mapping, ret);
2106 end_extent_writepage(page, ret, page_start, page_end);
2107 ClearPageChecked(page);
2108 goto out;
2109 }
2110
Chris Mason247e7432008-07-17 12:53:51 -04002111 ClearPageChecked(page);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002112 set_page_dirty(page);
Qu Wenruo43b18592017-12-12 15:34:32 +08002113 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE, false);
Chris Mason247e7432008-07-17 12:53:51 -04002114out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00002115 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01002116 &cached_state);
Chris Mason247e7432008-07-17 12:53:51 -04002117out_page:
2118 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002119 put_page(page);
Miao Xieb897abe2011-01-26 16:19:22 +08002120 kfree(fixup);
Qu Wenruo364ecf32017-02-27 15:10:38 +08002121 extent_changeset_free(data_reserved);
Chris Mason247e7432008-07-17 12:53:51 -04002122}
2123
2124/*
2125 * There are a few paths in the higher layers of the kernel that directly
2126 * set the page dirty bit without asking the filesystem if it is a
2127 * good idea. This causes problems because we want to make sure COW
2128 * properly happens and the data=ordered rules are followed.
2129 *
Chris Masonc8b97812008-10-29 14:49:59 -04002130 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04002131 * hasn't been properly setup for IO. We kick off an async process
2132 * to fix it up. The async helper will wait for ordered extents, set
2133 * the delalloc bit and make it safe to write the page.
2134 */
Nikolay Borisovd75855b2018-11-01 14:09:47 +02002135int btrfs_writepage_cow_fixup(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04002136{
2137 struct inode *inode = page->mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002138 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason247e7432008-07-17 12:53:51 -04002139 struct btrfs_writepage_fixup *fixup;
Chris Mason247e7432008-07-17 12:53:51 -04002140
Chris Mason8b62b722009-09-02 16:53:46 -04002141 /* this page is properly in the ordered list */
2142 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04002143 return 0;
2144
2145 if (PageChecked(page))
2146 return -EAGAIN;
2147
2148 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
2149 if (!fixup)
2150 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04002151
Chris Mason247e7432008-07-17 12:53:51 -04002152 SetPageChecked(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002153 get_page(page);
Liu Bo9e0af232014-08-15 23:36:53 +08002154 btrfs_init_work(&fixup->work, btrfs_fixup_helper,
2155 btrfs_writepage_fixup_worker, NULL, NULL);
Chris Mason247e7432008-07-17 12:53:51 -04002156 fixup->page = page;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002157 btrfs_queue_work(fs_info->fixup_workers, &fixup->work);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002158 return -EBUSY;
Chris Mason247e7432008-07-17 12:53:51 -04002159}
2160
Yan Zhengd899e052008-10-30 14:25:28 -04002161static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
2162 struct inode *inode, u64 file_pos,
2163 u64 disk_bytenr, u64 disk_num_bytes,
2164 u64 num_bytes, u64 ram_bytes,
2165 u8 compression, u8 encryption,
2166 u16 other_encoding, int extent_type)
2167{
2168 struct btrfs_root *root = BTRFS_I(inode)->root;
2169 struct btrfs_file_extent_item *fi;
2170 struct btrfs_path *path;
2171 struct extent_buffer *leaf;
2172 struct btrfs_key ins;
Qu Wenruoa12b8772017-02-27 15:10:37 +08002173 u64 qg_released;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002174 int extent_inserted = 0;
Yan Zhengd899e052008-10-30 14:25:28 -04002175 int ret;
2176
2177 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07002178 if (!path)
2179 return -ENOMEM;
Yan Zhengd899e052008-10-30 14:25:28 -04002180
Chris Masona1ed8352009-09-11 12:27:37 -04002181 /*
2182 * we may be replacing one extent in the tree with another.
2183 * The new extent is pinned in the extent map, and we don't want
2184 * to drop it from the cache until it is completely in the btree.
2185 *
2186 * So, tell btrfs_drop_extents to leave this extent in the cache.
2187 * the caller is expected to unpin it and allow it to be merged
2188 * with the others.
2189 */
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002190 ret = __btrfs_drop_extents(trans, root, inode, path, file_pos,
2191 file_pos + num_bytes, NULL, 0,
2192 1, sizeof(*fi), &extent_inserted);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002193 if (ret)
2194 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04002195
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002196 if (!extent_inserted) {
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002197 ins.objectid = btrfs_ino(BTRFS_I(inode));
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002198 ins.offset = file_pos;
2199 ins.type = BTRFS_EXTENT_DATA_KEY;
2200
2201 path->leave_spinning = 1;
2202 ret = btrfs_insert_empty_item(trans, root, path, &ins,
2203 sizeof(*fi));
2204 if (ret)
2205 goto out;
2206 }
Yan Zhengd899e052008-10-30 14:25:28 -04002207 leaf = path->nodes[0];
2208 fi = btrfs_item_ptr(leaf, path->slots[0],
2209 struct btrfs_file_extent_item);
2210 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
2211 btrfs_set_file_extent_type(leaf, fi, extent_type);
2212 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
2213 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
2214 btrfs_set_file_extent_offset(leaf, fi, 0);
2215 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
2216 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
2217 btrfs_set_file_extent_compression(leaf, fi, compression);
2218 btrfs_set_file_extent_encryption(leaf, fi, encryption);
2219 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
Chris Masonb9473432009-03-13 11:00:37 -04002220
Yan Zhengd899e052008-10-30 14:25:28 -04002221 btrfs_mark_buffer_dirty(leaf);
Josef Bacikce195332012-09-25 15:26:16 -04002222 btrfs_release_path(path);
Yan Zhengd899e052008-10-30 14:25:28 -04002223
2224 inode_add_bytes(inode, num_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -04002225
2226 ins.objectid = disk_bytenr;
2227 ins.offset = disk_num_bytes;
2228 ins.type = BTRFS_EXTENT_ITEM_KEY;
Qu Wenruoa12b8772017-02-27 15:10:37 +08002229
Qu Wenruo297d7502015-09-08 17:08:37 +08002230 /*
Qu Wenruo5846a3c2015-10-26 14:11:18 +08002231 * Release the reserved range from inode dirty range map, as it is
2232 * already moved into delayed_ref_head
Qu Wenruo297d7502015-09-08 17:08:37 +08002233 */
Qu Wenruoa12b8772017-02-27 15:10:37 +08002234 ret = btrfs_qgroup_release_data(inode, file_pos, ram_bytes);
2235 if (ret < 0)
2236 goto out;
2237 qg_released = ret;
Josef Bacik84f7d8e2017-09-29 15:43:49 -04002238 ret = btrfs_alloc_reserved_file_extent(trans, root,
2239 btrfs_ino(BTRFS_I(inode)),
2240 file_pos, qg_released, &ins);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002241out:
Yan Zhengd899e052008-10-30 14:25:28 -04002242 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04002243
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002244 return ret;
Yan Zhengd899e052008-10-30 14:25:28 -04002245}
2246
Liu Bo38c227d2013-01-29 03:18:40 +00002247/* snapshot-aware defrag */
2248struct sa_defrag_extent_backref {
2249 struct rb_node node;
2250 struct old_sa_defrag_extent *old;
2251 u64 root_id;
2252 u64 inum;
2253 u64 file_pos;
2254 u64 extent_offset;
2255 u64 num_bytes;
2256 u64 generation;
2257};
2258
2259struct old_sa_defrag_extent {
2260 struct list_head list;
2261 struct new_sa_defrag_extent *new;
2262
2263 u64 extent_offset;
2264 u64 bytenr;
2265 u64 offset;
2266 u64 len;
2267 int count;
2268};
2269
2270struct new_sa_defrag_extent {
2271 struct rb_root root;
2272 struct list_head head;
2273 struct btrfs_path *path;
2274 struct inode *inode;
2275 u64 file_pos;
2276 u64 len;
2277 u64 bytenr;
2278 u64 disk_len;
2279 u8 compress_type;
2280};
2281
2282static int backref_comp(struct sa_defrag_extent_backref *b1,
2283 struct sa_defrag_extent_backref *b2)
2284{
2285 if (b1->root_id < b2->root_id)
2286 return -1;
2287 else if (b1->root_id > b2->root_id)
2288 return 1;
2289
2290 if (b1->inum < b2->inum)
2291 return -1;
2292 else if (b1->inum > b2->inum)
2293 return 1;
2294
2295 if (b1->file_pos < b2->file_pos)
2296 return -1;
2297 else if (b1->file_pos > b2->file_pos)
2298 return 1;
2299
2300 /*
2301 * [------------------------------] ===> (a range of space)
2302 * |<--->| |<---->| =============> (fs/file tree A)
2303 * |<---------------------------->| ===> (fs/file tree B)
2304 *
2305 * A range of space can refer to two file extents in one tree while
2306 * refer to only one file extent in another tree.
2307 *
2308 * So we may process a disk offset more than one time(two extents in A)
2309 * and locate at the same extent(one extent in B), then insert two same
2310 * backrefs(both refer to the extent in B).
2311 */
2312 return 0;
2313}
2314
2315static void backref_insert(struct rb_root *root,
2316 struct sa_defrag_extent_backref *backref)
2317{
2318 struct rb_node **p = &root->rb_node;
2319 struct rb_node *parent = NULL;
2320 struct sa_defrag_extent_backref *entry;
2321 int ret;
2322
2323 while (*p) {
2324 parent = *p;
2325 entry = rb_entry(parent, struct sa_defrag_extent_backref, node);
2326
2327 ret = backref_comp(backref, entry);
2328 if (ret < 0)
2329 p = &(*p)->rb_left;
2330 else
2331 p = &(*p)->rb_right;
2332 }
2333
2334 rb_link_node(&backref->node, parent, p);
2335 rb_insert_color(&backref->node, root);
2336}
2337
2338/*
2339 * Note the backref might has changed, and in this case we just return 0.
2340 */
2341static noinline int record_one_backref(u64 inum, u64 offset, u64 root_id,
2342 void *ctx)
2343{
2344 struct btrfs_file_extent_item *extent;
Liu Bo38c227d2013-01-29 03:18:40 +00002345 struct old_sa_defrag_extent *old = ctx;
2346 struct new_sa_defrag_extent *new = old->new;
2347 struct btrfs_path *path = new->path;
2348 struct btrfs_key key;
2349 struct btrfs_root *root;
2350 struct sa_defrag_extent_backref *backref;
2351 struct extent_buffer *leaf;
2352 struct inode *inode = new->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002353 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002354 int slot;
2355 int ret;
2356 u64 extent_offset;
2357 u64 num_bytes;
2358
2359 if (BTRFS_I(inode)->root->root_key.objectid == root_id &&
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002360 inum == btrfs_ino(BTRFS_I(inode)))
Liu Bo38c227d2013-01-29 03:18:40 +00002361 return 0;
2362
2363 key.objectid = root_id;
2364 key.type = BTRFS_ROOT_ITEM_KEY;
2365 key.offset = (u64)-1;
2366
Liu Bo38c227d2013-01-29 03:18:40 +00002367 root = btrfs_read_fs_root_no_name(fs_info, &key);
2368 if (IS_ERR(root)) {
2369 if (PTR_ERR(root) == -ENOENT)
2370 return 0;
2371 WARN_ON(1);
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002372 btrfs_debug(fs_info, "inum=%llu, offset=%llu, root_id=%llu",
Liu Bo38c227d2013-01-29 03:18:40 +00002373 inum, offset, root_id);
2374 return PTR_ERR(root);
2375 }
2376
2377 key.objectid = inum;
2378 key.type = BTRFS_EXTENT_DATA_KEY;
2379 if (offset > (u64)-1 << 32)
2380 key.offset = 0;
2381 else
2382 key.offset = offset;
2383
2384 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05302385 if (WARN_ON(ret < 0))
Liu Bo38c227d2013-01-29 03:18:40 +00002386 return ret;
Josef Bacik50f13192013-07-22 12:50:37 -04002387 ret = 0;
Liu Bo38c227d2013-01-29 03:18:40 +00002388
2389 while (1) {
2390 cond_resched();
2391
2392 leaf = path->nodes[0];
2393 slot = path->slots[0];
2394
2395 if (slot >= btrfs_header_nritems(leaf)) {
2396 ret = btrfs_next_leaf(root, path);
2397 if (ret < 0) {
2398 goto out;
2399 } else if (ret > 0) {
2400 ret = 0;
2401 goto out;
2402 }
2403 continue;
2404 }
2405
2406 path->slots[0]++;
2407
2408 btrfs_item_key_to_cpu(leaf, &key, slot);
2409
2410 if (key.objectid > inum)
2411 goto out;
2412
2413 if (key.objectid < inum || key.type != BTRFS_EXTENT_DATA_KEY)
2414 continue;
2415
2416 extent = btrfs_item_ptr(leaf, slot,
2417 struct btrfs_file_extent_item);
2418
2419 if (btrfs_file_extent_disk_bytenr(leaf, extent) != old->bytenr)
2420 continue;
2421
Liu Boe68afa42013-07-01 22:13:26 +08002422 /*
2423 * 'offset' refers to the exact key.offset,
2424 * NOT the 'offset' field in btrfs_extent_data_ref, ie.
2425 * (key.offset - extent_offset).
2426 */
2427 if (key.offset != offset)
Liu Bo38c227d2013-01-29 03:18:40 +00002428 continue;
2429
Liu Boe68afa42013-07-01 22:13:26 +08002430 extent_offset = btrfs_file_extent_offset(leaf, extent);
Liu Bo38c227d2013-01-29 03:18:40 +00002431 num_bytes = btrfs_file_extent_num_bytes(leaf, extent);
Liu Boe68afa42013-07-01 22:13:26 +08002432
Liu Bo38c227d2013-01-29 03:18:40 +00002433 if (extent_offset >= old->extent_offset + old->offset +
2434 old->len || extent_offset + num_bytes <=
2435 old->extent_offset + old->offset)
2436 continue;
Liu Bo38c227d2013-01-29 03:18:40 +00002437 break;
2438 }
2439
2440 backref = kmalloc(sizeof(*backref), GFP_NOFS);
2441 if (!backref) {
2442 ret = -ENOENT;
2443 goto out;
2444 }
2445
2446 backref->root_id = root_id;
2447 backref->inum = inum;
Liu Boe68afa42013-07-01 22:13:26 +08002448 backref->file_pos = offset;
Liu Bo38c227d2013-01-29 03:18:40 +00002449 backref->num_bytes = num_bytes;
2450 backref->extent_offset = extent_offset;
2451 backref->generation = btrfs_file_extent_generation(leaf, extent);
2452 backref->old = old;
2453 backref_insert(&new->root, backref);
2454 old->count++;
2455out:
2456 btrfs_release_path(path);
2457 WARN_ON(ret);
2458 return ret;
2459}
2460
2461static noinline bool record_extent_backrefs(struct btrfs_path *path,
2462 struct new_sa_defrag_extent *new)
2463{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002464 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002465 struct old_sa_defrag_extent *old, *tmp;
2466 int ret;
2467
2468 new->path = path;
2469
2470 list_for_each_entry_safe(old, tmp, &new->head, list) {
Liu Boe68afa42013-07-01 22:13:26 +08002471 ret = iterate_inodes_from_logical(old->bytenr +
2472 old->extent_offset, fs_info,
Liu Bo38c227d2013-01-29 03:18:40 +00002473 path, record_one_backref,
Zygo Blaxellc995ab32017-09-22 13:58:45 -04002474 old, false);
Josef Bacik4724b102013-11-05 11:11:40 -05002475 if (ret < 0 && ret != -ENOENT)
2476 return false;
Liu Bo38c227d2013-01-29 03:18:40 +00002477
2478 /* no backref to be processed for this extent */
2479 if (!old->count) {
2480 list_del(&old->list);
2481 kfree(old);
2482 }
2483 }
2484
2485 if (list_empty(&new->head))
2486 return false;
2487
2488 return true;
2489}
2490
2491static int relink_is_mergable(struct extent_buffer *leaf,
2492 struct btrfs_file_extent_item *fi,
Liu Bo116e0022013-08-02 16:30:40 +08002493 struct new_sa_defrag_extent *new)
Liu Bo38c227d2013-01-29 03:18:40 +00002494{
Liu Bo116e0022013-08-02 16:30:40 +08002495 if (btrfs_file_extent_disk_bytenr(leaf, fi) != new->bytenr)
Liu Bo38c227d2013-01-29 03:18:40 +00002496 return 0;
2497
2498 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG)
2499 return 0;
2500
Liu Bo116e0022013-08-02 16:30:40 +08002501 if (btrfs_file_extent_compression(leaf, fi) != new->compress_type)
2502 return 0;
2503
2504 if (btrfs_file_extent_encryption(leaf, fi) ||
Liu Bo38c227d2013-01-29 03:18:40 +00002505 btrfs_file_extent_other_encoding(leaf, fi))
2506 return 0;
2507
2508 return 1;
2509}
2510
2511/*
2512 * Note the backref might has changed, and in this case we just return 0.
2513 */
2514static noinline int relink_extent_backref(struct btrfs_path *path,
2515 struct sa_defrag_extent_backref *prev,
2516 struct sa_defrag_extent_backref *backref)
2517{
2518 struct btrfs_file_extent_item *extent;
2519 struct btrfs_file_extent_item *item;
2520 struct btrfs_ordered_extent *ordered;
2521 struct btrfs_trans_handle *trans;
Qu Wenruo82fa1132019-04-04 14:45:35 +08002522 struct btrfs_ref ref = { 0 };
Liu Bo38c227d2013-01-29 03:18:40 +00002523 struct btrfs_root *root;
2524 struct btrfs_key key;
2525 struct extent_buffer *leaf;
2526 struct old_sa_defrag_extent *old = backref->old;
2527 struct new_sa_defrag_extent *new = old->new;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002528 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002529 struct inode *inode;
2530 struct extent_state *cached = NULL;
2531 int ret = 0;
2532 u64 start;
2533 u64 len;
2534 u64 lock_start;
2535 u64 lock_end;
2536 bool merge = false;
2537 int index;
2538
2539 if (prev && prev->root_id == backref->root_id &&
2540 prev->inum == backref->inum &&
2541 prev->file_pos + prev->num_bytes == backref->file_pos)
2542 merge = true;
2543
2544 /* step 1: get root */
2545 key.objectid = backref->root_id;
2546 key.type = BTRFS_ROOT_ITEM_KEY;
2547 key.offset = (u64)-1;
2548
Liu Bo38c227d2013-01-29 03:18:40 +00002549 index = srcu_read_lock(&fs_info->subvol_srcu);
2550
2551 root = btrfs_read_fs_root_no_name(fs_info, &key);
2552 if (IS_ERR(root)) {
2553 srcu_read_unlock(&fs_info->subvol_srcu, index);
2554 if (PTR_ERR(root) == -ENOENT)
2555 return 0;
2556 return PTR_ERR(root);
2557 }
Liu Bo38c227d2013-01-29 03:18:40 +00002558
Wang Shilongbcbba5e2014-02-08 23:46:35 +08002559 if (btrfs_root_readonly(root)) {
2560 srcu_read_unlock(&fs_info->subvol_srcu, index);
2561 return 0;
2562 }
2563
Liu Bo38c227d2013-01-29 03:18:40 +00002564 /* step 2: get inode */
2565 key.objectid = backref->inum;
2566 key.type = BTRFS_INODE_ITEM_KEY;
2567 key.offset = 0;
2568
2569 inode = btrfs_iget(fs_info->sb, &key, root, NULL);
2570 if (IS_ERR(inode)) {
2571 srcu_read_unlock(&fs_info->subvol_srcu, index);
2572 return 0;
2573 }
2574
2575 srcu_read_unlock(&fs_info->subvol_srcu, index);
2576
2577 /* step 3: relink backref */
2578 lock_start = backref->file_pos;
2579 lock_end = backref->file_pos + backref->num_bytes - 1;
2580 lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
David Sterbaff13db42015-12-03 14:30:40 +01002581 &cached);
Liu Bo38c227d2013-01-29 03:18:40 +00002582
2583 ordered = btrfs_lookup_first_ordered_extent(inode, lock_end);
2584 if (ordered) {
2585 btrfs_put_ordered_extent(ordered);
2586 goto out_unlock;
2587 }
2588
2589 trans = btrfs_join_transaction(root);
2590 if (IS_ERR(trans)) {
2591 ret = PTR_ERR(trans);
2592 goto out_unlock;
2593 }
2594
2595 key.objectid = backref->inum;
2596 key.type = BTRFS_EXTENT_DATA_KEY;
2597 key.offset = backref->file_pos;
2598
2599 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2600 if (ret < 0) {
2601 goto out_free_path;
2602 } else if (ret > 0) {
2603 ret = 0;
2604 goto out_free_path;
2605 }
2606
2607 extent = btrfs_item_ptr(path->nodes[0], path->slots[0],
2608 struct btrfs_file_extent_item);
2609
2610 if (btrfs_file_extent_generation(path->nodes[0], extent) !=
2611 backref->generation)
2612 goto out_free_path;
2613
2614 btrfs_release_path(path);
2615
2616 start = backref->file_pos;
2617 if (backref->extent_offset < old->extent_offset + old->offset)
2618 start += old->extent_offset + old->offset -
2619 backref->extent_offset;
2620
2621 len = min(backref->extent_offset + backref->num_bytes,
2622 old->extent_offset + old->offset + old->len);
2623 len -= max(backref->extent_offset, old->extent_offset + old->offset);
2624
2625 ret = btrfs_drop_extents(trans, root, inode, start,
2626 start + len, 1);
2627 if (ret)
2628 goto out_free_path;
2629again:
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002630 key.objectid = btrfs_ino(BTRFS_I(inode));
Liu Bo38c227d2013-01-29 03:18:40 +00002631 key.type = BTRFS_EXTENT_DATA_KEY;
2632 key.offset = start;
2633
Liu Boa09a0a72013-03-11 09:20:58 +00002634 path->leave_spinning = 1;
Liu Bo38c227d2013-01-29 03:18:40 +00002635 if (merge) {
2636 struct btrfs_file_extent_item *fi;
2637 u64 extent_len;
2638 struct btrfs_key found_key;
2639
Gui Hecheng3c9665d2014-01-23 13:41:09 +08002640 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
Liu Bo38c227d2013-01-29 03:18:40 +00002641 if (ret < 0)
2642 goto out_free_path;
2643
2644 path->slots[0]--;
2645 leaf = path->nodes[0];
2646 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2647
2648 fi = btrfs_item_ptr(leaf, path->slots[0],
2649 struct btrfs_file_extent_item);
2650 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
2651
Liu Bo116e0022013-08-02 16:30:40 +08002652 if (extent_len + found_key.offset == start &&
2653 relink_is_mergable(leaf, fi, new)) {
Liu Bo38c227d2013-01-29 03:18:40 +00002654 btrfs_set_file_extent_num_bytes(leaf, fi,
2655 extent_len + len);
2656 btrfs_mark_buffer_dirty(leaf);
2657 inode_add_bytes(inode, len);
2658
2659 ret = 1;
2660 goto out_free_path;
2661 } else {
2662 merge = false;
2663 btrfs_release_path(path);
2664 goto again;
2665 }
2666 }
2667
2668 ret = btrfs_insert_empty_item(trans, root, path, &key,
2669 sizeof(*extent));
2670 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002671 btrfs_abort_transaction(trans, ret);
Liu Bo38c227d2013-01-29 03:18:40 +00002672 goto out_free_path;
2673 }
2674
2675 leaf = path->nodes[0];
2676 item = btrfs_item_ptr(leaf, path->slots[0],
2677 struct btrfs_file_extent_item);
2678 btrfs_set_file_extent_disk_bytenr(leaf, item, new->bytenr);
2679 btrfs_set_file_extent_disk_num_bytes(leaf, item, new->disk_len);
2680 btrfs_set_file_extent_offset(leaf, item, start - new->file_pos);
2681 btrfs_set_file_extent_num_bytes(leaf, item, len);
2682 btrfs_set_file_extent_ram_bytes(leaf, item, new->len);
2683 btrfs_set_file_extent_generation(leaf, item, trans->transid);
2684 btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG);
2685 btrfs_set_file_extent_compression(leaf, item, new->compress_type);
2686 btrfs_set_file_extent_encryption(leaf, item, 0);
2687 btrfs_set_file_extent_other_encoding(leaf, item, 0);
2688
2689 btrfs_mark_buffer_dirty(leaf);
2690 inode_add_bytes(inode, len);
Liu Boa09a0a72013-03-11 09:20:58 +00002691 btrfs_release_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002692
Qu Wenruo82fa1132019-04-04 14:45:35 +08002693 btrfs_init_generic_ref(&ref, BTRFS_ADD_DELAYED_REF, new->bytenr,
2694 new->disk_len, 0);
2695 btrfs_init_data_ref(&ref, backref->root_id, backref->inum,
2696 new->file_pos); /* start - extent_offset */
2697 ret = btrfs_inc_extent_ref(trans, &ref);
Liu Bo38c227d2013-01-29 03:18:40 +00002698 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002699 btrfs_abort_transaction(trans, ret);
Liu Bo38c227d2013-01-29 03:18:40 +00002700 goto out_free_path;
2701 }
2702
2703 ret = 1;
2704out_free_path:
2705 btrfs_release_path(path);
Liu Boa09a0a72013-03-11 09:20:58 +00002706 path->leave_spinning = 0;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002707 btrfs_end_transaction(trans);
Liu Bo38c227d2013-01-29 03:18:40 +00002708out_unlock:
2709 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
David Sterbae43bbe52017-12-12 21:43:52 +01002710 &cached);
Liu Bo38c227d2013-01-29 03:18:40 +00002711 iput(inode);
2712 return ret;
2713}
2714
Liu Bo6f519562013-10-29 10:45:05 +08002715static void free_sa_defrag_extent(struct new_sa_defrag_extent *new)
2716{
2717 struct old_sa_defrag_extent *old, *tmp;
2718
2719 if (!new)
2720 return;
2721
2722 list_for_each_entry_safe(old, tmp, &new->head, list) {
Liu Bo6f519562013-10-29 10:45:05 +08002723 kfree(old);
2724 }
2725 kfree(new);
2726}
2727
Liu Bo38c227d2013-01-29 03:18:40 +00002728static void relink_file_extents(struct new_sa_defrag_extent *new)
2729{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002730 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002731 struct btrfs_path *path;
Liu Bo38c227d2013-01-29 03:18:40 +00002732 struct sa_defrag_extent_backref *backref;
2733 struct sa_defrag_extent_backref *prev = NULL;
Liu Bo38c227d2013-01-29 03:18:40 +00002734 struct rb_node *node;
2735 int ret;
2736
Liu Bo38c227d2013-01-29 03:18:40 +00002737 path = btrfs_alloc_path();
2738 if (!path)
2739 return;
2740
2741 if (!record_extent_backrefs(path, new)) {
2742 btrfs_free_path(path);
2743 goto out;
2744 }
2745 btrfs_release_path(path);
2746
2747 while (1) {
2748 node = rb_first(&new->root);
2749 if (!node)
2750 break;
2751 rb_erase(node, &new->root);
2752
2753 backref = rb_entry(node, struct sa_defrag_extent_backref, node);
2754
2755 ret = relink_extent_backref(path, prev, backref);
2756 WARN_ON(ret < 0);
2757
2758 kfree(prev);
2759
2760 if (ret == 1)
2761 prev = backref;
2762 else
2763 prev = NULL;
2764 cond_resched();
2765 }
2766 kfree(prev);
2767
2768 btrfs_free_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002769out:
Liu Bo6f519562013-10-29 10:45:05 +08002770 free_sa_defrag_extent(new);
2771
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002772 atomic_dec(&fs_info->defrag_running);
2773 wake_up(&fs_info->transaction_wait);
Liu Bo38c227d2013-01-29 03:18:40 +00002774}
2775
2776static struct new_sa_defrag_extent *
2777record_old_file_extents(struct inode *inode,
2778 struct btrfs_ordered_extent *ordered)
2779{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002780 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002781 struct btrfs_root *root = BTRFS_I(inode)->root;
2782 struct btrfs_path *path;
2783 struct btrfs_key key;
Liu Bo6f519562013-10-29 10:45:05 +08002784 struct old_sa_defrag_extent *old;
Liu Bo38c227d2013-01-29 03:18:40 +00002785 struct new_sa_defrag_extent *new;
2786 int ret;
2787
2788 new = kmalloc(sizeof(*new), GFP_NOFS);
2789 if (!new)
2790 return NULL;
2791
2792 new->inode = inode;
2793 new->file_pos = ordered->file_offset;
2794 new->len = ordered->len;
2795 new->bytenr = ordered->start;
2796 new->disk_len = ordered->disk_len;
2797 new->compress_type = ordered->compress_type;
2798 new->root = RB_ROOT;
2799 INIT_LIST_HEAD(&new->head);
2800
2801 path = btrfs_alloc_path();
2802 if (!path)
2803 goto out_kfree;
2804
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002805 key.objectid = btrfs_ino(BTRFS_I(inode));
Liu Bo38c227d2013-01-29 03:18:40 +00002806 key.type = BTRFS_EXTENT_DATA_KEY;
2807 key.offset = new->file_pos;
2808
2809 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2810 if (ret < 0)
2811 goto out_free_path;
2812 if (ret > 0 && path->slots[0] > 0)
2813 path->slots[0]--;
2814
2815 /* find out all the old extents for the file range */
2816 while (1) {
2817 struct btrfs_file_extent_item *extent;
2818 struct extent_buffer *l;
2819 int slot;
2820 u64 num_bytes;
2821 u64 offset;
2822 u64 end;
2823 u64 disk_bytenr;
2824 u64 extent_offset;
2825
2826 l = path->nodes[0];
2827 slot = path->slots[0];
2828
2829 if (slot >= btrfs_header_nritems(l)) {
2830 ret = btrfs_next_leaf(root, path);
2831 if (ret < 0)
Liu Bo6f519562013-10-29 10:45:05 +08002832 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002833 else if (ret > 0)
2834 break;
2835 continue;
2836 }
2837
2838 btrfs_item_key_to_cpu(l, &key, slot);
2839
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002840 if (key.objectid != btrfs_ino(BTRFS_I(inode)))
Liu Bo38c227d2013-01-29 03:18:40 +00002841 break;
2842 if (key.type != BTRFS_EXTENT_DATA_KEY)
2843 break;
2844 if (key.offset >= new->file_pos + new->len)
2845 break;
2846
2847 extent = btrfs_item_ptr(l, slot, struct btrfs_file_extent_item);
2848
2849 num_bytes = btrfs_file_extent_num_bytes(l, extent);
2850 if (key.offset + num_bytes < new->file_pos)
2851 goto next;
2852
2853 disk_bytenr = btrfs_file_extent_disk_bytenr(l, extent);
2854 if (!disk_bytenr)
2855 goto next;
2856
2857 extent_offset = btrfs_file_extent_offset(l, extent);
2858
2859 old = kmalloc(sizeof(*old), GFP_NOFS);
2860 if (!old)
Liu Bo6f519562013-10-29 10:45:05 +08002861 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002862
2863 offset = max(new->file_pos, key.offset);
2864 end = min(new->file_pos + new->len, key.offset + num_bytes);
2865
2866 old->bytenr = disk_bytenr;
2867 old->extent_offset = extent_offset;
2868 old->offset = offset - key.offset;
2869 old->len = end - offset;
2870 old->new = new;
2871 old->count = 0;
2872 list_add_tail(&old->list, &new->head);
2873next:
2874 path->slots[0]++;
2875 cond_resched();
2876 }
2877
2878 btrfs_free_path(path);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002879 atomic_inc(&fs_info->defrag_running);
Liu Bo38c227d2013-01-29 03:18:40 +00002880
2881 return new;
2882
Liu Bo38c227d2013-01-29 03:18:40 +00002883out_free_path:
2884 btrfs_free_path(path);
2885out_kfree:
Liu Bo6f519562013-10-29 10:45:05 +08002886 free_sa_defrag_extent(new);
Liu Bo38c227d2013-01-29 03:18:40 +00002887 return NULL;
2888}
2889
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002890static void btrfs_release_delalloc_bytes(struct btrfs_fs_info *fs_info,
Miao Xiee570fd22014-06-19 10:42:50 +08002891 u64 start, u64 len)
2892{
2893 struct btrfs_block_group_cache *cache;
2894
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002895 cache = btrfs_lookup_block_group(fs_info, start);
Miao Xiee570fd22014-06-19 10:42:50 +08002896 ASSERT(cache);
2897
2898 spin_lock(&cache->lock);
2899 cache->delalloc_bytes -= len;
2900 spin_unlock(&cache->lock);
2901
2902 btrfs_put_block_group(cache);
2903}
2904
Chris Masond352ac62008-09-29 15:18:18 -04002905/* as ordered data IO finishes, this gets called so we can finish
2906 * an ordered extent if the range of bytes in the file it covers are
2907 * fully written.
2908 */
Josef Bacik5fd02042012-05-02 14:00:54 -04002909static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002910{
Josef Bacik5fd02042012-05-02 14:00:54 -04002911 struct inode *inode = ordered_extent->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002912 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002913 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002914 struct btrfs_trans_handle *trans = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002915 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002916 struct extent_state *cached_state = NULL;
Liu Bo38c227d2013-01-29 03:18:40 +00002917 struct new_sa_defrag_extent *new = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08002918 int compress_type = 0;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002919 int ret = 0;
2920 u64 logical_len = ordered_extent->len;
Li Zefan82d59022011-04-20 10:33:24 +08002921 bool nolock;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002922 bool truncated = false;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002923 bool range_locked = false;
2924 bool clear_new_delalloc_bytes = false;
Josef Bacik49940bd2018-10-11 15:54:21 -04002925 bool clear_reserved_extent = true;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002926
2927 if (!test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
2928 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags) &&
2929 !test_bit(BTRFS_ORDERED_DIRECT, &ordered_extent->flags))
2930 clear_new_delalloc_bytes = true;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002931
Nikolay Borisov70ddc552017-02-20 13:50:35 +02002932 nolock = btrfs_is_free_space_inode(BTRFS_I(inode));
Josef Bacik0cb59c92010-07-02 12:14:14 -04002933
Josef Bacik5fd02042012-05-02 14:00:54 -04002934 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
2935 ret = -EIO;
2936 goto out;
2937 }
2938
Nikolay Borisov7ab79562017-02-20 13:50:57 +02002939 btrfs_free_io_failure_record(BTRFS_I(inode),
2940 ordered_extent->file_offset,
2941 ordered_extent->file_offset +
2942 ordered_extent->len - 1);
Miao Xief6124962014-09-12 18:44:04 +08002943
Josef Bacik77cef2e2013-08-29 13:57:21 -04002944 if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) {
2945 truncated = true;
2946 logical_len = ordered_extent->truncated_len;
2947 /* Truncated the entire extent, don't bother adding */
2948 if (!logical_len)
2949 goto out;
2950 }
2951
Yan, Zhengc2167752009-11-12 09:34:21 +00002952 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002953 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
Qu Wenruo94ed9382015-09-08 17:25:56 +08002954
2955 /*
2956 * For mwrite(mmap + memset to write) case, we still reserve
2957 * space for NOCOW range.
2958 * As NOCOW won't cause a new delayed ref, just free the space
2959 */
Qu Wenruobc42bda2017-02-27 15:10:39 +08002960 btrfs_qgroup_free_data(inode, NULL, ordered_extent->file_offset,
Qu Wenruo94ed9382015-09-08 17:25:56 +08002961 ordered_extent->len);
Josef Bacik6c760c02012-11-09 10:53:21 -05002962 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2963 if (nolock)
2964 trans = btrfs_join_transaction_nolock(root);
2965 else
2966 trans = btrfs_join_transaction(root);
2967 if (IS_ERR(trans)) {
2968 ret = PTR_ERR(trans);
2969 trans = NULL;
2970 goto out;
Yan, Zhengc2167752009-11-12 09:34:21 +00002971 }
Josef Bacik69fe2d72017-10-19 14:15:57 -04002972 trans->block_rsv = &BTRFS_I(inode)->block_rsv;
Josef Bacik6c760c02012-11-09 10:53:21 -05002973 ret = btrfs_update_inode_fallback(trans, root, inode);
2974 if (ret) /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04002975 btrfs_abort_transaction(trans, ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00002976 goto out;
2977 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002978
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002979 range_locked = true;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002980 lock_extent_bits(io_tree, ordered_extent->file_offset,
2981 ordered_extent->file_offset + ordered_extent->len - 1,
David Sterbaff13db42015-12-03 14:30:40 +01002982 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002983
Liu Bo38c227d2013-01-29 03:18:40 +00002984 ret = test_range_bit(io_tree, ordered_extent->file_offset,
2985 ordered_extent->file_offset + ordered_extent->len - 1,
Liu Bo452e62b2017-05-26 17:44:23 -06002986 EXTENT_DEFRAG, 0, cached_state);
Liu Bo38c227d2013-01-29 03:18:40 +00002987 if (ret) {
2988 u64 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
Josef Bacik8101c8d2014-01-29 16:05:30 -05002989 if (0 && last_snapshot >= BTRFS_I(inode)->generation)
Liu Bo38c227d2013-01-29 03:18:40 +00002990 /* the inode is shared */
2991 new = record_old_file_extents(inode, ordered_extent);
2992
2993 clear_extent_bit(io_tree, ordered_extent->file_offset,
2994 ordered_extent->file_offset + ordered_extent->len - 1,
David Sterbaae0f1622017-10-31 16:37:52 +01002995 EXTENT_DEFRAG, 0, 0, &cached_state);
Liu Bo38c227d2013-01-29 03:18:40 +00002996 }
2997
Josef Bacik0cb59c92010-07-02 12:14:14 -04002998 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002999 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04003000 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003001 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003002 if (IS_ERR(trans)) {
3003 ret = PTR_ERR(trans);
3004 trans = NULL;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003005 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003006 }
Chris Masona79b7d42014-05-22 16:18:52 -07003007
Josef Bacik69fe2d72017-10-19 14:15:57 -04003008 trans->block_rsv = &BTRFS_I(inode)->block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00003009
Chris Masonc8b97812008-10-29 14:49:59 -04003010 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08003011 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04003012 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08003013 BUG_ON(compress_type);
Justin Maggardb430b772017-10-30 15:29:10 -07003014 btrfs_qgroup_free_data(inode, NULL, ordered_extent->file_offset,
3015 ordered_extent->len);
Nikolay Borisov7a6d7062017-02-20 13:50:48 +02003016 ret = btrfs_mark_extent_written(trans, BTRFS_I(inode),
Yan Zhengd899e052008-10-30 14:25:28 -04003017 ordered_extent->file_offset,
3018 ordered_extent->file_offset +
Josef Bacik77cef2e2013-08-29 13:57:21 -04003019 logical_len);
Yan Zhengd899e052008-10-30 14:25:28 -04003020 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003021 BUG_ON(root == fs_info->tree_root);
Yan Zhengd899e052008-10-30 14:25:28 -04003022 ret = insert_reserved_file_extent(trans, inode,
3023 ordered_extent->file_offset,
3024 ordered_extent->start,
3025 ordered_extent->disk_len,
Josef Bacik77cef2e2013-08-29 13:57:21 -04003026 logical_len, logical_len,
Li Zefan261507a02010-12-17 14:21:50 +08003027 compress_type, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04003028 BTRFS_FILE_EXTENT_REG);
Josef Bacik49940bd2018-10-11 15:54:21 -04003029 if (!ret) {
3030 clear_reserved_extent = false;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003031 btrfs_release_delalloc_bytes(fs_info,
Miao Xiee570fd22014-06-19 10:42:50 +08003032 ordered_extent->start,
3033 ordered_extent->disk_len);
Josef Bacik49940bd2018-10-11 15:54:21 -04003034 }
Yan Zhengd899e052008-10-30 14:25:28 -04003035 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04003036 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
3037 ordered_extent->file_offset, ordered_extent->len,
3038 trans->transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003039 if (ret < 0) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003040 btrfs_abort_transaction(trans, ret);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003041 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003042 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00003043
Nikolay Borisovac01f262018-01-08 10:59:43 +02003044 ret = add_pending_csums(trans, inode, &ordered_extent->list);
3045 if (ret) {
3046 btrfs_abort_transaction(trans, ret);
3047 goto out;
3048 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04003049
Josef Bacik6c760c02012-11-09 10:53:21 -05003050 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
3051 ret = btrfs_update_inode_fallback(trans, root, inode);
3052 if (ret) { /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04003053 btrfs_abort_transaction(trans, ret);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003054 goto out;
Josef Bacik1ef30be2011-04-05 19:25:36 -04003055 }
3056 ret = 0;
Yan, Zhengc2167752009-11-12 09:34:21 +00003057out:
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003058 if (range_locked || clear_new_delalloc_bytes) {
3059 unsigned int clear_bits = 0;
3060
3061 if (range_locked)
3062 clear_bits |= EXTENT_LOCKED;
3063 if (clear_new_delalloc_bytes)
3064 clear_bits |= EXTENT_DELALLOC_NEW;
3065 clear_extent_bit(&BTRFS_I(inode)->io_tree,
3066 ordered_extent->file_offset,
3067 ordered_extent->file_offset +
3068 ordered_extent->len - 1,
3069 clear_bits,
3070 (clear_bits & EXTENT_LOCKED) ? 1 : 0,
David Sterbaae0f1622017-10-31 16:37:52 +01003071 0, &cached_state);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003072 }
3073
Miao Xiea698d0752012-09-20 01:51:59 -06003074 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003075 btrfs_end_transaction(trans);
Josef Bacik0cb59c92010-07-02 12:14:14 -04003076
Josef Bacik77cef2e2013-08-29 13:57:21 -04003077 if (ret || truncated) {
3078 u64 start, end;
3079
3080 if (truncated)
3081 start = ordered_extent->file_offset + logical_len;
3082 else
3083 start = ordered_extent->file_offset;
3084 end = ordered_extent->file_offset + ordered_extent->len - 1;
David Sterbaf08dc362017-10-31 17:02:39 +01003085 clear_extent_uptodate(io_tree, start, end, NULL);
Josef Bacik77cef2e2013-08-29 13:57:21 -04003086
3087 /* Drop the cache for the part of the extent we didn't write. */
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02003088 btrfs_drop_extent_cache(BTRFS_I(inode), start, end, 0);
Josef Bacik5fd02042012-05-02 14:00:54 -04003089
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003090 /*
3091 * If the ordered extent had an IOERR or something else went
3092 * wrong we need to return the space for this ordered extent
Josef Bacik77cef2e2013-08-29 13:57:21 -04003093 * back to the allocator. We only free the extent in the
3094 * truncated case if we didn't write out the extent at all.
Josef Bacik49940bd2018-10-11 15:54:21 -04003095 *
3096 * If we made it past insert_reserved_file_extent before we
3097 * errored out then we don't need to do this as the accounting
3098 * has already been done.
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003099 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04003100 if ((ret || !logical_len) &&
Josef Bacik49940bd2018-10-11 15:54:21 -04003101 clear_reserved_extent &&
Josef Bacik77cef2e2013-08-29 13:57:21 -04003102 !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003103 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003104 btrfs_free_reserved_extent(fs_info,
3105 ordered_extent->start,
Miao Xiee570fd22014-06-19 10:42:50 +08003106 ordered_extent->disk_len, 1);
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003107 }
3108
3109
Josef Bacik5fd02042012-05-02 14:00:54 -04003110 /*
Liu Bo8bad3c02012-06-18 12:14:23 +08003111 * This needs to be done to make sure anybody waiting knows we are done
3112 * updating everything for this ordered extent.
Josef Bacik5fd02042012-05-02 14:00:54 -04003113 */
3114 btrfs_remove_ordered_extent(inode, ordered_extent);
3115
Liu Bo38c227d2013-01-29 03:18:40 +00003116 /* for snapshot-aware defrag */
Liu Bo6f519562013-10-29 10:45:05 +08003117 if (new) {
3118 if (ret) {
3119 free_sa_defrag_extent(new);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003120 atomic_dec(&fs_info->defrag_running);
Liu Bo6f519562013-10-29 10:45:05 +08003121 } else {
3122 relink_file_extents(new);
3123 }
3124 }
Liu Bo38c227d2013-01-29 03:18:40 +00003125
Chris Masone6dcd2d2008-07-17 12:53:50 -04003126 /* once for us */
3127 btrfs_put_ordered_extent(ordered_extent);
3128 /* once for the tree */
3129 btrfs_put_ordered_extent(ordered_extent);
3130
Josef Bacik5fd02042012-05-02 14:00:54 -04003131 return ret;
3132}
3133
3134static void finish_ordered_fn(struct btrfs_work *work)
3135{
3136 struct btrfs_ordered_extent *ordered_extent;
3137 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
3138 btrfs_finish_ordered_io(ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003139}
3140
Nikolay Borisovc6297322018-11-08 10:18:08 +02003141void btrfs_writepage_endio_finish_ordered(struct page *page, u64 start,
3142 u64 end, int uptodate)
Chris Mason211f90e2008-07-18 11:56:15 -04003143{
Josef Bacik5fd02042012-05-02 14:00:54 -04003144 struct inode *inode = page->mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003145 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik5fd02042012-05-02 14:00:54 -04003146 struct btrfs_ordered_extent *ordered_extent = NULL;
Liu Bo9e0af232014-08-15 23:36:53 +08003147 struct btrfs_workqueue *wq;
3148 btrfs_work_func_t func;
Josef Bacik5fd02042012-05-02 14:00:54 -04003149
liubo1abe9b82011-03-24 11:18:59 +00003150 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
3151
Chris Mason8b62b722009-09-02 16:53:46 -04003152 ClearPagePrivate2(page);
Josef Bacik5fd02042012-05-02 14:00:54 -04003153 if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
3154 end - start + 1, uptodate))
David Sterbac3988d62017-02-17 15:18:32 +01003155 return;
Josef Bacik5fd02042012-05-02 14:00:54 -04003156
Nikolay Borisov70ddc552017-02-20 13:50:35 +02003157 if (btrfs_is_free_space_inode(BTRFS_I(inode))) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003158 wq = fs_info->endio_freespace_worker;
Liu Bo9e0af232014-08-15 23:36:53 +08003159 func = btrfs_freespace_write_helper;
3160 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003161 wq = fs_info->endio_write_workers;
Liu Bo9e0af232014-08-15 23:36:53 +08003162 func = btrfs_endio_write_helper;
3163 }
Josef Bacik5fd02042012-05-02 14:00:54 -04003164
Liu Bo9e0af232014-08-15 23:36:53 +08003165 btrfs_init_work(&ordered_extent->work, func, finish_ordered_fn, NULL,
3166 NULL);
3167 btrfs_queue_work(wq, &ordered_extent->work);
Chris Mason211f90e2008-07-18 11:56:15 -04003168}
3169
Miao Xiedc380ae2014-09-12 18:43:55 +08003170static int __readpage_endio_check(struct inode *inode,
3171 struct btrfs_io_bio *io_bio,
3172 int icsum, struct page *page,
3173 int pgoff, u64 start, size_t len)
3174{
3175 char *kaddr;
3176 u32 csum_expected;
3177 u32 csum = ~(u32)0;
Miao Xiedc380ae2014-09-12 18:43:55 +08003178
3179 csum_expected = *(((u32 *)io_bio->csum) + icsum);
3180
3181 kaddr = kmap_atomic(page);
3182 csum = btrfs_csum_data(kaddr + pgoff, csum, len);
Domagoj Tršan0b5e3da2016-10-27 08:52:33 +01003183 btrfs_csum_final(csum, (u8 *)&csum);
Miao Xiedc380ae2014-09-12 18:43:55 +08003184 if (csum != csum_expected)
3185 goto zeroit;
3186
3187 kunmap_atomic(kaddr);
3188 return 0;
3189zeroit:
Nikolay Borisov0970a222017-02-20 13:50:53 +02003190 btrfs_print_data_csum_error(BTRFS_I(inode), start, csum, csum_expected,
Qu Wenruo6f6b6432017-02-09 10:45:06 +08003191 io_bio->mirror_num);
Miao Xiedc380ae2014-09-12 18:43:55 +08003192 memset(kaddr + pgoff, 1, len);
3193 flush_dcache_page(page);
3194 kunmap_atomic(kaddr);
Miao Xiedc380ae2014-09-12 18:43:55 +08003195 return -EIO;
3196}
3197
Chris Masond352ac62008-09-29 15:18:18 -04003198/*
Chris Masond352ac62008-09-29 15:18:18 -04003199 * when reads are done, we need to check csums to verify the data is correct
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02003200 * if there's a match, we allow the bio to finish. If not, the code in
3201 * extent_io.c will try to find good copies for us.
Chris Masond352ac62008-09-29 15:18:18 -04003202 */
Miao Xiefacc8a222013-07-25 19:22:34 +08003203static int btrfs_readpage_end_io_hook(struct btrfs_io_bio *io_bio,
3204 u64 phy_offset, struct page *page,
3205 u64 start, u64 end, int mirror)
Chris Mason07157aa2007-08-30 08:50:51 -04003206{
Miao Xie4eee4fa2012-12-21 09:17:45 +00003207 size_t offset = start - page_offset(page);
Chris Mason07157aa2007-08-30 08:50:51 -04003208 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05003209 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masonff79f812007-10-15 16:22:25 -04003210 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -05003211
Chris Masond20f7042008-12-08 16:58:54 -05003212 if (PageChecked(page)) {
3213 ClearPageChecked(page);
Miao Xiedc380ae2014-09-12 18:43:55 +08003214 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003215 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003216
3217 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Miao Xiedc380ae2014-09-12 18:43:55 +08003218 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003219
Yan Zheng17d217f2008-12-12 10:03:38 -05003220 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04003221 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
David Sterba91166212016-04-26 23:54:39 +02003222 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM);
Yan Zheng17d217f2008-12-12 10:03:38 -05003223 return 0;
3224 }
3225
Miao Xiefacc8a222013-07-25 19:22:34 +08003226 phy_offset >>= inode->i_sb->s_blocksize_bits;
Miao Xiedc380ae2014-09-12 18:43:55 +08003227 return __readpage_endio_check(inode, io_bio, phy_offset, page, offset,
3228 start, (size_t)(end - start + 1));
Chris Mason07157aa2007-08-30 08:50:51 -04003229}
Chris Masonb888db22007-08-27 16:49:44 -04003230
Nikolay Borisovc1c3fac2018-01-16 09:31:58 +02003231/*
3232 * btrfs_add_delayed_iput - perform a delayed iput on @inode
3233 *
3234 * @inode: The inode we want to perform iput on
3235 *
3236 * This function uses the generic vfs_inode::i_count to track whether we should
3237 * just decrement it (in case it's > 1) or if this is the last iput then link
3238 * the inode to the delayed iput machinery. Delayed iputs are processed at
3239 * transaction commit time/superblock commit/cleaner kthread.
3240 */
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003241void btrfs_add_delayed_iput(struct inode *inode)
3242{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003243 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
David Sterba8089fe62015-11-19 14:15:51 +01003244 struct btrfs_inode *binode = BTRFS_I(inode);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003245
3246 if (atomic_add_unless(&inode->i_count, -1, 1))
3247 return;
3248
Josef Bacik034f7842018-12-03 11:06:52 -05003249 atomic_inc(&fs_info->nr_delayed_iputs);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003250 spin_lock(&fs_info->delayed_iput_lock);
Nikolay Borisovc1c3fac2018-01-16 09:31:58 +02003251 ASSERT(list_empty(&binode->delayed_iput));
3252 list_add_tail(&binode->delayed_iput, &fs_info->delayed_iputs);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003253 spin_unlock(&fs_info->delayed_iput_lock);
Josef Bacikfd340d02019-01-11 10:21:02 -05003254 if (!test_bit(BTRFS_FS_CLEANER_RUNNING, &fs_info->flags))
3255 wake_up_process(fs_info->cleaner_kthread);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003256}
3257
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003258void btrfs_run_delayed_iputs(struct btrfs_fs_info *fs_info)
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003259{
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003260
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003261 spin_lock(&fs_info->delayed_iput_lock);
David Sterba8089fe62015-11-19 14:15:51 +01003262 while (!list_empty(&fs_info->delayed_iputs)) {
3263 struct btrfs_inode *inode;
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003264
David Sterba8089fe62015-11-19 14:15:51 +01003265 inode = list_first_entry(&fs_info->delayed_iputs,
3266 struct btrfs_inode, delayed_iput);
Nikolay Borisovc1c3fac2018-01-16 09:31:58 +02003267 list_del_init(&inode->delayed_iput);
David Sterba8089fe62015-11-19 14:15:51 +01003268 spin_unlock(&fs_info->delayed_iput_lock);
3269 iput(&inode->vfs_inode);
Josef Bacik034f7842018-12-03 11:06:52 -05003270 if (atomic_dec_and_test(&fs_info->nr_delayed_iputs))
3271 wake_up(&fs_info->delayed_iputs_wait);
David Sterba8089fe62015-11-19 14:15:51 +01003272 spin_lock(&fs_info->delayed_iput_lock);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003273 }
David Sterba8089fe62015-11-19 14:15:51 +01003274 spin_unlock(&fs_info->delayed_iput_lock);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003275}
3276
Josef Bacik034f7842018-12-03 11:06:52 -05003277/**
3278 * btrfs_wait_on_delayed_iputs - wait on the delayed iputs to be done running
3279 * @fs_info - the fs_info for this fs
3280 * @return - EINTR if we were killed, 0 if nothing's pending
3281 *
3282 * This will wait on any delayed iputs that are currently running with KILLABLE
3283 * set. Once they are all done running we will return, unless we are killed in
3284 * which case we return EINTR. This helps in user operations like fallocate etc
3285 * that might get blocked on the iputs.
3286 */
3287int btrfs_wait_on_delayed_iputs(struct btrfs_fs_info *fs_info)
3288{
3289 int ret = wait_event_killable(fs_info->delayed_iputs_wait,
3290 atomic_read(&fs_info->nr_delayed_iputs) == 0);
3291 if (ret)
3292 return -EINTR;
3293 return 0;
3294}
3295
Yan, Zhengd68fc572010-05-16 10:49:58 -04003296/*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003297 * This creates an orphan entry for the given inode in case something goes wrong
3298 * in the middle of an unlink.
Josef Bacik7b128762008-07-24 12:17:14 -04003299 */
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003300int btrfs_orphan_add(struct btrfs_trans_handle *trans,
Omar Sandoval27919062018-05-11 13:13:37 -07003301 struct btrfs_inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003302{
Yan, Zhengd68fc572010-05-16 10:49:58 -04003303 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003304
Omar Sandoval27919062018-05-11 13:13:37 -07003305 ret = btrfs_insert_orphan_item(trans, inode->root, btrfs_ino(inode));
3306 if (ret && ret != -EEXIST) {
3307 btrfs_abort_transaction(trans, ret);
3308 return ret;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003309 }
3310
Yan, Zhengd68fc572010-05-16 10:49:58 -04003311 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003312}
3313
3314/*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003315 * We have done the delete so we can go ahead and remove the orphan item for
3316 * this particular inode.
Josef Bacik7b128762008-07-24 12:17:14 -04003317 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00003318static int btrfs_orphan_del(struct btrfs_trans_handle *trans,
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003319 struct btrfs_inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003320{
Omar Sandoval27919062018-05-11 13:13:37 -07003321 return btrfs_del_orphan_item(trans, inode->root, btrfs_ino(inode));
Josef Bacik7b128762008-07-24 12:17:14 -04003322}
3323
3324/*
3325 * this cleans up any orphans that may be left on the list from the last use
3326 * of this root.
3327 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003328int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04003329{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003330 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik7b128762008-07-24 12:17:14 -04003331 struct btrfs_path *path;
3332 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04003333 struct btrfs_key key, found_key;
3334 struct btrfs_trans_handle *trans;
3335 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003336 u64 last_objectid = 0;
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003337 int ret = 0, nr_unlink = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003338
Yan, Zhengd68fc572010-05-16 10:49:58 -04003339 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003340 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00003341
3342 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003343 if (!path) {
3344 ret = -ENOMEM;
3345 goto out;
3346 }
David Sterbae4058b52015-11-27 16:31:35 +01003347 path->reada = READA_BACK;
Josef Bacik7b128762008-07-24 12:17:14 -04003348
3349 key.objectid = BTRFS_ORPHAN_OBJECTID;
David Sterba962a2982014-06-04 18:41:45 +02003350 key.type = BTRFS_ORPHAN_ITEM_KEY;
Josef Bacik7b128762008-07-24 12:17:14 -04003351 key.offset = (u64)-1;
3352
Josef Bacik7b128762008-07-24 12:17:14 -04003353 while (1) {
3354 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003355 if (ret < 0)
3356 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003357
3358 /*
3359 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003360 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04003361 * find the key and see if we have stuff that matches
3362 */
3363 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003364 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003365 if (path->slots[0] == 0)
3366 break;
3367 path->slots[0]--;
3368 }
3369
3370 /* pull out the item */
3371 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04003372 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3373
3374 /* make sure the item matches what we want */
3375 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
3376 break;
David Sterba962a2982014-06-04 18:41:45 +02003377 if (found_key.type != BTRFS_ORPHAN_ITEM_KEY)
Josef Bacik7b128762008-07-24 12:17:14 -04003378 break;
3379
3380 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02003381 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04003382
3383 /*
3384 * this is where we are basically btrfs_lookup, without the
3385 * crossing root thing. we store the inode number in the
3386 * offset of the orphan item.
3387 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003388
3389 if (found_key.offset == last_objectid) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003390 btrfs_err(fs_info,
3391 "Error removing orphan entry, stopping orphan cleanup");
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003392 ret = -EINVAL;
3393 goto out;
3394 }
3395
3396 last_objectid = found_key.offset;
3397
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003398 found_key.objectid = found_key.offset;
3399 found_key.type = BTRFS_INODE_ITEM_KEY;
3400 found_key.offset = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003401 inode = btrfs_iget(fs_info->sb, &found_key, root, NULL);
Rusty Russell8c6ffba2013-07-15 11:20:32 +09303402 ret = PTR_ERR_OR_ZERO(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003403 if (ret && ret != -ENOENT)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003404 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003405
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003406 if (ret == -ENOENT && root == fs_info->tree_root) {
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003407 struct btrfs_root *dead_root;
3408 struct btrfs_fs_info *fs_info = root->fs_info;
3409 int is_dead_root = 0;
3410
3411 /*
3412 * this is an orphan in the tree root. Currently these
3413 * could come from 2 sources:
3414 * a) a snapshot deletion in progress
3415 * b) a free space cache inode
3416 * We need to distinguish those two, as the snapshot
3417 * orphan must not get deleted.
3418 * find_dead_roots already ran before us, so if this
3419 * is a snapshot deletion, we should find the root
3420 * in the dead_roots list
3421 */
3422 spin_lock(&fs_info->trans_lock);
3423 list_for_each_entry(dead_root, &fs_info->dead_roots,
3424 root_list) {
3425 if (dead_root->root_key.objectid ==
3426 found_key.objectid) {
3427 is_dead_root = 1;
3428 break;
3429 }
3430 }
3431 spin_unlock(&fs_info->trans_lock);
3432 if (is_dead_root) {
3433 /* prevent this orphan from being found again */
3434 key.offset = found_key.objectid - 1;
3435 continue;
3436 }
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003437
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003438 }
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003439
Josef Bacika8c9e572011-09-21 16:55:59 -04003440 /*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003441 * If we have an inode with links, there are a couple of
3442 * possibilities. Old kernels (before v3.12) used to create an
3443 * orphan item for truncate indicating that there were possibly
3444 * extent items past i_size that needed to be deleted. In v3.12,
3445 * truncate was changed to update i_size in sync with the extent
3446 * items, but the (useless) orphan item was still created. Since
3447 * v4.18, we don't create the orphan item for truncate at all.
3448 *
3449 * So, this item could mean that we need to do a truncate, but
3450 * only if this filesystem was last used on a pre-v3.12 kernel
3451 * and was not cleanly unmounted. The odds of that are quite
3452 * slim, and it's a pain to do the truncate now, so just delete
3453 * the orphan item.
3454 *
3455 * It's also possible that this orphan item was supposed to be
3456 * deleted but wasn't. The inode number may have been reused,
3457 * but either way, we can delete the orphan item.
Josef Bacika8c9e572011-09-21 16:55:59 -04003458 */
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003459 if (ret == -ENOENT || inode->i_nlink) {
3460 if (!ret)
3461 iput(inode);
Josef Bacika8c9e572011-09-21 16:55:59 -04003462 trans = btrfs_start_transaction(root, 1);
3463 if (IS_ERR(trans)) {
3464 ret = PTR_ERR(trans);
3465 goto out;
3466 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003467 btrfs_debug(fs_info, "auto deleting %Lu",
3468 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003469 ret = btrfs_del_orphan_item(trans, root,
3470 found_key.objectid);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003471 btrfs_end_transaction(trans);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003472 if (ret)
3473 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003474 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003475 }
Josef Bacik7b128762008-07-24 12:17:14 -04003476
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003477 nr_unlink++;
Josef Bacik7b128762008-07-24 12:17:14 -04003478
3479 /* this will do delete_inode and everything for us */
3480 iput(inode);
3481 }
Miao Xie3254c872011-11-10 20:45:05 -05003482 /* release the path since we're done with it */
3483 btrfs_release_path(path);
3484
Yan, Zhengd68fc572010-05-16 10:49:58 -04003485 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
3486
Omar Sandovala575cee2018-05-11 13:13:38 -07003487 if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003488 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003489 if (!IS_ERR(trans))
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003490 btrfs_end_transaction(trans);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003491 }
Josef Bacik7b128762008-07-24 12:17:14 -04003492
3493 if (nr_unlink)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003494 btrfs_debug(fs_info, "unlinked %d orphans", nr_unlink);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003495
3496out:
3497 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003498 btrfs_err(fs_info, "could not do orphan cleanup %d", ret);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003499 btrfs_free_path(path);
3500 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003501}
3502
Chris Masond352ac62008-09-29 15:18:18 -04003503/*
Chris Mason46a53cc2009-04-27 11:47:50 -04003504 * very simple check to peek ahead in the leaf looking for xattrs. If we
3505 * don't find any xattrs, we know there can't be any acls.
3506 *
3507 * slot is the slot the inode is in, objectid is the objectid of the inode
3508 */
3509static noinline int acls_after_inode_item(struct extent_buffer *leaf,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003510 int slot, u64 objectid,
3511 int *first_xattr_slot)
Chris Mason46a53cc2009-04-27 11:47:50 -04003512{
3513 u32 nritems = btrfs_header_nritems(leaf);
3514 struct btrfs_key found_key;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003515 static u64 xattr_access = 0;
3516 static u64 xattr_default = 0;
Chris Mason46a53cc2009-04-27 11:47:50 -04003517 int scanned = 0;
3518
Josef Bacikf23b5a52013-06-19 10:16:26 -04003519 if (!xattr_access) {
Andreas Gruenbacher97d79292015-12-02 14:44:35 +01003520 xattr_access = btrfs_name_hash(XATTR_NAME_POSIX_ACL_ACCESS,
3521 strlen(XATTR_NAME_POSIX_ACL_ACCESS));
3522 xattr_default = btrfs_name_hash(XATTR_NAME_POSIX_ACL_DEFAULT,
3523 strlen(XATTR_NAME_POSIX_ACL_DEFAULT));
Josef Bacikf23b5a52013-06-19 10:16:26 -04003524 }
3525
Chris Mason46a53cc2009-04-27 11:47:50 -04003526 slot++;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003527 *first_xattr_slot = -1;
Chris Mason46a53cc2009-04-27 11:47:50 -04003528 while (slot < nritems) {
3529 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3530
3531 /* we found a different objectid, there must not be acls */
3532 if (found_key.objectid != objectid)
3533 return 0;
3534
3535 /* we found an xattr, assume we've got an acl */
Josef Bacikf23b5a52013-06-19 10:16:26 -04003536 if (found_key.type == BTRFS_XATTR_ITEM_KEY) {
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003537 if (*first_xattr_slot == -1)
3538 *first_xattr_slot = slot;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003539 if (found_key.offset == xattr_access ||
3540 found_key.offset == xattr_default)
3541 return 1;
3542 }
Chris Mason46a53cc2009-04-27 11:47:50 -04003543
3544 /*
3545 * we found a key greater than an xattr key, there can't
3546 * be any acls later on
3547 */
3548 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
3549 return 0;
3550
3551 slot++;
3552 scanned++;
3553
3554 /*
3555 * it goes inode, inode backrefs, xattrs, extents,
3556 * so if there are a ton of hard links to an inode there can
3557 * be a lot of backrefs. Don't waste time searching too hard,
3558 * this is just an optimization
3559 */
3560 if (scanned >= 8)
3561 break;
3562 }
3563 /* we hit the end of the leaf before we found an xattr or
3564 * something larger than an xattr. We have to assume the inode
3565 * has acls
3566 */
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003567 if (*first_xattr_slot == -1)
3568 *first_xattr_slot = slot;
Chris Mason46a53cc2009-04-27 11:47:50 -04003569 return 1;
3570}
3571
3572/*
Chris Masond352ac62008-09-29 15:18:18 -04003573 * read an inode from the btree into the in-memory inode
3574 */
Filipe Manana4222ea72018-10-24 10:13:03 +01003575static int btrfs_read_locked_inode(struct inode *inode,
3576 struct btrfs_path *in_path)
Chris Mason39279cc2007-06-12 06:35:45 -04003577{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003578 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Filipe Manana4222ea72018-10-24 10:13:03 +01003579 struct btrfs_path *path = in_path;
Chris Mason5f39d392007-10-15 16:14:19 -04003580 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003581 struct btrfs_inode_item *inode_item;
3582 struct btrfs_root *root = BTRFS_I(inode)->root;
3583 struct btrfs_key location;
Miao Xie67de1172013-12-26 13:07:06 +08003584 unsigned long ptr;
Chris Mason46a53cc2009-04-27 11:47:50 -04003585 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04003586 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04003587 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003588 bool filled = false;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003589 int first_xattr_slot;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003590
3591 ret = btrfs_fill_inode(inode, &rdev);
3592 if (!ret)
3593 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04003594
Filipe Manana4222ea72018-10-24 10:13:03 +01003595 if (!path) {
3596 path = btrfs_alloc_path();
3597 if (!path)
3598 return -ENOMEM;
3599 }
Mark Fasheh1748f842011-07-12 11:25:31 -07003600
Chris Mason39279cc2007-06-12 06:35:45 -04003601 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05003602
Chris Mason39279cc2007-06-12 06:35:45 -04003603 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Filipe Manana67710892016-06-06 11:51:25 +01003604 if (ret) {
Filipe Manana4222ea72018-10-24 10:13:03 +01003605 if (path != in_path)
3606 btrfs_free_path(path);
Al Virof5b3a412018-07-29 23:04:51 +01003607 return ret;
Filipe Manana67710892016-06-06 11:51:25 +01003608 }
Chris Mason39279cc2007-06-12 06:35:45 -04003609
Chris Mason5f39d392007-10-15 16:14:19 -04003610 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00003611
3612 if (filled)
Miao Xie67de1172013-12-26 13:07:06 +08003613 goto cache_index;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003614
Chris Mason5f39d392007-10-15 16:14:19 -04003615 inode_item = btrfs_item_ptr(leaf, path->slots[0],
3616 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003617 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02003618 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08003619 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
3620 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02003621 btrfs_i_size_write(BTRFS_I(inode), btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04003622
David Sterbaa937b972014-12-12 17:39:12 +01003623 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->atime);
3624 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->atime);
Chris Mason5f39d392007-10-15 16:14:19 -04003625
David Sterbaa937b972014-12-12 17:39:12 +01003626 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->mtime);
3627 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->mtime);
Chris Mason5f39d392007-10-15 16:14:19 -04003628
David Sterbaa937b972014-12-12 17:39:12 +01003629 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->ctime);
3630 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->ctime);
Chris Mason5f39d392007-10-15 16:14:19 -04003631
chandan r9cc97d62012-07-04 12:48:07 +05303632 BTRFS_I(inode)->i_otime.tv_sec =
3633 btrfs_timespec_sec(leaf, &inode_item->otime);
3634 BTRFS_I(inode)->i_otime.tv_nsec =
3635 btrfs_timespec_nsec(leaf, &inode_item->otime);
Chris Mason5f39d392007-10-15 16:14:19 -04003636
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003637 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003638 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003639 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
3640
Jeff Laytonc7f88c42017-12-11 06:35:12 -05003641 inode_set_iversion_queried(inode,
3642 btrfs_inode_sequence(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003643 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04003644 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003645 rdev = btrfs_inode_rdev(leaf, inode_item);
3646
Josef Bacikaec74772008-07-24 12:12:38 -04003647 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05003648 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Miao Xie67de1172013-12-26 13:07:06 +08003649
3650cache_index:
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003651 /*
3652 * If we were modified in the current generation and evicted from memory
3653 * and then re-read we need to do a full sync since we don't have any
3654 * idea about which extents were modified before we were evicted from
3655 * cache.
3656 *
3657 * This is required for both inode re-read from disk and delayed inode
3658 * in delayed_nodes_tree.
3659 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003660 if (BTRFS_I(inode)->last_trans == fs_info->generation)
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003661 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3662 &BTRFS_I(inode)->runtime_flags);
3663
Filipe Mananabde6c242015-07-24 00:00:19 +01003664 /*
3665 * We don't persist the id of the transaction where an unlink operation
3666 * against the inode was last made. So here we assume the inode might
3667 * have been evicted, and therefore the exact value of last_unlink_trans
3668 * lost, and set it to last_trans to avoid metadata inconsistencies
3669 * between the inode and its parent if the inode is fsync'ed and the log
3670 * replayed. For example, in the scenario:
3671 *
3672 * touch mydir/foo
3673 * ln mydir/foo mydir/bar
3674 * sync
3675 * unlink mydir/bar
3676 * echo 2 > /proc/sys/vm/drop_caches # evicts inode
3677 * xfs_io -c fsync mydir/foo
3678 * <power failure>
3679 * mount fs, triggers fsync log replay
3680 *
3681 * We must make sure that when we fsync our inode foo we also log its
3682 * parent inode, otherwise after log replay the parent still has the
3683 * dentry with the "bar" name but our inode foo has a link count of 1
3684 * and doesn't have an inode ref with the name "bar" anymore.
3685 *
3686 * Setting last_unlink_trans to last_trans is a pessimistic approach,
Nicholas D Steeves01327612016-05-19 21:18:45 -04003687 * but it guarantees correctness at the expense of occasional full
Filipe Mananabde6c242015-07-24 00:00:19 +01003688 * transaction commits on fsync if our inode is a directory, or if our
3689 * inode is not a directory, logging its parent unnecessarily.
3690 */
3691 BTRFS_I(inode)->last_unlink_trans = BTRFS_I(inode)->last_trans;
Filipe Manana41bd6062018-11-28 14:54:28 +00003692 /*
3693 * Similar reasoning for last_link_trans, needs to be set otherwise
3694 * for a case like the following:
3695 *
3696 * mkdir A
3697 * touch foo
3698 * ln foo A/bar
3699 * echo 2 > /proc/sys/vm/drop_caches
3700 * fsync foo
3701 * <power failure>
3702 *
3703 * Would result in link bar and directory A not existing after the power
3704 * failure.
3705 */
3706 BTRFS_I(inode)->last_link_trans = BTRFS_I(inode)->last_trans;
Filipe Mananabde6c242015-07-24 00:00:19 +01003707
Miao Xie67de1172013-12-26 13:07:06 +08003708 path->slots[0]++;
3709 if (inode->i_nlink != 1 ||
3710 path->slots[0] >= btrfs_header_nritems(leaf))
3711 goto cache_acl;
3712
3713 btrfs_item_key_to_cpu(leaf, &location, path->slots[0]);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003714 if (location.objectid != btrfs_ino(BTRFS_I(inode)))
Miao Xie67de1172013-12-26 13:07:06 +08003715 goto cache_acl;
3716
3717 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3718 if (location.type == BTRFS_INODE_REF_KEY) {
3719 struct btrfs_inode_ref *ref;
3720
3721 ref = (struct btrfs_inode_ref *)ptr;
3722 BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref);
3723 } else if (location.type == BTRFS_INODE_EXTREF_KEY) {
3724 struct btrfs_inode_extref *extref;
3725
3726 extref = (struct btrfs_inode_extref *)ptr;
3727 BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf,
3728 extref);
3729 }
Miao Xie2f7e33d2011-06-23 07:27:13 +00003730cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04003731 /*
3732 * try to precache a NULL acl entry for files that don't have
3733 * any xattrs or acls
3734 */
Li Zefan33345d012011-04-20 10:31:50 +08003735 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
David Sterbaf85b7372017-01-20 14:54:07 +01003736 btrfs_ino(BTRFS_I(inode)), &first_xattr_slot);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003737 if (first_xattr_slot != -1) {
3738 path->slots[0] = first_xattr_slot;
3739 ret = btrfs_load_inode_props(inode, path);
3740 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003741 btrfs_err(fs_info,
David Sterba351fd352014-05-15 16:48:20 +02003742 "error loading props for ino %llu (root %llu): %d",
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003743 btrfs_ino(BTRFS_I(inode)),
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003744 root->root_key.objectid, ret);
3745 }
Filipe Manana4222ea72018-10-24 10:13:03 +01003746 if (path != in_path)
3747 btrfs_free_path(path);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003748
Al Viro72c04902009-06-24 16:58:48 -04003749 if (!maybe_acls)
3750 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04003751
Chris Mason39279cc2007-06-12 06:35:45 -04003752 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04003753 case S_IFREG:
3754 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masond1310b22008-01-24 16:13:08 -05003755 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04003756 inode->i_fop = &btrfs_file_operations;
3757 inode->i_op = &btrfs_file_inode_operations;
3758 break;
3759 case S_IFDIR:
3760 inode->i_fop = &btrfs_dir_file_operations;
Omar Sandoval67ade052017-01-25 17:06:38 -08003761 inode->i_op = &btrfs_dir_inode_operations;
Chris Mason39279cc2007-06-12 06:35:45 -04003762 break;
3763 case S_IFLNK:
3764 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -05003765 inode_nohighmem(inode);
Omar Sandoval4779cc02018-09-24 15:16:55 -07003766 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason39279cc2007-06-12 06:35:45 -04003767 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04003768 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05003769 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04003770 init_special_inode(inode, inode->i_mode, rdev);
3771 break;
Chris Mason39279cc2007-06-12 06:35:45 -04003772 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003773
David Sterba7b6a2212018-03-26 18:40:21 +02003774 btrfs_sync_inode_flags_to_i_flags(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003775 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003776}
3777
Chris Masond352ac62008-09-29 15:18:18 -04003778/*
3779 * given a leaf and an inode, copy the inode fields into the leaf
3780 */
Chris Masone02119d2008-09-05 16:13:11 -04003781static void fill_inode_item(struct btrfs_trans_handle *trans,
3782 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04003783 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04003784 struct inode *inode)
3785{
Liu Bo51fab692012-12-27 09:01:21 +00003786 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003787
Liu Bo51fab692012-12-27 09:01:21 +00003788 btrfs_init_map_token(&token);
Chris Mason5f39d392007-10-15 16:14:19 -04003789
Liu Bo51fab692012-12-27 09:01:21 +00003790 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
3791 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
3792 btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size,
3793 &token);
3794 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3795 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003796
David Sterbaa937b972014-12-12 17:39:12 +01003797 btrfs_set_token_timespec_sec(leaf, &item->atime,
Liu Bo51fab692012-12-27 09:01:21 +00003798 inode->i_atime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003799 btrfs_set_token_timespec_nsec(leaf, &item->atime,
Liu Bo51fab692012-12-27 09:01:21 +00003800 inode->i_atime.tv_nsec, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003801
David Sterbaa937b972014-12-12 17:39:12 +01003802 btrfs_set_token_timespec_sec(leaf, &item->mtime,
Liu Bo51fab692012-12-27 09:01:21 +00003803 inode->i_mtime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003804 btrfs_set_token_timespec_nsec(leaf, &item->mtime,
Liu Bo51fab692012-12-27 09:01:21 +00003805 inode->i_mtime.tv_nsec, &token);
3806
David Sterbaa937b972014-12-12 17:39:12 +01003807 btrfs_set_token_timespec_sec(leaf, &item->ctime,
Liu Bo51fab692012-12-27 09:01:21 +00003808 inode->i_ctime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003809 btrfs_set_token_timespec_nsec(leaf, &item->ctime,
Liu Bo51fab692012-12-27 09:01:21 +00003810 inode->i_ctime.tv_nsec, &token);
3811
chandan r9cc97d62012-07-04 12:48:07 +05303812 btrfs_set_token_timespec_sec(leaf, &item->otime,
3813 BTRFS_I(inode)->i_otime.tv_sec, &token);
3814 btrfs_set_token_timespec_nsec(leaf, &item->otime,
3815 BTRFS_I(inode)->i_otime.tv_nsec, &token);
3816
Liu Bo51fab692012-12-27 09:01:21 +00003817 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3818 &token);
3819 btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation,
3820 &token);
Jeff Laytonc7f88c42017-12-11 06:35:12 -05003821 btrfs_set_token_inode_sequence(leaf, item, inode_peek_iversion(inode),
3822 &token);
Liu Bo51fab692012-12-27 09:01:21 +00003823 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
3824 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3825 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3826 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
Chris Mason39279cc2007-06-12 06:35:45 -04003827}
3828
Chris Masond352ac62008-09-29 15:18:18 -04003829/*
3830 * copy everything in the in-memory inode into the btree.
3831 */
Chris Mason21151332011-11-10 20:39:08 -05003832static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Chris Masond3977122009-01-05 21:25:51 -05003833 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003834{
3835 struct btrfs_inode_item *inode_item;
3836 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003837 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003838 int ret;
3839
3840 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08003841 if (!path)
3842 return -ENOMEM;
3843
Chris Masonb9473432009-03-13 11:00:37 -04003844 path->leave_spinning = 1;
Miao Xie16cdcec2011-04-22 18:12:22 +08003845 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
3846 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003847 if (ret) {
3848 if (ret > 0)
3849 ret = -ENOENT;
3850 goto failed;
3851 }
3852
Chris Mason5f39d392007-10-15 16:14:19 -04003853 leaf = path->nodes[0];
3854 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08003855 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04003856
Chris Masone02119d2008-09-05 16:13:11 -04003857 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04003858 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04003859 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003860 ret = 0;
3861failed:
Chris Mason39279cc2007-06-12 06:35:45 -04003862 btrfs_free_path(path);
3863 return ret;
3864}
3865
Chris Masond352ac62008-09-29 15:18:18 -04003866/*
Chris Mason21151332011-11-10 20:39:08 -05003867 * copy everything in the in-memory inode into the btree.
3868 */
3869noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
3870 struct btrfs_root *root, struct inode *inode)
3871{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003872 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason21151332011-11-10 20:39:08 -05003873 int ret;
3874
3875 /*
3876 * If the inode is a free space inode, we can deadlock during commit
3877 * if we put it into the delayed code.
3878 *
3879 * The data relocation inode should also be directly updated
3880 * without delay
3881 */
Nikolay Borisov70ddc552017-02-20 13:50:35 +02003882 if (!btrfs_is_free_space_inode(BTRFS_I(inode))
Josef Bacik1d52c782014-09-18 11:30:44 -04003883 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003884 && !test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) {
Alexander Block8ea05e32012-07-25 17:35:53 +02003885 btrfs_update_root_times(trans, root);
3886
Chris Mason21151332011-11-10 20:39:08 -05003887 ret = btrfs_delayed_update_inode(trans, root, inode);
3888 if (!ret)
3889 btrfs_set_inode_last_trans(trans, inode);
3890 return ret;
3891 }
3892
3893 return btrfs_update_inode_item(trans, root, inode);
3894}
3895
Josef Bacikbe6aef62012-10-22 15:43:12 -04003896noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
3897 struct btrfs_root *root,
3898 struct inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05003899{
3900 int ret;
3901
3902 ret = btrfs_update_inode(trans, root, inode);
3903 if (ret == -ENOSPC)
3904 return btrfs_update_inode_item(trans, root, inode);
3905 return ret;
3906}
3907
3908/*
Chris Masond352ac62008-09-29 15:18:18 -04003909 * unlink helper that gets used here in inode.c and in the tree logging
3910 * recovery code. It remove a link in a directory with a given name, and
3911 * also drops the back refs in the inode to the directory
3912 */
Al Viro92986792011-03-04 17:14:37 +00003913static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3914 struct btrfs_root *root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003915 struct btrfs_inode *dir,
3916 struct btrfs_inode *inode,
Al Viro92986792011-03-04 17:14:37 +00003917 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04003918{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003919 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04003920 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04003921 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003922 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003923 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04003924 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04003925 u64 index;
Li Zefan33345d012011-04-20 10:31:50 +08003926 u64 ino = btrfs_ino(inode);
3927 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04003928
3929 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04003930 if (!path) {
3931 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00003932 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04003933 }
3934
Chris Masonb9473432009-03-13 11:00:37 -04003935 path->leave_spinning = 1;
Li Zefan33345d012011-04-20 10:31:50 +08003936 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04003937 name, name_len, -1);
Liu Bo3cf50682018-09-12 06:06:26 +08003938 if (IS_ERR_OR_NULL(di)) {
3939 ret = di ? PTR_ERR(di) : -ENOENT;
Chris Mason39279cc2007-06-12 06:35:45 -04003940 goto err;
3941 }
Chris Mason5f39d392007-10-15 16:14:19 -04003942 leaf = path->nodes[0];
3943 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04003944 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04003945 if (ret)
3946 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02003947 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003948
Miao Xie67de1172013-12-26 13:07:06 +08003949 /*
3950 * If we don't have dir index, we have to get it by looking up
3951 * the inode ref, since we get the inode ref, remove it directly,
3952 * it is unnecessary to do delayed deletion.
3953 *
3954 * But if we have dir index, needn't search inode ref to get it.
3955 * Since the inode ref is close to the inode item, it is better
3956 * that we delay to delete it, and just do this deletion when
3957 * we update the inode item.
3958 */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003959 if (inode->dir_index) {
Miao Xie67de1172013-12-26 13:07:06 +08003960 ret = btrfs_delayed_delete_inode_ref(inode);
3961 if (!ret) {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003962 index = inode->dir_index;
Miao Xie67de1172013-12-26 13:07:06 +08003963 goto skip_backref;
3964 }
3965 }
3966
Li Zefan33345d012011-04-20 10:31:50 +08003967 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
3968 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04003969 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003970 btrfs_info(fs_info,
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003971 "failed to delete reference to %.*s, inode %llu parent %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003972 name_len, name, ino, dir_ino);
Jeff Mahoney66642832016-06-10 18:19:25 -04003973 btrfs_abort_transaction(trans, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04003974 goto err;
3975 }
Miao Xie67de1172013-12-26 13:07:06 +08003976skip_backref:
Lu Fengqi9add2942018-08-01 11:32:26 +08003977 ret = btrfs_delete_delayed_dir_index(trans, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003978 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003979 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003980 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003981 }
Chris Mason39279cc2007-06-12 06:35:45 -04003982
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003983 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len, inode,
3984 dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003985 if (ret != 0 && ret != -ENOENT) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003986 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003987 goto err;
3988 }
Chris Masone02119d2008-09-05 16:13:11 -04003989
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003990 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len, dir,
3991 index);
Chris Mason6418c962010-10-30 07:34:24 -04003992 if (ret == -ENOENT)
3993 ret = 0;
Zach Brownd4e39912013-04-02 21:02:16 +00003994 else if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04003995 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003996err:
3997 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003998 if (ret)
3999 goto out;
4000
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004001 btrfs_i_size_write(dir, dir->vfs_inode.i_size - name_len * 2);
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004002 inode_inc_iversion(&inode->vfs_inode);
4003 inode_inc_iversion(&dir->vfs_inode);
4004 inode->vfs_inode.i_ctime = dir->vfs_inode.i_mtime =
4005 dir->vfs_inode.i_ctime = current_time(&inode->vfs_inode);
4006 ret = btrfs_update_inode(trans, root, &dir->vfs_inode);
Chris Masone02119d2008-09-05 16:13:11 -04004007out:
Chris Mason39279cc2007-06-12 06:35:45 -04004008 return ret;
4009}
4010
Al Viro92986792011-03-04 17:14:37 +00004011int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
4012 struct btrfs_root *root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004013 struct btrfs_inode *dir, struct btrfs_inode *inode,
Al Viro92986792011-03-04 17:14:37 +00004014 const char *name, int name_len)
4015{
4016 int ret;
4017 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
4018 if (!ret) {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004019 drop_nlink(&inode->vfs_inode);
4020 ret = btrfs_update_inode(trans, root, &inode->vfs_inode);
Al Viro92986792011-03-04 17:14:37 +00004021 }
4022 return ret;
4023}
Yan, Zhenga22285a2010-05-16 10:48:46 -04004024
4025/*
4026 * helper to start transaction for unlink and rmdir.
4027 *
Josef Bacikd52be812013-05-29 14:54:47 -04004028 * unlink and rmdir are special in btrfs, they do not always free space, so
4029 * if we cannot make our reservations the normal way try and see if there is
4030 * plenty of slack room in the global reserve to migrate, otherwise we cannot
4031 * allow the unlink to occur.
Yan, Zhenga22285a2010-05-16 10:48:46 -04004032 */
Josef Bacikd52be812013-05-29 14:54:47 -04004033static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir)
Yan, Zhenga22285a2010-05-16 10:48:46 -04004034{
Yan, Zhenga22285a2010-05-16 10:48:46 -04004035 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004036
Josef Bacike70bea52011-10-11 14:18:24 -04004037 /*
4038 * 1 for the possible orphan item
4039 * 1 for the dir item
4040 * 1 for the dir index
4041 * 1 for the inode ref
Josef Bacike70bea52011-10-11 14:18:24 -04004042 * 1 for the inode
4043 */
Filipe Manana8eab77f2015-11-13 23:57:16 +00004044 return btrfs_start_transaction_fallback_global_rsv(root, 5, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004045}
4046
Chris Mason39279cc2007-06-12 06:35:45 -04004047static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
4048{
Yan, Zhenga22285a2010-05-16 10:48:46 -04004049 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004050 struct btrfs_trans_handle *trans;
David Howells2b0143b2015-03-17 22:25:59 +00004051 struct inode *inode = d_inode(dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04004052 int ret;
4053
Josef Bacikd52be812013-05-29 14:54:47 -04004054 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004055 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004056 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04004057
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004058 btrfs_record_unlink_dir(trans, BTRFS_I(dir), BTRFS_I(d_inode(dentry)),
4059 0);
Chris Mason12fcfd22009-03-24 10:24:20 -04004060
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004061 ret = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
4062 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
4063 dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00004064 if (ret)
4065 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004066
Yan, Zhenga22285a2010-05-16 10:48:46 -04004067 if (inode->i_nlink == 0) {
Nikolay Borisov73f2e542017-02-20 13:50:59 +02004068 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Tsutomu Itohb5324022011-07-19 07:27:20 +00004069 if (ret)
4070 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004071 }
Josef Bacik7b128762008-07-24 12:17:14 -04004072
Tsutomu Itohb5324022011-07-19 07:27:20 +00004073out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004074 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004075 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04004076 return ret;
4077}
4078
Misono Tomohirof60a2362018-04-18 11:34:52 +09004079static int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
Lu Fengqi401b3b12018-08-01 11:32:30 +08004080 struct inode *dir, u64 objectid,
4081 const char *name, int name_len)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004082{
Lu Fengqi401b3b12018-08-01 11:32:30 +08004083 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004084 struct btrfs_path *path;
4085 struct extent_buffer *leaf;
4086 struct btrfs_dir_item *di;
4087 struct btrfs_key key;
4088 u64 index;
4089 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004090 u64 dir_ino = btrfs_ino(BTRFS_I(dir));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004091
4092 path = btrfs_alloc_path();
4093 if (!path)
4094 return -ENOMEM;
4095
Li Zefan33345d012011-04-20 10:31:50 +08004096 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004097 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004098 if (IS_ERR_OR_NULL(di)) {
Liu Bo3cf50682018-09-12 06:06:26 +08004099 ret = di ? PTR_ERR(di) : -ENOENT;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004100 goto out;
4101 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004102
4103 leaf = path->nodes[0];
4104 btrfs_dir_item_key_to_cpu(leaf, di, &key);
4105 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
4106 ret = btrfs_delete_one_dir_name(trans, root, path, di);
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 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004111 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004112
Lu Fengqi3ee1c552018-08-01 11:32:28 +08004113 ret = btrfs_del_root_ref(trans, objectid, root->root_key.objectid,
4114 dir_ino, &index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004115 if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004116 if (ret != -ENOENT) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004117 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004118 goto out;
4119 }
Li Zefan33345d012011-04-20 10:31:50 +08004120 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004121 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004122 if (IS_ERR_OR_NULL(di)) {
4123 if (!di)
4124 ret = -ENOENT;
4125 else
4126 ret = PTR_ERR(di);
Jeff Mahoney66642832016-06-10 18:19:25 -04004127 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004128 goto out;
4129 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004130
4131 leaf = path->nodes[0];
4132 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004133 index = key.offset;
4134 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004135 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004136
Lu Fengqi9add2942018-08-01 11:32:26 +08004137 ret = btrfs_delete_delayed_dir_index(trans, BTRFS_I(dir), index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004138 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004139 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004140 goto out;
4141 }
Miao Xie16cdcec2011-04-22 18:12:22 +08004142
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004143 btrfs_i_size_write(BTRFS_I(dir), dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004144 inode_inc_iversion(dir);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07004145 dir->i_mtime = dir->i_ctime = current_time(dir);
Josef Bacik5a24e842012-08-08 10:12:59 -06004146 ret = btrfs_update_inode_fallback(trans, root, dir);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004147 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004148 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004149out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04004150 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004151 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004152}
4153
Misono Tomohiroec42f162018-04-18 11:34:13 +09004154/*
4155 * Helper to check if the subvolume references other subvolumes or if it's
4156 * default.
4157 */
Misono Tomohirof60a2362018-04-18 11:34:52 +09004158static noinline int may_destroy_subvol(struct btrfs_root *root)
Misono Tomohiroec42f162018-04-18 11:34:13 +09004159{
4160 struct btrfs_fs_info *fs_info = root->fs_info;
4161 struct btrfs_path *path;
4162 struct btrfs_dir_item *di;
4163 struct btrfs_key key;
4164 u64 dir_id;
4165 int ret;
4166
4167 path = btrfs_alloc_path();
4168 if (!path)
4169 return -ENOMEM;
4170
4171 /* Make sure this root isn't set as the default subvol */
4172 dir_id = btrfs_super_root_dir(fs_info->super_copy);
4173 di = btrfs_lookup_dir_item(NULL, fs_info->tree_root, path,
4174 dir_id, "default", 7, 0);
4175 if (di && !IS_ERR(di)) {
4176 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key);
4177 if (key.objectid == root->root_key.objectid) {
4178 ret = -EPERM;
4179 btrfs_err(fs_info,
4180 "deleting default subvolume %llu is not allowed",
4181 key.objectid);
4182 goto out;
4183 }
4184 btrfs_release_path(path);
4185 }
4186
4187 key.objectid = root->root_key.objectid;
4188 key.type = BTRFS_ROOT_REF_KEY;
4189 key.offset = (u64)-1;
4190
4191 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
4192 if (ret < 0)
4193 goto out;
4194 BUG_ON(ret == 0);
4195
4196 ret = 0;
4197 if (path->slots[0] > 0) {
4198 path->slots[0]--;
4199 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
4200 if (key.objectid == root->root_key.objectid &&
4201 key.type == BTRFS_ROOT_REF_KEY)
4202 ret = -ENOTEMPTY;
4203 }
4204out:
4205 btrfs_free_path(path);
4206 return ret;
4207}
4208
Nikolay Borisov20a68002018-04-27 14:36:24 +03004209/* Delete all dentries for inodes belonging to the root */
4210static void btrfs_prune_dentries(struct btrfs_root *root)
4211{
4212 struct btrfs_fs_info *fs_info = root->fs_info;
4213 struct rb_node *node;
4214 struct rb_node *prev;
4215 struct btrfs_inode *entry;
4216 struct inode *inode;
4217 u64 objectid = 0;
4218
4219 if (!test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
4220 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
4221
4222 spin_lock(&root->inode_lock);
4223again:
4224 node = root->inode_tree.rb_node;
4225 prev = NULL;
4226 while (node) {
4227 prev = node;
4228 entry = rb_entry(node, struct btrfs_inode, rb_node);
4229
David Sterba37508512018-06-29 10:56:40 +02004230 if (objectid < btrfs_ino(entry))
Nikolay Borisov20a68002018-04-27 14:36:24 +03004231 node = node->rb_left;
David Sterba37508512018-06-29 10:56:40 +02004232 else if (objectid > btrfs_ino(entry))
Nikolay Borisov20a68002018-04-27 14:36:24 +03004233 node = node->rb_right;
4234 else
4235 break;
4236 }
4237 if (!node) {
4238 while (prev) {
4239 entry = rb_entry(prev, struct btrfs_inode, rb_node);
David Sterba37508512018-06-29 10:56:40 +02004240 if (objectid <= btrfs_ino(entry)) {
Nikolay Borisov20a68002018-04-27 14:36:24 +03004241 node = prev;
4242 break;
4243 }
4244 prev = rb_next(prev);
4245 }
4246 }
4247 while (node) {
4248 entry = rb_entry(node, struct btrfs_inode, rb_node);
David Sterba37508512018-06-29 10:56:40 +02004249 objectid = btrfs_ino(entry) + 1;
Nikolay Borisov20a68002018-04-27 14:36:24 +03004250 inode = igrab(&entry->vfs_inode);
4251 if (inode) {
4252 spin_unlock(&root->inode_lock);
4253 if (atomic_read(&inode->i_count) > 1)
4254 d_prune_aliases(inode);
4255 /*
4256 * btrfs_drop_inode will have it removed from the inode
4257 * cache when its usage count hits zero.
4258 */
4259 iput(inode);
4260 cond_resched();
4261 spin_lock(&root->inode_lock);
4262 goto again;
4263 }
4264
4265 if (cond_resched_lock(&root->inode_lock))
4266 goto again;
4267
4268 node = rb_next(node);
4269 }
4270 spin_unlock(&root->inode_lock);
4271}
4272
Misono Tomohirof60a2362018-04-18 11:34:52 +09004273int btrfs_delete_subvolume(struct inode *dir, struct dentry *dentry)
4274{
4275 struct btrfs_fs_info *fs_info = btrfs_sb(dentry->d_sb);
4276 struct btrfs_root *root = BTRFS_I(dir)->root;
4277 struct inode *inode = d_inode(dentry);
4278 struct btrfs_root *dest = BTRFS_I(inode)->root;
4279 struct btrfs_trans_handle *trans;
4280 struct btrfs_block_rsv block_rsv;
4281 u64 root_flags;
Misono Tomohirof60a2362018-04-18 11:34:52 +09004282 int ret;
4283 int err;
4284
4285 /*
4286 * Don't allow to delete a subvolume with send in progress. This is
4287 * inside the inode lock so the error handling that has to drop the bit
4288 * again is not run concurrently.
4289 */
4290 spin_lock(&dest->root_item_lock);
Lu Fengqia7176f72018-08-04 21:10:53 +08004291 if (dest->send_in_progress) {
Misono Tomohirof60a2362018-04-18 11:34:52 +09004292 spin_unlock(&dest->root_item_lock);
4293 btrfs_warn(fs_info,
4294 "attempt to delete subvolume %llu during send",
4295 dest->root_key.objectid);
4296 return -EPERM;
4297 }
Lu Fengqia7176f72018-08-04 21:10:53 +08004298 root_flags = btrfs_root_flags(&dest->root_item);
4299 btrfs_set_root_flags(&dest->root_item,
4300 root_flags | BTRFS_ROOT_SUBVOL_DEAD);
4301 spin_unlock(&dest->root_item_lock);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004302
4303 down_write(&fs_info->subvol_sem);
4304
4305 err = may_destroy_subvol(dest);
4306 if (err)
4307 goto out_up_write;
4308
4309 btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP);
4310 /*
4311 * One for dir inode,
4312 * two for dir entries,
4313 * two for root ref/backref.
4314 */
Gu JinXiangc4c129d2018-05-30 11:00:38 +08004315 err = btrfs_subvolume_reserve_metadata(root, &block_rsv, 5, true);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004316 if (err)
4317 goto out_up_write;
4318
4319 trans = btrfs_start_transaction(root, 0);
4320 if (IS_ERR(trans)) {
4321 err = PTR_ERR(trans);
4322 goto out_release;
4323 }
4324 trans->block_rsv = &block_rsv;
4325 trans->bytes_reserved = block_rsv.size;
4326
4327 btrfs_record_snapshot_destroy(trans, BTRFS_I(dir));
4328
Lu Fengqi401b3b12018-08-01 11:32:30 +08004329 ret = btrfs_unlink_subvol(trans, dir, dest->root_key.objectid,
4330 dentry->d_name.name, dentry->d_name.len);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004331 if (ret) {
4332 err = ret;
4333 btrfs_abort_transaction(trans, ret);
4334 goto out_end_trans;
4335 }
4336
4337 btrfs_record_root_in_trans(trans, dest);
4338
4339 memset(&dest->root_item.drop_progress, 0,
4340 sizeof(dest->root_item.drop_progress));
4341 dest->root_item.drop_level = 0;
4342 btrfs_set_root_refs(&dest->root_item, 0);
4343
4344 if (!test_and_set_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &dest->state)) {
4345 ret = btrfs_insert_orphan_item(trans,
4346 fs_info->tree_root,
4347 dest->root_key.objectid);
4348 if (ret) {
4349 btrfs_abort_transaction(trans, ret);
4350 err = ret;
4351 goto out_end_trans;
4352 }
4353 }
4354
Lu Fengqid1957792018-05-29 15:01:54 +08004355 ret = btrfs_uuid_tree_remove(trans, dest->root_item.uuid,
Misono Tomohirof60a2362018-04-18 11:34:52 +09004356 BTRFS_UUID_KEY_SUBVOL,
4357 dest->root_key.objectid);
4358 if (ret && ret != -ENOENT) {
4359 btrfs_abort_transaction(trans, ret);
4360 err = ret;
4361 goto out_end_trans;
4362 }
4363 if (!btrfs_is_empty_uuid(dest->root_item.received_uuid)) {
Lu Fengqid1957792018-05-29 15:01:54 +08004364 ret = btrfs_uuid_tree_remove(trans,
Misono Tomohirof60a2362018-04-18 11:34:52 +09004365 dest->root_item.received_uuid,
4366 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
4367 dest->root_key.objectid);
4368 if (ret && ret != -ENOENT) {
4369 btrfs_abort_transaction(trans, ret);
4370 err = ret;
4371 goto out_end_trans;
4372 }
4373 }
4374
4375out_end_trans:
4376 trans->block_rsv = NULL;
4377 trans->bytes_reserved = 0;
4378 ret = btrfs_end_transaction(trans);
4379 if (ret && !err)
4380 err = ret;
4381 inode->i_flags |= S_DEAD;
4382out_release:
4383 btrfs_subvolume_release_metadata(fs_info, &block_rsv);
4384out_up_write:
4385 up_write(&fs_info->subvol_sem);
4386 if (err) {
4387 spin_lock(&dest->root_item_lock);
4388 root_flags = btrfs_root_flags(&dest->root_item);
4389 btrfs_set_root_flags(&dest->root_item,
4390 root_flags & ~BTRFS_ROOT_SUBVOL_DEAD);
4391 spin_unlock(&dest->root_item_lock);
4392 } else {
4393 d_invalidate(dentry);
Nikolay Borisov20a68002018-04-27 14:36:24 +03004394 btrfs_prune_dentries(dest);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004395 ASSERT(dest->send_in_progress == 0);
4396
4397 /* the last ref */
4398 if (dest->ino_cache_inode) {
4399 iput(dest->ino_cache_inode);
4400 dest->ino_cache_inode = NULL;
4401 }
4402 }
4403
4404 return err;
4405}
4406
Chris Mason39279cc2007-06-12 06:35:45 -04004407static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
4408{
David Howells2b0143b2015-03-17 22:25:59 +00004409 struct inode *inode = d_inode(dentry);
Chris Mason1832a6d2007-12-21 16:27:21 -05004410 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004411 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004412 struct btrfs_trans_handle *trans;
Filipe Manana44f714d2016-06-06 16:11:13 +01004413 u64 last_unlink_trans;
Chris Mason39279cc2007-06-12 06:35:45 -04004414
David Sterbab3ae2442012-09-13 16:04:34 -06004415 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Yan134d4512007-10-25 15:49:25 -04004416 return -ENOTEMPTY;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004417 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_FIRST_FREE_OBJECTID)
Misono Tomohiroa79a4642018-04-18 11:35:31 +09004418 return btrfs_delete_subvolume(dir, dentry);
Yan134d4512007-10-25 15:49:25 -04004419
Josef Bacikd52be812013-05-29 14:54:47 -04004420 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004421 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004422 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004423
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004424 if (unlikely(btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Lu Fengqi401b3b12018-08-01 11:32:30 +08004425 err = btrfs_unlink_subvol(trans, dir,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004426 BTRFS_I(inode)->location.objectid,
4427 dentry->d_name.name,
4428 dentry->d_name.len);
4429 goto out;
4430 }
4431
Nikolay Borisov73f2e542017-02-20 13:50:59 +02004432 err = btrfs_orphan_add(trans, BTRFS_I(inode));
Josef Bacik7b128762008-07-24 12:17:14 -04004433 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004434 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004435
Filipe Manana44f714d2016-06-06 16:11:13 +01004436 last_unlink_trans = BTRFS_I(inode)->last_unlink_trans;
4437
Chris Mason39279cc2007-06-12 06:35:45 -04004438 /* now the directory is empty */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004439 err = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
4440 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
4441 dentry->d_name.len);
Filipe Manana44f714d2016-06-06 16:11:13 +01004442 if (!err) {
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004443 btrfs_i_size_write(BTRFS_I(inode), 0);
Filipe Manana44f714d2016-06-06 16:11:13 +01004444 /*
4445 * Propagate the last_unlink_trans value of the deleted dir to
4446 * its parent directory. This is to prevent an unrecoverable
4447 * log tree in the case we do something like this:
4448 * 1) create dir foo
4449 * 2) create snapshot under dir foo
4450 * 3) delete the snapshot
4451 * 4) rmdir foo
4452 * 5) mkdir foo
4453 * 6) fsync foo or some file inside foo
4454 */
4455 if (last_unlink_trans >= trans->transid)
4456 BTRFS_I(dir)->last_unlink_trans = last_unlink_trans;
4457 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004458out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004459 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004460 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39544012007-12-12 14:38:19 -05004461
Chris Mason39279cc2007-06-12 06:35:45 -04004462 return err;
4463}
4464
Josef Bacikddfae632017-10-19 14:16:02 -04004465/*
4466 * Return this if we need to call truncate_block for the last bit of the
4467 * truncate.
4468 */
4469#define NEED_TRUNCATE_BLOCK 1
Filipe Manana0305cd52015-10-16 12:34:25 +01004470
Chris Mason323ac952008-10-01 19:05:46 -04004471/*
Chris Mason39279cc2007-06-12 06:35:45 -04004472 * this can truncate away extent items, csum items and directory items.
4473 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04004474 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04004475 *
4476 * csum items that cross the new i_size are truncated to the new size
4477 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04004478 *
4479 * min_type is the minimum key type to truncate down to. If set to 0, this
4480 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04004481 */
Yan, Zheng80825102009-11-12 09:35:36 +00004482int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
4483 struct btrfs_root *root,
4484 struct inode *inode,
4485 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004486{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004487 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04004488 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004489 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004490 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00004491 struct btrfs_key key;
4492 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04004493 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04004494 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004495 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004496 u64 item_end = 0;
Filipe Mananac1aa4572015-06-20 18:20:09 +01004497 u64 last_size = new_size;
Yan, Zheng80825102009-11-12 09:35:36 +00004498 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04004499 int found_extent;
4500 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05004501 int pending_del_nr = 0;
4502 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04004503 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00004504 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004505 u64 ino = btrfs_ino(BTRFS_I(inode));
Chris Mason28ed1342014-12-17 09:41:04 -08004506 u64 bytes_deleted = 0;
Thomas Meyer897ca812017-10-07 16:02:21 +02004507 bool be_nice = false;
4508 bool should_throttle = false;
Yan, Zheng80825102009-11-12 09:35:36 +00004509
4510 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04004511
Chris Mason28ed1342014-12-17 09:41:04 -08004512 /*
4513 * for non-free space inodes and ref cows, we want to back off from
4514 * time to time
4515 */
Nikolay Borisov70ddc552017-02-20 13:50:35 +02004516 if (!btrfs_is_free_space_inode(BTRFS_I(inode)) &&
Chris Mason28ed1342014-12-17 09:41:04 -08004517 test_bit(BTRFS_ROOT_REF_COWS, &root->state))
Thomas Meyer897ca812017-10-07 16:02:21 +02004518 be_nice = true;
Chris Mason28ed1342014-12-17 09:41:04 -08004519
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004520 path = btrfs_alloc_path();
4521 if (!path)
4522 return -ENOMEM;
David Sterbae4058b52015-11-27 16:31:35 +01004523 path->reada = READA_BACK;
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004524
Josef Bacik5dc562c2012-08-17 13:14:17 -04004525 /*
4526 * We want to drop from the next block forward in case this new size is
4527 * not block aligned since we will be keeping the last block of the
4528 * extent just the way it is.
4529 */
Miao Xie27cdeb72014-04-02 19:51:05 +08004530 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004531 root == fs_info->tree_root)
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02004532 btrfs_drop_extent_cache(BTRFS_I(inode), ALIGN(new_size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004533 fs_info->sectorsize),
Jeff Mahoneyda170662016-06-15 09:22:56 -04004534 (u64)-1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00004535
Miao Xie16cdcec2011-04-22 18:12:22 +08004536 /*
4537 * This function is also used to drop the items in the log tree before
4538 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
Andrea Gelmini52042d82018-11-28 12:05:13 +01004539 * it is used to drop the logged items. So we shouldn't kill the delayed
Miao Xie16cdcec2011-04-22 18:12:22 +08004540 * items.
4541 */
4542 if (min_type == 0 && root == BTRFS_I(inode)->root)
Nikolay Borisov4ccb5c72017-01-10 20:35:38 +02004543 btrfs_kill_delayed_inode_items(BTRFS_I(inode));
Miao Xie16cdcec2011-04-22 18:12:22 +08004544
Li Zefan33345d012011-04-20 10:31:50 +08004545 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04004546 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04004547 key.type = (u8)-1;
4548
Chris Mason85e21ba2008-01-29 15:11:36 -05004549search_again:
Chris Mason28ed1342014-12-17 09:41:04 -08004550 /*
4551 * with a 16K leaf size and 128MB extents, you can actually queue
4552 * up a huge file in a single leaf. Most of the time that
4553 * bytes_deleted is > 0, it will be huge by the time we get here
4554 */
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004555 if (be_nice && bytes_deleted > SZ_32M &&
4556 btrfs_should_end_transaction(trans)) {
4557 ret = -EAGAIN;
Yan, Zheng80825102009-11-12 09:35:36 +00004558 goto out;
4559 }
Chris Masond3977122009-01-05 21:25:51 -05004560
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004561 path->leave_spinning = 1;
4562 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
4563 if (ret < 0)
4564 goto out;
4565
Chris Mason85e21ba2008-01-29 15:11:36 -05004566 if (ret > 0) {
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004567 ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -04004568 /* there are no items in the tree for us to truncate, we're
4569 * done
4570 */
Yan, Zheng80825102009-11-12 09:35:36 +00004571 if (path->slots[0] == 0)
4572 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05004573 path->slots[0]--;
4574 }
4575
Chris Masond3977122009-01-05 21:25:51 -05004576 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04004577 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04004578 leaf = path->nodes[0];
4579 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02004580 found_type = found_key.type;
Chris Mason39279cc2007-06-12 06:35:45 -04004581
Li Zefan33345d012011-04-20 10:31:50 +08004582 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04004583 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004584
Chris Mason85e21ba2008-01-29 15:11:36 -05004585 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004586 break;
4587
Chris Mason5f39d392007-10-15 16:14:19 -04004588 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04004589 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04004590 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04004591 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04004592 extent_type = btrfs_file_extent_type(leaf, fi);
4593 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04004594 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04004595 btrfs_file_extent_num_bytes(leaf, fi);
Liu Bo09ed2f12017-03-10 11:09:48 -08004596
4597 trace_btrfs_truncate_show_fi_regular(
4598 BTRFS_I(inode), leaf, fi,
4599 found_key.offset);
Chris Mason179e29e2007-11-01 11:28:41 -04004600 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Qu Wenruoe41ca582018-06-06 15:41:49 +08004601 item_end += btrfs_file_extent_ram_bytes(leaf,
4602 fi);
Liu Bo09ed2f12017-03-10 11:09:48 -08004603
4604 trace_btrfs_truncate_show_fi_inline(
4605 BTRFS_I(inode), leaf, fi, path->slots[0],
4606 found_key.offset);
Chris Mason39279cc2007-06-12 06:35:45 -04004607 }
Yan008630c2007-11-07 13:31:09 -05004608 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04004609 }
Yan, Zheng80825102009-11-12 09:35:36 +00004610 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04004611 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00004612 } else {
Filipe Manana76b42ab2017-02-14 16:56:01 +00004613 if (item_end < new_size)
Yan, Zheng80825102009-11-12 09:35:36 +00004614 break;
4615 if (found_key.offset >= new_size)
4616 del_item = 1;
4617 else
4618 del_item = 0;
4619 }
Chris Mason39279cc2007-06-12 06:35:45 -04004620 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004621 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04004622 if (found_type != BTRFS_EXTENT_DATA_KEY)
4623 goto delete;
4624
4625 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04004626 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04004627 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05004628 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04004629 u64 orig_num_bytes =
4630 btrfs_file_extent_num_bytes(leaf, fi);
Qu Wenruofda28322013-02-26 08:10:22 +00004631 extent_num_bytes = ALIGN(new_size -
4632 found_key.offset,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004633 fs_info->sectorsize);
Chris Masondb945352007-10-15 16:15:53 -04004634 btrfs_set_file_extent_num_bytes(leaf, fi,
4635 extent_num_bytes);
4636 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05004637 extent_num_bytes);
Miao Xie27cdeb72014-04-02 19:51:05 +08004638 if (test_bit(BTRFS_ROOT_REF_COWS,
4639 &root->state) &&
4640 extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004641 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04004642 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004643 } else {
Chris Masondb945352007-10-15 16:15:53 -04004644 extent_num_bytes =
4645 btrfs_file_extent_disk_num_bytes(leaf,
4646 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004647 extent_offset = found_key.offset -
4648 btrfs_file_extent_offset(leaf, fi);
4649
Chris Mason39279cc2007-06-12 06:35:45 -04004650 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05004651 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004652 if (extent_start != 0) {
4653 found_extent = 1;
Miao Xie27cdeb72014-04-02 19:51:05 +08004654 if (test_bit(BTRFS_ROOT_REF_COWS,
4655 &root->state))
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004656 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04004657 }
4658 }
Chris Mason90692182008-02-08 13:49:28 -05004659 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04004660 /*
4661 * we can't truncate inline items that have had
4662 * special encodings
4663 */
4664 if (!del_item &&
Chris Masonc8b97812008-10-29 14:49:59 -04004665 btrfs_file_extent_encryption(leaf, fi) == 0 &&
Josef Bacikddfae632017-10-19 14:16:02 -04004666 btrfs_file_extent_other_encoding(leaf, fi) == 0 &&
4667 btrfs_file_extent_compression(leaf, fi) == 0) {
4668 u32 size = (u32)(new_size - found_key.offset);
Chris Mason514ac8a2014-01-03 21:07:00 -08004669
Josef Bacikddfae632017-10-19 14:16:02 -04004670 btrfs_set_file_extent_ram_bytes(leaf, fi, size);
4671 size = btrfs_file_extent_calc_inline_size(size);
David Sterba78ac4f92019-03-20 14:49:12 +01004672 btrfs_truncate_item(path, size, 1);
Josef Bacikddfae632017-10-19 14:16:02 -04004673 } else if (!del_item) {
Chris Mason514ac8a2014-01-03 21:07:00 -08004674 /*
Josef Bacikddfae632017-10-19 14:16:02 -04004675 * We have to bail so the last_size is set to
4676 * just before this extent.
Chris Mason514ac8a2014-01-03 21:07:00 -08004677 */
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004678 ret = NEED_TRUNCATE_BLOCK;
Josef Bacikddfae632017-10-19 14:16:02 -04004679 break;
Chris Mason90692182008-02-08 13:49:28 -05004680 }
Josef Bacikddfae632017-10-19 14:16:02 -04004681
4682 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state))
4683 inode_sub_bytes(inode, item_end + 1 - new_size);
Chris Mason39279cc2007-06-12 06:35:45 -04004684 }
Chris Mason179e29e2007-11-01 11:28:41 -04004685delete:
Josef Bacikddfae632017-10-19 14:16:02 -04004686 if (del_item)
4687 last_size = found_key.offset;
4688 else
4689 last_size = new_size;
Chris Mason39279cc2007-06-12 06:35:45 -04004690 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05004691 if (!pending_del_nr) {
4692 /* no pending yet, add ourselves */
4693 pending_del_slot = path->slots[0];
4694 pending_del_nr = 1;
4695 } else if (pending_del_nr &&
4696 path->slots[0] + 1 == pending_del_slot) {
4697 /* hop on the pending chunk */
4698 pending_del_nr++;
4699 pending_del_slot = path->slots[0];
4700 } else {
Chris Masond3977122009-01-05 21:25:51 -05004701 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05004702 }
Chris Mason39279cc2007-06-12 06:35:45 -04004703 } else {
4704 break;
4705 }
Thomas Meyer897ca812017-10-07 16:02:21 +02004706 should_throttle = false;
Chris Mason28f75a02015-02-04 06:59:29 -08004707
Miao Xie27cdeb72014-04-02 19:51:05 +08004708 if (found_extent &&
4709 (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004710 root == fs_info->tree_root)) {
Qu Wenruoffd4bb22019-04-04 14:45:36 +08004711 struct btrfs_ref ref = { 0 };
4712
Chris Masonb9473432009-03-13 11:00:37 -04004713 btrfs_set_path_blocking(path);
Chris Mason28ed1342014-12-17 09:41:04 -08004714 bytes_deleted += extent_num_bytes;
Qu Wenruoffd4bb22019-04-04 14:45:36 +08004715
4716 btrfs_init_generic_ref(&ref, BTRFS_DROP_DELAYED_REF,
4717 extent_start, extent_num_bytes, 0);
4718 ref.real_root = root->root_key.objectid;
4719 btrfs_init_data_ref(&ref, btrfs_header_owner(leaf),
4720 ino, extent_offset);
4721 ret = btrfs_free_extent(trans, &ref);
Omar Sandoval05522102018-05-11 13:13:31 -07004722 if (ret) {
4723 btrfs_abort_transaction(trans, ret);
4724 break;
4725 }
Chris Mason28f75a02015-02-04 06:59:29 -08004726 if (be_nice) {
Lu Fengqi7c861622018-10-11 13:40:36 +08004727 if (btrfs_should_throttle_delayed_refs(trans))
Thomas Meyer897ca812017-10-07 16:02:21 +02004728 should_throttle = true;
Chris Mason28f75a02015-02-04 06:59:29 -08004729 }
Chris Mason39279cc2007-06-12 06:35:45 -04004730 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004731
Yan, Zheng80825102009-11-12 09:35:36 +00004732 if (found_type == BTRFS_INODE_ITEM_KEY)
4733 break;
4734
4735 if (path->slots[0] == 0 ||
Josef Bacik12621332015-02-03 07:50:16 -08004736 path->slots[0] != pending_del_slot ||
Josef Bacik28bad212018-12-03 10:20:38 -05004737 should_throttle) {
Yan, Zheng80825102009-11-12 09:35:36 +00004738 if (pending_del_nr) {
4739 ret = btrfs_del_items(trans, root, path,
4740 pending_del_slot,
4741 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004742 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004743 btrfs_abort_transaction(trans, ret);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004744 break;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004745 }
Yan, Zheng80825102009-11-12 09:35:36 +00004746 pending_del_nr = 0;
4747 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004748 btrfs_release_path(path);
Josef Bacik28bad212018-12-03 10:20:38 -05004749
Chris Mason28f75a02015-02-04 06:59:29 -08004750 /*
Josef Bacik28bad212018-12-03 10:20:38 -05004751 * We can generate a lot of delayed refs, so we need to
4752 * throttle every once and a while and make sure we're
4753 * adding enough space to keep up with the work we are
4754 * generating. Since we hold a transaction here we
4755 * can't flush, and we don't want to FLUSH_LIMIT because
4756 * we could have generated too many delayed refs to
4757 * actually allocate, so just bail if we're short and
4758 * let the normal reservation dance happen higher up.
Chris Mason28f75a02015-02-04 06:59:29 -08004759 */
Josef Bacik28bad212018-12-03 10:20:38 -05004760 if (should_throttle) {
4761 ret = btrfs_delayed_refs_rsv_refill(fs_info,
4762 BTRFS_RESERVE_NO_FLUSH);
4763 if (ret) {
4764 ret = -EAGAIN;
4765 break;
4766 }
Chris Mason28f75a02015-02-04 06:59:29 -08004767 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004768 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00004769 } else {
4770 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05004771 }
Chris Mason39279cc2007-06-12 06:35:45 -04004772 }
Yan, Zheng80825102009-11-12 09:35:36 +00004773out:
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004774 if (ret >= 0 && pending_del_nr) {
4775 int err;
4776
4777 err = btrfs_del_items(trans, root, path, pending_del_slot,
Chris Mason85e21ba2008-01-29 15:11:36 -05004778 pending_del_nr);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004779 if (err) {
4780 btrfs_abort_transaction(trans, err);
4781 ret = err;
4782 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004783 }
Filipe Manana76b42ab2017-02-14 16:56:01 +00004784 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
4785 ASSERT(last_size >= new_size);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004786 if (!ret && last_size > new_size)
Filipe Manana76b42ab2017-02-14 16:56:01 +00004787 last_size = new_size;
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004788 btrfs_ordered_update_i_size(inode, last_size, NULL);
Filipe Manana76b42ab2017-02-14 16:56:01 +00004789 }
Chris Mason28ed1342014-12-17 09:41:04 -08004790
Chris Mason39279cc2007-06-12 06:35:45 -04004791 btrfs_free_path(path);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004792 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04004793}
4794
Chris Masona52d9a82007-08-27 16:49:44 -04004795/*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304796 * btrfs_truncate_block - read, zero a chunk and write a block
Josef Bacik2aaa6652012-08-29 14:27:18 -04004797 * @inode - inode that we're zeroing
4798 * @from - the offset to start zeroing
4799 * @len - the length to zero, 0 to zero the entire range respective to the
4800 * offset
4801 * @front - zero up to the offset instead of from the offset on
4802 *
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304803 * This will find the block for the "from" offset and cow the block and zero the
Josef Bacik2aaa6652012-08-29 14:27:18 -04004804 * part we want to zero. This is used with truncate and hole punching.
Chris Masona52d9a82007-08-27 16:49:44 -04004805 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304806int btrfs_truncate_block(struct inode *inode, loff_t from, loff_t len,
Josef Bacik2aaa6652012-08-29 14:27:18 -04004807 int front)
Chris Masona52d9a82007-08-27 16:49:44 -04004808{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004809 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004810 struct address_space *mapping = inode->i_mapping;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004811 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4812 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004813 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08004814 struct extent_changeset *data_reserved = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004815 char *kaddr;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004816 u32 blocksize = fs_info->sectorsize;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004817 pgoff_t index = from >> PAGE_SHIFT;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304818 unsigned offset = from & (blocksize - 1);
Chris Masona52d9a82007-08-27 16:49:44 -04004819 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004820 gfp_t mask = btrfs_alloc_write_mask(mapping);
Chris Masona52d9a82007-08-27 16:49:44 -04004821 int ret = 0;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304822 u64 block_start;
4823 u64 block_end;
Chris Masona52d9a82007-08-27 16:49:44 -04004824
Nikolay Borisovb03ebd92018-01-18 14:47:06 +02004825 if (IS_ALIGNED(offset, blocksize) &&
4826 (!len || IS_ALIGNED(len, blocksize)))
Chris Masona52d9a82007-08-27 16:49:44 -04004827 goto out;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304828
Josef Bacik8b62f872017-10-19 14:15:55 -04004829 block_start = round_down(from, blocksize);
4830 block_end = block_start + blocksize - 1;
4831
Qu Wenruo364ecf32017-02-27 15:10:38 +08004832 ret = btrfs_delalloc_reserve_space(inode, &data_reserved,
Josef Bacik8b62f872017-10-19 14:15:55 -04004833 block_start, blocksize);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004834 if (ret)
4835 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004836
Chris Mason211c17f2008-05-15 09:13:45 -04004837again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004838 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004839 if (!page) {
Qu Wenruobc42bda2017-02-27 15:10:39 +08004840 btrfs_delalloc_release_space(inode, data_reserved,
Qu Wenruo43b18592017-12-12 15:34:32 +08004841 block_start, blocksize, true);
4842 btrfs_delalloc_release_extents(BTRFS_I(inode), blocksize, true);
Miao Xieac6a2b32012-12-05 10:56:13 +00004843 ret = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -04004844 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04004845 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004846
Chris Masona52d9a82007-08-27 16:49:44 -04004847 if (!PageUptodate(page)) {
4848 ret = btrfs_readpage(NULL, page);
4849 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004850 if (page->mapping != mapping) {
4851 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004852 put_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004853 goto again;
4854 }
Chris Masona52d9a82007-08-27 16:49:44 -04004855 if (!PageUptodate(page)) {
4856 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04004857 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04004858 }
4859 }
Chris Mason211c17f2008-05-15 09:13:45 -04004860 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004861
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304862 lock_extent_bits(io_tree, block_start, block_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004863 set_page_extent_mapped(page);
4864
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304865 ordered = btrfs_lookup_ordered_extent(inode, block_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004866 if (ordered) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304867 unlock_extent_cached(io_tree, block_start, block_end,
David Sterbae43bbe52017-12-12 21:43:52 +01004868 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004869 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004870 put_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04004871 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004872 btrfs_put_ordered_extent(ordered);
4873 goto again;
4874 }
4875
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304876 clear_extent_bit(&BTRFS_I(inode)->io_tree, block_start, block_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06004877 EXTENT_DIRTY | EXTENT_DELALLOC |
4878 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
David Sterbaae0f1622017-10-31 16:37:52 +01004879 0, 0, &cached_state);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004880
Filipe Mananae3b8a482017-11-04 00:16:59 +00004881 ret = btrfs_set_extent_delalloc(inode, block_start, block_end, 0,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08004882 &cached_state, 0);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004883 if (ret) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304884 unlock_extent_cached(io_tree, block_start, block_end,
David Sterbae43bbe52017-12-12 21:43:52 +01004885 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004886 goto out_unlock;
4887 }
4888
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304889 if (offset != blocksize) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04004890 if (!len)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304891 len = blocksize - offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004892 kaddr = kmap(page);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004893 if (front)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304894 memset(kaddr + (block_start - page_offset(page)),
4895 0, offset);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004896 else
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304897 memset(kaddr + (block_start - page_offset(page)) + offset,
4898 0, len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004899 flush_dcache_page(page);
4900 kunmap(page);
4901 }
Chris Mason247e7432008-07-17 12:53:51 -04004902 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004903 set_page_dirty(page);
David Sterbae43bbe52017-12-12 21:43:52 +01004904 unlock_extent_cached(io_tree, block_start, block_end, &cached_state);
Chris Mason39279cc2007-06-12 06:35:45 -04004905
Chris Mason89642222008-07-24 09:41:53 -04004906out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04004907 if (ret)
Qu Wenruobc42bda2017-02-27 15:10:39 +08004908 btrfs_delalloc_release_space(inode, data_reserved, block_start,
Qu Wenruo43b18592017-12-12 15:34:32 +08004909 blocksize, true);
4910 btrfs_delalloc_release_extents(BTRFS_I(inode), blocksize, (ret != 0));
Chris Mason39279cc2007-06-12 06:35:45 -04004911 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004912 put_page(page);
Chris Mason39279cc2007-06-12 06:35:45 -04004913out:
Qu Wenruo364ecf32017-02-27 15:10:38 +08004914 extent_changeset_free(data_reserved);
Chris Mason39279cc2007-06-12 06:35:45 -04004915 return ret;
4916}
4917
Josef Bacik16e75492013-10-22 12:18:51 -04004918static int maybe_insert_hole(struct btrfs_root *root, struct inode *inode,
4919 u64 offset, u64 len)
4920{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004921 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik16e75492013-10-22 12:18:51 -04004922 struct btrfs_trans_handle *trans;
4923 int ret;
4924
4925 /*
4926 * Still need to make sure the inode looks like it's been updated so
4927 * that any holes get logged if we fsync.
4928 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004929 if (btrfs_fs_incompat(fs_info, NO_HOLES)) {
4930 BTRFS_I(inode)->last_trans = fs_info->generation;
Josef Bacik16e75492013-10-22 12:18:51 -04004931 BTRFS_I(inode)->last_sub_trans = root->log_transid;
4932 BTRFS_I(inode)->last_log_commit = root->last_log_commit;
4933 return 0;
4934 }
4935
4936 /*
4937 * 1 - for the one we're dropping
4938 * 1 - for the one we're adding
4939 * 1 - for updating the inode.
4940 */
4941 trans = btrfs_start_transaction(root, 3);
4942 if (IS_ERR(trans))
4943 return PTR_ERR(trans);
4944
4945 ret = btrfs_drop_extents(trans, root, inode, offset, offset + len, 1);
4946 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004947 btrfs_abort_transaction(trans, ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004948 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04004949 return ret;
4950 }
4951
David Sterbaf85b7372017-01-20 14:54:07 +01004952 ret = btrfs_insert_file_extent(trans, root, btrfs_ino(BTRFS_I(inode)),
4953 offset, 0, 0, len, 0, len, 0, 0, 0);
Josef Bacik16e75492013-10-22 12:18:51 -04004954 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004955 btrfs_abort_transaction(trans, ret);
Josef Bacik16e75492013-10-22 12:18:51 -04004956 else
4957 btrfs_update_inode(trans, root, inode);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004958 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04004959 return ret;
4960}
4961
Josef Bacik695a0d02011-03-04 15:46:53 -05004962/*
4963 * This function puts in dummy file extents for the area we're creating a hole
4964 * for. So if we are truncating this file to a larger size we need to insert
4965 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
4966 * the range between oldsize and size
4967 */
Josef Bacika41ad392011-01-31 15:30:16 -05004968int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04004969{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004970 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Yan Zheng9036c102008-10-30 14:19:41 -04004971 struct btrfs_root *root = BTRFS_I(inode)->root;
4972 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004973 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004974 struct extent_state *cached_state = NULL;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004975 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004976 u64 hole_start = ALIGN(oldsize, fs_info->sectorsize);
4977 u64 block_end = ALIGN(size, fs_info->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04004978 u64 last_byte;
4979 u64 cur_offset;
4980 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04004981 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04004982
Josef Bacika71754f2013-06-17 17:14:39 -04004983 /*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304984 * If our size started in the middle of a block we need to zero out the
4985 * rest of the block before we expand the i_size, otherwise we could
Josef Bacika71754f2013-06-17 17:14:39 -04004986 * expose stale data.
4987 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304988 err = btrfs_truncate_block(inode, oldsize, 0, 0);
Josef Bacika71754f2013-06-17 17:14:39 -04004989 if (err)
4990 return err;
4991
Yan Zheng9036c102008-10-30 14:19:41 -04004992 if (size <= hole_start)
4993 return 0;
4994
Yan Zheng9036c102008-10-30 14:19:41 -04004995 while (1) {
4996 struct btrfs_ordered_extent *ordered;
Miao Xiefa7c1492013-09-26 13:15:27 +08004997
David Sterbaff13db42015-12-03 14:30:40 +01004998 lock_extent_bits(io_tree, hole_start, block_end - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004999 &cached_state);
Nikolay Borisova776c6f2017-02-20 13:50:49 +02005000 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), hole_start,
Miao Xiefa7c1492013-09-26 13:15:27 +08005001 block_end - hole_start);
Yan Zheng9036c102008-10-30 14:19:41 -04005002 if (!ordered)
5003 break;
Josef Bacik2ac55d42010-02-03 19:33:23 +00005004 unlock_extent_cached(io_tree, hole_start, block_end - 1,
David Sterbae43bbe52017-12-12 21:43:52 +01005005 &cached_state);
Miao Xiefa7c1492013-09-26 13:15:27 +08005006 btrfs_start_ordered_extent(inode, ordered, 1);
Yan Zheng9036c102008-10-30 14:19:41 -04005007 btrfs_put_ordered_extent(ordered);
5008 }
5009
Yan Zheng9036c102008-10-30 14:19:41 -04005010 cur_offset = hole_start;
5011 while (1) {
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02005012 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, cur_offset,
Yan Zheng9036c102008-10-30 14:19:41 -04005013 block_end - cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005014 if (IS_ERR(em)) {
5015 err = PTR_ERR(em);
Zach Brownf2767952013-01-08 19:37:58 +00005016 em = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005017 break;
5018 }
Yan Zheng9036c102008-10-30 14:19:41 -04005019 last_byte = min(extent_map_end(em), block_end);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005020 last_byte = ALIGN(last_byte, fs_info->sectorsize);
Yan, Zheng80825102009-11-12 09:35:36 +00005021 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04005022 struct extent_map *hole_em;
Yan Zheng9036c102008-10-30 14:19:41 -04005023 hole_size = last_byte - cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00005024
Josef Bacik16e75492013-10-22 12:18:51 -04005025 err = maybe_insert_hole(root, inode, cur_offset,
5026 hole_size);
5027 if (err)
Chris Mason771ed682008-11-06 22:02:51 -05005028 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02005029 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04005030 cur_offset + hole_size - 1, 0);
5031 hole_em = alloc_extent_map();
5032 if (!hole_em) {
5033 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
5034 &BTRFS_I(inode)->runtime_flags);
5035 goto next;
5036 }
5037 hole_em->start = cur_offset;
5038 hole_em->len = hole_size;
5039 hole_em->orig_start = cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00005040
Josef Bacik5dc562c2012-08-17 13:14:17 -04005041 hole_em->block_start = EXTENT_MAP_HOLE;
5042 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05005043 hole_em->orig_block_len = 0;
Josef Bacikcc95bef2013-04-04 14:31:27 -04005044 hole_em->ram_bytes = hole_size;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005045 hole_em->bdev = fs_info->fs_devices->latest_bdev;
Josef Bacik5dc562c2012-08-17 13:14:17 -04005046 hole_em->compress_type = BTRFS_COMPRESS_NONE;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005047 hole_em->generation = fs_info->generation;
Josef Bacik5dc562c2012-08-17 13:14:17 -04005048
5049 while (1) {
5050 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04005051 err = add_extent_mapping(em_tree, hole_em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04005052 write_unlock(&em_tree->lock);
5053 if (err != -EEXIST)
5054 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02005055 btrfs_drop_extent_cache(BTRFS_I(inode),
5056 cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04005057 cur_offset +
5058 hole_size - 1, 0);
5059 }
5060 free_extent_map(hole_em);
Yan Zheng9036c102008-10-30 14:19:41 -04005061 }
Josef Bacik16e75492013-10-22 12:18:51 -04005062next:
Yan Zheng9036c102008-10-30 14:19:41 -04005063 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04005064 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04005065 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00005066 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04005067 break;
5068 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04005069 free_extent_map(em);
David Sterbae43bbe52017-12-12 21:43:52 +01005070 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state);
Yan Zheng9036c102008-10-30 14:19:41 -04005071 return err;
5072}
5073
Eric Sandeen3972f262013-01-12 02:57:22 +00005074static int btrfs_setsize(struct inode *inode, struct iattr *attr)
Yan, Zheng80825102009-11-12 09:35:36 +00005075{
Miao Xief4a2f4c2011-12-14 20:12:01 -05005076 struct btrfs_root *root = BTRFS_I(inode)->root;
5077 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05005078 loff_t oldsize = i_size_read(inode);
Eric Sandeen3972f262013-01-12 02:57:22 +00005079 loff_t newsize = attr->ia_size;
5080 int mask = attr->ia_valid;
Yan, Zheng80825102009-11-12 09:35:36 +00005081 int ret;
5082
Eric Sandeen3972f262013-01-12 02:57:22 +00005083 /*
5084 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
5085 * special case where we need to update the times despite not having
5086 * these flags set. For all other operations the VFS set these flags
5087 * explicitly if it wants a timestamp update.
5088 */
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08005089 if (newsize != oldsize) {
5090 inode_inc_iversion(inode);
5091 if (!(mask & (ATTR_CTIME | ATTR_MTIME)))
5092 inode->i_ctime = inode->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07005093 current_time(inode);
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08005094 }
Eric Sandeen3972f262013-01-12 02:57:22 +00005095
Josef Bacika41ad392011-01-31 15:30:16 -05005096 if (newsize > oldsize) {
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005097 /*
David Sterbaea14b57f2017-06-22 02:19:11 +02005098 * Don't do an expanding truncate while snapshotting is ongoing.
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005099 * This is to ensure the snapshot captures a fully consistent
5100 * state of this file - if the snapshot captures this expanding
5101 * truncation, it must capture all writes that happened before
5102 * this truncation.
5103 */
Zhao Lei0bc19f902016-01-06 18:56:36 +08005104 btrfs_wait_for_snapshot_creation(root);
Josef Bacika41ad392011-01-31 15:30:16 -05005105 ret = btrfs_cont_expand(inode, oldsize, newsize);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005106 if (ret) {
David Sterbaea14b57f2017-06-22 02:19:11 +02005107 btrfs_end_write_no_snapshotting(root);
Yan, Zheng80825102009-11-12 09:35:36 +00005108 return ret;
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005109 }
Yan, Zheng80825102009-11-12 09:35:36 +00005110
Miao Xief4a2f4c2011-12-14 20:12:01 -05005111 trans = btrfs_start_transaction(root, 1);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005112 if (IS_ERR(trans)) {
David Sterbaea14b57f2017-06-22 02:19:11 +02005113 btrfs_end_write_no_snapshotting(root);
Miao Xief4a2f4c2011-12-14 20:12:01 -05005114 return PTR_ERR(trans);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005115 }
Miao Xief4a2f4c2011-12-14 20:12:01 -05005116
5117 i_size_write(inode, newsize);
5118 btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
Chandan Rajendra27772b62016-01-21 15:56:03 +05305119 pagecache_isize_extended(inode, oldsize, newsize);
Miao Xief4a2f4c2011-12-14 20:12:01 -05005120 ret = btrfs_update_inode(trans, root, inode);
David Sterbaea14b57f2017-06-22 02:19:11 +02005121 btrfs_end_write_no_snapshotting(root);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005122 btrfs_end_transaction(trans);
Josef Bacika41ad392011-01-31 15:30:16 -05005123 } else {
Yan, Zheng80825102009-11-12 09:35:36 +00005124
Josef Bacika41ad392011-01-31 15:30:16 -05005125 /*
5126 * We're truncating a file that used to have good data down to
5127 * zero. Make sure it gets into the ordered flush list so that
5128 * any new writes get down to disk quickly.
5129 */
5130 if (newsize == 0)
Josef Bacik72ac3c02012-05-23 14:13:11 -04005131 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
5132 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00005133
Josef Bacika41ad392011-01-31 15:30:16 -05005134 truncate_setsize(inode, newsize);
Miao Xie2e60a512013-02-08 07:01:08 +00005135
Andrea Gelmini52042d82018-11-28 12:05:13 +01005136 /* Disable nonlocked read DIO to avoid the endless truncate */
Nikolay Borisovabcefb12017-02-20 13:51:10 +02005137 btrfs_inode_block_unlocked_dio(BTRFS_I(inode));
Miao Xie2e60a512013-02-08 07:01:08 +00005138 inode_dio_wait(inode);
Nikolay Borisov0b581702017-02-20 13:51:11 +02005139 btrfs_inode_resume_unlocked_dio(BTRFS_I(inode));
Miao Xie2e60a512013-02-08 07:01:08 +00005140
Filipe Manana213e8c52018-02-06 20:40:31 +00005141 ret = btrfs_truncate(inode, newsize == oldsize);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005142 if (ret && inode->i_nlink) {
5143 int err;
5144
5145 /*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07005146 * Truncate failed, so fix up the in-memory size. We
5147 * adjusted disk_i_size down as we removed extents, so
5148 * wait for disk_i_size to be stable and then update the
5149 * in-memory size to match.
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005150 */
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07005151 err = btrfs_wait_ordered_range(inode, 0, (u64)-1);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005152 if (err)
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07005153 return err;
5154 i_size_write(inode, BTRFS_I(inode)->disk_i_size);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005155 }
Yan, Zheng80825102009-11-12 09:35:36 +00005156 }
5157
Josef Bacika41ad392011-01-31 15:30:16 -05005158 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00005159}
5160
Chris Mason39279cc2007-06-12 06:35:45 -04005161static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
5162{
David Howells2b0143b2015-03-17 22:25:59 +00005163 struct inode *inode = d_inode(dentry);
Li Zefanb83cc962010-12-20 16:04:08 +08005164 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005165 int err;
5166
Li Zefanb83cc962010-12-20 16:04:08 +08005167 if (btrfs_root_readonly(root))
5168 return -EROFS;
5169
Jan Kara31051c82016-05-26 16:55:18 +02005170 err = setattr_prepare(dentry, attr);
Chris Mason39279cc2007-06-12 06:35:45 -04005171 if (err)
5172 return err;
5173
Chris Mason5a3f23d2009-03-31 13:27:11 -04005174 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Eric Sandeen3972f262013-01-12 02:57:22 +00005175 err = btrfs_setsize(inode, attr);
Yan, Zheng80825102009-11-12 09:35:36 +00005176 if (err)
5177 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005178 }
Yan Zheng9036c102008-10-30 14:19:41 -04005179
Christoph Hellwig10257742010-06-04 11:30:02 +02005180 if (attr->ia_valid) {
5181 setattr_copy(inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005182 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005183 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04005184
Josef Bacik22c44fe2011-11-30 10:45:38 -05005185 if (!err && attr->ia_valid & ATTR_MODE)
Christoph Hellwig996a7102013-12-20 05:16:43 -08005186 err = posix_acl_chmod(inode, inode->i_mode);
Christoph Hellwig10257742010-06-04 11:30:02 +02005187 }
5188
Chris Mason39279cc2007-06-12 06:35:45 -04005189 return err;
5190}
Chris Mason61295eb2008-01-14 16:24:38 -05005191
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005192/*
5193 * While truncating the inode pages during eviction, we get the VFS calling
5194 * btrfs_invalidatepage() against each page of the inode. This is slow because
5195 * the calls to btrfs_invalidatepage() result in a huge amount of calls to
5196 * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting
5197 * extent_state structures over and over, wasting lots of time.
5198 *
5199 * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all
5200 * those expensive operations on a per page basis and do only the ordered io
5201 * finishing, while we release here the extent_map and extent_state structures,
5202 * without the excessive merging and splitting.
5203 */
5204static void evict_inode_truncate_pages(struct inode *inode)
5205{
5206 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
5207 struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree;
5208 struct rb_node *node;
5209
5210 ASSERT(inode->i_state & I_FREEING);
Johannes Weiner91b0abe2014-04-03 14:47:49 -07005211 truncate_inode_pages_final(&inode->i_data);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005212
5213 write_lock(&map_tree->lock);
Liu Bo07e1ce02018-08-23 03:51:52 +08005214 while (!RB_EMPTY_ROOT(&map_tree->map.rb_root)) {
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005215 struct extent_map *em;
5216
Liu Bo07e1ce02018-08-23 03:51:52 +08005217 node = rb_first_cached(&map_tree->map);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005218 em = rb_entry(node, struct extent_map, rb_node);
Wang Shilong180589e2013-12-14 15:27:31 +08005219 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
5220 clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005221 remove_extent_mapping(map_tree, em);
5222 free_extent_map(em);
Filipe Manana7064dd52014-08-08 02:47:05 +01005223 if (need_resched()) {
5224 write_unlock(&map_tree->lock);
5225 cond_resched();
5226 write_lock(&map_tree->lock);
5227 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005228 }
5229 write_unlock(&map_tree->lock);
5230
Filipe Manana6ca07092015-05-26 00:55:42 +01005231 /*
5232 * Keep looping until we have no more ranges in the io tree.
5233 * We can have ongoing bios started by readpages (called from readahead)
Filipe Manana9c6429d2015-06-10 12:55:41 +01005234 * that have their endio callback (extent_io.c:end_bio_extent_readpage)
5235 * still in progress (unlocked the pages in the bio but did not yet
5236 * unlocked the ranges in the io tree). Therefore this means some
Filipe Manana6ca07092015-05-26 00:55:42 +01005237 * ranges can still be locked and eviction started because before
5238 * submitting those bios, which are executed by a separate task (work
5239 * queue kthread), inode references (inode->i_count) were not taken
5240 * (which would be dropped in the end io callback of each bio).
5241 * Therefore here we effectively end up waiting for those bios and
5242 * anyone else holding locked ranges without having bumped the inode's
5243 * reference count - if we don't do it, when they access the inode's
5244 * io_tree to unlock a range it may be too late, leading to an
5245 * use-after-free issue.
5246 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005247 spin_lock(&io_tree->lock);
5248 while (!RB_EMPTY_ROOT(&io_tree->state)) {
5249 struct extent_state *state;
5250 struct extent_state *cached_state = NULL;
Filipe Manana6ca07092015-05-26 00:55:42 +01005251 u64 start;
5252 u64 end;
Filipe Manana421f0922018-10-12 13:02:48 +01005253 unsigned state_flags;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005254
5255 node = rb_first(&io_tree->state);
5256 state = rb_entry(node, struct extent_state, rb_node);
Filipe Manana6ca07092015-05-26 00:55:42 +01005257 start = state->start;
5258 end = state->end;
Filipe Manana421f0922018-10-12 13:02:48 +01005259 state_flags = state->state;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005260 spin_unlock(&io_tree->lock);
5261
David Sterbaff13db42015-12-03 14:30:40 +01005262 lock_extent_bits(io_tree, start, end, &cached_state);
Qu Wenruob9d0b382015-09-29 10:35:16 +08005263
5264 /*
5265 * If still has DELALLOC flag, the extent didn't reach disk,
5266 * and its reserved space won't be freed by delayed_ref.
5267 * So we need to free its reserved space here.
5268 * (Refer to comment in btrfs_invalidatepage, case 2)
5269 *
5270 * Note, end is the bytenr of last byte, so we need + 1 here.
5271 */
Filipe Manana421f0922018-10-12 13:02:48 +01005272 if (state_flags & EXTENT_DELALLOC)
Qu Wenruobc42bda2017-02-27 15:10:39 +08005273 btrfs_qgroup_free_data(inode, NULL, start, end - start + 1);
Qu Wenruob9d0b382015-09-29 10:35:16 +08005274
Filipe Manana6ca07092015-05-26 00:55:42 +01005275 clear_extent_bit(io_tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005276 EXTENT_LOCKED | EXTENT_DIRTY |
5277 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
David Sterbaae0f1622017-10-31 16:37:52 +01005278 EXTENT_DEFRAG, 1, 1, &cached_state);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005279
Filipe Manana7064dd52014-08-08 02:47:05 +01005280 cond_resched();
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005281 spin_lock(&io_tree->lock);
5282 }
5283 spin_unlock(&io_tree->lock);
5284}
5285
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005286static struct btrfs_trans_handle *evict_refill_and_join(struct btrfs_root *root,
Josef Bacikad80cf52018-09-28 07:18:19 -04005287 struct btrfs_block_rsv *rsv)
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005288{
5289 struct btrfs_fs_info *fs_info = root->fs_info;
5290 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
Josef Bacik260e7702018-11-21 14:03:13 -05005291 u64 delayed_refs_extra = btrfs_calc_trans_metadata_size(fs_info, 1);
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005292 int failures = 0;
5293
5294 for (;;) {
5295 struct btrfs_trans_handle *trans;
5296 int ret;
5297
Josef Bacik260e7702018-11-21 14:03:13 -05005298 ret = btrfs_block_rsv_refill(root, rsv,
5299 rsv->size + delayed_refs_extra,
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005300 BTRFS_RESERVE_FLUSH_LIMIT);
5301
5302 if (ret && ++failures > 2) {
5303 btrfs_warn(fs_info,
5304 "could not allocate space for a delete; will truncate on mount");
5305 return ERR_PTR(-ENOSPC);
5306 }
5307
Josef Bacik260e7702018-11-21 14:03:13 -05005308 /*
5309 * Evict can generate a large amount of delayed refs without
5310 * having a way to add space back since we exhaust our temporary
5311 * block rsv. We aren't allowed to do FLUSH_ALL in this case
5312 * because we could deadlock with so many things in the flushing
5313 * code, so we have to try and hold some extra space to
5314 * compensate for our delayed ref generation. If we can't get
5315 * that space then we need see if we can steal our minimum from
5316 * the global reserve. We will be ratelimited by the amount of
5317 * space we have for the delayed refs rsv, so we'll end up
5318 * committing and trying again.
5319 */
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005320 trans = btrfs_join_transaction(root);
Josef Bacik260e7702018-11-21 14:03:13 -05005321 if (IS_ERR(trans) || !ret) {
5322 if (!IS_ERR(trans)) {
5323 trans->block_rsv = &fs_info->trans_block_rsv;
5324 trans->bytes_reserved = delayed_refs_extra;
5325 btrfs_block_rsv_migrate(rsv, trans->block_rsv,
5326 delayed_refs_extra, 1);
5327 }
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005328 return trans;
Josef Bacik260e7702018-11-21 14:03:13 -05005329 }
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005330
5331 /*
5332 * Try to steal from the global reserve if there is space for
5333 * it.
5334 */
Josef Bacik644036122018-12-03 10:20:36 -05005335 if (!btrfs_check_space_for_delayed_refs(fs_info) &&
5336 !btrfs_block_rsv_migrate(global_rsv, rsv, rsv->size, 0))
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005337 return trans;
5338
5339 /* If not, commit and try again. */
5340 ret = btrfs_commit_transaction(trans);
5341 if (ret)
5342 return ERR_PTR(ret);
5343 }
5344}
5345
Al Virobd555972010-06-07 11:35:40 -04005346void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005347{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005348 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04005349 struct btrfs_trans_handle *trans;
5350 struct btrfs_root *root = BTRFS_I(inode)->root;
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005351 struct btrfs_block_rsv *rsv;
Chris Mason39279cc2007-06-12 06:35:45 -04005352 int ret;
5353
liubo1abe9b82011-03-24 11:18:59 +00005354 trace_btrfs_inode_evict(inode);
5355
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005356 if (!root) {
Liu Boe8f1bc12018-01-25 11:02:53 -07005357 clear_inode(inode);
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005358 return;
5359 }
5360
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005361 evict_inode_truncate_pages(inode);
5362
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005363 if (inode->i_nlink &&
5364 ((btrfs_root_refs(&root->root_item) != 0 &&
5365 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) ||
Nikolay Borisov70ddc552017-02-20 13:50:35 +02005366 btrfs_is_free_space_inode(BTRFS_I(inode))))
Al Virobd555972010-06-07 11:35:40 -04005367 goto no_delete;
5368
Omar Sandoval27919062018-05-11 13:13:37 -07005369 if (is_bad_inode(inode))
Chris Mason39279cc2007-06-12 06:35:45 -04005370 goto no_delete;
Chris Mason5f39d392007-10-15 16:14:19 -04005371
Nikolay Borisov7ab79562017-02-20 13:50:57 +02005372 btrfs_free_io_failure_record(BTRFS_I(inode), 0, (u64)-1);
Miao Xief6124962014-09-12 18:44:04 +08005373
Omar Sandoval7b40b692018-05-11 13:13:33 -07005374 if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags))
Yan, Zhengc71bf092009-11-12 09:34:40 +00005375 goto no_delete;
Yan, Zhengc71bf092009-11-12 09:34:40 +00005376
Yan, Zheng76dda932009-09-21 16:00:26 -04005377 if (inode->i_nlink > 0) {
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005378 BUG_ON(btrfs_root_refs(&root->root_item) != 0 &&
5379 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID);
Yan, Zheng76dda932009-09-21 16:00:26 -04005380 goto no_delete;
5381 }
5382
Nikolay Borisovaa790212017-01-10 20:35:40 +02005383 ret = btrfs_commit_inode_delayed_inode(BTRFS_I(inode));
Omar Sandoval27919062018-05-11 13:13:37 -07005384 if (ret)
Miao Xie0e8c36a2012-12-19 06:59:51 +00005385 goto no_delete;
Miao Xie0e8c36a2012-12-19 06:59:51 +00005386
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005387 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Omar Sandoval27919062018-05-11 13:13:37 -07005388 if (!rsv)
Josef Bacik4289a662011-08-05 13:22:24 -04005389 goto no_delete;
Josef Bacikad80cf52018-09-28 07:18:19 -04005390 rsv->size = btrfs_calc_trunc_metadata_size(fs_info, 1);
Josef Bacikca7e70f2012-08-27 17:48:15 -04005391 rsv->failfast = 1;
Josef Bacik4289a662011-08-05 13:22:24 -04005392
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02005393 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Mason5f39d392007-10-15 16:14:19 -04005394
Yan, Zheng80825102009-11-12 09:35:36 +00005395 while (1) {
Josef Bacikad80cf52018-09-28 07:18:19 -04005396 trans = evict_refill_and_join(root, rsv);
Omar Sandoval27919062018-05-11 13:13:37 -07005397 if (IS_ERR(trans))
5398 goto free_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04005399
5400 trans->block_rsv = rsv;
5401
Yan, Zhengd68fc572010-05-16 10:49:58 -04005402 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Omar Sandoval27919062018-05-11 13:13:37 -07005403 trans->block_rsv = &fs_info->trans_block_rsv;
5404 btrfs_end_transaction(trans);
5405 btrfs_btree_balance_dirty(fs_info);
5406 if (ret && ret != -ENOSPC && ret != -EAGAIN)
5407 goto free_rsv;
5408 else if (!ret)
Yan, Zheng80825102009-11-12 09:35:36 +00005409 break;
Josef Bacik7b128762008-07-24 12:17:14 -04005410 }
5411
Josef Bacik4ef31a42013-08-13 14:10:08 -04005412 /*
Omar Sandoval27919062018-05-11 13:13:37 -07005413 * Errors here aren't a big deal, it just means we leave orphan items in
5414 * the tree. They will be cleaned up on the next mount. If the inode
5415 * number gets reused, cleanup deletes the orphan item without doing
5416 * anything, and unlink reuses the existing orphan item.
5417 *
5418 * If it turns out that we are dropping too many of these, we might want
5419 * to add a mechanism for retrying these after a commit.
Josef Bacik4ef31a42013-08-13 14:10:08 -04005420 */
Josef Bacikad80cf52018-09-28 07:18:19 -04005421 trans = evict_refill_and_join(root, rsv);
Omar Sandoval27919062018-05-11 13:13:37 -07005422 if (!IS_ERR(trans)) {
5423 trans->block_rsv = rsv;
5424 btrfs_orphan_del(trans, BTRFS_I(inode));
5425 trans->block_rsv = &fs_info->trans_block_rsv;
5426 btrfs_end_transaction(trans);
5427 }
Chris Mason85e21ba2008-01-29 15:11:36 -05005428
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005429 if (!(root == fs_info->tree_root ||
Li Zefan581bb052011-04-20 10:06:11 +08005430 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005431 btrfs_return_ino(root, btrfs_ino(BTRFS_I(inode)));
Li Zefan581bb052011-04-20 10:06:11 +08005432
Omar Sandoval27919062018-05-11 13:13:37 -07005433free_rsv:
5434 btrfs_free_block_rsv(fs_info, rsv);
Chris Mason39279cc2007-06-12 06:35:45 -04005435no_delete:
Omar Sandoval27919062018-05-11 13:13:37 -07005436 /*
5437 * If we didn't successfully delete, the orphan item will still be in
5438 * the tree and we'll retry on the next mount. Again, we might also want
5439 * to retry these periodically in the future.
5440 */
Nikolay Borisovf48d1cf2017-01-10 20:35:39 +02005441 btrfs_remove_delayed_node(BTRFS_I(inode));
Jan Karadbd57682012-05-03 14:48:02 +02005442 clear_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005443}
5444
5445/*
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005446 * Return the key found in the dir entry in the location pointer, fill @type
5447 * with BTRFS_FT_*, and return 0.
5448 *
Su Yue005d6712018-03-05 17:13:37 +08005449 * If no dir entries were found, returns -ENOENT.
5450 * If found a corrupted location in dir entry, returns -EUCLEAN.
Chris Mason39279cc2007-06-12 06:35:45 -04005451 */
5452static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005453 struct btrfs_key *location, u8 *type)
Chris Mason39279cc2007-06-12 06:35:45 -04005454{
5455 const char *name = dentry->d_name.name;
5456 int namelen = dentry->d_name.len;
5457 struct btrfs_dir_item *di;
5458 struct btrfs_path *path;
5459 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04005460 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005461
5462 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005463 if (!path)
5464 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05005465
David Sterbaf85b7372017-01-20 14:54:07 +01005466 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(BTRFS_I(dir)),
5467 name, namelen, 0);
Liu Bo3cf50682018-09-12 06:06:26 +08005468 if (IS_ERR_OR_NULL(di)) {
5469 ret = di ? PTR_ERR(di) : -ENOENT;
Su Yue005d6712018-03-05 17:13:37 +08005470 goto out;
5471 }
Chris Masond3977122009-01-05 21:25:51 -05005472
Chris Mason5f39d392007-10-15 16:14:19 -04005473 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Liu Bo56a0e702017-10-30 11:14:38 -06005474 if (location->type != BTRFS_INODE_ITEM_KEY &&
5475 location->type != BTRFS_ROOT_ITEM_KEY) {
Su Yue005d6712018-03-05 17:13:37 +08005476 ret = -EUCLEAN;
Liu Bo56a0e702017-10-30 11:14:38 -06005477 btrfs_warn(root->fs_info,
5478"%s gets something invalid in DIR_ITEM (name %s, directory ino %llu, location(%llu %u %llu))",
5479 __func__, name, btrfs_ino(BTRFS_I(dir)),
5480 location->objectid, location->type, location->offset);
Liu Bo56a0e702017-10-30 11:14:38 -06005481 }
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005482 if (!ret)
5483 *type = btrfs_dir_type(path->nodes[0], di);
Chris Mason39279cc2007-06-12 06:35:45 -04005484out:
Chris Mason39279cc2007-06-12 06:35:45 -04005485 btrfs_free_path(path);
5486 return ret;
5487}
5488
5489/*
5490 * when we hit a tree root in a directory, the btrfs part of the inode
5491 * needs to be changed to reflect the root directory of the tree root. This
5492 * is kind of like crossing a mount point.
5493 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005494static int fixup_tree_root_location(struct btrfs_fs_info *fs_info,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005495 struct inode *dir,
5496 struct dentry *dentry,
5497 struct btrfs_key *location,
5498 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04005499{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005500 struct btrfs_path *path;
5501 struct btrfs_root *new_root;
5502 struct btrfs_root_ref *ref;
5503 struct extent_buffer *leaf;
David Sterba1d4c08e2015-01-02 19:36:14 +01005504 struct btrfs_key key;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005505 int ret;
5506 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005507
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005508 path = btrfs_alloc_path();
5509 if (!path) {
5510 err = -ENOMEM;
5511 goto out;
5512 }
Chris Mason39279cc2007-06-12 06:35:45 -04005513
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005514 err = -ENOENT;
David Sterba1d4c08e2015-01-02 19:36:14 +01005515 key.objectid = BTRFS_I(dir)->root->root_key.objectid;
5516 key.type = BTRFS_ROOT_REF_KEY;
5517 key.offset = location->objectid;
5518
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005519 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005520 if (ret) {
5521 if (ret < 0)
5522 err = ret;
5523 goto out;
5524 }
Chris Mason39279cc2007-06-12 06:35:45 -04005525
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005526 leaf = path->nodes[0];
5527 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005528 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(BTRFS_I(dir)) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005529 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
5530 goto out;
5531
5532 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
5533 (unsigned long)(ref + 1),
5534 dentry->d_name.len);
5535 if (ret)
5536 goto out;
5537
David Sterbab3b4aa72011-04-21 01:20:15 +02005538 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005539
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005540 new_root = btrfs_read_fs_root_no_name(fs_info, location);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005541 if (IS_ERR(new_root)) {
5542 err = PTR_ERR(new_root);
5543 goto out;
5544 }
5545
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005546 *sub_root = new_root;
5547 location->objectid = btrfs_root_dirid(&new_root->root_item);
5548 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04005549 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005550 err = 0;
5551out:
5552 btrfs_free_path(path);
5553 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005554}
5555
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005556static void inode_tree_add(struct inode *inode)
5557{
5558 struct btrfs_root *root = BTRFS_I(inode)->root;
5559 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005560 struct rb_node **p;
5561 struct rb_node *parent;
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005562 struct rb_node *new = &BTRFS_I(inode)->rb_node;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005563 u64 ino = btrfs_ino(BTRFS_I(inode));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005564
Al Viro1d3382cb2010-10-23 15:19:20 -04005565 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005566 return;
Miao Xiee1409ce2013-05-15 07:48:16 +00005567 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005568 spin_lock(&root->inode_lock);
Miao Xiee1409ce2013-05-15 07:48:16 +00005569 p = &root->inode_tree.rb_node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005570 while (*p) {
5571 parent = *p;
5572 entry = rb_entry(parent, struct btrfs_inode, rb_node);
5573
David Sterba37508512018-06-29 10:56:40 +02005574 if (ino < btrfs_ino(entry))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005575 p = &parent->rb_left;
David Sterba37508512018-06-29 10:56:40 +02005576 else if (ino > btrfs_ino(entry))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005577 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005578 else {
5579 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04005580 (I_WILL_FREE | I_FREEING)));
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005581 rb_replace_node(parent, new, &root->inode_tree);
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005582 RB_CLEAR_NODE(parent);
5583 spin_unlock(&root->inode_lock);
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005584 return;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005585 }
5586 }
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005587 rb_link_node(new, parent, p);
5588 rb_insert_color(new, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005589 spin_unlock(&root->inode_lock);
5590}
5591
5592static void inode_tree_del(struct inode *inode)
5593{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005594 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005595 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04005596 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005597
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005598 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005599 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005600 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005601 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04005602 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005603 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005604 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04005605
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005606 if (empty && btrfs_root_refs(&root->root_item) == 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005607 synchronize_srcu(&fs_info->subvol_srcu);
Yan, Zheng76dda932009-09-21 16:00:26 -04005608 spin_lock(&root->inode_lock);
5609 empty = RB_EMPTY_ROOT(&root->inode_tree);
5610 spin_unlock(&root->inode_lock);
5611 if (empty)
5612 btrfs_add_dead_root(root);
5613 }
5614}
5615
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005616
Chris Masone02119d2008-09-05 16:13:11 -04005617static int btrfs_init_locked_inode(struct inode *inode, void *p)
5618{
5619 struct btrfs_iget_args *args = p;
Chris Mason90d3e592014-01-09 17:28:00 -08005620 inode->i_ino = args->location->objectid;
5621 memcpy(&BTRFS_I(inode)->location, args->location,
5622 sizeof(*args->location));
Chris Masone02119d2008-09-05 16:13:11 -04005623 BTRFS_I(inode)->root = args->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005624 return 0;
5625}
5626
5627static int btrfs_find_actor(struct inode *inode, void *opaque)
5628{
5629 struct btrfs_iget_args *args = opaque;
Chris Mason90d3e592014-01-09 17:28:00 -08005630 return args->location->objectid == BTRFS_I(inode)->location.objectid &&
Chris Masond3977122009-01-05 21:25:51 -05005631 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005632}
5633
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005634static struct inode *btrfs_iget_locked(struct super_block *s,
Chris Mason90d3e592014-01-09 17:28:00 -08005635 struct btrfs_key *location,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005636 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04005637{
5638 struct inode *inode;
5639 struct btrfs_iget_args args;
Chris Mason90d3e592014-01-09 17:28:00 -08005640 unsigned long hashval = btrfs_inode_hash(location->objectid, root);
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005641
Chris Mason90d3e592014-01-09 17:28:00 -08005642 args.location = location;
Chris Mason39279cc2007-06-12 06:35:45 -04005643 args.root = root;
5644
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005645 inode = iget5_locked(s, hashval, btrfs_find_actor,
Chris Mason39279cc2007-06-12 06:35:45 -04005646 btrfs_init_locked_inode,
5647 (void *)&args);
5648 return inode;
5649}
5650
Balaji Rao1a54ef82008-07-21 02:01:04 +05305651/* Get an inode object given its location and corresponding root.
5652 * Returns in *is_new if the inode was read from disk
5653 */
Filipe Manana4222ea72018-10-24 10:13:03 +01005654struct inode *btrfs_iget_path(struct super_block *s, struct btrfs_key *location,
5655 struct btrfs_root *root, int *new,
5656 struct btrfs_path *path)
Balaji Rao1a54ef82008-07-21 02:01:04 +05305657{
5658 struct inode *inode;
5659
Chris Mason90d3e592014-01-09 17:28:00 -08005660 inode = btrfs_iget_locked(s, location, root);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305661 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005662 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305663
5664 if (inode->i_state & I_NEW) {
Filipe Manana67710892016-06-06 11:51:25 +01005665 int ret;
5666
Filipe Manana4222ea72018-10-24 10:13:03 +01005667 ret = btrfs_read_locked_inode(inode, path);
Al Viro9bc2cef2018-07-29 23:04:50 +01005668 if (!ret) {
Mark Fasheh1748f842011-07-12 11:25:31 -07005669 inode_tree_add(inode);
5670 unlock_new_inode(inode);
5671 if (new)
5672 *new = 1;
5673 } else {
Al Virof5b3a412018-07-29 23:04:51 +01005674 iget_failed(inode);
5675 /*
5676 * ret > 0 can come from btrfs_search_slot called by
5677 * btrfs_read_locked_inode, this means the inode item
5678 * was not found.
5679 */
5680 if (ret > 0)
5681 ret = -ENOENT;
5682 inode = ERR_PTR(ret);
Mark Fasheh1748f842011-07-12 11:25:31 -07005683 }
5684 }
5685
Balaji Rao1a54ef82008-07-21 02:01:04 +05305686 return inode;
5687}
5688
Filipe Manana4222ea72018-10-24 10:13:03 +01005689struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
5690 struct btrfs_root *root, int *new)
5691{
5692 return btrfs_iget_path(s, location, root, new, NULL);
5693}
5694
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005695static struct inode *new_simple_dir(struct super_block *s,
5696 struct btrfs_key *key,
5697 struct btrfs_root *root)
5698{
5699 struct inode *inode = new_inode(s);
5700
5701 if (!inode)
5702 return ERR_PTR(-ENOMEM);
5703
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005704 BTRFS_I(inode)->root = root;
5705 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04005706 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005707
5708 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Li Zefan848cce02012-02-21 17:04:28 +08005709 inode->i_op = &btrfs_dir_ro_inode_operations;
Omar Sandoval1fdf4192017-01-25 17:06:39 -08005710 inode->i_opflags &= ~IOP_XATTR;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005711 inode->i_fop = &simple_dir_operations;
5712 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
Deepa Dinamanic2050a42016-09-14 07:48:06 -07005713 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05305714 inode->i_atime = inode->i_mtime;
5715 inode->i_ctime = inode->i_mtime;
Arnd Bergmannd3c6be6f2018-06-21 18:04:06 +02005716 BTRFS_I(inode)->i_otime = inode->i_mtime;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005717
5718 return inode;
5719}
5720
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005721static inline u8 btrfs_inode_type(struct inode *inode)
5722{
5723 /*
5724 * Compile-time asserts that generic FT_* types still match
5725 * BTRFS_FT_* types
5726 */
5727 BUILD_BUG_ON(BTRFS_FT_UNKNOWN != FT_UNKNOWN);
5728 BUILD_BUG_ON(BTRFS_FT_REG_FILE != FT_REG_FILE);
5729 BUILD_BUG_ON(BTRFS_FT_DIR != FT_DIR);
5730 BUILD_BUG_ON(BTRFS_FT_CHRDEV != FT_CHRDEV);
5731 BUILD_BUG_ON(BTRFS_FT_BLKDEV != FT_BLKDEV);
5732 BUILD_BUG_ON(BTRFS_FT_FIFO != FT_FIFO);
5733 BUILD_BUG_ON(BTRFS_FT_SOCK != FT_SOCK);
5734 BUILD_BUG_ON(BTRFS_FT_SYMLINK != FT_SYMLINK);
5735
5736 return fs_umode_to_ftype(inode->i_mode);
5737}
5738
Chris Mason3de45862008-11-17 21:02:50 -05005739struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04005740{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005741 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masond3977122009-01-05 21:25:51 -05005742 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005743 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005744 struct btrfs_root *sub_root = root;
5745 struct btrfs_key location;
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005746 u8 di_type = 0;
Yan, Zheng76dda932009-09-21 16:00:26 -04005747 int index;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005748 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005749
5750 if (dentry->d_name.len > BTRFS_NAME_LEN)
5751 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04005752
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005753 ret = btrfs_inode_by_name(dir, dentry, &location, &di_type);
Chris Mason39279cc2007-06-12 06:35:45 -04005754 if (ret < 0)
5755 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04005756
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005757 if (location.type == BTRFS_INODE_ITEM_KEY) {
Josef Bacik73f73412009-12-04 17:38:27 +00005758 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005759 if (IS_ERR(inode))
5760 return inode;
5761
5762 /* Do extra check against inode mode with di_type */
5763 if (btrfs_inode_type(inode) != di_type) {
5764 btrfs_crit(fs_info,
5765"inode mode mismatch with dir: inode mode=0%o btrfs type=%u dir type=%u",
5766 inode->i_mode, btrfs_inode_type(inode),
5767 di_type);
5768 iput(inode);
5769 return ERR_PTR(-EUCLEAN);
5770 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005771 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04005772 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005773
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005774 index = srcu_read_lock(&fs_info->subvol_srcu);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005775 ret = fixup_tree_root_location(fs_info, dir, dentry,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005776 &location, &sub_root);
5777 if (ret < 0) {
5778 if (ret != -ENOENT)
5779 inode = ERR_PTR(ret);
5780 else
5781 inode = new_simple_dir(dir->i_sb, &location, sub_root);
5782 } else {
Josef Bacik73f73412009-12-04 17:38:27 +00005783 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04005784 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005785 srcu_read_unlock(&fs_info->subvol_srcu, index);
Yan, Zheng76dda932009-09-21 16:00:26 -04005786
Julia Lawall34d19ba2011-01-24 19:55:19 +00005787 if (!IS_ERR(inode) && root != sub_root) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005788 down_read(&fs_info->cleanup_work_sem);
David Howellsbc98a422017-07-17 08:45:34 +01005789 if (!sb_rdonly(inode->i_sb))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005790 ret = btrfs_orphan_cleanup(sub_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005791 up_read(&fs_info->cleanup_work_sem);
Josef Bacik01cd3362013-06-03 21:39:49 -04005792 if (ret) {
5793 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005794 inode = ERR_PTR(ret);
Josef Bacik01cd3362013-06-03 21:39:49 -04005795 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00005796 }
5797
Chris Mason3de45862008-11-17 21:02:50 -05005798 return inode;
5799}
5800
Nick Pigginfe15ce42011-01-07 17:49:23 +11005801static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04005802{
5803 struct btrfs_root *root;
David Howells2b0143b2015-03-17 22:25:59 +00005804 struct inode *inode = d_inode(dentry);
Yan, Zheng76dda932009-09-21 16:00:26 -04005805
Li Zefan848cce02012-02-21 17:04:28 +08005806 if (!inode && !IS_ROOT(dentry))
David Howells2b0143b2015-03-17 22:25:59 +00005807 inode = d_inode(dentry->d_parent);
Yan, Zheng76dda932009-09-21 16:00:26 -04005808
Li Zefan848cce02012-02-21 17:04:28 +08005809 if (inode) {
5810 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04005811 if (btrfs_root_refs(&root->root_item) == 0)
5812 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08005813
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005814 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Li Zefan848cce02012-02-21 17:04:28 +08005815 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04005816 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005817 return 0;
5818}
5819
Chris Mason3de45862008-11-17 21:02:50 -05005820static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04005821 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05005822{
Al Viro3837d202018-10-10 16:38:27 -04005823 struct inode *inode = btrfs_lookup_dentry(dir, dentry);
Josef Bacika66e7cc2011-09-18 10:34:03 -04005824
Al Viro3837d202018-10-10 16:38:27 -04005825 if (inode == ERR_PTR(-ENOENT))
5826 inode = NULL;
Al Viro41d28bc2014-10-12 22:24:21 -04005827 return d_splice_alias(inode, dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04005828}
5829
Josef Bacik23b5ec72017-07-24 15:14:25 -04005830/*
5831 * All this infrastructure exists because dir_emit can fault, and we are holding
5832 * the tree lock when doing readdir. For now just allocate a buffer and copy
5833 * our information into that, and then dir_emit from the buffer. This is
5834 * similar to what NFS does, only we don't keep the buffer around in pagecache
5835 * because I'm afraid I'll mess that up. Long term we need to make filldir do
5836 * copy_to_user_inatomic so we don't have to worry about page faulting under the
5837 * tree lock.
5838 */
5839static int btrfs_opendir(struct inode *inode, struct file *file)
5840{
5841 struct btrfs_file_private *private;
5842
5843 private = kzalloc(sizeof(struct btrfs_file_private), GFP_KERNEL);
5844 if (!private)
5845 return -ENOMEM;
5846 private->filldir_buf = kzalloc(PAGE_SIZE, GFP_KERNEL);
5847 if (!private->filldir_buf) {
5848 kfree(private);
5849 return -ENOMEM;
5850 }
5851 file->private_data = private;
5852 return 0;
5853}
5854
5855struct dir_entry {
5856 u64 ino;
5857 u64 offset;
5858 unsigned type;
5859 int name_len;
5860};
5861
5862static int btrfs_filldir(void *addr, int entries, struct dir_context *ctx)
5863{
5864 while (entries--) {
5865 struct dir_entry *entry = addr;
5866 char *name = (char *)(entry + 1);
5867
David Sterba92d32172018-04-16 21:10:14 +02005868 ctx->pos = get_unaligned(&entry->offset);
5869 if (!dir_emit(ctx, name, get_unaligned(&entry->name_len),
5870 get_unaligned(&entry->ino),
5871 get_unaligned(&entry->type)))
Josef Bacik23b5ec72017-07-24 15:14:25 -04005872 return 1;
David Sterba92d32172018-04-16 21:10:14 +02005873 addr += sizeof(struct dir_entry) +
5874 get_unaligned(&entry->name_len);
Josef Bacik23b5ec72017-07-24 15:14:25 -04005875 ctx->pos++;
5876 }
5877 return 0;
5878}
5879
Al Viro9cdda8d2013-05-22 16:48:09 -04005880static int btrfs_real_readdir(struct file *file, struct dir_context *ctx)
Chris Mason39279cc2007-06-12 06:35:45 -04005881{
Al Viro9cdda8d2013-05-22 16:48:09 -04005882 struct inode *inode = file_inode(file);
Chris Mason39279cc2007-06-12 06:35:45 -04005883 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005884 struct btrfs_file_private *private = file->private_data;
Chris Mason39279cc2007-06-12 06:35:45 -04005885 struct btrfs_dir_item *di;
5886 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04005887 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04005888 struct btrfs_path *path;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005889 void *addr;
Miao Xie16cdcec2011-04-22 18:12:22 +08005890 struct list_head ins_list;
5891 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04005892 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04005893 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04005894 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04005895 char *name_ptr;
5896 int name_len;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005897 int entries = 0;
5898 int total_len = 0;
Omar Sandoval02dbfc92016-05-20 13:50:33 -07005899 bool put = false;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005900 struct btrfs_key location;
Chris Mason5f39d392007-10-15 16:14:19 -04005901
Al Viro9cdda8d2013-05-22 16:48:09 -04005902 if (!dir_emit_dots(file, ctx))
5903 return 0;
5904
David Woodhouse49593bf2008-08-17 17:08:36 +01005905 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08005906 if (!path)
5907 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04005908
Josef Bacik23b5ec72017-07-24 15:14:25 -04005909 addr = private->filldir_buf;
David Sterbae4058b52015-11-27 16:31:35 +01005910 path->reada = READA_FORWARD;
David Woodhouse49593bf2008-08-17 17:08:36 +01005911
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005912 INIT_LIST_HEAD(&ins_list);
5913 INIT_LIST_HEAD(&del_list);
5914 put = btrfs_readdir_get_delayed_items(inode, &ins_list, &del_list);
Miao Xie16cdcec2011-04-22 18:12:22 +08005915
Josef Bacik23b5ec72017-07-24 15:14:25 -04005916again:
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005917 key.type = BTRFS_DIR_INDEX_KEY;
Al Viro9cdda8d2013-05-22 16:48:09 -04005918 key.offset = ctx->pos;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005919 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason5f39d392007-10-15 16:14:19 -04005920
Chris Mason39279cc2007-06-12 06:35:45 -04005921 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5922 if (ret < 0)
5923 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01005924
5925 while (1) {
Josef Bacik23b5ec72017-07-24 15:14:25 -04005926 struct dir_entry *entry;
5927
Chris Mason5f39d392007-10-15 16:14:19 -04005928 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04005929 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08005930 if (slot >= btrfs_header_nritems(leaf)) {
5931 ret = btrfs_next_leaf(root, path);
5932 if (ret < 0)
5933 goto err;
5934 else if (ret > 0)
5935 break;
5936 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04005937 }
Chris Mason3de45862008-11-17 21:02:50 -05005938
Chris Mason5f39d392007-10-15 16:14:19 -04005939 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5940
5941 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04005942 break;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005943 if (found_key.type != BTRFS_DIR_INDEX_KEY)
Chris Mason39279cc2007-06-12 06:35:45 -04005944 break;
Al Viro9cdda8d2013-05-22 16:48:09 -04005945 if (found_key.offset < ctx->pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08005946 goto next;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005947 if (btrfs_should_delete_dir_index(&del_list, found_key.offset))
Miao Xie16cdcec2011-04-22 18:12:22 +08005948 goto next;
Chris Mason39279cc2007-06-12 06:35:45 -04005949 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005950 name_len = btrfs_dir_name_len(leaf, di);
Josef Bacik23b5ec72017-07-24 15:14:25 -04005951 if ((total_len + sizeof(struct dir_entry) + name_len) >=
5952 PAGE_SIZE) {
5953 btrfs_release_path(path);
5954 ret = btrfs_filldir(private->filldir_buf, entries, ctx);
5955 if (ret)
5956 goto nopos;
5957 addr = private->filldir_buf;
5958 entries = 0;
5959 total_len = 0;
5960 goto again;
Chris Mason39279cc2007-06-12 06:35:45 -04005961 }
Josef Bacik23b5ec72017-07-24 15:14:25 -04005962
5963 entry = addr;
David Sterba92d32172018-04-16 21:10:14 +02005964 put_unaligned(name_len, &entry->name_len);
Josef Bacik23b5ec72017-07-24 15:14:25 -04005965 name_ptr = (char *)(entry + 1);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005966 read_extent_buffer(leaf, name_ptr, (unsigned long)(di + 1),
5967 name_len);
Phillip Potter7d157c32019-03-26 21:39:34 +00005968 put_unaligned(fs_ftype_to_dtype(btrfs_dir_type(leaf, di)),
David Sterba92d32172018-04-16 21:10:14 +02005969 &entry->type);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005970 btrfs_dir_item_key_to_cpu(leaf, di, &location);
David Sterba92d32172018-04-16 21:10:14 +02005971 put_unaligned(location.objectid, &entry->ino);
5972 put_unaligned(found_key.offset, &entry->offset);
Josef Bacik23b5ec72017-07-24 15:14:25 -04005973 entries++;
5974 addr += sizeof(struct dir_entry) + name_len;
5975 total_len += sizeof(struct dir_entry) + name_len;
Li Zefanb9e03af2011-03-23 10:43:58 +08005976next:
5977 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04005978 }
Josef Bacik23b5ec72017-07-24 15:14:25 -04005979 btrfs_release_path(path);
5980
5981 ret = btrfs_filldir(private->filldir_buf, entries, ctx);
5982 if (ret)
5983 goto nopos;
David Woodhouse49593bf2008-08-17 17:08:36 +01005984
Jeff Mahoneyd2fbb2b2016-11-05 13:26:35 -04005985 ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005986 if (ret)
David Sterbabc4ef752015-11-13 13:44:28 +01005987 goto nopos;
5988
Zach Browndb62efb2013-07-11 16:19:42 -07005989 /*
5990 * Stop new entries from being returned after we return the last
5991 * entry.
5992 *
5993 * New directory entries are assigned a strictly increasing
5994 * offset. This means that new entries created during readdir
5995 * are *guaranteed* to be seen in the future by that readdir.
5996 * This has broken buggy programs which operate on names as
5997 * they're returned by readdir. Until we re-use freed offsets
5998 * we have this hack to stop new entries from being returned
5999 * under the assumption that they'll never reach this huge
6000 * offset.
6001 *
6002 * This is being careful not to overflow 32bit loff_t unless the
6003 * last entry requires it because doing so has broken 32bit apps
6004 * in the past.
6005 */
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006006 if (ctx->pos >= INT_MAX)
6007 ctx->pos = LLONG_MAX;
6008 else
6009 ctx->pos = INT_MAX;
Chris Mason39279cc2007-06-12 06:35:45 -04006010nopos:
6011 ret = 0;
6012err:
Omar Sandoval02dbfc92016-05-20 13:50:33 -07006013 if (put)
6014 btrfs_readdir_put_delayed_items(inode, &ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04006015 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04006016 return ret;
6017}
6018
Chris Mason39279cc2007-06-12 06:35:45 -04006019/*
Chris Mason54aa1f42007-06-22 14:16:25 -04006020 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04006021 * inode changes. But, it is most likely to find the inode in cache.
6022 * FIXME, needs more benchmarking...there are no reasons other than performance
6023 * to keep or drop this code.
6024 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00006025static int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04006026{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006027 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04006028 struct btrfs_root *root = BTRFS_I(inode)->root;
6029 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006030 int ret;
6031
Josef Bacik72ac3c02012-05-23 14:13:11 -04006032 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05006033 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006034
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006035 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006036 if (IS_ERR(trans))
6037 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006038
6039 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04006040 if (ret && ret == -ENOSPC) {
6041 /* whoops, lets try again with the full transaction */
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006042 btrfs_end_transaction(trans);
Chris Mason94b60442010-05-26 11:02:00 -04006043 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006044 if (IS_ERR(trans))
6045 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006046
Chris Mason94b60442010-05-26 11:02:00 -04006047 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04006048 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006049 btrfs_end_transaction(trans);
Miao Xie16cdcec2011-04-22 18:12:22 +08006050 if (BTRFS_I(inode)->delayed_node)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006051 btrfs_balance_delayed_items(fs_info);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006052
6053 return ret;
6054}
6055
6056/*
6057 * This is a copy of file_update_time. We need this so we can return error on
6058 * ENOSPC for updating the inode in the case of file write and mmap writes.
6059 */
Deepa Dinamani95582b02018-05-08 19:36:02 -07006060static int btrfs_update_time(struct inode *inode, struct timespec64 *now,
Josef Bacike41f9412012-03-26 09:46:47 -04006061 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05006062{
Alexander Block2bc5565282012-06-15 09:49:33 +02006063 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Layton3a8c7232017-12-11 06:35:24 -05006064 bool dirty = flags & ~S_VERSION;
Alexander Block2bc5565282012-06-15 09:49:33 +02006065
6066 if (btrfs_root_readonly(root))
6067 return -EROFS;
6068
Josef Bacike41f9412012-03-26 09:46:47 -04006069 if (flags & S_VERSION)
Jeff Layton3a8c7232017-12-11 06:35:24 -05006070 dirty |= inode_maybe_inc_iversion(inode, dirty);
Josef Bacike41f9412012-03-26 09:46:47 -04006071 if (flags & S_CTIME)
6072 inode->i_ctime = *now;
6073 if (flags & S_MTIME)
6074 inode->i_mtime = *now;
6075 if (flags & S_ATIME)
6076 inode->i_atime = *now;
Jeff Layton3a8c7232017-12-11 06:35:24 -05006077 return dirty ? btrfs_dirty_inode(inode) : 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006078}
6079
Chris Masond352ac62008-09-29 15:18:18 -04006080/*
6081 * find the highest existing sequence number in a directory
6082 * and then set the in-memory index_cnt variable to reflect
6083 * free sequence numbers
6084 */
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006085static int btrfs_set_inode_index_count(struct btrfs_inode *inode)
Josef Bacikaec74772008-07-24 12:12:38 -04006086{
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006087 struct btrfs_root *root = inode->root;
Josef Bacikaec74772008-07-24 12:12:38 -04006088 struct btrfs_key key, found_key;
6089 struct btrfs_path *path;
6090 struct extent_buffer *leaf;
6091 int ret;
6092
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006093 key.objectid = btrfs_ino(inode);
David Sterba962a2982014-06-04 18:41:45 +02006094 key.type = BTRFS_DIR_INDEX_KEY;
Josef Bacikaec74772008-07-24 12:12:38 -04006095 key.offset = (u64)-1;
6096
6097 path = btrfs_alloc_path();
6098 if (!path)
6099 return -ENOMEM;
6100
6101 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
6102 if (ret < 0)
6103 goto out;
6104 /* FIXME: we should be able to handle this */
6105 if (ret == 0)
6106 goto out;
6107 ret = 0;
6108
6109 /*
6110 * MAGIC NUMBER EXPLANATION:
6111 * since we search a directory based on f_pos we have to start at 2
6112 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
6113 * else has to start at 2
6114 */
6115 if (path->slots[0] == 0) {
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006116 inode->index_cnt = 2;
Josef Bacikaec74772008-07-24 12:12:38 -04006117 goto out;
6118 }
6119
6120 path->slots[0]--;
6121
6122 leaf = path->nodes[0];
6123 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6124
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006125 if (found_key.objectid != btrfs_ino(inode) ||
David Sterba962a2982014-06-04 18:41:45 +02006126 found_key.type != BTRFS_DIR_INDEX_KEY) {
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006127 inode->index_cnt = 2;
Josef Bacikaec74772008-07-24 12:12:38 -04006128 goto out;
6129 }
6130
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006131 inode->index_cnt = found_key.offset + 1;
Josef Bacikaec74772008-07-24 12:12:38 -04006132out:
6133 btrfs_free_path(path);
6134 return ret;
6135}
6136
Chris Masond352ac62008-09-29 15:18:18 -04006137/*
6138 * helper to find a free sequence number in a given directory. This current
6139 * code is very simple, later versions will do smarter things in the btree
6140 */
Nikolay Borisov877574e2017-02-20 13:50:33 +02006141int btrfs_set_inode_index(struct btrfs_inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04006142{
6143 int ret = 0;
6144
Nikolay Borisov877574e2017-02-20 13:50:33 +02006145 if (dir->index_cnt == (u64)-1) {
6146 ret = btrfs_inode_delayed_dir_index_count(dir);
Miao Xie16cdcec2011-04-22 18:12:22 +08006147 if (ret) {
6148 ret = btrfs_set_inode_index_count(dir);
6149 if (ret)
6150 return ret;
6151 }
Josef Bacikaec74772008-07-24 12:12:38 -04006152 }
6153
Nikolay Borisov877574e2017-02-20 13:50:33 +02006154 *index = dir->index_cnt;
6155 dir->index_cnt++;
Josef Bacikaec74772008-07-24 12:12:38 -04006156
6157 return ret;
6158}
6159
Chris Masonb0d5d102014-09-08 13:08:51 -07006160static int btrfs_insert_inode_locked(struct inode *inode)
6161{
6162 struct btrfs_iget_args args;
6163 args.location = &BTRFS_I(inode)->location;
6164 args.root = BTRFS_I(inode)->root;
6165
6166 return insert_inode_locked4(inode,
6167 btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root),
6168 btrfs_find_actor, &args);
6169}
6170
Anand Jain19aee8d2017-07-18 17:37:05 +08006171/*
6172 * Inherit flags from the parent inode.
6173 *
6174 * Currently only the compression flags and the cow flags are inherited.
6175 */
6176static void btrfs_inherit_iflags(struct inode *inode, struct inode *dir)
6177{
6178 unsigned int flags;
6179
6180 if (!dir)
6181 return;
6182
6183 flags = BTRFS_I(dir)->flags;
6184
6185 if (flags & BTRFS_INODE_NOCOMPRESS) {
6186 BTRFS_I(inode)->flags &= ~BTRFS_INODE_COMPRESS;
6187 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
6188 } else if (flags & BTRFS_INODE_COMPRESS) {
6189 BTRFS_I(inode)->flags &= ~BTRFS_INODE_NOCOMPRESS;
6190 BTRFS_I(inode)->flags |= BTRFS_INODE_COMPRESS;
6191 }
6192
6193 if (flags & BTRFS_INODE_NODATACOW) {
6194 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
6195 if (S_ISREG(inode->i_mode))
6196 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
6197 }
6198
David Sterba7b6a2212018-03-26 18:40:21 +02006199 btrfs_sync_inode_flags_to_i_flags(inode);
Anand Jain19aee8d2017-07-18 17:37:05 +08006200}
6201
Chris Mason39279cc2007-06-12 06:35:45 -04006202static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
6203 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04006204 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05006205 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04006206 u64 ref_objectid, u64 objectid,
6207 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04006208{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006209 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04006210 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006211 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04006212 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04006213 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05006214 struct btrfs_inode_ref *ref;
6215 struct btrfs_key key[2];
6216 u32 sizes[2];
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006217 int nitems = name ? 2 : 1;
Chris Mason9c583092008-01-29 15:15:18 -05006218 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04006219 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006220
Chris Mason5f39d392007-10-15 16:14:19 -04006221 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07006222 if (!path)
6223 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04006224
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006225 inode = new_inode(fs_info->sb);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006226 if (!inode) {
6227 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04006228 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006229 }
Chris Mason39279cc2007-06-12 06:35:45 -04006230
Li Zefan581bb052011-04-20 10:06:11 +08006231 /*
Filipe Manana5762b5c2014-08-01 00:10:32 +01006232 * O_TMPFILE, set link count to 0, so that after this point,
6233 * we fill in an inode item with the correct link count.
6234 */
6235 if (!name)
6236 set_nlink(inode, 0);
6237
6238 /*
Li Zefan581bb052011-04-20 10:06:11 +08006239 * we have to initialize this early, so we can reclaim the inode
6240 * number if we fail afterwards in this function.
6241 */
6242 inode->i_ino = objectid;
6243
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006244 if (dir && name) {
liubo1abe9b82011-03-24 11:18:59 +00006245 trace_btrfs_inode_request(dir);
6246
Nikolay Borisov877574e2017-02-20 13:50:33 +02006247 ret = btrfs_set_inode_index(BTRFS_I(dir), index);
Shen Feng09771432009-04-02 16:46:06 -04006248 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006249 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04006250 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04006251 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04006252 }
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006253 } else if (dir) {
6254 *index = 0;
Josef Bacikaec74772008-07-24 12:12:38 -04006255 }
6256 /*
6257 * index_cnt is ignored for everything but a dir,
Su Yuedf6703e2018-01-12 11:08:02 +08006258 * btrfs_set_inode_index_count has an explanation for the magic
Josef Bacikaec74772008-07-24 12:12:38 -04006259 * number
6260 */
6261 BTRFS_I(inode)->index_cnt = 2;
Miao Xie67de1172013-12-26 13:07:06 +08006262 BTRFS_I(inode)->dir_index = *index;
Chris Mason39279cc2007-06-12 06:35:45 -04006263 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04006264 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00006265 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db22007-08-27 16:49:44 -04006266
Josef Bacik5dc562c2012-08-17 13:14:17 -04006267 /*
6268 * We could have gotten an inode number from somebody who was fsynced
6269 * and then removed in this same transaction, so let's just set full
6270 * sync since it will be a full sync anyway and this will blow away the
6271 * old info in the log.
6272 */
6273 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
6274
Chris Mason9c583092008-01-29 15:15:18 -05006275 key[0].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006276 key[0].type = BTRFS_INODE_ITEM_KEY;
Chris Mason9c583092008-01-29 15:15:18 -05006277 key[0].offset = 0;
6278
Chris Mason9c583092008-01-29 15:15:18 -05006279 sizes[0] = sizeof(struct btrfs_inode_item);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006280
6281 if (name) {
6282 /*
6283 * Start new inodes with an inode_ref. This is slightly more
6284 * efficient for small numbers of hard links since they will
6285 * be packed into one item. Extended refs will kick in if we
6286 * add more hard links than can fit in the ref item.
6287 */
6288 key[1].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006289 key[1].type = BTRFS_INODE_REF_KEY;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006290 key[1].offset = ref_objectid;
6291
6292 sizes[1] = name_len + sizeof(*ref);
6293 }
Chris Mason9c583092008-01-29 15:15:18 -05006294
Chris Masonb0d5d102014-09-08 13:08:51 -07006295 location = &BTRFS_I(inode)->location;
6296 location->objectid = objectid;
6297 location->offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02006298 location->type = BTRFS_INODE_ITEM_KEY;
Chris Masonb0d5d102014-09-08 13:08:51 -07006299
6300 ret = btrfs_insert_inode_locked(inode);
Al Viro32955c52018-05-16 12:20:05 -04006301 if (ret < 0) {
6302 iput(inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006303 goto fail;
Al Viro32955c52018-05-16 12:20:05 -04006304 }
Chris Masonb0d5d102014-09-08 13:08:51 -07006305
Chris Masonb9473432009-03-13 11:00:37 -04006306 path->leave_spinning = 1;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006307 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems);
Chris Mason9c583092008-01-29 15:15:18 -05006308 if (ret != 0)
Chris Masonb0d5d102014-09-08 13:08:51 -07006309 goto fail_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04006310
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03006311 inode_init_owner(inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04006312 inode_set_bytes(inode, 0);
chandan r9cc97d62012-07-04 12:48:07 +05306313
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006314 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05306315 inode->i_atime = inode->i_mtime;
6316 inode->i_ctime = inode->i_mtime;
Arnd Bergmannd3c6be6f2018-06-21 18:04:06 +02006317 BTRFS_I(inode)->i_otime = inode->i_mtime;
chandan r9cc97d62012-07-04 12:48:07 +05306318
Chris Mason5f39d392007-10-15 16:14:19 -04006319 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
6320 struct btrfs_inode_item);
David Sterbab159fa22016-11-08 18:09:03 +01006321 memzero_extent_buffer(path->nodes[0], (unsigned long)inode_item,
Li Zefan293f7e02012-07-10 00:58:58 -06006322 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04006323 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05006324
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006325 if (name) {
6326 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
6327 struct btrfs_inode_ref);
6328 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
6329 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
6330 ptr = (unsigned long)(ref + 1);
6331 write_extent_buffer(path->nodes[0], name, ptr, name_len);
6332 }
Chris Mason9c583092008-01-29 15:15:18 -05006333
Chris Mason5f39d392007-10-15 16:14:19 -04006334 btrfs_mark_buffer_dirty(path->nodes[0]);
6335 btrfs_free_path(path);
6336
Christoph Hellwig6cbff002009-04-17 10:37:41 +02006337 btrfs_inherit_iflags(inode, dir);
6338
Al Viro569254b2011-07-24 17:08:40 -04006339 if (S_ISREG(mode)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006340 if (btrfs_test_opt(fs_info, NODATASUM))
Chris Mason94272162009-07-02 12:26:06 -04006341 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006342 if (btrfs_test_opt(fs_info, NODATACOW))
Josef Bacikf2bdf9a2013-02-21 15:28:28 -05006343 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
6344 BTRFS_INODE_NODATASUM;
Chris Mason94272162009-07-02 12:26:06 -04006345 }
6346
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006347 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00006348
6349 trace_btrfs_inode_new(inode);
Chris Mason1973f0f2011-06-24 13:13:29 -04006350 btrfs_set_inode_last_trans(trans, inode);
liubo1abe9b82011-03-24 11:18:59 +00006351
Alexander Block8ea05e32012-07-25 17:35:53 +02006352 btrfs_update_root_times(trans, root);
6353
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006354 ret = btrfs_inode_inherit_props(trans, inode, dir);
6355 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006356 btrfs_err(fs_info,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006357 "error inheriting props for ino %llu (root %llu): %d",
David Sterbaf85b7372017-01-20 14:54:07 +01006358 btrfs_ino(BTRFS_I(inode)), root->root_key.objectid, ret);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006359
Chris Mason39279cc2007-06-12 06:35:45 -04006360 return inode;
Chris Masonb0d5d102014-09-08 13:08:51 -07006361
6362fail_unlock:
Al Viro32955c52018-05-16 12:20:05 -04006363 discard_new_inode(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006364fail:
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006365 if (dir && name)
Josef Bacikaec74772008-07-24 12:12:38 -04006366 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04006367 btrfs_free_path(path);
6368 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006369}
6370
Chris Masond352ac62008-09-29 15:18:18 -04006371/*
6372 * utility function to add 'inode' into 'parent_inode' with
6373 * a give name and a given sequence number.
6374 * if 'add_backref' is true, also insert a backref from the
6375 * inode to the parent directory.
6376 */
Chris Masone02119d2008-09-05 16:13:11 -04006377int btrfs_add_link(struct btrfs_trans_handle *trans,
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006378 struct btrfs_inode *parent_inode, struct btrfs_inode *inode,
Chris Masone02119d2008-09-05 16:13:11 -04006379 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006380{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006381 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006382 struct btrfs_key key;
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006383 struct btrfs_root *root = parent_inode->root;
6384 u64 ino = btrfs_ino(inode);
6385 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006386
Li Zefan33345d012011-04-20 10:31:50 +08006387 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006388 memcpy(&key, &inode->root->root_key, sizeof(key));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006389 } else {
Li Zefan33345d012011-04-20 10:31:50 +08006390 key.objectid = ino;
David Sterba962a2982014-06-04 18:41:45 +02006391 key.type = BTRFS_INODE_ITEM_KEY;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006392 key.offset = 0;
6393 }
Chris Mason39279cc2007-06-12 06:35:45 -04006394
Li Zefan33345d012011-04-20 10:31:50 +08006395 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Lu Fengqi6025c192018-08-01 11:32:29 +08006396 ret = btrfs_add_root_ref(trans, key.objectid,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006397 root->root_key.objectid, parent_ino,
6398 index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006399 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08006400 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
6401 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006402 }
6403
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006404 /* Nothing to clean up yet */
6405 if (ret)
6406 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006407
Lu Fengqi684572d2018-08-04 21:10:57 +08006408 ret = btrfs_insert_dir_item(trans, name, name_len, parent_inode, &key,
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006409 btrfs_inode_type(&inode->vfs_inode), index);
Chris Mason9c520572012-12-17 14:26:57 -05006410 if (ret == -EEXIST || ret == -EOVERFLOW)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006411 goto fail_dir_item;
6412 else if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04006413 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006414 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006415 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006416
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006417 btrfs_i_size_write(parent_inode, parent_inode->vfs_inode.i_size +
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006418 name_len * 2);
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006419 inode_inc_iversion(&parent_inode->vfs_inode);
6420 parent_inode->vfs_inode.i_mtime = parent_inode->vfs_inode.i_ctime =
6421 current_time(&parent_inode->vfs_inode);
6422 ret = btrfs_update_inode(trans, root, &parent_inode->vfs_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006423 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04006424 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006425 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05006426
6427fail_dir_item:
6428 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
6429 u64 local_index;
6430 int err;
Lu Fengqi3ee1c552018-08-01 11:32:28 +08006431 err = btrfs_del_root_ref(trans, key.objectid,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006432 root->root_key.objectid, parent_ino,
6433 &local_index, name, name_len);
Johannes Thumshirn1690dd412018-12-12 15:14:17 +01006434 if (err)
6435 btrfs_abort_transaction(trans, err);
Chris Masonfe66a052012-02-20 08:40:56 -05006436 } else if (add_backref) {
6437 u64 local_index;
6438 int err;
6439
6440 err = btrfs_del_inode_ref(trans, root, name, name_len,
6441 ino, parent_ino, &local_index);
Johannes Thumshirn1690dd412018-12-12 15:14:17 +01006442 if (err)
6443 btrfs_abort_transaction(trans, err);
Chris Masonfe66a052012-02-20 08:40:56 -05006444 }
Johannes Thumshirn1690dd412018-12-12 15:14:17 +01006445
6446 /* Return the original error code */
Chris Masonfe66a052012-02-20 08:40:56 -05006447 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006448}
6449
6450static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006451 struct btrfs_inode *dir, struct dentry *dentry,
6452 struct btrfs_inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006453{
Josef Bacika1b075d2010-11-19 20:36:11 +00006454 int err = btrfs_add_link(trans, dir, inode,
6455 dentry->d_name.name, dentry->d_name.len,
6456 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006457 if (err > 0)
6458 err = -EEXIST;
6459 return err;
6460}
6461
Josef Bacik618e21d2007-07-11 10:18:17 -04006462static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04006463 umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04006464{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006465 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Josef Bacik618e21d2007-07-11 10:18:17 -04006466 struct btrfs_trans_handle *trans;
6467 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006468 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04006469 int err;
Josef Bacik618e21d2007-07-11 10:18:17 -04006470 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006471 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04006472
Josef Bacik9ed74f22009-09-11 16:12:44 -04006473 /*
6474 * 2 for inode item and ref
6475 * 2 for dir items
6476 * 1 for xattr if selinux is on
6477 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006478 trans = btrfs_start_transaction(root, 5);
6479 if (IS_ERR(trans))
6480 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05006481
Li Zefan581bb052011-04-20 10:06:11 +08006482 err = btrfs_find_free_ino(root, &objectid);
6483 if (err)
6484 goto out_unlock;
6485
Josef Bacikaec74772008-07-24 12:12:38 -04006486 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006487 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6488 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006489 if (IS_ERR(inode)) {
6490 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -04006491 inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04006492 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006493 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006494
Casey Schauflerad19db72011-12-15 10:09:07 -05006495 /*
6496 * If the active LSM wants to access the inode during
6497 * d_instantiate it needs these. Smack checks to see
6498 * if the filesystem supports xattrs by looking at the
6499 * ops vector.
6500 */
Casey Schauflerad19db72011-12-15 10:09:07 -05006501 inode->i_op = &btrfs_special_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006502 init_special_inode(inode, inode->i_mode, rdev);
6503
6504 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik618e21d2007-07-11 10:18:17 -04006505 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006506 goto out_unlock;
Chris Masonb0d5d102014-09-08 13:08:51 -07006507
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006508 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6509 0, index);
Al Viro32955c52018-05-16 12:20:05 -04006510 if (err)
6511 goto out_unlock;
6512
6513 btrfs_update_inode(trans, root, inode);
6514 d_instantiate_new(dentry, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006515
Josef Bacik618e21d2007-07-11 10:18:17 -04006516out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006517 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006518 btrfs_btree_balance_dirty(fs_info);
Al Viro32955c52018-05-16 12:20:05 -04006519 if (err && inode) {
Josef Bacik618e21d2007-07-11 10:18:17 -04006520 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -04006521 discard_new_inode(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006522 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006523 return err;
6524}
6525
Chris Mason39279cc2007-06-12 06:35:45 -04006526static int btrfs_create(struct inode *dir, struct dentry *dentry,
Al Viroebfc3b42012-06-10 18:05:36 -04006527 umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -04006528{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006529 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04006530 struct btrfs_trans_handle *trans;
6531 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006532 struct inode *inode = NULL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006533 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04006534 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006535 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006536
Josef Bacik9ed74f22009-09-11 16:12:44 -04006537 /*
6538 * 2 for inode item and ref
6539 * 2 for dir items
6540 * 1 for xattr if selinux is on
6541 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006542 trans = btrfs_start_transaction(root, 5);
6543 if (IS_ERR(trans))
6544 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006545
Li Zefan581bb052011-04-20 10:06:11 +08006546 err = btrfs_find_free_ino(root, &objectid);
6547 if (err)
6548 goto out_unlock;
6549
Josef Bacikaec74772008-07-24 12:12:38 -04006550 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006551 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6552 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006553 if (IS_ERR(inode)) {
6554 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -04006555 inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006556 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006557 }
Casey Schauflerad19db72011-12-15 10:09:07 -05006558 /*
6559 * If the active LSM wants to access the inode during
6560 * d_instantiate it needs these. Smack checks to see
6561 * if the filesystem supports xattrs by looking at the
6562 * ops vector.
6563 */
6564 inode->i_fop = &btrfs_file_operations;
6565 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006566 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006567
6568 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
6569 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006570 goto out_unlock;
Chris Masonb0d5d102014-09-08 13:08:51 -07006571
6572 err = btrfs_update_inode(trans, root, inode);
6573 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006574 goto out_unlock;
Casey Schauflerad19db72011-12-15 10:09:07 -05006575
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006576 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6577 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006578 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006579 goto out_unlock;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006580
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006581 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Al Viro1e2e5472018-05-04 08:23:01 -04006582 d_instantiate_new(dentry, inode);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006583
Chris Mason39279cc2007-06-12 06:35:45 -04006584out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006585 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -04006586 if (err && inode) {
Chris Mason39279cc2007-06-12 06:35:45 -04006587 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -04006588 discard_new_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006589 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006590 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006591 return err;
6592}
6593
6594static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
6595 struct dentry *dentry)
6596{
Filipe Manana271dba452016-01-05 16:24:05 +00006597 struct btrfs_trans_handle *trans = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006598 struct btrfs_root *root = BTRFS_I(dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00006599 struct inode *inode = d_inode(old_dentry);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006600 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason00e4e6b2008-08-05 11:18:09 -04006601 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04006602 int err;
6603 int drop_inode = 0;
6604
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006605 /* do not allow sys_link's with other subvols of the same device */
Misono Tomohiro4fd786e2018-08-06 14:25:24 +09006606 if (root->root_key.objectid != BTRFS_I(inode)->root->root_key.objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00006607 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006608
Mark Fashehf1863732012-08-08 11:32:27 -07006609 if (inode->i_nlink >= BTRFS_LINK_MAX)
Al Viroc055e992011-03-04 17:15:18 +00006610 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04006611
Nikolay Borisov877574e2017-02-20 13:50:33 +02006612 err = btrfs_set_inode_index(BTRFS_I(dir), &index);
Josef Bacikaec74772008-07-24 12:12:38 -04006613 if (err)
6614 goto fail;
6615
Yan, Zhenga22285a2010-05-16 10:48:46 -04006616 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00006617 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04006618 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00006619 * 1 item for parent inode
Omar Sandoval399b0bb2018-05-11 13:13:40 -07006620 * 1 item for orphan item deletion if O_TMPFILE
Yan, Zhenga22285a2010-05-16 10:48:46 -04006621 */
Omar Sandoval399b0bb2018-05-11 13:13:40 -07006622 trans = btrfs_start_transaction(root, inode->i_nlink ? 5 : 6);
Yan, Zhenga22285a2010-05-16 10:48:46 -04006623 if (IS_ERR(trans)) {
6624 err = PTR_ERR(trans);
Filipe Manana271dba452016-01-05 16:24:05 +00006625 trans = NULL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006626 goto fail;
6627 }
Chris Mason5f39d392007-10-15 16:14:19 -04006628
Miao Xie67de1172013-12-26 13:07:06 +08006629 /* There are several dir indexes for this inode, clear the cache. */
6630 BTRFS_I(inode)->dir_index = 0ULL;
Zach Brown8b558c52013-10-16 12:10:34 -07006631 inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04006632 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006633 inode->i_ctime = current_time(inode);
Al Viro7de9c6ee2010-10-23 11:11:40 -04006634 ihold(inode);
Josef Bacike9976152012-10-11 15:53:56 -04006635 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
Josef Bacikaec74772008-07-24 12:12:38 -04006636
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006637 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6638 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04006639
Yan, Zhenga5719522009-09-24 09:17:31 -04006640 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006641 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04006642 } else {
Al Viro10d9f302011-07-16 23:09:10 -04006643 struct dentry *parent = dentry->d_parent;
Filipe Mananad4682ba2018-06-11 19:24:28 +01006644 int ret;
6645
Yan, Zhenga5719522009-09-24 09:17:31 -04006646 err = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006647 if (err)
6648 goto fail;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006649 if (inode->i_nlink == 1) {
6650 /*
6651 * If new hard link count is 1, it's a file created
6652 * with open(2) O_TMPFILE flag.
6653 */
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02006654 err = btrfs_orphan_del(trans, BTRFS_I(inode));
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006655 if (err)
6656 goto fail;
6657 }
Filipe Manana41bd6062018-11-28 14:54:28 +00006658 BTRFS_I(inode)->last_link_trans = trans->transid;
Al Viro08c422c2011-12-23 07:58:13 -05006659 d_instantiate(dentry, inode);
Filipe Mananad4682ba2018-06-11 19:24:28 +01006660 ret = btrfs_log_new_name(trans, BTRFS_I(inode), NULL, parent,
6661 true, NULL);
6662 if (ret == BTRFS_NEED_TRANS_COMMIT) {
6663 err = btrfs_commit_transaction(trans);
6664 trans = NULL;
6665 }
Yan, Zhenga5719522009-09-24 09:17:31 -04006666 }
Chris Mason39279cc2007-06-12 06:35:45 -04006667
Chris Mason1832a6d2007-12-21 16:27:21 -05006668fail:
Filipe Manana271dba452016-01-05 16:24:05 +00006669 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006670 btrfs_end_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006671 if (drop_inode) {
6672 inode_dec_link_count(inode);
6673 iput(inode);
6674 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006675 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006676 return err;
6677}
6678
Al Viro18bb1db2011-07-26 01:41:39 -04006679static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04006680{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006681 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masonb9d86662008-05-02 16:13:49 -04006682 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006683 struct btrfs_trans_handle *trans;
6684 struct btrfs_root *root = BTRFS_I(dir)->root;
6685 int err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04006686 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006687 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006688
Josef Bacik9ed74f22009-09-11 16:12:44 -04006689 /*
6690 * 2 items for inode and ref
6691 * 2 items for dir items
6692 * 1 for xattr if selinux is on
6693 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006694 trans = btrfs_start_transaction(root, 5);
6695 if (IS_ERR(trans))
6696 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006697
Li Zefan581bb052011-04-20 10:06:11 +08006698 err = btrfs_find_free_ino(root, &objectid);
6699 if (err)
6700 goto out_fail;
6701
Josef Bacikaec74772008-07-24 12:12:38 -04006702 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006703 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6704 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04006705 if (IS_ERR(inode)) {
6706 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -04006707 inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006708 goto out_fail;
6709 }
Chris Mason5f39d392007-10-15 16:14:19 -04006710
Chris Masonb0d5d102014-09-08 13:08:51 -07006711 /* these must be set before we unlock the inode */
6712 inode->i_op = &btrfs_dir_inode_operations;
6713 inode->i_fop = &btrfs_dir_file_operations;
Josef Bacik33268ea2008-07-24 12:16:36 -04006714
Eric Paris2a7dba32011-02-01 11:05:39 -05006715 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04006716 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006717 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04006718
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02006719 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Mason39279cc2007-06-12 06:35:45 -04006720 err = btrfs_update_inode(trans, root, inode);
6721 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006722 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04006723
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006724 err = btrfs_add_link(trans, BTRFS_I(dir), BTRFS_I(inode),
6725 dentry->d_name.name,
6726 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006727 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006728 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04006729
Al Viro1e2e5472018-05-04 08:23:01 -04006730 d_instantiate_new(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006731
6732out_fail:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006733 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -04006734 if (err && inode) {
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006735 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -04006736 discard_new_inode(inode);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006737 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006738 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006739 return err;
6740}
6741
Chris Masonc8b97812008-10-29 14:49:59 -04006742static noinline int uncompress_inline(struct btrfs_path *path,
Byongho Leee40da0e2015-05-19 23:46:45 +09006743 struct page *page,
Chris Masonc8b97812008-10-29 14:49:59 -04006744 size_t pg_offset, u64 extent_offset,
6745 struct btrfs_file_extent_item *item)
6746{
6747 int ret;
6748 struct extent_buffer *leaf = path->nodes[0];
6749 char *tmp;
6750 size_t max_size;
6751 unsigned long inline_size;
6752 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08006753 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04006754
6755 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08006756 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04006757 max_size = btrfs_file_extent_ram_bytes(leaf, item);
6758 inline_size = btrfs_file_extent_inline_item_len(leaf,
Ross Kirkdd3cc162013-09-16 15:58:09 +01006759 btrfs_item_nr(path->slots[0]));
Chris Masonc8b97812008-10-29 14:49:59 -04006760 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04006761 if (!tmp)
6762 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04006763 ptr = btrfs_file_extent_inline_start(item);
6764
6765 read_extent_buffer(leaf, tmp, ptr, inline_size);
6766
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006767 max_size = min_t(unsigned long, PAGE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08006768 ret = btrfs_decompress(compress_type, tmp, page,
6769 extent_offset, inline_size, max_size);
Zygo Blaxelle1699d22017-03-10 16:45:44 -05006770
6771 /*
6772 * decompression code contains a memset to fill in any space between the end
6773 * of the uncompressed data and the end of max_size in case the decompressed
6774 * data ends up shorter than ram_bytes. That doesn't cover the hole between
6775 * the end of an inline extent and the beginning of the next block, so we
6776 * cover that region here.
6777 */
6778
6779 if (max_size + pg_offset < PAGE_SIZE) {
6780 char *map = kmap(page);
6781 memset(map + pg_offset + max_size, 0, PAGE_SIZE - max_size - pg_offset);
6782 kunmap(page);
6783 }
Chris Masonc8b97812008-10-29 14:49:59 -04006784 kfree(tmp);
Zach Brown166ae5a2014-05-09 17:15:10 -04006785 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -04006786}
6787
Chris Masond352ac62008-09-29 15:18:18 -04006788/*
6789 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05006790 * the ugly parts come from merging extents from the disk with the in-ram
6791 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04006792 * where the in-ram extents might be locked pending data=ordered completion.
6793 *
6794 * This also copies inline extents directly into the page.
6795 */
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006796struct extent_map *btrfs_get_extent(struct btrfs_inode *inode,
Liu Bode2c6612018-08-25 13:47:59 +08006797 struct page *page,
6798 size_t pg_offset, u64 start, u64 len,
6799 int create)
Chris Masona52d9a82007-08-27 16:49:44 -04006800{
David Sterba3ffbd682018-06-29 10:56:42 +02006801 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Chris Masona52d9a82007-08-27 16:49:44 -04006802 int ret;
6803 int err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006804 u64 extent_start = 0;
6805 u64 extent_end = 0;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006806 u64 objectid = btrfs_ino(inode);
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006807 u8 extent_type;
Chris Masonf4219502008-07-22 11:18:09 -04006808 struct btrfs_path *path = NULL;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006809 struct btrfs_root *root = inode->root;
Chris Masona52d9a82007-08-27 16:49:44 -04006810 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04006811 struct extent_buffer *leaf;
6812 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04006813 struct extent_map *em = NULL;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006814 struct extent_map_tree *em_tree = &inode->extent_tree;
6815 struct extent_io_tree *io_tree = &inode->io_tree;
Filipe Manana7ffbb592014-06-09 03:48:05 +01006816 const bool new_inline = !page || create;
Chris Masona52d9a82007-08-27 16:49:44 -04006817
Chris Mason890871b2009-09-02 16:24:52 -04006818 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006819 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04006820 if (em)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006821 em->bdev = fs_info->fs_devices->latest_bdev;
Chris Mason890871b2009-09-02 16:24:52 -04006822 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006823
Chris Masona52d9a82007-08-27 16:49:44 -04006824 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04006825 if (em->start > start || em->start + em->len <= start)
6826 free_extent_map(em);
6827 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05006828 free_extent_map(em);
6829 else
6830 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006831 }
David Sterba172ddd62011-04-21 00:48:27 +02006832 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04006833 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05006834 err = -ENOMEM;
6835 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006836 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006837 em->bdev = fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05006838 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05006839 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05006840 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04006841 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04006842
Liu Bobee6ec82018-08-17 05:05:28 +08006843 path = btrfs_alloc_path();
Chris Masonf4219502008-07-22 11:18:09 -04006844 if (!path) {
Liu Bobee6ec82018-08-17 05:05:28 +08006845 err = -ENOMEM;
6846 goto out;
Chris Masonf4219502008-07-22 11:18:09 -04006847 }
6848
Liu Bobee6ec82018-08-17 05:05:28 +08006849 /* Chances are we'll be called again, so go ahead and do readahead */
6850 path->reada = READA_FORWARD;
6851
Liu Boe49aabd2018-08-25 13:47:09 +08006852 /*
6853 * Unless we're going to uncompress the inline extent, no sleep would
6854 * happen.
6855 */
6856 path->leave_spinning = 1;
6857
Nikolay Borisov5c9a7022017-12-01 11:19:40 +02006858 ret = btrfs_lookup_file_extent(NULL, root, path, objectid, start, 0);
Chris Masona52d9a82007-08-27 16:49:44 -04006859 if (ret < 0) {
6860 err = ret;
6861 goto out;
Nikolay Borisovb8eeab72018-12-17 11:49:00 +02006862 } else if (ret > 0) {
Chris Masona52d9a82007-08-27 16:49:44 -04006863 if (path->slots[0] == 0)
6864 goto not_found;
6865 path->slots[0]--;
6866 }
6867
Chris Mason5f39d392007-10-15 16:14:19 -04006868 leaf = path->nodes[0];
6869 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04006870 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04006871 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04006872 if (found_key.objectid != objectid ||
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006873 found_key.type != BTRFS_EXTENT_DATA_KEY) {
Josef Bacik25a50342013-10-14 12:08:38 -04006874 /*
6875 * If we backup past the first extent we want to move forward
6876 * and see if there is an extent in front of us, otherwise we'll
6877 * say there is a hole for our whole search range which can
6878 * cause problems.
6879 */
6880 extent_end = start;
6881 goto next;
Chris Masona52d9a82007-08-27 16:49:44 -04006882 }
6883
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006884 extent_type = btrfs_file_extent_type(leaf, item);
Chris Mason5f39d392007-10-15 16:14:19 -04006885 extent_start = found_key.offset;
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006886 if (extent_type == BTRFS_FILE_EXTENT_REG ||
6887 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08006888 /* Only regular file could have regular/prealloc extent */
6889 if (!S_ISREG(inode->vfs_inode.i_mode)) {
6890 ret = -EUCLEAN;
6891 btrfs_crit(fs_info,
6892 "regular/prealloc extent found for non-regular inode %llu",
6893 btrfs_ino(inode));
6894 goto out;
6895 }
Chris Masona52d9a82007-08-27 16:49:44 -04006896 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04006897 btrfs_file_extent_num_bytes(leaf, item);
Liu Bo09ed2f12017-03-10 11:09:48 -08006898
6899 trace_btrfs_get_extent_show_fi_regular(inode, leaf, item,
6900 extent_start);
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006901 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Yan Zheng9036c102008-10-30 14:19:41 -04006902 size_t size;
Qu Wenruoe41ca582018-06-06 15:41:49 +08006903
6904 size = btrfs_file_extent_ram_bytes(leaf, item);
Jeff Mahoneyda170662016-06-15 09:22:56 -04006905 extent_end = ALIGN(extent_start + size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006906 fs_info->sectorsize);
Liu Bo09ed2f12017-03-10 11:09:48 -08006907
6908 trace_btrfs_get_extent_show_fi_inline(inode, leaf, item,
6909 path->slots[0],
6910 extent_start);
Yan Zheng9036c102008-10-30 14:19:41 -04006911 }
Josef Bacik25a50342013-10-14 12:08:38 -04006912next:
Yan Zheng9036c102008-10-30 14:19:41 -04006913 if (start >= extent_end) {
6914 path->slots[0]++;
6915 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
6916 ret = btrfs_next_leaf(root, path);
6917 if (ret < 0) {
6918 err = ret;
6919 goto out;
Nikolay Borisovb8eeab72018-12-17 11:49:00 +02006920 } else if (ret > 0) {
Yan Zheng9036c102008-10-30 14:19:41 -04006921 goto not_found;
Nikolay Borisovb8eeab72018-12-17 11:49:00 +02006922 }
Yan Zheng9036c102008-10-30 14:19:41 -04006923 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04006924 }
Yan Zheng9036c102008-10-30 14:19:41 -04006925 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6926 if (found_key.objectid != objectid ||
6927 found_key.type != BTRFS_EXTENT_DATA_KEY)
6928 goto not_found;
6929 if (start + len <= found_key.offset)
6930 goto not_found;
Wang Shilonge2eca692014-07-17 11:44:14 +08006931 if (start > found_key.offset)
6932 goto next;
Nikolay Borisov02a033d2018-12-17 10:36:02 +02006933
6934 /* New extent overlaps with existing one */
Yan Zheng9036c102008-10-30 14:19:41 -04006935 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006936 em->orig_start = start;
Yan Zheng9036c102008-10-30 14:19:41 -04006937 em->len = found_key.offset - start;
Nikolay Borisov02a033d2018-12-17 10:36:02 +02006938 em->block_start = EXTENT_MAP_HOLE;
6939 goto insert;
Yan Zheng9036c102008-10-30 14:19:41 -04006940 }
6941
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006942 btrfs_extent_item_to_extent_map(inode, path, item,
Nikolay Borisov9cdc5122017-02-20 13:51:02 +02006943 new_inline, em);
Filipe Manana7ffbb592014-06-09 03:48:05 +01006944
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006945 if (extent_type == BTRFS_FILE_EXTENT_REG ||
6946 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006947 goto insert;
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006948 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04006949 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04006950 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04006951 size_t size;
6952 size_t extent_offset;
6953 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04006954
Filipe Manana7ffbb592014-06-09 03:48:05 +01006955 if (new_inline)
Yan689f9342007-10-29 11:41:07 -04006956 goto out;
Yan689f9342007-10-29 11:41:07 -04006957
Qu Wenruoe41ca582018-06-06 15:41:49 +08006958 size = btrfs_file_extent_ram_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04006959 extent_offset = page_offset(page) + pg_offset - extent_start;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006960 copy_size = min_t(u64, PAGE_SIZE - pg_offset,
6961 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04006962 em->start = extent_start + extent_offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006963 em->len = ALIGN(copy_size, fs_info->sectorsize);
Josef Bacikb4939682012-12-03 10:31:19 -05006964 em->orig_block_len = em->len;
Josef Bacik70c8a912012-10-11 16:54:30 -04006965 em->orig_start = em->start;
Yan689f9342007-10-29 11:41:07 -04006966 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Liu Boe49aabd2018-08-25 13:47:09 +08006967
6968 btrfs_set_path_blocking(path);
Edmund Nadolskibf46f522017-11-20 13:24:49 -07006969 if (!PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08006970 if (btrfs_file_extent_compression(leaf, item) !=
6971 BTRFS_COMPRESS_NONE) {
Byongho Leee40da0e2015-05-19 23:46:45 +09006972 ret = uncompress_inline(path, page, pg_offset,
Chris Masonc8b97812008-10-29 14:49:59 -04006973 extent_offset, item);
Zach Brown166ae5a2014-05-09 17:15:10 -04006974 if (ret) {
6975 err = ret;
6976 goto out;
6977 }
Chris Masonc8b97812008-10-29 14:49:59 -04006978 } else {
6979 map = kmap(page);
6980 read_extent_buffer(leaf, map + pg_offset, ptr,
6981 copy_size);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006982 if (pg_offset + copy_size < PAGE_SIZE) {
Chris Mason93c82d52009-09-11 12:36:29 -04006983 memset(map + pg_offset + copy_size, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006984 PAGE_SIZE - pg_offset -
Chris Mason93c82d52009-09-11 12:36:29 -04006985 copy_size);
6986 }
Chris Masonc8b97812008-10-29 14:49:59 -04006987 kunmap(page);
6988 }
Chris Mason179e29e2007-11-01 11:28:41 -04006989 flush_dcache_page(page);
Chris Masona52d9a82007-08-27 16:49:44 -04006990 }
Chris Masond1310b22008-01-24 16:13:08 -05006991 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00006992 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04006993 goto insert;
Chris Masona52d9a82007-08-27 16:49:44 -04006994 }
6995not_found:
6996 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006997 em->orig_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05006998 em->len = len;
Chris Mason5f39d392007-10-15 16:14:19 -04006999 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04007000insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02007001 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05007002 if (em->start > start || extent_map_end(em) <= start) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007003 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04007004 "bad extent! em: [%llu %llu] passed [%llu %llu]",
7005 em->start, em->len, start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04007006 err = -EIO;
7007 goto out;
7008 }
Chris Masond1310b22008-01-24 16:13:08 -05007009
7010 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04007011 write_lock(&em_tree->lock);
David Sterbaf46b24c2018-04-03 21:45:57 +02007012 err = btrfs_add_extent_mapping(fs_info, em_tree, &em, start, len);
Chris Mason890871b2009-09-02 16:24:52 -04007013 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04007014out:
Liu Boc6414282018-08-23 07:36:17 +08007015 btrfs_free_path(path);
liubo1abe9b82011-03-24 11:18:59 +00007016
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007017 trace_btrfs_get_extent(root, inode, em);
liubo1abe9b82011-03-24 11:18:59 +00007018
Chris Masona52d9a82007-08-27 16:49:44 -04007019 if (err) {
7020 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04007021 return ERR_PTR(err);
7022 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007023 BUG_ON(!em); /* Error is always set */
Chris Masona52d9a82007-08-27 16:49:44 -04007024 return em;
7025}
7026
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007027struct extent_map *btrfs_get_extent_fiemap(struct btrfs_inode *inode,
Nikolay Borisov4ab47a82018-12-12 09:42:32 +02007028 u64 start, u64 len)
Chris Masonec29ed52011-02-23 16:23:20 -05007029{
7030 struct extent_map *em;
7031 struct extent_map *hole_em = NULL;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007032 u64 delalloc_start = start;
Chris Masonec29ed52011-02-23 16:23:20 -05007033 u64 end;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007034 u64 delalloc_len;
7035 u64 delalloc_end;
Chris Masonec29ed52011-02-23 16:23:20 -05007036 int err = 0;
7037
Nikolay Borisov4ab47a82018-12-12 09:42:32 +02007038 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
Chris Masonec29ed52011-02-23 16:23:20 -05007039 if (IS_ERR(em))
7040 return em;
Dan Carpenter99862772017-04-11 11:57:15 +03007041 /*
7042 * If our em maps to:
7043 * - a hole or
7044 * - a pre-alloc extent,
7045 * there might actually be delalloc bytes behind it.
7046 */
7047 if (em->block_start != EXTENT_MAP_HOLE &&
7048 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7049 return em;
7050 else
7051 hole_em = em;
Chris Masonec29ed52011-02-23 16:23:20 -05007052
7053 /* check to see if we've wrapped (len == -1 or similar) */
7054 end = start + len;
7055 if (end < start)
7056 end = (u64)-1;
7057 else
7058 end -= 1;
7059
7060 em = NULL;
7061
7062 /* ok, we didn't find anything, lets look for delalloc */
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007063 delalloc_len = count_range_bits(&inode->io_tree, &delalloc_start,
Chris Masonec29ed52011-02-23 16:23:20 -05007064 end, len, EXTENT_DELALLOC, 1);
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007065 delalloc_end = delalloc_start + delalloc_len;
7066 if (delalloc_end < delalloc_start)
7067 delalloc_end = (u64)-1;
Chris Masonec29ed52011-02-23 16:23:20 -05007068
7069 /*
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007070 * We didn't find anything useful, return the original results from
7071 * get_extent()
Chris Masonec29ed52011-02-23 16:23:20 -05007072 */
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007073 if (delalloc_start > end || delalloc_end <= start) {
Chris Masonec29ed52011-02-23 16:23:20 -05007074 em = hole_em;
7075 hole_em = NULL;
7076 goto out;
7077 }
7078
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007079 /*
7080 * Adjust the delalloc_start to make sure it doesn't go backwards from
7081 * the start they passed in
Chris Masonec29ed52011-02-23 16:23:20 -05007082 */
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007083 delalloc_start = max(start, delalloc_start);
7084 delalloc_len = delalloc_end - delalloc_start;
Chris Masonec29ed52011-02-23 16:23:20 -05007085
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007086 if (delalloc_len > 0) {
7087 u64 hole_start;
Nikolay Borisov02950af2018-12-12 09:42:34 +02007088 u64 hole_len;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007089 const u64 hole_end = extent_map_end(hole_em);
Chris Masonec29ed52011-02-23 16:23:20 -05007090
David Sterba172ddd62011-04-21 00:48:27 +02007091 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05007092 if (!em) {
7093 err = -ENOMEM;
7094 goto out;
7095 }
Chris Masonec29ed52011-02-23 16:23:20 -05007096 em->bdev = NULL;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007097
7098 ASSERT(hole_em);
7099 /*
7100 * When btrfs_get_extent can't find anything it returns one
7101 * huge hole
7102 *
7103 * Make sure what it found really fits our range, and adjust to
7104 * make sure it is based on the start from the caller
7105 */
7106 if (hole_end <= start || hole_em->start > end) {
7107 free_extent_map(hole_em);
7108 hole_em = NULL;
7109 } else {
7110 hole_start = max(hole_em->start, start);
7111 hole_len = hole_end - hole_start;
7112 }
7113
7114 if (hole_em && delalloc_start > hole_start) {
7115 /*
7116 * Our hole starts before our delalloc, so we have to
7117 * return just the parts of the hole that go until the
7118 * delalloc starts
Chris Masonec29ed52011-02-23 16:23:20 -05007119 */
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007120 em->len = min(hole_len, delalloc_start - hole_start);
Chris Masonec29ed52011-02-23 16:23:20 -05007121 em->start = hole_start;
7122 em->orig_start = hole_start;
7123 /*
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007124 * Don't adjust block start at all, it is fixed at
7125 * EXTENT_MAP_HOLE
Chris Masonec29ed52011-02-23 16:23:20 -05007126 */
7127 em->block_start = hole_em->block_start;
7128 em->block_len = hole_len;
Liu Bof9e4fb52013-01-07 10:10:12 +00007129 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
7130 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonec29ed52011-02-23 16:23:20 -05007131 } else {
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007132 /*
7133 * Hole is out of passed range or it starts after
7134 * delalloc range
7135 */
7136 em->start = delalloc_start;
7137 em->len = delalloc_len;
7138 em->orig_start = delalloc_start;
Chris Masonec29ed52011-02-23 16:23:20 -05007139 em->block_start = EXTENT_MAP_DELALLOC;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007140 em->block_len = delalloc_len;
Chris Masonec29ed52011-02-23 16:23:20 -05007141 }
Nikolay Borisovbf8d32b2017-12-01 11:19:43 +02007142 } else {
Chris Masonec29ed52011-02-23 16:23:20 -05007143 return hole_em;
7144 }
7145out:
7146
7147 free_extent_map(hole_em);
7148 if (err) {
7149 free_extent_map(em);
7150 return ERR_PTR(err);
7151 }
7152 return em;
7153}
7154
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007155static struct extent_map *btrfs_create_dio_extent(struct inode *inode,
7156 const u64 start,
7157 const u64 len,
7158 const u64 orig_start,
7159 const u64 block_start,
7160 const u64 block_len,
7161 const u64 orig_block_len,
7162 const u64 ram_bytes,
7163 const int type)
7164{
7165 struct extent_map *em = NULL;
7166 int ret;
7167
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007168 if (type != BTRFS_ORDERED_NOCOW) {
Liu Bo6f9994d2017-01-31 07:50:22 -08007169 em = create_io_em(inode, start, len, orig_start,
7170 block_start, block_len, orig_block_len,
7171 ram_bytes,
7172 BTRFS_COMPRESS_NONE, /* compress_type */
7173 type);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007174 if (IS_ERR(em))
7175 goto out;
7176 }
7177 ret = btrfs_add_ordered_extent_dio(inode, start, block_start,
7178 len, block_len, type);
7179 if (ret) {
7180 if (em) {
7181 free_extent_map(em);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02007182 btrfs_drop_extent_cache(BTRFS_I(inode), start,
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007183 start + len - 1, 0);
7184 }
7185 em = ERR_PTR(ret);
7186 }
7187 out:
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007188
7189 return em;
7190}
7191
Josef Bacik4b46fce2010-05-23 11:00:55 -04007192static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
7193 u64 start, u64 len)
7194{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007195 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007196 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik70c8a912012-10-11 16:54:30 -04007197 struct extent_map *em;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007198 struct btrfs_key ins;
7199 u64 alloc_hint;
7200 int ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007201
Josef Bacik4b46fce2010-05-23 11:00:55 -04007202 alloc_hint = get_extent_allocation_hint(inode, start, len);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007203 ret = btrfs_reserve_extent(root, len, len, fs_info->sectorsize,
Jeff Mahoneyda170662016-06-15 09:22:56 -04007204 0, alloc_hint, &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007205 if (ret)
7206 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007207
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007208 em = btrfs_create_dio_extent(inode, start, ins.offset, start,
7209 ins.objectid, ins.offset, ins.offset,
Liu Bo6288d6e2017-02-21 12:12:58 -08007210 ins.offset, BTRFS_ORDERED_REGULAR);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007211 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007212 if (IS_ERR(em))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007213 btrfs_free_reserved_extent(fs_info, ins.objectid,
7214 ins.offset, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007215
Josef Bacik4b46fce2010-05-23 11:00:55 -04007216 return em;
7217}
7218
Chris Mason46bfbb52010-05-26 11:04:10 -04007219/*
7220 * returns 1 when the nocow is safe, < 1 on error, 0 if the
7221 * block must be cow'd
7222 */
Josef Bacik00361582013-08-14 14:02:47 -04007223noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
Josef Bacik7ee9e442013-06-21 16:37:03 -04007224 u64 *orig_start, u64 *orig_block_len,
7225 u64 *ram_bytes)
Chris Mason46bfbb52010-05-26 11:04:10 -04007226{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007227 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason46bfbb52010-05-26 11:04:10 -04007228 struct btrfs_path *path;
7229 int ret;
7230 struct extent_buffer *leaf;
7231 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xie7b2b7082014-02-27 13:58:05 +08007232 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason46bfbb52010-05-26 11:04:10 -04007233 struct btrfs_file_extent_item *fi;
7234 struct btrfs_key key;
7235 u64 disk_bytenr;
7236 u64 backref_offset;
7237 u64 extent_end;
7238 u64 num_bytes;
7239 int slot;
7240 int found_type;
Josef Bacik7ee9e442013-06-21 16:37:03 -04007241 bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW);
Miao Xiee77751a2013-12-27 21:11:50 +08007242
Chris Mason46bfbb52010-05-26 11:04:10 -04007243 path = btrfs_alloc_path();
7244 if (!path)
7245 return -ENOMEM;
7246
David Sterbaf85b7372017-01-20 14:54:07 +01007247 ret = btrfs_lookup_file_extent(NULL, root, path,
7248 btrfs_ino(BTRFS_I(inode)), offset, 0);
Chris Mason46bfbb52010-05-26 11:04:10 -04007249 if (ret < 0)
7250 goto out;
7251
7252 slot = path->slots[0];
7253 if (ret == 1) {
7254 if (slot == 0) {
7255 /* can't find the item, must cow */
7256 ret = 0;
7257 goto out;
7258 }
7259 slot--;
7260 }
7261 ret = 0;
7262 leaf = path->nodes[0];
7263 btrfs_item_key_to_cpu(leaf, &key, slot);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02007264 if (key.objectid != btrfs_ino(BTRFS_I(inode)) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04007265 key.type != BTRFS_EXTENT_DATA_KEY) {
7266 /* not our file or wrong item type, must cow */
7267 goto out;
7268 }
7269
7270 if (key.offset > offset) {
7271 /* Wrong offset, must cow */
7272 goto out;
7273 }
7274
7275 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
7276 found_type = btrfs_file_extent_type(leaf, fi);
7277 if (found_type != BTRFS_FILE_EXTENT_REG &&
7278 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
7279 /* not a regular extent, must cow */
7280 goto out;
7281 }
Josef Bacik7ee9e442013-06-21 16:37:03 -04007282
7283 if (!nocow && found_type == BTRFS_FILE_EXTENT_REG)
7284 goto out;
7285
Miao Xiee77751a2013-12-27 21:11:50 +08007286 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
7287 if (extent_end <= offset)
7288 goto out;
7289
Chris Mason46bfbb52010-05-26 11:04:10 -04007290 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Josef Bacik7ee9e442013-06-21 16:37:03 -04007291 if (disk_bytenr == 0)
7292 goto out;
7293
7294 if (btrfs_file_extent_compression(leaf, fi) ||
7295 btrfs_file_extent_encryption(leaf, fi) ||
7296 btrfs_file_extent_other_encoding(leaf, fi))
7297 goto out;
7298
Ethan Lien78d42952018-05-17 14:58:29 +08007299 /*
7300 * Do the same check as in btrfs_cross_ref_exist but without the
7301 * unnecessary search.
7302 */
7303 if (btrfs_file_extent_generation(leaf, fi) <=
7304 btrfs_root_last_snapshot(&root->root_item))
7305 goto out;
7306
Chris Mason46bfbb52010-05-26 11:04:10 -04007307 backref_offset = btrfs_file_extent_offset(leaf, fi);
7308
Josef Bacik7ee9e442013-06-21 16:37:03 -04007309 if (orig_start) {
7310 *orig_start = key.offset - backref_offset;
7311 *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
7312 *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
7313 }
Josef Bacikeb384b52013-04-24 16:32:55 -04007314
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007315 if (btrfs_extent_readonly(fs_info, disk_bytenr))
Chris Mason46bfbb52010-05-26 11:04:10 -04007316 goto out;
Miao Xie7b2b7082014-02-27 13:58:05 +08007317
7318 num_bytes = min(offset + *len, extent_end) - offset;
7319 if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) {
7320 u64 range_end;
7321
Jeff Mahoneyda170662016-06-15 09:22:56 -04007322 range_end = round_up(offset + num_bytes,
7323 root->fs_info->sectorsize) - 1;
Miao Xie7b2b7082014-02-27 13:58:05 +08007324 ret = test_range_bit(io_tree, offset, range_end,
7325 EXTENT_DELALLOC, 0, NULL);
7326 if (ret) {
7327 ret = -EAGAIN;
7328 goto out;
7329 }
7330 }
7331
Josef Bacik1bda19e2013-10-18 12:10:36 -04007332 btrfs_release_path(path);
Chris Mason46bfbb52010-05-26 11:04:10 -04007333
7334 /*
7335 * look for other files referencing this extent, if we
7336 * find any we must cow
7337 */
Josef Bacik00361582013-08-14 14:02:47 -04007338
Liu Boe4c3b2d2017-01-30 12:25:28 -08007339 ret = btrfs_cross_ref_exist(root, btrfs_ino(BTRFS_I(inode)),
Josef Bacik00361582013-08-14 14:02:47 -04007340 key.offset - backref_offset, disk_bytenr);
Josef Bacik00361582013-08-14 14:02:47 -04007341 if (ret) {
7342 ret = 0;
7343 goto out;
7344 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007345
7346 /*
7347 * adjust disk_bytenr and num_bytes to cover just the bytes
7348 * in this extent we are about to write. If there
7349 * are any csums in that range we have to cow in order
7350 * to keep the csums correct
7351 */
7352 disk_bytenr += backref_offset;
7353 disk_bytenr += offset - key.offset;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007354 if (csum_exist_in_range(fs_info, disk_bytenr, num_bytes))
7355 goto out;
Chris Mason46bfbb52010-05-26 11:04:10 -04007356 /*
7357 * all of the above have passed, it is safe to overwrite this extent
7358 * without cow
7359 */
Josef Bacikeb384b52013-04-24 16:32:55 -04007360 *len = num_bytes;
Chris Mason46bfbb52010-05-26 11:04:10 -04007361 ret = 1;
7362out:
7363 btrfs_free_path(path);
7364 return ret;
7365}
7366
Josef Bacikeb838e72012-07-31 16:28:48 -04007367static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
7368 struct extent_state **cached_state, int writing)
7369{
7370 struct btrfs_ordered_extent *ordered;
7371 int ret = 0;
7372
7373 while (1) {
7374 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbaff13db42015-12-03 14:30:40 +01007375 cached_state);
Josef Bacikeb838e72012-07-31 16:28:48 -04007376 /*
7377 * We're concerned with the entire range that we're going to be
Nicholas D Steeves01327612016-05-19 21:18:45 -04007378 * doing DIO to, so we need to make sure there's no ordered
Josef Bacikeb838e72012-07-31 16:28:48 -04007379 * extents in this range.
7380 */
Nikolay Borisova776c6f2017-02-20 13:50:49 +02007381 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), lockstart,
Josef Bacikeb838e72012-07-31 16:28:48 -04007382 lockend - lockstart + 1);
7383
7384 /*
7385 * We need to make sure there are no buffered pages in this
7386 * range either, we could have raced between the invalidate in
7387 * generic_file_direct_write and locking the extent. The
7388 * invalidate needs to happen so that reads after a write do not
7389 * get stale data.
7390 */
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007391 if (!ordered &&
David Sterba051c98e2018-03-07 15:33:22 +01007392 (!writing || !filemap_range_has_page(inode->i_mapping,
7393 lockstart, lockend)))
Josef Bacikeb838e72012-07-31 16:28:48 -04007394 break;
7395
7396 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbae43bbe52017-12-12 21:43:52 +01007397 cached_state);
Josef Bacikeb838e72012-07-31 16:28:48 -04007398
7399 if (ordered) {
Filipe Mananaade77022016-02-18 14:28:55 +00007400 /*
7401 * If we are doing a DIO read and the ordered extent we
7402 * found is for a buffered write, we can not wait for it
7403 * to complete and retry, because if we do so we can
7404 * deadlock with concurrent buffered writes on page
7405 * locks. This happens only if our DIO read covers more
7406 * than one extent map, if at this point has already
7407 * created an ordered extent for a previous extent map
7408 * and locked its range in the inode's io tree, and a
7409 * concurrent write against that previous extent map's
7410 * range and this range started (we unlock the ranges
7411 * in the io tree only when the bios complete and
7412 * buffered writes always lock pages before attempting
7413 * to lock range in the io tree).
7414 */
7415 if (writing ||
7416 test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags))
7417 btrfs_start_ordered_extent(inode, ordered, 1);
7418 else
7419 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007420 btrfs_put_ordered_extent(ordered);
7421 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007422 /*
Filipe Mananab850ae12015-12-08 16:23:16 +00007423 * We could trigger writeback for this range (and wait
7424 * for it to complete) and then invalidate the pages for
7425 * this range (through invalidate_inode_pages2_range()),
7426 * but that can lead us to a deadlock with a concurrent
7427 * call to readpages() (a buffered read or a defrag call
7428 * triggered a readahead) on a page lock due to an
7429 * ordered dio extent we created before but did not have
7430 * yet a corresponding bio submitted (whence it can not
7431 * complete), which makes readpages() wait for that
7432 * ordered extent to complete while holding a lock on
7433 * that page.
Josef Bacikeb838e72012-07-31 16:28:48 -04007434 */
Filipe Mananab850ae12015-12-08 16:23:16 +00007435 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007436 }
7437
Filipe Mananaade77022016-02-18 14:28:55 +00007438 if (ret)
7439 break;
7440
Josef Bacikeb838e72012-07-31 16:28:48 -04007441 cond_resched();
7442 }
7443
7444 return ret;
7445}
7446
Liu Bo6f9994d2017-01-31 07:50:22 -08007447/* The callers of this must take lock_extent() */
7448static struct extent_map *create_io_em(struct inode *inode, u64 start, u64 len,
7449 u64 orig_start, u64 block_start,
7450 u64 block_len, u64 orig_block_len,
7451 u64 ram_bytes, int compress_type,
7452 int type)
Josef Bacik69ffb542012-09-11 15:40:07 -04007453{
7454 struct extent_map_tree *em_tree;
7455 struct extent_map *em;
7456 struct btrfs_root *root = BTRFS_I(inode)->root;
7457 int ret;
7458
Liu Bo6f9994d2017-01-31 07:50:22 -08007459 ASSERT(type == BTRFS_ORDERED_PREALLOC ||
7460 type == BTRFS_ORDERED_COMPRESSED ||
7461 type == BTRFS_ORDERED_NOCOW ||
Liu Bo1af4a0a2017-02-13 15:35:09 -08007462 type == BTRFS_ORDERED_REGULAR);
Liu Bo6f9994d2017-01-31 07:50:22 -08007463
Josef Bacik69ffb542012-09-11 15:40:07 -04007464 em_tree = &BTRFS_I(inode)->extent_tree;
7465 em = alloc_extent_map();
7466 if (!em)
7467 return ERR_PTR(-ENOMEM);
7468
7469 em->start = start;
7470 em->orig_start = orig_start;
7471 em->len = len;
7472 em->block_len = block_len;
7473 em->block_start = block_start;
7474 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacikb4939682012-12-03 10:31:19 -05007475 em->orig_block_len = orig_block_len;
Josef Bacikcc95bef2013-04-04 14:31:27 -04007476 em->ram_bytes = ram_bytes;
Josef Bacik70c8a912012-10-11 16:54:30 -04007477 em->generation = -1;
Josef Bacik69ffb542012-09-11 15:40:07 -04007478 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Liu Bo1af4a0a2017-02-13 15:35:09 -08007479 if (type == BTRFS_ORDERED_PREALLOC) {
Josef Bacikb11e2342012-12-03 10:58:15 -05007480 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Liu Bo1af4a0a2017-02-13 15:35:09 -08007481 } else if (type == BTRFS_ORDERED_COMPRESSED) {
Liu Bo6f9994d2017-01-31 07:50:22 -08007482 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
7483 em->compress_type = compress_type;
7484 }
Josef Bacik69ffb542012-09-11 15:40:07 -04007485
7486 do {
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02007487 btrfs_drop_extent_cache(BTRFS_I(inode), em->start,
Josef Bacik69ffb542012-09-11 15:40:07 -04007488 em->start + em->len - 1, 0);
7489 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04007490 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik69ffb542012-09-11 15:40:07 -04007491 write_unlock(&em_tree->lock);
Liu Bo6f9994d2017-01-31 07:50:22 -08007492 /*
7493 * The caller has taken lock_extent(), who could race with us
7494 * to add em?
7495 */
Josef Bacik69ffb542012-09-11 15:40:07 -04007496 } while (ret == -EEXIST);
7497
7498 if (ret) {
7499 free_extent_map(em);
7500 return ERR_PTR(ret);
7501 }
7502
Liu Bo6f9994d2017-01-31 07:50:22 -08007503 /* em got 2 refs now, callers needs to do free_extent_map once. */
Josef Bacik69ffb542012-09-11 15:40:07 -04007504 return em;
7505}
7506
Nikolay Borisov1c8d0172018-05-02 15:19:32 +03007507
7508static int btrfs_get_blocks_direct_read(struct extent_map *em,
7509 struct buffer_head *bh_result,
7510 struct inode *inode,
7511 u64 start, u64 len)
7512{
7513 if (em->block_start == EXTENT_MAP_HOLE ||
7514 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7515 return -ENOENT;
7516
7517 len = min(len, em->len - (start - em->start));
7518
7519 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
7520 inode->i_blkbits;
7521 bh_result->b_size = len;
7522 bh_result->b_bdev = em->bdev;
7523 set_buffer_mapped(bh_result);
7524
7525 return 0;
7526}
7527
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007528static int btrfs_get_blocks_direct_write(struct extent_map **map,
7529 struct buffer_head *bh_result,
7530 struct inode *inode,
7531 struct btrfs_dio_data *dio_data,
7532 u64 start, u64 len)
7533{
7534 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
7535 struct extent_map *em = *map;
7536 int ret = 0;
7537
7538 /*
7539 * We don't allocate a new extent in the following cases
7540 *
7541 * 1) The inode is marked as NODATACOW. In this case we'll just use the
7542 * existing extent.
7543 * 2) The extent is marked as PREALLOC. We're good to go here and can
7544 * just use the extent.
7545 *
7546 */
7547 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
7548 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
7549 em->block_start != EXTENT_MAP_HOLE)) {
7550 int type;
7551 u64 block_start, orig_start, orig_block_len, ram_bytes;
7552
7553 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7554 type = BTRFS_ORDERED_PREALLOC;
7555 else
7556 type = BTRFS_ORDERED_NOCOW;
7557 len = min(len, em->len - (start - em->start));
7558 block_start = em->block_start + (start - em->start);
7559
7560 if (can_nocow_extent(inode, start, &len, &orig_start,
7561 &orig_block_len, &ram_bytes) == 1 &&
7562 btrfs_inc_nocow_writers(fs_info, block_start)) {
7563 struct extent_map *em2;
7564
7565 em2 = btrfs_create_dio_extent(inode, start, len,
7566 orig_start, block_start,
7567 len, orig_block_len,
7568 ram_bytes, type);
7569 btrfs_dec_nocow_writers(fs_info, block_start);
7570 if (type == BTRFS_ORDERED_PREALLOC) {
7571 free_extent_map(em);
7572 *map = em = em2;
7573 }
7574
7575 if (em2 && IS_ERR(em2)) {
7576 ret = PTR_ERR(em2);
7577 goto out;
7578 }
7579 /*
7580 * For inode marked NODATACOW or extent marked PREALLOC,
7581 * use the existing or preallocated extent, so does not
7582 * need to adjust btrfs_space_info's bytes_may_use.
7583 */
7584 btrfs_free_reserved_data_space_noquota(inode, start,
7585 len);
7586 goto skip_cow;
7587 }
7588 }
7589
7590 /* this will cow the extent */
7591 len = bh_result->b_size;
7592 free_extent_map(em);
7593 *map = em = btrfs_new_extent_direct(inode, start, len);
7594 if (IS_ERR(em)) {
7595 ret = PTR_ERR(em);
7596 goto out;
7597 }
7598
7599 len = min(len, em->len - (start - em->start));
7600
7601skip_cow:
7602 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
7603 inode->i_blkbits;
7604 bh_result->b_size = len;
7605 bh_result->b_bdev = em->bdev;
7606 set_buffer_mapped(bh_result);
7607
7608 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7609 set_buffer_new(bh_result);
7610
7611 /*
7612 * Need to update the i_size under the extent lock so buffered
7613 * readers will get the updated i_size when we unlock.
7614 */
7615 if (!dio_data->overwrite && start + len > i_size_read(inode))
7616 i_size_write(inode, start + len);
7617
7618 WARN_ON(dio_data->reserve < len);
7619 dio_data->reserve -= len;
7620 dio_data->unsubmitted_oe_range_end = start + len;
7621 current->journal_info = dio_data;
7622out:
7623 return ret;
7624}
7625
Josef Bacik4b46fce2010-05-23 11:00:55 -04007626static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
7627 struct buffer_head *bh_result, int create)
7628{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007629 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007630 struct extent_map *em;
Josef Bacikeb838e72012-07-31 16:28:48 -04007631 struct extent_state *cached_state = NULL;
chandan50745b02015-08-28 21:10:13 +05307632 struct btrfs_dio_data *dio_data = NULL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007633 u64 start = iblock << inode->i_blkbits;
Josef Bacikeb838e72012-07-31 16:28:48 -04007634 u64 lockstart, lockend;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007635 u64 len = bh_result->b_size;
Josef Bacikeb838e72012-07-31 16:28:48 -04007636 int unlock_bits = EXTENT_LOCKED;
Miao Xie09348562013-02-07 10:12:07 +00007637 int ret = 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007638
Miao Xie172a5042013-02-21 02:48:22 -07007639 if (create)
Josef Bacik32667892015-02-11 15:08:58 -05007640 unlock_bits |= EXTENT_DIRTY;
Miao Xie172a5042013-02-21 02:48:22 -07007641 else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007642 len = min_t(u64, len, fs_info->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04007643
Josef Bacikc3298612012-08-03 16:49:19 -04007644 lockstart = start;
7645 lockend = start + len - 1;
7646
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007647 if (current->journal_info) {
7648 /*
7649 * Need to pull our outstanding extents and set journal_info to NULL so
Nicholas D Steeves01327612016-05-19 21:18:45 -04007650 * that anything that needs to check if there's a transaction doesn't get
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007651 * confused.
7652 */
chandan50745b02015-08-28 21:10:13 +05307653 dio_data = current->journal_info;
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007654 current->journal_info = NULL;
7655 }
7656
Josef Bacikeb838e72012-07-31 16:28:48 -04007657 /*
7658 * If this errors out it's because we couldn't invalidate pagecache for
7659 * this range and we need to fallback to buffered.
7660 */
Filipe Manana9c9464c2015-11-04 09:52:04 +00007661 if (lock_extent_direct(inode, lockstart, lockend, &cached_state,
7662 create)) {
7663 ret = -ENOTBLK;
7664 goto err;
7665 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007666
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007667 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len, 0);
Josef Bacikeb838e72012-07-31 16:28:48 -04007668 if (IS_ERR(em)) {
7669 ret = PTR_ERR(em);
7670 goto unlock_err;
7671 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007672
7673 /*
7674 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
7675 * io. INLINE is special, and we could probably kludge it in here, but
7676 * it's still buffered so for safety lets just fall back to the generic
7677 * buffered path.
7678 *
7679 * For COMPRESSED we _have_ to read the entire extent in so we can
7680 * decompress it, so there will be buffering required no matter what we
7681 * do, so go ahead and fallback to buffered.
7682 *
Nicholas D Steeves01327612016-05-19 21:18:45 -04007683 * We return -ENOTBLK because that's what makes DIO go ahead and go back
Josef Bacik4b46fce2010-05-23 11:00:55 -04007684 * to buffered IO. Don't blame me, this is the price we pay for using
7685 * the generic code.
7686 */
7687 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
7688 em->block_start == EXTENT_MAP_INLINE) {
7689 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007690 ret = -ENOTBLK;
7691 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007692 }
7693
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007694 if (create) {
7695 ret = btrfs_get_blocks_direct_write(&em, bh_result, inode,
7696 dio_data, start, len);
7697 if (ret < 0)
7698 goto unlock_err;
7699
7700 /* clear and unlock the entire range */
7701 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7702 unlock_bits, 1, 0, &cached_state);
7703 } else {
Nikolay Borisov1c8d0172018-05-02 15:19:32 +03007704 ret = btrfs_get_blocks_direct_read(em, bh_result, inode,
7705 start, len);
7706 /* Can be negative only if we read from a hole */
7707 if (ret < 0) {
7708 ret = 0;
7709 free_extent_map(em);
7710 goto unlock_err;
7711 }
7712 /*
7713 * We need to unlock only the end area that we aren't using.
7714 * The rest is going to be unlocked by the endio routine.
7715 */
7716 lockstart = start + bh_result->b_size;
7717 if (lockstart < lockend) {
7718 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
7719 lockend, unlock_bits, 1, 0,
7720 &cached_state);
7721 } else {
7722 free_extent_state(cached_state);
7723 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007724 }
7725
Josef Bacik4b46fce2010-05-23 11:00:55 -04007726 free_extent_map(em);
7727
7728 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007729
7730unlock_err:
Josef Bacikeb838e72012-07-31 16:28:48 -04007731 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbaae0f1622017-10-31 16:37:52 +01007732 unlock_bits, 1, 0, &cached_state);
Filipe Manana9c9464c2015-11-04 09:52:04 +00007733err:
chandan50745b02015-08-28 21:10:13 +05307734 if (dio_data)
7735 current->journal_info = dio_data;
Josef Bacikeb838e72012-07-31 16:28:48 -04007736 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007737}
7738
Omar Sandoval58efbc92017-08-22 23:45:59 -07007739static inline blk_status_t submit_dio_repair_bio(struct inode *inode,
7740 struct bio *bio,
7741 int mirror_num)
Miao Xie8b110e32014-09-12 18:44:03 +08007742{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007743 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Omar Sandoval58efbc92017-08-22 23:45:59 -07007744 blk_status_t ret;
Miao Xie8b110e32014-09-12 18:44:03 +08007745
Mike Christie37226b22016-06-05 14:31:52 -05007746 BUG_ON(bio_op(bio) == REQ_OP_WRITE);
Miao Xie8b110e32014-09-12 18:44:03 +08007747
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007748 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DIO_REPAIR);
Miao Xie8b110e32014-09-12 18:44:03 +08007749 if (ret)
Nikolay Borisovea057f62017-12-13 10:25:38 +02007750 return ret;
Miao Xie8b110e32014-09-12 18:44:03 +08007751
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007752 ret = btrfs_map_bio(fs_info, bio, mirror_num, 0);
Nikolay Borisovea057f62017-12-13 10:25:38 +02007753
Miao Xie8b110e32014-09-12 18:44:03 +08007754 return ret;
7755}
7756
7757static int btrfs_check_dio_repairable(struct inode *inode,
7758 struct bio *failed_bio,
7759 struct io_failure_record *failrec,
7760 int failed_mirror)
7761{
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007762 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xie8b110e32014-09-12 18:44:03 +08007763 int num_copies;
7764
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007765 num_copies = btrfs_num_copies(fs_info, failrec->logical, failrec->len);
Miao Xie8b110e32014-09-12 18:44:03 +08007766 if (num_copies == 1) {
7767 /*
7768 * we only have a single copy of the data, so don't bother with
7769 * all the retry and error correction code that follows. no
7770 * matter what the error is, it is very likely to persist.
7771 */
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007772 btrfs_debug(fs_info,
7773 "Check DIO Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d",
7774 num_copies, failrec->this_mirror, failed_mirror);
Miao Xie8b110e32014-09-12 18:44:03 +08007775 return 0;
7776 }
7777
7778 failrec->failed_mirror = failed_mirror;
7779 failrec->this_mirror++;
7780 if (failrec->this_mirror == failed_mirror)
7781 failrec->this_mirror++;
7782
7783 if (failrec->this_mirror > num_copies) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007784 btrfs_debug(fs_info,
7785 "Check DIO Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d",
7786 num_copies, failrec->this_mirror, failed_mirror);
Miao Xie8b110e32014-09-12 18:44:03 +08007787 return 0;
7788 }
7789
7790 return 1;
7791}
7792
Omar Sandoval58efbc92017-08-22 23:45:59 -07007793static blk_status_t dio_read_error(struct inode *inode, struct bio *failed_bio,
7794 struct page *page, unsigned int pgoff,
7795 u64 start, u64 end, int failed_mirror,
7796 bio_end_io_t *repair_endio, void *repair_arg)
Miao Xie8b110e32014-09-12 18:44:03 +08007797{
7798 struct io_failure_record *failrec;
Josef Bacik7870d082017-05-05 11:57:15 -04007799 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
7800 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
Miao Xie8b110e32014-09-12 18:44:03 +08007801 struct bio *bio;
7802 int isector;
David Sterbaf1c77c52017-06-06 19:03:49 +02007803 unsigned int read_mode = 0;
Liu Bo17347ce2017-05-15 15:33:27 -07007804 int segs;
Miao Xie8b110e32014-09-12 18:44:03 +08007805 int ret;
Omar Sandoval58efbc92017-08-22 23:45:59 -07007806 blk_status_t status;
Ming Leic16a8ac2017-12-18 20:22:12 +08007807 struct bio_vec bvec;
Miao Xie8b110e32014-09-12 18:44:03 +08007808
Mike Christie37226b22016-06-05 14:31:52 -05007809 BUG_ON(bio_op(failed_bio) == REQ_OP_WRITE);
Miao Xie8b110e32014-09-12 18:44:03 +08007810
7811 ret = btrfs_get_io_failure_record(inode, start, end, &failrec);
7812 if (ret)
Omar Sandoval58efbc92017-08-22 23:45:59 -07007813 return errno_to_blk_status(ret);
Miao Xie8b110e32014-09-12 18:44:03 +08007814
7815 ret = btrfs_check_dio_repairable(inode, failed_bio, failrec,
7816 failed_mirror);
7817 if (!ret) {
Josef Bacik7870d082017-05-05 11:57:15 -04007818 free_io_failure(failure_tree, io_tree, failrec);
Omar Sandoval58efbc92017-08-22 23:45:59 -07007819 return BLK_STS_IOERR;
Miao Xie8b110e32014-09-12 18:44:03 +08007820 }
7821
Liu Bo17347ce2017-05-15 15:33:27 -07007822 segs = bio_segments(failed_bio);
Ming Leic16a8ac2017-12-18 20:22:12 +08007823 bio_get_first_bvec(failed_bio, &bvec);
Liu Bo17347ce2017-05-15 15:33:27 -07007824 if (segs > 1 ||
Ming Leic16a8ac2017-12-18 20:22:12 +08007825 (bvec.bv_len > btrfs_inode_sectorsize(inode)))
Christoph Hellwig70fd7612016-11-01 07:40:10 -06007826 read_mode |= REQ_FAILFAST_DEV;
Miao Xie8b110e32014-09-12 18:44:03 +08007827
7828 isector = start - btrfs_io_bio(failed_bio)->logical;
7829 isector >>= inode->i_sb->s_blocksize_bits;
7830 bio = btrfs_create_repair_bio(inode, failed_bio, failrec, page,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307831 pgoff, isector, repair_endio, repair_arg);
David Sterbaebcc3262018-06-29 10:56:53 +02007832 bio->bi_opf = REQ_OP_READ | read_mode;
Miao Xie8b110e32014-09-12 18:44:03 +08007833
7834 btrfs_debug(BTRFS_I(inode)->root->fs_info,
David Sterba913e1532017-07-13 15:32:18 +02007835 "repair DIO read error: submitting new dio read[%#x] to this_mirror=%d, in_validation=%d",
Miao Xie8b110e32014-09-12 18:44:03 +08007836 read_mode, failrec->this_mirror, failrec->in_validation);
7837
Omar Sandoval58efbc92017-08-22 23:45:59 -07007838 status = submit_dio_repair_bio(inode, bio, failrec->this_mirror);
7839 if (status) {
Josef Bacik7870d082017-05-05 11:57:15 -04007840 free_io_failure(failure_tree, io_tree, failrec);
Miao Xie8b110e32014-09-12 18:44:03 +08007841 bio_put(bio);
7842 }
7843
Omar Sandoval58efbc92017-08-22 23:45:59 -07007844 return status;
Miao Xie8b110e32014-09-12 18:44:03 +08007845}
7846
7847struct btrfs_retry_complete {
7848 struct completion done;
7849 struct inode *inode;
7850 u64 start;
7851 int uptodate;
7852};
7853
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007854static void btrfs_retry_endio_nocsum(struct bio *bio)
Miao Xie8b110e32014-09-12 18:44:03 +08007855{
7856 struct btrfs_retry_complete *done = bio->bi_private;
Josef Bacik7870d082017-05-05 11:57:15 -04007857 struct inode *inode = done->inode;
Miao Xie8b110e32014-09-12 18:44:03 +08007858 struct bio_vec *bvec;
Josef Bacik7870d082017-05-05 11:57:15 -04007859 struct extent_io_tree *io_tree, *failure_tree;
Miao Xie8b110e32014-09-12 18:44:03 +08007860 int i;
Ming Lei6dc4f102019-02-15 19:13:19 +08007861 struct bvec_iter_all iter_all;
Miao Xie8b110e32014-09-12 18:44:03 +08007862
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02007863 if (bio->bi_status)
Miao Xie8b110e32014-09-12 18:44:03 +08007864 goto end;
7865
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307866 ASSERT(bio->bi_vcnt == 1);
Josef Bacik7870d082017-05-05 11:57:15 -04007867 io_tree = &BTRFS_I(inode)->io_tree;
7868 failure_tree = &BTRFS_I(inode)->io_failure_tree;
Ming Lei263663c2017-12-18 20:22:04 +08007869 ASSERT(bio_first_bvec_all(bio)->bv_len == btrfs_inode_sectorsize(inode));
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307870
Miao Xie8b110e32014-09-12 18:44:03 +08007871 done->uptodate = 1;
David Sterbac09abff2017-07-13 18:10:07 +02007872 ASSERT(!bio_flagged(bio, BIO_CLONED));
Ming Lei6dc4f102019-02-15 19:13:19 +08007873 bio_for_each_segment_all(bvec, bio, i, iter_all)
Josef Bacik7870d082017-05-05 11:57:15 -04007874 clean_io_failure(BTRFS_I(inode)->root->fs_info, failure_tree,
7875 io_tree, done->start, bvec->bv_page,
7876 btrfs_ino(BTRFS_I(inode)), 0);
Miao Xie8b110e32014-09-12 18:44:03 +08007877end:
7878 complete(&done->done);
7879 bio_put(bio);
7880}
7881
Omar Sandoval58efbc92017-08-22 23:45:59 -07007882static blk_status_t __btrfs_correct_data_nocsum(struct inode *inode,
7883 struct btrfs_io_bio *io_bio)
Josef Bacik4b46fce2010-05-23 11:00:55 -04007884{
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307885 struct btrfs_fs_info *fs_info;
Liu Bo17347ce2017-05-15 15:33:27 -07007886 struct bio_vec bvec;
7887 struct bvec_iter iter;
Miao Xie8b110e32014-09-12 18:44:03 +08007888 struct btrfs_retry_complete done;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007889 u64 start;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307890 unsigned int pgoff;
7891 u32 sectorsize;
7892 int nr_sectors;
Omar Sandoval58efbc92017-08-22 23:45:59 -07007893 blk_status_t ret;
7894 blk_status_t err = BLK_STS_OK;
Miao Xiedc380ae2014-09-12 18:43:55 +08007895
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307896 fs_info = BTRFS_I(inode)->root->fs_info;
Jeff Mahoneyda170662016-06-15 09:22:56 -04007897 sectorsize = fs_info->sectorsize;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307898
Miao Xiec1dc0892014-09-12 18:43:56 +08007899 start = io_bio->logical;
Miao Xie8b110e32014-09-12 18:44:03 +08007900 done.inode = inode;
Liu Bo17347ce2017-05-15 15:33:27 -07007901 io_bio->bio.bi_iter = io_bio->iter;
Miao Xie8b110e32014-09-12 18:44:03 +08007902
Liu Bo17347ce2017-05-15 15:33:27 -07007903 bio_for_each_segment(bvec, &io_bio->bio, iter) {
7904 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec.bv_len);
7905 pgoff = bvec.bv_offset;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307906
7907next_block_or_try_again:
Miao Xie8b110e32014-09-12 18:44:03 +08007908 done.uptodate = 0;
7909 done.start = start;
7910 init_completion(&done.done);
7911
Liu Bo17347ce2017-05-15 15:33:27 -07007912 ret = dio_read_error(inode, &io_bio->bio, bvec.bv_page,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307913 pgoff, start, start + sectorsize - 1,
7914 io_bio->mirror_num,
7915 btrfs_retry_endio_nocsum, &done);
Liu Bo629ebf42017-05-15 17:20:07 -07007916 if (ret) {
7917 err = ret;
7918 goto next;
7919 }
Miao Xie8b110e32014-09-12 18:44:03 +08007920
David Sterba9c17f6c2017-07-19 19:26:45 +02007921 wait_for_completion_io(&done.done);
Miao Xie8b110e32014-09-12 18:44:03 +08007922
7923 if (!done.uptodate) {
7924 /* We might have another mirror, so try again */
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307925 goto next_block_or_try_again;
Miao Xie8b110e32014-09-12 18:44:03 +08007926 }
7927
Liu Bo629ebf42017-05-15 17:20:07 -07007928next:
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307929 start += sectorsize;
7930
Liu Bo97bf5a52017-04-07 13:11:10 -07007931 nr_sectors--;
7932 if (nr_sectors) {
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307933 pgoff += sectorsize;
Liu Bo97bf5a52017-04-07 13:11:10 -07007934 ASSERT(pgoff < PAGE_SIZE);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307935 goto next_block_or_try_again;
7936 }
Miao Xie8b110e32014-09-12 18:44:03 +08007937 }
7938
Liu Bo629ebf42017-05-15 17:20:07 -07007939 return err;
Miao Xie8b110e32014-09-12 18:44:03 +08007940}
7941
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007942static void btrfs_retry_endio(struct bio *bio)
Miao Xie8b110e32014-09-12 18:44:03 +08007943{
7944 struct btrfs_retry_complete *done = bio->bi_private;
7945 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
Josef Bacik7870d082017-05-05 11:57:15 -04007946 struct extent_io_tree *io_tree, *failure_tree;
7947 struct inode *inode = done->inode;
Miao Xie8b110e32014-09-12 18:44:03 +08007948 struct bio_vec *bvec;
7949 int uptodate;
7950 int ret;
7951 int i;
Ming Lei6dc4f102019-02-15 19:13:19 +08007952 struct bvec_iter_all iter_all;
Miao Xie8b110e32014-09-12 18:44:03 +08007953
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02007954 if (bio->bi_status)
Miao Xie8b110e32014-09-12 18:44:03 +08007955 goto end;
7956
7957 uptodate = 1;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307958
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307959 ASSERT(bio->bi_vcnt == 1);
Ming Lei263663c2017-12-18 20:22:04 +08007960 ASSERT(bio_first_bvec_all(bio)->bv_len == btrfs_inode_sectorsize(done->inode));
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307961
Josef Bacik7870d082017-05-05 11:57:15 -04007962 io_tree = &BTRFS_I(inode)->io_tree;
7963 failure_tree = &BTRFS_I(inode)->io_failure_tree;
7964
David Sterbac09abff2017-07-13 18:10:07 +02007965 ASSERT(!bio_flagged(bio, BIO_CLONED));
Ming Lei6dc4f102019-02-15 19:13:19 +08007966 bio_for_each_segment_all(bvec, bio, i, iter_all) {
Josef Bacik7870d082017-05-05 11:57:15 -04007967 ret = __readpage_endio_check(inode, io_bio, i, bvec->bv_page,
7968 bvec->bv_offset, done->start,
7969 bvec->bv_len);
Miao Xie8b110e32014-09-12 18:44:03 +08007970 if (!ret)
Josef Bacik7870d082017-05-05 11:57:15 -04007971 clean_io_failure(BTRFS_I(inode)->root->fs_info,
7972 failure_tree, io_tree, done->start,
7973 bvec->bv_page,
7974 btrfs_ino(BTRFS_I(inode)),
7975 bvec->bv_offset);
Miao Xie8b110e32014-09-12 18:44:03 +08007976 else
7977 uptodate = 0;
7978 }
7979
7980 done->uptodate = uptodate;
7981end:
7982 complete(&done->done);
7983 bio_put(bio);
7984}
7985
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02007986static blk_status_t __btrfs_subio_endio_read(struct inode *inode,
7987 struct btrfs_io_bio *io_bio, blk_status_t err)
Miao Xie8b110e32014-09-12 18:44:03 +08007988{
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307989 struct btrfs_fs_info *fs_info;
Liu Bo17347ce2017-05-15 15:33:27 -07007990 struct bio_vec bvec;
7991 struct bvec_iter iter;
Miao Xie8b110e32014-09-12 18:44:03 +08007992 struct btrfs_retry_complete done;
7993 u64 start;
7994 u64 offset = 0;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307995 u32 sectorsize;
7996 int nr_sectors;
7997 unsigned int pgoff;
7998 int csum_pos;
Liu Boef7cdac12017-04-13 18:11:48 -07007999 bool uptodate = (err == 0);
Miao Xie8b110e32014-09-12 18:44:03 +08008000 int ret;
Omar Sandoval58efbc92017-08-22 23:45:59 -07008001 blk_status_t status;
Miao Xie8b110e32014-09-12 18:44:03 +08008002
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308003 fs_info = BTRFS_I(inode)->root->fs_info;
Jeff Mahoneyda170662016-06-15 09:22:56 -04008004 sectorsize = fs_info->sectorsize;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308005
Omar Sandoval58efbc92017-08-22 23:45:59 -07008006 err = BLK_STS_OK;
Miao Xie8b110e32014-09-12 18:44:03 +08008007 start = io_bio->logical;
8008 done.inode = inode;
Liu Bo17347ce2017-05-15 15:33:27 -07008009 io_bio->bio.bi_iter = io_bio->iter;
Miao Xie8b110e32014-09-12 18:44:03 +08008010
Liu Bo17347ce2017-05-15 15:33:27 -07008011 bio_for_each_segment(bvec, &io_bio->bio, iter) {
8012 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec.bv_len);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308013
Liu Bo17347ce2017-05-15 15:33:27 -07008014 pgoff = bvec.bv_offset;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308015next_block:
Liu Boef7cdac12017-04-13 18:11:48 -07008016 if (uptodate) {
8017 csum_pos = BTRFS_BYTES_TO_BLKS(fs_info, offset);
8018 ret = __readpage_endio_check(inode, io_bio, csum_pos,
8019 bvec.bv_page, pgoff, start, sectorsize);
8020 if (likely(!ret))
8021 goto next;
8022 }
Miao Xie8b110e32014-09-12 18:44:03 +08008023try_again:
8024 done.uptodate = 0;
8025 done.start = start;
8026 init_completion(&done.done);
8027
Omar Sandoval58efbc92017-08-22 23:45:59 -07008028 status = dio_read_error(inode, &io_bio->bio, bvec.bv_page,
8029 pgoff, start, start + sectorsize - 1,
8030 io_bio->mirror_num, btrfs_retry_endio,
8031 &done);
8032 if (status) {
8033 err = status;
Miao Xie8b110e32014-09-12 18:44:03 +08008034 goto next;
8035 }
8036
David Sterba9c17f6c2017-07-19 19:26:45 +02008037 wait_for_completion_io(&done.done);
Miao Xie8b110e32014-09-12 18:44:03 +08008038
8039 if (!done.uptodate) {
8040 /* We might have another mirror, so try again */
8041 goto try_again;
8042 }
8043next:
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308044 offset += sectorsize;
8045 start += sectorsize;
8046
8047 ASSERT(nr_sectors);
8048
Liu Bo97bf5a52017-04-07 13:11:10 -07008049 nr_sectors--;
8050 if (nr_sectors) {
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308051 pgoff += sectorsize;
Liu Bo97bf5a52017-04-07 13:11:10 -07008052 ASSERT(pgoff < PAGE_SIZE);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308053 goto next_block;
8054 }
Kent Overstreet2c30c712013-11-07 12:20:26 -08008055 }
Miao Xiec1dc0892014-09-12 18:43:56 +08008056
8057 return err;
8058}
8059
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008060static blk_status_t btrfs_subio_endio_read(struct inode *inode,
8061 struct btrfs_io_bio *io_bio, blk_status_t err)
Miao Xie8b110e32014-09-12 18:44:03 +08008062{
8063 bool skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
8064
8065 if (skip_csum) {
8066 if (unlikely(err))
8067 return __btrfs_correct_data_nocsum(inode, io_bio);
8068 else
Omar Sandoval58efbc92017-08-22 23:45:59 -07008069 return BLK_STS_OK;
Miao Xie8b110e32014-09-12 18:44:03 +08008070 } else {
8071 return __btrfs_subio_endio_read(inode, io_bio, err);
8072 }
8073}
8074
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008075static void btrfs_endio_direct_read(struct bio *bio)
Miao Xiec1dc0892014-09-12 18:43:56 +08008076{
8077 struct btrfs_dio_private *dip = bio->bi_private;
8078 struct inode *inode = dip->inode;
8079 struct bio *dio_bio;
8080 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008081 blk_status_t err = bio->bi_status;
Miao Xiec1dc0892014-09-12 18:43:56 +08008082
Liu Bo99c4e3b92017-09-15 15:06:51 -06008083 if (dip->flags & BTRFS_DIO_ORIG_BIO_SUBMITTED)
Miao Xie8b110e32014-09-12 18:44:03 +08008084 err = btrfs_subio_endio_read(inode, io_bio, err);
Miao Xiec1dc0892014-09-12 18:43:56 +08008085
Josef Bacik4b46fce2010-05-23 11:00:55 -04008086 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01008087 dip->logical_offset + dip->bytes - 1);
Chris Mason9be33952013-05-17 18:30:14 -04008088 dio_bio = dip->dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008089
Josef Bacik4b46fce2010-05-23 11:00:55 -04008090 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04008091
Liu Bo99c4e3b92017-09-15 15:06:51 -06008092 dio_bio->bi_status = err;
Christoph Hellwig40553512017-06-03 09:37:58 +02008093 dio_end_io(dio_bio);
David Sterbab3a0dd52018-11-22 17:16:49 +01008094 btrfs_io_bio_free_csum(io_bio);
Chris Mason9be33952013-05-17 18:30:14 -04008095 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008096}
8097
Qu Wenruo524272602017-03-08 10:25:52 +08008098static void __endio_write_update_ordered(struct inode *inode,
8099 const u64 offset, const u64 bytes,
8100 const bool uptodate)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008101{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008102 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008103 struct btrfs_ordered_extent *ordered = NULL;
Qu Wenruo524272602017-03-08 10:25:52 +08008104 struct btrfs_workqueue *wq;
8105 btrfs_work_func_t func;
Filipe Manana14543772015-11-24 16:23:54 +00008106 u64 ordered_offset = offset;
8107 u64 ordered_bytes = bytes;
Naohiro Aota67c003f2017-09-01 17:59:07 +09008108 u64 last_offset;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008109
Qu Wenruo524272602017-03-08 10:25:52 +08008110 if (btrfs_is_free_space_inode(BTRFS_I(inode))) {
8111 wq = fs_info->endio_freespace_worker;
8112 func = btrfs_freespace_write_helper;
8113 } else {
8114 wq = fs_info->endio_write_workers;
8115 func = btrfs_endio_write_helper;
8116 }
8117
Nikolay Borisovb25f0d02018-04-11 11:21:17 +03008118 while (ordered_offset < offset + bytes) {
8119 last_offset = ordered_offset;
8120 if (btrfs_dec_test_first_ordered_pending(inode, &ordered,
8121 &ordered_offset,
8122 ordered_bytes,
8123 uptodate)) {
8124 btrfs_init_work(&ordered->work, func,
8125 finish_ordered_fn,
8126 NULL, NULL);
8127 btrfs_queue_work(wq, &ordered->work);
8128 }
8129 /*
8130 * If btrfs_dec_test_ordered_pending does not find any ordered
8131 * extent in the range, we can exit.
8132 */
8133 if (ordered_offset == last_offset)
8134 return;
8135 /*
8136 * Our bio might span multiple ordered extents. In this case
Andrea Gelmini52042d82018-11-28 12:05:13 +01008137 * we keep going until we have accounted the whole dio.
Nikolay Borisovb25f0d02018-04-11 11:21:17 +03008138 */
8139 if (ordered_offset < offset + bytes) {
8140 ordered_bytes = offset + bytes - ordered_offset;
8141 ordered = NULL;
8142 }
Chris Mason163cf092010-11-28 19:56:33 -05008143 }
Filipe Manana14543772015-11-24 16:23:54 +00008144}
8145
8146static void btrfs_endio_direct_write(struct bio *bio)
8147{
8148 struct btrfs_dio_private *dip = bio->bi_private;
8149 struct bio *dio_bio = dip->dio_bio;
8150
Qu Wenruo524272602017-03-08 10:25:52 +08008151 __endio_write_update_ordered(dip->inode, dip->logical_offset,
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008152 dip->bytes, !bio->bi_status);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008153
Josef Bacik4b46fce2010-05-23 11:00:55 -04008154 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04008155
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008156 dio_bio->bi_status = bio->bi_status;
Christoph Hellwig40553512017-06-03 09:37:58 +02008157 dio_end_io(dio_bio);
Chris Mason9be33952013-05-17 18:30:14 -04008158 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008159}
8160
David Sterbad0ee3932018-03-08 14:35:48 +01008161static blk_status_t btrfs_submit_bio_start_direct_io(void *private_data,
David Sterbad0779292018-03-08 13:47:33 +01008162 struct bio *bio, u64 offset)
Chris Masoneaf25d92010-05-25 09:48:28 -04008163{
Josef Bacikc6100a42017-05-05 11:57:13 -04008164 struct inode *inode = private_data;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008165 blk_status_t ret;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008166 ret = btrfs_csum_one_bio(inode, bio, offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008167 BUG_ON(ret); /* -ENOMEM */
Chris Masoneaf25d92010-05-25 09:48:28 -04008168 return 0;
8169}
8170
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008171static void btrfs_end_dio_bio(struct bio *bio)
Miao Xiee65e1532010-11-22 03:04:43 +00008172{
8173 struct btrfs_dio_private *dip = bio->bi_private;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008174 blk_status_t err = bio->bi_status;
Miao Xiee65e1532010-11-22 03:04:43 +00008175
Miao Xie8b110e32014-09-12 18:44:03 +08008176 if (err)
8177 btrfs_warn(BTRFS_I(dip->inode)->root->fs_info,
Mike Christie6296b962016-06-05 14:32:21 -05008178 "direct IO failed ino %llu rw %d,%u sector %#Lx len %u err no %d",
David Sterbaf85b7372017-01-20 14:54:07 +01008179 btrfs_ino(BTRFS_I(dip->inode)), bio_op(bio),
8180 bio->bi_opf,
Miao Xie8b110e32014-09-12 18:44:03 +08008181 (unsigned long long)bio->bi_iter.bi_sector,
8182 bio->bi_iter.bi_size, err);
8183
8184 if (dip->subio_endio)
8185 err = dip->subio_endio(dip->inode, btrfs_io_bio(bio), err);
Miao Xiec1dc0892014-09-12 18:43:56 +08008186
8187 if (err) {
Miao Xiee65e1532010-11-22 03:04:43 +00008188 /*
Nikolay Borisovde224b72018-02-14 10:53:36 +02008189 * We want to perceive the errors flag being set before
8190 * decrementing the reference count. We don't need a barrier
8191 * since atomic operations with a return value are fully
8192 * ordered as per atomic_t.txt
Miao Xiee65e1532010-11-22 03:04:43 +00008193 */
Nikolay Borisovde224b72018-02-14 10:53:36 +02008194 dip->errors = 1;
Miao Xiee65e1532010-11-22 03:04:43 +00008195 }
8196
8197 /* if there are more bios still pending for this dio, just exit */
8198 if (!atomic_dec_and_test(&dip->pending_bios))
8199 goto out;
8200
Chris Mason9be33952013-05-17 18:30:14 -04008201 if (dip->errors) {
Miao Xiee65e1532010-11-22 03:04:43 +00008202 bio_io_error(dip->orig_bio);
Chris Mason9be33952013-05-17 18:30:14 -04008203 } else {
Anand Jain2dbe0c72017-10-14 08:35:56 +08008204 dip->dio_bio->bi_status = BLK_STS_OK;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008205 bio_endio(dip->orig_bio);
Miao Xiee65e1532010-11-22 03:04:43 +00008206 }
8207out:
8208 bio_put(bio);
8209}
8210
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008211static inline blk_status_t btrfs_lookup_and_bind_dio_csum(struct inode *inode,
Miao Xiec1dc0892014-09-12 18:43:56 +08008212 struct btrfs_dio_private *dip,
8213 struct bio *bio,
8214 u64 file_offset)
8215{
8216 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
8217 struct btrfs_io_bio *orig_io_bio = btrfs_io_bio(dip->orig_bio);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008218 blk_status_t ret;
Miao Xiec1dc0892014-09-12 18:43:56 +08008219
8220 /*
8221 * We load all the csum data we need when we submit
8222 * the first bio to reduce the csum tree search and
8223 * contention.
8224 */
8225 if (dip->logical_offset == file_offset) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008226 ret = btrfs_lookup_bio_sums_dio(inode, dip->orig_bio,
Miao Xiec1dc0892014-09-12 18:43:56 +08008227 file_offset);
8228 if (ret)
8229 return ret;
8230 }
8231
8232 if (bio == dip->orig_bio)
8233 return 0;
8234
8235 file_offset -= dip->logical_offset;
8236 file_offset >>= inode->i_sb->s_blocksize_bits;
8237 io_bio->csum = (u8 *)(((u32 *)orig_io_bio->csum) + file_offset);
8238
8239 return 0;
8240}
8241
David Sterbad0ee3932018-03-08 14:35:48 +01008242static inline blk_status_t btrfs_submit_dio_bio(struct bio *bio,
8243 struct inode *inode, u64 file_offset, int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00008244{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008245 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xiefacc8a222013-07-25 19:22:34 +08008246 struct btrfs_dio_private *dip = bio->bi_private;
Mike Christie37226b22016-06-05 14:31:52 -05008247 bool write = bio_op(bio) == REQ_OP_WRITE;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008248 blk_status_t ret;
Miao Xiee65e1532010-11-22 03:04:43 +00008249
Liu Bo4c274bc2017-11-01 17:19:27 -06008250 /* Check btrfs_submit_bio_hook() for rules about async submit. */
Josef Bacikb812ce22012-11-16 13:56:32 -05008251 if (async_submit)
8252 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
8253
Josef Bacik5fd02042012-05-02 14:00:54 -04008254 if (!write) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008255 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DATA);
Josef Bacik5fd02042012-05-02 14:00:54 -04008256 if (ret)
8257 goto err;
8258 }
Miao Xiee65e1532010-11-22 03:04:43 +00008259
Nikolay Borisove6961ca2017-08-03 15:44:58 +03008260 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Josef Bacik1ae39932011-04-06 14:41:34 -04008261 goto map;
8262
8263 if (write && async_submit) {
Josef Bacikc6100a42017-05-05 11:57:13 -04008264 ret = btrfs_wq_submit_bio(fs_info, bio, 0, 0,
8265 file_offset, inode,
David Sterbae288c082018-07-18 17:36:24 +02008266 btrfs_submit_bio_start_direct_io);
Miao Xiee65e1532010-11-22 03:04:43 +00008267 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04008268 } else if (write) {
8269 /*
8270 * If we aren't doing async submit, calculate the csum of the
8271 * bio now.
8272 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008273 ret = btrfs_csum_one_bio(inode, bio, file_offset, 1);
Josef Bacik1ae39932011-04-06 14:41:34 -04008274 if (ret)
8275 goto err;
Miao Xie23ea8e52014-09-12 18:43:54 +08008276 } else {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008277 ret = btrfs_lookup_and_bind_dio_csum(inode, dip, bio,
Miao Xiec1dc0892014-09-12 18:43:56 +08008278 file_offset);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00008279 if (ret)
8280 goto err;
8281 }
Josef Bacik1ae39932011-04-06 14:41:34 -04008282map:
Liu Bo9b4a9b22017-08-18 11:54:02 -06008283 ret = btrfs_map_bio(fs_info, bio, 0, 0);
Miao Xiee65e1532010-11-22 03:04:43 +00008284err:
Miao Xiee65e1532010-11-22 03:04:43 +00008285 return ret;
8286}
8287
Nikolay Borisove6961ca2017-08-03 15:44:58 +03008288static int btrfs_submit_direct_hook(struct btrfs_dio_private *dip)
Miao Xiee65e1532010-11-22 03:04:43 +00008289{
8290 struct inode *inode = dip->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008291 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xiee65e1532010-11-22 03:04:43 +00008292 struct bio *bio;
8293 struct bio *orig_bio = dip->orig_bio;
Kent Overstreet4f024f32013-10-11 15:44:27 -07008294 u64 start_sector = orig_bio->bi_iter.bi_sector;
Miao Xiee65e1532010-11-22 03:04:43 +00008295 u64 file_offset = dip->logical_offset;
Miao Xiee65e1532010-11-22 03:04:43 +00008296 u64 map_length;
Josef Bacik1ae39932011-04-06 14:41:34 -04008297 int async_submit = 0;
Liu Bo725130b2017-05-16 09:51:39 -07008298 u64 submit_len;
8299 int clone_offset = 0;
8300 int clone_len;
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05308301 int ret;
Omar Sandoval58efbc92017-08-22 23:45:59 -07008302 blk_status_t status;
Miao Xiee65e1532010-11-22 03:04:43 +00008303
Kent Overstreet4f024f32013-10-11 15:44:27 -07008304 map_length = orig_bio->bi_iter.bi_size;
Liu Bo725130b2017-05-16 09:51:39 -07008305 submit_len = map_length;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008306 ret = btrfs_map_block(fs_info, btrfs_op(orig_bio), start_sector << 9,
8307 &map_length, NULL, 0);
Miao Xie7a5c3c92014-06-17 18:58:59 +08008308 if (ret)
Miao Xiee65e1532010-11-22 03:04:43 +00008309 return -EIO;
Miao Xiefacc8a222013-07-25 19:22:34 +08008310
Liu Bo725130b2017-05-16 09:51:39 -07008311 if (map_length >= submit_len) {
Josef Bacik02f57c72011-04-06 14:25:44 -04008312 bio = orig_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08008313 dip->flags |= BTRFS_DIO_ORIG_BIO_SUBMITTED;
Josef Bacik02f57c72011-04-06 14:25:44 -04008314 goto submit;
8315 }
8316
David Woodhouse53b381b2013-01-29 18:40:14 -05008317 /* async crcs make it difficult to collect full stripe writes. */
Jeff Mahoney1b868262017-05-17 11:38:35 -04008318 if (btrfs_data_alloc_profile(fs_info) & BTRFS_BLOCK_GROUP_RAID56_MASK)
David Woodhouse53b381b2013-01-29 18:40:14 -05008319 async_submit = 0;
8320 else
8321 async_submit = 1;
8322
Liu Bo725130b2017-05-16 09:51:39 -07008323 /* bio split */
8324 ASSERT(map_length <= INT_MAX);
Josef Bacik02f57c72011-04-06 14:25:44 -04008325 atomic_inc(&dip->pending_bios);
Arnd Bergmann3c91ee62017-05-18 15:33:29 +02008326 do {
Liu Bo725130b2017-05-16 09:51:39 -07008327 clone_len = min_t(int, submit_len, map_length);
Josef Bacik02f57c72011-04-06 14:25:44 -04008328
Liu Bo725130b2017-05-16 09:51:39 -07008329 /*
8330 * This will never fail as it's passing GPF_NOFS and
8331 * the allocation is backed by btrfs_bioset.
8332 */
Liu Boe4770942017-05-16 10:57:14 -07008333 bio = btrfs_bio_clone_partial(orig_bio, clone_offset,
Liu Bo725130b2017-05-16 09:51:39 -07008334 clone_len);
8335 bio->bi_private = dip;
8336 bio->bi_end_io = btrfs_end_dio_bio;
8337 btrfs_io_bio(bio)->logical = file_offset;
Miao Xiee65e1532010-11-22 03:04:43 +00008338
Liu Bo725130b2017-05-16 09:51:39 -07008339 ASSERT(submit_len >= clone_len);
8340 submit_len -= clone_len;
8341 if (submit_len == 0)
8342 break;
Miao Xiee65e1532010-11-22 03:04:43 +00008343
Liu Bo725130b2017-05-16 09:51:39 -07008344 /*
8345 * Increase the count before we submit the bio so we know
8346 * the end IO handler won't happen before we increase the
8347 * count. Otherwise, the dip might get freed before we're
8348 * done setting it up.
8349 */
8350 atomic_inc(&dip->pending_bios);
Miao Xiee65e1532010-11-22 03:04:43 +00008351
David Sterbad0ee3932018-03-08 14:35:48 +01008352 status = btrfs_submit_dio_bio(bio, inode, file_offset,
Omar Sandoval58efbc92017-08-22 23:45:59 -07008353 async_submit);
8354 if (status) {
Liu Bo725130b2017-05-16 09:51:39 -07008355 bio_put(bio);
8356 atomic_dec(&dip->pending_bios);
8357 goto out_err;
Miao Xiee65e1532010-11-22 03:04:43 +00008358 }
Liu Bo725130b2017-05-16 09:51:39 -07008359
8360 clone_offset += clone_len;
8361 start_sector += clone_len >> 9;
8362 file_offset += clone_len;
8363
8364 map_length = submit_len;
8365 ret = btrfs_map_block(fs_info, btrfs_op(orig_bio),
8366 start_sector << 9, &map_length, NULL, 0);
8367 if (ret)
8368 goto out_err;
Arnd Bergmann3c91ee62017-05-18 15:33:29 +02008369 } while (submit_len > 0);
Miao Xiee65e1532010-11-22 03:04:43 +00008370
Josef Bacik02f57c72011-04-06 14:25:44 -04008371submit:
David Sterbad0ee3932018-03-08 14:35:48 +01008372 status = btrfs_submit_dio_bio(bio, inode, file_offset, async_submit);
Omar Sandoval58efbc92017-08-22 23:45:59 -07008373 if (!status)
Miao Xiee65e1532010-11-22 03:04:43 +00008374 return 0;
8375
8376 bio_put(bio);
8377out_err:
8378 dip->errors = 1;
8379 /*
Nikolay Borisovde224b72018-02-14 10:53:36 +02008380 * Before atomic variable goto zero, we must make sure dip->errors is
8381 * perceived to be set. This ordering is ensured by the fact that an
8382 * atomic operations with a return value are fully ordered as per
8383 * atomic_t.txt
Miao Xiee65e1532010-11-22 03:04:43 +00008384 */
Miao Xiee65e1532010-11-22 03:04:43 +00008385 if (atomic_dec_and_test(&dip->pending_bios))
8386 bio_io_error(dip->orig_bio);
8387
8388 /* bio_end_io() will handle error, so we needn't return it */
8389 return 0;
8390}
8391
Mike Christie8a4c1e42016-06-05 14:31:50 -05008392static void btrfs_submit_direct(struct bio *dio_bio, struct inode *inode,
8393 loff_t file_offset)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008394{
Filipe Manana61de7182015-07-01 12:13:10 +01008395 struct btrfs_dio_private *dip = NULL;
Liu Bo3892ac92017-04-17 15:00:28 -07008396 struct bio *bio = NULL;
8397 struct btrfs_io_bio *io_bio;
Mike Christie8a4c1e42016-06-05 14:31:50 -05008398 bool write = (bio_op(dio_bio) == REQ_OP_WRITE);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008399 int ret = 0;
8400
David Sterba8b6c1d52017-06-02 17:48:13 +02008401 bio = btrfs_bio_clone(dio_bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008402
Miao Xiec1dc0892014-09-12 18:43:56 +08008403 dip = kzalloc(sizeof(*dip), GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04008404 if (!dip) {
8405 ret = -ENOMEM;
Filipe Manana61de7182015-07-01 12:13:10 +01008406 goto free_ordered;
Chris Mason9be33952013-05-17 18:30:14 -04008407 }
8408
8409 dip->private = dio_bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008410 dip->inode = inode;
8411 dip->logical_offset = file_offset;
Kent Overstreet4f024f32013-10-11 15:44:27 -07008412 dip->bytes = dio_bio->bi_iter.bi_size;
8413 dip->disk_bytenr = (u64)dio_bio->bi_iter.bi_sector << 9;
Liu Bo3892ac92017-04-17 15:00:28 -07008414 bio->bi_private = dip;
8415 dip->orig_bio = bio;
Chris Mason9be33952013-05-17 18:30:14 -04008416 dip->dio_bio = dio_bio;
Miao Xiee65e1532010-11-22 03:04:43 +00008417 atomic_set(&dip->pending_bios, 0);
Liu Bo3892ac92017-04-17 15:00:28 -07008418 io_bio = btrfs_io_bio(bio);
8419 io_bio->logical = file_offset;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008420
Miao Xiec1dc0892014-09-12 18:43:56 +08008421 if (write) {
Liu Bo3892ac92017-04-17 15:00:28 -07008422 bio->bi_end_io = btrfs_endio_direct_write;
Miao Xiec1dc0892014-09-12 18:43:56 +08008423 } else {
Liu Bo3892ac92017-04-17 15:00:28 -07008424 bio->bi_end_io = btrfs_endio_direct_read;
Miao Xiec1dc0892014-09-12 18:43:56 +08008425 dip->subio_endio = btrfs_subio_endio_read;
8426 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04008427
Filipe Mananaf28a4922015-12-08 19:23:20 +00008428 /*
8429 * Reset the range for unsubmitted ordered extents (to a 0 length range)
8430 * even if we fail to submit a bio, because in such case we do the
8431 * corresponding error handling below and it must not be done a second
8432 * time by btrfs_direct_IO().
8433 */
8434 if (write) {
8435 struct btrfs_dio_data *dio_data = current->journal_info;
8436
8437 dio_data->unsubmitted_oe_range_end = dip->logical_offset +
8438 dip->bytes;
8439 dio_data->unsubmitted_oe_range_start =
8440 dio_data->unsubmitted_oe_range_end;
8441 }
8442
Nikolay Borisove6961ca2017-08-03 15:44:58 +03008443 ret = btrfs_submit_direct_hook(dip);
Miao Xiee65e1532010-11-22 03:04:43 +00008444 if (!ret)
Chris Masoneaf25d92010-05-25 09:48:28 -04008445 return;
Chris Mason9be33952013-05-17 18:30:14 -04008446
David Sterbab3a0dd52018-11-22 17:16:49 +01008447 btrfs_io_bio_free_csum(io_bio);
Chris Mason9be33952013-05-17 18:30:14 -04008448
Josef Bacik4b46fce2010-05-23 11:00:55 -04008449free_ordered:
8450 /*
Filipe Manana61de7182015-07-01 12:13:10 +01008451 * If we arrived here it means either we failed to submit the dip
8452 * or we either failed to clone the dio_bio or failed to allocate the
8453 * dip. If we cloned the dio_bio and allocated the dip, we can just
8454 * call bio_endio against our io_bio so that we get proper resource
8455 * cleanup if we fail to submit the dip, otherwise, we must do the
8456 * same as btrfs_endio_direct_[write|read] because we can't call these
8457 * callbacks - they require an allocated dip and a clone of dio_bio.
Josef Bacik4b46fce2010-05-23 11:00:55 -04008458 */
Liu Bo3892ac92017-04-17 15:00:28 -07008459 if (bio && dip) {
Guoqing Jiang054ec2f2017-06-02 16:08:50 +08008460 bio_io_error(bio);
Filipe Manana61de7182015-07-01 12:13:10 +01008461 /*
Liu Bo3892ac92017-04-17 15:00:28 -07008462 * The end io callbacks free our dip, do the final put on bio
Filipe Manana61de7182015-07-01 12:13:10 +01008463 * and all the cleanup and final put for dio_bio (through
8464 * dio_end_io()).
8465 */
8466 dip = NULL;
Liu Bo3892ac92017-04-17 15:00:28 -07008467 bio = NULL;
Filipe Manana61de7182015-07-01 12:13:10 +01008468 } else {
Filipe Manana14543772015-11-24 16:23:54 +00008469 if (write)
Qu Wenruo524272602017-03-08 10:25:52 +08008470 __endio_write_update_ordered(inode,
Filipe Manana14543772015-11-24 16:23:54 +00008471 file_offset,
8472 dio_bio->bi_iter.bi_size,
Qu Wenruo524272602017-03-08 10:25:52 +08008473 false);
Filipe Manana14543772015-11-24 16:23:54 +00008474 else
Filipe Manana61de7182015-07-01 12:13:10 +01008475 unlock_extent(&BTRFS_I(inode)->io_tree, file_offset,
8476 file_offset + dio_bio->bi_iter.bi_size - 1);
Filipe Manana14543772015-11-24 16:23:54 +00008477
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008478 dio_bio->bi_status = BLK_STS_IOERR;
Filipe Manana61de7182015-07-01 12:13:10 +01008479 /*
8480 * Releases and cleans up our dio_bio, no need to bio_put()
8481 * nor bio_endio()/bio_io_error() against dio_bio.
8482 */
Christoph Hellwig40553512017-06-03 09:37:58 +02008483 dio_end_io(dio_bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008484 }
Liu Bo3892ac92017-04-17 15:00:28 -07008485 if (bio)
8486 bio_put(bio);
Filipe Manana61de7182015-07-01 12:13:10 +01008487 kfree(dip);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008488}
8489
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008490static ssize_t check_direct_IO(struct btrfs_fs_info *fs_info,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008491 const struct iov_iter *iter, loff_t offset)
Chris Mason5a5f79b2010-05-26 21:33:37 -04008492{
8493 int seg;
Josef Bacika1b75f72011-04-08 15:51:18 +00008494 int i;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008495 unsigned int blocksize_mask = fs_info->sectorsize - 1;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008496 ssize_t retval = -EINVAL;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008497
8498 if (offset & blocksize_mask)
8499 goto out;
8500
Al Viro28060d52014-03-22 05:15:17 -04008501 if (iov_iter_alignment(iter) & blocksize_mask)
8502 goto out;
Josef Bacika1b75f72011-04-08 15:51:18 +00008503
Al Viro28060d52014-03-22 05:15:17 -04008504 /* If this is a write we don't need to check anymore */
Al Virocd27e452016-10-10 13:39:05 -04008505 if (iov_iter_rw(iter) != READ || !iter_is_iovec(iter))
Al Viro28060d52014-03-22 05:15:17 -04008506 return 0;
8507 /*
8508 * Check to make sure we don't have duplicate iov_base's in this
8509 * iovec, if so return EINVAL, otherwise we'll get csum errors
8510 * when reading back.
8511 */
8512 for (seg = 0; seg < iter->nr_segs; seg++) {
8513 for (i = seg + 1; i < iter->nr_segs; i++) {
8514 if (iter->iov[seg].iov_base == iter->iov[i].iov_base)
Josef Bacika1b75f72011-04-08 15:51:18 +00008515 goto out;
8516 }
Chris Mason5a5f79b2010-05-26 21:33:37 -04008517 }
8518 retval = 0;
8519out:
8520 return retval;
8521}
Josef Bacikeb838e72012-07-31 16:28:48 -04008522
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008523static ssize_t btrfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
Chris Mason16432982008-04-10 10:23:21 -04008524{
Josef Bacik4b46fce2010-05-23 11:00:55 -04008525 struct file *file = iocb->ki_filp;
8526 struct inode *inode = file->f_mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008527 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
chandan50745b02015-08-28 21:10:13 +05308528 struct btrfs_dio_data dio_data = { 0 };
Qu Wenruo364ecf32017-02-27 15:10:38 +08008529 struct extent_changeset *data_reserved = NULL;
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008530 loff_t offset = iocb->ki_pos;
Miao Xie09348562013-02-07 10:12:07 +00008531 size_t count = 0;
Miao Xie2e60a512013-02-08 07:01:08 +00008532 int flags = 0;
Miao Xie38851cc2013-02-08 07:04:11 +00008533 bool wakeup = true;
8534 bool relock = false;
Miao Xie09348562013-02-07 10:12:07 +00008535 ssize_t ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008536
Nikolay Borisov8c70c9f2017-08-21 12:43:46 +03008537 if (check_direct_IO(fs_info, iter, offset))
Chris Mason5a5f79b2010-05-26 21:33:37 -04008538 return 0;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008539
Jens Axboefe0f07d2015-04-15 17:05:48 -06008540 inode_dio_begin(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008541
Josef Bacik0e267c42013-07-02 10:38:02 -04008542 /*
Miao Xie41bd9ca2014-03-06 13:54:57 +08008543 * The generic stuff only does filemap_write_and_wait_range, which
8544 * isn't enough if we've written compressed pages to this area, so
8545 * we need to flush the dirty pages again to make absolutely sure
8546 * that any outstanding dirty pages are on disk.
Josef Bacik0e267c42013-07-02 10:38:02 -04008547 */
Al Viroa6cbcd42014-03-04 22:38:00 -05008548 count = iov_iter_count(iter);
Miao Xie41bd9ca2014-03-06 13:54:57 +08008549 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
8550 &BTRFS_I(inode)->runtime_flags))
Wang Shilong9a025a02014-07-17 11:44:13 +08008551 filemap_fdatawrite_range(inode->i_mapping, offset,
8552 offset + count - 1);
Josef Bacik0e267c42013-07-02 10:38:02 -04008553
Omar Sandoval6f673762015-03-16 04:33:52 -07008554 if (iov_iter_rw(iter) == WRITE) {
Miao Xie38851cc2013-02-08 07:04:11 +00008555 /*
8556 * If the write DIO is beyond the EOF, we need update
8557 * the isize, but it is protected by i_mutex. So we can
8558 * not unlock the i_mutex at this case.
8559 */
8560 if (offset + count <= inode->i_size) {
Liu Bo4aaedfb2016-12-14 22:36:05 -08008561 dio_data.overwrite = 1;
Al Viro59551022016-01-22 15:40:57 -05008562 inode_unlock(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008563 relock = true;
Goldwyn Rodriguesedf064e2017-06-20 07:05:49 -05008564 } else if (iocb->ki_flags & IOCB_NOWAIT) {
8565 ret = -EAGAIN;
8566 goto out;
Miao Xie38851cc2013-02-08 07:04:11 +00008567 }
Qu Wenruo364ecf32017-02-27 15:10:38 +08008568 ret = btrfs_delalloc_reserve_space(inode, &data_reserved,
8569 offset, count);
Miao Xie09348562013-02-07 10:12:07 +00008570 if (ret)
Miao Xie38851cc2013-02-08 07:04:11 +00008571 goto out;
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008572
8573 /*
8574 * We need to know how many extents we reserved so that we can
8575 * do the accounting properly if we go over the number we
8576 * originally calculated. Abuse current->journal_info for this.
8577 */
Jeff Mahoneyda170662016-06-15 09:22:56 -04008578 dio_data.reserve = round_up(count,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008579 fs_info->sectorsize);
Filipe Mananaf28a4922015-12-08 19:23:20 +00008580 dio_data.unsubmitted_oe_range_start = (u64)offset;
8581 dio_data.unsubmitted_oe_range_end = (u64)offset;
chandan50745b02015-08-28 21:10:13 +05308582 current->journal_info = &dio_data;
Chandan Rajendra97dcdea2016-12-23 15:00:18 +05308583 down_read(&BTRFS_I(inode)->dio_sem);
David Sterbaee39b432014-09-30 01:33:33 +02008584 } else if (test_bit(BTRFS_INODE_READDIO_NEED_LOCK,
8585 &BTRFS_I(inode)->runtime_flags)) {
Jens Axboefe0f07d2015-04-15 17:05:48 -06008586 inode_dio_end(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008587 flags = DIO_LOCKING | DIO_SKIP_HOLES;
8588 wakeup = false;
Miao Xie09348562013-02-07 10:12:07 +00008589 }
8590
Omar Sandoval17f8c842015-03-16 04:33:50 -07008591 ret = __blockdev_direct_IO(iocb, inode,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008592 fs_info->fs_devices->latest_bdev,
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008593 iter, btrfs_get_blocks_direct, NULL,
Omar Sandoval17f8c842015-03-16 04:33:50 -07008594 btrfs_submit_direct, flags);
Omar Sandoval6f673762015-03-16 04:33:52 -07008595 if (iov_iter_rw(iter) == WRITE) {
Chandan Rajendra97dcdea2016-12-23 15:00:18 +05308596 up_read(&BTRFS_I(inode)->dio_sem);
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008597 current->journal_info = NULL;
Liu Boddba1bf2015-06-17 16:59:58 +08008598 if (ret < 0 && ret != -EIOCBQUEUED) {
chandan50745b02015-08-28 21:10:13 +05308599 if (dio_data.reserve)
Qu Wenruobc42bda2017-02-27 15:10:39 +08008600 btrfs_delalloc_release_space(inode, data_reserved,
Qu Wenruo43b18592017-12-12 15:34:32 +08008601 offset, dio_data.reserve, true);
Filipe Mananaf28a4922015-12-08 19:23:20 +00008602 /*
8603 * On error we might have left some ordered extents
8604 * without submitting corresponding bios for them, so
8605 * cleanup them up to avoid other tasks getting them
8606 * and waiting for them to complete forever.
8607 */
8608 if (dio_data.unsubmitted_oe_range_start <
8609 dio_data.unsubmitted_oe_range_end)
Qu Wenruo524272602017-03-08 10:25:52 +08008610 __endio_write_update_ordered(inode,
Filipe Mananaf28a4922015-12-08 19:23:20 +00008611 dio_data.unsubmitted_oe_range_start,
8612 dio_data.unsubmitted_oe_range_end -
8613 dio_data.unsubmitted_oe_range_start,
Qu Wenruo524272602017-03-08 10:25:52 +08008614 false);
Liu Boddba1bf2015-06-17 16:59:58 +08008615 } else if (ret >= 0 && (size_t)ret < count)
Qu Wenruobc42bda2017-02-27 15:10:39 +08008616 btrfs_delalloc_release_space(inode, data_reserved,
Qu Wenruo43b18592017-12-12 15:34:32 +08008617 offset, count - (size_t)ret, true);
8618 btrfs_delalloc_release_extents(BTRFS_I(inode), count, false);
Miao Xie09348562013-02-07 10:12:07 +00008619 }
Miao Xie38851cc2013-02-08 07:04:11 +00008620out:
Miao Xie2e60a512013-02-08 07:01:08 +00008621 if (wakeup)
Jens Axboefe0f07d2015-04-15 17:05:48 -06008622 inode_dio_end(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008623 if (relock)
Al Viro59551022016-01-22 15:40:57 -05008624 inode_lock(inode);
Miao Xie09348562013-02-07 10:12:07 +00008625
Qu Wenruo364ecf32017-02-27 15:10:38 +08008626 extent_changeset_free(data_reserved);
Miao Xie09348562013-02-07 10:12:07 +00008627 return ret;
Chris Mason16432982008-04-10 10:23:21 -04008628}
8629
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008630#define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
8631
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008632static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
8633 __u64 start, __u64 len)
8634{
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008635 int ret;
8636
8637 ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS);
8638 if (ret)
8639 return ret;
8640
David Sterba2135fb92017-06-23 04:09:57 +02008641 return extent_fiemap(inode, fieinfo, start, len);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008642}
8643
Chris Mason9ebefb182007-06-15 13:50:00 -04008644int btrfs_readpage(struct file *file, struct page *page)
8645{
Chris Masond1310b22008-01-24 16:13:08 -05008646 struct extent_io_tree *tree;
8647 tree = &BTRFS_I(page->mapping->host)->io_tree;
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02008648 return extent_read_full_page(tree, page, btrfs_get_extent, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04008649}
Chris Mason1832a6d2007-12-21 16:27:21 -05008650
Chris Mason39279cc2007-06-12 06:35:45 -04008651static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
8652{
Josef Bacikbe7bd732015-10-22 15:05:09 -04008653 struct inode *inode = page->mapping->host;
8654 int ret;
Chris Masonb888db22007-08-27 16:49:44 -04008655
8656 if (current->flags & PF_MEMALLOC) {
8657 redirty_page_for_writepage(wbc, page);
8658 unlock_page(page);
8659 return 0;
8660 }
Josef Bacikbe7bd732015-10-22 15:05:09 -04008661
8662 /*
8663 * If we are under memory pressure we will call this directly from the
8664 * VM, we need to make sure we have the inode referenced for the ordered
8665 * extent. If not just return like we didn't do anything.
8666 */
8667 if (!igrab(inode)) {
8668 redirty_page_for_writepage(wbc, page);
8669 return AOP_WRITEPAGE_ACTIVATE;
8670 }
Nikolay Borisov0a9b0e52017-12-08 15:55:59 +02008671 ret = extent_write_full_page(page, wbc);
Josef Bacikbe7bd732015-10-22 15:05:09 -04008672 btrfs_add_delayed_iput(inode);
8673 return ret;
Chris Masona52d9a82007-08-27 16:49:44 -04008674}
Chris Mason39279cc2007-06-12 06:35:45 -04008675
Eric Sandeen48a3b632013-04-25 20:41:01 +00008676static int btrfs_writepages(struct address_space *mapping,
8677 struct writeback_control *wbc)
Chris Masonb293f02e2007-11-01 19:45:34 -04008678{
Nikolay Borisov8ae225a2018-04-19 10:46:38 +03008679 return extent_writepages(mapping, wbc);
Chris Masonb293f02e2007-11-01 19:45:34 -04008680}
8681
Chris Mason3ab2fb52007-11-08 10:59:22 -05008682static int
8683btrfs_readpages(struct file *file, struct address_space *mapping,
8684 struct list_head *pages, unsigned nr_pages)
8685{
Nikolay Borisov2a3ff0a2018-04-19 10:46:36 +03008686 return extent_readpages(mapping, pages, nr_pages);
Chris Mason3ab2fb52007-11-08 10:59:22 -05008687}
Nikolay Borisov2a3ff0a2018-04-19 10:46:36 +03008688
Chris Masone6dcd2d2008-07-17 12:53:50 -04008689static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04008690{
Nikolay Borisov477a30b2018-04-19 10:46:34 +03008691 int ret = try_release_extent_mapping(page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04008692 if (ret == 1) {
8693 ClearPagePrivate(page);
8694 set_page_private(page, 0);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008695 put_page(page);
Chris Masona52d9a82007-08-27 16:49:44 -04008696 }
8697 return ret;
8698}
Chris Mason39279cc2007-06-12 06:35:45 -04008699
Chris Masone6dcd2d2008-07-17 12:53:50 -04008700static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
8701{
Chris Mason98509cf2008-09-11 15:51:43 -04008702 if (PageWriteback(page) || PageDirty(page))
8703 return 0;
Michal Hocko3ba7ab22017-01-09 15:39:02 +01008704 return __btrfs_releasepage(page, gfp_flags);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008705}
8706
Lukas Czernerd47992f2013-05-21 23:17:23 -04008707static void btrfs_invalidatepage(struct page *page, unsigned int offset,
8708 unsigned int length)
Chris Masona52d9a82007-08-27 16:49:44 -04008709{
Josef Bacik5fd02042012-05-02 14:00:54 -04008710 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05008711 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008712 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008713 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008714 u64 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008715 u64 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308716 u64 start;
8717 u64 end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008718 int inode_evicting = inode->i_state & I_FREEING;
Chris Masona52d9a82007-08-27 16:49:44 -04008719
Chris Mason8b62b722009-09-02 16:53:46 -04008720 /*
8721 * we have the page locked, so new writeback can't start,
8722 * and the dirty bit won't be cleared while we are here.
8723 *
8724 * Wait for IO on this page so that we can safely clear
8725 * the PagePrivate2 bit and do ordered accounting
8726 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008727 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04008728
Josef Bacik5fd02042012-05-02 14:00:54 -04008729 tree = &BTRFS_I(inode)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008730 if (offset) {
8731 btrfs_releasepage(page, GFP_NOFS);
8732 return;
8733 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008734
8735 if (!inode_evicting)
David Sterbaff13db42015-12-03 14:30:40 +01008736 lock_extent_bits(tree, page_start, page_end, &cached_state);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308737again:
8738 start = page_start;
Nikolay Borisova776c6f2017-02-20 13:50:49 +02008739 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), start,
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308740 page_end - start + 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008741 if (ordered) {
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308742 end = min(page_end, ordered->file_offset + ordered->len - 1);
Chris Masoneb84ae02008-07-17 13:53:27 -04008743 /*
8744 * IO on this page will never be started, so we need
8745 * to account for any ordered extents now
8746 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008747 if (!inode_evicting)
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308748 clear_extent_bit(tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008749 EXTENT_DIRTY | EXTENT_DELALLOC |
Filipe Mananaa7e3b972017-04-03 10:45:46 +01008750 EXTENT_DELALLOC_NEW |
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008751 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
David Sterbaae0f1622017-10-31 16:37:52 +01008752 EXTENT_DEFRAG, 1, 0, &cached_state);
Chris Mason8b62b722009-09-02 16:53:46 -04008753 /*
8754 * whoever cleared the private bit is responsible
8755 * for the finish_ordered_io
8756 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04008757 if (TestClearPagePrivate2(page)) {
8758 struct btrfs_ordered_inode_tree *tree;
8759 u64 new_len;
8760
8761 tree = &BTRFS_I(inode)->ordered_tree;
8762
8763 spin_lock_irq(&tree->lock);
8764 set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308765 new_len = start - ordered->file_offset;
Josef Bacik77cef2e2013-08-29 13:57:21 -04008766 if (new_len < ordered->truncated_len)
8767 ordered->truncated_len = new_len;
8768 spin_unlock_irq(&tree->lock);
8769
8770 if (btrfs_dec_test_ordered_pending(inode, &ordered,
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308771 start,
8772 end - start + 1, 1))
Josef Bacik77cef2e2013-08-29 13:57:21 -04008773 btrfs_finish_ordered_io(ordered);
Chris Mason8b62b722009-09-02 16:53:46 -04008774 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008775 btrfs_put_ordered_extent(ordered);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008776 if (!inode_evicting) {
8777 cached_state = NULL;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308778 lock_extent_bits(tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008779 &cached_state);
8780 }
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308781
8782 start = end + 1;
8783 if (start < page_end)
8784 goto again;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008785 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008786
Qu Wenruob9d0b382015-09-29 10:35:16 +08008787 /*
8788 * Qgroup reserved space handler
8789 * Page here will be either
8790 * 1) Already written to disk
8791 * In this case, its reserved space is released from data rsv map
8792 * and will be freed by delayed_ref handler finally.
8793 * So even we call qgroup_free_data(), it won't decrease reserved
8794 * space.
8795 * 2) Not written to disk
Goldwyn Rodrigues0b34c262016-09-30 10:40:52 -05008796 * This means the reserved space should be freed here. However,
8797 * if a truncate invalidates the page (by clearing PageDirty)
8798 * and the page is accounted for while allocating extent
8799 * in btrfs_check_data_free_space() we let delayed_ref to
8800 * free the entire extent.
Qu Wenruob9d0b382015-09-29 10:35:16 +08008801 */
Goldwyn Rodrigues0b34c262016-09-30 10:40:52 -05008802 if (PageDirty(page))
Qu Wenruobc42bda2017-02-27 15:10:39 +08008803 btrfs_qgroup_free_data(inode, NULL, page_start, PAGE_SIZE);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008804 if (!inode_evicting) {
8805 clear_extent_bit(tree, page_start, page_end,
8806 EXTENT_LOCKED | EXTENT_DIRTY |
Filipe Mananaa7e3b972017-04-03 10:45:46 +01008807 EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
8808 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1,
David Sterbaae0f1622017-10-31 16:37:52 +01008809 &cached_state);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008810
8811 __btrfs_releasepage(page, GFP_NOFS);
8812 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008813
Chris Mason4a096752008-07-21 10:29:44 -04008814 ClearPageChecked(page);
Chris Mason9ad6b7bc2008-04-18 16:11:30 -04008815 if (PagePrivate(page)) {
Chris Mason9ad6b7bc2008-04-18 16:11:30 -04008816 ClearPagePrivate(page);
8817 set_page_private(page, 0);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008818 put_page(page);
Chris Mason9ad6b7bc2008-04-18 16:11:30 -04008819 }
Chris Mason39279cc2007-06-12 06:35:45 -04008820}
8821
Chris Mason9ebefb182007-06-15 13:50:00 -04008822/*
8823 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
8824 * called from a page fault handler when a page is first dirtied. Hence we must
8825 * be careful to check for EOF conditions here. We set the page up correctly
8826 * for a written page which means we get ENOSPC checking when writing into
8827 * holes and correct delalloc and unwritten extent mapping on filesystems that
8828 * support these features.
8829 *
8830 * We are not allowed to take the i_mutex here so we have to play games to
8831 * protect against truncate races as the page could now be beyond EOF. Because
Omar Sandovald1342aa2018-05-11 13:13:29 -07008832 * truncate_setsize() writes the inode size before removing pages, once we have
8833 * the page lock we can determine safely if the page is beyond EOF. If it is not
Chris Mason9ebefb182007-06-15 13:50:00 -04008834 * beyond EOF, then the page is guaranteed safe against truncation until we
8835 * unlock the page.
8836 */
Souptick Joardera528a242018-06-06 19:54:44 +05308837vm_fault_t btrfs_page_mkwrite(struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04008838{
Nick Pigginc2ec1752009-03-31 15:23:21 -07008839 struct page *page = vmf->page;
Dave Jiang11bac802017-02-24 14:56:41 -08008840 struct inode *inode = file_inode(vmf->vma->vm_file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008841 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008842 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
8843 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008844 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08008845 struct extent_changeset *data_reserved = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008846 char *kaddr;
8847 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04008848 loff_t size;
Souptick Joardera528a242018-06-06 19:54:44 +05308849 vm_fault_t ret;
8850 int ret2;
Chris Mason9998eb72012-01-25 13:47:40 -05008851 int reserved = 0;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308852 u64 reserved_space;
Chris Masona52d9a82007-08-27 16:49:44 -04008853 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008854 u64 page_end;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308855 u64 end;
8856
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008857 reserved_space = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04008858
Jan Karab2b5ef52012-06-12 16:20:45 +02008859 sb_start_pagefault(inode->i_sb);
Qu Wenruodf480632015-09-08 17:25:54 +08008860 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008861 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308862 end = page_end;
Qu Wenruodf480632015-09-08 17:25:54 +08008863
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308864 /*
8865 * Reserving delalloc space after obtaining the page lock can lead to
8866 * deadlock. For example, if a dirty page is locked by this function
8867 * and the call to btrfs_delalloc_reserve_space() ends up triggering
8868 * dirty page write out, then the btrfs_writepage() function could
8869 * end up waiting indefinitely to get a lock on the page currently
8870 * being processed by btrfs_page_mkwrite() function.
8871 */
Souptick Joardera528a242018-06-06 19:54:44 +05308872 ret2 = btrfs_delalloc_reserve_space(inode, &data_reserved, page_start,
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308873 reserved_space);
Souptick Joardera528a242018-06-06 19:54:44 +05308874 if (!ret2) {
8875 ret2 = file_update_time(vmf->vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05008876 reserved = 1;
8877 }
Souptick Joardera528a242018-06-06 19:54:44 +05308878 if (ret2) {
8879 ret = vmf_error(ret2);
Chris Mason9998eb72012-01-25 13:47:40 -05008880 if (reserved)
8881 goto out;
8882 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07008883 }
Chris Mason1832a6d2007-12-21 16:27:21 -05008884
Nick Piggin56a76f82009-03-31 15:23:23 -07008885 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008886again:
Chris Mason9ebefb182007-06-15 13:50:00 -04008887 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04008888 size = i_size_read(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04008889
Chris Mason9ebefb182007-06-15 13:50:00 -04008890 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04008891 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04008892 /* page got truncated out from underneath us */
8893 goto out_unlock;
8894 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008895 wait_on_page_writeback(page);
8896
David Sterbaff13db42015-12-03 14:30:40 +01008897 lock_extent_bits(io_tree, page_start, page_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008898 set_page_extent_mapped(page);
8899
Chris Masoneb84ae02008-07-17 13:53:27 -04008900 /*
8901 * we can't set the delalloc bits if there are pending ordered
8902 * extents. Drop our locks and wait for them to finish
8903 */
Nikolay Borisova776c6f2017-02-20 13:50:49 +02008904 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start,
8905 PAGE_SIZE);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008906 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00008907 unlock_extent_cached(io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01008908 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008909 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04008910 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008911 btrfs_put_ordered_extent(ordered);
8912 goto again;
8913 }
8914
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008915 if (page->index == ((size - 1) >> PAGE_SHIFT)) {
Jeff Mahoneyda170662016-06-15 09:22:56 -04008916 reserved_space = round_up(size - page_start,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008917 fs_info->sectorsize);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008918 if (reserved_space < PAGE_SIZE) {
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308919 end = page_start + reserved_space - 1;
Qu Wenruobc42bda2017-02-27 15:10:39 +08008920 btrfs_delalloc_release_space(inode, data_reserved,
Qu Wenruo43b18592017-12-12 15:34:32 +08008921 page_start, PAGE_SIZE - reserved_space,
8922 true);
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308923 }
8924 }
8925
Josef Bacikfbf19082009-10-01 17:10:23 -04008926 /*
Liu Bo54160342016-12-13 12:15:19 -08008927 * page_mkwrite gets called when the page is firstly dirtied after it's
8928 * faulted in, but write(2) could also dirty a page and set delalloc
8929 * bits, thus in this case for space account reason, we still need to
8930 * clear any delalloc bits within this page range since we have to
8931 * reserve data&meta space before lock_page() (see above comments).
Josef Bacikfbf19082009-10-01 17:10:23 -04008932 */
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308933 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06008934 EXTENT_DIRTY | EXTENT_DELALLOC |
8935 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
David Sterbaae0f1622017-10-31 16:37:52 +01008936 0, 0, &cached_state);
Josef Bacikfbf19082009-10-01 17:10:23 -04008937
Souptick Joardera528a242018-06-06 19:54:44 +05308938 ret2 = btrfs_set_extent_delalloc(inode, page_start, end, 0,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08008939 &cached_state, 0);
Souptick Joardera528a242018-06-06 19:54:44 +05308940 if (ret2) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00008941 unlock_extent_cached(io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01008942 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04008943 ret = VM_FAULT_SIGBUS;
8944 goto out_unlock;
8945 }
Souptick Joardera528a242018-06-06 19:54:44 +05308946 ret2 = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04008947
8948 /* page is wholly or partially inside EOF */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008949 if (page_start + PAGE_SIZE > size)
Johannes Thumshirn70730172018-12-05 15:23:03 +01008950 zero_start = offset_in_page(size);
Chris Mason9ebefb182007-06-15 13:50:00 -04008951 else
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008952 zero_start = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04008953
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008954 if (zero_start != PAGE_SIZE) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04008955 kaddr = kmap(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008956 memset(kaddr + zero_start, 0, PAGE_SIZE - zero_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008957 flush_dcache_page(page);
8958 kunmap(page);
8959 }
Chris Mason247e7432008-07-17 12:53:51 -04008960 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008961 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04008962 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04008963
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008964 BTRFS_I(inode)->last_trans = fs_info->generation;
Chris Mason257c62e2009-10-13 13:21:08 -04008965 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06008966 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
Chris Mason257c62e2009-10-13 13:21:08 -04008967
David Sterbae43bbe52017-12-12 21:43:52 +01008968 unlock_extent_cached(io_tree, page_start, page_end, &cached_state);
Chris Mason9ebefb182007-06-15 13:50:00 -04008969
Souptick Joardera528a242018-06-06 19:54:44 +05308970 if (!ret2) {
Qu Wenruo43b18592017-12-12 15:34:32 +08008971 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE, true);
Jan Karab2b5ef52012-06-12 16:20:45 +02008972 sb_end_pagefault(inode->i_sb);
Qu Wenruo364ecf32017-02-27 15:10:38 +08008973 extent_changeset_free(data_reserved);
Chris Mason50a9b212009-09-11 12:33:12 -04008974 return VM_FAULT_LOCKED;
Jan Karab2b5ef52012-06-12 16:20:45 +02008975 }
Chris Mason717beb92018-06-25 10:03:41 -07008976
8977out_unlock:
Chris Mason9ebefb182007-06-15 13:50:00 -04008978 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05008979out:
Qu Wenruo43b18592017-12-12 15:34:32 +08008980 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE, (ret != 0));
Qu Wenruobc42bda2017-02-27 15:10:39 +08008981 btrfs_delalloc_release_space(inode, data_reserved, page_start,
Qu Wenruo43b18592017-12-12 15:34:32 +08008982 reserved_space, (ret != 0));
Chris Mason9998eb72012-01-25 13:47:40 -05008983out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02008984 sb_end_pagefault(inode->i_sb);
Qu Wenruo364ecf32017-02-27 15:10:38 +08008985 extent_changeset_free(data_reserved);
Chris Mason9ebefb182007-06-15 13:50:00 -04008986 return ret;
8987}
8988
Filipe Manana213e8c52018-02-06 20:40:31 +00008989static int btrfs_truncate(struct inode *inode, bool skip_writeback)
Chris Mason39279cc2007-06-12 06:35:45 -04008990{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008991 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04008992 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04008993 struct btrfs_block_rsv *rsv;
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008994 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04008995 struct btrfs_trans_handle *trans;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008996 u64 mask = fs_info->sectorsize - 1;
8997 u64 min_size = btrfs_calc_trunc_metadata_size(fs_info, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04008998
Filipe Manana213e8c52018-02-06 20:40:31 +00008999 if (!skip_writeback) {
9000 ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask),
9001 (u64)-1);
9002 if (ret)
9003 return ret;
9004 }
Chris Mason39279cc2007-06-12 06:35:45 -04009005
Josef Bacikfcb80c22011-05-03 10:40:22 -04009006 /*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07009007 * Yes ladies and gentlemen, this is indeed ugly. We have a couple of
9008 * things going on here:
Josef Bacikfcb80c22011-05-03 10:40:22 -04009009 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07009010 * 1) We need to reserve space to update our inode.
Josef Bacikfcb80c22011-05-03 10:40:22 -04009011 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07009012 * 2) We need to have something to cache all the space that is going to
Josef Bacikfcb80c22011-05-03 10:40:22 -04009013 * be free'd up by the truncate operation, but also have some slack
9014 * space reserved in case it uses space during the truncate (thank you
9015 * very much snapshotting).
9016 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07009017 * And we need these to be separate. The fact is we can use a lot of
Josef Bacikfcb80c22011-05-03 10:40:22 -04009018 * space doing the truncate, and we have no earthly idea how much space
Nicholas D Steeves01327612016-05-19 21:18:45 -04009019 * we will use, so we need the truncate reservation to be separate so it
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07009020 * doesn't end up using space reserved for updating the inode. We also
9021 * need to be able to stop the transaction and start a new one, which
9022 * means we need to be able to update the inode several times, and we
9023 * have no idea of knowing how many times that will be, so we can't just
9024 * reserve 1 item for the entirety of the operation, so that has to be
9025 * done separately as well.
Josef Bacikfcb80c22011-05-03 10:40:22 -04009026 *
9027 * So that leaves us with
9028 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07009029 * 1) rsv - for the truncate reservation, which we will steal from the
Josef Bacikfcb80c22011-05-03 10:40:22 -04009030 * transaction reservation.
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07009031 * 2) fs_info->trans_block_rsv - this will have 1 items worth left for
Josef Bacikfcb80c22011-05-03 10:40:22 -04009032 * updating the inode.
9033 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009034 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009035 if (!rsv)
9036 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04009037 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04009038 rsv->failfast = 1;
Josef Bacikf0cd8462011-03-04 14:37:08 -05009039
Josef Bacik907cbce2011-08-08 13:46:15 -04009040 /*
Josef Bacik07127182011-08-19 10:29:59 -04009041 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04009042 * 1 for updating the inode.
9043 */
Josef Bacikf3fe8202013-01-07 17:03:21 -05009044 trans = btrfs_start_transaction(root, 2);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009045 if (IS_ERR(trans)) {
Omar Sandovalad7e1a72018-05-22 09:59:50 -07009046 ret = PTR_ERR(trans);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009047 goto out;
9048 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05009049
Josef Bacik907cbce2011-08-08 13:46:15 -04009050 /* Migrate the slack space for the truncate to our reserve */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009051 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, rsv,
Lu Fengqi3a584172018-08-04 21:10:55 +08009052 min_size, false);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009053 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05009054
Chris Mason5a3f23d2009-03-31 13:27:11 -04009055 /*
Josef Bacik5dc562c2012-08-17 13:14:17 -04009056 * So if we truncate and then write and fsync we normally would just
9057 * write the extents that changed, which is a problem if we need to
9058 * first truncate that entire inode. So set this flag so we write out
9059 * all of the extents in the inode to the sync log so we're completely
9060 * safe.
9061 */
9062 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009063 trans->block_rsv = rsv;
Josef Bacik5dc562c2012-08-17 13:14:17 -04009064
Yan, Zheng80825102009-11-12 09:35:36 +00009065 while (1) {
9066 ret = btrfs_truncate_inode_items(trans, root, inode,
9067 inode->i_size,
9068 BTRFS_EXTENT_DATA_KEY);
Josef Bacikddfae632017-10-19 14:16:02 -04009069 trans->block_rsv = &fs_info->trans_block_rsv;
Omar Sandovalad7e1a72018-05-22 09:59:50 -07009070 if (ret != -ENOSPC && ret != -EAGAIN)
Yan, Zheng80825102009-11-12 09:35:36 +00009071 break;
Chris Mason39279cc2007-06-12 06:35:45 -04009072
Yan, Zheng80825102009-11-12 09:35:36 +00009073 ret = btrfs_update_inode(trans, root, inode);
Omar Sandovalad7e1a72018-05-22 09:59:50 -07009074 if (ret)
Josef Bacik3893e332011-01-31 16:03:11 -05009075 break;
Josef Bacikca7e70f2012-08-27 17:48:15 -04009076
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009077 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009078 btrfs_btree_balance_dirty(fs_info);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009079
9080 trans = btrfs_start_transaction(root, 2);
9081 if (IS_ERR(trans)) {
Omar Sandovalad7e1a72018-05-22 09:59:50 -07009082 ret = PTR_ERR(trans);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009083 trans = NULL;
9084 break;
9085 }
9086
Wang Xiaoguang47b5d642016-09-07 20:17:38 +08009087 btrfs_block_rsv_release(fs_info, rsv, -1);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009088 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv,
Lu Fengqi3a584172018-08-04 21:10:55 +08009089 rsv, min_size, false);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009090 BUG_ON(ret); /* shouldn't happen */
9091 trans->block_rsv = rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00009092 }
9093
Josef Bacikddfae632017-10-19 14:16:02 -04009094 /*
9095 * We can't call btrfs_truncate_block inside a trans handle as we could
9096 * deadlock with freeze, if we got NEED_TRUNCATE_BLOCK then we know
9097 * we've truncated everything except the last little bit, and can do
9098 * btrfs_truncate_block and then update the disk_i_size.
9099 */
9100 if (ret == NEED_TRUNCATE_BLOCK) {
9101 btrfs_end_transaction(trans);
9102 btrfs_btree_balance_dirty(fs_info);
9103
9104 ret = btrfs_truncate_block(inode, inode->i_size, 0, 0);
9105 if (ret)
9106 goto out;
9107 trans = btrfs_start_transaction(root, 1);
9108 if (IS_ERR(trans)) {
9109 ret = PTR_ERR(trans);
9110 goto out;
9111 }
9112 btrfs_ordered_update_i_size(inode, inode->i_size, NULL);
9113 }
9114
Chris Mason917c16b2011-11-08 14:49:59 -05009115 if (trans) {
Omar Sandovalad7e1a72018-05-22 09:59:50 -07009116 int ret2;
Josef Bacik7b128762008-07-24 12:17:14 -04009117
Omar Sandovalad7e1a72018-05-22 09:59:50 -07009118 trans->block_rsv = &fs_info->trans_block_rsv;
9119 ret2 = btrfs_update_inode(trans, root, inode);
9120 if (ret2 && !ret)
9121 ret = ret2;
9122
9123 ret2 = btrfs_end_transaction(trans);
9124 if (ret2 && !ret)
9125 ret = ret2;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009126 btrfs_btree_balance_dirty(fs_info);
Chris Mason917c16b2011-11-08 14:49:59 -05009127 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04009128out:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009129 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009130
Omar Sandovalad7e1a72018-05-22 09:59:50 -07009131 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04009132}
9133
Sven Wegener3b963622008-06-09 21:57:42 -04009134/*
Chris Masond352ac62008-09-29 15:18:18 -04009135 * create a new subvolume directory/inode (helper for the ioctl).
9136 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05009137int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009138 struct btrfs_root *new_root,
9139 struct btrfs_root *parent_root,
9140 u64 new_dirid)
Chris Mason39279cc2007-06-12 06:35:45 -04009141{
Chris Mason39279cc2007-06-12 06:35:45 -04009142 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04009143 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04009144 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009145
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01009146 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
9147 new_dirid, new_dirid,
9148 S_IFDIR | (~current_umask() & S_IRWXUGO),
9149 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04009150 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04009151 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009152 inode->i_op = &btrfs_dir_inode_operations;
9153 inode->i_fop = &btrfs_dir_file_operations;
9154
Miklos Szeredibfe86842011-10-28 14:13:29 +02009155 set_nlink(inode, 1);
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02009156 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Masonb0d5d102014-09-08 13:08:51 -07009157 unlock_new_inode(inode);
Sven Wegener3b963622008-06-09 21:57:42 -04009158
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009159 err = btrfs_subvol_inherit_props(trans, new_root, parent_root);
9160 if (err)
9161 btrfs_err(new_root->fs_info,
David Sterba351fd352014-05-15 16:48:20 +02009162 "error inheriting subvolume %llu properties: %d",
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009163 new_root->root_key.objectid, err);
9164
Yan, Zheng76dda932009-09-21 16:00:26 -04009165 err = btrfs_update_inode(trans, new_root, inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04009166
Yan, Zheng76dda932009-09-21 16:00:26 -04009167 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07009168 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04009169}
9170
Chris Mason39279cc2007-06-12 06:35:45 -04009171struct inode *btrfs_alloc_inode(struct super_block *sb)
9172{
Josef Bacik69fe2d72017-10-19 14:15:57 -04009173 struct btrfs_fs_info *fs_info = btrfs_sb(sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009174 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009175 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009176
David Sterba712e36c2017-10-31 17:08:27 +01009177 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_KERNEL);
Chris Mason39279cc2007-06-12 06:35:45 -04009178 if (!ei)
9179 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009180
9181 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009182 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04009183 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04009184 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04009185 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009186 ei->delalloc_bytes = 0;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01009187 ei->new_delalloc_bytes = 0;
Wang Shilong47059d92014-07-03 18:22:07 +08009188 ei->defrag_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009189 ei->disk_i_size = 0;
9190 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04009191 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009192 ei->index_cnt = (u64)-1;
Miao Xie67de1172013-12-26 13:07:06 +08009193 ei->dir_index = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009194 ei->last_unlink_trans = 0;
Filipe Manana41bd6062018-11-28 14:54:28 +00009195 ei->last_link_trans = 0;
Liu Bo46d8bc32012-08-29 01:07:55 -06009196 ei->last_log_commit = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009197
Josef Bacik9e0baf62011-07-15 15:16:44 +00009198 spin_lock_init(&ei->lock);
9199 ei->outstanding_extents = 0;
Josef Bacik69fe2d72017-10-19 14:15:57 -04009200 if (sb->s_magic != BTRFS_TEST_MAGIC)
9201 btrfs_init_metadata_block_rsv(fs_info, &ei->block_rsv,
9202 BTRFS_BLOCK_RSV_DELALLOC);
Josef Bacik72ac3c02012-05-23 14:13:11 -04009203 ei->runtime_flags = 0;
David Sterbab52aa8c2017-07-17 19:17:20 +02009204 ei->prop_compress = BTRFS_COMPRESS_NONE;
David Sterbaeec63c62017-07-17 19:41:31 +02009205 ei->defrag_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009206
Miao Xie16cdcec2011-04-22 18:12:22 +08009207 ei->delayed_node = NULL;
9208
chandan r9cc97d62012-07-04 12:48:07 +05309209 ei->i_otime.tv_sec = 0;
9210 ei->i_otime.tv_nsec = 0;
9211
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009212 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02009213 extent_map_tree_init(&ei->extent_tree);
Qu Wenruo43eb5f22019-03-01 10:47:59 +08009214 extent_io_tree_init(fs_info, &ei->io_tree, IO_TREE_INODE_IO, inode);
9215 extent_io_tree_init(fs_info, &ei->io_failure_tree,
9216 IO_TREE_INODE_IO_FAILURE, inode);
David Sterba7b439732019-03-11 15:58:30 +01009217 ei->io_tree.track_uptodate = true;
9218 ei->io_failure_tree.track_uptodate = true;
Josef Bacikb812ce22012-11-16 13:56:32 -05009219 atomic_set(&ei->sync_writers, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009220 mutex_init(&ei->log_mutex);
Josef Bacikf2486792012-01-13 12:09:22 -05009221 mutex_init(&ei->delalloc_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009222 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009223 INIT_LIST_HEAD(&ei->delalloc_inodes);
David Sterba8089fe62015-11-19 14:15:51 +01009224 INIT_LIST_HEAD(&ei->delayed_iput);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009225 RB_CLEAR_NODE(&ei->rb_node);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01009226 init_rwsem(&ei->dio_sem);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009227
9228 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009229}
9230
Josef Bacikaaedb552013-10-11 14:44:09 -04009231#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
9232void btrfs_test_destroy_inode(struct inode *inode)
9233{
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02009234 btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0);
Josef Bacikaaedb552013-10-11 14:44:09 -04009235 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
9236}
9237#endif
9238
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009239static void btrfs_i_callback(struct rcu_head *head)
9240{
9241 struct inode *inode = container_of(head, struct inode, i_rcu);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009242 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
9243}
9244
Chris Mason39279cc2007-06-12 06:35:45 -04009245void btrfs_destroy_inode(struct inode *inode)
9246{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009247 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009248 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009249 struct btrfs_root *root = BTRFS_I(inode)->root;
9250
Al Virob3d9b7a2012-06-09 13:51:19 -04009251 WARN_ON(!hlist_empty(&inode->i_dentry));
Chris Mason39279cc2007-06-12 06:35:45 -04009252 WARN_ON(inode->i_data.nrpages);
Josef Bacik69fe2d72017-10-19 14:15:57 -04009253 WARN_ON(BTRFS_I(inode)->block_rsv.reserved);
9254 WARN_ON(BTRFS_I(inode)->block_rsv.size);
Josef Bacik9e0baf62011-07-15 15:16:44 +00009255 WARN_ON(BTRFS_I(inode)->outstanding_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04009256 WARN_ON(BTRFS_I(inode)->delalloc_bytes);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01009257 WARN_ON(BTRFS_I(inode)->new_delalloc_bytes);
Josef Bacik7709cde2011-08-04 10:25:02 -04009258 WARN_ON(BTRFS_I(inode)->csum_bytes);
Wang Shilong47059d92014-07-03 18:22:07 +08009259 WARN_ON(BTRFS_I(inode)->defrag_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04009260
Chris Mason5a3f23d2009-03-31 13:27:11 -04009261 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05009262 * This can happen where we create an inode, but somebody else also
9263 * created the same inode and we need to destroy the one we already
9264 * created.
9265 */
9266 if (!root)
9267 goto free;
9268
Chris Masond3977122009-01-05 21:25:51 -05009269 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04009270 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
9271 if (!ordered)
9272 break;
9273 else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009274 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04009275 "found ordered extent %llu %llu on inode cleanup",
9276 ordered->file_offset, ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009277 btrfs_remove_ordered_extent(inode, ordered);
9278 btrfs_put_ordered_extent(ordered);
9279 btrfs_put_ordered_extent(ordered);
9280 }
9281 }
Qu Wenruo56fa9d02015-10-13 09:53:10 +08009282 btrfs_qgroup_check_reserved_leak(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04009283 inode_tree_del(inode);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02009284 btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0);
Josef Bacika6dbd422009-11-11 15:53:34 -05009285free:
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009286 call_rcu(&inode->i_rcu, btrfs_i_callback);
Chris Mason39279cc2007-06-12 06:35:45 -04009287}
9288
Al Viro45321ac2010-06-07 13:43:19 -04009289int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04009290{
9291 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04009292
Naohiro Aota6379ef92013-06-06 09:56:34 +00009293 if (root == NULL)
9294 return 1;
9295
Liu Bofa6ac872013-02-20 14:10:23 +00009296 /* the snap/subvol tree is on deleting */
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02009297 if (btrfs_root_refs(&root->root_item) == 0)
Al Viro45321ac2010-06-07 13:43:19 -04009298 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04009299 else
Al Viro45321ac2010-06-07 13:43:19 -04009300 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04009301}
9302
Sven Wegener0ee0fda2008-07-30 16:54:26 -04009303static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04009304{
9305 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
9306
9307 inode_init_once(&ei->vfs_inode);
9308}
9309
David Sterbae67c7182018-02-19 17:24:18 +01009310void __cold btrfs_destroy_cachep(void)
Chris Mason39279cc2007-06-12 06:35:45 -04009311{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10009312 /*
9313 * Make sure all delayed rcu free inodes are flushed before we
9314 * destroy cache.
9315 */
9316 rcu_barrier();
Kinglong Mee5598e902016-01-29 21:36:35 +08009317 kmem_cache_destroy(btrfs_inode_cachep);
9318 kmem_cache_destroy(btrfs_trans_handle_cachep);
Kinglong Mee5598e902016-01-29 21:36:35 +08009319 kmem_cache_destroy(btrfs_path_cachep);
9320 kmem_cache_destroy(btrfs_free_space_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04009321}
9322
Liu Bof5c29bd2017-11-02 17:21:50 -06009323int __init btrfs_init_cachep(void)
Chris Mason39279cc2007-06-12 06:35:45 -04009324{
David Sterba837e1972012-09-07 03:00:48 -06009325 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009326 sizeof(struct btrfs_inode), 0,
Vladimir Davydov5d097052016-01-14 15:18:21 -08009327 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD | SLAB_ACCOUNT,
9328 init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04009329 if (!btrfs_inode_cachep)
9330 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009331
David Sterba837e1972012-09-07 03:00:48 -06009332 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009333 sizeof(struct btrfs_trans_handle), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009334 SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009335 if (!btrfs_trans_handle_cachep)
9336 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009337
David Sterba837e1972012-09-07 03:00:48 -06009338 btrfs_path_cachep = kmem_cache_create("btrfs_path",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009339 sizeof(struct btrfs_path), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009340 SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009341 if (!btrfs_path_cachep)
9342 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009343
David Sterba837e1972012-09-07 03:00:48 -06009344 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
Josef Bacikdc89e982011-01-28 17:05:48 -05009345 sizeof(struct btrfs_free_space), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009346 SLAB_MEM_SPREAD, NULL);
Josef Bacikdc89e982011-01-28 17:05:48 -05009347 if (!btrfs_free_space_cachep)
9348 goto fail;
9349
Chris Mason39279cc2007-06-12 06:35:45 -04009350 return 0;
9351fail:
9352 btrfs_destroy_cachep();
9353 return -ENOMEM;
9354}
9355
David Howellsa528d352017-01-31 16:46:22 +00009356static int btrfs_getattr(const struct path *path, struct kstat *stat,
9357 u32 request_mask, unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04009358{
Miao Xiedf0af1a2013-01-29 10:11:59 +00009359 u64 delalloc_bytes;
David Howellsa528d352017-01-31 16:46:22 +00009360 struct inode *inode = d_inode(path->dentry);
David Sterbafadc0d82011-11-20 07:33:38 -05009361 u32 blocksize = inode->i_sb->s_blocksize;
Yonghong Song04a87e32017-05-12 15:07:43 -07009362 u32 bi_flags = BTRFS_I(inode)->flags;
9363
9364 stat->result_mask |= STATX_BTIME;
9365 stat->btime.tv_sec = BTRFS_I(inode)->i_otime.tv_sec;
9366 stat->btime.tv_nsec = BTRFS_I(inode)->i_otime.tv_nsec;
9367 if (bi_flags & BTRFS_INODE_APPEND)
9368 stat->attributes |= STATX_ATTR_APPEND;
9369 if (bi_flags & BTRFS_INODE_COMPRESS)
9370 stat->attributes |= STATX_ATTR_COMPRESSED;
9371 if (bi_flags & BTRFS_INODE_IMMUTABLE)
9372 stat->attributes |= STATX_ATTR_IMMUTABLE;
9373 if (bi_flags & BTRFS_INODE_NODUMP)
9374 stat->attributes |= STATX_ATTR_NODUMP;
9375
9376 stat->attributes_mask |= (STATX_ATTR_APPEND |
9377 STATX_ATTR_COMPRESSED |
9378 STATX_ATTR_IMMUTABLE |
9379 STATX_ATTR_NODUMP);
David Sterbafadc0d82011-11-20 07:33:38 -05009380
Chris Mason39279cc2007-06-12 06:35:45 -04009381 generic_fillattr(inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04009382 stat->dev = BTRFS_I(inode)->root->anon_dev;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009383
9384 spin_lock(&BTRFS_I(inode)->lock);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01009385 delalloc_bytes = BTRFS_I(inode)->new_delalloc_bytes;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009386 spin_unlock(&BTRFS_I(inode)->lock);
David Sterbafadc0d82011-11-20 07:33:38 -05009387 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
Miao Xiedf0af1a2013-01-29 10:11:59 +00009388 ALIGN(delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04009389 return 0;
9390}
9391
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009392static int btrfs_rename_exchange(struct inode *old_dir,
9393 struct dentry *old_dentry,
9394 struct inode *new_dir,
9395 struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04009396{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009397 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009398 struct btrfs_trans_handle *trans;
9399 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009400 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009401 struct inode *new_inode = new_dentry->d_inode;
9402 struct inode *old_inode = old_dentry->d_inode;
Deepa Dinamani95582b02018-05-08 19:36:02 -07009403 struct timespec64 ctime = current_time(old_inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009404 struct dentry *parent;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009405 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
9406 u64 new_ino = btrfs_ino(BTRFS_I(new_inode));
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009407 u64 old_idx = 0;
9408 u64 new_idx = 0;
9409 u64 root_objectid;
9410 int ret;
Filipe Manana86e8aa02016-05-05 02:02:27 +01009411 bool root_log_pinned = false;
9412 bool dest_log_pinned = false;
Filipe Mananad4682ba2018-06-11 19:24:28 +01009413 struct btrfs_log_ctx ctx_root;
9414 struct btrfs_log_ctx ctx_dest;
9415 bool sync_log_root = false;
9416 bool sync_log_dest = false;
9417 bool commit_transaction = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009418
9419 /* we only allow rename subvolume link between subvolumes */
9420 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
9421 return -EXDEV;
9422
Filipe Mananad4682ba2018-06-11 19:24:28 +01009423 btrfs_init_log_ctx(&ctx_root, old_inode);
9424 btrfs_init_log_ctx(&ctx_dest, new_inode);
9425
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009426 /* close the race window with snapshot create/destroy ioctl */
9427 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009428 down_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009429 if (new_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009430 down_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009431
9432 /*
9433 * We want to reserve the absolute worst case amount of items. So if
9434 * both inodes are subvols and we need to unlink them then that would
9435 * require 4 item modifications, but if they are both normal inodes it
9436 * would require 5 item modifications, so we'll assume their normal
9437 * inodes. So 5 * 2 is 10, plus 2 for the new links, so 12 total items
9438 * should cover the worst case number of items we'll modify.
9439 */
9440 trans = btrfs_start_transaction(root, 12);
9441 if (IS_ERR(trans)) {
9442 ret = PTR_ERR(trans);
9443 goto out_notrans;
9444 }
9445
9446 /*
9447 * We need to find a free sequence number both in the source and
9448 * in the destination directory for the exchange.
9449 */
Nikolay Borisov877574e2017-02-20 13:50:33 +02009450 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009451 if (ret)
9452 goto out_fail;
Nikolay Borisov877574e2017-02-20 13:50:33 +02009453 ret = btrfs_set_inode_index(BTRFS_I(old_dir), &new_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009454 if (ret)
9455 goto out_fail;
9456
9457 BTRFS_I(old_inode)->dir_index = 0ULL;
9458 BTRFS_I(new_inode)->dir_index = 0ULL;
9459
9460 /* Reference for the source. */
9461 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
9462 /* force full log commit if subvolume involved. */
David Sterba90787762019-03-20 13:28:05 +01009463 btrfs_set_log_full_commit(trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009464 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01009465 btrfs_pin_log_trans(root);
9466 root_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009467 ret = btrfs_insert_inode_ref(trans, dest,
9468 new_dentry->d_name.name,
9469 new_dentry->d_name.len,
9470 old_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01009471 btrfs_ino(BTRFS_I(new_dir)),
9472 old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009473 if (ret)
9474 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009475 }
9476
9477 /* And now for the dest. */
9478 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
9479 /* force full log commit if subvolume involved. */
David Sterba90787762019-03-20 13:28:05 +01009480 btrfs_set_log_full_commit(trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009481 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01009482 btrfs_pin_log_trans(dest);
9483 dest_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009484 ret = btrfs_insert_inode_ref(trans, root,
9485 old_dentry->d_name.name,
9486 old_dentry->d_name.len,
9487 new_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01009488 btrfs_ino(BTRFS_I(old_dir)),
9489 new_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009490 if (ret)
9491 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009492 }
9493
9494 /* Update inode version and ctime/mtime. */
9495 inode_inc_iversion(old_dir);
9496 inode_inc_iversion(new_dir);
9497 inode_inc_iversion(old_inode);
9498 inode_inc_iversion(new_inode);
9499 old_dir->i_ctime = old_dir->i_mtime = ctime;
9500 new_dir->i_ctime = new_dir->i_mtime = ctime;
9501 old_inode->i_ctime = ctime;
9502 new_inode->i_ctime = ctime;
9503
9504 if (old_dentry->d_parent != new_dentry->d_parent) {
David Sterbaf85b7372017-01-20 14:54:07 +01009505 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
9506 BTRFS_I(old_inode), 1);
9507 btrfs_record_unlink_dir(trans, BTRFS_I(new_dir),
9508 BTRFS_I(new_inode), 1);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009509 }
9510
9511 /* src is a subvolume */
9512 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
9513 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
Lu Fengqi401b3b12018-08-01 11:32:30 +08009514 ret = btrfs_unlink_subvol(trans, old_dir, root_objectid,
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009515 old_dentry->d_name.name,
9516 old_dentry->d_name.len);
9517 } else { /* src is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009518 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
9519 BTRFS_I(old_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009520 old_dentry->d_name.name,
9521 old_dentry->d_name.len);
9522 if (!ret)
9523 ret = btrfs_update_inode(trans, root, old_inode);
9524 }
9525 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009526 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009527 goto out_fail;
9528 }
9529
9530 /* dest is a subvolume */
9531 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
9532 root_objectid = BTRFS_I(new_inode)->root->root_key.objectid;
Lu Fengqi401b3b12018-08-01 11:32:30 +08009533 ret = btrfs_unlink_subvol(trans, new_dir, root_objectid,
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009534 new_dentry->d_name.name,
9535 new_dentry->d_name.len);
9536 } else { /* dest is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009537 ret = __btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
9538 BTRFS_I(new_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009539 new_dentry->d_name.name,
9540 new_dentry->d_name.len);
9541 if (!ret)
9542 ret = btrfs_update_inode(trans, dest, new_inode);
9543 }
9544 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009545 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009546 goto out_fail;
9547 }
9548
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009549 ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009550 new_dentry->d_name.name,
9551 new_dentry->d_name.len, 0, old_idx);
9552 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009553 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009554 goto out_fail;
9555 }
9556
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009557 ret = btrfs_add_link(trans, BTRFS_I(old_dir), BTRFS_I(new_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009558 old_dentry->d_name.name,
9559 old_dentry->d_name.len, 0, new_idx);
9560 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009561 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009562 goto out_fail;
9563 }
9564
9565 if (old_inode->i_nlink == 1)
9566 BTRFS_I(old_inode)->dir_index = old_idx;
9567 if (new_inode->i_nlink == 1)
9568 BTRFS_I(new_inode)->dir_index = new_idx;
9569
Filipe Manana86e8aa02016-05-05 02:02:27 +01009570 if (root_log_pinned) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009571 parent = new_dentry->d_parent;
Filipe Mananad4682ba2018-06-11 19:24:28 +01009572 ret = btrfs_log_new_name(trans, BTRFS_I(old_inode),
9573 BTRFS_I(old_dir), parent,
9574 false, &ctx_root);
9575 if (ret == BTRFS_NEED_LOG_SYNC)
9576 sync_log_root = true;
9577 else if (ret == BTRFS_NEED_TRANS_COMMIT)
9578 commit_transaction = true;
9579 ret = 0;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009580 btrfs_end_log_trans(root);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009581 root_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009582 }
Filipe Manana86e8aa02016-05-05 02:02:27 +01009583 if (dest_log_pinned) {
Filipe Mananad4682ba2018-06-11 19:24:28 +01009584 if (!commit_transaction) {
9585 parent = old_dentry->d_parent;
9586 ret = btrfs_log_new_name(trans, BTRFS_I(new_inode),
9587 BTRFS_I(new_dir), parent,
9588 false, &ctx_dest);
9589 if (ret == BTRFS_NEED_LOG_SYNC)
9590 sync_log_dest = true;
9591 else if (ret == BTRFS_NEED_TRANS_COMMIT)
9592 commit_transaction = true;
9593 ret = 0;
9594 }
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009595 btrfs_end_log_trans(dest);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009596 dest_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009597 }
9598out_fail:
Filipe Manana86e8aa02016-05-05 02:02:27 +01009599 /*
9600 * If we have pinned a log and an error happened, we unpin tasks
9601 * trying to sync the log and force them to fallback to a transaction
9602 * commit if the log currently contains any of the inodes involved in
9603 * this rename operation (to ensure we do not persist a log with an
9604 * inconsistent state for any of these inodes or leading to any
9605 * inconsistencies when replayed). If the transaction was aborted, the
9606 * abortion reason is propagated to userspace when attempting to commit
9607 * the transaction. If the log does not contain any of these inodes, we
9608 * allow the tasks to sync it.
9609 */
9610 if (ret && (root_log_pinned || dest_log_pinned)) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009611 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
9612 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
9613 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana86e8aa02016-05-05 02:02:27 +01009614 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009615 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
David Sterba90787762019-03-20 13:28:05 +01009616 btrfs_set_log_full_commit(trans);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009617
9618 if (root_log_pinned) {
9619 btrfs_end_log_trans(root);
9620 root_log_pinned = false;
9621 }
9622 if (dest_log_pinned) {
9623 btrfs_end_log_trans(dest);
9624 dest_log_pinned = false;
9625 }
9626 }
Filipe Mananad4682ba2018-06-11 19:24:28 +01009627 if (!ret && sync_log_root && !commit_transaction) {
9628 ret = btrfs_sync_log(trans, BTRFS_I(old_inode)->root,
9629 &ctx_root);
9630 if (ret)
9631 commit_transaction = true;
9632 }
9633 if (!ret && sync_log_dest && !commit_transaction) {
9634 ret = btrfs_sync_log(trans, BTRFS_I(new_inode)->root,
9635 &ctx_dest);
9636 if (ret)
9637 commit_transaction = true;
9638 }
9639 if (commit_transaction) {
9640 ret = btrfs_commit_transaction(trans);
9641 } else {
9642 int ret2;
9643
9644 ret2 = btrfs_end_transaction(trans);
9645 ret = ret ? ret : ret2;
9646 }
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009647out_notrans:
9648 if (new_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009649 up_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009650 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009651 up_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009652
9653 return ret;
9654}
9655
9656static int btrfs_whiteout_for_rename(struct btrfs_trans_handle *trans,
9657 struct btrfs_root *root,
9658 struct inode *dir,
9659 struct dentry *dentry)
9660{
9661 int ret;
9662 struct inode *inode;
9663 u64 objectid;
9664 u64 index;
9665
9666 ret = btrfs_find_free_ino(root, &objectid);
9667 if (ret)
9668 return ret;
9669
9670 inode = btrfs_new_inode(trans, root, dir,
9671 dentry->d_name.name,
9672 dentry->d_name.len,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009673 btrfs_ino(BTRFS_I(dir)),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009674 objectid,
9675 S_IFCHR | WHITEOUT_MODE,
9676 &index);
9677
9678 if (IS_ERR(inode)) {
9679 ret = PTR_ERR(inode);
9680 return ret;
9681 }
9682
9683 inode->i_op = &btrfs_special_inode_operations;
9684 init_special_inode(inode, inode->i_mode,
9685 WHITEOUT_DEV);
9686
9687 ret = btrfs_init_inode_security(trans, inode, dir,
9688 &dentry->d_name);
9689 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009690 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009691
Nikolay Borisovcef415a2017-02-20 13:51:09 +02009692 ret = btrfs_add_nondir(trans, BTRFS_I(dir), dentry,
9693 BTRFS_I(inode), 0, index);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009694 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009695 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009696
9697 ret = btrfs_update_inode(trans, root, inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009698out:
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009699 unlock_new_inode(inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009700 if (ret)
9701 inode_dec_link_count(inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009702 iput(inode);
9703
Filipe Mananac9901612016-05-05 01:41:57 +01009704 return ret;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009705}
9706
Chris Mason39279cc2007-06-12 06:35:45 -04009707static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009708 struct inode *new_dir, struct dentry *new_dentry,
9709 unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04009710{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009711 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009712 struct btrfs_trans_handle *trans;
Filipe Manana5062af32016-05-05 10:26:26 +01009713 unsigned int trans_num_items;
Chris Mason39279cc2007-06-12 06:35:45 -04009714 struct btrfs_root *root = BTRFS_I(old_dir)->root;
9715 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00009716 struct inode *new_inode = d_inode(new_dentry);
9717 struct inode *old_inode = d_inode(old_dentry);
Chris Mason00e4e6b2008-08-05 11:18:09 -04009718 u64 index = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009719 u64 root_objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04009720 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009721 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009722 bool log_pinned = false;
Filipe Mananad4682ba2018-06-11 19:24:28 +01009723 struct btrfs_log_ctx ctx;
9724 bool sync_log = false;
9725 bool commit_transaction = false;
Chris Mason39279cc2007-06-12 06:35:45 -04009726
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009727 if (btrfs_ino(BTRFS_I(new_dir)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04009728 return -EPERM;
9729
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009730 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08009731 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05009732 return -EXDEV;
9733
Li Zefan33345d012011-04-20 10:31:50 +08009734 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009735 (new_inode && btrfs_ino(BTRFS_I(new_inode)) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009736 return -ENOTEMPTY;
9737
Chris Mason39279cc2007-06-12 06:35:45 -04009738 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009739 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04009740 return -ENOTEMPTY;
Chris Mason9c520572012-12-17 14:26:57 -05009741
9742
9743 /* check for collisions, even if the name isn't there */
Josef Bacik4871c152013-10-09 12:24:04 -04009744 ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino,
Chris Mason9c520572012-12-17 14:26:57 -05009745 new_dentry->d_name.name,
9746 new_dentry->d_name.len);
9747
9748 if (ret) {
9749 if (ret == -EEXIST) {
9750 /* we shouldn't get
9751 * eexist without a new_inode */
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05309752 if (WARN_ON(!new_inode)) {
Chris Mason9c520572012-12-17 14:26:57 -05009753 return ret;
9754 }
9755 } else {
9756 /* maybe -EOVERFLOW */
9757 return ret;
9758 }
9759 }
9760 ret = 0;
9761
Chris Mason5a3f23d2009-03-31 13:27:11 -04009762 /*
Chris Mason8d875f92014-08-12 10:47:42 -07009763 * we're using rename to replace one file with another. Start IO on it
9764 * now so we don't add too much work to the end of the transaction
Chris Mason5a3f23d2009-03-31 13:27:11 -04009765 */
Chris Mason8d875f92014-08-12 10:47:42 -07009766 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size)
Chris Mason5a3f23d2009-03-31 13:27:11 -04009767 filemap_flush(old_inode->i_mapping);
9768
Yan, Zheng76dda932009-09-21 16:00:26 -04009769 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08009770 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009771 down_read(&fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04009772 /*
9773 * We want to reserve the absolute worst case amount of items. So if
9774 * both inodes are subvols and we need to unlink them then that would
9775 * require 4 item modifications, but if they are both normal inodes it
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009776 * would require 5 item modifications, so we'll assume they are normal
Yan, Zhenga22285a2010-05-16 10:48:46 -04009777 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
9778 * should cover the worst case number of items we'll modify.
Filipe Manana5062af32016-05-05 10:26:26 +01009779 * If our rename has the whiteout flag, we need more 5 units for the
9780 * new inode (1 inode item, 1 inode ref, 2 dir items and 1 xattr item
9781 * when selinux is enabled).
Yan, Zhenga22285a2010-05-16 10:48:46 -04009782 */
Filipe Manana5062af32016-05-05 10:26:26 +01009783 trans_num_items = 11;
9784 if (flags & RENAME_WHITEOUT)
9785 trans_num_items += 5;
9786 trans = btrfs_start_transaction(root, trans_num_items);
Johann Lombardib44c59a2011-03-31 13:23:47 +00009787 if (IS_ERR(trans)) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009788 ret = PTR_ERR(trans);
9789 goto out_notrans;
9790 }
Chris Mason5f39d392007-10-15 16:14:19 -04009791
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009792 if (dest != root)
9793 btrfs_record_root_in_trans(trans, dest);
9794
Nikolay Borisov877574e2017-02-20 13:50:33 +02009795 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009796 if (ret)
9797 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009798
Miao Xie67de1172013-12-26 13:07:06 +08009799 BTRFS_I(old_inode)->dir_index = 0ULL;
Li Zefan33345d012011-04-20 10:31:50 +08009800 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009801 /* force full log commit if subvolume involved. */
David Sterba90787762019-03-20 13:28:05 +01009802 btrfs_set_log_full_commit(trans);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009803 } else {
Filipe Mananac4aba952016-04-29 13:14:42 +01009804 btrfs_pin_log_trans(root);
9805 log_pinned = true;
Yan, Zhenga5719522009-09-24 09:17:31 -04009806 ret = btrfs_insert_inode_ref(trans, dest,
9807 new_dentry->d_name.name,
9808 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08009809 old_ino,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009810 btrfs_ino(BTRFS_I(new_dir)), index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009811 if (ret)
9812 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009813 }
Chris Mason5a3f23d2009-03-31 13:27:11 -04009814
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009815 inode_inc_iversion(old_dir);
9816 inode_inc_iversion(new_dir);
9817 inode_inc_iversion(old_inode);
Deepa Dinamani04b285f2016-02-06 23:57:21 -08009818 old_dir->i_ctime = old_dir->i_mtime =
9819 new_dir->i_ctime = new_dir->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009820 old_inode->i_ctime = current_time(old_dir);
Chris Mason5f39d392007-10-15 16:14:19 -04009821
Chris Mason12fcfd22009-03-24 10:24:20 -04009822 if (old_dentry->d_parent != new_dentry->d_parent)
David Sterbaf85b7372017-01-20 14:54:07 +01009823 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
9824 BTRFS_I(old_inode), 1);
Chris Mason12fcfd22009-03-24 10:24:20 -04009825
Li Zefan33345d012011-04-20 10:31:50 +08009826 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009827 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
Lu Fengqi401b3b12018-08-01 11:32:30 +08009828 ret = btrfs_unlink_subvol(trans, old_dir, root_objectid,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009829 old_dentry->d_name.name,
9830 old_dentry->d_name.len);
9831 } else {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009832 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
9833 BTRFS_I(d_inode(old_dentry)),
Al Viro92986792011-03-04 17:14:37 +00009834 old_dentry->d_name.name,
9835 old_dentry->d_name.len);
9836 if (!ret)
9837 ret = btrfs_update_inode(trans, root, old_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009838 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009839 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009840 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009841 goto out_fail;
9842 }
Chris Mason39279cc2007-06-12 06:35:45 -04009843
9844 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009845 inode_inc_iversion(new_inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009846 new_inode->i_ctime = current_time(new_inode);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009847 if (unlikely(btrfs_ino(BTRFS_I(new_inode)) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009848 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
9849 root_objectid = BTRFS_I(new_inode)->location.objectid;
Lu Fengqi401b3b12018-08-01 11:32:30 +08009850 ret = btrfs_unlink_subvol(trans, new_dir, root_objectid,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009851 new_dentry->d_name.name,
9852 new_dentry->d_name.len);
9853 BUG_ON(new_inode->i_nlink == 0);
9854 } else {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009855 ret = btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
9856 BTRFS_I(d_inode(new_dentry)),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009857 new_dentry->d_name.name,
9858 new_dentry->d_name.len);
9859 }
Josef Bacik4ef31a42013-08-13 14:10:08 -04009860 if (!ret && new_inode->i_nlink == 0)
Nikolay Borisov73f2e542017-02-20 13:50:59 +02009861 ret = btrfs_orphan_add(trans,
9862 BTRFS_I(d_inode(new_dentry)));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009863 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009864 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009865 goto out_fail;
9866 }
Chris Mason39279cc2007-06-12 06:35:45 -04009867 }
Josef Bacikaec74772008-07-24 12:12:38 -04009868
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009869 ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009870 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04009871 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009872 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009873 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009874 goto out_fail;
9875 }
Chris Mason39279cc2007-06-12 06:35:45 -04009876
Miao Xie67de1172013-12-26 13:07:06 +08009877 if (old_inode->i_nlink == 1)
9878 BTRFS_I(old_inode)->dir_index = index;
9879
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009880 if (log_pinned) {
Al Viro10d9f302011-07-16 23:09:10 -04009881 struct dentry *parent = new_dentry->d_parent;
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009882
Filipe Mananad4682ba2018-06-11 19:24:28 +01009883 btrfs_init_log_ctx(&ctx, old_inode);
9884 ret = btrfs_log_new_name(trans, BTRFS_I(old_inode),
9885 BTRFS_I(old_dir), parent,
9886 false, &ctx);
9887 if (ret == BTRFS_NEED_LOG_SYNC)
9888 sync_log = true;
9889 else if (ret == BTRFS_NEED_TRANS_COMMIT)
9890 commit_transaction = true;
9891 ret = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009892 btrfs_end_log_trans(root);
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009893 log_pinned = false;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009894 }
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009895
9896 if (flags & RENAME_WHITEOUT) {
9897 ret = btrfs_whiteout_for_rename(trans, root, old_dir,
9898 old_dentry);
9899
9900 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009901 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009902 goto out_fail;
9903 }
Chris Mason12fcfd22009-03-24 10:24:20 -04009904 }
Chris Mason39279cc2007-06-12 06:35:45 -04009905out_fail:
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009906 /*
9907 * If we have pinned the log and an error happened, we unpin tasks
9908 * trying to sync the log and force them to fallback to a transaction
9909 * commit if the log currently contains any of the inodes involved in
9910 * this rename operation (to ensure we do not persist a log with an
9911 * inconsistent state for any of these inodes or leading to any
9912 * inconsistencies when replayed). If the transaction was aborted, the
9913 * abortion reason is propagated to userspace when attempting to commit
9914 * the transaction. If the log does not contain any of these inodes, we
9915 * allow the tasks to sync it.
9916 */
9917 if (ret && log_pinned) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009918 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
9919 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
9920 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009921 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009922 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
David Sterba90787762019-03-20 13:28:05 +01009923 btrfs_set_log_full_commit(trans);
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009924
9925 btrfs_end_log_trans(root);
9926 log_pinned = false;
9927 }
Filipe Mananad4682ba2018-06-11 19:24:28 +01009928 if (!ret && sync_log) {
9929 ret = btrfs_sync_log(trans, BTRFS_I(old_inode)->root, &ctx);
9930 if (ret)
9931 commit_transaction = true;
9932 }
9933 if (commit_transaction) {
9934 ret = btrfs_commit_transaction(trans);
9935 } else {
9936 int ret2;
9937
9938 ret2 = btrfs_end_transaction(trans);
9939 ret = ret ? ret : ret2;
9940 }
Johann Lombardib44c59a2011-03-31 13:23:47 +00009941out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +08009942 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009943 up_read(&fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009944
Chris Mason39279cc2007-06-12 06:35:45 -04009945 return ret;
9946}
9947
Miklos Szeredi80ace852014-07-23 15:15:32 +02009948static int btrfs_rename2(struct inode *old_dir, struct dentry *old_dentry,
9949 struct inode *new_dir, struct dentry *new_dentry,
9950 unsigned int flags)
9951{
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009952 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
Miklos Szeredi80ace852014-07-23 15:15:32 +02009953 return -EINVAL;
9954
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009955 if (flags & RENAME_EXCHANGE)
9956 return btrfs_rename_exchange(old_dir, old_dentry, new_dir,
9957 new_dentry);
9958
9959 return btrfs_rename(old_dir, old_dentry, new_dir, new_dentry, flags);
Miklos Szeredi80ace852014-07-23 15:15:32 +02009960}
9961
Nikolay Borisov3a2f8c02018-04-24 17:23:59 +03009962struct btrfs_delalloc_work {
9963 struct inode *inode;
9964 struct completion completion;
9965 struct list_head list;
9966 struct btrfs_work work;
9967};
9968
Miao Xie8ccf6f192012-10-25 09:28:04 +00009969static void btrfs_run_delalloc_work(struct btrfs_work *work)
9970{
9971 struct btrfs_delalloc_work *delalloc_work;
Josef Bacik9f23e282013-10-28 15:03:41 -04009972 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009973
9974 delalloc_work = container_of(work, struct btrfs_delalloc_work,
9975 work);
Josef Bacik9f23e282013-10-28 15:03:41 -04009976 inode = delalloc_work->inode;
David Sterba30424602015-11-27 19:27:11 +01009977 filemap_flush(inode->i_mapping);
9978 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
9979 &BTRFS_I(inode)->runtime_flags))
Josef Bacik9f23e282013-10-28 15:03:41 -04009980 filemap_flush(inode->i_mapping);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009981
Nikolay Borisov076da912018-04-23 10:54:16 +03009982 iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009983 complete(&delalloc_work->completion);
9984}
9985
Nikolay Borisov3a2f8c02018-04-24 17:23:59 +03009986static struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode)
Miao Xie8ccf6f192012-10-25 09:28:04 +00009987{
9988 struct btrfs_delalloc_work *work;
9989
David Sterba100d5702015-12-08 14:39:32 +01009990 work = kmalloc(sizeof(*work), GFP_NOFS);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009991 if (!work)
9992 return NULL;
9993
9994 init_completion(&work->completion);
9995 INIT_LIST_HEAD(&work->list);
9996 work->inode = inode;
Liu Bo9e0af232014-08-15 23:36:53 +08009997 btrfs_init_work(&work->work, btrfs_flush_delalloc_helper,
9998 btrfs_run_delalloc_work, NULL, NULL);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009999
10000 return work;
10001}
10002
Chris Masond352ac62008-09-29 15:18:18 -040010003/*
10004 * some fairly slow code that needs optimization. This walks the list
10005 * of all the inodes with pending delalloc and forces them to disk.
10006 */
Ethan Lien3cd24c62018-11-01 14:49:03 +080010007static int start_delalloc_inodes(struct btrfs_root *root, int nr, bool snapshot)
Chris Masonea8c2812008-08-04 23:17:27 -040010008{
Chris Masonea8c2812008-08-04 23:17:27 -040010009 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010010 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010011 struct btrfs_delalloc_work *work, *next;
10012 struct list_head works;
Miao Xie1eafa6c2013-01-22 10:49:00 +000010013 struct list_head splice;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010014 int ret = 0;
Chris Masonea8c2812008-08-04 23:17:27 -040010015
Miao Xie8ccf6f192012-10-25 09:28:04 +000010016 INIT_LIST_HEAD(&works);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010017 INIT_LIST_HEAD(&splice);
Miao Xie63607cc2013-01-22 10:50:35 +000010018
Miao Xie573bfb72014-03-06 13:55:03 +080010019 mutex_lock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010020 spin_lock(&root->delalloc_lock);
10021 list_splice_init(&root->delalloc_inodes, &splice);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010022 while (!list_empty(&splice)) {
10023 binode = list_entry(splice.next, struct btrfs_inode,
Chris Masonea8c2812008-08-04 23:17:27 -040010024 delalloc_inodes);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010025
Miao Xieeb73c1b2013-05-15 07:48:22 +000010026 list_move_tail(&binode->delalloc_inodes,
10027 &root->delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010028 inode = igrab(&binode->vfs_inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +000010029 if (!inode) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010030 cond_resched_lock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010031 continue;
Miao Xiedf0af1a2013-01-29 10:11:59 +000010032 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010033 spin_unlock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010034
Ethan Lien3cd24c62018-11-01 14:49:03 +080010035 if (snapshot)
10036 set_bit(BTRFS_INODE_SNAPSHOT_FLUSH,
10037 &binode->runtime_flags);
Nikolay Borisov076da912018-04-23 10:54:16 +030010038 work = btrfs_alloc_delalloc_work(inode);
David Sterba5d99a9982014-09-29 19:20:37 +020010039 if (!work) {
Nikolay Borisov4fbb5142018-04-23 10:54:15 +030010040 iput(inode);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010041 ret = -ENOMEM;
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010042 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010043 }
Miao Xie1eafa6c2013-01-22 10:49:00 +000010044 list_add_tail(&work->list, &works);
Qu Wenruoa44903a2014-02-28 10:46:09 +080010045 btrfs_queue_work(root->fs_info->flush_workers,
10046 &work->work);
Miao Xie6c255e62014-03-06 13:55:01 +080010047 ret++;
10048 if (nr != -1 && ret >= nr)
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010049 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010050 cond_resched();
Miao Xieeb73c1b2013-05-15 07:48:22 +000010051 spin_lock(&root->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -040010052 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010053 spin_unlock(&root->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -040010054
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010055out:
Miao Xie1eafa6c2013-01-22 10:49:00 +000010056 list_for_each_entry_safe(work, next, &works, list) {
10057 list_del_init(&work->list);
Nikolay Borisov40012f92018-04-19 10:46:39 +030010058 wait_for_completion(&work->completion);
10059 kfree(work);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010060 }
10061
Nikolay Borisov81f1d392018-04-19 10:46:37 +030010062 if (!list_empty(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010063 spin_lock(&root->delalloc_lock);
10064 list_splice_tail(&splice, &root->delalloc_inodes);
10065 spin_unlock(&root->delalloc_lock);
10066 }
Miao Xie573bfb72014-03-06 13:55:03 +080010067 mutex_unlock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010068 return ret;
10069}
10070
Ethan Lien3cd24c62018-11-01 14:49:03 +080010071int btrfs_start_delalloc_snapshot(struct btrfs_root *root)
Miao Xieeb73c1b2013-05-15 07:48:22 +000010072{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010073 struct btrfs_fs_info *fs_info = root->fs_info;
Miao Xieeb73c1b2013-05-15 07:48:22 +000010074 int ret;
10075
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010076 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +000010077 return -EROFS;
10078
Ethan Lien3cd24c62018-11-01 14:49:03 +080010079 ret = start_delalloc_inodes(root, -1, true);
Miao Xie6c255e62014-03-06 13:55:01 +080010080 if (ret > 0)
10081 ret = 0;
Miao Xieeb73c1b2013-05-15 07:48:22 +000010082 return ret;
10083}
10084
Nikolay Borisov82b3e532018-04-23 10:54:13 +030010085int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, int nr)
Miao Xieeb73c1b2013-05-15 07:48:22 +000010086{
10087 struct btrfs_root *root;
10088 struct list_head splice;
10089 int ret;
10090
Wang Shilong2c21b4d2014-01-14 19:42:20 +080010091 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +000010092 return -EROFS;
10093
10094 INIT_LIST_HEAD(&splice);
10095
Miao Xie573bfb72014-03-06 13:55:03 +080010096 mutex_lock(&fs_info->delalloc_root_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010097 spin_lock(&fs_info->delalloc_root_lock);
10098 list_splice_init(&fs_info->delalloc_roots, &splice);
Miao Xie6c255e62014-03-06 13:55:01 +080010099 while (!list_empty(&splice) && nr) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010100 root = list_first_entry(&splice, struct btrfs_root,
10101 delalloc_root);
10102 root = btrfs_grab_fs_root(root);
10103 BUG_ON(!root);
10104 list_move_tail(&root->delalloc_root,
10105 &fs_info->delalloc_roots);
10106 spin_unlock(&fs_info->delalloc_root_lock);
10107
Ethan Lien3cd24c62018-11-01 14:49:03 +080010108 ret = start_delalloc_inodes(root, nr, false);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010109 btrfs_put_fs_root(root);
Miao Xie6c255e62014-03-06 13:55:01 +080010110 if (ret < 0)
Miao Xieeb73c1b2013-05-15 07:48:22 +000010111 goto out;
10112
Miao Xie6c255e62014-03-06 13:55:01 +080010113 if (nr != -1) {
10114 nr -= ret;
10115 WARN_ON(nr < 0);
10116 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010117 spin_lock(&fs_info->delalloc_root_lock);
10118 }
10119 spin_unlock(&fs_info->delalloc_root_lock);
10120
Miao Xie6c255e62014-03-06 13:55:01 +080010121 ret = 0;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010122out:
Nikolay Borisov81f1d392018-04-19 10:46:37 +030010123 if (!list_empty(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010124 spin_lock(&fs_info->delalloc_root_lock);
10125 list_splice_tail(&splice, &fs_info->delalloc_roots);
10126 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010127 }
Miao Xie573bfb72014-03-06 13:55:03 +080010128 mutex_unlock(&fs_info->delalloc_root_mutex);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010129 return ret;
Chris Masonea8c2812008-08-04 23:17:27 -040010130}
10131
Chris Mason39279cc2007-06-12 06:35:45 -040010132static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
10133 const char *symname)
10134{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010135 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -040010136 struct btrfs_trans_handle *trans;
10137 struct btrfs_root *root = BTRFS_I(dir)->root;
10138 struct btrfs_path *path;
10139 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -050010140 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -040010141 int err;
Chris Mason39279cc2007-06-12 06:35:45 -040010142 u64 objectid;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +053010143 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -040010144 int name_len;
10145 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -040010146 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -040010147 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -040010148 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -040010149
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +010010150 name_len = strlen(symname);
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010151 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info))
Chris Mason39279cc2007-06-12 06:35:45 -040010152 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -050010153
Josef Bacik9ed74f22009-09-11 16:12:44 -040010154 /*
10155 * 2 items for inode item and ref
10156 * 2 items for dir items
Filipe Manana9269d122015-12-31 18:16:29 +000010157 * 1 item for updating parent inode item
10158 * 1 item for the inline extent item
Josef Bacik9ed74f22009-09-11 16:12:44 -040010159 * 1 item for xattr if selinux is on
10160 */
Filipe Manana9269d122015-12-31 18:16:29 +000010161 trans = btrfs_start_transaction(root, 7);
Yan, Zhenga22285a2010-05-16 10:48:46 -040010162 if (IS_ERR(trans))
10163 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -050010164
Li Zefan581bb052011-04-20 10:06:11 +080010165 err = btrfs_find_free_ino(root, &objectid);
10166 if (err)
10167 goto out_unlock;
10168
Josef Bacikaec74772008-07-24 12:12:38 -040010169 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +010010170 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)),
10171 objectid, S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -040010172 if (IS_ERR(inode)) {
10173 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -040010174 inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -040010175 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -040010176 }
Chris Mason39279cc2007-06-12 06:35:45 -040010177
Casey Schauflerad19db72011-12-15 10:09:07 -050010178 /*
10179 * If the active LSM wants to access the inode during
10180 * d_instantiate it needs these. Smack checks to see
10181 * if the filesystem supports xattrs by looking at the
10182 * ops vector.
10183 */
10184 inode->i_fop = &btrfs_file_operations;
10185 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -070010186 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -070010187 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
10188
10189 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
10190 if (err)
Al Viro32955c52018-05-16 12:20:05 -040010191 goto out_unlock;
Casey Schauflerad19db72011-12-15 10:09:07 -050010192
Chris Mason39279cc2007-06-12 06:35:45 -040010193 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -070010194 if (!path) {
10195 err = -ENOMEM;
Al Viro32955c52018-05-16 12:20:05 -040010196 goto out_unlock;
Mark Fashehd8926bb2011-07-13 10:38:47 -070010197 }
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +020010198 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason39279cc2007-06-12 06:35:45 -040010199 key.offset = 0;
David Sterba962a2982014-06-04 18:41:45 +020010200 key.type = BTRFS_EXTENT_DATA_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -040010201 datasize = btrfs_file_extent_calc_inline_size(name_len);
10202 err = btrfs_insert_empty_item(trans, root, path, &key,
10203 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -040010204 if (err) {
Julia Lawallb0839162011-05-14 07:10:51 +000010205 btrfs_free_path(path);
Al Viro32955c52018-05-16 12:20:05 -040010206 goto out_unlock;
Chris Mason54aa1f42007-06-22 14:16:25 -040010207 }
Chris Mason5f39d392007-10-15 16:14:19 -040010208 leaf = path->nodes[0];
10209 ei = btrfs_item_ptr(leaf, path->slots[0],
10210 struct btrfs_file_extent_item);
10211 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
10212 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -040010213 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -040010214 btrfs_set_file_extent_encryption(leaf, ei, 0);
10215 btrfs_set_file_extent_compression(leaf, ei, 0);
10216 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
10217 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
10218
Chris Mason39279cc2007-06-12 06:35:45 -040010219 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -040010220 write_extent_buffer(leaf, symname, ptr, name_len);
10221 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -040010222 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -040010223
Chris Mason39279cc2007-06-12 06:35:45 -040010224 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -050010225 inode_nohighmem(inode);
Yan Zhengd899e052008-10-30 14:25:28 -040010226 inode_set_bytes(inode, name_len);
Nikolay Borisov6ef06d22017-02-20 13:50:34 +020010227 btrfs_i_size_write(BTRFS_I(inode), name_len);
Chris Mason54aa1f42007-06-22 14:16:25 -040010228 err = btrfs_update_inode(trans, root, inode);
Filipe Mananad50866d2015-12-31 18:08:24 +000010229 /*
10230 * Last step, add directory indexes for our symlink inode. This is the
10231 * last step to avoid extra cleanup of these indexes if an error happens
10232 * elsewhere above.
10233 */
10234 if (!err)
Nikolay Borisovcef415a2017-02-20 13:51:09 +020010235 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry,
10236 BTRFS_I(inode), 0, index);
Al Viro32955c52018-05-16 12:20:05 -040010237 if (err)
10238 goto out_unlock;
Chris Masonb0d5d102014-09-08 13:08:51 -070010239
Al Viro1e2e5472018-05-04 08:23:01 -040010240 d_instantiate_new(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010241
10242out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010243 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -040010244 if (err && inode) {
Chris Mason39279cc2007-06-12 06:35:45 -040010245 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -040010246 discard_new_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010247 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010248 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -040010249 return err;
10250}
Chris Mason16432982008-04-10 10:23:21 -040010251
Josef Bacik0af3d002010-06-21 14:48:16 -040010252static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
10253 u64 start, u64 num_bytes, u64 min_size,
10254 loff_t actual_len, u64 *alloc_hint,
10255 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -040010256{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010257 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik5dc562c2012-08-17 13:14:17 -040010258 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
10259 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -040010260 struct btrfs_root *root = BTRFS_I(inode)->root;
10261 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -040010262 u64 cur_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +000010263 u64 i_size;
Chris Mason154ea282013-03-05 11:11:26 -050010264 u64 cur_bytes;
Josef Bacik0b670dc2015-09-23 17:11:16 -040010265 u64 last_alloc = (u64)-1;
Yan Zhengd899e052008-10-30 14:25:28 -040010266 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -040010267 bool own_trans = true;
Wang Xiaoguang18513092016-07-25 15:51:40 +080010268 u64 end = start + num_bytes - 1;
Yan Zhengd899e052008-10-30 14:25:28 -040010269
Josef Bacik0af3d002010-06-21 14:48:16 -040010270 if (trans)
10271 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -040010272 while (num_bytes > 0) {
Josef Bacik0af3d002010-06-21 14:48:16 -040010273 if (own_trans) {
10274 trans = btrfs_start_transaction(root, 3);
10275 if (IS_ERR(trans)) {
10276 ret = PTR_ERR(trans);
10277 break;
10278 }
Yan Zhengd899e052008-10-30 14:25:28 -040010279 }
Yan, Zheng5a303d52009-11-12 09:34:52 +000010280
Byongho Leeee221842015-12-15 01:42:10 +090010281 cur_bytes = min_t(u64, num_bytes, SZ_256M);
Chris Mason154ea282013-03-05 11:11:26 -050010282 cur_bytes = max(cur_bytes, min_size);
Josef Bacik0b670dc2015-09-23 17:11:16 -040010283 /*
10284 * If we are severely fragmented we could end up with really
10285 * small allocations, so if the allocator is returning small
10286 * chunks lets make its job easier by only searching for those
10287 * sized chunks.
10288 */
10289 cur_bytes = min(cur_bytes, last_alloc);
Wang Xiaoguang18513092016-07-25 15:51:40 +080010290 ret = btrfs_reserve_extent(root, cur_bytes, cur_bytes,
10291 min_size, 0, *alloc_hint, &ins, 1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010292 if (ret) {
Josef Bacik0af3d002010-06-21 14:48:16 -040010293 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010294 btrfs_end_transaction(trans);
Yan, Zhenga22285a2010-05-16 10:48:46 -040010295 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +000010296 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010297 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010298
Josef Bacik0b670dc2015-09-23 17:11:16 -040010299 last_alloc = ins.offset;
Yan Zhengd899e052008-10-30 14:25:28 -040010300 ret = insert_reserved_file_extent(trans, inode,
10301 cur_offset, ins.objectid,
10302 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +000010303 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -040010304 BTRFS_FILE_EXTENT_PREALLOC);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010305 if (ret) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010306 btrfs_free_reserved_extent(fs_info, ins.objectid,
Miao Xiee570fd22014-06-19 10:42:50 +080010307 ins.offset, 0);
Jeff Mahoney66642832016-06-10 18:19:25 -040010308 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010309 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010310 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010311 break;
10312 }
Dongsheng Yang31193212014-12-12 16:44:35 +080010313
Nikolay Borisovdcdbc052017-02-20 13:50:45 +020010314 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Chris Masona1ed8352009-09-11 12:27:37 -040010315 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010316
Josef Bacik5dc562c2012-08-17 13:14:17 -040010317 em = alloc_extent_map();
10318 if (!em) {
10319 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
10320 &BTRFS_I(inode)->runtime_flags);
10321 goto next;
10322 }
10323
10324 em->start = cur_offset;
10325 em->orig_start = cur_offset;
10326 em->len = ins.offset;
10327 em->block_start = ins.objectid;
10328 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -050010329 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -040010330 em->ram_bytes = ins.offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010331 em->bdev = fs_info->fs_devices->latest_bdev;
Josef Bacik5dc562c2012-08-17 13:14:17 -040010332 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
10333 em->generation = trans->transid;
10334
10335 while (1) {
10336 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -040010337 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -040010338 write_unlock(&em_tree->lock);
10339 if (ret != -EEXIST)
10340 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +020010341 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -040010342 cur_offset + ins.offset - 1,
10343 0);
10344 }
10345 free_extent_map(em);
10346next:
Yan Zhengd899e052008-10-30 14:25:28 -040010347 num_bytes -= ins.offset;
10348 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -040010349 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +000010350
Josef Bacik0c4d2d92012-04-05 15:03:02 -040010351 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -070010352 inode->i_ctime = current_time(inode);
Christoph Hellwig6cbff002009-04-17 10:37:41 +020010353 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -040010354 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -040010355 (actual_len > inode->i_size) &&
10356 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +000010357 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +000010358 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +000010359 else
Josef Bacik55a61d12010-11-22 18:50:32 +000010360 i_size = cur_offset;
10361 i_size_write(inode, i_size);
10362 btrfs_ordered_update_i_size(inode, i_size, NULL);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010363 }
10364
Yan Zhengd899e052008-10-30 14:25:28 -040010365 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010366
10367 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -040010368 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010369 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010370 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010371 break;
10372 }
Yan Zhengd899e052008-10-30 14:25:28 -040010373
Josef Bacik0af3d002010-06-21 14:48:16 -040010374 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010375 btrfs_end_transaction(trans);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010376 }
Wang Xiaoguang18513092016-07-25 15:51:40 +080010377 if (cur_offset < end)
Qu Wenruobc42bda2017-02-27 15:10:39 +080010378 btrfs_free_reserved_data_space(inode, NULL, cur_offset,
Wang Xiaoguang18513092016-07-25 15:51:40 +080010379 end - cur_offset + 1);
Yan Zhengd899e052008-10-30 14:25:28 -040010380 return ret;
10381}
10382
Josef Bacik0af3d002010-06-21 14:48:16 -040010383int btrfs_prealloc_file_range(struct inode *inode, int mode,
10384 u64 start, u64 num_bytes, u64 min_size,
10385 loff_t actual_len, u64 *alloc_hint)
10386{
10387 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
10388 min_size, actual_len, alloc_hint,
10389 NULL);
10390}
10391
10392int btrfs_prealloc_file_range_trans(struct inode *inode,
10393 struct btrfs_trans_handle *trans, int mode,
10394 u64 start, u64 num_bytes, u64 min_size,
10395 loff_t actual_len, u64 *alloc_hint)
10396{
10397 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
10398 min_size, actual_len, alloc_hint, trans);
10399}
10400
Chris Masone6dcd2d2008-07-17 12:53:50 -040010401static int btrfs_set_page_dirty(struct page *page)
10402{
Chris Masone6dcd2d2008-07-17 12:53:50 -040010403 return __set_page_dirty_nobuffers(page);
10404}
10405
Al Viro10556cb22011-06-20 19:28:19 -040010406static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -050010407{
Li Zefanb83cc962010-12-20 16:04:08 +080010408 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +000010409 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +080010410
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +000010411 if (mask & MAY_WRITE &&
10412 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
10413 if (btrfs_root_readonly(root))
10414 return -EROFS;
10415 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
10416 return -EACCES;
10417 }
Al Viro2830ba72011-06-20 19:16:29 -040010418 return generic_permission(inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -050010419}
Chris Mason39279cc2007-06-12 06:35:45 -040010420
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010421static int btrfs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
10422{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010423 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010424 struct btrfs_trans_handle *trans;
10425 struct btrfs_root *root = BTRFS_I(dir)->root;
10426 struct inode *inode = NULL;
10427 u64 objectid;
10428 u64 index;
10429 int ret = 0;
10430
10431 /*
10432 * 5 units required for adding orphan entry
10433 */
10434 trans = btrfs_start_transaction(root, 5);
10435 if (IS_ERR(trans))
10436 return PTR_ERR(trans);
10437
10438 ret = btrfs_find_free_ino(root, &objectid);
10439 if (ret)
10440 goto out;
10441
10442 inode = btrfs_new_inode(trans, root, dir, NULL, 0,
David Sterbaf85b7372017-01-20 14:54:07 +010010443 btrfs_ino(BTRFS_I(dir)), objectid, mode, &index);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010444 if (IS_ERR(inode)) {
10445 ret = PTR_ERR(inode);
10446 inode = NULL;
10447 goto out;
10448 }
10449
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010450 inode->i_fop = &btrfs_file_operations;
10451 inode->i_op = &btrfs_file_inode_operations;
10452
10453 inode->i_mapping->a_ops = &btrfs_aops;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010454 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
10455
Chris Masonb0d5d102014-09-08 13:08:51 -070010456 ret = btrfs_init_inode_security(trans, inode, dir, NULL);
10457 if (ret)
Al Viro32955c52018-05-16 12:20:05 -040010458 goto out;
Chris Masonb0d5d102014-09-08 13:08:51 -070010459
10460 ret = btrfs_update_inode(trans, root, inode);
10461 if (ret)
Al Viro32955c52018-05-16 12:20:05 -040010462 goto out;
Nikolay Borisov73f2e542017-02-20 13:50:59 +020010463 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010464 if (ret)
Al Viro32955c52018-05-16 12:20:05 -040010465 goto out;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010466
Filipe Manana5762b5c2014-08-01 00:10:32 +010010467 /*
10468 * We set number of links to 0 in btrfs_new_inode(), and here we set
10469 * it to 1 because d_tmpfile() will issue a warning if the count is 0,
10470 * through:
10471 *
10472 * d_tmpfile() -> inode_dec_link_count() -> drop_nlink()
10473 */
10474 set_nlink(inode, 1);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010475 d_tmpfile(dentry, inode);
Al Viro32955c52018-05-16 12:20:05 -040010476 unlock_new_inode(inode);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010477 mark_inode_dirty(inode);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010478out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010479 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -040010480 if (ret && inode)
10481 discard_new_inode(inode);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010482 btrfs_btree_balance_dirty(fs_info);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010483 return ret;
10484}
10485
David Sterba5cdc84b2018-07-18 20:32:52 +020010486void btrfs_set_range_writeback(struct extent_io_tree *tree, u64 start, u64 end)
Josef Bacikc6100a42017-05-05 11:57:13 -040010487{
David Sterba5cdc84b2018-07-18 20:32:52 +020010488 struct inode *inode = tree->private_data;
Josef Bacikc6100a42017-05-05 11:57:13 -040010489 unsigned long index = start >> PAGE_SHIFT;
10490 unsigned long end_index = end >> PAGE_SHIFT;
10491 struct page *page;
10492
10493 while (index <= end_index) {
10494 page = find_get_page(inode->i_mapping, index);
10495 ASSERT(page); /* Pages should be in the extent_io_tree */
10496 set_page_writeback(page);
10497 put_page(page);
10498 index++;
10499 }
10500}
10501
Omar Sandovaled46ff32016-11-03 10:28:14 -070010502#ifdef CONFIG_SWAP
10503/*
10504 * Add an entry indicating a block group or device which is pinned by a
10505 * swapfile. Returns 0 on success, 1 if there is already an entry for it, or a
10506 * negative errno on failure.
10507 */
10508static int btrfs_add_swapfile_pin(struct inode *inode, void *ptr,
10509 bool is_block_group)
10510{
10511 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
10512 struct btrfs_swapfile_pin *sp, *entry;
10513 struct rb_node **p;
10514 struct rb_node *parent = NULL;
10515
10516 sp = kmalloc(sizeof(*sp), GFP_NOFS);
10517 if (!sp)
10518 return -ENOMEM;
10519 sp->ptr = ptr;
10520 sp->inode = inode;
10521 sp->is_block_group = is_block_group;
10522
10523 spin_lock(&fs_info->swapfile_pins_lock);
10524 p = &fs_info->swapfile_pins.rb_node;
10525 while (*p) {
10526 parent = *p;
10527 entry = rb_entry(parent, struct btrfs_swapfile_pin, node);
10528 if (sp->ptr < entry->ptr ||
10529 (sp->ptr == entry->ptr && sp->inode < entry->inode)) {
10530 p = &(*p)->rb_left;
10531 } else if (sp->ptr > entry->ptr ||
10532 (sp->ptr == entry->ptr && sp->inode > entry->inode)) {
10533 p = &(*p)->rb_right;
10534 } else {
10535 spin_unlock(&fs_info->swapfile_pins_lock);
10536 kfree(sp);
10537 return 1;
10538 }
10539 }
10540 rb_link_node(&sp->node, parent, p);
10541 rb_insert_color(&sp->node, &fs_info->swapfile_pins);
10542 spin_unlock(&fs_info->swapfile_pins_lock);
10543 return 0;
10544}
10545
10546/* Free all of the entries pinned by this swapfile. */
10547static void btrfs_free_swapfile_pins(struct inode *inode)
10548{
10549 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
10550 struct btrfs_swapfile_pin *sp;
10551 struct rb_node *node, *next;
10552
10553 spin_lock(&fs_info->swapfile_pins_lock);
10554 node = rb_first(&fs_info->swapfile_pins);
10555 while (node) {
10556 next = rb_next(node);
10557 sp = rb_entry(node, struct btrfs_swapfile_pin, node);
10558 if (sp->inode == inode) {
10559 rb_erase(&sp->node, &fs_info->swapfile_pins);
10560 if (sp->is_block_group)
10561 btrfs_put_block_group(sp->ptr);
10562 kfree(sp);
10563 }
10564 node = next;
10565 }
10566 spin_unlock(&fs_info->swapfile_pins_lock);
10567}
10568
10569struct btrfs_swap_info {
10570 u64 start;
10571 u64 block_start;
10572 u64 block_len;
10573 u64 lowest_ppage;
10574 u64 highest_ppage;
10575 unsigned long nr_pages;
10576 int nr_extents;
10577};
10578
10579static int btrfs_add_swap_extent(struct swap_info_struct *sis,
10580 struct btrfs_swap_info *bsi)
10581{
10582 unsigned long nr_pages;
10583 u64 first_ppage, first_ppage_reported, next_ppage;
10584 int ret;
10585
10586 first_ppage = ALIGN(bsi->block_start, PAGE_SIZE) >> PAGE_SHIFT;
10587 next_ppage = ALIGN_DOWN(bsi->block_start + bsi->block_len,
10588 PAGE_SIZE) >> PAGE_SHIFT;
10589
10590 if (first_ppage >= next_ppage)
10591 return 0;
10592 nr_pages = next_ppage - first_ppage;
10593
10594 first_ppage_reported = first_ppage;
10595 if (bsi->start == 0)
10596 first_ppage_reported++;
10597 if (bsi->lowest_ppage > first_ppage_reported)
10598 bsi->lowest_ppage = first_ppage_reported;
10599 if (bsi->highest_ppage < (next_ppage - 1))
10600 bsi->highest_ppage = next_ppage - 1;
10601
10602 ret = add_swap_extent(sis, bsi->nr_pages, nr_pages, first_ppage);
10603 if (ret < 0)
10604 return ret;
10605 bsi->nr_extents += ret;
10606 bsi->nr_pages += nr_pages;
10607 return 0;
10608}
10609
10610static void btrfs_swap_deactivate(struct file *file)
10611{
10612 struct inode *inode = file_inode(file);
10613
10614 btrfs_free_swapfile_pins(inode);
10615 atomic_dec(&BTRFS_I(inode)->root->nr_swapfiles);
10616}
10617
10618static int btrfs_swap_activate(struct swap_info_struct *sis, struct file *file,
10619 sector_t *span)
10620{
10621 struct inode *inode = file_inode(file);
10622 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
10623 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
10624 struct extent_state *cached_state = NULL;
10625 struct extent_map *em = NULL;
10626 struct btrfs_device *device = NULL;
10627 struct btrfs_swap_info bsi = {
10628 .lowest_ppage = (sector_t)-1ULL,
10629 };
10630 int ret = 0;
10631 u64 isize;
10632 u64 start;
10633
10634 /*
10635 * If the swap file was just created, make sure delalloc is done. If the
10636 * file changes again after this, the user is doing something stupid and
10637 * we don't really care.
10638 */
10639 ret = btrfs_wait_ordered_range(inode, 0, (u64)-1);
10640 if (ret)
10641 return ret;
10642
10643 /*
10644 * The inode is locked, so these flags won't change after we check them.
10645 */
10646 if (BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS) {
10647 btrfs_warn(fs_info, "swapfile must not be compressed");
10648 return -EINVAL;
10649 }
10650 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW)) {
10651 btrfs_warn(fs_info, "swapfile must not be copy-on-write");
10652 return -EINVAL;
10653 }
10654 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
10655 btrfs_warn(fs_info, "swapfile must not be checksummed");
10656 return -EINVAL;
10657 }
10658
10659 /*
10660 * Balance or device remove/replace/resize can move stuff around from
10661 * under us. The EXCL_OP flag makes sure they aren't running/won't run
10662 * concurrently while we are mapping the swap extents, and
10663 * fs_info->swapfile_pins prevents them from running while the swap file
10664 * is active and moving the extents. Note that this also prevents a
10665 * concurrent device add which isn't actually necessary, but it's not
10666 * really worth the trouble to allow it.
10667 */
10668 if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) {
10669 btrfs_warn(fs_info,
10670 "cannot activate swapfile while exclusive operation is running");
10671 return -EBUSY;
10672 }
10673 /*
10674 * Snapshots can create extents which require COW even if NODATACOW is
10675 * set. We use this counter to prevent snapshots. We must increment it
10676 * before walking the extents because we don't want a concurrent
10677 * snapshot to run after we've already checked the extents.
10678 */
10679 atomic_inc(&BTRFS_I(inode)->root->nr_swapfiles);
10680
10681 isize = ALIGN_DOWN(inode->i_size, fs_info->sectorsize);
10682
10683 lock_extent_bits(io_tree, 0, isize - 1, &cached_state);
10684 start = 0;
10685 while (start < isize) {
10686 u64 logical_block_start, physical_block_start;
10687 struct btrfs_block_group_cache *bg;
10688 u64 len = isize - start;
10689
10690 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len, 0);
10691 if (IS_ERR(em)) {
10692 ret = PTR_ERR(em);
10693 goto out;
10694 }
10695
10696 if (em->block_start == EXTENT_MAP_HOLE) {
10697 btrfs_warn(fs_info, "swapfile must not have holes");
10698 ret = -EINVAL;
10699 goto out;
10700 }
10701 if (em->block_start == EXTENT_MAP_INLINE) {
10702 /*
10703 * It's unlikely we'll ever actually find ourselves
10704 * here, as a file small enough to fit inline won't be
10705 * big enough to store more than the swap header, but in
10706 * case something changes in the future, let's catch it
10707 * here rather than later.
10708 */
10709 btrfs_warn(fs_info, "swapfile must not be inline");
10710 ret = -EINVAL;
10711 goto out;
10712 }
10713 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
10714 btrfs_warn(fs_info, "swapfile must not be compressed");
10715 ret = -EINVAL;
10716 goto out;
10717 }
10718
10719 logical_block_start = em->block_start + (start - em->start);
10720 len = min(len, em->len - (start - em->start));
10721 free_extent_map(em);
10722 em = NULL;
10723
10724 ret = can_nocow_extent(inode, start, &len, NULL, NULL, NULL);
10725 if (ret < 0) {
10726 goto out;
10727 } else if (ret) {
10728 ret = 0;
10729 } else {
10730 btrfs_warn(fs_info,
10731 "swapfile must not be copy-on-write");
10732 ret = -EINVAL;
10733 goto out;
10734 }
10735
10736 em = btrfs_get_chunk_map(fs_info, logical_block_start, len);
10737 if (IS_ERR(em)) {
10738 ret = PTR_ERR(em);
10739 goto out;
10740 }
10741
10742 if (em->map_lookup->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
10743 btrfs_warn(fs_info,
10744 "swapfile must have single data profile");
10745 ret = -EINVAL;
10746 goto out;
10747 }
10748
10749 if (device == NULL) {
10750 device = em->map_lookup->stripes[0].dev;
10751 ret = btrfs_add_swapfile_pin(inode, device, false);
10752 if (ret == 1)
10753 ret = 0;
10754 else if (ret)
10755 goto out;
10756 } else if (device != em->map_lookup->stripes[0].dev) {
10757 btrfs_warn(fs_info, "swapfile must be on one device");
10758 ret = -EINVAL;
10759 goto out;
10760 }
10761
10762 physical_block_start = (em->map_lookup->stripes[0].physical +
10763 (logical_block_start - em->start));
10764 len = min(len, em->len - (logical_block_start - em->start));
10765 free_extent_map(em);
10766 em = NULL;
10767
10768 bg = btrfs_lookup_block_group(fs_info, logical_block_start);
10769 if (!bg) {
10770 btrfs_warn(fs_info,
10771 "could not find block group containing swapfile");
10772 ret = -EINVAL;
10773 goto out;
10774 }
10775
10776 ret = btrfs_add_swapfile_pin(inode, bg, true);
10777 if (ret) {
10778 btrfs_put_block_group(bg);
10779 if (ret == 1)
10780 ret = 0;
10781 else
10782 goto out;
10783 }
10784
10785 if (bsi.block_len &&
10786 bsi.block_start + bsi.block_len == physical_block_start) {
10787 bsi.block_len += len;
10788 } else {
10789 if (bsi.block_len) {
10790 ret = btrfs_add_swap_extent(sis, &bsi);
10791 if (ret)
10792 goto out;
10793 }
10794 bsi.start = start;
10795 bsi.block_start = physical_block_start;
10796 bsi.block_len = len;
10797 }
10798
10799 start += len;
10800 }
10801
10802 if (bsi.block_len)
10803 ret = btrfs_add_swap_extent(sis, &bsi);
10804
10805out:
10806 if (!IS_ERR_OR_NULL(em))
10807 free_extent_map(em);
10808
10809 unlock_extent_cached(io_tree, 0, isize - 1, &cached_state);
10810
10811 if (ret)
10812 btrfs_swap_deactivate(file);
10813
10814 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
10815
10816 if (ret)
10817 return ret;
10818
10819 if (device)
10820 sis->bdev = device->bdev;
10821 *span = bsi.highest_ppage - bsi.lowest_ppage + 1;
10822 sis->max = bsi.nr_pages;
10823 sis->pages = bsi.nr_pages - 1;
10824 sis->highest_bit = bsi.nr_pages - 1;
10825 return bsi.nr_extents;
10826}
10827#else
10828static void btrfs_swap_deactivate(struct file *file)
10829{
10830}
10831
10832static int btrfs_swap_activate(struct swap_info_struct *sis, struct file *file,
10833 sector_t *span)
10834{
10835 return -EOPNOTSUPP;
10836}
10837#endif
10838
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010839static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -050010840 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -040010841 .lookup = btrfs_lookup,
10842 .create = btrfs_create,
10843 .unlink = btrfs_unlink,
10844 .link = btrfs_link,
10845 .mkdir = btrfs_mkdir,
10846 .rmdir = btrfs_rmdir,
Miklos Szeredi2773bf02016-09-27 11:03:58 +020010847 .rename = btrfs_rename2,
Chris Mason39279cc2007-06-12 06:35:45 -040010848 .symlink = btrfs_symlink,
10849 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -040010850 .mknod = btrfs_mknod,
Josef Bacik5103e942007-11-16 11:45:54 -050010851 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010852 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010853 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010854 .set_acl = btrfs_set_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070010855 .update_time = btrfs_update_time,
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010856 .tmpfile = btrfs_tmpfile,
Chris Mason39279cc2007-06-12 06:35:45 -040010857};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010858static const struct inode_operations btrfs_dir_ro_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010859 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -050010860 .permission = btrfs_permission,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070010861 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010862};
Yan, Zheng76dda932009-09-21 16:00:26 -040010863
Alexey Dobriyan828c0952009-10-01 15:43:56 -070010864static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010865 .llseek = generic_file_llseek,
10866 .read = generic_read_dir,
Omar Sandoval02dbfc92016-05-20 13:50:33 -070010867 .iterate_shared = btrfs_real_readdir,
Josef Bacik23b5ec72017-07-24 15:14:25 -040010868 .open = btrfs_opendir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -040010869 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010870#ifdef CONFIG_COMPAT
Luke Dashjr4c63c242015-10-29 08:22:21 +000010871 .compat_ioctl = btrfs_compat_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010872#endif
Sage Weil6bf13c02008-06-10 10:07:39 -040010873 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -040010874 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -040010875};
10876
David Sterba20e55062015-11-19 11:42:28 +010010877static const struct extent_io_ops btrfs_extent_io_ops = {
David Sterba4d53ddd2017-02-17 15:27:44 +010010878 /* mandatory callbacks */
Chris Mason065631f2008-02-20 12:07:25 -050010879 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -040010880 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
10881};
10882
Chris Mason35054392009-01-21 13:11:13 -050010883/*
10884 * btrfs doesn't support the bmap operation because swapfiles
10885 * use bmap to make a mapping of extents in the file. They assume
10886 * these extents won't change over the life of the file and they
10887 * use the bmap result to do IO directly to the drive.
10888 *
10889 * the btrfs bmap call would return logical addresses that aren't
10890 * suitable for IO and they also will change frequently as COW
10891 * operations happen. So, swapfile + btrfs == corruption.
10892 *
10893 * For now we're avoiding this by dropping bmap.
10894 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -070010895static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -040010896 .readpage = btrfs_readpage,
10897 .writepage = btrfs_writepage,
Chris Masonb293f02e2007-11-01 19:45:34 -040010898 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -050010899 .readpages = btrfs_readpages,
Chris Mason16432982008-04-10 10:23:21 -040010900 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -040010901 .invalidatepage = btrfs_invalidatepage,
10902 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -040010903 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +020010904 .error_remove_page = generic_error_remove_page,
Omar Sandovaled46ff32016-11-03 10:28:14 -070010905 .swap_activate = btrfs_swap_activate,
10906 .swap_deactivate = btrfs_swap_deactivate,
Chris Mason39279cc2007-06-12 06:35:45 -040010907};
10908
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010909static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010910 .getattr = btrfs_getattr,
10911 .setattr = btrfs_setattr,
Josef Bacik5103e942007-11-16 11:45:54 -050010912 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010913 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -050010914 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010915 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010916 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010917 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010918};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010919static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -040010920 .getattr = btrfs_getattr,
10921 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010922 .permission = btrfs_permission,
Josef Bacik33268ea2008-07-24 12:16:36 -040010923 .listxattr = btrfs_listxattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010924 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010925 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010926 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -040010927};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010928static const struct inode_operations btrfs_symlink_inode_operations = {
Al Viro6b255392015-11-17 10:20:54 -050010929 .get_link = page_get_link,
Li Zefanf2095612010-11-19 02:05:24 +000010930 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -050010931 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010932 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -050010933 .listxattr = btrfs_listxattr,
Josef Bacike41f9412012-03-26 09:46:47 -040010934 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010935};
Yan, Zheng76dda932009-09-21 16:00:26 -040010936
Alexey Dobriyan82d339d2009-10-09 09:54:36 -040010937const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -040010938 .d_delete = btrfs_dentry_delete,
10939};