blob: 861105e11b377e480a8d7c87fa2e53e7e4c806ba [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>
Nikolay Borisovb1c16ac2019-04-01 11:29:57 +030031#include <linux/sched/mm.h>
David Sterba92d32172018-04-16 21:10:14 +020032#include <asm/unaligned.h>
David Sterba602cbe92019-08-21 18:48:25 +020033#include "misc.h"
Chris Mason39279cc2007-06-12 06:35:45 -040034#include "ctree.h"
35#include "disk-io.h"
36#include "transaction.h"
37#include "btrfs_inode.h"
Chris Mason39279cc2007-06-12 06:35:45 -040038#include "print-tree.h"
Chris Masone6dcd2d2008-07-17 12:53:50 -040039#include "ordered-data.h"
Christoph Hellwig95819c02008-08-28 06:21:17 -040040#include "xattr.h"
Chris Masone02119d2008-09-05 16:13:11 -040041#include "tree-log.h"
Jan Schmidt4a54c8c2011-07-22 15:41:52 +020042#include "volumes.h"
Chris Masonc8b97812008-10-29 14:49:59 -040043#include "compression.h"
Chris Masonb4ce94d2009-02-04 09:25:08 -050044#include "locking.h"
Josef Bacikdc89e982011-01-28 17:05:48 -050045#include "free-space-cache.h"
Li Zefan581bb052011-04-20 10:06:11 +080046#include "inode-map.h"
Liu Bo38c227d2013-01-29 03:18:40 +000047#include "backref.h"
Filipe David Borba Manana63541922014-01-07 11:47:46 +000048#include "props.h"
Dongsheng Yang31193212014-12-12 16:44:35 +080049#include "qgroup.h"
Josef Bacik86736342019-06-19 15:12:00 -040050#include "delalloc-space.h"
Josef Bacikaac00232019-06-20 15:37:44 -040051#include "block-group.h"
Chris Mason39279cc2007-06-12 06:35:45 -040052
53struct btrfs_iget_args {
Chris Mason90d3e592014-01-09 17:28:00 -080054 struct btrfs_key *location;
Chris Mason39279cc2007-06-12 06:35:45 -040055 struct btrfs_root *root;
56};
57
Filipe Mananaf28a4922015-12-08 19:23:20 +000058struct btrfs_dio_data {
Filipe Mananaf28a4922015-12-08 19:23:20 +000059 u64 reserve;
60 u64 unsubmitted_oe_range_start;
61 u64 unsubmitted_oe_range_end;
Liu Bo4aaedfb2016-12-14 22:36:05 -080062 int overwrite;
Filipe Mananaf28a4922015-12-08 19:23:20 +000063};
64
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070065static const struct inode_operations btrfs_dir_inode_operations;
66static const struct inode_operations btrfs_symlink_inode_operations;
67static const struct inode_operations btrfs_dir_ro_inode_operations;
68static const struct inode_operations btrfs_special_inode_operations;
69static const struct inode_operations btrfs_file_inode_operations;
Alexey Dobriyan7f094102009-09-21 17:01:10 -070070static const struct address_space_operations btrfs_aops;
Alexey Dobriyan828c0952009-10-01 15:43:56 -070071static const struct file_operations btrfs_dir_file_operations;
David Sterba20e55062015-11-19 11:42:28 +010072static const struct extent_io_ops btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -040073
74static struct kmem_cache *btrfs_inode_cachep;
75struct kmem_cache *btrfs_trans_handle_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040076struct kmem_cache *btrfs_path_cachep;
Josef Bacikdc89e982011-01-28 17:05:48 -050077struct kmem_cache *btrfs_free_space_cachep;
Christophe Leroy3acd4852019-08-21 15:05:55 +000078struct kmem_cache *btrfs_free_space_bitmap_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040079
Eric Sandeen3972f262013-01-12 02:57:22 +000080static int btrfs_setsize(struct inode *inode, struct iattr *attr);
Filipe Manana213e8c52018-02-06 20:40:31 +000081static int btrfs_truncate(struct inode *inode, bool skip_writeback);
Josef Bacik5fd02042012-05-02 14:00:54 -040082static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent);
Chris Mason771ed682008-11-06 22:02:51 -050083static noinline int cow_file_range(struct inode *inode,
84 struct page *locked_page,
Nikolay Borisov74e91942019-07-17 16:18:16 +030085 u64 start, u64 end, int *page_started,
Nikolay Borisov330a5822019-07-17 16:18:17 +030086 unsigned long *nr_written, int unlock);
Liu Bo6f9994d2017-01-31 07:50:22 -080087static struct extent_map *create_io_em(struct inode *inode, u64 start, u64 len,
88 u64 orig_start, u64 block_start,
89 u64 block_len, u64 orig_block_len,
90 u64 ram_bytes, int compress_type,
91 int type);
Josef Bacik7b128762008-07-24 12:17:14 -040092
Qu Wenruo524272602017-03-08 10:25:52 +080093static void __endio_write_update_ordered(struct inode *inode,
94 const u64 offset, const u64 bytes,
95 const bool uptodate);
96
97/*
98 * Cleanup all submitted ordered extents in specified range to handle errors
Andrea Gelmini52042d82018-11-28 12:05:13 +010099 * from the btrfs_run_delalloc_range() callback.
Qu Wenruo524272602017-03-08 10:25:52 +0800100 *
101 * NOTE: caller must ensure that when an error happens, it can not call
102 * extent_clear_unlock_delalloc() to clear both the bits EXTENT_DO_ACCOUNTING
103 * and EXTENT_DELALLOC simultaneously, because that causes the reserved metadata
104 * to be released, which we want to happen only when finishing the ordered
Nikolay Borisovd1051d62018-11-21 17:10:52 +0200105 * extent (btrfs_finish_ordered_io()).
Qu Wenruo524272602017-03-08 10:25:52 +0800106 */
107static inline void btrfs_cleanup_ordered_extents(struct inode *inode,
Nikolay Borisovd1051d62018-11-21 17:10:52 +0200108 struct page *locked_page,
109 u64 offset, u64 bytes)
Qu Wenruo524272602017-03-08 10:25:52 +0800110{
Naohiro Aota63d71450c2017-09-01 17:58:47 +0900111 unsigned long index = offset >> PAGE_SHIFT;
112 unsigned long end_index = (offset + bytes - 1) >> PAGE_SHIFT;
Nikolay Borisovd1051d62018-11-21 17:10:52 +0200113 u64 page_start = page_offset(locked_page);
114 u64 page_end = page_start + PAGE_SIZE - 1;
115
Naohiro Aota63d71450c2017-09-01 17:58:47 +0900116 struct page *page;
117
118 while (index <= end_index) {
119 page = find_get_page(inode->i_mapping, index);
120 index++;
121 if (!page)
122 continue;
123 ClearPagePrivate2(page);
124 put_page(page);
125 }
Nikolay Borisovd1051d62018-11-21 17:10:52 +0200126
127 /*
128 * In case this page belongs to the delalloc range being instantiated
129 * then skip it, since the first page of a range is going to be
130 * properly cleaned up by the caller of run_delalloc_range
131 */
132 if (page_start >= offset && page_end <= (offset + bytes - 1)) {
133 offset += PAGE_SIZE;
134 bytes -= PAGE_SIZE;
135 }
136
137 return __endio_write_update_ordered(inode, offset, bytes, false);
Qu Wenruo524272602017-03-08 10:25:52 +0800138}
139
Eric Sandeen48a3b632013-04-25 20:41:01 +0000140static int btrfs_dirty_inode(struct inode *inode);
Chris Mason39279cc2007-06-12 06:35:45 -0400141
Josef Bacik6a3891c2015-03-16 17:38:52 -0400142#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
143void btrfs_test_inode_set_ops(struct inode *inode)
144{
145 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
146}
147#endif
148
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000149static int btrfs_init_inode_security(struct btrfs_trans_handle *trans,
Eric Paris2a7dba32011-02-01 11:05:39 -0500150 struct inode *inode, struct inode *dir,
151 const struct qstr *qstr)
Jim Owens0279b4c2009-02-04 09:29:13 -0500152{
153 int err;
154
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000155 err = btrfs_init_acl(trans, inode, dir);
Jim Owens0279b4c2009-02-04 09:29:13 -0500156 if (!err)
Eric Paris2a7dba32011-02-01 11:05:39 -0500157 err = btrfs_xattr_security_init(trans, inode, dir, qstr);
Jim Owens0279b4c2009-02-04 09:29:13 -0500158 return err;
159}
160
Chris Masond352ac62008-09-29 15:18:18 -0400161/*
Chris Masonc8b97812008-10-29 14:49:59 -0400162 * this does all the hard work for inserting an inline extent into
163 * the btree. The caller should have done a btrfs_drop_extents so that
164 * no overlapping inline items exist in the btree
165 */
Chris Mason40f76582014-05-21 13:35:51 -0700166static int insert_inline_extent(struct btrfs_trans_handle *trans,
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000167 struct btrfs_path *path, int extent_inserted,
Chris Masonc8b97812008-10-29 14:49:59 -0400168 struct btrfs_root *root, struct inode *inode,
169 u64 start, size_t size, size_t compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000170 int compress_type,
Chris Masonc8b97812008-10-29 14:49:59 -0400171 struct page **compressed_pages)
172{
Chris Masonc8b97812008-10-29 14:49:59 -0400173 struct extent_buffer *leaf;
174 struct page *page = NULL;
175 char *kaddr;
176 unsigned long ptr;
177 struct btrfs_file_extent_item *ei;
Chris Masonc8b97812008-10-29 14:49:59 -0400178 int ret;
179 size_t cur_size = size;
Chris Masonc8b97812008-10-29 14:49:59 -0400180 unsigned long offset;
Chris Masonc8b97812008-10-29 14:49:59 -0400181
Jia-Ju Bai982f1f5d2019-07-27 16:51:13 +0800182 ASSERT((compressed_size > 0 && compressed_pages) ||
183 (compressed_size == 0 && !compressed_pages));
184
Li Zefanfe3f5662011-03-28 08:30:38 +0000185 if (compressed_size && compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400186 cur_size = compressed_size;
Chris Masonc8b97812008-10-29 14:49:59 -0400187
Chris Masonc8b97812008-10-29 14:49:59 -0400188 inode_add_bytes(inode, size);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000189
190 if (!extent_inserted) {
191 struct btrfs_key key;
192 size_t datasize;
193
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +0200194 key.objectid = btrfs_ino(BTRFS_I(inode));
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000195 key.offset = start;
David Sterba962a2982014-06-04 18:41:45 +0200196 key.type = BTRFS_EXTENT_DATA_KEY;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000197
198 datasize = btrfs_file_extent_calc_inline_size(cur_size);
199 path->leave_spinning = 1;
200 ret = btrfs_insert_empty_item(trans, root, path, &key,
201 datasize);
David Sterba79b4f4c2017-06-15 19:09:51 +0200202 if (ret)
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000203 goto fail;
Chris Masonc8b97812008-10-29 14:49:59 -0400204 }
205 leaf = path->nodes[0];
206 ei = btrfs_item_ptr(leaf, path->slots[0],
207 struct btrfs_file_extent_item);
208 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
209 btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
210 btrfs_set_file_extent_encryption(leaf, ei, 0);
211 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
212 btrfs_set_file_extent_ram_bytes(leaf, ei, size);
213 ptr = btrfs_file_extent_inline_start(ei);
214
Li Zefan261507a02010-12-17 14:21:50 +0800215 if (compress_type != BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -0400216 struct page *cpage;
217 int i = 0;
Chris Masond3977122009-01-05 21:25:51 -0500218 while (compressed_size > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -0400219 cpage = compressed_pages[i];
Chris Mason5b050f02008-11-11 09:34:41 -0500220 cur_size = min_t(unsigned long, compressed_size,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300221 PAGE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400222
Cong Wang7ac687d2011-11-25 23:14:28 +0800223 kaddr = kmap_atomic(cpage);
Chris Masonc8b97812008-10-29 14:49:59 -0400224 write_extent_buffer(leaf, kaddr, ptr, cur_size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800225 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400226
227 i++;
228 ptr += cur_size;
229 compressed_size -= cur_size;
230 }
231 btrfs_set_file_extent_compression(leaf, ei,
Li Zefan261507a02010-12-17 14:21:50 +0800232 compress_type);
Chris Masonc8b97812008-10-29 14:49:59 -0400233 } else {
234 page = find_get_page(inode->i_mapping,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300235 start >> PAGE_SHIFT);
Chris Masonc8b97812008-10-29 14:49:59 -0400236 btrfs_set_file_extent_compression(leaf, ei, 0);
Cong Wang7ac687d2011-11-25 23:14:28 +0800237 kaddr = kmap_atomic(page);
Johannes Thumshirn70730172018-12-05 15:23:03 +0100238 offset = offset_in_page(start);
Chris Masonc8b97812008-10-29 14:49:59 -0400239 write_extent_buffer(leaf, kaddr + offset, ptr, size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800240 kunmap_atomic(kaddr);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300241 put_page(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400242 }
243 btrfs_mark_buffer_dirty(leaf);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000244 btrfs_release_path(path);
Chris Masonc8b97812008-10-29 14:49:59 -0400245
Yan, Zhengc2167752009-11-12 09:34:21 +0000246 /*
247 * we're an inline extent, so nobody can
248 * extend the file past i_size without locking
249 * a page we already have locked.
250 *
251 * We must do any isize and inode updates
252 * before we unlock the pages. Otherwise we
253 * could end up racing with unlink.
254 */
Chris Masonc8b97812008-10-29 14:49:59 -0400255 BTRFS_I(inode)->disk_i_size = inode->i_size;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100256 ret = btrfs_update_inode(trans, root, inode);
Yan, Zhengc2167752009-11-12 09:34:21 +0000257
Chris Masonc8b97812008-10-29 14:49:59 -0400258fail:
David Sterba79b4f4c2017-06-15 19:09:51 +0200259 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400260}
261
262
263/*
264 * conditionally insert an inline extent into the file. This
265 * does the checks required to make sure the data is small enough
266 * to fit as an inline extent.
267 */
Nikolay Borisovd02c0e22018-03-02 09:43:15 +0200268static noinline int cow_file_range_inline(struct inode *inode, u64 start,
Josef Bacik00361582013-08-14 14:02:47 -0400269 u64 end, size_t compressed_size,
270 int compress_type,
271 struct page **compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400272{
Nikolay Borisovd02c0e22018-03-02 09:43:15 +0200273 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400274 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik00361582013-08-14 14:02:47 -0400275 struct btrfs_trans_handle *trans;
Chris Masonc8b97812008-10-29 14:49:59 -0400276 u64 isize = i_size_read(inode);
277 u64 actual_end = min(end + 1, isize);
278 u64 inline_len = actual_end - start;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400279 u64 aligned_end = ALIGN(end, fs_info->sectorsize);
Chris Masonc8b97812008-10-29 14:49:59 -0400280 u64 data_len = inline_len;
281 int ret;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000282 struct btrfs_path *path;
283 int extent_inserted = 0;
284 u32 extent_item_size;
Chris Masonc8b97812008-10-29 14:49:59 -0400285
286 if (compressed_size)
287 data_len = compressed_size;
288
289 if (start > 0 ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400290 actual_end > fs_info->sectorsize ||
291 data_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info) ||
Chris Masonc8b97812008-10-29 14:49:59 -0400292 (!compressed_size &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400293 (actual_end & (fs_info->sectorsize - 1)) == 0) ||
Chris Masonc8b97812008-10-29 14:49:59 -0400294 end + 1 < isize ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400295 data_len > fs_info->max_inline) {
Chris Masonc8b97812008-10-29 14:49:59 -0400296 return 1;
297 }
298
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000299 path = btrfs_alloc_path();
300 if (!path)
301 return -ENOMEM;
302
Josef Bacik00361582013-08-14 14:02:47 -0400303 trans = btrfs_join_transaction(root);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000304 if (IS_ERR(trans)) {
305 btrfs_free_path(path);
Josef Bacik00361582013-08-14 14:02:47 -0400306 return PTR_ERR(trans);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000307 }
Josef Bacik69fe2d72017-10-19 14:15:57 -0400308 trans->block_rsv = &BTRFS_I(inode)->block_rsv;
Josef Bacik00361582013-08-14 14:02:47 -0400309
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000310 if (compressed_size && compressed_pages)
311 extent_item_size = btrfs_file_extent_calc_inline_size(
312 compressed_size);
313 else
314 extent_item_size = btrfs_file_extent_calc_inline_size(
315 inline_len);
316
317 ret = __btrfs_drop_extents(trans, root, inode, path,
318 start, aligned_end, NULL,
319 1, 1, extent_item_size, &extent_inserted);
Josef Bacik00361582013-08-14 14:02:47 -0400320 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -0400321 btrfs_abort_transaction(trans, ret);
Josef Bacik00361582013-08-14 14:02:47 -0400322 goto out;
323 }
Chris Masonc8b97812008-10-29 14:49:59 -0400324
325 if (isize > actual_end)
326 inline_len = min_t(u64, isize, actual_end);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000327 ret = insert_inline_extent(trans, path, extent_inserted,
328 root, inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -0400329 inline_len, compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000330 compress_type, compressed_pages);
Josef Bacik2adcac12012-05-23 16:10:14 -0400331 if (ret && ret != -ENOSPC) {
Jeff Mahoney66642832016-06-10 18:19:25 -0400332 btrfs_abort_transaction(trans, ret);
Josef Bacik00361582013-08-14 14:02:47 -0400333 goto out;
Josef Bacik2adcac12012-05-23 16:10:14 -0400334 } else if (ret == -ENOSPC) {
Josef Bacik00361582013-08-14 14:02:47 -0400335 ret = 1;
336 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100337 }
Josef Bacik2adcac12012-05-23 16:10:14 -0400338
Josef Bacikbdc20e62013-02-28 13:23:38 -0500339 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +0200340 btrfs_drop_extent_cache(BTRFS_I(inode), start, aligned_end - 1, 0);
Josef Bacik00361582013-08-14 14:02:47 -0400341out:
Qu Wenruo94ed9382015-09-08 17:25:56 +0800342 /*
343 * Don't forget to free the reserved space, as for inlined extent
344 * it won't count as data extent, free them directly here.
345 * And at reserve time, it's always aligned to page size, so
346 * just free one page here.
347 */
Qu Wenruobc42bda2017-02-27 15:10:39 +0800348 btrfs_qgroup_free_data(inode, NULL, 0, PAGE_SIZE);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000349 btrfs_free_path(path);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -0400350 btrfs_end_transaction(trans);
Josef Bacik00361582013-08-14 14:02:47 -0400351 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400352}
353
Chris Mason771ed682008-11-06 22:02:51 -0500354struct async_extent {
355 u64 start;
356 u64 ram_size;
357 u64 compressed_size;
358 struct page **pages;
359 unsigned long nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800360 int compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500361 struct list_head list;
362};
363
Nikolay Borisov97db1202019-03-12 17:20:24 +0200364struct async_chunk {
Chris Mason771ed682008-11-06 22:02:51 -0500365 struct inode *inode;
Chris Mason771ed682008-11-06 22:02:51 -0500366 struct page *locked_page;
367 u64 start;
368 u64 end;
Liu Bof82b7352017-10-23 23:18:16 -0600369 unsigned int write_flags;
Chris Mason771ed682008-11-06 22:02:51 -0500370 struct list_head extents;
371 struct btrfs_work work;
Nikolay Borisov97db1202019-03-12 17:20:24 +0200372 atomic_t *pending;
Chris Mason771ed682008-11-06 22:02:51 -0500373};
374
Nikolay Borisov97db1202019-03-12 17:20:24 +0200375struct async_cow {
376 /* Number of chunks in flight; must be first in the structure */
377 atomic_t num_chunks;
378 struct async_chunk chunks[];
379};
380
381static noinline int add_async_extent(struct async_chunk *cow,
Chris Mason771ed682008-11-06 22:02:51 -0500382 u64 start, u64 ram_size,
383 u64 compressed_size,
384 struct page **pages,
Li Zefan261507a02010-12-17 14:21:50 +0800385 unsigned long nr_pages,
386 int compress_type)
Chris Mason771ed682008-11-06 22:02:51 -0500387{
388 struct async_extent *async_extent;
389
390 async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100391 BUG_ON(!async_extent); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -0500392 async_extent->start = start;
393 async_extent->ram_size = ram_size;
394 async_extent->compressed_size = compressed_size;
395 async_extent->pages = pages;
396 async_extent->nr_pages = nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800397 async_extent->compress_type = compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500398 list_add_tail(&async_extent->list, &cow->extents);
399 return 0;
400}
401
Qu Wenruo42c16da2019-07-01 05:12:46 +0000402/*
403 * Check if the inode has flags compatible with compression
404 */
405static inline bool inode_can_compress(struct inode *inode)
406{
407 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW ||
408 BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
409 return false;
410 return true;
411}
412
413/*
414 * Check if the inode needs to be submitted to compression, based on mount
415 * options, defragmentation, properties or heuristics.
416 */
Timofey Titovetsc2fcdcd2017-07-17 16:52:58 +0300417static inline int inode_need_compress(struct inode *inode, u64 start, u64 end)
Wang Shilongf79707b2014-07-17 11:44:09 +0800418{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400419 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Wang Shilongf79707b2014-07-17 11:44:09 +0800420
Qu Wenruo42c16da2019-07-01 05:12:46 +0000421 if (!inode_can_compress(inode)) {
422 WARN(IS_ENABLED(CONFIG_BTRFS_DEBUG),
423 KERN_ERR "BTRFS: unexpected compression for ino %llu\n",
424 btrfs_ino(BTRFS_I(inode)));
425 return 0;
426 }
Wang Shilongf79707b2014-07-17 11:44:09 +0800427 /* force compress */
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400428 if (btrfs_test_opt(fs_info, FORCE_COMPRESS))
Wang Shilongf79707b2014-07-17 11:44:09 +0800429 return 1;
David Sterbaeec63c62017-07-17 19:41:31 +0200430 /* defrag ioctl */
431 if (BTRFS_I(inode)->defrag_compress)
432 return 1;
Wang Shilongf79707b2014-07-17 11:44:09 +0800433 /* bad compression ratios */
434 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS)
435 return 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400436 if (btrfs_test_opt(fs_info, COMPRESS) ||
Wang Shilongf79707b2014-07-17 11:44:09 +0800437 BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS ||
David Sterbab52aa8c2017-07-17 19:17:20 +0200438 BTRFS_I(inode)->prop_compress)
Timofey Titovetsc2fcdcd2017-07-17 16:52:58 +0300439 return btrfs_compress_heuristic(inode, start, end);
Wang Shilongf79707b2014-07-17 11:44:09 +0800440 return 0;
441}
442
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200443static inline void inode_should_defrag(struct btrfs_inode *inode,
Anand Jain26d30f82016-12-19 19:09:06 +0800444 u64 start, u64 end, u64 num_bytes, u64 small_write)
445{
446 /* If this is a small write inside eof, kick off a defrag */
447 if (num_bytes < small_write &&
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200448 (start > 0 || end + 1 < inode->disk_i_size))
Anand Jain26d30f82016-12-19 19:09:06 +0800449 btrfs_add_inode_defrag(NULL, inode);
450}
451
Chris Masonc8b97812008-10-29 14:49:59 -0400452/*
Chris Mason771ed682008-11-06 22:02:51 -0500453 * we create compressed extents in two phases. The first
454 * phase compresses a range of pages that have already been
455 * locked (both pages and state bits are locked).
Chris Masonc8b97812008-10-29 14:49:59 -0400456 *
Chris Mason771ed682008-11-06 22:02:51 -0500457 * This is done inside an ordered work queue, and the compression
458 * is spread across many cpus. The actual IO submission is step
459 * two, and the ordered work queue takes care of making sure that
460 * happens in the same order things were put onto the queue by
461 * writepages and friends.
Chris Masonc8b97812008-10-29 14:49:59 -0400462 *
Chris Mason771ed682008-11-06 22:02:51 -0500463 * If this code finds it can't get good compression, it puts an
464 * entry onto the work queue to write the uncompressed bytes. This
465 * makes sure that both compressed inodes and uncompressed inodes
Artem Bityutskiyb2570312012-07-25 18:12:06 +0300466 * are written in the same order that the flusher thread sent them
467 * down.
Chris Masond352ac62008-09-29 15:18:18 -0400468 */
Nikolay Borisovac3e9932019-07-17 14:41:44 +0300469static noinline int compress_file_range(struct async_chunk *async_chunk)
Chris Masonb888db22007-08-27 16:49:44 -0400470{
Nikolay Borisov1368c6d2019-03-12 17:20:27 +0200471 struct inode *inode = async_chunk->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400472 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400473 u64 blocksize = fs_info->sectorsize;
Nikolay Borisov1368c6d2019-03-12 17:20:27 +0200474 u64 start = async_chunk->start;
475 u64 end = async_chunk->end;
Chris Masonc8b97812008-10-29 14:49:59 -0400476 u64 actual_end;
Josef Bacikd98da492019-10-11 09:03:54 -0400477 u64 i_size;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400478 int ret = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400479 struct page **pages = NULL;
480 unsigned long nr_pages;
Chris Masonc8b97812008-10-29 14:49:59 -0400481 unsigned long total_compressed = 0;
482 unsigned long total_in = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400483 int i;
484 int will_compress;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400485 int compress_type = fs_info->compress_type;
Nikolay Borisovac3e9932019-07-17 14:41:44 +0300486 int compressed_extents = 0;
Chris Mason4adaa612013-03-26 13:07:00 -0400487 int redirty = 0;
Chris Masonb888db22007-08-27 16:49:44 -0400488
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200489 inode_should_defrag(BTRFS_I(inode), start, end, end - start + 1,
490 SZ_16K);
Chris Mason4cb53002011-05-24 15:35:30 -0400491
Josef Bacikd98da492019-10-11 09:03:54 -0400492 /*
493 * We need to save i_size before now because it could change in between
494 * us evaluating the size and assigning it. This is because we lock and
495 * unlock the page in truncate and fallocate, and then modify the i_size
496 * later on.
497 *
498 * The barriers are to emulate READ_ONCE, remove that once i_size_read
499 * does that for us.
500 */
501 barrier();
502 i_size = i_size_read(inode);
503 barrier();
504 actual_end = min_t(u64, i_size, end + 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400505again:
506 will_compress = 0;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300507 nr_pages = (end >> PAGE_SHIFT) - (start >> PAGE_SHIFT) + 1;
David Sterba069eac72017-02-14 19:36:54 +0100508 BUILD_BUG_ON((BTRFS_MAX_COMPRESSED % PAGE_SIZE) != 0);
509 nr_pages = min_t(unsigned long, nr_pages,
510 BTRFS_MAX_COMPRESSED / PAGE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400511
Chris Masonf03d9301f2009-02-04 09:31:06 -0500512 /*
513 * we don't want to send crud past the end of i_size through
514 * compression, that's just a waste of CPU time. So, if the
515 * end of the file is before the start of our current
516 * requested range of bytes, we bail out to the uncompressed
517 * cleanup code that can deal with all of this.
518 *
519 * It isn't really the fastest way to fix things, but this is a
520 * very uncommon corner.
521 */
522 if (actual_end <= start)
523 goto cleanup_and_bail_uncompressed;
524
Chris Masonc8b97812008-10-29 14:49:59 -0400525 total_compressed = actual_end - start;
526
Shilong Wang4bcbb332014-10-07 18:44:35 -0400527 /*
528 * skip compression for a small file range(<=blocksize) that
Nicholas D Steeves01327612016-05-19 21:18:45 -0400529 * isn't an inline extent, since it doesn't save disk space at all.
Shilong Wang4bcbb332014-10-07 18:44:35 -0400530 */
531 if (total_compressed <= blocksize &&
532 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
533 goto cleanup_and_bail_uncompressed;
534
David Sterba069eac72017-02-14 19:36:54 +0100535 total_compressed = min_t(unsigned long, total_compressed,
536 BTRFS_MAX_UNCOMPRESSED);
Chris Masonc8b97812008-10-29 14:49:59 -0400537 total_in = 0;
538 ret = 0;
Chris Masondb945352007-10-15 16:15:53 -0400539
Chris Mason771ed682008-11-06 22:02:51 -0500540 /*
541 * we do compression for mount -o compress and when the
542 * inode has not been flagged as nocompress. This flag can
543 * change at any time if we discover bad compression ratios.
Chris Masonc8b97812008-10-29 14:49:59 -0400544 */
Timofey Titovetsc2fcdcd2017-07-17 16:52:58 +0300545 if (inode_need_compress(inode, start, end)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400546 WARN_ON(pages);
David Sterba31e818f2015-02-20 18:00:26 +0100547 pages = kcalloc(nr_pages, sizeof(struct page *), GFP_NOFS);
Li Zefan560f7d72011-09-08 10:22:01 +0800548 if (!pages) {
549 /* just bail out to the uncompressed code */
Filipe Manana3527a012018-10-13 00:37:25 +0100550 nr_pages = 0;
Li Zefan560f7d72011-09-08 10:22:01 +0800551 goto cont;
552 }
Chris Mason179e29e2007-11-01 11:28:41 -0400553
David Sterbaeec63c62017-07-17 19:41:31 +0200554 if (BTRFS_I(inode)->defrag_compress)
555 compress_type = BTRFS_I(inode)->defrag_compress;
556 else if (BTRFS_I(inode)->prop_compress)
David Sterbab52aa8c2017-07-17 19:17:20 +0200557 compress_type = BTRFS_I(inode)->prop_compress;
Li Zefan261507a02010-12-17 14:21:50 +0800558
Chris Mason4adaa612013-03-26 13:07:00 -0400559 /*
560 * we need to call clear_page_dirty_for_io on each
561 * page in the range. Otherwise applications with the file
562 * mmap'd can wander in and change the page contents while
563 * we are compressing them.
564 *
565 * If the compression fails for any reason, we set the pages
566 * dirty again later on.
Timofey Titovetse9679de2017-10-24 01:29:48 +0300567 *
568 * Note that the remaining part is redirtied, the start pointer
569 * has moved, the end is the original one.
Chris Mason4adaa612013-03-26 13:07:00 -0400570 */
Timofey Titovetse9679de2017-10-24 01:29:48 +0300571 if (!redirty) {
572 extent_range_clear_dirty_for_io(inode, start, end);
573 redirty = 1;
574 }
David Sterbaf51d2b52017-09-15 17:36:57 +0200575
576 /* Compression level is applied here and only here */
577 ret = btrfs_compress_pages(
578 compress_type | (fs_info->compress_level << 4),
Li Zefan261507a02010-12-17 14:21:50 +0800579 inode->i_mapping, start,
David Sterba38c31462017-02-14 19:04:07 +0100580 pages,
David Sterba4d3a8002017-02-14 19:04:07 +0100581 &nr_pages,
Li Zefan261507a02010-12-17 14:21:50 +0800582 &total_in,
David Sterbae5d74902017-02-14 19:45:05 +0100583 &total_compressed);
Chris Masonc8b97812008-10-29 14:49:59 -0400584
585 if (!ret) {
Johannes Thumshirn70730172018-12-05 15:23:03 +0100586 unsigned long offset = offset_in_page(total_compressed);
David Sterba4d3a8002017-02-14 19:04:07 +0100587 struct page *page = pages[nr_pages - 1];
Chris Masonc8b97812008-10-29 14:49:59 -0400588 char *kaddr;
589
590 /* zero the tail end of the last page, we might be
591 * sending it down to disk
592 */
593 if (offset) {
Cong Wang7ac687d2011-11-25 23:14:28 +0800594 kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400595 memset(kaddr + offset, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300596 PAGE_SIZE - offset);
Cong Wang7ac687d2011-11-25 23:14:28 +0800597 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400598 }
599 will_compress = 1;
600 }
601 }
Li Zefan560f7d72011-09-08 10:22:01 +0800602cont:
Chris Masonc8b97812008-10-29 14:49:59 -0400603 if (start == 0) {
604 /* lets try to make an inline extent */
Timofey Titovets6018ba02017-09-15 01:57:26 +0300605 if (ret || total_in < actual_end) {
Chris Masonc8b97812008-10-29 14:49:59 -0400606 /* we didn't compress the entire range, try
Chris Mason771ed682008-11-06 22:02:51 -0500607 * to make an uncompressed inline extent.
Chris Masonc8b97812008-10-29 14:49:59 -0400608 */
Nikolay Borisovd02c0e22018-03-02 09:43:15 +0200609 ret = cow_file_range_inline(inode, start, end, 0,
610 BTRFS_COMPRESS_NONE, NULL);
Chris Masonc8b97812008-10-29 14:49:59 -0400611 } else {
Chris Mason771ed682008-11-06 22:02:51 -0500612 /* try making a compressed inline extent */
Nikolay Borisovd02c0e22018-03-02 09:43:15 +0200613 ret = cow_file_range_inline(inode, start, end,
Li Zefanfe3f5662011-03-28 08:30:38 +0000614 total_compressed,
615 compress_type, pages);
Chris Masonc8b97812008-10-29 14:49:59 -0400616 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100617 if (ret <= 0) {
Josef Bacik151a41b2013-07-29 13:22:24 -0400618 unsigned long clear_flags = EXTENT_DELALLOC |
Josef Bacik8b62f872017-10-19 14:15:55 -0400619 EXTENT_DELALLOC_NEW | EXTENT_DEFRAG |
620 EXTENT_DO_ACCOUNTING;
Filipe Mananae6eb4312014-10-10 10:45:12 +0100621 unsigned long page_error_op;
622
Filipe Mananae6eb4312014-10-10 10:45:12 +0100623 page_error_op = ret < 0 ? PAGE_SET_ERROR : 0;
Josef Bacik151a41b2013-07-29 13:22:24 -0400624
Chris Mason771ed682008-11-06 22:02:51 -0500625 /*
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100626 * inline extent creation worked or returned error,
627 * we don't need to create any more async work items.
628 * Unlock and free up our temp pages.
Josef Bacik8b62f872017-10-19 14:15:55 -0400629 *
630 * We use DO_ACCOUNTING here because we need the
631 * delalloc_release_metadata to be done _after_ we drop
632 * our outstanding extent for clearing delalloc for this
633 * range.
Chris Mason771ed682008-11-06 22:02:51 -0500634 */
Nikolay Borisov74e91942019-07-17 16:18:16 +0300635 extent_clear_unlock_delalloc(inode, start, end, NULL,
636 clear_flags,
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800637 PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -0400638 PAGE_CLEAR_DIRTY |
639 PAGE_SET_WRITEBACK |
Filipe Mananae6eb4312014-10-10 10:45:12 +0100640 page_error_op |
Josef Bacikc2790a22013-07-29 11:20:47 -0400641 PAGE_END_WRITEBACK);
Nikolay Borisovcecc8d92019-07-17 14:41:45 +0300642
643 for (i = 0; i < nr_pages; i++) {
644 WARN_ON(pages[i]->mapping);
645 put_page(pages[i]);
646 }
647 kfree(pages);
648
649 return 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400650 }
651 }
652
653 if (will_compress) {
654 /*
655 * we aren't doing an inline extent round the compressed size
656 * up to a block size boundary so the allocator does sane
657 * things
658 */
Qu Wenruofda28322013-02-26 08:10:22 +0000659 total_compressed = ALIGN(total_compressed, blocksize);
Chris Masonc8b97812008-10-29 14:49:59 -0400660
661 /*
662 * one last check to make sure the compression is really a
Timofey Titovets170607e2017-06-06 14:41:15 +0300663 * win, compare the page count read with the blocks on disk,
664 * compression must free at least one sector size
Chris Masonc8b97812008-10-29 14:49:59 -0400665 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300666 total_in = ALIGN(total_in, PAGE_SIZE);
Timofey Titovets170607e2017-06-06 14:41:15 +0300667 if (total_compressed + blocksize <= total_in) {
Nikolay Borisovac3e9932019-07-17 14:41:44 +0300668 compressed_extents++;
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700669
670 /*
671 * The async work queues will take care of doing actual
672 * allocation on disk for these compressed pages, and
673 * will submit them to the elevator.
674 */
Nikolay Borisovb5326272019-03-12 17:20:25 +0200675 add_async_extent(async_chunk, start, total_in,
David Sterba4d3a8002017-02-14 19:04:07 +0100676 total_compressed, pages, nr_pages,
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700677 compress_type);
678
Timofey Titovets11708622017-10-03 18:06:01 +0300679 if (start + total_in < end) {
680 start += total_in;
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700681 pages = NULL;
682 cond_resched();
683 goto again;
684 }
Nikolay Borisovac3e9932019-07-17 14:41:44 +0300685 return compressed_extents;
Chris Masonc8b97812008-10-29 14:49:59 -0400686 }
687 }
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700688 if (pages) {
Chris Masonc8b97812008-10-29 14:49:59 -0400689 /*
690 * the compression code ran but failed to make things smaller,
691 * free any pages it allocated and our page pointer array
692 */
David Sterba4d3a8002017-02-14 19:04:07 +0100693 for (i = 0; i < nr_pages; i++) {
Chris Mason70b99e62008-10-31 12:46:39 -0400694 WARN_ON(pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300695 put_page(pages[i]);
Chris Masonc8b97812008-10-29 14:49:59 -0400696 }
697 kfree(pages);
698 pages = NULL;
699 total_compressed = 0;
David Sterba4d3a8002017-02-14 19:04:07 +0100700 nr_pages = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400701
702 /* flag the file so we don't compress in the future */
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400703 if (!btrfs_test_opt(fs_info, FORCE_COMPRESS) &&
David Sterbab52aa8c2017-07-17 19:17:20 +0200704 !(BTRFS_I(inode)->prop_compress)) {
Chris Masona555f812010-01-28 16:18:15 -0500705 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
Chris Mason1e701a32010-03-11 09:42:04 -0500706 }
Chris Masonc8b97812008-10-29 14:49:59 -0400707 }
Chris Masonf03d9301f2009-02-04 09:31:06 -0500708cleanup_and_bail_uncompressed:
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700709 /*
710 * No compression, but we still need to write the pages in the file
711 * we've been given so far. redirty the locked page if it corresponds
712 * to our extent and set things up for the async work queue to run
713 * cow_file_range to do the normal delalloc dance.
714 */
Chris Mason1d53c9e2019-07-10 12:28:16 -0700715 if (async_chunk->locked_page &&
716 (page_offset(async_chunk->locked_page) >= start &&
717 page_offset(async_chunk->locked_page)) <= end) {
Nikolay Borisov1368c6d2019-03-12 17:20:27 +0200718 __set_page_dirty_nobuffers(async_chunk->locked_page);
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700719 /* unlocked later on in the async handlers */
Chris Mason1d53c9e2019-07-10 12:28:16 -0700720 }
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700721
722 if (redirty)
723 extent_range_redirty_for_io(inode, start, end);
Nikolay Borisovb5326272019-03-12 17:20:25 +0200724 add_async_extent(async_chunk, start, end - start + 1, 0, NULL, 0,
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700725 BTRFS_COMPRESS_NONE);
Nikolay Borisovac3e9932019-07-17 14:41:44 +0300726 compressed_extents++;
Chris Mason771ed682008-11-06 22:02:51 -0500727
Nikolay Borisovac3e9932019-07-17 14:41:44 +0300728 return compressed_extents;
Chris Mason771ed682008-11-06 22:02:51 -0500729}
Chris Mason771ed682008-11-06 22:02:51 -0500730
Filipe Manana40ae8372014-10-06 22:14:24 +0100731static void free_async_extent_pages(struct async_extent *async_extent)
732{
733 int i;
734
735 if (!async_extent->pages)
736 return;
737
738 for (i = 0; i < async_extent->nr_pages; i++) {
739 WARN_ON(async_extent->pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300740 put_page(async_extent->pages[i]);
Filipe Manana40ae8372014-10-06 22:14:24 +0100741 }
742 kfree(async_extent->pages);
743 async_extent->nr_pages = 0;
744 async_extent->pages = NULL;
Chris Mason771ed682008-11-06 22:02:51 -0500745}
746
747/*
748 * phase two of compressed writeback. This is the ordered portion
749 * of the code, which only gets called in the order the work was
750 * queued. We walk all the async extents created by compress_file_range
751 * and send them down to the disk.
752 */
Nikolay Borisovb5326272019-03-12 17:20:25 +0200753static noinline void submit_compressed_extents(struct async_chunk *async_chunk)
Chris Mason771ed682008-11-06 22:02:51 -0500754{
Nikolay Borisovb5326272019-03-12 17:20:25 +0200755 struct inode *inode = async_chunk->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400756 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason771ed682008-11-06 22:02:51 -0500757 struct async_extent *async_extent;
758 u64 alloc_hint = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500759 struct btrfs_key ins;
760 struct extent_map *em;
761 struct btrfs_root *root = BTRFS_I(inode)->root;
Nikolay Borisov43366502019-03-12 17:20:29 +0200762 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500763 int ret = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500764
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500765again:
Nikolay Borisovb5326272019-03-12 17:20:25 +0200766 while (!list_empty(&async_chunk->extents)) {
767 async_extent = list_entry(async_chunk->extents.next,
Chris Mason771ed682008-11-06 22:02:51 -0500768 struct async_extent, list);
769 list_del(&async_extent->list);
770
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500771retry:
Nikolay Borisov74475552019-03-12 17:20:30 +0200772 lock_extent(io_tree, async_extent->start,
773 async_extent->start + async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500774 /* did the compression code fall back to uncompressed IO? */
775 if (!async_extent->pages) {
776 int page_started = 0;
777 unsigned long nr_written = 0;
778
Chris Mason771ed682008-11-06 22:02:51 -0500779 /* allocate blocks */
Nikolay Borisovb5326272019-03-12 17:20:25 +0200780 ret = cow_file_range(inode, async_chunk->locked_page,
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500781 async_extent->start,
782 async_extent->start +
783 async_extent->ram_size - 1,
Nikolay Borisov330a5822019-07-17 16:18:17 +0300784 &page_started, &nr_written, 0);
Chris Mason771ed682008-11-06 22:02:51 -0500785
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100786 /* JDM XXX */
787
Chris Mason771ed682008-11-06 22:02:51 -0500788 /*
789 * if page_started, cow_file_range inserted an
790 * inline extent and took care of all the unlocking
791 * and IO for us. Otherwise, we need to submit
792 * all those pages down to the drive.
793 */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500794 if (!page_started && !ret)
Nikolay Borisov5e3ee232017-12-08 15:55:58 +0200795 extent_write_locked_range(inode,
796 async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500797 async_extent->start +
Chris Mason771ed682008-11-06 22:02:51 -0500798 async_extent->ram_size - 1,
Chris Mason771ed682008-11-06 22:02:51 -0500799 WB_SYNC_ALL);
Chris Mason1d53c9e2019-07-10 12:28:16 -0700800 else if (ret && async_chunk->locked_page)
Nikolay Borisovb5326272019-03-12 17:20:25 +0200801 unlock_page(async_chunk->locked_page);
Chris Mason771ed682008-11-06 22:02:51 -0500802 kfree(async_extent);
803 cond_resched();
804 continue;
805 }
806
Wang Xiaoguang18513092016-07-25 15:51:40 +0800807 ret = btrfs_reserve_extent(root, async_extent->ram_size,
Chris Mason771ed682008-11-06 22:02:51 -0500808 async_extent->compressed_size,
809 async_extent->compressed_size,
Miao Xiee570fd22014-06-19 10:42:50 +0800810 0, alloc_hint, &ins, 1, 1);
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500811 if (ret) {
Filipe Manana40ae8372014-10-06 22:14:24 +0100812 free_async_extent_pages(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500813
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400814 if (ret == -ENOSPC) {
815 unlock_extent(io_tree, async_extent->start,
816 async_extent->start +
817 async_extent->ram_size - 1);
Liu Boce620032014-07-24 22:48:05 +0800818
819 /*
820 * we need to redirty the pages if we decide to
821 * fallback to uncompressed IO, otherwise we
822 * will not submit these pages down to lower
823 * layers.
824 */
825 extent_range_redirty_for_io(inode,
826 async_extent->start,
827 async_extent->start +
828 async_extent->ram_size - 1);
829
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100830 goto retry;
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400831 }
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500832 goto out_free;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500833 }
Yan, Zhengc2167752009-11-12 09:34:21 +0000834 /*
835 * here we're doing allocation and writeback of the
836 * compressed pages
837 */
Liu Bo6f9994d2017-01-31 07:50:22 -0800838 em = create_io_em(inode, async_extent->start,
839 async_extent->ram_size, /* len */
840 async_extent->start, /* orig_start */
841 ins.objectid, /* block_start */
842 ins.offset, /* block_len */
843 ins.offset, /* orig_block_len */
844 async_extent->ram_size, /* ram_bytes */
845 async_extent->compress_type,
846 BTRFS_ORDERED_COMPRESSED);
847 if (IS_ERR(em))
848 /* ret value is not necessary due to void function */
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500849 goto out_free_reserve;
Liu Bo6f9994d2017-01-31 07:50:22 -0800850 free_extent_map(em);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500851
Li Zefan261507a02010-12-17 14:21:50 +0800852 ret = btrfs_add_ordered_extent_compress(inode,
853 async_extent->start,
854 ins.objectid,
855 async_extent->ram_size,
856 ins.offset,
857 BTRFS_ORDERED_COMPRESSED,
858 async_extent->compress_type);
Filipe Mananad9f85962014-08-25 10:43:00 +0100859 if (ret) {
Nikolay Borisovdcdbc052017-02-20 13:50:45 +0200860 btrfs_drop_extent_cache(BTRFS_I(inode),
861 async_extent->start,
Filipe Mananad9f85962014-08-25 10:43:00 +0100862 async_extent->start +
863 async_extent->ram_size - 1, 0);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500864 goto out_free_reserve;
Filipe Mananad9f85962014-08-25 10:43:00 +0100865 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400866 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Chris Mason771ed682008-11-06 22:02:51 -0500867
Chris Mason771ed682008-11-06 22:02:51 -0500868 /*
869 * clear dirty, set writeback and unlock the pages.
870 */
Josef Bacikc2790a22013-07-29 11:20:47 -0400871 extent_clear_unlock_delalloc(inode, async_extent->start,
Chris Masona791e352009-10-08 11:27:10 -0400872 async_extent->start +
873 async_extent->ram_size - 1,
Josef Bacik151a41b2013-07-29 13:22:24 -0400874 NULL, EXTENT_LOCKED | EXTENT_DELALLOC,
875 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -0400876 PAGE_SET_WRITEBACK);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200877 if (btrfs_submit_compressed_write(inode,
Chris Masond3977122009-01-05 21:25:51 -0500878 async_extent->start,
879 async_extent->ram_size,
880 ins.objectid,
881 ins.offset, async_extent->pages,
Liu Bof82b7352017-10-23 23:18:16 -0600882 async_extent->nr_pages,
Nikolay Borisovb5326272019-03-12 17:20:25 +0200883 async_chunk->write_flags)) {
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100884 struct page *p = async_extent->pages[0];
885 const u64 start = async_extent->start;
886 const u64 end = start + async_extent->ram_size - 1;
887
888 p->mapping = inode->i_mapping;
Nikolay Borisovc6297322018-11-08 10:18:08 +0200889 btrfs_writepage_endio_finish_ordered(p, start, end, 0);
Nikolay Borisov7087a9d2018-11-01 14:09:48 +0200890
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100891 p->mapping = NULL;
Nikolay Borisov74e91942019-07-17 16:18:16 +0300892 extent_clear_unlock_delalloc(inode, start, end,
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800893 NULL, 0,
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100894 PAGE_END_WRITEBACK |
895 PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +0100896 free_async_extent_pages(async_extent);
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100897 }
Chris Mason771ed682008-11-06 22:02:51 -0500898 alloc_hint = ins.objectid + ins.offset;
899 kfree(async_extent);
900 cond_resched();
901 }
Filipe Mananadec8f172014-10-06 22:14:26 +0100902 return;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500903out_free_reserve:
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400904 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400905 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100906out_free:
Josef Bacikc2790a22013-07-29 11:20:47 -0400907 extent_clear_unlock_delalloc(inode, async_extent->start,
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500908 async_extent->start +
909 async_extent->ram_size - 1,
Josef Bacikc2790a22013-07-29 11:20:47 -0400910 NULL, EXTENT_LOCKED | EXTENT_DELALLOC |
Filipe Mananaa7e3b972017-04-03 10:45:46 +0100911 EXTENT_DELALLOC_NEW |
Josef Bacik151a41b2013-07-29 13:22:24 -0400912 EXTENT_DEFRAG | EXTENT_DO_ACCOUNTING,
913 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Filipe Manana704de492014-10-06 22:14:22 +0100914 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK |
915 PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +0100916 free_async_extent_pages(async_extent);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100917 kfree(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500918 goto again;
Chris Mason771ed682008-11-06 22:02:51 -0500919}
920
Josef Bacik4b46fce2010-05-23 11:00:55 -0400921static u64 get_extent_allocation_hint(struct inode *inode, u64 start,
922 u64 num_bytes)
923{
924 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
925 struct extent_map *em;
926 u64 alloc_hint = 0;
927
928 read_lock(&em_tree->lock);
929 em = search_extent_mapping(em_tree, start, num_bytes);
930 if (em) {
931 /*
932 * if block start isn't an actual block number then find the
933 * first block in this inode and use that as a hint. If that
934 * block is also bogus then just don't worry about it.
935 */
936 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
937 free_extent_map(em);
938 em = search_extent_mapping(em_tree, 0, 0);
939 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
940 alloc_hint = em->block_start;
941 if (em)
942 free_extent_map(em);
943 } else {
944 alloc_hint = em->block_start;
945 free_extent_map(em);
946 }
947 }
948 read_unlock(&em_tree->lock);
949
950 return alloc_hint;
951}
952
Chris Mason771ed682008-11-06 22:02:51 -0500953/*
954 * when extent_io.c finds a delayed allocation range in the file,
955 * the call backs end up in this code. The basic idea is to
956 * allocate extents on disk for the range, and create ordered data structs
957 * in ram to track those extents.
958 *
959 * locked_page is the page that writepage had locked already. We use
960 * it to make sure we don't do extra locks or unlocks.
961 *
962 * *page_started is set to one if we unlock locked_page and do everything
963 * required to start IO on it. It may be clean and already done with
964 * IO when we return.
965 */
Josef Bacik00361582013-08-14 14:02:47 -0400966static noinline int cow_file_range(struct inode *inode,
967 struct page *locked_page,
Nikolay Borisov74e91942019-07-17 16:18:16 +0300968 u64 start, u64 end, int *page_started,
Nikolay Borisov330a5822019-07-17 16:18:17 +0300969 unsigned long *nr_written, int unlock)
Chris Mason771ed682008-11-06 22:02:51 -0500970{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400971 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik00361582013-08-14 14:02:47 -0400972 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason771ed682008-11-06 22:02:51 -0500973 u64 alloc_hint = 0;
974 u64 num_bytes;
975 unsigned long ram_size;
Filipe Mananaa315e682017-03-06 23:04:20 +0000976 u64 cur_alloc_size = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400977 u64 blocksize = fs_info->sectorsize;
Chris Mason771ed682008-11-06 22:02:51 -0500978 struct btrfs_key ins;
979 struct extent_map *em;
Filipe Mananaa315e682017-03-06 23:04:20 +0000980 unsigned clear_bits;
981 unsigned long page_ops;
982 bool extent_reserved = false;
Chris Mason771ed682008-11-06 22:02:51 -0500983 int ret = 0;
984
Nikolay Borisov70ddc552017-02-20 13:50:35 +0200985 if (btrfs_is_free_space_inode(BTRFS_I(inode))) {
Josef Bacik02ecd2c2013-10-25 16:19:08 -0400986 WARN_ON_ONCE(1);
Josef Bacik29bce2f2014-02-07 12:21:23 -0500987 ret = -EINVAL;
988 goto out_unlock;
Josef Bacik02ecd2c2013-10-25 16:19:08 -0400989 }
Chris Mason771ed682008-11-06 22:02:51 -0500990
Qu Wenruofda28322013-02-26 08:10:22 +0000991 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Mason771ed682008-11-06 22:02:51 -0500992 num_bytes = max(blocksize, num_bytes);
Anand Jain566b1762018-02-15 18:07:59 +0800993 ASSERT(num_bytes <= btrfs_super_total_bytes(fs_info->super_copy));
Chris Mason771ed682008-11-06 22:02:51 -0500994
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200995 inode_should_defrag(BTRFS_I(inode), start, end, num_bytes, SZ_64K);
Chris Mason4cb53002011-05-24 15:35:30 -0400996
Chris Mason771ed682008-11-06 22:02:51 -0500997 if (start == 0) {
998 /* lets try to make an inline extent */
Nikolay Borisovd02c0e22018-03-02 09:43:15 +0200999 ret = cow_file_range_inline(inode, start, end, 0,
1000 BTRFS_COMPRESS_NONE, NULL);
Chris Mason771ed682008-11-06 22:02:51 -05001001 if (ret == 0) {
Josef Bacik8b62f872017-10-19 14:15:55 -04001002 /*
1003 * We use DO_ACCOUNTING here because we need the
1004 * delalloc_release_metadata to be run _after_ we drop
1005 * our outstanding extent for clearing delalloc for this
1006 * range.
1007 */
Nikolay Borisov74e91942019-07-17 16:18:16 +03001008 extent_clear_unlock_delalloc(inode, start, end, NULL,
Josef Bacikc2790a22013-07-29 11:20:47 -04001009 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik8b62f872017-10-19 14:15:55 -04001010 EXTENT_DELALLOC_NEW | EXTENT_DEFRAG |
1011 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001012 PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
1013 PAGE_END_WRITEBACK);
Chris Mason771ed682008-11-06 22:02:51 -05001014 *nr_written = *nr_written +
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001015 (end - start + PAGE_SIZE) / PAGE_SIZE;
Chris Mason771ed682008-11-06 22:02:51 -05001016 *page_started = 1;
Chris Mason771ed682008-11-06 22:02:51 -05001017 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001018 } else if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001019 goto out_unlock;
Chris Mason771ed682008-11-06 22:02:51 -05001020 }
1021 }
Chris Masonc8b97812008-10-29 14:49:59 -04001022
Josef Bacik4b46fce2010-05-23 11:00:55 -04001023 alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02001024 btrfs_drop_extent_cache(BTRFS_I(inode), start,
1025 start + num_bytes - 1, 0);
Chris Mason3b951512008-04-17 11:29:12 -04001026
Anand Jain3752d222018-02-15 12:29:38 +08001027 while (num_bytes > 0) {
1028 cur_alloc_size = num_bytes;
Wang Xiaoguang18513092016-07-25 15:51:40 +08001029 ret = btrfs_reserve_extent(root, cur_alloc_size, cur_alloc_size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001030 fs_info->sectorsize, 0, alloc_hint,
Miao Xiee570fd22014-06-19 10:42:50 +08001031 &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04001032 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001033 goto out_unlock;
Filipe Mananaa315e682017-03-06 23:04:20 +00001034 cur_alloc_size = ins.offset;
1035 extent_reserved = true;
Chris Masond3977122009-01-05 21:25:51 -05001036
Chris Mason771ed682008-11-06 22:02:51 -05001037 ram_size = ins.offset;
Liu Bo6f9994d2017-01-31 07:50:22 -08001038 em = create_io_em(inode, start, ins.offset, /* len */
1039 start, /* orig_start */
1040 ins.objectid, /* block_start */
1041 ins.offset, /* block_len */
1042 ins.offset, /* orig_block_len */
1043 ram_size, /* ram_bytes */
1044 BTRFS_COMPRESS_NONE, /* compress_type */
Liu Bo1af4a0a2017-02-13 15:35:09 -08001045 BTRFS_ORDERED_REGULAR /* type */);
Su Yue090a127a2018-05-30 16:48:56 +08001046 if (IS_ERR(em)) {
1047 ret = PTR_ERR(em);
Liu Boace68ba2013-04-22 10:53:47 +00001048 goto out_reserve;
Su Yue090a127a2018-05-30 16:48:56 +08001049 }
Liu Bo6f9994d2017-01-31 07:50:22 -08001050 free_extent_map(em);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001051
Chris Masone6dcd2d2008-07-17 12:53:50 -04001052 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
Chris Mason771ed682008-11-06 22:02:51 -05001053 ram_size, cur_alloc_size, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001054 if (ret)
Filipe Mananad9f85962014-08-25 10:43:00 +01001055 goto out_drop_extent_cache;
Chris Masonc8b97812008-10-29 14:49:59 -04001056
Yan Zheng17d217f2008-12-12 10:03:38 -05001057 if (root->root_key.objectid ==
1058 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1059 ret = btrfs_reloc_clone_csums(inode, start,
1060 cur_alloc_size);
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001061 /*
1062 * Only drop cache here, and process as normal.
1063 *
1064 * We must not allow extent_clear_unlock_delalloc()
1065 * at out_unlock label to free meta of this ordered
1066 * extent, as its meta should be freed by
1067 * btrfs_finish_ordered_io().
1068 *
1069 * So we must continue until @start is increased to
1070 * skip current ordered extent.
1071 */
Josef Bacik00361582013-08-14 14:02:47 -04001072 if (ret)
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001073 btrfs_drop_extent_cache(BTRFS_I(inode), start,
1074 start + ram_size - 1, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001075 }
1076
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001077 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana9cfa3e32016-04-26 15:39:32 +01001078
Chris Masonc8b97812008-10-29 14:49:59 -04001079 /* we're not doing compressed IO, don't unlock the first
1080 * page (which the caller expects to stay locked), don't
1081 * clear any dirty bits and don't set any writeback bits
Chris Mason8b62b722009-09-02 16:53:46 -04001082 *
1083 * Do set the Private2 bit so we know this page was properly
1084 * setup for writepage
Chris Masonc8b97812008-10-29 14:49:59 -04001085 */
Filipe Mananaa315e682017-03-06 23:04:20 +00001086 page_ops = unlock ? PAGE_UNLOCK : 0;
1087 page_ops |= PAGE_SET_PRIVATE2;
Chris Masona791e352009-10-08 11:27:10 -04001088
Josef Bacikc2790a22013-07-29 11:20:47 -04001089 extent_clear_unlock_delalloc(inode, start,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001090 start + ram_size - 1,
Nikolay Borisov74e91942019-07-17 16:18:16 +03001091 locked_page,
Josef Bacikc2790a22013-07-29 11:20:47 -04001092 EXTENT_LOCKED | EXTENT_DELALLOC,
Filipe Mananaa315e682017-03-06 23:04:20 +00001093 page_ops);
Anand Jain3752d222018-02-15 12:29:38 +08001094 if (num_bytes < cur_alloc_size)
1095 num_bytes = 0;
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001096 else
Anand Jain3752d222018-02-15 12:29:38 +08001097 num_bytes -= cur_alloc_size;
Chris Masonc59f8952007-12-17 20:14:04 -05001098 alloc_hint = ins.objectid + ins.offset;
1099 start += cur_alloc_size;
Filipe Mananaa315e682017-03-06 23:04:20 +00001100 extent_reserved = false;
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001101
1102 /*
1103 * btrfs_reloc_clone_csums() error, since start is increased
1104 * extent_clear_unlock_delalloc() at out_unlock label won't
1105 * free metadata of current ordered extent, we're OK to exit.
1106 */
1107 if (ret)
1108 goto out_unlock;
Chris Masonb888db22007-08-27 16:49:44 -04001109 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001110out:
Chris Masonbe20aa92007-12-17 20:14:01 -05001111 return ret;
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001112
Filipe Mananad9f85962014-08-25 10:43:00 +01001113out_drop_extent_cache:
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02001114 btrfs_drop_extent_cache(BTRFS_I(inode), start, start + ram_size - 1, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001115out_reserve:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001116 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001117 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001118out_unlock:
Filipe Mananaa7e3b972017-04-03 10:45:46 +01001119 clear_bits = EXTENT_LOCKED | EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
1120 EXTENT_DEFRAG | EXTENT_CLEAR_META_RESV;
Filipe Mananaa315e682017-03-06 23:04:20 +00001121 page_ops = PAGE_UNLOCK | PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
1122 PAGE_END_WRITEBACK;
1123 /*
1124 * If we reserved an extent for our delalloc range (or a subrange) and
1125 * failed to create the respective ordered extent, then it means that
1126 * when we reserved the extent we decremented the extent's size from
1127 * the data space_info's bytes_may_use counter and incremented the
1128 * space_info's bytes_reserved counter by the same amount. We must make
1129 * sure extent_clear_unlock_delalloc() does not try to decrement again
1130 * the data space_info's bytes_may_use counter, therefore we do not pass
1131 * it the flag EXTENT_CLEAR_DATA_RESV.
1132 */
1133 if (extent_reserved) {
1134 extent_clear_unlock_delalloc(inode, start,
1135 start + cur_alloc_size,
Filipe Mananaa315e682017-03-06 23:04:20 +00001136 locked_page,
1137 clear_bits,
1138 page_ops);
1139 start += cur_alloc_size;
1140 if (start >= end)
1141 goto out;
1142 }
Nikolay Borisov74e91942019-07-17 16:18:16 +03001143 extent_clear_unlock_delalloc(inode, start, end, locked_page,
Filipe Mananaa315e682017-03-06 23:04:20 +00001144 clear_bits | EXTENT_CLEAR_DATA_RESV,
1145 page_ops);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001146 goto out;
Chris Mason771ed682008-11-06 22:02:51 -05001147}
Chris Masonc8b97812008-10-29 14:49:59 -04001148
Chris Mason771ed682008-11-06 22:02:51 -05001149/*
1150 * work queue call back to started compression on a file and pages
1151 */
1152static noinline void async_cow_start(struct btrfs_work *work)
1153{
Nikolay Borisovb5326272019-03-12 17:20:25 +02001154 struct async_chunk *async_chunk;
Nikolay Borisovac3e9932019-07-17 14:41:44 +03001155 int compressed_extents;
Chris Mason771ed682008-11-06 22:02:51 -05001156
Nikolay Borisovb5326272019-03-12 17:20:25 +02001157 async_chunk = container_of(work, struct async_chunk, work);
Chris Mason771ed682008-11-06 22:02:51 -05001158
Nikolay Borisovac3e9932019-07-17 14:41:44 +03001159 compressed_extents = compress_file_range(async_chunk);
1160 if (compressed_extents == 0) {
Nikolay Borisovb5326272019-03-12 17:20:25 +02001161 btrfs_add_delayed_iput(async_chunk->inode);
1162 async_chunk->inode = NULL;
Josef Bacik8180ef82012-06-08 15:16:12 -04001163 }
Chris Mason771ed682008-11-06 22:02:51 -05001164}
1165
1166/*
1167 * work queue call back to submit previously compressed pages
1168 */
1169static noinline void async_cow_submit(struct btrfs_work *work)
1170{
Nikolay Borisovc5a68ae2019-03-12 17:20:26 +02001171 struct async_chunk *async_chunk = container_of(work, struct async_chunk,
1172 work);
1173 struct btrfs_fs_info *fs_info = btrfs_work_owner(work);
Chris Mason771ed682008-11-06 22:02:51 -05001174 unsigned long nr_pages;
1175
Nikolay Borisovb5326272019-03-12 17:20:25 +02001176 nr_pages = (async_chunk->end - async_chunk->start + PAGE_SIZE) >>
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001177 PAGE_SHIFT;
Chris Mason771ed682008-11-06 22:02:51 -05001178
David Sterba093258e2018-02-26 16:15:17 +01001179 /* atomic_sub_return implies a barrier */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001180 if (atomic_sub_return(nr_pages, &fs_info->async_delalloc_pages) <
David Sterba093258e2018-02-26 16:15:17 +01001181 5 * SZ_1M)
1182 cond_wake_up_nomb(&fs_info->async_submit_wait);
Chris Mason771ed682008-11-06 22:02:51 -05001183
Nikolay Borisov4546d172019-01-03 10:50:03 +02001184 /*
Nikolay Borisovb5326272019-03-12 17:20:25 +02001185 * ->inode could be NULL if async_chunk_start has failed to compress,
Nikolay Borisov4546d172019-01-03 10:50:03 +02001186 * in which case we don't have anything to submit, yet we need to
1187 * always adjust ->async_delalloc_pages as its paired with the init
1188 * happening in cow_file_range_async
1189 */
Nikolay Borisovb5326272019-03-12 17:20:25 +02001190 if (async_chunk->inode)
1191 submit_compressed_extents(async_chunk);
Chris Mason771ed682008-11-06 22:02:51 -05001192}
Chris Masonc8b97812008-10-29 14:49:59 -04001193
Chris Mason771ed682008-11-06 22:02:51 -05001194static noinline void async_cow_free(struct btrfs_work *work)
1195{
Nikolay Borisovb5326272019-03-12 17:20:25 +02001196 struct async_chunk *async_chunk;
Nikolay Borisov97db1202019-03-12 17:20:24 +02001197
Nikolay Borisovb5326272019-03-12 17:20:25 +02001198 async_chunk = container_of(work, struct async_chunk, work);
1199 if (async_chunk->inode)
1200 btrfs_add_delayed_iput(async_chunk->inode);
Nikolay Borisov97db1202019-03-12 17:20:24 +02001201 /*
1202 * Since the pointer to 'pending' is at the beginning of the array of
Nikolay Borisovb5326272019-03-12 17:20:25 +02001203 * async_chunk's, freeing it ensures the whole array has been freed.
Nikolay Borisov97db1202019-03-12 17:20:24 +02001204 */
Nikolay Borisovb5326272019-03-12 17:20:25 +02001205 if (atomic_dec_and_test(async_chunk->pending))
Nikolay Borisovb1c16ac2019-04-01 11:29:57 +03001206 kvfree(async_chunk->pending);
Chris Mason771ed682008-11-06 22:02:51 -05001207}
1208
1209static int cow_file_range_async(struct inode *inode, struct page *locked_page,
1210 u64 start, u64 end, int *page_started,
Liu Bof82b7352017-10-23 23:18:16 -06001211 unsigned long *nr_written,
1212 unsigned int write_flags)
Chris Mason771ed682008-11-06 22:02:51 -05001213{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001214 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Nikolay Borisov97db1202019-03-12 17:20:24 +02001215 struct async_cow *ctx;
1216 struct async_chunk *async_chunk;
Chris Mason771ed682008-11-06 22:02:51 -05001217 unsigned long nr_pages;
1218 u64 cur_end;
Nikolay Borisov97db1202019-03-12 17:20:24 +02001219 u64 num_chunks = DIV_ROUND_UP(end - start, SZ_512K);
1220 int i;
1221 bool should_compress;
Nikolay Borisovb1c16ac2019-04-01 11:29:57 +03001222 unsigned nofs_flag;
Chris Mason771ed682008-11-06 22:02:51 -05001223
Nikolay Borisov69684c52019-03-12 17:20:28 +02001224 unlock_extent(&BTRFS_I(inode)->io_tree, start, end);
Nikolay Borisov97db1202019-03-12 17:20:24 +02001225
1226 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS &&
1227 !btrfs_test_opt(fs_info, FORCE_COMPRESS)) {
1228 num_chunks = 1;
1229 should_compress = false;
1230 } else {
1231 should_compress = true;
1232 }
1233
Nikolay Borisovb1c16ac2019-04-01 11:29:57 +03001234 nofs_flag = memalloc_nofs_save();
1235 ctx = kvmalloc(struct_size(ctx, chunks, num_chunks), GFP_KERNEL);
1236 memalloc_nofs_restore(nofs_flag);
1237
Nikolay Borisov97db1202019-03-12 17:20:24 +02001238 if (!ctx) {
1239 unsigned clear_bits = EXTENT_LOCKED | EXTENT_DELALLOC |
1240 EXTENT_DELALLOC_NEW | EXTENT_DEFRAG |
1241 EXTENT_DO_ACCOUNTING;
1242 unsigned long page_ops = PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
1243 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK |
1244 PAGE_SET_ERROR;
1245
Nikolay Borisov74e91942019-07-17 16:18:16 +03001246 extent_clear_unlock_delalloc(inode, start, end, locked_page,
Nikolay Borisov97db1202019-03-12 17:20:24 +02001247 clear_bits, page_ops);
1248 return -ENOMEM;
1249 }
1250
1251 async_chunk = ctx->chunks;
1252 atomic_set(&ctx->num_chunks, num_chunks);
1253
1254 for (i = 0; i < num_chunks; i++) {
1255 if (should_compress)
1256 cur_end = min(end, start + SZ_512K - 1);
1257 else
1258 cur_end = end;
1259
Nikolay Borisovbd4691a2019-01-03 10:50:01 +02001260 /*
1261 * igrab is called higher up in the call chain, take only the
1262 * lightweight reference for the callback lifetime
1263 */
1264 ihold(inode);
Nikolay Borisov97db1202019-03-12 17:20:24 +02001265 async_chunk[i].pending = &ctx->num_chunks;
1266 async_chunk[i].inode = inode;
1267 async_chunk[i].start = start;
1268 async_chunk[i].end = cur_end;
Nikolay Borisov97db1202019-03-12 17:20:24 +02001269 async_chunk[i].write_flags = write_flags;
1270 INIT_LIST_HEAD(&async_chunk[i].extents);
Chris Mason771ed682008-11-06 22:02:51 -05001271
Chris Mason1d53c9e2019-07-10 12:28:16 -07001272 /*
1273 * The locked_page comes all the way from writepage and its
1274 * the original page we were actually given. As we spread
1275 * this large delalloc region across multiple async_chunk
1276 * structs, only the first struct needs a pointer to locked_page
1277 *
1278 * This way we don't need racey decisions about who is supposed
1279 * to unlock it.
1280 */
1281 if (locked_page) {
1282 async_chunk[i].locked_page = locked_page;
1283 locked_page = NULL;
1284 } else {
1285 async_chunk[i].locked_page = NULL;
1286 }
1287
Omar Sandovala0cac0e2019-09-16 11:30:57 -07001288 btrfs_init_work(&async_chunk[i].work, async_cow_start,
1289 async_cow_submit, async_cow_free);
Chris Mason771ed682008-11-06 22:02:51 -05001290
Nikolay Borisov97db1202019-03-12 17:20:24 +02001291 nr_pages = DIV_ROUND_UP(cur_end - start, PAGE_SIZE);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001292 atomic_add(nr_pages, &fs_info->async_delalloc_pages);
Chris Mason771ed682008-11-06 22:02:51 -05001293
Nikolay Borisov97db1202019-03-12 17:20:24 +02001294 btrfs_queue_work(fs_info->delalloc_workers, &async_chunk[i].work);
Chris Mason771ed682008-11-06 22:02:51 -05001295
Chris Mason771ed682008-11-06 22:02:51 -05001296 *nr_written += nr_pages;
1297 start = cur_end + 1;
1298 }
1299 *page_started = 1;
1300 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001301}
1302
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001303static noinline int csum_exist_in_range(struct btrfs_fs_info *fs_info,
Yan Zheng17d217f2008-12-12 10:03:38 -05001304 u64 bytenr, u64 num_bytes)
1305{
1306 int ret;
1307 struct btrfs_ordered_sum *sums;
1308 LIST_HEAD(list);
1309
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001310 ret = btrfs_lookup_csums_range(fs_info->csum_root, bytenr,
Arne Jansena2de7332011-03-08 14:14:00 +01001311 bytenr + num_bytes - 1, &list, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001312 if (ret == 0 && list_empty(&list))
1313 return 0;
1314
1315 while (!list_empty(&list)) {
1316 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1317 list_del(&sums->list);
1318 kfree(sums);
1319 }
Liu Bo58113752018-01-31 17:09:13 -07001320 if (ret < 0)
1321 return ret;
Yan Zheng17d217f2008-12-12 10:03:38 -05001322 return 1;
1323}
1324
Chris Masond352ac62008-09-29 15:18:18 -04001325/*
1326 * when nowcow writeback call back. This checks for snapshots or COW copies
1327 * of the extents that exist in the file, and COWs the file as required.
1328 *
1329 * If no cow copies or snapshots exist, we write directly to the existing
1330 * blocks on disk
1331 */
Chris Mason7f366cf2009-03-12 20:12:45 -04001332static noinline int run_delalloc_nocow(struct inode *inode,
1333 struct page *locked_page,
Nikolay Borisov3e024842019-08-21 10:42:03 +03001334 const u64 start, const u64 end,
1335 int *page_started, int force,
1336 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001337{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001338 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masonbe20aa92007-12-17 20:14:01 -05001339 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masonbe20aa92007-12-17 20:14:01 -05001340 struct btrfs_path *path;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001341 u64 cow_start = (u64)-1;
1342 u64 cur_offset = start;
Robbie Ko8ecebf4d2018-08-06 10:30:30 +08001343 int ret;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001344 bool check_prev = true;
1345 const bool freespace_inode = btrfs_is_free_space_inode(BTRFS_I(inode));
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02001346 u64 ino = btrfs_ino(BTRFS_I(inode));
Nikolay Borisov762bf092019-08-22 17:24:20 +03001347 bool nocow = false;
1348 u64 disk_bytenr = 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001349
1350 path = btrfs_alloc_path();
Josef Bacik17ca04a2012-05-31 15:58:55 -04001351 if (!path) {
Nikolay Borisov74e91942019-07-17 16:18:16 +03001352 extent_clear_unlock_delalloc(inode, start, end, locked_page,
Josef Bacikc2790a22013-07-29 11:20:47 -04001353 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -04001354 EXTENT_DO_ACCOUNTING |
1355 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001356 PAGE_CLEAR_DIRTY |
1357 PAGE_SET_WRITEBACK |
1358 PAGE_END_WRITEBACK);
Mark Fashehd8926bb2011-07-13 10:38:47 -07001359 return -ENOMEM;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001360 }
Li Zefan82d59022011-04-20 10:33:24 +08001361
Yan Zheng80ff3852008-10-30 14:20:02 -04001362 while (1) {
Nikolay Borisov3e024842019-08-21 10:42:03 +03001363 struct btrfs_key found_key;
1364 struct btrfs_file_extent_item *fi;
1365 struct extent_buffer *leaf;
1366 u64 extent_end;
1367 u64 extent_offset;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001368 u64 num_bytes = 0;
1369 u64 disk_num_bytes;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001370 u64 ram_bytes;
1371 int extent_type;
Nikolay Borisov762bf092019-08-22 17:24:20 +03001372
1373 nocow = false;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001374
Liu Boe4c3b2d2017-01-30 12:25:28 -08001375 ret = btrfs_lookup_file_extent(NULL, root, path, ino,
Yan Zheng80ff3852008-10-30 14:20:02 -04001376 cur_offset, 0);
Josef Bacikd788a342013-10-25 16:55:08 -04001377 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001378 goto error;
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001379
1380 /*
1381 * If there is no extent for our range when doing the initial
1382 * search, then go back to the previous slot as it will be the
1383 * one containing the search offset
1384 */
Yan Zheng80ff3852008-10-30 14:20:02 -04001385 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1386 leaf = path->nodes[0];
1387 btrfs_item_key_to_cpu(leaf, &found_key,
1388 path->slots[0] - 1);
Li Zefan33345d012011-04-20 10:31:50 +08001389 if (found_key.objectid == ino &&
Yan Zheng80ff3852008-10-30 14:20:02 -04001390 found_key.type == BTRFS_EXTENT_DATA_KEY)
1391 path->slots[0]--;
1392 }
Nikolay Borisov3e024842019-08-21 10:42:03 +03001393 check_prev = false;
Yan Zheng80ff3852008-10-30 14:20:02 -04001394next_slot:
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001395 /* Go to next leaf if we have exhausted the current one */
Yan Zheng80ff3852008-10-30 14:20:02 -04001396 leaf = path->nodes[0];
1397 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1398 ret = btrfs_next_leaf(root, path);
Liu Boe8916692018-01-25 11:02:50 -07001399 if (ret < 0) {
1400 if (cow_start != (u64)-1)
1401 cur_offset = cow_start;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001402 goto error;
Liu Boe8916692018-01-25 11:02:50 -07001403 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001404 if (ret > 0)
1405 break;
1406 leaf = path->nodes[0];
1407 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001408
Yan Zheng80ff3852008-10-30 14:20:02 -04001409 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001410
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001411 /* Didn't find anything for our INO */
Filipe Manana1d512cb2015-11-09 00:33:58 +00001412 if (found_key.objectid > ino)
1413 break;
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001414 /*
1415 * Keep searching until we find an EXTENT_ITEM or there are no
1416 * more extents for this inode
1417 */
Filipe Manana1d512cb2015-11-09 00:33:58 +00001418 if (WARN_ON_ONCE(found_key.objectid < ino) ||
1419 found_key.type < BTRFS_EXTENT_DATA_KEY) {
1420 path->slots[0]++;
1421 goto next_slot;
1422 }
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001423
1424 /* Found key is not EXTENT_DATA_KEY or starts after req range */
Filipe Manana1d512cb2015-11-09 00:33:58 +00001425 if (found_key.type > BTRFS_EXTENT_DATA_KEY ||
Yan Zheng80ff3852008-10-30 14:20:02 -04001426 found_key.offset > end)
1427 break;
Chris Masonbe20aa92007-12-17 20:14:01 -05001428
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001429 /*
1430 * If the found extent starts after requested offset, then
1431 * adjust extent_end to be right before this extent begins
1432 */
Yan Zheng80ff3852008-10-30 14:20:02 -04001433 if (found_key.offset > cur_offset) {
1434 extent_end = found_key.offset;
Chris Masone9061e22009-10-09 09:57:45 -04001435 extent_type = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001436 goto out_check;
1437 }
Chris Masonc31f8832008-01-08 15:46:31 -05001438
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001439 /*
1440 * Found extent which begins before our range and potentially
1441 * intersect it
1442 */
Yan Zheng80ff3852008-10-30 14:20:02 -04001443 fi = btrfs_item_ptr(leaf, path->slots[0],
1444 struct btrfs_file_extent_item);
1445 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001446
Josef Bacikcc95bef2013-04-04 14:31:27 -04001447 ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
Yan Zhengd899e052008-10-30 14:25:28 -04001448 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1449 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001450 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001451 extent_offset = btrfs_file_extent_offset(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001452 extent_end = found_key.offset +
1453 btrfs_file_extent_num_bytes(leaf, fi);
Josef Bacikb4939682012-12-03 10:31:19 -05001454 disk_num_bytes =
1455 btrfs_file_extent_disk_num_bytes(leaf, fi);
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001456 /*
1457 * If extent we got ends before our range starts, skip
1458 * to next extent
1459 */
Yan Zheng80ff3852008-10-30 14:20:02 -04001460 if (extent_end <= start) {
1461 path->slots[0]++;
1462 goto next_slot;
1463 }
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001464 /* Skip holes */
Yan Zheng17d217f2008-12-12 10:03:38 -05001465 if (disk_bytenr == 0)
1466 goto out_check;
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001467 /* Skip compressed/encrypted/encoded extents */
Yan Zheng80ff3852008-10-30 14:20:02 -04001468 if (btrfs_file_extent_compression(leaf, fi) ||
1469 btrfs_file_extent_encryption(leaf, fi) ||
1470 btrfs_file_extent_other_encoding(leaf, fi))
1471 goto out_check;
Ethan Lien78d42952018-05-17 14:58:29 +08001472 /*
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001473 * If extent is created before the last volume's snapshot
1474 * this implies the extent is shared, hence we can't do
1475 * nocow. This is the same check as in
1476 * btrfs_cross_ref_exist but without calling
1477 * btrfs_search_slot.
Ethan Lien78d42952018-05-17 14:58:29 +08001478 */
Nikolay Borisov3e024842019-08-21 10:42:03 +03001479 if (!freespace_inode &&
Lu Fengqi27a7ff52018-11-29 17:31:32 +08001480 btrfs_file_extent_generation(leaf, fi) <=
Ethan Lien78d42952018-05-17 14:58:29 +08001481 btrfs_root_last_snapshot(&root->root_item))
1482 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001483 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1484 goto out_check;
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001485 /* If extent is RO, we must COW it */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001486 if (btrfs_extent_readonly(fs_info, disk_bytenr))
Yan Zheng80ff3852008-10-30 14:20:02 -04001487 goto out_check;
Liu Bo58113752018-01-31 17:09:13 -07001488 ret = btrfs_cross_ref_exist(root, ino,
1489 found_key.offset -
1490 extent_offset, disk_bytenr);
1491 if (ret) {
1492 /*
1493 * ret could be -EIO if the above fails to read
1494 * metadata.
1495 */
1496 if (ret < 0) {
1497 if (cow_start != (u64)-1)
1498 cur_offset = cow_start;
1499 goto error;
1500 }
1501
Nikolay Borisov3e024842019-08-21 10:42:03 +03001502 WARN_ON_ONCE(freespace_inode);
Yan Zheng17d217f2008-12-12 10:03:38 -05001503 goto out_check;
Liu Bo58113752018-01-31 17:09:13 -07001504 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001505 disk_bytenr += extent_offset;
Yan Zheng17d217f2008-12-12 10:03:38 -05001506 disk_bytenr += cur_offset - found_key.offset;
1507 num_bytes = min(end + 1, extent_end) - cur_offset;
1508 /*
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001509 * If there are pending snapshots for this root, we
1510 * fall into common COW way
Wang Shilonge9894fd2014-03-27 11:12:25 +08001511 */
Nikolay Borisov3e024842019-08-21 10:42:03 +03001512 if (!freespace_inode && atomic_read(&root->snapshot_force_cow))
Robbie Ko8ecebf4d2018-08-06 10:30:30 +08001513 goto out_check;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001514 /*
Yan Zheng17d217f2008-12-12 10:03:38 -05001515 * force cow if csum exists in the range.
1516 * this ensure that csum for a given extent are
1517 * either valid or do not exist.
1518 */
Liu Bo58113752018-01-31 17:09:13 -07001519 ret = csum_exist_in_range(fs_info, disk_bytenr,
1520 num_bytes);
1521 if (ret) {
Liu Bo58113752018-01-31 17:09:13 -07001522 /*
1523 * ret could be -EIO if the above fails to read
1524 * metadata.
1525 */
1526 if (ret < 0) {
1527 if (cow_start != (u64)-1)
1528 cur_offset = cow_start;
1529 goto error;
1530 }
Nikolay Borisov3e024842019-08-21 10:42:03 +03001531 WARN_ON_ONCE(freespace_inode);
Yan Zheng17d217f2008-12-12 10:03:38 -05001532 goto out_check;
Robbie Ko91e1f562016-10-07 10:01:29 +08001533 }
Robbie Ko8ecebf4d2018-08-06 10:30:30 +08001534 if (!btrfs_inc_nocow_writers(fs_info, disk_bytenr))
Filipe Mananaf78c4362016-05-09 13:15:41 +01001535 goto out_check;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001536 nocow = true;
Yan Zheng80ff3852008-10-30 14:20:02 -04001537 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Nikolay Borisove8e21002019-08-22 17:25:23 +03001538 extent_end = found_key.offset + ram_bytes;
1539 extent_end = ALIGN(extent_end, fs_info->sectorsize);
Nikolay Borisov922f0512019-08-05 17:47:06 +03001540 /* Skip extents outside of our requested range */
1541 if (extent_end <= start) {
1542 path->slots[0]++;
1543 goto next_slot;
1544 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001545 } else {
Nikolay Borisove8e21002019-08-22 17:25:23 +03001546 /* If this triggers then we have a memory corruption */
Arnd Bergmann290342f2019-03-25 14:02:25 +01001547 BUG();
Yan Zheng80ff3852008-10-30 14:20:02 -04001548 }
1549out_check:
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001550 /*
1551 * If nocow is false then record the beginning of the range
1552 * that needs to be COWed
1553 */
Yan Zheng80ff3852008-10-30 14:20:02 -04001554 if (!nocow) {
1555 if (cow_start == (u64)-1)
1556 cow_start = cur_offset;
1557 cur_offset = extent_end;
1558 if (cur_offset > end)
1559 break;
1560 path->slots[0]++;
1561 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001562 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001563
David Sterbab3b4aa72011-04-21 01:20:15 +02001564 btrfs_release_path(path);
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001565
1566 /*
1567 * COW range from cow_start to found_key.offset - 1. As the key
1568 * will contain the beginning of the first extent that can be
1569 * NOCOW, following one which needs to be COW'ed
1570 */
Yan Zheng80ff3852008-10-30 14:20:02 -04001571 if (cow_start != (u64)-1) {
Josef Bacik00361582013-08-14 14:02:47 -04001572 ret = cow_file_range(inode, locked_page,
1573 cow_start, found_key.offset - 1,
Nikolay Borisov330a5822019-07-17 16:18:17 +03001574 page_started, nr_written, 1);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001575 if (ret) {
Filipe Mananaf78c4362016-05-09 13:15:41 +01001576 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001577 btrfs_dec_nocow_writers(fs_info,
Filipe Mananaf78c4362016-05-09 13:15:41 +01001578 disk_bytenr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001579 goto error;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001580 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001581 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001582 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001583
Yan Zhengd899e052008-10-30 14:25:28 -04001584 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Liu Bo6f9994d2017-01-31 07:50:22 -08001585 u64 orig_start = found_key.offset - extent_offset;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001586 struct extent_map *em;
Liu Bo6f9994d2017-01-31 07:50:22 -08001587
1588 em = create_io_em(inode, cur_offset, num_bytes,
1589 orig_start,
1590 disk_bytenr, /* block_start */
1591 num_bytes, /* block_len */
1592 disk_num_bytes, /* orig_block_len */
1593 ram_bytes, BTRFS_COMPRESS_NONE,
1594 BTRFS_ORDERED_PREALLOC);
1595 if (IS_ERR(em)) {
Liu Bo6f9994d2017-01-31 07:50:22 -08001596 if (nocow)
1597 btrfs_dec_nocow_writers(fs_info,
1598 disk_bytenr);
1599 ret = PTR_ERR(em);
1600 goto error;
Yan Zhengd899e052008-10-30 14:25:28 -04001601 }
Liu Bo6f9994d2017-01-31 07:50:22 -08001602 free_extent_map(em);
Nikolay Borisovbb55f622019-08-05 17:47:05 +03001603 ret = btrfs_add_ordered_extent(inode, cur_offset,
1604 disk_bytenr, num_bytes,
1605 num_bytes,
1606 BTRFS_ORDERED_PREALLOC);
Nikolay Borisov762bf092019-08-22 17:24:20 +03001607 if (ret) {
1608 btrfs_drop_extent_cache(BTRFS_I(inode),
1609 cur_offset,
1610 cur_offset + num_bytes - 1,
1611 0);
1612 goto error;
1613 }
Yan Zhengd899e052008-10-30 14:25:28 -04001614 } else {
Nikolay Borisovbb55f622019-08-05 17:47:05 +03001615 ret = btrfs_add_ordered_extent(inode, cur_offset,
1616 disk_bytenr, num_bytes,
1617 num_bytes,
1618 BTRFS_ORDERED_NOCOW);
Nikolay Borisov762bf092019-08-22 17:24:20 +03001619 if (ret)
1620 goto error;
Yan Zhengd899e052008-10-30 14:25:28 -04001621 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001622
Filipe Mananaf78c4362016-05-09 13:15:41 +01001623 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001624 btrfs_dec_nocow_writers(fs_info, disk_bytenr);
Nikolay Borisov762bf092019-08-22 17:24:20 +03001625 nocow = false;
Chris Mason771ed682008-11-06 22:02:51 -05001626
Yan, Zhengefa56462010-05-16 10:49:59 -04001627 if (root->root_key.objectid ==
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001628 BTRFS_DATA_RELOC_TREE_OBJECTID)
1629 /*
1630 * Error handled later, as we must prevent
1631 * extent_clear_unlock_delalloc() in error handler
1632 * from freeing metadata of created ordered extent.
1633 */
Yan, Zhengefa56462010-05-16 10:49:59 -04001634 ret = btrfs_reloc_clone_csums(inode, cur_offset,
1635 num_bytes);
Yan, Zhengefa56462010-05-16 10:49:59 -04001636
Josef Bacikc2790a22013-07-29 11:20:47 -04001637 extent_clear_unlock_delalloc(inode, cur_offset,
Nikolay Borisov74e91942019-07-17 16:18:16 +03001638 cur_offset + num_bytes - 1,
Josef Bacikc2790a22013-07-29 11:20:47 -04001639 locked_page, EXTENT_LOCKED |
Wang Xiaoguang18513092016-07-25 15:51:40 +08001640 EXTENT_DELALLOC |
1641 EXTENT_CLEAR_DATA_RESV,
1642 PAGE_UNLOCK | PAGE_SET_PRIVATE2);
1643
Yan Zheng80ff3852008-10-30 14:20:02 -04001644 cur_offset = extent_end;
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001645
1646 /*
1647 * btrfs_reloc_clone_csums() error, now we're OK to call error
1648 * handler, as metadata for created ordered extent will only
1649 * be freed by btrfs_finish_ordered_io().
1650 */
1651 if (ret)
1652 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001653 if (cur_offset > end)
1654 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001655 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001656 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001657
Robbie Ko506481b2018-10-30 18:04:04 +08001658 if (cur_offset <= end && cow_start == (u64)-1)
Yan Zheng80ff3852008-10-30 14:20:02 -04001659 cow_start = cur_offset;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001660
Yan Zheng80ff3852008-10-30 14:20:02 -04001661 if (cow_start != (u64)-1) {
Robbie Ko506481b2018-10-30 18:04:04 +08001662 cur_offset = end;
Nikolay Borisov74e91942019-07-17 16:18:16 +03001663 ret = cow_file_range(inode, locked_page, cow_start, end,
Nikolay Borisov330a5822019-07-17 16:18:17 +03001664 page_started, nr_written, 1);
Josef Bacikd788a342013-10-25 16:55:08 -04001665 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001666 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001667 }
1668
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001669error:
Nikolay Borisov762bf092019-08-22 17:24:20 +03001670 if (nocow)
1671 btrfs_dec_nocow_writers(fs_info, disk_bytenr);
1672
Josef Bacik17ca04a2012-05-31 15:58:55 -04001673 if (ret && cur_offset < end)
Nikolay Borisov74e91942019-07-17 16:18:16 +03001674 extent_clear_unlock_delalloc(inode, cur_offset, end,
Josef Bacikc2790a22013-07-29 11:20:47 -04001675 locked_page, EXTENT_LOCKED |
Josef Bacik151a41b2013-07-29 13:22:24 -04001676 EXTENT_DELALLOC | EXTENT_DEFRAG |
1677 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
1678 PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -04001679 PAGE_SET_WRITEBACK |
1680 PAGE_END_WRITEBACK);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001681 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001682 return ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001683}
1684
Wang Shilong47059d92014-07-03 18:22:07 +08001685static inline int need_force_cow(struct inode *inode, u64 start, u64 end)
1686{
1687
1688 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
1689 !(BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC))
1690 return 0;
1691
1692 /*
1693 * @defrag_bytes is a hint value, no spinlock held here,
1694 * if is not zero, it means the file is defragging.
1695 * Force cow if given extent needs to be defragged.
1696 */
1697 if (BTRFS_I(inode)->defrag_bytes &&
1698 test_range_bit(&BTRFS_I(inode)->io_tree, start, end,
1699 EXTENT_DEFRAG, 0, NULL))
1700 return 1;
1701
1702 return 0;
1703}
1704
Chris Masond352ac62008-09-29 15:18:18 -04001705/*
Nikolay Borisov5eaad972018-11-01 14:09:46 +02001706 * Function to process delayed allocation (create CoW) for ranges which are
1707 * being touched for the first time.
Chris Masond352ac62008-09-29 15:18:18 -04001708 */
Nikolay Borisovbc9a8bf2018-12-19 09:50:20 +02001709int btrfs_run_delalloc_range(struct inode *inode, struct page *locked_page,
Nikolay Borisov5eaad972018-11-01 14:09:46 +02001710 u64 start, u64 end, int *page_started, unsigned long *nr_written,
1711 struct writeback_control *wbc)
Chris Masonbe20aa92007-12-17 20:14:01 -05001712{
Chris Masonbe20aa92007-12-17 20:14:01 -05001713 int ret;
Wang Shilong47059d92014-07-03 18:22:07 +08001714 int force_cow = need_force_cow(inode, start, end);
Liu Bof82b7352017-10-23 23:18:16 -06001715 unsigned int write_flags = wbc_to_write_flags(wbc);
Chris Masona2135012008-06-25 16:01:30 -04001716
Wang Shilong47059d92014-07-03 18:22:07 +08001717 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW && !force_cow) {
Chris Masonc8b97812008-10-29 14:49:59 -04001718 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001719 page_started, 1, nr_written);
Wang Shilong47059d92014-07-03 18:22:07 +08001720 } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC && !force_cow) {
Yan Zhengd899e052008-10-30 14:25:28 -04001721 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001722 page_started, 0, nr_written);
Qu Wenruo42c16da2019-07-01 05:12:46 +00001723 } else if (!inode_can_compress(inode) ||
1724 !inode_need_compress(inode, start, end)) {
Nikolay Borisov74e91942019-07-17 16:18:16 +03001725 ret = cow_file_range(inode, locked_page, start, end,
Nikolay Borisov330a5822019-07-17 16:18:17 +03001726 page_started, nr_written, 1);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001727 } else {
1728 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1729 &BTRFS_I(inode)->runtime_flags);
Chris Mason771ed682008-11-06 22:02:51 -05001730 ret = cow_file_range_async(inode, locked_page, start, end,
Liu Bof82b7352017-10-23 23:18:16 -06001731 page_started, nr_written,
1732 write_flags);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001733 }
Qu Wenruo524272602017-03-08 10:25:52 +08001734 if (ret)
Nikolay Borisovd1051d62018-11-21 17:10:52 +02001735 btrfs_cleanup_ordered_extents(inode, locked_page, start,
1736 end - start + 1);
Chris Masonb888db22007-08-27 16:49:44 -04001737 return ret;
1738}
1739
Nikolay Borisovabbb55f2018-11-01 14:09:53 +02001740void btrfs_split_delalloc_extent(struct inode *inode,
1741 struct extent_state *orig, u64 split)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001742{
Josef Bacikdcab6a32015-02-11 15:08:59 -05001743 u64 size;
1744
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001745 /* not delalloc, ignore it */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001746 if (!(orig->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001747 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001748
Josef Bacikdcab6a32015-02-11 15:08:59 -05001749 size = orig->end - orig->start + 1;
1750 if (size > BTRFS_MAX_EXTENT_SIZE) {
David Sterba823bb202017-01-04 11:09:51 +01001751 u32 num_extents;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001752 u64 new_size;
1753
1754 /*
Nikolay Borisov5c848192018-11-01 14:09:52 +02001755 * See the explanation in btrfs_merge_delalloc_extent, the same
Josef Bacikba117212015-03-13 15:01:24 -04001756 * applies here, just in reverse.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001757 */
1758 new_size = orig->end - split + 1;
David Sterba823bb202017-01-04 11:09:51 +01001759 num_extents = count_max_extents(new_size);
Josef Bacikba117212015-03-13 15:01:24 -04001760 new_size = split - orig->start;
David Sterba823bb202017-01-04 11:09:51 +01001761 num_extents += count_max_extents(new_size);
1762 if (count_max_extents(size) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001763 return;
1764 }
1765
Josef Bacik9e0baf62011-07-15 15:16:44 +00001766 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04001767 btrfs_mod_outstanding_extents(BTRFS_I(inode), 1);
Josef Bacik9e0baf62011-07-15 15:16:44 +00001768 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001769}
1770
1771/*
Nikolay Borisov5c848192018-11-01 14:09:52 +02001772 * Handle merged delayed allocation extents so we can keep track of new extents
1773 * that are just merged onto old extents, such as when we are doing sequential
1774 * writes, so we can properly account for the metadata space we'll need.
Josef Bacik9ed74f22009-09-11 16:12:44 -04001775 */
Nikolay Borisov5c848192018-11-01 14:09:52 +02001776void btrfs_merge_delalloc_extent(struct inode *inode, struct extent_state *new,
1777 struct extent_state *other)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001778{
Josef Bacikdcab6a32015-02-11 15:08:59 -05001779 u64 new_size, old_size;
David Sterba823bb202017-01-04 11:09:51 +01001780 u32 num_extents;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001781
Josef Bacik9ed74f22009-09-11 16:12:44 -04001782 /* not delalloc, ignore it */
1783 if (!(other->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001784 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001785
Josef Bacik8461a3d2015-03-13 15:12:08 -04001786 if (new->start > other->start)
1787 new_size = new->end - other->start + 1;
1788 else
1789 new_size = other->end - new->start + 1;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001790
1791 /* we're not bigger than the max, unreserve the space and go */
1792 if (new_size <= BTRFS_MAX_EXTENT_SIZE) {
1793 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04001794 btrfs_mod_outstanding_extents(BTRFS_I(inode), -1);
Josef Bacikdcab6a32015-02-11 15:08:59 -05001795 spin_unlock(&BTRFS_I(inode)->lock);
1796 return;
1797 }
1798
1799 /*
Josef Bacikba117212015-03-13 15:01:24 -04001800 * We have to add up either side to figure out how many extents were
1801 * accounted for before we merged into one big extent. If the number of
1802 * extents we accounted for is <= the amount we need for the new range
1803 * then we can return, otherwise drop. Think of it like this
1804 *
1805 * [ 4k][MAX_SIZE]
1806 *
1807 * So we've grown the extent by a MAX_SIZE extent, this would mean we
1808 * need 2 outstanding extents, on one side we have 1 and the other side
1809 * we have 1 so they are == and we can return. But in this case
1810 *
1811 * [MAX_SIZE+4k][MAX_SIZE+4k]
1812 *
1813 * Each range on their own accounts for 2 extents, but merged together
1814 * they are only 3 extents worth of accounting, so we need to drop in
1815 * this case.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001816 */
Josef Bacikba117212015-03-13 15:01:24 -04001817 old_size = other->end - other->start + 1;
David Sterba823bb202017-01-04 11:09:51 +01001818 num_extents = count_max_extents(old_size);
Josef Bacikba117212015-03-13 15:01:24 -04001819 old_size = new->end - new->start + 1;
David Sterba823bb202017-01-04 11:09:51 +01001820 num_extents += count_max_extents(old_size);
1821 if (count_max_extents(new_size) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001822 return;
1823
Josef Bacik9e0baf62011-07-15 15:16:44 +00001824 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04001825 btrfs_mod_outstanding_extents(BTRFS_I(inode), -1);
Josef Bacik9e0baf62011-07-15 15:16:44 +00001826 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001827}
1828
Miao Xieeb73c1b2013-05-15 07:48:22 +00001829static void btrfs_add_delalloc_inodes(struct btrfs_root *root,
1830 struct inode *inode)
1831{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001832 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1833
Miao Xieeb73c1b2013-05-15 07:48:22 +00001834 spin_lock(&root->delalloc_lock);
1835 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1836 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1837 &root->delalloc_inodes);
1838 set_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1839 &BTRFS_I(inode)->runtime_flags);
1840 root->nr_delalloc_inodes++;
1841 if (root->nr_delalloc_inodes == 1) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001842 spin_lock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001843 BUG_ON(!list_empty(&root->delalloc_root));
1844 list_add_tail(&root->delalloc_root,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001845 &fs_info->delalloc_roots);
1846 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001847 }
1848 }
1849 spin_unlock(&root->delalloc_lock);
1850}
1851
Nikolay Borisov2b877332018-04-27 12:21:51 +03001852
1853void __btrfs_del_delalloc_inode(struct btrfs_root *root,
1854 struct btrfs_inode *inode)
Miao Xieeb73c1b2013-05-15 07:48:22 +00001855{
David Sterba3ffbd682018-06-29 10:56:42 +02001856 struct btrfs_fs_info *fs_info = root->fs_info;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001857
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001858 if (!list_empty(&inode->delalloc_inodes)) {
1859 list_del_init(&inode->delalloc_inodes);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001860 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001861 &inode->runtime_flags);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001862 root->nr_delalloc_inodes--;
1863 if (!root->nr_delalloc_inodes) {
Nikolay Borisov7c8a0d32018-04-27 12:21:52 +03001864 ASSERT(list_empty(&root->delalloc_inodes));
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001865 spin_lock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001866 BUG_ON(list_empty(&root->delalloc_root));
1867 list_del_init(&root->delalloc_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001868 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001869 }
1870 }
Nikolay Borisov2b877332018-04-27 12:21:51 +03001871}
1872
1873static void btrfs_del_delalloc_inode(struct btrfs_root *root,
1874 struct btrfs_inode *inode)
1875{
1876 spin_lock(&root->delalloc_lock);
1877 __btrfs_del_delalloc_inode(root, inode);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001878 spin_unlock(&root->delalloc_lock);
1879}
1880
Chris Masond352ac62008-09-29 15:18:18 -04001881/*
Nikolay Borisove06a1fc2018-11-01 14:09:50 +02001882 * Properly track delayed allocation bytes in the inode and to maintain the
1883 * list of inodes that have pending delalloc work to be done.
Chris Masond352ac62008-09-29 15:18:18 -04001884 */
Nikolay Borisove06a1fc2018-11-01 14:09:50 +02001885void btrfs_set_delalloc_extent(struct inode *inode, struct extent_state *state,
1886 unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001887{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001888 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1889
Wang Shilong47059d92014-07-03 18:22:07 +08001890 if ((*bits & EXTENT_DEFRAG) && !(*bits & EXTENT_DELALLOC))
1891 WARN_ON(1);
Chris Mason75eff682008-12-15 15:54:40 -05001892 /*
1893 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001894 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001895 * bit, which is only set or cleared with irqs on
1896 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001897 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001898 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001899 u64 len = state->end + 1 - state->start;
Josef Bacik8b62f872017-10-19 14:15:55 -04001900 u32 num_extents = count_max_extents(len);
Nikolay Borisov70ddc552017-02-20 13:50:35 +02001901 bool do_list = !btrfs_is_free_space_inode(BTRFS_I(inode));
Josef Bacik9ed74f22009-09-11 16:12:44 -04001902
Josef Bacik8b62f872017-10-19 14:15:55 -04001903 spin_lock(&BTRFS_I(inode)->lock);
1904 btrfs_mod_outstanding_extents(BTRFS_I(inode), num_extents);
1905 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik287a0ab2010-03-19 18:07:23 +00001906
Josef Bacik6a3891c2015-03-16 17:38:52 -04001907 /* For sanity tests */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001908 if (btrfs_is_testing(fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04001909 return;
1910
Nikolay Borisov104b4e52017-06-20 21:01:20 +03001911 percpu_counter_add_batch(&fs_info->delalloc_bytes, len,
1912 fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001913 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001914 BTRFS_I(inode)->delalloc_bytes += len;
Wang Shilong47059d92014-07-03 18:22:07 +08001915 if (*bits & EXTENT_DEFRAG)
1916 BTRFS_I(inode)->defrag_bytes += len;
Miao Xiedf0af1a2013-01-29 10:11:59 +00001917 if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00001918 &BTRFS_I(inode)->runtime_flags))
1919 btrfs_add_delalloc_inodes(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001920 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001921 }
Filipe Mananaa7e3b972017-04-03 10:45:46 +01001922
1923 if (!(state->state & EXTENT_DELALLOC_NEW) &&
1924 (*bits & EXTENT_DELALLOC_NEW)) {
1925 spin_lock(&BTRFS_I(inode)->lock);
1926 BTRFS_I(inode)->new_delalloc_bytes += state->end + 1 -
1927 state->start;
1928 spin_unlock(&BTRFS_I(inode)->lock);
1929 }
Chris Mason291d6732008-01-29 15:55:23 -05001930}
1931
Chris Masond352ac62008-09-29 15:18:18 -04001932/*
Nikolay Borisova36bb5f2018-11-01 14:09:51 +02001933 * Once a range is no longer delalloc this function ensures that proper
1934 * accounting happens.
Chris Masond352ac62008-09-29 15:18:18 -04001935 */
Nikolay Borisova36bb5f2018-11-01 14:09:51 +02001936void btrfs_clear_delalloc_extent(struct inode *vfs_inode,
1937 struct extent_state *state, unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001938{
Nikolay Borisova36bb5f2018-11-01 14:09:51 +02001939 struct btrfs_inode *inode = BTRFS_I(vfs_inode);
1940 struct btrfs_fs_info *fs_info = btrfs_sb(vfs_inode->i_sb);
Wang Shilong47059d92014-07-03 18:22:07 +08001941 u64 len = state->end + 1 - state->start;
David Sterba823bb202017-01-04 11:09:51 +01001942 u32 num_extents = count_max_extents(len);
Wang Shilong47059d92014-07-03 18:22:07 +08001943
Filipe Manana4a4b9642017-07-27 19:52:55 +01001944 if ((state->state & EXTENT_DEFRAG) && (*bits & EXTENT_DEFRAG)) {
1945 spin_lock(&inode->lock);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001946 inode->defrag_bytes -= len;
Filipe Manana4a4b9642017-07-27 19:52:55 +01001947 spin_unlock(&inode->lock);
1948 }
Wang Shilong47059d92014-07-03 18:22:07 +08001949
Chris Mason75eff682008-12-15 15:54:40 -05001950 /*
1951 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001952 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001953 * bit, which is only set or cleared with irqs on
1954 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001955 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001956 struct btrfs_root *root = inode->root;
Liu Bo83eea1f2012-07-10 05:28:39 -06001957 bool do_list = !btrfs_is_free_space_inode(inode);
Chris Masonbcbfce82008-04-22 13:26:47 -04001958
Josef Bacik8b62f872017-10-19 14:15:55 -04001959 spin_lock(&inode->lock);
1960 btrfs_mod_outstanding_extents(inode, -num_extents);
1961 spin_unlock(&inode->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001962
Josef Bacikb6d08f02013-09-27 14:57:43 -04001963 /*
1964 * We don't reserve metadata space for space cache inodes so we
Andrea Gelmini52042d82018-11-28 12:05:13 +01001965 * don't need to call delalloc_release_metadata if there is an
Josef Bacikb6d08f02013-09-27 14:57:43 -04001966 * error.
1967 */
Filipe Mananaa315e682017-03-06 23:04:20 +00001968 if (*bits & EXTENT_CLEAR_META_RESV &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001969 root != fs_info->tree_root)
Qu Wenruo43b18592017-12-12 15:34:32 +08001970 btrfs_delalloc_release_metadata(inode, len, false);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001971
Josef Bacik6a3891c2015-03-16 17:38:52 -04001972 /* For sanity tests. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001973 if (btrfs_is_testing(fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04001974 return;
1975
Filipe Mananaa315e682017-03-06 23:04:20 +00001976 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID &&
1977 do_list && !(state->state & EXTENT_NORESERVE) &&
1978 (*bits & EXTENT_CLEAR_DATA_RESV))
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001979 btrfs_free_reserved_data_space_noquota(
1980 &inode->vfs_inode,
Qu Wenruo51773be2015-10-08 18:19:37 +08001981 state->start, len);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001982
Nikolay Borisov104b4e52017-06-20 21:01:20 +03001983 percpu_counter_add_batch(&fs_info->delalloc_bytes, -len,
1984 fs_info->delalloc_batch);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001985 spin_lock(&inode->lock);
1986 inode->delalloc_bytes -= len;
1987 if (do_list && inode->delalloc_bytes == 0 &&
Miao Xiedf0af1a2013-01-29 10:11:59 +00001988 test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001989 &inode->runtime_flags))
Miao Xieeb73c1b2013-05-15 07:48:22 +00001990 btrfs_del_delalloc_inode(root, inode);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001991 spin_unlock(&inode->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001992 }
Filipe Mananaa7e3b972017-04-03 10:45:46 +01001993
1994 if ((state->state & EXTENT_DELALLOC_NEW) &&
1995 (*bits & EXTENT_DELALLOC_NEW)) {
1996 spin_lock(&inode->lock);
1997 ASSERT(inode->new_delalloc_bytes >= len);
1998 inode->new_delalloc_bytes -= len;
1999 spin_unlock(&inode->lock);
2000 }
Chris Mason291d6732008-01-29 15:55:23 -05002001}
2002
Chris Masond352ac62008-09-29 15:18:18 -04002003/*
Nikolay Borisovda12fe52018-11-27 20:57:58 +02002004 * btrfs_bio_fits_in_stripe - Checks whether the size of the given bio will fit
2005 * in a chunk's stripe. This function ensures that bios do not span a
2006 * stripe/chunk
Liu Bo6f034ec2016-06-22 18:31:49 -07002007 *
Nikolay Borisovda12fe52018-11-27 20:57:58 +02002008 * @page - The page we are about to add to the bio
2009 * @size - size we want to add to the bio
2010 * @bio - bio we want to ensure is smaller than a stripe
2011 * @bio_flags - flags of the bio
2012 *
2013 * return 1 if page cannot be added to the bio
2014 * return 0 if page can be added to the bio
Liu Bo6f034ec2016-06-22 18:31:49 -07002015 * return error otherwise
Chris Masond352ac62008-09-29 15:18:18 -04002016 */
Nikolay Borisovda12fe52018-11-27 20:57:58 +02002017int btrfs_bio_fits_in_stripe(struct page *page, size_t size, struct bio *bio,
2018 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04002019{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002020 struct inode *inode = page->mapping->host;
2021 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Kent Overstreet4f024f32013-10-11 15:44:27 -07002022 u64 logical = (u64)bio->bi_iter.bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04002023 u64 length = 0;
2024 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04002025 int ret;
Nikolay Borisov89b798a2019-06-03 12:05:05 +03002026 struct btrfs_io_geometry geom;
Chris Mason239b14b2008-03-24 15:02:07 -04002027
Chris Mason771ed682008-11-06 22:02:51 -05002028 if (bio_flags & EXTENT_BIO_COMPRESSED)
2029 return 0;
2030
Kent Overstreet4f024f32013-10-11 15:44:27 -07002031 length = bio->bi_iter.bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04002032 map_length = length;
Nikolay Borisov89b798a2019-06-03 12:05:05 +03002033 ret = btrfs_get_io_geometry(fs_info, btrfs_op(bio), logical, map_length,
2034 &geom);
Liu Bo6f034ec2016-06-22 18:31:49 -07002035 if (ret < 0)
2036 return ret;
Nikolay Borisov89b798a2019-06-03 12:05:05 +03002037
2038 if (geom.len < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04002039 return 1;
Jeff Mahoney3444a972011-10-03 23:23:13 -04002040 return 0;
Chris Mason239b14b2008-03-24 15:02:07 -04002041}
2042
Chris Masond352ac62008-09-29 15:18:18 -04002043/*
2044 * in order to insert checksums into the metadata in large chunks,
2045 * we wait until bio submission time. All the pages in the bio are
2046 * checksummed and sums are attached onto the ordered extent record.
2047 *
2048 * At IO completion time the cums attached on the ordered extent record
2049 * are inserted into the btree
2050 */
David Sterbad0ee3932018-03-08 14:35:48 +01002051static blk_status_t btrfs_submit_bio_start(void *private_data, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04002052 u64 bio_offset)
Chris Mason065631f2008-02-20 12:07:25 -05002053{
Josef Bacikc6100a42017-05-05 11:57:13 -04002054 struct inode *inode = private_data;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002055 blk_status_t ret = 0;
Chris Masone0156402008-04-16 11:15:20 -04002056
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002057 ret = btrfs_csum_one_bio(inode, bio, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002058 BUG_ON(ret); /* -ENOMEM */
Chris Mason4a69a412008-11-06 22:03:00 -05002059 return 0;
2060}
Chris Masone0156402008-04-16 11:15:20 -04002061
Chris Mason4a69a412008-11-06 22:03:00 -05002062/*
Chris Masoncad321a2008-12-17 14:51:42 -05002063 * extent_io.c submission hook. This does the right thing for csum calculation
Liu Bo4c274bc2017-11-01 17:19:27 -06002064 * on write, or reading the csums from the tree before a read.
2065 *
2066 * Rules about async/sync submit,
2067 * a) read: sync submit
2068 *
2069 * b) write without checksum: sync submit
2070 *
2071 * c) write with checksum:
2072 * c-1) if bio is issued by fsync: sync submit
2073 * (sync_writers != 0)
2074 *
2075 * c-2) if root is reloc root: sync submit
2076 * (only in case of buffered IO)
2077 *
2078 * c-3) otherwise: async submit
Chris Masond352ac62008-09-29 15:18:18 -04002079 */
Nikolay Borisova56b1c72019-04-10 17:24:39 +03002080static blk_status_t btrfs_submit_bio_hook(struct inode *inode, struct bio *bio,
Nikolay Borisov50489a52019-04-10 19:46:04 +03002081 int mirror_num,
2082 unsigned long bio_flags)
2083
Chris Mason44b8bd72008-04-16 11:14:51 -04002084{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002085 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason44b8bd72008-04-16 11:14:51 -04002086 struct btrfs_root *root = BTRFS_I(inode)->root;
Chandan Rajendra0d51e282015-07-27 15:26:43 +05302087 enum btrfs_wq_endio_type metadata = BTRFS_WQ_ENDIO_DATA;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002088 blk_status_t ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04002089 int skip_sum;
Josef Bacikb812ce22012-11-16 13:56:32 -05002090 int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
Chris Mason44b8bd72008-04-16 11:14:51 -04002091
Christoph Hellwig6cbff002009-04-17 10:37:41 +02002092 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masoncad321a2008-12-17 14:51:42 -05002093
Nikolay Borisov70ddc552017-02-20 13:50:35 +02002094 if (btrfs_is_free_space_inode(BTRFS_I(inode)))
Chandan Rajendra0d51e282015-07-27 15:26:43 +05302095 metadata = BTRFS_WQ_ENDIO_FREE_SPACE;
Jeff Mahoney04173412011-10-03 23:23:12 -04002096
Mike Christie37226b22016-06-05 14:31:52 -05002097 if (bio_op(bio) != REQ_OP_WRITE) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002098 ret = btrfs_bio_wq_end_io(fs_info, bio, metadata);
Josef Bacik5fd02042012-05-02 14:00:54 -04002099 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01002100 goto out;
Josef Bacik5fd02042012-05-02 14:00:54 -04002101
Chris Masond20f7042008-12-08 16:58:54 -05002102 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Stefan Behrens61891922012-11-05 18:51:52 +01002103 ret = btrfs_submit_compressed_read(inode, bio,
2104 mirror_num,
2105 bio_flags);
2106 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00002107 } else if (!skip_sum) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002108 ret = btrfs_lookup_bio_sums(inode, bio, NULL);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00002109 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01002110 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00002111 }
Chris Mason4d1b5fb2008-08-20 09:44:52 -04002112 goto mapit;
Josef Bacikb812ce22012-11-16 13:56:32 -05002113 } else if (async && !skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05002114 /* csum items have already been cloned */
2115 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
2116 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04002117 /* we're doing a write, do the async checksumming */
Josef Bacikc6100a42017-05-05 11:57:13 -04002118 ret = btrfs_wq_submit_bio(fs_info, bio, mirror_num, bio_flags,
Nikolay Borisove7681162019-04-10 17:24:41 +03002119 0, inode, btrfs_submit_bio_start);
Stefan Behrens61891922012-11-05 18:51:52 +01002120 goto out;
Josef Bacikb812ce22012-11-16 13:56:32 -05002121 } else if (!skip_sum) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002122 ret = btrfs_csum_one_bio(inode, bio, 0, 0);
Josef Bacikb812ce22012-11-16 13:56:32 -05002123 if (ret)
2124 goto out;
Chris Mason19b9bdb2008-10-30 14:23:13 -04002125 }
2126
Chris Mason0b86a832008-03-24 15:01:56 -04002127mapit:
Chris Mason08635ba2019-07-10 12:28:14 -07002128 ret = btrfs_map_bio(fs_info, bio, mirror_num);
Stefan Behrens61891922012-11-05 18:51:52 +01002129
2130out:
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002131 if (ret) {
2132 bio->bi_status = ret;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002133 bio_endio(bio);
2134 }
Stefan Behrens61891922012-11-05 18:51:52 +01002135 return ret;
Chris Mason065631f2008-02-20 12:07:25 -05002136}
Chris Mason6885f302008-02-20 16:11:05 -05002137
Chris Masond352ac62008-09-29 15:18:18 -04002138/*
2139 * given a list of ordered sums record them in the inode. This happens
2140 * at IO completion time based on sums calculated at bio submission time.
2141 */
Chris Masonba1da2f2008-07-17 12:54:15 -04002142static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
David Sterbadf9f6282017-02-10 19:35:37 +01002143 struct inode *inode, struct list_head *list)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002144{
Chris Masone6dcd2d2008-07-17 12:53:50 -04002145 struct btrfs_ordered_sum *sum;
Nikolay Borisovac01f262018-01-08 10:59:43 +02002146 int ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002147
Qinghuang Fengc6e30872009-01-21 10:59:08 -05002148 list_for_each_entry(sum, list, list) {
David Sterba7c2871a2017-11-08 01:07:43 +01002149 trans->adding_csums = true;
Nikolay Borisovac01f262018-01-08 10:59:43 +02002150 ret = btrfs_csum_file_blocks(trans,
Chris Masond20f7042008-12-08 16:58:54 -05002151 BTRFS_I(inode)->root->fs_info->csum_root, sum);
David Sterba7c2871a2017-11-08 01:07:43 +01002152 trans->adding_csums = false;
Nikolay Borisovac01f262018-01-08 10:59:43 +02002153 if (ret)
2154 return ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002155 }
2156 return 0;
2157}
2158
Josef Bacik2ac55d42010-02-03 19:33:23 +00002159int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
Filipe Mananae3b8a482017-11-04 00:16:59 +00002160 unsigned int extra_bits,
Nikolay Borisov330a5822019-07-17 16:18:17 +03002161 struct extent_state **cached_state)
Chris Masonea8c2812008-08-04 23:17:27 -04002162{
Johannes Thumshirnfdb1e122018-12-05 15:23:04 +01002163 WARN_ON(PAGE_ALIGNED(end));
Chris Masonea8c2812008-08-04 23:17:27 -04002164 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
Filipe Mananae3b8a482017-11-04 00:16:59 +00002165 extra_bits, cached_state);
Chris Masonea8c2812008-08-04 23:17:27 -04002166}
2167
Chris Masond352ac62008-09-29 15:18:18 -04002168/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04002169struct btrfs_writepage_fixup {
2170 struct page *page;
2171 struct btrfs_work work;
2172};
2173
Christoph Hellwigb2950862008-12-02 09:54:17 -05002174static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04002175{
2176 struct btrfs_writepage_fixup *fixup;
2177 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002178 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08002179 struct extent_changeset *data_reserved = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04002180 struct page *page;
2181 struct inode *inode;
2182 u64 page_start;
2183 u64 page_end;
Jeff Mahoney87826df2012-02-15 16:23:57 +01002184 int ret;
Chris Mason247e7432008-07-17 12:53:51 -04002185
2186 fixup = container_of(work, struct btrfs_writepage_fixup, work);
2187 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -04002188again:
Chris Mason247e7432008-07-17 12:53:51 -04002189 lock_page(page);
2190 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
2191 ClearPageChecked(page);
2192 goto out_page;
2193 }
2194
2195 inode = page->mapping->host;
2196 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002197 page_end = page_offset(page) + PAGE_SIZE - 1;
Chris Mason247e7432008-07-17 12:53:51 -04002198
David Sterbaff13db42015-12-03 14:30:40 +01002199 lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002200 &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04002201
2202 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04002203 if (PagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04002204 goto out;
Chris Mason4a096752008-07-21 10:29:44 -04002205
Nikolay Borisova776c6f2017-02-20 13:50:49 +02002206 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002207 PAGE_SIZE);
Chris Mason4a096752008-07-21 10:29:44 -04002208 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00002209 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
David Sterbae43bbe52017-12-12 21:43:52 +01002210 page_end, &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04002211 unlock_page(page);
2212 btrfs_start_ordered_extent(inode, ordered, 1);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002213 btrfs_put_ordered_extent(ordered);
Chris Mason4a096752008-07-21 10:29:44 -04002214 goto again;
2215 }
Chris Mason247e7432008-07-17 12:53:51 -04002216
Qu Wenruo364ecf32017-02-27 15:10:38 +08002217 ret = btrfs_delalloc_reserve_space(inode, &data_reserved, page_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002218 PAGE_SIZE);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002219 if (ret) {
2220 mapping_set_error(page->mapping, ret);
2221 end_extent_writepage(page, ret, page_start, page_end);
2222 ClearPageChecked(page);
2223 goto out;
2224 }
2225
Nikolay Borisovf3038ee2017-12-05 09:29:19 +02002226 ret = btrfs_set_extent_delalloc(inode, page_start, page_end, 0,
Nikolay Borisov330a5822019-07-17 16:18:17 +03002227 &cached_state);
Nikolay Borisovf3038ee2017-12-05 09:29:19 +02002228 if (ret) {
2229 mapping_set_error(page->mapping, ret);
2230 end_extent_writepage(page, ret, page_start, page_end);
2231 ClearPageChecked(page);
2232 goto out;
2233 }
2234
Chris Mason247e7432008-07-17 12:53:51 -04002235 ClearPageChecked(page);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002236 set_page_dirty(page);
Qu Wenruo8702ba92019-10-14 14:34:51 +08002237 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE);
Chris Mason247e7432008-07-17 12:53:51 -04002238out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00002239 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01002240 &cached_state);
Chris Mason247e7432008-07-17 12:53:51 -04002241out_page:
2242 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002243 put_page(page);
Miao Xieb897abe2011-01-26 16:19:22 +08002244 kfree(fixup);
Qu Wenruo364ecf32017-02-27 15:10:38 +08002245 extent_changeset_free(data_reserved);
Chris Mason247e7432008-07-17 12:53:51 -04002246}
2247
2248/*
2249 * There are a few paths in the higher layers of the kernel that directly
2250 * set the page dirty bit without asking the filesystem if it is a
2251 * good idea. This causes problems because we want to make sure COW
2252 * properly happens and the data=ordered rules are followed.
2253 *
Chris Masonc8b97812008-10-29 14:49:59 -04002254 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04002255 * hasn't been properly setup for IO. We kick off an async process
2256 * to fix it up. The async helper will wait for ordered extents, set
2257 * the delalloc bit and make it safe to write the page.
2258 */
Nikolay Borisovd75855b2018-11-01 14:09:47 +02002259int btrfs_writepage_cow_fixup(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04002260{
2261 struct inode *inode = page->mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002262 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason247e7432008-07-17 12:53:51 -04002263 struct btrfs_writepage_fixup *fixup;
Chris Mason247e7432008-07-17 12:53:51 -04002264
Chris Mason8b62b722009-09-02 16:53:46 -04002265 /* this page is properly in the ordered list */
2266 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04002267 return 0;
2268
2269 if (PageChecked(page))
2270 return -EAGAIN;
2271
2272 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
2273 if (!fixup)
2274 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04002275
Chris Mason247e7432008-07-17 12:53:51 -04002276 SetPageChecked(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002277 get_page(page);
Omar Sandovala0cac0e2019-09-16 11:30:57 -07002278 btrfs_init_work(&fixup->work, btrfs_writepage_fixup_worker, NULL, NULL);
Chris Mason247e7432008-07-17 12:53:51 -04002279 fixup->page = page;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002280 btrfs_queue_work(fs_info->fixup_workers, &fixup->work);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002281 return -EBUSY;
Chris Mason247e7432008-07-17 12:53:51 -04002282}
2283
Yan Zhengd899e052008-10-30 14:25:28 -04002284static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
2285 struct inode *inode, u64 file_pos,
2286 u64 disk_bytenr, u64 disk_num_bytes,
2287 u64 num_bytes, u64 ram_bytes,
2288 u8 compression, u8 encryption,
2289 u16 other_encoding, int extent_type)
2290{
2291 struct btrfs_root *root = BTRFS_I(inode)->root;
2292 struct btrfs_file_extent_item *fi;
2293 struct btrfs_path *path;
2294 struct extent_buffer *leaf;
2295 struct btrfs_key ins;
Qu Wenruoa12b8772017-02-27 15:10:37 +08002296 u64 qg_released;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002297 int extent_inserted = 0;
Yan Zhengd899e052008-10-30 14:25:28 -04002298 int ret;
2299
2300 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07002301 if (!path)
2302 return -ENOMEM;
Yan Zhengd899e052008-10-30 14:25:28 -04002303
Chris Masona1ed8352009-09-11 12:27:37 -04002304 /*
2305 * we may be replacing one extent in the tree with another.
2306 * The new extent is pinned in the extent map, and we don't want
2307 * to drop it from the cache until it is completely in the btree.
2308 *
2309 * So, tell btrfs_drop_extents to leave this extent in the cache.
2310 * the caller is expected to unpin it and allow it to be merged
2311 * with the others.
2312 */
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002313 ret = __btrfs_drop_extents(trans, root, inode, path, file_pos,
2314 file_pos + num_bytes, NULL, 0,
2315 1, sizeof(*fi), &extent_inserted);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002316 if (ret)
2317 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04002318
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002319 if (!extent_inserted) {
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002320 ins.objectid = btrfs_ino(BTRFS_I(inode));
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002321 ins.offset = file_pos;
2322 ins.type = BTRFS_EXTENT_DATA_KEY;
2323
2324 path->leave_spinning = 1;
2325 ret = btrfs_insert_empty_item(trans, root, path, &ins,
2326 sizeof(*fi));
2327 if (ret)
2328 goto out;
2329 }
Yan Zhengd899e052008-10-30 14:25:28 -04002330 leaf = path->nodes[0];
2331 fi = btrfs_item_ptr(leaf, path->slots[0],
2332 struct btrfs_file_extent_item);
2333 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
2334 btrfs_set_file_extent_type(leaf, fi, extent_type);
2335 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
2336 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
2337 btrfs_set_file_extent_offset(leaf, fi, 0);
2338 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
2339 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
2340 btrfs_set_file_extent_compression(leaf, fi, compression);
2341 btrfs_set_file_extent_encryption(leaf, fi, encryption);
2342 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
Chris Masonb9473432009-03-13 11:00:37 -04002343
Yan Zhengd899e052008-10-30 14:25:28 -04002344 btrfs_mark_buffer_dirty(leaf);
Josef Bacikce195332012-09-25 15:26:16 -04002345 btrfs_release_path(path);
Yan Zhengd899e052008-10-30 14:25:28 -04002346
2347 inode_add_bytes(inode, num_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -04002348
2349 ins.objectid = disk_bytenr;
2350 ins.offset = disk_num_bytes;
2351 ins.type = BTRFS_EXTENT_ITEM_KEY;
Qu Wenruoa12b8772017-02-27 15:10:37 +08002352
Qu Wenruo297d7502015-09-08 17:08:37 +08002353 /*
Qu Wenruo5846a3c2015-10-26 14:11:18 +08002354 * Release the reserved range from inode dirty range map, as it is
2355 * already moved into delayed_ref_head
Qu Wenruo297d7502015-09-08 17:08:37 +08002356 */
Qu Wenruoa12b8772017-02-27 15:10:37 +08002357 ret = btrfs_qgroup_release_data(inode, file_pos, ram_bytes);
2358 if (ret < 0)
2359 goto out;
2360 qg_released = ret;
Josef Bacik84f7d8e2017-09-29 15:43:49 -04002361 ret = btrfs_alloc_reserved_file_extent(trans, root,
2362 btrfs_ino(BTRFS_I(inode)),
2363 file_pos, qg_released, &ins);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002364out:
Yan Zhengd899e052008-10-30 14:25:28 -04002365 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04002366
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002367 return ret;
Yan Zhengd899e052008-10-30 14:25:28 -04002368}
2369
Liu Bo38c227d2013-01-29 03:18:40 +00002370/* snapshot-aware defrag */
2371struct sa_defrag_extent_backref {
2372 struct rb_node node;
2373 struct old_sa_defrag_extent *old;
2374 u64 root_id;
2375 u64 inum;
2376 u64 file_pos;
2377 u64 extent_offset;
2378 u64 num_bytes;
2379 u64 generation;
2380};
2381
2382struct old_sa_defrag_extent {
2383 struct list_head list;
2384 struct new_sa_defrag_extent *new;
2385
2386 u64 extent_offset;
2387 u64 bytenr;
2388 u64 offset;
2389 u64 len;
2390 int count;
2391};
2392
2393struct new_sa_defrag_extent {
2394 struct rb_root root;
2395 struct list_head head;
2396 struct btrfs_path *path;
2397 struct inode *inode;
2398 u64 file_pos;
2399 u64 len;
2400 u64 bytenr;
2401 u64 disk_len;
2402 u8 compress_type;
2403};
2404
2405static int backref_comp(struct sa_defrag_extent_backref *b1,
2406 struct sa_defrag_extent_backref *b2)
2407{
2408 if (b1->root_id < b2->root_id)
2409 return -1;
2410 else if (b1->root_id > b2->root_id)
2411 return 1;
2412
2413 if (b1->inum < b2->inum)
2414 return -1;
2415 else if (b1->inum > b2->inum)
2416 return 1;
2417
2418 if (b1->file_pos < b2->file_pos)
2419 return -1;
2420 else if (b1->file_pos > b2->file_pos)
2421 return 1;
2422
2423 /*
2424 * [------------------------------] ===> (a range of space)
2425 * |<--->| |<---->| =============> (fs/file tree A)
2426 * |<---------------------------->| ===> (fs/file tree B)
2427 *
2428 * A range of space can refer to two file extents in one tree while
2429 * refer to only one file extent in another tree.
2430 *
2431 * So we may process a disk offset more than one time(two extents in A)
2432 * and locate at the same extent(one extent in B), then insert two same
2433 * backrefs(both refer to the extent in B).
2434 */
2435 return 0;
2436}
2437
2438static void backref_insert(struct rb_root *root,
2439 struct sa_defrag_extent_backref *backref)
2440{
2441 struct rb_node **p = &root->rb_node;
2442 struct rb_node *parent = NULL;
2443 struct sa_defrag_extent_backref *entry;
2444 int ret;
2445
2446 while (*p) {
2447 parent = *p;
2448 entry = rb_entry(parent, struct sa_defrag_extent_backref, node);
2449
2450 ret = backref_comp(backref, entry);
2451 if (ret < 0)
2452 p = &(*p)->rb_left;
2453 else
2454 p = &(*p)->rb_right;
2455 }
2456
2457 rb_link_node(&backref->node, parent, p);
2458 rb_insert_color(&backref->node, root);
2459}
2460
2461/*
2462 * Note the backref might has changed, and in this case we just return 0.
2463 */
2464static noinline int record_one_backref(u64 inum, u64 offset, u64 root_id,
2465 void *ctx)
2466{
2467 struct btrfs_file_extent_item *extent;
Liu Bo38c227d2013-01-29 03:18:40 +00002468 struct old_sa_defrag_extent *old = ctx;
2469 struct new_sa_defrag_extent *new = old->new;
2470 struct btrfs_path *path = new->path;
2471 struct btrfs_key key;
2472 struct btrfs_root *root;
2473 struct sa_defrag_extent_backref *backref;
2474 struct extent_buffer *leaf;
2475 struct inode *inode = new->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002476 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002477 int slot;
2478 int ret;
2479 u64 extent_offset;
2480 u64 num_bytes;
2481
2482 if (BTRFS_I(inode)->root->root_key.objectid == root_id &&
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002483 inum == btrfs_ino(BTRFS_I(inode)))
Liu Bo38c227d2013-01-29 03:18:40 +00002484 return 0;
2485
2486 key.objectid = root_id;
2487 key.type = BTRFS_ROOT_ITEM_KEY;
2488 key.offset = (u64)-1;
2489
Liu Bo38c227d2013-01-29 03:18:40 +00002490 root = btrfs_read_fs_root_no_name(fs_info, &key);
2491 if (IS_ERR(root)) {
2492 if (PTR_ERR(root) == -ENOENT)
2493 return 0;
2494 WARN_ON(1);
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002495 btrfs_debug(fs_info, "inum=%llu, offset=%llu, root_id=%llu",
Liu Bo38c227d2013-01-29 03:18:40 +00002496 inum, offset, root_id);
2497 return PTR_ERR(root);
2498 }
2499
2500 key.objectid = inum;
2501 key.type = BTRFS_EXTENT_DATA_KEY;
2502 if (offset > (u64)-1 << 32)
2503 key.offset = 0;
2504 else
2505 key.offset = offset;
2506
2507 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05302508 if (WARN_ON(ret < 0))
Liu Bo38c227d2013-01-29 03:18:40 +00002509 return ret;
Josef Bacik50f13192013-07-22 12:50:37 -04002510 ret = 0;
Liu Bo38c227d2013-01-29 03:18:40 +00002511
2512 while (1) {
2513 cond_resched();
2514
2515 leaf = path->nodes[0];
2516 slot = path->slots[0];
2517
2518 if (slot >= btrfs_header_nritems(leaf)) {
2519 ret = btrfs_next_leaf(root, path);
2520 if (ret < 0) {
2521 goto out;
2522 } else if (ret > 0) {
2523 ret = 0;
2524 goto out;
2525 }
2526 continue;
2527 }
2528
2529 path->slots[0]++;
2530
2531 btrfs_item_key_to_cpu(leaf, &key, slot);
2532
2533 if (key.objectid > inum)
2534 goto out;
2535
2536 if (key.objectid < inum || key.type != BTRFS_EXTENT_DATA_KEY)
2537 continue;
2538
2539 extent = btrfs_item_ptr(leaf, slot,
2540 struct btrfs_file_extent_item);
2541
2542 if (btrfs_file_extent_disk_bytenr(leaf, extent) != old->bytenr)
2543 continue;
2544
Liu Boe68afa42013-07-01 22:13:26 +08002545 /*
2546 * 'offset' refers to the exact key.offset,
2547 * NOT the 'offset' field in btrfs_extent_data_ref, ie.
2548 * (key.offset - extent_offset).
2549 */
2550 if (key.offset != offset)
Liu Bo38c227d2013-01-29 03:18:40 +00002551 continue;
2552
Liu Boe68afa42013-07-01 22:13:26 +08002553 extent_offset = btrfs_file_extent_offset(leaf, extent);
Liu Bo38c227d2013-01-29 03:18:40 +00002554 num_bytes = btrfs_file_extent_num_bytes(leaf, extent);
Liu Boe68afa42013-07-01 22:13:26 +08002555
Liu Bo38c227d2013-01-29 03:18:40 +00002556 if (extent_offset >= old->extent_offset + old->offset +
2557 old->len || extent_offset + num_bytes <=
2558 old->extent_offset + old->offset)
2559 continue;
Liu Bo38c227d2013-01-29 03:18:40 +00002560 break;
2561 }
2562
2563 backref = kmalloc(sizeof(*backref), GFP_NOFS);
2564 if (!backref) {
2565 ret = -ENOENT;
2566 goto out;
2567 }
2568
2569 backref->root_id = root_id;
2570 backref->inum = inum;
Liu Boe68afa42013-07-01 22:13:26 +08002571 backref->file_pos = offset;
Liu Bo38c227d2013-01-29 03:18:40 +00002572 backref->num_bytes = num_bytes;
2573 backref->extent_offset = extent_offset;
2574 backref->generation = btrfs_file_extent_generation(leaf, extent);
2575 backref->old = old;
2576 backref_insert(&new->root, backref);
2577 old->count++;
2578out:
2579 btrfs_release_path(path);
2580 WARN_ON(ret);
2581 return ret;
2582}
2583
2584static noinline bool record_extent_backrefs(struct btrfs_path *path,
2585 struct new_sa_defrag_extent *new)
2586{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002587 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002588 struct old_sa_defrag_extent *old, *tmp;
2589 int ret;
2590
2591 new->path = path;
2592
2593 list_for_each_entry_safe(old, tmp, &new->head, list) {
Liu Boe68afa42013-07-01 22:13:26 +08002594 ret = iterate_inodes_from_logical(old->bytenr +
2595 old->extent_offset, fs_info,
Liu Bo38c227d2013-01-29 03:18:40 +00002596 path, record_one_backref,
Zygo Blaxellc995ab32017-09-22 13:58:45 -04002597 old, false);
Josef Bacik4724b102013-11-05 11:11:40 -05002598 if (ret < 0 && ret != -ENOENT)
2599 return false;
Liu Bo38c227d2013-01-29 03:18:40 +00002600
2601 /* no backref to be processed for this extent */
2602 if (!old->count) {
2603 list_del(&old->list);
2604 kfree(old);
2605 }
2606 }
2607
2608 if (list_empty(&new->head))
2609 return false;
2610
2611 return true;
2612}
2613
2614static int relink_is_mergable(struct extent_buffer *leaf,
2615 struct btrfs_file_extent_item *fi,
Liu Bo116e0022013-08-02 16:30:40 +08002616 struct new_sa_defrag_extent *new)
Liu Bo38c227d2013-01-29 03:18:40 +00002617{
Liu Bo116e0022013-08-02 16:30:40 +08002618 if (btrfs_file_extent_disk_bytenr(leaf, fi) != new->bytenr)
Liu Bo38c227d2013-01-29 03:18:40 +00002619 return 0;
2620
2621 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG)
2622 return 0;
2623
Liu Bo116e0022013-08-02 16:30:40 +08002624 if (btrfs_file_extent_compression(leaf, fi) != new->compress_type)
2625 return 0;
2626
2627 if (btrfs_file_extent_encryption(leaf, fi) ||
Liu Bo38c227d2013-01-29 03:18:40 +00002628 btrfs_file_extent_other_encoding(leaf, fi))
2629 return 0;
2630
2631 return 1;
2632}
2633
2634/*
2635 * Note the backref might has changed, and in this case we just return 0.
2636 */
2637static noinline int relink_extent_backref(struct btrfs_path *path,
2638 struct sa_defrag_extent_backref *prev,
2639 struct sa_defrag_extent_backref *backref)
2640{
2641 struct btrfs_file_extent_item *extent;
2642 struct btrfs_file_extent_item *item;
2643 struct btrfs_ordered_extent *ordered;
2644 struct btrfs_trans_handle *trans;
Qu Wenruo82fa1132019-04-04 14:45:35 +08002645 struct btrfs_ref ref = { 0 };
Liu Bo38c227d2013-01-29 03:18:40 +00002646 struct btrfs_root *root;
2647 struct btrfs_key key;
2648 struct extent_buffer *leaf;
2649 struct old_sa_defrag_extent *old = backref->old;
2650 struct new_sa_defrag_extent *new = old->new;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002651 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002652 struct inode *inode;
2653 struct extent_state *cached = NULL;
2654 int ret = 0;
2655 u64 start;
2656 u64 len;
2657 u64 lock_start;
2658 u64 lock_end;
2659 bool merge = false;
2660 int index;
2661
2662 if (prev && prev->root_id == backref->root_id &&
2663 prev->inum == backref->inum &&
2664 prev->file_pos + prev->num_bytes == backref->file_pos)
2665 merge = true;
2666
2667 /* step 1: get root */
2668 key.objectid = backref->root_id;
2669 key.type = BTRFS_ROOT_ITEM_KEY;
2670 key.offset = (u64)-1;
2671
Liu Bo38c227d2013-01-29 03:18:40 +00002672 index = srcu_read_lock(&fs_info->subvol_srcu);
2673
2674 root = btrfs_read_fs_root_no_name(fs_info, &key);
2675 if (IS_ERR(root)) {
2676 srcu_read_unlock(&fs_info->subvol_srcu, index);
2677 if (PTR_ERR(root) == -ENOENT)
2678 return 0;
2679 return PTR_ERR(root);
2680 }
Liu Bo38c227d2013-01-29 03:18:40 +00002681
Wang Shilongbcbba5e2014-02-08 23:46:35 +08002682 if (btrfs_root_readonly(root)) {
2683 srcu_read_unlock(&fs_info->subvol_srcu, index);
2684 return 0;
2685 }
2686
Liu Bo38c227d2013-01-29 03:18:40 +00002687 /* step 2: get inode */
2688 key.objectid = backref->inum;
2689 key.type = BTRFS_INODE_ITEM_KEY;
2690 key.offset = 0;
2691
David Sterba4c66e0d2019-10-03 19:09:35 +02002692 inode = btrfs_iget(fs_info->sb, &key, root);
Liu Bo38c227d2013-01-29 03:18:40 +00002693 if (IS_ERR(inode)) {
2694 srcu_read_unlock(&fs_info->subvol_srcu, index);
2695 return 0;
2696 }
2697
2698 srcu_read_unlock(&fs_info->subvol_srcu, index);
2699
2700 /* step 3: relink backref */
2701 lock_start = backref->file_pos;
2702 lock_end = backref->file_pos + backref->num_bytes - 1;
2703 lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
David Sterbaff13db42015-12-03 14:30:40 +01002704 &cached);
Liu Bo38c227d2013-01-29 03:18:40 +00002705
2706 ordered = btrfs_lookup_first_ordered_extent(inode, lock_end);
2707 if (ordered) {
2708 btrfs_put_ordered_extent(ordered);
2709 goto out_unlock;
2710 }
2711
2712 trans = btrfs_join_transaction(root);
2713 if (IS_ERR(trans)) {
2714 ret = PTR_ERR(trans);
2715 goto out_unlock;
2716 }
2717
2718 key.objectid = backref->inum;
2719 key.type = BTRFS_EXTENT_DATA_KEY;
2720 key.offset = backref->file_pos;
2721
2722 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2723 if (ret < 0) {
2724 goto out_free_path;
2725 } else if (ret > 0) {
2726 ret = 0;
2727 goto out_free_path;
2728 }
2729
2730 extent = btrfs_item_ptr(path->nodes[0], path->slots[0],
2731 struct btrfs_file_extent_item);
2732
2733 if (btrfs_file_extent_generation(path->nodes[0], extent) !=
2734 backref->generation)
2735 goto out_free_path;
2736
2737 btrfs_release_path(path);
2738
2739 start = backref->file_pos;
2740 if (backref->extent_offset < old->extent_offset + old->offset)
2741 start += old->extent_offset + old->offset -
2742 backref->extent_offset;
2743
2744 len = min(backref->extent_offset + backref->num_bytes,
2745 old->extent_offset + old->offset + old->len);
2746 len -= max(backref->extent_offset, old->extent_offset + old->offset);
2747
2748 ret = btrfs_drop_extents(trans, root, inode, start,
2749 start + len, 1);
2750 if (ret)
2751 goto out_free_path;
2752again:
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002753 key.objectid = btrfs_ino(BTRFS_I(inode));
Liu Bo38c227d2013-01-29 03:18:40 +00002754 key.type = BTRFS_EXTENT_DATA_KEY;
2755 key.offset = start;
2756
Liu Boa09a0a72013-03-11 09:20:58 +00002757 path->leave_spinning = 1;
Liu Bo38c227d2013-01-29 03:18:40 +00002758 if (merge) {
2759 struct btrfs_file_extent_item *fi;
2760 u64 extent_len;
2761 struct btrfs_key found_key;
2762
Gui Hecheng3c9665d2014-01-23 13:41:09 +08002763 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
Liu Bo38c227d2013-01-29 03:18:40 +00002764 if (ret < 0)
2765 goto out_free_path;
2766
2767 path->slots[0]--;
2768 leaf = path->nodes[0];
2769 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2770
2771 fi = btrfs_item_ptr(leaf, path->slots[0],
2772 struct btrfs_file_extent_item);
2773 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
2774
Liu Bo116e0022013-08-02 16:30:40 +08002775 if (extent_len + found_key.offset == start &&
2776 relink_is_mergable(leaf, fi, new)) {
Liu Bo38c227d2013-01-29 03:18:40 +00002777 btrfs_set_file_extent_num_bytes(leaf, fi,
2778 extent_len + len);
2779 btrfs_mark_buffer_dirty(leaf);
2780 inode_add_bytes(inode, len);
2781
2782 ret = 1;
2783 goto out_free_path;
2784 } else {
2785 merge = false;
2786 btrfs_release_path(path);
2787 goto again;
2788 }
2789 }
2790
2791 ret = btrfs_insert_empty_item(trans, root, path, &key,
2792 sizeof(*extent));
2793 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002794 btrfs_abort_transaction(trans, ret);
Liu Bo38c227d2013-01-29 03:18:40 +00002795 goto out_free_path;
2796 }
2797
2798 leaf = path->nodes[0];
2799 item = btrfs_item_ptr(leaf, path->slots[0],
2800 struct btrfs_file_extent_item);
2801 btrfs_set_file_extent_disk_bytenr(leaf, item, new->bytenr);
2802 btrfs_set_file_extent_disk_num_bytes(leaf, item, new->disk_len);
2803 btrfs_set_file_extent_offset(leaf, item, start - new->file_pos);
2804 btrfs_set_file_extent_num_bytes(leaf, item, len);
2805 btrfs_set_file_extent_ram_bytes(leaf, item, new->len);
2806 btrfs_set_file_extent_generation(leaf, item, trans->transid);
2807 btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG);
2808 btrfs_set_file_extent_compression(leaf, item, new->compress_type);
2809 btrfs_set_file_extent_encryption(leaf, item, 0);
2810 btrfs_set_file_extent_other_encoding(leaf, item, 0);
2811
2812 btrfs_mark_buffer_dirty(leaf);
2813 inode_add_bytes(inode, len);
Liu Boa09a0a72013-03-11 09:20:58 +00002814 btrfs_release_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002815
Qu Wenruo82fa1132019-04-04 14:45:35 +08002816 btrfs_init_generic_ref(&ref, BTRFS_ADD_DELAYED_REF, new->bytenr,
2817 new->disk_len, 0);
2818 btrfs_init_data_ref(&ref, backref->root_id, backref->inum,
2819 new->file_pos); /* start - extent_offset */
2820 ret = btrfs_inc_extent_ref(trans, &ref);
Liu Bo38c227d2013-01-29 03:18:40 +00002821 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002822 btrfs_abort_transaction(trans, ret);
Liu Bo38c227d2013-01-29 03:18:40 +00002823 goto out_free_path;
2824 }
2825
2826 ret = 1;
2827out_free_path:
2828 btrfs_release_path(path);
Liu Boa09a0a72013-03-11 09:20:58 +00002829 path->leave_spinning = 0;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002830 btrfs_end_transaction(trans);
Liu Bo38c227d2013-01-29 03:18:40 +00002831out_unlock:
2832 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
David Sterbae43bbe52017-12-12 21:43:52 +01002833 &cached);
Liu Bo38c227d2013-01-29 03:18:40 +00002834 iput(inode);
2835 return ret;
2836}
2837
Liu Bo6f519562013-10-29 10:45:05 +08002838static void free_sa_defrag_extent(struct new_sa_defrag_extent *new)
2839{
2840 struct old_sa_defrag_extent *old, *tmp;
2841
2842 if (!new)
2843 return;
2844
2845 list_for_each_entry_safe(old, tmp, &new->head, list) {
Liu Bo6f519562013-10-29 10:45:05 +08002846 kfree(old);
2847 }
2848 kfree(new);
2849}
2850
Liu Bo38c227d2013-01-29 03:18:40 +00002851static void relink_file_extents(struct new_sa_defrag_extent *new)
2852{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002853 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002854 struct btrfs_path *path;
Liu Bo38c227d2013-01-29 03:18:40 +00002855 struct sa_defrag_extent_backref *backref;
2856 struct sa_defrag_extent_backref *prev = NULL;
Liu Bo38c227d2013-01-29 03:18:40 +00002857 struct rb_node *node;
2858 int ret;
2859
Liu Bo38c227d2013-01-29 03:18:40 +00002860 path = btrfs_alloc_path();
2861 if (!path)
2862 return;
2863
2864 if (!record_extent_backrefs(path, new)) {
2865 btrfs_free_path(path);
2866 goto out;
2867 }
2868 btrfs_release_path(path);
2869
2870 while (1) {
2871 node = rb_first(&new->root);
2872 if (!node)
2873 break;
2874 rb_erase(node, &new->root);
2875
2876 backref = rb_entry(node, struct sa_defrag_extent_backref, node);
2877
2878 ret = relink_extent_backref(path, prev, backref);
2879 WARN_ON(ret < 0);
2880
2881 kfree(prev);
2882
2883 if (ret == 1)
2884 prev = backref;
2885 else
2886 prev = NULL;
2887 cond_resched();
2888 }
2889 kfree(prev);
2890
2891 btrfs_free_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002892out:
Liu Bo6f519562013-10-29 10:45:05 +08002893 free_sa_defrag_extent(new);
2894
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002895 atomic_dec(&fs_info->defrag_running);
2896 wake_up(&fs_info->transaction_wait);
Liu Bo38c227d2013-01-29 03:18:40 +00002897}
2898
2899static struct new_sa_defrag_extent *
2900record_old_file_extents(struct inode *inode,
2901 struct btrfs_ordered_extent *ordered)
2902{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002903 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002904 struct btrfs_root *root = BTRFS_I(inode)->root;
2905 struct btrfs_path *path;
2906 struct btrfs_key key;
Liu Bo6f519562013-10-29 10:45:05 +08002907 struct old_sa_defrag_extent *old;
Liu Bo38c227d2013-01-29 03:18:40 +00002908 struct new_sa_defrag_extent *new;
2909 int ret;
2910
2911 new = kmalloc(sizeof(*new), GFP_NOFS);
2912 if (!new)
2913 return NULL;
2914
2915 new->inode = inode;
2916 new->file_pos = ordered->file_offset;
2917 new->len = ordered->len;
2918 new->bytenr = ordered->start;
2919 new->disk_len = ordered->disk_len;
2920 new->compress_type = ordered->compress_type;
2921 new->root = RB_ROOT;
2922 INIT_LIST_HEAD(&new->head);
2923
2924 path = btrfs_alloc_path();
2925 if (!path)
2926 goto out_kfree;
2927
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002928 key.objectid = btrfs_ino(BTRFS_I(inode));
Liu Bo38c227d2013-01-29 03:18:40 +00002929 key.type = BTRFS_EXTENT_DATA_KEY;
2930 key.offset = new->file_pos;
2931
2932 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2933 if (ret < 0)
2934 goto out_free_path;
2935 if (ret > 0 && path->slots[0] > 0)
2936 path->slots[0]--;
2937
2938 /* find out all the old extents for the file range */
2939 while (1) {
2940 struct btrfs_file_extent_item *extent;
2941 struct extent_buffer *l;
2942 int slot;
2943 u64 num_bytes;
2944 u64 offset;
2945 u64 end;
2946 u64 disk_bytenr;
2947 u64 extent_offset;
2948
2949 l = path->nodes[0];
2950 slot = path->slots[0];
2951
2952 if (slot >= btrfs_header_nritems(l)) {
2953 ret = btrfs_next_leaf(root, path);
2954 if (ret < 0)
Liu Bo6f519562013-10-29 10:45:05 +08002955 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002956 else if (ret > 0)
2957 break;
2958 continue;
2959 }
2960
2961 btrfs_item_key_to_cpu(l, &key, slot);
2962
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002963 if (key.objectid != btrfs_ino(BTRFS_I(inode)))
Liu Bo38c227d2013-01-29 03:18:40 +00002964 break;
2965 if (key.type != BTRFS_EXTENT_DATA_KEY)
2966 break;
2967 if (key.offset >= new->file_pos + new->len)
2968 break;
2969
2970 extent = btrfs_item_ptr(l, slot, struct btrfs_file_extent_item);
2971
2972 num_bytes = btrfs_file_extent_num_bytes(l, extent);
2973 if (key.offset + num_bytes < new->file_pos)
2974 goto next;
2975
2976 disk_bytenr = btrfs_file_extent_disk_bytenr(l, extent);
2977 if (!disk_bytenr)
2978 goto next;
2979
2980 extent_offset = btrfs_file_extent_offset(l, extent);
2981
2982 old = kmalloc(sizeof(*old), GFP_NOFS);
2983 if (!old)
Liu Bo6f519562013-10-29 10:45:05 +08002984 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002985
2986 offset = max(new->file_pos, key.offset);
2987 end = min(new->file_pos + new->len, key.offset + num_bytes);
2988
2989 old->bytenr = disk_bytenr;
2990 old->extent_offset = extent_offset;
2991 old->offset = offset - key.offset;
2992 old->len = end - offset;
2993 old->new = new;
2994 old->count = 0;
2995 list_add_tail(&old->list, &new->head);
2996next:
2997 path->slots[0]++;
2998 cond_resched();
2999 }
3000
3001 btrfs_free_path(path);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003002 atomic_inc(&fs_info->defrag_running);
Liu Bo38c227d2013-01-29 03:18:40 +00003003
3004 return new;
3005
Liu Bo38c227d2013-01-29 03:18:40 +00003006out_free_path:
3007 btrfs_free_path(path);
3008out_kfree:
Liu Bo6f519562013-10-29 10:45:05 +08003009 free_sa_defrag_extent(new);
Liu Bo38c227d2013-01-29 03:18:40 +00003010 return NULL;
3011}
3012
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003013static void btrfs_release_delalloc_bytes(struct btrfs_fs_info *fs_info,
Miao Xiee570fd22014-06-19 10:42:50 +08003014 u64 start, u64 len)
3015{
3016 struct btrfs_block_group_cache *cache;
3017
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003018 cache = btrfs_lookup_block_group(fs_info, start);
Miao Xiee570fd22014-06-19 10:42:50 +08003019 ASSERT(cache);
3020
3021 spin_lock(&cache->lock);
3022 cache->delalloc_bytes -= len;
3023 spin_unlock(&cache->lock);
3024
3025 btrfs_put_block_group(cache);
3026}
3027
Chris Masond352ac62008-09-29 15:18:18 -04003028/* as ordered data IO finishes, this gets called so we can finish
3029 * an ordered extent if the range of bytes in the file it covers are
3030 * fully written.
3031 */
Josef Bacik5fd02042012-05-02 14:00:54 -04003032static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
Chris Masone6dcd2d2008-07-17 12:53:50 -04003033{
Josef Bacik5fd02042012-05-02 14:00:54 -04003034 struct inode *inode = ordered_extent->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003035 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003036 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003037 struct btrfs_trans_handle *trans = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003038 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00003039 struct extent_state *cached_state = NULL;
Liu Bo38c227d2013-01-29 03:18:40 +00003040 struct new_sa_defrag_extent *new = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08003041 int compress_type = 0;
Josef Bacik77cef2e2013-08-29 13:57:21 -04003042 int ret = 0;
3043 u64 logical_len = ordered_extent->len;
Li Zefan82d59022011-04-20 10:33:24 +08003044 bool nolock;
Josef Bacik77cef2e2013-08-29 13:57:21 -04003045 bool truncated = false;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003046 bool range_locked = false;
3047 bool clear_new_delalloc_bytes = false;
Josef Bacik49940bd2018-10-11 15:54:21 -04003048 bool clear_reserved_extent = true;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003049
3050 if (!test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
3051 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags) &&
3052 !test_bit(BTRFS_ORDERED_DIRECT, &ordered_extent->flags))
3053 clear_new_delalloc_bytes = true;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003054
Nikolay Borisov70ddc552017-02-20 13:50:35 +02003055 nolock = btrfs_is_free_space_inode(BTRFS_I(inode));
Josef Bacik0cb59c92010-07-02 12:14:14 -04003056
Josef Bacik5fd02042012-05-02 14:00:54 -04003057 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
3058 ret = -EIO;
3059 goto out;
3060 }
3061
Nikolay Borisov7ab79562017-02-20 13:50:57 +02003062 btrfs_free_io_failure_record(BTRFS_I(inode),
3063 ordered_extent->file_offset,
3064 ordered_extent->file_offset +
3065 ordered_extent->len - 1);
Miao Xief6124962014-09-12 18:44:04 +08003066
Josef Bacik77cef2e2013-08-29 13:57:21 -04003067 if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) {
3068 truncated = true;
3069 logical_len = ordered_extent->truncated_len;
3070 /* Truncated the entire extent, don't bother adding */
3071 if (!logical_len)
3072 goto out;
3073 }
3074
Yan, Zhengc2167752009-11-12 09:34:21 +00003075 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003076 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
Qu Wenruo94ed9382015-09-08 17:25:56 +08003077
3078 /*
3079 * For mwrite(mmap + memset to write) case, we still reserve
3080 * space for NOCOW range.
3081 * As NOCOW won't cause a new delayed ref, just free the space
3082 */
Qu Wenruobc42bda2017-02-27 15:10:39 +08003083 btrfs_qgroup_free_data(inode, NULL, ordered_extent->file_offset,
Qu Wenruo94ed9382015-09-08 17:25:56 +08003084 ordered_extent->len);
Josef Bacik6c760c02012-11-09 10:53:21 -05003085 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
3086 if (nolock)
3087 trans = btrfs_join_transaction_nolock(root);
3088 else
3089 trans = btrfs_join_transaction(root);
3090 if (IS_ERR(trans)) {
3091 ret = PTR_ERR(trans);
3092 trans = NULL;
3093 goto out;
Yan, Zhengc2167752009-11-12 09:34:21 +00003094 }
Josef Bacik69fe2d72017-10-19 14:15:57 -04003095 trans->block_rsv = &BTRFS_I(inode)->block_rsv;
Josef Bacik6c760c02012-11-09 10:53:21 -05003096 ret = btrfs_update_inode_fallback(trans, root, inode);
3097 if (ret) /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04003098 btrfs_abort_transaction(trans, ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00003099 goto out;
3100 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04003101
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003102 range_locked = true;
Josef Bacik2ac55d42010-02-03 19:33:23 +00003103 lock_extent_bits(io_tree, ordered_extent->file_offset,
3104 ordered_extent->file_offset + ordered_extent->len - 1,
David Sterbaff13db42015-12-03 14:30:40 +01003105 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003106
Liu Bo38c227d2013-01-29 03:18:40 +00003107 ret = test_range_bit(io_tree, ordered_extent->file_offset,
3108 ordered_extent->file_offset + ordered_extent->len - 1,
Liu Bo452e62b2017-05-26 17:44:23 -06003109 EXTENT_DEFRAG, 0, cached_state);
Liu Bo38c227d2013-01-29 03:18:40 +00003110 if (ret) {
3111 u64 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
Josef Bacik8101c8d2014-01-29 16:05:30 -05003112 if (0 && last_snapshot >= BTRFS_I(inode)->generation)
Liu Bo38c227d2013-01-29 03:18:40 +00003113 /* the inode is shared */
3114 new = record_old_file_extents(inode, ordered_extent);
3115
3116 clear_extent_bit(io_tree, ordered_extent->file_offset,
3117 ordered_extent->file_offset + ordered_extent->len - 1,
David Sterbaae0f1622017-10-31 16:37:52 +01003118 EXTENT_DEFRAG, 0, 0, &cached_state);
Liu Bo38c227d2013-01-29 03:18:40 +00003119 }
3120
Josef Bacik0cb59c92010-07-02 12:14:14 -04003121 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003122 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04003123 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003124 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003125 if (IS_ERR(trans)) {
3126 ret = PTR_ERR(trans);
3127 trans = NULL;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003128 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003129 }
Chris Masona79b7d42014-05-22 16:18:52 -07003130
Josef Bacik69fe2d72017-10-19 14:15:57 -04003131 trans->block_rsv = &BTRFS_I(inode)->block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00003132
Chris Masonc8b97812008-10-29 14:49:59 -04003133 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08003134 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04003135 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08003136 BUG_ON(compress_type);
Justin Maggardb430b772017-10-30 15:29:10 -07003137 btrfs_qgroup_free_data(inode, NULL, ordered_extent->file_offset,
3138 ordered_extent->len);
Nikolay Borisov7a6d7062017-02-20 13:50:48 +02003139 ret = btrfs_mark_extent_written(trans, BTRFS_I(inode),
Yan Zhengd899e052008-10-30 14:25:28 -04003140 ordered_extent->file_offset,
3141 ordered_extent->file_offset +
Josef Bacik77cef2e2013-08-29 13:57:21 -04003142 logical_len);
Yan Zhengd899e052008-10-30 14:25:28 -04003143 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003144 BUG_ON(root == fs_info->tree_root);
Yan Zhengd899e052008-10-30 14:25:28 -04003145 ret = insert_reserved_file_extent(trans, inode,
3146 ordered_extent->file_offset,
3147 ordered_extent->start,
3148 ordered_extent->disk_len,
Josef Bacik77cef2e2013-08-29 13:57:21 -04003149 logical_len, logical_len,
Li Zefan261507a02010-12-17 14:21:50 +08003150 compress_type, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04003151 BTRFS_FILE_EXTENT_REG);
Josef Bacik49940bd2018-10-11 15:54:21 -04003152 if (!ret) {
3153 clear_reserved_extent = false;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003154 btrfs_release_delalloc_bytes(fs_info,
Miao Xiee570fd22014-06-19 10:42:50 +08003155 ordered_extent->start,
3156 ordered_extent->disk_len);
Josef Bacik49940bd2018-10-11 15:54:21 -04003157 }
Yan Zhengd899e052008-10-30 14:25:28 -04003158 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04003159 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
3160 ordered_extent->file_offset, ordered_extent->len,
3161 trans->transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003162 if (ret < 0) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003163 btrfs_abort_transaction(trans, ret);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003164 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003165 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00003166
Nikolay Borisovac01f262018-01-08 10:59:43 +02003167 ret = add_pending_csums(trans, inode, &ordered_extent->list);
3168 if (ret) {
3169 btrfs_abort_transaction(trans, ret);
3170 goto out;
3171 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04003172
Josef Bacik6c760c02012-11-09 10:53:21 -05003173 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
3174 ret = btrfs_update_inode_fallback(trans, root, inode);
3175 if (ret) { /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04003176 btrfs_abort_transaction(trans, ret);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003177 goto out;
Josef Bacik1ef30be2011-04-05 19:25:36 -04003178 }
3179 ret = 0;
Yan, Zhengc2167752009-11-12 09:34:21 +00003180out:
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003181 if (range_locked || clear_new_delalloc_bytes) {
3182 unsigned int clear_bits = 0;
3183
3184 if (range_locked)
3185 clear_bits |= EXTENT_LOCKED;
3186 if (clear_new_delalloc_bytes)
3187 clear_bits |= EXTENT_DELALLOC_NEW;
3188 clear_extent_bit(&BTRFS_I(inode)->io_tree,
3189 ordered_extent->file_offset,
3190 ordered_extent->file_offset +
3191 ordered_extent->len - 1,
3192 clear_bits,
3193 (clear_bits & EXTENT_LOCKED) ? 1 : 0,
David Sterbaae0f1622017-10-31 16:37:52 +01003194 0, &cached_state);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003195 }
3196
Miao Xiea698d0752012-09-20 01:51:59 -06003197 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003198 btrfs_end_transaction(trans);
Josef Bacik0cb59c92010-07-02 12:14:14 -04003199
Josef Bacik77cef2e2013-08-29 13:57:21 -04003200 if (ret || truncated) {
3201 u64 start, end;
3202
3203 if (truncated)
3204 start = ordered_extent->file_offset + logical_len;
3205 else
3206 start = ordered_extent->file_offset;
3207 end = ordered_extent->file_offset + ordered_extent->len - 1;
David Sterbaf08dc362017-10-31 17:02:39 +01003208 clear_extent_uptodate(io_tree, start, end, NULL);
Josef Bacik77cef2e2013-08-29 13:57:21 -04003209
3210 /* Drop the cache for the part of the extent we didn't write. */
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02003211 btrfs_drop_extent_cache(BTRFS_I(inode), start, end, 0);
Josef Bacik5fd02042012-05-02 14:00:54 -04003212
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003213 /*
3214 * If the ordered extent had an IOERR or something else went
3215 * wrong we need to return the space for this ordered extent
Josef Bacik77cef2e2013-08-29 13:57:21 -04003216 * back to the allocator. We only free the extent in the
3217 * truncated case if we didn't write out the extent at all.
Josef Bacik49940bd2018-10-11 15:54:21 -04003218 *
3219 * If we made it past insert_reserved_file_extent before we
3220 * errored out then we don't need to do this as the accounting
3221 * has already been done.
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003222 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04003223 if ((ret || !logical_len) &&
Josef Bacik49940bd2018-10-11 15:54:21 -04003224 clear_reserved_extent &&
Josef Bacik77cef2e2013-08-29 13:57:21 -04003225 !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003226 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003227 btrfs_free_reserved_extent(fs_info,
3228 ordered_extent->start,
Miao Xiee570fd22014-06-19 10:42:50 +08003229 ordered_extent->disk_len, 1);
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003230 }
3231
3232
Josef Bacik5fd02042012-05-02 14:00:54 -04003233 /*
Liu Bo8bad3c02012-06-18 12:14:23 +08003234 * This needs to be done to make sure anybody waiting knows we are done
3235 * updating everything for this ordered extent.
Josef Bacik5fd02042012-05-02 14:00:54 -04003236 */
3237 btrfs_remove_ordered_extent(inode, ordered_extent);
3238
Liu Bo38c227d2013-01-29 03:18:40 +00003239 /* for snapshot-aware defrag */
Liu Bo6f519562013-10-29 10:45:05 +08003240 if (new) {
3241 if (ret) {
3242 free_sa_defrag_extent(new);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003243 atomic_dec(&fs_info->defrag_running);
Liu Bo6f519562013-10-29 10:45:05 +08003244 } else {
3245 relink_file_extents(new);
3246 }
3247 }
Liu Bo38c227d2013-01-29 03:18:40 +00003248
Chris Masone6dcd2d2008-07-17 12:53:50 -04003249 /* once for us */
3250 btrfs_put_ordered_extent(ordered_extent);
3251 /* once for the tree */
3252 btrfs_put_ordered_extent(ordered_extent);
3253
Josef Bacik5fd02042012-05-02 14:00:54 -04003254 return ret;
3255}
3256
3257static void finish_ordered_fn(struct btrfs_work *work)
3258{
3259 struct btrfs_ordered_extent *ordered_extent;
3260 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
3261 btrfs_finish_ordered_io(ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003262}
3263
Nikolay Borisovc6297322018-11-08 10:18:08 +02003264void btrfs_writepage_endio_finish_ordered(struct page *page, u64 start,
3265 u64 end, int uptodate)
Chris Mason211f90e2008-07-18 11:56:15 -04003266{
Josef Bacik5fd02042012-05-02 14:00:54 -04003267 struct inode *inode = page->mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003268 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik5fd02042012-05-02 14:00:54 -04003269 struct btrfs_ordered_extent *ordered_extent = NULL;
Liu Bo9e0af232014-08-15 23:36:53 +08003270 struct btrfs_workqueue *wq;
Josef Bacik5fd02042012-05-02 14:00:54 -04003271
liubo1abe9b82011-03-24 11:18:59 +00003272 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
3273
Chris Mason8b62b722009-09-02 16:53:46 -04003274 ClearPagePrivate2(page);
Josef Bacik5fd02042012-05-02 14:00:54 -04003275 if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
3276 end - start + 1, uptodate))
David Sterbac3988d62017-02-17 15:18:32 +01003277 return;
Josef Bacik5fd02042012-05-02 14:00:54 -04003278
Omar Sandovala0cac0e2019-09-16 11:30:57 -07003279 if (btrfs_is_free_space_inode(BTRFS_I(inode)))
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003280 wq = fs_info->endio_freespace_worker;
Omar Sandovala0cac0e2019-09-16 11:30:57 -07003281 else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003282 wq = fs_info->endio_write_workers;
Josef Bacik5fd02042012-05-02 14:00:54 -04003283
Omar Sandovala0cac0e2019-09-16 11:30:57 -07003284 btrfs_init_work(&ordered_extent->work, finish_ordered_fn, NULL, NULL);
Liu Bo9e0af232014-08-15 23:36:53 +08003285 btrfs_queue_work(wq, &ordered_extent->work);
Chris Mason211f90e2008-07-18 11:56:15 -04003286}
3287
Miao Xiedc380ae2014-09-12 18:43:55 +08003288static int __readpage_endio_check(struct inode *inode,
3289 struct btrfs_io_bio *io_bio,
3290 int icsum, struct page *page,
3291 int pgoff, u64 start, size_t len)
3292{
Johannes Thumshirnd5178572019-06-03 16:58:57 +02003293 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
3294 SHASH_DESC_ON_STACK(shash, fs_info->csum_shash);
Miao Xiedc380ae2014-09-12 18:43:55 +08003295 char *kaddr;
Johannes Thumshirnd5178572019-06-03 16:58:57 +02003296 u16 csum_size = btrfs_super_csum_size(fs_info->super_copy);
3297 u8 *csum_expected;
3298 u8 csum[BTRFS_CSUM_SIZE];
Miao Xiedc380ae2014-09-12 18:43:55 +08003299
Johannes Thumshirnd5178572019-06-03 16:58:57 +02003300 csum_expected = ((u8 *)io_bio->csum) + icsum * csum_size;
Miao Xiedc380ae2014-09-12 18:43:55 +08003301
3302 kaddr = kmap_atomic(page);
Johannes Thumshirnd5178572019-06-03 16:58:57 +02003303 shash->tfm = fs_info->csum_shash;
3304
3305 crypto_shash_init(shash);
3306 crypto_shash_update(shash, kaddr + pgoff, len);
3307 crypto_shash_final(shash, csum);
3308
3309 if (memcmp(csum, csum_expected, csum_size))
Miao Xiedc380ae2014-09-12 18:43:55 +08003310 goto zeroit;
3311
3312 kunmap_atomic(kaddr);
3313 return 0;
3314zeroit:
Johannes Thumshirnea41d6b2019-06-03 16:58:58 +02003315 btrfs_print_data_csum_error(BTRFS_I(inode), start, csum, csum_expected,
3316 io_bio->mirror_num);
Miao Xiedc380ae2014-09-12 18:43:55 +08003317 memset(kaddr + pgoff, 1, len);
3318 flush_dcache_page(page);
3319 kunmap_atomic(kaddr);
Miao Xiedc380ae2014-09-12 18:43:55 +08003320 return -EIO;
3321}
3322
Chris Masond352ac62008-09-29 15:18:18 -04003323/*
Chris Masond352ac62008-09-29 15:18:18 -04003324 * when reads are done, we need to check csums to verify the data is correct
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02003325 * if there's a match, we allow the bio to finish. If not, the code in
3326 * extent_io.c will try to find good copies for us.
Chris Masond352ac62008-09-29 15:18:18 -04003327 */
Miao Xiefacc8a222013-07-25 19:22:34 +08003328static int btrfs_readpage_end_io_hook(struct btrfs_io_bio *io_bio,
3329 u64 phy_offset, struct page *page,
3330 u64 start, u64 end, int mirror)
Chris Mason07157aa2007-08-30 08:50:51 -04003331{
Miao Xie4eee4fa2012-12-21 09:17:45 +00003332 size_t offset = start - page_offset(page);
Chris Mason07157aa2007-08-30 08:50:51 -04003333 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05003334 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masonff79f812007-10-15 16:22:25 -04003335 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -05003336
Chris Masond20f7042008-12-08 16:58:54 -05003337 if (PageChecked(page)) {
3338 ClearPageChecked(page);
Miao Xiedc380ae2014-09-12 18:43:55 +08003339 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003340 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003341
3342 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Miao Xiedc380ae2014-09-12 18:43:55 +08003343 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003344
Yan Zheng17d217f2008-12-12 10:03:38 -05003345 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04003346 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
David Sterba91166212016-04-26 23:54:39 +02003347 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM);
Yan Zheng17d217f2008-12-12 10:03:38 -05003348 return 0;
3349 }
3350
Miao Xiefacc8a222013-07-25 19:22:34 +08003351 phy_offset >>= inode->i_sb->s_blocksize_bits;
Miao Xiedc380ae2014-09-12 18:43:55 +08003352 return __readpage_endio_check(inode, io_bio, phy_offset, page, offset,
3353 start, (size_t)(end - start + 1));
Chris Mason07157aa2007-08-30 08:50:51 -04003354}
Chris Masonb888db22007-08-27 16:49:44 -04003355
Nikolay Borisovc1c3fac2018-01-16 09:31:58 +02003356/*
3357 * btrfs_add_delayed_iput - perform a delayed iput on @inode
3358 *
3359 * @inode: The inode we want to perform iput on
3360 *
3361 * This function uses the generic vfs_inode::i_count to track whether we should
3362 * just decrement it (in case it's > 1) or if this is the last iput then link
3363 * the inode to the delayed iput machinery. Delayed iputs are processed at
3364 * transaction commit time/superblock commit/cleaner kthread.
3365 */
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003366void btrfs_add_delayed_iput(struct inode *inode)
3367{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003368 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
David Sterba8089fe62015-11-19 14:15:51 +01003369 struct btrfs_inode *binode = BTRFS_I(inode);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003370
3371 if (atomic_add_unless(&inode->i_count, -1, 1))
3372 return;
3373
Josef Bacik034f7842018-12-03 11:06:52 -05003374 atomic_inc(&fs_info->nr_delayed_iputs);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003375 spin_lock(&fs_info->delayed_iput_lock);
Nikolay Borisovc1c3fac2018-01-16 09:31:58 +02003376 ASSERT(list_empty(&binode->delayed_iput));
3377 list_add_tail(&binode->delayed_iput, &fs_info->delayed_iputs);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003378 spin_unlock(&fs_info->delayed_iput_lock);
Josef Bacikfd340d02019-01-11 10:21:02 -05003379 if (!test_bit(BTRFS_FS_CLEANER_RUNNING, &fs_info->flags))
3380 wake_up_process(fs_info->cleaner_kthread);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003381}
3382
Josef Bacik63611e72019-06-18 10:59:18 -04003383static void run_delayed_iput_locked(struct btrfs_fs_info *fs_info,
3384 struct btrfs_inode *inode)
3385{
3386 list_del_init(&inode->delayed_iput);
3387 spin_unlock(&fs_info->delayed_iput_lock);
3388 iput(&inode->vfs_inode);
3389 if (atomic_dec_and_test(&fs_info->nr_delayed_iputs))
3390 wake_up(&fs_info->delayed_iputs_wait);
3391 spin_lock(&fs_info->delayed_iput_lock);
3392}
3393
3394static void btrfs_run_delayed_iput(struct btrfs_fs_info *fs_info,
3395 struct btrfs_inode *inode)
3396{
3397 if (!list_empty(&inode->delayed_iput)) {
3398 spin_lock(&fs_info->delayed_iput_lock);
3399 if (!list_empty(&inode->delayed_iput))
3400 run_delayed_iput_locked(fs_info, inode);
3401 spin_unlock(&fs_info->delayed_iput_lock);
3402 }
3403}
3404
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003405void btrfs_run_delayed_iputs(struct btrfs_fs_info *fs_info)
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003406{
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003407
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003408 spin_lock(&fs_info->delayed_iput_lock);
David Sterba8089fe62015-11-19 14:15:51 +01003409 while (!list_empty(&fs_info->delayed_iputs)) {
3410 struct btrfs_inode *inode;
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003411
David Sterba8089fe62015-11-19 14:15:51 +01003412 inode = list_first_entry(&fs_info->delayed_iputs,
3413 struct btrfs_inode, delayed_iput);
Josef Bacik63611e72019-06-18 10:59:18 -04003414 run_delayed_iput_locked(fs_info, inode);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003415 }
David Sterba8089fe62015-11-19 14:15:51 +01003416 spin_unlock(&fs_info->delayed_iput_lock);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003417}
3418
Josef Bacik034f7842018-12-03 11:06:52 -05003419/**
3420 * btrfs_wait_on_delayed_iputs - wait on the delayed iputs to be done running
3421 * @fs_info - the fs_info for this fs
3422 * @return - EINTR if we were killed, 0 if nothing's pending
3423 *
3424 * This will wait on any delayed iputs that are currently running with KILLABLE
3425 * set. Once they are all done running we will return, unless we are killed in
3426 * which case we return EINTR. This helps in user operations like fallocate etc
3427 * that might get blocked on the iputs.
3428 */
3429int btrfs_wait_on_delayed_iputs(struct btrfs_fs_info *fs_info)
3430{
3431 int ret = wait_event_killable(fs_info->delayed_iputs_wait,
3432 atomic_read(&fs_info->nr_delayed_iputs) == 0);
3433 if (ret)
3434 return -EINTR;
3435 return 0;
3436}
3437
Yan, Zhengd68fc572010-05-16 10:49:58 -04003438/*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003439 * This creates an orphan entry for the given inode in case something goes wrong
3440 * in the middle of an unlink.
Josef Bacik7b128762008-07-24 12:17:14 -04003441 */
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003442int btrfs_orphan_add(struct btrfs_trans_handle *trans,
Omar Sandoval27919062018-05-11 13:13:37 -07003443 struct btrfs_inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003444{
Yan, Zhengd68fc572010-05-16 10:49:58 -04003445 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003446
Omar Sandoval27919062018-05-11 13:13:37 -07003447 ret = btrfs_insert_orphan_item(trans, inode->root, btrfs_ino(inode));
3448 if (ret && ret != -EEXIST) {
3449 btrfs_abort_transaction(trans, ret);
3450 return ret;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003451 }
3452
Yan, Zhengd68fc572010-05-16 10:49:58 -04003453 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003454}
3455
3456/*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003457 * We have done the delete so we can go ahead and remove the orphan item for
3458 * this particular inode.
Josef Bacik7b128762008-07-24 12:17:14 -04003459 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00003460static int btrfs_orphan_del(struct btrfs_trans_handle *trans,
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003461 struct btrfs_inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003462{
Omar Sandoval27919062018-05-11 13:13:37 -07003463 return btrfs_del_orphan_item(trans, inode->root, btrfs_ino(inode));
Josef Bacik7b128762008-07-24 12:17:14 -04003464}
3465
3466/*
3467 * this cleans up any orphans that may be left on the list from the last use
3468 * of this root.
3469 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003470int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04003471{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003472 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik7b128762008-07-24 12:17:14 -04003473 struct btrfs_path *path;
3474 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04003475 struct btrfs_key key, found_key;
3476 struct btrfs_trans_handle *trans;
3477 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003478 u64 last_objectid = 0;
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003479 int ret = 0, nr_unlink = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003480
Yan, Zhengd68fc572010-05-16 10:49:58 -04003481 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003482 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00003483
3484 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003485 if (!path) {
3486 ret = -ENOMEM;
3487 goto out;
3488 }
David Sterbae4058b52015-11-27 16:31:35 +01003489 path->reada = READA_BACK;
Josef Bacik7b128762008-07-24 12:17:14 -04003490
3491 key.objectid = BTRFS_ORPHAN_OBJECTID;
David Sterba962a2982014-06-04 18:41:45 +02003492 key.type = BTRFS_ORPHAN_ITEM_KEY;
Josef Bacik7b128762008-07-24 12:17:14 -04003493 key.offset = (u64)-1;
3494
Josef Bacik7b128762008-07-24 12:17:14 -04003495 while (1) {
3496 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003497 if (ret < 0)
3498 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003499
3500 /*
3501 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003502 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04003503 * find the key and see if we have stuff that matches
3504 */
3505 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003506 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003507 if (path->slots[0] == 0)
3508 break;
3509 path->slots[0]--;
3510 }
3511
3512 /* pull out the item */
3513 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04003514 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3515
3516 /* make sure the item matches what we want */
3517 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
3518 break;
David Sterba962a2982014-06-04 18:41:45 +02003519 if (found_key.type != BTRFS_ORPHAN_ITEM_KEY)
Josef Bacik7b128762008-07-24 12:17:14 -04003520 break;
3521
3522 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02003523 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04003524
3525 /*
3526 * this is where we are basically btrfs_lookup, without the
3527 * crossing root thing. we store the inode number in the
3528 * offset of the orphan item.
3529 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003530
3531 if (found_key.offset == last_objectid) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003532 btrfs_err(fs_info,
3533 "Error removing orphan entry, stopping orphan cleanup");
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003534 ret = -EINVAL;
3535 goto out;
3536 }
3537
3538 last_objectid = found_key.offset;
3539
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003540 found_key.objectid = found_key.offset;
3541 found_key.type = BTRFS_INODE_ITEM_KEY;
3542 found_key.offset = 0;
David Sterba4c66e0d2019-10-03 19:09:35 +02003543 inode = btrfs_iget(fs_info->sb, &found_key, root);
Rusty Russell8c6ffba2013-07-15 11:20:32 +09303544 ret = PTR_ERR_OR_ZERO(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003545 if (ret && ret != -ENOENT)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003546 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003547
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003548 if (ret == -ENOENT && root == fs_info->tree_root) {
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003549 struct btrfs_root *dead_root;
3550 struct btrfs_fs_info *fs_info = root->fs_info;
3551 int is_dead_root = 0;
3552
3553 /*
3554 * this is an orphan in the tree root. Currently these
3555 * could come from 2 sources:
3556 * a) a snapshot deletion in progress
3557 * b) a free space cache inode
3558 * We need to distinguish those two, as the snapshot
3559 * orphan must not get deleted.
3560 * find_dead_roots already ran before us, so if this
3561 * is a snapshot deletion, we should find the root
3562 * in the dead_roots list
3563 */
3564 spin_lock(&fs_info->trans_lock);
3565 list_for_each_entry(dead_root, &fs_info->dead_roots,
3566 root_list) {
3567 if (dead_root->root_key.objectid ==
3568 found_key.objectid) {
3569 is_dead_root = 1;
3570 break;
3571 }
3572 }
3573 spin_unlock(&fs_info->trans_lock);
3574 if (is_dead_root) {
3575 /* prevent this orphan from being found again */
3576 key.offset = found_key.objectid - 1;
3577 continue;
3578 }
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003579
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003580 }
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003581
Josef Bacika8c9e572011-09-21 16:55:59 -04003582 /*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003583 * If we have an inode with links, there are a couple of
3584 * possibilities. Old kernels (before v3.12) used to create an
3585 * orphan item for truncate indicating that there were possibly
3586 * extent items past i_size that needed to be deleted. In v3.12,
3587 * truncate was changed to update i_size in sync with the extent
3588 * items, but the (useless) orphan item was still created. Since
3589 * v4.18, we don't create the orphan item for truncate at all.
3590 *
3591 * So, this item could mean that we need to do a truncate, but
3592 * only if this filesystem was last used on a pre-v3.12 kernel
3593 * and was not cleanly unmounted. The odds of that are quite
3594 * slim, and it's a pain to do the truncate now, so just delete
3595 * the orphan item.
3596 *
3597 * It's also possible that this orphan item was supposed to be
3598 * deleted but wasn't. The inode number may have been reused,
3599 * but either way, we can delete the orphan item.
Josef Bacika8c9e572011-09-21 16:55:59 -04003600 */
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003601 if (ret == -ENOENT || inode->i_nlink) {
3602 if (!ret)
3603 iput(inode);
Josef Bacika8c9e572011-09-21 16:55:59 -04003604 trans = btrfs_start_transaction(root, 1);
3605 if (IS_ERR(trans)) {
3606 ret = PTR_ERR(trans);
3607 goto out;
3608 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003609 btrfs_debug(fs_info, "auto deleting %Lu",
3610 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003611 ret = btrfs_del_orphan_item(trans, root,
3612 found_key.objectid);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003613 btrfs_end_transaction(trans);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003614 if (ret)
3615 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003616 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003617 }
Josef Bacik7b128762008-07-24 12:17:14 -04003618
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003619 nr_unlink++;
Josef Bacik7b128762008-07-24 12:17:14 -04003620
3621 /* this will do delete_inode and everything for us */
3622 iput(inode);
3623 }
Miao Xie3254c872011-11-10 20:45:05 -05003624 /* release the path since we're done with it */
3625 btrfs_release_path(path);
3626
Yan, Zhengd68fc572010-05-16 10:49:58 -04003627 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
3628
Omar Sandovala575cee2018-05-11 13:13:38 -07003629 if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003630 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003631 if (!IS_ERR(trans))
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003632 btrfs_end_transaction(trans);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003633 }
Josef Bacik7b128762008-07-24 12:17:14 -04003634
3635 if (nr_unlink)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003636 btrfs_debug(fs_info, "unlinked %d orphans", nr_unlink);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003637
3638out:
3639 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003640 btrfs_err(fs_info, "could not do orphan cleanup %d", ret);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003641 btrfs_free_path(path);
3642 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003643}
3644
Chris Masond352ac62008-09-29 15:18:18 -04003645/*
Chris Mason46a53cc2009-04-27 11:47:50 -04003646 * very simple check to peek ahead in the leaf looking for xattrs. If we
3647 * don't find any xattrs, we know there can't be any acls.
3648 *
3649 * slot is the slot the inode is in, objectid is the objectid of the inode
3650 */
3651static noinline int acls_after_inode_item(struct extent_buffer *leaf,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003652 int slot, u64 objectid,
3653 int *first_xattr_slot)
Chris Mason46a53cc2009-04-27 11:47:50 -04003654{
3655 u32 nritems = btrfs_header_nritems(leaf);
3656 struct btrfs_key found_key;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003657 static u64 xattr_access = 0;
3658 static u64 xattr_default = 0;
Chris Mason46a53cc2009-04-27 11:47:50 -04003659 int scanned = 0;
3660
Josef Bacikf23b5a52013-06-19 10:16:26 -04003661 if (!xattr_access) {
Andreas Gruenbacher97d79292015-12-02 14:44:35 +01003662 xattr_access = btrfs_name_hash(XATTR_NAME_POSIX_ACL_ACCESS,
3663 strlen(XATTR_NAME_POSIX_ACL_ACCESS));
3664 xattr_default = btrfs_name_hash(XATTR_NAME_POSIX_ACL_DEFAULT,
3665 strlen(XATTR_NAME_POSIX_ACL_DEFAULT));
Josef Bacikf23b5a52013-06-19 10:16:26 -04003666 }
3667
Chris Mason46a53cc2009-04-27 11:47:50 -04003668 slot++;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003669 *first_xattr_slot = -1;
Chris Mason46a53cc2009-04-27 11:47:50 -04003670 while (slot < nritems) {
3671 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3672
3673 /* we found a different objectid, there must not be acls */
3674 if (found_key.objectid != objectid)
3675 return 0;
3676
3677 /* we found an xattr, assume we've got an acl */
Josef Bacikf23b5a52013-06-19 10:16:26 -04003678 if (found_key.type == BTRFS_XATTR_ITEM_KEY) {
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003679 if (*first_xattr_slot == -1)
3680 *first_xattr_slot = slot;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003681 if (found_key.offset == xattr_access ||
3682 found_key.offset == xattr_default)
3683 return 1;
3684 }
Chris Mason46a53cc2009-04-27 11:47:50 -04003685
3686 /*
3687 * we found a key greater than an xattr key, there can't
3688 * be any acls later on
3689 */
3690 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
3691 return 0;
3692
3693 slot++;
3694 scanned++;
3695
3696 /*
3697 * it goes inode, inode backrefs, xattrs, extents,
3698 * so if there are a ton of hard links to an inode there can
3699 * be a lot of backrefs. Don't waste time searching too hard,
3700 * this is just an optimization
3701 */
3702 if (scanned >= 8)
3703 break;
3704 }
3705 /* we hit the end of the leaf before we found an xattr or
3706 * something larger than an xattr. We have to assume the inode
3707 * has acls
3708 */
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003709 if (*first_xattr_slot == -1)
3710 *first_xattr_slot = slot;
Chris Mason46a53cc2009-04-27 11:47:50 -04003711 return 1;
3712}
3713
3714/*
Chris Masond352ac62008-09-29 15:18:18 -04003715 * read an inode from the btree into the in-memory inode
3716 */
Filipe Manana4222ea72018-10-24 10:13:03 +01003717static int btrfs_read_locked_inode(struct inode *inode,
3718 struct btrfs_path *in_path)
Chris Mason39279cc2007-06-12 06:35:45 -04003719{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003720 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Filipe Manana4222ea72018-10-24 10:13:03 +01003721 struct btrfs_path *path = in_path;
Chris Mason5f39d392007-10-15 16:14:19 -04003722 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003723 struct btrfs_inode_item *inode_item;
3724 struct btrfs_root *root = BTRFS_I(inode)->root;
3725 struct btrfs_key location;
Miao Xie67de1172013-12-26 13:07:06 +08003726 unsigned long ptr;
Chris Mason46a53cc2009-04-27 11:47:50 -04003727 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04003728 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04003729 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003730 bool filled = false;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003731 int first_xattr_slot;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003732
3733 ret = btrfs_fill_inode(inode, &rdev);
3734 if (!ret)
3735 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04003736
Filipe Manana4222ea72018-10-24 10:13:03 +01003737 if (!path) {
3738 path = btrfs_alloc_path();
3739 if (!path)
3740 return -ENOMEM;
3741 }
Mark Fasheh1748f842011-07-12 11:25:31 -07003742
Chris Mason39279cc2007-06-12 06:35:45 -04003743 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05003744
Chris Mason39279cc2007-06-12 06:35:45 -04003745 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Filipe Manana67710892016-06-06 11:51:25 +01003746 if (ret) {
Filipe Manana4222ea72018-10-24 10:13:03 +01003747 if (path != in_path)
3748 btrfs_free_path(path);
Al Virof5b3a412018-07-29 23:04:51 +01003749 return ret;
Filipe Manana67710892016-06-06 11:51:25 +01003750 }
Chris Mason39279cc2007-06-12 06:35:45 -04003751
Chris Mason5f39d392007-10-15 16:14:19 -04003752 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00003753
3754 if (filled)
Miao Xie67de1172013-12-26 13:07:06 +08003755 goto cache_index;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003756
Chris Mason5f39d392007-10-15 16:14:19 -04003757 inode_item = btrfs_item_ptr(leaf, path->slots[0],
3758 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003759 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02003760 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08003761 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
3762 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02003763 btrfs_i_size_write(BTRFS_I(inode), btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04003764
David Sterbaa937b972014-12-12 17:39:12 +01003765 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->atime);
3766 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->atime);
Chris Mason5f39d392007-10-15 16:14:19 -04003767
David Sterbaa937b972014-12-12 17:39:12 +01003768 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->mtime);
3769 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->mtime);
Chris Mason5f39d392007-10-15 16:14:19 -04003770
David Sterbaa937b972014-12-12 17:39:12 +01003771 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->ctime);
3772 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->ctime);
Chris Mason5f39d392007-10-15 16:14:19 -04003773
chandan r9cc97d62012-07-04 12:48:07 +05303774 BTRFS_I(inode)->i_otime.tv_sec =
3775 btrfs_timespec_sec(leaf, &inode_item->otime);
3776 BTRFS_I(inode)->i_otime.tv_nsec =
3777 btrfs_timespec_nsec(leaf, &inode_item->otime);
Chris Mason5f39d392007-10-15 16:14:19 -04003778
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003779 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003780 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003781 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
3782
Jeff Laytonc7f88c42017-12-11 06:35:12 -05003783 inode_set_iversion_queried(inode,
3784 btrfs_inode_sequence(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003785 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04003786 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003787 rdev = btrfs_inode_rdev(leaf, inode_item);
3788
Josef Bacikaec74772008-07-24 12:12:38 -04003789 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05003790 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Miao Xie67de1172013-12-26 13:07:06 +08003791
3792cache_index:
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003793 /*
3794 * If we were modified in the current generation and evicted from memory
3795 * and then re-read we need to do a full sync since we don't have any
3796 * idea about which extents were modified before we were evicted from
3797 * cache.
3798 *
3799 * This is required for both inode re-read from disk and delayed inode
3800 * in delayed_nodes_tree.
3801 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003802 if (BTRFS_I(inode)->last_trans == fs_info->generation)
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003803 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3804 &BTRFS_I(inode)->runtime_flags);
3805
Filipe Mananabde6c242015-07-24 00:00:19 +01003806 /*
3807 * We don't persist the id of the transaction where an unlink operation
3808 * against the inode was last made. So here we assume the inode might
3809 * have been evicted, and therefore the exact value of last_unlink_trans
3810 * lost, and set it to last_trans to avoid metadata inconsistencies
3811 * between the inode and its parent if the inode is fsync'ed and the log
3812 * replayed. For example, in the scenario:
3813 *
3814 * touch mydir/foo
3815 * ln mydir/foo mydir/bar
3816 * sync
3817 * unlink mydir/bar
3818 * echo 2 > /proc/sys/vm/drop_caches # evicts inode
3819 * xfs_io -c fsync mydir/foo
3820 * <power failure>
3821 * mount fs, triggers fsync log replay
3822 *
3823 * We must make sure that when we fsync our inode foo we also log its
3824 * parent inode, otherwise after log replay the parent still has the
3825 * dentry with the "bar" name but our inode foo has a link count of 1
3826 * and doesn't have an inode ref with the name "bar" anymore.
3827 *
3828 * Setting last_unlink_trans to last_trans is a pessimistic approach,
Nicholas D Steeves01327612016-05-19 21:18:45 -04003829 * but it guarantees correctness at the expense of occasional full
Filipe Mananabde6c242015-07-24 00:00:19 +01003830 * transaction commits on fsync if our inode is a directory, or if our
3831 * inode is not a directory, logging its parent unnecessarily.
3832 */
3833 BTRFS_I(inode)->last_unlink_trans = BTRFS_I(inode)->last_trans;
3834
Miao Xie67de1172013-12-26 13:07:06 +08003835 path->slots[0]++;
3836 if (inode->i_nlink != 1 ||
3837 path->slots[0] >= btrfs_header_nritems(leaf))
3838 goto cache_acl;
3839
3840 btrfs_item_key_to_cpu(leaf, &location, path->slots[0]);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003841 if (location.objectid != btrfs_ino(BTRFS_I(inode)))
Miao Xie67de1172013-12-26 13:07:06 +08003842 goto cache_acl;
3843
3844 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3845 if (location.type == BTRFS_INODE_REF_KEY) {
3846 struct btrfs_inode_ref *ref;
3847
3848 ref = (struct btrfs_inode_ref *)ptr;
3849 BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref);
3850 } else if (location.type == BTRFS_INODE_EXTREF_KEY) {
3851 struct btrfs_inode_extref *extref;
3852
3853 extref = (struct btrfs_inode_extref *)ptr;
3854 BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf,
3855 extref);
3856 }
Miao Xie2f7e33d2011-06-23 07:27:13 +00003857cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04003858 /*
3859 * try to precache a NULL acl entry for files that don't have
3860 * any xattrs or acls
3861 */
Li Zefan33345d012011-04-20 10:31:50 +08003862 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
David Sterbaf85b7372017-01-20 14:54:07 +01003863 btrfs_ino(BTRFS_I(inode)), &first_xattr_slot);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003864 if (first_xattr_slot != -1) {
3865 path->slots[0] = first_xattr_slot;
3866 ret = btrfs_load_inode_props(inode, path);
3867 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003868 btrfs_err(fs_info,
David Sterba351fd352014-05-15 16:48:20 +02003869 "error loading props for ino %llu (root %llu): %d",
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003870 btrfs_ino(BTRFS_I(inode)),
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003871 root->root_key.objectid, ret);
3872 }
Filipe Manana4222ea72018-10-24 10:13:03 +01003873 if (path != in_path)
3874 btrfs_free_path(path);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003875
Al Viro72c04902009-06-24 16:58:48 -04003876 if (!maybe_acls)
3877 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04003878
Chris Mason39279cc2007-06-12 06:35:45 -04003879 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04003880 case S_IFREG:
3881 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masond1310b22008-01-24 16:13:08 -05003882 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04003883 inode->i_fop = &btrfs_file_operations;
3884 inode->i_op = &btrfs_file_inode_operations;
3885 break;
3886 case S_IFDIR:
3887 inode->i_fop = &btrfs_dir_file_operations;
Omar Sandoval67ade052017-01-25 17:06:38 -08003888 inode->i_op = &btrfs_dir_inode_operations;
Chris Mason39279cc2007-06-12 06:35:45 -04003889 break;
3890 case S_IFLNK:
3891 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -05003892 inode_nohighmem(inode);
Omar Sandoval4779cc02018-09-24 15:16:55 -07003893 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason39279cc2007-06-12 06:35:45 -04003894 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04003895 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05003896 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04003897 init_special_inode(inode, inode->i_mode, rdev);
3898 break;
Chris Mason39279cc2007-06-12 06:35:45 -04003899 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003900
David Sterba7b6a2212018-03-26 18:40:21 +02003901 btrfs_sync_inode_flags_to_i_flags(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003902 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003903}
3904
Chris Masond352ac62008-09-29 15:18:18 -04003905/*
3906 * given a leaf and an inode, copy the inode fields into the leaf
3907 */
Chris Masone02119d2008-09-05 16:13:11 -04003908static void fill_inode_item(struct btrfs_trans_handle *trans,
3909 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04003910 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04003911 struct inode *inode)
3912{
Liu Bo51fab692012-12-27 09:01:21 +00003913 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003914
David Sterbac82f8232019-08-09 17:48:21 +02003915 btrfs_init_map_token(&token, leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04003916
Liu Bo51fab692012-12-27 09:01:21 +00003917 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
3918 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
3919 btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size,
3920 &token);
3921 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3922 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003923
David Sterbaa937b972014-12-12 17:39:12 +01003924 btrfs_set_token_timespec_sec(leaf, &item->atime,
Liu Bo51fab692012-12-27 09:01:21 +00003925 inode->i_atime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003926 btrfs_set_token_timespec_nsec(leaf, &item->atime,
Liu Bo51fab692012-12-27 09:01:21 +00003927 inode->i_atime.tv_nsec, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003928
David Sterbaa937b972014-12-12 17:39:12 +01003929 btrfs_set_token_timespec_sec(leaf, &item->mtime,
Liu Bo51fab692012-12-27 09:01:21 +00003930 inode->i_mtime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003931 btrfs_set_token_timespec_nsec(leaf, &item->mtime,
Liu Bo51fab692012-12-27 09:01:21 +00003932 inode->i_mtime.tv_nsec, &token);
3933
David Sterbaa937b972014-12-12 17:39:12 +01003934 btrfs_set_token_timespec_sec(leaf, &item->ctime,
Liu Bo51fab692012-12-27 09:01:21 +00003935 inode->i_ctime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003936 btrfs_set_token_timespec_nsec(leaf, &item->ctime,
Liu Bo51fab692012-12-27 09:01:21 +00003937 inode->i_ctime.tv_nsec, &token);
3938
chandan r9cc97d62012-07-04 12:48:07 +05303939 btrfs_set_token_timespec_sec(leaf, &item->otime,
3940 BTRFS_I(inode)->i_otime.tv_sec, &token);
3941 btrfs_set_token_timespec_nsec(leaf, &item->otime,
3942 BTRFS_I(inode)->i_otime.tv_nsec, &token);
3943
Liu Bo51fab692012-12-27 09:01:21 +00003944 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3945 &token);
3946 btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation,
3947 &token);
Jeff Laytonc7f88c42017-12-11 06:35:12 -05003948 btrfs_set_token_inode_sequence(leaf, item, inode_peek_iversion(inode),
3949 &token);
Liu Bo51fab692012-12-27 09:01:21 +00003950 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
3951 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3952 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3953 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
Chris Mason39279cc2007-06-12 06:35:45 -04003954}
3955
Chris Masond352ac62008-09-29 15:18:18 -04003956/*
3957 * copy everything in the in-memory inode into the btree.
3958 */
Chris Mason21151332011-11-10 20:39:08 -05003959static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Chris Masond3977122009-01-05 21:25:51 -05003960 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003961{
3962 struct btrfs_inode_item *inode_item;
3963 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003964 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003965 int ret;
3966
3967 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08003968 if (!path)
3969 return -ENOMEM;
3970
Chris Masonb9473432009-03-13 11:00:37 -04003971 path->leave_spinning = 1;
Miao Xie16cdcec2011-04-22 18:12:22 +08003972 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
3973 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003974 if (ret) {
3975 if (ret > 0)
3976 ret = -ENOENT;
3977 goto failed;
3978 }
3979
Chris Mason5f39d392007-10-15 16:14:19 -04003980 leaf = path->nodes[0];
3981 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08003982 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04003983
Chris Masone02119d2008-09-05 16:13:11 -04003984 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04003985 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04003986 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003987 ret = 0;
3988failed:
Chris Mason39279cc2007-06-12 06:35:45 -04003989 btrfs_free_path(path);
3990 return ret;
3991}
3992
Chris Masond352ac62008-09-29 15:18:18 -04003993/*
Chris Mason21151332011-11-10 20:39:08 -05003994 * copy everything in the in-memory inode into the btree.
3995 */
3996noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
3997 struct btrfs_root *root, struct inode *inode)
3998{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003999 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason21151332011-11-10 20:39:08 -05004000 int ret;
4001
4002 /*
4003 * If the inode is a free space inode, we can deadlock during commit
4004 * if we put it into the delayed code.
4005 *
4006 * The data relocation inode should also be directly updated
4007 * without delay
4008 */
Nikolay Borisov70ddc552017-02-20 13:50:35 +02004009 if (!btrfs_is_free_space_inode(BTRFS_I(inode))
Josef Bacik1d52c782014-09-18 11:30:44 -04004010 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004011 && !test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) {
Alexander Block8ea05e32012-07-25 17:35:53 +02004012 btrfs_update_root_times(trans, root);
4013
Chris Mason21151332011-11-10 20:39:08 -05004014 ret = btrfs_delayed_update_inode(trans, root, inode);
4015 if (!ret)
4016 btrfs_set_inode_last_trans(trans, inode);
4017 return ret;
4018 }
4019
4020 return btrfs_update_inode_item(trans, root, inode);
4021}
4022
Josef Bacikbe6aef62012-10-22 15:43:12 -04004023noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
4024 struct btrfs_root *root,
4025 struct inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05004026{
4027 int ret;
4028
4029 ret = btrfs_update_inode(trans, root, inode);
4030 if (ret == -ENOSPC)
4031 return btrfs_update_inode_item(trans, root, inode);
4032 return ret;
4033}
4034
4035/*
Chris Masond352ac62008-09-29 15:18:18 -04004036 * unlink helper that gets used here in inode.c and in the tree logging
4037 * recovery code. It remove a link in a directory with a given name, and
4038 * also drops the back refs in the inode to the directory
4039 */
Al Viro92986792011-03-04 17:14:37 +00004040static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
4041 struct btrfs_root *root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004042 struct btrfs_inode *dir,
4043 struct btrfs_inode *inode,
Al Viro92986792011-03-04 17:14:37 +00004044 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04004045{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004046 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04004047 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04004048 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004049 struct btrfs_dir_item *di;
Josef Bacikaec74772008-07-24 12:12:38 -04004050 u64 index;
Li Zefan33345d012011-04-20 10:31:50 +08004051 u64 ino = btrfs_ino(inode);
4052 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04004053
4054 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04004055 if (!path) {
4056 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00004057 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04004058 }
4059
Chris Masonb9473432009-03-13 11:00:37 -04004060 path->leave_spinning = 1;
Li Zefan33345d012011-04-20 10:31:50 +08004061 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04004062 name, name_len, -1);
Liu Bo3cf50682018-09-12 06:06:26 +08004063 if (IS_ERR_OR_NULL(di)) {
4064 ret = di ? PTR_ERR(di) : -ENOENT;
Chris Mason39279cc2007-06-12 06:35:45 -04004065 goto err;
4066 }
Chris Mason39279cc2007-06-12 06:35:45 -04004067 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04004068 if (ret)
4069 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02004070 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04004071
Miao Xie67de1172013-12-26 13:07:06 +08004072 /*
4073 * If we don't have dir index, we have to get it by looking up
4074 * the inode ref, since we get the inode ref, remove it directly,
4075 * it is unnecessary to do delayed deletion.
4076 *
4077 * But if we have dir index, needn't search inode ref to get it.
4078 * Since the inode ref is close to the inode item, it is better
4079 * that we delay to delete it, and just do this deletion when
4080 * we update the inode item.
4081 */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004082 if (inode->dir_index) {
Miao Xie67de1172013-12-26 13:07:06 +08004083 ret = btrfs_delayed_delete_inode_ref(inode);
4084 if (!ret) {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004085 index = inode->dir_index;
Miao Xie67de1172013-12-26 13:07:06 +08004086 goto skip_backref;
4087 }
4088 }
4089
Li Zefan33345d012011-04-20 10:31:50 +08004090 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
4091 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04004092 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004093 btrfs_info(fs_info,
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00004094 "failed to delete reference to %.*s, inode %llu parent %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02004095 name_len, name, ino, dir_ino);
Jeff Mahoney66642832016-06-10 18:19:25 -04004096 btrfs_abort_transaction(trans, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04004097 goto err;
4098 }
Miao Xie67de1172013-12-26 13:07:06 +08004099skip_backref:
Lu Fengqi9add2942018-08-01 11:32:26 +08004100 ret = btrfs_delete_delayed_dir_index(trans, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004101 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004102 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04004103 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004104 }
Chris Mason39279cc2007-06-12 06:35:45 -04004105
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004106 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len, inode,
4107 dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004108 if (ret != 0 && ret != -ENOENT) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004109 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004110 goto err;
4111 }
Chris Masone02119d2008-09-05 16:13:11 -04004112
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004113 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len, dir,
4114 index);
Chris Mason6418c962010-10-30 07:34:24 -04004115 if (ret == -ENOENT)
4116 ret = 0;
Zach Brownd4e39912013-04-02 21:02:16 +00004117 else if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004118 btrfs_abort_transaction(trans, ret);
Josef Bacik63611e72019-06-18 10:59:18 -04004119
4120 /*
4121 * If we have a pending delayed iput we could end up with the final iput
4122 * being run in btrfs-cleaner context. If we have enough of these built
4123 * up we can end up burning a lot of time in btrfs-cleaner without any
4124 * way to throttle the unlinks. Since we're currently holding a ref on
4125 * the inode we can run the delayed iput here without any issues as the
4126 * final iput won't be done until after we drop the ref we're currently
4127 * holding.
4128 */
4129 btrfs_run_delayed_iput(fs_info, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004130err:
4131 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04004132 if (ret)
4133 goto out;
4134
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004135 btrfs_i_size_write(dir, dir->vfs_inode.i_size - name_len * 2);
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004136 inode_inc_iversion(&inode->vfs_inode);
4137 inode_inc_iversion(&dir->vfs_inode);
4138 inode->vfs_inode.i_ctime = dir->vfs_inode.i_mtime =
4139 dir->vfs_inode.i_ctime = current_time(&inode->vfs_inode);
4140 ret = btrfs_update_inode(trans, root, &dir->vfs_inode);
Chris Masone02119d2008-09-05 16:13:11 -04004141out:
Chris Mason39279cc2007-06-12 06:35:45 -04004142 return ret;
4143}
4144
Al Viro92986792011-03-04 17:14:37 +00004145int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
4146 struct btrfs_root *root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004147 struct btrfs_inode *dir, struct btrfs_inode *inode,
Al Viro92986792011-03-04 17:14:37 +00004148 const char *name, int name_len)
4149{
4150 int ret;
4151 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
4152 if (!ret) {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004153 drop_nlink(&inode->vfs_inode);
4154 ret = btrfs_update_inode(trans, root, &inode->vfs_inode);
Al Viro92986792011-03-04 17:14:37 +00004155 }
4156 return ret;
4157}
Yan, Zhenga22285a2010-05-16 10:48:46 -04004158
4159/*
4160 * helper to start transaction for unlink and rmdir.
4161 *
Josef Bacikd52be812013-05-29 14:54:47 -04004162 * unlink and rmdir are special in btrfs, they do not always free space, so
4163 * if we cannot make our reservations the normal way try and see if there is
4164 * plenty of slack room in the global reserve to migrate, otherwise we cannot
4165 * allow the unlink to occur.
Yan, Zhenga22285a2010-05-16 10:48:46 -04004166 */
Josef Bacikd52be812013-05-29 14:54:47 -04004167static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir)
Yan, Zhenga22285a2010-05-16 10:48:46 -04004168{
Yan, Zhenga22285a2010-05-16 10:48:46 -04004169 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004170
Josef Bacike70bea52011-10-11 14:18:24 -04004171 /*
4172 * 1 for the possible orphan item
4173 * 1 for the dir item
4174 * 1 for the dir index
4175 * 1 for the inode ref
Josef Bacike70bea52011-10-11 14:18:24 -04004176 * 1 for the inode
4177 */
Filipe Manana8eab77f2015-11-13 23:57:16 +00004178 return btrfs_start_transaction_fallback_global_rsv(root, 5, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004179}
4180
Chris Mason39279cc2007-06-12 06:35:45 -04004181static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
4182{
Yan, Zhenga22285a2010-05-16 10:48:46 -04004183 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004184 struct btrfs_trans_handle *trans;
David Howells2b0143b2015-03-17 22:25:59 +00004185 struct inode *inode = d_inode(dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04004186 int ret;
4187
Josef Bacikd52be812013-05-29 14:54:47 -04004188 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004189 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004190 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04004191
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004192 btrfs_record_unlink_dir(trans, BTRFS_I(dir), BTRFS_I(d_inode(dentry)),
4193 0);
Chris Mason12fcfd22009-03-24 10:24:20 -04004194
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004195 ret = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
4196 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
4197 dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00004198 if (ret)
4199 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004200
Yan, Zhenga22285a2010-05-16 10:48:46 -04004201 if (inode->i_nlink == 0) {
Nikolay Borisov73f2e542017-02-20 13:50:59 +02004202 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Tsutomu Itohb5324022011-07-19 07:27:20 +00004203 if (ret)
4204 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004205 }
Josef Bacik7b128762008-07-24 12:17:14 -04004206
Tsutomu Itohb5324022011-07-19 07:27:20 +00004207out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004208 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004209 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04004210 return ret;
4211}
4212
Misono Tomohirof60a2362018-04-18 11:34:52 +09004213static int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
Lu Fengqi401b3b12018-08-01 11:32:30 +08004214 struct inode *dir, u64 objectid,
4215 const char *name, int name_len)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004216{
Lu Fengqi401b3b12018-08-01 11:32:30 +08004217 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004218 struct btrfs_path *path;
4219 struct extent_buffer *leaf;
4220 struct btrfs_dir_item *di;
4221 struct btrfs_key key;
4222 u64 index;
4223 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004224 u64 dir_ino = btrfs_ino(BTRFS_I(dir));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004225
4226 path = btrfs_alloc_path();
4227 if (!path)
4228 return -ENOMEM;
4229
Li Zefan33345d012011-04-20 10:31:50 +08004230 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004231 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004232 if (IS_ERR_OR_NULL(di)) {
Liu Bo3cf50682018-09-12 06:06:26 +08004233 ret = di ? PTR_ERR(di) : -ENOENT;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004234 goto out;
4235 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004236
4237 leaf = path->nodes[0];
4238 btrfs_dir_item_key_to_cpu(leaf, di, &key);
4239 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
4240 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004241 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004242 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004243 goto out;
4244 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004245 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004246
Lu Fengqi3ee1c552018-08-01 11:32:28 +08004247 ret = btrfs_del_root_ref(trans, objectid, root->root_key.objectid,
4248 dir_ino, &index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004249 if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004250 if (ret != -ENOENT) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004251 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004252 goto out;
4253 }
Li Zefan33345d012011-04-20 10:31:50 +08004254 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004255 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004256 if (IS_ERR_OR_NULL(di)) {
4257 if (!di)
4258 ret = -ENOENT;
4259 else
4260 ret = PTR_ERR(di);
Jeff Mahoney66642832016-06-10 18:19:25 -04004261 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004262 goto out;
4263 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004264
4265 leaf = path->nodes[0];
4266 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004267 index = key.offset;
4268 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004269 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004270
Lu Fengqi9add2942018-08-01 11:32:26 +08004271 ret = btrfs_delete_delayed_dir_index(trans, BTRFS_I(dir), index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004272 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004273 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004274 goto out;
4275 }
Miao Xie16cdcec2011-04-22 18:12:22 +08004276
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004277 btrfs_i_size_write(BTRFS_I(dir), dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004278 inode_inc_iversion(dir);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07004279 dir->i_mtime = dir->i_ctime = current_time(dir);
Josef Bacik5a24e842012-08-08 10:12:59 -06004280 ret = btrfs_update_inode_fallback(trans, root, dir);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004281 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004282 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004283out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04004284 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004285 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004286}
4287
Misono Tomohiroec42f162018-04-18 11:34:13 +09004288/*
4289 * Helper to check if the subvolume references other subvolumes or if it's
4290 * default.
4291 */
Misono Tomohirof60a2362018-04-18 11:34:52 +09004292static noinline int may_destroy_subvol(struct btrfs_root *root)
Misono Tomohiroec42f162018-04-18 11:34:13 +09004293{
4294 struct btrfs_fs_info *fs_info = root->fs_info;
4295 struct btrfs_path *path;
4296 struct btrfs_dir_item *di;
4297 struct btrfs_key key;
4298 u64 dir_id;
4299 int ret;
4300
4301 path = btrfs_alloc_path();
4302 if (!path)
4303 return -ENOMEM;
4304
4305 /* Make sure this root isn't set as the default subvol */
4306 dir_id = btrfs_super_root_dir(fs_info->super_copy);
4307 di = btrfs_lookup_dir_item(NULL, fs_info->tree_root, path,
4308 dir_id, "default", 7, 0);
4309 if (di && !IS_ERR(di)) {
4310 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key);
4311 if (key.objectid == root->root_key.objectid) {
4312 ret = -EPERM;
4313 btrfs_err(fs_info,
4314 "deleting default subvolume %llu is not allowed",
4315 key.objectid);
4316 goto out;
4317 }
4318 btrfs_release_path(path);
4319 }
4320
4321 key.objectid = root->root_key.objectid;
4322 key.type = BTRFS_ROOT_REF_KEY;
4323 key.offset = (u64)-1;
4324
4325 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
4326 if (ret < 0)
4327 goto out;
4328 BUG_ON(ret == 0);
4329
4330 ret = 0;
4331 if (path->slots[0] > 0) {
4332 path->slots[0]--;
4333 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
4334 if (key.objectid == root->root_key.objectid &&
4335 key.type == BTRFS_ROOT_REF_KEY)
4336 ret = -ENOTEMPTY;
4337 }
4338out:
4339 btrfs_free_path(path);
4340 return ret;
4341}
4342
Nikolay Borisov20a68002018-04-27 14:36:24 +03004343/* Delete all dentries for inodes belonging to the root */
4344static void btrfs_prune_dentries(struct btrfs_root *root)
4345{
4346 struct btrfs_fs_info *fs_info = root->fs_info;
4347 struct rb_node *node;
4348 struct rb_node *prev;
4349 struct btrfs_inode *entry;
4350 struct inode *inode;
4351 u64 objectid = 0;
4352
4353 if (!test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
4354 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
4355
4356 spin_lock(&root->inode_lock);
4357again:
4358 node = root->inode_tree.rb_node;
4359 prev = NULL;
4360 while (node) {
4361 prev = node;
4362 entry = rb_entry(node, struct btrfs_inode, rb_node);
4363
David Sterba37508512018-06-29 10:56:40 +02004364 if (objectid < btrfs_ino(entry))
Nikolay Borisov20a68002018-04-27 14:36:24 +03004365 node = node->rb_left;
David Sterba37508512018-06-29 10:56:40 +02004366 else if (objectid > btrfs_ino(entry))
Nikolay Borisov20a68002018-04-27 14:36:24 +03004367 node = node->rb_right;
4368 else
4369 break;
4370 }
4371 if (!node) {
4372 while (prev) {
4373 entry = rb_entry(prev, struct btrfs_inode, rb_node);
David Sterba37508512018-06-29 10:56:40 +02004374 if (objectid <= btrfs_ino(entry)) {
Nikolay Borisov20a68002018-04-27 14:36:24 +03004375 node = prev;
4376 break;
4377 }
4378 prev = rb_next(prev);
4379 }
4380 }
4381 while (node) {
4382 entry = rb_entry(node, struct btrfs_inode, rb_node);
David Sterba37508512018-06-29 10:56:40 +02004383 objectid = btrfs_ino(entry) + 1;
Nikolay Borisov20a68002018-04-27 14:36:24 +03004384 inode = igrab(&entry->vfs_inode);
4385 if (inode) {
4386 spin_unlock(&root->inode_lock);
4387 if (atomic_read(&inode->i_count) > 1)
4388 d_prune_aliases(inode);
4389 /*
4390 * btrfs_drop_inode will have it removed from the inode
4391 * cache when its usage count hits zero.
4392 */
4393 iput(inode);
4394 cond_resched();
4395 spin_lock(&root->inode_lock);
4396 goto again;
4397 }
4398
4399 if (cond_resched_lock(&root->inode_lock))
4400 goto again;
4401
4402 node = rb_next(node);
4403 }
4404 spin_unlock(&root->inode_lock);
4405}
4406
Misono Tomohirof60a2362018-04-18 11:34:52 +09004407int btrfs_delete_subvolume(struct inode *dir, struct dentry *dentry)
4408{
4409 struct btrfs_fs_info *fs_info = btrfs_sb(dentry->d_sb);
4410 struct btrfs_root *root = BTRFS_I(dir)->root;
4411 struct inode *inode = d_inode(dentry);
4412 struct btrfs_root *dest = BTRFS_I(inode)->root;
4413 struct btrfs_trans_handle *trans;
4414 struct btrfs_block_rsv block_rsv;
4415 u64 root_flags;
Misono Tomohirof60a2362018-04-18 11:34:52 +09004416 int ret;
4417 int err;
4418
4419 /*
4420 * Don't allow to delete a subvolume with send in progress. This is
4421 * inside the inode lock so the error handling that has to drop the bit
4422 * again is not run concurrently.
4423 */
4424 spin_lock(&dest->root_item_lock);
Lu Fengqia7176f72018-08-04 21:10:53 +08004425 if (dest->send_in_progress) {
Misono Tomohirof60a2362018-04-18 11:34:52 +09004426 spin_unlock(&dest->root_item_lock);
4427 btrfs_warn(fs_info,
4428 "attempt to delete subvolume %llu during send",
4429 dest->root_key.objectid);
4430 return -EPERM;
4431 }
Lu Fengqia7176f72018-08-04 21:10:53 +08004432 root_flags = btrfs_root_flags(&dest->root_item);
4433 btrfs_set_root_flags(&dest->root_item,
4434 root_flags | BTRFS_ROOT_SUBVOL_DEAD);
4435 spin_unlock(&dest->root_item_lock);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004436
4437 down_write(&fs_info->subvol_sem);
4438
4439 err = may_destroy_subvol(dest);
4440 if (err)
4441 goto out_up_write;
4442
4443 btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP);
4444 /*
4445 * One for dir inode,
4446 * two for dir entries,
4447 * two for root ref/backref.
4448 */
Gu JinXiangc4c129d2018-05-30 11:00:38 +08004449 err = btrfs_subvolume_reserve_metadata(root, &block_rsv, 5, true);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004450 if (err)
4451 goto out_up_write;
4452
4453 trans = btrfs_start_transaction(root, 0);
4454 if (IS_ERR(trans)) {
4455 err = PTR_ERR(trans);
4456 goto out_release;
4457 }
4458 trans->block_rsv = &block_rsv;
4459 trans->bytes_reserved = block_rsv.size;
4460
4461 btrfs_record_snapshot_destroy(trans, BTRFS_I(dir));
4462
Lu Fengqi401b3b12018-08-01 11:32:30 +08004463 ret = btrfs_unlink_subvol(trans, dir, dest->root_key.objectid,
4464 dentry->d_name.name, dentry->d_name.len);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004465 if (ret) {
4466 err = ret;
4467 btrfs_abort_transaction(trans, ret);
4468 goto out_end_trans;
4469 }
4470
4471 btrfs_record_root_in_trans(trans, dest);
4472
4473 memset(&dest->root_item.drop_progress, 0,
4474 sizeof(dest->root_item.drop_progress));
4475 dest->root_item.drop_level = 0;
4476 btrfs_set_root_refs(&dest->root_item, 0);
4477
4478 if (!test_and_set_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &dest->state)) {
4479 ret = btrfs_insert_orphan_item(trans,
4480 fs_info->tree_root,
4481 dest->root_key.objectid);
4482 if (ret) {
4483 btrfs_abort_transaction(trans, ret);
4484 err = ret;
4485 goto out_end_trans;
4486 }
4487 }
4488
Lu Fengqid1957792018-05-29 15:01:54 +08004489 ret = btrfs_uuid_tree_remove(trans, dest->root_item.uuid,
Misono Tomohirof60a2362018-04-18 11:34:52 +09004490 BTRFS_UUID_KEY_SUBVOL,
4491 dest->root_key.objectid);
4492 if (ret && ret != -ENOENT) {
4493 btrfs_abort_transaction(trans, ret);
4494 err = ret;
4495 goto out_end_trans;
4496 }
4497 if (!btrfs_is_empty_uuid(dest->root_item.received_uuid)) {
Lu Fengqid1957792018-05-29 15:01:54 +08004498 ret = btrfs_uuid_tree_remove(trans,
Misono Tomohirof60a2362018-04-18 11:34:52 +09004499 dest->root_item.received_uuid,
4500 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
4501 dest->root_key.objectid);
4502 if (ret && ret != -ENOENT) {
4503 btrfs_abort_transaction(trans, ret);
4504 err = ret;
4505 goto out_end_trans;
4506 }
4507 }
4508
4509out_end_trans:
4510 trans->block_rsv = NULL;
4511 trans->bytes_reserved = 0;
4512 ret = btrfs_end_transaction(trans);
4513 if (ret && !err)
4514 err = ret;
4515 inode->i_flags |= S_DEAD;
4516out_release:
4517 btrfs_subvolume_release_metadata(fs_info, &block_rsv);
4518out_up_write:
4519 up_write(&fs_info->subvol_sem);
4520 if (err) {
4521 spin_lock(&dest->root_item_lock);
4522 root_flags = btrfs_root_flags(&dest->root_item);
4523 btrfs_set_root_flags(&dest->root_item,
4524 root_flags & ~BTRFS_ROOT_SUBVOL_DEAD);
4525 spin_unlock(&dest->root_item_lock);
4526 } else {
4527 d_invalidate(dentry);
Nikolay Borisov20a68002018-04-27 14:36:24 +03004528 btrfs_prune_dentries(dest);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004529 ASSERT(dest->send_in_progress == 0);
4530
4531 /* the last ref */
4532 if (dest->ino_cache_inode) {
4533 iput(dest->ino_cache_inode);
4534 dest->ino_cache_inode = NULL;
4535 }
4536 }
4537
4538 return err;
4539}
4540
Chris Mason39279cc2007-06-12 06:35:45 -04004541static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
4542{
David Howells2b0143b2015-03-17 22:25:59 +00004543 struct inode *inode = d_inode(dentry);
Chris Mason1832a6d2007-12-21 16:27:21 -05004544 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004545 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004546 struct btrfs_trans_handle *trans;
Filipe Manana44f714d2016-06-06 16:11:13 +01004547 u64 last_unlink_trans;
Chris Mason39279cc2007-06-12 06:35:45 -04004548
David Sterbab3ae2442012-09-13 16:04:34 -06004549 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Yan134d4512007-10-25 15:49:25 -04004550 return -ENOTEMPTY;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004551 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_FIRST_FREE_OBJECTID)
Misono Tomohiroa79a4642018-04-18 11:35:31 +09004552 return btrfs_delete_subvolume(dir, dentry);
Yan134d4512007-10-25 15:49:25 -04004553
Josef Bacikd52be812013-05-29 14:54:47 -04004554 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004555 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004556 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004557
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004558 if (unlikely(btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Lu Fengqi401b3b12018-08-01 11:32:30 +08004559 err = btrfs_unlink_subvol(trans, dir,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004560 BTRFS_I(inode)->location.objectid,
4561 dentry->d_name.name,
4562 dentry->d_name.len);
4563 goto out;
4564 }
4565
Nikolay Borisov73f2e542017-02-20 13:50:59 +02004566 err = btrfs_orphan_add(trans, BTRFS_I(inode));
Josef Bacik7b128762008-07-24 12:17:14 -04004567 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004568 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004569
Filipe Manana44f714d2016-06-06 16:11:13 +01004570 last_unlink_trans = BTRFS_I(inode)->last_unlink_trans;
4571
Chris Mason39279cc2007-06-12 06:35:45 -04004572 /* now the directory is empty */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004573 err = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
4574 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
4575 dentry->d_name.len);
Filipe Manana44f714d2016-06-06 16:11:13 +01004576 if (!err) {
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004577 btrfs_i_size_write(BTRFS_I(inode), 0);
Filipe Manana44f714d2016-06-06 16:11:13 +01004578 /*
4579 * Propagate the last_unlink_trans value of the deleted dir to
4580 * its parent directory. This is to prevent an unrecoverable
4581 * log tree in the case we do something like this:
4582 * 1) create dir foo
4583 * 2) create snapshot under dir foo
4584 * 3) delete the snapshot
4585 * 4) rmdir foo
4586 * 5) mkdir foo
4587 * 6) fsync foo or some file inside foo
4588 */
4589 if (last_unlink_trans >= trans->transid)
4590 BTRFS_I(dir)->last_unlink_trans = last_unlink_trans;
4591 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004592out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004593 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004594 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39544012007-12-12 14:38:19 -05004595
Chris Mason39279cc2007-06-12 06:35:45 -04004596 return err;
4597}
4598
Josef Bacikddfae632017-10-19 14:16:02 -04004599/*
4600 * Return this if we need to call truncate_block for the last bit of the
4601 * truncate.
4602 */
4603#define NEED_TRUNCATE_BLOCK 1
Filipe Manana0305cd52015-10-16 12:34:25 +01004604
Chris Mason323ac952008-10-01 19:05:46 -04004605/*
Chris Mason39279cc2007-06-12 06:35:45 -04004606 * this can truncate away extent items, csum items and directory items.
4607 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04004608 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04004609 *
4610 * csum items that cross the new i_size are truncated to the new size
4611 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04004612 *
4613 * min_type is the minimum key type to truncate down to. If set to 0, this
4614 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04004615 */
Yan, Zheng80825102009-11-12 09:35:36 +00004616int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
4617 struct btrfs_root *root,
4618 struct inode *inode,
4619 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004620{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004621 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04004622 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004623 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004624 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00004625 struct btrfs_key key;
4626 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04004627 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04004628 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004629 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004630 u64 item_end = 0;
Filipe Mananac1aa4572015-06-20 18:20:09 +01004631 u64 last_size = new_size;
Yan, Zheng80825102009-11-12 09:35:36 +00004632 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04004633 int found_extent;
4634 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05004635 int pending_del_nr = 0;
4636 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04004637 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00004638 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004639 u64 ino = btrfs_ino(BTRFS_I(inode));
Chris Mason28ed1342014-12-17 09:41:04 -08004640 u64 bytes_deleted = 0;
Thomas Meyer897ca812017-10-07 16:02:21 +02004641 bool be_nice = false;
4642 bool should_throttle = false;
Yan, Zheng80825102009-11-12 09:35:36 +00004643
4644 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04004645
Chris Mason28ed1342014-12-17 09:41:04 -08004646 /*
4647 * for non-free space inodes and ref cows, we want to back off from
4648 * time to time
4649 */
Nikolay Borisov70ddc552017-02-20 13:50:35 +02004650 if (!btrfs_is_free_space_inode(BTRFS_I(inode)) &&
Chris Mason28ed1342014-12-17 09:41:04 -08004651 test_bit(BTRFS_ROOT_REF_COWS, &root->state))
Thomas Meyer897ca812017-10-07 16:02:21 +02004652 be_nice = true;
Chris Mason28ed1342014-12-17 09:41:04 -08004653
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004654 path = btrfs_alloc_path();
4655 if (!path)
4656 return -ENOMEM;
David Sterbae4058b52015-11-27 16:31:35 +01004657 path->reada = READA_BACK;
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004658
Josef Bacik5dc562c2012-08-17 13:14:17 -04004659 /*
4660 * We want to drop from the next block forward in case this new size is
4661 * not block aligned since we will be keeping the last block of the
4662 * extent just the way it is.
4663 */
Miao Xie27cdeb72014-04-02 19:51:05 +08004664 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004665 root == fs_info->tree_root)
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02004666 btrfs_drop_extent_cache(BTRFS_I(inode), ALIGN(new_size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004667 fs_info->sectorsize),
Jeff Mahoneyda170662016-06-15 09:22:56 -04004668 (u64)-1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00004669
Miao Xie16cdcec2011-04-22 18:12:22 +08004670 /*
4671 * This function is also used to drop the items in the log tree before
4672 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
Andrea Gelmini52042d82018-11-28 12:05:13 +01004673 * it is used to drop the logged items. So we shouldn't kill the delayed
Miao Xie16cdcec2011-04-22 18:12:22 +08004674 * items.
4675 */
4676 if (min_type == 0 && root == BTRFS_I(inode)->root)
Nikolay Borisov4ccb5c72017-01-10 20:35:38 +02004677 btrfs_kill_delayed_inode_items(BTRFS_I(inode));
Miao Xie16cdcec2011-04-22 18:12:22 +08004678
Li Zefan33345d012011-04-20 10:31:50 +08004679 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04004680 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04004681 key.type = (u8)-1;
4682
Chris Mason85e21ba2008-01-29 15:11:36 -05004683search_again:
Chris Mason28ed1342014-12-17 09:41:04 -08004684 /*
4685 * with a 16K leaf size and 128MB extents, you can actually queue
4686 * up a huge file in a single leaf. Most of the time that
4687 * bytes_deleted is > 0, it will be huge by the time we get here
4688 */
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004689 if (be_nice && bytes_deleted > SZ_32M &&
4690 btrfs_should_end_transaction(trans)) {
4691 ret = -EAGAIN;
Yan, Zheng80825102009-11-12 09:35:36 +00004692 goto out;
4693 }
Chris Masond3977122009-01-05 21:25:51 -05004694
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004695 path->leave_spinning = 1;
4696 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
4697 if (ret < 0)
4698 goto out;
4699
Chris Mason85e21ba2008-01-29 15:11:36 -05004700 if (ret > 0) {
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004701 ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -04004702 /* there are no items in the tree for us to truncate, we're
4703 * done
4704 */
Yan, Zheng80825102009-11-12 09:35:36 +00004705 if (path->slots[0] == 0)
4706 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05004707 path->slots[0]--;
4708 }
4709
Chris Masond3977122009-01-05 21:25:51 -05004710 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04004711 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04004712 leaf = path->nodes[0];
4713 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02004714 found_type = found_key.type;
Chris Mason39279cc2007-06-12 06:35:45 -04004715
Li Zefan33345d012011-04-20 10:31:50 +08004716 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04004717 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004718
Chris Mason85e21ba2008-01-29 15:11:36 -05004719 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004720 break;
4721
Chris Mason5f39d392007-10-15 16:14:19 -04004722 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04004723 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04004724 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04004725 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04004726 extent_type = btrfs_file_extent_type(leaf, fi);
4727 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04004728 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04004729 btrfs_file_extent_num_bytes(leaf, fi);
Liu Bo09ed2f12017-03-10 11:09:48 -08004730
4731 trace_btrfs_truncate_show_fi_regular(
4732 BTRFS_I(inode), leaf, fi,
4733 found_key.offset);
Chris Mason179e29e2007-11-01 11:28:41 -04004734 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Qu Wenruoe41ca582018-06-06 15:41:49 +08004735 item_end += btrfs_file_extent_ram_bytes(leaf,
4736 fi);
Liu Bo09ed2f12017-03-10 11:09:48 -08004737
4738 trace_btrfs_truncate_show_fi_inline(
4739 BTRFS_I(inode), leaf, fi, path->slots[0],
4740 found_key.offset);
Chris Mason39279cc2007-06-12 06:35:45 -04004741 }
Yan008630c2007-11-07 13:31:09 -05004742 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04004743 }
Yan, Zheng80825102009-11-12 09:35:36 +00004744 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04004745 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00004746 } else {
Filipe Manana76b42ab2017-02-14 16:56:01 +00004747 if (item_end < new_size)
Yan, Zheng80825102009-11-12 09:35:36 +00004748 break;
4749 if (found_key.offset >= new_size)
4750 del_item = 1;
4751 else
4752 del_item = 0;
4753 }
Chris Mason39279cc2007-06-12 06:35:45 -04004754 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004755 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04004756 if (found_type != BTRFS_EXTENT_DATA_KEY)
4757 goto delete;
4758
4759 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04004760 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04004761 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05004762 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04004763 u64 orig_num_bytes =
4764 btrfs_file_extent_num_bytes(leaf, fi);
Qu Wenruofda28322013-02-26 08:10:22 +00004765 extent_num_bytes = ALIGN(new_size -
4766 found_key.offset,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004767 fs_info->sectorsize);
Chris Masondb945352007-10-15 16:15:53 -04004768 btrfs_set_file_extent_num_bytes(leaf, fi,
4769 extent_num_bytes);
4770 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05004771 extent_num_bytes);
Miao Xie27cdeb72014-04-02 19:51:05 +08004772 if (test_bit(BTRFS_ROOT_REF_COWS,
4773 &root->state) &&
4774 extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004775 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04004776 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004777 } else {
Chris Masondb945352007-10-15 16:15:53 -04004778 extent_num_bytes =
4779 btrfs_file_extent_disk_num_bytes(leaf,
4780 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004781 extent_offset = found_key.offset -
4782 btrfs_file_extent_offset(leaf, fi);
4783
Chris Mason39279cc2007-06-12 06:35:45 -04004784 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05004785 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004786 if (extent_start != 0) {
4787 found_extent = 1;
Miao Xie27cdeb72014-04-02 19:51:05 +08004788 if (test_bit(BTRFS_ROOT_REF_COWS,
4789 &root->state))
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004790 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04004791 }
4792 }
Chris Mason90692182008-02-08 13:49:28 -05004793 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04004794 /*
4795 * we can't truncate inline items that have had
4796 * special encodings
4797 */
4798 if (!del_item &&
Chris Masonc8b97812008-10-29 14:49:59 -04004799 btrfs_file_extent_encryption(leaf, fi) == 0 &&
Josef Bacikddfae632017-10-19 14:16:02 -04004800 btrfs_file_extent_other_encoding(leaf, fi) == 0 &&
4801 btrfs_file_extent_compression(leaf, fi) == 0) {
4802 u32 size = (u32)(new_size - found_key.offset);
Chris Mason514ac8a2014-01-03 21:07:00 -08004803
Josef Bacikddfae632017-10-19 14:16:02 -04004804 btrfs_set_file_extent_ram_bytes(leaf, fi, size);
4805 size = btrfs_file_extent_calc_inline_size(size);
David Sterba78ac4f92019-03-20 14:49:12 +01004806 btrfs_truncate_item(path, size, 1);
Josef Bacikddfae632017-10-19 14:16:02 -04004807 } else if (!del_item) {
Chris Mason514ac8a2014-01-03 21:07:00 -08004808 /*
Josef Bacikddfae632017-10-19 14:16:02 -04004809 * We have to bail so the last_size is set to
4810 * just before this extent.
Chris Mason514ac8a2014-01-03 21:07:00 -08004811 */
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004812 ret = NEED_TRUNCATE_BLOCK;
Josef Bacikddfae632017-10-19 14:16:02 -04004813 break;
Chris Mason90692182008-02-08 13:49:28 -05004814 }
Josef Bacikddfae632017-10-19 14:16:02 -04004815
4816 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state))
4817 inode_sub_bytes(inode, item_end + 1 - new_size);
Chris Mason39279cc2007-06-12 06:35:45 -04004818 }
Chris Mason179e29e2007-11-01 11:28:41 -04004819delete:
Josef Bacikddfae632017-10-19 14:16:02 -04004820 if (del_item)
4821 last_size = found_key.offset;
4822 else
4823 last_size = new_size;
Chris Mason39279cc2007-06-12 06:35:45 -04004824 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05004825 if (!pending_del_nr) {
4826 /* no pending yet, add ourselves */
4827 pending_del_slot = path->slots[0];
4828 pending_del_nr = 1;
4829 } else if (pending_del_nr &&
4830 path->slots[0] + 1 == pending_del_slot) {
4831 /* hop on the pending chunk */
4832 pending_del_nr++;
4833 pending_del_slot = path->slots[0];
4834 } else {
Chris Masond3977122009-01-05 21:25:51 -05004835 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05004836 }
Chris Mason39279cc2007-06-12 06:35:45 -04004837 } else {
4838 break;
4839 }
Thomas Meyer897ca812017-10-07 16:02:21 +02004840 should_throttle = false;
Chris Mason28f75a02015-02-04 06:59:29 -08004841
Miao Xie27cdeb72014-04-02 19:51:05 +08004842 if (found_extent &&
4843 (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004844 root == fs_info->tree_root)) {
Qu Wenruoffd4bb22019-04-04 14:45:36 +08004845 struct btrfs_ref ref = { 0 };
4846
Chris Masonb9473432009-03-13 11:00:37 -04004847 btrfs_set_path_blocking(path);
Chris Mason28ed1342014-12-17 09:41:04 -08004848 bytes_deleted += extent_num_bytes;
Qu Wenruoffd4bb22019-04-04 14:45:36 +08004849
4850 btrfs_init_generic_ref(&ref, BTRFS_DROP_DELAYED_REF,
4851 extent_start, extent_num_bytes, 0);
4852 ref.real_root = root->root_key.objectid;
4853 btrfs_init_data_ref(&ref, btrfs_header_owner(leaf),
4854 ino, extent_offset);
4855 ret = btrfs_free_extent(trans, &ref);
Omar Sandoval05522102018-05-11 13:13:31 -07004856 if (ret) {
4857 btrfs_abort_transaction(trans, ret);
4858 break;
4859 }
Chris Mason28f75a02015-02-04 06:59:29 -08004860 if (be_nice) {
Lu Fengqi7c861622018-10-11 13:40:36 +08004861 if (btrfs_should_throttle_delayed_refs(trans))
Thomas Meyer897ca812017-10-07 16:02:21 +02004862 should_throttle = true;
Chris Mason28f75a02015-02-04 06:59:29 -08004863 }
Chris Mason39279cc2007-06-12 06:35:45 -04004864 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004865
Yan, Zheng80825102009-11-12 09:35:36 +00004866 if (found_type == BTRFS_INODE_ITEM_KEY)
4867 break;
4868
4869 if (path->slots[0] == 0 ||
Josef Bacik12621332015-02-03 07:50:16 -08004870 path->slots[0] != pending_del_slot ||
Josef Bacik28bad212018-12-03 10:20:38 -05004871 should_throttle) {
Yan, Zheng80825102009-11-12 09:35:36 +00004872 if (pending_del_nr) {
4873 ret = btrfs_del_items(trans, root, path,
4874 pending_del_slot,
4875 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004876 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004877 btrfs_abort_transaction(trans, ret);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004878 break;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004879 }
Yan, Zheng80825102009-11-12 09:35:36 +00004880 pending_del_nr = 0;
4881 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004882 btrfs_release_path(path);
Josef Bacik28bad212018-12-03 10:20:38 -05004883
Chris Mason28f75a02015-02-04 06:59:29 -08004884 /*
Josef Bacik28bad212018-12-03 10:20:38 -05004885 * We can generate a lot of delayed refs, so we need to
4886 * throttle every once and a while and make sure we're
4887 * adding enough space to keep up with the work we are
4888 * generating. Since we hold a transaction here we
4889 * can't flush, and we don't want to FLUSH_LIMIT because
4890 * we could have generated too many delayed refs to
4891 * actually allocate, so just bail if we're short and
4892 * let the normal reservation dance happen higher up.
Chris Mason28f75a02015-02-04 06:59:29 -08004893 */
Josef Bacik28bad212018-12-03 10:20:38 -05004894 if (should_throttle) {
4895 ret = btrfs_delayed_refs_rsv_refill(fs_info,
4896 BTRFS_RESERVE_NO_FLUSH);
4897 if (ret) {
4898 ret = -EAGAIN;
4899 break;
4900 }
Chris Mason28f75a02015-02-04 06:59:29 -08004901 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004902 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00004903 } else {
4904 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05004905 }
Chris Mason39279cc2007-06-12 06:35:45 -04004906 }
Yan, Zheng80825102009-11-12 09:35:36 +00004907out:
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004908 if (ret >= 0 && pending_del_nr) {
4909 int err;
4910
4911 err = btrfs_del_items(trans, root, path, pending_del_slot,
Chris Mason85e21ba2008-01-29 15:11:36 -05004912 pending_del_nr);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004913 if (err) {
4914 btrfs_abort_transaction(trans, err);
4915 ret = err;
4916 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004917 }
Filipe Manana76b42ab2017-02-14 16:56:01 +00004918 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
4919 ASSERT(last_size >= new_size);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004920 if (!ret && last_size > new_size)
Filipe Manana76b42ab2017-02-14 16:56:01 +00004921 last_size = new_size;
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004922 btrfs_ordered_update_i_size(inode, last_size, NULL);
Filipe Manana76b42ab2017-02-14 16:56:01 +00004923 }
Chris Mason28ed1342014-12-17 09:41:04 -08004924
Chris Mason39279cc2007-06-12 06:35:45 -04004925 btrfs_free_path(path);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004926 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04004927}
4928
Chris Masona52d9a82007-08-27 16:49:44 -04004929/*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304930 * btrfs_truncate_block - read, zero a chunk and write a block
Josef Bacik2aaa6652012-08-29 14:27:18 -04004931 * @inode - inode that we're zeroing
4932 * @from - the offset to start zeroing
4933 * @len - the length to zero, 0 to zero the entire range respective to the
4934 * offset
4935 * @front - zero up to the offset instead of from the offset on
4936 *
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304937 * This will find the block for the "from" offset and cow the block and zero the
Josef Bacik2aaa6652012-08-29 14:27:18 -04004938 * part we want to zero. This is used with truncate and hole punching.
Chris Masona52d9a82007-08-27 16:49:44 -04004939 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304940int btrfs_truncate_block(struct inode *inode, loff_t from, loff_t len,
Josef Bacik2aaa6652012-08-29 14:27:18 -04004941 int front)
Chris Masona52d9a82007-08-27 16:49:44 -04004942{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004943 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004944 struct address_space *mapping = inode->i_mapping;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004945 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4946 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004947 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08004948 struct extent_changeset *data_reserved = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004949 char *kaddr;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004950 u32 blocksize = fs_info->sectorsize;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004951 pgoff_t index = from >> PAGE_SHIFT;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304952 unsigned offset = from & (blocksize - 1);
Chris Masona52d9a82007-08-27 16:49:44 -04004953 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004954 gfp_t mask = btrfs_alloc_write_mask(mapping);
Chris Masona52d9a82007-08-27 16:49:44 -04004955 int ret = 0;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304956 u64 block_start;
4957 u64 block_end;
Chris Masona52d9a82007-08-27 16:49:44 -04004958
Nikolay Borisovb03ebd92018-01-18 14:47:06 +02004959 if (IS_ALIGNED(offset, blocksize) &&
4960 (!len || IS_ALIGNED(len, blocksize)))
Chris Masona52d9a82007-08-27 16:49:44 -04004961 goto out;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304962
Josef Bacik8b62f872017-10-19 14:15:55 -04004963 block_start = round_down(from, blocksize);
4964 block_end = block_start + blocksize - 1;
4965
Qu Wenruo364ecf32017-02-27 15:10:38 +08004966 ret = btrfs_delalloc_reserve_space(inode, &data_reserved,
Josef Bacik8b62f872017-10-19 14:15:55 -04004967 block_start, blocksize);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004968 if (ret)
4969 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004970
Chris Mason211c17f2008-05-15 09:13:45 -04004971again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004972 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004973 if (!page) {
Qu Wenruobc42bda2017-02-27 15:10:39 +08004974 btrfs_delalloc_release_space(inode, data_reserved,
Qu Wenruo43b18592017-12-12 15:34:32 +08004975 block_start, blocksize, true);
Qu Wenruo8702ba92019-10-14 14:34:51 +08004976 btrfs_delalloc_release_extents(BTRFS_I(inode), blocksize);
Miao Xieac6a2b32012-12-05 10:56:13 +00004977 ret = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -04004978 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04004979 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004980
Chris Masona52d9a82007-08-27 16:49:44 -04004981 if (!PageUptodate(page)) {
4982 ret = btrfs_readpage(NULL, page);
4983 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004984 if (page->mapping != mapping) {
4985 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004986 put_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004987 goto again;
4988 }
Chris Masona52d9a82007-08-27 16:49:44 -04004989 if (!PageUptodate(page)) {
4990 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04004991 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04004992 }
4993 }
Chris Mason211c17f2008-05-15 09:13:45 -04004994 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004995
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304996 lock_extent_bits(io_tree, block_start, block_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004997 set_page_extent_mapped(page);
4998
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304999 ordered = btrfs_lookup_ordered_extent(inode, block_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04005000 if (ordered) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05305001 unlock_extent_cached(io_tree, block_start, block_end,
David Sterbae43bbe52017-12-12 21:43:52 +01005002 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04005003 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005004 put_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04005005 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04005006 btrfs_put_ordered_extent(ordered);
5007 goto again;
5008 }
5009
Chandan Rajendra9703fef2016-01-21 15:55:56 +05305010 clear_extent_bit(&BTRFS_I(inode)->io_tree, block_start, block_end,
Omar Sandovale1821632019-08-15 14:04:04 -07005011 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
5012 0, 0, &cached_state);
Josef Bacik5d5e1032009-10-13 16:46:49 -04005013
Filipe Mananae3b8a482017-11-04 00:16:59 +00005014 ret = btrfs_set_extent_delalloc(inode, block_start, block_end, 0,
Nikolay Borisov330a5822019-07-17 16:18:17 +03005015 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04005016 if (ret) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05305017 unlock_extent_cached(io_tree, block_start, block_end,
David Sterbae43bbe52017-12-12 21:43:52 +01005018 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04005019 goto out_unlock;
5020 }
5021
Chandan Rajendra9703fef2016-01-21 15:55:56 +05305022 if (offset != blocksize) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04005023 if (!len)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05305024 len = blocksize - offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04005025 kaddr = kmap(page);
Josef Bacik2aaa6652012-08-29 14:27:18 -04005026 if (front)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05305027 memset(kaddr + (block_start - page_offset(page)),
5028 0, offset);
Josef Bacik2aaa6652012-08-29 14:27:18 -04005029 else
Chandan Rajendra9703fef2016-01-21 15:55:56 +05305030 memset(kaddr + (block_start - page_offset(page)) + offset,
5031 0, len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04005032 flush_dcache_page(page);
5033 kunmap(page);
5034 }
Chris Mason247e7432008-07-17 12:53:51 -04005035 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04005036 set_page_dirty(page);
David Sterbae43bbe52017-12-12 21:43:52 +01005037 unlock_extent_cached(io_tree, block_start, block_end, &cached_state);
Chris Mason39279cc2007-06-12 06:35:45 -04005038
Chris Mason89642222008-07-24 09:41:53 -04005039out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04005040 if (ret)
Qu Wenruobc42bda2017-02-27 15:10:39 +08005041 btrfs_delalloc_release_space(inode, data_reserved, block_start,
Qu Wenruo43b18592017-12-12 15:34:32 +08005042 blocksize, true);
Qu Wenruo8702ba92019-10-14 14:34:51 +08005043 btrfs_delalloc_release_extents(BTRFS_I(inode), blocksize);
Chris Mason39279cc2007-06-12 06:35:45 -04005044 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005045 put_page(page);
Chris Mason39279cc2007-06-12 06:35:45 -04005046out:
Qu Wenruo364ecf32017-02-27 15:10:38 +08005047 extent_changeset_free(data_reserved);
Chris Mason39279cc2007-06-12 06:35:45 -04005048 return ret;
5049}
5050
Josef Bacik16e75492013-10-22 12:18:51 -04005051static int maybe_insert_hole(struct btrfs_root *root, struct inode *inode,
5052 u64 offset, u64 len)
5053{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005054 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik16e75492013-10-22 12:18:51 -04005055 struct btrfs_trans_handle *trans;
5056 int ret;
5057
5058 /*
5059 * Still need to make sure the inode looks like it's been updated so
5060 * that any holes get logged if we fsync.
5061 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005062 if (btrfs_fs_incompat(fs_info, NO_HOLES)) {
5063 BTRFS_I(inode)->last_trans = fs_info->generation;
Josef Bacik16e75492013-10-22 12:18:51 -04005064 BTRFS_I(inode)->last_sub_trans = root->log_transid;
5065 BTRFS_I(inode)->last_log_commit = root->last_log_commit;
5066 return 0;
5067 }
5068
5069 /*
5070 * 1 - for the one we're dropping
5071 * 1 - for the one we're adding
5072 * 1 - for updating the inode.
5073 */
5074 trans = btrfs_start_transaction(root, 3);
5075 if (IS_ERR(trans))
5076 return PTR_ERR(trans);
5077
5078 ret = btrfs_drop_extents(trans, root, inode, offset, offset + len, 1);
5079 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04005080 btrfs_abort_transaction(trans, ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005081 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04005082 return ret;
5083 }
5084
David Sterbaf85b7372017-01-20 14:54:07 +01005085 ret = btrfs_insert_file_extent(trans, root, btrfs_ino(BTRFS_I(inode)),
5086 offset, 0, 0, len, 0, len, 0, 0, 0);
Josef Bacik16e75492013-10-22 12:18:51 -04005087 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04005088 btrfs_abort_transaction(trans, ret);
Josef Bacik16e75492013-10-22 12:18:51 -04005089 else
5090 btrfs_update_inode(trans, root, inode);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005091 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04005092 return ret;
5093}
5094
Josef Bacik695a0d02011-03-04 15:46:53 -05005095/*
5096 * This function puts in dummy file extents for the area we're creating a hole
5097 * for. So if we are truncating this file to a larger size we need to insert
5098 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
5099 * the range between oldsize and size
5100 */
Josef Bacika41ad392011-01-31 15:30:16 -05005101int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04005102{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005103 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Yan Zheng9036c102008-10-30 14:19:41 -04005104 struct btrfs_root *root = BTRFS_I(inode)->root;
5105 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04005106 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00005107 struct extent_state *cached_state = NULL;
Josef Bacik5dc562c2012-08-17 13:14:17 -04005108 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005109 u64 hole_start = ALIGN(oldsize, fs_info->sectorsize);
5110 u64 block_end = ALIGN(size, fs_info->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04005111 u64 last_byte;
5112 u64 cur_offset;
5113 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04005114 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04005115
Josef Bacika71754f2013-06-17 17:14:39 -04005116 /*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05305117 * If our size started in the middle of a block we need to zero out the
5118 * rest of the block before we expand the i_size, otherwise we could
Josef Bacika71754f2013-06-17 17:14:39 -04005119 * expose stale data.
5120 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05305121 err = btrfs_truncate_block(inode, oldsize, 0, 0);
Josef Bacika71754f2013-06-17 17:14:39 -04005122 if (err)
5123 return err;
5124
Yan Zheng9036c102008-10-30 14:19:41 -04005125 if (size <= hole_start)
5126 return 0;
5127
Nikolay Borisov23d31bd2019-05-07 10:19:23 +03005128 btrfs_lock_and_flush_ordered_range(io_tree, BTRFS_I(inode), hole_start,
5129 block_end - 1, &cached_state);
Yan Zheng9036c102008-10-30 14:19:41 -04005130 cur_offset = hole_start;
5131 while (1) {
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02005132 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, cur_offset,
Yan Zheng9036c102008-10-30 14:19:41 -04005133 block_end - cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005134 if (IS_ERR(em)) {
5135 err = PTR_ERR(em);
Zach Brownf2767952013-01-08 19:37:58 +00005136 em = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005137 break;
5138 }
Yan Zheng9036c102008-10-30 14:19:41 -04005139 last_byte = min(extent_map_end(em), block_end);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005140 last_byte = ALIGN(last_byte, fs_info->sectorsize);
Yan, Zheng80825102009-11-12 09:35:36 +00005141 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04005142 struct extent_map *hole_em;
Yan Zheng9036c102008-10-30 14:19:41 -04005143 hole_size = last_byte - cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00005144
Josef Bacik16e75492013-10-22 12:18:51 -04005145 err = maybe_insert_hole(root, inode, cur_offset,
5146 hole_size);
5147 if (err)
Chris Mason771ed682008-11-06 22:02:51 -05005148 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02005149 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04005150 cur_offset + hole_size - 1, 0);
5151 hole_em = alloc_extent_map();
5152 if (!hole_em) {
5153 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
5154 &BTRFS_I(inode)->runtime_flags);
5155 goto next;
5156 }
5157 hole_em->start = cur_offset;
5158 hole_em->len = hole_size;
5159 hole_em->orig_start = cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00005160
Josef Bacik5dc562c2012-08-17 13:14:17 -04005161 hole_em->block_start = EXTENT_MAP_HOLE;
5162 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05005163 hole_em->orig_block_len = 0;
Josef Bacikcc95bef2013-04-04 14:31:27 -04005164 hole_em->ram_bytes = hole_size;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005165 hole_em->bdev = fs_info->fs_devices->latest_bdev;
Josef Bacik5dc562c2012-08-17 13:14:17 -04005166 hole_em->compress_type = BTRFS_COMPRESS_NONE;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005167 hole_em->generation = fs_info->generation;
Josef Bacik5dc562c2012-08-17 13:14:17 -04005168
5169 while (1) {
5170 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04005171 err = add_extent_mapping(em_tree, hole_em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04005172 write_unlock(&em_tree->lock);
5173 if (err != -EEXIST)
5174 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02005175 btrfs_drop_extent_cache(BTRFS_I(inode),
5176 cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04005177 cur_offset +
5178 hole_size - 1, 0);
5179 }
5180 free_extent_map(hole_em);
Yan Zheng9036c102008-10-30 14:19:41 -04005181 }
Josef Bacik16e75492013-10-22 12:18:51 -04005182next:
Yan Zheng9036c102008-10-30 14:19:41 -04005183 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04005184 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04005185 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00005186 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04005187 break;
5188 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04005189 free_extent_map(em);
David Sterbae43bbe52017-12-12 21:43:52 +01005190 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state);
Yan Zheng9036c102008-10-30 14:19:41 -04005191 return err;
5192}
5193
Eric Sandeen3972f262013-01-12 02:57:22 +00005194static int btrfs_setsize(struct inode *inode, struct iattr *attr)
Yan, Zheng80825102009-11-12 09:35:36 +00005195{
Miao Xief4a2f4c2011-12-14 20:12:01 -05005196 struct btrfs_root *root = BTRFS_I(inode)->root;
5197 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05005198 loff_t oldsize = i_size_read(inode);
Eric Sandeen3972f262013-01-12 02:57:22 +00005199 loff_t newsize = attr->ia_size;
5200 int mask = attr->ia_valid;
Yan, Zheng80825102009-11-12 09:35:36 +00005201 int ret;
5202
Eric Sandeen3972f262013-01-12 02:57:22 +00005203 /*
5204 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
5205 * special case where we need to update the times despite not having
5206 * these flags set. For all other operations the VFS set these flags
5207 * explicitly if it wants a timestamp update.
5208 */
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08005209 if (newsize != oldsize) {
5210 inode_inc_iversion(inode);
5211 if (!(mask & (ATTR_CTIME | ATTR_MTIME)))
5212 inode->i_ctime = inode->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07005213 current_time(inode);
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08005214 }
Eric Sandeen3972f262013-01-12 02:57:22 +00005215
Josef Bacika41ad392011-01-31 15:30:16 -05005216 if (newsize > oldsize) {
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005217 /*
David Sterbaea14b57f2017-06-22 02:19:11 +02005218 * Don't do an expanding truncate while snapshotting is ongoing.
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005219 * This is to ensure the snapshot captures a fully consistent
5220 * state of this file - if the snapshot captures this expanding
5221 * truncation, it must capture all writes that happened before
5222 * this truncation.
5223 */
Zhao Lei0bc19f902016-01-06 18:56:36 +08005224 btrfs_wait_for_snapshot_creation(root);
Josef Bacika41ad392011-01-31 15:30:16 -05005225 ret = btrfs_cont_expand(inode, oldsize, newsize);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005226 if (ret) {
David Sterbaea14b57f2017-06-22 02:19:11 +02005227 btrfs_end_write_no_snapshotting(root);
Yan, Zheng80825102009-11-12 09:35:36 +00005228 return ret;
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005229 }
Yan, Zheng80825102009-11-12 09:35:36 +00005230
Miao Xief4a2f4c2011-12-14 20:12:01 -05005231 trans = btrfs_start_transaction(root, 1);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005232 if (IS_ERR(trans)) {
David Sterbaea14b57f2017-06-22 02:19:11 +02005233 btrfs_end_write_no_snapshotting(root);
Miao Xief4a2f4c2011-12-14 20:12:01 -05005234 return PTR_ERR(trans);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005235 }
Miao Xief4a2f4c2011-12-14 20:12:01 -05005236
5237 i_size_write(inode, newsize);
5238 btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
Chandan Rajendra27772b62016-01-21 15:56:03 +05305239 pagecache_isize_extended(inode, oldsize, newsize);
Miao Xief4a2f4c2011-12-14 20:12:01 -05005240 ret = btrfs_update_inode(trans, root, inode);
David Sterbaea14b57f2017-06-22 02:19:11 +02005241 btrfs_end_write_no_snapshotting(root);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005242 btrfs_end_transaction(trans);
Josef Bacika41ad392011-01-31 15:30:16 -05005243 } else {
Yan, Zheng80825102009-11-12 09:35:36 +00005244
Josef Bacika41ad392011-01-31 15:30:16 -05005245 /*
5246 * We're truncating a file that used to have good data down to
5247 * zero. Make sure it gets into the ordered flush list so that
5248 * any new writes get down to disk quickly.
5249 */
5250 if (newsize == 0)
Josef Bacik72ac3c02012-05-23 14:13:11 -04005251 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
5252 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00005253
Josef Bacika41ad392011-01-31 15:30:16 -05005254 truncate_setsize(inode, newsize);
Miao Xie2e60a512013-02-08 07:01:08 +00005255
Andrea Gelmini52042d82018-11-28 12:05:13 +01005256 /* Disable nonlocked read DIO to avoid the endless truncate */
Nikolay Borisovabcefb12017-02-20 13:51:10 +02005257 btrfs_inode_block_unlocked_dio(BTRFS_I(inode));
Miao Xie2e60a512013-02-08 07:01:08 +00005258 inode_dio_wait(inode);
Nikolay Borisov0b581702017-02-20 13:51:11 +02005259 btrfs_inode_resume_unlocked_dio(BTRFS_I(inode));
Miao Xie2e60a512013-02-08 07:01:08 +00005260
Filipe Manana213e8c52018-02-06 20:40:31 +00005261 ret = btrfs_truncate(inode, newsize == oldsize);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005262 if (ret && inode->i_nlink) {
5263 int err;
5264
5265 /*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07005266 * Truncate failed, so fix up the in-memory size. We
5267 * adjusted disk_i_size down as we removed extents, so
5268 * wait for disk_i_size to be stable and then update the
5269 * in-memory size to match.
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005270 */
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07005271 err = btrfs_wait_ordered_range(inode, 0, (u64)-1);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005272 if (err)
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07005273 return err;
5274 i_size_write(inode, BTRFS_I(inode)->disk_i_size);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005275 }
Yan, Zheng80825102009-11-12 09:35:36 +00005276 }
5277
Josef Bacika41ad392011-01-31 15:30:16 -05005278 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00005279}
5280
Chris Mason39279cc2007-06-12 06:35:45 -04005281static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
5282{
David Howells2b0143b2015-03-17 22:25:59 +00005283 struct inode *inode = d_inode(dentry);
Li Zefanb83cc962010-12-20 16:04:08 +08005284 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005285 int err;
5286
Li Zefanb83cc962010-12-20 16:04:08 +08005287 if (btrfs_root_readonly(root))
5288 return -EROFS;
5289
Jan Kara31051c82016-05-26 16:55:18 +02005290 err = setattr_prepare(dentry, attr);
Chris Mason39279cc2007-06-12 06:35:45 -04005291 if (err)
5292 return err;
5293
Chris Mason5a3f23d2009-03-31 13:27:11 -04005294 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Eric Sandeen3972f262013-01-12 02:57:22 +00005295 err = btrfs_setsize(inode, attr);
Yan, Zheng80825102009-11-12 09:35:36 +00005296 if (err)
5297 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005298 }
Yan Zheng9036c102008-10-30 14:19:41 -04005299
Christoph Hellwig10257742010-06-04 11:30:02 +02005300 if (attr->ia_valid) {
5301 setattr_copy(inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005302 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005303 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04005304
Josef Bacik22c44fe2011-11-30 10:45:38 -05005305 if (!err && attr->ia_valid & ATTR_MODE)
Christoph Hellwig996a7102013-12-20 05:16:43 -08005306 err = posix_acl_chmod(inode, inode->i_mode);
Christoph Hellwig10257742010-06-04 11:30:02 +02005307 }
5308
Chris Mason39279cc2007-06-12 06:35:45 -04005309 return err;
5310}
Chris Mason61295eb2008-01-14 16:24:38 -05005311
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005312/*
5313 * While truncating the inode pages during eviction, we get the VFS calling
5314 * btrfs_invalidatepage() against each page of the inode. This is slow because
5315 * the calls to btrfs_invalidatepage() result in a huge amount of calls to
5316 * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting
5317 * extent_state structures over and over, wasting lots of time.
5318 *
5319 * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all
5320 * those expensive operations on a per page basis and do only the ordered io
5321 * finishing, while we release here the extent_map and extent_state structures,
5322 * without the excessive merging and splitting.
5323 */
5324static void evict_inode_truncate_pages(struct inode *inode)
5325{
5326 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
5327 struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree;
5328 struct rb_node *node;
5329
5330 ASSERT(inode->i_state & I_FREEING);
Johannes Weiner91b0abe2014-04-03 14:47:49 -07005331 truncate_inode_pages_final(&inode->i_data);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005332
5333 write_lock(&map_tree->lock);
Liu Bo07e1ce02018-08-23 03:51:52 +08005334 while (!RB_EMPTY_ROOT(&map_tree->map.rb_root)) {
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005335 struct extent_map *em;
5336
Liu Bo07e1ce02018-08-23 03:51:52 +08005337 node = rb_first_cached(&map_tree->map);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005338 em = rb_entry(node, struct extent_map, rb_node);
Wang Shilong180589e2013-12-14 15:27:31 +08005339 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
5340 clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005341 remove_extent_mapping(map_tree, em);
5342 free_extent_map(em);
Filipe Manana7064dd52014-08-08 02:47:05 +01005343 if (need_resched()) {
5344 write_unlock(&map_tree->lock);
5345 cond_resched();
5346 write_lock(&map_tree->lock);
5347 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005348 }
5349 write_unlock(&map_tree->lock);
5350
Filipe Manana6ca07092015-05-26 00:55:42 +01005351 /*
5352 * Keep looping until we have no more ranges in the io tree.
5353 * We can have ongoing bios started by readpages (called from readahead)
Filipe Manana9c6429d2015-06-10 12:55:41 +01005354 * that have their endio callback (extent_io.c:end_bio_extent_readpage)
5355 * still in progress (unlocked the pages in the bio but did not yet
5356 * unlocked the ranges in the io tree). Therefore this means some
Filipe Manana6ca07092015-05-26 00:55:42 +01005357 * ranges can still be locked and eviction started because before
5358 * submitting those bios, which are executed by a separate task (work
5359 * queue kthread), inode references (inode->i_count) were not taken
5360 * (which would be dropped in the end io callback of each bio).
5361 * Therefore here we effectively end up waiting for those bios and
5362 * anyone else holding locked ranges without having bumped the inode's
5363 * reference count - if we don't do it, when they access the inode's
5364 * io_tree to unlock a range it may be too late, leading to an
5365 * use-after-free issue.
5366 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005367 spin_lock(&io_tree->lock);
5368 while (!RB_EMPTY_ROOT(&io_tree->state)) {
5369 struct extent_state *state;
5370 struct extent_state *cached_state = NULL;
Filipe Manana6ca07092015-05-26 00:55:42 +01005371 u64 start;
5372 u64 end;
Filipe Manana421f0922018-10-12 13:02:48 +01005373 unsigned state_flags;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005374
5375 node = rb_first(&io_tree->state);
5376 state = rb_entry(node, struct extent_state, rb_node);
Filipe Manana6ca07092015-05-26 00:55:42 +01005377 start = state->start;
5378 end = state->end;
Filipe Manana421f0922018-10-12 13:02:48 +01005379 state_flags = state->state;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005380 spin_unlock(&io_tree->lock);
5381
David Sterbaff13db42015-12-03 14:30:40 +01005382 lock_extent_bits(io_tree, start, end, &cached_state);
Qu Wenruob9d0b382015-09-29 10:35:16 +08005383
5384 /*
5385 * If still has DELALLOC flag, the extent didn't reach disk,
5386 * and its reserved space won't be freed by delayed_ref.
5387 * So we need to free its reserved space here.
5388 * (Refer to comment in btrfs_invalidatepage, case 2)
5389 *
5390 * Note, end is the bytenr of last byte, so we need + 1 here.
5391 */
Filipe Manana421f0922018-10-12 13:02:48 +01005392 if (state_flags & EXTENT_DELALLOC)
Qu Wenruobc42bda2017-02-27 15:10:39 +08005393 btrfs_qgroup_free_data(inode, NULL, start, end - start + 1);
Qu Wenruob9d0b382015-09-29 10:35:16 +08005394
Filipe Manana6ca07092015-05-26 00:55:42 +01005395 clear_extent_bit(io_tree, start, end,
Omar Sandovale1821632019-08-15 14:04:04 -07005396 EXTENT_LOCKED | EXTENT_DELALLOC |
5397 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1,
5398 &cached_state);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005399
Filipe Manana7064dd52014-08-08 02:47:05 +01005400 cond_resched();
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005401 spin_lock(&io_tree->lock);
5402 }
5403 spin_unlock(&io_tree->lock);
5404}
5405
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005406static struct btrfs_trans_handle *evict_refill_and_join(struct btrfs_root *root,
Josef Bacikad80cf52018-09-28 07:18:19 -04005407 struct btrfs_block_rsv *rsv)
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005408{
5409 struct btrfs_fs_info *fs_info = root->fs_info;
5410 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
Josef Bacikd3984c92019-08-01 18:19:37 -04005411 struct btrfs_trans_handle *trans;
Josef Bacik2bd36e72019-08-22 15:14:33 -04005412 u64 delayed_refs_extra = btrfs_calc_insert_metadata_size(fs_info, 1);
Josef Bacikd3984c92019-08-01 18:19:37 -04005413 int ret;
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005414
Josef Bacikd3984c92019-08-01 18:19:37 -04005415 /*
5416 * Eviction should be taking place at some place safe because of our
5417 * delayed iputs. However the normal flushing code will run delayed
5418 * iputs, so we cannot use FLUSH_ALL otherwise we'll deadlock.
5419 *
5420 * We reserve the delayed_refs_extra here again because we can't use
5421 * btrfs_start_transaction(root, 0) for the same deadlocky reason as
5422 * above. We reserve our extra bit here because we generate a ton of
5423 * delayed refs activity by truncating.
5424 *
5425 * If we cannot make our reservation we'll attempt to steal from the
5426 * global reserve, because we really want to be able to free up space.
5427 */
5428 ret = btrfs_block_rsv_refill(root, rsv, rsv->size + delayed_refs_extra,
5429 BTRFS_RESERVE_FLUSH_EVICT);
5430 if (ret) {
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005431 /*
5432 * Try to steal from the global reserve if there is space for
5433 * it.
5434 */
Josef Bacikd3984c92019-08-01 18:19:37 -04005435 if (btrfs_check_space_for_delayed_refs(fs_info) ||
5436 btrfs_block_rsv_migrate(global_rsv, rsv, rsv->size, 0)) {
5437 btrfs_warn(fs_info,
5438 "could not allocate space for delete; will truncate on mount");
5439 return ERR_PTR(-ENOSPC);
5440 }
5441 delayed_refs_extra = 0;
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005442 }
Josef Bacikd3984c92019-08-01 18:19:37 -04005443
5444 trans = btrfs_join_transaction(root);
5445 if (IS_ERR(trans))
5446 return trans;
5447
5448 if (delayed_refs_extra) {
5449 trans->block_rsv = &fs_info->trans_block_rsv;
5450 trans->bytes_reserved = delayed_refs_extra;
5451 btrfs_block_rsv_migrate(rsv, trans->block_rsv,
5452 delayed_refs_extra, 1);
5453 }
5454 return trans;
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005455}
5456
Al Virobd555972010-06-07 11:35:40 -04005457void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005458{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005459 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04005460 struct btrfs_trans_handle *trans;
5461 struct btrfs_root *root = BTRFS_I(inode)->root;
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005462 struct btrfs_block_rsv *rsv;
Chris Mason39279cc2007-06-12 06:35:45 -04005463 int ret;
5464
liubo1abe9b82011-03-24 11:18:59 +00005465 trace_btrfs_inode_evict(inode);
5466
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005467 if (!root) {
Liu Boe8f1bc12018-01-25 11:02:53 -07005468 clear_inode(inode);
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005469 return;
5470 }
5471
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005472 evict_inode_truncate_pages(inode);
5473
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005474 if (inode->i_nlink &&
5475 ((btrfs_root_refs(&root->root_item) != 0 &&
5476 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) ||
Nikolay Borisov70ddc552017-02-20 13:50:35 +02005477 btrfs_is_free_space_inode(BTRFS_I(inode))))
Al Virobd555972010-06-07 11:35:40 -04005478 goto no_delete;
5479
Omar Sandoval27919062018-05-11 13:13:37 -07005480 if (is_bad_inode(inode))
Chris Mason39279cc2007-06-12 06:35:45 -04005481 goto no_delete;
Chris Mason5f39d392007-10-15 16:14:19 -04005482
Nikolay Borisov7ab79562017-02-20 13:50:57 +02005483 btrfs_free_io_failure_record(BTRFS_I(inode), 0, (u64)-1);
Miao Xief6124962014-09-12 18:44:04 +08005484
Omar Sandoval7b40b692018-05-11 13:13:33 -07005485 if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags))
Yan, Zhengc71bf092009-11-12 09:34:40 +00005486 goto no_delete;
Yan, Zhengc71bf092009-11-12 09:34:40 +00005487
Yan, Zheng76dda932009-09-21 16:00:26 -04005488 if (inode->i_nlink > 0) {
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005489 BUG_ON(btrfs_root_refs(&root->root_item) != 0 &&
5490 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID);
Yan, Zheng76dda932009-09-21 16:00:26 -04005491 goto no_delete;
5492 }
5493
Nikolay Borisovaa790212017-01-10 20:35:40 +02005494 ret = btrfs_commit_inode_delayed_inode(BTRFS_I(inode));
Omar Sandoval27919062018-05-11 13:13:37 -07005495 if (ret)
Miao Xie0e8c36a2012-12-19 06:59:51 +00005496 goto no_delete;
Miao Xie0e8c36a2012-12-19 06:59:51 +00005497
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005498 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Omar Sandoval27919062018-05-11 13:13:37 -07005499 if (!rsv)
Josef Bacik4289a662011-08-05 13:22:24 -04005500 goto no_delete;
Josef Bacik2bd36e72019-08-22 15:14:33 -04005501 rsv->size = btrfs_calc_metadata_size(fs_info, 1);
Josef Bacikca7e70f2012-08-27 17:48:15 -04005502 rsv->failfast = 1;
Josef Bacik4289a662011-08-05 13:22:24 -04005503
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02005504 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Mason5f39d392007-10-15 16:14:19 -04005505
Yan, Zheng80825102009-11-12 09:35:36 +00005506 while (1) {
Josef Bacikad80cf52018-09-28 07:18:19 -04005507 trans = evict_refill_and_join(root, rsv);
Omar Sandoval27919062018-05-11 13:13:37 -07005508 if (IS_ERR(trans))
5509 goto free_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04005510
5511 trans->block_rsv = rsv;
5512
Yan, Zhengd68fc572010-05-16 10:49:58 -04005513 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Omar Sandoval27919062018-05-11 13:13:37 -07005514 trans->block_rsv = &fs_info->trans_block_rsv;
5515 btrfs_end_transaction(trans);
5516 btrfs_btree_balance_dirty(fs_info);
5517 if (ret && ret != -ENOSPC && ret != -EAGAIN)
5518 goto free_rsv;
5519 else if (!ret)
Yan, Zheng80825102009-11-12 09:35:36 +00005520 break;
Josef Bacik7b128762008-07-24 12:17:14 -04005521 }
5522
Josef Bacik4ef31a42013-08-13 14:10:08 -04005523 /*
Omar Sandoval27919062018-05-11 13:13:37 -07005524 * Errors here aren't a big deal, it just means we leave orphan items in
5525 * the tree. They will be cleaned up on the next mount. If the inode
5526 * number gets reused, cleanup deletes the orphan item without doing
5527 * anything, and unlink reuses the existing orphan item.
5528 *
5529 * If it turns out that we are dropping too many of these, we might want
5530 * to add a mechanism for retrying these after a commit.
Josef Bacik4ef31a42013-08-13 14:10:08 -04005531 */
Josef Bacikad80cf52018-09-28 07:18:19 -04005532 trans = evict_refill_and_join(root, rsv);
Omar Sandoval27919062018-05-11 13:13:37 -07005533 if (!IS_ERR(trans)) {
5534 trans->block_rsv = rsv;
5535 btrfs_orphan_del(trans, BTRFS_I(inode));
5536 trans->block_rsv = &fs_info->trans_block_rsv;
5537 btrfs_end_transaction(trans);
5538 }
Chris Mason85e21ba2008-01-29 15:11:36 -05005539
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005540 if (!(root == fs_info->tree_root ||
Li Zefan581bb052011-04-20 10:06:11 +08005541 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005542 btrfs_return_ino(root, btrfs_ino(BTRFS_I(inode)));
Li Zefan581bb052011-04-20 10:06:11 +08005543
Omar Sandoval27919062018-05-11 13:13:37 -07005544free_rsv:
5545 btrfs_free_block_rsv(fs_info, rsv);
Chris Mason39279cc2007-06-12 06:35:45 -04005546no_delete:
Omar Sandoval27919062018-05-11 13:13:37 -07005547 /*
5548 * If we didn't successfully delete, the orphan item will still be in
5549 * the tree and we'll retry on the next mount. Again, we might also want
5550 * to retry these periodically in the future.
5551 */
Nikolay Borisovf48d1cf2017-01-10 20:35:39 +02005552 btrfs_remove_delayed_node(BTRFS_I(inode));
Jan Karadbd57682012-05-03 14:48:02 +02005553 clear_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005554}
5555
5556/*
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005557 * Return the key found in the dir entry in the location pointer, fill @type
5558 * with BTRFS_FT_*, and return 0.
5559 *
Su Yue005d6712018-03-05 17:13:37 +08005560 * If no dir entries were found, returns -ENOENT.
5561 * If found a corrupted location in dir entry, returns -EUCLEAN.
Chris Mason39279cc2007-06-12 06:35:45 -04005562 */
5563static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005564 struct btrfs_key *location, u8 *type)
Chris Mason39279cc2007-06-12 06:35:45 -04005565{
5566 const char *name = dentry->d_name.name;
5567 int namelen = dentry->d_name.len;
5568 struct btrfs_dir_item *di;
5569 struct btrfs_path *path;
5570 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04005571 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005572
5573 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005574 if (!path)
5575 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05005576
David Sterbaf85b7372017-01-20 14:54:07 +01005577 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(BTRFS_I(dir)),
5578 name, namelen, 0);
Liu Bo3cf50682018-09-12 06:06:26 +08005579 if (IS_ERR_OR_NULL(di)) {
5580 ret = di ? PTR_ERR(di) : -ENOENT;
Su Yue005d6712018-03-05 17:13:37 +08005581 goto out;
5582 }
Chris Masond3977122009-01-05 21:25:51 -05005583
Chris Mason5f39d392007-10-15 16:14:19 -04005584 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Liu Bo56a0e702017-10-30 11:14:38 -06005585 if (location->type != BTRFS_INODE_ITEM_KEY &&
5586 location->type != BTRFS_ROOT_ITEM_KEY) {
Su Yue005d6712018-03-05 17:13:37 +08005587 ret = -EUCLEAN;
Liu Bo56a0e702017-10-30 11:14:38 -06005588 btrfs_warn(root->fs_info,
5589"%s gets something invalid in DIR_ITEM (name %s, directory ino %llu, location(%llu %u %llu))",
5590 __func__, name, btrfs_ino(BTRFS_I(dir)),
5591 location->objectid, location->type, location->offset);
Liu Bo56a0e702017-10-30 11:14:38 -06005592 }
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005593 if (!ret)
5594 *type = btrfs_dir_type(path->nodes[0], di);
Chris Mason39279cc2007-06-12 06:35:45 -04005595out:
Chris Mason39279cc2007-06-12 06:35:45 -04005596 btrfs_free_path(path);
5597 return ret;
5598}
5599
5600/*
5601 * when we hit a tree root in a directory, the btrfs part of the inode
5602 * needs to be changed to reflect the root directory of the tree root. This
5603 * is kind of like crossing a mount point.
5604 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005605static int fixup_tree_root_location(struct btrfs_fs_info *fs_info,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005606 struct inode *dir,
5607 struct dentry *dentry,
5608 struct btrfs_key *location,
5609 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04005610{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005611 struct btrfs_path *path;
5612 struct btrfs_root *new_root;
5613 struct btrfs_root_ref *ref;
5614 struct extent_buffer *leaf;
David Sterba1d4c08e2015-01-02 19:36:14 +01005615 struct btrfs_key key;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005616 int ret;
5617 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005618
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005619 path = btrfs_alloc_path();
5620 if (!path) {
5621 err = -ENOMEM;
5622 goto out;
5623 }
Chris Mason39279cc2007-06-12 06:35:45 -04005624
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005625 err = -ENOENT;
David Sterba1d4c08e2015-01-02 19:36:14 +01005626 key.objectid = BTRFS_I(dir)->root->root_key.objectid;
5627 key.type = BTRFS_ROOT_REF_KEY;
5628 key.offset = location->objectid;
5629
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005630 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005631 if (ret) {
5632 if (ret < 0)
5633 err = ret;
5634 goto out;
5635 }
Chris Mason39279cc2007-06-12 06:35:45 -04005636
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005637 leaf = path->nodes[0];
5638 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005639 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(BTRFS_I(dir)) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005640 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
5641 goto out;
5642
5643 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
5644 (unsigned long)(ref + 1),
5645 dentry->d_name.len);
5646 if (ret)
5647 goto out;
5648
David Sterbab3b4aa72011-04-21 01:20:15 +02005649 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005650
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005651 new_root = btrfs_read_fs_root_no_name(fs_info, location);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005652 if (IS_ERR(new_root)) {
5653 err = PTR_ERR(new_root);
5654 goto out;
5655 }
5656
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005657 *sub_root = new_root;
5658 location->objectid = btrfs_root_dirid(&new_root->root_item);
5659 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04005660 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005661 err = 0;
5662out:
5663 btrfs_free_path(path);
5664 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005665}
5666
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005667static void inode_tree_add(struct inode *inode)
5668{
5669 struct btrfs_root *root = BTRFS_I(inode)->root;
5670 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005671 struct rb_node **p;
5672 struct rb_node *parent;
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005673 struct rb_node *new = &BTRFS_I(inode)->rb_node;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005674 u64 ino = btrfs_ino(BTRFS_I(inode));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005675
Al Viro1d3382cb2010-10-23 15:19:20 -04005676 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005677 return;
Miao Xiee1409ce2013-05-15 07:48:16 +00005678 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005679 spin_lock(&root->inode_lock);
Miao Xiee1409ce2013-05-15 07:48:16 +00005680 p = &root->inode_tree.rb_node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005681 while (*p) {
5682 parent = *p;
5683 entry = rb_entry(parent, struct btrfs_inode, rb_node);
5684
David Sterba37508512018-06-29 10:56:40 +02005685 if (ino < btrfs_ino(entry))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005686 p = &parent->rb_left;
David Sterba37508512018-06-29 10:56:40 +02005687 else if (ino > btrfs_ino(entry))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005688 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005689 else {
5690 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04005691 (I_WILL_FREE | I_FREEING)));
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005692 rb_replace_node(parent, new, &root->inode_tree);
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005693 RB_CLEAR_NODE(parent);
5694 spin_unlock(&root->inode_lock);
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005695 return;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005696 }
5697 }
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005698 rb_link_node(new, parent, p);
5699 rb_insert_color(new, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005700 spin_unlock(&root->inode_lock);
5701}
5702
5703static void inode_tree_del(struct inode *inode)
5704{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005705 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005706 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04005707 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005708
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005709 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005710 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005711 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005712 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04005713 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005714 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005715 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04005716
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005717 if (empty && btrfs_root_refs(&root->root_item) == 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005718 synchronize_srcu(&fs_info->subvol_srcu);
Yan, Zheng76dda932009-09-21 16:00:26 -04005719 spin_lock(&root->inode_lock);
5720 empty = RB_EMPTY_ROOT(&root->inode_tree);
5721 spin_unlock(&root->inode_lock);
5722 if (empty)
5723 btrfs_add_dead_root(root);
5724 }
5725}
5726
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005727
Chris Masone02119d2008-09-05 16:13:11 -04005728static int btrfs_init_locked_inode(struct inode *inode, void *p)
5729{
5730 struct btrfs_iget_args *args = p;
Chris Mason90d3e592014-01-09 17:28:00 -08005731 inode->i_ino = args->location->objectid;
5732 memcpy(&BTRFS_I(inode)->location, args->location,
5733 sizeof(*args->location));
Chris Masone02119d2008-09-05 16:13:11 -04005734 BTRFS_I(inode)->root = args->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005735 return 0;
5736}
5737
5738static int btrfs_find_actor(struct inode *inode, void *opaque)
5739{
5740 struct btrfs_iget_args *args = opaque;
Chris Mason90d3e592014-01-09 17:28:00 -08005741 return args->location->objectid == BTRFS_I(inode)->location.objectid &&
Chris Masond3977122009-01-05 21:25:51 -05005742 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005743}
5744
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005745static struct inode *btrfs_iget_locked(struct super_block *s,
Chris Mason90d3e592014-01-09 17:28:00 -08005746 struct btrfs_key *location,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005747 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04005748{
5749 struct inode *inode;
5750 struct btrfs_iget_args args;
Chris Mason90d3e592014-01-09 17:28:00 -08005751 unsigned long hashval = btrfs_inode_hash(location->objectid, root);
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005752
Chris Mason90d3e592014-01-09 17:28:00 -08005753 args.location = location;
Chris Mason39279cc2007-06-12 06:35:45 -04005754 args.root = root;
5755
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005756 inode = iget5_locked(s, hashval, btrfs_find_actor,
Chris Mason39279cc2007-06-12 06:35:45 -04005757 btrfs_init_locked_inode,
5758 (void *)&args);
5759 return inode;
5760}
5761
David Sterba4c66e0d2019-10-03 19:09:35 +02005762/*
5763 * Get an inode object given its location and corresponding root.
5764 * Path can be preallocated to prevent recursing back to iget through
5765 * allocator. NULL is also valid but may require an additional allocation
5766 * later.
Balaji Rao1a54ef82008-07-21 02:01:04 +05305767 */
Filipe Manana4222ea72018-10-24 10:13:03 +01005768struct inode *btrfs_iget_path(struct super_block *s, struct btrfs_key *location,
David Sterba4c66e0d2019-10-03 19:09:35 +02005769 struct btrfs_root *root, struct btrfs_path *path)
Balaji Rao1a54ef82008-07-21 02:01:04 +05305770{
5771 struct inode *inode;
5772
Chris Mason90d3e592014-01-09 17:28:00 -08005773 inode = btrfs_iget_locked(s, location, root);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305774 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005775 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305776
5777 if (inode->i_state & I_NEW) {
Filipe Manana67710892016-06-06 11:51:25 +01005778 int ret;
5779
Filipe Manana4222ea72018-10-24 10:13:03 +01005780 ret = btrfs_read_locked_inode(inode, path);
Al Viro9bc2cef2018-07-29 23:04:50 +01005781 if (!ret) {
Mark Fasheh1748f842011-07-12 11:25:31 -07005782 inode_tree_add(inode);
5783 unlock_new_inode(inode);
Mark Fasheh1748f842011-07-12 11:25:31 -07005784 } else {
Al Virof5b3a412018-07-29 23:04:51 +01005785 iget_failed(inode);
5786 /*
5787 * ret > 0 can come from btrfs_search_slot called by
5788 * btrfs_read_locked_inode, this means the inode item
5789 * was not found.
5790 */
5791 if (ret > 0)
5792 ret = -ENOENT;
5793 inode = ERR_PTR(ret);
Mark Fasheh1748f842011-07-12 11:25:31 -07005794 }
5795 }
5796
Balaji Rao1a54ef82008-07-21 02:01:04 +05305797 return inode;
5798}
5799
Filipe Manana4222ea72018-10-24 10:13:03 +01005800struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
David Sterba4c66e0d2019-10-03 19:09:35 +02005801 struct btrfs_root *root)
Filipe Manana4222ea72018-10-24 10:13:03 +01005802{
David Sterba4c66e0d2019-10-03 19:09:35 +02005803 return btrfs_iget_path(s, location, root, NULL);
Filipe Manana4222ea72018-10-24 10:13:03 +01005804}
5805
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005806static struct inode *new_simple_dir(struct super_block *s,
5807 struct btrfs_key *key,
5808 struct btrfs_root *root)
5809{
5810 struct inode *inode = new_inode(s);
5811
5812 if (!inode)
5813 return ERR_PTR(-ENOMEM);
5814
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005815 BTRFS_I(inode)->root = root;
5816 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04005817 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005818
5819 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Li Zefan848cce02012-02-21 17:04:28 +08005820 inode->i_op = &btrfs_dir_ro_inode_operations;
Omar Sandoval1fdf4192017-01-25 17:06:39 -08005821 inode->i_opflags &= ~IOP_XATTR;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005822 inode->i_fop = &simple_dir_operations;
5823 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
Deepa Dinamanic2050a42016-09-14 07:48:06 -07005824 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05305825 inode->i_atime = inode->i_mtime;
5826 inode->i_ctime = inode->i_mtime;
Arnd Bergmannd3c6be6f2018-06-21 18:04:06 +02005827 BTRFS_I(inode)->i_otime = inode->i_mtime;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005828
5829 return inode;
5830}
5831
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005832static inline u8 btrfs_inode_type(struct inode *inode)
5833{
5834 /*
5835 * Compile-time asserts that generic FT_* types still match
5836 * BTRFS_FT_* types
5837 */
5838 BUILD_BUG_ON(BTRFS_FT_UNKNOWN != FT_UNKNOWN);
5839 BUILD_BUG_ON(BTRFS_FT_REG_FILE != FT_REG_FILE);
5840 BUILD_BUG_ON(BTRFS_FT_DIR != FT_DIR);
5841 BUILD_BUG_ON(BTRFS_FT_CHRDEV != FT_CHRDEV);
5842 BUILD_BUG_ON(BTRFS_FT_BLKDEV != FT_BLKDEV);
5843 BUILD_BUG_ON(BTRFS_FT_FIFO != FT_FIFO);
5844 BUILD_BUG_ON(BTRFS_FT_SOCK != FT_SOCK);
5845 BUILD_BUG_ON(BTRFS_FT_SYMLINK != FT_SYMLINK);
5846
5847 return fs_umode_to_ftype(inode->i_mode);
5848}
5849
Chris Mason3de45862008-11-17 21:02:50 -05005850struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04005851{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005852 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masond3977122009-01-05 21:25:51 -05005853 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005854 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005855 struct btrfs_root *sub_root = root;
5856 struct btrfs_key location;
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005857 u8 di_type = 0;
Yan, Zheng76dda932009-09-21 16:00:26 -04005858 int index;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005859 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005860
5861 if (dentry->d_name.len > BTRFS_NAME_LEN)
5862 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04005863
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005864 ret = btrfs_inode_by_name(dir, dentry, &location, &di_type);
Chris Mason39279cc2007-06-12 06:35:45 -04005865 if (ret < 0)
5866 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04005867
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005868 if (location.type == BTRFS_INODE_ITEM_KEY) {
David Sterba4c66e0d2019-10-03 19:09:35 +02005869 inode = btrfs_iget(dir->i_sb, &location, root);
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005870 if (IS_ERR(inode))
5871 return inode;
5872
5873 /* Do extra check against inode mode with di_type */
5874 if (btrfs_inode_type(inode) != di_type) {
5875 btrfs_crit(fs_info,
5876"inode mode mismatch with dir: inode mode=0%o btrfs type=%u dir type=%u",
5877 inode->i_mode, btrfs_inode_type(inode),
5878 di_type);
5879 iput(inode);
5880 return ERR_PTR(-EUCLEAN);
5881 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005882 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04005883 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005884
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005885 index = srcu_read_lock(&fs_info->subvol_srcu);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005886 ret = fixup_tree_root_location(fs_info, dir, dentry,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005887 &location, &sub_root);
5888 if (ret < 0) {
5889 if (ret != -ENOENT)
5890 inode = ERR_PTR(ret);
5891 else
5892 inode = new_simple_dir(dir->i_sb, &location, sub_root);
5893 } else {
David Sterba4c66e0d2019-10-03 19:09:35 +02005894 inode = btrfs_iget(dir->i_sb, &location, sub_root);
Chris Mason39279cc2007-06-12 06:35:45 -04005895 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005896 srcu_read_unlock(&fs_info->subvol_srcu, index);
Yan, Zheng76dda932009-09-21 16:00:26 -04005897
Julia Lawall34d19ba2011-01-24 19:55:19 +00005898 if (!IS_ERR(inode) && root != sub_root) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005899 down_read(&fs_info->cleanup_work_sem);
David Howellsbc98a422017-07-17 08:45:34 +01005900 if (!sb_rdonly(inode->i_sb))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005901 ret = btrfs_orphan_cleanup(sub_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005902 up_read(&fs_info->cleanup_work_sem);
Josef Bacik01cd3362013-06-03 21:39:49 -04005903 if (ret) {
5904 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005905 inode = ERR_PTR(ret);
Josef Bacik01cd3362013-06-03 21:39:49 -04005906 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00005907 }
5908
Chris Mason3de45862008-11-17 21:02:50 -05005909 return inode;
5910}
5911
Nick Pigginfe15ce42011-01-07 17:49:23 +11005912static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04005913{
5914 struct btrfs_root *root;
David Howells2b0143b2015-03-17 22:25:59 +00005915 struct inode *inode = d_inode(dentry);
Yan, Zheng76dda932009-09-21 16:00:26 -04005916
Li Zefan848cce02012-02-21 17:04:28 +08005917 if (!inode && !IS_ROOT(dentry))
David Howells2b0143b2015-03-17 22:25:59 +00005918 inode = d_inode(dentry->d_parent);
Yan, Zheng76dda932009-09-21 16:00:26 -04005919
Li Zefan848cce02012-02-21 17:04:28 +08005920 if (inode) {
5921 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04005922 if (btrfs_root_refs(&root->root_item) == 0)
5923 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08005924
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005925 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Li Zefan848cce02012-02-21 17:04:28 +08005926 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04005927 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005928 return 0;
5929}
5930
Chris Mason3de45862008-11-17 21:02:50 -05005931static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04005932 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05005933{
Al Viro3837d202018-10-10 16:38:27 -04005934 struct inode *inode = btrfs_lookup_dentry(dir, dentry);
Josef Bacika66e7cc2011-09-18 10:34:03 -04005935
Al Viro3837d202018-10-10 16:38:27 -04005936 if (inode == ERR_PTR(-ENOENT))
5937 inode = NULL;
Al Viro41d28bc2014-10-12 22:24:21 -04005938 return d_splice_alias(inode, dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04005939}
5940
Josef Bacik23b5ec72017-07-24 15:14:25 -04005941/*
5942 * All this infrastructure exists because dir_emit can fault, and we are holding
5943 * the tree lock when doing readdir. For now just allocate a buffer and copy
5944 * our information into that, and then dir_emit from the buffer. This is
5945 * similar to what NFS does, only we don't keep the buffer around in pagecache
5946 * because I'm afraid I'll mess that up. Long term we need to make filldir do
5947 * copy_to_user_inatomic so we don't have to worry about page faulting under the
5948 * tree lock.
5949 */
5950static int btrfs_opendir(struct inode *inode, struct file *file)
5951{
5952 struct btrfs_file_private *private;
5953
5954 private = kzalloc(sizeof(struct btrfs_file_private), GFP_KERNEL);
5955 if (!private)
5956 return -ENOMEM;
5957 private->filldir_buf = kzalloc(PAGE_SIZE, GFP_KERNEL);
5958 if (!private->filldir_buf) {
5959 kfree(private);
5960 return -ENOMEM;
5961 }
5962 file->private_data = private;
5963 return 0;
5964}
5965
5966struct dir_entry {
5967 u64 ino;
5968 u64 offset;
5969 unsigned type;
5970 int name_len;
5971};
5972
5973static int btrfs_filldir(void *addr, int entries, struct dir_context *ctx)
5974{
5975 while (entries--) {
5976 struct dir_entry *entry = addr;
5977 char *name = (char *)(entry + 1);
5978
David Sterba92d32172018-04-16 21:10:14 +02005979 ctx->pos = get_unaligned(&entry->offset);
5980 if (!dir_emit(ctx, name, get_unaligned(&entry->name_len),
5981 get_unaligned(&entry->ino),
5982 get_unaligned(&entry->type)))
Josef Bacik23b5ec72017-07-24 15:14:25 -04005983 return 1;
David Sterba92d32172018-04-16 21:10:14 +02005984 addr += sizeof(struct dir_entry) +
5985 get_unaligned(&entry->name_len);
Josef Bacik23b5ec72017-07-24 15:14:25 -04005986 ctx->pos++;
5987 }
5988 return 0;
5989}
5990
Al Viro9cdda8d2013-05-22 16:48:09 -04005991static int btrfs_real_readdir(struct file *file, struct dir_context *ctx)
Chris Mason39279cc2007-06-12 06:35:45 -04005992{
Al Viro9cdda8d2013-05-22 16:48:09 -04005993 struct inode *inode = file_inode(file);
Chris Mason39279cc2007-06-12 06:35:45 -04005994 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005995 struct btrfs_file_private *private = file->private_data;
Chris Mason39279cc2007-06-12 06:35:45 -04005996 struct btrfs_dir_item *di;
5997 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04005998 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04005999 struct btrfs_path *path;
Josef Bacik23b5ec72017-07-24 15:14:25 -04006000 void *addr;
Miao Xie16cdcec2011-04-22 18:12:22 +08006001 struct list_head ins_list;
6002 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04006003 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04006004 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04006005 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04006006 char *name_ptr;
6007 int name_len;
Josef Bacik23b5ec72017-07-24 15:14:25 -04006008 int entries = 0;
6009 int total_len = 0;
Omar Sandoval02dbfc92016-05-20 13:50:33 -07006010 bool put = false;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006011 struct btrfs_key location;
Chris Mason5f39d392007-10-15 16:14:19 -04006012
Al Viro9cdda8d2013-05-22 16:48:09 -04006013 if (!dir_emit_dots(file, ctx))
6014 return 0;
6015
David Woodhouse49593bf2008-08-17 17:08:36 +01006016 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08006017 if (!path)
6018 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04006019
Josef Bacik23b5ec72017-07-24 15:14:25 -04006020 addr = private->filldir_buf;
David Sterbae4058b52015-11-27 16:31:35 +01006021 path->reada = READA_FORWARD;
David Woodhouse49593bf2008-08-17 17:08:36 +01006022
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006023 INIT_LIST_HEAD(&ins_list);
6024 INIT_LIST_HEAD(&del_list);
6025 put = btrfs_readdir_get_delayed_items(inode, &ins_list, &del_list);
Miao Xie16cdcec2011-04-22 18:12:22 +08006026
Josef Bacik23b5ec72017-07-24 15:14:25 -04006027again:
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006028 key.type = BTRFS_DIR_INDEX_KEY;
Al Viro9cdda8d2013-05-22 16:48:09 -04006029 key.offset = ctx->pos;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02006030 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason5f39d392007-10-15 16:14:19 -04006031
Chris Mason39279cc2007-06-12 06:35:45 -04006032 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
6033 if (ret < 0)
6034 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01006035
6036 while (1) {
Josef Bacik23b5ec72017-07-24 15:14:25 -04006037 struct dir_entry *entry;
6038
Chris Mason5f39d392007-10-15 16:14:19 -04006039 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04006040 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08006041 if (slot >= btrfs_header_nritems(leaf)) {
6042 ret = btrfs_next_leaf(root, path);
6043 if (ret < 0)
6044 goto err;
6045 else if (ret > 0)
6046 break;
6047 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04006048 }
Chris Mason3de45862008-11-17 21:02:50 -05006049
Chris Mason5f39d392007-10-15 16:14:19 -04006050 btrfs_item_key_to_cpu(leaf, &found_key, slot);
6051
6052 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04006053 break;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006054 if (found_key.type != BTRFS_DIR_INDEX_KEY)
Chris Mason39279cc2007-06-12 06:35:45 -04006055 break;
Al Viro9cdda8d2013-05-22 16:48:09 -04006056 if (found_key.offset < ctx->pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08006057 goto next;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006058 if (btrfs_should_delete_dir_index(&del_list, found_key.offset))
Miao Xie16cdcec2011-04-22 18:12:22 +08006059 goto next;
Chris Mason39279cc2007-06-12 06:35:45 -04006060 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006061 name_len = btrfs_dir_name_len(leaf, di);
Josef Bacik23b5ec72017-07-24 15:14:25 -04006062 if ((total_len + sizeof(struct dir_entry) + name_len) >=
6063 PAGE_SIZE) {
6064 btrfs_release_path(path);
6065 ret = btrfs_filldir(private->filldir_buf, entries, ctx);
6066 if (ret)
6067 goto nopos;
6068 addr = private->filldir_buf;
6069 entries = 0;
6070 total_len = 0;
6071 goto again;
Chris Mason39279cc2007-06-12 06:35:45 -04006072 }
Josef Bacik23b5ec72017-07-24 15:14:25 -04006073
6074 entry = addr;
David Sterba92d32172018-04-16 21:10:14 +02006075 put_unaligned(name_len, &entry->name_len);
Josef Bacik23b5ec72017-07-24 15:14:25 -04006076 name_ptr = (char *)(entry + 1);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006077 read_extent_buffer(leaf, name_ptr, (unsigned long)(di + 1),
6078 name_len);
Phillip Potter7d157c32019-03-26 21:39:34 +00006079 put_unaligned(fs_ftype_to_dtype(btrfs_dir_type(leaf, di)),
David Sterba92d32172018-04-16 21:10:14 +02006080 &entry->type);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006081 btrfs_dir_item_key_to_cpu(leaf, di, &location);
David Sterba92d32172018-04-16 21:10:14 +02006082 put_unaligned(location.objectid, &entry->ino);
6083 put_unaligned(found_key.offset, &entry->offset);
Josef Bacik23b5ec72017-07-24 15:14:25 -04006084 entries++;
6085 addr += sizeof(struct dir_entry) + name_len;
6086 total_len += sizeof(struct dir_entry) + name_len;
Li Zefanb9e03af2011-03-23 10:43:58 +08006087next:
6088 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04006089 }
Josef Bacik23b5ec72017-07-24 15:14:25 -04006090 btrfs_release_path(path);
6091
6092 ret = btrfs_filldir(private->filldir_buf, entries, ctx);
6093 if (ret)
6094 goto nopos;
David Woodhouse49593bf2008-08-17 17:08:36 +01006095
Jeff Mahoneyd2fbb2b2016-11-05 13:26:35 -04006096 ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006097 if (ret)
David Sterbabc4ef752015-11-13 13:44:28 +01006098 goto nopos;
6099
Zach Browndb62efb2013-07-11 16:19:42 -07006100 /*
6101 * Stop new entries from being returned after we return the last
6102 * entry.
6103 *
6104 * New directory entries are assigned a strictly increasing
6105 * offset. This means that new entries created during readdir
6106 * are *guaranteed* to be seen in the future by that readdir.
6107 * This has broken buggy programs which operate on names as
6108 * they're returned by readdir. Until we re-use freed offsets
6109 * we have this hack to stop new entries from being returned
6110 * under the assumption that they'll never reach this huge
6111 * offset.
6112 *
6113 * This is being careful not to overflow 32bit loff_t unless the
6114 * last entry requires it because doing so has broken 32bit apps
6115 * in the past.
6116 */
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006117 if (ctx->pos >= INT_MAX)
6118 ctx->pos = LLONG_MAX;
6119 else
6120 ctx->pos = INT_MAX;
Chris Mason39279cc2007-06-12 06:35:45 -04006121nopos:
6122 ret = 0;
6123err:
Omar Sandoval02dbfc92016-05-20 13:50:33 -07006124 if (put)
6125 btrfs_readdir_put_delayed_items(inode, &ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04006126 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04006127 return ret;
6128}
6129
Chris Mason39279cc2007-06-12 06:35:45 -04006130/*
Chris Mason54aa1f42007-06-22 14:16:25 -04006131 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04006132 * inode changes. But, it is most likely to find the inode in cache.
6133 * FIXME, needs more benchmarking...there are no reasons other than performance
6134 * to keep or drop this code.
6135 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00006136static int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04006137{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006138 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04006139 struct btrfs_root *root = BTRFS_I(inode)->root;
6140 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006141 int ret;
6142
Josef Bacik72ac3c02012-05-23 14:13:11 -04006143 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05006144 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006145
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006146 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006147 if (IS_ERR(trans))
6148 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006149
6150 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04006151 if (ret && ret == -ENOSPC) {
6152 /* whoops, lets try again with the full transaction */
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006153 btrfs_end_transaction(trans);
Chris Mason94b60442010-05-26 11:02:00 -04006154 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006155 if (IS_ERR(trans))
6156 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006157
Chris Mason94b60442010-05-26 11:02:00 -04006158 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04006159 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006160 btrfs_end_transaction(trans);
Miao Xie16cdcec2011-04-22 18:12:22 +08006161 if (BTRFS_I(inode)->delayed_node)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006162 btrfs_balance_delayed_items(fs_info);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006163
6164 return ret;
6165}
6166
6167/*
6168 * This is a copy of file_update_time. We need this so we can return error on
6169 * ENOSPC for updating the inode in the case of file write and mmap writes.
6170 */
Deepa Dinamani95582b02018-05-08 19:36:02 -07006171static int btrfs_update_time(struct inode *inode, struct timespec64 *now,
Josef Bacike41f9412012-03-26 09:46:47 -04006172 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05006173{
Alexander Block2bc5565282012-06-15 09:49:33 +02006174 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Layton3a8c7232017-12-11 06:35:24 -05006175 bool dirty = flags & ~S_VERSION;
Alexander Block2bc5565282012-06-15 09:49:33 +02006176
6177 if (btrfs_root_readonly(root))
6178 return -EROFS;
6179
Josef Bacike41f9412012-03-26 09:46:47 -04006180 if (flags & S_VERSION)
Jeff Layton3a8c7232017-12-11 06:35:24 -05006181 dirty |= inode_maybe_inc_iversion(inode, dirty);
Josef Bacike41f9412012-03-26 09:46:47 -04006182 if (flags & S_CTIME)
6183 inode->i_ctime = *now;
6184 if (flags & S_MTIME)
6185 inode->i_mtime = *now;
6186 if (flags & S_ATIME)
6187 inode->i_atime = *now;
Jeff Layton3a8c7232017-12-11 06:35:24 -05006188 return dirty ? btrfs_dirty_inode(inode) : 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006189}
6190
Chris Masond352ac62008-09-29 15:18:18 -04006191/*
6192 * find the highest existing sequence number in a directory
6193 * and then set the in-memory index_cnt variable to reflect
6194 * free sequence numbers
6195 */
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006196static int btrfs_set_inode_index_count(struct btrfs_inode *inode)
Josef Bacikaec74772008-07-24 12:12:38 -04006197{
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006198 struct btrfs_root *root = inode->root;
Josef Bacikaec74772008-07-24 12:12:38 -04006199 struct btrfs_key key, found_key;
6200 struct btrfs_path *path;
6201 struct extent_buffer *leaf;
6202 int ret;
6203
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006204 key.objectid = btrfs_ino(inode);
David Sterba962a2982014-06-04 18:41:45 +02006205 key.type = BTRFS_DIR_INDEX_KEY;
Josef Bacikaec74772008-07-24 12:12:38 -04006206 key.offset = (u64)-1;
6207
6208 path = btrfs_alloc_path();
6209 if (!path)
6210 return -ENOMEM;
6211
6212 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
6213 if (ret < 0)
6214 goto out;
6215 /* FIXME: we should be able to handle this */
6216 if (ret == 0)
6217 goto out;
6218 ret = 0;
6219
6220 /*
6221 * MAGIC NUMBER EXPLANATION:
6222 * since we search a directory based on f_pos we have to start at 2
6223 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
6224 * else has to start at 2
6225 */
6226 if (path->slots[0] == 0) {
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006227 inode->index_cnt = 2;
Josef Bacikaec74772008-07-24 12:12:38 -04006228 goto out;
6229 }
6230
6231 path->slots[0]--;
6232
6233 leaf = path->nodes[0];
6234 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6235
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006236 if (found_key.objectid != btrfs_ino(inode) ||
David Sterba962a2982014-06-04 18:41:45 +02006237 found_key.type != BTRFS_DIR_INDEX_KEY) {
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006238 inode->index_cnt = 2;
Josef Bacikaec74772008-07-24 12:12:38 -04006239 goto out;
6240 }
6241
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006242 inode->index_cnt = found_key.offset + 1;
Josef Bacikaec74772008-07-24 12:12:38 -04006243out:
6244 btrfs_free_path(path);
6245 return ret;
6246}
6247
Chris Masond352ac62008-09-29 15:18:18 -04006248/*
6249 * helper to find a free sequence number in a given directory. This current
6250 * code is very simple, later versions will do smarter things in the btree
6251 */
Nikolay Borisov877574e2017-02-20 13:50:33 +02006252int btrfs_set_inode_index(struct btrfs_inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04006253{
6254 int ret = 0;
6255
Nikolay Borisov877574e2017-02-20 13:50:33 +02006256 if (dir->index_cnt == (u64)-1) {
6257 ret = btrfs_inode_delayed_dir_index_count(dir);
Miao Xie16cdcec2011-04-22 18:12:22 +08006258 if (ret) {
6259 ret = btrfs_set_inode_index_count(dir);
6260 if (ret)
6261 return ret;
6262 }
Josef Bacikaec74772008-07-24 12:12:38 -04006263 }
6264
Nikolay Borisov877574e2017-02-20 13:50:33 +02006265 *index = dir->index_cnt;
6266 dir->index_cnt++;
Josef Bacikaec74772008-07-24 12:12:38 -04006267
6268 return ret;
6269}
6270
Chris Masonb0d5d102014-09-08 13:08:51 -07006271static int btrfs_insert_inode_locked(struct inode *inode)
6272{
6273 struct btrfs_iget_args args;
6274 args.location = &BTRFS_I(inode)->location;
6275 args.root = BTRFS_I(inode)->root;
6276
6277 return insert_inode_locked4(inode,
6278 btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root),
6279 btrfs_find_actor, &args);
6280}
6281
Anand Jain19aee8d2017-07-18 17:37:05 +08006282/*
6283 * Inherit flags from the parent inode.
6284 *
6285 * Currently only the compression flags and the cow flags are inherited.
6286 */
6287static void btrfs_inherit_iflags(struct inode *inode, struct inode *dir)
6288{
6289 unsigned int flags;
6290
6291 if (!dir)
6292 return;
6293
6294 flags = BTRFS_I(dir)->flags;
6295
6296 if (flags & BTRFS_INODE_NOCOMPRESS) {
6297 BTRFS_I(inode)->flags &= ~BTRFS_INODE_COMPRESS;
6298 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
6299 } else if (flags & BTRFS_INODE_COMPRESS) {
6300 BTRFS_I(inode)->flags &= ~BTRFS_INODE_NOCOMPRESS;
6301 BTRFS_I(inode)->flags |= BTRFS_INODE_COMPRESS;
6302 }
6303
6304 if (flags & BTRFS_INODE_NODATACOW) {
6305 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
6306 if (S_ISREG(inode->i_mode))
6307 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
6308 }
6309
David Sterba7b6a2212018-03-26 18:40:21 +02006310 btrfs_sync_inode_flags_to_i_flags(inode);
Anand Jain19aee8d2017-07-18 17:37:05 +08006311}
6312
Chris Mason39279cc2007-06-12 06:35:45 -04006313static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
6314 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04006315 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05006316 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04006317 u64 ref_objectid, u64 objectid,
6318 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04006319{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006320 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04006321 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006322 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04006323 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04006324 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05006325 struct btrfs_inode_ref *ref;
6326 struct btrfs_key key[2];
6327 u32 sizes[2];
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006328 int nitems = name ? 2 : 1;
Chris Mason9c583092008-01-29 15:15:18 -05006329 unsigned long ptr;
Josef Bacik11a19a92019-09-09 10:12:04 -04006330 unsigned int nofs_flag;
Chris Mason39279cc2007-06-12 06:35:45 -04006331 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006332
Chris Mason5f39d392007-10-15 16:14:19 -04006333 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07006334 if (!path)
6335 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04006336
Josef Bacik11a19a92019-09-09 10:12:04 -04006337 nofs_flag = memalloc_nofs_save();
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006338 inode = new_inode(fs_info->sb);
Josef Bacik11a19a92019-09-09 10:12:04 -04006339 memalloc_nofs_restore(nofs_flag);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006340 if (!inode) {
6341 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04006342 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006343 }
Chris Mason39279cc2007-06-12 06:35:45 -04006344
Li Zefan581bb052011-04-20 10:06:11 +08006345 /*
Filipe Manana5762b5c2014-08-01 00:10:32 +01006346 * O_TMPFILE, set link count to 0, so that after this point,
6347 * we fill in an inode item with the correct link count.
6348 */
6349 if (!name)
6350 set_nlink(inode, 0);
6351
6352 /*
Li Zefan581bb052011-04-20 10:06:11 +08006353 * we have to initialize this early, so we can reclaim the inode
6354 * number if we fail afterwards in this function.
6355 */
6356 inode->i_ino = objectid;
6357
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006358 if (dir && name) {
liubo1abe9b82011-03-24 11:18:59 +00006359 trace_btrfs_inode_request(dir);
6360
Nikolay Borisov877574e2017-02-20 13:50:33 +02006361 ret = btrfs_set_inode_index(BTRFS_I(dir), index);
Shen Feng09771432009-04-02 16:46:06 -04006362 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006363 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04006364 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04006365 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04006366 }
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006367 } else if (dir) {
6368 *index = 0;
Josef Bacikaec74772008-07-24 12:12:38 -04006369 }
6370 /*
6371 * index_cnt is ignored for everything but a dir,
Su Yuedf6703e2018-01-12 11:08:02 +08006372 * btrfs_set_inode_index_count has an explanation for the magic
Josef Bacikaec74772008-07-24 12:12:38 -04006373 * number
6374 */
6375 BTRFS_I(inode)->index_cnt = 2;
Miao Xie67de1172013-12-26 13:07:06 +08006376 BTRFS_I(inode)->dir_index = *index;
Chris Mason39279cc2007-06-12 06:35:45 -04006377 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04006378 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00006379 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db22007-08-27 16:49:44 -04006380
Josef Bacik5dc562c2012-08-17 13:14:17 -04006381 /*
6382 * We could have gotten an inode number from somebody who was fsynced
6383 * and then removed in this same transaction, so let's just set full
6384 * sync since it will be a full sync anyway and this will blow away the
6385 * old info in the log.
6386 */
6387 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
6388
Chris Mason9c583092008-01-29 15:15:18 -05006389 key[0].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006390 key[0].type = BTRFS_INODE_ITEM_KEY;
Chris Mason9c583092008-01-29 15:15:18 -05006391 key[0].offset = 0;
6392
Chris Mason9c583092008-01-29 15:15:18 -05006393 sizes[0] = sizeof(struct btrfs_inode_item);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006394
6395 if (name) {
6396 /*
6397 * Start new inodes with an inode_ref. This is slightly more
6398 * efficient for small numbers of hard links since they will
6399 * be packed into one item. Extended refs will kick in if we
6400 * add more hard links than can fit in the ref item.
6401 */
6402 key[1].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006403 key[1].type = BTRFS_INODE_REF_KEY;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006404 key[1].offset = ref_objectid;
6405
6406 sizes[1] = name_len + sizeof(*ref);
6407 }
Chris Mason9c583092008-01-29 15:15:18 -05006408
Chris Masonb0d5d102014-09-08 13:08:51 -07006409 location = &BTRFS_I(inode)->location;
6410 location->objectid = objectid;
6411 location->offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02006412 location->type = BTRFS_INODE_ITEM_KEY;
Chris Masonb0d5d102014-09-08 13:08:51 -07006413
6414 ret = btrfs_insert_inode_locked(inode);
Al Viro32955c52018-05-16 12:20:05 -04006415 if (ret < 0) {
6416 iput(inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006417 goto fail;
Al Viro32955c52018-05-16 12:20:05 -04006418 }
Chris Masonb0d5d102014-09-08 13:08:51 -07006419
Chris Masonb9473432009-03-13 11:00:37 -04006420 path->leave_spinning = 1;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006421 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems);
Chris Mason9c583092008-01-29 15:15:18 -05006422 if (ret != 0)
Chris Masonb0d5d102014-09-08 13:08:51 -07006423 goto fail_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04006424
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03006425 inode_init_owner(inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04006426 inode_set_bytes(inode, 0);
chandan r9cc97d62012-07-04 12:48:07 +05306427
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006428 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05306429 inode->i_atime = inode->i_mtime;
6430 inode->i_ctime = inode->i_mtime;
Arnd Bergmannd3c6be6f2018-06-21 18:04:06 +02006431 BTRFS_I(inode)->i_otime = inode->i_mtime;
chandan r9cc97d62012-07-04 12:48:07 +05306432
Chris Mason5f39d392007-10-15 16:14:19 -04006433 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
6434 struct btrfs_inode_item);
David Sterbab159fa22016-11-08 18:09:03 +01006435 memzero_extent_buffer(path->nodes[0], (unsigned long)inode_item,
Li Zefan293f7e02012-07-10 00:58:58 -06006436 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04006437 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05006438
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006439 if (name) {
6440 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
6441 struct btrfs_inode_ref);
6442 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
6443 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
6444 ptr = (unsigned long)(ref + 1);
6445 write_extent_buffer(path->nodes[0], name, ptr, name_len);
6446 }
Chris Mason9c583092008-01-29 15:15:18 -05006447
Chris Mason5f39d392007-10-15 16:14:19 -04006448 btrfs_mark_buffer_dirty(path->nodes[0]);
6449 btrfs_free_path(path);
6450
Christoph Hellwig6cbff002009-04-17 10:37:41 +02006451 btrfs_inherit_iflags(inode, dir);
6452
Al Viro569254b2011-07-24 17:08:40 -04006453 if (S_ISREG(mode)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006454 if (btrfs_test_opt(fs_info, NODATASUM))
Chris Mason94272162009-07-02 12:26:06 -04006455 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006456 if (btrfs_test_opt(fs_info, NODATACOW))
Josef Bacikf2bdf9a2013-02-21 15:28:28 -05006457 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
6458 BTRFS_INODE_NODATASUM;
Chris Mason94272162009-07-02 12:26:06 -04006459 }
6460
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006461 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00006462
6463 trace_btrfs_inode_new(inode);
Chris Mason1973f0f2011-06-24 13:13:29 -04006464 btrfs_set_inode_last_trans(trans, inode);
liubo1abe9b82011-03-24 11:18:59 +00006465
Alexander Block8ea05e32012-07-25 17:35:53 +02006466 btrfs_update_root_times(trans, root);
6467
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006468 ret = btrfs_inode_inherit_props(trans, inode, dir);
6469 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006470 btrfs_err(fs_info,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006471 "error inheriting props for ino %llu (root %llu): %d",
David Sterbaf85b7372017-01-20 14:54:07 +01006472 btrfs_ino(BTRFS_I(inode)), root->root_key.objectid, ret);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006473
Chris Mason39279cc2007-06-12 06:35:45 -04006474 return inode;
Chris Masonb0d5d102014-09-08 13:08:51 -07006475
6476fail_unlock:
Al Viro32955c52018-05-16 12:20:05 -04006477 discard_new_inode(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006478fail:
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006479 if (dir && name)
Josef Bacikaec74772008-07-24 12:12:38 -04006480 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04006481 btrfs_free_path(path);
6482 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006483}
6484
Chris Masond352ac62008-09-29 15:18:18 -04006485/*
6486 * utility function to add 'inode' into 'parent_inode' with
6487 * a give name and a given sequence number.
6488 * if 'add_backref' is true, also insert a backref from the
6489 * inode to the parent directory.
6490 */
Chris Masone02119d2008-09-05 16:13:11 -04006491int btrfs_add_link(struct btrfs_trans_handle *trans,
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006492 struct btrfs_inode *parent_inode, struct btrfs_inode *inode,
Chris Masone02119d2008-09-05 16:13:11 -04006493 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006494{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006495 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006496 struct btrfs_key key;
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006497 struct btrfs_root *root = parent_inode->root;
6498 u64 ino = btrfs_ino(inode);
6499 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006500
Li Zefan33345d012011-04-20 10:31:50 +08006501 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006502 memcpy(&key, &inode->root->root_key, sizeof(key));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006503 } else {
Li Zefan33345d012011-04-20 10:31:50 +08006504 key.objectid = ino;
David Sterba962a2982014-06-04 18:41:45 +02006505 key.type = BTRFS_INODE_ITEM_KEY;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006506 key.offset = 0;
6507 }
Chris Mason39279cc2007-06-12 06:35:45 -04006508
Li Zefan33345d012011-04-20 10:31:50 +08006509 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Lu Fengqi6025c192018-08-01 11:32:29 +08006510 ret = btrfs_add_root_ref(trans, key.objectid,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006511 root->root_key.objectid, parent_ino,
6512 index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006513 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08006514 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
6515 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006516 }
6517
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006518 /* Nothing to clean up yet */
6519 if (ret)
6520 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006521
Lu Fengqi684572d2018-08-04 21:10:57 +08006522 ret = btrfs_insert_dir_item(trans, name, name_len, parent_inode, &key,
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006523 btrfs_inode_type(&inode->vfs_inode), index);
Chris Mason9c520572012-12-17 14:26:57 -05006524 if (ret == -EEXIST || ret == -EOVERFLOW)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006525 goto fail_dir_item;
6526 else if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04006527 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006528 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006529 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006530
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006531 btrfs_i_size_write(parent_inode, parent_inode->vfs_inode.i_size +
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006532 name_len * 2);
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006533 inode_inc_iversion(&parent_inode->vfs_inode);
Filipe Manana5338e432019-05-15 16:02:47 +01006534 /*
6535 * If we are replaying a log tree, we do not want to update the mtime
6536 * and ctime of the parent directory with the current time, since the
6537 * log replay procedure is responsible for setting them to their correct
6538 * values (the ones it had when the fsync was done).
6539 */
6540 if (!test_bit(BTRFS_FS_LOG_RECOVERING, &root->fs_info->flags)) {
6541 struct timespec64 now = current_time(&parent_inode->vfs_inode);
6542
6543 parent_inode->vfs_inode.i_mtime = now;
6544 parent_inode->vfs_inode.i_ctime = now;
6545 }
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006546 ret = btrfs_update_inode(trans, root, &parent_inode->vfs_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006547 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04006548 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006549 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05006550
6551fail_dir_item:
6552 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
6553 u64 local_index;
6554 int err;
Lu Fengqi3ee1c552018-08-01 11:32:28 +08006555 err = btrfs_del_root_ref(trans, key.objectid,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006556 root->root_key.objectid, parent_ino,
6557 &local_index, name, name_len);
Johannes Thumshirn1690dd412018-12-12 15:14:17 +01006558 if (err)
6559 btrfs_abort_transaction(trans, err);
Chris Masonfe66a052012-02-20 08:40:56 -05006560 } else if (add_backref) {
6561 u64 local_index;
6562 int err;
6563
6564 err = btrfs_del_inode_ref(trans, root, name, name_len,
6565 ino, parent_ino, &local_index);
Johannes Thumshirn1690dd412018-12-12 15:14:17 +01006566 if (err)
6567 btrfs_abort_transaction(trans, err);
Chris Masonfe66a052012-02-20 08:40:56 -05006568 }
Johannes Thumshirn1690dd412018-12-12 15:14:17 +01006569
6570 /* Return the original error code */
Chris Masonfe66a052012-02-20 08:40:56 -05006571 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006572}
6573
6574static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006575 struct btrfs_inode *dir, struct dentry *dentry,
6576 struct btrfs_inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006577{
Josef Bacika1b075d2010-11-19 20:36:11 +00006578 int err = btrfs_add_link(trans, dir, inode,
6579 dentry->d_name.name, dentry->d_name.len,
6580 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006581 if (err > 0)
6582 err = -EEXIST;
6583 return err;
6584}
6585
Josef Bacik618e21d2007-07-11 10:18:17 -04006586static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04006587 umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04006588{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006589 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Josef Bacik618e21d2007-07-11 10:18:17 -04006590 struct btrfs_trans_handle *trans;
6591 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006592 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04006593 int err;
Josef Bacik618e21d2007-07-11 10:18:17 -04006594 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006595 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04006596
Josef Bacik9ed74f22009-09-11 16:12:44 -04006597 /*
6598 * 2 for inode item and ref
6599 * 2 for dir items
6600 * 1 for xattr if selinux is on
6601 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006602 trans = btrfs_start_transaction(root, 5);
6603 if (IS_ERR(trans))
6604 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05006605
Li Zefan581bb052011-04-20 10:06:11 +08006606 err = btrfs_find_free_ino(root, &objectid);
6607 if (err)
6608 goto out_unlock;
6609
Josef Bacikaec74772008-07-24 12:12:38 -04006610 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006611 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6612 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006613 if (IS_ERR(inode)) {
6614 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -04006615 inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04006616 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006617 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006618
Casey Schauflerad19db72011-12-15 10:09:07 -05006619 /*
6620 * If the active LSM wants to access the inode during
6621 * d_instantiate it needs these. Smack checks to see
6622 * if the filesystem supports xattrs by looking at the
6623 * ops vector.
6624 */
Casey Schauflerad19db72011-12-15 10:09:07 -05006625 inode->i_op = &btrfs_special_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006626 init_special_inode(inode, inode->i_mode, rdev);
6627
6628 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik618e21d2007-07-11 10:18:17 -04006629 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006630 goto out_unlock;
Chris Masonb0d5d102014-09-08 13:08:51 -07006631
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006632 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6633 0, index);
Al Viro32955c52018-05-16 12:20:05 -04006634 if (err)
6635 goto out_unlock;
6636
6637 btrfs_update_inode(trans, root, inode);
6638 d_instantiate_new(dentry, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006639
Josef Bacik618e21d2007-07-11 10:18:17 -04006640out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006641 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006642 btrfs_btree_balance_dirty(fs_info);
Al Viro32955c52018-05-16 12:20:05 -04006643 if (err && inode) {
Josef Bacik618e21d2007-07-11 10:18:17 -04006644 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -04006645 discard_new_inode(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006646 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006647 return err;
6648}
6649
Chris Mason39279cc2007-06-12 06:35:45 -04006650static int btrfs_create(struct inode *dir, struct dentry *dentry,
Al Viroebfc3b42012-06-10 18:05:36 -04006651 umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -04006652{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006653 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04006654 struct btrfs_trans_handle *trans;
6655 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006656 struct inode *inode = NULL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006657 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04006658 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006659 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006660
Josef Bacik9ed74f22009-09-11 16:12:44 -04006661 /*
6662 * 2 for inode item and ref
6663 * 2 for dir items
6664 * 1 for xattr if selinux is on
6665 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006666 trans = btrfs_start_transaction(root, 5);
6667 if (IS_ERR(trans))
6668 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006669
Li Zefan581bb052011-04-20 10:06:11 +08006670 err = btrfs_find_free_ino(root, &objectid);
6671 if (err)
6672 goto out_unlock;
6673
Josef Bacikaec74772008-07-24 12:12:38 -04006674 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006675 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6676 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006677 if (IS_ERR(inode)) {
6678 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -04006679 inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006680 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006681 }
Casey Schauflerad19db72011-12-15 10:09:07 -05006682 /*
6683 * If the active LSM wants to access the inode during
6684 * d_instantiate it needs these. Smack checks to see
6685 * if the filesystem supports xattrs by looking at the
6686 * ops vector.
6687 */
6688 inode->i_fop = &btrfs_file_operations;
6689 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006690 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006691
6692 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
6693 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006694 goto out_unlock;
Chris Masonb0d5d102014-09-08 13:08:51 -07006695
6696 err = btrfs_update_inode(trans, root, inode);
6697 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006698 goto out_unlock;
Casey Schauflerad19db72011-12-15 10:09:07 -05006699
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006700 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6701 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006702 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006703 goto out_unlock;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006704
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006705 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Al Viro1e2e5472018-05-04 08:23:01 -04006706 d_instantiate_new(dentry, inode);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006707
Chris Mason39279cc2007-06-12 06:35:45 -04006708out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006709 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -04006710 if (err && inode) {
Chris Mason39279cc2007-06-12 06:35:45 -04006711 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -04006712 discard_new_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006713 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006714 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006715 return err;
6716}
6717
6718static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
6719 struct dentry *dentry)
6720{
Filipe Manana271dba452016-01-05 16:24:05 +00006721 struct btrfs_trans_handle *trans = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006722 struct btrfs_root *root = BTRFS_I(dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00006723 struct inode *inode = d_inode(old_dentry);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006724 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason00e4e6b2008-08-05 11:18:09 -04006725 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04006726 int err;
6727 int drop_inode = 0;
6728
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006729 /* do not allow sys_link's with other subvols of the same device */
Misono Tomohiro4fd786e2018-08-06 14:25:24 +09006730 if (root->root_key.objectid != BTRFS_I(inode)->root->root_key.objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00006731 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006732
Mark Fashehf1863732012-08-08 11:32:27 -07006733 if (inode->i_nlink >= BTRFS_LINK_MAX)
Al Viroc055e992011-03-04 17:15:18 +00006734 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04006735
Nikolay Borisov877574e2017-02-20 13:50:33 +02006736 err = btrfs_set_inode_index(BTRFS_I(dir), &index);
Josef Bacikaec74772008-07-24 12:12:38 -04006737 if (err)
6738 goto fail;
6739
Yan, Zhenga22285a2010-05-16 10:48:46 -04006740 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00006741 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04006742 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00006743 * 1 item for parent inode
Omar Sandoval399b0bb2018-05-11 13:13:40 -07006744 * 1 item for orphan item deletion if O_TMPFILE
Yan, Zhenga22285a2010-05-16 10:48:46 -04006745 */
Omar Sandoval399b0bb2018-05-11 13:13:40 -07006746 trans = btrfs_start_transaction(root, inode->i_nlink ? 5 : 6);
Yan, Zhenga22285a2010-05-16 10:48:46 -04006747 if (IS_ERR(trans)) {
6748 err = PTR_ERR(trans);
Filipe Manana271dba452016-01-05 16:24:05 +00006749 trans = NULL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006750 goto fail;
6751 }
Chris Mason5f39d392007-10-15 16:14:19 -04006752
Miao Xie67de1172013-12-26 13:07:06 +08006753 /* There are several dir indexes for this inode, clear the cache. */
6754 BTRFS_I(inode)->dir_index = 0ULL;
Zach Brown8b558c52013-10-16 12:10:34 -07006755 inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04006756 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006757 inode->i_ctime = current_time(inode);
Al Viro7de9c6ee2010-10-23 11:11:40 -04006758 ihold(inode);
Josef Bacike9976152012-10-11 15:53:56 -04006759 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
Josef Bacikaec74772008-07-24 12:12:38 -04006760
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006761 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6762 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04006763
Yan, Zhenga5719522009-09-24 09:17:31 -04006764 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006765 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04006766 } else {
Al Viro10d9f302011-07-16 23:09:10 -04006767 struct dentry *parent = dentry->d_parent;
Filipe Mananad4682ba2018-06-11 19:24:28 +01006768 int ret;
6769
Yan, Zhenga5719522009-09-24 09:17:31 -04006770 err = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006771 if (err)
6772 goto fail;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006773 if (inode->i_nlink == 1) {
6774 /*
6775 * If new hard link count is 1, it's a file created
6776 * with open(2) O_TMPFILE flag.
6777 */
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02006778 err = btrfs_orphan_del(trans, BTRFS_I(inode));
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006779 if (err)
6780 goto fail;
6781 }
Al Viro08c422c2011-12-23 07:58:13 -05006782 d_instantiate(dentry, inode);
Filipe Mananad4682ba2018-06-11 19:24:28 +01006783 ret = btrfs_log_new_name(trans, BTRFS_I(inode), NULL, parent,
6784 true, NULL);
6785 if (ret == BTRFS_NEED_TRANS_COMMIT) {
6786 err = btrfs_commit_transaction(trans);
6787 trans = NULL;
6788 }
Yan, Zhenga5719522009-09-24 09:17:31 -04006789 }
Chris Mason39279cc2007-06-12 06:35:45 -04006790
Chris Mason1832a6d2007-12-21 16:27:21 -05006791fail:
Filipe Manana271dba452016-01-05 16:24:05 +00006792 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006793 btrfs_end_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006794 if (drop_inode) {
6795 inode_dec_link_count(inode);
6796 iput(inode);
6797 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006798 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006799 return err;
6800}
6801
Al Viro18bb1db2011-07-26 01:41:39 -04006802static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04006803{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006804 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masonb9d86662008-05-02 16:13:49 -04006805 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006806 struct btrfs_trans_handle *trans;
6807 struct btrfs_root *root = BTRFS_I(dir)->root;
6808 int err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04006809 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006810 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006811
Josef Bacik9ed74f22009-09-11 16:12:44 -04006812 /*
6813 * 2 items for inode and ref
6814 * 2 items for dir items
6815 * 1 for xattr if selinux is on
6816 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006817 trans = btrfs_start_transaction(root, 5);
6818 if (IS_ERR(trans))
6819 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006820
Li Zefan581bb052011-04-20 10:06:11 +08006821 err = btrfs_find_free_ino(root, &objectid);
6822 if (err)
6823 goto out_fail;
6824
Josef Bacikaec74772008-07-24 12:12:38 -04006825 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006826 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6827 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04006828 if (IS_ERR(inode)) {
6829 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -04006830 inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006831 goto out_fail;
6832 }
Chris Mason5f39d392007-10-15 16:14:19 -04006833
Chris Masonb0d5d102014-09-08 13:08:51 -07006834 /* these must be set before we unlock the inode */
6835 inode->i_op = &btrfs_dir_inode_operations;
6836 inode->i_fop = &btrfs_dir_file_operations;
Josef Bacik33268ea2008-07-24 12:16:36 -04006837
Eric Paris2a7dba32011-02-01 11:05:39 -05006838 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04006839 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006840 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04006841
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02006842 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Mason39279cc2007-06-12 06:35:45 -04006843 err = btrfs_update_inode(trans, root, inode);
6844 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006845 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04006846
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006847 err = btrfs_add_link(trans, BTRFS_I(dir), BTRFS_I(inode),
6848 dentry->d_name.name,
6849 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006850 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006851 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04006852
Al Viro1e2e5472018-05-04 08:23:01 -04006853 d_instantiate_new(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006854
6855out_fail:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006856 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -04006857 if (err && inode) {
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006858 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -04006859 discard_new_inode(inode);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006860 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006861 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006862 return err;
6863}
6864
Chris Masonc8b97812008-10-29 14:49:59 -04006865static noinline int uncompress_inline(struct btrfs_path *path,
Byongho Leee40da0e2015-05-19 23:46:45 +09006866 struct page *page,
Chris Masonc8b97812008-10-29 14:49:59 -04006867 size_t pg_offset, u64 extent_offset,
6868 struct btrfs_file_extent_item *item)
6869{
6870 int ret;
6871 struct extent_buffer *leaf = path->nodes[0];
6872 char *tmp;
6873 size_t max_size;
6874 unsigned long inline_size;
6875 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08006876 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04006877
6878 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08006879 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04006880 max_size = btrfs_file_extent_ram_bytes(leaf, item);
6881 inline_size = btrfs_file_extent_inline_item_len(leaf,
Ross Kirkdd3cc162013-09-16 15:58:09 +01006882 btrfs_item_nr(path->slots[0]));
Chris Masonc8b97812008-10-29 14:49:59 -04006883 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04006884 if (!tmp)
6885 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04006886 ptr = btrfs_file_extent_inline_start(item);
6887
6888 read_extent_buffer(leaf, tmp, ptr, inline_size);
6889
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006890 max_size = min_t(unsigned long, PAGE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08006891 ret = btrfs_decompress(compress_type, tmp, page,
6892 extent_offset, inline_size, max_size);
Zygo Blaxelle1699d22017-03-10 16:45:44 -05006893
6894 /*
6895 * decompression code contains a memset to fill in any space between the end
6896 * of the uncompressed data and the end of max_size in case the decompressed
6897 * data ends up shorter than ram_bytes. That doesn't cover the hole between
6898 * the end of an inline extent and the beginning of the next block, so we
6899 * cover that region here.
6900 */
6901
6902 if (max_size + pg_offset < PAGE_SIZE) {
6903 char *map = kmap(page);
6904 memset(map + pg_offset + max_size, 0, PAGE_SIZE - max_size - pg_offset);
6905 kunmap(page);
6906 }
Chris Masonc8b97812008-10-29 14:49:59 -04006907 kfree(tmp);
Zach Brown166ae5a2014-05-09 17:15:10 -04006908 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -04006909}
6910
Chris Masond352ac62008-09-29 15:18:18 -04006911/*
6912 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05006913 * the ugly parts come from merging extents from the disk with the in-ram
6914 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04006915 * where the in-ram extents might be locked pending data=ordered completion.
6916 *
6917 * This also copies inline extents directly into the page.
6918 */
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006919struct extent_map *btrfs_get_extent(struct btrfs_inode *inode,
Liu Bode2c6612018-08-25 13:47:59 +08006920 struct page *page,
6921 size_t pg_offset, u64 start, u64 len,
6922 int create)
Chris Masona52d9a82007-08-27 16:49:44 -04006923{
David Sterba3ffbd682018-06-29 10:56:42 +02006924 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Chris Masona52d9a82007-08-27 16:49:44 -04006925 int ret;
6926 int err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006927 u64 extent_start = 0;
6928 u64 extent_end = 0;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006929 u64 objectid = btrfs_ino(inode);
Linus Torvalds7e74e232019-05-01 12:19:20 -07006930 int extent_type = -1;
Chris Masonf4219502008-07-22 11:18:09 -04006931 struct btrfs_path *path = NULL;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006932 struct btrfs_root *root = inode->root;
Chris Masona52d9a82007-08-27 16:49:44 -04006933 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04006934 struct extent_buffer *leaf;
6935 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04006936 struct extent_map *em = NULL;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006937 struct extent_map_tree *em_tree = &inode->extent_tree;
6938 struct extent_io_tree *io_tree = &inode->io_tree;
Filipe Manana7ffbb592014-06-09 03:48:05 +01006939 const bool new_inline = !page || create;
Chris Masona52d9a82007-08-27 16:49:44 -04006940
Chris Mason890871b2009-09-02 16:24:52 -04006941 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006942 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04006943 if (em)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006944 em->bdev = fs_info->fs_devices->latest_bdev;
Chris Mason890871b2009-09-02 16:24:52 -04006945 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006946
Chris Masona52d9a82007-08-27 16:49:44 -04006947 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04006948 if (em->start > start || em->start + em->len <= start)
6949 free_extent_map(em);
6950 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05006951 free_extent_map(em);
6952 else
6953 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006954 }
David Sterba172ddd62011-04-21 00:48:27 +02006955 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04006956 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05006957 err = -ENOMEM;
6958 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006959 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006960 em->bdev = fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05006961 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05006962 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05006963 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04006964 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04006965
Liu Bobee6ec82018-08-17 05:05:28 +08006966 path = btrfs_alloc_path();
Chris Masonf4219502008-07-22 11:18:09 -04006967 if (!path) {
Liu Bobee6ec82018-08-17 05:05:28 +08006968 err = -ENOMEM;
6969 goto out;
Chris Masonf4219502008-07-22 11:18:09 -04006970 }
6971
Liu Bobee6ec82018-08-17 05:05:28 +08006972 /* Chances are we'll be called again, so go ahead and do readahead */
6973 path->reada = READA_FORWARD;
6974
Liu Boe49aabd2018-08-25 13:47:09 +08006975 /*
6976 * Unless we're going to uncompress the inline extent, no sleep would
6977 * happen.
6978 */
6979 path->leave_spinning = 1;
6980
Nikolay Borisov5c9a7022017-12-01 11:19:40 +02006981 ret = btrfs_lookup_file_extent(NULL, root, path, objectid, start, 0);
Chris Masona52d9a82007-08-27 16:49:44 -04006982 if (ret < 0) {
6983 err = ret;
6984 goto out;
Nikolay Borisovb8eeab72018-12-17 11:49:00 +02006985 } else if (ret > 0) {
Chris Masona52d9a82007-08-27 16:49:44 -04006986 if (path->slots[0] == 0)
6987 goto not_found;
6988 path->slots[0]--;
6989 }
6990
Chris Mason5f39d392007-10-15 16:14:19 -04006991 leaf = path->nodes[0];
6992 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04006993 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04006994 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04006995 if (found_key.objectid != objectid ||
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006996 found_key.type != BTRFS_EXTENT_DATA_KEY) {
Josef Bacik25a50342013-10-14 12:08:38 -04006997 /*
6998 * If we backup past the first extent we want to move forward
6999 * and see if there is an extent in front of us, otherwise we'll
7000 * say there is a hole for our whole search range which can
7001 * cause problems.
7002 */
7003 extent_end = start;
7004 goto next;
Chris Masona52d9a82007-08-27 16:49:44 -04007005 }
7006
Nikolay Borisov694c12e2018-12-17 10:35:59 +02007007 extent_type = btrfs_file_extent_type(leaf, item);
Chris Mason5f39d392007-10-15 16:14:19 -04007008 extent_start = found_key.offset;
Nikolay Borisov694c12e2018-12-17 10:35:59 +02007009 if (extent_type == BTRFS_FILE_EXTENT_REG ||
7010 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08007011 /* Only regular file could have regular/prealloc extent */
7012 if (!S_ISREG(inode->vfs_inode.i_mode)) {
7013 ret = -EUCLEAN;
7014 btrfs_crit(fs_info,
7015 "regular/prealloc extent found for non-regular inode %llu",
7016 btrfs_ino(inode));
7017 goto out;
7018 }
Chris Masona52d9a82007-08-27 16:49:44 -04007019 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04007020 btrfs_file_extent_num_bytes(leaf, item);
Liu Bo09ed2f12017-03-10 11:09:48 -08007021
7022 trace_btrfs_get_extent_show_fi_regular(inode, leaf, item,
7023 extent_start);
Nikolay Borisov694c12e2018-12-17 10:35:59 +02007024 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Yan Zheng9036c102008-10-30 14:19:41 -04007025 size_t size;
Qu Wenruoe41ca582018-06-06 15:41:49 +08007026
7027 size = btrfs_file_extent_ram_bytes(leaf, item);
Jeff Mahoneyda170662016-06-15 09:22:56 -04007028 extent_end = ALIGN(extent_start + size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007029 fs_info->sectorsize);
Liu Bo09ed2f12017-03-10 11:09:48 -08007030
7031 trace_btrfs_get_extent_show_fi_inline(inode, leaf, item,
7032 path->slots[0],
7033 extent_start);
Yan Zheng9036c102008-10-30 14:19:41 -04007034 }
Josef Bacik25a50342013-10-14 12:08:38 -04007035next:
Yan Zheng9036c102008-10-30 14:19:41 -04007036 if (start >= extent_end) {
7037 path->slots[0]++;
7038 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
7039 ret = btrfs_next_leaf(root, path);
7040 if (ret < 0) {
7041 err = ret;
7042 goto out;
Nikolay Borisovb8eeab72018-12-17 11:49:00 +02007043 } else if (ret > 0) {
Yan Zheng9036c102008-10-30 14:19:41 -04007044 goto not_found;
Nikolay Borisovb8eeab72018-12-17 11:49:00 +02007045 }
Yan Zheng9036c102008-10-30 14:19:41 -04007046 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04007047 }
Yan Zheng9036c102008-10-30 14:19:41 -04007048 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
7049 if (found_key.objectid != objectid ||
7050 found_key.type != BTRFS_EXTENT_DATA_KEY)
7051 goto not_found;
7052 if (start + len <= found_key.offset)
7053 goto not_found;
Wang Shilonge2eca692014-07-17 11:44:14 +08007054 if (start > found_key.offset)
7055 goto next;
Nikolay Borisov02a033d2018-12-17 10:36:02 +02007056
7057 /* New extent overlaps with existing one */
Yan Zheng9036c102008-10-30 14:19:41 -04007058 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04007059 em->orig_start = start;
Yan Zheng9036c102008-10-30 14:19:41 -04007060 em->len = found_key.offset - start;
Nikolay Borisov02a033d2018-12-17 10:36:02 +02007061 em->block_start = EXTENT_MAP_HOLE;
7062 goto insert;
Yan Zheng9036c102008-10-30 14:19:41 -04007063 }
7064
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007065 btrfs_extent_item_to_extent_map(inode, path, item,
Nikolay Borisov9cdc5122017-02-20 13:51:02 +02007066 new_inline, em);
Filipe Manana7ffbb592014-06-09 03:48:05 +01007067
Nikolay Borisov694c12e2018-12-17 10:35:59 +02007068 if (extent_type == BTRFS_FILE_EXTENT_REG ||
7069 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04007070 goto insert;
Nikolay Borisov694c12e2018-12-17 10:35:59 +02007071 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04007072 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04007073 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04007074 size_t size;
7075 size_t extent_offset;
7076 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04007077
Filipe Manana7ffbb592014-06-09 03:48:05 +01007078 if (new_inline)
Yan689f9342007-10-29 11:41:07 -04007079 goto out;
Yan689f9342007-10-29 11:41:07 -04007080
Qu Wenruoe41ca582018-06-06 15:41:49 +08007081 size = btrfs_file_extent_ram_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04007082 extent_offset = page_offset(page) + pg_offset - extent_start;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007083 copy_size = min_t(u64, PAGE_SIZE - pg_offset,
7084 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04007085 em->start = extent_start + extent_offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007086 em->len = ALIGN(copy_size, fs_info->sectorsize);
Josef Bacikb4939682012-12-03 10:31:19 -05007087 em->orig_block_len = em->len;
Josef Bacik70c8a912012-10-11 16:54:30 -04007088 em->orig_start = em->start;
Yan689f9342007-10-29 11:41:07 -04007089 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Liu Boe49aabd2018-08-25 13:47:09 +08007090
7091 btrfs_set_path_blocking(path);
Edmund Nadolskibf46f522017-11-20 13:24:49 -07007092 if (!PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08007093 if (btrfs_file_extent_compression(leaf, item) !=
7094 BTRFS_COMPRESS_NONE) {
Byongho Leee40da0e2015-05-19 23:46:45 +09007095 ret = uncompress_inline(path, page, pg_offset,
Chris Masonc8b97812008-10-29 14:49:59 -04007096 extent_offset, item);
Zach Brown166ae5a2014-05-09 17:15:10 -04007097 if (ret) {
7098 err = ret;
7099 goto out;
7100 }
Chris Masonc8b97812008-10-29 14:49:59 -04007101 } else {
7102 map = kmap(page);
7103 read_extent_buffer(leaf, map + pg_offset, ptr,
7104 copy_size);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007105 if (pg_offset + copy_size < PAGE_SIZE) {
Chris Mason93c82d52009-09-11 12:36:29 -04007106 memset(map + pg_offset + copy_size, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007107 PAGE_SIZE - pg_offset -
Chris Mason93c82d52009-09-11 12:36:29 -04007108 copy_size);
7109 }
Chris Masonc8b97812008-10-29 14:49:59 -04007110 kunmap(page);
7111 }
Chris Mason179e29e2007-11-01 11:28:41 -04007112 flush_dcache_page(page);
Chris Masona52d9a82007-08-27 16:49:44 -04007113 }
Chris Masond1310b22008-01-24 16:13:08 -05007114 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00007115 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04007116 goto insert;
Chris Masona52d9a82007-08-27 16:49:44 -04007117 }
7118not_found:
7119 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04007120 em->orig_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05007121 em->len = len;
Chris Mason5f39d392007-10-15 16:14:19 -04007122 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04007123insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02007124 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05007125 if (em->start > start || extent_map_end(em) <= start) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007126 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04007127 "bad extent! em: [%llu %llu] passed [%llu %llu]",
7128 em->start, em->len, start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04007129 err = -EIO;
7130 goto out;
7131 }
Chris Masond1310b22008-01-24 16:13:08 -05007132
7133 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04007134 write_lock(&em_tree->lock);
David Sterbaf46b24c2018-04-03 21:45:57 +02007135 err = btrfs_add_extent_mapping(fs_info, em_tree, &em, start, len);
Chris Mason890871b2009-09-02 16:24:52 -04007136 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04007137out:
Liu Boc6414282018-08-23 07:36:17 +08007138 btrfs_free_path(path);
liubo1abe9b82011-03-24 11:18:59 +00007139
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007140 trace_btrfs_get_extent(root, inode, em);
liubo1abe9b82011-03-24 11:18:59 +00007141
Chris Masona52d9a82007-08-27 16:49:44 -04007142 if (err) {
7143 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04007144 return ERR_PTR(err);
7145 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007146 BUG_ON(!em); /* Error is always set */
Chris Masona52d9a82007-08-27 16:49:44 -04007147 return em;
7148}
7149
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007150struct extent_map *btrfs_get_extent_fiemap(struct btrfs_inode *inode,
Nikolay Borisov4ab47a82018-12-12 09:42:32 +02007151 u64 start, u64 len)
Chris Masonec29ed52011-02-23 16:23:20 -05007152{
7153 struct extent_map *em;
7154 struct extent_map *hole_em = NULL;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007155 u64 delalloc_start = start;
Chris Masonec29ed52011-02-23 16:23:20 -05007156 u64 end;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007157 u64 delalloc_len;
7158 u64 delalloc_end;
Chris Masonec29ed52011-02-23 16:23:20 -05007159 int err = 0;
7160
Nikolay Borisov4ab47a82018-12-12 09:42:32 +02007161 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
Chris Masonec29ed52011-02-23 16:23:20 -05007162 if (IS_ERR(em))
7163 return em;
Dan Carpenter99862772017-04-11 11:57:15 +03007164 /*
7165 * If our em maps to:
7166 * - a hole or
7167 * - a pre-alloc extent,
7168 * there might actually be delalloc bytes behind it.
7169 */
7170 if (em->block_start != EXTENT_MAP_HOLE &&
7171 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7172 return em;
7173 else
7174 hole_em = em;
Chris Masonec29ed52011-02-23 16:23:20 -05007175
7176 /* check to see if we've wrapped (len == -1 or similar) */
7177 end = start + len;
7178 if (end < start)
7179 end = (u64)-1;
7180 else
7181 end -= 1;
7182
7183 em = NULL;
7184
7185 /* ok, we didn't find anything, lets look for delalloc */
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007186 delalloc_len = count_range_bits(&inode->io_tree, &delalloc_start,
Chris Masonec29ed52011-02-23 16:23:20 -05007187 end, len, EXTENT_DELALLOC, 1);
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007188 delalloc_end = delalloc_start + delalloc_len;
7189 if (delalloc_end < delalloc_start)
7190 delalloc_end = (u64)-1;
Chris Masonec29ed52011-02-23 16:23:20 -05007191
7192 /*
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007193 * We didn't find anything useful, return the original results from
7194 * get_extent()
Chris Masonec29ed52011-02-23 16:23:20 -05007195 */
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007196 if (delalloc_start > end || delalloc_end <= start) {
Chris Masonec29ed52011-02-23 16:23:20 -05007197 em = hole_em;
7198 hole_em = NULL;
7199 goto out;
7200 }
7201
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007202 /*
7203 * Adjust the delalloc_start to make sure it doesn't go backwards from
7204 * the start they passed in
Chris Masonec29ed52011-02-23 16:23:20 -05007205 */
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007206 delalloc_start = max(start, delalloc_start);
7207 delalloc_len = delalloc_end - delalloc_start;
Chris Masonec29ed52011-02-23 16:23:20 -05007208
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007209 if (delalloc_len > 0) {
7210 u64 hole_start;
Nikolay Borisov02950af2018-12-12 09:42:34 +02007211 u64 hole_len;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007212 const u64 hole_end = extent_map_end(hole_em);
Chris Masonec29ed52011-02-23 16:23:20 -05007213
David Sterba172ddd62011-04-21 00:48:27 +02007214 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05007215 if (!em) {
7216 err = -ENOMEM;
7217 goto out;
7218 }
Chris Masonec29ed52011-02-23 16:23:20 -05007219 em->bdev = NULL;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007220
7221 ASSERT(hole_em);
7222 /*
7223 * When btrfs_get_extent can't find anything it returns one
7224 * huge hole
7225 *
7226 * Make sure what it found really fits our range, and adjust to
7227 * make sure it is based on the start from the caller
7228 */
7229 if (hole_end <= start || hole_em->start > end) {
7230 free_extent_map(hole_em);
7231 hole_em = NULL;
7232 } else {
7233 hole_start = max(hole_em->start, start);
7234 hole_len = hole_end - hole_start;
7235 }
7236
7237 if (hole_em && delalloc_start > hole_start) {
7238 /*
7239 * Our hole starts before our delalloc, so we have to
7240 * return just the parts of the hole that go until the
7241 * delalloc starts
Chris Masonec29ed52011-02-23 16:23:20 -05007242 */
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007243 em->len = min(hole_len, delalloc_start - hole_start);
Chris Masonec29ed52011-02-23 16:23:20 -05007244 em->start = hole_start;
7245 em->orig_start = hole_start;
7246 /*
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007247 * Don't adjust block start at all, it is fixed at
7248 * EXTENT_MAP_HOLE
Chris Masonec29ed52011-02-23 16:23:20 -05007249 */
7250 em->block_start = hole_em->block_start;
7251 em->block_len = hole_len;
Liu Bof9e4fb52013-01-07 10:10:12 +00007252 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
7253 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonec29ed52011-02-23 16:23:20 -05007254 } else {
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007255 /*
7256 * Hole is out of passed range or it starts after
7257 * delalloc range
7258 */
7259 em->start = delalloc_start;
7260 em->len = delalloc_len;
7261 em->orig_start = delalloc_start;
Chris Masonec29ed52011-02-23 16:23:20 -05007262 em->block_start = EXTENT_MAP_DELALLOC;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007263 em->block_len = delalloc_len;
Chris Masonec29ed52011-02-23 16:23:20 -05007264 }
Nikolay Borisovbf8d32b2017-12-01 11:19:43 +02007265 } else {
Chris Masonec29ed52011-02-23 16:23:20 -05007266 return hole_em;
7267 }
7268out:
7269
7270 free_extent_map(hole_em);
7271 if (err) {
7272 free_extent_map(em);
7273 return ERR_PTR(err);
7274 }
7275 return em;
7276}
7277
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007278static struct extent_map *btrfs_create_dio_extent(struct inode *inode,
7279 const u64 start,
7280 const u64 len,
7281 const u64 orig_start,
7282 const u64 block_start,
7283 const u64 block_len,
7284 const u64 orig_block_len,
7285 const u64 ram_bytes,
7286 const int type)
7287{
7288 struct extent_map *em = NULL;
7289 int ret;
7290
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007291 if (type != BTRFS_ORDERED_NOCOW) {
Liu Bo6f9994d2017-01-31 07:50:22 -08007292 em = create_io_em(inode, start, len, orig_start,
7293 block_start, block_len, orig_block_len,
7294 ram_bytes,
7295 BTRFS_COMPRESS_NONE, /* compress_type */
7296 type);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007297 if (IS_ERR(em))
7298 goto out;
7299 }
7300 ret = btrfs_add_ordered_extent_dio(inode, start, block_start,
7301 len, block_len, type);
7302 if (ret) {
7303 if (em) {
7304 free_extent_map(em);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02007305 btrfs_drop_extent_cache(BTRFS_I(inode), start,
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007306 start + len - 1, 0);
7307 }
7308 em = ERR_PTR(ret);
7309 }
7310 out:
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007311
7312 return em;
7313}
7314
Josef Bacik4b46fce2010-05-23 11:00:55 -04007315static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
7316 u64 start, u64 len)
7317{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007318 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007319 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik70c8a912012-10-11 16:54:30 -04007320 struct extent_map *em;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007321 struct btrfs_key ins;
7322 u64 alloc_hint;
7323 int ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007324
Josef Bacik4b46fce2010-05-23 11:00:55 -04007325 alloc_hint = get_extent_allocation_hint(inode, start, len);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007326 ret = btrfs_reserve_extent(root, len, len, fs_info->sectorsize,
Jeff Mahoneyda170662016-06-15 09:22:56 -04007327 0, alloc_hint, &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007328 if (ret)
7329 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007330
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007331 em = btrfs_create_dio_extent(inode, start, ins.offset, start,
7332 ins.objectid, ins.offset, ins.offset,
Liu Bo6288d6e2017-02-21 12:12:58 -08007333 ins.offset, BTRFS_ORDERED_REGULAR);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007334 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007335 if (IS_ERR(em))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007336 btrfs_free_reserved_extent(fs_info, ins.objectid,
7337 ins.offset, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007338
Josef Bacik4b46fce2010-05-23 11:00:55 -04007339 return em;
7340}
7341
Chris Mason46bfbb52010-05-26 11:04:10 -04007342/*
7343 * returns 1 when the nocow is safe, < 1 on error, 0 if the
7344 * block must be cow'd
7345 */
Josef Bacik00361582013-08-14 14:02:47 -04007346noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
Josef Bacik7ee9e442013-06-21 16:37:03 -04007347 u64 *orig_start, u64 *orig_block_len,
7348 u64 *ram_bytes)
Chris Mason46bfbb52010-05-26 11:04:10 -04007349{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007350 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason46bfbb52010-05-26 11:04:10 -04007351 struct btrfs_path *path;
7352 int ret;
7353 struct extent_buffer *leaf;
7354 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xie7b2b7082014-02-27 13:58:05 +08007355 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason46bfbb52010-05-26 11:04:10 -04007356 struct btrfs_file_extent_item *fi;
7357 struct btrfs_key key;
7358 u64 disk_bytenr;
7359 u64 backref_offset;
7360 u64 extent_end;
7361 u64 num_bytes;
7362 int slot;
7363 int found_type;
Josef Bacik7ee9e442013-06-21 16:37:03 -04007364 bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW);
Miao Xiee77751a2013-12-27 21:11:50 +08007365
Chris Mason46bfbb52010-05-26 11:04:10 -04007366 path = btrfs_alloc_path();
7367 if (!path)
7368 return -ENOMEM;
7369
David Sterbaf85b7372017-01-20 14:54:07 +01007370 ret = btrfs_lookup_file_extent(NULL, root, path,
7371 btrfs_ino(BTRFS_I(inode)), offset, 0);
Chris Mason46bfbb52010-05-26 11:04:10 -04007372 if (ret < 0)
7373 goto out;
7374
7375 slot = path->slots[0];
7376 if (ret == 1) {
7377 if (slot == 0) {
7378 /* can't find the item, must cow */
7379 ret = 0;
7380 goto out;
7381 }
7382 slot--;
7383 }
7384 ret = 0;
7385 leaf = path->nodes[0];
7386 btrfs_item_key_to_cpu(leaf, &key, slot);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02007387 if (key.objectid != btrfs_ino(BTRFS_I(inode)) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04007388 key.type != BTRFS_EXTENT_DATA_KEY) {
7389 /* not our file or wrong item type, must cow */
7390 goto out;
7391 }
7392
7393 if (key.offset > offset) {
7394 /* Wrong offset, must cow */
7395 goto out;
7396 }
7397
7398 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
7399 found_type = btrfs_file_extent_type(leaf, fi);
7400 if (found_type != BTRFS_FILE_EXTENT_REG &&
7401 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
7402 /* not a regular extent, must cow */
7403 goto out;
7404 }
Josef Bacik7ee9e442013-06-21 16:37:03 -04007405
7406 if (!nocow && found_type == BTRFS_FILE_EXTENT_REG)
7407 goto out;
7408
Miao Xiee77751a2013-12-27 21:11:50 +08007409 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
7410 if (extent_end <= offset)
7411 goto out;
7412
Chris Mason46bfbb52010-05-26 11:04:10 -04007413 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Josef Bacik7ee9e442013-06-21 16:37:03 -04007414 if (disk_bytenr == 0)
7415 goto out;
7416
7417 if (btrfs_file_extent_compression(leaf, fi) ||
7418 btrfs_file_extent_encryption(leaf, fi) ||
7419 btrfs_file_extent_other_encoding(leaf, fi))
7420 goto out;
7421
Ethan Lien78d42952018-05-17 14:58:29 +08007422 /*
7423 * Do the same check as in btrfs_cross_ref_exist but without the
7424 * unnecessary search.
7425 */
7426 if (btrfs_file_extent_generation(leaf, fi) <=
7427 btrfs_root_last_snapshot(&root->root_item))
7428 goto out;
7429
Chris Mason46bfbb52010-05-26 11:04:10 -04007430 backref_offset = btrfs_file_extent_offset(leaf, fi);
7431
Josef Bacik7ee9e442013-06-21 16:37:03 -04007432 if (orig_start) {
7433 *orig_start = key.offset - backref_offset;
7434 *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
7435 *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
7436 }
Josef Bacikeb384b52013-04-24 16:32:55 -04007437
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007438 if (btrfs_extent_readonly(fs_info, disk_bytenr))
Chris Mason46bfbb52010-05-26 11:04:10 -04007439 goto out;
Miao Xie7b2b7082014-02-27 13:58:05 +08007440
7441 num_bytes = min(offset + *len, extent_end) - offset;
7442 if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) {
7443 u64 range_end;
7444
Jeff Mahoneyda170662016-06-15 09:22:56 -04007445 range_end = round_up(offset + num_bytes,
7446 root->fs_info->sectorsize) - 1;
Miao Xie7b2b7082014-02-27 13:58:05 +08007447 ret = test_range_bit(io_tree, offset, range_end,
7448 EXTENT_DELALLOC, 0, NULL);
7449 if (ret) {
7450 ret = -EAGAIN;
7451 goto out;
7452 }
7453 }
7454
Josef Bacik1bda19e2013-10-18 12:10:36 -04007455 btrfs_release_path(path);
Chris Mason46bfbb52010-05-26 11:04:10 -04007456
7457 /*
7458 * look for other files referencing this extent, if we
7459 * find any we must cow
7460 */
Josef Bacik00361582013-08-14 14:02:47 -04007461
Liu Boe4c3b2d2017-01-30 12:25:28 -08007462 ret = btrfs_cross_ref_exist(root, btrfs_ino(BTRFS_I(inode)),
Josef Bacik00361582013-08-14 14:02:47 -04007463 key.offset - backref_offset, disk_bytenr);
Josef Bacik00361582013-08-14 14:02:47 -04007464 if (ret) {
7465 ret = 0;
7466 goto out;
7467 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007468
7469 /*
7470 * adjust disk_bytenr and num_bytes to cover just the bytes
7471 * in this extent we are about to write. If there
7472 * are any csums in that range we have to cow in order
7473 * to keep the csums correct
7474 */
7475 disk_bytenr += backref_offset;
7476 disk_bytenr += offset - key.offset;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007477 if (csum_exist_in_range(fs_info, disk_bytenr, num_bytes))
7478 goto out;
Chris Mason46bfbb52010-05-26 11:04:10 -04007479 /*
7480 * all of the above have passed, it is safe to overwrite this extent
7481 * without cow
7482 */
Josef Bacikeb384b52013-04-24 16:32:55 -04007483 *len = num_bytes;
Chris Mason46bfbb52010-05-26 11:04:10 -04007484 ret = 1;
7485out:
7486 btrfs_free_path(path);
7487 return ret;
7488}
7489
Josef Bacikeb838e72012-07-31 16:28:48 -04007490static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
7491 struct extent_state **cached_state, int writing)
7492{
7493 struct btrfs_ordered_extent *ordered;
7494 int ret = 0;
7495
7496 while (1) {
7497 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbaff13db42015-12-03 14:30:40 +01007498 cached_state);
Josef Bacikeb838e72012-07-31 16:28:48 -04007499 /*
7500 * We're concerned with the entire range that we're going to be
Nicholas D Steeves01327612016-05-19 21:18:45 -04007501 * doing DIO to, so we need to make sure there's no ordered
Josef Bacikeb838e72012-07-31 16:28:48 -04007502 * extents in this range.
7503 */
Nikolay Borisova776c6f2017-02-20 13:50:49 +02007504 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), lockstart,
Josef Bacikeb838e72012-07-31 16:28:48 -04007505 lockend - lockstart + 1);
7506
7507 /*
7508 * We need to make sure there are no buffered pages in this
7509 * range either, we could have raced between the invalidate in
7510 * generic_file_direct_write and locking the extent. The
7511 * invalidate needs to happen so that reads after a write do not
7512 * get stale data.
7513 */
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007514 if (!ordered &&
David Sterba051c98e2018-03-07 15:33:22 +01007515 (!writing || !filemap_range_has_page(inode->i_mapping,
7516 lockstart, lockend)))
Josef Bacikeb838e72012-07-31 16:28:48 -04007517 break;
7518
7519 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbae43bbe52017-12-12 21:43:52 +01007520 cached_state);
Josef Bacikeb838e72012-07-31 16:28:48 -04007521
7522 if (ordered) {
Filipe Mananaade77022016-02-18 14:28:55 +00007523 /*
7524 * If we are doing a DIO read and the ordered extent we
7525 * found is for a buffered write, we can not wait for it
7526 * to complete and retry, because if we do so we can
7527 * deadlock with concurrent buffered writes on page
7528 * locks. This happens only if our DIO read covers more
7529 * than one extent map, if at this point has already
7530 * created an ordered extent for a previous extent map
7531 * and locked its range in the inode's io tree, and a
7532 * concurrent write against that previous extent map's
7533 * range and this range started (we unlock the ranges
7534 * in the io tree only when the bios complete and
7535 * buffered writes always lock pages before attempting
7536 * to lock range in the io tree).
7537 */
7538 if (writing ||
7539 test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags))
7540 btrfs_start_ordered_extent(inode, ordered, 1);
7541 else
7542 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007543 btrfs_put_ordered_extent(ordered);
7544 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007545 /*
Filipe Mananab850ae12015-12-08 16:23:16 +00007546 * We could trigger writeback for this range (and wait
7547 * for it to complete) and then invalidate the pages for
7548 * this range (through invalidate_inode_pages2_range()),
7549 * but that can lead us to a deadlock with a concurrent
7550 * call to readpages() (a buffered read or a defrag call
7551 * triggered a readahead) on a page lock due to an
7552 * ordered dio extent we created before but did not have
7553 * yet a corresponding bio submitted (whence it can not
7554 * complete), which makes readpages() wait for that
7555 * ordered extent to complete while holding a lock on
7556 * that page.
Josef Bacikeb838e72012-07-31 16:28:48 -04007557 */
Filipe Mananab850ae12015-12-08 16:23:16 +00007558 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007559 }
7560
Filipe Mananaade77022016-02-18 14:28:55 +00007561 if (ret)
7562 break;
7563
Josef Bacikeb838e72012-07-31 16:28:48 -04007564 cond_resched();
7565 }
7566
7567 return ret;
7568}
7569
Liu Bo6f9994d2017-01-31 07:50:22 -08007570/* The callers of this must take lock_extent() */
7571static struct extent_map *create_io_em(struct inode *inode, u64 start, u64 len,
7572 u64 orig_start, u64 block_start,
7573 u64 block_len, u64 orig_block_len,
7574 u64 ram_bytes, int compress_type,
7575 int type)
Josef Bacik69ffb542012-09-11 15:40:07 -04007576{
7577 struct extent_map_tree *em_tree;
7578 struct extent_map *em;
7579 struct btrfs_root *root = BTRFS_I(inode)->root;
7580 int ret;
7581
Liu Bo6f9994d2017-01-31 07:50:22 -08007582 ASSERT(type == BTRFS_ORDERED_PREALLOC ||
7583 type == BTRFS_ORDERED_COMPRESSED ||
7584 type == BTRFS_ORDERED_NOCOW ||
Liu Bo1af4a0a2017-02-13 15:35:09 -08007585 type == BTRFS_ORDERED_REGULAR);
Liu Bo6f9994d2017-01-31 07:50:22 -08007586
Josef Bacik69ffb542012-09-11 15:40:07 -04007587 em_tree = &BTRFS_I(inode)->extent_tree;
7588 em = alloc_extent_map();
7589 if (!em)
7590 return ERR_PTR(-ENOMEM);
7591
7592 em->start = start;
7593 em->orig_start = orig_start;
7594 em->len = len;
7595 em->block_len = block_len;
7596 em->block_start = block_start;
7597 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacikb4939682012-12-03 10:31:19 -05007598 em->orig_block_len = orig_block_len;
Josef Bacikcc95bef2013-04-04 14:31:27 -04007599 em->ram_bytes = ram_bytes;
Josef Bacik70c8a912012-10-11 16:54:30 -04007600 em->generation = -1;
Josef Bacik69ffb542012-09-11 15:40:07 -04007601 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Liu Bo1af4a0a2017-02-13 15:35:09 -08007602 if (type == BTRFS_ORDERED_PREALLOC) {
Josef Bacikb11e2342012-12-03 10:58:15 -05007603 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Liu Bo1af4a0a2017-02-13 15:35:09 -08007604 } else if (type == BTRFS_ORDERED_COMPRESSED) {
Liu Bo6f9994d2017-01-31 07:50:22 -08007605 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
7606 em->compress_type = compress_type;
7607 }
Josef Bacik69ffb542012-09-11 15:40:07 -04007608
7609 do {
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02007610 btrfs_drop_extent_cache(BTRFS_I(inode), em->start,
Josef Bacik69ffb542012-09-11 15:40:07 -04007611 em->start + em->len - 1, 0);
7612 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04007613 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik69ffb542012-09-11 15:40:07 -04007614 write_unlock(&em_tree->lock);
Liu Bo6f9994d2017-01-31 07:50:22 -08007615 /*
7616 * The caller has taken lock_extent(), who could race with us
7617 * to add em?
7618 */
Josef Bacik69ffb542012-09-11 15:40:07 -04007619 } while (ret == -EEXIST);
7620
7621 if (ret) {
7622 free_extent_map(em);
7623 return ERR_PTR(ret);
7624 }
7625
Liu Bo6f9994d2017-01-31 07:50:22 -08007626 /* em got 2 refs now, callers needs to do free_extent_map once. */
Josef Bacik69ffb542012-09-11 15:40:07 -04007627 return em;
7628}
7629
Nikolay Borisov1c8d0172018-05-02 15:19:32 +03007630
7631static int btrfs_get_blocks_direct_read(struct extent_map *em,
7632 struct buffer_head *bh_result,
7633 struct inode *inode,
7634 u64 start, u64 len)
7635{
7636 if (em->block_start == EXTENT_MAP_HOLE ||
7637 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7638 return -ENOENT;
7639
7640 len = min(len, em->len - (start - em->start));
7641
7642 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
7643 inode->i_blkbits;
7644 bh_result->b_size = len;
7645 bh_result->b_bdev = em->bdev;
7646 set_buffer_mapped(bh_result);
7647
7648 return 0;
7649}
7650
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007651static int btrfs_get_blocks_direct_write(struct extent_map **map,
7652 struct buffer_head *bh_result,
7653 struct inode *inode,
7654 struct btrfs_dio_data *dio_data,
7655 u64 start, u64 len)
7656{
7657 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
7658 struct extent_map *em = *map;
7659 int ret = 0;
7660
7661 /*
7662 * We don't allocate a new extent in the following cases
7663 *
7664 * 1) The inode is marked as NODATACOW. In this case we'll just use the
7665 * existing extent.
7666 * 2) The extent is marked as PREALLOC. We're good to go here and can
7667 * just use the extent.
7668 *
7669 */
7670 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
7671 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
7672 em->block_start != EXTENT_MAP_HOLE)) {
7673 int type;
7674 u64 block_start, orig_start, orig_block_len, ram_bytes;
7675
7676 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7677 type = BTRFS_ORDERED_PREALLOC;
7678 else
7679 type = BTRFS_ORDERED_NOCOW;
7680 len = min(len, em->len - (start - em->start));
7681 block_start = em->block_start + (start - em->start);
7682
7683 if (can_nocow_extent(inode, start, &len, &orig_start,
7684 &orig_block_len, &ram_bytes) == 1 &&
7685 btrfs_inc_nocow_writers(fs_info, block_start)) {
7686 struct extent_map *em2;
7687
7688 em2 = btrfs_create_dio_extent(inode, start, len,
7689 orig_start, block_start,
7690 len, orig_block_len,
7691 ram_bytes, type);
7692 btrfs_dec_nocow_writers(fs_info, block_start);
7693 if (type == BTRFS_ORDERED_PREALLOC) {
7694 free_extent_map(em);
7695 *map = em = em2;
7696 }
7697
7698 if (em2 && IS_ERR(em2)) {
7699 ret = PTR_ERR(em2);
7700 goto out;
7701 }
7702 /*
7703 * For inode marked NODATACOW or extent marked PREALLOC,
7704 * use the existing or preallocated extent, so does not
7705 * need to adjust btrfs_space_info's bytes_may_use.
7706 */
7707 btrfs_free_reserved_data_space_noquota(inode, start,
7708 len);
7709 goto skip_cow;
7710 }
7711 }
7712
7713 /* this will cow the extent */
7714 len = bh_result->b_size;
7715 free_extent_map(em);
7716 *map = em = btrfs_new_extent_direct(inode, start, len);
7717 if (IS_ERR(em)) {
7718 ret = PTR_ERR(em);
7719 goto out;
7720 }
7721
7722 len = min(len, em->len - (start - em->start));
7723
7724skip_cow:
7725 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
7726 inode->i_blkbits;
7727 bh_result->b_size = len;
7728 bh_result->b_bdev = em->bdev;
7729 set_buffer_mapped(bh_result);
7730
7731 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7732 set_buffer_new(bh_result);
7733
7734 /*
7735 * Need to update the i_size under the extent lock so buffered
7736 * readers will get the updated i_size when we unlock.
7737 */
7738 if (!dio_data->overwrite && start + len > i_size_read(inode))
7739 i_size_write(inode, start + len);
7740
7741 WARN_ON(dio_data->reserve < len);
7742 dio_data->reserve -= len;
7743 dio_data->unsubmitted_oe_range_end = start + len;
7744 current->journal_info = dio_data;
7745out:
7746 return ret;
7747}
7748
Josef Bacik4b46fce2010-05-23 11:00:55 -04007749static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
7750 struct buffer_head *bh_result, int create)
7751{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007752 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007753 struct extent_map *em;
Josef Bacikeb838e72012-07-31 16:28:48 -04007754 struct extent_state *cached_state = NULL;
chandan50745b02015-08-28 21:10:13 +05307755 struct btrfs_dio_data *dio_data = NULL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007756 u64 start = iblock << inode->i_blkbits;
Josef Bacikeb838e72012-07-31 16:28:48 -04007757 u64 lockstart, lockend;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007758 u64 len = bh_result->b_size;
Miao Xie09348562013-02-07 10:12:07 +00007759 int ret = 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007760
Omar Sandovale1821632019-08-15 14:04:04 -07007761 if (!create)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007762 len = min_t(u64, len, fs_info->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04007763
Josef Bacikc3298612012-08-03 16:49:19 -04007764 lockstart = start;
7765 lockend = start + len - 1;
7766
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007767 if (current->journal_info) {
7768 /*
7769 * Need to pull our outstanding extents and set journal_info to NULL so
Nicholas D Steeves01327612016-05-19 21:18:45 -04007770 * that anything that needs to check if there's a transaction doesn't get
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007771 * confused.
7772 */
chandan50745b02015-08-28 21:10:13 +05307773 dio_data = current->journal_info;
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007774 current->journal_info = NULL;
7775 }
7776
Josef Bacikeb838e72012-07-31 16:28:48 -04007777 /*
7778 * If this errors out it's because we couldn't invalidate pagecache for
7779 * this range and we need to fallback to buffered.
7780 */
Filipe Manana9c9464c2015-11-04 09:52:04 +00007781 if (lock_extent_direct(inode, lockstart, lockend, &cached_state,
7782 create)) {
7783 ret = -ENOTBLK;
7784 goto err;
7785 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007786
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007787 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len, 0);
Josef Bacikeb838e72012-07-31 16:28:48 -04007788 if (IS_ERR(em)) {
7789 ret = PTR_ERR(em);
7790 goto unlock_err;
7791 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007792
7793 /*
7794 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
7795 * io. INLINE is special, and we could probably kludge it in here, but
7796 * it's still buffered so for safety lets just fall back to the generic
7797 * buffered path.
7798 *
7799 * For COMPRESSED we _have_ to read the entire extent in so we can
7800 * decompress it, so there will be buffering required no matter what we
7801 * do, so go ahead and fallback to buffered.
7802 *
Nicholas D Steeves01327612016-05-19 21:18:45 -04007803 * We return -ENOTBLK because that's what makes DIO go ahead and go back
Josef Bacik4b46fce2010-05-23 11:00:55 -04007804 * to buffered IO. Don't blame me, this is the price we pay for using
7805 * the generic code.
7806 */
7807 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
7808 em->block_start == EXTENT_MAP_INLINE) {
7809 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007810 ret = -ENOTBLK;
7811 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007812 }
7813
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007814 if (create) {
7815 ret = btrfs_get_blocks_direct_write(&em, bh_result, inode,
7816 dio_data, start, len);
7817 if (ret < 0)
7818 goto unlock_err;
7819
Omar Sandovale1821632019-08-15 14:04:04 -07007820 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart,
7821 lockend, &cached_state);
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007822 } else {
Nikolay Borisov1c8d0172018-05-02 15:19:32 +03007823 ret = btrfs_get_blocks_direct_read(em, bh_result, inode,
7824 start, len);
7825 /* Can be negative only if we read from a hole */
7826 if (ret < 0) {
7827 ret = 0;
7828 free_extent_map(em);
7829 goto unlock_err;
7830 }
7831 /*
7832 * We need to unlock only the end area that we aren't using.
7833 * The rest is going to be unlocked by the endio routine.
7834 */
7835 lockstart = start + bh_result->b_size;
7836 if (lockstart < lockend) {
Omar Sandovale1821632019-08-15 14:04:04 -07007837 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
7838 lockstart, lockend, &cached_state);
Nikolay Borisov1c8d0172018-05-02 15:19:32 +03007839 } else {
7840 free_extent_state(cached_state);
7841 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007842 }
7843
Josef Bacik4b46fce2010-05-23 11:00:55 -04007844 free_extent_map(em);
7845
7846 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007847
7848unlock_err:
Omar Sandovale1821632019-08-15 14:04:04 -07007849 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7850 &cached_state);
Filipe Manana9c9464c2015-11-04 09:52:04 +00007851err:
chandan50745b02015-08-28 21:10:13 +05307852 if (dio_data)
7853 current->journal_info = dio_data;
Josef Bacikeb838e72012-07-31 16:28:48 -04007854 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007855}
7856
Omar Sandoval58efbc92017-08-22 23:45:59 -07007857static inline blk_status_t submit_dio_repair_bio(struct inode *inode,
7858 struct bio *bio,
7859 int mirror_num)
Miao Xie8b110e32014-09-12 18:44:03 +08007860{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007861 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Omar Sandoval58efbc92017-08-22 23:45:59 -07007862 blk_status_t ret;
Miao Xie8b110e32014-09-12 18:44:03 +08007863
Mike Christie37226b22016-06-05 14:31:52 -05007864 BUG_ON(bio_op(bio) == REQ_OP_WRITE);
Miao Xie8b110e32014-09-12 18:44:03 +08007865
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007866 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DIO_REPAIR);
Miao Xie8b110e32014-09-12 18:44:03 +08007867 if (ret)
Nikolay Borisovea057f62017-12-13 10:25:38 +02007868 return ret;
Miao Xie8b110e32014-09-12 18:44:03 +08007869
Chris Mason08635ba2019-07-10 12:28:14 -07007870 ret = btrfs_map_bio(fs_info, bio, mirror_num);
Nikolay Borisovea057f62017-12-13 10:25:38 +02007871
Miao Xie8b110e32014-09-12 18:44:03 +08007872 return ret;
7873}
7874
7875static int btrfs_check_dio_repairable(struct inode *inode,
7876 struct bio *failed_bio,
7877 struct io_failure_record *failrec,
7878 int failed_mirror)
7879{
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007880 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xie8b110e32014-09-12 18:44:03 +08007881 int num_copies;
7882
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007883 num_copies = btrfs_num_copies(fs_info, failrec->logical, failrec->len);
Miao Xie8b110e32014-09-12 18:44:03 +08007884 if (num_copies == 1) {
7885 /*
7886 * we only have a single copy of the data, so don't bother with
7887 * all the retry and error correction code that follows. no
7888 * matter what the error is, it is very likely to persist.
7889 */
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007890 btrfs_debug(fs_info,
7891 "Check DIO Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d",
7892 num_copies, failrec->this_mirror, failed_mirror);
Miao Xie8b110e32014-09-12 18:44:03 +08007893 return 0;
7894 }
7895
7896 failrec->failed_mirror = failed_mirror;
7897 failrec->this_mirror++;
7898 if (failrec->this_mirror == failed_mirror)
7899 failrec->this_mirror++;
7900
7901 if (failrec->this_mirror > num_copies) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007902 btrfs_debug(fs_info,
7903 "Check DIO Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d",
7904 num_copies, failrec->this_mirror, failed_mirror);
Miao Xie8b110e32014-09-12 18:44:03 +08007905 return 0;
7906 }
7907
7908 return 1;
7909}
7910
Omar Sandoval58efbc92017-08-22 23:45:59 -07007911static blk_status_t dio_read_error(struct inode *inode, struct bio *failed_bio,
7912 struct page *page, unsigned int pgoff,
7913 u64 start, u64 end, int failed_mirror,
7914 bio_end_io_t *repair_endio, void *repair_arg)
Miao Xie8b110e32014-09-12 18:44:03 +08007915{
7916 struct io_failure_record *failrec;
Josef Bacik7870d082017-05-05 11:57:15 -04007917 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
7918 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
Miao Xie8b110e32014-09-12 18:44:03 +08007919 struct bio *bio;
7920 int isector;
David Sterbaf1c77c52017-06-06 19:03:49 +02007921 unsigned int read_mode = 0;
Liu Bo17347ce2017-05-15 15:33:27 -07007922 int segs;
Miao Xie8b110e32014-09-12 18:44:03 +08007923 int ret;
Omar Sandoval58efbc92017-08-22 23:45:59 -07007924 blk_status_t status;
Ming Leic16a8ac2017-12-18 20:22:12 +08007925 struct bio_vec bvec;
Miao Xie8b110e32014-09-12 18:44:03 +08007926
Mike Christie37226b22016-06-05 14:31:52 -05007927 BUG_ON(bio_op(failed_bio) == REQ_OP_WRITE);
Miao Xie8b110e32014-09-12 18:44:03 +08007928
7929 ret = btrfs_get_io_failure_record(inode, start, end, &failrec);
7930 if (ret)
Omar Sandoval58efbc92017-08-22 23:45:59 -07007931 return errno_to_blk_status(ret);
Miao Xie8b110e32014-09-12 18:44:03 +08007932
7933 ret = btrfs_check_dio_repairable(inode, failed_bio, failrec,
7934 failed_mirror);
7935 if (!ret) {
Josef Bacik7870d082017-05-05 11:57:15 -04007936 free_io_failure(failure_tree, io_tree, failrec);
Omar Sandoval58efbc92017-08-22 23:45:59 -07007937 return BLK_STS_IOERR;
Miao Xie8b110e32014-09-12 18:44:03 +08007938 }
7939
Liu Bo17347ce2017-05-15 15:33:27 -07007940 segs = bio_segments(failed_bio);
Ming Leic16a8ac2017-12-18 20:22:12 +08007941 bio_get_first_bvec(failed_bio, &bvec);
Liu Bo17347ce2017-05-15 15:33:27 -07007942 if (segs > 1 ||
Ming Leic16a8ac2017-12-18 20:22:12 +08007943 (bvec.bv_len > btrfs_inode_sectorsize(inode)))
Christoph Hellwig70fd7612016-11-01 07:40:10 -06007944 read_mode |= REQ_FAILFAST_DEV;
Miao Xie8b110e32014-09-12 18:44:03 +08007945
7946 isector = start - btrfs_io_bio(failed_bio)->logical;
7947 isector >>= inode->i_sb->s_blocksize_bits;
7948 bio = btrfs_create_repair_bio(inode, failed_bio, failrec, page,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307949 pgoff, isector, repair_endio, repair_arg);
David Sterbaebcc3262018-06-29 10:56:53 +02007950 bio->bi_opf = REQ_OP_READ | read_mode;
Miao Xie8b110e32014-09-12 18:44:03 +08007951
7952 btrfs_debug(BTRFS_I(inode)->root->fs_info,
David Sterba913e1532017-07-13 15:32:18 +02007953 "repair DIO read error: submitting new dio read[%#x] to this_mirror=%d, in_validation=%d",
Miao Xie8b110e32014-09-12 18:44:03 +08007954 read_mode, failrec->this_mirror, failrec->in_validation);
7955
Omar Sandoval58efbc92017-08-22 23:45:59 -07007956 status = submit_dio_repair_bio(inode, bio, failrec->this_mirror);
7957 if (status) {
Josef Bacik7870d082017-05-05 11:57:15 -04007958 free_io_failure(failure_tree, io_tree, failrec);
Miao Xie8b110e32014-09-12 18:44:03 +08007959 bio_put(bio);
7960 }
7961
Omar Sandoval58efbc92017-08-22 23:45:59 -07007962 return status;
Miao Xie8b110e32014-09-12 18:44:03 +08007963}
7964
7965struct btrfs_retry_complete {
7966 struct completion done;
7967 struct inode *inode;
7968 u64 start;
7969 int uptodate;
7970};
7971
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007972static void btrfs_retry_endio_nocsum(struct bio *bio)
Miao Xie8b110e32014-09-12 18:44:03 +08007973{
7974 struct btrfs_retry_complete *done = bio->bi_private;
Josef Bacik7870d082017-05-05 11:57:15 -04007975 struct inode *inode = done->inode;
Miao Xie8b110e32014-09-12 18:44:03 +08007976 struct bio_vec *bvec;
Josef Bacik7870d082017-05-05 11:57:15 -04007977 struct extent_io_tree *io_tree, *failure_tree;
Ming Lei6dc4f102019-02-15 19:13:19 +08007978 struct bvec_iter_all iter_all;
Miao Xie8b110e32014-09-12 18:44:03 +08007979
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02007980 if (bio->bi_status)
Miao Xie8b110e32014-09-12 18:44:03 +08007981 goto end;
7982
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307983 ASSERT(bio->bi_vcnt == 1);
Josef Bacik7870d082017-05-05 11:57:15 -04007984 io_tree = &BTRFS_I(inode)->io_tree;
7985 failure_tree = &BTRFS_I(inode)->io_failure_tree;
Ming Lei263663c2017-12-18 20:22:04 +08007986 ASSERT(bio_first_bvec_all(bio)->bv_len == btrfs_inode_sectorsize(inode));
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307987
Miao Xie8b110e32014-09-12 18:44:03 +08007988 done->uptodate = 1;
David Sterbac09abff2017-07-13 18:10:07 +02007989 ASSERT(!bio_flagged(bio, BIO_CLONED));
Christoph Hellwig2b070cf2019-04-25 09:03:00 +02007990 bio_for_each_segment_all(bvec, bio, iter_all)
Josef Bacik7870d082017-05-05 11:57:15 -04007991 clean_io_failure(BTRFS_I(inode)->root->fs_info, failure_tree,
7992 io_tree, done->start, bvec->bv_page,
7993 btrfs_ino(BTRFS_I(inode)), 0);
Miao Xie8b110e32014-09-12 18:44:03 +08007994end:
7995 complete(&done->done);
7996 bio_put(bio);
7997}
7998
Omar Sandoval58efbc92017-08-22 23:45:59 -07007999static blk_status_t __btrfs_correct_data_nocsum(struct inode *inode,
8000 struct btrfs_io_bio *io_bio)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008001{
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308002 struct btrfs_fs_info *fs_info;
Liu Bo17347ce2017-05-15 15:33:27 -07008003 struct bio_vec bvec;
8004 struct bvec_iter iter;
Miao Xie8b110e32014-09-12 18:44:03 +08008005 struct btrfs_retry_complete done;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008006 u64 start;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308007 unsigned int pgoff;
8008 u32 sectorsize;
8009 int nr_sectors;
Omar Sandoval58efbc92017-08-22 23:45:59 -07008010 blk_status_t ret;
8011 blk_status_t err = BLK_STS_OK;
Miao Xiedc380ae2014-09-12 18:43:55 +08008012
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308013 fs_info = BTRFS_I(inode)->root->fs_info;
Jeff Mahoneyda170662016-06-15 09:22:56 -04008014 sectorsize = fs_info->sectorsize;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308015
Miao Xiec1dc0892014-09-12 18:43:56 +08008016 start = io_bio->logical;
Miao Xie8b110e32014-09-12 18:44:03 +08008017 done.inode = inode;
Liu Bo17347ce2017-05-15 15:33:27 -07008018 io_bio->bio.bi_iter = io_bio->iter;
Miao Xie8b110e32014-09-12 18:44:03 +08008019
Liu Bo17347ce2017-05-15 15:33:27 -07008020 bio_for_each_segment(bvec, &io_bio->bio, iter) {
8021 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec.bv_len);
8022 pgoff = bvec.bv_offset;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308023
8024next_block_or_try_again:
Miao Xie8b110e32014-09-12 18:44:03 +08008025 done.uptodate = 0;
8026 done.start = start;
8027 init_completion(&done.done);
8028
Liu Bo17347ce2017-05-15 15:33:27 -07008029 ret = dio_read_error(inode, &io_bio->bio, bvec.bv_page,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308030 pgoff, start, start + sectorsize - 1,
8031 io_bio->mirror_num,
8032 btrfs_retry_endio_nocsum, &done);
Liu Bo629ebf42017-05-15 17:20:07 -07008033 if (ret) {
8034 err = ret;
8035 goto next;
8036 }
Miao Xie8b110e32014-09-12 18:44:03 +08008037
David Sterba9c17f6c2017-07-19 19:26:45 +02008038 wait_for_completion_io(&done.done);
Miao Xie8b110e32014-09-12 18:44:03 +08008039
8040 if (!done.uptodate) {
8041 /* We might have another mirror, so try again */
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308042 goto next_block_or_try_again;
Miao Xie8b110e32014-09-12 18:44:03 +08008043 }
8044
Liu Bo629ebf42017-05-15 17:20:07 -07008045next:
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308046 start += sectorsize;
8047
Liu Bo97bf5a52017-04-07 13:11:10 -07008048 nr_sectors--;
8049 if (nr_sectors) {
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308050 pgoff += sectorsize;
Liu Bo97bf5a52017-04-07 13:11:10 -07008051 ASSERT(pgoff < PAGE_SIZE);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308052 goto next_block_or_try_again;
8053 }
Miao Xie8b110e32014-09-12 18:44:03 +08008054 }
8055
Liu Bo629ebf42017-05-15 17:20:07 -07008056 return err;
Miao Xie8b110e32014-09-12 18:44:03 +08008057}
8058
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008059static void btrfs_retry_endio(struct bio *bio)
Miao Xie8b110e32014-09-12 18:44:03 +08008060{
8061 struct btrfs_retry_complete *done = bio->bi_private;
8062 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
Josef Bacik7870d082017-05-05 11:57:15 -04008063 struct extent_io_tree *io_tree, *failure_tree;
8064 struct inode *inode = done->inode;
Miao Xie8b110e32014-09-12 18:44:03 +08008065 struct bio_vec *bvec;
8066 int uptodate;
8067 int ret;
Christoph Hellwig2b070cf2019-04-25 09:03:00 +02008068 int i = 0;
Ming Lei6dc4f102019-02-15 19:13:19 +08008069 struct bvec_iter_all iter_all;
Miao Xie8b110e32014-09-12 18:44:03 +08008070
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008071 if (bio->bi_status)
Miao Xie8b110e32014-09-12 18:44:03 +08008072 goto end;
8073
8074 uptodate = 1;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308075
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308076 ASSERT(bio->bi_vcnt == 1);
Ming Lei263663c2017-12-18 20:22:04 +08008077 ASSERT(bio_first_bvec_all(bio)->bv_len == btrfs_inode_sectorsize(done->inode));
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308078
Josef Bacik7870d082017-05-05 11:57:15 -04008079 io_tree = &BTRFS_I(inode)->io_tree;
8080 failure_tree = &BTRFS_I(inode)->io_failure_tree;
8081
David Sterbac09abff2017-07-13 18:10:07 +02008082 ASSERT(!bio_flagged(bio, BIO_CLONED));
Christoph Hellwig2b070cf2019-04-25 09:03:00 +02008083 bio_for_each_segment_all(bvec, bio, iter_all) {
Josef Bacik7870d082017-05-05 11:57:15 -04008084 ret = __readpage_endio_check(inode, io_bio, i, bvec->bv_page,
8085 bvec->bv_offset, done->start,
8086 bvec->bv_len);
Miao Xie8b110e32014-09-12 18:44:03 +08008087 if (!ret)
Josef Bacik7870d082017-05-05 11:57:15 -04008088 clean_io_failure(BTRFS_I(inode)->root->fs_info,
8089 failure_tree, io_tree, done->start,
8090 bvec->bv_page,
8091 btrfs_ino(BTRFS_I(inode)),
8092 bvec->bv_offset);
Miao Xie8b110e32014-09-12 18:44:03 +08008093 else
8094 uptodate = 0;
Christoph Hellwig2b070cf2019-04-25 09:03:00 +02008095 i++;
Miao Xie8b110e32014-09-12 18:44:03 +08008096 }
8097
8098 done->uptodate = uptodate;
8099end:
8100 complete(&done->done);
8101 bio_put(bio);
8102}
8103
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008104static blk_status_t __btrfs_subio_endio_read(struct inode *inode,
8105 struct btrfs_io_bio *io_bio, blk_status_t err)
Miao Xie8b110e32014-09-12 18:44:03 +08008106{
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308107 struct btrfs_fs_info *fs_info;
Liu Bo17347ce2017-05-15 15:33:27 -07008108 struct bio_vec bvec;
8109 struct bvec_iter iter;
Miao Xie8b110e32014-09-12 18:44:03 +08008110 struct btrfs_retry_complete done;
8111 u64 start;
8112 u64 offset = 0;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308113 u32 sectorsize;
8114 int nr_sectors;
8115 unsigned int pgoff;
8116 int csum_pos;
Liu Boef7cdac12017-04-13 18:11:48 -07008117 bool uptodate = (err == 0);
Miao Xie8b110e32014-09-12 18:44:03 +08008118 int ret;
Omar Sandoval58efbc92017-08-22 23:45:59 -07008119 blk_status_t status;
Miao Xie8b110e32014-09-12 18:44:03 +08008120
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308121 fs_info = BTRFS_I(inode)->root->fs_info;
Jeff Mahoneyda170662016-06-15 09:22:56 -04008122 sectorsize = fs_info->sectorsize;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308123
Omar Sandoval58efbc92017-08-22 23:45:59 -07008124 err = BLK_STS_OK;
Miao Xie8b110e32014-09-12 18:44:03 +08008125 start = io_bio->logical;
8126 done.inode = inode;
Liu Bo17347ce2017-05-15 15:33:27 -07008127 io_bio->bio.bi_iter = io_bio->iter;
Miao Xie8b110e32014-09-12 18:44:03 +08008128
Liu Bo17347ce2017-05-15 15:33:27 -07008129 bio_for_each_segment(bvec, &io_bio->bio, iter) {
8130 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec.bv_len);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308131
Liu Bo17347ce2017-05-15 15:33:27 -07008132 pgoff = bvec.bv_offset;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308133next_block:
Liu Boef7cdac12017-04-13 18:11:48 -07008134 if (uptodate) {
8135 csum_pos = BTRFS_BYTES_TO_BLKS(fs_info, offset);
8136 ret = __readpage_endio_check(inode, io_bio, csum_pos,
8137 bvec.bv_page, pgoff, start, sectorsize);
8138 if (likely(!ret))
8139 goto next;
8140 }
Miao Xie8b110e32014-09-12 18:44:03 +08008141try_again:
8142 done.uptodate = 0;
8143 done.start = start;
8144 init_completion(&done.done);
8145
Omar Sandoval58efbc92017-08-22 23:45:59 -07008146 status = dio_read_error(inode, &io_bio->bio, bvec.bv_page,
8147 pgoff, start, start + sectorsize - 1,
8148 io_bio->mirror_num, btrfs_retry_endio,
8149 &done);
8150 if (status) {
8151 err = status;
Miao Xie8b110e32014-09-12 18:44:03 +08008152 goto next;
8153 }
8154
David Sterba9c17f6c2017-07-19 19:26:45 +02008155 wait_for_completion_io(&done.done);
Miao Xie8b110e32014-09-12 18:44:03 +08008156
8157 if (!done.uptodate) {
8158 /* We might have another mirror, so try again */
8159 goto try_again;
8160 }
8161next:
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308162 offset += sectorsize;
8163 start += sectorsize;
8164
8165 ASSERT(nr_sectors);
8166
Liu Bo97bf5a52017-04-07 13:11:10 -07008167 nr_sectors--;
8168 if (nr_sectors) {
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308169 pgoff += sectorsize;
Liu Bo97bf5a52017-04-07 13:11:10 -07008170 ASSERT(pgoff < PAGE_SIZE);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308171 goto next_block;
8172 }
Kent Overstreet2c30c712013-11-07 12:20:26 -08008173 }
Miao Xiec1dc0892014-09-12 18:43:56 +08008174
8175 return err;
8176}
8177
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008178static blk_status_t btrfs_subio_endio_read(struct inode *inode,
8179 struct btrfs_io_bio *io_bio, blk_status_t err)
Miao Xie8b110e32014-09-12 18:44:03 +08008180{
8181 bool skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
8182
8183 if (skip_csum) {
8184 if (unlikely(err))
8185 return __btrfs_correct_data_nocsum(inode, io_bio);
8186 else
Omar Sandoval58efbc92017-08-22 23:45:59 -07008187 return BLK_STS_OK;
Miao Xie8b110e32014-09-12 18:44:03 +08008188 } else {
8189 return __btrfs_subio_endio_read(inode, io_bio, err);
8190 }
8191}
8192
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008193static void btrfs_endio_direct_read(struct bio *bio)
Miao Xiec1dc0892014-09-12 18:43:56 +08008194{
8195 struct btrfs_dio_private *dip = bio->bi_private;
8196 struct inode *inode = dip->inode;
8197 struct bio *dio_bio;
8198 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008199 blk_status_t err = bio->bi_status;
Miao Xiec1dc0892014-09-12 18:43:56 +08008200
Liu Bo99c4e3b92017-09-15 15:06:51 -06008201 if (dip->flags & BTRFS_DIO_ORIG_BIO_SUBMITTED)
Miao Xie8b110e32014-09-12 18:44:03 +08008202 err = btrfs_subio_endio_read(inode, io_bio, err);
Miao Xiec1dc0892014-09-12 18:43:56 +08008203
Josef Bacik4b46fce2010-05-23 11:00:55 -04008204 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01008205 dip->logical_offset + dip->bytes - 1);
Chris Mason9be33952013-05-17 18:30:14 -04008206 dio_bio = dip->dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008207
Josef Bacik4b46fce2010-05-23 11:00:55 -04008208 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04008209
Liu Bo99c4e3b92017-09-15 15:06:51 -06008210 dio_bio->bi_status = err;
Christoph Hellwig40553512017-06-03 09:37:58 +02008211 dio_end_io(dio_bio);
David Sterbab3a0dd52018-11-22 17:16:49 +01008212 btrfs_io_bio_free_csum(io_bio);
Chris Mason9be33952013-05-17 18:30:14 -04008213 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008214}
8215
Qu Wenruo524272602017-03-08 10:25:52 +08008216static void __endio_write_update_ordered(struct inode *inode,
8217 const u64 offset, const u64 bytes,
8218 const bool uptodate)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008219{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008220 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008221 struct btrfs_ordered_extent *ordered = NULL;
Qu Wenruo524272602017-03-08 10:25:52 +08008222 struct btrfs_workqueue *wq;
Filipe Manana14543772015-11-24 16:23:54 +00008223 u64 ordered_offset = offset;
8224 u64 ordered_bytes = bytes;
Naohiro Aota67c003f2017-09-01 17:59:07 +09008225 u64 last_offset;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008226
Omar Sandovala0cac0e2019-09-16 11:30:57 -07008227 if (btrfs_is_free_space_inode(BTRFS_I(inode)))
Qu Wenruo524272602017-03-08 10:25:52 +08008228 wq = fs_info->endio_freespace_worker;
Omar Sandovala0cac0e2019-09-16 11:30:57 -07008229 else
Qu Wenruo524272602017-03-08 10:25:52 +08008230 wq = fs_info->endio_write_workers;
Qu Wenruo524272602017-03-08 10:25:52 +08008231
Nikolay Borisovb25f0d02018-04-11 11:21:17 +03008232 while (ordered_offset < offset + bytes) {
8233 last_offset = ordered_offset;
8234 if (btrfs_dec_test_first_ordered_pending(inode, &ordered,
8235 &ordered_offset,
8236 ordered_bytes,
8237 uptodate)) {
Omar Sandovala0cac0e2019-09-16 11:30:57 -07008238 btrfs_init_work(&ordered->work, finish_ordered_fn, NULL,
8239 NULL);
Nikolay Borisovb25f0d02018-04-11 11:21:17 +03008240 btrfs_queue_work(wq, &ordered->work);
8241 }
8242 /*
8243 * If btrfs_dec_test_ordered_pending does not find any ordered
8244 * extent in the range, we can exit.
8245 */
8246 if (ordered_offset == last_offset)
8247 return;
8248 /*
8249 * Our bio might span multiple ordered extents. In this case
Andrea Gelmini52042d82018-11-28 12:05:13 +01008250 * we keep going until we have accounted the whole dio.
Nikolay Borisovb25f0d02018-04-11 11:21:17 +03008251 */
8252 if (ordered_offset < offset + bytes) {
8253 ordered_bytes = offset + bytes - ordered_offset;
8254 ordered = NULL;
8255 }
Chris Mason163cf092010-11-28 19:56:33 -05008256 }
Filipe Manana14543772015-11-24 16:23:54 +00008257}
8258
8259static void btrfs_endio_direct_write(struct bio *bio)
8260{
8261 struct btrfs_dio_private *dip = bio->bi_private;
8262 struct bio *dio_bio = dip->dio_bio;
8263
Qu Wenruo524272602017-03-08 10:25:52 +08008264 __endio_write_update_ordered(dip->inode, dip->logical_offset,
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008265 dip->bytes, !bio->bi_status);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008266
Josef Bacik4b46fce2010-05-23 11:00:55 -04008267 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04008268
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008269 dio_bio->bi_status = bio->bi_status;
Christoph Hellwig40553512017-06-03 09:37:58 +02008270 dio_end_io(dio_bio);
Chris Mason9be33952013-05-17 18:30:14 -04008271 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008272}
8273
David Sterbad0ee3932018-03-08 14:35:48 +01008274static blk_status_t btrfs_submit_bio_start_direct_io(void *private_data,
David Sterbad0779292018-03-08 13:47:33 +01008275 struct bio *bio, u64 offset)
Chris Masoneaf25d92010-05-25 09:48:28 -04008276{
Josef Bacikc6100a42017-05-05 11:57:13 -04008277 struct inode *inode = private_data;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008278 blk_status_t ret;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008279 ret = btrfs_csum_one_bio(inode, bio, offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008280 BUG_ON(ret); /* -ENOMEM */
Chris Masoneaf25d92010-05-25 09:48:28 -04008281 return 0;
8282}
8283
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008284static void btrfs_end_dio_bio(struct bio *bio)
Miao Xiee65e1532010-11-22 03:04:43 +00008285{
8286 struct btrfs_dio_private *dip = bio->bi_private;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008287 blk_status_t err = bio->bi_status;
Miao Xiee65e1532010-11-22 03:04:43 +00008288
Miao Xie8b110e32014-09-12 18:44:03 +08008289 if (err)
8290 btrfs_warn(BTRFS_I(dip->inode)->root->fs_info,
Mike Christie6296b962016-06-05 14:32:21 -05008291 "direct IO failed ino %llu rw %d,%u sector %#Lx len %u err no %d",
David Sterbaf85b7372017-01-20 14:54:07 +01008292 btrfs_ino(BTRFS_I(dip->inode)), bio_op(bio),
8293 bio->bi_opf,
Miao Xie8b110e32014-09-12 18:44:03 +08008294 (unsigned long long)bio->bi_iter.bi_sector,
8295 bio->bi_iter.bi_size, err);
8296
8297 if (dip->subio_endio)
8298 err = dip->subio_endio(dip->inode, btrfs_io_bio(bio), err);
Miao Xiec1dc0892014-09-12 18:43:56 +08008299
8300 if (err) {
Miao Xiee65e1532010-11-22 03:04:43 +00008301 /*
Nikolay Borisovde224b72018-02-14 10:53:36 +02008302 * We want to perceive the errors flag being set before
8303 * decrementing the reference count. We don't need a barrier
8304 * since atomic operations with a return value are fully
8305 * ordered as per atomic_t.txt
Miao Xiee65e1532010-11-22 03:04:43 +00008306 */
Nikolay Borisovde224b72018-02-14 10:53:36 +02008307 dip->errors = 1;
Miao Xiee65e1532010-11-22 03:04:43 +00008308 }
8309
8310 /* if there are more bios still pending for this dio, just exit */
8311 if (!atomic_dec_and_test(&dip->pending_bios))
8312 goto out;
8313
Chris Mason9be33952013-05-17 18:30:14 -04008314 if (dip->errors) {
Miao Xiee65e1532010-11-22 03:04:43 +00008315 bio_io_error(dip->orig_bio);
Chris Mason9be33952013-05-17 18:30:14 -04008316 } else {
Anand Jain2dbe0c72017-10-14 08:35:56 +08008317 dip->dio_bio->bi_status = BLK_STS_OK;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008318 bio_endio(dip->orig_bio);
Miao Xiee65e1532010-11-22 03:04:43 +00008319 }
8320out:
8321 bio_put(bio);
8322}
8323
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008324static inline blk_status_t btrfs_lookup_and_bind_dio_csum(struct inode *inode,
Miao Xiec1dc0892014-09-12 18:43:56 +08008325 struct btrfs_dio_private *dip,
8326 struct bio *bio,
8327 u64 file_offset)
8328{
8329 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
8330 struct btrfs_io_bio *orig_io_bio = btrfs_io_bio(dip->orig_bio);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008331 blk_status_t ret;
Miao Xiec1dc0892014-09-12 18:43:56 +08008332
8333 /*
8334 * We load all the csum data we need when we submit
8335 * the first bio to reduce the csum tree search and
8336 * contention.
8337 */
8338 if (dip->logical_offset == file_offset) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008339 ret = btrfs_lookup_bio_sums_dio(inode, dip->orig_bio,
Miao Xiec1dc0892014-09-12 18:43:56 +08008340 file_offset);
8341 if (ret)
8342 return ret;
8343 }
8344
8345 if (bio == dip->orig_bio)
8346 return 0;
8347
8348 file_offset -= dip->logical_offset;
8349 file_offset >>= inode->i_sb->s_blocksize_bits;
8350 io_bio->csum = (u8 *)(((u32 *)orig_io_bio->csum) + file_offset);
8351
8352 return 0;
8353}
8354
David Sterbad0ee3932018-03-08 14:35:48 +01008355static inline blk_status_t btrfs_submit_dio_bio(struct bio *bio,
8356 struct inode *inode, u64 file_offset, int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00008357{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008358 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xiefacc8a222013-07-25 19:22:34 +08008359 struct btrfs_dio_private *dip = bio->bi_private;
Mike Christie37226b22016-06-05 14:31:52 -05008360 bool write = bio_op(bio) == REQ_OP_WRITE;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008361 blk_status_t ret;
Miao Xiee65e1532010-11-22 03:04:43 +00008362
Liu Bo4c274bc2017-11-01 17:19:27 -06008363 /* Check btrfs_submit_bio_hook() for rules about async submit. */
Josef Bacikb812ce22012-11-16 13:56:32 -05008364 if (async_submit)
8365 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
8366
Josef Bacik5fd02042012-05-02 14:00:54 -04008367 if (!write) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008368 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DATA);
Josef Bacik5fd02042012-05-02 14:00:54 -04008369 if (ret)
8370 goto err;
8371 }
Miao Xiee65e1532010-11-22 03:04:43 +00008372
Nikolay Borisove6961ca2017-08-03 15:44:58 +03008373 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Josef Bacik1ae39932011-04-06 14:41:34 -04008374 goto map;
8375
8376 if (write && async_submit) {
Josef Bacikc6100a42017-05-05 11:57:13 -04008377 ret = btrfs_wq_submit_bio(fs_info, bio, 0, 0,
8378 file_offset, inode,
David Sterbae288c082018-07-18 17:36:24 +02008379 btrfs_submit_bio_start_direct_io);
Miao Xiee65e1532010-11-22 03:04:43 +00008380 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04008381 } else if (write) {
8382 /*
8383 * If we aren't doing async submit, calculate the csum of the
8384 * bio now.
8385 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008386 ret = btrfs_csum_one_bio(inode, bio, file_offset, 1);
Josef Bacik1ae39932011-04-06 14:41:34 -04008387 if (ret)
8388 goto err;
Miao Xie23ea8e52014-09-12 18:43:54 +08008389 } else {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008390 ret = btrfs_lookup_and_bind_dio_csum(inode, dip, bio,
Miao Xiec1dc0892014-09-12 18:43:56 +08008391 file_offset);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00008392 if (ret)
8393 goto err;
8394 }
Josef Bacik1ae39932011-04-06 14:41:34 -04008395map:
Chris Mason08635ba2019-07-10 12:28:14 -07008396 ret = btrfs_map_bio(fs_info, bio, 0);
Miao Xiee65e1532010-11-22 03:04:43 +00008397err:
Miao Xiee65e1532010-11-22 03:04:43 +00008398 return ret;
8399}
8400
Nikolay Borisove6961ca2017-08-03 15:44:58 +03008401static int btrfs_submit_direct_hook(struct btrfs_dio_private *dip)
Miao Xiee65e1532010-11-22 03:04:43 +00008402{
8403 struct inode *inode = dip->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008404 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xiee65e1532010-11-22 03:04:43 +00008405 struct bio *bio;
8406 struct bio *orig_bio = dip->orig_bio;
Kent Overstreet4f024f32013-10-11 15:44:27 -07008407 u64 start_sector = orig_bio->bi_iter.bi_sector;
Miao Xiee65e1532010-11-22 03:04:43 +00008408 u64 file_offset = dip->logical_offset;
Josef Bacik1ae39932011-04-06 14:41:34 -04008409 int async_submit = 0;
Liu Bo725130b2017-05-16 09:51:39 -07008410 u64 submit_len;
8411 int clone_offset = 0;
8412 int clone_len;
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05308413 int ret;
Omar Sandoval58efbc92017-08-22 23:45:59 -07008414 blk_status_t status;
Nikolay Borisov89b798a2019-06-03 12:05:05 +03008415 struct btrfs_io_geometry geom;
Miao Xiee65e1532010-11-22 03:04:43 +00008416
Nikolay Borisov89b798a2019-06-03 12:05:05 +03008417 submit_len = orig_bio->bi_iter.bi_size;
8418 ret = btrfs_get_io_geometry(fs_info, btrfs_op(orig_bio),
8419 start_sector << 9, submit_len, &geom);
Miao Xie7a5c3c92014-06-17 18:58:59 +08008420 if (ret)
Miao Xiee65e1532010-11-22 03:04:43 +00008421 return -EIO;
Miao Xiefacc8a222013-07-25 19:22:34 +08008422
Nikolay Borisov89b798a2019-06-03 12:05:05 +03008423 if (geom.len >= submit_len) {
Josef Bacik02f57c72011-04-06 14:25:44 -04008424 bio = orig_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08008425 dip->flags |= BTRFS_DIO_ORIG_BIO_SUBMITTED;
Josef Bacik02f57c72011-04-06 14:25:44 -04008426 goto submit;
8427 }
8428
David Woodhouse53b381b2013-01-29 18:40:14 -05008429 /* async crcs make it difficult to collect full stripe writes. */
Jeff Mahoney1b868262017-05-17 11:38:35 -04008430 if (btrfs_data_alloc_profile(fs_info) & BTRFS_BLOCK_GROUP_RAID56_MASK)
David Woodhouse53b381b2013-01-29 18:40:14 -05008431 async_submit = 0;
8432 else
8433 async_submit = 1;
8434
Liu Bo725130b2017-05-16 09:51:39 -07008435 /* bio split */
Nikolay Borisov89b798a2019-06-03 12:05:05 +03008436 ASSERT(geom.len <= INT_MAX);
Josef Bacik02f57c72011-04-06 14:25:44 -04008437 atomic_inc(&dip->pending_bios);
Arnd Bergmann3c91ee62017-05-18 15:33:29 +02008438 do {
Nikolay Borisov89b798a2019-06-03 12:05:05 +03008439 clone_len = min_t(int, submit_len, geom.len);
Josef Bacik02f57c72011-04-06 14:25:44 -04008440
Liu Bo725130b2017-05-16 09:51:39 -07008441 /*
8442 * This will never fail as it's passing GPF_NOFS and
8443 * the allocation is backed by btrfs_bioset.
8444 */
Liu Boe4770942017-05-16 10:57:14 -07008445 bio = btrfs_bio_clone_partial(orig_bio, clone_offset,
Liu Bo725130b2017-05-16 09:51:39 -07008446 clone_len);
8447 bio->bi_private = dip;
8448 bio->bi_end_io = btrfs_end_dio_bio;
8449 btrfs_io_bio(bio)->logical = file_offset;
Miao Xiee65e1532010-11-22 03:04:43 +00008450
Liu Bo725130b2017-05-16 09:51:39 -07008451 ASSERT(submit_len >= clone_len);
8452 submit_len -= clone_len;
8453 if (submit_len == 0)
8454 break;
Miao Xiee65e1532010-11-22 03:04:43 +00008455
Liu Bo725130b2017-05-16 09:51:39 -07008456 /*
8457 * Increase the count before we submit the bio so we know
8458 * the end IO handler won't happen before we increase the
8459 * count. Otherwise, the dip might get freed before we're
8460 * done setting it up.
8461 */
8462 atomic_inc(&dip->pending_bios);
Miao Xiee65e1532010-11-22 03:04:43 +00008463
David Sterbad0ee3932018-03-08 14:35:48 +01008464 status = btrfs_submit_dio_bio(bio, inode, file_offset,
Omar Sandoval58efbc92017-08-22 23:45:59 -07008465 async_submit);
8466 if (status) {
Liu Bo725130b2017-05-16 09:51:39 -07008467 bio_put(bio);
8468 atomic_dec(&dip->pending_bios);
8469 goto out_err;
Miao Xiee65e1532010-11-22 03:04:43 +00008470 }
Liu Bo725130b2017-05-16 09:51:39 -07008471
8472 clone_offset += clone_len;
8473 start_sector += clone_len >> 9;
8474 file_offset += clone_len;
8475
Nikolay Borisov89b798a2019-06-03 12:05:05 +03008476 ret = btrfs_get_io_geometry(fs_info, btrfs_op(orig_bio),
8477 start_sector << 9, submit_len, &geom);
Liu Bo725130b2017-05-16 09:51:39 -07008478 if (ret)
8479 goto out_err;
Arnd Bergmann3c91ee62017-05-18 15:33:29 +02008480 } while (submit_len > 0);
Miao Xiee65e1532010-11-22 03:04:43 +00008481
Josef Bacik02f57c72011-04-06 14:25:44 -04008482submit:
David Sterbad0ee3932018-03-08 14:35:48 +01008483 status = btrfs_submit_dio_bio(bio, inode, file_offset, async_submit);
Omar Sandoval58efbc92017-08-22 23:45:59 -07008484 if (!status)
Miao Xiee65e1532010-11-22 03:04:43 +00008485 return 0;
8486
8487 bio_put(bio);
8488out_err:
8489 dip->errors = 1;
8490 /*
Nikolay Borisovde224b72018-02-14 10:53:36 +02008491 * Before atomic variable goto zero, we must make sure dip->errors is
8492 * perceived to be set. This ordering is ensured by the fact that an
8493 * atomic operations with a return value are fully ordered as per
8494 * atomic_t.txt
Miao Xiee65e1532010-11-22 03:04:43 +00008495 */
Miao Xiee65e1532010-11-22 03:04:43 +00008496 if (atomic_dec_and_test(&dip->pending_bios))
8497 bio_io_error(dip->orig_bio);
8498
8499 /* bio_end_io() will handle error, so we needn't return it */
8500 return 0;
8501}
8502
Mike Christie8a4c1e42016-06-05 14:31:50 -05008503static void btrfs_submit_direct(struct bio *dio_bio, struct inode *inode,
8504 loff_t file_offset)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008505{
Filipe Manana61de7182015-07-01 12:13:10 +01008506 struct btrfs_dio_private *dip = NULL;
Liu Bo3892ac92017-04-17 15:00:28 -07008507 struct bio *bio = NULL;
8508 struct btrfs_io_bio *io_bio;
Mike Christie8a4c1e42016-06-05 14:31:50 -05008509 bool write = (bio_op(dio_bio) == REQ_OP_WRITE);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008510 int ret = 0;
8511
David Sterba8b6c1d52017-06-02 17:48:13 +02008512 bio = btrfs_bio_clone(dio_bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008513
Miao Xiec1dc0892014-09-12 18:43:56 +08008514 dip = kzalloc(sizeof(*dip), GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04008515 if (!dip) {
8516 ret = -ENOMEM;
Filipe Manana61de7182015-07-01 12:13:10 +01008517 goto free_ordered;
Chris Mason9be33952013-05-17 18:30:14 -04008518 }
8519
8520 dip->private = dio_bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008521 dip->inode = inode;
8522 dip->logical_offset = file_offset;
Kent Overstreet4f024f32013-10-11 15:44:27 -07008523 dip->bytes = dio_bio->bi_iter.bi_size;
8524 dip->disk_bytenr = (u64)dio_bio->bi_iter.bi_sector << 9;
Liu Bo3892ac92017-04-17 15:00:28 -07008525 bio->bi_private = dip;
8526 dip->orig_bio = bio;
Chris Mason9be33952013-05-17 18:30:14 -04008527 dip->dio_bio = dio_bio;
Miao Xiee65e1532010-11-22 03:04:43 +00008528 atomic_set(&dip->pending_bios, 0);
Liu Bo3892ac92017-04-17 15:00:28 -07008529 io_bio = btrfs_io_bio(bio);
8530 io_bio->logical = file_offset;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008531
Miao Xiec1dc0892014-09-12 18:43:56 +08008532 if (write) {
Liu Bo3892ac92017-04-17 15:00:28 -07008533 bio->bi_end_io = btrfs_endio_direct_write;
Miao Xiec1dc0892014-09-12 18:43:56 +08008534 } else {
Liu Bo3892ac92017-04-17 15:00:28 -07008535 bio->bi_end_io = btrfs_endio_direct_read;
Miao Xiec1dc0892014-09-12 18:43:56 +08008536 dip->subio_endio = btrfs_subio_endio_read;
8537 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04008538
Filipe Mananaf28a4922015-12-08 19:23:20 +00008539 /*
8540 * Reset the range for unsubmitted ordered extents (to a 0 length range)
8541 * even if we fail to submit a bio, because in such case we do the
8542 * corresponding error handling below and it must not be done a second
8543 * time by btrfs_direct_IO().
8544 */
8545 if (write) {
8546 struct btrfs_dio_data *dio_data = current->journal_info;
8547
8548 dio_data->unsubmitted_oe_range_end = dip->logical_offset +
8549 dip->bytes;
8550 dio_data->unsubmitted_oe_range_start =
8551 dio_data->unsubmitted_oe_range_end;
8552 }
8553
Nikolay Borisove6961ca2017-08-03 15:44:58 +03008554 ret = btrfs_submit_direct_hook(dip);
Miao Xiee65e1532010-11-22 03:04:43 +00008555 if (!ret)
Chris Masoneaf25d92010-05-25 09:48:28 -04008556 return;
Chris Mason9be33952013-05-17 18:30:14 -04008557
David Sterbab3a0dd52018-11-22 17:16:49 +01008558 btrfs_io_bio_free_csum(io_bio);
Chris Mason9be33952013-05-17 18:30:14 -04008559
Josef Bacik4b46fce2010-05-23 11:00:55 -04008560free_ordered:
8561 /*
Filipe Manana61de7182015-07-01 12:13:10 +01008562 * If we arrived here it means either we failed to submit the dip
8563 * or we either failed to clone the dio_bio or failed to allocate the
8564 * dip. If we cloned the dio_bio and allocated the dip, we can just
8565 * call bio_endio against our io_bio so that we get proper resource
8566 * cleanup if we fail to submit the dip, otherwise, we must do the
8567 * same as btrfs_endio_direct_[write|read] because we can't call these
8568 * callbacks - they require an allocated dip and a clone of dio_bio.
Josef Bacik4b46fce2010-05-23 11:00:55 -04008569 */
Liu Bo3892ac92017-04-17 15:00:28 -07008570 if (bio && dip) {
Guoqing Jiang054ec2f2017-06-02 16:08:50 +08008571 bio_io_error(bio);
Filipe Manana61de7182015-07-01 12:13:10 +01008572 /*
Liu Bo3892ac92017-04-17 15:00:28 -07008573 * The end io callbacks free our dip, do the final put on bio
Filipe Manana61de7182015-07-01 12:13:10 +01008574 * and all the cleanup and final put for dio_bio (through
8575 * dio_end_io()).
8576 */
8577 dip = NULL;
Liu Bo3892ac92017-04-17 15:00:28 -07008578 bio = NULL;
Filipe Manana61de7182015-07-01 12:13:10 +01008579 } else {
Filipe Manana14543772015-11-24 16:23:54 +00008580 if (write)
Qu Wenruo524272602017-03-08 10:25:52 +08008581 __endio_write_update_ordered(inode,
Filipe Manana14543772015-11-24 16:23:54 +00008582 file_offset,
8583 dio_bio->bi_iter.bi_size,
Qu Wenruo524272602017-03-08 10:25:52 +08008584 false);
Filipe Manana14543772015-11-24 16:23:54 +00008585 else
Filipe Manana61de7182015-07-01 12:13:10 +01008586 unlock_extent(&BTRFS_I(inode)->io_tree, file_offset,
8587 file_offset + dio_bio->bi_iter.bi_size - 1);
Filipe Manana14543772015-11-24 16:23:54 +00008588
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008589 dio_bio->bi_status = BLK_STS_IOERR;
Filipe Manana61de7182015-07-01 12:13:10 +01008590 /*
8591 * Releases and cleans up our dio_bio, no need to bio_put()
8592 * nor bio_endio()/bio_io_error() against dio_bio.
8593 */
Christoph Hellwig40553512017-06-03 09:37:58 +02008594 dio_end_io(dio_bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008595 }
Liu Bo3892ac92017-04-17 15:00:28 -07008596 if (bio)
8597 bio_put(bio);
Filipe Manana61de7182015-07-01 12:13:10 +01008598 kfree(dip);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008599}
8600
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008601static ssize_t check_direct_IO(struct btrfs_fs_info *fs_info,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008602 const struct iov_iter *iter, loff_t offset)
Chris Mason5a5f79b2010-05-26 21:33:37 -04008603{
8604 int seg;
Josef Bacika1b75f72011-04-08 15:51:18 +00008605 int i;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008606 unsigned int blocksize_mask = fs_info->sectorsize - 1;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008607 ssize_t retval = -EINVAL;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008608
8609 if (offset & blocksize_mask)
8610 goto out;
8611
Al Viro28060d52014-03-22 05:15:17 -04008612 if (iov_iter_alignment(iter) & blocksize_mask)
8613 goto out;
Josef Bacika1b75f72011-04-08 15:51:18 +00008614
Al Viro28060d52014-03-22 05:15:17 -04008615 /* If this is a write we don't need to check anymore */
Al Virocd27e452016-10-10 13:39:05 -04008616 if (iov_iter_rw(iter) != READ || !iter_is_iovec(iter))
Al Viro28060d52014-03-22 05:15:17 -04008617 return 0;
8618 /*
8619 * Check to make sure we don't have duplicate iov_base's in this
8620 * iovec, if so return EINVAL, otherwise we'll get csum errors
8621 * when reading back.
8622 */
8623 for (seg = 0; seg < iter->nr_segs; seg++) {
8624 for (i = seg + 1; i < iter->nr_segs; i++) {
8625 if (iter->iov[seg].iov_base == iter->iov[i].iov_base)
Josef Bacika1b75f72011-04-08 15:51:18 +00008626 goto out;
8627 }
Chris Mason5a5f79b2010-05-26 21:33:37 -04008628 }
8629 retval = 0;
8630out:
8631 return retval;
8632}
Josef Bacikeb838e72012-07-31 16:28:48 -04008633
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008634static ssize_t btrfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
Chris Mason16432982008-04-10 10:23:21 -04008635{
Josef Bacik4b46fce2010-05-23 11:00:55 -04008636 struct file *file = iocb->ki_filp;
8637 struct inode *inode = file->f_mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008638 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
chandan50745b02015-08-28 21:10:13 +05308639 struct btrfs_dio_data dio_data = { 0 };
Qu Wenruo364ecf32017-02-27 15:10:38 +08008640 struct extent_changeset *data_reserved = NULL;
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008641 loff_t offset = iocb->ki_pos;
Miao Xie09348562013-02-07 10:12:07 +00008642 size_t count = 0;
Miao Xie2e60a512013-02-08 07:01:08 +00008643 int flags = 0;
Miao Xie38851cc2013-02-08 07:04:11 +00008644 bool wakeup = true;
8645 bool relock = false;
Miao Xie09348562013-02-07 10:12:07 +00008646 ssize_t ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008647
Nikolay Borisov8c70c9f2017-08-21 12:43:46 +03008648 if (check_direct_IO(fs_info, iter, offset))
Chris Mason5a5f79b2010-05-26 21:33:37 -04008649 return 0;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008650
Jens Axboefe0f07d2015-04-15 17:05:48 -06008651 inode_dio_begin(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008652
Josef Bacik0e267c42013-07-02 10:38:02 -04008653 /*
Miao Xie41bd9ca2014-03-06 13:54:57 +08008654 * The generic stuff only does filemap_write_and_wait_range, which
8655 * isn't enough if we've written compressed pages to this area, so
8656 * we need to flush the dirty pages again to make absolutely sure
8657 * that any outstanding dirty pages are on disk.
Josef Bacik0e267c42013-07-02 10:38:02 -04008658 */
Al Viroa6cbcd42014-03-04 22:38:00 -05008659 count = iov_iter_count(iter);
Miao Xie41bd9ca2014-03-06 13:54:57 +08008660 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
8661 &BTRFS_I(inode)->runtime_flags))
Wang Shilong9a025a02014-07-17 11:44:13 +08008662 filemap_fdatawrite_range(inode->i_mapping, offset,
8663 offset + count - 1);
Josef Bacik0e267c42013-07-02 10:38:02 -04008664
Omar Sandoval6f673762015-03-16 04:33:52 -07008665 if (iov_iter_rw(iter) == WRITE) {
Miao Xie38851cc2013-02-08 07:04:11 +00008666 /*
8667 * If the write DIO is beyond the EOF, we need update
8668 * the isize, but it is protected by i_mutex. So we can
8669 * not unlock the i_mutex at this case.
8670 */
8671 if (offset + count <= inode->i_size) {
Liu Bo4aaedfb2016-12-14 22:36:05 -08008672 dio_data.overwrite = 1;
Al Viro59551022016-01-22 15:40:57 -05008673 inode_unlock(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008674 relock = true;
Goldwyn Rodriguesedf064e2017-06-20 07:05:49 -05008675 } else if (iocb->ki_flags & IOCB_NOWAIT) {
8676 ret = -EAGAIN;
8677 goto out;
Miao Xie38851cc2013-02-08 07:04:11 +00008678 }
Qu Wenruo364ecf32017-02-27 15:10:38 +08008679 ret = btrfs_delalloc_reserve_space(inode, &data_reserved,
8680 offset, count);
Miao Xie09348562013-02-07 10:12:07 +00008681 if (ret)
Miao Xie38851cc2013-02-08 07:04:11 +00008682 goto out;
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008683
8684 /*
8685 * We need to know how many extents we reserved so that we can
8686 * do the accounting properly if we go over the number we
8687 * originally calculated. Abuse current->journal_info for this.
8688 */
Jeff Mahoneyda170662016-06-15 09:22:56 -04008689 dio_data.reserve = round_up(count,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008690 fs_info->sectorsize);
Filipe Mananaf28a4922015-12-08 19:23:20 +00008691 dio_data.unsubmitted_oe_range_start = (u64)offset;
8692 dio_data.unsubmitted_oe_range_end = (u64)offset;
chandan50745b02015-08-28 21:10:13 +05308693 current->journal_info = &dio_data;
Chandan Rajendra97dcdea2016-12-23 15:00:18 +05308694 down_read(&BTRFS_I(inode)->dio_sem);
David Sterbaee39b432014-09-30 01:33:33 +02008695 } else if (test_bit(BTRFS_INODE_READDIO_NEED_LOCK,
8696 &BTRFS_I(inode)->runtime_flags)) {
Jens Axboefe0f07d2015-04-15 17:05:48 -06008697 inode_dio_end(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008698 flags = DIO_LOCKING | DIO_SKIP_HOLES;
8699 wakeup = false;
Miao Xie09348562013-02-07 10:12:07 +00008700 }
8701
Omar Sandoval17f8c842015-03-16 04:33:50 -07008702 ret = __blockdev_direct_IO(iocb, inode,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008703 fs_info->fs_devices->latest_bdev,
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008704 iter, btrfs_get_blocks_direct, NULL,
Omar Sandoval17f8c842015-03-16 04:33:50 -07008705 btrfs_submit_direct, flags);
Omar Sandoval6f673762015-03-16 04:33:52 -07008706 if (iov_iter_rw(iter) == WRITE) {
Chandan Rajendra97dcdea2016-12-23 15:00:18 +05308707 up_read(&BTRFS_I(inode)->dio_sem);
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008708 current->journal_info = NULL;
Liu Boddba1bf2015-06-17 16:59:58 +08008709 if (ret < 0 && ret != -EIOCBQUEUED) {
chandan50745b02015-08-28 21:10:13 +05308710 if (dio_data.reserve)
Qu Wenruobc42bda2017-02-27 15:10:39 +08008711 btrfs_delalloc_release_space(inode, data_reserved,
Qu Wenruo43b18592017-12-12 15:34:32 +08008712 offset, dio_data.reserve, true);
Filipe Mananaf28a4922015-12-08 19:23:20 +00008713 /*
8714 * On error we might have left some ordered extents
8715 * without submitting corresponding bios for them, so
8716 * cleanup them up to avoid other tasks getting them
8717 * and waiting for them to complete forever.
8718 */
8719 if (dio_data.unsubmitted_oe_range_start <
8720 dio_data.unsubmitted_oe_range_end)
Qu Wenruo524272602017-03-08 10:25:52 +08008721 __endio_write_update_ordered(inode,
Filipe Mananaf28a4922015-12-08 19:23:20 +00008722 dio_data.unsubmitted_oe_range_start,
8723 dio_data.unsubmitted_oe_range_end -
8724 dio_data.unsubmitted_oe_range_start,
Qu Wenruo524272602017-03-08 10:25:52 +08008725 false);
Liu Boddba1bf2015-06-17 16:59:58 +08008726 } else if (ret >= 0 && (size_t)ret < count)
Qu Wenruobc42bda2017-02-27 15:10:39 +08008727 btrfs_delalloc_release_space(inode, data_reserved,
Qu Wenruo43b18592017-12-12 15:34:32 +08008728 offset, count - (size_t)ret, true);
Qu Wenruo8702ba92019-10-14 14:34:51 +08008729 btrfs_delalloc_release_extents(BTRFS_I(inode), count);
Miao Xie09348562013-02-07 10:12:07 +00008730 }
Miao Xie38851cc2013-02-08 07:04:11 +00008731out:
Miao Xie2e60a512013-02-08 07:01:08 +00008732 if (wakeup)
Jens Axboefe0f07d2015-04-15 17:05:48 -06008733 inode_dio_end(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008734 if (relock)
Al Viro59551022016-01-22 15:40:57 -05008735 inode_lock(inode);
Miao Xie09348562013-02-07 10:12:07 +00008736
Qu Wenruo364ecf32017-02-27 15:10:38 +08008737 extent_changeset_free(data_reserved);
Miao Xie09348562013-02-07 10:12:07 +00008738 return ret;
Chris Mason16432982008-04-10 10:23:21 -04008739}
8740
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008741#define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
8742
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008743static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
8744 __u64 start, __u64 len)
8745{
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008746 int ret;
8747
8748 ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS);
8749 if (ret)
8750 return ret;
8751
David Sterba2135fb92017-06-23 04:09:57 +02008752 return extent_fiemap(inode, fieinfo, start, len);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008753}
8754
Chris Mason9ebefb182007-06-15 13:50:00 -04008755int btrfs_readpage(struct file *file, struct page *page)
8756{
Chris Masond1310b22008-01-24 16:13:08 -05008757 struct extent_io_tree *tree;
8758 tree = &BTRFS_I(page->mapping->host)->io_tree;
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02008759 return extent_read_full_page(tree, page, btrfs_get_extent, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04008760}
Chris Mason1832a6d2007-12-21 16:27:21 -05008761
Chris Mason39279cc2007-06-12 06:35:45 -04008762static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
8763{
Josef Bacikbe7bd732015-10-22 15:05:09 -04008764 struct inode *inode = page->mapping->host;
8765 int ret;
Chris Masonb888db22007-08-27 16:49:44 -04008766
8767 if (current->flags & PF_MEMALLOC) {
8768 redirty_page_for_writepage(wbc, page);
8769 unlock_page(page);
8770 return 0;
8771 }
Josef Bacikbe7bd732015-10-22 15:05:09 -04008772
8773 /*
8774 * If we are under memory pressure we will call this directly from the
8775 * VM, we need to make sure we have the inode referenced for the ordered
8776 * extent. If not just return like we didn't do anything.
8777 */
8778 if (!igrab(inode)) {
8779 redirty_page_for_writepage(wbc, page);
8780 return AOP_WRITEPAGE_ACTIVATE;
8781 }
Nikolay Borisov0a9b0e52017-12-08 15:55:59 +02008782 ret = extent_write_full_page(page, wbc);
Josef Bacikbe7bd732015-10-22 15:05:09 -04008783 btrfs_add_delayed_iput(inode);
8784 return ret;
Chris Masona52d9a82007-08-27 16:49:44 -04008785}
Chris Mason39279cc2007-06-12 06:35:45 -04008786
Eric Sandeen48a3b632013-04-25 20:41:01 +00008787static int btrfs_writepages(struct address_space *mapping,
8788 struct writeback_control *wbc)
Chris Masonb293f02e2007-11-01 19:45:34 -04008789{
Nikolay Borisov8ae225a2018-04-19 10:46:38 +03008790 return extent_writepages(mapping, wbc);
Chris Masonb293f02e2007-11-01 19:45:34 -04008791}
8792
Chris Mason3ab2fb52007-11-08 10:59:22 -05008793static int
8794btrfs_readpages(struct file *file, struct address_space *mapping,
8795 struct list_head *pages, unsigned nr_pages)
8796{
Nikolay Borisov2a3ff0a2018-04-19 10:46:36 +03008797 return extent_readpages(mapping, pages, nr_pages);
Chris Mason3ab2fb52007-11-08 10:59:22 -05008798}
Nikolay Borisov2a3ff0a2018-04-19 10:46:36 +03008799
Chris Masone6dcd2d2008-07-17 12:53:50 -04008800static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04008801{
Nikolay Borisov477a30b2018-04-19 10:46:34 +03008802 int ret = try_release_extent_mapping(page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04008803 if (ret == 1) {
8804 ClearPagePrivate(page);
8805 set_page_private(page, 0);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008806 put_page(page);
Chris Masona52d9a82007-08-27 16:49:44 -04008807 }
8808 return ret;
8809}
Chris Mason39279cc2007-06-12 06:35:45 -04008810
Chris Masone6dcd2d2008-07-17 12:53:50 -04008811static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
8812{
Chris Mason98509cf2008-09-11 15:51:43 -04008813 if (PageWriteback(page) || PageDirty(page))
8814 return 0;
Michal Hocko3ba7ab22017-01-09 15:39:02 +01008815 return __btrfs_releasepage(page, gfp_flags);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008816}
8817
Lukas Czernerd47992f2013-05-21 23:17:23 -04008818static void btrfs_invalidatepage(struct page *page, unsigned int offset,
8819 unsigned int length)
Chris Masona52d9a82007-08-27 16:49:44 -04008820{
Josef Bacik5fd02042012-05-02 14:00:54 -04008821 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05008822 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008823 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008824 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008825 u64 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008826 u64 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308827 u64 start;
8828 u64 end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008829 int inode_evicting = inode->i_state & I_FREEING;
Chris Masona52d9a82007-08-27 16:49:44 -04008830
Chris Mason8b62b722009-09-02 16:53:46 -04008831 /*
8832 * we have the page locked, so new writeback can't start,
8833 * and the dirty bit won't be cleared while we are here.
8834 *
8835 * Wait for IO on this page so that we can safely clear
8836 * the PagePrivate2 bit and do ordered accounting
8837 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008838 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04008839
Josef Bacik5fd02042012-05-02 14:00:54 -04008840 tree = &BTRFS_I(inode)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008841 if (offset) {
8842 btrfs_releasepage(page, GFP_NOFS);
8843 return;
8844 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008845
8846 if (!inode_evicting)
David Sterbaff13db42015-12-03 14:30:40 +01008847 lock_extent_bits(tree, page_start, page_end, &cached_state);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308848again:
8849 start = page_start;
Nikolay Borisova776c6f2017-02-20 13:50:49 +02008850 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), start,
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308851 page_end - start + 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008852 if (ordered) {
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308853 end = min(page_end, ordered->file_offset + ordered->len - 1);
Chris Masoneb84ae02008-07-17 13:53:27 -04008854 /*
8855 * IO on this page will never be started, so we need
8856 * to account for any ordered extents now
8857 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008858 if (!inode_evicting)
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308859 clear_extent_bit(tree, start, end,
Omar Sandovale1821632019-08-15 14:04:04 -07008860 EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008861 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
David Sterbaae0f1622017-10-31 16:37:52 +01008862 EXTENT_DEFRAG, 1, 0, &cached_state);
Chris Mason8b62b722009-09-02 16:53:46 -04008863 /*
8864 * whoever cleared the private bit is responsible
8865 * for the finish_ordered_io
8866 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04008867 if (TestClearPagePrivate2(page)) {
8868 struct btrfs_ordered_inode_tree *tree;
8869 u64 new_len;
8870
8871 tree = &BTRFS_I(inode)->ordered_tree;
8872
8873 spin_lock_irq(&tree->lock);
8874 set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308875 new_len = start - ordered->file_offset;
Josef Bacik77cef2e2013-08-29 13:57:21 -04008876 if (new_len < ordered->truncated_len)
8877 ordered->truncated_len = new_len;
8878 spin_unlock_irq(&tree->lock);
8879
8880 if (btrfs_dec_test_ordered_pending(inode, &ordered,
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308881 start,
8882 end - start + 1, 1))
Josef Bacik77cef2e2013-08-29 13:57:21 -04008883 btrfs_finish_ordered_io(ordered);
Chris Mason8b62b722009-09-02 16:53:46 -04008884 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008885 btrfs_put_ordered_extent(ordered);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008886 if (!inode_evicting) {
8887 cached_state = NULL;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308888 lock_extent_bits(tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008889 &cached_state);
8890 }
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308891
8892 start = end + 1;
8893 if (start < page_end)
8894 goto again;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008895 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008896
Qu Wenruob9d0b382015-09-29 10:35:16 +08008897 /*
8898 * Qgroup reserved space handler
8899 * Page here will be either
8900 * 1) Already written to disk
8901 * In this case, its reserved space is released from data rsv map
8902 * and will be freed by delayed_ref handler finally.
8903 * So even we call qgroup_free_data(), it won't decrease reserved
8904 * space.
8905 * 2) Not written to disk
Goldwyn Rodrigues0b34c262016-09-30 10:40:52 -05008906 * This means the reserved space should be freed here. However,
8907 * if a truncate invalidates the page (by clearing PageDirty)
8908 * and the page is accounted for while allocating extent
8909 * in btrfs_check_data_free_space() we let delayed_ref to
8910 * free the entire extent.
Qu Wenruob9d0b382015-09-29 10:35:16 +08008911 */
Goldwyn Rodrigues0b34c262016-09-30 10:40:52 -05008912 if (PageDirty(page))
Qu Wenruobc42bda2017-02-27 15:10:39 +08008913 btrfs_qgroup_free_data(inode, NULL, page_start, PAGE_SIZE);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008914 if (!inode_evicting) {
Omar Sandovale1821632019-08-15 14:04:04 -07008915 clear_extent_bit(tree, page_start, page_end, EXTENT_LOCKED |
Filipe Mananaa7e3b972017-04-03 10:45:46 +01008916 EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
8917 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1,
David Sterbaae0f1622017-10-31 16:37:52 +01008918 &cached_state);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008919
8920 __btrfs_releasepage(page, GFP_NOFS);
8921 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008922
Chris Mason4a096752008-07-21 10:29:44 -04008923 ClearPageChecked(page);
Chris Mason9ad6b7bc2008-04-18 16:11:30 -04008924 if (PagePrivate(page)) {
Chris Mason9ad6b7bc2008-04-18 16:11:30 -04008925 ClearPagePrivate(page);
8926 set_page_private(page, 0);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008927 put_page(page);
Chris Mason9ad6b7bc2008-04-18 16:11:30 -04008928 }
Chris Mason39279cc2007-06-12 06:35:45 -04008929}
8930
Chris Mason9ebefb182007-06-15 13:50:00 -04008931/*
8932 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
8933 * called from a page fault handler when a page is first dirtied. Hence we must
8934 * be careful to check for EOF conditions here. We set the page up correctly
8935 * for a written page which means we get ENOSPC checking when writing into
8936 * holes and correct delalloc and unwritten extent mapping on filesystems that
8937 * support these features.
8938 *
8939 * We are not allowed to take the i_mutex here so we have to play games to
8940 * protect against truncate races as the page could now be beyond EOF. Because
Omar Sandovald1342aa2018-05-11 13:13:29 -07008941 * truncate_setsize() writes the inode size before removing pages, once we have
8942 * the page lock we can determine safely if the page is beyond EOF. If it is not
Chris Mason9ebefb182007-06-15 13:50:00 -04008943 * beyond EOF, then the page is guaranteed safe against truncation until we
8944 * unlock the page.
8945 */
Souptick Joardera528a242018-06-06 19:54:44 +05308946vm_fault_t btrfs_page_mkwrite(struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04008947{
Nick Pigginc2ec1752009-03-31 15:23:21 -07008948 struct page *page = vmf->page;
Dave Jiang11bac802017-02-24 14:56:41 -08008949 struct inode *inode = file_inode(vmf->vma->vm_file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008950 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008951 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
8952 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008953 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08008954 struct extent_changeset *data_reserved = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008955 char *kaddr;
8956 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04008957 loff_t size;
Souptick Joardera528a242018-06-06 19:54:44 +05308958 vm_fault_t ret;
8959 int ret2;
Chris Mason9998eb72012-01-25 13:47:40 -05008960 int reserved = 0;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308961 u64 reserved_space;
Chris Masona52d9a82007-08-27 16:49:44 -04008962 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008963 u64 page_end;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308964 u64 end;
8965
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008966 reserved_space = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04008967
Jan Karab2b5ef52012-06-12 16:20:45 +02008968 sb_start_pagefault(inode->i_sb);
Qu Wenruodf480632015-09-08 17:25:54 +08008969 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008970 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308971 end = page_end;
Qu Wenruodf480632015-09-08 17:25:54 +08008972
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308973 /*
8974 * Reserving delalloc space after obtaining the page lock can lead to
8975 * deadlock. For example, if a dirty page is locked by this function
8976 * and the call to btrfs_delalloc_reserve_space() ends up triggering
8977 * dirty page write out, then the btrfs_writepage() function could
8978 * end up waiting indefinitely to get a lock on the page currently
8979 * being processed by btrfs_page_mkwrite() function.
8980 */
Souptick Joardera528a242018-06-06 19:54:44 +05308981 ret2 = btrfs_delalloc_reserve_space(inode, &data_reserved, page_start,
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308982 reserved_space);
Souptick Joardera528a242018-06-06 19:54:44 +05308983 if (!ret2) {
8984 ret2 = file_update_time(vmf->vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05008985 reserved = 1;
8986 }
Souptick Joardera528a242018-06-06 19:54:44 +05308987 if (ret2) {
8988 ret = vmf_error(ret2);
Chris Mason9998eb72012-01-25 13:47:40 -05008989 if (reserved)
8990 goto out;
8991 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07008992 }
Chris Mason1832a6d2007-12-21 16:27:21 -05008993
Nick Piggin56a76f82009-03-31 15:23:23 -07008994 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008995again:
Chris Mason9ebefb182007-06-15 13:50:00 -04008996 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04008997 size = i_size_read(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04008998
Chris Mason9ebefb182007-06-15 13:50:00 -04008999 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04009000 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04009001 /* page got truncated out from underneath us */
9002 goto out_unlock;
9003 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04009004 wait_on_page_writeback(page);
9005
David Sterbaff13db42015-12-03 14:30:40 +01009006 lock_extent_bits(io_tree, page_start, page_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009007 set_page_extent_mapped(page);
9008
Chris Masoneb84ae02008-07-17 13:53:27 -04009009 /*
9010 * we can't set the delalloc bits if there are pending ordered
9011 * extents. Drop our locks and wait for them to finish
9012 */
Nikolay Borisova776c6f2017-02-20 13:50:49 +02009013 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start,
9014 PAGE_SIZE);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009015 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00009016 unlock_extent_cached(io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01009017 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009018 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04009019 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009020 btrfs_put_ordered_extent(ordered);
9021 goto again;
9022 }
9023
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009024 if (page->index == ((size - 1) >> PAGE_SHIFT)) {
Jeff Mahoneyda170662016-06-15 09:22:56 -04009025 reserved_space = round_up(size - page_start,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009026 fs_info->sectorsize);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009027 if (reserved_space < PAGE_SIZE) {
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309028 end = page_start + reserved_space - 1;
Qu Wenruobc42bda2017-02-27 15:10:39 +08009029 btrfs_delalloc_release_space(inode, data_reserved,
Qu Wenruo43b18592017-12-12 15:34:32 +08009030 page_start, PAGE_SIZE - reserved_space,
9031 true);
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309032 }
9033 }
9034
Josef Bacikfbf19082009-10-01 17:10:23 -04009035 /*
Liu Bo54160342016-12-13 12:15:19 -08009036 * page_mkwrite gets called when the page is firstly dirtied after it's
9037 * faulted in, but write(2) could also dirty a page and set delalloc
9038 * bits, thus in this case for space account reason, we still need to
9039 * clear any delalloc bits within this page range since we have to
9040 * reserve data&meta space before lock_page() (see above comments).
Josef Bacikfbf19082009-10-01 17:10:23 -04009041 */
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309042 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, end,
Omar Sandovale1821632019-08-15 14:04:04 -07009043 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
9044 EXTENT_DEFRAG, 0, 0, &cached_state);
Josef Bacikfbf19082009-10-01 17:10:23 -04009045
Souptick Joardera528a242018-06-06 19:54:44 +05309046 ret2 = btrfs_set_extent_delalloc(inode, page_start, end, 0,
Nikolay Borisov330a5822019-07-17 16:18:17 +03009047 &cached_state);
Souptick Joardera528a242018-06-06 19:54:44 +05309048 if (ret2) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00009049 unlock_extent_cached(io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01009050 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009051 ret = VM_FAULT_SIGBUS;
9052 goto out_unlock;
9053 }
Souptick Joardera528a242018-06-06 19:54:44 +05309054 ret2 = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04009055
9056 /* page is wholly or partially inside EOF */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009057 if (page_start + PAGE_SIZE > size)
Johannes Thumshirn70730172018-12-05 15:23:03 +01009058 zero_start = offset_in_page(size);
Chris Mason9ebefb182007-06-15 13:50:00 -04009059 else
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009060 zero_start = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04009061
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009062 if (zero_start != PAGE_SIZE) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04009063 kaddr = kmap(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009064 memset(kaddr + zero_start, 0, PAGE_SIZE - zero_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009065 flush_dcache_page(page);
9066 kunmap(page);
9067 }
Chris Mason247e7432008-07-17 12:53:51 -04009068 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009069 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04009070 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04009071
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009072 BTRFS_I(inode)->last_trans = fs_info->generation;
Chris Mason257c62e2009-10-13 13:21:08 -04009073 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06009074 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
Chris Mason257c62e2009-10-13 13:21:08 -04009075
David Sterbae43bbe52017-12-12 21:43:52 +01009076 unlock_extent_cached(io_tree, page_start, page_end, &cached_state);
Chris Mason9ebefb182007-06-15 13:50:00 -04009077
Souptick Joardera528a242018-06-06 19:54:44 +05309078 if (!ret2) {
Qu Wenruo8702ba92019-10-14 14:34:51 +08009079 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE);
Jan Karab2b5ef52012-06-12 16:20:45 +02009080 sb_end_pagefault(inode->i_sb);
Qu Wenruo364ecf32017-02-27 15:10:38 +08009081 extent_changeset_free(data_reserved);
Chris Mason50a9b212009-09-11 12:33:12 -04009082 return VM_FAULT_LOCKED;
Jan Karab2b5ef52012-06-12 16:20:45 +02009083 }
Chris Mason717beb92018-06-25 10:03:41 -07009084
9085out_unlock:
Chris Mason9ebefb182007-06-15 13:50:00 -04009086 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05009087out:
Qu Wenruo8702ba92019-10-14 14:34:51 +08009088 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE);
Qu Wenruobc42bda2017-02-27 15:10:39 +08009089 btrfs_delalloc_release_space(inode, data_reserved, page_start,
Qu Wenruo43b18592017-12-12 15:34:32 +08009090 reserved_space, (ret != 0));
Chris Mason9998eb72012-01-25 13:47:40 -05009091out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02009092 sb_end_pagefault(inode->i_sb);
Qu Wenruo364ecf32017-02-27 15:10:38 +08009093 extent_changeset_free(data_reserved);
Chris Mason9ebefb182007-06-15 13:50:00 -04009094 return ret;
9095}
9096
Filipe Manana213e8c52018-02-06 20:40:31 +00009097static int btrfs_truncate(struct inode *inode, bool skip_writeback)
Chris Mason39279cc2007-06-12 06:35:45 -04009098{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009099 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009100 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04009101 struct btrfs_block_rsv *rsv;
Omar Sandovalad7e1a72018-05-22 09:59:50 -07009102 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04009103 struct btrfs_trans_handle *trans;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009104 u64 mask = fs_info->sectorsize - 1;
Josef Bacik2bd36e72019-08-22 15:14:33 -04009105 u64 min_size = btrfs_calc_metadata_size(fs_info, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04009106
Filipe Manana213e8c52018-02-06 20:40:31 +00009107 if (!skip_writeback) {
9108 ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask),
9109 (u64)-1);
9110 if (ret)
9111 return ret;
9112 }
Chris Mason39279cc2007-06-12 06:35:45 -04009113
Josef Bacikfcb80c22011-05-03 10:40:22 -04009114 /*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07009115 * Yes ladies and gentlemen, this is indeed ugly. We have a couple of
9116 * things going on here:
Josef Bacikfcb80c22011-05-03 10:40:22 -04009117 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07009118 * 1) We need to reserve space to update our inode.
Josef Bacikfcb80c22011-05-03 10:40:22 -04009119 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07009120 * 2) We need to have something to cache all the space that is going to
Josef Bacikfcb80c22011-05-03 10:40:22 -04009121 * be free'd up by the truncate operation, but also have some slack
9122 * space reserved in case it uses space during the truncate (thank you
9123 * very much snapshotting).
9124 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07009125 * And we need these to be separate. The fact is we can use a lot of
Josef Bacikfcb80c22011-05-03 10:40:22 -04009126 * space doing the truncate, and we have no earthly idea how much space
Nicholas D Steeves01327612016-05-19 21:18:45 -04009127 * we will use, so we need the truncate reservation to be separate so it
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07009128 * doesn't end up using space reserved for updating the inode. We also
9129 * need to be able to stop the transaction and start a new one, which
9130 * means we need to be able to update the inode several times, and we
9131 * have no idea of knowing how many times that will be, so we can't just
9132 * reserve 1 item for the entirety of the operation, so that has to be
9133 * done separately as well.
Josef Bacikfcb80c22011-05-03 10:40:22 -04009134 *
9135 * So that leaves us with
9136 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07009137 * 1) rsv - for the truncate reservation, which we will steal from the
Josef Bacikfcb80c22011-05-03 10:40:22 -04009138 * transaction reservation.
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07009139 * 2) fs_info->trans_block_rsv - this will have 1 items worth left for
Josef Bacikfcb80c22011-05-03 10:40:22 -04009140 * updating the inode.
9141 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009142 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009143 if (!rsv)
9144 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04009145 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04009146 rsv->failfast = 1;
Josef Bacikf0cd8462011-03-04 14:37:08 -05009147
Josef Bacik907cbce2011-08-08 13:46:15 -04009148 /*
Josef Bacik07127182011-08-19 10:29:59 -04009149 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04009150 * 1 for updating the inode.
9151 */
Josef Bacikf3fe8202013-01-07 17:03:21 -05009152 trans = btrfs_start_transaction(root, 2);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009153 if (IS_ERR(trans)) {
Omar Sandovalad7e1a72018-05-22 09:59:50 -07009154 ret = PTR_ERR(trans);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009155 goto out;
9156 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05009157
Josef Bacik907cbce2011-08-08 13:46:15 -04009158 /* Migrate the slack space for the truncate to our reserve */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009159 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, rsv,
Lu Fengqi3a584172018-08-04 21:10:55 +08009160 min_size, false);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009161 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05009162
Chris Mason5a3f23d2009-03-31 13:27:11 -04009163 /*
Josef Bacik5dc562c2012-08-17 13:14:17 -04009164 * So if we truncate and then write and fsync we normally would just
9165 * write the extents that changed, which is a problem if we need to
9166 * first truncate that entire inode. So set this flag so we write out
9167 * all of the extents in the inode to the sync log so we're completely
9168 * safe.
9169 */
9170 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009171 trans->block_rsv = rsv;
Josef Bacik5dc562c2012-08-17 13:14:17 -04009172
Yan, Zheng80825102009-11-12 09:35:36 +00009173 while (1) {
9174 ret = btrfs_truncate_inode_items(trans, root, inode,
9175 inode->i_size,
9176 BTRFS_EXTENT_DATA_KEY);
Josef Bacikddfae632017-10-19 14:16:02 -04009177 trans->block_rsv = &fs_info->trans_block_rsv;
Omar Sandovalad7e1a72018-05-22 09:59:50 -07009178 if (ret != -ENOSPC && ret != -EAGAIN)
Yan, Zheng80825102009-11-12 09:35:36 +00009179 break;
Chris Mason39279cc2007-06-12 06:35:45 -04009180
Yan, Zheng80825102009-11-12 09:35:36 +00009181 ret = btrfs_update_inode(trans, root, inode);
Omar Sandovalad7e1a72018-05-22 09:59:50 -07009182 if (ret)
Josef Bacik3893e332011-01-31 16:03:11 -05009183 break;
Josef Bacikca7e70f2012-08-27 17:48:15 -04009184
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009185 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009186 btrfs_btree_balance_dirty(fs_info);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009187
9188 trans = btrfs_start_transaction(root, 2);
9189 if (IS_ERR(trans)) {
Omar Sandovalad7e1a72018-05-22 09:59:50 -07009190 ret = PTR_ERR(trans);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009191 trans = NULL;
9192 break;
9193 }
9194
Wang Xiaoguang47b5d642016-09-07 20:17:38 +08009195 btrfs_block_rsv_release(fs_info, rsv, -1);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009196 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv,
Lu Fengqi3a584172018-08-04 21:10:55 +08009197 rsv, min_size, false);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009198 BUG_ON(ret); /* shouldn't happen */
9199 trans->block_rsv = rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00009200 }
9201
Josef Bacikddfae632017-10-19 14:16:02 -04009202 /*
9203 * We can't call btrfs_truncate_block inside a trans handle as we could
9204 * deadlock with freeze, if we got NEED_TRUNCATE_BLOCK then we know
9205 * we've truncated everything except the last little bit, and can do
9206 * btrfs_truncate_block and then update the disk_i_size.
9207 */
9208 if (ret == NEED_TRUNCATE_BLOCK) {
9209 btrfs_end_transaction(trans);
9210 btrfs_btree_balance_dirty(fs_info);
9211
9212 ret = btrfs_truncate_block(inode, inode->i_size, 0, 0);
9213 if (ret)
9214 goto out;
9215 trans = btrfs_start_transaction(root, 1);
9216 if (IS_ERR(trans)) {
9217 ret = PTR_ERR(trans);
9218 goto out;
9219 }
9220 btrfs_ordered_update_i_size(inode, inode->i_size, NULL);
9221 }
9222
Chris Mason917c16b2011-11-08 14:49:59 -05009223 if (trans) {
Omar Sandovalad7e1a72018-05-22 09:59:50 -07009224 int ret2;
Josef Bacik7b128762008-07-24 12:17:14 -04009225
Omar Sandovalad7e1a72018-05-22 09:59:50 -07009226 trans->block_rsv = &fs_info->trans_block_rsv;
9227 ret2 = btrfs_update_inode(trans, root, inode);
9228 if (ret2 && !ret)
9229 ret = ret2;
9230
9231 ret2 = btrfs_end_transaction(trans);
9232 if (ret2 && !ret)
9233 ret = ret2;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009234 btrfs_btree_balance_dirty(fs_info);
Chris Mason917c16b2011-11-08 14:49:59 -05009235 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04009236out:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009237 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009238
Omar Sandovalad7e1a72018-05-22 09:59:50 -07009239 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04009240}
9241
Sven Wegener3b963622008-06-09 21:57:42 -04009242/*
Chris Masond352ac62008-09-29 15:18:18 -04009243 * create a new subvolume directory/inode (helper for the ioctl).
9244 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05009245int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009246 struct btrfs_root *new_root,
9247 struct btrfs_root *parent_root,
9248 u64 new_dirid)
Chris Mason39279cc2007-06-12 06:35:45 -04009249{
Chris Mason39279cc2007-06-12 06:35:45 -04009250 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04009251 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04009252 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009253
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01009254 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
9255 new_dirid, new_dirid,
9256 S_IFDIR | (~current_umask() & S_IRWXUGO),
9257 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04009258 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04009259 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009260 inode->i_op = &btrfs_dir_inode_operations;
9261 inode->i_fop = &btrfs_dir_file_operations;
9262
Miklos Szeredibfe86842011-10-28 14:13:29 +02009263 set_nlink(inode, 1);
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02009264 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Masonb0d5d102014-09-08 13:08:51 -07009265 unlock_new_inode(inode);
Sven Wegener3b963622008-06-09 21:57:42 -04009266
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009267 err = btrfs_subvol_inherit_props(trans, new_root, parent_root);
9268 if (err)
9269 btrfs_err(new_root->fs_info,
David Sterba351fd352014-05-15 16:48:20 +02009270 "error inheriting subvolume %llu properties: %d",
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009271 new_root->root_key.objectid, err);
9272
Yan, Zheng76dda932009-09-21 16:00:26 -04009273 err = btrfs_update_inode(trans, new_root, inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04009274
Yan, Zheng76dda932009-09-21 16:00:26 -04009275 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07009276 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04009277}
9278
Chris Mason39279cc2007-06-12 06:35:45 -04009279struct inode *btrfs_alloc_inode(struct super_block *sb)
9280{
Josef Bacik69fe2d72017-10-19 14:15:57 -04009281 struct btrfs_fs_info *fs_info = btrfs_sb(sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009282 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009283 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009284
David Sterba712e36c2017-10-31 17:08:27 +01009285 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_KERNEL);
Chris Mason39279cc2007-06-12 06:35:45 -04009286 if (!ei)
9287 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009288
9289 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009290 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04009291 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04009292 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04009293 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009294 ei->delalloc_bytes = 0;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01009295 ei->new_delalloc_bytes = 0;
Wang Shilong47059d92014-07-03 18:22:07 +08009296 ei->defrag_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009297 ei->disk_i_size = 0;
9298 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04009299 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009300 ei->index_cnt = (u64)-1;
Miao Xie67de1172013-12-26 13:07:06 +08009301 ei->dir_index = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009302 ei->last_unlink_trans = 0;
Liu Bo46d8bc32012-08-29 01:07:55 -06009303 ei->last_log_commit = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009304
Josef Bacik9e0baf62011-07-15 15:16:44 +00009305 spin_lock_init(&ei->lock);
9306 ei->outstanding_extents = 0;
Josef Bacik69fe2d72017-10-19 14:15:57 -04009307 if (sb->s_magic != BTRFS_TEST_MAGIC)
9308 btrfs_init_metadata_block_rsv(fs_info, &ei->block_rsv,
9309 BTRFS_BLOCK_RSV_DELALLOC);
Josef Bacik72ac3c02012-05-23 14:13:11 -04009310 ei->runtime_flags = 0;
David Sterbab52aa8c2017-07-17 19:17:20 +02009311 ei->prop_compress = BTRFS_COMPRESS_NONE;
David Sterbaeec63c62017-07-17 19:41:31 +02009312 ei->defrag_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009313
Miao Xie16cdcec2011-04-22 18:12:22 +08009314 ei->delayed_node = NULL;
9315
chandan r9cc97d62012-07-04 12:48:07 +05309316 ei->i_otime.tv_sec = 0;
9317 ei->i_otime.tv_nsec = 0;
9318
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009319 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02009320 extent_map_tree_init(&ei->extent_tree);
Qu Wenruo43eb5f22019-03-01 10:47:59 +08009321 extent_io_tree_init(fs_info, &ei->io_tree, IO_TREE_INODE_IO, inode);
9322 extent_io_tree_init(fs_info, &ei->io_failure_tree,
9323 IO_TREE_INODE_IO_FAILURE, inode);
David Sterba7b439732019-03-11 15:58:30 +01009324 ei->io_tree.track_uptodate = true;
9325 ei->io_failure_tree.track_uptodate = true;
Josef Bacikb812ce22012-11-16 13:56:32 -05009326 atomic_set(&ei->sync_writers, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009327 mutex_init(&ei->log_mutex);
Josef Bacikf2486792012-01-13 12:09:22 -05009328 mutex_init(&ei->delalloc_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009329 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009330 INIT_LIST_HEAD(&ei->delalloc_inodes);
David Sterba8089fe62015-11-19 14:15:51 +01009331 INIT_LIST_HEAD(&ei->delayed_iput);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009332 RB_CLEAR_NODE(&ei->rb_node);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01009333 init_rwsem(&ei->dio_sem);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009334
9335 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009336}
9337
Josef Bacikaaedb552013-10-11 14:44:09 -04009338#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
9339void btrfs_test_destroy_inode(struct inode *inode)
9340{
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02009341 btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0);
Josef Bacikaaedb552013-10-11 14:44:09 -04009342 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
9343}
9344#endif
9345
Al Viro26602ca2019-04-10 15:14:41 -04009346void btrfs_free_inode(struct inode *inode)
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009347{
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009348 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
9349}
9350
Chris Mason39279cc2007-06-12 06:35:45 -04009351void btrfs_destroy_inode(struct inode *inode)
9352{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009353 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009354 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009355 struct btrfs_root *root = BTRFS_I(inode)->root;
9356
Al Virob3d9b7a2012-06-09 13:51:19 -04009357 WARN_ON(!hlist_empty(&inode->i_dentry));
Chris Mason39279cc2007-06-12 06:35:45 -04009358 WARN_ON(inode->i_data.nrpages);
Josef Bacik69fe2d72017-10-19 14:15:57 -04009359 WARN_ON(BTRFS_I(inode)->block_rsv.reserved);
9360 WARN_ON(BTRFS_I(inode)->block_rsv.size);
Josef Bacik9e0baf62011-07-15 15:16:44 +00009361 WARN_ON(BTRFS_I(inode)->outstanding_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04009362 WARN_ON(BTRFS_I(inode)->delalloc_bytes);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01009363 WARN_ON(BTRFS_I(inode)->new_delalloc_bytes);
Josef Bacik7709cde2011-08-04 10:25:02 -04009364 WARN_ON(BTRFS_I(inode)->csum_bytes);
Wang Shilong47059d92014-07-03 18:22:07 +08009365 WARN_ON(BTRFS_I(inode)->defrag_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04009366
Chris Mason5a3f23d2009-03-31 13:27:11 -04009367 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05009368 * This can happen where we create an inode, but somebody else also
9369 * created the same inode and we need to destroy the one we already
9370 * created.
9371 */
9372 if (!root)
Al Viro26602ca2019-04-10 15:14:41 -04009373 return;
Josef Bacika6dbd422009-11-11 15:53:34 -05009374
Chris Masond3977122009-01-05 21:25:51 -05009375 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04009376 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
9377 if (!ordered)
9378 break;
9379 else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009380 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04009381 "found ordered extent %llu %llu on inode cleanup",
9382 ordered->file_offset, ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009383 btrfs_remove_ordered_extent(inode, ordered);
9384 btrfs_put_ordered_extent(ordered);
9385 btrfs_put_ordered_extent(ordered);
9386 }
9387 }
Qu Wenruo56fa9d02015-10-13 09:53:10 +08009388 btrfs_qgroup_check_reserved_leak(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04009389 inode_tree_del(inode);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02009390 btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04009391}
9392
Al Viro45321ac2010-06-07 13:43:19 -04009393int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04009394{
9395 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04009396
Naohiro Aota6379ef92013-06-06 09:56:34 +00009397 if (root == NULL)
9398 return 1;
9399
Liu Bofa6ac872013-02-20 14:10:23 +00009400 /* the snap/subvol tree is on deleting */
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02009401 if (btrfs_root_refs(&root->root_item) == 0)
Al Viro45321ac2010-06-07 13:43:19 -04009402 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04009403 else
Al Viro45321ac2010-06-07 13:43:19 -04009404 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04009405}
9406
Sven Wegener0ee0fda2008-07-30 16:54:26 -04009407static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04009408{
9409 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
9410
9411 inode_init_once(&ei->vfs_inode);
9412}
9413
David Sterbae67c7182018-02-19 17:24:18 +01009414void __cold btrfs_destroy_cachep(void)
Chris Mason39279cc2007-06-12 06:35:45 -04009415{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10009416 /*
9417 * Make sure all delayed rcu free inodes are flushed before we
9418 * destroy cache.
9419 */
9420 rcu_barrier();
Kinglong Mee5598e902016-01-29 21:36:35 +08009421 kmem_cache_destroy(btrfs_inode_cachep);
9422 kmem_cache_destroy(btrfs_trans_handle_cachep);
Kinglong Mee5598e902016-01-29 21:36:35 +08009423 kmem_cache_destroy(btrfs_path_cachep);
9424 kmem_cache_destroy(btrfs_free_space_cachep);
Christophe Leroy3acd4852019-08-21 15:05:55 +00009425 kmem_cache_destroy(btrfs_free_space_bitmap_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04009426}
9427
Liu Bof5c29bd2017-11-02 17:21:50 -06009428int __init btrfs_init_cachep(void)
Chris Mason39279cc2007-06-12 06:35:45 -04009429{
David Sterba837e1972012-09-07 03:00:48 -06009430 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009431 sizeof(struct btrfs_inode), 0,
Vladimir Davydov5d097052016-01-14 15:18:21 -08009432 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD | SLAB_ACCOUNT,
9433 init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04009434 if (!btrfs_inode_cachep)
9435 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009436
David Sterba837e1972012-09-07 03:00:48 -06009437 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009438 sizeof(struct btrfs_trans_handle), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009439 SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009440 if (!btrfs_trans_handle_cachep)
9441 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009442
David Sterba837e1972012-09-07 03:00:48 -06009443 btrfs_path_cachep = kmem_cache_create("btrfs_path",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009444 sizeof(struct btrfs_path), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009445 SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009446 if (!btrfs_path_cachep)
9447 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009448
David Sterba837e1972012-09-07 03:00:48 -06009449 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
Josef Bacikdc89e982011-01-28 17:05:48 -05009450 sizeof(struct btrfs_free_space), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009451 SLAB_MEM_SPREAD, NULL);
Josef Bacikdc89e982011-01-28 17:05:48 -05009452 if (!btrfs_free_space_cachep)
9453 goto fail;
9454
Christophe Leroy3acd4852019-08-21 15:05:55 +00009455 btrfs_free_space_bitmap_cachep = kmem_cache_create("btrfs_free_space_bitmap",
9456 PAGE_SIZE, PAGE_SIZE,
9457 SLAB_RED_ZONE, NULL);
9458 if (!btrfs_free_space_bitmap_cachep)
9459 goto fail;
9460
Chris Mason39279cc2007-06-12 06:35:45 -04009461 return 0;
9462fail:
9463 btrfs_destroy_cachep();
9464 return -ENOMEM;
9465}
9466
David Howellsa528d352017-01-31 16:46:22 +00009467static int btrfs_getattr(const struct path *path, struct kstat *stat,
9468 u32 request_mask, unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04009469{
Miao Xiedf0af1a2013-01-29 10:11:59 +00009470 u64 delalloc_bytes;
David Howellsa528d352017-01-31 16:46:22 +00009471 struct inode *inode = d_inode(path->dentry);
David Sterbafadc0d82011-11-20 07:33:38 -05009472 u32 blocksize = inode->i_sb->s_blocksize;
Yonghong Song04a87e32017-05-12 15:07:43 -07009473 u32 bi_flags = BTRFS_I(inode)->flags;
9474
9475 stat->result_mask |= STATX_BTIME;
9476 stat->btime.tv_sec = BTRFS_I(inode)->i_otime.tv_sec;
9477 stat->btime.tv_nsec = BTRFS_I(inode)->i_otime.tv_nsec;
9478 if (bi_flags & BTRFS_INODE_APPEND)
9479 stat->attributes |= STATX_ATTR_APPEND;
9480 if (bi_flags & BTRFS_INODE_COMPRESS)
9481 stat->attributes |= STATX_ATTR_COMPRESSED;
9482 if (bi_flags & BTRFS_INODE_IMMUTABLE)
9483 stat->attributes |= STATX_ATTR_IMMUTABLE;
9484 if (bi_flags & BTRFS_INODE_NODUMP)
9485 stat->attributes |= STATX_ATTR_NODUMP;
9486
9487 stat->attributes_mask |= (STATX_ATTR_APPEND |
9488 STATX_ATTR_COMPRESSED |
9489 STATX_ATTR_IMMUTABLE |
9490 STATX_ATTR_NODUMP);
David Sterbafadc0d82011-11-20 07:33:38 -05009491
Chris Mason39279cc2007-06-12 06:35:45 -04009492 generic_fillattr(inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04009493 stat->dev = BTRFS_I(inode)->root->anon_dev;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009494
9495 spin_lock(&BTRFS_I(inode)->lock);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01009496 delalloc_bytes = BTRFS_I(inode)->new_delalloc_bytes;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009497 spin_unlock(&BTRFS_I(inode)->lock);
David Sterbafadc0d82011-11-20 07:33:38 -05009498 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
Miao Xiedf0af1a2013-01-29 10:11:59 +00009499 ALIGN(delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04009500 return 0;
9501}
9502
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009503static int btrfs_rename_exchange(struct inode *old_dir,
9504 struct dentry *old_dentry,
9505 struct inode *new_dir,
9506 struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04009507{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009508 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009509 struct btrfs_trans_handle *trans;
9510 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009511 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009512 struct inode *new_inode = new_dentry->d_inode;
9513 struct inode *old_inode = old_dentry->d_inode;
Deepa Dinamani95582b02018-05-08 19:36:02 -07009514 struct timespec64 ctime = current_time(old_inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009515 struct dentry *parent;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009516 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
9517 u64 new_ino = btrfs_ino(BTRFS_I(new_inode));
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009518 u64 old_idx = 0;
9519 u64 new_idx = 0;
9520 u64 root_objectid;
9521 int ret;
Filipe Manana86e8aa02016-05-05 02:02:27 +01009522 bool root_log_pinned = false;
9523 bool dest_log_pinned = false;
Filipe Mananad4682ba2018-06-11 19:24:28 +01009524 struct btrfs_log_ctx ctx_root;
9525 struct btrfs_log_ctx ctx_dest;
9526 bool sync_log_root = false;
9527 bool sync_log_dest = false;
9528 bool commit_transaction = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009529
9530 /* we only allow rename subvolume link between subvolumes */
9531 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
9532 return -EXDEV;
9533
Filipe Mananad4682ba2018-06-11 19:24:28 +01009534 btrfs_init_log_ctx(&ctx_root, old_inode);
9535 btrfs_init_log_ctx(&ctx_dest, new_inode);
9536
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009537 /* close the race window with snapshot create/destroy ioctl */
9538 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009539 down_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009540 if (new_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009541 down_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009542
9543 /*
9544 * We want to reserve the absolute worst case amount of items. So if
9545 * both inodes are subvols and we need to unlink them then that would
9546 * require 4 item modifications, but if they are both normal inodes it
9547 * would require 5 item modifications, so we'll assume their normal
9548 * inodes. So 5 * 2 is 10, plus 2 for the new links, so 12 total items
9549 * should cover the worst case number of items we'll modify.
9550 */
9551 trans = btrfs_start_transaction(root, 12);
9552 if (IS_ERR(trans)) {
9553 ret = PTR_ERR(trans);
9554 goto out_notrans;
9555 }
9556
9557 /*
9558 * We need to find a free sequence number both in the source and
9559 * in the destination directory for the exchange.
9560 */
Nikolay Borisov877574e2017-02-20 13:50:33 +02009561 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009562 if (ret)
9563 goto out_fail;
Nikolay Borisov877574e2017-02-20 13:50:33 +02009564 ret = btrfs_set_inode_index(BTRFS_I(old_dir), &new_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009565 if (ret)
9566 goto out_fail;
9567
9568 BTRFS_I(old_inode)->dir_index = 0ULL;
9569 BTRFS_I(new_inode)->dir_index = 0ULL;
9570
9571 /* Reference for the source. */
9572 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
9573 /* force full log commit if subvolume involved. */
David Sterba90787762019-03-20 13:28:05 +01009574 btrfs_set_log_full_commit(trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009575 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01009576 btrfs_pin_log_trans(root);
9577 root_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009578 ret = btrfs_insert_inode_ref(trans, dest,
9579 new_dentry->d_name.name,
9580 new_dentry->d_name.len,
9581 old_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01009582 btrfs_ino(BTRFS_I(new_dir)),
9583 old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009584 if (ret)
9585 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009586 }
9587
9588 /* And now for the dest. */
9589 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
9590 /* force full log commit if subvolume involved. */
David Sterba90787762019-03-20 13:28:05 +01009591 btrfs_set_log_full_commit(trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009592 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01009593 btrfs_pin_log_trans(dest);
9594 dest_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009595 ret = btrfs_insert_inode_ref(trans, root,
9596 old_dentry->d_name.name,
9597 old_dentry->d_name.len,
9598 new_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01009599 btrfs_ino(BTRFS_I(old_dir)),
9600 new_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009601 if (ret)
9602 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009603 }
9604
9605 /* Update inode version and ctime/mtime. */
9606 inode_inc_iversion(old_dir);
9607 inode_inc_iversion(new_dir);
9608 inode_inc_iversion(old_inode);
9609 inode_inc_iversion(new_inode);
9610 old_dir->i_ctime = old_dir->i_mtime = ctime;
9611 new_dir->i_ctime = new_dir->i_mtime = ctime;
9612 old_inode->i_ctime = ctime;
9613 new_inode->i_ctime = ctime;
9614
9615 if (old_dentry->d_parent != new_dentry->d_parent) {
David Sterbaf85b7372017-01-20 14:54:07 +01009616 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
9617 BTRFS_I(old_inode), 1);
9618 btrfs_record_unlink_dir(trans, BTRFS_I(new_dir),
9619 BTRFS_I(new_inode), 1);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009620 }
9621
9622 /* src is a subvolume */
9623 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
9624 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
Lu Fengqi401b3b12018-08-01 11:32:30 +08009625 ret = btrfs_unlink_subvol(trans, old_dir, root_objectid,
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009626 old_dentry->d_name.name,
9627 old_dentry->d_name.len);
9628 } else { /* src is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009629 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
9630 BTRFS_I(old_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009631 old_dentry->d_name.name,
9632 old_dentry->d_name.len);
9633 if (!ret)
9634 ret = btrfs_update_inode(trans, root, old_inode);
9635 }
9636 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009637 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009638 goto out_fail;
9639 }
9640
9641 /* dest is a subvolume */
9642 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
9643 root_objectid = BTRFS_I(new_inode)->root->root_key.objectid;
Lu Fengqi401b3b12018-08-01 11:32:30 +08009644 ret = btrfs_unlink_subvol(trans, new_dir, root_objectid,
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009645 new_dentry->d_name.name,
9646 new_dentry->d_name.len);
9647 } else { /* dest is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009648 ret = __btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
9649 BTRFS_I(new_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009650 new_dentry->d_name.name,
9651 new_dentry->d_name.len);
9652 if (!ret)
9653 ret = btrfs_update_inode(trans, dest, new_inode);
9654 }
9655 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009656 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009657 goto out_fail;
9658 }
9659
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009660 ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009661 new_dentry->d_name.name,
9662 new_dentry->d_name.len, 0, old_idx);
9663 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009664 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009665 goto out_fail;
9666 }
9667
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009668 ret = btrfs_add_link(trans, BTRFS_I(old_dir), BTRFS_I(new_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009669 old_dentry->d_name.name,
9670 old_dentry->d_name.len, 0, new_idx);
9671 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009672 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009673 goto out_fail;
9674 }
9675
9676 if (old_inode->i_nlink == 1)
9677 BTRFS_I(old_inode)->dir_index = old_idx;
9678 if (new_inode->i_nlink == 1)
9679 BTRFS_I(new_inode)->dir_index = new_idx;
9680
Filipe Manana86e8aa02016-05-05 02:02:27 +01009681 if (root_log_pinned) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009682 parent = new_dentry->d_parent;
Filipe Mananad4682ba2018-06-11 19:24:28 +01009683 ret = btrfs_log_new_name(trans, BTRFS_I(old_inode),
9684 BTRFS_I(old_dir), parent,
9685 false, &ctx_root);
9686 if (ret == BTRFS_NEED_LOG_SYNC)
9687 sync_log_root = true;
9688 else if (ret == BTRFS_NEED_TRANS_COMMIT)
9689 commit_transaction = true;
9690 ret = 0;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009691 btrfs_end_log_trans(root);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009692 root_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009693 }
Filipe Manana86e8aa02016-05-05 02:02:27 +01009694 if (dest_log_pinned) {
Filipe Mananad4682ba2018-06-11 19:24:28 +01009695 if (!commit_transaction) {
9696 parent = old_dentry->d_parent;
9697 ret = btrfs_log_new_name(trans, BTRFS_I(new_inode),
9698 BTRFS_I(new_dir), parent,
9699 false, &ctx_dest);
9700 if (ret == BTRFS_NEED_LOG_SYNC)
9701 sync_log_dest = true;
9702 else if (ret == BTRFS_NEED_TRANS_COMMIT)
9703 commit_transaction = true;
9704 ret = 0;
9705 }
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009706 btrfs_end_log_trans(dest);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009707 dest_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009708 }
9709out_fail:
Filipe Manana86e8aa02016-05-05 02:02:27 +01009710 /*
9711 * If we have pinned a log and an error happened, we unpin tasks
9712 * trying to sync the log and force them to fallback to a transaction
9713 * commit if the log currently contains any of the inodes involved in
9714 * this rename operation (to ensure we do not persist a log with an
9715 * inconsistent state for any of these inodes or leading to any
9716 * inconsistencies when replayed). If the transaction was aborted, the
9717 * abortion reason is propagated to userspace when attempting to commit
9718 * the transaction. If the log does not contain any of these inodes, we
9719 * allow the tasks to sync it.
9720 */
9721 if (ret && (root_log_pinned || dest_log_pinned)) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009722 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
9723 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
9724 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana86e8aa02016-05-05 02:02:27 +01009725 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009726 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
David Sterba90787762019-03-20 13:28:05 +01009727 btrfs_set_log_full_commit(trans);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009728
9729 if (root_log_pinned) {
9730 btrfs_end_log_trans(root);
9731 root_log_pinned = false;
9732 }
9733 if (dest_log_pinned) {
9734 btrfs_end_log_trans(dest);
9735 dest_log_pinned = false;
9736 }
9737 }
Filipe Mananad4682ba2018-06-11 19:24:28 +01009738 if (!ret && sync_log_root && !commit_transaction) {
9739 ret = btrfs_sync_log(trans, BTRFS_I(old_inode)->root,
9740 &ctx_root);
9741 if (ret)
9742 commit_transaction = true;
9743 }
9744 if (!ret && sync_log_dest && !commit_transaction) {
9745 ret = btrfs_sync_log(trans, BTRFS_I(new_inode)->root,
9746 &ctx_dest);
9747 if (ret)
9748 commit_transaction = true;
9749 }
9750 if (commit_transaction) {
Filipe Mananae6c61712019-11-08 16:11:56 +00009751 /*
9752 * We may have set commit_transaction when logging the new name
9753 * in the destination root, in which case we left the source
9754 * root context in the list of log contextes. So make sure we
9755 * remove it to avoid invalid memory accesses, since the context
9756 * was allocated in our stack frame.
9757 */
9758 if (sync_log_root) {
9759 mutex_lock(&root->log_mutex);
9760 list_del_init(&ctx_root.list);
9761 mutex_unlock(&root->log_mutex);
9762 }
Filipe Mananad4682ba2018-06-11 19:24:28 +01009763 ret = btrfs_commit_transaction(trans);
9764 } else {
9765 int ret2;
9766
9767 ret2 = btrfs_end_transaction(trans);
9768 ret = ret ? ret : ret2;
9769 }
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009770out_notrans:
9771 if (new_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009772 up_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009773 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009774 up_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009775
Filipe Mananae6c61712019-11-08 16:11:56 +00009776 ASSERT(list_empty(&ctx_root.list));
9777 ASSERT(list_empty(&ctx_dest.list));
9778
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009779 return ret;
9780}
9781
9782static int btrfs_whiteout_for_rename(struct btrfs_trans_handle *trans,
9783 struct btrfs_root *root,
9784 struct inode *dir,
9785 struct dentry *dentry)
9786{
9787 int ret;
9788 struct inode *inode;
9789 u64 objectid;
9790 u64 index;
9791
9792 ret = btrfs_find_free_ino(root, &objectid);
9793 if (ret)
9794 return ret;
9795
9796 inode = btrfs_new_inode(trans, root, dir,
9797 dentry->d_name.name,
9798 dentry->d_name.len,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009799 btrfs_ino(BTRFS_I(dir)),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009800 objectid,
9801 S_IFCHR | WHITEOUT_MODE,
9802 &index);
9803
9804 if (IS_ERR(inode)) {
9805 ret = PTR_ERR(inode);
9806 return ret;
9807 }
9808
9809 inode->i_op = &btrfs_special_inode_operations;
9810 init_special_inode(inode, inode->i_mode,
9811 WHITEOUT_DEV);
9812
9813 ret = btrfs_init_inode_security(trans, inode, dir,
9814 &dentry->d_name);
9815 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009816 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009817
Nikolay Borisovcef415a2017-02-20 13:51:09 +02009818 ret = btrfs_add_nondir(trans, BTRFS_I(dir), dentry,
9819 BTRFS_I(inode), 0, index);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009820 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009821 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009822
9823 ret = btrfs_update_inode(trans, root, inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009824out:
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009825 unlock_new_inode(inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009826 if (ret)
9827 inode_dec_link_count(inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009828 iput(inode);
9829
Filipe Mananac9901612016-05-05 01:41:57 +01009830 return ret;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009831}
9832
Chris Mason39279cc2007-06-12 06:35:45 -04009833static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009834 struct inode *new_dir, struct dentry *new_dentry,
9835 unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04009836{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009837 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009838 struct btrfs_trans_handle *trans;
Filipe Manana5062af32016-05-05 10:26:26 +01009839 unsigned int trans_num_items;
Chris Mason39279cc2007-06-12 06:35:45 -04009840 struct btrfs_root *root = BTRFS_I(old_dir)->root;
9841 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00009842 struct inode *new_inode = d_inode(new_dentry);
9843 struct inode *old_inode = d_inode(old_dentry);
Chris Mason00e4e6b2008-08-05 11:18:09 -04009844 u64 index = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009845 u64 root_objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04009846 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009847 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009848 bool log_pinned = false;
Filipe Mananad4682ba2018-06-11 19:24:28 +01009849 struct btrfs_log_ctx ctx;
9850 bool sync_log = false;
9851 bool commit_transaction = false;
Chris Mason39279cc2007-06-12 06:35:45 -04009852
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009853 if (btrfs_ino(BTRFS_I(new_dir)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04009854 return -EPERM;
9855
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009856 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08009857 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05009858 return -EXDEV;
9859
Li Zefan33345d012011-04-20 10:31:50 +08009860 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009861 (new_inode && btrfs_ino(BTRFS_I(new_inode)) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009862 return -ENOTEMPTY;
9863
Chris Mason39279cc2007-06-12 06:35:45 -04009864 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009865 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04009866 return -ENOTEMPTY;
Chris Mason9c520572012-12-17 14:26:57 -05009867
9868
9869 /* check for collisions, even if the name isn't there */
Josef Bacik4871c152013-10-09 12:24:04 -04009870 ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino,
Chris Mason9c520572012-12-17 14:26:57 -05009871 new_dentry->d_name.name,
9872 new_dentry->d_name.len);
9873
9874 if (ret) {
9875 if (ret == -EEXIST) {
9876 /* we shouldn't get
9877 * eexist without a new_inode */
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05309878 if (WARN_ON(!new_inode)) {
Chris Mason9c520572012-12-17 14:26:57 -05009879 return ret;
9880 }
9881 } else {
9882 /* maybe -EOVERFLOW */
9883 return ret;
9884 }
9885 }
9886 ret = 0;
9887
Chris Mason5a3f23d2009-03-31 13:27:11 -04009888 /*
Chris Mason8d875f92014-08-12 10:47:42 -07009889 * we're using rename to replace one file with another. Start IO on it
9890 * now so we don't add too much work to the end of the transaction
Chris Mason5a3f23d2009-03-31 13:27:11 -04009891 */
Chris Mason8d875f92014-08-12 10:47:42 -07009892 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size)
Chris Mason5a3f23d2009-03-31 13:27:11 -04009893 filemap_flush(old_inode->i_mapping);
9894
Yan, Zheng76dda932009-09-21 16:00:26 -04009895 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08009896 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009897 down_read(&fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04009898 /*
9899 * We want to reserve the absolute worst case amount of items. So if
9900 * both inodes are subvols and we need to unlink them then that would
9901 * require 4 item modifications, but if they are both normal inodes it
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009902 * would require 5 item modifications, so we'll assume they are normal
Yan, Zhenga22285a2010-05-16 10:48:46 -04009903 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
9904 * should cover the worst case number of items we'll modify.
Filipe Manana5062af32016-05-05 10:26:26 +01009905 * If our rename has the whiteout flag, we need more 5 units for the
9906 * new inode (1 inode item, 1 inode ref, 2 dir items and 1 xattr item
9907 * when selinux is enabled).
Yan, Zhenga22285a2010-05-16 10:48:46 -04009908 */
Filipe Manana5062af32016-05-05 10:26:26 +01009909 trans_num_items = 11;
9910 if (flags & RENAME_WHITEOUT)
9911 trans_num_items += 5;
9912 trans = btrfs_start_transaction(root, trans_num_items);
Johann Lombardib44c59a2011-03-31 13:23:47 +00009913 if (IS_ERR(trans)) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009914 ret = PTR_ERR(trans);
9915 goto out_notrans;
9916 }
Chris Mason5f39d392007-10-15 16:14:19 -04009917
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009918 if (dest != root)
9919 btrfs_record_root_in_trans(trans, dest);
9920
Nikolay Borisov877574e2017-02-20 13:50:33 +02009921 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009922 if (ret)
9923 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009924
Miao Xie67de1172013-12-26 13:07:06 +08009925 BTRFS_I(old_inode)->dir_index = 0ULL;
Li Zefan33345d012011-04-20 10:31:50 +08009926 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009927 /* force full log commit if subvolume involved. */
David Sterba90787762019-03-20 13:28:05 +01009928 btrfs_set_log_full_commit(trans);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009929 } else {
Filipe Mananac4aba952016-04-29 13:14:42 +01009930 btrfs_pin_log_trans(root);
9931 log_pinned = true;
Yan, Zhenga5719522009-09-24 09:17:31 -04009932 ret = btrfs_insert_inode_ref(trans, dest,
9933 new_dentry->d_name.name,
9934 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08009935 old_ino,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009936 btrfs_ino(BTRFS_I(new_dir)), index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009937 if (ret)
9938 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009939 }
Chris Mason5a3f23d2009-03-31 13:27:11 -04009940
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009941 inode_inc_iversion(old_dir);
9942 inode_inc_iversion(new_dir);
9943 inode_inc_iversion(old_inode);
Deepa Dinamani04b285f2016-02-06 23:57:21 -08009944 old_dir->i_ctime = old_dir->i_mtime =
9945 new_dir->i_ctime = new_dir->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009946 old_inode->i_ctime = current_time(old_dir);
Chris Mason5f39d392007-10-15 16:14:19 -04009947
Chris Mason12fcfd22009-03-24 10:24:20 -04009948 if (old_dentry->d_parent != new_dentry->d_parent)
David Sterbaf85b7372017-01-20 14:54:07 +01009949 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
9950 BTRFS_I(old_inode), 1);
Chris Mason12fcfd22009-03-24 10:24:20 -04009951
Li Zefan33345d012011-04-20 10:31:50 +08009952 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009953 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
Lu Fengqi401b3b12018-08-01 11:32:30 +08009954 ret = btrfs_unlink_subvol(trans, old_dir, root_objectid,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009955 old_dentry->d_name.name,
9956 old_dentry->d_name.len);
9957 } else {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009958 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
9959 BTRFS_I(d_inode(old_dentry)),
Al Viro92986792011-03-04 17:14:37 +00009960 old_dentry->d_name.name,
9961 old_dentry->d_name.len);
9962 if (!ret)
9963 ret = btrfs_update_inode(trans, root, old_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009964 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009965 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009966 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009967 goto out_fail;
9968 }
Chris Mason39279cc2007-06-12 06:35:45 -04009969
9970 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009971 inode_inc_iversion(new_inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009972 new_inode->i_ctime = current_time(new_inode);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009973 if (unlikely(btrfs_ino(BTRFS_I(new_inode)) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009974 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
9975 root_objectid = BTRFS_I(new_inode)->location.objectid;
Lu Fengqi401b3b12018-08-01 11:32:30 +08009976 ret = btrfs_unlink_subvol(trans, new_dir, root_objectid,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009977 new_dentry->d_name.name,
9978 new_dentry->d_name.len);
9979 BUG_ON(new_inode->i_nlink == 0);
9980 } else {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009981 ret = btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
9982 BTRFS_I(d_inode(new_dentry)),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009983 new_dentry->d_name.name,
9984 new_dentry->d_name.len);
9985 }
Josef Bacik4ef31a42013-08-13 14:10:08 -04009986 if (!ret && new_inode->i_nlink == 0)
Nikolay Borisov73f2e542017-02-20 13:50:59 +02009987 ret = btrfs_orphan_add(trans,
9988 BTRFS_I(d_inode(new_dentry)));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009989 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009990 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009991 goto out_fail;
9992 }
Chris Mason39279cc2007-06-12 06:35:45 -04009993 }
Josef Bacikaec74772008-07-24 12:12:38 -04009994
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009995 ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009996 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04009997 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009998 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009999 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010000 goto out_fail;
10001 }
Chris Mason39279cc2007-06-12 06:35:45 -040010002
Miao Xie67de1172013-12-26 13:07:06 +080010003 if (old_inode->i_nlink == 1)
10004 BTRFS_I(old_inode)->dir_index = index;
10005
Filipe Manana3dc9e8f72016-04-29 11:34:22 +010010006 if (log_pinned) {
Al Viro10d9f302011-07-16 23:09:10 -040010007 struct dentry *parent = new_dentry->d_parent;
Filipe Manana3dc9e8f72016-04-29 11:34:22 +010010008
Filipe Mananad4682ba2018-06-11 19:24:28 +010010009 btrfs_init_log_ctx(&ctx, old_inode);
10010 ret = btrfs_log_new_name(trans, BTRFS_I(old_inode),
10011 BTRFS_I(old_dir), parent,
10012 false, &ctx);
10013 if (ret == BTRFS_NEED_LOG_SYNC)
10014 sync_log = true;
10015 else if (ret == BTRFS_NEED_TRANS_COMMIT)
10016 commit_transaction = true;
10017 ret = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010018 btrfs_end_log_trans(root);
Filipe Manana3dc9e8f72016-04-29 11:34:22 +010010019 log_pinned = false;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010020 }
Dan Fuhrycdd1fed2016-03-17 15:23:38 +010010021
10022 if (flags & RENAME_WHITEOUT) {
10023 ret = btrfs_whiteout_for_rename(trans, root, old_dir,
10024 old_dentry);
10025
10026 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -040010027 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +010010028 goto out_fail;
10029 }
Chris Mason12fcfd22009-03-24 10:24:20 -040010030 }
Chris Mason39279cc2007-06-12 06:35:45 -040010031out_fail:
Filipe Manana3dc9e8f72016-04-29 11:34:22 +010010032 /*
10033 * If we have pinned the log and an error happened, we unpin tasks
10034 * trying to sync the log and force them to fallback to a transaction
10035 * commit if the log currently contains any of the inodes involved in
10036 * this rename operation (to ensure we do not persist a log with an
10037 * inconsistent state for any of these inodes or leading to any
10038 * inconsistencies when replayed). If the transaction was aborted, the
10039 * abortion reason is propagated to userspace when attempting to commit
10040 * the transaction. If the log does not contain any of these inodes, we
10041 * allow the tasks to sync it.
10042 */
10043 if (ret && log_pinned) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +020010044 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
10045 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
10046 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana3dc9e8f72016-04-29 11:34:22 +010010047 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +020010048 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
David Sterba90787762019-03-20 13:28:05 +010010049 btrfs_set_log_full_commit(trans);
Filipe Manana3dc9e8f72016-04-29 11:34:22 +010010050
10051 btrfs_end_log_trans(root);
10052 log_pinned = false;
10053 }
Filipe Mananad4682ba2018-06-11 19:24:28 +010010054 if (!ret && sync_log) {
10055 ret = btrfs_sync_log(trans, BTRFS_I(old_inode)->root, &ctx);
10056 if (ret)
10057 commit_transaction = true;
10058 }
10059 if (commit_transaction) {
10060 ret = btrfs_commit_transaction(trans);
10061 } else {
10062 int ret2;
10063
10064 ret2 = btrfs_end_transaction(trans);
10065 ret = ret ? ret : ret2;
10066 }
Johann Lombardib44c59a2011-03-31 13:23:47 +000010067out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +080010068 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010069 up_read(&fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -040010070
Chris Mason39279cc2007-06-12 06:35:45 -040010071 return ret;
10072}
10073
Miklos Szeredi80ace852014-07-23 15:15:32 +020010074static int btrfs_rename2(struct inode *old_dir, struct dentry *old_dentry,
10075 struct inode *new_dir, struct dentry *new_dentry,
10076 unsigned int flags)
10077{
Dan Fuhrycdd1fed2016-03-17 15:23:38 +010010078 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
Miklos Szeredi80ace852014-07-23 15:15:32 +020010079 return -EINVAL;
10080
Dan Fuhrycdd1fed2016-03-17 15:23:38 +010010081 if (flags & RENAME_EXCHANGE)
10082 return btrfs_rename_exchange(old_dir, old_dentry, new_dir,
10083 new_dentry);
10084
10085 return btrfs_rename(old_dir, old_dentry, new_dir, new_dentry, flags);
Miklos Szeredi80ace852014-07-23 15:15:32 +020010086}
10087
Nikolay Borisov3a2f8c02018-04-24 17:23:59 +030010088struct btrfs_delalloc_work {
10089 struct inode *inode;
10090 struct completion completion;
10091 struct list_head list;
10092 struct btrfs_work work;
10093};
10094
Miao Xie8ccf6f192012-10-25 09:28:04 +000010095static void btrfs_run_delalloc_work(struct btrfs_work *work)
10096{
10097 struct btrfs_delalloc_work *delalloc_work;
Josef Bacik9f23e282013-10-28 15:03:41 -040010098 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010099
10100 delalloc_work = container_of(work, struct btrfs_delalloc_work,
10101 work);
Josef Bacik9f23e282013-10-28 15:03:41 -040010102 inode = delalloc_work->inode;
David Sterba30424602015-11-27 19:27:11 +010010103 filemap_flush(inode->i_mapping);
10104 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
10105 &BTRFS_I(inode)->runtime_flags))
Josef Bacik9f23e282013-10-28 15:03:41 -040010106 filemap_flush(inode->i_mapping);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010107
Nikolay Borisov076da912018-04-23 10:54:16 +030010108 iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010109 complete(&delalloc_work->completion);
10110}
10111
Nikolay Borisov3a2f8c02018-04-24 17:23:59 +030010112static struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode)
Miao Xie8ccf6f192012-10-25 09:28:04 +000010113{
10114 struct btrfs_delalloc_work *work;
10115
David Sterba100d5702015-12-08 14:39:32 +010010116 work = kmalloc(sizeof(*work), GFP_NOFS);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010117 if (!work)
10118 return NULL;
10119
10120 init_completion(&work->completion);
10121 INIT_LIST_HEAD(&work->list);
10122 work->inode = inode;
Omar Sandovala0cac0e2019-09-16 11:30:57 -070010123 btrfs_init_work(&work->work, btrfs_run_delalloc_work, NULL, NULL);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010124
10125 return work;
10126}
10127
Chris Masond352ac62008-09-29 15:18:18 -040010128/*
10129 * some fairly slow code that needs optimization. This walks the list
10130 * of all the inodes with pending delalloc and forces them to disk.
10131 */
Ethan Lien3cd24c62018-11-01 14:49:03 +080010132static int start_delalloc_inodes(struct btrfs_root *root, int nr, bool snapshot)
Chris Masonea8c2812008-08-04 23:17:27 -040010133{
Chris Masonea8c2812008-08-04 23:17:27 -040010134 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010135 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010136 struct btrfs_delalloc_work *work, *next;
10137 struct list_head works;
Miao Xie1eafa6c2013-01-22 10:49:00 +000010138 struct list_head splice;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010139 int ret = 0;
Chris Masonea8c2812008-08-04 23:17:27 -040010140
Miao Xie8ccf6f192012-10-25 09:28:04 +000010141 INIT_LIST_HEAD(&works);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010142 INIT_LIST_HEAD(&splice);
Miao Xie63607cc2013-01-22 10:50:35 +000010143
Miao Xie573bfb72014-03-06 13:55:03 +080010144 mutex_lock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010145 spin_lock(&root->delalloc_lock);
10146 list_splice_init(&root->delalloc_inodes, &splice);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010147 while (!list_empty(&splice)) {
10148 binode = list_entry(splice.next, struct btrfs_inode,
Chris Masonea8c2812008-08-04 23:17:27 -040010149 delalloc_inodes);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010150
Miao Xieeb73c1b2013-05-15 07:48:22 +000010151 list_move_tail(&binode->delalloc_inodes,
10152 &root->delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010153 inode = igrab(&binode->vfs_inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +000010154 if (!inode) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010155 cond_resched_lock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010156 continue;
Miao Xiedf0af1a2013-01-29 10:11:59 +000010157 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010158 spin_unlock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010159
Ethan Lien3cd24c62018-11-01 14:49:03 +080010160 if (snapshot)
10161 set_bit(BTRFS_INODE_SNAPSHOT_FLUSH,
10162 &binode->runtime_flags);
Nikolay Borisov076da912018-04-23 10:54:16 +030010163 work = btrfs_alloc_delalloc_work(inode);
David Sterba5d99a9982014-09-29 19:20:37 +020010164 if (!work) {
Nikolay Borisov4fbb5142018-04-23 10:54:15 +030010165 iput(inode);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010166 ret = -ENOMEM;
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010167 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010168 }
Miao Xie1eafa6c2013-01-22 10:49:00 +000010169 list_add_tail(&work->list, &works);
Qu Wenruoa44903a2014-02-28 10:46:09 +080010170 btrfs_queue_work(root->fs_info->flush_workers,
10171 &work->work);
Miao Xie6c255e62014-03-06 13:55:01 +080010172 ret++;
10173 if (nr != -1 && ret >= nr)
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010174 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010175 cond_resched();
Miao Xieeb73c1b2013-05-15 07:48:22 +000010176 spin_lock(&root->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -040010177 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010178 spin_unlock(&root->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -040010179
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010180out:
Miao Xie1eafa6c2013-01-22 10:49:00 +000010181 list_for_each_entry_safe(work, next, &works, list) {
10182 list_del_init(&work->list);
Nikolay Borisov40012f92018-04-19 10:46:39 +030010183 wait_for_completion(&work->completion);
10184 kfree(work);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010185 }
10186
Nikolay Borisov81f1d392018-04-19 10:46:37 +030010187 if (!list_empty(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010188 spin_lock(&root->delalloc_lock);
10189 list_splice_tail(&splice, &root->delalloc_inodes);
10190 spin_unlock(&root->delalloc_lock);
10191 }
Miao Xie573bfb72014-03-06 13:55:03 +080010192 mutex_unlock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010193 return ret;
10194}
10195
Ethan Lien3cd24c62018-11-01 14:49:03 +080010196int btrfs_start_delalloc_snapshot(struct btrfs_root *root)
Miao Xieeb73c1b2013-05-15 07:48:22 +000010197{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010198 struct btrfs_fs_info *fs_info = root->fs_info;
Miao Xieeb73c1b2013-05-15 07:48:22 +000010199 int ret;
10200
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010201 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +000010202 return -EROFS;
10203
Ethan Lien3cd24c62018-11-01 14:49:03 +080010204 ret = start_delalloc_inodes(root, -1, true);
Miao Xie6c255e62014-03-06 13:55:01 +080010205 if (ret > 0)
10206 ret = 0;
Miao Xieeb73c1b2013-05-15 07:48:22 +000010207 return ret;
10208}
10209
Nikolay Borisov82b3e532018-04-23 10:54:13 +030010210int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, int nr)
Miao Xieeb73c1b2013-05-15 07:48:22 +000010211{
10212 struct btrfs_root *root;
10213 struct list_head splice;
10214 int ret;
10215
Wang Shilong2c21b4d2014-01-14 19:42:20 +080010216 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +000010217 return -EROFS;
10218
10219 INIT_LIST_HEAD(&splice);
10220
Miao Xie573bfb72014-03-06 13:55:03 +080010221 mutex_lock(&fs_info->delalloc_root_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010222 spin_lock(&fs_info->delalloc_root_lock);
10223 list_splice_init(&fs_info->delalloc_roots, &splice);
Miao Xie6c255e62014-03-06 13:55:01 +080010224 while (!list_empty(&splice) && nr) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010225 root = list_first_entry(&splice, struct btrfs_root,
10226 delalloc_root);
10227 root = btrfs_grab_fs_root(root);
10228 BUG_ON(!root);
10229 list_move_tail(&root->delalloc_root,
10230 &fs_info->delalloc_roots);
10231 spin_unlock(&fs_info->delalloc_root_lock);
10232
Ethan Lien3cd24c62018-11-01 14:49:03 +080010233 ret = start_delalloc_inodes(root, nr, false);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010234 btrfs_put_fs_root(root);
Miao Xie6c255e62014-03-06 13:55:01 +080010235 if (ret < 0)
Miao Xieeb73c1b2013-05-15 07:48:22 +000010236 goto out;
10237
Miao Xie6c255e62014-03-06 13:55:01 +080010238 if (nr != -1) {
10239 nr -= ret;
10240 WARN_ON(nr < 0);
10241 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010242 spin_lock(&fs_info->delalloc_root_lock);
10243 }
10244 spin_unlock(&fs_info->delalloc_root_lock);
10245
Miao Xie6c255e62014-03-06 13:55:01 +080010246 ret = 0;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010247out:
Nikolay Borisov81f1d392018-04-19 10:46:37 +030010248 if (!list_empty(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010249 spin_lock(&fs_info->delalloc_root_lock);
10250 list_splice_tail(&splice, &fs_info->delalloc_roots);
10251 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010252 }
Miao Xie573bfb72014-03-06 13:55:03 +080010253 mutex_unlock(&fs_info->delalloc_root_mutex);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010254 return ret;
Chris Masonea8c2812008-08-04 23:17:27 -040010255}
10256
Chris Mason39279cc2007-06-12 06:35:45 -040010257static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
10258 const char *symname)
10259{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010260 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -040010261 struct btrfs_trans_handle *trans;
10262 struct btrfs_root *root = BTRFS_I(dir)->root;
10263 struct btrfs_path *path;
10264 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -050010265 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -040010266 int err;
Chris Mason39279cc2007-06-12 06:35:45 -040010267 u64 objectid;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +053010268 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -040010269 int name_len;
10270 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -040010271 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -040010272 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -040010273 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -040010274
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +010010275 name_len = strlen(symname);
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010276 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info))
Chris Mason39279cc2007-06-12 06:35:45 -040010277 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -050010278
Josef Bacik9ed74f22009-09-11 16:12:44 -040010279 /*
10280 * 2 items for inode item and ref
10281 * 2 items for dir items
Filipe Manana9269d122015-12-31 18:16:29 +000010282 * 1 item for updating parent inode item
10283 * 1 item for the inline extent item
Josef Bacik9ed74f22009-09-11 16:12:44 -040010284 * 1 item for xattr if selinux is on
10285 */
Filipe Manana9269d122015-12-31 18:16:29 +000010286 trans = btrfs_start_transaction(root, 7);
Yan, Zhenga22285a2010-05-16 10:48:46 -040010287 if (IS_ERR(trans))
10288 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -050010289
Li Zefan581bb052011-04-20 10:06:11 +080010290 err = btrfs_find_free_ino(root, &objectid);
10291 if (err)
10292 goto out_unlock;
10293
Josef Bacikaec74772008-07-24 12:12:38 -040010294 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +010010295 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)),
10296 objectid, S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -040010297 if (IS_ERR(inode)) {
10298 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -040010299 inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -040010300 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -040010301 }
Chris Mason39279cc2007-06-12 06:35:45 -040010302
Casey Schauflerad19db72011-12-15 10:09:07 -050010303 /*
10304 * If the active LSM wants to access the inode during
10305 * d_instantiate it needs these. Smack checks to see
10306 * if the filesystem supports xattrs by looking at the
10307 * ops vector.
10308 */
10309 inode->i_fop = &btrfs_file_operations;
10310 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -070010311 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -070010312 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
10313
10314 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
10315 if (err)
Al Viro32955c52018-05-16 12:20:05 -040010316 goto out_unlock;
Casey Schauflerad19db72011-12-15 10:09:07 -050010317
Chris Mason39279cc2007-06-12 06:35:45 -040010318 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -070010319 if (!path) {
10320 err = -ENOMEM;
Al Viro32955c52018-05-16 12:20:05 -040010321 goto out_unlock;
Mark Fashehd8926bb2011-07-13 10:38:47 -070010322 }
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +020010323 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason39279cc2007-06-12 06:35:45 -040010324 key.offset = 0;
David Sterba962a2982014-06-04 18:41:45 +020010325 key.type = BTRFS_EXTENT_DATA_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -040010326 datasize = btrfs_file_extent_calc_inline_size(name_len);
10327 err = btrfs_insert_empty_item(trans, root, path, &key,
10328 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -040010329 if (err) {
Julia Lawallb0839162011-05-14 07:10:51 +000010330 btrfs_free_path(path);
Al Viro32955c52018-05-16 12:20:05 -040010331 goto out_unlock;
Chris Mason54aa1f42007-06-22 14:16:25 -040010332 }
Chris Mason5f39d392007-10-15 16:14:19 -040010333 leaf = path->nodes[0];
10334 ei = btrfs_item_ptr(leaf, path->slots[0],
10335 struct btrfs_file_extent_item);
10336 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
10337 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -040010338 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -040010339 btrfs_set_file_extent_encryption(leaf, ei, 0);
10340 btrfs_set_file_extent_compression(leaf, ei, 0);
10341 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
10342 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
10343
Chris Mason39279cc2007-06-12 06:35:45 -040010344 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -040010345 write_extent_buffer(leaf, symname, ptr, name_len);
10346 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -040010347 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -040010348
Chris Mason39279cc2007-06-12 06:35:45 -040010349 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -050010350 inode_nohighmem(inode);
Yan Zhengd899e052008-10-30 14:25:28 -040010351 inode_set_bytes(inode, name_len);
Nikolay Borisov6ef06d22017-02-20 13:50:34 +020010352 btrfs_i_size_write(BTRFS_I(inode), name_len);
Chris Mason54aa1f42007-06-22 14:16:25 -040010353 err = btrfs_update_inode(trans, root, inode);
Filipe Mananad50866d2015-12-31 18:08:24 +000010354 /*
10355 * Last step, add directory indexes for our symlink inode. This is the
10356 * last step to avoid extra cleanup of these indexes if an error happens
10357 * elsewhere above.
10358 */
10359 if (!err)
Nikolay Borisovcef415a2017-02-20 13:51:09 +020010360 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry,
10361 BTRFS_I(inode), 0, index);
Al Viro32955c52018-05-16 12:20:05 -040010362 if (err)
10363 goto out_unlock;
Chris Masonb0d5d102014-09-08 13:08:51 -070010364
Al Viro1e2e5472018-05-04 08:23:01 -040010365 d_instantiate_new(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010366
10367out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010368 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -040010369 if (err && inode) {
Chris Mason39279cc2007-06-12 06:35:45 -040010370 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -040010371 discard_new_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010372 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010373 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -040010374 return err;
10375}
Chris Mason16432982008-04-10 10:23:21 -040010376
Josef Bacik0af3d002010-06-21 14:48:16 -040010377static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
10378 u64 start, u64 num_bytes, u64 min_size,
10379 loff_t actual_len, u64 *alloc_hint,
10380 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -040010381{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010382 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik5dc562c2012-08-17 13:14:17 -040010383 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
10384 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -040010385 struct btrfs_root *root = BTRFS_I(inode)->root;
10386 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -040010387 u64 cur_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +000010388 u64 i_size;
Chris Mason154ea282013-03-05 11:11:26 -050010389 u64 cur_bytes;
Josef Bacik0b670dc2015-09-23 17:11:16 -040010390 u64 last_alloc = (u64)-1;
Yan Zhengd899e052008-10-30 14:25:28 -040010391 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -040010392 bool own_trans = true;
Wang Xiaoguang18513092016-07-25 15:51:40 +080010393 u64 end = start + num_bytes - 1;
Yan Zhengd899e052008-10-30 14:25:28 -040010394
Josef Bacik0af3d002010-06-21 14:48:16 -040010395 if (trans)
10396 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -040010397 while (num_bytes > 0) {
Josef Bacik0af3d002010-06-21 14:48:16 -040010398 if (own_trans) {
10399 trans = btrfs_start_transaction(root, 3);
10400 if (IS_ERR(trans)) {
10401 ret = PTR_ERR(trans);
10402 break;
10403 }
Yan Zhengd899e052008-10-30 14:25:28 -040010404 }
Yan, Zheng5a303d52009-11-12 09:34:52 +000010405
Byongho Leeee221842015-12-15 01:42:10 +090010406 cur_bytes = min_t(u64, num_bytes, SZ_256M);
Chris Mason154ea282013-03-05 11:11:26 -050010407 cur_bytes = max(cur_bytes, min_size);
Josef Bacik0b670dc2015-09-23 17:11:16 -040010408 /*
10409 * If we are severely fragmented we could end up with really
10410 * small allocations, so if the allocator is returning small
10411 * chunks lets make its job easier by only searching for those
10412 * sized chunks.
10413 */
10414 cur_bytes = min(cur_bytes, last_alloc);
Wang Xiaoguang18513092016-07-25 15:51:40 +080010415 ret = btrfs_reserve_extent(root, cur_bytes, cur_bytes,
10416 min_size, 0, *alloc_hint, &ins, 1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010417 if (ret) {
Josef Bacik0af3d002010-06-21 14:48:16 -040010418 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010419 btrfs_end_transaction(trans);
Yan, Zhenga22285a2010-05-16 10:48:46 -040010420 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +000010421 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010422 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010423
Josef Bacik0b670dc2015-09-23 17:11:16 -040010424 last_alloc = ins.offset;
Yan Zhengd899e052008-10-30 14:25:28 -040010425 ret = insert_reserved_file_extent(trans, inode,
10426 cur_offset, ins.objectid,
10427 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +000010428 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -040010429 BTRFS_FILE_EXTENT_PREALLOC);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010430 if (ret) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010431 btrfs_free_reserved_extent(fs_info, ins.objectid,
Miao Xiee570fd22014-06-19 10:42:50 +080010432 ins.offset, 0);
Jeff Mahoney66642832016-06-10 18:19:25 -040010433 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010434 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010435 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010436 break;
10437 }
Dongsheng Yang31193212014-12-12 16:44:35 +080010438
Nikolay Borisovdcdbc052017-02-20 13:50:45 +020010439 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Chris Masona1ed8352009-09-11 12:27:37 -040010440 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010441
Josef Bacik5dc562c2012-08-17 13:14:17 -040010442 em = alloc_extent_map();
10443 if (!em) {
10444 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
10445 &BTRFS_I(inode)->runtime_flags);
10446 goto next;
10447 }
10448
10449 em->start = cur_offset;
10450 em->orig_start = cur_offset;
10451 em->len = ins.offset;
10452 em->block_start = ins.objectid;
10453 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -050010454 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -040010455 em->ram_bytes = ins.offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010456 em->bdev = fs_info->fs_devices->latest_bdev;
Josef Bacik5dc562c2012-08-17 13:14:17 -040010457 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
10458 em->generation = trans->transid;
10459
10460 while (1) {
10461 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -040010462 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -040010463 write_unlock(&em_tree->lock);
10464 if (ret != -EEXIST)
10465 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +020010466 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -040010467 cur_offset + ins.offset - 1,
10468 0);
10469 }
10470 free_extent_map(em);
10471next:
Yan Zhengd899e052008-10-30 14:25:28 -040010472 num_bytes -= ins.offset;
10473 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -040010474 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +000010475
Josef Bacik0c4d2d92012-04-05 15:03:02 -040010476 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -070010477 inode->i_ctime = current_time(inode);
Christoph Hellwig6cbff002009-04-17 10:37:41 +020010478 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -040010479 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -040010480 (actual_len > inode->i_size) &&
10481 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +000010482 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +000010483 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +000010484 else
Josef Bacik55a61d12010-11-22 18:50:32 +000010485 i_size = cur_offset;
10486 i_size_write(inode, i_size);
10487 btrfs_ordered_update_i_size(inode, i_size, NULL);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010488 }
10489
Yan Zhengd899e052008-10-30 14:25:28 -040010490 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010491
10492 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -040010493 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010494 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010495 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010496 break;
10497 }
Yan Zhengd899e052008-10-30 14:25:28 -040010498
Josef Bacik0af3d002010-06-21 14:48:16 -040010499 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010500 btrfs_end_transaction(trans);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010501 }
Wang Xiaoguang18513092016-07-25 15:51:40 +080010502 if (cur_offset < end)
Qu Wenruobc42bda2017-02-27 15:10:39 +080010503 btrfs_free_reserved_data_space(inode, NULL, cur_offset,
Wang Xiaoguang18513092016-07-25 15:51:40 +080010504 end - cur_offset + 1);
Yan Zhengd899e052008-10-30 14:25:28 -040010505 return ret;
10506}
10507
Josef Bacik0af3d002010-06-21 14:48:16 -040010508int btrfs_prealloc_file_range(struct inode *inode, int mode,
10509 u64 start, u64 num_bytes, u64 min_size,
10510 loff_t actual_len, u64 *alloc_hint)
10511{
10512 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
10513 min_size, actual_len, alloc_hint,
10514 NULL);
10515}
10516
10517int btrfs_prealloc_file_range_trans(struct inode *inode,
10518 struct btrfs_trans_handle *trans, int mode,
10519 u64 start, u64 num_bytes, u64 min_size,
10520 loff_t actual_len, u64 *alloc_hint)
10521{
10522 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
10523 min_size, actual_len, alloc_hint, trans);
10524}
10525
Chris Masone6dcd2d2008-07-17 12:53:50 -040010526static int btrfs_set_page_dirty(struct page *page)
10527{
Chris Masone6dcd2d2008-07-17 12:53:50 -040010528 return __set_page_dirty_nobuffers(page);
10529}
10530
Al Viro10556cb22011-06-20 19:28:19 -040010531static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -050010532{
Li Zefanb83cc962010-12-20 16:04:08 +080010533 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +000010534 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +080010535
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +000010536 if (mask & MAY_WRITE &&
10537 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
10538 if (btrfs_root_readonly(root))
10539 return -EROFS;
10540 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
10541 return -EACCES;
10542 }
Al Viro2830ba72011-06-20 19:16:29 -040010543 return generic_permission(inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -050010544}
Chris Mason39279cc2007-06-12 06:35:45 -040010545
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010546static int btrfs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
10547{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010548 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010549 struct btrfs_trans_handle *trans;
10550 struct btrfs_root *root = BTRFS_I(dir)->root;
10551 struct inode *inode = NULL;
10552 u64 objectid;
10553 u64 index;
10554 int ret = 0;
10555
10556 /*
10557 * 5 units required for adding orphan entry
10558 */
10559 trans = btrfs_start_transaction(root, 5);
10560 if (IS_ERR(trans))
10561 return PTR_ERR(trans);
10562
10563 ret = btrfs_find_free_ino(root, &objectid);
10564 if (ret)
10565 goto out;
10566
10567 inode = btrfs_new_inode(trans, root, dir, NULL, 0,
David Sterbaf85b7372017-01-20 14:54:07 +010010568 btrfs_ino(BTRFS_I(dir)), objectid, mode, &index);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010569 if (IS_ERR(inode)) {
10570 ret = PTR_ERR(inode);
10571 inode = NULL;
10572 goto out;
10573 }
10574
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010575 inode->i_fop = &btrfs_file_operations;
10576 inode->i_op = &btrfs_file_inode_operations;
10577
10578 inode->i_mapping->a_ops = &btrfs_aops;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010579 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
10580
Chris Masonb0d5d102014-09-08 13:08:51 -070010581 ret = btrfs_init_inode_security(trans, inode, dir, NULL);
10582 if (ret)
Al Viro32955c52018-05-16 12:20:05 -040010583 goto out;
Chris Masonb0d5d102014-09-08 13:08:51 -070010584
10585 ret = btrfs_update_inode(trans, root, inode);
10586 if (ret)
Al Viro32955c52018-05-16 12:20:05 -040010587 goto out;
Nikolay Borisov73f2e542017-02-20 13:50:59 +020010588 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010589 if (ret)
Al Viro32955c52018-05-16 12:20:05 -040010590 goto out;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010591
Filipe Manana5762b5c2014-08-01 00:10:32 +010010592 /*
10593 * We set number of links to 0 in btrfs_new_inode(), and here we set
10594 * it to 1 because d_tmpfile() will issue a warning if the count is 0,
10595 * through:
10596 *
10597 * d_tmpfile() -> inode_dec_link_count() -> drop_nlink()
10598 */
10599 set_nlink(inode, 1);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010600 d_tmpfile(dentry, inode);
Al Viro32955c52018-05-16 12:20:05 -040010601 unlock_new_inode(inode);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010602 mark_inode_dirty(inode);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010603out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010604 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -040010605 if (ret && inode)
10606 discard_new_inode(inode);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010607 btrfs_btree_balance_dirty(fs_info);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010608 return ret;
10609}
10610
David Sterba5cdc84b2018-07-18 20:32:52 +020010611void btrfs_set_range_writeback(struct extent_io_tree *tree, u64 start, u64 end)
Josef Bacikc6100a42017-05-05 11:57:13 -040010612{
David Sterba5cdc84b2018-07-18 20:32:52 +020010613 struct inode *inode = tree->private_data;
Josef Bacikc6100a42017-05-05 11:57:13 -040010614 unsigned long index = start >> PAGE_SHIFT;
10615 unsigned long end_index = end >> PAGE_SHIFT;
10616 struct page *page;
10617
10618 while (index <= end_index) {
10619 page = find_get_page(inode->i_mapping, index);
10620 ASSERT(page); /* Pages should be in the extent_io_tree */
10621 set_page_writeback(page);
10622 put_page(page);
10623 index++;
10624 }
10625}
10626
Omar Sandovaled46ff32016-11-03 10:28:14 -070010627#ifdef CONFIG_SWAP
10628/*
10629 * Add an entry indicating a block group or device which is pinned by a
10630 * swapfile. Returns 0 on success, 1 if there is already an entry for it, or a
10631 * negative errno on failure.
10632 */
10633static int btrfs_add_swapfile_pin(struct inode *inode, void *ptr,
10634 bool is_block_group)
10635{
10636 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
10637 struct btrfs_swapfile_pin *sp, *entry;
10638 struct rb_node **p;
10639 struct rb_node *parent = NULL;
10640
10641 sp = kmalloc(sizeof(*sp), GFP_NOFS);
10642 if (!sp)
10643 return -ENOMEM;
10644 sp->ptr = ptr;
10645 sp->inode = inode;
10646 sp->is_block_group = is_block_group;
10647
10648 spin_lock(&fs_info->swapfile_pins_lock);
10649 p = &fs_info->swapfile_pins.rb_node;
10650 while (*p) {
10651 parent = *p;
10652 entry = rb_entry(parent, struct btrfs_swapfile_pin, node);
10653 if (sp->ptr < entry->ptr ||
10654 (sp->ptr == entry->ptr && sp->inode < entry->inode)) {
10655 p = &(*p)->rb_left;
10656 } else if (sp->ptr > entry->ptr ||
10657 (sp->ptr == entry->ptr && sp->inode > entry->inode)) {
10658 p = &(*p)->rb_right;
10659 } else {
10660 spin_unlock(&fs_info->swapfile_pins_lock);
10661 kfree(sp);
10662 return 1;
10663 }
10664 }
10665 rb_link_node(&sp->node, parent, p);
10666 rb_insert_color(&sp->node, &fs_info->swapfile_pins);
10667 spin_unlock(&fs_info->swapfile_pins_lock);
10668 return 0;
10669}
10670
10671/* Free all of the entries pinned by this swapfile. */
10672static void btrfs_free_swapfile_pins(struct inode *inode)
10673{
10674 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
10675 struct btrfs_swapfile_pin *sp;
10676 struct rb_node *node, *next;
10677
10678 spin_lock(&fs_info->swapfile_pins_lock);
10679 node = rb_first(&fs_info->swapfile_pins);
10680 while (node) {
10681 next = rb_next(node);
10682 sp = rb_entry(node, struct btrfs_swapfile_pin, node);
10683 if (sp->inode == inode) {
10684 rb_erase(&sp->node, &fs_info->swapfile_pins);
10685 if (sp->is_block_group)
10686 btrfs_put_block_group(sp->ptr);
10687 kfree(sp);
10688 }
10689 node = next;
10690 }
10691 spin_unlock(&fs_info->swapfile_pins_lock);
10692}
10693
10694struct btrfs_swap_info {
10695 u64 start;
10696 u64 block_start;
10697 u64 block_len;
10698 u64 lowest_ppage;
10699 u64 highest_ppage;
10700 unsigned long nr_pages;
10701 int nr_extents;
10702};
10703
10704static int btrfs_add_swap_extent(struct swap_info_struct *sis,
10705 struct btrfs_swap_info *bsi)
10706{
10707 unsigned long nr_pages;
10708 u64 first_ppage, first_ppage_reported, next_ppage;
10709 int ret;
10710
10711 first_ppage = ALIGN(bsi->block_start, PAGE_SIZE) >> PAGE_SHIFT;
10712 next_ppage = ALIGN_DOWN(bsi->block_start + bsi->block_len,
10713 PAGE_SIZE) >> PAGE_SHIFT;
10714
10715 if (first_ppage >= next_ppage)
10716 return 0;
10717 nr_pages = next_ppage - first_ppage;
10718
10719 first_ppage_reported = first_ppage;
10720 if (bsi->start == 0)
10721 first_ppage_reported++;
10722 if (bsi->lowest_ppage > first_ppage_reported)
10723 bsi->lowest_ppage = first_ppage_reported;
10724 if (bsi->highest_ppage < (next_ppage - 1))
10725 bsi->highest_ppage = next_ppage - 1;
10726
10727 ret = add_swap_extent(sis, bsi->nr_pages, nr_pages, first_ppage);
10728 if (ret < 0)
10729 return ret;
10730 bsi->nr_extents += ret;
10731 bsi->nr_pages += nr_pages;
10732 return 0;
10733}
10734
10735static void btrfs_swap_deactivate(struct file *file)
10736{
10737 struct inode *inode = file_inode(file);
10738
10739 btrfs_free_swapfile_pins(inode);
10740 atomic_dec(&BTRFS_I(inode)->root->nr_swapfiles);
10741}
10742
10743static int btrfs_swap_activate(struct swap_info_struct *sis, struct file *file,
10744 sector_t *span)
10745{
10746 struct inode *inode = file_inode(file);
10747 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
10748 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
10749 struct extent_state *cached_state = NULL;
10750 struct extent_map *em = NULL;
10751 struct btrfs_device *device = NULL;
10752 struct btrfs_swap_info bsi = {
10753 .lowest_ppage = (sector_t)-1ULL,
10754 };
10755 int ret = 0;
10756 u64 isize;
10757 u64 start;
10758
10759 /*
10760 * If the swap file was just created, make sure delalloc is done. If the
10761 * file changes again after this, the user is doing something stupid and
10762 * we don't really care.
10763 */
10764 ret = btrfs_wait_ordered_range(inode, 0, (u64)-1);
10765 if (ret)
10766 return ret;
10767
10768 /*
10769 * The inode is locked, so these flags won't change after we check them.
10770 */
10771 if (BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS) {
10772 btrfs_warn(fs_info, "swapfile must not be compressed");
10773 return -EINVAL;
10774 }
10775 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW)) {
10776 btrfs_warn(fs_info, "swapfile must not be copy-on-write");
10777 return -EINVAL;
10778 }
10779 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
10780 btrfs_warn(fs_info, "swapfile must not be checksummed");
10781 return -EINVAL;
10782 }
10783
10784 /*
10785 * Balance or device remove/replace/resize can move stuff around from
10786 * under us. The EXCL_OP flag makes sure they aren't running/won't run
10787 * concurrently while we are mapping the swap extents, and
10788 * fs_info->swapfile_pins prevents them from running while the swap file
10789 * is active and moving the extents. Note that this also prevents a
10790 * concurrent device add which isn't actually necessary, but it's not
10791 * really worth the trouble to allow it.
10792 */
10793 if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) {
10794 btrfs_warn(fs_info,
10795 "cannot activate swapfile while exclusive operation is running");
10796 return -EBUSY;
10797 }
10798 /*
10799 * Snapshots can create extents which require COW even if NODATACOW is
10800 * set. We use this counter to prevent snapshots. We must increment it
10801 * before walking the extents because we don't want a concurrent
10802 * snapshot to run after we've already checked the extents.
10803 */
10804 atomic_inc(&BTRFS_I(inode)->root->nr_swapfiles);
10805
10806 isize = ALIGN_DOWN(inode->i_size, fs_info->sectorsize);
10807
10808 lock_extent_bits(io_tree, 0, isize - 1, &cached_state);
10809 start = 0;
10810 while (start < isize) {
10811 u64 logical_block_start, physical_block_start;
10812 struct btrfs_block_group_cache *bg;
10813 u64 len = isize - start;
10814
10815 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len, 0);
10816 if (IS_ERR(em)) {
10817 ret = PTR_ERR(em);
10818 goto out;
10819 }
10820
10821 if (em->block_start == EXTENT_MAP_HOLE) {
10822 btrfs_warn(fs_info, "swapfile must not have holes");
10823 ret = -EINVAL;
10824 goto out;
10825 }
10826 if (em->block_start == EXTENT_MAP_INLINE) {
10827 /*
10828 * It's unlikely we'll ever actually find ourselves
10829 * here, as a file small enough to fit inline won't be
10830 * big enough to store more than the swap header, but in
10831 * case something changes in the future, let's catch it
10832 * here rather than later.
10833 */
10834 btrfs_warn(fs_info, "swapfile must not be inline");
10835 ret = -EINVAL;
10836 goto out;
10837 }
10838 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
10839 btrfs_warn(fs_info, "swapfile must not be compressed");
10840 ret = -EINVAL;
10841 goto out;
10842 }
10843
10844 logical_block_start = em->block_start + (start - em->start);
10845 len = min(len, em->len - (start - em->start));
10846 free_extent_map(em);
10847 em = NULL;
10848
10849 ret = can_nocow_extent(inode, start, &len, NULL, NULL, NULL);
10850 if (ret < 0) {
10851 goto out;
10852 } else if (ret) {
10853 ret = 0;
10854 } else {
10855 btrfs_warn(fs_info,
10856 "swapfile must not be copy-on-write");
10857 ret = -EINVAL;
10858 goto out;
10859 }
10860
10861 em = btrfs_get_chunk_map(fs_info, logical_block_start, len);
10862 if (IS_ERR(em)) {
10863 ret = PTR_ERR(em);
10864 goto out;
10865 }
10866
10867 if (em->map_lookup->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
10868 btrfs_warn(fs_info,
10869 "swapfile must have single data profile");
10870 ret = -EINVAL;
10871 goto out;
10872 }
10873
10874 if (device == NULL) {
10875 device = em->map_lookup->stripes[0].dev;
10876 ret = btrfs_add_swapfile_pin(inode, device, false);
10877 if (ret == 1)
10878 ret = 0;
10879 else if (ret)
10880 goto out;
10881 } else if (device != em->map_lookup->stripes[0].dev) {
10882 btrfs_warn(fs_info, "swapfile must be on one device");
10883 ret = -EINVAL;
10884 goto out;
10885 }
10886
10887 physical_block_start = (em->map_lookup->stripes[0].physical +
10888 (logical_block_start - em->start));
10889 len = min(len, em->len - (logical_block_start - em->start));
10890 free_extent_map(em);
10891 em = NULL;
10892
10893 bg = btrfs_lookup_block_group(fs_info, logical_block_start);
10894 if (!bg) {
10895 btrfs_warn(fs_info,
10896 "could not find block group containing swapfile");
10897 ret = -EINVAL;
10898 goto out;
10899 }
10900
10901 ret = btrfs_add_swapfile_pin(inode, bg, true);
10902 if (ret) {
10903 btrfs_put_block_group(bg);
10904 if (ret == 1)
10905 ret = 0;
10906 else
10907 goto out;
10908 }
10909
10910 if (bsi.block_len &&
10911 bsi.block_start + bsi.block_len == physical_block_start) {
10912 bsi.block_len += len;
10913 } else {
10914 if (bsi.block_len) {
10915 ret = btrfs_add_swap_extent(sis, &bsi);
10916 if (ret)
10917 goto out;
10918 }
10919 bsi.start = start;
10920 bsi.block_start = physical_block_start;
10921 bsi.block_len = len;
10922 }
10923
10924 start += len;
10925 }
10926
10927 if (bsi.block_len)
10928 ret = btrfs_add_swap_extent(sis, &bsi);
10929
10930out:
10931 if (!IS_ERR_OR_NULL(em))
10932 free_extent_map(em);
10933
10934 unlock_extent_cached(io_tree, 0, isize - 1, &cached_state);
10935
10936 if (ret)
10937 btrfs_swap_deactivate(file);
10938
10939 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
10940
10941 if (ret)
10942 return ret;
10943
10944 if (device)
10945 sis->bdev = device->bdev;
10946 *span = bsi.highest_ppage - bsi.lowest_ppage + 1;
10947 sis->max = bsi.nr_pages;
10948 sis->pages = bsi.nr_pages - 1;
10949 sis->highest_bit = bsi.nr_pages - 1;
10950 return bsi.nr_extents;
10951}
10952#else
10953static void btrfs_swap_deactivate(struct file *file)
10954{
10955}
10956
10957static int btrfs_swap_activate(struct swap_info_struct *sis, struct file *file,
10958 sector_t *span)
10959{
10960 return -EOPNOTSUPP;
10961}
10962#endif
10963
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010964static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -050010965 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -040010966 .lookup = btrfs_lookup,
10967 .create = btrfs_create,
10968 .unlink = btrfs_unlink,
10969 .link = btrfs_link,
10970 .mkdir = btrfs_mkdir,
10971 .rmdir = btrfs_rmdir,
Miklos Szeredi2773bf02016-09-27 11:03:58 +020010972 .rename = btrfs_rename2,
Chris Mason39279cc2007-06-12 06:35:45 -040010973 .symlink = btrfs_symlink,
10974 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -040010975 .mknod = btrfs_mknod,
Josef Bacik5103e942007-11-16 11:45:54 -050010976 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010977 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010978 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010979 .set_acl = btrfs_set_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070010980 .update_time = btrfs_update_time,
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010981 .tmpfile = btrfs_tmpfile,
Chris Mason39279cc2007-06-12 06:35:45 -040010982};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010983static const struct inode_operations btrfs_dir_ro_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010984 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -050010985 .permission = btrfs_permission,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070010986 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010987};
Yan, Zheng76dda932009-09-21 16:00:26 -040010988
Alexey Dobriyan828c0952009-10-01 15:43:56 -070010989static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010990 .llseek = generic_file_llseek,
10991 .read = generic_read_dir,
Omar Sandoval02dbfc92016-05-20 13:50:33 -070010992 .iterate_shared = btrfs_real_readdir,
Josef Bacik23b5ec72017-07-24 15:14:25 -040010993 .open = btrfs_opendir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -040010994 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010995#ifdef CONFIG_COMPAT
Luke Dashjr4c63c242015-10-29 08:22:21 +000010996 .compat_ioctl = btrfs_compat_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010997#endif
Sage Weil6bf13c02008-06-10 10:07:39 -040010998 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -040010999 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -040011000};
11001
David Sterba20e55062015-11-19 11:42:28 +010011002static const struct extent_io_ops btrfs_extent_io_ops = {
David Sterba4d53ddd2017-02-17 15:27:44 +010011003 /* mandatory callbacks */
Chris Mason065631f2008-02-20 12:07:25 -050011004 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -040011005 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
11006};
11007
Chris Mason35054392009-01-21 13:11:13 -050011008/*
11009 * btrfs doesn't support the bmap operation because swapfiles
11010 * use bmap to make a mapping of extents in the file. They assume
11011 * these extents won't change over the life of the file and they
11012 * use the bmap result to do IO directly to the drive.
11013 *
11014 * the btrfs bmap call would return logical addresses that aren't
11015 * suitable for IO and they also will change frequently as COW
11016 * operations happen. So, swapfile + btrfs == corruption.
11017 *
11018 * For now we're avoiding this by dropping bmap.
11019 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -070011020static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -040011021 .readpage = btrfs_readpage,
11022 .writepage = btrfs_writepage,
Chris Masonb293f02e2007-11-01 19:45:34 -040011023 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -050011024 .readpages = btrfs_readpages,
Chris Mason16432982008-04-10 10:23:21 -040011025 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -040011026 .invalidatepage = btrfs_invalidatepage,
11027 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -040011028 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +020011029 .error_remove_page = generic_error_remove_page,
Omar Sandovaled46ff32016-11-03 10:28:14 -070011030 .swap_activate = btrfs_swap_activate,
11031 .swap_deactivate = btrfs_swap_deactivate,
Chris Mason39279cc2007-06-12 06:35:45 -040011032};
11033
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070011034static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040011035 .getattr = btrfs_getattr,
11036 .setattr = btrfs_setattr,
Josef Bacik5103e942007-11-16 11:45:54 -050011037 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050011038 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -050011039 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020011040 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080011041 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040011042 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040011043};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070011044static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -040011045 .getattr = btrfs_getattr,
11046 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050011047 .permission = btrfs_permission,
Josef Bacik33268ea2008-07-24 12:16:36 -040011048 .listxattr = btrfs_listxattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020011049 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080011050 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040011051 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -040011052};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070011053static const struct inode_operations btrfs_symlink_inode_operations = {
Al Viro6b255392015-11-17 10:20:54 -050011054 .get_link = page_get_link,
Li Zefanf2095612010-11-19 02:05:24 +000011055 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -050011056 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050011057 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -050011058 .listxattr = btrfs_listxattr,
Josef Bacike41f9412012-03-26 09:46:47 -040011059 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040011060};
Yan, Zheng76dda932009-09-21 16:00:26 -040011061
Alexey Dobriyan82d339d2009-10-09 09:54:36 -040011062const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -040011063 .d_delete = btrfs_dentry_delete,
11064};