blob: 05362559645e1c508bd0d6e22802ec5928de7d85 [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
Nikolay Borisov97db1202019-03-12 17:20:24 +0200358struct async_chunk {
Chris Mason771ed682008-11-06 22:02:51 -0500359 struct inode *inode;
Chris Mason771ed682008-11-06 22:02:51 -0500360 struct page *locked_page;
361 u64 start;
362 u64 end;
Liu Bof82b7352017-10-23 23:18:16 -0600363 unsigned int write_flags;
Chris Mason771ed682008-11-06 22:02:51 -0500364 struct list_head extents;
365 struct btrfs_work work;
Nikolay Borisov97db1202019-03-12 17:20:24 +0200366 atomic_t *pending;
Chris Mason771ed682008-11-06 22:02:51 -0500367};
368
Nikolay Borisov97db1202019-03-12 17:20:24 +0200369struct async_cow {
370 /* Number of chunks in flight; must be first in the structure */
371 atomic_t num_chunks;
372 struct async_chunk chunks[];
373};
374
375static noinline int add_async_extent(struct async_chunk *cow,
Chris Mason771ed682008-11-06 22:02:51 -0500376 u64 start, u64 ram_size,
377 u64 compressed_size,
378 struct page **pages,
Li Zefan261507a02010-12-17 14:21:50 +0800379 unsigned long nr_pages,
380 int compress_type)
Chris Mason771ed682008-11-06 22:02:51 -0500381{
382 struct async_extent *async_extent;
383
384 async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100385 BUG_ON(!async_extent); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -0500386 async_extent->start = start;
387 async_extent->ram_size = ram_size;
388 async_extent->compressed_size = compressed_size;
389 async_extent->pages = pages;
390 async_extent->nr_pages = nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800391 async_extent->compress_type = compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500392 list_add_tail(&async_extent->list, &cow->extents);
393 return 0;
394}
395
Timofey Titovetsc2fcdcd2017-07-17 16:52:58 +0300396static inline int inode_need_compress(struct inode *inode, u64 start, u64 end)
Wang Shilongf79707b2014-07-17 11:44:09 +0800397{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400398 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Wang Shilongf79707b2014-07-17 11:44:09 +0800399
400 /* force compress */
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400401 if (btrfs_test_opt(fs_info, FORCE_COMPRESS))
Wang Shilongf79707b2014-07-17 11:44:09 +0800402 return 1;
David Sterbaeec63c62017-07-17 19:41:31 +0200403 /* defrag ioctl */
404 if (BTRFS_I(inode)->defrag_compress)
405 return 1;
Wang Shilongf79707b2014-07-17 11:44:09 +0800406 /* bad compression ratios */
407 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS)
408 return 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400409 if (btrfs_test_opt(fs_info, COMPRESS) ||
Wang Shilongf79707b2014-07-17 11:44:09 +0800410 BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS ||
David Sterbab52aa8c2017-07-17 19:17:20 +0200411 BTRFS_I(inode)->prop_compress)
Timofey Titovetsc2fcdcd2017-07-17 16:52:58 +0300412 return btrfs_compress_heuristic(inode, start, end);
Wang Shilongf79707b2014-07-17 11:44:09 +0800413 return 0;
414}
415
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200416static inline void inode_should_defrag(struct btrfs_inode *inode,
Anand Jain26d30f82016-12-19 19:09:06 +0800417 u64 start, u64 end, u64 num_bytes, u64 small_write)
418{
419 /* If this is a small write inside eof, kick off a defrag */
420 if (num_bytes < small_write &&
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200421 (start > 0 || end + 1 < inode->disk_i_size))
Anand Jain26d30f82016-12-19 19:09:06 +0800422 btrfs_add_inode_defrag(NULL, inode);
423}
424
Chris Masonc8b97812008-10-29 14:49:59 -0400425/*
Chris Mason771ed682008-11-06 22:02:51 -0500426 * we create compressed extents in two phases. The first
427 * phase compresses a range of pages that have already been
428 * locked (both pages and state bits are locked).
Chris Masonc8b97812008-10-29 14:49:59 -0400429 *
Chris Mason771ed682008-11-06 22:02:51 -0500430 * This is done inside an ordered work queue, and the compression
431 * is spread across many cpus. The actual IO submission is step
432 * two, and the ordered work queue takes care of making sure that
433 * happens in the same order things were put onto the queue by
434 * writepages and friends.
Chris Masonc8b97812008-10-29 14:49:59 -0400435 *
Chris Mason771ed682008-11-06 22:02:51 -0500436 * If this code finds it can't get good compression, it puts an
437 * entry onto the work queue to write the uncompressed bytes. This
438 * makes sure that both compressed inodes and uncompressed inodes
Artem Bityutskiyb2570312012-07-25 18:12:06 +0300439 * are written in the same order that the flusher thread sent them
440 * down.
Chris Masond352ac62008-09-29 15:18:18 -0400441 */
Nikolay Borisov1368c6d2019-03-12 17:20:27 +0200442static noinline void compress_file_range(struct async_chunk *async_chunk,
443 int *num_added)
Chris Masonb888db22007-08-27 16:49:44 -0400444{
Nikolay Borisov1368c6d2019-03-12 17:20:27 +0200445 struct inode *inode = async_chunk->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400446 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400447 u64 blocksize = fs_info->sectorsize;
Nikolay Borisov1368c6d2019-03-12 17:20:27 +0200448 u64 start = async_chunk->start;
449 u64 end = async_chunk->end;
Chris Masonc8b97812008-10-29 14:49:59 -0400450 u64 actual_end;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400451 int ret = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400452 struct page **pages = NULL;
453 unsigned long nr_pages;
Chris Masonc8b97812008-10-29 14:49:59 -0400454 unsigned long total_compressed = 0;
455 unsigned long total_in = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400456 int i;
457 int will_compress;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400458 int compress_type = fs_info->compress_type;
Chris Mason4adaa612013-03-26 13:07:00 -0400459 int redirty = 0;
Chris Masonb888db22007-08-27 16:49:44 -0400460
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200461 inode_should_defrag(BTRFS_I(inode), start, end, end - start + 1,
462 SZ_16K);
Chris Mason4cb53002011-05-24 15:35:30 -0400463
Nikolay Borisov62b37622019-01-03 10:50:00 +0200464 actual_end = min_t(u64, i_size_read(inode), end + 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400465again:
466 will_compress = 0;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300467 nr_pages = (end >> PAGE_SHIFT) - (start >> PAGE_SHIFT) + 1;
David Sterba069eac72017-02-14 19:36:54 +0100468 BUILD_BUG_ON((BTRFS_MAX_COMPRESSED % PAGE_SIZE) != 0);
469 nr_pages = min_t(unsigned long, nr_pages,
470 BTRFS_MAX_COMPRESSED / PAGE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400471
Chris Masonf03d9301f2009-02-04 09:31:06 -0500472 /*
473 * we don't want to send crud past the end of i_size through
474 * compression, that's just a waste of CPU time. So, if the
475 * end of the file is before the start of our current
476 * requested range of bytes, we bail out to the uncompressed
477 * cleanup code that can deal with all of this.
478 *
479 * It isn't really the fastest way to fix things, but this is a
480 * very uncommon corner.
481 */
482 if (actual_end <= start)
483 goto cleanup_and_bail_uncompressed;
484
Chris Masonc8b97812008-10-29 14:49:59 -0400485 total_compressed = actual_end - start;
486
Shilong Wang4bcbb332014-10-07 18:44:35 -0400487 /*
488 * skip compression for a small file range(<=blocksize) that
Nicholas D Steeves01327612016-05-19 21:18:45 -0400489 * isn't an inline extent, since it doesn't save disk space at all.
Shilong Wang4bcbb332014-10-07 18:44:35 -0400490 */
491 if (total_compressed <= blocksize &&
492 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
493 goto cleanup_and_bail_uncompressed;
494
David Sterba069eac72017-02-14 19:36:54 +0100495 total_compressed = min_t(unsigned long, total_compressed,
496 BTRFS_MAX_UNCOMPRESSED);
Chris Masonc8b97812008-10-29 14:49:59 -0400497 total_in = 0;
498 ret = 0;
Chris Masondb945352007-10-15 16:15:53 -0400499
Chris Mason771ed682008-11-06 22:02:51 -0500500 /*
501 * we do compression for mount -o compress and when the
502 * inode has not been flagged as nocompress. This flag can
503 * change at any time if we discover bad compression ratios.
Chris Masonc8b97812008-10-29 14:49:59 -0400504 */
Timofey Titovetsc2fcdcd2017-07-17 16:52:58 +0300505 if (inode_need_compress(inode, start, end)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400506 WARN_ON(pages);
David Sterba31e818f2015-02-20 18:00:26 +0100507 pages = kcalloc(nr_pages, sizeof(struct page *), GFP_NOFS);
Li Zefan560f7d72011-09-08 10:22:01 +0800508 if (!pages) {
509 /* just bail out to the uncompressed code */
Filipe Manana3527a012018-10-13 00:37:25 +0100510 nr_pages = 0;
Li Zefan560f7d72011-09-08 10:22:01 +0800511 goto cont;
512 }
Chris Mason179e29e2007-11-01 11:28:41 -0400513
David Sterbaeec63c62017-07-17 19:41:31 +0200514 if (BTRFS_I(inode)->defrag_compress)
515 compress_type = BTRFS_I(inode)->defrag_compress;
516 else if (BTRFS_I(inode)->prop_compress)
David Sterbab52aa8c2017-07-17 19:17:20 +0200517 compress_type = BTRFS_I(inode)->prop_compress;
Li Zefan261507a02010-12-17 14:21:50 +0800518
Chris Mason4adaa612013-03-26 13:07:00 -0400519 /*
520 * we need to call clear_page_dirty_for_io on each
521 * page in the range. Otherwise applications with the file
522 * mmap'd can wander in and change the page contents while
523 * we are compressing them.
524 *
525 * If the compression fails for any reason, we set the pages
526 * dirty again later on.
Timofey Titovetse9679de2017-10-24 01:29:48 +0300527 *
528 * Note that the remaining part is redirtied, the start pointer
529 * has moved, the end is the original one.
Chris Mason4adaa612013-03-26 13:07:00 -0400530 */
Timofey Titovetse9679de2017-10-24 01:29:48 +0300531 if (!redirty) {
532 extent_range_clear_dirty_for_io(inode, start, end);
533 redirty = 1;
534 }
David Sterbaf51d2b52017-09-15 17:36:57 +0200535
536 /* Compression level is applied here and only here */
537 ret = btrfs_compress_pages(
538 compress_type | (fs_info->compress_level << 4),
Li Zefan261507a02010-12-17 14:21:50 +0800539 inode->i_mapping, start,
David Sterba38c31462017-02-14 19:04:07 +0100540 pages,
David Sterba4d3a8002017-02-14 19:04:07 +0100541 &nr_pages,
Li Zefan261507a02010-12-17 14:21:50 +0800542 &total_in,
David Sterbae5d74902017-02-14 19:45:05 +0100543 &total_compressed);
Chris Masonc8b97812008-10-29 14:49:59 -0400544
545 if (!ret) {
Johannes Thumshirn70730172018-12-05 15:23:03 +0100546 unsigned long offset = offset_in_page(total_compressed);
David Sterba4d3a8002017-02-14 19:04:07 +0100547 struct page *page = pages[nr_pages - 1];
Chris Masonc8b97812008-10-29 14:49:59 -0400548 char *kaddr;
549
550 /* zero the tail end of the last page, we might be
551 * sending it down to disk
552 */
553 if (offset) {
Cong Wang7ac687d2011-11-25 23:14:28 +0800554 kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400555 memset(kaddr + offset, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300556 PAGE_SIZE - offset);
Cong Wang7ac687d2011-11-25 23:14:28 +0800557 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400558 }
559 will_compress = 1;
560 }
561 }
Li Zefan560f7d72011-09-08 10:22:01 +0800562cont:
Chris Masonc8b97812008-10-29 14:49:59 -0400563 if (start == 0) {
564 /* lets try to make an inline extent */
Timofey Titovets6018ba02017-09-15 01:57:26 +0300565 if (ret || total_in < actual_end) {
Chris Masonc8b97812008-10-29 14:49:59 -0400566 /* we didn't compress the entire range, try
Chris Mason771ed682008-11-06 22:02:51 -0500567 * to make an uncompressed inline extent.
Chris Masonc8b97812008-10-29 14:49:59 -0400568 */
Nikolay Borisovd02c0e22018-03-02 09:43:15 +0200569 ret = cow_file_range_inline(inode, start, end, 0,
570 BTRFS_COMPRESS_NONE, NULL);
Chris Masonc8b97812008-10-29 14:49:59 -0400571 } else {
Chris Mason771ed682008-11-06 22:02:51 -0500572 /* try making a compressed inline extent */
Nikolay Borisovd02c0e22018-03-02 09:43:15 +0200573 ret = cow_file_range_inline(inode, start, end,
Li Zefanfe3f5662011-03-28 08:30:38 +0000574 total_compressed,
575 compress_type, pages);
Chris Masonc8b97812008-10-29 14:49:59 -0400576 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100577 if (ret <= 0) {
Josef Bacik151a41b2013-07-29 13:22:24 -0400578 unsigned long clear_flags = EXTENT_DELALLOC |
Josef Bacik8b62f872017-10-19 14:15:55 -0400579 EXTENT_DELALLOC_NEW | EXTENT_DEFRAG |
580 EXTENT_DO_ACCOUNTING;
Filipe Mananae6eb4312014-10-10 10:45:12 +0100581 unsigned long page_error_op;
582
Filipe Mananae6eb4312014-10-10 10:45:12 +0100583 page_error_op = ret < 0 ? PAGE_SET_ERROR : 0;
Josef Bacik151a41b2013-07-29 13:22:24 -0400584
Chris Mason771ed682008-11-06 22:02:51 -0500585 /*
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100586 * inline extent creation worked or returned error,
587 * we don't need to create any more async work items.
588 * Unlock and free up our temp pages.
Josef Bacik8b62f872017-10-19 14:15:55 -0400589 *
590 * We use DO_ACCOUNTING here because we need the
591 * delalloc_release_metadata to be done _after_ we drop
592 * our outstanding extent for clearing delalloc for this
593 * range.
Chris Mason771ed682008-11-06 22:02:51 -0500594 */
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800595 extent_clear_unlock_delalloc(inode, start, end, end,
596 NULL, clear_flags,
597 PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -0400598 PAGE_CLEAR_DIRTY |
599 PAGE_SET_WRITEBACK |
Filipe Mananae6eb4312014-10-10 10:45:12 +0100600 page_error_op |
Josef Bacikc2790a22013-07-29 11:20:47 -0400601 PAGE_END_WRITEBACK);
Chris Masonc8b97812008-10-29 14:49:59 -0400602 goto free_pages_out;
603 }
604 }
605
606 if (will_compress) {
607 /*
608 * we aren't doing an inline extent round the compressed size
609 * up to a block size boundary so the allocator does sane
610 * things
611 */
Qu Wenruofda28322013-02-26 08:10:22 +0000612 total_compressed = ALIGN(total_compressed, blocksize);
Chris Masonc8b97812008-10-29 14:49:59 -0400613
614 /*
615 * one last check to make sure the compression is really a
Timofey Titovets170607e2017-06-06 14:41:15 +0300616 * win, compare the page count read with the blocks on disk,
617 * compression must free at least one sector size
Chris Masonc8b97812008-10-29 14:49:59 -0400618 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300619 total_in = ALIGN(total_in, PAGE_SIZE);
Timofey Titovets170607e2017-06-06 14:41:15 +0300620 if (total_compressed + blocksize <= total_in) {
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700621 *num_added += 1;
622
623 /*
624 * The async work queues will take care of doing actual
625 * allocation on disk for these compressed pages, and
626 * will submit them to the elevator.
627 */
Nikolay Borisovb5326272019-03-12 17:20:25 +0200628 add_async_extent(async_chunk, start, total_in,
David Sterba4d3a8002017-02-14 19:04:07 +0100629 total_compressed, pages, nr_pages,
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700630 compress_type);
631
Timofey Titovets11708622017-10-03 18:06:01 +0300632 if (start + total_in < end) {
633 start += total_in;
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700634 pages = NULL;
635 cond_resched();
636 goto again;
637 }
638 return;
Chris Masonc8b97812008-10-29 14:49:59 -0400639 }
640 }
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700641 if (pages) {
Chris Masonc8b97812008-10-29 14:49:59 -0400642 /*
643 * the compression code ran but failed to make things smaller,
644 * free any pages it allocated and our page pointer array
645 */
David Sterba4d3a8002017-02-14 19:04:07 +0100646 for (i = 0; i < nr_pages; i++) {
Chris Mason70b99e62008-10-31 12:46:39 -0400647 WARN_ON(pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300648 put_page(pages[i]);
Chris Masonc8b97812008-10-29 14:49:59 -0400649 }
650 kfree(pages);
651 pages = NULL;
652 total_compressed = 0;
David Sterba4d3a8002017-02-14 19:04:07 +0100653 nr_pages = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400654
655 /* flag the file so we don't compress in the future */
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400656 if (!btrfs_test_opt(fs_info, FORCE_COMPRESS) &&
David Sterbab52aa8c2017-07-17 19:17:20 +0200657 !(BTRFS_I(inode)->prop_compress)) {
Chris Masona555f812010-01-28 16:18:15 -0500658 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
Chris Mason1e701a32010-03-11 09:42:04 -0500659 }
Chris Masonc8b97812008-10-29 14:49:59 -0400660 }
Chris Masonf03d9301f2009-02-04 09:31:06 -0500661cleanup_and_bail_uncompressed:
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700662 /*
663 * No compression, but we still need to write the pages in the file
664 * we've been given so far. redirty the locked page if it corresponds
665 * to our extent and set things up for the async work queue to run
666 * cow_file_range to do the normal delalloc dance.
667 */
Nikolay Borisov1368c6d2019-03-12 17:20:27 +0200668 if (page_offset(async_chunk->locked_page) >= start &&
669 page_offset(async_chunk->locked_page) <= end)
670 __set_page_dirty_nobuffers(async_chunk->locked_page);
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700671 /* unlocked later on in the async handlers */
672
673 if (redirty)
674 extent_range_redirty_for_io(inode, start, end);
Nikolay Borisovb5326272019-03-12 17:20:25 +0200675 add_async_extent(async_chunk, start, end - start + 1, 0, NULL, 0,
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700676 BTRFS_COMPRESS_NONE);
677 *num_added += 1;
Chris Mason771ed682008-11-06 22:02:51 -0500678
Filipe Mananac44f6492014-10-09 21:15:44 +0100679 return;
Chris Mason771ed682008-11-06 22:02:51 -0500680
681free_pages_out:
David Sterba4d3a8002017-02-14 19:04:07 +0100682 for (i = 0; i < nr_pages; i++) {
Chris Mason771ed682008-11-06 22:02:51 -0500683 WARN_ON(pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300684 put_page(pages[i]);
Chris Mason771ed682008-11-06 22:02:51 -0500685 }
Chris Masond3977122009-01-05 21:25:51 -0500686 kfree(pages);
Chris Mason771ed682008-11-06 22:02:51 -0500687}
Chris Mason771ed682008-11-06 22:02:51 -0500688
Filipe Manana40ae8372014-10-06 22:14:24 +0100689static void free_async_extent_pages(struct async_extent *async_extent)
690{
691 int i;
692
693 if (!async_extent->pages)
694 return;
695
696 for (i = 0; i < async_extent->nr_pages; i++) {
697 WARN_ON(async_extent->pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300698 put_page(async_extent->pages[i]);
Filipe Manana40ae8372014-10-06 22:14:24 +0100699 }
700 kfree(async_extent->pages);
701 async_extent->nr_pages = 0;
702 async_extent->pages = NULL;
Chris Mason771ed682008-11-06 22:02:51 -0500703}
704
705/*
706 * phase two of compressed writeback. This is the ordered portion
707 * of the code, which only gets called in the order the work was
708 * queued. We walk all the async extents created by compress_file_range
709 * and send them down to the disk.
710 */
Nikolay Borisovb5326272019-03-12 17:20:25 +0200711static noinline void submit_compressed_extents(struct async_chunk *async_chunk)
Chris Mason771ed682008-11-06 22:02:51 -0500712{
Nikolay Borisovb5326272019-03-12 17:20:25 +0200713 struct inode *inode = async_chunk->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400714 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason771ed682008-11-06 22:02:51 -0500715 struct async_extent *async_extent;
716 u64 alloc_hint = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500717 struct btrfs_key ins;
718 struct extent_map *em;
719 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason771ed682008-11-06 22:02:51 -0500720 struct extent_io_tree *io_tree;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500721 int ret = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500722
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500723again:
Nikolay Borisovb5326272019-03-12 17:20:25 +0200724 while (!list_empty(&async_chunk->extents)) {
725 async_extent = list_entry(async_chunk->extents.next,
Chris Mason771ed682008-11-06 22:02:51 -0500726 struct async_extent, list);
727 list_del(&async_extent->list);
728
729 io_tree = &BTRFS_I(inode)->io_tree;
730
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500731retry:
Chris Mason771ed682008-11-06 22:02:51 -0500732 /* did the compression code fall back to uncompressed IO? */
733 if (!async_extent->pages) {
734 int page_started = 0;
735 unsigned long nr_written = 0;
736
737 lock_extent(io_tree, async_extent->start,
Josef Bacik2ac55d42010-02-03 19:33:23 +0000738 async_extent->start +
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100739 async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500740
741 /* allocate blocks */
Nikolay Borisovb5326272019-03-12 17:20:25 +0200742 ret = cow_file_range(inode, async_chunk->locked_page,
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500743 async_extent->start,
744 async_extent->start +
745 async_extent->ram_size - 1,
Wang Xiaoguangdda32452016-07-11 11:05:29 +0800746 async_extent->start +
747 async_extent->ram_size - 1,
748 &page_started, &nr_written, 0,
749 NULL);
Chris Mason771ed682008-11-06 22:02:51 -0500750
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100751 /* JDM XXX */
752
Chris Mason771ed682008-11-06 22:02:51 -0500753 /*
754 * if page_started, cow_file_range inserted an
755 * inline extent and took care of all the unlocking
756 * and IO for us. Otherwise, we need to submit
757 * all those pages down to the drive.
758 */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500759 if (!page_started && !ret)
Nikolay Borisov5e3ee232017-12-08 15:55:58 +0200760 extent_write_locked_range(inode,
761 async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500762 async_extent->start +
Chris Mason771ed682008-11-06 22:02:51 -0500763 async_extent->ram_size - 1,
Chris Mason771ed682008-11-06 22:02:51 -0500764 WB_SYNC_ALL);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500765 else if (ret)
Nikolay Borisovb5326272019-03-12 17:20:25 +0200766 unlock_page(async_chunk->locked_page);
Chris Mason771ed682008-11-06 22:02:51 -0500767 kfree(async_extent);
768 cond_resched();
769 continue;
770 }
771
772 lock_extent(io_tree, async_extent->start,
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100773 async_extent->start + async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500774
Wang Xiaoguang18513092016-07-25 15:51:40 +0800775 ret = btrfs_reserve_extent(root, async_extent->ram_size,
Chris Mason771ed682008-11-06 22:02:51 -0500776 async_extent->compressed_size,
777 async_extent->compressed_size,
Miao Xiee570fd22014-06-19 10:42:50 +0800778 0, alloc_hint, &ins, 1, 1);
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500779 if (ret) {
Filipe Manana40ae8372014-10-06 22:14:24 +0100780 free_async_extent_pages(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500781
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400782 if (ret == -ENOSPC) {
783 unlock_extent(io_tree, async_extent->start,
784 async_extent->start +
785 async_extent->ram_size - 1);
Liu Boce620032014-07-24 22:48:05 +0800786
787 /*
788 * we need to redirty the pages if we decide to
789 * fallback to uncompressed IO, otherwise we
790 * will not submit these pages down to lower
791 * layers.
792 */
793 extent_range_redirty_for_io(inode,
794 async_extent->start,
795 async_extent->start +
796 async_extent->ram_size - 1);
797
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100798 goto retry;
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400799 }
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500800 goto out_free;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500801 }
Yan, Zhengc2167752009-11-12 09:34:21 +0000802 /*
803 * here we're doing allocation and writeback of the
804 * compressed pages
805 */
Liu Bo6f9994d2017-01-31 07:50:22 -0800806 em = create_io_em(inode, async_extent->start,
807 async_extent->ram_size, /* len */
808 async_extent->start, /* orig_start */
809 ins.objectid, /* block_start */
810 ins.offset, /* block_len */
811 ins.offset, /* orig_block_len */
812 async_extent->ram_size, /* ram_bytes */
813 async_extent->compress_type,
814 BTRFS_ORDERED_COMPRESSED);
815 if (IS_ERR(em))
816 /* ret value is not necessary due to void function */
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500817 goto out_free_reserve;
Liu Bo6f9994d2017-01-31 07:50:22 -0800818 free_extent_map(em);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500819
Li Zefan261507a02010-12-17 14:21:50 +0800820 ret = btrfs_add_ordered_extent_compress(inode,
821 async_extent->start,
822 ins.objectid,
823 async_extent->ram_size,
824 ins.offset,
825 BTRFS_ORDERED_COMPRESSED,
826 async_extent->compress_type);
Filipe Mananad9f85962014-08-25 10:43:00 +0100827 if (ret) {
Nikolay Borisovdcdbc052017-02-20 13:50:45 +0200828 btrfs_drop_extent_cache(BTRFS_I(inode),
829 async_extent->start,
Filipe Mananad9f85962014-08-25 10:43:00 +0100830 async_extent->start +
831 async_extent->ram_size - 1, 0);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500832 goto out_free_reserve;
Filipe Mananad9f85962014-08-25 10:43:00 +0100833 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400834 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Chris Mason771ed682008-11-06 22:02:51 -0500835
Chris Mason771ed682008-11-06 22:02:51 -0500836 /*
837 * clear dirty, set writeback and unlock the pages.
838 */
Josef Bacikc2790a22013-07-29 11:20:47 -0400839 extent_clear_unlock_delalloc(inode, async_extent->start,
Chris Masona791e352009-10-08 11:27:10 -0400840 async_extent->start +
841 async_extent->ram_size - 1,
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800842 async_extent->start +
843 async_extent->ram_size - 1,
Josef Bacik151a41b2013-07-29 13:22:24 -0400844 NULL, EXTENT_LOCKED | EXTENT_DELALLOC,
845 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -0400846 PAGE_SET_WRITEBACK);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200847 if (btrfs_submit_compressed_write(inode,
Chris Masond3977122009-01-05 21:25:51 -0500848 async_extent->start,
849 async_extent->ram_size,
850 ins.objectid,
851 ins.offset, async_extent->pages,
Liu Bof82b7352017-10-23 23:18:16 -0600852 async_extent->nr_pages,
Nikolay Borisovb5326272019-03-12 17:20:25 +0200853 async_chunk->write_flags)) {
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100854 struct page *p = async_extent->pages[0];
855 const u64 start = async_extent->start;
856 const u64 end = start + async_extent->ram_size - 1;
857
858 p->mapping = inode->i_mapping;
Nikolay Borisovc6297322018-11-08 10:18:08 +0200859 btrfs_writepage_endio_finish_ordered(p, start, end, 0);
Nikolay Borisov7087a9d2018-11-01 14:09:48 +0200860
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100861 p->mapping = NULL;
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800862 extent_clear_unlock_delalloc(inode, start, end, end,
863 NULL, 0,
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100864 PAGE_END_WRITEBACK |
865 PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +0100866 free_async_extent_pages(async_extent);
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100867 }
Chris Mason771ed682008-11-06 22:02:51 -0500868 alloc_hint = ins.objectid + ins.offset;
869 kfree(async_extent);
870 cond_resched();
871 }
Filipe Mananadec8f172014-10-06 22:14:26 +0100872 return;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500873out_free_reserve:
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400874 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400875 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100876out_free:
Josef Bacikc2790a22013-07-29 11:20:47 -0400877 extent_clear_unlock_delalloc(inode, async_extent->start,
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500878 async_extent->start +
879 async_extent->ram_size - 1,
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800880 async_extent->start +
881 async_extent->ram_size - 1,
Josef Bacikc2790a22013-07-29 11:20:47 -0400882 NULL, EXTENT_LOCKED | EXTENT_DELALLOC |
Filipe Mananaa7e3b972017-04-03 10:45:46 +0100883 EXTENT_DELALLOC_NEW |
Josef Bacik151a41b2013-07-29 13:22:24 -0400884 EXTENT_DEFRAG | EXTENT_DO_ACCOUNTING,
885 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Filipe Manana704de492014-10-06 22:14:22 +0100886 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK |
887 PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +0100888 free_async_extent_pages(async_extent);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100889 kfree(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500890 goto again;
Chris Mason771ed682008-11-06 22:02:51 -0500891}
892
Josef Bacik4b46fce2010-05-23 11:00:55 -0400893static u64 get_extent_allocation_hint(struct inode *inode, u64 start,
894 u64 num_bytes)
895{
896 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
897 struct extent_map *em;
898 u64 alloc_hint = 0;
899
900 read_lock(&em_tree->lock);
901 em = search_extent_mapping(em_tree, start, num_bytes);
902 if (em) {
903 /*
904 * if block start isn't an actual block number then find the
905 * first block in this inode and use that as a hint. If that
906 * block is also bogus then just don't worry about it.
907 */
908 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
909 free_extent_map(em);
910 em = search_extent_mapping(em_tree, 0, 0);
911 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
912 alloc_hint = em->block_start;
913 if (em)
914 free_extent_map(em);
915 } else {
916 alloc_hint = em->block_start;
917 free_extent_map(em);
918 }
919 }
920 read_unlock(&em_tree->lock);
921
922 return alloc_hint;
923}
924
Chris Mason771ed682008-11-06 22:02:51 -0500925/*
926 * when extent_io.c finds a delayed allocation range in the file,
927 * the call backs end up in this code. The basic idea is to
928 * allocate extents on disk for the range, and create ordered data structs
929 * in ram to track those extents.
930 *
931 * locked_page is the page that writepage had locked already. We use
932 * it to make sure we don't do extra locks or unlocks.
933 *
934 * *page_started is set to one if we unlock locked_page and do everything
935 * required to start IO on it. It may be clean and already done with
936 * IO when we return.
937 */
Josef Bacik00361582013-08-14 14:02:47 -0400938static noinline int cow_file_range(struct inode *inode,
939 struct page *locked_page,
Wang Xiaoguangdda32452016-07-11 11:05:29 +0800940 u64 start, u64 end, u64 delalloc_end,
941 int *page_started, unsigned long *nr_written,
942 int unlock, struct btrfs_dedupe_hash *hash)
Chris Mason771ed682008-11-06 22:02:51 -0500943{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400944 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik00361582013-08-14 14:02:47 -0400945 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason771ed682008-11-06 22:02:51 -0500946 u64 alloc_hint = 0;
947 u64 num_bytes;
948 unsigned long ram_size;
Filipe Mananaa315e682017-03-06 23:04:20 +0000949 u64 cur_alloc_size = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400950 u64 blocksize = fs_info->sectorsize;
Chris Mason771ed682008-11-06 22:02:51 -0500951 struct btrfs_key ins;
952 struct extent_map *em;
Filipe Mananaa315e682017-03-06 23:04:20 +0000953 unsigned clear_bits;
954 unsigned long page_ops;
955 bool extent_reserved = false;
Chris Mason771ed682008-11-06 22:02:51 -0500956 int ret = 0;
957
Nikolay Borisov70ddc552017-02-20 13:50:35 +0200958 if (btrfs_is_free_space_inode(BTRFS_I(inode))) {
Josef Bacik02ecd2c2013-10-25 16:19:08 -0400959 WARN_ON_ONCE(1);
Josef Bacik29bce2f2014-02-07 12:21:23 -0500960 ret = -EINVAL;
961 goto out_unlock;
Josef Bacik02ecd2c2013-10-25 16:19:08 -0400962 }
Chris Mason771ed682008-11-06 22:02:51 -0500963
Qu Wenruofda28322013-02-26 08:10:22 +0000964 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Mason771ed682008-11-06 22:02:51 -0500965 num_bytes = max(blocksize, num_bytes);
Anand Jain566b1762018-02-15 18:07:59 +0800966 ASSERT(num_bytes <= btrfs_super_total_bytes(fs_info->super_copy));
Chris Mason771ed682008-11-06 22:02:51 -0500967
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200968 inode_should_defrag(BTRFS_I(inode), start, end, num_bytes, SZ_64K);
Chris Mason4cb53002011-05-24 15:35:30 -0400969
Chris Mason771ed682008-11-06 22:02:51 -0500970 if (start == 0) {
971 /* lets try to make an inline extent */
Nikolay Borisovd02c0e22018-03-02 09:43:15 +0200972 ret = cow_file_range_inline(inode, start, end, 0,
973 BTRFS_COMPRESS_NONE, NULL);
Chris Mason771ed682008-11-06 22:02:51 -0500974 if (ret == 0) {
Josef Bacik8b62f872017-10-19 14:15:55 -0400975 /*
976 * We use DO_ACCOUNTING here because we need the
977 * delalloc_release_metadata to be run _after_ we drop
978 * our outstanding extent for clearing delalloc for this
979 * range.
980 */
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800981 extent_clear_unlock_delalloc(inode, start, end,
982 delalloc_end, NULL,
Josef Bacikc2790a22013-07-29 11:20:47 -0400983 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik8b62f872017-10-19 14:15:55 -0400984 EXTENT_DELALLOC_NEW | EXTENT_DEFRAG |
985 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -0400986 PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
987 PAGE_END_WRITEBACK);
Chris Mason771ed682008-11-06 22:02:51 -0500988 *nr_written = *nr_written +
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300989 (end - start + PAGE_SIZE) / PAGE_SIZE;
Chris Mason771ed682008-11-06 22:02:51 -0500990 *page_started = 1;
Chris Mason771ed682008-11-06 22:02:51 -0500991 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100992 } else if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100993 goto out_unlock;
Chris Mason771ed682008-11-06 22:02:51 -0500994 }
995 }
Chris Masonc8b97812008-10-29 14:49:59 -0400996
Josef Bacik4b46fce2010-05-23 11:00:55 -0400997 alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +0200998 btrfs_drop_extent_cache(BTRFS_I(inode), start,
999 start + num_bytes - 1, 0);
Chris Mason3b951512008-04-17 11:29:12 -04001000
Anand Jain3752d222018-02-15 12:29:38 +08001001 while (num_bytes > 0) {
1002 cur_alloc_size = num_bytes;
Wang Xiaoguang18513092016-07-25 15:51:40 +08001003 ret = btrfs_reserve_extent(root, cur_alloc_size, cur_alloc_size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001004 fs_info->sectorsize, 0, alloc_hint,
Miao Xiee570fd22014-06-19 10:42:50 +08001005 &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04001006 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001007 goto out_unlock;
Filipe Mananaa315e682017-03-06 23:04:20 +00001008 cur_alloc_size = ins.offset;
1009 extent_reserved = true;
Chris Masond3977122009-01-05 21:25:51 -05001010
Chris Mason771ed682008-11-06 22:02:51 -05001011 ram_size = ins.offset;
Liu Bo6f9994d2017-01-31 07:50:22 -08001012 em = create_io_em(inode, start, ins.offset, /* len */
1013 start, /* orig_start */
1014 ins.objectid, /* block_start */
1015 ins.offset, /* block_len */
1016 ins.offset, /* orig_block_len */
1017 ram_size, /* ram_bytes */
1018 BTRFS_COMPRESS_NONE, /* compress_type */
Liu Bo1af4a0a2017-02-13 15:35:09 -08001019 BTRFS_ORDERED_REGULAR /* type */);
Su Yue090a127a2018-05-30 16:48:56 +08001020 if (IS_ERR(em)) {
1021 ret = PTR_ERR(em);
Liu Boace68ba2013-04-22 10:53:47 +00001022 goto out_reserve;
Su Yue090a127a2018-05-30 16:48:56 +08001023 }
Liu Bo6f9994d2017-01-31 07:50:22 -08001024 free_extent_map(em);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001025
Chris Masone6dcd2d2008-07-17 12:53:50 -04001026 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
Chris Mason771ed682008-11-06 22:02:51 -05001027 ram_size, cur_alloc_size, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001028 if (ret)
Filipe Mananad9f85962014-08-25 10:43:00 +01001029 goto out_drop_extent_cache;
Chris Masonc8b97812008-10-29 14:49:59 -04001030
Yan Zheng17d217f2008-12-12 10:03:38 -05001031 if (root->root_key.objectid ==
1032 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1033 ret = btrfs_reloc_clone_csums(inode, start,
1034 cur_alloc_size);
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001035 /*
1036 * Only drop cache here, and process as normal.
1037 *
1038 * We must not allow extent_clear_unlock_delalloc()
1039 * at out_unlock label to free meta of this ordered
1040 * extent, as its meta should be freed by
1041 * btrfs_finish_ordered_io().
1042 *
1043 * So we must continue until @start is increased to
1044 * skip current ordered extent.
1045 */
Josef Bacik00361582013-08-14 14:02:47 -04001046 if (ret)
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001047 btrfs_drop_extent_cache(BTRFS_I(inode), start,
1048 start + ram_size - 1, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001049 }
1050
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001051 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana9cfa3e32016-04-26 15:39:32 +01001052
Chris Masonc8b97812008-10-29 14:49:59 -04001053 /* we're not doing compressed IO, don't unlock the first
1054 * page (which the caller expects to stay locked), don't
1055 * clear any dirty bits and don't set any writeback bits
Chris Mason8b62b722009-09-02 16:53:46 -04001056 *
1057 * Do set the Private2 bit so we know this page was properly
1058 * setup for writepage
Chris Masonc8b97812008-10-29 14:49:59 -04001059 */
Filipe Mananaa315e682017-03-06 23:04:20 +00001060 page_ops = unlock ? PAGE_UNLOCK : 0;
1061 page_ops |= PAGE_SET_PRIVATE2;
Chris Masona791e352009-10-08 11:27:10 -04001062
Josef Bacikc2790a22013-07-29 11:20:47 -04001063 extent_clear_unlock_delalloc(inode, start,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001064 start + ram_size - 1,
1065 delalloc_end, locked_page,
Josef Bacikc2790a22013-07-29 11:20:47 -04001066 EXTENT_LOCKED | EXTENT_DELALLOC,
Filipe Mananaa315e682017-03-06 23:04:20 +00001067 page_ops);
Anand Jain3752d222018-02-15 12:29:38 +08001068 if (num_bytes < cur_alloc_size)
1069 num_bytes = 0;
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001070 else
Anand Jain3752d222018-02-15 12:29:38 +08001071 num_bytes -= cur_alloc_size;
Chris Masonc59f8952007-12-17 20:14:04 -05001072 alloc_hint = ins.objectid + ins.offset;
1073 start += cur_alloc_size;
Filipe Mananaa315e682017-03-06 23:04:20 +00001074 extent_reserved = false;
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001075
1076 /*
1077 * btrfs_reloc_clone_csums() error, since start is increased
1078 * extent_clear_unlock_delalloc() at out_unlock label won't
1079 * free metadata of current ordered extent, we're OK to exit.
1080 */
1081 if (ret)
1082 goto out_unlock;
Chris Masonb888db22007-08-27 16:49:44 -04001083 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001084out:
Chris Masonbe20aa92007-12-17 20:14:01 -05001085 return ret;
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001086
Filipe Mananad9f85962014-08-25 10:43:00 +01001087out_drop_extent_cache:
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02001088 btrfs_drop_extent_cache(BTRFS_I(inode), start, start + ram_size - 1, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001089out_reserve:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001090 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001091 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001092out_unlock:
Filipe Mananaa7e3b972017-04-03 10:45:46 +01001093 clear_bits = EXTENT_LOCKED | EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
1094 EXTENT_DEFRAG | EXTENT_CLEAR_META_RESV;
Filipe Mananaa315e682017-03-06 23:04:20 +00001095 page_ops = PAGE_UNLOCK | PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
1096 PAGE_END_WRITEBACK;
1097 /*
1098 * If we reserved an extent for our delalloc range (or a subrange) and
1099 * failed to create the respective ordered extent, then it means that
1100 * when we reserved the extent we decremented the extent's size from
1101 * the data space_info's bytes_may_use counter and incremented the
1102 * space_info's bytes_reserved counter by the same amount. We must make
1103 * sure extent_clear_unlock_delalloc() does not try to decrement again
1104 * the data space_info's bytes_may_use counter, therefore we do not pass
1105 * it the flag EXTENT_CLEAR_DATA_RESV.
1106 */
1107 if (extent_reserved) {
1108 extent_clear_unlock_delalloc(inode, start,
1109 start + cur_alloc_size,
1110 start + cur_alloc_size,
1111 locked_page,
1112 clear_bits,
1113 page_ops);
1114 start += cur_alloc_size;
1115 if (start >= end)
1116 goto out;
1117 }
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001118 extent_clear_unlock_delalloc(inode, start, end, delalloc_end,
1119 locked_page,
Filipe Mananaa315e682017-03-06 23:04:20 +00001120 clear_bits | EXTENT_CLEAR_DATA_RESV,
1121 page_ops);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001122 goto out;
Chris Mason771ed682008-11-06 22:02:51 -05001123}
Chris Masonc8b97812008-10-29 14:49:59 -04001124
Chris Mason771ed682008-11-06 22:02:51 -05001125/*
1126 * work queue call back to started compression on a file and pages
1127 */
1128static noinline void async_cow_start(struct btrfs_work *work)
1129{
Nikolay Borisovb5326272019-03-12 17:20:25 +02001130 struct async_chunk *async_chunk;
Chris Mason771ed682008-11-06 22:02:51 -05001131 int num_added = 0;
Nikolay Borisov97db1202019-03-12 17:20:24 +02001132
Nikolay Borisovb5326272019-03-12 17:20:25 +02001133 async_chunk = container_of(work, struct async_chunk, work);
Chris Mason771ed682008-11-06 22:02:51 -05001134
Nikolay Borisov1368c6d2019-03-12 17:20:27 +02001135 compress_file_range(async_chunk, &num_added);
Josef Bacik8180ef82012-06-08 15:16:12 -04001136 if (num_added == 0) {
Nikolay Borisovb5326272019-03-12 17:20:25 +02001137 btrfs_add_delayed_iput(async_chunk->inode);
1138 async_chunk->inode = NULL;
Josef Bacik8180ef82012-06-08 15:16:12 -04001139 }
Chris Mason771ed682008-11-06 22:02:51 -05001140}
1141
1142/*
1143 * work queue call back to submit previously compressed pages
1144 */
1145static noinline void async_cow_submit(struct btrfs_work *work)
1146{
Nikolay Borisovc5a68ae2019-03-12 17:20:26 +02001147 struct async_chunk *async_chunk = container_of(work, struct async_chunk,
1148 work);
1149 struct btrfs_fs_info *fs_info = btrfs_work_owner(work);
Chris Mason771ed682008-11-06 22:02:51 -05001150 unsigned long nr_pages;
1151
Nikolay Borisovb5326272019-03-12 17:20:25 +02001152 nr_pages = (async_chunk->end - async_chunk->start + PAGE_SIZE) >>
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001153 PAGE_SHIFT;
Chris Mason771ed682008-11-06 22:02:51 -05001154
David Sterba093258e2018-02-26 16:15:17 +01001155 /* atomic_sub_return implies a barrier */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001156 if (atomic_sub_return(nr_pages, &fs_info->async_delalloc_pages) <
David Sterba093258e2018-02-26 16:15:17 +01001157 5 * SZ_1M)
1158 cond_wake_up_nomb(&fs_info->async_submit_wait);
Chris Mason771ed682008-11-06 22:02:51 -05001159
Nikolay Borisov4546d172019-01-03 10:50:03 +02001160 /*
Nikolay Borisovb5326272019-03-12 17:20:25 +02001161 * ->inode could be NULL if async_chunk_start has failed to compress,
Nikolay Borisov4546d172019-01-03 10:50:03 +02001162 * in which case we don't have anything to submit, yet we need to
1163 * always adjust ->async_delalloc_pages as its paired with the init
1164 * happening in cow_file_range_async
1165 */
Nikolay Borisovb5326272019-03-12 17:20:25 +02001166 if (async_chunk->inode)
1167 submit_compressed_extents(async_chunk);
Chris Mason771ed682008-11-06 22:02:51 -05001168}
Chris Masonc8b97812008-10-29 14:49:59 -04001169
Chris Mason771ed682008-11-06 22:02:51 -05001170static noinline void async_cow_free(struct btrfs_work *work)
1171{
Nikolay Borisovb5326272019-03-12 17:20:25 +02001172 struct async_chunk *async_chunk;
Nikolay Borisov97db1202019-03-12 17:20:24 +02001173
Nikolay Borisovb5326272019-03-12 17:20:25 +02001174 async_chunk = container_of(work, struct async_chunk, work);
1175 if (async_chunk->inode)
1176 btrfs_add_delayed_iput(async_chunk->inode);
Nikolay Borisov97db1202019-03-12 17:20:24 +02001177 /*
1178 * Since the pointer to 'pending' is at the beginning of the array of
Nikolay Borisovb5326272019-03-12 17:20:25 +02001179 * async_chunk's, freeing it ensures the whole array has been freed.
Nikolay Borisov97db1202019-03-12 17:20:24 +02001180 */
Nikolay Borisovb5326272019-03-12 17:20:25 +02001181 if (atomic_dec_and_test(async_chunk->pending))
1182 kfree(async_chunk->pending);
Chris Mason771ed682008-11-06 22:02:51 -05001183}
1184
1185static int cow_file_range_async(struct inode *inode, struct page *locked_page,
1186 u64 start, u64 end, int *page_started,
Liu Bof82b7352017-10-23 23:18:16 -06001187 unsigned long *nr_written,
1188 unsigned int write_flags)
Chris Mason771ed682008-11-06 22:02:51 -05001189{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001190 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Nikolay Borisov97db1202019-03-12 17:20:24 +02001191 struct async_cow *ctx;
1192 struct async_chunk *async_chunk;
Chris Mason771ed682008-11-06 22:02:51 -05001193 unsigned long nr_pages;
1194 u64 cur_end;
Nikolay Borisov97db1202019-03-12 17:20:24 +02001195 u64 num_chunks = DIV_ROUND_UP(end - start, SZ_512K);
1196 int i;
1197 bool should_compress;
Chris Mason771ed682008-11-06 22:02:51 -05001198
Nikolay Borisov69684c52019-03-12 17:20:28 +02001199 unlock_extent(&BTRFS_I(inode)->io_tree, start, end);
Nikolay Borisov97db1202019-03-12 17:20:24 +02001200
1201 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS &&
1202 !btrfs_test_opt(fs_info, FORCE_COMPRESS)) {
1203 num_chunks = 1;
1204 should_compress = false;
1205 } else {
1206 should_compress = true;
1207 }
1208
1209 ctx = kmalloc(struct_size(ctx, chunks, num_chunks), GFP_NOFS);
1210 if (!ctx) {
1211 unsigned clear_bits = EXTENT_LOCKED | EXTENT_DELALLOC |
1212 EXTENT_DELALLOC_NEW | EXTENT_DEFRAG |
1213 EXTENT_DO_ACCOUNTING;
1214 unsigned long page_ops = PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
1215 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK |
1216 PAGE_SET_ERROR;
1217
1218 extent_clear_unlock_delalloc(inode, start, end, 0, locked_page,
1219 clear_bits, page_ops);
1220 return -ENOMEM;
1221 }
1222
1223 async_chunk = ctx->chunks;
1224 atomic_set(&ctx->num_chunks, num_chunks);
1225
1226 for (i = 0; i < num_chunks; i++) {
1227 if (should_compress)
1228 cur_end = min(end, start + SZ_512K - 1);
1229 else
1230 cur_end = end;
1231
Nikolay Borisovbd4691a2019-01-03 10:50:01 +02001232 /*
1233 * igrab is called higher up in the call chain, take only the
1234 * lightweight reference for the callback lifetime
1235 */
1236 ihold(inode);
Nikolay Borisov97db1202019-03-12 17:20:24 +02001237 async_chunk[i].pending = &ctx->num_chunks;
1238 async_chunk[i].inode = inode;
1239 async_chunk[i].start = start;
1240 async_chunk[i].end = cur_end;
Nikolay Borisov97db1202019-03-12 17:20:24 +02001241 async_chunk[i].locked_page = locked_page;
1242 async_chunk[i].write_flags = write_flags;
1243 INIT_LIST_HEAD(&async_chunk[i].extents);
Chris Mason771ed682008-11-06 22:02:51 -05001244
Nikolay Borisov97db1202019-03-12 17:20:24 +02001245 btrfs_init_work(&async_chunk[i].work,
Liu Bo9e0af232014-08-15 23:36:53 +08001246 btrfs_delalloc_helper,
1247 async_cow_start, async_cow_submit,
1248 async_cow_free);
Chris Mason771ed682008-11-06 22:02:51 -05001249
Nikolay Borisov97db1202019-03-12 17:20:24 +02001250 nr_pages = DIV_ROUND_UP(cur_end - start, PAGE_SIZE);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001251 atomic_add(nr_pages, &fs_info->async_delalloc_pages);
Chris Mason771ed682008-11-06 22:02:51 -05001252
Nikolay Borisov97db1202019-03-12 17:20:24 +02001253 btrfs_queue_work(fs_info->delalloc_workers, &async_chunk[i].work);
Chris Mason771ed682008-11-06 22:02:51 -05001254
Chris Mason771ed682008-11-06 22:02:51 -05001255 *nr_written += nr_pages;
1256 start = cur_end + 1;
1257 }
1258 *page_started = 1;
1259 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001260}
1261
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001262static noinline int csum_exist_in_range(struct btrfs_fs_info *fs_info,
Yan Zheng17d217f2008-12-12 10:03:38 -05001263 u64 bytenr, u64 num_bytes)
1264{
1265 int ret;
1266 struct btrfs_ordered_sum *sums;
1267 LIST_HEAD(list);
1268
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001269 ret = btrfs_lookup_csums_range(fs_info->csum_root, bytenr,
Arne Jansena2de7332011-03-08 14:14:00 +01001270 bytenr + num_bytes - 1, &list, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001271 if (ret == 0 && list_empty(&list))
1272 return 0;
1273
1274 while (!list_empty(&list)) {
1275 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1276 list_del(&sums->list);
1277 kfree(sums);
1278 }
Liu Bo58113752018-01-31 17:09:13 -07001279 if (ret < 0)
1280 return ret;
Yan Zheng17d217f2008-12-12 10:03:38 -05001281 return 1;
1282}
1283
Chris Masond352ac62008-09-29 15:18:18 -04001284/*
1285 * when nowcow writeback call back. This checks for snapshots or COW copies
1286 * of the extents that exist in the file, and COWs the file as required.
1287 *
1288 * If no cow copies or snapshots exist, we write directly to the existing
1289 * blocks on disk
1290 */
Chris Mason7f366cf2009-03-12 20:12:45 -04001291static noinline int run_delalloc_nocow(struct inode *inode,
1292 struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001293 u64 start, u64 end, int *page_started, int force,
1294 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001295{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001296 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masonbe20aa92007-12-17 20:14:01 -05001297 struct btrfs_root *root = BTRFS_I(inode)->root;
1298 struct extent_buffer *leaf;
Chris Masonbe20aa92007-12-17 20:14:01 -05001299 struct btrfs_path *path;
Yan Zheng80ff3852008-10-30 14:20:02 -04001300 struct btrfs_file_extent_item *fi;
Chris Masonbe20aa92007-12-17 20:14:01 -05001301 struct btrfs_key found_key;
Liu Bo6f9994d2017-01-31 07:50:22 -08001302 struct extent_map *em;
Yan Zheng80ff3852008-10-30 14:20:02 -04001303 u64 cow_start;
1304 u64 cur_offset;
1305 u64 extent_end;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001306 u64 extent_offset;
Yan Zheng80ff3852008-10-30 14:20:02 -04001307 u64 disk_bytenr;
1308 u64 num_bytes;
Josef Bacikb4939682012-12-03 10:31:19 -05001309 u64 disk_num_bytes;
Josef Bacikcc95bef2013-04-04 14:31:27 -04001310 u64 ram_bytes;
Yan Zheng80ff3852008-10-30 14:20:02 -04001311 int extent_type;
Robbie Ko8ecebf4d2018-08-06 10:30:30 +08001312 int ret;
Yan Zhengd899e052008-10-30 14:25:28 -04001313 int type;
Yan Zheng80ff3852008-10-30 14:20:02 -04001314 int nocow;
1315 int check_prev = 1;
Li Zefan82d59022011-04-20 10:33:24 +08001316 bool nolock;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02001317 u64 ino = btrfs_ino(BTRFS_I(inode));
Chris Masonbe20aa92007-12-17 20:14:01 -05001318
1319 path = btrfs_alloc_path();
Josef Bacik17ca04a2012-05-31 15:58:55 -04001320 if (!path) {
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001321 extent_clear_unlock_delalloc(inode, start, end, end,
1322 locked_page,
Josef Bacikc2790a22013-07-29 11:20:47 -04001323 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -04001324 EXTENT_DO_ACCOUNTING |
1325 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001326 PAGE_CLEAR_DIRTY |
1327 PAGE_SET_WRITEBACK |
1328 PAGE_END_WRITEBACK);
Mark Fashehd8926bb2011-07-13 10:38:47 -07001329 return -ENOMEM;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001330 }
Li Zefan82d59022011-04-20 10:33:24 +08001331
Nikolay Borisov70ddc552017-02-20 13:50:35 +02001332 nolock = btrfs_is_free_space_inode(BTRFS_I(inode));
Li Zefan82d59022011-04-20 10:33:24 +08001333
Yan Zheng80ff3852008-10-30 14:20:02 -04001334 cow_start = (u64)-1;
1335 cur_offset = start;
1336 while (1) {
Liu Boe4c3b2d2017-01-30 12:25:28 -08001337 ret = btrfs_lookup_file_extent(NULL, root, path, ino,
Yan Zheng80ff3852008-10-30 14:20:02 -04001338 cur_offset, 0);
Josef Bacikd788a342013-10-25 16:55:08 -04001339 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001340 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001341 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1342 leaf = path->nodes[0];
1343 btrfs_item_key_to_cpu(leaf, &found_key,
1344 path->slots[0] - 1);
Li Zefan33345d012011-04-20 10:31:50 +08001345 if (found_key.objectid == ino &&
Yan Zheng80ff3852008-10-30 14:20:02 -04001346 found_key.type == BTRFS_EXTENT_DATA_KEY)
1347 path->slots[0]--;
1348 }
1349 check_prev = 0;
1350next_slot:
1351 leaf = path->nodes[0];
1352 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1353 ret = btrfs_next_leaf(root, path);
Liu Boe8916692018-01-25 11:02:50 -07001354 if (ret < 0) {
1355 if (cow_start != (u64)-1)
1356 cur_offset = cow_start;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001357 goto error;
Liu Boe8916692018-01-25 11:02:50 -07001358 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001359 if (ret > 0)
1360 break;
1361 leaf = path->nodes[0];
1362 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001363
Yan Zheng80ff3852008-10-30 14:20:02 -04001364 nocow = 0;
1365 disk_bytenr = 0;
Yan Zheng17d217f2008-12-12 10:03:38 -05001366 num_bytes = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001367 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001368
Filipe Manana1d512cb2015-11-09 00:33:58 +00001369 if (found_key.objectid > ino)
1370 break;
1371 if (WARN_ON_ONCE(found_key.objectid < ino) ||
1372 found_key.type < BTRFS_EXTENT_DATA_KEY) {
1373 path->slots[0]++;
1374 goto next_slot;
1375 }
1376 if (found_key.type > BTRFS_EXTENT_DATA_KEY ||
Yan Zheng80ff3852008-10-30 14:20:02 -04001377 found_key.offset > end)
1378 break;
Chris Masonbe20aa92007-12-17 20:14:01 -05001379
Yan Zheng80ff3852008-10-30 14:20:02 -04001380 if (found_key.offset > cur_offset) {
1381 extent_end = found_key.offset;
Chris Masone9061e22009-10-09 09:57:45 -04001382 extent_type = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001383 goto out_check;
1384 }
Chris Masonc31f8832008-01-08 15:46:31 -05001385
Yan Zheng80ff3852008-10-30 14:20:02 -04001386 fi = btrfs_item_ptr(leaf, path->slots[0],
1387 struct btrfs_file_extent_item);
1388 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001389
Josef Bacikcc95bef2013-04-04 14:31:27 -04001390 ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
Yan Zhengd899e052008-10-30 14:25:28 -04001391 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1392 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001393 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001394 extent_offset = btrfs_file_extent_offset(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001395 extent_end = found_key.offset +
1396 btrfs_file_extent_num_bytes(leaf, fi);
Josef Bacikb4939682012-12-03 10:31:19 -05001397 disk_num_bytes =
1398 btrfs_file_extent_disk_num_bytes(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001399 if (extent_end <= start) {
1400 path->slots[0]++;
1401 goto next_slot;
1402 }
Yan Zheng17d217f2008-12-12 10:03:38 -05001403 if (disk_bytenr == 0)
1404 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001405 if (btrfs_file_extent_compression(leaf, fi) ||
1406 btrfs_file_extent_encryption(leaf, fi) ||
1407 btrfs_file_extent_other_encoding(leaf, fi))
1408 goto out_check;
Ethan Lien78d42952018-05-17 14:58:29 +08001409 /*
1410 * Do the same check as in btrfs_cross_ref_exist but
1411 * without the unnecessary search.
1412 */
Lu Fengqi27a7ff52018-11-29 17:31:32 +08001413 if (!nolock &&
1414 btrfs_file_extent_generation(leaf, fi) <=
Ethan Lien78d42952018-05-17 14:58:29 +08001415 btrfs_root_last_snapshot(&root->root_item))
1416 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001417 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1418 goto out_check;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001419 if (btrfs_extent_readonly(fs_info, disk_bytenr))
Yan Zheng80ff3852008-10-30 14:20:02 -04001420 goto out_check;
Liu Bo58113752018-01-31 17:09:13 -07001421 ret = btrfs_cross_ref_exist(root, ino,
1422 found_key.offset -
1423 extent_offset, disk_bytenr);
1424 if (ret) {
1425 /*
1426 * ret could be -EIO if the above fails to read
1427 * metadata.
1428 */
1429 if (ret < 0) {
1430 if (cow_start != (u64)-1)
1431 cur_offset = cow_start;
1432 goto error;
1433 }
1434
1435 WARN_ON_ONCE(nolock);
Yan Zheng17d217f2008-12-12 10:03:38 -05001436 goto out_check;
Liu Bo58113752018-01-31 17:09:13 -07001437 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001438 disk_bytenr += extent_offset;
Yan Zheng17d217f2008-12-12 10:03:38 -05001439 disk_bytenr += cur_offset - found_key.offset;
1440 num_bytes = min(end + 1, extent_end) - cur_offset;
1441 /*
Wang Shilonge9894fd2014-03-27 11:12:25 +08001442 * if there are pending snapshots for this root,
1443 * we fall into common COW way.
1444 */
Robbie Ko8ecebf4d2018-08-06 10:30:30 +08001445 if (!nolock && atomic_read(&root->snapshot_force_cow))
1446 goto out_check;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001447 /*
Yan Zheng17d217f2008-12-12 10:03:38 -05001448 * force cow if csum exists in the range.
1449 * this ensure that csum for a given extent are
1450 * either valid or do not exist.
1451 */
Liu Bo58113752018-01-31 17:09:13 -07001452 ret = csum_exist_in_range(fs_info, disk_bytenr,
1453 num_bytes);
1454 if (ret) {
Liu Bo58113752018-01-31 17:09:13 -07001455 /*
1456 * ret could be -EIO if the above fails to read
1457 * metadata.
1458 */
1459 if (ret < 0) {
1460 if (cow_start != (u64)-1)
1461 cur_offset = cow_start;
1462 goto error;
1463 }
1464 WARN_ON_ONCE(nolock);
Yan Zheng17d217f2008-12-12 10:03:38 -05001465 goto out_check;
Robbie Ko91e1f562016-10-07 10:01:29 +08001466 }
Robbie Ko8ecebf4d2018-08-06 10:30:30 +08001467 if (!btrfs_inc_nocow_writers(fs_info, disk_bytenr))
Filipe Mananaf78c4362016-05-09 13:15:41 +01001468 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001469 nocow = 1;
1470 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1471 extent_end = found_key.offset +
Qu Wenruoe41ca582018-06-06 15:41:49 +08001472 btrfs_file_extent_ram_bytes(leaf, fi);
Jeff Mahoneyda170662016-06-15 09:22:56 -04001473 extent_end = ALIGN(extent_end,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001474 fs_info->sectorsize);
Yan Zheng80ff3852008-10-30 14:20:02 -04001475 } else {
Arnd Bergmann290342f2019-03-25 14:02:25 +01001476 BUG();
Yan Zheng80ff3852008-10-30 14:20:02 -04001477 }
1478out_check:
1479 if (extent_end <= start) {
1480 path->slots[0]++;
Filipe Mananaf78c4362016-05-09 13:15:41 +01001481 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001482 btrfs_dec_nocow_writers(fs_info, disk_bytenr);
Yan Zheng80ff3852008-10-30 14:20:02 -04001483 goto next_slot;
1484 }
1485 if (!nocow) {
1486 if (cow_start == (u64)-1)
1487 cow_start = cur_offset;
1488 cur_offset = extent_end;
1489 if (cur_offset > end)
1490 break;
1491 path->slots[0]++;
1492 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001493 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001494
David Sterbab3b4aa72011-04-21 01:20:15 +02001495 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001496 if (cow_start != (u64)-1) {
Josef Bacik00361582013-08-14 14:02:47 -04001497 ret = cow_file_range(inode, locked_page,
1498 cow_start, found_key.offset - 1,
Wang Xiaoguangdda32452016-07-11 11:05:29 +08001499 end, page_started, nr_written, 1,
1500 NULL);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001501 if (ret) {
Filipe Mananaf78c4362016-05-09 13:15:41 +01001502 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001503 btrfs_dec_nocow_writers(fs_info,
Filipe Mananaf78c4362016-05-09 13:15:41 +01001504 disk_bytenr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001505 goto error;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001506 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001507 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001508 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001509
Yan Zhengd899e052008-10-30 14:25:28 -04001510 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Liu Bo6f9994d2017-01-31 07:50:22 -08001511 u64 orig_start = found_key.offset - extent_offset;
1512
1513 em = create_io_em(inode, cur_offset, num_bytes,
1514 orig_start,
1515 disk_bytenr, /* block_start */
1516 num_bytes, /* block_len */
1517 disk_num_bytes, /* orig_block_len */
1518 ram_bytes, BTRFS_COMPRESS_NONE,
1519 BTRFS_ORDERED_PREALLOC);
1520 if (IS_ERR(em)) {
Liu Bo6f9994d2017-01-31 07:50:22 -08001521 if (nocow)
1522 btrfs_dec_nocow_writers(fs_info,
1523 disk_bytenr);
1524 ret = PTR_ERR(em);
1525 goto error;
Yan Zhengd899e052008-10-30 14:25:28 -04001526 }
Liu Bo6f9994d2017-01-31 07:50:22 -08001527 free_extent_map(em);
1528 }
1529
1530 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zhengd899e052008-10-30 14:25:28 -04001531 type = BTRFS_ORDERED_PREALLOC;
1532 } else {
1533 type = BTRFS_ORDERED_NOCOW;
1534 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001535
1536 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
Yan Zhengd899e052008-10-30 14:25:28 -04001537 num_bytes, num_bytes, type);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001538 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001539 btrfs_dec_nocow_writers(fs_info, disk_bytenr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001540 BUG_ON(ret); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -05001541
Yan, Zhengefa56462010-05-16 10:49:59 -04001542 if (root->root_key.objectid ==
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001543 BTRFS_DATA_RELOC_TREE_OBJECTID)
1544 /*
1545 * Error handled later, as we must prevent
1546 * extent_clear_unlock_delalloc() in error handler
1547 * from freeing metadata of created ordered extent.
1548 */
Yan, Zhengefa56462010-05-16 10:49:59 -04001549 ret = btrfs_reloc_clone_csums(inode, cur_offset,
1550 num_bytes);
Yan, Zhengefa56462010-05-16 10:49:59 -04001551
Josef Bacikc2790a22013-07-29 11:20:47 -04001552 extent_clear_unlock_delalloc(inode, cur_offset,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001553 cur_offset + num_bytes - 1, end,
Josef Bacikc2790a22013-07-29 11:20:47 -04001554 locked_page, EXTENT_LOCKED |
Wang Xiaoguang18513092016-07-25 15:51:40 +08001555 EXTENT_DELALLOC |
1556 EXTENT_CLEAR_DATA_RESV,
1557 PAGE_UNLOCK | PAGE_SET_PRIVATE2);
1558
Yan Zheng80ff3852008-10-30 14:20:02 -04001559 cur_offset = extent_end;
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001560
1561 /*
1562 * btrfs_reloc_clone_csums() error, now we're OK to call error
1563 * handler, as metadata for created ordered extent will only
1564 * be freed by btrfs_finish_ordered_io().
1565 */
1566 if (ret)
1567 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001568 if (cur_offset > end)
1569 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001570 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001571 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001572
Robbie Ko506481b2018-10-30 18:04:04 +08001573 if (cur_offset <= end && cow_start == (u64)-1)
Yan Zheng80ff3852008-10-30 14:20:02 -04001574 cow_start = cur_offset;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001575
Yan Zheng80ff3852008-10-30 14:20:02 -04001576 if (cow_start != (u64)-1) {
Robbie Ko506481b2018-10-30 18:04:04 +08001577 cur_offset = end;
Wang Xiaoguangdda32452016-07-11 11:05:29 +08001578 ret = cow_file_range(inode, locked_page, cow_start, end, end,
1579 page_started, nr_written, 1, NULL);
Josef Bacikd788a342013-10-25 16:55:08 -04001580 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001581 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001582 }
1583
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001584error:
Josef Bacik17ca04a2012-05-31 15:58:55 -04001585 if (ret && cur_offset < end)
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001586 extent_clear_unlock_delalloc(inode, cur_offset, end, end,
Josef Bacikc2790a22013-07-29 11:20:47 -04001587 locked_page, EXTENT_LOCKED |
Josef Bacik151a41b2013-07-29 13:22:24 -04001588 EXTENT_DELALLOC | EXTENT_DEFRAG |
1589 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
1590 PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -04001591 PAGE_SET_WRITEBACK |
1592 PAGE_END_WRITEBACK);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001593 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001594 return ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001595}
1596
Wang Shilong47059d92014-07-03 18:22:07 +08001597static inline int need_force_cow(struct inode *inode, u64 start, u64 end)
1598{
1599
1600 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
1601 !(BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC))
1602 return 0;
1603
1604 /*
1605 * @defrag_bytes is a hint value, no spinlock held here,
1606 * if is not zero, it means the file is defragging.
1607 * Force cow if given extent needs to be defragged.
1608 */
1609 if (BTRFS_I(inode)->defrag_bytes &&
1610 test_range_bit(&BTRFS_I(inode)->io_tree, start, end,
1611 EXTENT_DEFRAG, 0, NULL))
1612 return 1;
1613
1614 return 0;
1615}
1616
Chris Masond352ac62008-09-29 15:18:18 -04001617/*
Nikolay Borisov5eaad972018-11-01 14:09:46 +02001618 * Function to process delayed allocation (create CoW) for ranges which are
1619 * being touched for the first time.
Chris Masond352ac62008-09-29 15:18:18 -04001620 */
Nikolay Borisovbc9a8bf2018-12-19 09:50:20 +02001621int btrfs_run_delalloc_range(struct inode *inode, struct page *locked_page,
Nikolay Borisov5eaad972018-11-01 14:09:46 +02001622 u64 start, u64 end, int *page_started, unsigned long *nr_written,
1623 struct writeback_control *wbc)
Chris Masonbe20aa92007-12-17 20:14:01 -05001624{
Chris Masonbe20aa92007-12-17 20:14:01 -05001625 int ret;
Wang Shilong47059d92014-07-03 18:22:07 +08001626 int force_cow = need_force_cow(inode, start, end);
Liu Bof82b7352017-10-23 23:18:16 -06001627 unsigned int write_flags = wbc_to_write_flags(wbc);
Chris Masona2135012008-06-25 16:01:30 -04001628
Wang Shilong47059d92014-07-03 18:22:07 +08001629 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW && !force_cow) {
Chris Masonc8b97812008-10-29 14:49:59 -04001630 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001631 page_started, 1, nr_written);
Wang Shilong47059d92014-07-03 18:22:07 +08001632 } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC && !force_cow) {
Yan Zhengd899e052008-10-30 14:25:28 -04001633 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001634 page_started, 0, nr_written);
Timofey Titovetsc2fcdcd2017-07-17 16:52:58 +03001635 } else if (!inode_need_compress(inode, start, end)) {
Wang Xiaoguangdda32452016-07-11 11:05:29 +08001636 ret = cow_file_range(inode, locked_page, start, end, end,
1637 page_started, nr_written, 1, NULL);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001638 } else {
1639 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1640 &BTRFS_I(inode)->runtime_flags);
Chris Mason771ed682008-11-06 22:02:51 -05001641 ret = cow_file_range_async(inode, locked_page, start, end,
Liu Bof82b7352017-10-23 23:18:16 -06001642 page_started, nr_written,
1643 write_flags);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001644 }
Qu Wenruo524272602017-03-08 10:25:52 +08001645 if (ret)
Nikolay Borisovd1051d62018-11-21 17:10:52 +02001646 btrfs_cleanup_ordered_extents(inode, locked_page, start,
1647 end - start + 1);
Chris Masonb888db22007-08-27 16:49:44 -04001648 return ret;
1649}
1650
Nikolay Borisovabbb55f2018-11-01 14:09:53 +02001651void btrfs_split_delalloc_extent(struct inode *inode,
1652 struct extent_state *orig, u64 split)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001653{
Josef Bacikdcab6a32015-02-11 15:08:59 -05001654 u64 size;
1655
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001656 /* not delalloc, ignore it */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001657 if (!(orig->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001658 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001659
Josef Bacikdcab6a32015-02-11 15:08:59 -05001660 size = orig->end - orig->start + 1;
1661 if (size > BTRFS_MAX_EXTENT_SIZE) {
David Sterba823bb202017-01-04 11:09:51 +01001662 u32 num_extents;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001663 u64 new_size;
1664
1665 /*
Nikolay Borisov5c848192018-11-01 14:09:52 +02001666 * See the explanation in btrfs_merge_delalloc_extent, the same
Josef Bacikba117212015-03-13 15:01:24 -04001667 * applies here, just in reverse.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001668 */
1669 new_size = orig->end - split + 1;
David Sterba823bb202017-01-04 11:09:51 +01001670 num_extents = count_max_extents(new_size);
Josef Bacikba117212015-03-13 15:01:24 -04001671 new_size = split - orig->start;
David Sterba823bb202017-01-04 11:09:51 +01001672 num_extents += count_max_extents(new_size);
1673 if (count_max_extents(size) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001674 return;
1675 }
1676
Josef Bacik9e0baf62011-07-15 15:16:44 +00001677 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04001678 btrfs_mod_outstanding_extents(BTRFS_I(inode), 1);
Josef Bacik9e0baf62011-07-15 15:16:44 +00001679 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001680}
1681
1682/*
Nikolay Borisov5c848192018-11-01 14:09:52 +02001683 * Handle merged delayed allocation extents so we can keep track of new extents
1684 * that are just merged onto old extents, such as when we are doing sequential
1685 * writes, so we can properly account for the metadata space we'll need.
Josef Bacik9ed74f22009-09-11 16:12:44 -04001686 */
Nikolay Borisov5c848192018-11-01 14:09:52 +02001687void btrfs_merge_delalloc_extent(struct inode *inode, struct extent_state *new,
1688 struct extent_state *other)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001689{
Josef Bacikdcab6a32015-02-11 15:08:59 -05001690 u64 new_size, old_size;
David Sterba823bb202017-01-04 11:09:51 +01001691 u32 num_extents;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001692
Josef Bacik9ed74f22009-09-11 16:12:44 -04001693 /* not delalloc, ignore it */
1694 if (!(other->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001695 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001696
Josef Bacik8461a3d2015-03-13 15:12:08 -04001697 if (new->start > other->start)
1698 new_size = new->end - other->start + 1;
1699 else
1700 new_size = other->end - new->start + 1;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001701
1702 /* we're not bigger than the max, unreserve the space and go */
1703 if (new_size <= BTRFS_MAX_EXTENT_SIZE) {
1704 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04001705 btrfs_mod_outstanding_extents(BTRFS_I(inode), -1);
Josef Bacikdcab6a32015-02-11 15:08:59 -05001706 spin_unlock(&BTRFS_I(inode)->lock);
1707 return;
1708 }
1709
1710 /*
Josef Bacikba117212015-03-13 15:01:24 -04001711 * We have to add up either side to figure out how many extents were
1712 * accounted for before we merged into one big extent. If the number of
1713 * extents we accounted for is <= the amount we need for the new range
1714 * then we can return, otherwise drop. Think of it like this
1715 *
1716 * [ 4k][MAX_SIZE]
1717 *
1718 * So we've grown the extent by a MAX_SIZE extent, this would mean we
1719 * need 2 outstanding extents, on one side we have 1 and the other side
1720 * we have 1 so they are == and we can return. But in this case
1721 *
1722 * [MAX_SIZE+4k][MAX_SIZE+4k]
1723 *
1724 * Each range on their own accounts for 2 extents, but merged together
1725 * they are only 3 extents worth of accounting, so we need to drop in
1726 * this case.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001727 */
Josef Bacikba117212015-03-13 15:01:24 -04001728 old_size = other->end - other->start + 1;
David Sterba823bb202017-01-04 11:09:51 +01001729 num_extents = count_max_extents(old_size);
Josef Bacikba117212015-03-13 15:01:24 -04001730 old_size = new->end - new->start + 1;
David Sterba823bb202017-01-04 11:09:51 +01001731 num_extents += count_max_extents(old_size);
1732 if (count_max_extents(new_size) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001733 return;
1734
Josef Bacik9e0baf62011-07-15 15:16:44 +00001735 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04001736 btrfs_mod_outstanding_extents(BTRFS_I(inode), -1);
Josef Bacik9e0baf62011-07-15 15:16:44 +00001737 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001738}
1739
Miao Xieeb73c1b2013-05-15 07:48:22 +00001740static void btrfs_add_delalloc_inodes(struct btrfs_root *root,
1741 struct inode *inode)
1742{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001743 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1744
Miao Xieeb73c1b2013-05-15 07:48:22 +00001745 spin_lock(&root->delalloc_lock);
1746 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1747 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1748 &root->delalloc_inodes);
1749 set_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1750 &BTRFS_I(inode)->runtime_flags);
1751 root->nr_delalloc_inodes++;
1752 if (root->nr_delalloc_inodes == 1) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001753 spin_lock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001754 BUG_ON(!list_empty(&root->delalloc_root));
1755 list_add_tail(&root->delalloc_root,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001756 &fs_info->delalloc_roots);
1757 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001758 }
1759 }
1760 spin_unlock(&root->delalloc_lock);
1761}
1762
Nikolay Borisov2b877332018-04-27 12:21:51 +03001763
1764void __btrfs_del_delalloc_inode(struct btrfs_root *root,
1765 struct btrfs_inode *inode)
Miao Xieeb73c1b2013-05-15 07:48:22 +00001766{
David Sterba3ffbd682018-06-29 10:56:42 +02001767 struct btrfs_fs_info *fs_info = root->fs_info;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001768
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001769 if (!list_empty(&inode->delalloc_inodes)) {
1770 list_del_init(&inode->delalloc_inodes);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001771 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001772 &inode->runtime_flags);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001773 root->nr_delalloc_inodes--;
1774 if (!root->nr_delalloc_inodes) {
Nikolay Borisov7c8a0d32018-04-27 12:21:52 +03001775 ASSERT(list_empty(&root->delalloc_inodes));
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001776 spin_lock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001777 BUG_ON(list_empty(&root->delalloc_root));
1778 list_del_init(&root->delalloc_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001779 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001780 }
1781 }
Nikolay Borisov2b877332018-04-27 12:21:51 +03001782}
1783
1784static void btrfs_del_delalloc_inode(struct btrfs_root *root,
1785 struct btrfs_inode *inode)
1786{
1787 spin_lock(&root->delalloc_lock);
1788 __btrfs_del_delalloc_inode(root, inode);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001789 spin_unlock(&root->delalloc_lock);
1790}
1791
Chris Masond352ac62008-09-29 15:18:18 -04001792/*
Nikolay Borisove06a1fc2018-11-01 14:09:50 +02001793 * Properly track delayed allocation bytes in the inode and to maintain the
1794 * list of inodes that have pending delalloc work to be done.
Chris Masond352ac62008-09-29 15:18:18 -04001795 */
Nikolay Borisove06a1fc2018-11-01 14:09:50 +02001796void btrfs_set_delalloc_extent(struct inode *inode, struct extent_state *state,
1797 unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001798{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001799 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1800
Wang Shilong47059d92014-07-03 18:22:07 +08001801 if ((*bits & EXTENT_DEFRAG) && !(*bits & EXTENT_DELALLOC))
1802 WARN_ON(1);
Chris Mason75eff682008-12-15 15:54:40 -05001803 /*
1804 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001805 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001806 * bit, which is only set or cleared with irqs on
1807 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001808 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001809 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001810 u64 len = state->end + 1 - state->start;
Josef Bacik8b62f872017-10-19 14:15:55 -04001811 u32 num_extents = count_max_extents(len);
Nikolay Borisov70ddc552017-02-20 13:50:35 +02001812 bool do_list = !btrfs_is_free_space_inode(BTRFS_I(inode));
Josef Bacik9ed74f22009-09-11 16:12:44 -04001813
Josef Bacik8b62f872017-10-19 14:15:55 -04001814 spin_lock(&BTRFS_I(inode)->lock);
1815 btrfs_mod_outstanding_extents(BTRFS_I(inode), num_extents);
1816 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik287a0ab2010-03-19 18:07:23 +00001817
Josef Bacik6a3891c2015-03-16 17:38:52 -04001818 /* For sanity tests */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001819 if (btrfs_is_testing(fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04001820 return;
1821
Nikolay Borisov104b4e52017-06-20 21:01:20 +03001822 percpu_counter_add_batch(&fs_info->delalloc_bytes, len,
1823 fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001824 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001825 BTRFS_I(inode)->delalloc_bytes += len;
Wang Shilong47059d92014-07-03 18:22:07 +08001826 if (*bits & EXTENT_DEFRAG)
1827 BTRFS_I(inode)->defrag_bytes += len;
Miao Xiedf0af1a2013-01-29 10:11:59 +00001828 if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00001829 &BTRFS_I(inode)->runtime_flags))
1830 btrfs_add_delalloc_inodes(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001831 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001832 }
Filipe Mananaa7e3b972017-04-03 10:45:46 +01001833
1834 if (!(state->state & EXTENT_DELALLOC_NEW) &&
1835 (*bits & EXTENT_DELALLOC_NEW)) {
1836 spin_lock(&BTRFS_I(inode)->lock);
1837 BTRFS_I(inode)->new_delalloc_bytes += state->end + 1 -
1838 state->start;
1839 spin_unlock(&BTRFS_I(inode)->lock);
1840 }
Chris Mason291d6732008-01-29 15:55:23 -05001841}
1842
Chris Masond352ac62008-09-29 15:18:18 -04001843/*
Nikolay Borisova36bb5f2018-11-01 14:09:51 +02001844 * Once a range is no longer delalloc this function ensures that proper
1845 * accounting happens.
Chris Masond352ac62008-09-29 15:18:18 -04001846 */
Nikolay Borisova36bb5f2018-11-01 14:09:51 +02001847void btrfs_clear_delalloc_extent(struct inode *vfs_inode,
1848 struct extent_state *state, unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001849{
Nikolay Borisova36bb5f2018-11-01 14:09:51 +02001850 struct btrfs_inode *inode = BTRFS_I(vfs_inode);
1851 struct btrfs_fs_info *fs_info = btrfs_sb(vfs_inode->i_sb);
Wang Shilong47059d92014-07-03 18:22:07 +08001852 u64 len = state->end + 1 - state->start;
David Sterba823bb202017-01-04 11:09:51 +01001853 u32 num_extents = count_max_extents(len);
Wang Shilong47059d92014-07-03 18:22:07 +08001854
Filipe Manana4a4b9642017-07-27 19:52:55 +01001855 if ((state->state & EXTENT_DEFRAG) && (*bits & EXTENT_DEFRAG)) {
1856 spin_lock(&inode->lock);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001857 inode->defrag_bytes -= len;
Filipe Manana4a4b9642017-07-27 19:52:55 +01001858 spin_unlock(&inode->lock);
1859 }
Wang Shilong47059d92014-07-03 18:22:07 +08001860
Chris Mason75eff682008-12-15 15:54:40 -05001861 /*
1862 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001863 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001864 * bit, which is only set or cleared with irqs on
1865 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001866 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001867 struct btrfs_root *root = inode->root;
Liu Bo83eea1f2012-07-10 05:28:39 -06001868 bool do_list = !btrfs_is_free_space_inode(inode);
Chris Masonbcbfce82008-04-22 13:26:47 -04001869
Josef Bacik8b62f872017-10-19 14:15:55 -04001870 spin_lock(&inode->lock);
1871 btrfs_mod_outstanding_extents(inode, -num_extents);
1872 spin_unlock(&inode->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001873
Josef Bacikb6d08f02013-09-27 14:57:43 -04001874 /*
1875 * We don't reserve metadata space for space cache inodes so we
Andrea Gelmini52042d82018-11-28 12:05:13 +01001876 * don't need to call delalloc_release_metadata if there is an
Josef Bacikb6d08f02013-09-27 14:57:43 -04001877 * error.
1878 */
Filipe Mananaa315e682017-03-06 23:04:20 +00001879 if (*bits & EXTENT_CLEAR_META_RESV &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001880 root != fs_info->tree_root)
Qu Wenruo43b18592017-12-12 15:34:32 +08001881 btrfs_delalloc_release_metadata(inode, len, false);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001882
Josef Bacik6a3891c2015-03-16 17:38:52 -04001883 /* For sanity tests. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001884 if (btrfs_is_testing(fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04001885 return;
1886
Filipe Mananaa315e682017-03-06 23:04:20 +00001887 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID &&
1888 do_list && !(state->state & EXTENT_NORESERVE) &&
1889 (*bits & EXTENT_CLEAR_DATA_RESV))
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001890 btrfs_free_reserved_data_space_noquota(
1891 &inode->vfs_inode,
Qu Wenruo51773be2015-10-08 18:19:37 +08001892 state->start, len);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001893
Nikolay Borisov104b4e52017-06-20 21:01:20 +03001894 percpu_counter_add_batch(&fs_info->delalloc_bytes, -len,
1895 fs_info->delalloc_batch);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001896 spin_lock(&inode->lock);
1897 inode->delalloc_bytes -= len;
1898 if (do_list && inode->delalloc_bytes == 0 &&
Miao Xiedf0af1a2013-01-29 10:11:59 +00001899 test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001900 &inode->runtime_flags))
Miao Xieeb73c1b2013-05-15 07:48:22 +00001901 btrfs_del_delalloc_inode(root, inode);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001902 spin_unlock(&inode->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001903 }
Filipe Mananaa7e3b972017-04-03 10:45:46 +01001904
1905 if ((state->state & EXTENT_DELALLOC_NEW) &&
1906 (*bits & EXTENT_DELALLOC_NEW)) {
1907 spin_lock(&inode->lock);
1908 ASSERT(inode->new_delalloc_bytes >= len);
1909 inode->new_delalloc_bytes -= len;
1910 spin_unlock(&inode->lock);
1911 }
Chris Mason291d6732008-01-29 15:55:23 -05001912}
1913
Chris Masond352ac62008-09-29 15:18:18 -04001914/*
Nikolay Borisovda12fe52018-11-27 20:57:58 +02001915 * btrfs_bio_fits_in_stripe - Checks whether the size of the given bio will fit
1916 * in a chunk's stripe. This function ensures that bios do not span a
1917 * stripe/chunk
Liu Bo6f034ec2016-06-22 18:31:49 -07001918 *
Nikolay Borisovda12fe52018-11-27 20:57:58 +02001919 * @page - The page we are about to add to the bio
1920 * @size - size we want to add to the bio
1921 * @bio - bio we want to ensure is smaller than a stripe
1922 * @bio_flags - flags of the bio
1923 *
1924 * return 1 if page cannot be added to the bio
1925 * return 0 if page can be added to the bio
Liu Bo6f034ec2016-06-22 18:31:49 -07001926 * return error otherwise
Chris Masond352ac62008-09-29 15:18:18 -04001927 */
Nikolay Borisovda12fe52018-11-27 20:57:58 +02001928int btrfs_bio_fits_in_stripe(struct page *page, size_t size, struct bio *bio,
1929 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04001930{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001931 struct inode *inode = page->mapping->host;
1932 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001933 u64 logical = (u64)bio->bi_iter.bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04001934 u64 length = 0;
1935 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04001936 int ret;
1937
Chris Mason771ed682008-11-06 22:02:51 -05001938 if (bio_flags & EXTENT_BIO_COMPRESSED)
1939 return 0;
1940
Kent Overstreet4f024f32013-10-11 15:44:27 -07001941 length = bio->bi_iter.bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04001942 map_length = length;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001943 ret = btrfs_map_block(fs_info, btrfs_op(bio), logical, &map_length,
1944 NULL, 0);
Liu Bo6f034ec2016-06-22 18:31:49 -07001945 if (ret < 0)
1946 return ret;
Chris Masond3977122009-01-05 21:25:51 -05001947 if (map_length < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04001948 return 1;
Jeff Mahoney3444a972011-10-03 23:23:13 -04001949 return 0;
Chris Mason239b14b2008-03-24 15:02:07 -04001950}
1951
Chris Masond352ac62008-09-29 15:18:18 -04001952/*
1953 * in order to insert checksums into the metadata in large chunks,
1954 * we wait until bio submission time. All the pages in the bio are
1955 * checksummed and sums are attached onto the ordered extent record.
1956 *
1957 * At IO completion time the cums attached on the ordered extent record
1958 * are inserted into the btree
1959 */
David Sterbad0ee3932018-03-08 14:35:48 +01001960static blk_status_t btrfs_submit_bio_start(void *private_data, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001961 u64 bio_offset)
Chris Mason065631f2008-02-20 12:07:25 -05001962{
Josef Bacikc6100a42017-05-05 11:57:13 -04001963 struct inode *inode = private_data;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001964 blk_status_t ret = 0;
Chris Masone0156402008-04-16 11:15:20 -04001965
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001966 ret = btrfs_csum_one_bio(inode, bio, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001967 BUG_ON(ret); /* -ENOMEM */
Chris Mason4a69a412008-11-06 22:03:00 -05001968 return 0;
1969}
Chris Masone0156402008-04-16 11:15:20 -04001970
Chris Mason4a69a412008-11-06 22:03:00 -05001971/*
Chris Masoncad321a2008-12-17 14:51:42 -05001972 * extent_io.c submission hook. This does the right thing for csum calculation
Liu Bo4c274bc2017-11-01 17:19:27 -06001973 * on write, or reading the csums from the tree before a read.
1974 *
1975 * Rules about async/sync submit,
1976 * a) read: sync submit
1977 *
1978 * b) write without checksum: sync submit
1979 *
1980 * c) write with checksum:
1981 * c-1) if bio is issued by fsync: sync submit
1982 * (sync_writers != 0)
1983 *
1984 * c-2) if root is reloc root: sync submit
1985 * (only in case of buffered IO)
1986 *
1987 * c-3) otherwise: async submit
Chris Masond352ac62008-09-29 15:18:18 -04001988 */
Nikolay Borisova56b1c72019-04-10 17:24:39 +03001989static blk_status_t btrfs_submit_bio_hook(struct inode *inode, struct bio *bio,
Nikolay Borisov50489a52019-04-10 19:46:04 +03001990 int mirror_num,
1991 unsigned long bio_flags)
1992
Chris Mason44b8bd72008-04-16 11:14:51 -04001993{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001994 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason44b8bd72008-04-16 11:14:51 -04001995 struct btrfs_root *root = BTRFS_I(inode)->root;
Chandan Rajendra0d51e282015-07-27 15:26:43 +05301996 enum btrfs_wq_endio_type metadata = BTRFS_WQ_ENDIO_DATA;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001997 blk_status_t ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001998 int skip_sum;
Josef Bacikb812ce22012-11-16 13:56:32 -05001999 int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
Chris Mason44b8bd72008-04-16 11:14:51 -04002000
Christoph Hellwig6cbff002009-04-17 10:37:41 +02002001 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masoncad321a2008-12-17 14:51:42 -05002002
Nikolay Borisov70ddc552017-02-20 13:50:35 +02002003 if (btrfs_is_free_space_inode(BTRFS_I(inode)))
Chandan Rajendra0d51e282015-07-27 15:26:43 +05302004 metadata = BTRFS_WQ_ENDIO_FREE_SPACE;
Jeff Mahoney04173412011-10-03 23:23:12 -04002005
Mike Christie37226b22016-06-05 14:31:52 -05002006 if (bio_op(bio) != REQ_OP_WRITE) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002007 ret = btrfs_bio_wq_end_io(fs_info, bio, metadata);
Josef Bacik5fd02042012-05-02 14:00:54 -04002008 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01002009 goto out;
Josef Bacik5fd02042012-05-02 14:00:54 -04002010
Chris Masond20f7042008-12-08 16:58:54 -05002011 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Stefan Behrens61891922012-11-05 18:51:52 +01002012 ret = btrfs_submit_compressed_read(inode, bio,
2013 mirror_num,
2014 bio_flags);
2015 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00002016 } else if (!skip_sum) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002017 ret = btrfs_lookup_bio_sums(inode, bio, NULL);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00002018 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01002019 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00002020 }
Chris Mason4d1b5fb2008-08-20 09:44:52 -04002021 goto mapit;
Josef Bacikb812ce22012-11-16 13:56:32 -05002022 } else if (async && !skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05002023 /* csum items have already been cloned */
2024 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
2025 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04002026 /* we're doing a write, do the async checksumming */
Josef Bacikc6100a42017-05-05 11:57:13 -04002027 ret = btrfs_wq_submit_bio(fs_info, bio, mirror_num, bio_flags,
Nikolay Borisove7681162019-04-10 17:24:41 +03002028 0, inode, btrfs_submit_bio_start);
Stefan Behrens61891922012-11-05 18:51:52 +01002029 goto out;
Josef Bacikb812ce22012-11-16 13:56:32 -05002030 } else if (!skip_sum) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002031 ret = btrfs_csum_one_bio(inode, bio, 0, 0);
Josef Bacikb812ce22012-11-16 13:56:32 -05002032 if (ret)
2033 goto out;
Chris Mason19b9bdb2008-10-30 14:23:13 -04002034 }
2035
Chris Mason0b86a832008-03-24 15:01:56 -04002036mapit:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002037 ret = btrfs_map_bio(fs_info, bio, mirror_num, 0);
Stefan Behrens61891922012-11-05 18:51:52 +01002038
2039out:
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002040 if (ret) {
2041 bio->bi_status = ret;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002042 bio_endio(bio);
2043 }
Stefan Behrens61891922012-11-05 18:51:52 +01002044 return ret;
Chris Mason065631f2008-02-20 12:07:25 -05002045}
Chris Mason6885f302008-02-20 16:11:05 -05002046
Chris Masond352ac62008-09-29 15:18:18 -04002047/*
2048 * given a list of ordered sums record them in the inode. This happens
2049 * at IO completion time based on sums calculated at bio submission time.
2050 */
Chris Masonba1da2f2008-07-17 12:54:15 -04002051static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
David Sterbadf9f6282017-02-10 19:35:37 +01002052 struct inode *inode, struct list_head *list)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002053{
Chris Masone6dcd2d2008-07-17 12:53:50 -04002054 struct btrfs_ordered_sum *sum;
Nikolay Borisovac01f262018-01-08 10:59:43 +02002055 int ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002056
Qinghuang Fengc6e30872009-01-21 10:59:08 -05002057 list_for_each_entry(sum, list, list) {
David Sterba7c2871a2017-11-08 01:07:43 +01002058 trans->adding_csums = true;
Nikolay Borisovac01f262018-01-08 10:59:43 +02002059 ret = btrfs_csum_file_blocks(trans,
Chris Masond20f7042008-12-08 16:58:54 -05002060 BTRFS_I(inode)->root->fs_info->csum_root, sum);
David Sterba7c2871a2017-11-08 01:07:43 +01002061 trans->adding_csums = false;
Nikolay Borisovac01f262018-01-08 10:59:43 +02002062 if (ret)
2063 return ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002064 }
2065 return 0;
2066}
2067
Josef Bacik2ac55d42010-02-03 19:33:23 +00002068int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
Filipe Mananae3b8a482017-11-04 00:16:59 +00002069 unsigned int extra_bits,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08002070 struct extent_state **cached_state, int dedupe)
Chris Masonea8c2812008-08-04 23:17:27 -04002071{
Johannes Thumshirnfdb1e122018-12-05 15:23:04 +01002072 WARN_ON(PAGE_ALIGNED(end));
Chris Masonea8c2812008-08-04 23:17:27 -04002073 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
Filipe Mananae3b8a482017-11-04 00:16:59 +00002074 extra_bits, cached_state);
Chris Masonea8c2812008-08-04 23:17:27 -04002075}
2076
Chris Masond352ac62008-09-29 15:18:18 -04002077/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04002078struct btrfs_writepage_fixup {
2079 struct page *page;
2080 struct btrfs_work work;
2081};
2082
Christoph Hellwigb2950862008-12-02 09:54:17 -05002083static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04002084{
2085 struct btrfs_writepage_fixup *fixup;
2086 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002087 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08002088 struct extent_changeset *data_reserved = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04002089 struct page *page;
2090 struct inode *inode;
2091 u64 page_start;
2092 u64 page_end;
Jeff Mahoney87826df2012-02-15 16:23:57 +01002093 int ret;
Chris Mason247e7432008-07-17 12:53:51 -04002094
2095 fixup = container_of(work, struct btrfs_writepage_fixup, work);
2096 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -04002097again:
Chris Mason247e7432008-07-17 12:53:51 -04002098 lock_page(page);
2099 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
2100 ClearPageChecked(page);
2101 goto out_page;
2102 }
2103
2104 inode = page->mapping->host;
2105 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002106 page_end = page_offset(page) + PAGE_SIZE - 1;
Chris Mason247e7432008-07-17 12:53:51 -04002107
David Sterbaff13db42015-12-03 14:30:40 +01002108 lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002109 &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04002110
2111 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04002112 if (PagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04002113 goto out;
Chris Mason4a096752008-07-21 10:29:44 -04002114
Nikolay Borisova776c6f2017-02-20 13:50:49 +02002115 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002116 PAGE_SIZE);
Chris Mason4a096752008-07-21 10:29:44 -04002117 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00002118 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
David Sterbae43bbe52017-12-12 21:43:52 +01002119 page_end, &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04002120 unlock_page(page);
2121 btrfs_start_ordered_extent(inode, ordered, 1);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002122 btrfs_put_ordered_extent(ordered);
Chris Mason4a096752008-07-21 10:29:44 -04002123 goto again;
2124 }
Chris Mason247e7432008-07-17 12:53:51 -04002125
Qu Wenruo364ecf32017-02-27 15:10:38 +08002126 ret = btrfs_delalloc_reserve_space(inode, &data_reserved, page_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002127 PAGE_SIZE);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002128 if (ret) {
2129 mapping_set_error(page->mapping, ret);
2130 end_extent_writepage(page, ret, page_start, page_end);
2131 ClearPageChecked(page);
2132 goto out;
2133 }
2134
Nikolay Borisovf3038ee2017-12-05 09:29:19 +02002135 ret = btrfs_set_extent_delalloc(inode, page_start, page_end, 0,
2136 &cached_state, 0);
2137 if (ret) {
2138 mapping_set_error(page->mapping, ret);
2139 end_extent_writepage(page, ret, page_start, page_end);
2140 ClearPageChecked(page);
2141 goto out;
2142 }
2143
Chris Mason247e7432008-07-17 12:53:51 -04002144 ClearPageChecked(page);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002145 set_page_dirty(page);
Qu Wenruo43b18592017-12-12 15:34:32 +08002146 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE, false);
Chris Mason247e7432008-07-17 12:53:51 -04002147out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00002148 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01002149 &cached_state);
Chris Mason247e7432008-07-17 12:53:51 -04002150out_page:
2151 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002152 put_page(page);
Miao Xieb897abe2011-01-26 16:19:22 +08002153 kfree(fixup);
Qu Wenruo364ecf32017-02-27 15:10:38 +08002154 extent_changeset_free(data_reserved);
Chris Mason247e7432008-07-17 12:53:51 -04002155}
2156
2157/*
2158 * There are a few paths in the higher layers of the kernel that directly
2159 * set the page dirty bit without asking the filesystem if it is a
2160 * good idea. This causes problems because we want to make sure COW
2161 * properly happens and the data=ordered rules are followed.
2162 *
Chris Masonc8b97812008-10-29 14:49:59 -04002163 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04002164 * hasn't been properly setup for IO. We kick off an async process
2165 * to fix it up. The async helper will wait for ordered extents, set
2166 * the delalloc bit and make it safe to write the page.
2167 */
Nikolay Borisovd75855b2018-11-01 14:09:47 +02002168int btrfs_writepage_cow_fixup(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04002169{
2170 struct inode *inode = page->mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002171 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason247e7432008-07-17 12:53:51 -04002172 struct btrfs_writepage_fixup *fixup;
Chris Mason247e7432008-07-17 12:53:51 -04002173
Chris Mason8b62b722009-09-02 16:53:46 -04002174 /* this page is properly in the ordered list */
2175 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04002176 return 0;
2177
2178 if (PageChecked(page))
2179 return -EAGAIN;
2180
2181 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
2182 if (!fixup)
2183 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04002184
Chris Mason247e7432008-07-17 12:53:51 -04002185 SetPageChecked(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002186 get_page(page);
Liu Bo9e0af232014-08-15 23:36:53 +08002187 btrfs_init_work(&fixup->work, btrfs_fixup_helper,
2188 btrfs_writepage_fixup_worker, NULL, NULL);
Chris Mason247e7432008-07-17 12:53:51 -04002189 fixup->page = page;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002190 btrfs_queue_work(fs_info->fixup_workers, &fixup->work);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002191 return -EBUSY;
Chris Mason247e7432008-07-17 12:53:51 -04002192}
2193
Yan Zhengd899e052008-10-30 14:25:28 -04002194static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
2195 struct inode *inode, u64 file_pos,
2196 u64 disk_bytenr, u64 disk_num_bytes,
2197 u64 num_bytes, u64 ram_bytes,
2198 u8 compression, u8 encryption,
2199 u16 other_encoding, int extent_type)
2200{
2201 struct btrfs_root *root = BTRFS_I(inode)->root;
2202 struct btrfs_file_extent_item *fi;
2203 struct btrfs_path *path;
2204 struct extent_buffer *leaf;
2205 struct btrfs_key ins;
Qu Wenruoa12b8772017-02-27 15:10:37 +08002206 u64 qg_released;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002207 int extent_inserted = 0;
Yan Zhengd899e052008-10-30 14:25:28 -04002208 int ret;
2209
2210 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07002211 if (!path)
2212 return -ENOMEM;
Yan Zhengd899e052008-10-30 14:25:28 -04002213
Chris Masona1ed8352009-09-11 12:27:37 -04002214 /*
2215 * we may be replacing one extent in the tree with another.
2216 * The new extent is pinned in the extent map, and we don't want
2217 * to drop it from the cache until it is completely in the btree.
2218 *
2219 * So, tell btrfs_drop_extents to leave this extent in the cache.
2220 * the caller is expected to unpin it and allow it to be merged
2221 * with the others.
2222 */
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002223 ret = __btrfs_drop_extents(trans, root, inode, path, file_pos,
2224 file_pos + num_bytes, NULL, 0,
2225 1, sizeof(*fi), &extent_inserted);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002226 if (ret)
2227 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04002228
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002229 if (!extent_inserted) {
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002230 ins.objectid = btrfs_ino(BTRFS_I(inode));
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002231 ins.offset = file_pos;
2232 ins.type = BTRFS_EXTENT_DATA_KEY;
2233
2234 path->leave_spinning = 1;
2235 ret = btrfs_insert_empty_item(trans, root, path, &ins,
2236 sizeof(*fi));
2237 if (ret)
2238 goto out;
2239 }
Yan Zhengd899e052008-10-30 14:25:28 -04002240 leaf = path->nodes[0];
2241 fi = btrfs_item_ptr(leaf, path->slots[0],
2242 struct btrfs_file_extent_item);
2243 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
2244 btrfs_set_file_extent_type(leaf, fi, extent_type);
2245 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
2246 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
2247 btrfs_set_file_extent_offset(leaf, fi, 0);
2248 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
2249 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
2250 btrfs_set_file_extent_compression(leaf, fi, compression);
2251 btrfs_set_file_extent_encryption(leaf, fi, encryption);
2252 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
Chris Masonb9473432009-03-13 11:00:37 -04002253
Yan Zhengd899e052008-10-30 14:25:28 -04002254 btrfs_mark_buffer_dirty(leaf);
Josef Bacikce195332012-09-25 15:26:16 -04002255 btrfs_release_path(path);
Yan Zhengd899e052008-10-30 14:25:28 -04002256
2257 inode_add_bytes(inode, num_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -04002258
2259 ins.objectid = disk_bytenr;
2260 ins.offset = disk_num_bytes;
2261 ins.type = BTRFS_EXTENT_ITEM_KEY;
Qu Wenruoa12b8772017-02-27 15:10:37 +08002262
Qu Wenruo297d7502015-09-08 17:08:37 +08002263 /*
Qu Wenruo5846a3c2015-10-26 14:11:18 +08002264 * Release the reserved range from inode dirty range map, as it is
2265 * already moved into delayed_ref_head
Qu Wenruo297d7502015-09-08 17:08:37 +08002266 */
Qu Wenruoa12b8772017-02-27 15:10:37 +08002267 ret = btrfs_qgroup_release_data(inode, file_pos, ram_bytes);
2268 if (ret < 0)
2269 goto out;
2270 qg_released = ret;
Josef Bacik84f7d8e2017-09-29 15:43:49 -04002271 ret = btrfs_alloc_reserved_file_extent(trans, root,
2272 btrfs_ino(BTRFS_I(inode)),
2273 file_pos, qg_released, &ins);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002274out:
Yan Zhengd899e052008-10-30 14:25:28 -04002275 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04002276
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002277 return ret;
Yan Zhengd899e052008-10-30 14:25:28 -04002278}
2279
Liu Bo38c227d2013-01-29 03:18:40 +00002280/* snapshot-aware defrag */
2281struct sa_defrag_extent_backref {
2282 struct rb_node node;
2283 struct old_sa_defrag_extent *old;
2284 u64 root_id;
2285 u64 inum;
2286 u64 file_pos;
2287 u64 extent_offset;
2288 u64 num_bytes;
2289 u64 generation;
2290};
2291
2292struct old_sa_defrag_extent {
2293 struct list_head list;
2294 struct new_sa_defrag_extent *new;
2295
2296 u64 extent_offset;
2297 u64 bytenr;
2298 u64 offset;
2299 u64 len;
2300 int count;
2301};
2302
2303struct new_sa_defrag_extent {
2304 struct rb_root root;
2305 struct list_head head;
2306 struct btrfs_path *path;
2307 struct inode *inode;
2308 u64 file_pos;
2309 u64 len;
2310 u64 bytenr;
2311 u64 disk_len;
2312 u8 compress_type;
2313};
2314
2315static int backref_comp(struct sa_defrag_extent_backref *b1,
2316 struct sa_defrag_extent_backref *b2)
2317{
2318 if (b1->root_id < b2->root_id)
2319 return -1;
2320 else if (b1->root_id > b2->root_id)
2321 return 1;
2322
2323 if (b1->inum < b2->inum)
2324 return -1;
2325 else if (b1->inum > b2->inum)
2326 return 1;
2327
2328 if (b1->file_pos < b2->file_pos)
2329 return -1;
2330 else if (b1->file_pos > b2->file_pos)
2331 return 1;
2332
2333 /*
2334 * [------------------------------] ===> (a range of space)
2335 * |<--->| |<---->| =============> (fs/file tree A)
2336 * |<---------------------------->| ===> (fs/file tree B)
2337 *
2338 * A range of space can refer to two file extents in one tree while
2339 * refer to only one file extent in another tree.
2340 *
2341 * So we may process a disk offset more than one time(two extents in A)
2342 * and locate at the same extent(one extent in B), then insert two same
2343 * backrefs(both refer to the extent in B).
2344 */
2345 return 0;
2346}
2347
2348static void backref_insert(struct rb_root *root,
2349 struct sa_defrag_extent_backref *backref)
2350{
2351 struct rb_node **p = &root->rb_node;
2352 struct rb_node *parent = NULL;
2353 struct sa_defrag_extent_backref *entry;
2354 int ret;
2355
2356 while (*p) {
2357 parent = *p;
2358 entry = rb_entry(parent, struct sa_defrag_extent_backref, node);
2359
2360 ret = backref_comp(backref, entry);
2361 if (ret < 0)
2362 p = &(*p)->rb_left;
2363 else
2364 p = &(*p)->rb_right;
2365 }
2366
2367 rb_link_node(&backref->node, parent, p);
2368 rb_insert_color(&backref->node, root);
2369}
2370
2371/*
2372 * Note the backref might has changed, and in this case we just return 0.
2373 */
2374static noinline int record_one_backref(u64 inum, u64 offset, u64 root_id,
2375 void *ctx)
2376{
2377 struct btrfs_file_extent_item *extent;
Liu Bo38c227d2013-01-29 03:18:40 +00002378 struct old_sa_defrag_extent *old = ctx;
2379 struct new_sa_defrag_extent *new = old->new;
2380 struct btrfs_path *path = new->path;
2381 struct btrfs_key key;
2382 struct btrfs_root *root;
2383 struct sa_defrag_extent_backref *backref;
2384 struct extent_buffer *leaf;
2385 struct inode *inode = new->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002386 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002387 int slot;
2388 int ret;
2389 u64 extent_offset;
2390 u64 num_bytes;
2391
2392 if (BTRFS_I(inode)->root->root_key.objectid == root_id &&
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002393 inum == btrfs_ino(BTRFS_I(inode)))
Liu Bo38c227d2013-01-29 03:18:40 +00002394 return 0;
2395
2396 key.objectid = root_id;
2397 key.type = BTRFS_ROOT_ITEM_KEY;
2398 key.offset = (u64)-1;
2399
Liu Bo38c227d2013-01-29 03:18:40 +00002400 root = btrfs_read_fs_root_no_name(fs_info, &key);
2401 if (IS_ERR(root)) {
2402 if (PTR_ERR(root) == -ENOENT)
2403 return 0;
2404 WARN_ON(1);
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002405 btrfs_debug(fs_info, "inum=%llu, offset=%llu, root_id=%llu",
Liu Bo38c227d2013-01-29 03:18:40 +00002406 inum, offset, root_id);
2407 return PTR_ERR(root);
2408 }
2409
2410 key.objectid = inum;
2411 key.type = BTRFS_EXTENT_DATA_KEY;
2412 if (offset > (u64)-1 << 32)
2413 key.offset = 0;
2414 else
2415 key.offset = offset;
2416
2417 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05302418 if (WARN_ON(ret < 0))
Liu Bo38c227d2013-01-29 03:18:40 +00002419 return ret;
Josef Bacik50f13192013-07-22 12:50:37 -04002420 ret = 0;
Liu Bo38c227d2013-01-29 03:18:40 +00002421
2422 while (1) {
2423 cond_resched();
2424
2425 leaf = path->nodes[0];
2426 slot = path->slots[0];
2427
2428 if (slot >= btrfs_header_nritems(leaf)) {
2429 ret = btrfs_next_leaf(root, path);
2430 if (ret < 0) {
2431 goto out;
2432 } else if (ret > 0) {
2433 ret = 0;
2434 goto out;
2435 }
2436 continue;
2437 }
2438
2439 path->slots[0]++;
2440
2441 btrfs_item_key_to_cpu(leaf, &key, slot);
2442
2443 if (key.objectid > inum)
2444 goto out;
2445
2446 if (key.objectid < inum || key.type != BTRFS_EXTENT_DATA_KEY)
2447 continue;
2448
2449 extent = btrfs_item_ptr(leaf, slot,
2450 struct btrfs_file_extent_item);
2451
2452 if (btrfs_file_extent_disk_bytenr(leaf, extent) != old->bytenr)
2453 continue;
2454
Liu Boe68afa42013-07-01 22:13:26 +08002455 /*
2456 * 'offset' refers to the exact key.offset,
2457 * NOT the 'offset' field in btrfs_extent_data_ref, ie.
2458 * (key.offset - extent_offset).
2459 */
2460 if (key.offset != offset)
Liu Bo38c227d2013-01-29 03:18:40 +00002461 continue;
2462
Liu Boe68afa42013-07-01 22:13:26 +08002463 extent_offset = btrfs_file_extent_offset(leaf, extent);
Liu Bo38c227d2013-01-29 03:18:40 +00002464 num_bytes = btrfs_file_extent_num_bytes(leaf, extent);
Liu Boe68afa42013-07-01 22:13:26 +08002465
Liu Bo38c227d2013-01-29 03:18:40 +00002466 if (extent_offset >= old->extent_offset + old->offset +
2467 old->len || extent_offset + num_bytes <=
2468 old->extent_offset + old->offset)
2469 continue;
Liu Bo38c227d2013-01-29 03:18:40 +00002470 break;
2471 }
2472
2473 backref = kmalloc(sizeof(*backref), GFP_NOFS);
2474 if (!backref) {
2475 ret = -ENOENT;
2476 goto out;
2477 }
2478
2479 backref->root_id = root_id;
2480 backref->inum = inum;
Liu Boe68afa42013-07-01 22:13:26 +08002481 backref->file_pos = offset;
Liu Bo38c227d2013-01-29 03:18:40 +00002482 backref->num_bytes = num_bytes;
2483 backref->extent_offset = extent_offset;
2484 backref->generation = btrfs_file_extent_generation(leaf, extent);
2485 backref->old = old;
2486 backref_insert(&new->root, backref);
2487 old->count++;
2488out:
2489 btrfs_release_path(path);
2490 WARN_ON(ret);
2491 return ret;
2492}
2493
2494static noinline bool record_extent_backrefs(struct btrfs_path *path,
2495 struct new_sa_defrag_extent *new)
2496{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002497 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002498 struct old_sa_defrag_extent *old, *tmp;
2499 int ret;
2500
2501 new->path = path;
2502
2503 list_for_each_entry_safe(old, tmp, &new->head, list) {
Liu Boe68afa42013-07-01 22:13:26 +08002504 ret = iterate_inodes_from_logical(old->bytenr +
2505 old->extent_offset, fs_info,
Liu Bo38c227d2013-01-29 03:18:40 +00002506 path, record_one_backref,
Zygo Blaxellc995ab32017-09-22 13:58:45 -04002507 old, false);
Josef Bacik4724b102013-11-05 11:11:40 -05002508 if (ret < 0 && ret != -ENOENT)
2509 return false;
Liu Bo38c227d2013-01-29 03:18:40 +00002510
2511 /* no backref to be processed for this extent */
2512 if (!old->count) {
2513 list_del(&old->list);
2514 kfree(old);
2515 }
2516 }
2517
2518 if (list_empty(&new->head))
2519 return false;
2520
2521 return true;
2522}
2523
2524static int relink_is_mergable(struct extent_buffer *leaf,
2525 struct btrfs_file_extent_item *fi,
Liu Bo116e0022013-08-02 16:30:40 +08002526 struct new_sa_defrag_extent *new)
Liu Bo38c227d2013-01-29 03:18:40 +00002527{
Liu Bo116e0022013-08-02 16:30:40 +08002528 if (btrfs_file_extent_disk_bytenr(leaf, fi) != new->bytenr)
Liu Bo38c227d2013-01-29 03:18:40 +00002529 return 0;
2530
2531 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG)
2532 return 0;
2533
Liu Bo116e0022013-08-02 16:30:40 +08002534 if (btrfs_file_extent_compression(leaf, fi) != new->compress_type)
2535 return 0;
2536
2537 if (btrfs_file_extent_encryption(leaf, fi) ||
Liu Bo38c227d2013-01-29 03:18:40 +00002538 btrfs_file_extent_other_encoding(leaf, fi))
2539 return 0;
2540
2541 return 1;
2542}
2543
2544/*
2545 * Note the backref might has changed, and in this case we just return 0.
2546 */
2547static noinline int relink_extent_backref(struct btrfs_path *path,
2548 struct sa_defrag_extent_backref *prev,
2549 struct sa_defrag_extent_backref *backref)
2550{
2551 struct btrfs_file_extent_item *extent;
2552 struct btrfs_file_extent_item *item;
2553 struct btrfs_ordered_extent *ordered;
2554 struct btrfs_trans_handle *trans;
Qu Wenruo82fa1132019-04-04 14:45:35 +08002555 struct btrfs_ref ref = { 0 };
Liu Bo38c227d2013-01-29 03:18:40 +00002556 struct btrfs_root *root;
2557 struct btrfs_key key;
2558 struct extent_buffer *leaf;
2559 struct old_sa_defrag_extent *old = backref->old;
2560 struct new_sa_defrag_extent *new = old->new;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002561 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002562 struct inode *inode;
2563 struct extent_state *cached = NULL;
2564 int ret = 0;
2565 u64 start;
2566 u64 len;
2567 u64 lock_start;
2568 u64 lock_end;
2569 bool merge = false;
2570 int index;
2571
2572 if (prev && prev->root_id == backref->root_id &&
2573 prev->inum == backref->inum &&
2574 prev->file_pos + prev->num_bytes == backref->file_pos)
2575 merge = true;
2576
2577 /* step 1: get root */
2578 key.objectid = backref->root_id;
2579 key.type = BTRFS_ROOT_ITEM_KEY;
2580 key.offset = (u64)-1;
2581
Liu Bo38c227d2013-01-29 03:18:40 +00002582 index = srcu_read_lock(&fs_info->subvol_srcu);
2583
2584 root = btrfs_read_fs_root_no_name(fs_info, &key);
2585 if (IS_ERR(root)) {
2586 srcu_read_unlock(&fs_info->subvol_srcu, index);
2587 if (PTR_ERR(root) == -ENOENT)
2588 return 0;
2589 return PTR_ERR(root);
2590 }
Liu Bo38c227d2013-01-29 03:18:40 +00002591
Wang Shilongbcbba5e2014-02-08 23:46:35 +08002592 if (btrfs_root_readonly(root)) {
2593 srcu_read_unlock(&fs_info->subvol_srcu, index);
2594 return 0;
2595 }
2596
Liu Bo38c227d2013-01-29 03:18:40 +00002597 /* step 2: get inode */
2598 key.objectid = backref->inum;
2599 key.type = BTRFS_INODE_ITEM_KEY;
2600 key.offset = 0;
2601
2602 inode = btrfs_iget(fs_info->sb, &key, root, NULL);
2603 if (IS_ERR(inode)) {
2604 srcu_read_unlock(&fs_info->subvol_srcu, index);
2605 return 0;
2606 }
2607
2608 srcu_read_unlock(&fs_info->subvol_srcu, index);
2609
2610 /* step 3: relink backref */
2611 lock_start = backref->file_pos;
2612 lock_end = backref->file_pos + backref->num_bytes - 1;
2613 lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
David Sterbaff13db42015-12-03 14:30:40 +01002614 &cached);
Liu Bo38c227d2013-01-29 03:18:40 +00002615
2616 ordered = btrfs_lookup_first_ordered_extent(inode, lock_end);
2617 if (ordered) {
2618 btrfs_put_ordered_extent(ordered);
2619 goto out_unlock;
2620 }
2621
2622 trans = btrfs_join_transaction(root);
2623 if (IS_ERR(trans)) {
2624 ret = PTR_ERR(trans);
2625 goto out_unlock;
2626 }
2627
2628 key.objectid = backref->inum;
2629 key.type = BTRFS_EXTENT_DATA_KEY;
2630 key.offset = backref->file_pos;
2631
2632 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2633 if (ret < 0) {
2634 goto out_free_path;
2635 } else if (ret > 0) {
2636 ret = 0;
2637 goto out_free_path;
2638 }
2639
2640 extent = btrfs_item_ptr(path->nodes[0], path->slots[0],
2641 struct btrfs_file_extent_item);
2642
2643 if (btrfs_file_extent_generation(path->nodes[0], extent) !=
2644 backref->generation)
2645 goto out_free_path;
2646
2647 btrfs_release_path(path);
2648
2649 start = backref->file_pos;
2650 if (backref->extent_offset < old->extent_offset + old->offset)
2651 start += old->extent_offset + old->offset -
2652 backref->extent_offset;
2653
2654 len = min(backref->extent_offset + backref->num_bytes,
2655 old->extent_offset + old->offset + old->len);
2656 len -= max(backref->extent_offset, old->extent_offset + old->offset);
2657
2658 ret = btrfs_drop_extents(trans, root, inode, start,
2659 start + len, 1);
2660 if (ret)
2661 goto out_free_path;
2662again:
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002663 key.objectid = btrfs_ino(BTRFS_I(inode));
Liu Bo38c227d2013-01-29 03:18:40 +00002664 key.type = BTRFS_EXTENT_DATA_KEY;
2665 key.offset = start;
2666
Liu Boa09a0a72013-03-11 09:20:58 +00002667 path->leave_spinning = 1;
Liu Bo38c227d2013-01-29 03:18:40 +00002668 if (merge) {
2669 struct btrfs_file_extent_item *fi;
2670 u64 extent_len;
2671 struct btrfs_key found_key;
2672
Gui Hecheng3c9665d2014-01-23 13:41:09 +08002673 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
Liu Bo38c227d2013-01-29 03:18:40 +00002674 if (ret < 0)
2675 goto out_free_path;
2676
2677 path->slots[0]--;
2678 leaf = path->nodes[0];
2679 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2680
2681 fi = btrfs_item_ptr(leaf, path->slots[0],
2682 struct btrfs_file_extent_item);
2683 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
2684
Liu Bo116e0022013-08-02 16:30:40 +08002685 if (extent_len + found_key.offset == start &&
2686 relink_is_mergable(leaf, fi, new)) {
Liu Bo38c227d2013-01-29 03:18:40 +00002687 btrfs_set_file_extent_num_bytes(leaf, fi,
2688 extent_len + len);
2689 btrfs_mark_buffer_dirty(leaf);
2690 inode_add_bytes(inode, len);
2691
2692 ret = 1;
2693 goto out_free_path;
2694 } else {
2695 merge = false;
2696 btrfs_release_path(path);
2697 goto again;
2698 }
2699 }
2700
2701 ret = btrfs_insert_empty_item(trans, root, path, &key,
2702 sizeof(*extent));
2703 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002704 btrfs_abort_transaction(trans, ret);
Liu Bo38c227d2013-01-29 03:18:40 +00002705 goto out_free_path;
2706 }
2707
2708 leaf = path->nodes[0];
2709 item = btrfs_item_ptr(leaf, path->slots[0],
2710 struct btrfs_file_extent_item);
2711 btrfs_set_file_extent_disk_bytenr(leaf, item, new->bytenr);
2712 btrfs_set_file_extent_disk_num_bytes(leaf, item, new->disk_len);
2713 btrfs_set_file_extent_offset(leaf, item, start - new->file_pos);
2714 btrfs_set_file_extent_num_bytes(leaf, item, len);
2715 btrfs_set_file_extent_ram_bytes(leaf, item, new->len);
2716 btrfs_set_file_extent_generation(leaf, item, trans->transid);
2717 btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG);
2718 btrfs_set_file_extent_compression(leaf, item, new->compress_type);
2719 btrfs_set_file_extent_encryption(leaf, item, 0);
2720 btrfs_set_file_extent_other_encoding(leaf, item, 0);
2721
2722 btrfs_mark_buffer_dirty(leaf);
2723 inode_add_bytes(inode, len);
Liu Boa09a0a72013-03-11 09:20:58 +00002724 btrfs_release_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002725
Qu Wenruo82fa1132019-04-04 14:45:35 +08002726 btrfs_init_generic_ref(&ref, BTRFS_ADD_DELAYED_REF, new->bytenr,
2727 new->disk_len, 0);
2728 btrfs_init_data_ref(&ref, backref->root_id, backref->inum,
2729 new->file_pos); /* start - extent_offset */
2730 ret = btrfs_inc_extent_ref(trans, &ref);
Liu Bo38c227d2013-01-29 03:18:40 +00002731 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002732 btrfs_abort_transaction(trans, ret);
Liu Bo38c227d2013-01-29 03:18:40 +00002733 goto out_free_path;
2734 }
2735
2736 ret = 1;
2737out_free_path:
2738 btrfs_release_path(path);
Liu Boa09a0a72013-03-11 09:20:58 +00002739 path->leave_spinning = 0;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002740 btrfs_end_transaction(trans);
Liu Bo38c227d2013-01-29 03:18:40 +00002741out_unlock:
2742 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
David Sterbae43bbe52017-12-12 21:43:52 +01002743 &cached);
Liu Bo38c227d2013-01-29 03:18:40 +00002744 iput(inode);
2745 return ret;
2746}
2747
Liu Bo6f519562013-10-29 10:45:05 +08002748static void free_sa_defrag_extent(struct new_sa_defrag_extent *new)
2749{
2750 struct old_sa_defrag_extent *old, *tmp;
2751
2752 if (!new)
2753 return;
2754
2755 list_for_each_entry_safe(old, tmp, &new->head, list) {
Liu Bo6f519562013-10-29 10:45:05 +08002756 kfree(old);
2757 }
2758 kfree(new);
2759}
2760
Liu Bo38c227d2013-01-29 03:18:40 +00002761static void relink_file_extents(struct new_sa_defrag_extent *new)
2762{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002763 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002764 struct btrfs_path *path;
Liu Bo38c227d2013-01-29 03:18:40 +00002765 struct sa_defrag_extent_backref *backref;
2766 struct sa_defrag_extent_backref *prev = NULL;
Liu Bo38c227d2013-01-29 03:18:40 +00002767 struct rb_node *node;
2768 int ret;
2769
Liu Bo38c227d2013-01-29 03:18:40 +00002770 path = btrfs_alloc_path();
2771 if (!path)
2772 return;
2773
2774 if (!record_extent_backrefs(path, new)) {
2775 btrfs_free_path(path);
2776 goto out;
2777 }
2778 btrfs_release_path(path);
2779
2780 while (1) {
2781 node = rb_first(&new->root);
2782 if (!node)
2783 break;
2784 rb_erase(node, &new->root);
2785
2786 backref = rb_entry(node, struct sa_defrag_extent_backref, node);
2787
2788 ret = relink_extent_backref(path, prev, backref);
2789 WARN_ON(ret < 0);
2790
2791 kfree(prev);
2792
2793 if (ret == 1)
2794 prev = backref;
2795 else
2796 prev = NULL;
2797 cond_resched();
2798 }
2799 kfree(prev);
2800
2801 btrfs_free_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002802out:
Liu Bo6f519562013-10-29 10:45:05 +08002803 free_sa_defrag_extent(new);
2804
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002805 atomic_dec(&fs_info->defrag_running);
2806 wake_up(&fs_info->transaction_wait);
Liu Bo38c227d2013-01-29 03:18:40 +00002807}
2808
2809static struct new_sa_defrag_extent *
2810record_old_file_extents(struct inode *inode,
2811 struct btrfs_ordered_extent *ordered)
2812{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002813 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002814 struct btrfs_root *root = BTRFS_I(inode)->root;
2815 struct btrfs_path *path;
2816 struct btrfs_key key;
Liu Bo6f519562013-10-29 10:45:05 +08002817 struct old_sa_defrag_extent *old;
Liu Bo38c227d2013-01-29 03:18:40 +00002818 struct new_sa_defrag_extent *new;
2819 int ret;
2820
2821 new = kmalloc(sizeof(*new), GFP_NOFS);
2822 if (!new)
2823 return NULL;
2824
2825 new->inode = inode;
2826 new->file_pos = ordered->file_offset;
2827 new->len = ordered->len;
2828 new->bytenr = ordered->start;
2829 new->disk_len = ordered->disk_len;
2830 new->compress_type = ordered->compress_type;
2831 new->root = RB_ROOT;
2832 INIT_LIST_HEAD(&new->head);
2833
2834 path = btrfs_alloc_path();
2835 if (!path)
2836 goto out_kfree;
2837
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002838 key.objectid = btrfs_ino(BTRFS_I(inode));
Liu Bo38c227d2013-01-29 03:18:40 +00002839 key.type = BTRFS_EXTENT_DATA_KEY;
2840 key.offset = new->file_pos;
2841
2842 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2843 if (ret < 0)
2844 goto out_free_path;
2845 if (ret > 0 && path->slots[0] > 0)
2846 path->slots[0]--;
2847
2848 /* find out all the old extents for the file range */
2849 while (1) {
2850 struct btrfs_file_extent_item *extent;
2851 struct extent_buffer *l;
2852 int slot;
2853 u64 num_bytes;
2854 u64 offset;
2855 u64 end;
2856 u64 disk_bytenr;
2857 u64 extent_offset;
2858
2859 l = path->nodes[0];
2860 slot = path->slots[0];
2861
2862 if (slot >= btrfs_header_nritems(l)) {
2863 ret = btrfs_next_leaf(root, path);
2864 if (ret < 0)
Liu Bo6f519562013-10-29 10:45:05 +08002865 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002866 else if (ret > 0)
2867 break;
2868 continue;
2869 }
2870
2871 btrfs_item_key_to_cpu(l, &key, slot);
2872
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002873 if (key.objectid != btrfs_ino(BTRFS_I(inode)))
Liu Bo38c227d2013-01-29 03:18:40 +00002874 break;
2875 if (key.type != BTRFS_EXTENT_DATA_KEY)
2876 break;
2877 if (key.offset >= new->file_pos + new->len)
2878 break;
2879
2880 extent = btrfs_item_ptr(l, slot, struct btrfs_file_extent_item);
2881
2882 num_bytes = btrfs_file_extent_num_bytes(l, extent);
2883 if (key.offset + num_bytes < new->file_pos)
2884 goto next;
2885
2886 disk_bytenr = btrfs_file_extent_disk_bytenr(l, extent);
2887 if (!disk_bytenr)
2888 goto next;
2889
2890 extent_offset = btrfs_file_extent_offset(l, extent);
2891
2892 old = kmalloc(sizeof(*old), GFP_NOFS);
2893 if (!old)
Liu Bo6f519562013-10-29 10:45:05 +08002894 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002895
2896 offset = max(new->file_pos, key.offset);
2897 end = min(new->file_pos + new->len, key.offset + num_bytes);
2898
2899 old->bytenr = disk_bytenr;
2900 old->extent_offset = extent_offset;
2901 old->offset = offset - key.offset;
2902 old->len = end - offset;
2903 old->new = new;
2904 old->count = 0;
2905 list_add_tail(&old->list, &new->head);
2906next:
2907 path->slots[0]++;
2908 cond_resched();
2909 }
2910
2911 btrfs_free_path(path);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002912 atomic_inc(&fs_info->defrag_running);
Liu Bo38c227d2013-01-29 03:18:40 +00002913
2914 return new;
2915
Liu Bo38c227d2013-01-29 03:18:40 +00002916out_free_path:
2917 btrfs_free_path(path);
2918out_kfree:
Liu Bo6f519562013-10-29 10:45:05 +08002919 free_sa_defrag_extent(new);
Liu Bo38c227d2013-01-29 03:18:40 +00002920 return NULL;
2921}
2922
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002923static void btrfs_release_delalloc_bytes(struct btrfs_fs_info *fs_info,
Miao Xiee570fd22014-06-19 10:42:50 +08002924 u64 start, u64 len)
2925{
2926 struct btrfs_block_group_cache *cache;
2927
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002928 cache = btrfs_lookup_block_group(fs_info, start);
Miao Xiee570fd22014-06-19 10:42:50 +08002929 ASSERT(cache);
2930
2931 spin_lock(&cache->lock);
2932 cache->delalloc_bytes -= len;
2933 spin_unlock(&cache->lock);
2934
2935 btrfs_put_block_group(cache);
2936}
2937
Chris Masond352ac62008-09-29 15:18:18 -04002938/* as ordered data IO finishes, this gets called so we can finish
2939 * an ordered extent if the range of bytes in the file it covers are
2940 * fully written.
2941 */
Josef Bacik5fd02042012-05-02 14:00:54 -04002942static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002943{
Josef Bacik5fd02042012-05-02 14:00:54 -04002944 struct inode *inode = ordered_extent->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002945 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002946 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002947 struct btrfs_trans_handle *trans = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002948 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002949 struct extent_state *cached_state = NULL;
Liu Bo38c227d2013-01-29 03:18:40 +00002950 struct new_sa_defrag_extent *new = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08002951 int compress_type = 0;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002952 int ret = 0;
2953 u64 logical_len = ordered_extent->len;
Li Zefan82d59022011-04-20 10:33:24 +08002954 bool nolock;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002955 bool truncated = false;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002956 bool range_locked = false;
2957 bool clear_new_delalloc_bytes = false;
Josef Bacik49940bd2018-10-11 15:54:21 -04002958 bool clear_reserved_extent = true;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002959
2960 if (!test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
2961 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags) &&
2962 !test_bit(BTRFS_ORDERED_DIRECT, &ordered_extent->flags))
2963 clear_new_delalloc_bytes = true;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002964
Nikolay Borisov70ddc552017-02-20 13:50:35 +02002965 nolock = btrfs_is_free_space_inode(BTRFS_I(inode));
Josef Bacik0cb59c92010-07-02 12:14:14 -04002966
Josef Bacik5fd02042012-05-02 14:00:54 -04002967 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
2968 ret = -EIO;
2969 goto out;
2970 }
2971
Nikolay Borisov7ab79562017-02-20 13:50:57 +02002972 btrfs_free_io_failure_record(BTRFS_I(inode),
2973 ordered_extent->file_offset,
2974 ordered_extent->file_offset +
2975 ordered_extent->len - 1);
Miao Xief6124962014-09-12 18:44:04 +08002976
Josef Bacik77cef2e2013-08-29 13:57:21 -04002977 if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) {
2978 truncated = true;
2979 logical_len = ordered_extent->truncated_len;
2980 /* Truncated the entire extent, don't bother adding */
2981 if (!logical_len)
2982 goto out;
2983 }
2984
Yan, Zhengc2167752009-11-12 09:34:21 +00002985 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002986 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
Qu Wenruo94ed9382015-09-08 17:25:56 +08002987
2988 /*
2989 * For mwrite(mmap + memset to write) case, we still reserve
2990 * space for NOCOW range.
2991 * As NOCOW won't cause a new delayed ref, just free the space
2992 */
Qu Wenruobc42bda2017-02-27 15:10:39 +08002993 btrfs_qgroup_free_data(inode, NULL, ordered_extent->file_offset,
Qu Wenruo94ed9382015-09-08 17:25:56 +08002994 ordered_extent->len);
Josef Bacik6c760c02012-11-09 10:53:21 -05002995 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2996 if (nolock)
2997 trans = btrfs_join_transaction_nolock(root);
2998 else
2999 trans = btrfs_join_transaction(root);
3000 if (IS_ERR(trans)) {
3001 ret = PTR_ERR(trans);
3002 trans = NULL;
3003 goto out;
Yan, Zhengc2167752009-11-12 09:34:21 +00003004 }
Josef Bacik69fe2d72017-10-19 14:15:57 -04003005 trans->block_rsv = &BTRFS_I(inode)->block_rsv;
Josef Bacik6c760c02012-11-09 10:53:21 -05003006 ret = btrfs_update_inode_fallback(trans, root, inode);
3007 if (ret) /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04003008 btrfs_abort_transaction(trans, ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00003009 goto out;
3010 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04003011
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003012 range_locked = true;
Josef Bacik2ac55d42010-02-03 19:33:23 +00003013 lock_extent_bits(io_tree, ordered_extent->file_offset,
3014 ordered_extent->file_offset + ordered_extent->len - 1,
David Sterbaff13db42015-12-03 14:30:40 +01003015 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003016
Liu Bo38c227d2013-01-29 03:18:40 +00003017 ret = test_range_bit(io_tree, ordered_extent->file_offset,
3018 ordered_extent->file_offset + ordered_extent->len - 1,
Liu Bo452e62b2017-05-26 17:44:23 -06003019 EXTENT_DEFRAG, 0, cached_state);
Liu Bo38c227d2013-01-29 03:18:40 +00003020 if (ret) {
3021 u64 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
Josef Bacik8101c8d2014-01-29 16:05:30 -05003022 if (0 && last_snapshot >= BTRFS_I(inode)->generation)
Liu Bo38c227d2013-01-29 03:18:40 +00003023 /* the inode is shared */
3024 new = record_old_file_extents(inode, ordered_extent);
3025
3026 clear_extent_bit(io_tree, ordered_extent->file_offset,
3027 ordered_extent->file_offset + ordered_extent->len - 1,
David Sterbaae0f1622017-10-31 16:37:52 +01003028 EXTENT_DEFRAG, 0, 0, &cached_state);
Liu Bo38c227d2013-01-29 03:18:40 +00003029 }
3030
Josef Bacik0cb59c92010-07-02 12:14:14 -04003031 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003032 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04003033 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003034 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003035 if (IS_ERR(trans)) {
3036 ret = PTR_ERR(trans);
3037 trans = NULL;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003038 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003039 }
Chris Masona79b7d42014-05-22 16:18:52 -07003040
Josef Bacik69fe2d72017-10-19 14:15:57 -04003041 trans->block_rsv = &BTRFS_I(inode)->block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00003042
Chris Masonc8b97812008-10-29 14:49:59 -04003043 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08003044 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04003045 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08003046 BUG_ON(compress_type);
Justin Maggardb430b772017-10-30 15:29:10 -07003047 btrfs_qgroup_free_data(inode, NULL, ordered_extent->file_offset,
3048 ordered_extent->len);
Nikolay Borisov7a6d7062017-02-20 13:50:48 +02003049 ret = btrfs_mark_extent_written(trans, BTRFS_I(inode),
Yan Zhengd899e052008-10-30 14:25:28 -04003050 ordered_extent->file_offset,
3051 ordered_extent->file_offset +
Josef Bacik77cef2e2013-08-29 13:57:21 -04003052 logical_len);
Yan Zhengd899e052008-10-30 14:25:28 -04003053 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003054 BUG_ON(root == fs_info->tree_root);
Yan Zhengd899e052008-10-30 14:25:28 -04003055 ret = insert_reserved_file_extent(trans, inode,
3056 ordered_extent->file_offset,
3057 ordered_extent->start,
3058 ordered_extent->disk_len,
Josef Bacik77cef2e2013-08-29 13:57:21 -04003059 logical_len, logical_len,
Li Zefan261507a02010-12-17 14:21:50 +08003060 compress_type, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04003061 BTRFS_FILE_EXTENT_REG);
Josef Bacik49940bd2018-10-11 15:54:21 -04003062 if (!ret) {
3063 clear_reserved_extent = false;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003064 btrfs_release_delalloc_bytes(fs_info,
Miao Xiee570fd22014-06-19 10:42:50 +08003065 ordered_extent->start,
3066 ordered_extent->disk_len);
Josef Bacik49940bd2018-10-11 15:54:21 -04003067 }
Yan Zhengd899e052008-10-30 14:25:28 -04003068 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04003069 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
3070 ordered_extent->file_offset, ordered_extent->len,
3071 trans->transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003072 if (ret < 0) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003073 btrfs_abort_transaction(trans, ret);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003074 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003075 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00003076
Nikolay Borisovac01f262018-01-08 10:59:43 +02003077 ret = add_pending_csums(trans, inode, &ordered_extent->list);
3078 if (ret) {
3079 btrfs_abort_transaction(trans, ret);
3080 goto out;
3081 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04003082
Josef Bacik6c760c02012-11-09 10:53:21 -05003083 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
3084 ret = btrfs_update_inode_fallback(trans, root, inode);
3085 if (ret) { /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04003086 btrfs_abort_transaction(trans, ret);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003087 goto out;
Josef Bacik1ef30be2011-04-05 19:25:36 -04003088 }
3089 ret = 0;
Yan, Zhengc2167752009-11-12 09:34:21 +00003090out:
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003091 if (range_locked || clear_new_delalloc_bytes) {
3092 unsigned int clear_bits = 0;
3093
3094 if (range_locked)
3095 clear_bits |= EXTENT_LOCKED;
3096 if (clear_new_delalloc_bytes)
3097 clear_bits |= EXTENT_DELALLOC_NEW;
3098 clear_extent_bit(&BTRFS_I(inode)->io_tree,
3099 ordered_extent->file_offset,
3100 ordered_extent->file_offset +
3101 ordered_extent->len - 1,
3102 clear_bits,
3103 (clear_bits & EXTENT_LOCKED) ? 1 : 0,
David Sterbaae0f1622017-10-31 16:37:52 +01003104 0, &cached_state);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003105 }
3106
Miao Xiea698d0752012-09-20 01:51:59 -06003107 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003108 btrfs_end_transaction(trans);
Josef Bacik0cb59c92010-07-02 12:14:14 -04003109
Josef Bacik77cef2e2013-08-29 13:57:21 -04003110 if (ret || truncated) {
3111 u64 start, end;
3112
3113 if (truncated)
3114 start = ordered_extent->file_offset + logical_len;
3115 else
3116 start = ordered_extent->file_offset;
3117 end = ordered_extent->file_offset + ordered_extent->len - 1;
David Sterbaf08dc362017-10-31 17:02:39 +01003118 clear_extent_uptodate(io_tree, start, end, NULL);
Josef Bacik77cef2e2013-08-29 13:57:21 -04003119
3120 /* Drop the cache for the part of the extent we didn't write. */
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02003121 btrfs_drop_extent_cache(BTRFS_I(inode), start, end, 0);
Josef Bacik5fd02042012-05-02 14:00:54 -04003122
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003123 /*
3124 * If the ordered extent had an IOERR or something else went
3125 * wrong we need to return the space for this ordered extent
Josef Bacik77cef2e2013-08-29 13:57:21 -04003126 * back to the allocator. We only free the extent in the
3127 * truncated case if we didn't write out the extent at all.
Josef Bacik49940bd2018-10-11 15:54:21 -04003128 *
3129 * If we made it past insert_reserved_file_extent before we
3130 * errored out then we don't need to do this as the accounting
3131 * has already been done.
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003132 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04003133 if ((ret || !logical_len) &&
Josef Bacik49940bd2018-10-11 15:54:21 -04003134 clear_reserved_extent &&
Josef Bacik77cef2e2013-08-29 13:57:21 -04003135 !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003136 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003137 btrfs_free_reserved_extent(fs_info,
3138 ordered_extent->start,
Miao Xiee570fd22014-06-19 10:42:50 +08003139 ordered_extent->disk_len, 1);
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003140 }
3141
3142
Josef Bacik5fd02042012-05-02 14:00:54 -04003143 /*
Liu Bo8bad3c02012-06-18 12:14:23 +08003144 * This needs to be done to make sure anybody waiting knows we are done
3145 * updating everything for this ordered extent.
Josef Bacik5fd02042012-05-02 14:00:54 -04003146 */
3147 btrfs_remove_ordered_extent(inode, ordered_extent);
3148
Liu Bo38c227d2013-01-29 03:18:40 +00003149 /* for snapshot-aware defrag */
Liu Bo6f519562013-10-29 10:45:05 +08003150 if (new) {
3151 if (ret) {
3152 free_sa_defrag_extent(new);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003153 atomic_dec(&fs_info->defrag_running);
Liu Bo6f519562013-10-29 10:45:05 +08003154 } else {
3155 relink_file_extents(new);
3156 }
3157 }
Liu Bo38c227d2013-01-29 03:18:40 +00003158
Chris Masone6dcd2d2008-07-17 12:53:50 -04003159 /* once for us */
3160 btrfs_put_ordered_extent(ordered_extent);
3161 /* once for the tree */
3162 btrfs_put_ordered_extent(ordered_extent);
3163
Josef Bacik5fd02042012-05-02 14:00:54 -04003164 return ret;
3165}
3166
3167static void finish_ordered_fn(struct btrfs_work *work)
3168{
3169 struct btrfs_ordered_extent *ordered_extent;
3170 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
3171 btrfs_finish_ordered_io(ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003172}
3173
Nikolay Borisovc6297322018-11-08 10:18:08 +02003174void btrfs_writepage_endio_finish_ordered(struct page *page, u64 start,
3175 u64 end, int uptodate)
Chris Mason211f90e2008-07-18 11:56:15 -04003176{
Josef Bacik5fd02042012-05-02 14:00:54 -04003177 struct inode *inode = page->mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003178 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik5fd02042012-05-02 14:00:54 -04003179 struct btrfs_ordered_extent *ordered_extent = NULL;
Liu Bo9e0af232014-08-15 23:36:53 +08003180 struct btrfs_workqueue *wq;
3181 btrfs_work_func_t func;
Josef Bacik5fd02042012-05-02 14:00:54 -04003182
liubo1abe9b82011-03-24 11:18:59 +00003183 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
3184
Chris Mason8b62b722009-09-02 16:53:46 -04003185 ClearPagePrivate2(page);
Josef Bacik5fd02042012-05-02 14:00:54 -04003186 if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
3187 end - start + 1, uptodate))
David Sterbac3988d62017-02-17 15:18:32 +01003188 return;
Josef Bacik5fd02042012-05-02 14:00:54 -04003189
Nikolay Borisov70ddc552017-02-20 13:50:35 +02003190 if (btrfs_is_free_space_inode(BTRFS_I(inode))) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003191 wq = fs_info->endio_freespace_worker;
Liu Bo9e0af232014-08-15 23:36:53 +08003192 func = btrfs_freespace_write_helper;
3193 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003194 wq = fs_info->endio_write_workers;
Liu Bo9e0af232014-08-15 23:36:53 +08003195 func = btrfs_endio_write_helper;
3196 }
Josef Bacik5fd02042012-05-02 14:00:54 -04003197
Liu Bo9e0af232014-08-15 23:36:53 +08003198 btrfs_init_work(&ordered_extent->work, func, finish_ordered_fn, NULL,
3199 NULL);
3200 btrfs_queue_work(wq, &ordered_extent->work);
Chris Mason211f90e2008-07-18 11:56:15 -04003201}
3202
Miao Xiedc380ae2014-09-12 18:43:55 +08003203static int __readpage_endio_check(struct inode *inode,
3204 struct btrfs_io_bio *io_bio,
3205 int icsum, struct page *page,
3206 int pgoff, u64 start, size_t len)
3207{
3208 char *kaddr;
3209 u32 csum_expected;
3210 u32 csum = ~(u32)0;
Miao Xiedc380ae2014-09-12 18:43:55 +08003211
3212 csum_expected = *(((u32 *)io_bio->csum) + icsum);
3213
3214 kaddr = kmap_atomic(page);
3215 csum = btrfs_csum_data(kaddr + pgoff, csum, len);
Domagoj Tršan0b5e3da2016-10-27 08:52:33 +01003216 btrfs_csum_final(csum, (u8 *)&csum);
Miao Xiedc380ae2014-09-12 18:43:55 +08003217 if (csum != csum_expected)
3218 goto zeroit;
3219
3220 kunmap_atomic(kaddr);
3221 return 0;
3222zeroit:
Nikolay Borisov0970a222017-02-20 13:50:53 +02003223 btrfs_print_data_csum_error(BTRFS_I(inode), start, csum, csum_expected,
Qu Wenruo6f6b6432017-02-09 10:45:06 +08003224 io_bio->mirror_num);
Miao Xiedc380ae2014-09-12 18:43:55 +08003225 memset(kaddr + pgoff, 1, len);
3226 flush_dcache_page(page);
3227 kunmap_atomic(kaddr);
Miao Xiedc380ae2014-09-12 18:43:55 +08003228 return -EIO;
3229}
3230
Chris Masond352ac62008-09-29 15:18:18 -04003231/*
Chris Masond352ac62008-09-29 15:18:18 -04003232 * when reads are done, we need to check csums to verify the data is correct
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02003233 * if there's a match, we allow the bio to finish. If not, the code in
3234 * extent_io.c will try to find good copies for us.
Chris Masond352ac62008-09-29 15:18:18 -04003235 */
Miao Xiefacc8a222013-07-25 19:22:34 +08003236static int btrfs_readpage_end_io_hook(struct btrfs_io_bio *io_bio,
3237 u64 phy_offset, struct page *page,
3238 u64 start, u64 end, int mirror)
Chris Mason07157aa2007-08-30 08:50:51 -04003239{
Miao Xie4eee4fa2012-12-21 09:17:45 +00003240 size_t offset = start - page_offset(page);
Chris Mason07157aa2007-08-30 08:50:51 -04003241 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05003242 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masonff79f812007-10-15 16:22:25 -04003243 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -05003244
Chris Masond20f7042008-12-08 16:58:54 -05003245 if (PageChecked(page)) {
3246 ClearPageChecked(page);
Miao Xiedc380ae2014-09-12 18:43:55 +08003247 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003248 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003249
3250 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Miao Xiedc380ae2014-09-12 18:43:55 +08003251 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003252
Yan Zheng17d217f2008-12-12 10:03:38 -05003253 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04003254 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
David Sterba91166212016-04-26 23:54:39 +02003255 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM);
Yan Zheng17d217f2008-12-12 10:03:38 -05003256 return 0;
3257 }
3258
Miao Xiefacc8a222013-07-25 19:22:34 +08003259 phy_offset >>= inode->i_sb->s_blocksize_bits;
Miao Xiedc380ae2014-09-12 18:43:55 +08003260 return __readpage_endio_check(inode, io_bio, phy_offset, page, offset,
3261 start, (size_t)(end - start + 1));
Chris Mason07157aa2007-08-30 08:50:51 -04003262}
Chris Masonb888db22007-08-27 16:49:44 -04003263
Nikolay Borisovc1c3fac2018-01-16 09:31:58 +02003264/*
3265 * btrfs_add_delayed_iput - perform a delayed iput on @inode
3266 *
3267 * @inode: The inode we want to perform iput on
3268 *
3269 * This function uses the generic vfs_inode::i_count to track whether we should
3270 * just decrement it (in case it's > 1) or if this is the last iput then link
3271 * the inode to the delayed iput machinery. Delayed iputs are processed at
3272 * transaction commit time/superblock commit/cleaner kthread.
3273 */
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003274void btrfs_add_delayed_iput(struct inode *inode)
3275{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003276 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
David Sterba8089fe62015-11-19 14:15:51 +01003277 struct btrfs_inode *binode = BTRFS_I(inode);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003278
3279 if (atomic_add_unless(&inode->i_count, -1, 1))
3280 return;
3281
Josef Bacik034f7842018-12-03 11:06:52 -05003282 atomic_inc(&fs_info->nr_delayed_iputs);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003283 spin_lock(&fs_info->delayed_iput_lock);
Nikolay Borisovc1c3fac2018-01-16 09:31:58 +02003284 ASSERT(list_empty(&binode->delayed_iput));
3285 list_add_tail(&binode->delayed_iput, &fs_info->delayed_iputs);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003286 spin_unlock(&fs_info->delayed_iput_lock);
Josef Bacikfd340d02019-01-11 10:21:02 -05003287 if (!test_bit(BTRFS_FS_CLEANER_RUNNING, &fs_info->flags))
3288 wake_up_process(fs_info->cleaner_kthread);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003289}
3290
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003291void btrfs_run_delayed_iputs(struct btrfs_fs_info *fs_info)
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003292{
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003293
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003294 spin_lock(&fs_info->delayed_iput_lock);
David Sterba8089fe62015-11-19 14:15:51 +01003295 while (!list_empty(&fs_info->delayed_iputs)) {
3296 struct btrfs_inode *inode;
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003297
David Sterba8089fe62015-11-19 14:15:51 +01003298 inode = list_first_entry(&fs_info->delayed_iputs,
3299 struct btrfs_inode, delayed_iput);
Nikolay Borisovc1c3fac2018-01-16 09:31:58 +02003300 list_del_init(&inode->delayed_iput);
David Sterba8089fe62015-11-19 14:15:51 +01003301 spin_unlock(&fs_info->delayed_iput_lock);
3302 iput(&inode->vfs_inode);
Josef Bacik034f7842018-12-03 11:06:52 -05003303 if (atomic_dec_and_test(&fs_info->nr_delayed_iputs))
3304 wake_up(&fs_info->delayed_iputs_wait);
David Sterba8089fe62015-11-19 14:15:51 +01003305 spin_lock(&fs_info->delayed_iput_lock);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003306 }
David Sterba8089fe62015-11-19 14:15:51 +01003307 spin_unlock(&fs_info->delayed_iput_lock);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003308}
3309
Josef Bacik034f7842018-12-03 11:06:52 -05003310/**
3311 * btrfs_wait_on_delayed_iputs - wait on the delayed iputs to be done running
3312 * @fs_info - the fs_info for this fs
3313 * @return - EINTR if we were killed, 0 if nothing's pending
3314 *
3315 * This will wait on any delayed iputs that are currently running with KILLABLE
3316 * set. Once they are all done running we will return, unless we are killed in
3317 * which case we return EINTR. This helps in user operations like fallocate etc
3318 * that might get blocked on the iputs.
3319 */
3320int btrfs_wait_on_delayed_iputs(struct btrfs_fs_info *fs_info)
3321{
3322 int ret = wait_event_killable(fs_info->delayed_iputs_wait,
3323 atomic_read(&fs_info->nr_delayed_iputs) == 0);
3324 if (ret)
3325 return -EINTR;
3326 return 0;
3327}
3328
Yan, Zhengd68fc572010-05-16 10:49:58 -04003329/*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003330 * This creates an orphan entry for the given inode in case something goes wrong
3331 * in the middle of an unlink.
Josef Bacik7b128762008-07-24 12:17:14 -04003332 */
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003333int btrfs_orphan_add(struct btrfs_trans_handle *trans,
Omar Sandoval27919062018-05-11 13:13:37 -07003334 struct btrfs_inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003335{
Yan, Zhengd68fc572010-05-16 10:49:58 -04003336 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003337
Omar Sandoval27919062018-05-11 13:13:37 -07003338 ret = btrfs_insert_orphan_item(trans, inode->root, btrfs_ino(inode));
3339 if (ret && ret != -EEXIST) {
3340 btrfs_abort_transaction(trans, ret);
3341 return ret;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003342 }
3343
Yan, Zhengd68fc572010-05-16 10:49:58 -04003344 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003345}
3346
3347/*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003348 * We have done the delete so we can go ahead and remove the orphan item for
3349 * this particular inode.
Josef Bacik7b128762008-07-24 12:17:14 -04003350 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00003351static int btrfs_orphan_del(struct btrfs_trans_handle *trans,
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003352 struct btrfs_inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003353{
Omar Sandoval27919062018-05-11 13:13:37 -07003354 return btrfs_del_orphan_item(trans, inode->root, btrfs_ino(inode));
Josef Bacik7b128762008-07-24 12:17:14 -04003355}
3356
3357/*
3358 * this cleans up any orphans that may be left on the list from the last use
3359 * of this root.
3360 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003361int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04003362{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003363 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik7b128762008-07-24 12:17:14 -04003364 struct btrfs_path *path;
3365 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04003366 struct btrfs_key key, found_key;
3367 struct btrfs_trans_handle *trans;
3368 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003369 u64 last_objectid = 0;
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003370 int ret = 0, nr_unlink = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003371
Yan, Zhengd68fc572010-05-16 10:49:58 -04003372 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003373 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00003374
3375 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003376 if (!path) {
3377 ret = -ENOMEM;
3378 goto out;
3379 }
David Sterbae4058b52015-11-27 16:31:35 +01003380 path->reada = READA_BACK;
Josef Bacik7b128762008-07-24 12:17:14 -04003381
3382 key.objectid = BTRFS_ORPHAN_OBJECTID;
David Sterba962a2982014-06-04 18:41:45 +02003383 key.type = BTRFS_ORPHAN_ITEM_KEY;
Josef Bacik7b128762008-07-24 12:17:14 -04003384 key.offset = (u64)-1;
3385
Josef Bacik7b128762008-07-24 12:17:14 -04003386 while (1) {
3387 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003388 if (ret < 0)
3389 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003390
3391 /*
3392 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003393 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04003394 * find the key and see if we have stuff that matches
3395 */
3396 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003397 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003398 if (path->slots[0] == 0)
3399 break;
3400 path->slots[0]--;
3401 }
3402
3403 /* pull out the item */
3404 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04003405 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3406
3407 /* make sure the item matches what we want */
3408 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
3409 break;
David Sterba962a2982014-06-04 18:41:45 +02003410 if (found_key.type != BTRFS_ORPHAN_ITEM_KEY)
Josef Bacik7b128762008-07-24 12:17:14 -04003411 break;
3412
3413 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02003414 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04003415
3416 /*
3417 * this is where we are basically btrfs_lookup, without the
3418 * crossing root thing. we store the inode number in the
3419 * offset of the orphan item.
3420 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003421
3422 if (found_key.offset == last_objectid) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003423 btrfs_err(fs_info,
3424 "Error removing orphan entry, stopping orphan cleanup");
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003425 ret = -EINVAL;
3426 goto out;
3427 }
3428
3429 last_objectid = found_key.offset;
3430
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003431 found_key.objectid = found_key.offset;
3432 found_key.type = BTRFS_INODE_ITEM_KEY;
3433 found_key.offset = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003434 inode = btrfs_iget(fs_info->sb, &found_key, root, NULL);
Rusty Russell8c6ffba2013-07-15 11:20:32 +09303435 ret = PTR_ERR_OR_ZERO(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003436 if (ret && ret != -ENOENT)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003437 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003438
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003439 if (ret == -ENOENT && root == fs_info->tree_root) {
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003440 struct btrfs_root *dead_root;
3441 struct btrfs_fs_info *fs_info = root->fs_info;
3442 int is_dead_root = 0;
3443
3444 /*
3445 * this is an orphan in the tree root. Currently these
3446 * could come from 2 sources:
3447 * a) a snapshot deletion in progress
3448 * b) a free space cache inode
3449 * We need to distinguish those two, as the snapshot
3450 * orphan must not get deleted.
3451 * find_dead_roots already ran before us, so if this
3452 * is a snapshot deletion, we should find the root
3453 * in the dead_roots list
3454 */
3455 spin_lock(&fs_info->trans_lock);
3456 list_for_each_entry(dead_root, &fs_info->dead_roots,
3457 root_list) {
3458 if (dead_root->root_key.objectid ==
3459 found_key.objectid) {
3460 is_dead_root = 1;
3461 break;
3462 }
3463 }
3464 spin_unlock(&fs_info->trans_lock);
3465 if (is_dead_root) {
3466 /* prevent this orphan from being found again */
3467 key.offset = found_key.objectid - 1;
3468 continue;
3469 }
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003470
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003471 }
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003472
Josef Bacika8c9e572011-09-21 16:55:59 -04003473 /*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003474 * If we have an inode with links, there are a couple of
3475 * possibilities. Old kernels (before v3.12) used to create an
3476 * orphan item for truncate indicating that there were possibly
3477 * extent items past i_size that needed to be deleted. In v3.12,
3478 * truncate was changed to update i_size in sync with the extent
3479 * items, but the (useless) orphan item was still created. Since
3480 * v4.18, we don't create the orphan item for truncate at all.
3481 *
3482 * So, this item could mean that we need to do a truncate, but
3483 * only if this filesystem was last used on a pre-v3.12 kernel
3484 * and was not cleanly unmounted. The odds of that are quite
3485 * slim, and it's a pain to do the truncate now, so just delete
3486 * the orphan item.
3487 *
3488 * It's also possible that this orphan item was supposed to be
3489 * deleted but wasn't. The inode number may have been reused,
3490 * but either way, we can delete the orphan item.
Josef Bacika8c9e572011-09-21 16:55:59 -04003491 */
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003492 if (ret == -ENOENT || inode->i_nlink) {
3493 if (!ret)
3494 iput(inode);
Josef Bacika8c9e572011-09-21 16:55:59 -04003495 trans = btrfs_start_transaction(root, 1);
3496 if (IS_ERR(trans)) {
3497 ret = PTR_ERR(trans);
3498 goto out;
3499 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003500 btrfs_debug(fs_info, "auto deleting %Lu",
3501 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003502 ret = btrfs_del_orphan_item(trans, root,
3503 found_key.objectid);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003504 btrfs_end_transaction(trans);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003505 if (ret)
3506 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003507 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003508 }
Josef Bacik7b128762008-07-24 12:17:14 -04003509
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003510 nr_unlink++;
Josef Bacik7b128762008-07-24 12:17:14 -04003511
3512 /* this will do delete_inode and everything for us */
3513 iput(inode);
3514 }
Miao Xie3254c872011-11-10 20:45:05 -05003515 /* release the path since we're done with it */
3516 btrfs_release_path(path);
3517
Yan, Zhengd68fc572010-05-16 10:49:58 -04003518 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
3519
Omar Sandovala575cee2018-05-11 13:13:38 -07003520 if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003521 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003522 if (!IS_ERR(trans))
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003523 btrfs_end_transaction(trans);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003524 }
Josef Bacik7b128762008-07-24 12:17:14 -04003525
3526 if (nr_unlink)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003527 btrfs_debug(fs_info, "unlinked %d orphans", nr_unlink);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003528
3529out:
3530 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003531 btrfs_err(fs_info, "could not do orphan cleanup %d", ret);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003532 btrfs_free_path(path);
3533 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003534}
3535
Chris Masond352ac62008-09-29 15:18:18 -04003536/*
Chris Mason46a53cc2009-04-27 11:47:50 -04003537 * very simple check to peek ahead in the leaf looking for xattrs. If we
3538 * don't find any xattrs, we know there can't be any acls.
3539 *
3540 * slot is the slot the inode is in, objectid is the objectid of the inode
3541 */
3542static noinline int acls_after_inode_item(struct extent_buffer *leaf,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003543 int slot, u64 objectid,
3544 int *first_xattr_slot)
Chris Mason46a53cc2009-04-27 11:47:50 -04003545{
3546 u32 nritems = btrfs_header_nritems(leaf);
3547 struct btrfs_key found_key;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003548 static u64 xattr_access = 0;
3549 static u64 xattr_default = 0;
Chris Mason46a53cc2009-04-27 11:47:50 -04003550 int scanned = 0;
3551
Josef Bacikf23b5a52013-06-19 10:16:26 -04003552 if (!xattr_access) {
Andreas Gruenbacher97d79292015-12-02 14:44:35 +01003553 xattr_access = btrfs_name_hash(XATTR_NAME_POSIX_ACL_ACCESS,
3554 strlen(XATTR_NAME_POSIX_ACL_ACCESS));
3555 xattr_default = btrfs_name_hash(XATTR_NAME_POSIX_ACL_DEFAULT,
3556 strlen(XATTR_NAME_POSIX_ACL_DEFAULT));
Josef Bacikf23b5a52013-06-19 10:16:26 -04003557 }
3558
Chris Mason46a53cc2009-04-27 11:47:50 -04003559 slot++;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003560 *first_xattr_slot = -1;
Chris Mason46a53cc2009-04-27 11:47:50 -04003561 while (slot < nritems) {
3562 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3563
3564 /* we found a different objectid, there must not be acls */
3565 if (found_key.objectid != objectid)
3566 return 0;
3567
3568 /* we found an xattr, assume we've got an acl */
Josef Bacikf23b5a52013-06-19 10:16:26 -04003569 if (found_key.type == BTRFS_XATTR_ITEM_KEY) {
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003570 if (*first_xattr_slot == -1)
3571 *first_xattr_slot = slot;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003572 if (found_key.offset == xattr_access ||
3573 found_key.offset == xattr_default)
3574 return 1;
3575 }
Chris Mason46a53cc2009-04-27 11:47:50 -04003576
3577 /*
3578 * we found a key greater than an xattr key, there can't
3579 * be any acls later on
3580 */
3581 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
3582 return 0;
3583
3584 slot++;
3585 scanned++;
3586
3587 /*
3588 * it goes inode, inode backrefs, xattrs, extents,
3589 * so if there are a ton of hard links to an inode there can
3590 * be a lot of backrefs. Don't waste time searching too hard,
3591 * this is just an optimization
3592 */
3593 if (scanned >= 8)
3594 break;
3595 }
3596 /* we hit the end of the leaf before we found an xattr or
3597 * something larger than an xattr. We have to assume the inode
3598 * has acls
3599 */
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003600 if (*first_xattr_slot == -1)
3601 *first_xattr_slot = slot;
Chris Mason46a53cc2009-04-27 11:47:50 -04003602 return 1;
3603}
3604
3605/*
Chris Masond352ac62008-09-29 15:18:18 -04003606 * read an inode from the btree into the in-memory inode
3607 */
Filipe Manana4222ea72018-10-24 10:13:03 +01003608static int btrfs_read_locked_inode(struct inode *inode,
3609 struct btrfs_path *in_path)
Chris Mason39279cc2007-06-12 06:35:45 -04003610{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003611 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Filipe Manana4222ea72018-10-24 10:13:03 +01003612 struct btrfs_path *path = in_path;
Chris Mason5f39d392007-10-15 16:14:19 -04003613 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003614 struct btrfs_inode_item *inode_item;
3615 struct btrfs_root *root = BTRFS_I(inode)->root;
3616 struct btrfs_key location;
Miao Xie67de1172013-12-26 13:07:06 +08003617 unsigned long ptr;
Chris Mason46a53cc2009-04-27 11:47:50 -04003618 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04003619 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04003620 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003621 bool filled = false;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003622 int first_xattr_slot;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003623
3624 ret = btrfs_fill_inode(inode, &rdev);
3625 if (!ret)
3626 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04003627
Filipe Manana4222ea72018-10-24 10:13:03 +01003628 if (!path) {
3629 path = btrfs_alloc_path();
3630 if (!path)
3631 return -ENOMEM;
3632 }
Mark Fasheh1748f842011-07-12 11:25:31 -07003633
Chris Mason39279cc2007-06-12 06:35:45 -04003634 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05003635
Chris Mason39279cc2007-06-12 06:35:45 -04003636 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Filipe Manana67710892016-06-06 11:51:25 +01003637 if (ret) {
Filipe Manana4222ea72018-10-24 10:13:03 +01003638 if (path != in_path)
3639 btrfs_free_path(path);
Al Virof5b3a412018-07-29 23:04:51 +01003640 return ret;
Filipe Manana67710892016-06-06 11:51:25 +01003641 }
Chris Mason39279cc2007-06-12 06:35:45 -04003642
Chris Mason5f39d392007-10-15 16:14:19 -04003643 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00003644
3645 if (filled)
Miao Xie67de1172013-12-26 13:07:06 +08003646 goto cache_index;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003647
Chris Mason5f39d392007-10-15 16:14:19 -04003648 inode_item = btrfs_item_ptr(leaf, path->slots[0],
3649 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003650 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02003651 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08003652 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
3653 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02003654 btrfs_i_size_write(BTRFS_I(inode), btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04003655
David Sterbaa937b972014-12-12 17:39:12 +01003656 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->atime);
3657 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->atime);
Chris Mason5f39d392007-10-15 16:14:19 -04003658
David Sterbaa937b972014-12-12 17:39:12 +01003659 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->mtime);
3660 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->mtime);
Chris Mason5f39d392007-10-15 16:14:19 -04003661
David Sterbaa937b972014-12-12 17:39:12 +01003662 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->ctime);
3663 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->ctime);
Chris Mason5f39d392007-10-15 16:14:19 -04003664
chandan r9cc97d62012-07-04 12:48:07 +05303665 BTRFS_I(inode)->i_otime.tv_sec =
3666 btrfs_timespec_sec(leaf, &inode_item->otime);
3667 BTRFS_I(inode)->i_otime.tv_nsec =
3668 btrfs_timespec_nsec(leaf, &inode_item->otime);
Chris Mason5f39d392007-10-15 16:14:19 -04003669
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003670 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003671 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003672 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
3673
Jeff Laytonc7f88c42017-12-11 06:35:12 -05003674 inode_set_iversion_queried(inode,
3675 btrfs_inode_sequence(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003676 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04003677 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003678 rdev = btrfs_inode_rdev(leaf, inode_item);
3679
Josef Bacikaec74772008-07-24 12:12:38 -04003680 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05003681 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Miao Xie67de1172013-12-26 13:07:06 +08003682
3683cache_index:
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003684 /*
3685 * If we were modified in the current generation and evicted from memory
3686 * and then re-read we need to do a full sync since we don't have any
3687 * idea about which extents were modified before we were evicted from
3688 * cache.
3689 *
3690 * This is required for both inode re-read from disk and delayed inode
3691 * in delayed_nodes_tree.
3692 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003693 if (BTRFS_I(inode)->last_trans == fs_info->generation)
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003694 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3695 &BTRFS_I(inode)->runtime_flags);
3696
Filipe Mananabde6c242015-07-24 00:00:19 +01003697 /*
3698 * We don't persist the id of the transaction where an unlink operation
3699 * against the inode was last made. So here we assume the inode might
3700 * have been evicted, and therefore the exact value of last_unlink_trans
3701 * lost, and set it to last_trans to avoid metadata inconsistencies
3702 * between the inode and its parent if the inode is fsync'ed and the log
3703 * replayed. For example, in the scenario:
3704 *
3705 * touch mydir/foo
3706 * ln mydir/foo mydir/bar
3707 * sync
3708 * unlink mydir/bar
3709 * echo 2 > /proc/sys/vm/drop_caches # evicts inode
3710 * xfs_io -c fsync mydir/foo
3711 * <power failure>
3712 * mount fs, triggers fsync log replay
3713 *
3714 * We must make sure that when we fsync our inode foo we also log its
3715 * parent inode, otherwise after log replay the parent still has the
3716 * dentry with the "bar" name but our inode foo has a link count of 1
3717 * and doesn't have an inode ref with the name "bar" anymore.
3718 *
3719 * Setting last_unlink_trans to last_trans is a pessimistic approach,
Nicholas D Steeves01327612016-05-19 21:18:45 -04003720 * but it guarantees correctness at the expense of occasional full
Filipe Mananabde6c242015-07-24 00:00:19 +01003721 * transaction commits on fsync if our inode is a directory, or if our
3722 * inode is not a directory, logging its parent unnecessarily.
3723 */
3724 BTRFS_I(inode)->last_unlink_trans = BTRFS_I(inode)->last_trans;
3725
Miao Xie67de1172013-12-26 13:07:06 +08003726 path->slots[0]++;
3727 if (inode->i_nlink != 1 ||
3728 path->slots[0] >= btrfs_header_nritems(leaf))
3729 goto cache_acl;
3730
3731 btrfs_item_key_to_cpu(leaf, &location, path->slots[0]);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003732 if (location.objectid != btrfs_ino(BTRFS_I(inode)))
Miao Xie67de1172013-12-26 13:07:06 +08003733 goto cache_acl;
3734
3735 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3736 if (location.type == BTRFS_INODE_REF_KEY) {
3737 struct btrfs_inode_ref *ref;
3738
3739 ref = (struct btrfs_inode_ref *)ptr;
3740 BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref);
3741 } else if (location.type == BTRFS_INODE_EXTREF_KEY) {
3742 struct btrfs_inode_extref *extref;
3743
3744 extref = (struct btrfs_inode_extref *)ptr;
3745 BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf,
3746 extref);
3747 }
Miao Xie2f7e33d2011-06-23 07:27:13 +00003748cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04003749 /*
3750 * try to precache a NULL acl entry for files that don't have
3751 * any xattrs or acls
3752 */
Li Zefan33345d012011-04-20 10:31:50 +08003753 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
David Sterbaf85b7372017-01-20 14:54:07 +01003754 btrfs_ino(BTRFS_I(inode)), &first_xattr_slot);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003755 if (first_xattr_slot != -1) {
3756 path->slots[0] = first_xattr_slot;
3757 ret = btrfs_load_inode_props(inode, path);
3758 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003759 btrfs_err(fs_info,
David Sterba351fd352014-05-15 16:48:20 +02003760 "error loading props for ino %llu (root %llu): %d",
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003761 btrfs_ino(BTRFS_I(inode)),
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003762 root->root_key.objectid, ret);
3763 }
Filipe Manana4222ea72018-10-24 10:13:03 +01003764 if (path != in_path)
3765 btrfs_free_path(path);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003766
Al Viro72c04902009-06-24 16:58:48 -04003767 if (!maybe_acls)
3768 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04003769
Chris Mason39279cc2007-06-12 06:35:45 -04003770 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04003771 case S_IFREG:
3772 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masond1310b22008-01-24 16:13:08 -05003773 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04003774 inode->i_fop = &btrfs_file_operations;
3775 inode->i_op = &btrfs_file_inode_operations;
3776 break;
3777 case S_IFDIR:
3778 inode->i_fop = &btrfs_dir_file_operations;
Omar Sandoval67ade052017-01-25 17:06:38 -08003779 inode->i_op = &btrfs_dir_inode_operations;
Chris Mason39279cc2007-06-12 06:35:45 -04003780 break;
3781 case S_IFLNK:
3782 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -05003783 inode_nohighmem(inode);
Omar Sandoval4779cc02018-09-24 15:16:55 -07003784 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason39279cc2007-06-12 06:35:45 -04003785 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04003786 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05003787 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04003788 init_special_inode(inode, inode->i_mode, rdev);
3789 break;
Chris Mason39279cc2007-06-12 06:35:45 -04003790 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003791
David Sterba7b6a2212018-03-26 18:40:21 +02003792 btrfs_sync_inode_flags_to_i_flags(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003793 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003794}
3795
Chris Masond352ac62008-09-29 15:18:18 -04003796/*
3797 * given a leaf and an inode, copy the inode fields into the leaf
3798 */
Chris Masone02119d2008-09-05 16:13:11 -04003799static void fill_inode_item(struct btrfs_trans_handle *trans,
3800 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04003801 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04003802 struct inode *inode)
3803{
Liu Bo51fab692012-12-27 09:01:21 +00003804 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003805
Liu Bo51fab692012-12-27 09:01:21 +00003806 btrfs_init_map_token(&token);
Chris Mason5f39d392007-10-15 16:14:19 -04003807
Liu Bo51fab692012-12-27 09:01:21 +00003808 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
3809 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
3810 btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size,
3811 &token);
3812 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3813 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003814
David Sterbaa937b972014-12-12 17:39:12 +01003815 btrfs_set_token_timespec_sec(leaf, &item->atime,
Liu Bo51fab692012-12-27 09:01:21 +00003816 inode->i_atime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003817 btrfs_set_token_timespec_nsec(leaf, &item->atime,
Liu Bo51fab692012-12-27 09:01:21 +00003818 inode->i_atime.tv_nsec, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003819
David Sterbaa937b972014-12-12 17:39:12 +01003820 btrfs_set_token_timespec_sec(leaf, &item->mtime,
Liu Bo51fab692012-12-27 09:01:21 +00003821 inode->i_mtime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003822 btrfs_set_token_timespec_nsec(leaf, &item->mtime,
Liu Bo51fab692012-12-27 09:01:21 +00003823 inode->i_mtime.tv_nsec, &token);
3824
David Sterbaa937b972014-12-12 17:39:12 +01003825 btrfs_set_token_timespec_sec(leaf, &item->ctime,
Liu Bo51fab692012-12-27 09:01:21 +00003826 inode->i_ctime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003827 btrfs_set_token_timespec_nsec(leaf, &item->ctime,
Liu Bo51fab692012-12-27 09:01:21 +00003828 inode->i_ctime.tv_nsec, &token);
3829
chandan r9cc97d62012-07-04 12:48:07 +05303830 btrfs_set_token_timespec_sec(leaf, &item->otime,
3831 BTRFS_I(inode)->i_otime.tv_sec, &token);
3832 btrfs_set_token_timespec_nsec(leaf, &item->otime,
3833 BTRFS_I(inode)->i_otime.tv_nsec, &token);
3834
Liu Bo51fab692012-12-27 09:01:21 +00003835 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3836 &token);
3837 btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation,
3838 &token);
Jeff Laytonc7f88c42017-12-11 06:35:12 -05003839 btrfs_set_token_inode_sequence(leaf, item, inode_peek_iversion(inode),
3840 &token);
Liu Bo51fab692012-12-27 09:01:21 +00003841 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
3842 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3843 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3844 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
Chris Mason39279cc2007-06-12 06:35:45 -04003845}
3846
Chris Masond352ac62008-09-29 15:18:18 -04003847/*
3848 * copy everything in the in-memory inode into the btree.
3849 */
Chris Mason21151332011-11-10 20:39:08 -05003850static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Chris Masond3977122009-01-05 21:25:51 -05003851 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003852{
3853 struct btrfs_inode_item *inode_item;
3854 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003855 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003856 int ret;
3857
3858 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08003859 if (!path)
3860 return -ENOMEM;
3861
Chris Masonb9473432009-03-13 11:00:37 -04003862 path->leave_spinning = 1;
Miao Xie16cdcec2011-04-22 18:12:22 +08003863 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
3864 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003865 if (ret) {
3866 if (ret > 0)
3867 ret = -ENOENT;
3868 goto failed;
3869 }
3870
Chris Mason5f39d392007-10-15 16:14:19 -04003871 leaf = path->nodes[0];
3872 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08003873 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04003874
Chris Masone02119d2008-09-05 16:13:11 -04003875 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04003876 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04003877 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003878 ret = 0;
3879failed:
Chris Mason39279cc2007-06-12 06:35:45 -04003880 btrfs_free_path(path);
3881 return ret;
3882}
3883
Chris Masond352ac62008-09-29 15:18:18 -04003884/*
Chris Mason21151332011-11-10 20:39:08 -05003885 * copy everything in the in-memory inode into the btree.
3886 */
3887noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
3888 struct btrfs_root *root, struct inode *inode)
3889{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003890 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason21151332011-11-10 20:39:08 -05003891 int ret;
3892
3893 /*
3894 * If the inode is a free space inode, we can deadlock during commit
3895 * if we put it into the delayed code.
3896 *
3897 * The data relocation inode should also be directly updated
3898 * without delay
3899 */
Nikolay Borisov70ddc552017-02-20 13:50:35 +02003900 if (!btrfs_is_free_space_inode(BTRFS_I(inode))
Josef Bacik1d52c782014-09-18 11:30:44 -04003901 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003902 && !test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) {
Alexander Block8ea05e32012-07-25 17:35:53 +02003903 btrfs_update_root_times(trans, root);
3904
Chris Mason21151332011-11-10 20:39:08 -05003905 ret = btrfs_delayed_update_inode(trans, root, inode);
3906 if (!ret)
3907 btrfs_set_inode_last_trans(trans, inode);
3908 return ret;
3909 }
3910
3911 return btrfs_update_inode_item(trans, root, inode);
3912}
3913
Josef Bacikbe6aef62012-10-22 15:43:12 -04003914noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
3915 struct btrfs_root *root,
3916 struct inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05003917{
3918 int ret;
3919
3920 ret = btrfs_update_inode(trans, root, inode);
3921 if (ret == -ENOSPC)
3922 return btrfs_update_inode_item(trans, root, inode);
3923 return ret;
3924}
3925
3926/*
Chris Masond352ac62008-09-29 15:18:18 -04003927 * unlink helper that gets used here in inode.c and in the tree logging
3928 * recovery code. It remove a link in a directory with a given name, and
3929 * also drops the back refs in the inode to the directory
3930 */
Al Viro92986792011-03-04 17:14:37 +00003931static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3932 struct btrfs_root *root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003933 struct btrfs_inode *dir,
3934 struct btrfs_inode *inode,
Al Viro92986792011-03-04 17:14:37 +00003935 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04003936{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003937 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04003938 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04003939 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003940 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003941 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04003942 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04003943 u64 index;
Li Zefan33345d012011-04-20 10:31:50 +08003944 u64 ino = btrfs_ino(inode);
3945 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04003946
3947 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04003948 if (!path) {
3949 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00003950 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04003951 }
3952
Chris Masonb9473432009-03-13 11:00:37 -04003953 path->leave_spinning = 1;
Li Zefan33345d012011-04-20 10:31:50 +08003954 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04003955 name, name_len, -1);
Liu Bo3cf50682018-09-12 06:06:26 +08003956 if (IS_ERR_OR_NULL(di)) {
3957 ret = di ? PTR_ERR(di) : -ENOENT;
Chris Mason39279cc2007-06-12 06:35:45 -04003958 goto err;
3959 }
Chris Mason5f39d392007-10-15 16:14:19 -04003960 leaf = path->nodes[0];
3961 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04003962 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04003963 if (ret)
3964 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02003965 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003966
Miao Xie67de1172013-12-26 13:07:06 +08003967 /*
3968 * If we don't have dir index, we have to get it by looking up
3969 * the inode ref, since we get the inode ref, remove it directly,
3970 * it is unnecessary to do delayed deletion.
3971 *
3972 * But if we have dir index, needn't search inode ref to get it.
3973 * Since the inode ref is close to the inode item, it is better
3974 * that we delay to delete it, and just do this deletion when
3975 * we update the inode item.
3976 */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003977 if (inode->dir_index) {
Miao Xie67de1172013-12-26 13:07:06 +08003978 ret = btrfs_delayed_delete_inode_ref(inode);
3979 if (!ret) {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003980 index = inode->dir_index;
Miao Xie67de1172013-12-26 13:07:06 +08003981 goto skip_backref;
3982 }
3983 }
3984
Li Zefan33345d012011-04-20 10:31:50 +08003985 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
3986 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04003987 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003988 btrfs_info(fs_info,
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003989 "failed to delete reference to %.*s, inode %llu parent %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003990 name_len, name, ino, dir_ino);
Jeff Mahoney66642832016-06-10 18:19:25 -04003991 btrfs_abort_transaction(trans, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04003992 goto err;
3993 }
Miao Xie67de1172013-12-26 13:07:06 +08003994skip_backref:
Lu Fengqi9add2942018-08-01 11:32:26 +08003995 ret = btrfs_delete_delayed_dir_index(trans, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003996 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003997 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003998 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003999 }
Chris Mason39279cc2007-06-12 06:35:45 -04004000
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004001 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len, inode,
4002 dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004003 if (ret != 0 && ret != -ENOENT) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004004 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004005 goto err;
4006 }
Chris Masone02119d2008-09-05 16:13:11 -04004007
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004008 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len, dir,
4009 index);
Chris Mason6418c962010-10-30 07:34:24 -04004010 if (ret == -ENOENT)
4011 ret = 0;
Zach Brownd4e39912013-04-02 21:02:16 +00004012 else if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004013 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04004014err:
4015 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04004016 if (ret)
4017 goto out;
4018
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004019 btrfs_i_size_write(dir, dir->vfs_inode.i_size - name_len * 2);
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004020 inode_inc_iversion(&inode->vfs_inode);
4021 inode_inc_iversion(&dir->vfs_inode);
4022 inode->vfs_inode.i_ctime = dir->vfs_inode.i_mtime =
4023 dir->vfs_inode.i_ctime = current_time(&inode->vfs_inode);
4024 ret = btrfs_update_inode(trans, root, &dir->vfs_inode);
Chris Masone02119d2008-09-05 16:13:11 -04004025out:
Chris Mason39279cc2007-06-12 06:35:45 -04004026 return ret;
4027}
4028
Al Viro92986792011-03-04 17:14:37 +00004029int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
4030 struct btrfs_root *root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004031 struct btrfs_inode *dir, struct btrfs_inode *inode,
Al Viro92986792011-03-04 17:14:37 +00004032 const char *name, int name_len)
4033{
4034 int ret;
4035 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
4036 if (!ret) {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004037 drop_nlink(&inode->vfs_inode);
4038 ret = btrfs_update_inode(trans, root, &inode->vfs_inode);
Al Viro92986792011-03-04 17:14:37 +00004039 }
4040 return ret;
4041}
Yan, Zhenga22285a2010-05-16 10:48:46 -04004042
4043/*
4044 * helper to start transaction for unlink and rmdir.
4045 *
Josef Bacikd52be812013-05-29 14:54:47 -04004046 * unlink and rmdir are special in btrfs, they do not always free space, so
4047 * if we cannot make our reservations the normal way try and see if there is
4048 * plenty of slack room in the global reserve to migrate, otherwise we cannot
4049 * allow the unlink to occur.
Yan, Zhenga22285a2010-05-16 10:48:46 -04004050 */
Josef Bacikd52be812013-05-29 14:54:47 -04004051static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir)
Yan, Zhenga22285a2010-05-16 10:48:46 -04004052{
Yan, Zhenga22285a2010-05-16 10:48:46 -04004053 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004054
Josef Bacike70bea52011-10-11 14:18:24 -04004055 /*
4056 * 1 for the possible orphan item
4057 * 1 for the dir item
4058 * 1 for the dir index
4059 * 1 for the inode ref
Josef Bacike70bea52011-10-11 14:18:24 -04004060 * 1 for the inode
4061 */
Filipe Manana8eab77f2015-11-13 23:57:16 +00004062 return btrfs_start_transaction_fallback_global_rsv(root, 5, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004063}
4064
Chris Mason39279cc2007-06-12 06:35:45 -04004065static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
4066{
Yan, Zhenga22285a2010-05-16 10:48:46 -04004067 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004068 struct btrfs_trans_handle *trans;
David Howells2b0143b2015-03-17 22:25:59 +00004069 struct inode *inode = d_inode(dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04004070 int ret;
4071
Josef Bacikd52be812013-05-29 14:54:47 -04004072 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004073 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004074 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04004075
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004076 btrfs_record_unlink_dir(trans, BTRFS_I(dir), BTRFS_I(d_inode(dentry)),
4077 0);
Chris Mason12fcfd22009-03-24 10:24:20 -04004078
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004079 ret = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
4080 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
4081 dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00004082 if (ret)
4083 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004084
Yan, Zhenga22285a2010-05-16 10:48:46 -04004085 if (inode->i_nlink == 0) {
Nikolay Borisov73f2e542017-02-20 13:50:59 +02004086 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Tsutomu Itohb5324022011-07-19 07:27:20 +00004087 if (ret)
4088 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004089 }
Josef Bacik7b128762008-07-24 12:17:14 -04004090
Tsutomu Itohb5324022011-07-19 07:27:20 +00004091out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004092 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004093 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04004094 return ret;
4095}
4096
Misono Tomohirof60a2362018-04-18 11:34:52 +09004097static int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
Lu Fengqi401b3b12018-08-01 11:32:30 +08004098 struct inode *dir, u64 objectid,
4099 const char *name, int name_len)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004100{
Lu Fengqi401b3b12018-08-01 11:32:30 +08004101 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004102 struct btrfs_path *path;
4103 struct extent_buffer *leaf;
4104 struct btrfs_dir_item *di;
4105 struct btrfs_key key;
4106 u64 index;
4107 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004108 u64 dir_ino = btrfs_ino(BTRFS_I(dir));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004109
4110 path = btrfs_alloc_path();
4111 if (!path)
4112 return -ENOMEM;
4113
Li Zefan33345d012011-04-20 10:31:50 +08004114 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004115 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004116 if (IS_ERR_OR_NULL(di)) {
Liu Bo3cf50682018-09-12 06:06:26 +08004117 ret = di ? PTR_ERR(di) : -ENOENT;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004118 goto out;
4119 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004120
4121 leaf = path->nodes[0];
4122 btrfs_dir_item_key_to_cpu(leaf, di, &key);
4123 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
4124 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004125 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004126 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004127 goto out;
4128 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004129 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004130
Lu Fengqi3ee1c552018-08-01 11:32:28 +08004131 ret = btrfs_del_root_ref(trans, objectid, root->root_key.objectid,
4132 dir_ino, &index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004133 if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004134 if (ret != -ENOENT) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004135 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004136 goto out;
4137 }
Li Zefan33345d012011-04-20 10:31:50 +08004138 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004139 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004140 if (IS_ERR_OR_NULL(di)) {
4141 if (!di)
4142 ret = -ENOENT;
4143 else
4144 ret = PTR_ERR(di);
Jeff Mahoney66642832016-06-10 18:19:25 -04004145 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004146 goto out;
4147 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004148
4149 leaf = path->nodes[0];
4150 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004151 index = key.offset;
4152 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004153 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004154
Lu Fengqi9add2942018-08-01 11:32:26 +08004155 ret = btrfs_delete_delayed_dir_index(trans, BTRFS_I(dir), index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004156 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004157 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004158 goto out;
4159 }
Miao Xie16cdcec2011-04-22 18:12:22 +08004160
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004161 btrfs_i_size_write(BTRFS_I(dir), dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004162 inode_inc_iversion(dir);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07004163 dir->i_mtime = dir->i_ctime = current_time(dir);
Josef Bacik5a24e842012-08-08 10:12:59 -06004164 ret = btrfs_update_inode_fallback(trans, root, dir);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004165 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004166 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004167out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04004168 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004169 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004170}
4171
Misono Tomohiroec42f162018-04-18 11:34:13 +09004172/*
4173 * Helper to check if the subvolume references other subvolumes or if it's
4174 * default.
4175 */
Misono Tomohirof60a2362018-04-18 11:34:52 +09004176static noinline int may_destroy_subvol(struct btrfs_root *root)
Misono Tomohiroec42f162018-04-18 11:34:13 +09004177{
4178 struct btrfs_fs_info *fs_info = root->fs_info;
4179 struct btrfs_path *path;
4180 struct btrfs_dir_item *di;
4181 struct btrfs_key key;
4182 u64 dir_id;
4183 int ret;
4184
4185 path = btrfs_alloc_path();
4186 if (!path)
4187 return -ENOMEM;
4188
4189 /* Make sure this root isn't set as the default subvol */
4190 dir_id = btrfs_super_root_dir(fs_info->super_copy);
4191 di = btrfs_lookup_dir_item(NULL, fs_info->tree_root, path,
4192 dir_id, "default", 7, 0);
4193 if (di && !IS_ERR(di)) {
4194 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key);
4195 if (key.objectid == root->root_key.objectid) {
4196 ret = -EPERM;
4197 btrfs_err(fs_info,
4198 "deleting default subvolume %llu is not allowed",
4199 key.objectid);
4200 goto out;
4201 }
4202 btrfs_release_path(path);
4203 }
4204
4205 key.objectid = root->root_key.objectid;
4206 key.type = BTRFS_ROOT_REF_KEY;
4207 key.offset = (u64)-1;
4208
4209 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
4210 if (ret < 0)
4211 goto out;
4212 BUG_ON(ret == 0);
4213
4214 ret = 0;
4215 if (path->slots[0] > 0) {
4216 path->slots[0]--;
4217 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
4218 if (key.objectid == root->root_key.objectid &&
4219 key.type == BTRFS_ROOT_REF_KEY)
4220 ret = -ENOTEMPTY;
4221 }
4222out:
4223 btrfs_free_path(path);
4224 return ret;
4225}
4226
Nikolay Borisov20a68002018-04-27 14:36:24 +03004227/* Delete all dentries for inodes belonging to the root */
4228static void btrfs_prune_dentries(struct btrfs_root *root)
4229{
4230 struct btrfs_fs_info *fs_info = root->fs_info;
4231 struct rb_node *node;
4232 struct rb_node *prev;
4233 struct btrfs_inode *entry;
4234 struct inode *inode;
4235 u64 objectid = 0;
4236
4237 if (!test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
4238 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
4239
4240 spin_lock(&root->inode_lock);
4241again:
4242 node = root->inode_tree.rb_node;
4243 prev = NULL;
4244 while (node) {
4245 prev = node;
4246 entry = rb_entry(node, struct btrfs_inode, rb_node);
4247
David Sterba37508512018-06-29 10:56:40 +02004248 if (objectid < btrfs_ino(entry))
Nikolay Borisov20a68002018-04-27 14:36:24 +03004249 node = node->rb_left;
David Sterba37508512018-06-29 10:56:40 +02004250 else if (objectid > btrfs_ino(entry))
Nikolay Borisov20a68002018-04-27 14:36:24 +03004251 node = node->rb_right;
4252 else
4253 break;
4254 }
4255 if (!node) {
4256 while (prev) {
4257 entry = rb_entry(prev, struct btrfs_inode, rb_node);
David Sterba37508512018-06-29 10:56:40 +02004258 if (objectid <= btrfs_ino(entry)) {
Nikolay Borisov20a68002018-04-27 14:36:24 +03004259 node = prev;
4260 break;
4261 }
4262 prev = rb_next(prev);
4263 }
4264 }
4265 while (node) {
4266 entry = rb_entry(node, struct btrfs_inode, rb_node);
David Sterba37508512018-06-29 10:56:40 +02004267 objectid = btrfs_ino(entry) + 1;
Nikolay Borisov20a68002018-04-27 14:36:24 +03004268 inode = igrab(&entry->vfs_inode);
4269 if (inode) {
4270 spin_unlock(&root->inode_lock);
4271 if (atomic_read(&inode->i_count) > 1)
4272 d_prune_aliases(inode);
4273 /*
4274 * btrfs_drop_inode will have it removed from the inode
4275 * cache when its usage count hits zero.
4276 */
4277 iput(inode);
4278 cond_resched();
4279 spin_lock(&root->inode_lock);
4280 goto again;
4281 }
4282
4283 if (cond_resched_lock(&root->inode_lock))
4284 goto again;
4285
4286 node = rb_next(node);
4287 }
4288 spin_unlock(&root->inode_lock);
4289}
4290
Misono Tomohirof60a2362018-04-18 11:34:52 +09004291int btrfs_delete_subvolume(struct inode *dir, struct dentry *dentry)
4292{
4293 struct btrfs_fs_info *fs_info = btrfs_sb(dentry->d_sb);
4294 struct btrfs_root *root = BTRFS_I(dir)->root;
4295 struct inode *inode = d_inode(dentry);
4296 struct btrfs_root *dest = BTRFS_I(inode)->root;
4297 struct btrfs_trans_handle *trans;
4298 struct btrfs_block_rsv block_rsv;
4299 u64 root_flags;
Misono Tomohirof60a2362018-04-18 11:34:52 +09004300 int ret;
4301 int err;
4302
4303 /*
4304 * Don't allow to delete a subvolume with send in progress. This is
4305 * inside the inode lock so the error handling that has to drop the bit
4306 * again is not run concurrently.
4307 */
4308 spin_lock(&dest->root_item_lock);
Lu Fengqia7176f72018-08-04 21:10:53 +08004309 if (dest->send_in_progress) {
Misono Tomohirof60a2362018-04-18 11:34:52 +09004310 spin_unlock(&dest->root_item_lock);
4311 btrfs_warn(fs_info,
4312 "attempt to delete subvolume %llu during send",
4313 dest->root_key.objectid);
4314 return -EPERM;
4315 }
Lu Fengqia7176f72018-08-04 21:10:53 +08004316 root_flags = btrfs_root_flags(&dest->root_item);
4317 btrfs_set_root_flags(&dest->root_item,
4318 root_flags | BTRFS_ROOT_SUBVOL_DEAD);
4319 spin_unlock(&dest->root_item_lock);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004320
4321 down_write(&fs_info->subvol_sem);
4322
4323 err = may_destroy_subvol(dest);
4324 if (err)
4325 goto out_up_write;
4326
4327 btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP);
4328 /*
4329 * One for dir inode,
4330 * two for dir entries,
4331 * two for root ref/backref.
4332 */
Gu JinXiangc4c129d2018-05-30 11:00:38 +08004333 err = btrfs_subvolume_reserve_metadata(root, &block_rsv, 5, true);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004334 if (err)
4335 goto out_up_write;
4336
4337 trans = btrfs_start_transaction(root, 0);
4338 if (IS_ERR(trans)) {
4339 err = PTR_ERR(trans);
4340 goto out_release;
4341 }
4342 trans->block_rsv = &block_rsv;
4343 trans->bytes_reserved = block_rsv.size;
4344
4345 btrfs_record_snapshot_destroy(trans, BTRFS_I(dir));
4346
Lu Fengqi401b3b12018-08-01 11:32:30 +08004347 ret = btrfs_unlink_subvol(trans, dir, dest->root_key.objectid,
4348 dentry->d_name.name, dentry->d_name.len);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004349 if (ret) {
4350 err = ret;
4351 btrfs_abort_transaction(trans, ret);
4352 goto out_end_trans;
4353 }
4354
4355 btrfs_record_root_in_trans(trans, dest);
4356
4357 memset(&dest->root_item.drop_progress, 0,
4358 sizeof(dest->root_item.drop_progress));
4359 dest->root_item.drop_level = 0;
4360 btrfs_set_root_refs(&dest->root_item, 0);
4361
4362 if (!test_and_set_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &dest->state)) {
4363 ret = btrfs_insert_orphan_item(trans,
4364 fs_info->tree_root,
4365 dest->root_key.objectid);
4366 if (ret) {
4367 btrfs_abort_transaction(trans, ret);
4368 err = ret;
4369 goto out_end_trans;
4370 }
4371 }
4372
Lu Fengqid1957792018-05-29 15:01:54 +08004373 ret = btrfs_uuid_tree_remove(trans, dest->root_item.uuid,
Misono Tomohirof60a2362018-04-18 11:34:52 +09004374 BTRFS_UUID_KEY_SUBVOL,
4375 dest->root_key.objectid);
4376 if (ret && ret != -ENOENT) {
4377 btrfs_abort_transaction(trans, ret);
4378 err = ret;
4379 goto out_end_trans;
4380 }
4381 if (!btrfs_is_empty_uuid(dest->root_item.received_uuid)) {
Lu Fengqid1957792018-05-29 15:01:54 +08004382 ret = btrfs_uuid_tree_remove(trans,
Misono Tomohirof60a2362018-04-18 11:34:52 +09004383 dest->root_item.received_uuid,
4384 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
4385 dest->root_key.objectid);
4386 if (ret && ret != -ENOENT) {
4387 btrfs_abort_transaction(trans, ret);
4388 err = ret;
4389 goto out_end_trans;
4390 }
4391 }
4392
4393out_end_trans:
4394 trans->block_rsv = NULL;
4395 trans->bytes_reserved = 0;
4396 ret = btrfs_end_transaction(trans);
4397 if (ret && !err)
4398 err = ret;
4399 inode->i_flags |= S_DEAD;
4400out_release:
4401 btrfs_subvolume_release_metadata(fs_info, &block_rsv);
4402out_up_write:
4403 up_write(&fs_info->subvol_sem);
4404 if (err) {
4405 spin_lock(&dest->root_item_lock);
4406 root_flags = btrfs_root_flags(&dest->root_item);
4407 btrfs_set_root_flags(&dest->root_item,
4408 root_flags & ~BTRFS_ROOT_SUBVOL_DEAD);
4409 spin_unlock(&dest->root_item_lock);
4410 } else {
4411 d_invalidate(dentry);
Nikolay Borisov20a68002018-04-27 14:36:24 +03004412 btrfs_prune_dentries(dest);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004413 ASSERT(dest->send_in_progress == 0);
4414
4415 /* the last ref */
4416 if (dest->ino_cache_inode) {
4417 iput(dest->ino_cache_inode);
4418 dest->ino_cache_inode = NULL;
4419 }
4420 }
4421
4422 return err;
4423}
4424
Chris Mason39279cc2007-06-12 06:35:45 -04004425static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
4426{
David Howells2b0143b2015-03-17 22:25:59 +00004427 struct inode *inode = d_inode(dentry);
Chris Mason1832a6d2007-12-21 16:27:21 -05004428 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004429 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004430 struct btrfs_trans_handle *trans;
Filipe Manana44f714d2016-06-06 16:11:13 +01004431 u64 last_unlink_trans;
Chris Mason39279cc2007-06-12 06:35:45 -04004432
David Sterbab3ae2442012-09-13 16:04:34 -06004433 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Yan134d4512007-10-25 15:49:25 -04004434 return -ENOTEMPTY;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004435 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_FIRST_FREE_OBJECTID)
Misono Tomohiroa79a4642018-04-18 11:35:31 +09004436 return btrfs_delete_subvolume(dir, dentry);
Yan134d4512007-10-25 15:49:25 -04004437
Josef Bacikd52be812013-05-29 14:54:47 -04004438 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004439 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004440 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004441
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004442 if (unlikely(btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Lu Fengqi401b3b12018-08-01 11:32:30 +08004443 err = btrfs_unlink_subvol(trans, dir,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004444 BTRFS_I(inode)->location.objectid,
4445 dentry->d_name.name,
4446 dentry->d_name.len);
4447 goto out;
4448 }
4449
Nikolay Borisov73f2e542017-02-20 13:50:59 +02004450 err = btrfs_orphan_add(trans, BTRFS_I(inode));
Josef Bacik7b128762008-07-24 12:17:14 -04004451 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004452 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004453
Filipe Manana44f714d2016-06-06 16:11:13 +01004454 last_unlink_trans = BTRFS_I(inode)->last_unlink_trans;
4455
Chris Mason39279cc2007-06-12 06:35:45 -04004456 /* now the directory is empty */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004457 err = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
4458 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
4459 dentry->d_name.len);
Filipe Manana44f714d2016-06-06 16:11:13 +01004460 if (!err) {
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004461 btrfs_i_size_write(BTRFS_I(inode), 0);
Filipe Manana44f714d2016-06-06 16:11:13 +01004462 /*
4463 * Propagate the last_unlink_trans value of the deleted dir to
4464 * its parent directory. This is to prevent an unrecoverable
4465 * log tree in the case we do something like this:
4466 * 1) create dir foo
4467 * 2) create snapshot under dir foo
4468 * 3) delete the snapshot
4469 * 4) rmdir foo
4470 * 5) mkdir foo
4471 * 6) fsync foo or some file inside foo
4472 */
4473 if (last_unlink_trans >= trans->transid)
4474 BTRFS_I(dir)->last_unlink_trans = last_unlink_trans;
4475 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004476out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004477 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004478 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39544012007-12-12 14:38:19 -05004479
Chris Mason39279cc2007-06-12 06:35:45 -04004480 return err;
4481}
4482
Josef Bacikddfae632017-10-19 14:16:02 -04004483/*
4484 * Return this if we need to call truncate_block for the last bit of the
4485 * truncate.
4486 */
4487#define NEED_TRUNCATE_BLOCK 1
Filipe Manana0305cd52015-10-16 12:34:25 +01004488
Chris Mason323ac952008-10-01 19:05:46 -04004489/*
Chris Mason39279cc2007-06-12 06:35:45 -04004490 * this can truncate away extent items, csum items and directory items.
4491 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04004492 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04004493 *
4494 * csum items that cross the new i_size are truncated to the new size
4495 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04004496 *
4497 * min_type is the minimum key type to truncate down to. If set to 0, this
4498 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04004499 */
Yan, Zheng80825102009-11-12 09:35:36 +00004500int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
4501 struct btrfs_root *root,
4502 struct inode *inode,
4503 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004504{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004505 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04004506 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004507 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004508 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00004509 struct btrfs_key key;
4510 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04004511 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04004512 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004513 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004514 u64 item_end = 0;
Filipe Mananac1aa4572015-06-20 18:20:09 +01004515 u64 last_size = new_size;
Yan, Zheng80825102009-11-12 09:35:36 +00004516 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04004517 int found_extent;
4518 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05004519 int pending_del_nr = 0;
4520 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04004521 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00004522 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004523 u64 ino = btrfs_ino(BTRFS_I(inode));
Chris Mason28ed1342014-12-17 09:41:04 -08004524 u64 bytes_deleted = 0;
Thomas Meyer897ca812017-10-07 16:02:21 +02004525 bool be_nice = false;
4526 bool should_throttle = false;
Yan, Zheng80825102009-11-12 09:35:36 +00004527
4528 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04004529
Chris Mason28ed1342014-12-17 09:41:04 -08004530 /*
4531 * for non-free space inodes and ref cows, we want to back off from
4532 * time to time
4533 */
Nikolay Borisov70ddc552017-02-20 13:50:35 +02004534 if (!btrfs_is_free_space_inode(BTRFS_I(inode)) &&
Chris Mason28ed1342014-12-17 09:41:04 -08004535 test_bit(BTRFS_ROOT_REF_COWS, &root->state))
Thomas Meyer897ca812017-10-07 16:02:21 +02004536 be_nice = true;
Chris Mason28ed1342014-12-17 09:41:04 -08004537
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004538 path = btrfs_alloc_path();
4539 if (!path)
4540 return -ENOMEM;
David Sterbae4058b52015-11-27 16:31:35 +01004541 path->reada = READA_BACK;
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004542
Josef Bacik5dc562c2012-08-17 13:14:17 -04004543 /*
4544 * We want to drop from the next block forward in case this new size is
4545 * not block aligned since we will be keeping the last block of the
4546 * extent just the way it is.
4547 */
Miao Xie27cdeb72014-04-02 19:51:05 +08004548 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004549 root == fs_info->tree_root)
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02004550 btrfs_drop_extent_cache(BTRFS_I(inode), ALIGN(new_size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004551 fs_info->sectorsize),
Jeff Mahoneyda170662016-06-15 09:22:56 -04004552 (u64)-1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00004553
Miao Xie16cdcec2011-04-22 18:12:22 +08004554 /*
4555 * This function is also used to drop the items in the log tree before
4556 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
Andrea Gelmini52042d82018-11-28 12:05:13 +01004557 * it is used to drop the logged items. So we shouldn't kill the delayed
Miao Xie16cdcec2011-04-22 18:12:22 +08004558 * items.
4559 */
4560 if (min_type == 0 && root == BTRFS_I(inode)->root)
Nikolay Borisov4ccb5c72017-01-10 20:35:38 +02004561 btrfs_kill_delayed_inode_items(BTRFS_I(inode));
Miao Xie16cdcec2011-04-22 18:12:22 +08004562
Li Zefan33345d012011-04-20 10:31:50 +08004563 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04004564 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04004565 key.type = (u8)-1;
4566
Chris Mason85e21ba2008-01-29 15:11:36 -05004567search_again:
Chris Mason28ed1342014-12-17 09:41:04 -08004568 /*
4569 * with a 16K leaf size and 128MB extents, you can actually queue
4570 * up a huge file in a single leaf. Most of the time that
4571 * bytes_deleted is > 0, it will be huge by the time we get here
4572 */
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004573 if (be_nice && bytes_deleted > SZ_32M &&
4574 btrfs_should_end_transaction(trans)) {
4575 ret = -EAGAIN;
Yan, Zheng80825102009-11-12 09:35:36 +00004576 goto out;
4577 }
Chris Masond3977122009-01-05 21:25:51 -05004578
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004579 path->leave_spinning = 1;
4580 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
4581 if (ret < 0)
4582 goto out;
4583
Chris Mason85e21ba2008-01-29 15:11:36 -05004584 if (ret > 0) {
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004585 ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -04004586 /* there are no items in the tree for us to truncate, we're
4587 * done
4588 */
Yan, Zheng80825102009-11-12 09:35:36 +00004589 if (path->slots[0] == 0)
4590 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05004591 path->slots[0]--;
4592 }
4593
Chris Masond3977122009-01-05 21:25:51 -05004594 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04004595 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04004596 leaf = path->nodes[0];
4597 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02004598 found_type = found_key.type;
Chris Mason39279cc2007-06-12 06:35:45 -04004599
Li Zefan33345d012011-04-20 10:31:50 +08004600 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04004601 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004602
Chris Mason85e21ba2008-01-29 15:11:36 -05004603 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004604 break;
4605
Chris Mason5f39d392007-10-15 16:14:19 -04004606 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04004607 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04004608 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04004609 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04004610 extent_type = btrfs_file_extent_type(leaf, fi);
4611 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04004612 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04004613 btrfs_file_extent_num_bytes(leaf, fi);
Liu Bo09ed2f12017-03-10 11:09:48 -08004614
4615 trace_btrfs_truncate_show_fi_regular(
4616 BTRFS_I(inode), leaf, fi,
4617 found_key.offset);
Chris Mason179e29e2007-11-01 11:28:41 -04004618 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Qu Wenruoe41ca582018-06-06 15:41:49 +08004619 item_end += btrfs_file_extent_ram_bytes(leaf,
4620 fi);
Liu Bo09ed2f12017-03-10 11:09:48 -08004621
4622 trace_btrfs_truncate_show_fi_inline(
4623 BTRFS_I(inode), leaf, fi, path->slots[0],
4624 found_key.offset);
Chris Mason39279cc2007-06-12 06:35:45 -04004625 }
Yan008630c2007-11-07 13:31:09 -05004626 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04004627 }
Yan, Zheng80825102009-11-12 09:35:36 +00004628 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04004629 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00004630 } else {
Filipe Manana76b42ab2017-02-14 16:56:01 +00004631 if (item_end < new_size)
Yan, Zheng80825102009-11-12 09:35:36 +00004632 break;
4633 if (found_key.offset >= new_size)
4634 del_item = 1;
4635 else
4636 del_item = 0;
4637 }
Chris Mason39279cc2007-06-12 06:35:45 -04004638 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004639 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04004640 if (found_type != BTRFS_EXTENT_DATA_KEY)
4641 goto delete;
4642
4643 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04004644 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04004645 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05004646 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04004647 u64 orig_num_bytes =
4648 btrfs_file_extent_num_bytes(leaf, fi);
Qu Wenruofda28322013-02-26 08:10:22 +00004649 extent_num_bytes = ALIGN(new_size -
4650 found_key.offset,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004651 fs_info->sectorsize);
Chris Masondb945352007-10-15 16:15:53 -04004652 btrfs_set_file_extent_num_bytes(leaf, fi,
4653 extent_num_bytes);
4654 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05004655 extent_num_bytes);
Miao Xie27cdeb72014-04-02 19:51:05 +08004656 if (test_bit(BTRFS_ROOT_REF_COWS,
4657 &root->state) &&
4658 extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004659 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04004660 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004661 } else {
Chris Masondb945352007-10-15 16:15:53 -04004662 extent_num_bytes =
4663 btrfs_file_extent_disk_num_bytes(leaf,
4664 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004665 extent_offset = found_key.offset -
4666 btrfs_file_extent_offset(leaf, fi);
4667
Chris Mason39279cc2007-06-12 06:35:45 -04004668 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05004669 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004670 if (extent_start != 0) {
4671 found_extent = 1;
Miao Xie27cdeb72014-04-02 19:51:05 +08004672 if (test_bit(BTRFS_ROOT_REF_COWS,
4673 &root->state))
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004674 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04004675 }
4676 }
Chris Mason90692182008-02-08 13:49:28 -05004677 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04004678 /*
4679 * we can't truncate inline items that have had
4680 * special encodings
4681 */
4682 if (!del_item &&
Chris Masonc8b97812008-10-29 14:49:59 -04004683 btrfs_file_extent_encryption(leaf, fi) == 0 &&
Josef Bacikddfae632017-10-19 14:16:02 -04004684 btrfs_file_extent_other_encoding(leaf, fi) == 0 &&
4685 btrfs_file_extent_compression(leaf, fi) == 0) {
4686 u32 size = (u32)(new_size - found_key.offset);
Chris Mason514ac8a2014-01-03 21:07:00 -08004687
Josef Bacikddfae632017-10-19 14:16:02 -04004688 btrfs_set_file_extent_ram_bytes(leaf, fi, size);
4689 size = btrfs_file_extent_calc_inline_size(size);
David Sterba78ac4f92019-03-20 14:49:12 +01004690 btrfs_truncate_item(path, size, 1);
Josef Bacikddfae632017-10-19 14:16:02 -04004691 } else if (!del_item) {
Chris Mason514ac8a2014-01-03 21:07:00 -08004692 /*
Josef Bacikddfae632017-10-19 14:16:02 -04004693 * We have to bail so the last_size is set to
4694 * just before this extent.
Chris Mason514ac8a2014-01-03 21:07:00 -08004695 */
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004696 ret = NEED_TRUNCATE_BLOCK;
Josef Bacikddfae632017-10-19 14:16:02 -04004697 break;
Chris Mason90692182008-02-08 13:49:28 -05004698 }
Josef Bacikddfae632017-10-19 14:16:02 -04004699
4700 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state))
4701 inode_sub_bytes(inode, item_end + 1 - new_size);
Chris Mason39279cc2007-06-12 06:35:45 -04004702 }
Chris Mason179e29e2007-11-01 11:28:41 -04004703delete:
Josef Bacikddfae632017-10-19 14:16:02 -04004704 if (del_item)
4705 last_size = found_key.offset;
4706 else
4707 last_size = new_size;
Chris Mason39279cc2007-06-12 06:35:45 -04004708 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05004709 if (!pending_del_nr) {
4710 /* no pending yet, add ourselves */
4711 pending_del_slot = path->slots[0];
4712 pending_del_nr = 1;
4713 } else if (pending_del_nr &&
4714 path->slots[0] + 1 == pending_del_slot) {
4715 /* hop on the pending chunk */
4716 pending_del_nr++;
4717 pending_del_slot = path->slots[0];
4718 } else {
Chris Masond3977122009-01-05 21:25:51 -05004719 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05004720 }
Chris Mason39279cc2007-06-12 06:35:45 -04004721 } else {
4722 break;
4723 }
Thomas Meyer897ca812017-10-07 16:02:21 +02004724 should_throttle = false;
Chris Mason28f75a02015-02-04 06:59:29 -08004725
Miao Xie27cdeb72014-04-02 19:51:05 +08004726 if (found_extent &&
4727 (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004728 root == fs_info->tree_root)) {
Qu Wenruoffd4bb22019-04-04 14:45:36 +08004729 struct btrfs_ref ref = { 0 };
4730
Chris Masonb9473432009-03-13 11:00:37 -04004731 btrfs_set_path_blocking(path);
Chris Mason28ed1342014-12-17 09:41:04 -08004732 bytes_deleted += extent_num_bytes;
Qu Wenruoffd4bb22019-04-04 14:45:36 +08004733
4734 btrfs_init_generic_ref(&ref, BTRFS_DROP_DELAYED_REF,
4735 extent_start, extent_num_bytes, 0);
4736 ref.real_root = root->root_key.objectid;
4737 btrfs_init_data_ref(&ref, btrfs_header_owner(leaf),
4738 ino, extent_offset);
4739 ret = btrfs_free_extent(trans, &ref);
Omar Sandoval05522102018-05-11 13:13:31 -07004740 if (ret) {
4741 btrfs_abort_transaction(trans, ret);
4742 break;
4743 }
Chris Mason28f75a02015-02-04 06:59:29 -08004744 if (be_nice) {
Lu Fengqi7c861622018-10-11 13:40:36 +08004745 if (btrfs_should_throttle_delayed_refs(trans))
Thomas Meyer897ca812017-10-07 16:02:21 +02004746 should_throttle = true;
Chris Mason28f75a02015-02-04 06:59:29 -08004747 }
Chris Mason39279cc2007-06-12 06:35:45 -04004748 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004749
Yan, Zheng80825102009-11-12 09:35:36 +00004750 if (found_type == BTRFS_INODE_ITEM_KEY)
4751 break;
4752
4753 if (path->slots[0] == 0 ||
Josef Bacik12621332015-02-03 07:50:16 -08004754 path->slots[0] != pending_del_slot ||
Josef Bacik28bad212018-12-03 10:20:38 -05004755 should_throttle) {
Yan, Zheng80825102009-11-12 09:35:36 +00004756 if (pending_del_nr) {
4757 ret = btrfs_del_items(trans, root, path,
4758 pending_del_slot,
4759 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004760 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004761 btrfs_abort_transaction(trans, ret);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004762 break;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004763 }
Yan, Zheng80825102009-11-12 09:35:36 +00004764 pending_del_nr = 0;
4765 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004766 btrfs_release_path(path);
Josef Bacik28bad212018-12-03 10:20:38 -05004767
Chris Mason28f75a02015-02-04 06:59:29 -08004768 /*
Josef Bacik28bad212018-12-03 10:20:38 -05004769 * We can generate a lot of delayed refs, so we need to
4770 * throttle every once and a while and make sure we're
4771 * adding enough space to keep up with the work we are
4772 * generating. Since we hold a transaction here we
4773 * can't flush, and we don't want to FLUSH_LIMIT because
4774 * we could have generated too many delayed refs to
4775 * actually allocate, so just bail if we're short and
4776 * let the normal reservation dance happen higher up.
Chris Mason28f75a02015-02-04 06:59:29 -08004777 */
Josef Bacik28bad212018-12-03 10:20:38 -05004778 if (should_throttle) {
4779 ret = btrfs_delayed_refs_rsv_refill(fs_info,
4780 BTRFS_RESERVE_NO_FLUSH);
4781 if (ret) {
4782 ret = -EAGAIN;
4783 break;
4784 }
Chris Mason28f75a02015-02-04 06:59:29 -08004785 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004786 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00004787 } else {
4788 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05004789 }
Chris Mason39279cc2007-06-12 06:35:45 -04004790 }
Yan, Zheng80825102009-11-12 09:35:36 +00004791out:
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004792 if (ret >= 0 && pending_del_nr) {
4793 int err;
4794
4795 err = btrfs_del_items(trans, root, path, pending_del_slot,
Chris Mason85e21ba2008-01-29 15:11:36 -05004796 pending_del_nr);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004797 if (err) {
4798 btrfs_abort_transaction(trans, err);
4799 ret = err;
4800 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004801 }
Filipe Manana76b42ab2017-02-14 16:56:01 +00004802 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
4803 ASSERT(last_size >= new_size);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004804 if (!ret && last_size > new_size)
Filipe Manana76b42ab2017-02-14 16:56:01 +00004805 last_size = new_size;
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004806 btrfs_ordered_update_i_size(inode, last_size, NULL);
Filipe Manana76b42ab2017-02-14 16:56:01 +00004807 }
Chris Mason28ed1342014-12-17 09:41:04 -08004808
Chris Mason39279cc2007-06-12 06:35:45 -04004809 btrfs_free_path(path);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004810 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04004811}
4812
Chris Masona52d9a82007-08-27 16:49:44 -04004813/*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304814 * btrfs_truncate_block - read, zero a chunk and write a block
Josef Bacik2aaa6652012-08-29 14:27:18 -04004815 * @inode - inode that we're zeroing
4816 * @from - the offset to start zeroing
4817 * @len - the length to zero, 0 to zero the entire range respective to the
4818 * offset
4819 * @front - zero up to the offset instead of from the offset on
4820 *
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304821 * This will find the block for the "from" offset and cow the block and zero the
Josef Bacik2aaa6652012-08-29 14:27:18 -04004822 * part we want to zero. This is used with truncate and hole punching.
Chris Masona52d9a82007-08-27 16:49:44 -04004823 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304824int btrfs_truncate_block(struct inode *inode, loff_t from, loff_t len,
Josef Bacik2aaa6652012-08-29 14:27:18 -04004825 int front)
Chris Masona52d9a82007-08-27 16:49:44 -04004826{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004827 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004828 struct address_space *mapping = inode->i_mapping;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004829 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4830 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004831 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08004832 struct extent_changeset *data_reserved = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004833 char *kaddr;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004834 u32 blocksize = fs_info->sectorsize;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004835 pgoff_t index = from >> PAGE_SHIFT;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304836 unsigned offset = from & (blocksize - 1);
Chris Masona52d9a82007-08-27 16:49:44 -04004837 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004838 gfp_t mask = btrfs_alloc_write_mask(mapping);
Chris Masona52d9a82007-08-27 16:49:44 -04004839 int ret = 0;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304840 u64 block_start;
4841 u64 block_end;
Chris Masona52d9a82007-08-27 16:49:44 -04004842
Nikolay Borisovb03ebd92018-01-18 14:47:06 +02004843 if (IS_ALIGNED(offset, blocksize) &&
4844 (!len || IS_ALIGNED(len, blocksize)))
Chris Masona52d9a82007-08-27 16:49:44 -04004845 goto out;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304846
Josef Bacik8b62f872017-10-19 14:15:55 -04004847 block_start = round_down(from, blocksize);
4848 block_end = block_start + blocksize - 1;
4849
Qu Wenruo364ecf32017-02-27 15:10:38 +08004850 ret = btrfs_delalloc_reserve_space(inode, &data_reserved,
Josef Bacik8b62f872017-10-19 14:15:55 -04004851 block_start, blocksize);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004852 if (ret)
4853 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004854
Chris Mason211c17f2008-05-15 09:13:45 -04004855again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004856 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004857 if (!page) {
Qu Wenruobc42bda2017-02-27 15:10:39 +08004858 btrfs_delalloc_release_space(inode, data_reserved,
Qu Wenruo43b18592017-12-12 15:34:32 +08004859 block_start, blocksize, true);
4860 btrfs_delalloc_release_extents(BTRFS_I(inode), blocksize, true);
Miao Xieac6a2b32012-12-05 10:56:13 +00004861 ret = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -04004862 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04004863 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004864
Chris Masona52d9a82007-08-27 16:49:44 -04004865 if (!PageUptodate(page)) {
4866 ret = btrfs_readpage(NULL, page);
4867 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004868 if (page->mapping != mapping) {
4869 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004870 put_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004871 goto again;
4872 }
Chris Masona52d9a82007-08-27 16:49:44 -04004873 if (!PageUptodate(page)) {
4874 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04004875 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04004876 }
4877 }
Chris Mason211c17f2008-05-15 09:13:45 -04004878 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004879
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304880 lock_extent_bits(io_tree, block_start, block_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004881 set_page_extent_mapped(page);
4882
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304883 ordered = btrfs_lookup_ordered_extent(inode, block_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004884 if (ordered) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304885 unlock_extent_cached(io_tree, block_start, block_end,
David Sterbae43bbe52017-12-12 21:43:52 +01004886 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004887 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004888 put_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04004889 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004890 btrfs_put_ordered_extent(ordered);
4891 goto again;
4892 }
4893
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304894 clear_extent_bit(&BTRFS_I(inode)->io_tree, block_start, block_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06004895 EXTENT_DIRTY | EXTENT_DELALLOC |
4896 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
David Sterbaae0f1622017-10-31 16:37:52 +01004897 0, 0, &cached_state);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004898
Filipe Mananae3b8a482017-11-04 00:16:59 +00004899 ret = btrfs_set_extent_delalloc(inode, block_start, block_end, 0,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08004900 &cached_state, 0);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004901 if (ret) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304902 unlock_extent_cached(io_tree, block_start, block_end,
David Sterbae43bbe52017-12-12 21:43:52 +01004903 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004904 goto out_unlock;
4905 }
4906
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304907 if (offset != blocksize) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04004908 if (!len)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304909 len = blocksize - offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004910 kaddr = kmap(page);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004911 if (front)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304912 memset(kaddr + (block_start - page_offset(page)),
4913 0, offset);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004914 else
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304915 memset(kaddr + (block_start - page_offset(page)) + offset,
4916 0, len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004917 flush_dcache_page(page);
4918 kunmap(page);
4919 }
Chris Mason247e7432008-07-17 12:53:51 -04004920 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004921 set_page_dirty(page);
David Sterbae43bbe52017-12-12 21:43:52 +01004922 unlock_extent_cached(io_tree, block_start, block_end, &cached_state);
Chris Mason39279cc2007-06-12 06:35:45 -04004923
Chris Mason89642222008-07-24 09:41:53 -04004924out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04004925 if (ret)
Qu Wenruobc42bda2017-02-27 15:10:39 +08004926 btrfs_delalloc_release_space(inode, data_reserved, block_start,
Qu Wenruo43b18592017-12-12 15:34:32 +08004927 blocksize, true);
4928 btrfs_delalloc_release_extents(BTRFS_I(inode), blocksize, (ret != 0));
Chris Mason39279cc2007-06-12 06:35:45 -04004929 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004930 put_page(page);
Chris Mason39279cc2007-06-12 06:35:45 -04004931out:
Qu Wenruo364ecf32017-02-27 15:10:38 +08004932 extent_changeset_free(data_reserved);
Chris Mason39279cc2007-06-12 06:35:45 -04004933 return ret;
4934}
4935
Josef Bacik16e75492013-10-22 12:18:51 -04004936static int maybe_insert_hole(struct btrfs_root *root, struct inode *inode,
4937 u64 offset, u64 len)
4938{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004939 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik16e75492013-10-22 12:18:51 -04004940 struct btrfs_trans_handle *trans;
4941 int ret;
4942
4943 /*
4944 * Still need to make sure the inode looks like it's been updated so
4945 * that any holes get logged if we fsync.
4946 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004947 if (btrfs_fs_incompat(fs_info, NO_HOLES)) {
4948 BTRFS_I(inode)->last_trans = fs_info->generation;
Josef Bacik16e75492013-10-22 12:18:51 -04004949 BTRFS_I(inode)->last_sub_trans = root->log_transid;
4950 BTRFS_I(inode)->last_log_commit = root->last_log_commit;
4951 return 0;
4952 }
4953
4954 /*
4955 * 1 - for the one we're dropping
4956 * 1 - for the one we're adding
4957 * 1 - for updating the inode.
4958 */
4959 trans = btrfs_start_transaction(root, 3);
4960 if (IS_ERR(trans))
4961 return PTR_ERR(trans);
4962
4963 ret = btrfs_drop_extents(trans, root, inode, offset, offset + len, 1);
4964 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004965 btrfs_abort_transaction(trans, ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004966 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04004967 return ret;
4968 }
4969
David Sterbaf85b7372017-01-20 14:54:07 +01004970 ret = btrfs_insert_file_extent(trans, root, btrfs_ino(BTRFS_I(inode)),
4971 offset, 0, 0, len, 0, len, 0, 0, 0);
Josef Bacik16e75492013-10-22 12:18:51 -04004972 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004973 btrfs_abort_transaction(trans, ret);
Josef Bacik16e75492013-10-22 12:18:51 -04004974 else
4975 btrfs_update_inode(trans, root, inode);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004976 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04004977 return ret;
4978}
4979
Josef Bacik695a0d02011-03-04 15:46:53 -05004980/*
4981 * This function puts in dummy file extents for the area we're creating a hole
4982 * for. So if we are truncating this file to a larger size we need to insert
4983 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
4984 * the range between oldsize and size
4985 */
Josef Bacika41ad392011-01-31 15:30:16 -05004986int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04004987{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004988 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Yan Zheng9036c102008-10-30 14:19:41 -04004989 struct btrfs_root *root = BTRFS_I(inode)->root;
4990 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004991 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004992 struct extent_state *cached_state = NULL;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004993 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004994 u64 hole_start = ALIGN(oldsize, fs_info->sectorsize);
4995 u64 block_end = ALIGN(size, fs_info->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04004996 u64 last_byte;
4997 u64 cur_offset;
4998 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04004999 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04005000
Josef Bacika71754f2013-06-17 17:14:39 -04005001 /*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05305002 * If our size started in the middle of a block we need to zero out the
5003 * rest of the block before we expand the i_size, otherwise we could
Josef Bacika71754f2013-06-17 17:14:39 -04005004 * expose stale data.
5005 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05305006 err = btrfs_truncate_block(inode, oldsize, 0, 0);
Josef Bacika71754f2013-06-17 17:14:39 -04005007 if (err)
5008 return err;
5009
Yan Zheng9036c102008-10-30 14:19:41 -04005010 if (size <= hole_start)
5011 return 0;
5012
Yan Zheng9036c102008-10-30 14:19:41 -04005013 while (1) {
5014 struct btrfs_ordered_extent *ordered;
Miao Xiefa7c1492013-09-26 13:15:27 +08005015
David Sterbaff13db42015-12-03 14:30:40 +01005016 lock_extent_bits(io_tree, hole_start, block_end - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01005017 &cached_state);
Nikolay Borisova776c6f2017-02-20 13:50:49 +02005018 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), hole_start,
Miao Xiefa7c1492013-09-26 13:15:27 +08005019 block_end - hole_start);
Yan Zheng9036c102008-10-30 14:19:41 -04005020 if (!ordered)
5021 break;
Josef Bacik2ac55d42010-02-03 19:33:23 +00005022 unlock_extent_cached(io_tree, hole_start, block_end - 1,
David Sterbae43bbe52017-12-12 21:43:52 +01005023 &cached_state);
Miao Xiefa7c1492013-09-26 13:15:27 +08005024 btrfs_start_ordered_extent(inode, ordered, 1);
Yan Zheng9036c102008-10-30 14:19:41 -04005025 btrfs_put_ordered_extent(ordered);
5026 }
5027
Yan Zheng9036c102008-10-30 14:19:41 -04005028 cur_offset = hole_start;
5029 while (1) {
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02005030 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, cur_offset,
Yan Zheng9036c102008-10-30 14:19:41 -04005031 block_end - cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005032 if (IS_ERR(em)) {
5033 err = PTR_ERR(em);
Zach Brownf2767952013-01-08 19:37:58 +00005034 em = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005035 break;
5036 }
Yan Zheng9036c102008-10-30 14:19:41 -04005037 last_byte = min(extent_map_end(em), block_end);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005038 last_byte = ALIGN(last_byte, fs_info->sectorsize);
Yan, Zheng80825102009-11-12 09:35:36 +00005039 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04005040 struct extent_map *hole_em;
Yan Zheng9036c102008-10-30 14:19:41 -04005041 hole_size = last_byte - cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00005042
Josef Bacik16e75492013-10-22 12:18:51 -04005043 err = maybe_insert_hole(root, inode, cur_offset,
5044 hole_size);
5045 if (err)
Chris Mason771ed682008-11-06 22:02:51 -05005046 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02005047 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04005048 cur_offset + hole_size - 1, 0);
5049 hole_em = alloc_extent_map();
5050 if (!hole_em) {
5051 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
5052 &BTRFS_I(inode)->runtime_flags);
5053 goto next;
5054 }
5055 hole_em->start = cur_offset;
5056 hole_em->len = hole_size;
5057 hole_em->orig_start = cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00005058
Josef Bacik5dc562c2012-08-17 13:14:17 -04005059 hole_em->block_start = EXTENT_MAP_HOLE;
5060 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05005061 hole_em->orig_block_len = 0;
Josef Bacikcc95bef2013-04-04 14:31:27 -04005062 hole_em->ram_bytes = hole_size;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005063 hole_em->bdev = fs_info->fs_devices->latest_bdev;
Josef Bacik5dc562c2012-08-17 13:14:17 -04005064 hole_em->compress_type = BTRFS_COMPRESS_NONE;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005065 hole_em->generation = fs_info->generation;
Josef Bacik5dc562c2012-08-17 13:14:17 -04005066
5067 while (1) {
5068 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04005069 err = add_extent_mapping(em_tree, hole_em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04005070 write_unlock(&em_tree->lock);
5071 if (err != -EEXIST)
5072 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02005073 btrfs_drop_extent_cache(BTRFS_I(inode),
5074 cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04005075 cur_offset +
5076 hole_size - 1, 0);
5077 }
5078 free_extent_map(hole_em);
Yan Zheng9036c102008-10-30 14:19:41 -04005079 }
Josef Bacik16e75492013-10-22 12:18:51 -04005080next:
Yan Zheng9036c102008-10-30 14:19:41 -04005081 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04005082 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04005083 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00005084 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04005085 break;
5086 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04005087 free_extent_map(em);
David Sterbae43bbe52017-12-12 21:43:52 +01005088 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state);
Yan Zheng9036c102008-10-30 14:19:41 -04005089 return err;
5090}
5091
Eric Sandeen3972f262013-01-12 02:57:22 +00005092static int btrfs_setsize(struct inode *inode, struct iattr *attr)
Yan, Zheng80825102009-11-12 09:35:36 +00005093{
Miao Xief4a2f4c2011-12-14 20:12:01 -05005094 struct btrfs_root *root = BTRFS_I(inode)->root;
5095 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05005096 loff_t oldsize = i_size_read(inode);
Eric Sandeen3972f262013-01-12 02:57:22 +00005097 loff_t newsize = attr->ia_size;
5098 int mask = attr->ia_valid;
Yan, Zheng80825102009-11-12 09:35:36 +00005099 int ret;
5100
Eric Sandeen3972f262013-01-12 02:57:22 +00005101 /*
5102 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
5103 * special case where we need to update the times despite not having
5104 * these flags set. For all other operations the VFS set these flags
5105 * explicitly if it wants a timestamp update.
5106 */
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08005107 if (newsize != oldsize) {
5108 inode_inc_iversion(inode);
5109 if (!(mask & (ATTR_CTIME | ATTR_MTIME)))
5110 inode->i_ctime = inode->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07005111 current_time(inode);
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08005112 }
Eric Sandeen3972f262013-01-12 02:57:22 +00005113
Josef Bacika41ad392011-01-31 15:30:16 -05005114 if (newsize > oldsize) {
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005115 /*
David Sterbaea14b57f2017-06-22 02:19:11 +02005116 * Don't do an expanding truncate while snapshotting is ongoing.
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005117 * This is to ensure the snapshot captures a fully consistent
5118 * state of this file - if the snapshot captures this expanding
5119 * truncation, it must capture all writes that happened before
5120 * this truncation.
5121 */
Zhao Lei0bc19f902016-01-06 18:56:36 +08005122 btrfs_wait_for_snapshot_creation(root);
Josef Bacika41ad392011-01-31 15:30:16 -05005123 ret = btrfs_cont_expand(inode, oldsize, newsize);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005124 if (ret) {
David Sterbaea14b57f2017-06-22 02:19:11 +02005125 btrfs_end_write_no_snapshotting(root);
Yan, Zheng80825102009-11-12 09:35:36 +00005126 return ret;
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005127 }
Yan, Zheng80825102009-11-12 09:35:36 +00005128
Miao Xief4a2f4c2011-12-14 20:12:01 -05005129 trans = btrfs_start_transaction(root, 1);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005130 if (IS_ERR(trans)) {
David Sterbaea14b57f2017-06-22 02:19:11 +02005131 btrfs_end_write_no_snapshotting(root);
Miao Xief4a2f4c2011-12-14 20:12:01 -05005132 return PTR_ERR(trans);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005133 }
Miao Xief4a2f4c2011-12-14 20:12:01 -05005134
5135 i_size_write(inode, newsize);
5136 btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
Chandan Rajendra27772b62016-01-21 15:56:03 +05305137 pagecache_isize_extended(inode, oldsize, newsize);
Miao Xief4a2f4c2011-12-14 20:12:01 -05005138 ret = btrfs_update_inode(trans, root, inode);
David Sterbaea14b57f2017-06-22 02:19:11 +02005139 btrfs_end_write_no_snapshotting(root);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005140 btrfs_end_transaction(trans);
Josef Bacika41ad392011-01-31 15:30:16 -05005141 } else {
Yan, Zheng80825102009-11-12 09:35:36 +00005142
Josef Bacika41ad392011-01-31 15:30:16 -05005143 /*
5144 * We're truncating a file that used to have good data down to
5145 * zero. Make sure it gets into the ordered flush list so that
5146 * any new writes get down to disk quickly.
5147 */
5148 if (newsize == 0)
Josef Bacik72ac3c02012-05-23 14:13:11 -04005149 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
5150 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00005151
Josef Bacika41ad392011-01-31 15:30:16 -05005152 truncate_setsize(inode, newsize);
Miao Xie2e60a512013-02-08 07:01:08 +00005153
Andrea Gelmini52042d82018-11-28 12:05:13 +01005154 /* Disable nonlocked read DIO to avoid the endless truncate */
Nikolay Borisovabcefb12017-02-20 13:51:10 +02005155 btrfs_inode_block_unlocked_dio(BTRFS_I(inode));
Miao Xie2e60a512013-02-08 07:01:08 +00005156 inode_dio_wait(inode);
Nikolay Borisov0b581702017-02-20 13:51:11 +02005157 btrfs_inode_resume_unlocked_dio(BTRFS_I(inode));
Miao Xie2e60a512013-02-08 07:01:08 +00005158
Filipe Manana213e8c52018-02-06 20:40:31 +00005159 ret = btrfs_truncate(inode, newsize == oldsize);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005160 if (ret && inode->i_nlink) {
5161 int err;
5162
5163 /*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07005164 * Truncate failed, so fix up the in-memory size. We
5165 * adjusted disk_i_size down as we removed extents, so
5166 * wait for disk_i_size to be stable and then update the
5167 * in-memory size to match.
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005168 */
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07005169 err = btrfs_wait_ordered_range(inode, 0, (u64)-1);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005170 if (err)
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07005171 return err;
5172 i_size_write(inode, BTRFS_I(inode)->disk_i_size);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005173 }
Yan, Zheng80825102009-11-12 09:35:36 +00005174 }
5175
Josef Bacika41ad392011-01-31 15:30:16 -05005176 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00005177}
5178
Chris Mason39279cc2007-06-12 06:35:45 -04005179static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
5180{
David Howells2b0143b2015-03-17 22:25:59 +00005181 struct inode *inode = d_inode(dentry);
Li Zefanb83cc962010-12-20 16:04:08 +08005182 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005183 int err;
5184
Li Zefanb83cc962010-12-20 16:04:08 +08005185 if (btrfs_root_readonly(root))
5186 return -EROFS;
5187
Jan Kara31051c82016-05-26 16:55:18 +02005188 err = setattr_prepare(dentry, attr);
Chris Mason39279cc2007-06-12 06:35:45 -04005189 if (err)
5190 return err;
5191
Chris Mason5a3f23d2009-03-31 13:27:11 -04005192 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Eric Sandeen3972f262013-01-12 02:57:22 +00005193 err = btrfs_setsize(inode, attr);
Yan, Zheng80825102009-11-12 09:35:36 +00005194 if (err)
5195 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005196 }
Yan Zheng9036c102008-10-30 14:19:41 -04005197
Christoph Hellwig10257742010-06-04 11:30:02 +02005198 if (attr->ia_valid) {
5199 setattr_copy(inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005200 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005201 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04005202
Josef Bacik22c44fe2011-11-30 10:45:38 -05005203 if (!err && attr->ia_valid & ATTR_MODE)
Christoph Hellwig996a7102013-12-20 05:16:43 -08005204 err = posix_acl_chmod(inode, inode->i_mode);
Christoph Hellwig10257742010-06-04 11:30:02 +02005205 }
5206
Chris Mason39279cc2007-06-12 06:35:45 -04005207 return err;
5208}
Chris Mason61295eb2008-01-14 16:24:38 -05005209
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005210/*
5211 * While truncating the inode pages during eviction, we get the VFS calling
5212 * btrfs_invalidatepage() against each page of the inode. This is slow because
5213 * the calls to btrfs_invalidatepage() result in a huge amount of calls to
5214 * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting
5215 * extent_state structures over and over, wasting lots of time.
5216 *
5217 * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all
5218 * those expensive operations on a per page basis and do only the ordered io
5219 * finishing, while we release here the extent_map and extent_state structures,
5220 * without the excessive merging and splitting.
5221 */
5222static void evict_inode_truncate_pages(struct inode *inode)
5223{
5224 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
5225 struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree;
5226 struct rb_node *node;
5227
5228 ASSERT(inode->i_state & I_FREEING);
Johannes Weiner91b0abe2014-04-03 14:47:49 -07005229 truncate_inode_pages_final(&inode->i_data);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005230
5231 write_lock(&map_tree->lock);
Liu Bo07e1ce02018-08-23 03:51:52 +08005232 while (!RB_EMPTY_ROOT(&map_tree->map.rb_root)) {
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005233 struct extent_map *em;
5234
Liu Bo07e1ce02018-08-23 03:51:52 +08005235 node = rb_first_cached(&map_tree->map);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005236 em = rb_entry(node, struct extent_map, rb_node);
Wang Shilong180589e2013-12-14 15:27:31 +08005237 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
5238 clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005239 remove_extent_mapping(map_tree, em);
5240 free_extent_map(em);
Filipe Manana7064dd52014-08-08 02:47:05 +01005241 if (need_resched()) {
5242 write_unlock(&map_tree->lock);
5243 cond_resched();
5244 write_lock(&map_tree->lock);
5245 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005246 }
5247 write_unlock(&map_tree->lock);
5248
Filipe Manana6ca07092015-05-26 00:55:42 +01005249 /*
5250 * Keep looping until we have no more ranges in the io tree.
5251 * We can have ongoing bios started by readpages (called from readahead)
Filipe Manana9c6429d2015-06-10 12:55:41 +01005252 * that have their endio callback (extent_io.c:end_bio_extent_readpage)
5253 * still in progress (unlocked the pages in the bio but did not yet
5254 * unlocked the ranges in the io tree). Therefore this means some
Filipe Manana6ca07092015-05-26 00:55:42 +01005255 * ranges can still be locked and eviction started because before
5256 * submitting those bios, which are executed by a separate task (work
5257 * queue kthread), inode references (inode->i_count) were not taken
5258 * (which would be dropped in the end io callback of each bio).
5259 * Therefore here we effectively end up waiting for those bios and
5260 * anyone else holding locked ranges without having bumped the inode's
5261 * reference count - if we don't do it, when they access the inode's
5262 * io_tree to unlock a range it may be too late, leading to an
5263 * use-after-free issue.
5264 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005265 spin_lock(&io_tree->lock);
5266 while (!RB_EMPTY_ROOT(&io_tree->state)) {
5267 struct extent_state *state;
5268 struct extent_state *cached_state = NULL;
Filipe Manana6ca07092015-05-26 00:55:42 +01005269 u64 start;
5270 u64 end;
Filipe Manana421f0922018-10-12 13:02:48 +01005271 unsigned state_flags;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005272
5273 node = rb_first(&io_tree->state);
5274 state = rb_entry(node, struct extent_state, rb_node);
Filipe Manana6ca07092015-05-26 00:55:42 +01005275 start = state->start;
5276 end = state->end;
Filipe Manana421f0922018-10-12 13:02:48 +01005277 state_flags = state->state;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005278 spin_unlock(&io_tree->lock);
5279
David Sterbaff13db42015-12-03 14:30:40 +01005280 lock_extent_bits(io_tree, start, end, &cached_state);
Qu Wenruob9d0b382015-09-29 10:35:16 +08005281
5282 /*
5283 * If still has DELALLOC flag, the extent didn't reach disk,
5284 * and its reserved space won't be freed by delayed_ref.
5285 * So we need to free its reserved space here.
5286 * (Refer to comment in btrfs_invalidatepage, case 2)
5287 *
5288 * Note, end is the bytenr of last byte, so we need + 1 here.
5289 */
Filipe Manana421f0922018-10-12 13:02:48 +01005290 if (state_flags & EXTENT_DELALLOC)
Qu Wenruobc42bda2017-02-27 15:10:39 +08005291 btrfs_qgroup_free_data(inode, NULL, start, end - start + 1);
Qu Wenruob9d0b382015-09-29 10:35:16 +08005292
Filipe Manana6ca07092015-05-26 00:55:42 +01005293 clear_extent_bit(io_tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005294 EXTENT_LOCKED | EXTENT_DIRTY |
5295 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
David Sterbaae0f1622017-10-31 16:37:52 +01005296 EXTENT_DEFRAG, 1, 1, &cached_state);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005297
Filipe Manana7064dd52014-08-08 02:47:05 +01005298 cond_resched();
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005299 spin_lock(&io_tree->lock);
5300 }
5301 spin_unlock(&io_tree->lock);
5302}
5303
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005304static struct btrfs_trans_handle *evict_refill_and_join(struct btrfs_root *root,
Josef Bacikad80cf52018-09-28 07:18:19 -04005305 struct btrfs_block_rsv *rsv)
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005306{
5307 struct btrfs_fs_info *fs_info = root->fs_info;
5308 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
Josef Bacik260e7702018-11-21 14:03:13 -05005309 u64 delayed_refs_extra = btrfs_calc_trans_metadata_size(fs_info, 1);
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005310 int failures = 0;
5311
5312 for (;;) {
5313 struct btrfs_trans_handle *trans;
5314 int ret;
5315
Josef Bacik260e7702018-11-21 14:03:13 -05005316 ret = btrfs_block_rsv_refill(root, rsv,
5317 rsv->size + delayed_refs_extra,
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005318 BTRFS_RESERVE_FLUSH_LIMIT);
5319
5320 if (ret && ++failures > 2) {
5321 btrfs_warn(fs_info,
5322 "could not allocate space for a delete; will truncate on mount");
5323 return ERR_PTR(-ENOSPC);
5324 }
5325
Josef Bacik260e7702018-11-21 14:03:13 -05005326 /*
5327 * Evict can generate a large amount of delayed refs without
5328 * having a way to add space back since we exhaust our temporary
5329 * block rsv. We aren't allowed to do FLUSH_ALL in this case
5330 * because we could deadlock with so many things in the flushing
5331 * code, so we have to try and hold some extra space to
5332 * compensate for our delayed ref generation. If we can't get
5333 * that space then we need see if we can steal our minimum from
5334 * the global reserve. We will be ratelimited by the amount of
5335 * space we have for the delayed refs rsv, so we'll end up
5336 * committing and trying again.
5337 */
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005338 trans = btrfs_join_transaction(root);
Josef Bacik260e7702018-11-21 14:03:13 -05005339 if (IS_ERR(trans) || !ret) {
5340 if (!IS_ERR(trans)) {
5341 trans->block_rsv = &fs_info->trans_block_rsv;
5342 trans->bytes_reserved = delayed_refs_extra;
5343 btrfs_block_rsv_migrate(rsv, trans->block_rsv,
5344 delayed_refs_extra, 1);
5345 }
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005346 return trans;
Josef Bacik260e7702018-11-21 14:03:13 -05005347 }
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005348
5349 /*
5350 * Try to steal from the global reserve if there is space for
5351 * it.
5352 */
Josef Bacik644036122018-12-03 10:20:36 -05005353 if (!btrfs_check_space_for_delayed_refs(fs_info) &&
5354 !btrfs_block_rsv_migrate(global_rsv, rsv, rsv->size, 0))
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005355 return trans;
5356
5357 /* If not, commit and try again. */
5358 ret = btrfs_commit_transaction(trans);
5359 if (ret)
5360 return ERR_PTR(ret);
5361 }
5362}
5363
Al Virobd555972010-06-07 11:35:40 -04005364void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005365{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005366 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04005367 struct btrfs_trans_handle *trans;
5368 struct btrfs_root *root = BTRFS_I(inode)->root;
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005369 struct btrfs_block_rsv *rsv;
Chris Mason39279cc2007-06-12 06:35:45 -04005370 int ret;
5371
liubo1abe9b82011-03-24 11:18:59 +00005372 trace_btrfs_inode_evict(inode);
5373
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005374 if (!root) {
Liu Boe8f1bc12018-01-25 11:02:53 -07005375 clear_inode(inode);
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005376 return;
5377 }
5378
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005379 evict_inode_truncate_pages(inode);
5380
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005381 if (inode->i_nlink &&
5382 ((btrfs_root_refs(&root->root_item) != 0 &&
5383 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) ||
Nikolay Borisov70ddc552017-02-20 13:50:35 +02005384 btrfs_is_free_space_inode(BTRFS_I(inode))))
Al Virobd555972010-06-07 11:35:40 -04005385 goto no_delete;
5386
Omar Sandoval27919062018-05-11 13:13:37 -07005387 if (is_bad_inode(inode))
Chris Mason39279cc2007-06-12 06:35:45 -04005388 goto no_delete;
Chris Mason5f39d392007-10-15 16:14:19 -04005389
Nikolay Borisov7ab79562017-02-20 13:50:57 +02005390 btrfs_free_io_failure_record(BTRFS_I(inode), 0, (u64)-1);
Miao Xief6124962014-09-12 18:44:04 +08005391
Omar Sandoval7b40b692018-05-11 13:13:33 -07005392 if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags))
Yan, Zhengc71bf092009-11-12 09:34:40 +00005393 goto no_delete;
Yan, Zhengc71bf092009-11-12 09:34:40 +00005394
Yan, Zheng76dda932009-09-21 16:00:26 -04005395 if (inode->i_nlink > 0) {
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005396 BUG_ON(btrfs_root_refs(&root->root_item) != 0 &&
5397 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID);
Yan, Zheng76dda932009-09-21 16:00:26 -04005398 goto no_delete;
5399 }
5400
Nikolay Borisovaa790212017-01-10 20:35:40 +02005401 ret = btrfs_commit_inode_delayed_inode(BTRFS_I(inode));
Omar Sandoval27919062018-05-11 13:13:37 -07005402 if (ret)
Miao Xie0e8c36a2012-12-19 06:59:51 +00005403 goto no_delete;
Miao Xie0e8c36a2012-12-19 06:59:51 +00005404
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005405 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Omar Sandoval27919062018-05-11 13:13:37 -07005406 if (!rsv)
Josef Bacik4289a662011-08-05 13:22:24 -04005407 goto no_delete;
Josef Bacikad80cf52018-09-28 07:18:19 -04005408 rsv->size = btrfs_calc_trunc_metadata_size(fs_info, 1);
Josef Bacikca7e70f2012-08-27 17:48:15 -04005409 rsv->failfast = 1;
Josef Bacik4289a662011-08-05 13:22:24 -04005410
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02005411 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Mason5f39d392007-10-15 16:14:19 -04005412
Yan, Zheng80825102009-11-12 09:35:36 +00005413 while (1) {
Josef Bacikad80cf52018-09-28 07:18:19 -04005414 trans = evict_refill_and_join(root, rsv);
Omar Sandoval27919062018-05-11 13:13:37 -07005415 if (IS_ERR(trans))
5416 goto free_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04005417
5418 trans->block_rsv = rsv;
5419
Yan, Zhengd68fc572010-05-16 10:49:58 -04005420 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Omar Sandoval27919062018-05-11 13:13:37 -07005421 trans->block_rsv = &fs_info->trans_block_rsv;
5422 btrfs_end_transaction(trans);
5423 btrfs_btree_balance_dirty(fs_info);
5424 if (ret && ret != -ENOSPC && ret != -EAGAIN)
5425 goto free_rsv;
5426 else if (!ret)
Yan, Zheng80825102009-11-12 09:35:36 +00005427 break;
Josef Bacik7b128762008-07-24 12:17:14 -04005428 }
5429
Josef Bacik4ef31a42013-08-13 14:10:08 -04005430 /*
Omar Sandoval27919062018-05-11 13:13:37 -07005431 * Errors here aren't a big deal, it just means we leave orphan items in
5432 * the tree. They will be cleaned up on the next mount. If the inode
5433 * number gets reused, cleanup deletes the orphan item without doing
5434 * anything, and unlink reuses the existing orphan item.
5435 *
5436 * If it turns out that we are dropping too many of these, we might want
5437 * to add a mechanism for retrying these after a commit.
Josef Bacik4ef31a42013-08-13 14:10:08 -04005438 */
Josef Bacikad80cf52018-09-28 07:18:19 -04005439 trans = evict_refill_and_join(root, rsv);
Omar Sandoval27919062018-05-11 13:13:37 -07005440 if (!IS_ERR(trans)) {
5441 trans->block_rsv = rsv;
5442 btrfs_orphan_del(trans, BTRFS_I(inode));
5443 trans->block_rsv = &fs_info->trans_block_rsv;
5444 btrfs_end_transaction(trans);
5445 }
Chris Mason85e21ba2008-01-29 15:11:36 -05005446
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005447 if (!(root == fs_info->tree_root ||
Li Zefan581bb052011-04-20 10:06:11 +08005448 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005449 btrfs_return_ino(root, btrfs_ino(BTRFS_I(inode)));
Li Zefan581bb052011-04-20 10:06:11 +08005450
Omar Sandoval27919062018-05-11 13:13:37 -07005451free_rsv:
5452 btrfs_free_block_rsv(fs_info, rsv);
Chris Mason39279cc2007-06-12 06:35:45 -04005453no_delete:
Omar Sandoval27919062018-05-11 13:13:37 -07005454 /*
5455 * If we didn't successfully delete, the orphan item will still be in
5456 * the tree and we'll retry on the next mount. Again, we might also want
5457 * to retry these periodically in the future.
5458 */
Nikolay Borisovf48d1cf2017-01-10 20:35:39 +02005459 btrfs_remove_delayed_node(BTRFS_I(inode));
Jan Karadbd57682012-05-03 14:48:02 +02005460 clear_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005461}
5462
5463/*
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005464 * Return the key found in the dir entry in the location pointer, fill @type
5465 * with BTRFS_FT_*, and return 0.
5466 *
Su Yue005d6712018-03-05 17:13:37 +08005467 * If no dir entries were found, returns -ENOENT.
5468 * If found a corrupted location in dir entry, returns -EUCLEAN.
Chris Mason39279cc2007-06-12 06:35:45 -04005469 */
5470static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005471 struct btrfs_key *location, u8 *type)
Chris Mason39279cc2007-06-12 06:35:45 -04005472{
5473 const char *name = dentry->d_name.name;
5474 int namelen = dentry->d_name.len;
5475 struct btrfs_dir_item *di;
5476 struct btrfs_path *path;
5477 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04005478 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005479
5480 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005481 if (!path)
5482 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05005483
David Sterbaf85b7372017-01-20 14:54:07 +01005484 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(BTRFS_I(dir)),
5485 name, namelen, 0);
Liu Bo3cf50682018-09-12 06:06:26 +08005486 if (IS_ERR_OR_NULL(di)) {
5487 ret = di ? PTR_ERR(di) : -ENOENT;
Su Yue005d6712018-03-05 17:13:37 +08005488 goto out;
5489 }
Chris Masond3977122009-01-05 21:25:51 -05005490
Chris Mason5f39d392007-10-15 16:14:19 -04005491 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Liu Bo56a0e702017-10-30 11:14:38 -06005492 if (location->type != BTRFS_INODE_ITEM_KEY &&
5493 location->type != BTRFS_ROOT_ITEM_KEY) {
Su Yue005d6712018-03-05 17:13:37 +08005494 ret = -EUCLEAN;
Liu Bo56a0e702017-10-30 11:14:38 -06005495 btrfs_warn(root->fs_info,
5496"%s gets something invalid in DIR_ITEM (name %s, directory ino %llu, location(%llu %u %llu))",
5497 __func__, name, btrfs_ino(BTRFS_I(dir)),
5498 location->objectid, location->type, location->offset);
Liu Bo56a0e702017-10-30 11:14:38 -06005499 }
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005500 if (!ret)
5501 *type = btrfs_dir_type(path->nodes[0], di);
Chris Mason39279cc2007-06-12 06:35:45 -04005502out:
Chris Mason39279cc2007-06-12 06:35:45 -04005503 btrfs_free_path(path);
5504 return ret;
5505}
5506
5507/*
5508 * when we hit a tree root in a directory, the btrfs part of the inode
5509 * needs to be changed to reflect the root directory of the tree root. This
5510 * is kind of like crossing a mount point.
5511 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005512static int fixup_tree_root_location(struct btrfs_fs_info *fs_info,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005513 struct inode *dir,
5514 struct dentry *dentry,
5515 struct btrfs_key *location,
5516 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04005517{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005518 struct btrfs_path *path;
5519 struct btrfs_root *new_root;
5520 struct btrfs_root_ref *ref;
5521 struct extent_buffer *leaf;
David Sterba1d4c08e2015-01-02 19:36:14 +01005522 struct btrfs_key key;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005523 int ret;
5524 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005525
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005526 path = btrfs_alloc_path();
5527 if (!path) {
5528 err = -ENOMEM;
5529 goto out;
5530 }
Chris Mason39279cc2007-06-12 06:35:45 -04005531
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005532 err = -ENOENT;
David Sterba1d4c08e2015-01-02 19:36:14 +01005533 key.objectid = BTRFS_I(dir)->root->root_key.objectid;
5534 key.type = BTRFS_ROOT_REF_KEY;
5535 key.offset = location->objectid;
5536
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005537 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005538 if (ret) {
5539 if (ret < 0)
5540 err = ret;
5541 goto out;
5542 }
Chris Mason39279cc2007-06-12 06:35:45 -04005543
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005544 leaf = path->nodes[0];
5545 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005546 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(BTRFS_I(dir)) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005547 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
5548 goto out;
5549
5550 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
5551 (unsigned long)(ref + 1),
5552 dentry->d_name.len);
5553 if (ret)
5554 goto out;
5555
David Sterbab3b4aa72011-04-21 01:20:15 +02005556 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005557
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005558 new_root = btrfs_read_fs_root_no_name(fs_info, location);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005559 if (IS_ERR(new_root)) {
5560 err = PTR_ERR(new_root);
5561 goto out;
5562 }
5563
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005564 *sub_root = new_root;
5565 location->objectid = btrfs_root_dirid(&new_root->root_item);
5566 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04005567 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005568 err = 0;
5569out:
5570 btrfs_free_path(path);
5571 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005572}
5573
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005574static void inode_tree_add(struct inode *inode)
5575{
5576 struct btrfs_root *root = BTRFS_I(inode)->root;
5577 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005578 struct rb_node **p;
5579 struct rb_node *parent;
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005580 struct rb_node *new = &BTRFS_I(inode)->rb_node;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005581 u64 ino = btrfs_ino(BTRFS_I(inode));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005582
Al Viro1d3382cb2010-10-23 15:19:20 -04005583 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005584 return;
Miao Xiee1409ce2013-05-15 07:48:16 +00005585 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005586 spin_lock(&root->inode_lock);
Miao Xiee1409ce2013-05-15 07:48:16 +00005587 p = &root->inode_tree.rb_node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005588 while (*p) {
5589 parent = *p;
5590 entry = rb_entry(parent, struct btrfs_inode, rb_node);
5591
David Sterba37508512018-06-29 10:56:40 +02005592 if (ino < btrfs_ino(entry))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005593 p = &parent->rb_left;
David Sterba37508512018-06-29 10:56:40 +02005594 else if (ino > btrfs_ino(entry))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005595 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005596 else {
5597 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04005598 (I_WILL_FREE | I_FREEING)));
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005599 rb_replace_node(parent, new, &root->inode_tree);
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005600 RB_CLEAR_NODE(parent);
5601 spin_unlock(&root->inode_lock);
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005602 return;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005603 }
5604 }
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005605 rb_link_node(new, parent, p);
5606 rb_insert_color(new, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005607 spin_unlock(&root->inode_lock);
5608}
5609
5610static void inode_tree_del(struct inode *inode)
5611{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005612 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005613 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04005614 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005615
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005616 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005617 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005618 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005619 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04005620 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005621 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005622 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04005623
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005624 if (empty && btrfs_root_refs(&root->root_item) == 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005625 synchronize_srcu(&fs_info->subvol_srcu);
Yan, Zheng76dda932009-09-21 16:00:26 -04005626 spin_lock(&root->inode_lock);
5627 empty = RB_EMPTY_ROOT(&root->inode_tree);
5628 spin_unlock(&root->inode_lock);
5629 if (empty)
5630 btrfs_add_dead_root(root);
5631 }
5632}
5633
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005634
Chris Masone02119d2008-09-05 16:13:11 -04005635static int btrfs_init_locked_inode(struct inode *inode, void *p)
5636{
5637 struct btrfs_iget_args *args = p;
Chris Mason90d3e592014-01-09 17:28:00 -08005638 inode->i_ino = args->location->objectid;
5639 memcpy(&BTRFS_I(inode)->location, args->location,
5640 sizeof(*args->location));
Chris Masone02119d2008-09-05 16:13:11 -04005641 BTRFS_I(inode)->root = args->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005642 return 0;
5643}
5644
5645static int btrfs_find_actor(struct inode *inode, void *opaque)
5646{
5647 struct btrfs_iget_args *args = opaque;
Chris Mason90d3e592014-01-09 17:28:00 -08005648 return args->location->objectid == BTRFS_I(inode)->location.objectid &&
Chris Masond3977122009-01-05 21:25:51 -05005649 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005650}
5651
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005652static struct inode *btrfs_iget_locked(struct super_block *s,
Chris Mason90d3e592014-01-09 17:28:00 -08005653 struct btrfs_key *location,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005654 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04005655{
5656 struct inode *inode;
5657 struct btrfs_iget_args args;
Chris Mason90d3e592014-01-09 17:28:00 -08005658 unsigned long hashval = btrfs_inode_hash(location->objectid, root);
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005659
Chris Mason90d3e592014-01-09 17:28:00 -08005660 args.location = location;
Chris Mason39279cc2007-06-12 06:35:45 -04005661 args.root = root;
5662
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005663 inode = iget5_locked(s, hashval, btrfs_find_actor,
Chris Mason39279cc2007-06-12 06:35:45 -04005664 btrfs_init_locked_inode,
5665 (void *)&args);
5666 return inode;
5667}
5668
Balaji Rao1a54ef82008-07-21 02:01:04 +05305669/* Get an inode object given its location and corresponding root.
5670 * Returns in *is_new if the inode was read from disk
5671 */
Filipe Manana4222ea72018-10-24 10:13:03 +01005672struct inode *btrfs_iget_path(struct super_block *s, struct btrfs_key *location,
5673 struct btrfs_root *root, int *new,
5674 struct btrfs_path *path)
Balaji Rao1a54ef82008-07-21 02:01:04 +05305675{
5676 struct inode *inode;
5677
Chris Mason90d3e592014-01-09 17:28:00 -08005678 inode = btrfs_iget_locked(s, location, root);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305679 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005680 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305681
5682 if (inode->i_state & I_NEW) {
Filipe Manana67710892016-06-06 11:51:25 +01005683 int ret;
5684
Filipe Manana4222ea72018-10-24 10:13:03 +01005685 ret = btrfs_read_locked_inode(inode, path);
Al Viro9bc2cef2018-07-29 23:04:50 +01005686 if (!ret) {
Mark Fasheh1748f842011-07-12 11:25:31 -07005687 inode_tree_add(inode);
5688 unlock_new_inode(inode);
5689 if (new)
5690 *new = 1;
5691 } else {
Al Virof5b3a412018-07-29 23:04:51 +01005692 iget_failed(inode);
5693 /*
5694 * ret > 0 can come from btrfs_search_slot called by
5695 * btrfs_read_locked_inode, this means the inode item
5696 * was not found.
5697 */
5698 if (ret > 0)
5699 ret = -ENOENT;
5700 inode = ERR_PTR(ret);
Mark Fasheh1748f842011-07-12 11:25:31 -07005701 }
5702 }
5703
Balaji Rao1a54ef82008-07-21 02:01:04 +05305704 return inode;
5705}
5706
Filipe Manana4222ea72018-10-24 10:13:03 +01005707struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
5708 struct btrfs_root *root, int *new)
5709{
5710 return btrfs_iget_path(s, location, root, new, NULL);
5711}
5712
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005713static struct inode *new_simple_dir(struct super_block *s,
5714 struct btrfs_key *key,
5715 struct btrfs_root *root)
5716{
5717 struct inode *inode = new_inode(s);
5718
5719 if (!inode)
5720 return ERR_PTR(-ENOMEM);
5721
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005722 BTRFS_I(inode)->root = root;
5723 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04005724 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005725
5726 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Li Zefan848cce02012-02-21 17:04:28 +08005727 inode->i_op = &btrfs_dir_ro_inode_operations;
Omar Sandoval1fdf4192017-01-25 17:06:39 -08005728 inode->i_opflags &= ~IOP_XATTR;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005729 inode->i_fop = &simple_dir_operations;
5730 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
Deepa Dinamanic2050a42016-09-14 07:48:06 -07005731 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05305732 inode->i_atime = inode->i_mtime;
5733 inode->i_ctime = inode->i_mtime;
Arnd Bergmannd3c6be6f2018-06-21 18:04:06 +02005734 BTRFS_I(inode)->i_otime = inode->i_mtime;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005735
5736 return inode;
5737}
5738
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005739static inline u8 btrfs_inode_type(struct inode *inode)
5740{
5741 /*
5742 * Compile-time asserts that generic FT_* types still match
5743 * BTRFS_FT_* types
5744 */
5745 BUILD_BUG_ON(BTRFS_FT_UNKNOWN != FT_UNKNOWN);
5746 BUILD_BUG_ON(BTRFS_FT_REG_FILE != FT_REG_FILE);
5747 BUILD_BUG_ON(BTRFS_FT_DIR != FT_DIR);
5748 BUILD_BUG_ON(BTRFS_FT_CHRDEV != FT_CHRDEV);
5749 BUILD_BUG_ON(BTRFS_FT_BLKDEV != FT_BLKDEV);
5750 BUILD_BUG_ON(BTRFS_FT_FIFO != FT_FIFO);
5751 BUILD_BUG_ON(BTRFS_FT_SOCK != FT_SOCK);
5752 BUILD_BUG_ON(BTRFS_FT_SYMLINK != FT_SYMLINK);
5753
5754 return fs_umode_to_ftype(inode->i_mode);
5755}
5756
Chris Mason3de45862008-11-17 21:02:50 -05005757struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04005758{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005759 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masond3977122009-01-05 21:25:51 -05005760 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005761 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005762 struct btrfs_root *sub_root = root;
5763 struct btrfs_key location;
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005764 u8 di_type = 0;
Yan, Zheng76dda932009-09-21 16:00:26 -04005765 int index;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005766 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005767
5768 if (dentry->d_name.len > BTRFS_NAME_LEN)
5769 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04005770
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005771 ret = btrfs_inode_by_name(dir, dentry, &location, &di_type);
Chris Mason39279cc2007-06-12 06:35:45 -04005772 if (ret < 0)
5773 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04005774
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005775 if (location.type == BTRFS_INODE_ITEM_KEY) {
Josef Bacik73f73412009-12-04 17:38:27 +00005776 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005777 if (IS_ERR(inode))
5778 return inode;
5779
5780 /* Do extra check against inode mode with di_type */
5781 if (btrfs_inode_type(inode) != di_type) {
5782 btrfs_crit(fs_info,
5783"inode mode mismatch with dir: inode mode=0%o btrfs type=%u dir type=%u",
5784 inode->i_mode, btrfs_inode_type(inode),
5785 di_type);
5786 iput(inode);
5787 return ERR_PTR(-EUCLEAN);
5788 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005789 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04005790 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005791
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005792 index = srcu_read_lock(&fs_info->subvol_srcu);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005793 ret = fixup_tree_root_location(fs_info, dir, dentry,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005794 &location, &sub_root);
5795 if (ret < 0) {
5796 if (ret != -ENOENT)
5797 inode = ERR_PTR(ret);
5798 else
5799 inode = new_simple_dir(dir->i_sb, &location, sub_root);
5800 } else {
Josef Bacik73f73412009-12-04 17:38:27 +00005801 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04005802 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005803 srcu_read_unlock(&fs_info->subvol_srcu, index);
Yan, Zheng76dda932009-09-21 16:00:26 -04005804
Julia Lawall34d19ba2011-01-24 19:55:19 +00005805 if (!IS_ERR(inode) && root != sub_root) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005806 down_read(&fs_info->cleanup_work_sem);
David Howellsbc98a422017-07-17 08:45:34 +01005807 if (!sb_rdonly(inode->i_sb))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005808 ret = btrfs_orphan_cleanup(sub_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005809 up_read(&fs_info->cleanup_work_sem);
Josef Bacik01cd3362013-06-03 21:39:49 -04005810 if (ret) {
5811 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005812 inode = ERR_PTR(ret);
Josef Bacik01cd3362013-06-03 21:39:49 -04005813 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00005814 }
5815
Chris Mason3de45862008-11-17 21:02:50 -05005816 return inode;
5817}
5818
Nick Pigginfe15ce42011-01-07 17:49:23 +11005819static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04005820{
5821 struct btrfs_root *root;
David Howells2b0143b2015-03-17 22:25:59 +00005822 struct inode *inode = d_inode(dentry);
Yan, Zheng76dda932009-09-21 16:00:26 -04005823
Li Zefan848cce02012-02-21 17:04:28 +08005824 if (!inode && !IS_ROOT(dentry))
David Howells2b0143b2015-03-17 22:25:59 +00005825 inode = d_inode(dentry->d_parent);
Yan, Zheng76dda932009-09-21 16:00:26 -04005826
Li Zefan848cce02012-02-21 17:04:28 +08005827 if (inode) {
5828 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04005829 if (btrfs_root_refs(&root->root_item) == 0)
5830 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08005831
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005832 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Li Zefan848cce02012-02-21 17:04:28 +08005833 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04005834 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005835 return 0;
5836}
5837
Chris Mason3de45862008-11-17 21:02:50 -05005838static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04005839 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05005840{
Al Viro3837d202018-10-10 16:38:27 -04005841 struct inode *inode = btrfs_lookup_dentry(dir, dentry);
Josef Bacika66e7cc2011-09-18 10:34:03 -04005842
Al Viro3837d202018-10-10 16:38:27 -04005843 if (inode == ERR_PTR(-ENOENT))
5844 inode = NULL;
Al Viro41d28bc2014-10-12 22:24:21 -04005845 return d_splice_alias(inode, dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04005846}
5847
Josef Bacik23b5ec72017-07-24 15:14:25 -04005848/*
5849 * All this infrastructure exists because dir_emit can fault, and we are holding
5850 * the tree lock when doing readdir. For now just allocate a buffer and copy
5851 * our information into that, and then dir_emit from the buffer. This is
5852 * similar to what NFS does, only we don't keep the buffer around in pagecache
5853 * because I'm afraid I'll mess that up. Long term we need to make filldir do
5854 * copy_to_user_inatomic so we don't have to worry about page faulting under the
5855 * tree lock.
5856 */
5857static int btrfs_opendir(struct inode *inode, struct file *file)
5858{
5859 struct btrfs_file_private *private;
5860
5861 private = kzalloc(sizeof(struct btrfs_file_private), GFP_KERNEL);
5862 if (!private)
5863 return -ENOMEM;
5864 private->filldir_buf = kzalloc(PAGE_SIZE, GFP_KERNEL);
5865 if (!private->filldir_buf) {
5866 kfree(private);
5867 return -ENOMEM;
5868 }
5869 file->private_data = private;
5870 return 0;
5871}
5872
5873struct dir_entry {
5874 u64 ino;
5875 u64 offset;
5876 unsigned type;
5877 int name_len;
5878};
5879
5880static int btrfs_filldir(void *addr, int entries, struct dir_context *ctx)
5881{
5882 while (entries--) {
5883 struct dir_entry *entry = addr;
5884 char *name = (char *)(entry + 1);
5885
David Sterba92d32172018-04-16 21:10:14 +02005886 ctx->pos = get_unaligned(&entry->offset);
5887 if (!dir_emit(ctx, name, get_unaligned(&entry->name_len),
5888 get_unaligned(&entry->ino),
5889 get_unaligned(&entry->type)))
Josef Bacik23b5ec72017-07-24 15:14:25 -04005890 return 1;
David Sterba92d32172018-04-16 21:10:14 +02005891 addr += sizeof(struct dir_entry) +
5892 get_unaligned(&entry->name_len);
Josef Bacik23b5ec72017-07-24 15:14:25 -04005893 ctx->pos++;
5894 }
5895 return 0;
5896}
5897
Al Viro9cdda8d2013-05-22 16:48:09 -04005898static int btrfs_real_readdir(struct file *file, struct dir_context *ctx)
Chris Mason39279cc2007-06-12 06:35:45 -04005899{
Al Viro9cdda8d2013-05-22 16:48:09 -04005900 struct inode *inode = file_inode(file);
Chris Mason39279cc2007-06-12 06:35:45 -04005901 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005902 struct btrfs_file_private *private = file->private_data;
Chris Mason39279cc2007-06-12 06:35:45 -04005903 struct btrfs_dir_item *di;
5904 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04005905 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04005906 struct btrfs_path *path;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005907 void *addr;
Miao Xie16cdcec2011-04-22 18:12:22 +08005908 struct list_head ins_list;
5909 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04005910 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04005911 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04005912 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04005913 char *name_ptr;
5914 int name_len;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005915 int entries = 0;
5916 int total_len = 0;
Omar Sandoval02dbfc92016-05-20 13:50:33 -07005917 bool put = false;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005918 struct btrfs_key location;
Chris Mason5f39d392007-10-15 16:14:19 -04005919
Al Viro9cdda8d2013-05-22 16:48:09 -04005920 if (!dir_emit_dots(file, ctx))
5921 return 0;
5922
David Woodhouse49593bf2008-08-17 17:08:36 +01005923 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08005924 if (!path)
5925 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04005926
Josef Bacik23b5ec72017-07-24 15:14:25 -04005927 addr = private->filldir_buf;
David Sterbae4058b52015-11-27 16:31:35 +01005928 path->reada = READA_FORWARD;
David Woodhouse49593bf2008-08-17 17:08:36 +01005929
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005930 INIT_LIST_HEAD(&ins_list);
5931 INIT_LIST_HEAD(&del_list);
5932 put = btrfs_readdir_get_delayed_items(inode, &ins_list, &del_list);
Miao Xie16cdcec2011-04-22 18:12:22 +08005933
Josef Bacik23b5ec72017-07-24 15:14:25 -04005934again:
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005935 key.type = BTRFS_DIR_INDEX_KEY;
Al Viro9cdda8d2013-05-22 16:48:09 -04005936 key.offset = ctx->pos;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005937 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason5f39d392007-10-15 16:14:19 -04005938
Chris Mason39279cc2007-06-12 06:35:45 -04005939 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5940 if (ret < 0)
5941 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01005942
5943 while (1) {
Josef Bacik23b5ec72017-07-24 15:14:25 -04005944 struct dir_entry *entry;
5945
Chris Mason5f39d392007-10-15 16:14:19 -04005946 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04005947 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08005948 if (slot >= btrfs_header_nritems(leaf)) {
5949 ret = btrfs_next_leaf(root, path);
5950 if (ret < 0)
5951 goto err;
5952 else if (ret > 0)
5953 break;
5954 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04005955 }
Chris Mason3de45862008-11-17 21:02:50 -05005956
Chris Mason5f39d392007-10-15 16:14:19 -04005957 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5958
5959 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04005960 break;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005961 if (found_key.type != BTRFS_DIR_INDEX_KEY)
Chris Mason39279cc2007-06-12 06:35:45 -04005962 break;
Al Viro9cdda8d2013-05-22 16:48:09 -04005963 if (found_key.offset < ctx->pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08005964 goto next;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005965 if (btrfs_should_delete_dir_index(&del_list, found_key.offset))
Miao Xie16cdcec2011-04-22 18:12:22 +08005966 goto next;
Chris Mason39279cc2007-06-12 06:35:45 -04005967 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005968 name_len = btrfs_dir_name_len(leaf, di);
Josef Bacik23b5ec72017-07-24 15:14:25 -04005969 if ((total_len + sizeof(struct dir_entry) + name_len) >=
5970 PAGE_SIZE) {
5971 btrfs_release_path(path);
5972 ret = btrfs_filldir(private->filldir_buf, entries, ctx);
5973 if (ret)
5974 goto nopos;
5975 addr = private->filldir_buf;
5976 entries = 0;
5977 total_len = 0;
5978 goto again;
Chris Mason39279cc2007-06-12 06:35:45 -04005979 }
Josef Bacik23b5ec72017-07-24 15:14:25 -04005980
5981 entry = addr;
David Sterba92d32172018-04-16 21:10:14 +02005982 put_unaligned(name_len, &entry->name_len);
Josef Bacik23b5ec72017-07-24 15:14:25 -04005983 name_ptr = (char *)(entry + 1);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005984 read_extent_buffer(leaf, name_ptr, (unsigned long)(di + 1),
5985 name_len);
Phillip Potter7d157c32019-03-26 21:39:34 +00005986 put_unaligned(fs_ftype_to_dtype(btrfs_dir_type(leaf, di)),
David Sterba92d32172018-04-16 21:10:14 +02005987 &entry->type);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005988 btrfs_dir_item_key_to_cpu(leaf, di, &location);
David Sterba92d32172018-04-16 21:10:14 +02005989 put_unaligned(location.objectid, &entry->ino);
5990 put_unaligned(found_key.offset, &entry->offset);
Josef Bacik23b5ec72017-07-24 15:14:25 -04005991 entries++;
5992 addr += sizeof(struct dir_entry) + name_len;
5993 total_len += sizeof(struct dir_entry) + name_len;
Li Zefanb9e03af2011-03-23 10:43:58 +08005994next:
5995 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04005996 }
Josef Bacik23b5ec72017-07-24 15:14:25 -04005997 btrfs_release_path(path);
5998
5999 ret = btrfs_filldir(private->filldir_buf, entries, ctx);
6000 if (ret)
6001 goto nopos;
David Woodhouse49593bf2008-08-17 17:08:36 +01006002
Jeff Mahoneyd2fbb2b2016-11-05 13:26:35 -04006003 ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006004 if (ret)
David Sterbabc4ef752015-11-13 13:44:28 +01006005 goto nopos;
6006
Zach Browndb62efb2013-07-11 16:19:42 -07006007 /*
6008 * Stop new entries from being returned after we return the last
6009 * entry.
6010 *
6011 * New directory entries are assigned a strictly increasing
6012 * offset. This means that new entries created during readdir
6013 * are *guaranteed* to be seen in the future by that readdir.
6014 * This has broken buggy programs which operate on names as
6015 * they're returned by readdir. Until we re-use freed offsets
6016 * we have this hack to stop new entries from being returned
6017 * under the assumption that they'll never reach this huge
6018 * offset.
6019 *
6020 * This is being careful not to overflow 32bit loff_t unless the
6021 * last entry requires it because doing so has broken 32bit apps
6022 * in the past.
6023 */
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006024 if (ctx->pos >= INT_MAX)
6025 ctx->pos = LLONG_MAX;
6026 else
6027 ctx->pos = INT_MAX;
Chris Mason39279cc2007-06-12 06:35:45 -04006028nopos:
6029 ret = 0;
6030err:
Omar Sandoval02dbfc92016-05-20 13:50:33 -07006031 if (put)
6032 btrfs_readdir_put_delayed_items(inode, &ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04006033 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04006034 return ret;
6035}
6036
Chris Mason39279cc2007-06-12 06:35:45 -04006037/*
Chris Mason54aa1f42007-06-22 14:16:25 -04006038 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04006039 * inode changes. But, it is most likely to find the inode in cache.
6040 * FIXME, needs more benchmarking...there are no reasons other than performance
6041 * to keep or drop this code.
6042 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00006043static int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04006044{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006045 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04006046 struct btrfs_root *root = BTRFS_I(inode)->root;
6047 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006048 int ret;
6049
Josef Bacik72ac3c02012-05-23 14:13:11 -04006050 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05006051 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006052
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006053 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006054 if (IS_ERR(trans))
6055 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006056
6057 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04006058 if (ret && ret == -ENOSPC) {
6059 /* whoops, lets try again with the full transaction */
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006060 btrfs_end_transaction(trans);
Chris Mason94b60442010-05-26 11:02:00 -04006061 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006062 if (IS_ERR(trans))
6063 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006064
Chris Mason94b60442010-05-26 11:02:00 -04006065 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04006066 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006067 btrfs_end_transaction(trans);
Miao Xie16cdcec2011-04-22 18:12:22 +08006068 if (BTRFS_I(inode)->delayed_node)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006069 btrfs_balance_delayed_items(fs_info);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006070
6071 return ret;
6072}
6073
6074/*
6075 * This is a copy of file_update_time. We need this so we can return error on
6076 * ENOSPC for updating the inode in the case of file write and mmap writes.
6077 */
Deepa Dinamani95582b02018-05-08 19:36:02 -07006078static int btrfs_update_time(struct inode *inode, struct timespec64 *now,
Josef Bacike41f9412012-03-26 09:46:47 -04006079 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05006080{
Alexander Block2bc5565282012-06-15 09:49:33 +02006081 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Layton3a8c7232017-12-11 06:35:24 -05006082 bool dirty = flags & ~S_VERSION;
Alexander Block2bc5565282012-06-15 09:49:33 +02006083
6084 if (btrfs_root_readonly(root))
6085 return -EROFS;
6086
Josef Bacike41f9412012-03-26 09:46:47 -04006087 if (flags & S_VERSION)
Jeff Layton3a8c7232017-12-11 06:35:24 -05006088 dirty |= inode_maybe_inc_iversion(inode, dirty);
Josef Bacike41f9412012-03-26 09:46:47 -04006089 if (flags & S_CTIME)
6090 inode->i_ctime = *now;
6091 if (flags & S_MTIME)
6092 inode->i_mtime = *now;
6093 if (flags & S_ATIME)
6094 inode->i_atime = *now;
Jeff Layton3a8c7232017-12-11 06:35:24 -05006095 return dirty ? btrfs_dirty_inode(inode) : 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006096}
6097
Chris Masond352ac62008-09-29 15:18:18 -04006098/*
6099 * find the highest existing sequence number in a directory
6100 * and then set the in-memory index_cnt variable to reflect
6101 * free sequence numbers
6102 */
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006103static int btrfs_set_inode_index_count(struct btrfs_inode *inode)
Josef Bacikaec74772008-07-24 12:12:38 -04006104{
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006105 struct btrfs_root *root = inode->root;
Josef Bacikaec74772008-07-24 12:12:38 -04006106 struct btrfs_key key, found_key;
6107 struct btrfs_path *path;
6108 struct extent_buffer *leaf;
6109 int ret;
6110
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006111 key.objectid = btrfs_ino(inode);
David Sterba962a2982014-06-04 18:41:45 +02006112 key.type = BTRFS_DIR_INDEX_KEY;
Josef Bacikaec74772008-07-24 12:12:38 -04006113 key.offset = (u64)-1;
6114
6115 path = btrfs_alloc_path();
6116 if (!path)
6117 return -ENOMEM;
6118
6119 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
6120 if (ret < 0)
6121 goto out;
6122 /* FIXME: we should be able to handle this */
6123 if (ret == 0)
6124 goto out;
6125 ret = 0;
6126
6127 /*
6128 * MAGIC NUMBER EXPLANATION:
6129 * since we search a directory based on f_pos we have to start at 2
6130 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
6131 * else has to start at 2
6132 */
6133 if (path->slots[0] == 0) {
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006134 inode->index_cnt = 2;
Josef Bacikaec74772008-07-24 12:12:38 -04006135 goto out;
6136 }
6137
6138 path->slots[0]--;
6139
6140 leaf = path->nodes[0];
6141 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6142
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006143 if (found_key.objectid != btrfs_ino(inode) ||
David Sterba962a2982014-06-04 18:41:45 +02006144 found_key.type != BTRFS_DIR_INDEX_KEY) {
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006145 inode->index_cnt = 2;
Josef Bacikaec74772008-07-24 12:12:38 -04006146 goto out;
6147 }
6148
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006149 inode->index_cnt = found_key.offset + 1;
Josef Bacikaec74772008-07-24 12:12:38 -04006150out:
6151 btrfs_free_path(path);
6152 return ret;
6153}
6154
Chris Masond352ac62008-09-29 15:18:18 -04006155/*
6156 * helper to find a free sequence number in a given directory. This current
6157 * code is very simple, later versions will do smarter things in the btree
6158 */
Nikolay Borisov877574e2017-02-20 13:50:33 +02006159int btrfs_set_inode_index(struct btrfs_inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04006160{
6161 int ret = 0;
6162
Nikolay Borisov877574e2017-02-20 13:50:33 +02006163 if (dir->index_cnt == (u64)-1) {
6164 ret = btrfs_inode_delayed_dir_index_count(dir);
Miao Xie16cdcec2011-04-22 18:12:22 +08006165 if (ret) {
6166 ret = btrfs_set_inode_index_count(dir);
6167 if (ret)
6168 return ret;
6169 }
Josef Bacikaec74772008-07-24 12:12:38 -04006170 }
6171
Nikolay Borisov877574e2017-02-20 13:50:33 +02006172 *index = dir->index_cnt;
6173 dir->index_cnt++;
Josef Bacikaec74772008-07-24 12:12:38 -04006174
6175 return ret;
6176}
6177
Chris Masonb0d5d102014-09-08 13:08:51 -07006178static int btrfs_insert_inode_locked(struct inode *inode)
6179{
6180 struct btrfs_iget_args args;
6181 args.location = &BTRFS_I(inode)->location;
6182 args.root = BTRFS_I(inode)->root;
6183
6184 return insert_inode_locked4(inode,
6185 btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root),
6186 btrfs_find_actor, &args);
6187}
6188
Anand Jain19aee8d2017-07-18 17:37:05 +08006189/*
6190 * Inherit flags from the parent inode.
6191 *
6192 * Currently only the compression flags and the cow flags are inherited.
6193 */
6194static void btrfs_inherit_iflags(struct inode *inode, struct inode *dir)
6195{
6196 unsigned int flags;
6197
6198 if (!dir)
6199 return;
6200
6201 flags = BTRFS_I(dir)->flags;
6202
6203 if (flags & BTRFS_INODE_NOCOMPRESS) {
6204 BTRFS_I(inode)->flags &= ~BTRFS_INODE_COMPRESS;
6205 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
6206 } else if (flags & BTRFS_INODE_COMPRESS) {
6207 BTRFS_I(inode)->flags &= ~BTRFS_INODE_NOCOMPRESS;
6208 BTRFS_I(inode)->flags |= BTRFS_INODE_COMPRESS;
6209 }
6210
6211 if (flags & BTRFS_INODE_NODATACOW) {
6212 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
6213 if (S_ISREG(inode->i_mode))
6214 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
6215 }
6216
David Sterba7b6a2212018-03-26 18:40:21 +02006217 btrfs_sync_inode_flags_to_i_flags(inode);
Anand Jain19aee8d2017-07-18 17:37:05 +08006218}
6219
Chris Mason39279cc2007-06-12 06:35:45 -04006220static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
6221 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04006222 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05006223 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04006224 u64 ref_objectid, u64 objectid,
6225 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04006226{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006227 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04006228 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006229 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04006230 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04006231 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05006232 struct btrfs_inode_ref *ref;
6233 struct btrfs_key key[2];
6234 u32 sizes[2];
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006235 int nitems = name ? 2 : 1;
Chris Mason9c583092008-01-29 15:15:18 -05006236 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04006237 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006238
Chris Mason5f39d392007-10-15 16:14:19 -04006239 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07006240 if (!path)
6241 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04006242
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006243 inode = new_inode(fs_info->sb);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006244 if (!inode) {
6245 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04006246 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006247 }
Chris Mason39279cc2007-06-12 06:35:45 -04006248
Li Zefan581bb052011-04-20 10:06:11 +08006249 /*
Filipe Manana5762b5c2014-08-01 00:10:32 +01006250 * O_TMPFILE, set link count to 0, so that after this point,
6251 * we fill in an inode item with the correct link count.
6252 */
6253 if (!name)
6254 set_nlink(inode, 0);
6255
6256 /*
Li Zefan581bb052011-04-20 10:06:11 +08006257 * we have to initialize this early, so we can reclaim the inode
6258 * number if we fail afterwards in this function.
6259 */
6260 inode->i_ino = objectid;
6261
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006262 if (dir && name) {
liubo1abe9b82011-03-24 11:18:59 +00006263 trace_btrfs_inode_request(dir);
6264
Nikolay Borisov877574e2017-02-20 13:50:33 +02006265 ret = btrfs_set_inode_index(BTRFS_I(dir), index);
Shen Feng09771432009-04-02 16:46:06 -04006266 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006267 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04006268 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04006269 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04006270 }
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006271 } else if (dir) {
6272 *index = 0;
Josef Bacikaec74772008-07-24 12:12:38 -04006273 }
6274 /*
6275 * index_cnt is ignored for everything but a dir,
Su Yuedf6703e2018-01-12 11:08:02 +08006276 * btrfs_set_inode_index_count has an explanation for the magic
Josef Bacikaec74772008-07-24 12:12:38 -04006277 * number
6278 */
6279 BTRFS_I(inode)->index_cnt = 2;
Miao Xie67de1172013-12-26 13:07:06 +08006280 BTRFS_I(inode)->dir_index = *index;
Chris Mason39279cc2007-06-12 06:35:45 -04006281 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04006282 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00006283 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db22007-08-27 16:49:44 -04006284
Josef Bacik5dc562c2012-08-17 13:14:17 -04006285 /*
6286 * We could have gotten an inode number from somebody who was fsynced
6287 * and then removed in this same transaction, so let's just set full
6288 * sync since it will be a full sync anyway and this will blow away the
6289 * old info in the log.
6290 */
6291 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
6292
Chris Mason9c583092008-01-29 15:15:18 -05006293 key[0].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006294 key[0].type = BTRFS_INODE_ITEM_KEY;
Chris Mason9c583092008-01-29 15:15:18 -05006295 key[0].offset = 0;
6296
Chris Mason9c583092008-01-29 15:15:18 -05006297 sizes[0] = sizeof(struct btrfs_inode_item);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006298
6299 if (name) {
6300 /*
6301 * Start new inodes with an inode_ref. This is slightly more
6302 * efficient for small numbers of hard links since they will
6303 * be packed into one item. Extended refs will kick in if we
6304 * add more hard links than can fit in the ref item.
6305 */
6306 key[1].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006307 key[1].type = BTRFS_INODE_REF_KEY;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006308 key[1].offset = ref_objectid;
6309
6310 sizes[1] = name_len + sizeof(*ref);
6311 }
Chris Mason9c583092008-01-29 15:15:18 -05006312
Chris Masonb0d5d102014-09-08 13:08:51 -07006313 location = &BTRFS_I(inode)->location;
6314 location->objectid = objectid;
6315 location->offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02006316 location->type = BTRFS_INODE_ITEM_KEY;
Chris Masonb0d5d102014-09-08 13:08:51 -07006317
6318 ret = btrfs_insert_inode_locked(inode);
Al Viro32955c52018-05-16 12:20:05 -04006319 if (ret < 0) {
6320 iput(inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006321 goto fail;
Al Viro32955c52018-05-16 12:20:05 -04006322 }
Chris Masonb0d5d102014-09-08 13:08:51 -07006323
Chris Masonb9473432009-03-13 11:00:37 -04006324 path->leave_spinning = 1;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006325 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems);
Chris Mason9c583092008-01-29 15:15:18 -05006326 if (ret != 0)
Chris Masonb0d5d102014-09-08 13:08:51 -07006327 goto fail_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04006328
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03006329 inode_init_owner(inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04006330 inode_set_bytes(inode, 0);
chandan r9cc97d62012-07-04 12:48:07 +05306331
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006332 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05306333 inode->i_atime = inode->i_mtime;
6334 inode->i_ctime = inode->i_mtime;
Arnd Bergmannd3c6be6f2018-06-21 18:04:06 +02006335 BTRFS_I(inode)->i_otime = inode->i_mtime;
chandan r9cc97d62012-07-04 12:48:07 +05306336
Chris Mason5f39d392007-10-15 16:14:19 -04006337 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
6338 struct btrfs_inode_item);
David Sterbab159fa22016-11-08 18:09:03 +01006339 memzero_extent_buffer(path->nodes[0], (unsigned long)inode_item,
Li Zefan293f7e02012-07-10 00:58:58 -06006340 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04006341 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05006342
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006343 if (name) {
6344 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
6345 struct btrfs_inode_ref);
6346 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
6347 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
6348 ptr = (unsigned long)(ref + 1);
6349 write_extent_buffer(path->nodes[0], name, ptr, name_len);
6350 }
Chris Mason9c583092008-01-29 15:15:18 -05006351
Chris Mason5f39d392007-10-15 16:14:19 -04006352 btrfs_mark_buffer_dirty(path->nodes[0]);
6353 btrfs_free_path(path);
6354
Christoph Hellwig6cbff002009-04-17 10:37:41 +02006355 btrfs_inherit_iflags(inode, dir);
6356
Al Viro569254b2011-07-24 17:08:40 -04006357 if (S_ISREG(mode)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006358 if (btrfs_test_opt(fs_info, NODATASUM))
Chris Mason94272162009-07-02 12:26:06 -04006359 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006360 if (btrfs_test_opt(fs_info, NODATACOW))
Josef Bacikf2bdf9a2013-02-21 15:28:28 -05006361 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
6362 BTRFS_INODE_NODATASUM;
Chris Mason94272162009-07-02 12:26:06 -04006363 }
6364
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006365 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00006366
6367 trace_btrfs_inode_new(inode);
Chris Mason1973f0f2011-06-24 13:13:29 -04006368 btrfs_set_inode_last_trans(trans, inode);
liubo1abe9b82011-03-24 11:18:59 +00006369
Alexander Block8ea05e32012-07-25 17:35:53 +02006370 btrfs_update_root_times(trans, root);
6371
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006372 ret = btrfs_inode_inherit_props(trans, inode, dir);
6373 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006374 btrfs_err(fs_info,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006375 "error inheriting props for ino %llu (root %llu): %d",
David Sterbaf85b7372017-01-20 14:54:07 +01006376 btrfs_ino(BTRFS_I(inode)), root->root_key.objectid, ret);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006377
Chris Mason39279cc2007-06-12 06:35:45 -04006378 return inode;
Chris Masonb0d5d102014-09-08 13:08:51 -07006379
6380fail_unlock:
Al Viro32955c52018-05-16 12:20:05 -04006381 discard_new_inode(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006382fail:
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006383 if (dir && name)
Josef Bacikaec74772008-07-24 12:12:38 -04006384 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04006385 btrfs_free_path(path);
6386 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006387}
6388
Chris Masond352ac62008-09-29 15:18:18 -04006389/*
6390 * utility function to add 'inode' into 'parent_inode' with
6391 * a give name and a given sequence number.
6392 * if 'add_backref' is true, also insert a backref from the
6393 * inode to the parent directory.
6394 */
Chris Masone02119d2008-09-05 16:13:11 -04006395int btrfs_add_link(struct btrfs_trans_handle *trans,
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006396 struct btrfs_inode *parent_inode, struct btrfs_inode *inode,
Chris Masone02119d2008-09-05 16:13:11 -04006397 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006398{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006399 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006400 struct btrfs_key key;
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006401 struct btrfs_root *root = parent_inode->root;
6402 u64 ino = btrfs_ino(inode);
6403 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006404
Li Zefan33345d012011-04-20 10:31:50 +08006405 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006406 memcpy(&key, &inode->root->root_key, sizeof(key));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006407 } else {
Li Zefan33345d012011-04-20 10:31:50 +08006408 key.objectid = ino;
David Sterba962a2982014-06-04 18:41:45 +02006409 key.type = BTRFS_INODE_ITEM_KEY;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006410 key.offset = 0;
6411 }
Chris Mason39279cc2007-06-12 06:35:45 -04006412
Li Zefan33345d012011-04-20 10:31:50 +08006413 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Lu Fengqi6025c192018-08-01 11:32:29 +08006414 ret = btrfs_add_root_ref(trans, key.objectid,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006415 root->root_key.objectid, parent_ino,
6416 index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006417 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08006418 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
6419 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006420 }
6421
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006422 /* Nothing to clean up yet */
6423 if (ret)
6424 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006425
Lu Fengqi684572d2018-08-04 21:10:57 +08006426 ret = btrfs_insert_dir_item(trans, name, name_len, parent_inode, &key,
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006427 btrfs_inode_type(&inode->vfs_inode), index);
Chris Mason9c520572012-12-17 14:26:57 -05006428 if (ret == -EEXIST || ret == -EOVERFLOW)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006429 goto fail_dir_item;
6430 else if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04006431 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006432 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006433 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006434
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006435 btrfs_i_size_write(parent_inode, parent_inode->vfs_inode.i_size +
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006436 name_len * 2);
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006437 inode_inc_iversion(&parent_inode->vfs_inode);
6438 parent_inode->vfs_inode.i_mtime = parent_inode->vfs_inode.i_ctime =
6439 current_time(&parent_inode->vfs_inode);
6440 ret = btrfs_update_inode(trans, root, &parent_inode->vfs_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006441 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04006442 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006443 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05006444
6445fail_dir_item:
6446 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
6447 u64 local_index;
6448 int err;
Lu Fengqi3ee1c552018-08-01 11:32:28 +08006449 err = btrfs_del_root_ref(trans, key.objectid,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006450 root->root_key.objectid, parent_ino,
6451 &local_index, name, name_len);
Johannes Thumshirn1690dd412018-12-12 15:14:17 +01006452 if (err)
6453 btrfs_abort_transaction(trans, err);
Chris Masonfe66a052012-02-20 08:40:56 -05006454 } else if (add_backref) {
6455 u64 local_index;
6456 int err;
6457
6458 err = btrfs_del_inode_ref(trans, root, name, name_len,
6459 ino, parent_ino, &local_index);
Johannes Thumshirn1690dd412018-12-12 15:14:17 +01006460 if (err)
6461 btrfs_abort_transaction(trans, err);
Chris Masonfe66a052012-02-20 08:40:56 -05006462 }
Johannes Thumshirn1690dd412018-12-12 15:14:17 +01006463
6464 /* Return the original error code */
Chris Masonfe66a052012-02-20 08:40:56 -05006465 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006466}
6467
6468static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006469 struct btrfs_inode *dir, struct dentry *dentry,
6470 struct btrfs_inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006471{
Josef Bacika1b075d2010-11-19 20:36:11 +00006472 int err = btrfs_add_link(trans, dir, inode,
6473 dentry->d_name.name, dentry->d_name.len,
6474 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006475 if (err > 0)
6476 err = -EEXIST;
6477 return err;
6478}
6479
Josef Bacik618e21d2007-07-11 10:18:17 -04006480static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04006481 umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04006482{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006483 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Josef Bacik618e21d2007-07-11 10:18:17 -04006484 struct btrfs_trans_handle *trans;
6485 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006486 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04006487 int err;
Josef Bacik618e21d2007-07-11 10:18:17 -04006488 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006489 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04006490
Josef Bacik9ed74f22009-09-11 16:12:44 -04006491 /*
6492 * 2 for inode item and ref
6493 * 2 for dir items
6494 * 1 for xattr if selinux is on
6495 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006496 trans = btrfs_start_transaction(root, 5);
6497 if (IS_ERR(trans))
6498 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05006499
Li Zefan581bb052011-04-20 10:06:11 +08006500 err = btrfs_find_free_ino(root, &objectid);
6501 if (err)
6502 goto out_unlock;
6503
Josef Bacikaec74772008-07-24 12:12:38 -04006504 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006505 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6506 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006507 if (IS_ERR(inode)) {
6508 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -04006509 inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04006510 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006511 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006512
Casey Schauflerad19db72011-12-15 10:09:07 -05006513 /*
6514 * If the active LSM wants to access the inode during
6515 * d_instantiate it needs these. Smack checks to see
6516 * if the filesystem supports xattrs by looking at the
6517 * ops vector.
6518 */
Casey Schauflerad19db72011-12-15 10:09:07 -05006519 inode->i_op = &btrfs_special_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006520 init_special_inode(inode, inode->i_mode, rdev);
6521
6522 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik618e21d2007-07-11 10:18:17 -04006523 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006524 goto out_unlock;
Chris Masonb0d5d102014-09-08 13:08:51 -07006525
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006526 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6527 0, index);
Al Viro32955c52018-05-16 12:20:05 -04006528 if (err)
6529 goto out_unlock;
6530
6531 btrfs_update_inode(trans, root, inode);
6532 d_instantiate_new(dentry, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006533
Josef Bacik618e21d2007-07-11 10:18:17 -04006534out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006535 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006536 btrfs_btree_balance_dirty(fs_info);
Al Viro32955c52018-05-16 12:20:05 -04006537 if (err && inode) {
Josef Bacik618e21d2007-07-11 10:18:17 -04006538 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -04006539 discard_new_inode(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006540 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006541 return err;
6542}
6543
Chris Mason39279cc2007-06-12 06:35:45 -04006544static int btrfs_create(struct inode *dir, struct dentry *dentry,
Al Viroebfc3b42012-06-10 18:05:36 -04006545 umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -04006546{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006547 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04006548 struct btrfs_trans_handle *trans;
6549 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006550 struct inode *inode = NULL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006551 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04006552 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006553 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006554
Josef Bacik9ed74f22009-09-11 16:12:44 -04006555 /*
6556 * 2 for inode item and ref
6557 * 2 for dir items
6558 * 1 for xattr if selinux is on
6559 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006560 trans = btrfs_start_transaction(root, 5);
6561 if (IS_ERR(trans))
6562 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006563
Li Zefan581bb052011-04-20 10:06:11 +08006564 err = btrfs_find_free_ino(root, &objectid);
6565 if (err)
6566 goto out_unlock;
6567
Josef Bacikaec74772008-07-24 12:12:38 -04006568 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006569 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6570 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006571 if (IS_ERR(inode)) {
6572 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -04006573 inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006574 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006575 }
Casey Schauflerad19db72011-12-15 10:09:07 -05006576 /*
6577 * If the active LSM wants to access the inode during
6578 * d_instantiate it needs these. Smack checks to see
6579 * if the filesystem supports xattrs by looking at the
6580 * ops vector.
6581 */
6582 inode->i_fop = &btrfs_file_operations;
6583 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006584 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006585
6586 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
6587 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006588 goto out_unlock;
Chris Masonb0d5d102014-09-08 13:08:51 -07006589
6590 err = btrfs_update_inode(trans, root, inode);
6591 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006592 goto out_unlock;
Casey Schauflerad19db72011-12-15 10:09:07 -05006593
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006594 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6595 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006596 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006597 goto out_unlock;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006598
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006599 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Al Viro1e2e5472018-05-04 08:23:01 -04006600 d_instantiate_new(dentry, inode);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006601
Chris Mason39279cc2007-06-12 06:35:45 -04006602out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006603 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -04006604 if (err && inode) {
Chris Mason39279cc2007-06-12 06:35:45 -04006605 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -04006606 discard_new_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006607 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006608 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006609 return err;
6610}
6611
6612static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
6613 struct dentry *dentry)
6614{
Filipe Manana271dba452016-01-05 16:24:05 +00006615 struct btrfs_trans_handle *trans = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006616 struct btrfs_root *root = BTRFS_I(dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00006617 struct inode *inode = d_inode(old_dentry);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006618 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason00e4e6b2008-08-05 11:18:09 -04006619 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04006620 int err;
6621 int drop_inode = 0;
6622
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006623 /* do not allow sys_link's with other subvols of the same device */
Misono Tomohiro4fd786e2018-08-06 14:25:24 +09006624 if (root->root_key.objectid != BTRFS_I(inode)->root->root_key.objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00006625 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006626
Mark Fashehf1863732012-08-08 11:32:27 -07006627 if (inode->i_nlink >= BTRFS_LINK_MAX)
Al Viroc055e992011-03-04 17:15:18 +00006628 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04006629
Nikolay Borisov877574e2017-02-20 13:50:33 +02006630 err = btrfs_set_inode_index(BTRFS_I(dir), &index);
Josef Bacikaec74772008-07-24 12:12:38 -04006631 if (err)
6632 goto fail;
6633
Yan, Zhenga22285a2010-05-16 10:48:46 -04006634 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00006635 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04006636 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00006637 * 1 item for parent inode
Omar Sandoval399b0bb2018-05-11 13:13:40 -07006638 * 1 item for orphan item deletion if O_TMPFILE
Yan, Zhenga22285a2010-05-16 10:48:46 -04006639 */
Omar Sandoval399b0bb2018-05-11 13:13:40 -07006640 trans = btrfs_start_transaction(root, inode->i_nlink ? 5 : 6);
Yan, Zhenga22285a2010-05-16 10:48:46 -04006641 if (IS_ERR(trans)) {
6642 err = PTR_ERR(trans);
Filipe Manana271dba452016-01-05 16:24:05 +00006643 trans = NULL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006644 goto fail;
6645 }
Chris Mason5f39d392007-10-15 16:14:19 -04006646
Miao Xie67de1172013-12-26 13:07:06 +08006647 /* There are several dir indexes for this inode, clear the cache. */
6648 BTRFS_I(inode)->dir_index = 0ULL;
Zach Brown8b558c52013-10-16 12:10:34 -07006649 inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04006650 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006651 inode->i_ctime = current_time(inode);
Al Viro7de9c6ee2010-10-23 11:11:40 -04006652 ihold(inode);
Josef Bacike9976152012-10-11 15:53:56 -04006653 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
Josef Bacikaec74772008-07-24 12:12:38 -04006654
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006655 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6656 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04006657
Yan, Zhenga5719522009-09-24 09:17:31 -04006658 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006659 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04006660 } else {
Al Viro10d9f302011-07-16 23:09:10 -04006661 struct dentry *parent = dentry->d_parent;
Filipe Mananad4682ba2018-06-11 19:24:28 +01006662 int ret;
6663
Yan, Zhenga5719522009-09-24 09:17:31 -04006664 err = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006665 if (err)
6666 goto fail;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006667 if (inode->i_nlink == 1) {
6668 /*
6669 * If new hard link count is 1, it's a file created
6670 * with open(2) O_TMPFILE flag.
6671 */
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02006672 err = btrfs_orphan_del(trans, BTRFS_I(inode));
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006673 if (err)
6674 goto fail;
6675 }
Al Viro08c422c2011-12-23 07:58:13 -05006676 d_instantiate(dentry, inode);
Filipe Mananad4682ba2018-06-11 19:24:28 +01006677 ret = btrfs_log_new_name(trans, BTRFS_I(inode), NULL, parent,
6678 true, NULL);
6679 if (ret == BTRFS_NEED_TRANS_COMMIT) {
6680 err = btrfs_commit_transaction(trans);
6681 trans = NULL;
6682 }
Yan, Zhenga5719522009-09-24 09:17:31 -04006683 }
Chris Mason39279cc2007-06-12 06:35:45 -04006684
Chris Mason1832a6d2007-12-21 16:27:21 -05006685fail:
Filipe Manana271dba452016-01-05 16:24:05 +00006686 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006687 btrfs_end_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006688 if (drop_inode) {
6689 inode_dec_link_count(inode);
6690 iput(inode);
6691 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006692 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006693 return err;
6694}
6695
Al Viro18bb1db2011-07-26 01:41:39 -04006696static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04006697{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006698 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masonb9d86662008-05-02 16:13:49 -04006699 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006700 struct btrfs_trans_handle *trans;
6701 struct btrfs_root *root = BTRFS_I(dir)->root;
6702 int err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04006703 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006704 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006705
Josef Bacik9ed74f22009-09-11 16:12:44 -04006706 /*
6707 * 2 items for inode and ref
6708 * 2 items for dir items
6709 * 1 for xattr if selinux is on
6710 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006711 trans = btrfs_start_transaction(root, 5);
6712 if (IS_ERR(trans))
6713 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006714
Li Zefan581bb052011-04-20 10:06:11 +08006715 err = btrfs_find_free_ino(root, &objectid);
6716 if (err)
6717 goto out_fail;
6718
Josef Bacikaec74772008-07-24 12:12:38 -04006719 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006720 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6721 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04006722 if (IS_ERR(inode)) {
6723 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -04006724 inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006725 goto out_fail;
6726 }
Chris Mason5f39d392007-10-15 16:14:19 -04006727
Chris Masonb0d5d102014-09-08 13:08:51 -07006728 /* these must be set before we unlock the inode */
6729 inode->i_op = &btrfs_dir_inode_operations;
6730 inode->i_fop = &btrfs_dir_file_operations;
Josef Bacik33268ea2008-07-24 12:16:36 -04006731
Eric Paris2a7dba32011-02-01 11:05:39 -05006732 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04006733 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006734 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04006735
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02006736 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Mason39279cc2007-06-12 06:35:45 -04006737 err = btrfs_update_inode(trans, root, inode);
6738 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006739 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04006740
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006741 err = btrfs_add_link(trans, BTRFS_I(dir), BTRFS_I(inode),
6742 dentry->d_name.name,
6743 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006744 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006745 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04006746
Al Viro1e2e5472018-05-04 08:23:01 -04006747 d_instantiate_new(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006748
6749out_fail:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006750 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -04006751 if (err && inode) {
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006752 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -04006753 discard_new_inode(inode);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006754 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006755 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006756 return err;
6757}
6758
Chris Masonc8b97812008-10-29 14:49:59 -04006759static noinline int uncompress_inline(struct btrfs_path *path,
Byongho Leee40da0e2015-05-19 23:46:45 +09006760 struct page *page,
Chris Masonc8b97812008-10-29 14:49:59 -04006761 size_t pg_offset, u64 extent_offset,
6762 struct btrfs_file_extent_item *item)
6763{
6764 int ret;
6765 struct extent_buffer *leaf = path->nodes[0];
6766 char *tmp;
6767 size_t max_size;
6768 unsigned long inline_size;
6769 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08006770 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04006771
6772 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08006773 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04006774 max_size = btrfs_file_extent_ram_bytes(leaf, item);
6775 inline_size = btrfs_file_extent_inline_item_len(leaf,
Ross Kirkdd3cc162013-09-16 15:58:09 +01006776 btrfs_item_nr(path->slots[0]));
Chris Masonc8b97812008-10-29 14:49:59 -04006777 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04006778 if (!tmp)
6779 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04006780 ptr = btrfs_file_extent_inline_start(item);
6781
6782 read_extent_buffer(leaf, tmp, ptr, inline_size);
6783
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006784 max_size = min_t(unsigned long, PAGE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08006785 ret = btrfs_decompress(compress_type, tmp, page,
6786 extent_offset, inline_size, max_size);
Zygo Blaxelle1699d22017-03-10 16:45:44 -05006787
6788 /*
6789 * decompression code contains a memset to fill in any space between the end
6790 * of the uncompressed data and the end of max_size in case the decompressed
6791 * data ends up shorter than ram_bytes. That doesn't cover the hole between
6792 * the end of an inline extent and the beginning of the next block, so we
6793 * cover that region here.
6794 */
6795
6796 if (max_size + pg_offset < PAGE_SIZE) {
6797 char *map = kmap(page);
6798 memset(map + pg_offset + max_size, 0, PAGE_SIZE - max_size - pg_offset);
6799 kunmap(page);
6800 }
Chris Masonc8b97812008-10-29 14:49:59 -04006801 kfree(tmp);
Zach Brown166ae5a2014-05-09 17:15:10 -04006802 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -04006803}
6804
Chris Masond352ac62008-09-29 15:18:18 -04006805/*
6806 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05006807 * the ugly parts come from merging extents from the disk with the in-ram
6808 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04006809 * where the in-ram extents might be locked pending data=ordered completion.
6810 *
6811 * This also copies inline extents directly into the page.
6812 */
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006813struct extent_map *btrfs_get_extent(struct btrfs_inode *inode,
Liu Bode2c6612018-08-25 13:47:59 +08006814 struct page *page,
6815 size_t pg_offset, u64 start, u64 len,
6816 int create)
Chris Masona52d9a82007-08-27 16:49:44 -04006817{
David Sterba3ffbd682018-06-29 10:56:42 +02006818 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Chris Masona52d9a82007-08-27 16:49:44 -04006819 int ret;
6820 int err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006821 u64 extent_start = 0;
6822 u64 extent_end = 0;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006823 u64 objectid = btrfs_ino(inode);
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006824 u8 extent_type;
Chris Masonf4219502008-07-22 11:18:09 -04006825 struct btrfs_path *path = NULL;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006826 struct btrfs_root *root = inode->root;
Chris Masona52d9a82007-08-27 16:49:44 -04006827 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04006828 struct extent_buffer *leaf;
6829 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04006830 struct extent_map *em = NULL;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006831 struct extent_map_tree *em_tree = &inode->extent_tree;
6832 struct extent_io_tree *io_tree = &inode->io_tree;
Filipe Manana7ffbb592014-06-09 03:48:05 +01006833 const bool new_inline = !page || create;
Chris Masona52d9a82007-08-27 16:49:44 -04006834
Chris Mason890871b2009-09-02 16:24:52 -04006835 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006836 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04006837 if (em)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006838 em->bdev = fs_info->fs_devices->latest_bdev;
Chris Mason890871b2009-09-02 16:24:52 -04006839 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006840
Chris Masona52d9a82007-08-27 16:49:44 -04006841 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04006842 if (em->start > start || em->start + em->len <= start)
6843 free_extent_map(em);
6844 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05006845 free_extent_map(em);
6846 else
6847 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006848 }
David Sterba172ddd62011-04-21 00:48:27 +02006849 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04006850 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05006851 err = -ENOMEM;
6852 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006853 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006854 em->bdev = fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05006855 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05006856 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05006857 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04006858 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04006859
Liu Bobee6ec82018-08-17 05:05:28 +08006860 path = btrfs_alloc_path();
Chris Masonf4219502008-07-22 11:18:09 -04006861 if (!path) {
Liu Bobee6ec82018-08-17 05:05:28 +08006862 err = -ENOMEM;
6863 goto out;
Chris Masonf4219502008-07-22 11:18:09 -04006864 }
6865
Liu Bobee6ec82018-08-17 05:05:28 +08006866 /* Chances are we'll be called again, so go ahead and do readahead */
6867 path->reada = READA_FORWARD;
6868
Liu Boe49aabd2018-08-25 13:47:09 +08006869 /*
6870 * Unless we're going to uncompress the inline extent, no sleep would
6871 * happen.
6872 */
6873 path->leave_spinning = 1;
6874
Nikolay Borisov5c9a7022017-12-01 11:19:40 +02006875 ret = btrfs_lookup_file_extent(NULL, root, path, objectid, start, 0);
Chris Masona52d9a82007-08-27 16:49:44 -04006876 if (ret < 0) {
6877 err = ret;
6878 goto out;
Nikolay Borisovb8eeab72018-12-17 11:49:00 +02006879 } else if (ret > 0) {
Chris Masona52d9a82007-08-27 16:49:44 -04006880 if (path->slots[0] == 0)
6881 goto not_found;
6882 path->slots[0]--;
6883 }
6884
Chris Mason5f39d392007-10-15 16:14:19 -04006885 leaf = path->nodes[0];
6886 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04006887 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04006888 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04006889 if (found_key.objectid != objectid ||
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006890 found_key.type != BTRFS_EXTENT_DATA_KEY) {
Josef Bacik25a50342013-10-14 12:08:38 -04006891 /*
6892 * If we backup past the first extent we want to move forward
6893 * and see if there is an extent in front of us, otherwise we'll
6894 * say there is a hole for our whole search range which can
6895 * cause problems.
6896 */
6897 extent_end = start;
6898 goto next;
Chris Masona52d9a82007-08-27 16:49:44 -04006899 }
6900
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006901 extent_type = btrfs_file_extent_type(leaf, item);
Chris Mason5f39d392007-10-15 16:14:19 -04006902 extent_start = found_key.offset;
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006903 if (extent_type == BTRFS_FILE_EXTENT_REG ||
6904 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08006905 /* Only regular file could have regular/prealloc extent */
6906 if (!S_ISREG(inode->vfs_inode.i_mode)) {
6907 ret = -EUCLEAN;
6908 btrfs_crit(fs_info,
6909 "regular/prealloc extent found for non-regular inode %llu",
6910 btrfs_ino(inode));
6911 goto out;
6912 }
Chris Masona52d9a82007-08-27 16:49:44 -04006913 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04006914 btrfs_file_extent_num_bytes(leaf, item);
Liu Bo09ed2f12017-03-10 11:09:48 -08006915
6916 trace_btrfs_get_extent_show_fi_regular(inode, leaf, item,
6917 extent_start);
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006918 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Yan Zheng9036c102008-10-30 14:19:41 -04006919 size_t size;
Qu Wenruoe41ca582018-06-06 15:41:49 +08006920
6921 size = btrfs_file_extent_ram_bytes(leaf, item);
Jeff Mahoneyda170662016-06-15 09:22:56 -04006922 extent_end = ALIGN(extent_start + size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006923 fs_info->sectorsize);
Liu Bo09ed2f12017-03-10 11:09:48 -08006924
6925 trace_btrfs_get_extent_show_fi_inline(inode, leaf, item,
6926 path->slots[0],
6927 extent_start);
Yan Zheng9036c102008-10-30 14:19:41 -04006928 }
Josef Bacik25a50342013-10-14 12:08:38 -04006929next:
Yan Zheng9036c102008-10-30 14:19:41 -04006930 if (start >= extent_end) {
6931 path->slots[0]++;
6932 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
6933 ret = btrfs_next_leaf(root, path);
6934 if (ret < 0) {
6935 err = ret;
6936 goto out;
Nikolay Borisovb8eeab72018-12-17 11:49:00 +02006937 } else if (ret > 0) {
Yan Zheng9036c102008-10-30 14:19:41 -04006938 goto not_found;
Nikolay Borisovb8eeab72018-12-17 11:49:00 +02006939 }
Yan Zheng9036c102008-10-30 14:19:41 -04006940 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04006941 }
Yan Zheng9036c102008-10-30 14:19:41 -04006942 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6943 if (found_key.objectid != objectid ||
6944 found_key.type != BTRFS_EXTENT_DATA_KEY)
6945 goto not_found;
6946 if (start + len <= found_key.offset)
6947 goto not_found;
Wang Shilonge2eca692014-07-17 11:44:14 +08006948 if (start > found_key.offset)
6949 goto next;
Nikolay Borisov02a033d2018-12-17 10:36:02 +02006950
6951 /* New extent overlaps with existing one */
Yan Zheng9036c102008-10-30 14:19:41 -04006952 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006953 em->orig_start = start;
Yan Zheng9036c102008-10-30 14:19:41 -04006954 em->len = found_key.offset - start;
Nikolay Borisov02a033d2018-12-17 10:36:02 +02006955 em->block_start = EXTENT_MAP_HOLE;
6956 goto insert;
Yan Zheng9036c102008-10-30 14:19:41 -04006957 }
6958
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006959 btrfs_extent_item_to_extent_map(inode, path, item,
Nikolay Borisov9cdc5122017-02-20 13:51:02 +02006960 new_inline, em);
Filipe Manana7ffbb592014-06-09 03:48:05 +01006961
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006962 if (extent_type == BTRFS_FILE_EXTENT_REG ||
6963 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006964 goto insert;
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006965 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04006966 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04006967 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04006968 size_t size;
6969 size_t extent_offset;
6970 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04006971
Filipe Manana7ffbb592014-06-09 03:48:05 +01006972 if (new_inline)
Yan689f9342007-10-29 11:41:07 -04006973 goto out;
Yan689f9342007-10-29 11:41:07 -04006974
Qu Wenruoe41ca582018-06-06 15:41:49 +08006975 size = btrfs_file_extent_ram_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04006976 extent_offset = page_offset(page) + pg_offset - extent_start;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006977 copy_size = min_t(u64, PAGE_SIZE - pg_offset,
6978 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04006979 em->start = extent_start + extent_offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006980 em->len = ALIGN(copy_size, fs_info->sectorsize);
Josef Bacikb4939682012-12-03 10:31:19 -05006981 em->orig_block_len = em->len;
Josef Bacik70c8a912012-10-11 16:54:30 -04006982 em->orig_start = em->start;
Yan689f9342007-10-29 11:41:07 -04006983 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Liu Boe49aabd2018-08-25 13:47:09 +08006984
6985 btrfs_set_path_blocking(path);
Edmund Nadolskibf46f522017-11-20 13:24:49 -07006986 if (!PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08006987 if (btrfs_file_extent_compression(leaf, item) !=
6988 BTRFS_COMPRESS_NONE) {
Byongho Leee40da0e2015-05-19 23:46:45 +09006989 ret = uncompress_inline(path, page, pg_offset,
Chris Masonc8b97812008-10-29 14:49:59 -04006990 extent_offset, item);
Zach Brown166ae5a2014-05-09 17:15:10 -04006991 if (ret) {
6992 err = ret;
6993 goto out;
6994 }
Chris Masonc8b97812008-10-29 14:49:59 -04006995 } else {
6996 map = kmap(page);
6997 read_extent_buffer(leaf, map + pg_offset, ptr,
6998 copy_size);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006999 if (pg_offset + copy_size < PAGE_SIZE) {
Chris Mason93c82d52009-09-11 12:36:29 -04007000 memset(map + pg_offset + copy_size, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007001 PAGE_SIZE - pg_offset -
Chris Mason93c82d52009-09-11 12:36:29 -04007002 copy_size);
7003 }
Chris Masonc8b97812008-10-29 14:49:59 -04007004 kunmap(page);
7005 }
Chris Mason179e29e2007-11-01 11:28:41 -04007006 flush_dcache_page(page);
Chris Masona52d9a82007-08-27 16:49:44 -04007007 }
Chris Masond1310b22008-01-24 16:13:08 -05007008 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00007009 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04007010 goto insert;
Chris Masona52d9a82007-08-27 16:49:44 -04007011 }
7012not_found:
7013 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04007014 em->orig_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05007015 em->len = len;
Chris Mason5f39d392007-10-15 16:14:19 -04007016 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04007017insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02007018 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05007019 if (em->start > start || extent_map_end(em) <= start) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007020 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04007021 "bad extent! em: [%llu %llu] passed [%llu %llu]",
7022 em->start, em->len, start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04007023 err = -EIO;
7024 goto out;
7025 }
Chris Masond1310b22008-01-24 16:13:08 -05007026
7027 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04007028 write_lock(&em_tree->lock);
David Sterbaf46b24c2018-04-03 21:45:57 +02007029 err = btrfs_add_extent_mapping(fs_info, em_tree, &em, start, len);
Chris Mason890871b2009-09-02 16:24:52 -04007030 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04007031out:
Liu Boc6414282018-08-23 07:36:17 +08007032 btrfs_free_path(path);
liubo1abe9b82011-03-24 11:18:59 +00007033
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007034 trace_btrfs_get_extent(root, inode, em);
liubo1abe9b82011-03-24 11:18:59 +00007035
Chris Masona52d9a82007-08-27 16:49:44 -04007036 if (err) {
7037 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04007038 return ERR_PTR(err);
7039 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007040 BUG_ON(!em); /* Error is always set */
Chris Masona52d9a82007-08-27 16:49:44 -04007041 return em;
7042}
7043
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007044struct extent_map *btrfs_get_extent_fiemap(struct btrfs_inode *inode,
Nikolay Borisov4ab47a82018-12-12 09:42:32 +02007045 u64 start, u64 len)
Chris Masonec29ed52011-02-23 16:23:20 -05007046{
7047 struct extent_map *em;
7048 struct extent_map *hole_em = NULL;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007049 u64 delalloc_start = start;
Chris Masonec29ed52011-02-23 16:23:20 -05007050 u64 end;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007051 u64 delalloc_len;
7052 u64 delalloc_end;
Chris Masonec29ed52011-02-23 16:23:20 -05007053 int err = 0;
7054
Nikolay Borisov4ab47a82018-12-12 09:42:32 +02007055 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
Chris Masonec29ed52011-02-23 16:23:20 -05007056 if (IS_ERR(em))
7057 return em;
Dan Carpenter99862772017-04-11 11:57:15 +03007058 /*
7059 * If our em maps to:
7060 * - a hole or
7061 * - a pre-alloc extent,
7062 * there might actually be delalloc bytes behind it.
7063 */
7064 if (em->block_start != EXTENT_MAP_HOLE &&
7065 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7066 return em;
7067 else
7068 hole_em = em;
Chris Masonec29ed52011-02-23 16:23:20 -05007069
7070 /* check to see if we've wrapped (len == -1 or similar) */
7071 end = start + len;
7072 if (end < start)
7073 end = (u64)-1;
7074 else
7075 end -= 1;
7076
7077 em = NULL;
7078
7079 /* ok, we didn't find anything, lets look for delalloc */
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007080 delalloc_len = count_range_bits(&inode->io_tree, &delalloc_start,
Chris Masonec29ed52011-02-23 16:23:20 -05007081 end, len, EXTENT_DELALLOC, 1);
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007082 delalloc_end = delalloc_start + delalloc_len;
7083 if (delalloc_end < delalloc_start)
7084 delalloc_end = (u64)-1;
Chris Masonec29ed52011-02-23 16:23:20 -05007085
7086 /*
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007087 * We didn't find anything useful, return the original results from
7088 * get_extent()
Chris Masonec29ed52011-02-23 16:23:20 -05007089 */
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007090 if (delalloc_start > end || delalloc_end <= start) {
Chris Masonec29ed52011-02-23 16:23:20 -05007091 em = hole_em;
7092 hole_em = NULL;
7093 goto out;
7094 }
7095
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007096 /*
7097 * Adjust the delalloc_start to make sure it doesn't go backwards from
7098 * the start they passed in
Chris Masonec29ed52011-02-23 16:23:20 -05007099 */
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007100 delalloc_start = max(start, delalloc_start);
7101 delalloc_len = delalloc_end - delalloc_start;
Chris Masonec29ed52011-02-23 16:23:20 -05007102
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007103 if (delalloc_len > 0) {
7104 u64 hole_start;
Nikolay Borisov02950af2018-12-12 09:42:34 +02007105 u64 hole_len;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007106 const u64 hole_end = extent_map_end(hole_em);
Chris Masonec29ed52011-02-23 16:23:20 -05007107
David Sterba172ddd62011-04-21 00:48:27 +02007108 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05007109 if (!em) {
7110 err = -ENOMEM;
7111 goto out;
7112 }
Chris Masonec29ed52011-02-23 16:23:20 -05007113 em->bdev = NULL;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007114
7115 ASSERT(hole_em);
7116 /*
7117 * When btrfs_get_extent can't find anything it returns one
7118 * huge hole
7119 *
7120 * Make sure what it found really fits our range, and adjust to
7121 * make sure it is based on the start from the caller
7122 */
7123 if (hole_end <= start || hole_em->start > end) {
7124 free_extent_map(hole_em);
7125 hole_em = NULL;
7126 } else {
7127 hole_start = max(hole_em->start, start);
7128 hole_len = hole_end - hole_start;
7129 }
7130
7131 if (hole_em && delalloc_start > hole_start) {
7132 /*
7133 * Our hole starts before our delalloc, so we have to
7134 * return just the parts of the hole that go until the
7135 * delalloc starts
Chris Masonec29ed52011-02-23 16:23:20 -05007136 */
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007137 em->len = min(hole_len, delalloc_start - hole_start);
Chris Masonec29ed52011-02-23 16:23:20 -05007138 em->start = hole_start;
7139 em->orig_start = hole_start;
7140 /*
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007141 * Don't adjust block start at all, it is fixed at
7142 * EXTENT_MAP_HOLE
Chris Masonec29ed52011-02-23 16:23:20 -05007143 */
7144 em->block_start = hole_em->block_start;
7145 em->block_len = hole_len;
Liu Bof9e4fb52013-01-07 10:10:12 +00007146 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
7147 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonec29ed52011-02-23 16:23:20 -05007148 } else {
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007149 /*
7150 * Hole is out of passed range or it starts after
7151 * delalloc range
7152 */
7153 em->start = delalloc_start;
7154 em->len = delalloc_len;
7155 em->orig_start = delalloc_start;
Chris Masonec29ed52011-02-23 16:23:20 -05007156 em->block_start = EXTENT_MAP_DELALLOC;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007157 em->block_len = delalloc_len;
Chris Masonec29ed52011-02-23 16:23:20 -05007158 }
Nikolay Borisovbf8d32b2017-12-01 11:19:43 +02007159 } else {
Chris Masonec29ed52011-02-23 16:23:20 -05007160 return hole_em;
7161 }
7162out:
7163
7164 free_extent_map(hole_em);
7165 if (err) {
7166 free_extent_map(em);
7167 return ERR_PTR(err);
7168 }
7169 return em;
7170}
7171
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007172static struct extent_map *btrfs_create_dio_extent(struct inode *inode,
7173 const u64 start,
7174 const u64 len,
7175 const u64 orig_start,
7176 const u64 block_start,
7177 const u64 block_len,
7178 const u64 orig_block_len,
7179 const u64 ram_bytes,
7180 const int type)
7181{
7182 struct extent_map *em = NULL;
7183 int ret;
7184
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007185 if (type != BTRFS_ORDERED_NOCOW) {
Liu Bo6f9994d2017-01-31 07:50:22 -08007186 em = create_io_em(inode, start, len, orig_start,
7187 block_start, block_len, orig_block_len,
7188 ram_bytes,
7189 BTRFS_COMPRESS_NONE, /* compress_type */
7190 type);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007191 if (IS_ERR(em))
7192 goto out;
7193 }
7194 ret = btrfs_add_ordered_extent_dio(inode, start, block_start,
7195 len, block_len, type);
7196 if (ret) {
7197 if (em) {
7198 free_extent_map(em);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02007199 btrfs_drop_extent_cache(BTRFS_I(inode), start,
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007200 start + len - 1, 0);
7201 }
7202 em = ERR_PTR(ret);
7203 }
7204 out:
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007205
7206 return em;
7207}
7208
Josef Bacik4b46fce2010-05-23 11:00:55 -04007209static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
7210 u64 start, u64 len)
7211{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007212 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007213 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik70c8a912012-10-11 16:54:30 -04007214 struct extent_map *em;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007215 struct btrfs_key ins;
7216 u64 alloc_hint;
7217 int ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007218
Josef Bacik4b46fce2010-05-23 11:00:55 -04007219 alloc_hint = get_extent_allocation_hint(inode, start, len);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007220 ret = btrfs_reserve_extent(root, len, len, fs_info->sectorsize,
Jeff Mahoneyda170662016-06-15 09:22:56 -04007221 0, alloc_hint, &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007222 if (ret)
7223 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007224
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007225 em = btrfs_create_dio_extent(inode, start, ins.offset, start,
7226 ins.objectid, ins.offset, ins.offset,
Liu Bo6288d6e2017-02-21 12:12:58 -08007227 ins.offset, BTRFS_ORDERED_REGULAR);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007228 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007229 if (IS_ERR(em))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007230 btrfs_free_reserved_extent(fs_info, ins.objectid,
7231 ins.offset, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007232
Josef Bacik4b46fce2010-05-23 11:00:55 -04007233 return em;
7234}
7235
Chris Mason46bfbb52010-05-26 11:04:10 -04007236/*
7237 * returns 1 when the nocow is safe, < 1 on error, 0 if the
7238 * block must be cow'd
7239 */
Josef Bacik00361582013-08-14 14:02:47 -04007240noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
Josef Bacik7ee9e442013-06-21 16:37:03 -04007241 u64 *orig_start, u64 *orig_block_len,
7242 u64 *ram_bytes)
Chris Mason46bfbb52010-05-26 11:04:10 -04007243{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007244 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason46bfbb52010-05-26 11:04:10 -04007245 struct btrfs_path *path;
7246 int ret;
7247 struct extent_buffer *leaf;
7248 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xie7b2b7082014-02-27 13:58:05 +08007249 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason46bfbb52010-05-26 11:04:10 -04007250 struct btrfs_file_extent_item *fi;
7251 struct btrfs_key key;
7252 u64 disk_bytenr;
7253 u64 backref_offset;
7254 u64 extent_end;
7255 u64 num_bytes;
7256 int slot;
7257 int found_type;
Josef Bacik7ee9e442013-06-21 16:37:03 -04007258 bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW);
Miao Xiee77751a2013-12-27 21:11:50 +08007259
Chris Mason46bfbb52010-05-26 11:04:10 -04007260 path = btrfs_alloc_path();
7261 if (!path)
7262 return -ENOMEM;
7263
David Sterbaf85b7372017-01-20 14:54:07 +01007264 ret = btrfs_lookup_file_extent(NULL, root, path,
7265 btrfs_ino(BTRFS_I(inode)), offset, 0);
Chris Mason46bfbb52010-05-26 11:04:10 -04007266 if (ret < 0)
7267 goto out;
7268
7269 slot = path->slots[0];
7270 if (ret == 1) {
7271 if (slot == 0) {
7272 /* can't find the item, must cow */
7273 ret = 0;
7274 goto out;
7275 }
7276 slot--;
7277 }
7278 ret = 0;
7279 leaf = path->nodes[0];
7280 btrfs_item_key_to_cpu(leaf, &key, slot);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02007281 if (key.objectid != btrfs_ino(BTRFS_I(inode)) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04007282 key.type != BTRFS_EXTENT_DATA_KEY) {
7283 /* not our file or wrong item type, must cow */
7284 goto out;
7285 }
7286
7287 if (key.offset > offset) {
7288 /* Wrong offset, must cow */
7289 goto out;
7290 }
7291
7292 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
7293 found_type = btrfs_file_extent_type(leaf, fi);
7294 if (found_type != BTRFS_FILE_EXTENT_REG &&
7295 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
7296 /* not a regular extent, must cow */
7297 goto out;
7298 }
Josef Bacik7ee9e442013-06-21 16:37:03 -04007299
7300 if (!nocow && found_type == BTRFS_FILE_EXTENT_REG)
7301 goto out;
7302
Miao Xiee77751a2013-12-27 21:11:50 +08007303 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
7304 if (extent_end <= offset)
7305 goto out;
7306
Chris Mason46bfbb52010-05-26 11:04:10 -04007307 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Josef Bacik7ee9e442013-06-21 16:37:03 -04007308 if (disk_bytenr == 0)
7309 goto out;
7310
7311 if (btrfs_file_extent_compression(leaf, fi) ||
7312 btrfs_file_extent_encryption(leaf, fi) ||
7313 btrfs_file_extent_other_encoding(leaf, fi))
7314 goto out;
7315
Ethan Lien78d42952018-05-17 14:58:29 +08007316 /*
7317 * Do the same check as in btrfs_cross_ref_exist but without the
7318 * unnecessary search.
7319 */
7320 if (btrfs_file_extent_generation(leaf, fi) <=
7321 btrfs_root_last_snapshot(&root->root_item))
7322 goto out;
7323
Chris Mason46bfbb52010-05-26 11:04:10 -04007324 backref_offset = btrfs_file_extent_offset(leaf, fi);
7325
Josef Bacik7ee9e442013-06-21 16:37:03 -04007326 if (orig_start) {
7327 *orig_start = key.offset - backref_offset;
7328 *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
7329 *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
7330 }
Josef Bacikeb384b52013-04-24 16:32:55 -04007331
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007332 if (btrfs_extent_readonly(fs_info, disk_bytenr))
Chris Mason46bfbb52010-05-26 11:04:10 -04007333 goto out;
Miao Xie7b2b7082014-02-27 13:58:05 +08007334
7335 num_bytes = min(offset + *len, extent_end) - offset;
7336 if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) {
7337 u64 range_end;
7338
Jeff Mahoneyda170662016-06-15 09:22:56 -04007339 range_end = round_up(offset + num_bytes,
7340 root->fs_info->sectorsize) - 1;
Miao Xie7b2b7082014-02-27 13:58:05 +08007341 ret = test_range_bit(io_tree, offset, range_end,
7342 EXTENT_DELALLOC, 0, NULL);
7343 if (ret) {
7344 ret = -EAGAIN;
7345 goto out;
7346 }
7347 }
7348
Josef Bacik1bda19e2013-10-18 12:10:36 -04007349 btrfs_release_path(path);
Chris Mason46bfbb52010-05-26 11:04:10 -04007350
7351 /*
7352 * look for other files referencing this extent, if we
7353 * find any we must cow
7354 */
Josef Bacik00361582013-08-14 14:02:47 -04007355
Liu Boe4c3b2d2017-01-30 12:25:28 -08007356 ret = btrfs_cross_ref_exist(root, btrfs_ino(BTRFS_I(inode)),
Josef Bacik00361582013-08-14 14:02:47 -04007357 key.offset - backref_offset, disk_bytenr);
Josef Bacik00361582013-08-14 14:02:47 -04007358 if (ret) {
7359 ret = 0;
7360 goto out;
7361 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007362
7363 /*
7364 * adjust disk_bytenr and num_bytes to cover just the bytes
7365 * in this extent we are about to write. If there
7366 * are any csums in that range we have to cow in order
7367 * to keep the csums correct
7368 */
7369 disk_bytenr += backref_offset;
7370 disk_bytenr += offset - key.offset;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007371 if (csum_exist_in_range(fs_info, disk_bytenr, num_bytes))
7372 goto out;
Chris Mason46bfbb52010-05-26 11:04:10 -04007373 /*
7374 * all of the above have passed, it is safe to overwrite this extent
7375 * without cow
7376 */
Josef Bacikeb384b52013-04-24 16:32:55 -04007377 *len = num_bytes;
Chris Mason46bfbb52010-05-26 11:04:10 -04007378 ret = 1;
7379out:
7380 btrfs_free_path(path);
7381 return ret;
7382}
7383
Josef Bacikeb838e72012-07-31 16:28:48 -04007384static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
7385 struct extent_state **cached_state, int writing)
7386{
7387 struct btrfs_ordered_extent *ordered;
7388 int ret = 0;
7389
7390 while (1) {
7391 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbaff13db42015-12-03 14:30:40 +01007392 cached_state);
Josef Bacikeb838e72012-07-31 16:28:48 -04007393 /*
7394 * We're concerned with the entire range that we're going to be
Nicholas D Steeves01327612016-05-19 21:18:45 -04007395 * doing DIO to, so we need to make sure there's no ordered
Josef Bacikeb838e72012-07-31 16:28:48 -04007396 * extents in this range.
7397 */
Nikolay Borisova776c6f2017-02-20 13:50:49 +02007398 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), lockstart,
Josef Bacikeb838e72012-07-31 16:28:48 -04007399 lockend - lockstart + 1);
7400
7401 /*
7402 * We need to make sure there are no buffered pages in this
7403 * range either, we could have raced between the invalidate in
7404 * generic_file_direct_write and locking the extent. The
7405 * invalidate needs to happen so that reads after a write do not
7406 * get stale data.
7407 */
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007408 if (!ordered &&
David Sterba051c98e2018-03-07 15:33:22 +01007409 (!writing || !filemap_range_has_page(inode->i_mapping,
7410 lockstart, lockend)))
Josef Bacikeb838e72012-07-31 16:28:48 -04007411 break;
7412
7413 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbae43bbe52017-12-12 21:43:52 +01007414 cached_state);
Josef Bacikeb838e72012-07-31 16:28:48 -04007415
7416 if (ordered) {
Filipe Mananaade77022016-02-18 14:28:55 +00007417 /*
7418 * If we are doing a DIO read and the ordered extent we
7419 * found is for a buffered write, we can not wait for it
7420 * to complete and retry, because if we do so we can
7421 * deadlock with concurrent buffered writes on page
7422 * locks. This happens only if our DIO read covers more
7423 * than one extent map, if at this point has already
7424 * created an ordered extent for a previous extent map
7425 * and locked its range in the inode's io tree, and a
7426 * concurrent write against that previous extent map's
7427 * range and this range started (we unlock the ranges
7428 * in the io tree only when the bios complete and
7429 * buffered writes always lock pages before attempting
7430 * to lock range in the io tree).
7431 */
7432 if (writing ||
7433 test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags))
7434 btrfs_start_ordered_extent(inode, ordered, 1);
7435 else
7436 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007437 btrfs_put_ordered_extent(ordered);
7438 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007439 /*
Filipe Mananab850ae12015-12-08 16:23:16 +00007440 * We could trigger writeback for this range (and wait
7441 * for it to complete) and then invalidate the pages for
7442 * this range (through invalidate_inode_pages2_range()),
7443 * but that can lead us to a deadlock with a concurrent
7444 * call to readpages() (a buffered read or a defrag call
7445 * triggered a readahead) on a page lock due to an
7446 * ordered dio extent we created before but did not have
7447 * yet a corresponding bio submitted (whence it can not
7448 * complete), which makes readpages() wait for that
7449 * ordered extent to complete while holding a lock on
7450 * that page.
Josef Bacikeb838e72012-07-31 16:28:48 -04007451 */
Filipe Mananab850ae12015-12-08 16:23:16 +00007452 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007453 }
7454
Filipe Mananaade77022016-02-18 14:28:55 +00007455 if (ret)
7456 break;
7457
Josef Bacikeb838e72012-07-31 16:28:48 -04007458 cond_resched();
7459 }
7460
7461 return ret;
7462}
7463
Liu Bo6f9994d2017-01-31 07:50:22 -08007464/* The callers of this must take lock_extent() */
7465static struct extent_map *create_io_em(struct inode *inode, u64 start, u64 len,
7466 u64 orig_start, u64 block_start,
7467 u64 block_len, u64 orig_block_len,
7468 u64 ram_bytes, int compress_type,
7469 int type)
Josef Bacik69ffb542012-09-11 15:40:07 -04007470{
7471 struct extent_map_tree *em_tree;
7472 struct extent_map *em;
7473 struct btrfs_root *root = BTRFS_I(inode)->root;
7474 int ret;
7475
Liu Bo6f9994d2017-01-31 07:50:22 -08007476 ASSERT(type == BTRFS_ORDERED_PREALLOC ||
7477 type == BTRFS_ORDERED_COMPRESSED ||
7478 type == BTRFS_ORDERED_NOCOW ||
Liu Bo1af4a0a2017-02-13 15:35:09 -08007479 type == BTRFS_ORDERED_REGULAR);
Liu Bo6f9994d2017-01-31 07:50:22 -08007480
Josef Bacik69ffb542012-09-11 15:40:07 -04007481 em_tree = &BTRFS_I(inode)->extent_tree;
7482 em = alloc_extent_map();
7483 if (!em)
7484 return ERR_PTR(-ENOMEM);
7485
7486 em->start = start;
7487 em->orig_start = orig_start;
7488 em->len = len;
7489 em->block_len = block_len;
7490 em->block_start = block_start;
7491 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacikb4939682012-12-03 10:31:19 -05007492 em->orig_block_len = orig_block_len;
Josef Bacikcc95bef2013-04-04 14:31:27 -04007493 em->ram_bytes = ram_bytes;
Josef Bacik70c8a912012-10-11 16:54:30 -04007494 em->generation = -1;
Josef Bacik69ffb542012-09-11 15:40:07 -04007495 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Liu Bo1af4a0a2017-02-13 15:35:09 -08007496 if (type == BTRFS_ORDERED_PREALLOC) {
Josef Bacikb11e2342012-12-03 10:58:15 -05007497 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Liu Bo1af4a0a2017-02-13 15:35:09 -08007498 } else if (type == BTRFS_ORDERED_COMPRESSED) {
Liu Bo6f9994d2017-01-31 07:50:22 -08007499 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
7500 em->compress_type = compress_type;
7501 }
Josef Bacik69ffb542012-09-11 15:40:07 -04007502
7503 do {
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02007504 btrfs_drop_extent_cache(BTRFS_I(inode), em->start,
Josef Bacik69ffb542012-09-11 15:40:07 -04007505 em->start + em->len - 1, 0);
7506 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04007507 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik69ffb542012-09-11 15:40:07 -04007508 write_unlock(&em_tree->lock);
Liu Bo6f9994d2017-01-31 07:50:22 -08007509 /*
7510 * The caller has taken lock_extent(), who could race with us
7511 * to add em?
7512 */
Josef Bacik69ffb542012-09-11 15:40:07 -04007513 } while (ret == -EEXIST);
7514
7515 if (ret) {
7516 free_extent_map(em);
7517 return ERR_PTR(ret);
7518 }
7519
Liu Bo6f9994d2017-01-31 07:50:22 -08007520 /* em got 2 refs now, callers needs to do free_extent_map once. */
Josef Bacik69ffb542012-09-11 15:40:07 -04007521 return em;
7522}
7523
Nikolay Borisov1c8d0172018-05-02 15:19:32 +03007524
7525static int btrfs_get_blocks_direct_read(struct extent_map *em,
7526 struct buffer_head *bh_result,
7527 struct inode *inode,
7528 u64 start, u64 len)
7529{
7530 if (em->block_start == EXTENT_MAP_HOLE ||
7531 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7532 return -ENOENT;
7533
7534 len = min(len, em->len - (start - em->start));
7535
7536 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
7537 inode->i_blkbits;
7538 bh_result->b_size = len;
7539 bh_result->b_bdev = em->bdev;
7540 set_buffer_mapped(bh_result);
7541
7542 return 0;
7543}
7544
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007545static int btrfs_get_blocks_direct_write(struct extent_map **map,
7546 struct buffer_head *bh_result,
7547 struct inode *inode,
7548 struct btrfs_dio_data *dio_data,
7549 u64 start, u64 len)
7550{
7551 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
7552 struct extent_map *em = *map;
7553 int ret = 0;
7554
7555 /*
7556 * We don't allocate a new extent in the following cases
7557 *
7558 * 1) The inode is marked as NODATACOW. In this case we'll just use the
7559 * existing extent.
7560 * 2) The extent is marked as PREALLOC. We're good to go here and can
7561 * just use the extent.
7562 *
7563 */
7564 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
7565 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
7566 em->block_start != EXTENT_MAP_HOLE)) {
7567 int type;
7568 u64 block_start, orig_start, orig_block_len, ram_bytes;
7569
7570 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7571 type = BTRFS_ORDERED_PREALLOC;
7572 else
7573 type = BTRFS_ORDERED_NOCOW;
7574 len = min(len, em->len - (start - em->start));
7575 block_start = em->block_start + (start - em->start);
7576
7577 if (can_nocow_extent(inode, start, &len, &orig_start,
7578 &orig_block_len, &ram_bytes) == 1 &&
7579 btrfs_inc_nocow_writers(fs_info, block_start)) {
7580 struct extent_map *em2;
7581
7582 em2 = btrfs_create_dio_extent(inode, start, len,
7583 orig_start, block_start,
7584 len, orig_block_len,
7585 ram_bytes, type);
7586 btrfs_dec_nocow_writers(fs_info, block_start);
7587 if (type == BTRFS_ORDERED_PREALLOC) {
7588 free_extent_map(em);
7589 *map = em = em2;
7590 }
7591
7592 if (em2 && IS_ERR(em2)) {
7593 ret = PTR_ERR(em2);
7594 goto out;
7595 }
7596 /*
7597 * For inode marked NODATACOW or extent marked PREALLOC,
7598 * use the existing or preallocated extent, so does not
7599 * need to adjust btrfs_space_info's bytes_may_use.
7600 */
7601 btrfs_free_reserved_data_space_noquota(inode, start,
7602 len);
7603 goto skip_cow;
7604 }
7605 }
7606
7607 /* this will cow the extent */
7608 len = bh_result->b_size;
7609 free_extent_map(em);
7610 *map = em = btrfs_new_extent_direct(inode, start, len);
7611 if (IS_ERR(em)) {
7612 ret = PTR_ERR(em);
7613 goto out;
7614 }
7615
7616 len = min(len, em->len - (start - em->start));
7617
7618skip_cow:
7619 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
7620 inode->i_blkbits;
7621 bh_result->b_size = len;
7622 bh_result->b_bdev = em->bdev;
7623 set_buffer_mapped(bh_result);
7624
7625 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7626 set_buffer_new(bh_result);
7627
7628 /*
7629 * Need to update the i_size under the extent lock so buffered
7630 * readers will get the updated i_size when we unlock.
7631 */
7632 if (!dio_data->overwrite && start + len > i_size_read(inode))
7633 i_size_write(inode, start + len);
7634
7635 WARN_ON(dio_data->reserve < len);
7636 dio_data->reserve -= len;
7637 dio_data->unsubmitted_oe_range_end = start + len;
7638 current->journal_info = dio_data;
7639out:
7640 return ret;
7641}
7642
Josef Bacik4b46fce2010-05-23 11:00:55 -04007643static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
7644 struct buffer_head *bh_result, int create)
7645{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007646 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007647 struct extent_map *em;
Josef Bacikeb838e72012-07-31 16:28:48 -04007648 struct extent_state *cached_state = NULL;
chandan50745b02015-08-28 21:10:13 +05307649 struct btrfs_dio_data *dio_data = NULL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007650 u64 start = iblock << inode->i_blkbits;
Josef Bacikeb838e72012-07-31 16:28:48 -04007651 u64 lockstart, lockend;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007652 u64 len = bh_result->b_size;
Josef Bacikeb838e72012-07-31 16:28:48 -04007653 int unlock_bits = EXTENT_LOCKED;
Miao Xie09348562013-02-07 10:12:07 +00007654 int ret = 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007655
Miao Xie172a5042013-02-21 02:48:22 -07007656 if (create)
Josef Bacik32667892015-02-11 15:08:58 -05007657 unlock_bits |= EXTENT_DIRTY;
Miao Xie172a5042013-02-21 02:48:22 -07007658 else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007659 len = min_t(u64, len, fs_info->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04007660
Josef Bacikc3298612012-08-03 16:49:19 -04007661 lockstart = start;
7662 lockend = start + len - 1;
7663
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007664 if (current->journal_info) {
7665 /*
7666 * Need to pull our outstanding extents and set journal_info to NULL so
Nicholas D Steeves01327612016-05-19 21:18:45 -04007667 * that anything that needs to check if there's a transaction doesn't get
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007668 * confused.
7669 */
chandan50745b02015-08-28 21:10:13 +05307670 dio_data = current->journal_info;
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007671 current->journal_info = NULL;
7672 }
7673
Josef Bacikeb838e72012-07-31 16:28:48 -04007674 /*
7675 * If this errors out it's because we couldn't invalidate pagecache for
7676 * this range and we need to fallback to buffered.
7677 */
Filipe Manana9c9464c2015-11-04 09:52:04 +00007678 if (lock_extent_direct(inode, lockstart, lockend, &cached_state,
7679 create)) {
7680 ret = -ENOTBLK;
7681 goto err;
7682 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007683
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007684 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len, 0);
Josef Bacikeb838e72012-07-31 16:28:48 -04007685 if (IS_ERR(em)) {
7686 ret = PTR_ERR(em);
7687 goto unlock_err;
7688 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007689
7690 /*
7691 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
7692 * io. INLINE is special, and we could probably kludge it in here, but
7693 * it's still buffered so for safety lets just fall back to the generic
7694 * buffered path.
7695 *
7696 * For COMPRESSED we _have_ to read the entire extent in so we can
7697 * decompress it, so there will be buffering required no matter what we
7698 * do, so go ahead and fallback to buffered.
7699 *
Nicholas D Steeves01327612016-05-19 21:18:45 -04007700 * We return -ENOTBLK because that's what makes DIO go ahead and go back
Josef Bacik4b46fce2010-05-23 11:00:55 -04007701 * to buffered IO. Don't blame me, this is the price we pay for using
7702 * the generic code.
7703 */
7704 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
7705 em->block_start == EXTENT_MAP_INLINE) {
7706 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007707 ret = -ENOTBLK;
7708 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007709 }
7710
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007711 if (create) {
7712 ret = btrfs_get_blocks_direct_write(&em, bh_result, inode,
7713 dio_data, start, len);
7714 if (ret < 0)
7715 goto unlock_err;
7716
7717 /* clear and unlock the entire range */
7718 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7719 unlock_bits, 1, 0, &cached_state);
7720 } else {
Nikolay Borisov1c8d0172018-05-02 15:19:32 +03007721 ret = btrfs_get_blocks_direct_read(em, bh_result, inode,
7722 start, len);
7723 /* Can be negative only if we read from a hole */
7724 if (ret < 0) {
7725 ret = 0;
7726 free_extent_map(em);
7727 goto unlock_err;
7728 }
7729 /*
7730 * We need to unlock only the end area that we aren't using.
7731 * The rest is going to be unlocked by the endio routine.
7732 */
7733 lockstart = start + bh_result->b_size;
7734 if (lockstart < lockend) {
7735 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
7736 lockend, unlock_bits, 1, 0,
7737 &cached_state);
7738 } else {
7739 free_extent_state(cached_state);
7740 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007741 }
7742
Josef Bacik4b46fce2010-05-23 11:00:55 -04007743 free_extent_map(em);
7744
7745 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007746
7747unlock_err:
Josef Bacikeb838e72012-07-31 16:28:48 -04007748 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbaae0f1622017-10-31 16:37:52 +01007749 unlock_bits, 1, 0, &cached_state);
Filipe Manana9c9464c2015-11-04 09:52:04 +00007750err:
chandan50745b02015-08-28 21:10:13 +05307751 if (dio_data)
7752 current->journal_info = dio_data;
Josef Bacikeb838e72012-07-31 16:28:48 -04007753 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007754}
7755
Omar Sandoval58efbc92017-08-22 23:45:59 -07007756static inline blk_status_t submit_dio_repair_bio(struct inode *inode,
7757 struct bio *bio,
7758 int mirror_num)
Miao Xie8b110e32014-09-12 18:44:03 +08007759{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007760 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Omar Sandoval58efbc92017-08-22 23:45:59 -07007761 blk_status_t ret;
Miao Xie8b110e32014-09-12 18:44:03 +08007762
Mike Christie37226b22016-06-05 14:31:52 -05007763 BUG_ON(bio_op(bio) == REQ_OP_WRITE);
Miao Xie8b110e32014-09-12 18:44:03 +08007764
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007765 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DIO_REPAIR);
Miao Xie8b110e32014-09-12 18:44:03 +08007766 if (ret)
Nikolay Borisovea057f62017-12-13 10:25:38 +02007767 return ret;
Miao Xie8b110e32014-09-12 18:44:03 +08007768
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007769 ret = btrfs_map_bio(fs_info, bio, mirror_num, 0);
Nikolay Borisovea057f62017-12-13 10:25:38 +02007770
Miao Xie8b110e32014-09-12 18:44:03 +08007771 return ret;
7772}
7773
7774static int btrfs_check_dio_repairable(struct inode *inode,
7775 struct bio *failed_bio,
7776 struct io_failure_record *failrec,
7777 int failed_mirror)
7778{
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007779 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xie8b110e32014-09-12 18:44:03 +08007780 int num_copies;
7781
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007782 num_copies = btrfs_num_copies(fs_info, failrec->logical, failrec->len);
Miao Xie8b110e32014-09-12 18:44:03 +08007783 if (num_copies == 1) {
7784 /*
7785 * we only have a single copy of the data, so don't bother with
7786 * all the retry and error correction code that follows. no
7787 * matter what the error is, it is very likely to persist.
7788 */
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007789 btrfs_debug(fs_info,
7790 "Check DIO Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d",
7791 num_copies, failrec->this_mirror, failed_mirror);
Miao Xie8b110e32014-09-12 18:44:03 +08007792 return 0;
7793 }
7794
7795 failrec->failed_mirror = failed_mirror;
7796 failrec->this_mirror++;
7797 if (failrec->this_mirror == failed_mirror)
7798 failrec->this_mirror++;
7799
7800 if (failrec->this_mirror > num_copies) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007801 btrfs_debug(fs_info,
7802 "Check DIO Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d",
7803 num_copies, failrec->this_mirror, failed_mirror);
Miao Xie8b110e32014-09-12 18:44:03 +08007804 return 0;
7805 }
7806
7807 return 1;
7808}
7809
Omar Sandoval58efbc92017-08-22 23:45:59 -07007810static blk_status_t dio_read_error(struct inode *inode, struct bio *failed_bio,
7811 struct page *page, unsigned int pgoff,
7812 u64 start, u64 end, int failed_mirror,
7813 bio_end_io_t *repair_endio, void *repair_arg)
Miao Xie8b110e32014-09-12 18:44:03 +08007814{
7815 struct io_failure_record *failrec;
Josef Bacik7870d082017-05-05 11:57:15 -04007816 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
7817 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
Miao Xie8b110e32014-09-12 18:44:03 +08007818 struct bio *bio;
7819 int isector;
David Sterbaf1c77c52017-06-06 19:03:49 +02007820 unsigned int read_mode = 0;
Liu Bo17347ce2017-05-15 15:33:27 -07007821 int segs;
Miao Xie8b110e32014-09-12 18:44:03 +08007822 int ret;
Omar Sandoval58efbc92017-08-22 23:45:59 -07007823 blk_status_t status;
Ming Leic16a8ac2017-12-18 20:22:12 +08007824 struct bio_vec bvec;
Miao Xie8b110e32014-09-12 18:44:03 +08007825
Mike Christie37226b22016-06-05 14:31:52 -05007826 BUG_ON(bio_op(failed_bio) == REQ_OP_WRITE);
Miao Xie8b110e32014-09-12 18:44:03 +08007827
7828 ret = btrfs_get_io_failure_record(inode, start, end, &failrec);
7829 if (ret)
Omar Sandoval58efbc92017-08-22 23:45:59 -07007830 return errno_to_blk_status(ret);
Miao Xie8b110e32014-09-12 18:44:03 +08007831
7832 ret = btrfs_check_dio_repairable(inode, failed_bio, failrec,
7833 failed_mirror);
7834 if (!ret) {
Josef Bacik7870d082017-05-05 11:57:15 -04007835 free_io_failure(failure_tree, io_tree, failrec);
Omar Sandoval58efbc92017-08-22 23:45:59 -07007836 return BLK_STS_IOERR;
Miao Xie8b110e32014-09-12 18:44:03 +08007837 }
7838
Liu Bo17347ce2017-05-15 15:33:27 -07007839 segs = bio_segments(failed_bio);
Ming Leic16a8ac2017-12-18 20:22:12 +08007840 bio_get_first_bvec(failed_bio, &bvec);
Liu Bo17347ce2017-05-15 15:33:27 -07007841 if (segs > 1 ||
Ming Leic16a8ac2017-12-18 20:22:12 +08007842 (bvec.bv_len > btrfs_inode_sectorsize(inode)))
Christoph Hellwig70fd7612016-11-01 07:40:10 -06007843 read_mode |= REQ_FAILFAST_DEV;
Miao Xie8b110e32014-09-12 18:44:03 +08007844
7845 isector = start - btrfs_io_bio(failed_bio)->logical;
7846 isector >>= inode->i_sb->s_blocksize_bits;
7847 bio = btrfs_create_repair_bio(inode, failed_bio, failrec, page,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307848 pgoff, isector, repair_endio, repair_arg);
David Sterbaebcc3262018-06-29 10:56:53 +02007849 bio->bi_opf = REQ_OP_READ | read_mode;
Miao Xie8b110e32014-09-12 18:44:03 +08007850
7851 btrfs_debug(BTRFS_I(inode)->root->fs_info,
David Sterba913e1532017-07-13 15:32:18 +02007852 "repair DIO read error: submitting new dio read[%#x] to this_mirror=%d, in_validation=%d",
Miao Xie8b110e32014-09-12 18:44:03 +08007853 read_mode, failrec->this_mirror, failrec->in_validation);
7854
Omar Sandoval58efbc92017-08-22 23:45:59 -07007855 status = submit_dio_repair_bio(inode, bio, failrec->this_mirror);
7856 if (status) {
Josef Bacik7870d082017-05-05 11:57:15 -04007857 free_io_failure(failure_tree, io_tree, failrec);
Miao Xie8b110e32014-09-12 18:44:03 +08007858 bio_put(bio);
7859 }
7860
Omar Sandoval58efbc92017-08-22 23:45:59 -07007861 return status;
Miao Xie8b110e32014-09-12 18:44:03 +08007862}
7863
7864struct btrfs_retry_complete {
7865 struct completion done;
7866 struct inode *inode;
7867 u64 start;
7868 int uptodate;
7869};
7870
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007871static void btrfs_retry_endio_nocsum(struct bio *bio)
Miao Xie8b110e32014-09-12 18:44:03 +08007872{
7873 struct btrfs_retry_complete *done = bio->bi_private;
Josef Bacik7870d082017-05-05 11:57:15 -04007874 struct inode *inode = done->inode;
Miao Xie8b110e32014-09-12 18:44:03 +08007875 struct bio_vec *bvec;
Josef Bacik7870d082017-05-05 11:57:15 -04007876 struct extent_io_tree *io_tree, *failure_tree;
Miao Xie8b110e32014-09-12 18:44:03 +08007877 int i;
Ming Lei6dc4f102019-02-15 19:13:19 +08007878 struct bvec_iter_all iter_all;
Miao Xie8b110e32014-09-12 18:44:03 +08007879
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02007880 if (bio->bi_status)
Miao Xie8b110e32014-09-12 18:44:03 +08007881 goto end;
7882
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307883 ASSERT(bio->bi_vcnt == 1);
Josef Bacik7870d082017-05-05 11:57:15 -04007884 io_tree = &BTRFS_I(inode)->io_tree;
7885 failure_tree = &BTRFS_I(inode)->io_failure_tree;
Ming Lei263663c2017-12-18 20:22:04 +08007886 ASSERT(bio_first_bvec_all(bio)->bv_len == btrfs_inode_sectorsize(inode));
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307887
Miao Xie8b110e32014-09-12 18:44:03 +08007888 done->uptodate = 1;
David Sterbac09abff2017-07-13 18:10:07 +02007889 ASSERT(!bio_flagged(bio, BIO_CLONED));
Ming Lei6dc4f102019-02-15 19:13:19 +08007890 bio_for_each_segment_all(bvec, bio, i, iter_all)
Josef Bacik7870d082017-05-05 11:57:15 -04007891 clean_io_failure(BTRFS_I(inode)->root->fs_info, failure_tree,
7892 io_tree, done->start, bvec->bv_page,
7893 btrfs_ino(BTRFS_I(inode)), 0);
Miao Xie8b110e32014-09-12 18:44:03 +08007894end:
7895 complete(&done->done);
7896 bio_put(bio);
7897}
7898
Omar Sandoval58efbc92017-08-22 23:45:59 -07007899static blk_status_t __btrfs_correct_data_nocsum(struct inode *inode,
7900 struct btrfs_io_bio *io_bio)
Josef Bacik4b46fce2010-05-23 11:00:55 -04007901{
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307902 struct btrfs_fs_info *fs_info;
Liu Bo17347ce2017-05-15 15:33:27 -07007903 struct bio_vec bvec;
7904 struct bvec_iter iter;
Miao Xie8b110e32014-09-12 18:44:03 +08007905 struct btrfs_retry_complete done;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007906 u64 start;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307907 unsigned int pgoff;
7908 u32 sectorsize;
7909 int nr_sectors;
Omar Sandoval58efbc92017-08-22 23:45:59 -07007910 blk_status_t ret;
7911 blk_status_t err = BLK_STS_OK;
Miao Xiedc380ae2014-09-12 18:43:55 +08007912
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307913 fs_info = BTRFS_I(inode)->root->fs_info;
Jeff Mahoneyda170662016-06-15 09:22:56 -04007914 sectorsize = fs_info->sectorsize;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307915
Miao Xiec1dc0892014-09-12 18:43:56 +08007916 start = io_bio->logical;
Miao Xie8b110e32014-09-12 18:44:03 +08007917 done.inode = inode;
Liu Bo17347ce2017-05-15 15:33:27 -07007918 io_bio->bio.bi_iter = io_bio->iter;
Miao Xie8b110e32014-09-12 18:44:03 +08007919
Liu Bo17347ce2017-05-15 15:33:27 -07007920 bio_for_each_segment(bvec, &io_bio->bio, iter) {
7921 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec.bv_len);
7922 pgoff = bvec.bv_offset;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307923
7924next_block_or_try_again:
Miao Xie8b110e32014-09-12 18:44:03 +08007925 done.uptodate = 0;
7926 done.start = start;
7927 init_completion(&done.done);
7928
Liu Bo17347ce2017-05-15 15:33:27 -07007929 ret = dio_read_error(inode, &io_bio->bio, bvec.bv_page,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307930 pgoff, start, start + sectorsize - 1,
7931 io_bio->mirror_num,
7932 btrfs_retry_endio_nocsum, &done);
Liu Bo629ebf42017-05-15 17:20:07 -07007933 if (ret) {
7934 err = ret;
7935 goto next;
7936 }
Miao Xie8b110e32014-09-12 18:44:03 +08007937
David Sterba9c17f6c2017-07-19 19:26:45 +02007938 wait_for_completion_io(&done.done);
Miao Xie8b110e32014-09-12 18:44:03 +08007939
7940 if (!done.uptodate) {
7941 /* We might have another mirror, so try again */
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307942 goto next_block_or_try_again;
Miao Xie8b110e32014-09-12 18:44:03 +08007943 }
7944
Liu Bo629ebf42017-05-15 17:20:07 -07007945next:
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307946 start += sectorsize;
7947
Liu Bo97bf5a52017-04-07 13:11:10 -07007948 nr_sectors--;
7949 if (nr_sectors) {
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307950 pgoff += sectorsize;
Liu Bo97bf5a52017-04-07 13:11:10 -07007951 ASSERT(pgoff < PAGE_SIZE);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307952 goto next_block_or_try_again;
7953 }
Miao Xie8b110e32014-09-12 18:44:03 +08007954 }
7955
Liu Bo629ebf42017-05-15 17:20:07 -07007956 return err;
Miao Xie8b110e32014-09-12 18:44:03 +08007957}
7958
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007959static void btrfs_retry_endio(struct bio *bio)
Miao Xie8b110e32014-09-12 18:44:03 +08007960{
7961 struct btrfs_retry_complete *done = bio->bi_private;
7962 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
Josef Bacik7870d082017-05-05 11:57:15 -04007963 struct extent_io_tree *io_tree, *failure_tree;
7964 struct inode *inode = done->inode;
Miao Xie8b110e32014-09-12 18:44:03 +08007965 struct bio_vec *bvec;
7966 int uptodate;
7967 int ret;
7968 int i;
Ming Lei6dc4f102019-02-15 19:13:19 +08007969 struct bvec_iter_all iter_all;
Miao Xie8b110e32014-09-12 18:44:03 +08007970
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02007971 if (bio->bi_status)
Miao Xie8b110e32014-09-12 18:44:03 +08007972 goto end;
7973
7974 uptodate = 1;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307975
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307976 ASSERT(bio->bi_vcnt == 1);
Ming Lei263663c2017-12-18 20:22:04 +08007977 ASSERT(bio_first_bvec_all(bio)->bv_len == btrfs_inode_sectorsize(done->inode));
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307978
Josef Bacik7870d082017-05-05 11:57:15 -04007979 io_tree = &BTRFS_I(inode)->io_tree;
7980 failure_tree = &BTRFS_I(inode)->io_failure_tree;
7981
David Sterbac09abff2017-07-13 18:10:07 +02007982 ASSERT(!bio_flagged(bio, BIO_CLONED));
Ming Lei6dc4f102019-02-15 19:13:19 +08007983 bio_for_each_segment_all(bvec, bio, i, iter_all) {
Josef Bacik7870d082017-05-05 11:57:15 -04007984 ret = __readpage_endio_check(inode, io_bio, i, bvec->bv_page,
7985 bvec->bv_offset, done->start,
7986 bvec->bv_len);
Miao Xie8b110e32014-09-12 18:44:03 +08007987 if (!ret)
Josef Bacik7870d082017-05-05 11:57:15 -04007988 clean_io_failure(BTRFS_I(inode)->root->fs_info,
7989 failure_tree, io_tree, done->start,
7990 bvec->bv_page,
7991 btrfs_ino(BTRFS_I(inode)),
7992 bvec->bv_offset);
Miao Xie8b110e32014-09-12 18:44:03 +08007993 else
7994 uptodate = 0;
7995 }
7996
7997 done->uptodate = uptodate;
7998end:
7999 complete(&done->done);
8000 bio_put(bio);
8001}
8002
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008003static blk_status_t __btrfs_subio_endio_read(struct inode *inode,
8004 struct btrfs_io_bio *io_bio, blk_status_t err)
Miao Xie8b110e32014-09-12 18:44:03 +08008005{
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308006 struct btrfs_fs_info *fs_info;
Liu Bo17347ce2017-05-15 15:33:27 -07008007 struct bio_vec bvec;
8008 struct bvec_iter iter;
Miao Xie8b110e32014-09-12 18:44:03 +08008009 struct btrfs_retry_complete done;
8010 u64 start;
8011 u64 offset = 0;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308012 u32 sectorsize;
8013 int nr_sectors;
8014 unsigned int pgoff;
8015 int csum_pos;
Liu Boef7cdac12017-04-13 18:11:48 -07008016 bool uptodate = (err == 0);
Miao Xie8b110e32014-09-12 18:44:03 +08008017 int ret;
Omar Sandoval58efbc92017-08-22 23:45:59 -07008018 blk_status_t status;
Miao Xie8b110e32014-09-12 18:44:03 +08008019
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308020 fs_info = BTRFS_I(inode)->root->fs_info;
Jeff Mahoneyda170662016-06-15 09:22:56 -04008021 sectorsize = fs_info->sectorsize;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308022
Omar Sandoval58efbc92017-08-22 23:45:59 -07008023 err = BLK_STS_OK;
Miao Xie8b110e32014-09-12 18:44:03 +08008024 start = io_bio->logical;
8025 done.inode = inode;
Liu Bo17347ce2017-05-15 15:33:27 -07008026 io_bio->bio.bi_iter = io_bio->iter;
Miao Xie8b110e32014-09-12 18:44:03 +08008027
Liu Bo17347ce2017-05-15 15:33:27 -07008028 bio_for_each_segment(bvec, &io_bio->bio, iter) {
8029 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec.bv_len);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308030
Liu Bo17347ce2017-05-15 15:33:27 -07008031 pgoff = bvec.bv_offset;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308032next_block:
Liu Boef7cdac12017-04-13 18:11:48 -07008033 if (uptodate) {
8034 csum_pos = BTRFS_BYTES_TO_BLKS(fs_info, offset);
8035 ret = __readpage_endio_check(inode, io_bio, csum_pos,
8036 bvec.bv_page, pgoff, start, sectorsize);
8037 if (likely(!ret))
8038 goto next;
8039 }
Miao Xie8b110e32014-09-12 18:44:03 +08008040try_again:
8041 done.uptodate = 0;
8042 done.start = start;
8043 init_completion(&done.done);
8044
Omar Sandoval58efbc92017-08-22 23:45:59 -07008045 status = dio_read_error(inode, &io_bio->bio, bvec.bv_page,
8046 pgoff, start, start + sectorsize - 1,
8047 io_bio->mirror_num, btrfs_retry_endio,
8048 &done);
8049 if (status) {
8050 err = status;
Miao Xie8b110e32014-09-12 18:44:03 +08008051 goto next;
8052 }
8053
David Sterba9c17f6c2017-07-19 19:26:45 +02008054 wait_for_completion_io(&done.done);
Miao Xie8b110e32014-09-12 18:44:03 +08008055
8056 if (!done.uptodate) {
8057 /* We might have another mirror, so try again */
8058 goto try_again;
8059 }
8060next:
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308061 offset += sectorsize;
8062 start += sectorsize;
8063
8064 ASSERT(nr_sectors);
8065
Liu Bo97bf5a52017-04-07 13:11:10 -07008066 nr_sectors--;
8067 if (nr_sectors) {
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308068 pgoff += sectorsize;
Liu Bo97bf5a52017-04-07 13:11:10 -07008069 ASSERT(pgoff < PAGE_SIZE);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308070 goto next_block;
8071 }
Kent Overstreet2c30c712013-11-07 12:20:26 -08008072 }
Miao Xiec1dc0892014-09-12 18:43:56 +08008073
8074 return err;
8075}
8076
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008077static blk_status_t btrfs_subio_endio_read(struct inode *inode,
8078 struct btrfs_io_bio *io_bio, blk_status_t err)
Miao Xie8b110e32014-09-12 18:44:03 +08008079{
8080 bool skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
8081
8082 if (skip_csum) {
8083 if (unlikely(err))
8084 return __btrfs_correct_data_nocsum(inode, io_bio);
8085 else
Omar Sandoval58efbc92017-08-22 23:45:59 -07008086 return BLK_STS_OK;
Miao Xie8b110e32014-09-12 18:44:03 +08008087 } else {
8088 return __btrfs_subio_endio_read(inode, io_bio, err);
8089 }
8090}
8091
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008092static void btrfs_endio_direct_read(struct bio *bio)
Miao Xiec1dc0892014-09-12 18:43:56 +08008093{
8094 struct btrfs_dio_private *dip = bio->bi_private;
8095 struct inode *inode = dip->inode;
8096 struct bio *dio_bio;
8097 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008098 blk_status_t err = bio->bi_status;
Miao Xiec1dc0892014-09-12 18:43:56 +08008099
Liu Bo99c4e3b92017-09-15 15:06:51 -06008100 if (dip->flags & BTRFS_DIO_ORIG_BIO_SUBMITTED)
Miao Xie8b110e32014-09-12 18:44:03 +08008101 err = btrfs_subio_endio_read(inode, io_bio, err);
Miao Xiec1dc0892014-09-12 18:43:56 +08008102
Josef Bacik4b46fce2010-05-23 11:00:55 -04008103 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01008104 dip->logical_offset + dip->bytes - 1);
Chris Mason9be33952013-05-17 18:30:14 -04008105 dio_bio = dip->dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008106
Josef Bacik4b46fce2010-05-23 11:00:55 -04008107 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04008108
Liu Bo99c4e3b92017-09-15 15:06:51 -06008109 dio_bio->bi_status = err;
Christoph Hellwig40553512017-06-03 09:37:58 +02008110 dio_end_io(dio_bio);
David Sterbab3a0dd52018-11-22 17:16:49 +01008111 btrfs_io_bio_free_csum(io_bio);
Chris Mason9be33952013-05-17 18:30:14 -04008112 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008113}
8114
Qu Wenruo524272602017-03-08 10:25:52 +08008115static void __endio_write_update_ordered(struct inode *inode,
8116 const u64 offset, const u64 bytes,
8117 const bool uptodate)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008118{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008119 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008120 struct btrfs_ordered_extent *ordered = NULL;
Qu Wenruo524272602017-03-08 10:25:52 +08008121 struct btrfs_workqueue *wq;
8122 btrfs_work_func_t func;
Filipe Manana14543772015-11-24 16:23:54 +00008123 u64 ordered_offset = offset;
8124 u64 ordered_bytes = bytes;
Naohiro Aota67c003f2017-09-01 17:59:07 +09008125 u64 last_offset;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008126
Qu Wenruo524272602017-03-08 10:25:52 +08008127 if (btrfs_is_free_space_inode(BTRFS_I(inode))) {
8128 wq = fs_info->endio_freespace_worker;
8129 func = btrfs_freespace_write_helper;
8130 } else {
8131 wq = fs_info->endio_write_workers;
8132 func = btrfs_endio_write_helper;
8133 }
8134
Nikolay Borisovb25f0d02018-04-11 11:21:17 +03008135 while (ordered_offset < offset + bytes) {
8136 last_offset = ordered_offset;
8137 if (btrfs_dec_test_first_ordered_pending(inode, &ordered,
8138 &ordered_offset,
8139 ordered_bytes,
8140 uptodate)) {
8141 btrfs_init_work(&ordered->work, func,
8142 finish_ordered_fn,
8143 NULL, NULL);
8144 btrfs_queue_work(wq, &ordered->work);
8145 }
8146 /*
8147 * If btrfs_dec_test_ordered_pending does not find any ordered
8148 * extent in the range, we can exit.
8149 */
8150 if (ordered_offset == last_offset)
8151 return;
8152 /*
8153 * Our bio might span multiple ordered extents. In this case
Andrea Gelmini52042d82018-11-28 12:05:13 +01008154 * we keep going until we have accounted the whole dio.
Nikolay Borisovb25f0d02018-04-11 11:21:17 +03008155 */
8156 if (ordered_offset < offset + bytes) {
8157 ordered_bytes = offset + bytes - ordered_offset;
8158 ordered = NULL;
8159 }
Chris Mason163cf092010-11-28 19:56:33 -05008160 }
Filipe Manana14543772015-11-24 16:23:54 +00008161}
8162
8163static void btrfs_endio_direct_write(struct bio *bio)
8164{
8165 struct btrfs_dio_private *dip = bio->bi_private;
8166 struct bio *dio_bio = dip->dio_bio;
8167
Qu Wenruo524272602017-03-08 10:25:52 +08008168 __endio_write_update_ordered(dip->inode, dip->logical_offset,
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008169 dip->bytes, !bio->bi_status);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008170
Josef Bacik4b46fce2010-05-23 11:00:55 -04008171 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04008172
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008173 dio_bio->bi_status = bio->bi_status;
Christoph Hellwig40553512017-06-03 09:37:58 +02008174 dio_end_io(dio_bio);
Chris Mason9be33952013-05-17 18:30:14 -04008175 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008176}
8177
David Sterbad0ee3932018-03-08 14:35:48 +01008178static blk_status_t btrfs_submit_bio_start_direct_io(void *private_data,
David Sterbad0779292018-03-08 13:47:33 +01008179 struct bio *bio, u64 offset)
Chris Masoneaf25d92010-05-25 09:48:28 -04008180{
Josef Bacikc6100a42017-05-05 11:57:13 -04008181 struct inode *inode = private_data;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008182 blk_status_t ret;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008183 ret = btrfs_csum_one_bio(inode, bio, offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008184 BUG_ON(ret); /* -ENOMEM */
Chris Masoneaf25d92010-05-25 09:48:28 -04008185 return 0;
8186}
8187
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008188static void btrfs_end_dio_bio(struct bio *bio)
Miao Xiee65e1532010-11-22 03:04:43 +00008189{
8190 struct btrfs_dio_private *dip = bio->bi_private;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008191 blk_status_t err = bio->bi_status;
Miao Xiee65e1532010-11-22 03:04:43 +00008192
Miao Xie8b110e32014-09-12 18:44:03 +08008193 if (err)
8194 btrfs_warn(BTRFS_I(dip->inode)->root->fs_info,
Mike Christie6296b962016-06-05 14:32:21 -05008195 "direct IO failed ino %llu rw %d,%u sector %#Lx len %u err no %d",
David Sterbaf85b7372017-01-20 14:54:07 +01008196 btrfs_ino(BTRFS_I(dip->inode)), bio_op(bio),
8197 bio->bi_opf,
Miao Xie8b110e32014-09-12 18:44:03 +08008198 (unsigned long long)bio->bi_iter.bi_sector,
8199 bio->bi_iter.bi_size, err);
8200
8201 if (dip->subio_endio)
8202 err = dip->subio_endio(dip->inode, btrfs_io_bio(bio), err);
Miao Xiec1dc0892014-09-12 18:43:56 +08008203
8204 if (err) {
Miao Xiee65e1532010-11-22 03:04:43 +00008205 /*
Nikolay Borisovde224b72018-02-14 10:53:36 +02008206 * We want to perceive the errors flag being set before
8207 * decrementing the reference count. We don't need a barrier
8208 * since atomic operations with a return value are fully
8209 * ordered as per atomic_t.txt
Miao Xiee65e1532010-11-22 03:04:43 +00008210 */
Nikolay Borisovde224b72018-02-14 10:53:36 +02008211 dip->errors = 1;
Miao Xiee65e1532010-11-22 03:04:43 +00008212 }
8213
8214 /* if there are more bios still pending for this dio, just exit */
8215 if (!atomic_dec_and_test(&dip->pending_bios))
8216 goto out;
8217
Chris Mason9be33952013-05-17 18:30:14 -04008218 if (dip->errors) {
Miao Xiee65e1532010-11-22 03:04:43 +00008219 bio_io_error(dip->orig_bio);
Chris Mason9be33952013-05-17 18:30:14 -04008220 } else {
Anand Jain2dbe0c72017-10-14 08:35:56 +08008221 dip->dio_bio->bi_status = BLK_STS_OK;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008222 bio_endio(dip->orig_bio);
Miao Xiee65e1532010-11-22 03:04:43 +00008223 }
8224out:
8225 bio_put(bio);
8226}
8227
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008228static inline blk_status_t btrfs_lookup_and_bind_dio_csum(struct inode *inode,
Miao Xiec1dc0892014-09-12 18:43:56 +08008229 struct btrfs_dio_private *dip,
8230 struct bio *bio,
8231 u64 file_offset)
8232{
8233 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
8234 struct btrfs_io_bio *orig_io_bio = btrfs_io_bio(dip->orig_bio);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008235 blk_status_t ret;
Miao Xiec1dc0892014-09-12 18:43:56 +08008236
8237 /*
8238 * We load all the csum data we need when we submit
8239 * the first bio to reduce the csum tree search and
8240 * contention.
8241 */
8242 if (dip->logical_offset == file_offset) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008243 ret = btrfs_lookup_bio_sums_dio(inode, dip->orig_bio,
Miao Xiec1dc0892014-09-12 18:43:56 +08008244 file_offset);
8245 if (ret)
8246 return ret;
8247 }
8248
8249 if (bio == dip->orig_bio)
8250 return 0;
8251
8252 file_offset -= dip->logical_offset;
8253 file_offset >>= inode->i_sb->s_blocksize_bits;
8254 io_bio->csum = (u8 *)(((u32 *)orig_io_bio->csum) + file_offset);
8255
8256 return 0;
8257}
8258
David Sterbad0ee3932018-03-08 14:35:48 +01008259static inline blk_status_t btrfs_submit_dio_bio(struct bio *bio,
8260 struct inode *inode, u64 file_offset, int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00008261{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008262 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xiefacc8a222013-07-25 19:22:34 +08008263 struct btrfs_dio_private *dip = bio->bi_private;
Mike Christie37226b22016-06-05 14:31:52 -05008264 bool write = bio_op(bio) == REQ_OP_WRITE;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008265 blk_status_t ret;
Miao Xiee65e1532010-11-22 03:04:43 +00008266
Liu Bo4c274bc2017-11-01 17:19:27 -06008267 /* Check btrfs_submit_bio_hook() for rules about async submit. */
Josef Bacikb812ce22012-11-16 13:56:32 -05008268 if (async_submit)
8269 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
8270
Josef Bacik5fd02042012-05-02 14:00:54 -04008271 if (!write) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008272 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DATA);
Josef Bacik5fd02042012-05-02 14:00:54 -04008273 if (ret)
8274 goto err;
8275 }
Miao Xiee65e1532010-11-22 03:04:43 +00008276
Nikolay Borisove6961ca2017-08-03 15:44:58 +03008277 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Josef Bacik1ae39932011-04-06 14:41:34 -04008278 goto map;
8279
8280 if (write && async_submit) {
Josef Bacikc6100a42017-05-05 11:57:13 -04008281 ret = btrfs_wq_submit_bio(fs_info, bio, 0, 0,
8282 file_offset, inode,
David Sterbae288c082018-07-18 17:36:24 +02008283 btrfs_submit_bio_start_direct_io);
Miao Xiee65e1532010-11-22 03:04:43 +00008284 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04008285 } else if (write) {
8286 /*
8287 * If we aren't doing async submit, calculate the csum of the
8288 * bio now.
8289 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008290 ret = btrfs_csum_one_bio(inode, bio, file_offset, 1);
Josef Bacik1ae39932011-04-06 14:41:34 -04008291 if (ret)
8292 goto err;
Miao Xie23ea8e52014-09-12 18:43:54 +08008293 } else {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008294 ret = btrfs_lookup_and_bind_dio_csum(inode, dip, bio,
Miao Xiec1dc0892014-09-12 18:43:56 +08008295 file_offset);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00008296 if (ret)
8297 goto err;
8298 }
Josef Bacik1ae39932011-04-06 14:41:34 -04008299map:
Liu Bo9b4a9b22017-08-18 11:54:02 -06008300 ret = btrfs_map_bio(fs_info, bio, 0, 0);
Miao Xiee65e1532010-11-22 03:04:43 +00008301err:
Miao Xiee65e1532010-11-22 03:04:43 +00008302 return ret;
8303}
8304
Nikolay Borisove6961ca2017-08-03 15:44:58 +03008305static int btrfs_submit_direct_hook(struct btrfs_dio_private *dip)
Miao Xiee65e1532010-11-22 03:04:43 +00008306{
8307 struct inode *inode = dip->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008308 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xiee65e1532010-11-22 03:04:43 +00008309 struct bio *bio;
8310 struct bio *orig_bio = dip->orig_bio;
Kent Overstreet4f024f32013-10-11 15:44:27 -07008311 u64 start_sector = orig_bio->bi_iter.bi_sector;
Miao Xiee65e1532010-11-22 03:04:43 +00008312 u64 file_offset = dip->logical_offset;
Miao Xiee65e1532010-11-22 03:04:43 +00008313 u64 map_length;
Josef Bacik1ae39932011-04-06 14:41:34 -04008314 int async_submit = 0;
Liu Bo725130b2017-05-16 09:51:39 -07008315 u64 submit_len;
8316 int clone_offset = 0;
8317 int clone_len;
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05308318 int ret;
Omar Sandoval58efbc92017-08-22 23:45:59 -07008319 blk_status_t status;
Miao Xiee65e1532010-11-22 03:04:43 +00008320
Kent Overstreet4f024f32013-10-11 15:44:27 -07008321 map_length = orig_bio->bi_iter.bi_size;
Liu Bo725130b2017-05-16 09:51:39 -07008322 submit_len = map_length;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008323 ret = btrfs_map_block(fs_info, btrfs_op(orig_bio), start_sector << 9,
8324 &map_length, NULL, 0);
Miao Xie7a5c3c92014-06-17 18:58:59 +08008325 if (ret)
Miao Xiee65e1532010-11-22 03:04:43 +00008326 return -EIO;
Miao Xiefacc8a222013-07-25 19:22:34 +08008327
Liu Bo725130b2017-05-16 09:51:39 -07008328 if (map_length >= submit_len) {
Josef Bacik02f57c72011-04-06 14:25:44 -04008329 bio = orig_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08008330 dip->flags |= BTRFS_DIO_ORIG_BIO_SUBMITTED;
Josef Bacik02f57c72011-04-06 14:25:44 -04008331 goto submit;
8332 }
8333
David Woodhouse53b381b2013-01-29 18:40:14 -05008334 /* async crcs make it difficult to collect full stripe writes. */
Jeff Mahoney1b868262017-05-17 11:38:35 -04008335 if (btrfs_data_alloc_profile(fs_info) & BTRFS_BLOCK_GROUP_RAID56_MASK)
David Woodhouse53b381b2013-01-29 18:40:14 -05008336 async_submit = 0;
8337 else
8338 async_submit = 1;
8339
Liu Bo725130b2017-05-16 09:51:39 -07008340 /* bio split */
8341 ASSERT(map_length <= INT_MAX);
Josef Bacik02f57c72011-04-06 14:25:44 -04008342 atomic_inc(&dip->pending_bios);
Arnd Bergmann3c91ee62017-05-18 15:33:29 +02008343 do {
Liu Bo725130b2017-05-16 09:51:39 -07008344 clone_len = min_t(int, submit_len, map_length);
Josef Bacik02f57c72011-04-06 14:25:44 -04008345
Liu Bo725130b2017-05-16 09:51:39 -07008346 /*
8347 * This will never fail as it's passing GPF_NOFS and
8348 * the allocation is backed by btrfs_bioset.
8349 */
Liu Boe4770942017-05-16 10:57:14 -07008350 bio = btrfs_bio_clone_partial(orig_bio, clone_offset,
Liu Bo725130b2017-05-16 09:51:39 -07008351 clone_len);
8352 bio->bi_private = dip;
8353 bio->bi_end_io = btrfs_end_dio_bio;
8354 btrfs_io_bio(bio)->logical = file_offset;
Miao Xiee65e1532010-11-22 03:04:43 +00008355
Liu Bo725130b2017-05-16 09:51:39 -07008356 ASSERT(submit_len >= clone_len);
8357 submit_len -= clone_len;
8358 if (submit_len == 0)
8359 break;
Miao Xiee65e1532010-11-22 03:04:43 +00008360
Liu Bo725130b2017-05-16 09:51:39 -07008361 /*
8362 * Increase the count before we submit the bio so we know
8363 * the end IO handler won't happen before we increase the
8364 * count. Otherwise, the dip might get freed before we're
8365 * done setting it up.
8366 */
8367 atomic_inc(&dip->pending_bios);
Miao Xiee65e1532010-11-22 03:04:43 +00008368
David Sterbad0ee3932018-03-08 14:35:48 +01008369 status = btrfs_submit_dio_bio(bio, inode, file_offset,
Omar Sandoval58efbc92017-08-22 23:45:59 -07008370 async_submit);
8371 if (status) {
Liu Bo725130b2017-05-16 09:51:39 -07008372 bio_put(bio);
8373 atomic_dec(&dip->pending_bios);
8374 goto out_err;
Miao Xiee65e1532010-11-22 03:04:43 +00008375 }
Liu Bo725130b2017-05-16 09:51:39 -07008376
8377 clone_offset += clone_len;
8378 start_sector += clone_len >> 9;
8379 file_offset += clone_len;
8380
8381 map_length = submit_len;
8382 ret = btrfs_map_block(fs_info, btrfs_op(orig_bio),
8383 start_sector << 9, &map_length, NULL, 0);
8384 if (ret)
8385 goto out_err;
Arnd Bergmann3c91ee62017-05-18 15:33:29 +02008386 } while (submit_len > 0);
Miao Xiee65e1532010-11-22 03:04:43 +00008387
Josef Bacik02f57c72011-04-06 14:25:44 -04008388submit:
David Sterbad0ee3932018-03-08 14:35:48 +01008389 status = btrfs_submit_dio_bio(bio, inode, file_offset, async_submit);
Omar Sandoval58efbc92017-08-22 23:45:59 -07008390 if (!status)
Miao Xiee65e1532010-11-22 03:04:43 +00008391 return 0;
8392
8393 bio_put(bio);
8394out_err:
8395 dip->errors = 1;
8396 /*
Nikolay Borisovde224b72018-02-14 10:53:36 +02008397 * Before atomic variable goto zero, we must make sure dip->errors is
8398 * perceived to be set. This ordering is ensured by the fact that an
8399 * atomic operations with a return value are fully ordered as per
8400 * atomic_t.txt
Miao Xiee65e1532010-11-22 03:04:43 +00008401 */
Miao Xiee65e1532010-11-22 03:04:43 +00008402 if (atomic_dec_and_test(&dip->pending_bios))
8403 bio_io_error(dip->orig_bio);
8404
8405 /* bio_end_io() will handle error, so we needn't return it */
8406 return 0;
8407}
8408
Mike Christie8a4c1e42016-06-05 14:31:50 -05008409static void btrfs_submit_direct(struct bio *dio_bio, struct inode *inode,
8410 loff_t file_offset)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008411{
Filipe Manana61de7182015-07-01 12:13:10 +01008412 struct btrfs_dio_private *dip = NULL;
Liu Bo3892ac92017-04-17 15:00:28 -07008413 struct bio *bio = NULL;
8414 struct btrfs_io_bio *io_bio;
Mike Christie8a4c1e42016-06-05 14:31:50 -05008415 bool write = (bio_op(dio_bio) == REQ_OP_WRITE);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008416 int ret = 0;
8417
David Sterba8b6c1d52017-06-02 17:48:13 +02008418 bio = btrfs_bio_clone(dio_bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008419
Miao Xiec1dc0892014-09-12 18:43:56 +08008420 dip = kzalloc(sizeof(*dip), GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04008421 if (!dip) {
8422 ret = -ENOMEM;
Filipe Manana61de7182015-07-01 12:13:10 +01008423 goto free_ordered;
Chris Mason9be33952013-05-17 18:30:14 -04008424 }
8425
8426 dip->private = dio_bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008427 dip->inode = inode;
8428 dip->logical_offset = file_offset;
Kent Overstreet4f024f32013-10-11 15:44:27 -07008429 dip->bytes = dio_bio->bi_iter.bi_size;
8430 dip->disk_bytenr = (u64)dio_bio->bi_iter.bi_sector << 9;
Liu Bo3892ac92017-04-17 15:00:28 -07008431 bio->bi_private = dip;
8432 dip->orig_bio = bio;
Chris Mason9be33952013-05-17 18:30:14 -04008433 dip->dio_bio = dio_bio;
Miao Xiee65e1532010-11-22 03:04:43 +00008434 atomic_set(&dip->pending_bios, 0);
Liu Bo3892ac92017-04-17 15:00:28 -07008435 io_bio = btrfs_io_bio(bio);
8436 io_bio->logical = file_offset;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008437
Miao Xiec1dc0892014-09-12 18:43:56 +08008438 if (write) {
Liu Bo3892ac92017-04-17 15:00:28 -07008439 bio->bi_end_io = btrfs_endio_direct_write;
Miao Xiec1dc0892014-09-12 18:43:56 +08008440 } else {
Liu Bo3892ac92017-04-17 15:00:28 -07008441 bio->bi_end_io = btrfs_endio_direct_read;
Miao Xiec1dc0892014-09-12 18:43:56 +08008442 dip->subio_endio = btrfs_subio_endio_read;
8443 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04008444
Filipe Mananaf28a4922015-12-08 19:23:20 +00008445 /*
8446 * Reset the range for unsubmitted ordered extents (to a 0 length range)
8447 * even if we fail to submit a bio, because in such case we do the
8448 * corresponding error handling below and it must not be done a second
8449 * time by btrfs_direct_IO().
8450 */
8451 if (write) {
8452 struct btrfs_dio_data *dio_data = current->journal_info;
8453
8454 dio_data->unsubmitted_oe_range_end = dip->logical_offset +
8455 dip->bytes;
8456 dio_data->unsubmitted_oe_range_start =
8457 dio_data->unsubmitted_oe_range_end;
8458 }
8459
Nikolay Borisove6961ca2017-08-03 15:44:58 +03008460 ret = btrfs_submit_direct_hook(dip);
Miao Xiee65e1532010-11-22 03:04:43 +00008461 if (!ret)
Chris Masoneaf25d92010-05-25 09:48:28 -04008462 return;
Chris Mason9be33952013-05-17 18:30:14 -04008463
David Sterbab3a0dd52018-11-22 17:16:49 +01008464 btrfs_io_bio_free_csum(io_bio);
Chris Mason9be33952013-05-17 18:30:14 -04008465
Josef Bacik4b46fce2010-05-23 11:00:55 -04008466free_ordered:
8467 /*
Filipe Manana61de7182015-07-01 12:13:10 +01008468 * If we arrived here it means either we failed to submit the dip
8469 * or we either failed to clone the dio_bio or failed to allocate the
8470 * dip. If we cloned the dio_bio and allocated the dip, we can just
8471 * call bio_endio against our io_bio so that we get proper resource
8472 * cleanup if we fail to submit the dip, otherwise, we must do the
8473 * same as btrfs_endio_direct_[write|read] because we can't call these
8474 * callbacks - they require an allocated dip and a clone of dio_bio.
Josef Bacik4b46fce2010-05-23 11:00:55 -04008475 */
Liu Bo3892ac92017-04-17 15:00:28 -07008476 if (bio && dip) {
Guoqing Jiang054ec2f2017-06-02 16:08:50 +08008477 bio_io_error(bio);
Filipe Manana61de7182015-07-01 12:13:10 +01008478 /*
Liu Bo3892ac92017-04-17 15:00:28 -07008479 * The end io callbacks free our dip, do the final put on bio
Filipe Manana61de7182015-07-01 12:13:10 +01008480 * and all the cleanup and final put for dio_bio (through
8481 * dio_end_io()).
8482 */
8483 dip = NULL;
Liu Bo3892ac92017-04-17 15:00:28 -07008484 bio = NULL;
Filipe Manana61de7182015-07-01 12:13:10 +01008485 } else {
Filipe Manana14543772015-11-24 16:23:54 +00008486 if (write)
Qu Wenruo524272602017-03-08 10:25:52 +08008487 __endio_write_update_ordered(inode,
Filipe Manana14543772015-11-24 16:23:54 +00008488 file_offset,
8489 dio_bio->bi_iter.bi_size,
Qu Wenruo524272602017-03-08 10:25:52 +08008490 false);
Filipe Manana14543772015-11-24 16:23:54 +00008491 else
Filipe Manana61de7182015-07-01 12:13:10 +01008492 unlock_extent(&BTRFS_I(inode)->io_tree, file_offset,
8493 file_offset + dio_bio->bi_iter.bi_size - 1);
Filipe Manana14543772015-11-24 16:23:54 +00008494
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008495 dio_bio->bi_status = BLK_STS_IOERR;
Filipe Manana61de7182015-07-01 12:13:10 +01008496 /*
8497 * Releases and cleans up our dio_bio, no need to bio_put()
8498 * nor bio_endio()/bio_io_error() against dio_bio.
8499 */
Christoph Hellwig40553512017-06-03 09:37:58 +02008500 dio_end_io(dio_bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008501 }
Liu Bo3892ac92017-04-17 15:00:28 -07008502 if (bio)
8503 bio_put(bio);
Filipe Manana61de7182015-07-01 12:13:10 +01008504 kfree(dip);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008505}
8506
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008507static ssize_t check_direct_IO(struct btrfs_fs_info *fs_info,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008508 const struct iov_iter *iter, loff_t offset)
Chris Mason5a5f79b2010-05-26 21:33:37 -04008509{
8510 int seg;
Josef Bacika1b75f72011-04-08 15:51:18 +00008511 int i;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008512 unsigned int blocksize_mask = fs_info->sectorsize - 1;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008513 ssize_t retval = -EINVAL;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008514
8515 if (offset & blocksize_mask)
8516 goto out;
8517
Al Viro28060d52014-03-22 05:15:17 -04008518 if (iov_iter_alignment(iter) & blocksize_mask)
8519 goto out;
Josef Bacika1b75f72011-04-08 15:51:18 +00008520
Al Viro28060d52014-03-22 05:15:17 -04008521 /* If this is a write we don't need to check anymore */
Al Virocd27e452016-10-10 13:39:05 -04008522 if (iov_iter_rw(iter) != READ || !iter_is_iovec(iter))
Al Viro28060d52014-03-22 05:15:17 -04008523 return 0;
8524 /*
8525 * Check to make sure we don't have duplicate iov_base's in this
8526 * iovec, if so return EINVAL, otherwise we'll get csum errors
8527 * when reading back.
8528 */
8529 for (seg = 0; seg < iter->nr_segs; seg++) {
8530 for (i = seg + 1; i < iter->nr_segs; i++) {
8531 if (iter->iov[seg].iov_base == iter->iov[i].iov_base)
Josef Bacika1b75f72011-04-08 15:51:18 +00008532 goto out;
8533 }
Chris Mason5a5f79b2010-05-26 21:33:37 -04008534 }
8535 retval = 0;
8536out:
8537 return retval;
8538}
Josef Bacikeb838e72012-07-31 16:28:48 -04008539
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008540static ssize_t btrfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
Chris Mason16432982008-04-10 10:23:21 -04008541{
Josef Bacik4b46fce2010-05-23 11:00:55 -04008542 struct file *file = iocb->ki_filp;
8543 struct inode *inode = file->f_mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008544 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
chandan50745b02015-08-28 21:10:13 +05308545 struct btrfs_dio_data dio_data = { 0 };
Qu Wenruo364ecf32017-02-27 15:10:38 +08008546 struct extent_changeset *data_reserved = NULL;
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008547 loff_t offset = iocb->ki_pos;
Miao Xie09348562013-02-07 10:12:07 +00008548 size_t count = 0;
Miao Xie2e60a512013-02-08 07:01:08 +00008549 int flags = 0;
Miao Xie38851cc2013-02-08 07:04:11 +00008550 bool wakeup = true;
8551 bool relock = false;
Miao Xie09348562013-02-07 10:12:07 +00008552 ssize_t ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008553
Nikolay Borisov8c70c9f2017-08-21 12:43:46 +03008554 if (check_direct_IO(fs_info, iter, offset))
Chris Mason5a5f79b2010-05-26 21:33:37 -04008555 return 0;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008556
Jens Axboefe0f07d2015-04-15 17:05:48 -06008557 inode_dio_begin(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008558
Josef Bacik0e267c42013-07-02 10:38:02 -04008559 /*
Miao Xie41bd9ca2014-03-06 13:54:57 +08008560 * The generic stuff only does filemap_write_and_wait_range, which
8561 * isn't enough if we've written compressed pages to this area, so
8562 * we need to flush the dirty pages again to make absolutely sure
8563 * that any outstanding dirty pages are on disk.
Josef Bacik0e267c42013-07-02 10:38:02 -04008564 */
Al Viroa6cbcd42014-03-04 22:38:00 -05008565 count = iov_iter_count(iter);
Miao Xie41bd9ca2014-03-06 13:54:57 +08008566 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
8567 &BTRFS_I(inode)->runtime_flags))
Wang Shilong9a025a02014-07-17 11:44:13 +08008568 filemap_fdatawrite_range(inode->i_mapping, offset,
8569 offset + count - 1);
Josef Bacik0e267c42013-07-02 10:38:02 -04008570
Omar Sandoval6f673762015-03-16 04:33:52 -07008571 if (iov_iter_rw(iter) == WRITE) {
Miao Xie38851cc2013-02-08 07:04:11 +00008572 /*
8573 * If the write DIO is beyond the EOF, we need update
8574 * the isize, but it is protected by i_mutex. So we can
8575 * not unlock the i_mutex at this case.
8576 */
8577 if (offset + count <= inode->i_size) {
Liu Bo4aaedfb2016-12-14 22:36:05 -08008578 dio_data.overwrite = 1;
Al Viro59551022016-01-22 15:40:57 -05008579 inode_unlock(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008580 relock = true;
Goldwyn Rodriguesedf064e2017-06-20 07:05:49 -05008581 } else if (iocb->ki_flags & IOCB_NOWAIT) {
8582 ret = -EAGAIN;
8583 goto out;
Miao Xie38851cc2013-02-08 07:04:11 +00008584 }
Qu Wenruo364ecf32017-02-27 15:10:38 +08008585 ret = btrfs_delalloc_reserve_space(inode, &data_reserved,
8586 offset, count);
Miao Xie09348562013-02-07 10:12:07 +00008587 if (ret)
Miao Xie38851cc2013-02-08 07:04:11 +00008588 goto out;
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008589
8590 /*
8591 * We need to know how many extents we reserved so that we can
8592 * do the accounting properly if we go over the number we
8593 * originally calculated. Abuse current->journal_info for this.
8594 */
Jeff Mahoneyda170662016-06-15 09:22:56 -04008595 dio_data.reserve = round_up(count,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008596 fs_info->sectorsize);
Filipe Mananaf28a4922015-12-08 19:23:20 +00008597 dio_data.unsubmitted_oe_range_start = (u64)offset;
8598 dio_data.unsubmitted_oe_range_end = (u64)offset;
chandan50745b02015-08-28 21:10:13 +05308599 current->journal_info = &dio_data;
Chandan Rajendra97dcdea2016-12-23 15:00:18 +05308600 down_read(&BTRFS_I(inode)->dio_sem);
David Sterbaee39b432014-09-30 01:33:33 +02008601 } else if (test_bit(BTRFS_INODE_READDIO_NEED_LOCK,
8602 &BTRFS_I(inode)->runtime_flags)) {
Jens Axboefe0f07d2015-04-15 17:05:48 -06008603 inode_dio_end(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008604 flags = DIO_LOCKING | DIO_SKIP_HOLES;
8605 wakeup = false;
Miao Xie09348562013-02-07 10:12:07 +00008606 }
8607
Omar Sandoval17f8c842015-03-16 04:33:50 -07008608 ret = __blockdev_direct_IO(iocb, inode,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008609 fs_info->fs_devices->latest_bdev,
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008610 iter, btrfs_get_blocks_direct, NULL,
Omar Sandoval17f8c842015-03-16 04:33:50 -07008611 btrfs_submit_direct, flags);
Omar Sandoval6f673762015-03-16 04:33:52 -07008612 if (iov_iter_rw(iter) == WRITE) {
Chandan Rajendra97dcdea2016-12-23 15:00:18 +05308613 up_read(&BTRFS_I(inode)->dio_sem);
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008614 current->journal_info = NULL;
Liu Boddba1bf2015-06-17 16:59:58 +08008615 if (ret < 0 && ret != -EIOCBQUEUED) {
chandan50745b02015-08-28 21:10:13 +05308616 if (dio_data.reserve)
Qu Wenruobc42bda2017-02-27 15:10:39 +08008617 btrfs_delalloc_release_space(inode, data_reserved,
Qu Wenruo43b18592017-12-12 15:34:32 +08008618 offset, dio_data.reserve, true);
Filipe Mananaf28a4922015-12-08 19:23:20 +00008619 /*
8620 * On error we might have left some ordered extents
8621 * without submitting corresponding bios for them, so
8622 * cleanup them up to avoid other tasks getting them
8623 * and waiting for them to complete forever.
8624 */
8625 if (dio_data.unsubmitted_oe_range_start <
8626 dio_data.unsubmitted_oe_range_end)
Qu Wenruo524272602017-03-08 10:25:52 +08008627 __endio_write_update_ordered(inode,
Filipe Mananaf28a4922015-12-08 19:23:20 +00008628 dio_data.unsubmitted_oe_range_start,
8629 dio_data.unsubmitted_oe_range_end -
8630 dio_data.unsubmitted_oe_range_start,
Qu Wenruo524272602017-03-08 10:25:52 +08008631 false);
Liu Boddba1bf2015-06-17 16:59:58 +08008632 } else if (ret >= 0 && (size_t)ret < count)
Qu Wenruobc42bda2017-02-27 15:10:39 +08008633 btrfs_delalloc_release_space(inode, data_reserved,
Qu Wenruo43b18592017-12-12 15:34:32 +08008634 offset, count - (size_t)ret, true);
8635 btrfs_delalloc_release_extents(BTRFS_I(inode), count, false);
Miao Xie09348562013-02-07 10:12:07 +00008636 }
Miao Xie38851cc2013-02-08 07:04:11 +00008637out:
Miao Xie2e60a512013-02-08 07:01:08 +00008638 if (wakeup)
Jens Axboefe0f07d2015-04-15 17:05:48 -06008639 inode_dio_end(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008640 if (relock)
Al Viro59551022016-01-22 15:40:57 -05008641 inode_lock(inode);
Miao Xie09348562013-02-07 10:12:07 +00008642
Qu Wenruo364ecf32017-02-27 15:10:38 +08008643 extent_changeset_free(data_reserved);
Miao Xie09348562013-02-07 10:12:07 +00008644 return ret;
Chris Mason16432982008-04-10 10:23:21 -04008645}
8646
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008647#define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
8648
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008649static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
8650 __u64 start, __u64 len)
8651{
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008652 int ret;
8653
8654 ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS);
8655 if (ret)
8656 return ret;
8657
David Sterba2135fb92017-06-23 04:09:57 +02008658 return extent_fiemap(inode, fieinfo, start, len);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008659}
8660
Chris Mason9ebefb182007-06-15 13:50:00 -04008661int btrfs_readpage(struct file *file, struct page *page)
8662{
Chris Masond1310b22008-01-24 16:13:08 -05008663 struct extent_io_tree *tree;
8664 tree = &BTRFS_I(page->mapping->host)->io_tree;
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02008665 return extent_read_full_page(tree, page, btrfs_get_extent, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04008666}
Chris Mason1832a6d2007-12-21 16:27:21 -05008667
Chris Mason39279cc2007-06-12 06:35:45 -04008668static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
8669{
Josef Bacikbe7bd732015-10-22 15:05:09 -04008670 struct inode *inode = page->mapping->host;
8671 int ret;
Chris Masonb888db22007-08-27 16:49:44 -04008672
8673 if (current->flags & PF_MEMALLOC) {
8674 redirty_page_for_writepage(wbc, page);
8675 unlock_page(page);
8676 return 0;
8677 }
Josef Bacikbe7bd732015-10-22 15:05:09 -04008678
8679 /*
8680 * If we are under memory pressure we will call this directly from the
8681 * VM, we need to make sure we have the inode referenced for the ordered
8682 * extent. If not just return like we didn't do anything.
8683 */
8684 if (!igrab(inode)) {
8685 redirty_page_for_writepage(wbc, page);
8686 return AOP_WRITEPAGE_ACTIVATE;
8687 }
Nikolay Borisov0a9b0e52017-12-08 15:55:59 +02008688 ret = extent_write_full_page(page, wbc);
Josef Bacikbe7bd732015-10-22 15:05:09 -04008689 btrfs_add_delayed_iput(inode);
8690 return ret;
Chris Masona52d9a82007-08-27 16:49:44 -04008691}
Chris Mason39279cc2007-06-12 06:35:45 -04008692
Eric Sandeen48a3b632013-04-25 20:41:01 +00008693static int btrfs_writepages(struct address_space *mapping,
8694 struct writeback_control *wbc)
Chris Masonb293f02e2007-11-01 19:45:34 -04008695{
Nikolay Borisov8ae225a2018-04-19 10:46:38 +03008696 return extent_writepages(mapping, wbc);
Chris Masonb293f02e2007-11-01 19:45:34 -04008697}
8698
Chris Mason3ab2fb52007-11-08 10:59:22 -05008699static int
8700btrfs_readpages(struct file *file, struct address_space *mapping,
8701 struct list_head *pages, unsigned nr_pages)
8702{
Nikolay Borisov2a3ff0a2018-04-19 10:46:36 +03008703 return extent_readpages(mapping, pages, nr_pages);
Chris Mason3ab2fb52007-11-08 10:59:22 -05008704}
Nikolay Borisov2a3ff0a2018-04-19 10:46:36 +03008705
Chris Masone6dcd2d2008-07-17 12:53:50 -04008706static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04008707{
Nikolay Borisov477a30b2018-04-19 10:46:34 +03008708 int ret = try_release_extent_mapping(page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04008709 if (ret == 1) {
8710 ClearPagePrivate(page);
8711 set_page_private(page, 0);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008712 put_page(page);
Chris Masona52d9a82007-08-27 16:49:44 -04008713 }
8714 return ret;
8715}
Chris Mason39279cc2007-06-12 06:35:45 -04008716
Chris Masone6dcd2d2008-07-17 12:53:50 -04008717static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
8718{
Chris Mason98509cf2008-09-11 15:51:43 -04008719 if (PageWriteback(page) || PageDirty(page))
8720 return 0;
Michal Hocko3ba7ab22017-01-09 15:39:02 +01008721 return __btrfs_releasepage(page, gfp_flags);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008722}
8723
Lukas Czernerd47992f2013-05-21 23:17:23 -04008724static void btrfs_invalidatepage(struct page *page, unsigned int offset,
8725 unsigned int length)
Chris Masona52d9a82007-08-27 16:49:44 -04008726{
Josef Bacik5fd02042012-05-02 14:00:54 -04008727 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05008728 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008729 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008730 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008731 u64 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008732 u64 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308733 u64 start;
8734 u64 end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008735 int inode_evicting = inode->i_state & I_FREEING;
Chris Masona52d9a82007-08-27 16:49:44 -04008736
Chris Mason8b62b722009-09-02 16:53:46 -04008737 /*
8738 * we have the page locked, so new writeback can't start,
8739 * and the dirty bit won't be cleared while we are here.
8740 *
8741 * Wait for IO on this page so that we can safely clear
8742 * the PagePrivate2 bit and do ordered accounting
8743 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008744 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04008745
Josef Bacik5fd02042012-05-02 14:00:54 -04008746 tree = &BTRFS_I(inode)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008747 if (offset) {
8748 btrfs_releasepage(page, GFP_NOFS);
8749 return;
8750 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008751
8752 if (!inode_evicting)
David Sterbaff13db42015-12-03 14:30:40 +01008753 lock_extent_bits(tree, page_start, page_end, &cached_state);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308754again:
8755 start = page_start;
Nikolay Borisova776c6f2017-02-20 13:50:49 +02008756 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), start,
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308757 page_end - start + 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008758 if (ordered) {
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308759 end = min(page_end, ordered->file_offset + ordered->len - 1);
Chris Masoneb84ae02008-07-17 13:53:27 -04008760 /*
8761 * IO on this page will never be started, so we need
8762 * to account for any ordered extents now
8763 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008764 if (!inode_evicting)
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308765 clear_extent_bit(tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008766 EXTENT_DIRTY | EXTENT_DELALLOC |
Filipe Mananaa7e3b972017-04-03 10:45:46 +01008767 EXTENT_DELALLOC_NEW |
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008768 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
David Sterbaae0f1622017-10-31 16:37:52 +01008769 EXTENT_DEFRAG, 1, 0, &cached_state);
Chris Mason8b62b722009-09-02 16:53:46 -04008770 /*
8771 * whoever cleared the private bit is responsible
8772 * for the finish_ordered_io
8773 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04008774 if (TestClearPagePrivate2(page)) {
8775 struct btrfs_ordered_inode_tree *tree;
8776 u64 new_len;
8777
8778 tree = &BTRFS_I(inode)->ordered_tree;
8779
8780 spin_lock_irq(&tree->lock);
8781 set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308782 new_len = start - ordered->file_offset;
Josef Bacik77cef2e2013-08-29 13:57:21 -04008783 if (new_len < ordered->truncated_len)
8784 ordered->truncated_len = new_len;
8785 spin_unlock_irq(&tree->lock);
8786
8787 if (btrfs_dec_test_ordered_pending(inode, &ordered,
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308788 start,
8789 end - start + 1, 1))
Josef Bacik77cef2e2013-08-29 13:57:21 -04008790 btrfs_finish_ordered_io(ordered);
Chris Mason8b62b722009-09-02 16:53:46 -04008791 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008792 btrfs_put_ordered_extent(ordered);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008793 if (!inode_evicting) {
8794 cached_state = NULL;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308795 lock_extent_bits(tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008796 &cached_state);
8797 }
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308798
8799 start = end + 1;
8800 if (start < page_end)
8801 goto again;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008802 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008803
Qu Wenruob9d0b382015-09-29 10:35:16 +08008804 /*
8805 * Qgroup reserved space handler
8806 * Page here will be either
8807 * 1) Already written to disk
8808 * In this case, its reserved space is released from data rsv map
8809 * and will be freed by delayed_ref handler finally.
8810 * So even we call qgroup_free_data(), it won't decrease reserved
8811 * space.
8812 * 2) Not written to disk
Goldwyn Rodrigues0b34c262016-09-30 10:40:52 -05008813 * This means the reserved space should be freed here. However,
8814 * if a truncate invalidates the page (by clearing PageDirty)
8815 * and the page is accounted for while allocating extent
8816 * in btrfs_check_data_free_space() we let delayed_ref to
8817 * free the entire extent.
Qu Wenruob9d0b382015-09-29 10:35:16 +08008818 */
Goldwyn Rodrigues0b34c262016-09-30 10:40:52 -05008819 if (PageDirty(page))
Qu Wenruobc42bda2017-02-27 15:10:39 +08008820 btrfs_qgroup_free_data(inode, NULL, page_start, PAGE_SIZE);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008821 if (!inode_evicting) {
8822 clear_extent_bit(tree, page_start, page_end,
8823 EXTENT_LOCKED | EXTENT_DIRTY |
Filipe Mananaa7e3b972017-04-03 10:45:46 +01008824 EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
8825 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1,
David Sterbaae0f1622017-10-31 16:37:52 +01008826 &cached_state);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008827
8828 __btrfs_releasepage(page, GFP_NOFS);
8829 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008830
Chris Mason4a096752008-07-21 10:29:44 -04008831 ClearPageChecked(page);
Chris Mason9ad6b7bc2008-04-18 16:11:30 -04008832 if (PagePrivate(page)) {
Chris Mason9ad6b7bc2008-04-18 16:11:30 -04008833 ClearPagePrivate(page);
8834 set_page_private(page, 0);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008835 put_page(page);
Chris Mason9ad6b7bc2008-04-18 16:11:30 -04008836 }
Chris Mason39279cc2007-06-12 06:35:45 -04008837}
8838
Chris Mason9ebefb182007-06-15 13:50:00 -04008839/*
8840 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
8841 * called from a page fault handler when a page is first dirtied. Hence we must
8842 * be careful to check for EOF conditions here. We set the page up correctly
8843 * for a written page which means we get ENOSPC checking when writing into
8844 * holes and correct delalloc and unwritten extent mapping on filesystems that
8845 * support these features.
8846 *
8847 * We are not allowed to take the i_mutex here so we have to play games to
8848 * protect against truncate races as the page could now be beyond EOF. Because
Omar Sandovald1342aa2018-05-11 13:13:29 -07008849 * truncate_setsize() writes the inode size before removing pages, once we have
8850 * the page lock we can determine safely if the page is beyond EOF. If it is not
Chris Mason9ebefb182007-06-15 13:50:00 -04008851 * beyond EOF, then the page is guaranteed safe against truncation until we
8852 * unlock the page.
8853 */
Souptick Joardera528a242018-06-06 19:54:44 +05308854vm_fault_t btrfs_page_mkwrite(struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04008855{
Nick Pigginc2ec1752009-03-31 15:23:21 -07008856 struct page *page = vmf->page;
Dave Jiang11bac802017-02-24 14:56:41 -08008857 struct inode *inode = file_inode(vmf->vma->vm_file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008858 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008859 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
8860 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008861 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08008862 struct extent_changeset *data_reserved = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008863 char *kaddr;
8864 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04008865 loff_t size;
Souptick Joardera528a242018-06-06 19:54:44 +05308866 vm_fault_t ret;
8867 int ret2;
Chris Mason9998eb72012-01-25 13:47:40 -05008868 int reserved = 0;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308869 u64 reserved_space;
Chris Masona52d9a82007-08-27 16:49:44 -04008870 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008871 u64 page_end;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308872 u64 end;
8873
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008874 reserved_space = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04008875
Jan Karab2b5ef52012-06-12 16:20:45 +02008876 sb_start_pagefault(inode->i_sb);
Qu Wenruodf480632015-09-08 17:25:54 +08008877 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008878 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308879 end = page_end;
Qu Wenruodf480632015-09-08 17:25:54 +08008880
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308881 /*
8882 * Reserving delalloc space after obtaining the page lock can lead to
8883 * deadlock. For example, if a dirty page is locked by this function
8884 * and the call to btrfs_delalloc_reserve_space() ends up triggering
8885 * dirty page write out, then the btrfs_writepage() function could
8886 * end up waiting indefinitely to get a lock on the page currently
8887 * being processed by btrfs_page_mkwrite() function.
8888 */
Souptick Joardera528a242018-06-06 19:54:44 +05308889 ret2 = btrfs_delalloc_reserve_space(inode, &data_reserved, page_start,
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308890 reserved_space);
Souptick Joardera528a242018-06-06 19:54:44 +05308891 if (!ret2) {
8892 ret2 = file_update_time(vmf->vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05008893 reserved = 1;
8894 }
Souptick Joardera528a242018-06-06 19:54:44 +05308895 if (ret2) {
8896 ret = vmf_error(ret2);
Chris Mason9998eb72012-01-25 13:47:40 -05008897 if (reserved)
8898 goto out;
8899 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07008900 }
Chris Mason1832a6d2007-12-21 16:27:21 -05008901
Nick Piggin56a76f82009-03-31 15:23:23 -07008902 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008903again:
Chris Mason9ebefb182007-06-15 13:50:00 -04008904 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04008905 size = i_size_read(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04008906
Chris Mason9ebefb182007-06-15 13:50:00 -04008907 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04008908 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04008909 /* page got truncated out from underneath us */
8910 goto out_unlock;
8911 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008912 wait_on_page_writeback(page);
8913
David Sterbaff13db42015-12-03 14:30:40 +01008914 lock_extent_bits(io_tree, page_start, page_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008915 set_page_extent_mapped(page);
8916
Chris Masoneb84ae02008-07-17 13:53:27 -04008917 /*
8918 * we can't set the delalloc bits if there are pending ordered
8919 * extents. Drop our locks and wait for them to finish
8920 */
Nikolay Borisova776c6f2017-02-20 13:50:49 +02008921 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start,
8922 PAGE_SIZE);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008923 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00008924 unlock_extent_cached(io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01008925 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008926 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04008927 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008928 btrfs_put_ordered_extent(ordered);
8929 goto again;
8930 }
8931
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008932 if (page->index == ((size - 1) >> PAGE_SHIFT)) {
Jeff Mahoneyda170662016-06-15 09:22:56 -04008933 reserved_space = round_up(size - page_start,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008934 fs_info->sectorsize);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008935 if (reserved_space < PAGE_SIZE) {
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308936 end = page_start + reserved_space - 1;
Qu Wenruobc42bda2017-02-27 15:10:39 +08008937 btrfs_delalloc_release_space(inode, data_reserved,
Qu Wenruo43b18592017-12-12 15:34:32 +08008938 page_start, PAGE_SIZE - reserved_space,
8939 true);
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308940 }
8941 }
8942
Josef Bacikfbf19082009-10-01 17:10:23 -04008943 /*
Liu Bo54160342016-12-13 12:15:19 -08008944 * page_mkwrite gets called when the page is firstly dirtied after it's
8945 * faulted in, but write(2) could also dirty a page and set delalloc
8946 * bits, thus in this case for space account reason, we still need to
8947 * clear any delalloc bits within this page range since we have to
8948 * reserve data&meta space before lock_page() (see above comments).
Josef Bacikfbf19082009-10-01 17:10:23 -04008949 */
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308950 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06008951 EXTENT_DIRTY | EXTENT_DELALLOC |
8952 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
David Sterbaae0f1622017-10-31 16:37:52 +01008953 0, 0, &cached_state);
Josef Bacikfbf19082009-10-01 17:10:23 -04008954
Souptick Joardera528a242018-06-06 19:54:44 +05308955 ret2 = btrfs_set_extent_delalloc(inode, page_start, end, 0,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08008956 &cached_state, 0);
Souptick Joardera528a242018-06-06 19:54:44 +05308957 if (ret2) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00008958 unlock_extent_cached(io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01008959 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04008960 ret = VM_FAULT_SIGBUS;
8961 goto out_unlock;
8962 }
Souptick Joardera528a242018-06-06 19:54:44 +05308963 ret2 = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04008964
8965 /* page is wholly or partially inside EOF */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008966 if (page_start + PAGE_SIZE > size)
Johannes Thumshirn70730172018-12-05 15:23:03 +01008967 zero_start = offset_in_page(size);
Chris Mason9ebefb182007-06-15 13:50:00 -04008968 else
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008969 zero_start = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04008970
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008971 if (zero_start != PAGE_SIZE) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04008972 kaddr = kmap(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008973 memset(kaddr + zero_start, 0, PAGE_SIZE - zero_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008974 flush_dcache_page(page);
8975 kunmap(page);
8976 }
Chris Mason247e7432008-07-17 12:53:51 -04008977 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008978 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04008979 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04008980
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008981 BTRFS_I(inode)->last_trans = fs_info->generation;
Chris Mason257c62e2009-10-13 13:21:08 -04008982 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06008983 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
Chris Mason257c62e2009-10-13 13:21:08 -04008984
David Sterbae43bbe52017-12-12 21:43:52 +01008985 unlock_extent_cached(io_tree, page_start, page_end, &cached_state);
Chris Mason9ebefb182007-06-15 13:50:00 -04008986
Souptick Joardera528a242018-06-06 19:54:44 +05308987 if (!ret2) {
Qu Wenruo43b18592017-12-12 15:34:32 +08008988 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE, true);
Jan Karab2b5ef52012-06-12 16:20:45 +02008989 sb_end_pagefault(inode->i_sb);
Qu Wenruo364ecf32017-02-27 15:10:38 +08008990 extent_changeset_free(data_reserved);
Chris Mason50a9b212009-09-11 12:33:12 -04008991 return VM_FAULT_LOCKED;
Jan Karab2b5ef52012-06-12 16:20:45 +02008992 }
Chris Mason717beb92018-06-25 10:03:41 -07008993
8994out_unlock:
Chris Mason9ebefb182007-06-15 13:50:00 -04008995 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05008996out:
Qu Wenruo43b18592017-12-12 15:34:32 +08008997 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE, (ret != 0));
Qu Wenruobc42bda2017-02-27 15:10:39 +08008998 btrfs_delalloc_release_space(inode, data_reserved, page_start,
Qu Wenruo43b18592017-12-12 15:34:32 +08008999 reserved_space, (ret != 0));
Chris Mason9998eb72012-01-25 13:47:40 -05009000out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02009001 sb_end_pagefault(inode->i_sb);
Qu Wenruo364ecf32017-02-27 15:10:38 +08009002 extent_changeset_free(data_reserved);
Chris Mason9ebefb182007-06-15 13:50:00 -04009003 return ret;
9004}
9005
Filipe Manana213e8c52018-02-06 20:40:31 +00009006static int btrfs_truncate(struct inode *inode, bool skip_writeback)
Chris Mason39279cc2007-06-12 06:35:45 -04009007{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009008 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009009 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04009010 struct btrfs_block_rsv *rsv;
Omar Sandovalad7e1a72018-05-22 09:59:50 -07009011 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04009012 struct btrfs_trans_handle *trans;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009013 u64 mask = fs_info->sectorsize - 1;
9014 u64 min_size = btrfs_calc_trunc_metadata_size(fs_info, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04009015
Filipe Manana213e8c52018-02-06 20:40:31 +00009016 if (!skip_writeback) {
9017 ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask),
9018 (u64)-1);
9019 if (ret)
9020 return ret;
9021 }
Chris Mason39279cc2007-06-12 06:35:45 -04009022
Josef Bacikfcb80c22011-05-03 10:40:22 -04009023 /*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07009024 * Yes ladies and gentlemen, this is indeed ugly. We have a couple of
9025 * things going on here:
Josef Bacikfcb80c22011-05-03 10:40:22 -04009026 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07009027 * 1) We need to reserve space to update our inode.
Josef Bacikfcb80c22011-05-03 10:40:22 -04009028 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07009029 * 2) We need to have something to cache all the space that is going to
Josef Bacikfcb80c22011-05-03 10:40:22 -04009030 * be free'd up by the truncate operation, but also have some slack
9031 * space reserved in case it uses space during the truncate (thank you
9032 * very much snapshotting).
9033 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07009034 * And we need these to be separate. The fact is we can use a lot of
Josef Bacikfcb80c22011-05-03 10:40:22 -04009035 * space doing the truncate, and we have no earthly idea how much space
Nicholas D Steeves01327612016-05-19 21:18:45 -04009036 * we will use, so we need the truncate reservation to be separate so it
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07009037 * doesn't end up using space reserved for updating the inode. We also
9038 * need to be able to stop the transaction and start a new one, which
9039 * means we need to be able to update the inode several times, and we
9040 * have no idea of knowing how many times that will be, so we can't just
9041 * reserve 1 item for the entirety of the operation, so that has to be
9042 * done separately as well.
Josef Bacikfcb80c22011-05-03 10:40:22 -04009043 *
9044 * So that leaves us with
9045 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07009046 * 1) rsv - for the truncate reservation, which we will steal from the
Josef Bacikfcb80c22011-05-03 10:40:22 -04009047 * transaction reservation.
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07009048 * 2) fs_info->trans_block_rsv - this will have 1 items worth left for
Josef Bacikfcb80c22011-05-03 10:40:22 -04009049 * updating the inode.
9050 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009051 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009052 if (!rsv)
9053 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04009054 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04009055 rsv->failfast = 1;
Josef Bacikf0cd8462011-03-04 14:37:08 -05009056
Josef Bacik907cbce2011-08-08 13:46:15 -04009057 /*
Josef Bacik07127182011-08-19 10:29:59 -04009058 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04009059 * 1 for updating the inode.
9060 */
Josef Bacikf3fe8202013-01-07 17:03:21 -05009061 trans = btrfs_start_transaction(root, 2);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009062 if (IS_ERR(trans)) {
Omar Sandovalad7e1a72018-05-22 09:59:50 -07009063 ret = PTR_ERR(trans);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009064 goto out;
9065 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05009066
Josef Bacik907cbce2011-08-08 13:46:15 -04009067 /* Migrate the slack space for the truncate to our reserve */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009068 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, rsv,
Lu Fengqi3a584172018-08-04 21:10:55 +08009069 min_size, false);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009070 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05009071
Chris Mason5a3f23d2009-03-31 13:27:11 -04009072 /*
Josef Bacik5dc562c2012-08-17 13:14:17 -04009073 * So if we truncate and then write and fsync we normally would just
9074 * write the extents that changed, which is a problem if we need to
9075 * first truncate that entire inode. So set this flag so we write out
9076 * all of the extents in the inode to the sync log so we're completely
9077 * safe.
9078 */
9079 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009080 trans->block_rsv = rsv;
Josef Bacik5dc562c2012-08-17 13:14:17 -04009081
Yan, Zheng80825102009-11-12 09:35:36 +00009082 while (1) {
9083 ret = btrfs_truncate_inode_items(trans, root, inode,
9084 inode->i_size,
9085 BTRFS_EXTENT_DATA_KEY);
Josef Bacikddfae632017-10-19 14:16:02 -04009086 trans->block_rsv = &fs_info->trans_block_rsv;
Omar Sandovalad7e1a72018-05-22 09:59:50 -07009087 if (ret != -ENOSPC && ret != -EAGAIN)
Yan, Zheng80825102009-11-12 09:35:36 +00009088 break;
Chris Mason39279cc2007-06-12 06:35:45 -04009089
Yan, Zheng80825102009-11-12 09:35:36 +00009090 ret = btrfs_update_inode(trans, root, inode);
Omar Sandovalad7e1a72018-05-22 09:59:50 -07009091 if (ret)
Josef Bacik3893e332011-01-31 16:03:11 -05009092 break;
Josef Bacikca7e70f2012-08-27 17:48:15 -04009093
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009094 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009095 btrfs_btree_balance_dirty(fs_info);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009096
9097 trans = btrfs_start_transaction(root, 2);
9098 if (IS_ERR(trans)) {
Omar Sandovalad7e1a72018-05-22 09:59:50 -07009099 ret = PTR_ERR(trans);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009100 trans = NULL;
9101 break;
9102 }
9103
Wang Xiaoguang47b5d642016-09-07 20:17:38 +08009104 btrfs_block_rsv_release(fs_info, rsv, -1);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009105 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv,
Lu Fengqi3a584172018-08-04 21:10:55 +08009106 rsv, min_size, false);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009107 BUG_ON(ret); /* shouldn't happen */
9108 trans->block_rsv = rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00009109 }
9110
Josef Bacikddfae632017-10-19 14:16:02 -04009111 /*
9112 * We can't call btrfs_truncate_block inside a trans handle as we could
9113 * deadlock with freeze, if we got NEED_TRUNCATE_BLOCK then we know
9114 * we've truncated everything except the last little bit, and can do
9115 * btrfs_truncate_block and then update the disk_i_size.
9116 */
9117 if (ret == NEED_TRUNCATE_BLOCK) {
9118 btrfs_end_transaction(trans);
9119 btrfs_btree_balance_dirty(fs_info);
9120
9121 ret = btrfs_truncate_block(inode, inode->i_size, 0, 0);
9122 if (ret)
9123 goto out;
9124 trans = btrfs_start_transaction(root, 1);
9125 if (IS_ERR(trans)) {
9126 ret = PTR_ERR(trans);
9127 goto out;
9128 }
9129 btrfs_ordered_update_i_size(inode, inode->i_size, NULL);
9130 }
9131
Chris Mason917c16b2011-11-08 14:49:59 -05009132 if (trans) {
Omar Sandovalad7e1a72018-05-22 09:59:50 -07009133 int ret2;
Josef Bacik7b128762008-07-24 12:17:14 -04009134
Omar Sandovalad7e1a72018-05-22 09:59:50 -07009135 trans->block_rsv = &fs_info->trans_block_rsv;
9136 ret2 = btrfs_update_inode(trans, root, inode);
9137 if (ret2 && !ret)
9138 ret = ret2;
9139
9140 ret2 = btrfs_end_transaction(trans);
9141 if (ret2 && !ret)
9142 ret = ret2;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009143 btrfs_btree_balance_dirty(fs_info);
Chris Mason917c16b2011-11-08 14:49:59 -05009144 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04009145out:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009146 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009147
Omar Sandovalad7e1a72018-05-22 09:59:50 -07009148 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04009149}
9150
Sven Wegener3b963622008-06-09 21:57:42 -04009151/*
Chris Masond352ac62008-09-29 15:18:18 -04009152 * create a new subvolume directory/inode (helper for the ioctl).
9153 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05009154int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009155 struct btrfs_root *new_root,
9156 struct btrfs_root *parent_root,
9157 u64 new_dirid)
Chris Mason39279cc2007-06-12 06:35:45 -04009158{
Chris Mason39279cc2007-06-12 06:35:45 -04009159 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04009160 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04009161 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009162
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01009163 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
9164 new_dirid, new_dirid,
9165 S_IFDIR | (~current_umask() & S_IRWXUGO),
9166 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04009167 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04009168 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009169 inode->i_op = &btrfs_dir_inode_operations;
9170 inode->i_fop = &btrfs_dir_file_operations;
9171
Miklos Szeredibfe86842011-10-28 14:13:29 +02009172 set_nlink(inode, 1);
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02009173 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Masonb0d5d102014-09-08 13:08:51 -07009174 unlock_new_inode(inode);
Sven Wegener3b963622008-06-09 21:57:42 -04009175
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009176 err = btrfs_subvol_inherit_props(trans, new_root, parent_root);
9177 if (err)
9178 btrfs_err(new_root->fs_info,
David Sterba351fd352014-05-15 16:48:20 +02009179 "error inheriting subvolume %llu properties: %d",
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009180 new_root->root_key.objectid, err);
9181
Yan, Zheng76dda932009-09-21 16:00:26 -04009182 err = btrfs_update_inode(trans, new_root, inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04009183
Yan, Zheng76dda932009-09-21 16:00:26 -04009184 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07009185 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04009186}
9187
Chris Mason39279cc2007-06-12 06:35:45 -04009188struct inode *btrfs_alloc_inode(struct super_block *sb)
9189{
Josef Bacik69fe2d72017-10-19 14:15:57 -04009190 struct btrfs_fs_info *fs_info = btrfs_sb(sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009191 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009192 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009193
David Sterba712e36c2017-10-31 17:08:27 +01009194 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_KERNEL);
Chris Mason39279cc2007-06-12 06:35:45 -04009195 if (!ei)
9196 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009197
9198 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009199 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04009200 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04009201 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04009202 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009203 ei->delalloc_bytes = 0;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01009204 ei->new_delalloc_bytes = 0;
Wang Shilong47059d92014-07-03 18:22:07 +08009205 ei->defrag_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009206 ei->disk_i_size = 0;
9207 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04009208 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009209 ei->index_cnt = (u64)-1;
Miao Xie67de1172013-12-26 13:07:06 +08009210 ei->dir_index = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009211 ei->last_unlink_trans = 0;
Liu Bo46d8bc32012-08-29 01:07:55 -06009212 ei->last_log_commit = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009213
Josef Bacik9e0baf62011-07-15 15:16:44 +00009214 spin_lock_init(&ei->lock);
9215 ei->outstanding_extents = 0;
Josef Bacik69fe2d72017-10-19 14:15:57 -04009216 if (sb->s_magic != BTRFS_TEST_MAGIC)
9217 btrfs_init_metadata_block_rsv(fs_info, &ei->block_rsv,
9218 BTRFS_BLOCK_RSV_DELALLOC);
Josef Bacik72ac3c02012-05-23 14:13:11 -04009219 ei->runtime_flags = 0;
David Sterbab52aa8c2017-07-17 19:17:20 +02009220 ei->prop_compress = BTRFS_COMPRESS_NONE;
David Sterbaeec63c62017-07-17 19:41:31 +02009221 ei->defrag_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009222
Miao Xie16cdcec2011-04-22 18:12:22 +08009223 ei->delayed_node = NULL;
9224
chandan r9cc97d62012-07-04 12:48:07 +05309225 ei->i_otime.tv_sec = 0;
9226 ei->i_otime.tv_nsec = 0;
9227
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009228 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02009229 extent_map_tree_init(&ei->extent_tree);
Qu Wenruo43eb5f22019-03-01 10:47:59 +08009230 extent_io_tree_init(fs_info, &ei->io_tree, IO_TREE_INODE_IO, inode);
9231 extent_io_tree_init(fs_info, &ei->io_failure_tree,
9232 IO_TREE_INODE_IO_FAILURE, inode);
David Sterba7b439732019-03-11 15:58:30 +01009233 ei->io_tree.track_uptodate = true;
9234 ei->io_failure_tree.track_uptodate = true;
Josef Bacikb812ce22012-11-16 13:56:32 -05009235 atomic_set(&ei->sync_writers, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009236 mutex_init(&ei->log_mutex);
Josef Bacikf2486792012-01-13 12:09:22 -05009237 mutex_init(&ei->delalloc_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009238 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009239 INIT_LIST_HEAD(&ei->delalloc_inodes);
David Sterba8089fe62015-11-19 14:15:51 +01009240 INIT_LIST_HEAD(&ei->delayed_iput);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009241 RB_CLEAR_NODE(&ei->rb_node);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01009242 init_rwsem(&ei->dio_sem);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009243
9244 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009245}
9246
Josef Bacikaaedb552013-10-11 14:44:09 -04009247#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
9248void btrfs_test_destroy_inode(struct inode *inode)
9249{
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02009250 btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0);
Josef Bacikaaedb552013-10-11 14:44:09 -04009251 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
9252}
9253#endif
9254
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009255static void btrfs_i_callback(struct rcu_head *head)
9256{
9257 struct inode *inode = container_of(head, struct inode, i_rcu);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009258 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
9259}
9260
Chris Mason39279cc2007-06-12 06:35:45 -04009261void btrfs_destroy_inode(struct inode *inode)
9262{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009263 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009264 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009265 struct btrfs_root *root = BTRFS_I(inode)->root;
9266
Al Virob3d9b7a2012-06-09 13:51:19 -04009267 WARN_ON(!hlist_empty(&inode->i_dentry));
Chris Mason39279cc2007-06-12 06:35:45 -04009268 WARN_ON(inode->i_data.nrpages);
Josef Bacik69fe2d72017-10-19 14:15:57 -04009269 WARN_ON(BTRFS_I(inode)->block_rsv.reserved);
9270 WARN_ON(BTRFS_I(inode)->block_rsv.size);
Josef Bacik9e0baf62011-07-15 15:16:44 +00009271 WARN_ON(BTRFS_I(inode)->outstanding_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04009272 WARN_ON(BTRFS_I(inode)->delalloc_bytes);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01009273 WARN_ON(BTRFS_I(inode)->new_delalloc_bytes);
Josef Bacik7709cde2011-08-04 10:25:02 -04009274 WARN_ON(BTRFS_I(inode)->csum_bytes);
Wang Shilong47059d92014-07-03 18:22:07 +08009275 WARN_ON(BTRFS_I(inode)->defrag_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04009276
Chris Mason5a3f23d2009-03-31 13:27:11 -04009277 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05009278 * This can happen where we create an inode, but somebody else also
9279 * created the same inode and we need to destroy the one we already
9280 * created.
9281 */
9282 if (!root)
9283 goto free;
9284
Chris Masond3977122009-01-05 21:25:51 -05009285 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04009286 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
9287 if (!ordered)
9288 break;
9289 else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009290 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04009291 "found ordered extent %llu %llu on inode cleanup",
9292 ordered->file_offset, ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009293 btrfs_remove_ordered_extent(inode, ordered);
9294 btrfs_put_ordered_extent(ordered);
9295 btrfs_put_ordered_extent(ordered);
9296 }
9297 }
Qu Wenruo56fa9d02015-10-13 09:53:10 +08009298 btrfs_qgroup_check_reserved_leak(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04009299 inode_tree_del(inode);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02009300 btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0);
Josef Bacika6dbd422009-11-11 15:53:34 -05009301free:
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009302 call_rcu(&inode->i_rcu, btrfs_i_callback);
Chris Mason39279cc2007-06-12 06:35:45 -04009303}
9304
Al Viro45321ac2010-06-07 13:43:19 -04009305int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04009306{
9307 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04009308
Naohiro Aota6379ef92013-06-06 09:56:34 +00009309 if (root == NULL)
9310 return 1;
9311
Liu Bofa6ac872013-02-20 14:10:23 +00009312 /* the snap/subvol tree is on deleting */
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02009313 if (btrfs_root_refs(&root->root_item) == 0)
Al Viro45321ac2010-06-07 13:43:19 -04009314 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04009315 else
Al Viro45321ac2010-06-07 13:43:19 -04009316 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04009317}
9318
Sven Wegener0ee0fda2008-07-30 16:54:26 -04009319static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04009320{
9321 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
9322
9323 inode_init_once(&ei->vfs_inode);
9324}
9325
David Sterbae67c7182018-02-19 17:24:18 +01009326void __cold btrfs_destroy_cachep(void)
Chris Mason39279cc2007-06-12 06:35:45 -04009327{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10009328 /*
9329 * Make sure all delayed rcu free inodes are flushed before we
9330 * destroy cache.
9331 */
9332 rcu_barrier();
Kinglong Mee5598e902016-01-29 21:36:35 +08009333 kmem_cache_destroy(btrfs_inode_cachep);
9334 kmem_cache_destroy(btrfs_trans_handle_cachep);
Kinglong Mee5598e902016-01-29 21:36:35 +08009335 kmem_cache_destroy(btrfs_path_cachep);
9336 kmem_cache_destroy(btrfs_free_space_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04009337}
9338
Liu Bof5c29bd2017-11-02 17:21:50 -06009339int __init btrfs_init_cachep(void)
Chris Mason39279cc2007-06-12 06:35:45 -04009340{
David Sterba837e1972012-09-07 03:00:48 -06009341 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009342 sizeof(struct btrfs_inode), 0,
Vladimir Davydov5d097052016-01-14 15:18:21 -08009343 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD | SLAB_ACCOUNT,
9344 init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04009345 if (!btrfs_inode_cachep)
9346 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009347
David Sterba837e1972012-09-07 03:00:48 -06009348 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009349 sizeof(struct btrfs_trans_handle), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009350 SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009351 if (!btrfs_trans_handle_cachep)
9352 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009353
David Sterba837e1972012-09-07 03:00:48 -06009354 btrfs_path_cachep = kmem_cache_create("btrfs_path",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009355 sizeof(struct btrfs_path), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009356 SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009357 if (!btrfs_path_cachep)
9358 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009359
David Sterba837e1972012-09-07 03:00:48 -06009360 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
Josef Bacikdc89e982011-01-28 17:05:48 -05009361 sizeof(struct btrfs_free_space), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009362 SLAB_MEM_SPREAD, NULL);
Josef Bacikdc89e982011-01-28 17:05:48 -05009363 if (!btrfs_free_space_cachep)
9364 goto fail;
9365
Chris Mason39279cc2007-06-12 06:35:45 -04009366 return 0;
9367fail:
9368 btrfs_destroy_cachep();
9369 return -ENOMEM;
9370}
9371
David Howellsa528d352017-01-31 16:46:22 +00009372static int btrfs_getattr(const struct path *path, struct kstat *stat,
9373 u32 request_mask, unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04009374{
Miao Xiedf0af1a2013-01-29 10:11:59 +00009375 u64 delalloc_bytes;
David Howellsa528d352017-01-31 16:46:22 +00009376 struct inode *inode = d_inode(path->dentry);
David Sterbafadc0d82011-11-20 07:33:38 -05009377 u32 blocksize = inode->i_sb->s_blocksize;
Yonghong Song04a87e32017-05-12 15:07:43 -07009378 u32 bi_flags = BTRFS_I(inode)->flags;
9379
9380 stat->result_mask |= STATX_BTIME;
9381 stat->btime.tv_sec = BTRFS_I(inode)->i_otime.tv_sec;
9382 stat->btime.tv_nsec = BTRFS_I(inode)->i_otime.tv_nsec;
9383 if (bi_flags & BTRFS_INODE_APPEND)
9384 stat->attributes |= STATX_ATTR_APPEND;
9385 if (bi_flags & BTRFS_INODE_COMPRESS)
9386 stat->attributes |= STATX_ATTR_COMPRESSED;
9387 if (bi_flags & BTRFS_INODE_IMMUTABLE)
9388 stat->attributes |= STATX_ATTR_IMMUTABLE;
9389 if (bi_flags & BTRFS_INODE_NODUMP)
9390 stat->attributes |= STATX_ATTR_NODUMP;
9391
9392 stat->attributes_mask |= (STATX_ATTR_APPEND |
9393 STATX_ATTR_COMPRESSED |
9394 STATX_ATTR_IMMUTABLE |
9395 STATX_ATTR_NODUMP);
David Sterbafadc0d82011-11-20 07:33:38 -05009396
Chris Mason39279cc2007-06-12 06:35:45 -04009397 generic_fillattr(inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04009398 stat->dev = BTRFS_I(inode)->root->anon_dev;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009399
9400 spin_lock(&BTRFS_I(inode)->lock);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01009401 delalloc_bytes = BTRFS_I(inode)->new_delalloc_bytes;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009402 spin_unlock(&BTRFS_I(inode)->lock);
David Sterbafadc0d82011-11-20 07:33:38 -05009403 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
Miao Xiedf0af1a2013-01-29 10:11:59 +00009404 ALIGN(delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04009405 return 0;
9406}
9407
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009408static int btrfs_rename_exchange(struct inode *old_dir,
9409 struct dentry *old_dentry,
9410 struct inode *new_dir,
9411 struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04009412{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009413 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009414 struct btrfs_trans_handle *trans;
9415 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009416 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009417 struct inode *new_inode = new_dentry->d_inode;
9418 struct inode *old_inode = old_dentry->d_inode;
Deepa Dinamani95582b02018-05-08 19:36:02 -07009419 struct timespec64 ctime = current_time(old_inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009420 struct dentry *parent;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009421 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
9422 u64 new_ino = btrfs_ino(BTRFS_I(new_inode));
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009423 u64 old_idx = 0;
9424 u64 new_idx = 0;
9425 u64 root_objectid;
9426 int ret;
Filipe Manana86e8aa02016-05-05 02:02:27 +01009427 bool root_log_pinned = false;
9428 bool dest_log_pinned = false;
Filipe Mananad4682ba2018-06-11 19:24:28 +01009429 struct btrfs_log_ctx ctx_root;
9430 struct btrfs_log_ctx ctx_dest;
9431 bool sync_log_root = false;
9432 bool sync_log_dest = false;
9433 bool commit_transaction = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009434
9435 /* we only allow rename subvolume link between subvolumes */
9436 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
9437 return -EXDEV;
9438
Filipe Mananad4682ba2018-06-11 19:24:28 +01009439 btrfs_init_log_ctx(&ctx_root, old_inode);
9440 btrfs_init_log_ctx(&ctx_dest, new_inode);
9441
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009442 /* close the race window with snapshot create/destroy ioctl */
9443 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009444 down_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009445 if (new_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009446 down_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009447
9448 /*
9449 * We want to reserve the absolute worst case amount of items. So if
9450 * both inodes are subvols and we need to unlink them then that would
9451 * require 4 item modifications, but if they are both normal inodes it
9452 * would require 5 item modifications, so we'll assume their normal
9453 * inodes. So 5 * 2 is 10, plus 2 for the new links, so 12 total items
9454 * should cover the worst case number of items we'll modify.
9455 */
9456 trans = btrfs_start_transaction(root, 12);
9457 if (IS_ERR(trans)) {
9458 ret = PTR_ERR(trans);
9459 goto out_notrans;
9460 }
9461
9462 /*
9463 * We need to find a free sequence number both in the source and
9464 * in the destination directory for the exchange.
9465 */
Nikolay Borisov877574e2017-02-20 13:50:33 +02009466 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009467 if (ret)
9468 goto out_fail;
Nikolay Borisov877574e2017-02-20 13:50:33 +02009469 ret = btrfs_set_inode_index(BTRFS_I(old_dir), &new_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009470 if (ret)
9471 goto out_fail;
9472
9473 BTRFS_I(old_inode)->dir_index = 0ULL;
9474 BTRFS_I(new_inode)->dir_index = 0ULL;
9475
9476 /* Reference for the source. */
9477 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
9478 /* force full log commit if subvolume involved. */
David Sterba90787762019-03-20 13:28:05 +01009479 btrfs_set_log_full_commit(trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009480 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01009481 btrfs_pin_log_trans(root);
9482 root_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009483 ret = btrfs_insert_inode_ref(trans, dest,
9484 new_dentry->d_name.name,
9485 new_dentry->d_name.len,
9486 old_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01009487 btrfs_ino(BTRFS_I(new_dir)),
9488 old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009489 if (ret)
9490 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009491 }
9492
9493 /* And now for the dest. */
9494 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
9495 /* force full log commit if subvolume involved. */
David Sterba90787762019-03-20 13:28:05 +01009496 btrfs_set_log_full_commit(trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009497 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01009498 btrfs_pin_log_trans(dest);
9499 dest_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009500 ret = btrfs_insert_inode_ref(trans, root,
9501 old_dentry->d_name.name,
9502 old_dentry->d_name.len,
9503 new_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01009504 btrfs_ino(BTRFS_I(old_dir)),
9505 new_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009506 if (ret)
9507 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009508 }
9509
9510 /* Update inode version and ctime/mtime. */
9511 inode_inc_iversion(old_dir);
9512 inode_inc_iversion(new_dir);
9513 inode_inc_iversion(old_inode);
9514 inode_inc_iversion(new_inode);
9515 old_dir->i_ctime = old_dir->i_mtime = ctime;
9516 new_dir->i_ctime = new_dir->i_mtime = ctime;
9517 old_inode->i_ctime = ctime;
9518 new_inode->i_ctime = ctime;
9519
9520 if (old_dentry->d_parent != new_dentry->d_parent) {
David Sterbaf85b7372017-01-20 14:54:07 +01009521 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
9522 BTRFS_I(old_inode), 1);
9523 btrfs_record_unlink_dir(trans, BTRFS_I(new_dir),
9524 BTRFS_I(new_inode), 1);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009525 }
9526
9527 /* src is a subvolume */
9528 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
9529 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
Lu Fengqi401b3b12018-08-01 11:32:30 +08009530 ret = btrfs_unlink_subvol(trans, old_dir, root_objectid,
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009531 old_dentry->d_name.name,
9532 old_dentry->d_name.len);
9533 } else { /* src is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009534 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
9535 BTRFS_I(old_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009536 old_dentry->d_name.name,
9537 old_dentry->d_name.len);
9538 if (!ret)
9539 ret = btrfs_update_inode(trans, root, old_inode);
9540 }
9541 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009542 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009543 goto out_fail;
9544 }
9545
9546 /* dest is a subvolume */
9547 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
9548 root_objectid = BTRFS_I(new_inode)->root->root_key.objectid;
Lu Fengqi401b3b12018-08-01 11:32:30 +08009549 ret = btrfs_unlink_subvol(trans, new_dir, root_objectid,
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009550 new_dentry->d_name.name,
9551 new_dentry->d_name.len);
9552 } else { /* dest is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009553 ret = __btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
9554 BTRFS_I(new_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009555 new_dentry->d_name.name,
9556 new_dentry->d_name.len);
9557 if (!ret)
9558 ret = btrfs_update_inode(trans, dest, new_inode);
9559 }
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
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009565 ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009566 new_dentry->d_name.name,
9567 new_dentry->d_name.len, 0, old_idx);
9568 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009569 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009570 goto out_fail;
9571 }
9572
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009573 ret = btrfs_add_link(trans, BTRFS_I(old_dir), BTRFS_I(new_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009574 old_dentry->d_name.name,
9575 old_dentry->d_name.len, 0, new_idx);
9576 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009577 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009578 goto out_fail;
9579 }
9580
9581 if (old_inode->i_nlink == 1)
9582 BTRFS_I(old_inode)->dir_index = old_idx;
9583 if (new_inode->i_nlink == 1)
9584 BTRFS_I(new_inode)->dir_index = new_idx;
9585
Filipe Manana86e8aa02016-05-05 02:02:27 +01009586 if (root_log_pinned) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009587 parent = new_dentry->d_parent;
Filipe Mananad4682ba2018-06-11 19:24:28 +01009588 ret = btrfs_log_new_name(trans, BTRFS_I(old_inode),
9589 BTRFS_I(old_dir), parent,
9590 false, &ctx_root);
9591 if (ret == BTRFS_NEED_LOG_SYNC)
9592 sync_log_root = true;
9593 else if (ret == BTRFS_NEED_TRANS_COMMIT)
9594 commit_transaction = true;
9595 ret = 0;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009596 btrfs_end_log_trans(root);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009597 root_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009598 }
Filipe Manana86e8aa02016-05-05 02:02:27 +01009599 if (dest_log_pinned) {
Filipe Mananad4682ba2018-06-11 19:24:28 +01009600 if (!commit_transaction) {
9601 parent = old_dentry->d_parent;
9602 ret = btrfs_log_new_name(trans, BTRFS_I(new_inode),
9603 BTRFS_I(new_dir), parent,
9604 false, &ctx_dest);
9605 if (ret == BTRFS_NEED_LOG_SYNC)
9606 sync_log_dest = true;
9607 else if (ret == BTRFS_NEED_TRANS_COMMIT)
9608 commit_transaction = true;
9609 ret = 0;
9610 }
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009611 btrfs_end_log_trans(dest);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009612 dest_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009613 }
9614out_fail:
Filipe Manana86e8aa02016-05-05 02:02:27 +01009615 /*
9616 * If we have pinned a log and an error happened, we unpin tasks
9617 * trying to sync the log and force them to fallback to a transaction
9618 * commit if the log currently contains any of the inodes involved in
9619 * this rename operation (to ensure we do not persist a log with an
9620 * inconsistent state for any of these inodes or leading to any
9621 * inconsistencies when replayed). If the transaction was aborted, the
9622 * abortion reason is propagated to userspace when attempting to commit
9623 * the transaction. If the log does not contain any of these inodes, we
9624 * allow the tasks to sync it.
9625 */
9626 if (ret && (root_log_pinned || dest_log_pinned)) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009627 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
9628 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
9629 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana86e8aa02016-05-05 02:02:27 +01009630 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009631 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
David Sterba90787762019-03-20 13:28:05 +01009632 btrfs_set_log_full_commit(trans);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009633
9634 if (root_log_pinned) {
9635 btrfs_end_log_trans(root);
9636 root_log_pinned = false;
9637 }
9638 if (dest_log_pinned) {
9639 btrfs_end_log_trans(dest);
9640 dest_log_pinned = false;
9641 }
9642 }
Filipe Mananad4682ba2018-06-11 19:24:28 +01009643 if (!ret && sync_log_root && !commit_transaction) {
9644 ret = btrfs_sync_log(trans, BTRFS_I(old_inode)->root,
9645 &ctx_root);
9646 if (ret)
9647 commit_transaction = true;
9648 }
9649 if (!ret && sync_log_dest && !commit_transaction) {
9650 ret = btrfs_sync_log(trans, BTRFS_I(new_inode)->root,
9651 &ctx_dest);
9652 if (ret)
9653 commit_transaction = true;
9654 }
9655 if (commit_transaction) {
9656 ret = btrfs_commit_transaction(trans);
9657 } else {
9658 int ret2;
9659
9660 ret2 = btrfs_end_transaction(trans);
9661 ret = ret ? ret : ret2;
9662 }
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009663out_notrans:
9664 if (new_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009665 up_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009666 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009667 up_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009668
9669 return ret;
9670}
9671
9672static int btrfs_whiteout_for_rename(struct btrfs_trans_handle *trans,
9673 struct btrfs_root *root,
9674 struct inode *dir,
9675 struct dentry *dentry)
9676{
9677 int ret;
9678 struct inode *inode;
9679 u64 objectid;
9680 u64 index;
9681
9682 ret = btrfs_find_free_ino(root, &objectid);
9683 if (ret)
9684 return ret;
9685
9686 inode = btrfs_new_inode(trans, root, dir,
9687 dentry->d_name.name,
9688 dentry->d_name.len,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009689 btrfs_ino(BTRFS_I(dir)),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009690 objectid,
9691 S_IFCHR | WHITEOUT_MODE,
9692 &index);
9693
9694 if (IS_ERR(inode)) {
9695 ret = PTR_ERR(inode);
9696 return ret;
9697 }
9698
9699 inode->i_op = &btrfs_special_inode_operations;
9700 init_special_inode(inode, inode->i_mode,
9701 WHITEOUT_DEV);
9702
9703 ret = btrfs_init_inode_security(trans, inode, dir,
9704 &dentry->d_name);
9705 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009706 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009707
Nikolay Borisovcef415a2017-02-20 13:51:09 +02009708 ret = btrfs_add_nondir(trans, BTRFS_I(dir), dentry,
9709 BTRFS_I(inode), 0, index);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009710 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009711 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009712
9713 ret = btrfs_update_inode(trans, root, inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009714out:
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009715 unlock_new_inode(inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009716 if (ret)
9717 inode_dec_link_count(inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009718 iput(inode);
9719
Filipe Mananac9901612016-05-05 01:41:57 +01009720 return ret;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009721}
9722
Chris Mason39279cc2007-06-12 06:35:45 -04009723static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009724 struct inode *new_dir, struct dentry *new_dentry,
9725 unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04009726{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009727 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009728 struct btrfs_trans_handle *trans;
Filipe Manana5062af32016-05-05 10:26:26 +01009729 unsigned int trans_num_items;
Chris Mason39279cc2007-06-12 06:35:45 -04009730 struct btrfs_root *root = BTRFS_I(old_dir)->root;
9731 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00009732 struct inode *new_inode = d_inode(new_dentry);
9733 struct inode *old_inode = d_inode(old_dentry);
Chris Mason00e4e6b2008-08-05 11:18:09 -04009734 u64 index = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009735 u64 root_objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04009736 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009737 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009738 bool log_pinned = false;
Filipe Mananad4682ba2018-06-11 19:24:28 +01009739 struct btrfs_log_ctx ctx;
9740 bool sync_log = false;
9741 bool commit_transaction = false;
Chris Mason39279cc2007-06-12 06:35:45 -04009742
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009743 if (btrfs_ino(BTRFS_I(new_dir)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04009744 return -EPERM;
9745
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009746 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08009747 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05009748 return -EXDEV;
9749
Li Zefan33345d012011-04-20 10:31:50 +08009750 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009751 (new_inode && btrfs_ino(BTRFS_I(new_inode)) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009752 return -ENOTEMPTY;
9753
Chris Mason39279cc2007-06-12 06:35:45 -04009754 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009755 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04009756 return -ENOTEMPTY;
Chris Mason9c520572012-12-17 14:26:57 -05009757
9758
9759 /* check for collisions, even if the name isn't there */
Josef Bacik4871c152013-10-09 12:24:04 -04009760 ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino,
Chris Mason9c520572012-12-17 14:26:57 -05009761 new_dentry->d_name.name,
9762 new_dentry->d_name.len);
9763
9764 if (ret) {
9765 if (ret == -EEXIST) {
9766 /* we shouldn't get
9767 * eexist without a new_inode */
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05309768 if (WARN_ON(!new_inode)) {
Chris Mason9c520572012-12-17 14:26:57 -05009769 return ret;
9770 }
9771 } else {
9772 /* maybe -EOVERFLOW */
9773 return ret;
9774 }
9775 }
9776 ret = 0;
9777
Chris Mason5a3f23d2009-03-31 13:27:11 -04009778 /*
Chris Mason8d875f92014-08-12 10:47:42 -07009779 * we're using rename to replace one file with another. Start IO on it
9780 * now so we don't add too much work to the end of the transaction
Chris Mason5a3f23d2009-03-31 13:27:11 -04009781 */
Chris Mason8d875f92014-08-12 10:47:42 -07009782 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size)
Chris Mason5a3f23d2009-03-31 13:27:11 -04009783 filemap_flush(old_inode->i_mapping);
9784
Yan, Zheng76dda932009-09-21 16:00:26 -04009785 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08009786 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009787 down_read(&fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04009788 /*
9789 * We want to reserve the absolute worst case amount of items. So if
9790 * both inodes are subvols and we need to unlink them then that would
9791 * require 4 item modifications, but if they are both normal inodes it
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009792 * would require 5 item modifications, so we'll assume they are normal
Yan, Zhenga22285a2010-05-16 10:48:46 -04009793 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
9794 * should cover the worst case number of items we'll modify.
Filipe Manana5062af32016-05-05 10:26:26 +01009795 * If our rename has the whiteout flag, we need more 5 units for the
9796 * new inode (1 inode item, 1 inode ref, 2 dir items and 1 xattr item
9797 * when selinux is enabled).
Yan, Zhenga22285a2010-05-16 10:48:46 -04009798 */
Filipe Manana5062af32016-05-05 10:26:26 +01009799 trans_num_items = 11;
9800 if (flags & RENAME_WHITEOUT)
9801 trans_num_items += 5;
9802 trans = btrfs_start_transaction(root, trans_num_items);
Johann Lombardib44c59a2011-03-31 13:23:47 +00009803 if (IS_ERR(trans)) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009804 ret = PTR_ERR(trans);
9805 goto out_notrans;
9806 }
Chris Mason5f39d392007-10-15 16:14:19 -04009807
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009808 if (dest != root)
9809 btrfs_record_root_in_trans(trans, dest);
9810
Nikolay Borisov877574e2017-02-20 13:50:33 +02009811 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009812 if (ret)
9813 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009814
Miao Xie67de1172013-12-26 13:07:06 +08009815 BTRFS_I(old_inode)->dir_index = 0ULL;
Li Zefan33345d012011-04-20 10:31:50 +08009816 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009817 /* force full log commit if subvolume involved. */
David Sterba90787762019-03-20 13:28:05 +01009818 btrfs_set_log_full_commit(trans);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009819 } else {
Filipe Mananac4aba952016-04-29 13:14:42 +01009820 btrfs_pin_log_trans(root);
9821 log_pinned = true;
Yan, Zhenga5719522009-09-24 09:17:31 -04009822 ret = btrfs_insert_inode_ref(trans, dest,
9823 new_dentry->d_name.name,
9824 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08009825 old_ino,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009826 btrfs_ino(BTRFS_I(new_dir)), index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009827 if (ret)
9828 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009829 }
Chris Mason5a3f23d2009-03-31 13:27:11 -04009830
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009831 inode_inc_iversion(old_dir);
9832 inode_inc_iversion(new_dir);
9833 inode_inc_iversion(old_inode);
Deepa Dinamani04b285f2016-02-06 23:57:21 -08009834 old_dir->i_ctime = old_dir->i_mtime =
9835 new_dir->i_ctime = new_dir->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009836 old_inode->i_ctime = current_time(old_dir);
Chris Mason5f39d392007-10-15 16:14:19 -04009837
Chris Mason12fcfd22009-03-24 10:24:20 -04009838 if (old_dentry->d_parent != new_dentry->d_parent)
David Sterbaf85b7372017-01-20 14:54:07 +01009839 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
9840 BTRFS_I(old_inode), 1);
Chris Mason12fcfd22009-03-24 10:24:20 -04009841
Li Zefan33345d012011-04-20 10:31:50 +08009842 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009843 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
Lu Fengqi401b3b12018-08-01 11:32:30 +08009844 ret = btrfs_unlink_subvol(trans, old_dir, root_objectid,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009845 old_dentry->d_name.name,
9846 old_dentry->d_name.len);
9847 } else {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009848 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
9849 BTRFS_I(d_inode(old_dentry)),
Al Viro92986792011-03-04 17:14:37 +00009850 old_dentry->d_name.name,
9851 old_dentry->d_name.len);
9852 if (!ret)
9853 ret = btrfs_update_inode(trans, root, old_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009854 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009855 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009856 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009857 goto out_fail;
9858 }
Chris Mason39279cc2007-06-12 06:35:45 -04009859
9860 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009861 inode_inc_iversion(new_inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009862 new_inode->i_ctime = current_time(new_inode);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009863 if (unlikely(btrfs_ino(BTRFS_I(new_inode)) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009864 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
9865 root_objectid = BTRFS_I(new_inode)->location.objectid;
Lu Fengqi401b3b12018-08-01 11:32:30 +08009866 ret = btrfs_unlink_subvol(trans, new_dir, root_objectid,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009867 new_dentry->d_name.name,
9868 new_dentry->d_name.len);
9869 BUG_ON(new_inode->i_nlink == 0);
9870 } else {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009871 ret = btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
9872 BTRFS_I(d_inode(new_dentry)),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009873 new_dentry->d_name.name,
9874 new_dentry->d_name.len);
9875 }
Josef Bacik4ef31a42013-08-13 14:10:08 -04009876 if (!ret && new_inode->i_nlink == 0)
Nikolay Borisov73f2e542017-02-20 13:50:59 +02009877 ret = btrfs_orphan_add(trans,
9878 BTRFS_I(d_inode(new_dentry)));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009879 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009880 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009881 goto out_fail;
9882 }
Chris Mason39279cc2007-06-12 06:35:45 -04009883 }
Josef Bacikaec74772008-07-24 12:12:38 -04009884
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009885 ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009886 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04009887 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009888 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009889 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009890 goto out_fail;
9891 }
Chris Mason39279cc2007-06-12 06:35:45 -04009892
Miao Xie67de1172013-12-26 13:07:06 +08009893 if (old_inode->i_nlink == 1)
9894 BTRFS_I(old_inode)->dir_index = index;
9895
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009896 if (log_pinned) {
Al Viro10d9f302011-07-16 23:09:10 -04009897 struct dentry *parent = new_dentry->d_parent;
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009898
Filipe Mananad4682ba2018-06-11 19:24:28 +01009899 btrfs_init_log_ctx(&ctx, old_inode);
9900 ret = btrfs_log_new_name(trans, BTRFS_I(old_inode),
9901 BTRFS_I(old_dir), parent,
9902 false, &ctx);
9903 if (ret == BTRFS_NEED_LOG_SYNC)
9904 sync_log = true;
9905 else if (ret == BTRFS_NEED_TRANS_COMMIT)
9906 commit_transaction = true;
9907 ret = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009908 btrfs_end_log_trans(root);
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009909 log_pinned = false;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009910 }
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009911
9912 if (flags & RENAME_WHITEOUT) {
9913 ret = btrfs_whiteout_for_rename(trans, root, old_dir,
9914 old_dentry);
9915
9916 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009917 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009918 goto out_fail;
9919 }
Chris Mason12fcfd22009-03-24 10:24:20 -04009920 }
Chris Mason39279cc2007-06-12 06:35:45 -04009921out_fail:
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009922 /*
9923 * If we have pinned the log and an error happened, we unpin tasks
9924 * trying to sync the log and force them to fallback to a transaction
9925 * commit if the log currently contains any of the inodes involved in
9926 * this rename operation (to ensure we do not persist a log with an
9927 * inconsistent state for any of these inodes or leading to any
9928 * inconsistencies when replayed). If the transaction was aborted, the
9929 * abortion reason is propagated to userspace when attempting to commit
9930 * the transaction. If the log does not contain any of these inodes, we
9931 * allow the tasks to sync it.
9932 */
9933 if (ret && log_pinned) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009934 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
9935 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
9936 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009937 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009938 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
David Sterba90787762019-03-20 13:28:05 +01009939 btrfs_set_log_full_commit(trans);
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009940
9941 btrfs_end_log_trans(root);
9942 log_pinned = false;
9943 }
Filipe Mananad4682ba2018-06-11 19:24:28 +01009944 if (!ret && sync_log) {
9945 ret = btrfs_sync_log(trans, BTRFS_I(old_inode)->root, &ctx);
9946 if (ret)
9947 commit_transaction = true;
9948 }
9949 if (commit_transaction) {
9950 ret = btrfs_commit_transaction(trans);
9951 } else {
9952 int ret2;
9953
9954 ret2 = btrfs_end_transaction(trans);
9955 ret = ret ? ret : ret2;
9956 }
Johann Lombardib44c59a2011-03-31 13:23:47 +00009957out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +08009958 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009959 up_read(&fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009960
Chris Mason39279cc2007-06-12 06:35:45 -04009961 return ret;
9962}
9963
Miklos Szeredi80ace852014-07-23 15:15:32 +02009964static int btrfs_rename2(struct inode *old_dir, struct dentry *old_dentry,
9965 struct inode *new_dir, struct dentry *new_dentry,
9966 unsigned int flags)
9967{
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009968 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
Miklos Szeredi80ace852014-07-23 15:15:32 +02009969 return -EINVAL;
9970
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009971 if (flags & RENAME_EXCHANGE)
9972 return btrfs_rename_exchange(old_dir, old_dentry, new_dir,
9973 new_dentry);
9974
9975 return btrfs_rename(old_dir, old_dentry, new_dir, new_dentry, flags);
Miklos Szeredi80ace852014-07-23 15:15:32 +02009976}
9977
Nikolay Borisov3a2f8c02018-04-24 17:23:59 +03009978struct btrfs_delalloc_work {
9979 struct inode *inode;
9980 struct completion completion;
9981 struct list_head list;
9982 struct btrfs_work work;
9983};
9984
Miao Xie8ccf6f192012-10-25 09:28:04 +00009985static void btrfs_run_delalloc_work(struct btrfs_work *work)
9986{
9987 struct btrfs_delalloc_work *delalloc_work;
Josef Bacik9f23e282013-10-28 15:03:41 -04009988 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009989
9990 delalloc_work = container_of(work, struct btrfs_delalloc_work,
9991 work);
Josef Bacik9f23e282013-10-28 15:03:41 -04009992 inode = delalloc_work->inode;
David Sterba30424602015-11-27 19:27:11 +01009993 filemap_flush(inode->i_mapping);
9994 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
9995 &BTRFS_I(inode)->runtime_flags))
Josef Bacik9f23e282013-10-28 15:03:41 -04009996 filemap_flush(inode->i_mapping);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009997
Nikolay Borisov076da912018-04-23 10:54:16 +03009998 iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009999 complete(&delalloc_work->completion);
10000}
10001
Nikolay Borisov3a2f8c02018-04-24 17:23:59 +030010002static struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode)
Miao Xie8ccf6f192012-10-25 09:28:04 +000010003{
10004 struct btrfs_delalloc_work *work;
10005
David Sterba100d5702015-12-08 14:39:32 +010010006 work = kmalloc(sizeof(*work), GFP_NOFS);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010007 if (!work)
10008 return NULL;
10009
10010 init_completion(&work->completion);
10011 INIT_LIST_HEAD(&work->list);
10012 work->inode = inode;
Liu Bo9e0af232014-08-15 23:36:53 +080010013 btrfs_init_work(&work->work, btrfs_flush_delalloc_helper,
10014 btrfs_run_delalloc_work, NULL, NULL);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010015
10016 return work;
10017}
10018
Chris Masond352ac62008-09-29 15:18:18 -040010019/*
10020 * some fairly slow code that needs optimization. This walks the list
10021 * of all the inodes with pending delalloc and forces them to disk.
10022 */
Ethan Lien3cd24c62018-11-01 14:49:03 +080010023static int start_delalloc_inodes(struct btrfs_root *root, int nr, bool snapshot)
Chris Masonea8c2812008-08-04 23:17:27 -040010024{
Chris Masonea8c2812008-08-04 23:17:27 -040010025 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010026 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010027 struct btrfs_delalloc_work *work, *next;
10028 struct list_head works;
Miao Xie1eafa6c2013-01-22 10:49:00 +000010029 struct list_head splice;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010030 int ret = 0;
Chris Masonea8c2812008-08-04 23:17:27 -040010031
Miao Xie8ccf6f192012-10-25 09:28:04 +000010032 INIT_LIST_HEAD(&works);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010033 INIT_LIST_HEAD(&splice);
Miao Xie63607cc2013-01-22 10:50:35 +000010034
Miao Xie573bfb72014-03-06 13:55:03 +080010035 mutex_lock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010036 spin_lock(&root->delalloc_lock);
10037 list_splice_init(&root->delalloc_inodes, &splice);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010038 while (!list_empty(&splice)) {
10039 binode = list_entry(splice.next, struct btrfs_inode,
Chris Masonea8c2812008-08-04 23:17:27 -040010040 delalloc_inodes);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010041
Miao Xieeb73c1b2013-05-15 07:48:22 +000010042 list_move_tail(&binode->delalloc_inodes,
10043 &root->delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010044 inode = igrab(&binode->vfs_inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +000010045 if (!inode) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010046 cond_resched_lock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010047 continue;
Miao Xiedf0af1a2013-01-29 10:11:59 +000010048 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010049 spin_unlock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010050
Ethan Lien3cd24c62018-11-01 14:49:03 +080010051 if (snapshot)
10052 set_bit(BTRFS_INODE_SNAPSHOT_FLUSH,
10053 &binode->runtime_flags);
Nikolay Borisov076da912018-04-23 10:54:16 +030010054 work = btrfs_alloc_delalloc_work(inode);
David Sterba5d99a9982014-09-29 19:20:37 +020010055 if (!work) {
Nikolay Borisov4fbb5142018-04-23 10:54:15 +030010056 iput(inode);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010057 ret = -ENOMEM;
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010058 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010059 }
Miao Xie1eafa6c2013-01-22 10:49:00 +000010060 list_add_tail(&work->list, &works);
Qu Wenruoa44903a2014-02-28 10:46:09 +080010061 btrfs_queue_work(root->fs_info->flush_workers,
10062 &work->work);
Miao Xie6c255e62014-03-06 13:55:01 +080010063 ret++;
10064 if (nr != -1 && ret >= nr)
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010065 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010066 cond_resched();
Miao Xieeb73c1b2013-05-15 07:48:22 +000010067 spin_lock(&root->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -040010068 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010069 spin_unlock(&root->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -040010070
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010071out:
Miao Xie1eafa6c2013-01-22 10:49:00 +000010072 list_for_each_entry_safe(work, next, &works, list) {
10073 list_del_init(&work->list);
Nikolay Borisov40012f92018-04-19 10:46:39 +030010074 wait_for_completion(&work->completion);
10075 kfree(work);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010076 }
10077
Nikolay Borisov81f1d392018-04-19 10:46:37 +030010078 if (!list_empty(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010079 spin_lock(&root->delalloc_lock);
10080 list_splice_tail(&splice, &root->delalloc_inodes);
10081 spin_unlock(&root->delalloc_lock);
10082 }
Miao Xie573bfb72014-03-06 13:55:03 +080010083 mutex_unlock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010084 return ret;
10085}
10086
Ethan Lien3cd24c62018-11-01 14:49:03 +080010087int btrfs_start_delalloc_snapshot(struct btrfs_root *root)
Miao Xieeb73c1b2013-05-15 07:48:22 +000010088{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010089 struct btrfs_fs_info *fs_info = root->fs_info;
Miao Xieeb73c1b2013-05-15 07:48:22 +000010090 int ret;
10091
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010092 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +000010093 return -EROFS;
10094
Ethan Lien3cd24c62018-11-01 14:49:03 +080010095 ret = start_delalloc_inodes(root, -1, true);
Miao Xie6c255e62014-03-06 13:55:01 +080010096 if (ret > 0)
10097 ret = 0;
Miao Xieeb73c1b2013-05-15 07:48:22 +000010098 return ret;
10099}
10100
Nikolay Borisov82b3e532018-04-23 10:54:13 +030010101int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, int nr)
Miao Xieeb73c1b2013-05-15 07:48:22 +000010102{
10103 struct btrfs_root *root;
10104 struct list_head splice;
10105 int ret;
10106
Wang Shilong2c21b4d2014-01-14 19:42:20 +080010107 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +000010108 return -EROFS;
10109
10110 INIT_LIST_HEAD(&splice);
10111
Miao Xie573bfb72014-03-06 13:55:03 +080010112 mutex_lock(&fs_info->delalloc_root_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010113 spin_lock(&fs_info->delalloc_root_lock);
10114 list_splice_init(&fs_info->delalloc_roots, &splice);
Miao Xie6c255e62014-03-06 13:55:01 +080010115 while (!list_empty(&splice) && nr) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010116 root = list_first_entry(&splice, struct btrfs_root,
10117 delalloc_root);
10118 root = btrfs_grab_fs_root(root);
10119 BUG_ON(!root);
10120 list_move_tail(&root->delalloc_root,
10121 &fs_info->delalloc_roots);
10122 spin_unlock(&fs_info->delalloc_root_lock);
10123
Ethan Lien3cd24c62018-11-01 14:49:03 +080010124 ret = start_delalloc_inodes(root, nr, false);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010125 btrfs_put_fs_root(root);
Miao Xie6c255e62014-03-06 13:55:01 +080010126 if (ret < 0)
Miao Xieeb73c1b2013-05-15 07:48:22 +000010127 goto out;
10128
Miao Xie6c255e62014-03-06 13:55:01 +080010129 if (nr != -1) {
10130 nr -= ret;
10131 WARN_ON(nr < 0);
10132 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010133 spin_lock(&fs_info->delalloc_root_lock);
10134 }
10135 spin_unlock(&fs_info->delalloc_root_lock);
10136
Miao Xie6c255e62014-03-06 13:55:01 +080010137 ret = 0;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010138out:
Nikolay Borisov81f1d392018-04-19 10:46:37 +030010139 if (!list_empty(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010140 spin_lock(&fs_info->delalloc_root_lock);
10141 list_splice_tail(&splice, &fs_info->delalloc_roots);
10142 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010143 }
Miao Xie573bfb72014-03-06 13:55:03 +080010144 mutex_unlock(&fs_info->delalloc_root_mutex);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010145 return ret;
Chris Masonea8c2812008-08-04 23:17:27 -040010146}
10147
Chris Mason39279cc2007-06-12 06:35:45 -040010148static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
10149 const char *symname)
10150{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010151 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -040010152 struct btrfs_trans_handle *trans;
10153 struct btrfs_root *root = BTRFS_I(dir)->root;
10154 struct btrfs_path *path;
10155 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -050010156 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -040010157 int err;
Chris Mason39279cc2007-06-12 06:35:45 -040010158 u64 objectid;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +053010159 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -040010160 int name_len;
10161 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -040010162 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -040010163 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -040010164 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -040010165
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +010010166 name_len = strlen(symname);
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010167 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info))
Chris Mason39279cc2007-06-12 06:35:45 -040010168 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -050010169
Josef Bacik9ed74f22009-09-11 16:12:44 -040010170 /*
10171 * 2 items for inode item and ref
10172 * 2 items for dir items
Filipe Manana9269d122015-12-31 18:16:29 +000010173 * 1 item for updating parent inode item
10174 * 1 item for the inline extent item
Josef Bacik9ed74f22009-09-11 16:12:44 -040010175 * 1 item for xattr if selinux is on
10176 */
Filipe Manana9269d122015-12-31 18:16:29 +000010177 trans = btrfs_start_transaction(root, 7);
Yan, Zhenga22285a2010-05-16 10:48:46 -040010178 if (IS_ERR(trans))
10179 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -050010180
Li Zefan581bb052011-04-20 10:06:11 +080010181 err = btrfs_find_free_ino(root, &objectid);
10182 if (err)
10183 goto out_unlock;
10184
Josef Bacikaec74772008-07-24 12:12:38 -040010185 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +010010186 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)),
10187 objectid, S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -040010188 if (IS_ERR(inode)) {
10189 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -040010190 inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -040010191 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -040010192 }
Chris Mason39279cc2007-06-12 06:35:45 -040010193
Casey Schauflerad19db72011-12-15 10:09:07 -050010194 /*
10195 * If the active LSM wants to access the inode during
10196 * d_instantiate it needs these. Smack checks to see
10197 * if the filesystem supports xattrs by looking at the
10198 * ops vector.
10199 */
10200 inode->i_fop = &btrfs_file_operations;
10201 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -070010202 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -070010203 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
10204
10205 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
10206 if (err)
Al Viro32955c52018-05-16 12:20:05 -040010207 goto out_unlock;
Casey Schauflerad19db72011-12-15 10:09:07 -050010208
Chris Mason39279cc2007-06-12 06:35:45 -040010209 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -070010210 if (!path) {
10211 err = -ENOMEM;
Al Viro32955c52018-05-16 12:20:05 -040010212 goto out_unlock;
Mark Fashehd8926bb2011-07-13 10:38:47 -070010213 }
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +020010214 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason39279cc2007-06-12 06:35:45 -040010215 key.offset = 0;
David Sterba962a2982014-06-04 18:41:45 +020010216 key.type = BTRFS_EXTENT_DATA_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -040010217 datasize = btrfs_file_extent_calc_inline_size(name_len);
10218 err = btrfs_insert_empty_item(trans, root, path, &key,
10219 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -040010220 if (err) {
Julia Lawallb0839162011-05-14 07:10:51 +000010221 btrfs_free_path(path);
Al Viro32955c52018-05-16 12:20:05 -040010222 goto out_unlock;
Chris Mason54aa1f42007-06-22 14:16:25 -040010223 }
Chris Mason5f39d392007-10-15 16:14:19 -040010224 leaf = path->nodes[0];
10225 ei = btrfs_item_ptr(leaf, path->slots[0],
10226 struct btrfs_file_extent_item);
10227 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
10228 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -040010229 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -040010230 btrfs_set_file_extent_encryption(leaf, ei, 0);
10231 btrfs_set_file_extent_compression(leaf, ei, 0);
10232 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
10233 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
10234
Chris Mason39279cc2007-06-12 06:35:45 -040010235 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -040010236 write_extent_buffer(leaf, symname, ptr, name_len);
10237 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -040010238 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -040010239
Chris Mason39279cc2007-06-12 06:35:45 -040010240 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -050010241 inode_nohighmem(inode);
Yan Zhengd899e052008-10-30 14:25:28 -040010242 inode_set_bytes(inode, name_len);
Nikolay Borisov6ef06d22017-02-20 13:50:34 +020010243 btrfs_i_size_write(BTRFS_I(inode), name_len);
Chris Mason54aa1f42007-06-22 14:16:25 -040010244 err = btrfs_update_inode(trans, root, inode);
Filipe Mananad50866d2015-12-31 18:08:24 +000010245 /*
10246 * Last step, add directory indexes for our symlink inode. This is the
10247 * last step to avoid extra cleanup of these indexes if an error happens
10248 * elsewhere above.
10249 */
10250 if (!err)
Nikolay Borisovcef415a2017-02-20 13:51:09 +020010251 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry,
10252 BTRFS_I(inode), 0, index);
Al Viro32955c52018-05-16 12:20:05 -040010253 if (err)
10254 goto out_unlock;
Chris Masonb0d5d102014-09-08 13:08:51 -070010255
Al Viro1e2e5472018-05-04 08:23:01 -040010256 d_instantiate_new(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010257
10258out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010259 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -040010260 if (err && inode) {
Chris Mason39279cc2007-06-12 06:35:45 -040010261 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -040010262 discard_new_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010263 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010264 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -040010265 return err;
10266}
Chris Mason16432982008-04-10 10:23:21 -040010267
Josef Bacik0af3d002010-06-21 14:48:16 -040010268static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
10269 u64 start, u64 num_bytes, u64 min_size,
10270 loff_t actual_len, u64 *alloc_hint,
10271 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -040010272{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010273 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik5dc562c2012-08-17 13:14:17 -040010274 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
10275 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -040010276 struct btrfs_root *root = BTRFS_I(inode)->root;
10277 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -040010278 u64 cur_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +000010279 u64 i_size;
Chris Mason154ea282013-03-05 11:11:26 -050010280 u64 cur_bytes;
Josef Bacik0b670dc2015-09-23 17:11:16 -040010281 u64 last_alloc = (u64)-1;
Yan Zhengd899e052008-10-30 14:25:28 -040010282 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -040010283 bool own_trans = true;
Wang Xiaoguang18513092016-07-25 15:51:40 +080010284 u64 end = start + num_bytes - 1;
Yan Zhengd899e052008-10-30 14:25:28 -040010285
Josef Bacik0af3d002010-06-21 14:48:16 -040010286 if (trans)
10287 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -040010288 while (num_bytes > 0) {
Josef Bacik0af3d002010-06-21 14:48:16 -040010289 if (own_trans) {
10290 trans = btrfs_start_transaction(root, 3);
10291 if (IS_ERR(trans)) {
10292 ret = PTR_ERR(trans);
10293 break;
10294 }
Yan Zhengd899e052008-10-30 14:25:28 -040010295 }
Yan, Zheng5a303d52009-11-12 09:34:52 +000010296
Byongho Leeee221842015-12-15 01:42:10 +090010297 cur_bytes = min_t(u64, num_bytes, SZ_256M);
Chris Mason154ea282013-03-05 11:11:26 -050010298 cur_bytes = max(cur_bytes, min_size);
Josef Bacik0b670dc2015-09-23 17:11:16 -040010299 /*
10300 * If we are severely fragmented we could end up with really
10301 * small allocations, so if the allocator is returning small
10302 * chunks lets make its job easier by only searching for those
10303 * sized chunks.
10304 */
10305 cur_bytes = min(cur_bytes, last_alloc);
Wang Xiaoguang18513092016-07-25 15:51:40 +080010306 ret = btrfs_reserve_extent(root, cur_bytes, cur_bytes,
10307 min_size, 0, *alloc_hint, &ins, 1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010308 if (ret) {
Josef Bacik0af3d002010-06-21 14:48:16 -040010309 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010310 btrfs_end_transaction(trans);
Yan, Zhenga22285a2010-05-16 10:48:46 -040010311 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +000010312 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010313 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010314
Josef Bacik0b670dc2015-09-23 17:11:16 -040010315 last_alloc = ins.offset;
Yan Zhengd899e052008-10-30 14:25:28 -040010316 ret = insert_reserved_file_extent(trans, inode,
10317 cur_offset, ins.objectid,
10318 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +000010319 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -040010320 BTRFS_FILE_EXTENT_PREALLOC);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010321 if (ret) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010322 btrfs_free_reserved_extent(fs_info, ins.objectid,
Miao Xiee570fd22014-06-19 10:42:50 +080010323 ins.offset, 0);
Jeff Mahoney66642832016-06-10 18:19:25 -040010324 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010325 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010326 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010327 break;
10328 }
Dongsheng Yang31193212014-12-12 16:44:35 +080010329
Nikolay Borisovdcdbc052017-02-20 13:50:45 +020010330 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Chris Masona1ed8352009-09-11 12:27:37 -040010331 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010332
Josef Bacik5dc562c2012-08-17 13:14:17 -040010333 em = alloc_extent_map();
10334 if (!em) {
10335 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
10336 &BTRFS_I(inode)->runtime_flags);
10337 goto next;
10338 }
10339
10340 em->start = cur_offset;
10341 em->orig_start = cur_offset;
10342 em->len = ins.offset;
10343 em->block_start = ins.objectid;
10344 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -050010345 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -040010346 em->ram_bytes = ins.offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010347 em->bdev = fs_info->fs_devices->latest_bdev;
Josef Bacik5dc562c2012-08-17 13:14:17 -040010348 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
10349 em->generation = trans->transid;
10350
10351 while (1) {
10352 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -040010353 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -040010354 write_unlock(&em_tree->lock);
10355 if (ret != -EEXIST)
10356 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +020010357 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -040010358 cur_offset + ins.offset - 1,
10359 0);
10360 }
10361 free_extent_map(em);
10362next:
Yan Zhengd899e052008-10-30 14:25:28 -040010363 num_bytes -= ins.offset;
10364 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -040010365 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +000010366
Josef Bacik0c4d2d92012-04-05 15:03:02 -040010367 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -070010368 inode->i_ctime = current_time(inode);
Christoph Hellwig6cbff002009-04-17 10:37:41 +020010369 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -040010370 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -040010371 (actual_len > inode->i_size) &&
10372 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +000010373 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +000010374 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +000010375 else
Josef Bacik55a61d12010-11-22 18:50:32 +000010376 i_size = cur_offset;
10377 i_size_write(inode, i_size);
10378 btrfs_ordered_update_i_size(inode, i_size, NULL);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010379 }
10380
Yan Zhengd899e052008-10-30 14:25:28 -040010381 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010382
10383 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -040010384 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010385 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010386 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010387 break;
10388 }
Yan Zhengd899e052008-10-30 14:25:28 -040010389
Josef Bacik0af3d002010-06-21 14:48:16 -040010390 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010391 btrfs_end_transaction(trans);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010392 }
Wang Xiaoguang18513092016-07-25 15:51:40 +080010393 if (cur_offset < end)
Qu Wenruobc42bda2017-02-27 15:10:39 +080010394 btrfs_free_reserved_data_space(inode, NULL, cur_offset,
Wang Xiaoguang18513092016-07-25 15:51:40 +080010395 end - cur_offset + 1);
Yan Zhengd899e052008-10-30 14:25:28 -040010396 return ret;
10397}
10398
Josef Bacik0af3d002010-06-21 14:48:16 -040010399int btrfs_prealloc_file_range(struct inode *inode, int mode,
10400 u64 start, u64 num_bytes, u64 min_size,
10401 loff_t actual_len, u64 *alloc_hint)
10402{
10403 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
10404 min_size, actual_len, alloc_hint,
10405 NULL);
10406}
10407
10408int btrfs_prealloc_file_range_trans(struct inode *inode,
10409 struct btrfs_trans_handle *trans, int mode,
10410 u64 start, u64 num_bytes, u64 min_size,
10411 loff_t actual_len, u64 *alloc_hint)
10412{
10413 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
10414 min_size, actual_len, alloc_hint, trans);
10415}
10416
Chris Masone6dcd2d2008-07-17 12:53:50 -040010417static int btrfs_set_page_dirty(struct page *page)
10418{
Chris Masone6dcd2d2008-07-17 12:53:50 -040010419 return __set_page_dirty_nobuffers(page);
10420}
10421
Al Viro10556cb22011-06-20 19:28:19 -040010422static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -050010423{
Li Zefanb83cc962010-12-20 16:04:08 +080010424 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +000010425 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +080010426
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +000010427 if (mask & MAY_WRITE &&
10428 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
10429 if (btrfs_root_readonly(root))
10430 return -EROFS;
10431 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
10432 return -EACCES;
10433 }
Al Viro2830ba72011-06-20 19:16:29 -040010434 return generic_permission(inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -050010435}
Chris Mason39279cc2007-06-12 06:35:45 -040010436
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010437static int btrfs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
10438{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010439 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010440 struct btrfs_trans_handle *trans;
10441 struct btrfs_root *root = BTRFS_I(dir)->root;
10442 struct inode *inode = NULL;
10443 u64 objectid;
10444 u64 index;
10445 int ret = 0;
10446
10447 /*
10448 * 5 units required for adding orphan entry
10449 */
10450 trans = btrfs_start_transaction(root, 5);
10451 if (IS_ERR(trans))
10452 return PTR_ERR(trans);
10453
10454 ret = btrfs_find_free_ino(root, &objectid);
10455 if (ret)
10456 goto out;
10457
10458 inode = btrfs_new_inode(trans, root, dir, NULL, 0,
David Sterbaf85b7372017-01-20 14:54:07 +010010459 btrfs_ino(BTRFS_I(dir)), objectid, mode, &index);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010460 if (IS_ERR(inode)) {
10461 ret = PTR_ERR(inode);
10462 inode = NULL;
10463 goto out;
10464 }
10465
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010466 inode->i_fop = &btrfs_file_operations;
10467 inode->i_op = &btrfs_file_inode_operations;
10468
10469 inode->i_mapping->a_ops = &btrfs_aops;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010470 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
10471
Chris Masonb0d5d102014-09-08 13:08:51 -070010472 ret = btrfs_init_inode_security(trans, inode, dir, NULL);
10473 if (ret)
Al Viro32955c52018-05-16 12:20:05 -040010474 goto out;
Chris Masonb0d5d102014-09-08 13:08:51 -070010475
10476 ret = btrfs_update_inode(trans, root, inode);
10477 if (ret)
Al Viro32955c52018-05-16 12:20:05 -040010478 goto out;
Nikolay Borisov73f2e542017-02-20 13:50:59 +020010479 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010480 if (ret)
Al Viro32955c52018-05-16 12:20:05 -040010481 goto out;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010482
Filipe Manana5762b5c2014-08-01 00:10:32 +010010483 /*
10484 * We set number of links to 0 in btrfs_new_inode(), and here we set
10485 * it to 1 because d_tmpfile() will issue a warning if the count is 0,
10486 * through:
10487 *
10488 * d_tmpfile() -> inode_dec_link_count() -> drop_nlink()
10489 */
10490 set_nlink(inode, 1);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010491 d_tmpfile(dentry, inode);
Al Viro32955c52018-05-16 12:20:05 -040010492 unlock_new_inode(inode);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010493 mark_inode_dirty(inode);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010494out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010495 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -040010496 if (ret && inode)
10497 discard_new_inode(inode);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010498 btrfs_btree_balance_dirty(fs_info);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010499 return ret;
10500}
10501
David Sterba5cdc84b2018-07-18 20:32:52 +020010502void btrfs_set_range_writeback(struct extent_io_tree *tree, u64 start, u64 end)
Josef Bacikc6100a42017-05-05 11:57:13 -040010503{
David Sterba5cdc84b2018-07-18 20:32:52 +020010504 struct inode *inode = tree->private_data;
Josef Bacikc6100a42017-05-05 11:57:13 -040010505 unsigned long index = start >> PAGE_SHIFT;
10506 unsigned long end_index = end >> PAGE_SHIFT;
10507 struct page *page;
10508
10509 while (index <= end_index) {
10510 page = find_get_page(inode->i_mapping, index);
10511 ASSERT(page); /* Pages should be in the extent_io_tree */
10512 set_page_writeback(page);
10513 put_page(page);
10514 index++;
10515 }
10516}
10517
Omar Sandovaled46ff32016-11-03 10:28:14 -070010518#ifdef CONFIG_SWAP
10519/*
10520 * Add an entry indicating a block group or device which is pinned by a
10521 * swapfile. Returns 0 on success, 1 if there is already an entry for it, or a
10522 * negative errno on failure.
10523 */
10524static int btrfs_add_swapfile_pin(struct inode *inode, void *ptr,
10525 bool is_block_group)
10526{
10527 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
10528 struct btrfs_swapfile_pin *sp, *entry;
10529 struct rb_node **p;
10530 struct rb_node *parent = NULL;
10531
10532 sp = kmalloc(sizeof(*sp), GFP_NOFS);
10533 if (!sp)
10534 return -ENOMEM;
10535 sp->ptr = ptr;
10536 sp->inode = inode;
10537 sp->is_block_group = is_block_group;
10538
10539 spin_lock(&fs_info->swapfile_pins_lock);
10540 p = &fs_info->swapfile_pins.rb_node;
10541 while (*p) {
10542 parent = *p;
10543 entry = rb_entry(parent, struct btrfs_swapfile_pin, node);
10544 if (sp->ptr < entry->ptr ||
10545 (sp->ptr == entry->ptr && sp->inode < entry->inode)) {
10546 p = &(*p)->rb_left;
10547 } else if (sp->ptr > entry->ptr ||
10548 (sp->ptr == entry->ptr && sp->inode > entry->inode)) {
10549 p = &(*p)->rb_right;
10550 } else {
10551 spin_unlock(&fs_info->swapfile_pins_lock);
10552 kfree(sp);
10553 return 1;
10554 }
10555 }
10556 rb_link_node(&sp->node, parent, p);
10557 rb_insert_color(&sp->node, &fs_info->swapfile_pins);
10558 spin_unlock(&fs_info->swapfile_pins_lock);
10559 return 0;
10560}
10561
10562/* Free all of the entries pinned by this swapfile. */
10563static void btrfs_free_swapfile_pins(struct inode *inode)
10564{
10565 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
10566 struct btrfs_swapfile_pin *sp;
10567 struct rb_node *node, *next;
10568
10569 spin_lock(&fs_info->swapfile_pins_lock);
10570 node = rb_first(&fs_info->swapfile_pins);
10571 while (node) {
10572 next = rb_next(node);
10573 sp = rb_entry(node, struct btrfs_swapfile_pin, node);
10574 if (sp->inode == inode) {
10575 rb_erase(&sp->node, &fs_info->swapfile_pins);
10576 if (sp->is_block_group)
10577 btrfs_put_block_group(sp->ptr);
10578 kfree(sp);
10579 }
10580 node = next;
10581 }
10582 spin_unlock(&fs_info->swapfile_pins_lock);
10583}
10584
10585struct btrfs_swap_info {
10586 u64 start;
10587 u64 block_start;
10588 u64 block_len;
10589 u64 lowest_ppage;
10590 u64 highest_ppage;
10591 unsigned long nr_pages;
10592 int nr_extents;
10593};
10594
10595static int btrfs_add_swap_extent(struct swap_info_struct *sis,
10596 struct btrfs_swap_info *bsi)
10597{
10598 unsigned long nr_pages;
10599 u64 first_ppage, first_ppage_reported, next_ppage;
10600 int ret;
10601
10602 first_ppage = ALIGN(bsi->block_start, PAGE_SIZE) >> PAGE_SHIFT;
10603 next_ppage = ALIGN_DOWN(bsi->block_start + bsi->block_len,
10604 PAGE_SIZE) >> PAGE_SHIFT;
10605
10606 if (first_ppage >= next_ppage)
10607 return 0;
10608 nr_pages = next_ppage - first_ppage;
10609
10610 first_ppage_reported = first_ppage;
10611 if (bsi->start == 0)
10612 first_ppage_reported++;
10613 if (bsi->lowest_ppage > first_ppage_reported)
10614 bsi->lowest_ppage = first_ppage_reported;
10615 if (bsi->highest_ppage < (next_ppage - 1))
10616 bsi->highest_ppage = next_ppage - 1;
10617
10618 ret = add_swap_extent(sis, bsi->nr_pages, nr_pages, first_ppage);
10619 if (ret < 0)
10620 return ret;
10621 bsi->nr_extents += ret;
10622 bsi->nr_pages += nr_pages;
10623 return 0;
10624}
10625
10626static void btrfs_swap_deactivate(struct file *file)
10627{
10628 struct inode *inode = file_inode(file);
10629
10630 btrfs_free_swapfile_pins(inode);
10631 atomic_dec(&BTRFS_I(inode)->root->nr_swapfiles);
10632}
10633
10634static int btrfs_swap_activate(struct swap_info_struct *sis, struct file *file,
10635 sector_t *span)
10636{
10637 struct inode *inode = file_inode(file);
10638 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
10639 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
10640 struct extent_state *cached_state = NULL;
10641 struct extent_map *em = NULL;
10642 struct btrfs_device *device = NULL;
10643 struct btrfs_swap_info bsi = {
10644 .lowest_ppage = (sector_t)-1ULL,
10645 };
10646 int ret = 0;
10647 u64 isize;
10648 u64 start;
10649
10650 /*
10651 * If the swap file was just created, make sure delalloc is done. If the
10652 * file changes again after this, the user is doing something stupid and
10653 * we don't really care.
10654 */
10655 ret = btrfs_wait_ordered_range(inode, 0, (u64)-1);
10656 if (ret)
10657 return ret;
10658
10659 /*
10660 * The inode is locked, so these flags won't change after we check them.
10661 */
10662 if (BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS) {
10663 btrfs_warn(fs_info, "swapfile must not be compressed");
10664 return -EINVAL;
10665 }
10666 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW)) {
10667 btrfs_warn(fs_info, "swapfile must not be copy-on-write");
10668 return -EINVAL;
10669 }
10670 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
10671 btrfs_warn(fs_info, "swapfile must not be checksummed");
10672 return -EINVAL;
10673 }
10674
10675 /*
10676 * Balance or device remove/replace/resize can move stuff around from
10677 * under us. The EXCL_OP flag makes sure they aren't running/won't run
10678 * concurrently while we are mapping the swap extents, and
10679 * fs_info->swapfile_pins prevents them from running while the swap file
10680 * is active and moving the extents. Note that this also prevents a
10681 * concurrent device add which isn't actually necessary, but it's not
10682 * really worth the trouble to allow it.
10683 */
10684 if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) {
10685 btrfs_warn(fs_info,
10686 "cannot activate swapfile while exclusive operation is running");
10687 return -EBUSY;
10688 }
10689 /*
10690 * Snapshots can create extents which require COW even if NODATACOW is
10691 * set. We use this counter to prevent snapshots. We must increment it
10692 * before walking the extents because we don't want a concurrent
10693 * snapshot to run after we've already checked the extents.
10694 */
10695 atomic_inc(&BTRFS_I(inode)->root->nr_swapfiles);
10696
10697 isize = ALIGN_DOWN(inode->i_size, fs_info->sectorsize);
10698
10699 lock_extent_bits(io_tree, 0, isize - 1, &cached_state);
10700 start = 0;
10701 while (start < isize) {
10702 u64 logical_block_start, physical_block_start;
10703 struct btrfs_block_group_cache *bg;
10704 u64 len = isize - start;
10705
10706 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len, 0);
10707 if (IS_ERR(em)) {
10708 ret = PTR_ERR(em);
10709 goto out;
10710 }
10711
10712 if (em->block_start == EXTENT_MAP_HOLE) {
10713 btrfs_warn(fs_info, "swapfile must not have holes");
10714 ret = -EINVAL;
10715 goto out;
10716 }
10717 if (em->block_start == EXTENT_MAP_INLINE) {
10718 /*
10719 * It's unlikely we'll ever actually find ourselves
10720 * here, as a file small enough to fit inline won't be
10721 * big enough to store more than the swap header, but in
10722 * case something changes in the future, let's catch it
10723 * here rather than later.
10724 */
10725 btrfs_warn(fs_info, "swapfile must not be inline");
10726 ret = -EINVAL;
10727 goto out;
10728 }
10729 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
10730 btrfs_warn(fs_info, "swapfile must not be compressed");
10731 ret = -EINVAL;
10732 goto out;
10733 }
10734
10735 logical_block_start = em->block_start + (start - em->start);
10736 len = min(len, em->len - (start - em->start));
10737 free_extent_map(em);
10738 em = NULL;
10739
10740 ret = can_nocow_extent(inode, start, &len, NULL, NULL, NULL);
10741 if (ret < 0) {
10742 goto out;
10743 } else if (ret) {
10744 ret = 0;
10745 } else {
10746 btrfs_warn(fs_info,
10747 "swapfile must not be copy-on-write");
10748 ret = -EINVAL;
10749 goto out;
10750 }
10751
10752 em = btrfs_get_chunk_map(fs_info, logical_block_start, len);
10753 if (IS_ERR(em)) {
10754 ret = PTR_ERR(em);
10755 goto out;
10756 }
10757
10758 if (em->map_lookup->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
10759 btrfs_warn(fs_info,
10760 "swapfile must have single data profile");
10761 ret = -EINVAL;
10762 goto out;
10763 }
10764
10765 if (device == NULL) {
10766 device = em->map_lookup->stripes[0].dev;
10767 ret = btrfs_add_swapfile_pin(inode, device, false);
10768 if (ret == 1)
10769 ret = 0;
10770 else if (ret)
10771 goto out;
10772 } else if (device != em->map_lookup->stripes[0].dev) {
10773 btrfs_warn(fs_info, "swapfile must be on one device");
10774 ret = -EINVAL;
10775 goto out;
10776 }
10777
10778 physical_block_start = (em->map_lookup->stripes[0].physical +
10779 (logical_block_start - em->start));
10780 len = min(len, em->len - (logical_block_start - em->start));
10781 free_extent_map(em);
10782 em = NULL;
10783
10784 bg = btrfs_lookup_block_group(fs_info, logical_block_start);
10785 if (!bg) {
10786 btrfs_warn(fs_info,
10787 "could not find block group containing swapfile");
10788 ret = -EINVAL;
10789 goto out;
10790 }
10791
10792 ret = btrfs_add_swapfile_pin(inode, bg, true);
10793 if (ret) {
10794 btrfs_put_block_group(bg);
10795 if (ret == 1)
10796 ret = 0;
10797 else
10798 goto out;
10799 }
10800
10801 if (bsi.block_len &&
10802 bsi.block_start + bsi.block_len == physical_block_start) {
10803 bsi.block_len += len;
10804 } else {
10805 if (bsi.block_len) {
10806 ret = btrfs_add_swap_extent(sis, &bsi);
10807 if (ret)
10808 goto out;
10809 }
10810 bsi.start = start;
10811 bsi.block_start = physical_block_start;
10812 bsi.block_len = len;
10813 }
10814
10815 start += len;
10816 }
10817
10818 if (bsi.block_len)
10819 ret = btrfs_add_swap_extent(sis, &bsi);
10820
10821out:
10822 if (!IS_ERR_OR_NULL(em))
10823 free_extent_map(em);
10824
10825 unlock_extent_cached(io_tree, 0, isize - 1, &cached_state);
10826
10827 if (ret)
10828 btrfs_swap_deactivate(file);
10829
10830 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
10831
10832 if (ret)
10833 return ret;
10834
10835 if (device)
10836 sis->bdev = device->bdev;
10837 *span = bsi.highest_ppage - bsi.lowest_ppage + 1;
10838 sis->max = bsi.nr_pages;
10839 sis->pages = bsi.nr_pages - 1;
10840 sis->highest_bit = bsi.nr_pages - 1;
10841 return bsi.nr_extents;
10842}
10843#else
10844static void btrfs_swap_deactivate(struct file *file)
10845{
10846}
10847
10848static int btrfs_swap_activate(struct swap_info_struct *sis, struct file *file,
10849 sector_t *span)
10850{
10851 return -EOPNOTSUPP;
10852}
10853#endif
10854
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010855static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -050010856 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -040010857 .lookup = btrfs_lookup,
10858 .create = btrfs_create,
10859 .unlink = btrfs_unlink,
10860 .link = btrfs_link,
10861 .mkdir = btrfs_mkdir,
10862 .rmdir = btrfs_rmdir,
Miklos Szeredi2773bf02016-09-27 11:03:58 +020010863 .rename = btrfs_rename2,
Chris Mason39279cc2007-06-12 06:35:45 -040010864 .symlink = btrfs_symlink,
10865 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -040010866 .mknod = btrfs_mknod,
Josef Bacik5103e942007-11-16 11:45:54 -050010867 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010868 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010869 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010870 .set_acl = btrfs_set_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070010871 .update_time = btrfs_update_time,
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010872 .tmpfile = btrfs_tmpfile,
Chris Mason39279cc2007-06-12 06:35:45 -040010873};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010874static const struct inode_operations btrfs_dir_ro_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010875 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -050010876 .permission = btrfs_permission,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070010877 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010878};
Yan, Zheng76dda932009-09-21 16:00:26 -040010879
Alexey Dobriyan828c0952009-10-01 15:43:56 -070010880static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010881 .llseek = generic_file_llseek,
10882 .read = generic_read_dir,
Omar Sandoval02dbfc92016-05-20 13:50:33 -070010883 .iterate_shared = btrfs_real_readdir,
Josef Bacik23b5ec72017-07-24 15:14:25 -040010884 .open = btrfs_opendir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -040010885 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010886#ifdef CONFIG_COMPAT
Luke Dashjr4c63c242015-10-29 08:22:21 +000010887 .compat_ioctl = btrfs_compat_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010888#endif
Sage Weil6bf13c02008-06-10 10:07:39 -040010889 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -040010890 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -040010891};
10892
David Sterba20e55062015-11-19 11:42:28 +010010893static const struct extent_io_ops btrfs_extent_io_ops = {
David Sterba4d53ddd2017-02-17 15:27:44 +010010894 /* mandatory callbacks */
Chris Mason065631f2008-02-20 12:07:25 -050010895 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -040010896 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
10897};
10898
Chris Mason35054392009-01-21 13:11:13 -050010899/*
10900 * btrfs doesn't support the bmap operation because swapfiles
10901 * use bmap to make a mapping of extents in the file. They assume
10902 * these extents won't change over the life of the file and they
10903 * use the bmap result to do IO directly to the drive.
10904 *
10905 * the btrfs bmap call would return logical addresses that aren't
10906 * suitable for IO and they also will change frequently as COW
10907 * operations happen. So, swapfile + btrfs == corruption.
10908 *
10909 * For now we're avoiding this by dropping bmap.
10910 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -070010911static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -040010912 .readpage = btrfs_readpage,
10913 .writepage = btrfs_writepage,
Chris Masonb293f02e2007-11-01 19:45:34 -040010914 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -050010915 .readpages = btrfs_readpages,
Chris Mason16432982008-04-10 10:23:21 -040010916 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -040010917 .invalidatepage = btrfs_invalidatepage,
10918 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -040010919 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +020010920 .error_remove_page = generic_error_remove_page,
Omar Sandovaled46ff32016-11-03 10:28:14 -070010921 .swap_activate = btrfs_swap_activate,
10922 .swap_deactivate = btrfs_swap_deactivate,
Chris Mason39279cc2007-06-12 06:35:45 -040010923};
10924
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010925static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010926 .getattr = btrfs_getattr,
10927 .setattr = btrfs_setattr,
Josef Bacik5103e942007-11-16 11:45:54 -050010928 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010929 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -050010930 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010931 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010932 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010933 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010934};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010935static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -040010936 .getattr = btrfs_getattr,
10937 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010938 .permission = btrfs_permission,
Josef Bacik33268ea2008-07-24 12:16:36 -040010939 .listxattr = btrfs_listxattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010940 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010941 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010942 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -040010943};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010944static const struct inode_operations btrfs_symlink_inode_operations = {
Al Viro6b255392015-11-17 10:20:54 -050010945 .get_link = page_get_link,
Li Zefanf2095612010-11-19 02:05:24 +000010946 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -050010947 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010948 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -050010949 .listxattr = btrfs_listxattr,
Josef Bacike41f9412012-03-26 09:46:47 -040010950 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010951};
Yan, Zheng76dda932009-09-21 16:00:26 -040010952
Alexey Dobriyan82d339d2009-10-09 09:54:36 -040010953const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -040010954 .d_delete = btrfs_dentry_delete,
10955};