blob: d3f7abf50c91d2cf3fa4509cd6f2ae32f1c8be39 [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 */
Nikolay Borisov1368c6d2019-03-12 17:20:27 +0200715 if (page_offset(async_chunk->locked_page) >= start &&
716 page_offset(async_chunk->locked_page) <= end)
717 __set_page_dirty_nobuffers(async_chunk->locked_page);
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700718 /* unlocked later on in the async handlers */
719
720 if (redirty)
721 extent_range_redirty_for_io(inode, start, end);
Nikolay Borisovb5326272019-03-12 17:20:25 +0200722 add_async_extent(async_chunk, start, end - start + 1, 0, NULL, 0,
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700723 BTRFS_COMPRESS_NONE);
Nikolay Borisovac3e9932019-07-17 14:41:44 +0300724 compressed_extents++;
Chris Mason771ed682008-11-06 22:02:51 -0500725
Nikolay Borisovac3e9932019-07-17 14:41:44 +0300726 return compressed_extents;
Chris Mason771ed682008-11-06 22:02:51 -0500727}
Chris Mason771ed682008-11-06 22:02:51 -0500728
Filipe Manana40ae8372014-10-06 22:14:24 +0100729static void free_async_extent_pages(struct async_extent *async_extent)
730{
731 int i;
732
733 if (!async_extent->pages)
734 return;
735
736 for (i = 0; i < async_extent->nr_pages; i++) {
737 WARN_ON(async_extent->pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300738 put_page(async_extent->pages[i]);
Filipe Manana40ae8372014-10-06 22:14:24 +0100739 }
740 kfree(async_extent->pages);
741 async_extent->nr_pages = 0;
742 async_extent->pages = NULL;
Chris Mason771ed682008-11-06 22:02:51 -0500743}
744
745/*
746 * phase two of compressed writeback. This is the ordered portion
747 * of the code, which only gets called in the order the work was
748 * queued. We walk all the async extents created by compress_file_range
749 * and send them down to the disk.
750 */
Nikolay Borisovb5326272019-03-12 17:20:25 +0200751static noinline void submit_compressed_extents(struct async_chunk *async_chunk)
Chris Mason771ed682008-11-06 22:02:51 -0500752{
Nikolay Borisovb5326272019-03-12 17:20:25 +0200753 struct inode *inode = async_chunk->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400754 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason771ed682008-11-06 22:02:51 -0500755 struct async_extent *async_extent;
756 u64 alloc_hint = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500757 struct btrfs_key ins;
758 struct extent_map *em;
759 struct btrfs_root *root = BTRFS_I(inode)->root;
Nikolay Borisov43366502019-03-12 17:20:29 +0200760 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500761 int ret = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500762
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500763again:
Nikolay Borisovb5326272019-03-12 17:20:25 +0200764 while (!list_empty(&async_chunk->extents)) {
765 async_extent = list_entry(async_chunk->extents.next,
Chris Mason771ed682008-11-06 22:02:51 -0500766 struct async_extent, list);
767 list_del(&async_extent->list);
768
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500769retry:
Nikolay Borisov74475552019-03-12 17:20:30 +0200770 lock_extent(io_tree, async_extent->start,
771 async_extent->start + async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500772 /* did the compression code fall back to uncompressed IO? */
773 if (!async_extent->pages) {
774 int page_started = 0;
775 unsigned long nr_written = 0;
776
Chris Mason771ed682008-11-06 22:02:51 -0500777 /* allocate blocks */
Nikolay Borisovb5326272019-03-12 17:20:25 +0200778 ret = cow_file_range(inode, async_chunk->locked_page,
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500779 async_extent->start,
780 async_extent->start +
781 async_extent->ram_size - 1,
Nikolay Borisov330a5822019-07-17 16:18:17 +0300782 &page_started, &nr_written, 0);
Chris Mason771ed682008-11-06 22:02:51 -0500783
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100784 /* JDM XXX */
785
Chris Mason771ed682008-11-06 22:02:51 -0500786 /*
787 * if page_started, cow_file_range inserted an
788 * inline extent and took care of all the unlocking
789 * and IO for us. Otherwise, we need to submit
790 * all those pages down to the drive.
791 */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500792 if (!page_started && !ret)
Nikolay Borisov5e3ee232017-12-08 15:55:58 +0200793 extent_write_locked_range(inode,
794 async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500795 async_extent->start +
Chris Mason771ed682008-11-06 22:02:51 -0500796 async_extent->ram_size - 1,
Chris Mason771ed682008-11-06 22:02:51 -0500797 WB_SYNC_ALL);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500798 else if (ret)
Nikolay Borisovb5326272019-03-12 17:20:25 +0200799 unlock_page(async_chunk->locked_page);
Chris Mason771ed682008-11-06 22:02:51 -0500800 kfree(async_extent);
801 cond_resched();
802 continue;
803 }
804
Wang Xiaoguang18513092016-07-25 15:51:40 +0800805 ret = btrfs_reserve_extent(root, async_extent->ram_size,
Chris Mason771ed682008-11-06 22:02:51 -0500806 async_extent->compressed_size,
807 async_extent->compressed_size,
Miao Xiee570fd22014-06-19 10:42:50 +0800808 0, alloc_hint, &ins, 1, 1);
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500809 if (ret) {
Filipe Manana40ae8372014-10-06 22:14:24 +0100810 free_async_extent_pages(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500811
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400812 if (ret == -ENOSPC) {
813 unlock_extent(io_tree, async_extent->start,
814 async_extent->start +
815 async_extent->ram_size - 1);
Liu Boce620032014-07-24 22:48:05 +0800816
817 /*
818 * we need to redirty the pages if we decide to
819 * fallback to uncompressed IO, otherwise we
820 * will not submit these pages down to lower
821 * layers.
822 */
823 extent_range_redirty_for_io(inode,
824 async_extent->start,
825 async_extent->start +
826 async_extent->ram_size - 1);
827
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100828 goto retry;
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400829 }
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500830 goto out_free;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500831 }
Yan, Zhengc2167752009-11-12 09:34:21 +0000832 /*
833 * here we're doing allocation and writeback of the
834 * compressed pages
835 */
Liu Bo6f9994d2017-01-31 07:50:22 -0800836 em = create_io_em(inode, async_extent->start,
837 async_extent->ram_size, /* len */
838 async_extent->start, /* orig_start */
839 ins.objectid, /* block_start */
840 ins.offset, /* block_len */
841 ins.offset, /* orig_block_len */
842 async_extent->ram_size, /* ram_bytes */
843 async_extent->compress_type,
844 BTRFS_ORDERED_COMPRESSED);
845 if (IS_ERR(em))
846 /* ret value is not necessary due to void function */
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500847 goto out_free_reserve;
Liu Bo6f9994d2017-01-31 07:50:22 -0800848 free_extent_map(em);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500849
Li Zefan261507a02010-12-17 14:21:50 +0800850 ret = btrfs_add_ordered_extent_compress(inode,
851 async_extent->start,
852 ins.objectid,
853 async_extent->ram_size,
854 ins.offset,
855 BTRFS_ORDERED_COMPRESSED,
856 async_extent->compress_type);
Filipe Mananad9f85962014-08-25 10:43:00 +0100857 if (ret) {
Nikolay Borisovdcdbc052017-02-20 13:50:45 +0200858 btrfs_drop_extent_cache(BTRFS_I(inode),
859 async_extent->start,
Filipe Mananad9f85962014-08-25 10:43:00 +0100860 async_extent->start +
861 async_extent->ram_size - 1, 0);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500862 goto out_free_reserve;
Filipe Mananad9f85962014-08-25 10:43:00 +0100863 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400864 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Chris Mason771ed682008-11-06 22:02:51 -0500865
Chris Mason771ed682008-11-06 22:02:51 -0500866 /*
867 * clear dirty, set writeback and unlock the pages.
868 */
Josef Bacikc2790a22013-07-29 11:20:47 -0400869 extent_clear_unlock_delalloc(inode, async_extent->start,
Chris Masona791e352009-10-08 11:27:10 -0400870 async_extent->start +
871 async_extent->ram_size - 1,
Josef Bacik151a41b2013-07-29 13:22:24 -0400872 NULL, EXTENT_LOCKED | EXTENT_DELALLOC,
873 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -0400874 PAGE_SET_WRITEBACK);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200875 if (btrfs_submit_compressed_write(inode,
Chris Masond3977122009-01-05 21:25:51 -0500876 async_extent->start,
877 async_extent->ram_size,
878 ins.objectid,
879 ins.offset, async_extent->pages,
Liu Bof82b7352017-10-23 23:18:16 -0600880 async_extent->nr_pages,
Nikolay Borisovb5326272019-03-12 17:20:25 +0200881 async_chunk->write_flags)) {
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100882 struct page *p = async_extent->pages[0];
883 const u64 start = async_extent->start;
884 const u64 end = start + async_extent->ram_size - 1;
885
886 p->mapping = inode->i_mapping;
Nikolay Borisovc6297322018-11-08 10:18:08 +0200887 btrfs_writepage_endio_finish_ordered(p, start, end, 0);
Nikolay Borisov7087a9d2018-11-01 14:09:48 +0200888
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100889 p->mapping = NULL;
Nikolay Borisov74e91942019-07-17 16:18:16 +0300890 extent_clear_unlock_delalloc(inode, start, end,
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800891 NULL, 0,
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100892 PAGE_END_WRITEBACK |
893 PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +0100894 free_async_extent_pages(async_extent);
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100895 }
Chris Mason771ed682008-11-06 22:02:51 -0500896 alloc_hint = ins.objectid + ins.offset;
897 kfree(async_extent);
898 cond_resched();
899 }
Filipe Mananadec8f172014-10-06 22:14:26 +0100900 return;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500901out_free_reserve:
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400902 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400903 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100904out_free:
Josef Bacikc2790a22013-07-29 11:20:47 -0400905 extent_clear_unlock_delalloc(inode, async_extent->start,
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500906 async_extent->start +
907 async_extent->ram_size - 1,
Josef Bacikc2790a22013-07-29 11:20:47 -0400908 NULL, EXTENT_LOCKED | EXTENT_DELALLOC |
Filipe Mananaa7e3b972017-04-03 10:45:46 +0100909 EXTENT_DELALLOC_NEW |
Josef Bacik151a41b2013-07-29 13:22:24 -0400910 EXTENT_DEFRAG | EXTENT_DO_ACCOUNTING,
911 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Filipe Manana704de492014-10-06 22:14:22 +0100912 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK |
913 PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +0100914 free_async_extent_pages(async_extent);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100915 kfree(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500916 goto again;
Chris Mason771ed682008-11-06 22:02:51 -0500917}
918
Josef Bacik4b46fce2010-05-23 11:00:55 -0400919static u64 get_extent_allocation_hint(struct inode *inode, u64 start,
920 u64 num_bytes)
921{
922 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
923 struct extent_map *em;
924 u64 alloc_hint = 0;
925
926 read_lock(&em_tree->lock);
927 em = search_extent_mapping(em_tree, start, num_bytes);
928 if (em) {
929 /*
930 * if block start isn't an actual block number then find the
931 * first block in this inode and use that as a hint. If that
932 * block is also bogus then just don't worry about it.
933 */
934 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
935 free_extent_map(em);
936 em = search_extent_mapping(em_tree, 0, 0);
937 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
938 alloc_hint = em->block_start;
939 if (em)
940 free_extent_map(em);
941 } else {
942 alloc_hint = em->block_start;
943 free_extent_map(em);
944 }
945 }
946 read_unlock(&em_tree->lock);
947
948 return alloc_hint;
949}
950
Chris Mason771ed682008-11-06 22:02:51 -0500951/*
952 * when extent_io.c finds a delayed allocation range in the file,
953 * the call backs end up in this code. The basic idea is to
954 * allocate extents on disk for the range, and create ordered data structs
955 * in ram to track those extents.
956 *
957 * locked_page is the page that writepage had locked already. We use
958 * it to make sure we don't do extra locks or unlocks.
959 *
960 * *page_started is set to one if we unlock locked_page and do everything
961 * required to start IO on it. It may be clean and already done with
962 * IO when we return.
963 */
Josef Bacik00361582013-08-14 14:02:47 -0400964static noinline int cow_file_range(struct inode *inode,
965 struct page *locked_page,
Nikolay Borisov74e91942019-07-17 16:18:16 +0300966 u64 start, u64 end, int *page_started,
Nikolay Borisov330a5822019-07-17 16:18:17 +0300967 unsigned long *nr_written, int unlock)
Chris Mason771ed682008-11-06 22:02:51 -0500968{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400969 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik00361582013-08-14 14:02:47 -0400970 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason771ed682008-11-06 22:02:51 -0500971 u64 alloc_hint = 0;
972 u64 num_bytes;
973 unsigned long ram_size;
Filipe Mananaa315e682017-03-06 23:04:20 +0000974 u64 cur_alloc_size = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400975 u64 blocksize = fs_info->sectorsize;
Chris Mason771ed682008-11-06 22:02:51 -0500976 struct btrfs_key ins;
977 struct extent_map *em;
Filipe Mananaa315e682017-03-06 23:04:20 +0000978 unsigned clear_bits;
979 unsigned long page_ops;
980 bool extent_reserved = false;
Chris Mason771ed682008-11-06 22:02:51 -0500981 int ret = 0;
982
Nikolay Borisov70ddc552017-02-20 13:50:35 +0200983 if (btrfs_is_free_space_inode(BTRFS_I(inode))) {
Josef Bacik02ecd2c2013-10-25 16:19:08 -0400984 WARN_ON_ONCE(1);
Josef Bacik29bce2f2014-02-07 12:21:23 -0500985 ret = -EINVAL;
986 goto out_unlock;
Josef Bacik02ecd2c2013-10-25 16:19:08 -0400987 }
Chris Mason771ed682008-11-06 22:02:51 -0500988
Qu Wenruofda28322013-02-26 08:10:22 +0000989 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Mason771ed682008-11-06 22:02:51 -0500990 num_bytes = max(blocksize, num_bytes);
Anand Jain566b1762018-02-15 18:07:59 +0800991 ASSERT(num_bytes <= btrfs_super_total_bytes(fs_info->super_copy));
Chris Mason771ed682008-11-06 22:02:51 -0500992
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200993 inode_should_defrag(BTRFS_I(inode), start, end, num_bytes, SZ_64K);
Chris Mason4cb53002011-05-24 15:35:30 -0400994
Chris Mason771ed682008-11-06 22:02:51 -0500995 if (start == 0) {
996 /* lets try to make an inline extent */
Nikolay Borisovd02c0e22018-03-02 09:43:15 +0200997 ret = cow_file_range_inline(inode, start, end, 0,
998 BTRFS_COMPRESS_NONE, NULL);
Chris Mason771ed682008-11-06 22:02:51 -0500999 if (ret == 0) {
Josef Bacik8b62f872017-10-19 14:15:55 -04001000 /*
1001 * We use DO_ACCOUNTING here because we need the
1002 * delalloc_release_metadata to be run _after_ we drop
1003 * our outstanding extent for clearing delalloc for this
1004 * range.
1005 */
Nikolay Borisov74e91942019-07-17 16:18:16 +03001006 extent_clear_unlock_delalloc(inode, start, end, NULL,
Josef Bacikc2790a22013-07-29 11:20:47 -04001007 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik8b62f872017-10-19 14:15:55 -04001008 EXTENT_DELALLOC_NEW | EXTENT_DEFRAG |
1009 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001010 PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
1011 PAGE_END_WRITEBACK);
Chris Mason771ed682008-11-06 22:02:51 -05001012 *nr_written = *nr_written +
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001013 (end - start + PAGE_SIZE) / PAGE_SIZE;
Chris Mason771ed682008-11-06 22:02:51 -05001014 *page_started = 1;
Chris Mason771ed682008-11-06 22:02:51 -05001015 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001016 } else if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001017 goto out_unlock;
Chris Mason771ed682008-11-06 22:02:51 -05001018 }
1019 }
Chris Masonc8b97812008-10-29 14:49:59 -04001020
Josef Bacik4b46fce2010-05-23 11:00:55 -04001021 alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02001022 btrfs_drop_extent_cache(BTRFS_I(inode), start,
1023 start + num_bytes - 1, 0);
Chris Mason3b951512008-04-17 11:29:12 -04001024
Anand Jain3752d222018-02-15 12:29:38 +08001025 while (num_bytes > 0) {
1026 cur_alloc_size = num_bytes;
Wang Xiaoguang18513092016-07-25 15:51:40 +08001027 ret = btrfs_reserve_extent(root, cur_alloc_size, cur_alloc_size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001028 fs_info->sectorsize, 0, alloc_hint,
Miao Xiee570fd22014-06-19 10:42:50 +08001029 &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04001030 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001031 goto out_unlock;
Filipe Mananaa315e682017-03-06 23:04:20 +00001032 cur_alloc_size = ins.offset;
1033 extent_reserved = true;
Chris Masond3977122009-01-05 21:25:51 -05001034
Chris Mason771ed682008-11-06 22:02:51 -05001035 ram_size = ins.offset;
Liu Bo6f9994d2017-01-31 07:50:22 -08001036 em = create_io_em(inode, start, ins.offset, /* len */
1037 start, /* orig_start */
1038 ins.objectid, /* block_start */
1039 ins.offset, /* block_len */
1040 ins.offset, /* orig_block_len */
1041 ram_size, /* ram_bytes */
1042 BTRFS_COMPRESS_NONE, /* compress_type */
Liu Bo1af4a0a2017-02-13 15:35:09 -08001043 BTRFS_ORDERED_REGULAR /* type */);
Su Yue090a127a2018-05-30 16:48:56 +08001044 if (IS_ERR(em)) {
1045 ret = PTR_ERR(em);
Liu Boace68ba2013-04-22 10:53:47 +00001046 goto out_reserve;
Su Yue090a127a2018-05-30 16:48:56 +08001047 }
Liu Bo6f9994d2017-01-31 07:50:22 -08001048 free_extent_map(em);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001049
Chris Masone6dcd2d2008-07-17 12:53:50 -04001050 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
Chris Mason771ed682008-11-06 22:02:51 -05001051 ram_size, cur_alloc_size, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001052 if (ret)
Filipe Mananad9f85962014-08-25 10:43:00 +01001053 goto out_drop_extent_cache;
Chris Masonc8b97812008-10-29 14:49:59 -04001054
Yan Zheng17d217f2008-12-12 10:03:38 -05001055 if (root->root_key.objectid ==
1056 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1057 ret = btrfs_reloc_clone_csums(inode, start,
1058 cur_alloc_size);
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001059 /*
1060 * Only drop cache here, and process as normal.
1061 *
1062 * We must not allow extent_clear_unlock_delalloc()
1063 * at out_unlock label to free meta of this ordered
1064 * extent, as its meta should be freed by
1065 * btrfs_finish_ordered_io().
1066 *
1067 * So we must continue until @start is increased to
1068 * skip current ordered extent.
1069 */
Josef Bacik00361582013-08-14 14:02:47 -04001070 if (ret)
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001071 btrfs_drop_extent_cache(BTRFS_I(inode), start,
1072 start + ram_size - 1, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001073 }
1074
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001075 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana9cfa3e32016-04-26 15:39:32 +01001076
Chris Masonc8b97812008-10-29 14:49:59 -04001077 /* we're not doing compressed IO, don't unlock the first
1078 * page (which the caller expects to stay locked), don't
1079 * clear any dirty bits and don't set any writeback bits
Chris Mason8b62b722009-09-02 16:53:46 -04001080 *
1081 * Do set the Private2 bit so we know this page was properly
1082 * setup for writepage
Chris Masonc8b97812008-10-29 14:49:59 -04001083 */
Filipe Mananaa315e682017-03-06 23:04:20 +00001084 page_ops = unlock ? PAGE_UNLOCK : 0;
1085 page_ops |= PAGE_SET_PRIVATE2;
Chris Masona791e352009-10-08 11:27:10 -04001086
Josef Bacikc2790a22013-07-29 11:20:47 -04001087 extent_clear_unlock_delalloc(inode, start,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001088 start + ram_size - 1,
Nikolay Borisov74e91942019-07-17 16:18:16 +03001089 locked_page,
Josef Bacikc2790a22013-07-29 11:20:47 -04001090 EXTENT_LOCKED | EXTENT_DELALLOC,
Filipe Mananaa315e682017-03-06 23:04:20 +00001091 page_ops);
Anand Jain3752d222018-02-15 12:29:38 +08001092 if (num_bytes < cur_alloc_size)
1093 num_bytes = 0;
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001094 else
Anand Jain3752d222018-02-15 12:29:38 +08001095 num_bytes -= cur_alloc_size;
Chris Masonc59f8952007-12-17 20:14:04 -05001096 alloc_hint = ins.objectid + ins.offset;
1097 start += cur_alloc_size;
Filipe Mananaa315e682017-03-06 23:04:20 +00001098 extent_reserved = false;
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001099
1100 /*
1101 * btrfs_reloc_clone_csums() error, since start is increased
1102 * extent_clear_unlock_delalloc() at out_unlock label won't
1103 * free metadata of current ordered extent, we're OK to exit.
1104 */
1105 if (ret)
1106 goto out_unlock;
Chris Masonb888db22007-08-27 16:49:44 -04001107 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001108out:
Chris Masonbe20aa92007-12-17 20:14:01 -05001109 return ret;
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001110
Filipe Mananad9f85962014-08-25 10:43:00 +01001111out_drop_extent_cache:
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02001112 btrfs_drop_extent_cache(BTRFS_I(inode), start, start + ram_size - 1, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001113out_reserve:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001114 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001115 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001116out_unlock:
Filipe Mananaa7e3b972017-04-03 10:45:46 +01001117 clear_bits = EXTENT_LOCKED | EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
1118 EXTENT_DEFRAG | EXTENT_CLEAR_META_RESV;
Filipe Mananaa315e682017-03-06 23:04:20 +00001119 page_ops = PAGE_UNLOCK | PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
1120 PAGE_END_WRITEBACK;
1121 /*
1122 * If we reserved an extent for our delalloc range (or a subrange) and
1123 * failed to create the respective ordered extent, then it means that
1124 * when we reserved the extent we decremented the extent's size from
1125 * the data space_info's bytes_may_use counter and incremented the
1126 * space_info's bytes_reserved counter by the same amount. We must make
1127 * sure extent_clear_unlock_delalloc() does not try to decrement again
1128 * the data space_info's bytes_may_use counter, therefore we do not pass
1129 * it the flag EXTENT_CLEAR_DATA_RESV.
1130 */
1131 if (extent_reserved) {
1132 extent_clear_unlock_delalloc(inode, start,
1133 start + cur_alloc_size,
Filipe Mananaa315e682017-03-06 23:04:20 +00001134 locked_page,
1135 clear_bits,
1136 page_ops);
1137 start += cur_alloc_size;
1138 if (start >= end)
1139 goto out;
1140 }
Nikolay Borisov74e91942019-07-17 16:18:16 +03001141 extent_clear_unlock_delalloc(inode, start, end, locked_page,
Filipe Mananaa315e682017-03-06 23:04:20 +00001142 clear_bits | EXTENT_CLEAR_DATA_RESV,
1143 page_ops);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001144 goto out;
Chris Mason771ed682008-11-06 22:02:51 -05001145}
Chris Masonc8b97812008-10-29 14:49:59 -04001146
Chris Mason771ed682008-11-06 22:02:51 -05001147/*
1148 * work queue call back to started compression on a file and pages
1149 */
1150static noinline void async_cow_start(struct btrfs_work *work)
1151{
Nikolay Borisovb5326272019-03-12 17:20:25 +02001152 struct async_chunk *async_chunk;
Nikolay Borisovac3e9932019-07-17 14:41:44 +03001153 int compressed_extents;
Chris Mason771ed682008-11-06 22:02:51 -05001154
Nikolay Borisovb5326272019-03-12 17:20:25 +02001155 async_chunk = container_of(work, struct async_chunk, work);
Chris Mason771ed682008-11-06 22:02:51 -05001156
Nikolay Borisovac3e9932019-07-17 14:41:44 +03001157 compressed_extents = compress_file_range(async_chunk);
1158 if (compressed_extents == 0) {
Nikolay Borisovb5326272019-03-12 17:20:25 +02001159 btrfs_add_delayed_iput(async_chunk->inode);
1160 async_chunk->inode = NULL;
Josef Bacik8180ef82012-06-08 15:16:12 -04001161 }
Chris Mason771ed682008-11-06 22:02:51 -05001162}
1163
1164/*
1165 * work queue call back to submit previously compressed pages
1166 */
1167static noinline void async_cow_submit(struct btrfs_work *work)
1168{
Nikolay Borisovc5a68ae2019-03-12 17:20:26 +02001169 struct async_chunk *async_chunk = container_of(work, struct async_chunk,
1170 work);
1171 struct btrfs_fs_info *fs_info = btrfs_work_owner(work);
Chris Mason771ed682008-11-06 22:02:51 -05001172 unsigned long nr_pages;
1173
Nikolay Borisovb5326272019-03-12 17:20:25 +02001174 nr_pages = (async_chunk->end - async_chunk->start + PAGE_SIZE) >>
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001175 PAGE_SHIFT;
Chris Mason771ed682008-11-06 22:02:51 -05001176
David Sterba093258e2018-02-26 16:15:17 +01001177 /* atomic_sub_return implies a barrier */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001178 if (atomic_sub_return(nr_pages, &fs_info->async_delalloc_pages) <
David Sterba093258e2018-02-26 16:15:17 +01001179 5 * SZ_1M)
1180 cond_wake_up_nomb(&fs_info->async_submit_wait);
Chris Mason771ed682008-11-06 22:02:51 -05001181
Nikolay Borisov4546d172019-01-03 10:50:03 +02001182 /*
Nikolay Borisovb5326272019-03-12 17:20:25 +02001183 * ->inode could be NULL if async_chunk_start has failed to compress,
Nikolay Borisov4546d172019-01-03 10:50:03 +02001184 * in which case we don't have anything to submit, yet we need to
1185 * always adjust ->async_delalloc_pages as its paired with the init
1186 * happening in cow_file_range_async
1187 */
Nikolay Borisovb5326272019-03-12 17:20:25 +02001188 if (async_chunk->inode)
1189 submit_compressed_extents(async_chunk);
Chris Mason771ed682008-11-06 22:02:51 -05001190}
Chris Masonc8b97812008-10-29 14:49:59 -04001191
Chris Mason771ed682008-11-06 22:02:51 -05001192static noinline void async_cow_free(struct btrfs_work *work)
1193{
Nikolay Borisovb5326272019-03-12 17:20:25 +02001194 struct async_chunk *async_chunk;
Nikolay Borisov97db1202019-03-12 17:20:24 +02001195
Nikolay Borisovb5326272019-03-12 17:20:25 +02001196 async_chunk = container_of(work, struct async_chunk, work);
1197 if (async_chunk->inode)
1198 btrfs_add_delayed_iput(async_chunk->inode);
Nikolay Borisov97db1202019-03-12 17:20:24 +02001199 /*
1200 * Since the pointer to 'pending' is at the beginning of the array of
Nikolay Borisovb5326272019-03-12 17:20:25 +02001201 * async_chunk's, freeing it ensures the whole array has been freed.
Nikolay Borisov97db1202019-03-12 17:20:24 +02001202 */
Nikolay Borisovb5326272019-03-12 17:20:25 +02001203 if (atomic_dec_and_test(async_chunk->pending))
Nikolay Borisovb1c16ac2019-04-01 11:29:57 +03001204 kvfree(async_chunk->pending);
Chris Mason771ed682008-11-06 22:02:51 -05001205}
1206
1207static int cow_file_range_async(struct inode *inode, struct page *locked_page,
1208 u64 start, u64 end, int *page_started,
Liu Bof82b7352017-10-23 23:18:16 -06001209 unsigned long *nr_written,
1210 unsigned int write_flags)
Chris Mason771ed682008-11-06 22:02:51 -05001211{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001212 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Nikolay Borisov97db1202019-03-12 17:20:24 +02001213 struct async_cow *ctx;
1214 struct async_chunk *async_chunk;
Chris Mason771ed682008-11-06 22:02:51 -05001215 unsigned long nr_pages;
1216 u64 cur_end;
Nikolay Borisov97db1202019-03-12 17:20:24 +02001217 u64 num_chunks = DIV_ROUND_UP(end - start, SZ_512K);
1218 int i;
1219 bool should_compress;
Nikolay Borisovb1c16ac2019-04-01 11:29:57 +03001220 unsigned nofs_flag;
Chris Mason771ed682008-11-06 22:02:51 -05001221
Nikolay Borisov69684c52019-03-12 17:20:28 +02001222 unlock_extent(&BTRFS_I(inode)->io_tree, start, end);
Nikolay Borisov97db1202019-03-12 17:20:24 +02001223
1224 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS &&
1225 !btrfs_test_opt(fs_info, FORCE_COMPRESS)) {
1226 num_chunks = 1;
1227 should_compress = false;
1228 } else {
1229 should_compress = true;
1230 }
1231
Nikolay Borisovb1c16ac2019-04-01 11:29:57 +03001232 nofs_flag = memalloc_nofs_save();
1233 ctx = kvmalloc(struct_size(ctx, chunks, num_chunks), GFP_KERNEL);
1234 memalloc_nofs_restore(nofs_flag);
1235
Nikolay Borisov97db1202019-03-12 17:20:24 +02001236 if (!ctx) {
1237 unsigned clear_bits = EXTENT_LOCKED | EXTENT_DELALLOC |
1238 EXTENT_DELALLOC_NEW | EXTENT_DEFRAG |
1239 EXTENT_DO_ACCOUNTING;
1240 unsigned long page_ops = PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
1241 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK |
1242 PAGE_SET_ERROR;
1243
Nikolay Borisov74e91942019-07-17 16:18:16 +03001244 extent_clear_unlock_delalloc(inode, start, end, locked_page,
Nikolay Borisov97db1202019-03-12 17:20:24 +02001245 clear_bits, page_ops);
1246 return -ENOMEM;
1247 }
1248
1249 async_chunk = ctx->chunks;
1250 atomic_set(&ctx->num_chunks, num_chunks);
1251
1252 for (i = 0; i < num_chunks; i++) {
1253 if (should_compress)
1254 cur_end = min(end, start + SZ_512K - 1);
1255 else
1256 cur_end = end;
1257
Nikolay Borisovbd4691a2019-01-03 10:50:01 +02001258 /*
1259 * igrab is called higher up in the call chain, take only the
1260 * lightweight reference for the callback lifetime
1261 */
1262 ihold(inode);
Nikolay Borisov97db1202019-03-12 17:20:24 +02001263 async_chunk[i].pending = &ctx->num_chunks;
1264 async_chunk[i].inode = inode;
1265 async_chunk[i].start = start;
1266 async_chunk[i].end = cur_end;
Nikolay Borisov97db1202019-03-12 17:20:24 +02001267 async_chunk[i].locked_page = locked_page;
1268 async_chunk[i].write_flags = write_flags;
1269 INIT_LIST_HEAD(&async_chunk[i].extents);
Chris Mason771ed682008-11-06 22:02:51 -05001270
Omar Sandovala0cac0e2019-09-16 11:30:57 -07001271 btrfs_init_work(&async_chunk[i].work, async_cow_start,
1272 async_cow_submit, async_cow_free);
Chris Mason771ed682008-11-06 22:02:51 -05001273
Nikolay Borisov97db1202019-03-12 17:20:24 +02001274 nr_pages = DIV_ROUND_UP(cur_end - start, PAGE_SIZE);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001275 atomic_add(nr_pages, &fs_info->async_delalloc_pages);
Chris Mason771ed682008-11-06 22:02:51 -05001276
Nikolay Borisov97db1202019-03-12 17:20:24 +02001277 btrfs_queue_work(fs_info->delalloc_workers, &async_chunk[i].work);
Chris Mason771ed682008-11-06 22:02:51 -05001278
Chris Mason771ed682008-11-06 22:02:51 -05001279 *nr_written += nr_pages;
1280 start = cur_end + 1;
1281 }
1282 *page_started = 1;
1283 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001284}
1285
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001286static noinline int csum_exist_in_range(struct btrfs_fs_info *fs_info,
Yan Zheng17d217f2008-12-12 10:03:38 -05001287 u64 bytenr, u64 num_bytes)
1288{
1289 int ret;
1290 struct btrfs_ordered_sum *sums;
1291 LIST_HEAD(list);
1292
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001293 ret = btrfs_lookup_csums_range(fs_info->csum_root, bytenr,
Arne Jansena2de7332011-03-08 14:14:00 +01001294 bytenr + num_bytes - 1, &list, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001295 if (ret == 0 && list_empty(&list))
1296 return 0;
1297
1298 while (!list_empty(&list)) {
1299 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1300 list_del(&sums->list);
1301 kfree(sums);
1302 }
Liu Bo58113752018-01-31 17:09:13 -07001303 if (ret < 0)
1304 return ret;
Yan Zheng17d217f2008-12-12 10:03:38 -05001305 return 1;
1306}
1307
Chris Masond352ac62008-09-29 15:18:18 -04001308/*
1309 * when nowcow writeback call back. This checks for snapshots or COW copies
1310 * of the extents that exist in the file, and COWs the file as required.
1311 *
1312 * If no cow copies or snapshots exist, we write directly to the existing
1313 * blocks on disk
1314 */
Chris Mason7f366cf2009-03-12 20:12:45 -04001315static noinline int run_delalloc_nocow(struct inode *inode,
1316 struct page *locked_page,
Nikolay Borisov3e024842019-08-21 10:42:03 +03001317 const u64 start, const u64 end,
1318 int *page_started, int force,
1319 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001320{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001321 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masonbe20aa92007-12-17 20:14:01 -05001322 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masonbe20aa92007-12-17 20:14:01 -05001323 struct btrfs_path *path;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001324 u64 cow_start = (u64)-1;
1325 u64 cur_offset = start;
Robbie Ko8ecebf4d2018-08-06 10:30:30 +08001326 int ret;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001327 bool check_prev = true;
1328 const bool freespace_inode = btrfs_is_free_space_inode(BTRFS_I(inode));
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02001329 u64 ino = btrfs_ino(BTRFS_I(inode));
Nikolay Borisov762bf092019-08-22 17:24:20 +03001330 bool nocow = false;
1331 u64 disk_bytenr = 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001332
1333 path = btrfs_alloc_path();
Josef Bacik17ca04a2012-05-31 15:58:55 -04001334 if (!path) {
Nikolay Borisov74e91942019-07-17 16:18:16 +03001335 extent_clear_unlock_delalloc(inode, start, end, locked_page,
Josef Bacikc2790a22013-07-29 11:20:47 -04001336 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -04001337 EXTENT_DO_ACCOUNTING |
1338 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001339 PAGE_CLEAR_DIRTY |
1340 PAGE_SET_WRITEBACK |
1341 PAGE_END_WRITEBACK);
Mark Fashehd8926bb2011-07-13 10:38:47 -07001342 return -ENOMEM;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001343 }
Li Zefan82d59022011-04-20 10:33:24 +08001344
Yan Zheng80ff3852008-10-30 14:20:02 -04001345 while (1) {
Nikolay Borisov3e024842019-08-21 10:42:03 +03001346 struct btrfs_key found_key;
1347 struct btrfs_file_extent_item *fi;
1348 struct extent_buffer *leaf;
1349 u64 extent_end;
1350 u64 extent_offset;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001351 u64 num_bytes = 0;
1352 u64 disk_num_bytes;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001353 u64 ram_bytes;
1354 int extent_type;
Nikolay Borisov762bf092019-08-22 17:24:20 +03001355
1356 nocow = false;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001357
Liu Boe4c3b2d2017-01-30 12:25:28 -08001358 ret = btrfs_lookup_file_extent(NULL, root, path, ino,
Yan Zheng80ff3852008-10-30 14:20:02 -04001359 cur_offset, 0);
Josef Bacikd788a342013-10-25 16:55:08 -04001360 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001361 goto error;
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001362
1363 /*
1364 * If there is no extent for our range when doing the initial
1365 * search, then go back to the previous slot as it will be the
1366 * one containing the search offset
1367 */
Yan Zheng80ff3852008-10-30 14:20:02 -04001368 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1369 leaf = path->nodes[0];
1370 btrfs_item_key_to_cpu(leaf, &found_key,
1371 path->slots[0] - 1);
Li Zefan33345d012011-04-20 10:31:50 +08001372 if (found_key.objectid == ino &&
Yan Zheng80ff3852008-10-30 14:20:02 -04001373 found_key.type == BTRFS_EXTENT_DATA_KEY)
1374 path->slots[0]--;
1375 }
Nikolay Borisov3e024842019-08-21 10:42:03 +03001376 check_prev = false;
Yan Zheng80ff3852008-10-30 14:20:02 -04001377next_slot:
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001378 /* Go to next leaf if we have exhausted the current one */
Yan Zheng80ff3852008-10-30 14:20:02 -04001379 leaf = path->nodes[0];
1380 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1381 ret = btrfs_next_leaf(root, path);
Liu Boe8916692018-01-25 11:02:50 -07001382 if (ret < 0) {
1383 if (cow_start != (u64)-1)
1384 cur_offset = cow_start;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001385 goto error;
Liu Boe8916692018-01-25 11:02:50 -07001386 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001387 if (ret > 0)
1388 break;
1389 leaf = path->nodes[0];
1390 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001391
Yan Zheng80ff3852008-10-30 14:20:02 -04001392 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001393
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001394 /* Didn't find anything for our INO */
Filipe Manana1d512cb2015-11-09 00:33:58 +00001395 if (found_key.objectid > ino)
1396 break;
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001397 /*
1398 * Keep searching until we find an EXTENT_ITEM or there are no
1399 * more extents for this inode
1400 */
Filipe Manana1d512cb2015-11-09 00:33:58 +00001401 if (WARN_ON_ONCE(found_key.objectid < ino) ||
1402 found_key.type < BTRFS_EXTENT_DATA_KEY) {
1403 path->slots[0]++;
1404 goto next_slot;
1405 }
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001406
1407 /* Found key is not EXTENT_DATA_KEY or starts after req range */
Filipe Manana1d512cb2015-11-09 00:33:58 +00001408 if (found_key.type > BTRFS_EXTENT_DATA_KEY ||
Yan Zheng80ff3852008-10-30 14:20:02 -04001409 found_key.offset > end)
1410 break;
Chris Masonbe20aa92007-12-17 20:14:01 -05001411
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001412 /*
1413 * If the found extent starts after requested offset, then
1414 * adjust extent_end to be right before this extent begins
1415 */
Yan Zheng80ff3852008-10-30 14:20:02 -04001416 if (found_key.offset > cur_offset) {
1417 extent_end = found_key.offset;
Chris Masone9061e22009-10-09 09:57:45 -04001418 extent_type = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001419 goto out_check;
1420 }
Chris Masonc31f8832008-01-08 15:46:31 -05001421
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001422 /*
1423 * Found extent which begins before our range and potentially
1424 * intersect it
1425 */
Yan Zheng80ff3852008-10-30 14:20:02 -04001426 fi = btrfs_item_ptr(leaf, path->slots[0],
1427 struct btrfs_file_extent_item);
1428 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001429
Josef Bacikcc95bef2013-04-04 14:31:27 -04001430 ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
Yan Zhengd899e052008-10-30 14:25:28 -04001431 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1432 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001433 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001434 extent_offset = btrfs_file_extent_offset(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001435 extent_end = found_key.offset +
1436 btrfs_file_extent_num_bytes(leaf, fi);
Josef Bacikb4939682012-12-03 10:31:19 -05001437 disk_num_bytes =
1438 btrfs_file_extent_disk_num_bytes(leaf, fi);
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001439 /*
1440 * If extent we got ends before our range starts, skip
1441 * to next extent
1442 */
Yan Zheng80ff3852008-10-30 14:20:02 -04001443 if (extent_end <= start) {
1444 path->slots[0]++;
1445 goto next_slot;
1446 }
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001447 /* Skip holes */
Yan Zheng17d217f2008-12-12 10:03:38 -05001448 if (disk_bytenr == 0)
1449 goto out_check;
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001450 /* Skip compressed/encrypted/encoded extents */
Yan Zheng80ff3852008-10-30 14:20:02 -04001451 if (btrfs_file_extent_compression(leaf, fi) ||
1452 btrfs_file_extent_encryption(leaf, fi) ||
1453 btrfs_file_extent_other_encoding(leaf, fi))
1454 goto out_check;
Ethan Lien78d42952018-05-17 14:58:29 +08001455 /*
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001456 * If extent is created before the last volume's snapshot
1457 * this implies the extent is shared, hence we can't do
1458 * nocow. This is the same check as in
1459 * btrfs_cross_ref_exist but without calling
1460 * btrfs_search_slot.
Ethan Lien78d42952018-05-17 14:58:29 +08001461 */
Nikolay Borisov3e024842019-08-21 10:42:03 +03001462 if (!freespace_inode &&
Lu Fengqi27a7ff52018-11-29 17:31:32 +08001463 btrfs_file_extent_generation(leaf, fi) <=
Ethan Lien78d42952018-05-17 14:58:29 +08001464 btrfs_root_last_snapshot(&root->root_item))
1465 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001466 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1467 goto out_check;
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001468 /* If extent is RO, we must COW it */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001469 if (btrfs_extent_readonly(fs_info, disk_bytenr))
Yan Zheng80ff3852008-10-30 14:20:02 -04001470 goto out_check;
Liu Bo58113752018-01-31 17:09:13 -07001471 ret = btrfs_cross_ref_exist(root, ino,
1472 found_key.offset -
1473 extent_offset, disk_bytenr);
1474 if (ret) {
1475 /*
1476 * ret could be -EIO if the above fails to read
1477 * metadata.
1478 */
1479 if (ret < 0) {
1480 if (cow_start != (u64)-1)
1481 cur_offset = cow_start;
1482 goto error;
1483 }
1484
Nikolay Borisov3e024842019-08-21 10:42:03 +03001485 WARN_ON_ONCE(freespace_inode);
Yan Zheng17d217f2008-12-12 10:03:38 -05001486 goto out_check;
Liu Bo58113752018-01-31 17:09:13 -07001487 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001488 disk_bytenr += extent_offset;
Yan Zheng17d217f2008-12-12 10:03:38 -05001489 disk_bytenr += cur_offset - found_key.offset;
1490 num_bytes = min(end + 1, extent_end) - cur_offset;
1491 /*
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001492 * If there are pending snapshots for this root, we
1493 * fall into common COW way
Wang Shilonge9894fd2014-03-27 11:12:25 +08001494 */
Nikolay Borisov3e024842019-08-21 10:42:03 +03001495 if (!freespace_inode && atomic_read(&root->snapshot_force_cow))
Robbie Ko8ecebf4d2018-08-06 10:30:30 +08001496 goto out_check;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001497 /*
Yan Zheng17d217f2008-12-12 10:03:38 -05001498 * force cow if csum exists in the range.
1499 * this ensure that csum for a given extent are
1500 * either valid or do not exist.
1501 */
Liu Bo58113752018-01-31 17:09:13 -07001502 ret = csum_exist_in_range(fs_info, disk_bytenr,
1503 num_bytes);
1504 if (ret) {
Liu Bo58113752018-01-31 17:09:13 -07001505 /*
1506 * ret could be -EIO if the above fails to read
1507 * metadata.
1508 */
1509 if (ret < 0) {
1510 if (cow_start != (u64)-1)
1511 cur_offset = cow_start;
1512 goto error;
1513 }
Nikolay Borisov3e024842019-08-21 10:42:03 +03001514 WARN_ON_ONCE(freespace_inode);
Yan Zheng17d217f2008-12-12 10:03:38 -05001515 goto out_check;
Robbie Ko91e1f562016-10-07 10:01:29 +08001516 }
Robbie Ko8ecebf4d2018-08-06 10:30:30 +08001517 if (!btrfs_inc_nocow_writers(fs_info, disk_bytenr))
Filipe Mananaf78c4362016-05-09 13:15:41 +01001518 goto out_check;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001519 nocow = true;
Yan Zheng80ff3852008-10-30 14:20:02 -04001520 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Nikolay Borisove8e21002019-08-22 17:25:23 +03001521 extent_end = found_key.offset + ram_bytes;
1522 extent_end = ALIGN(extent_end, fs_info->sectorsize);
Nikolay Borisov922f0512019-08-05 17:47:06 +03001523 /* Skip extents outside of our requested range */
1524 if (extent_end <= start) {
1525 path->slots[0]++;
1526 goto next_slot;
1527 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001528 } else {
Nikolay Borisove8e21002019-08-22 17:25:23 +03001529 /* If this triggers then we have a memory corruption */
Arnd Bergmann290342f2019-03-25 14:02:25 +01001530 BUG();
Yan Zheng80ff3852008-10-30 14:20:02 -04001531 }
1532out_check:
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001533 /*
1534 * If nocow is false then record the beginning of the range
1535 * that needs to be COWed
1536 */
Yan Zheng80ff3852008-10-30 14:20:02 -04001537 if (!nocow) {
1538 if (cow_start == (u64)-1)
1539 cow_start = cur_offset;
1540 cur_offset = extent_end;
1541 if (cur_offset > end)
1542 break;
1543 path->slots[0]++;
1544 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001545 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001546
David Sterbab3b4aa72011-04-21 01:20:15 +02001547 btrfs_release_path(path);
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001548
1549 /*
1550 * COW range from cow_start to found_key.offset - 1. As the key
1551 * will contain the beginning of the first extent that can be
1552 * NOCOW, following one which needs to be COW'ed
1553 */
Yan Zheng80ff3852008-10-30 14:20:02 -04001554 if (cow_start != (u64)-1) {
Josef Bacik00361582013-08-14 14:02:47 -04001555 ret = cow_file_range(inode, locked_page,
1556 cow_start, found_key.offset - 1,
Nikolay Borisov330a5822019-07-17 16:18:17 +03001557 page_started, nr_written, 1);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001558 if (ret) {
Filipe Mananaf78c4362016-05-09 13:15:41 +01001559 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001560 btrfs_dec_nocow_writers(fs_info,
Filipe Mananaf78c4362016-05-09 13:15:41 +01001561 disk_bytenr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001562 goto error;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001563 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001564 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001565 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001566
Yan Zhengd899e052008-10-30 14:25:28 -04001567 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Liu Bo6f9994d2017-01-31 07:50:22 -08001568 u64 orig_start = found_key.offset - extent_offset;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001569 struct extent_map *em;
Liu Bo6f9994d2017-01-31 07:50:22 -08001570
1571 em = create_io_em(inode, cur_offset, num_bytes,
1572 orig_start,
1573 disk_bytenr, /* block_start */
1574 num_bytes, /* block_len */
1575 disk_num_bytes, /* orig_block_len */
1576 ram_bytes, BTRFS_COMPRESS_NONE,
1577 BTRFS_ORDERED_PREALLOC);
1578 if (IS_ERR(em)) {
Liu Bo6f9994d2017-01-31 07:50:22 -08001579 if (nocow)
1580 btrfs_dec_nocow_writers(fs_info,
1581 disk_bytenr);
1582 ret = PTR_ERR(em);
1583 goto error;
Yan Zhengd899e052008-10-30 14:25:28 -04001584 }
Liu Bo6f9994d2017-01-31 07:50:22 -08001585 free_extent_map(em);
Nikolay Borisovbb55f622019-08-05 17:47:05 +03001586 ret = btrfs_add_ordered_extent(inode, cur_offset,
1587 disk_bytenr, num_bytes,
1588 num_bytes,
1589 BTRFS_ORDERED_PREALLOC);
Nikolay Borisov762bf092019-08-22 17:24:20 +03001590 if (ret) {
1591 btrfs_drop_extent_cache(BTRFS_I(inode),
1592 cur_offset,
1593 cur_offset + num_bytes - 1,
1594 0);
1595 goto error;
1596 }
Yan Zhengd899e052008-10-30 14:25:28 -04001597 } else {
Nikolay Borisovbb55f622019-08-05 17:47:05 +03001598 ret = btrfs_add_ordered_extent(inode, cur_offset,
1599 disk_bytenr, num_bytes,
1600 num_bytes,
1601 BTRFS_ORDERED_NOCOW);
Nikolay Borisov762bf092019-08-22 17:24:20 +03001602 if (ret)
1603 goto error;
Yan Zhengd899e052008-10-30 14:25:28 -04001604 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001605
Filipe Mananaf78c4362016-05-09 13:15:41 +01001606 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001607 btrfs_dec_nocow_writers(fs_info, disk_bytenr);
Nikolay Borisov762bf092019-08-22 17:24:20 +03001608 nocow = false;
Chris Mason771ed682008-11-06 22:02:51 -05001609
Yan, Zhengefa56462010-05-16 10:49:59 -04001610 if (root->root_key.objectid ==
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001611 BTRFS_DATA_RELOC_TREE_OBJECTID)
1612 /*
1613 * Error handled later, as we must prevent
1614 * extent_clear_unlock_delalloc() in error handler
1615 * from freeing metadata of created ordered extent.
1616 */
Yan, Zhengefa56462010-05-16 10:49:59 -04001617 ret = btrfs_reloc_clone_csums(inode, cur_offset,
1618 num_bytes);
Yan, Zhengefa56462010-05-16 10:49:59 -04001619
Josef Bacikc2790a22013-07-29 11:20:47 -04001620 extent_clear_unlock_delalloc(inode, cur_offset,
Nikolay Borisov74e91942019-07-17 16:18:16 +03001621 cur_offset + num_bytes - 1,
Josef Bacikc2790a22013-07-29 11:20:47 -04001622 locked_page, EXTENT_LOCKED |
Wang Xiaoguang18513092016-07-25 15:51:40 +08001623 EXTENT_DELALLOC |
1624 EXTENT_CLEAR_DATA_RESV,
1625 PAGE_UNLOCK | PAGE_SET_PRIVATE2);
1626
Yan Zheng80ff3852008-10-30 14:20:02 -04001627 cur_offset = extent_end;
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001628
1629 /*
1630 * btrfs_reloc_clone_csums() error, now we're OK to call error
1631 * handler, as metadata for created ordered extent will only
1632 * be freed by btrfs_finish_ordered_io().
1633 */
1634 if (ret)
1635 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001636 if (cur_offset > end)
1637 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001638 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001639 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001640
Robbie Ko506481b2018-10-30 18:04:04 +08001641 if (cur_offset <= end && cow_start == (u64)-1)
Yan Zheng80ff3852008-10-30 14:20:02 -04001642 cow_start = cur_offset;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001643
Yan Zheng80ff3852008-10-30 14:20:02 -04001644 if (cow_start != (u64)-1) {
Robbie Ko506481b2018-10-30 18:04:04 +08001645 cur_offset = end;
Nikolay Borisov74e91942019-07-17 16:18:16 +03001646 ret = cow_file_range(inode, locked_page, cow_start, end,
Nikolay Borisov330a5822019-07-17 16:18:17 +03001647 page_started, nr_written, 1);
Josef Bacikd788a342013-10-25 16:55:08 -04001648 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001649 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001650 }
1651
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001652error:
Nikolay Borisov762bf092019-08-22 17:24:20 +03001653 if (nocow)
1654 btrfs_dec_nocow_writers(fs_info, disk_bytenr);
1655
Josef Bacik17ca04a2012-05-31 15:58:55 -04001656 if (ret && cur_offset < end)
Nikolay Borisov74e91942019-07-17 16:18:16 +03001657 extent_clear_unlock_delalloc(inode, cur_offset, end,
Josef Bacikc2790a22013-07-29 11:20:47 -04001658 locked_page, EXTENT_LOCKED |
Josef Bacik151a41b2013-07-29 13:22:24 -04001659 EXTENT_DELALLOC | EXTENT_DEFRAG |
1660 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
1661 PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -04001662 PAGE_SET_WRITEBACK |
1663 PAGE_END_WRITEBACK);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001664 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001665 return ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001666}
1667
Wang Shilong47059d92014-07-03 18:22:07 +08001668static inline int need_force_cow(struct inode *inode, u64 start, u64 end)
1669{
1670
1671 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
1672 !(BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC))
1673 return 0;
1674
1675 /*
1676 * @defrag_bytes is a hint value, no spinlock held here,
1677 * if is not zero, it means the file is defragging.
1678 * Force cow if given extent needs to be defragged.
1679 */
1680 if (BTRFS_I(inode)->defrag_bytes &&
1681 test_range_bit(&BTRFS_I(inode)->io_tree, start, end,
1682 EXTENT_DEFRAG, 0, NULL))
1683 return 1;
1684
1685 return 0;
1686}
1687
Chris Masond352ac62008-09-29 15:18:18 -04001688/*
Nikolay Borisov5eaad972018-11-01 14:09:46 +02001689 * Function to process delayed allocation (create CoW) for ranges which are
1690 * being touched for the first time.
Chris Masond352ac62008-09-29 15:18:18 -04001691 */
Nikolay Borisovbc9a8bf2018-12-19 09:50:20 +02001692int btrfs_run_delalloc_range(struct inode *inode, struct page *locked_page,
Nikolay Borisov5eaad972018-11-01 14:09:46 +02001693 u64 start, u64 end, int *page_started, unsigned long *nr_written,
1694 struct writeback_control *wbc)
Chris Masonbe20aa92007-12-17 20:14:01 -05001695{
Chris Masonbe20aa92007-12-17 20:14:01 -05001696 int ret;
Wang Shilong47059d92014-07-03 18:22:07 +08001697 int force_cow = need_force_cow(inode, start, end);
Liu Bof82b7352017-10-23 23:18:16 -06001698 unsigned int write_flags = wbc_to_write_flags(wbc);
Chris Masona2135012008-06-25 16:01:30 -04001699
Wang Shilong47059d92014-07-03 18:22:07 +08001700 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW && !force_cow) {
Chris Masonc8b97812008-10-29 14:49:59 -04001701 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001702 page_started, 1, nr_written);
Wang Shilong47059d92014-07-03 18:22:07 +08001703 } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC && !force_cow) {
Yan Zhengd899e052008-10-30 14:25:28 -04001704 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001705 page_started, 0, nr_written);
Qu Wenruo42c16da2019-07-01 05:12:46 +00001706 } else if (!inode_can_compress(inode) ||
1707 !inode_need_compress(inode, start, end)) {
Nikolay Borisov74e91942019-07-17 16:18:16 +03001708 ret = cow_file_range(inode, locked_page, start, end,
Nikolay Borisov330a5822019-07-17 16:18:17 +03001709 page_started, nr_written, 1);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001710 } else {
1711 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1712 &BTRFS_I(inode)->runtime_flags);
Chris Mason771ed682008-11-06 22:02:51 -05001713 ret = cow_file_range_async(inode, locked_page, start, end,
Liu Bof82b7352017-10-23 23:18:16 -06001714 page_started, nr_written,
1715 write_flags);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001716 }
Qu Wenruo524272602017-03-08 10:25:52 +08001717 if (ret)
Nikolay Borisovd1051d62018-11-21 17:10:52 +02001718 btrfs_cleanup_ordered_extents(inode, locked_page, start,
1719 end - start + 1);
Chris Masonb888db22007-08-27 16:49:44 -04001720 return ret;
1721}
1722
Nikolay Borisovabbb55f2018-11-01 14:09:53 +02001723void btrfs_split_delalloc_extent(struct inode *inode,
1724 struct extent_state *orig, u64 split)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001725{
Josef Bacikdcab6a32015-02-11 15:08:59 -05001726 u64 size;
1727
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001728 /* not delalloc, ignore it */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001729 if (!(orig->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001730 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001731
Josef Bacikdcab6a32015-02-11 15:08:59 -05001732 size = orig->end - orig->start + 1;
1733 if (size > BTRFS_MAX_EXTENT_SIZE) {
David Sterba823bb202017-01-04 11:09:51 +01001734 u32 num_extents;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001735 u64 new_size;
1736
1737 /*
Nikolay Borisov5c848192018-11-01 14:09:52 +02001738 * See the explanation in btrfs_merge_delalloc_extent, the same
Josef Bacikba117212015-03-13 15:01:24 -04001739 * applies here, just in reverse.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001740 */
1741 new_size = orig->end - split + 1;
David Sterba823bb202017-01-04 11:09:51 +01001742 num_extents = count_max_extents(new_size);
Josef Bacikba117212015-03-13 15:01:24 -04001743 new_size = split - orig->start;
David Sterba823bb202017-01-04 11:09:51 +01001744 num_extents += count_max_extents(new_size);
1745 if (count_max_extents(size) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001746 return;
1747 }
1748
Josef Bacik9e0baf62011-07-15 15:16:44 +00001749 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04001750 btrfs_mod_outstanding_extents(BTRFS_I(inode), 1);
Josef Bacik9e0baf62011-07-15 15:16:44 +00001751 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001752}
1753
1754/*
Nikolay Borisov5c848192018-11-01 14:09:52 +02001755 * Handle merged delayed allocation extents so we can keep track of new extents
1756 * that are just merged onto old extents, such as when we are doing sequential
1757 * writes, so we can properly account for the metadata space we'll need.
Josef Bacik9ed74f22009-09-11 16:12:44 -04001758 */
Nikolay Borisov5c848192018-11-01 14:09:52 +02001759void btrfs_merge_delalloc_extent(struct inode *inode, struct extent_state *new,
1760 struct extent_state *other)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001761{
Josef Bacikdcab6a32015-02-11 15:08:59 -05001762 u64 new_size, old_size;
David Sterba823bb202017-01-04 11:09:51 +01001763 u32 num_extents;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001764
Josef Bacik9ed74f22009-09-11 16:12:44 -04001765 /* not delalloc, ignore it */
1766 if (!(other->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001767 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001768
Josef Bacik8461a3d2015-03-13 15:12:08 -04001769 if (new->start > other->start)
1770 new_size = new->end - other->start + 1;
1771 else
1772 new_size = other->end - new->start + 1;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001773
1774 /* we're not bigger than the max, unreserve the space and go */
1775 if (new_size <= BTRFS_MAX_EXTENT_SIZE) {
1776 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04001777 btrfs_mod_outstanding_extents(BTRFS_I(inode), -1);
Josef Bacikdcab6a32015-02-11 15:08:59 -05001778 spin_unlock(&BTRFS_I(inode)->lock);
1779 return;
1780 }
1781
1782 /*
Josef Bacikba117212015-03-13 15:01:24 -04001783 * We have to add up either side to figure out how many extents were
1784 * accounted for before we merged into one big extent. If the number of
1785 * extents we accounted for is <= the amount we need for the new range
1786 * then we can return, otherwise drop. Think of it like this
1787 *
1788 * [ 4k][MAX_SIZE]
1789 *
1790 * So we've grown the extent by a MAX_SIZE extent, this would mean we
1791 * need 2 outstanding extents, on one side we have 1 and the other side
1792 * we have 1 so they are == and we can return. But in this case
1793 *
1794 * [MAX_SIZE+4k][MAX_SIZE+4k]
1795 *
1796 * Each range on their own accounts for 2 extents, but merged together
1797 * they are only 3 extents worth of accounting, so we need to drop in
1798 * this case.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001799 */
Josef Bacikba117212015-03-13 15:01:24 -04001800 old_size = other->end - other->start + 1;
David Sterba823bb202017-01-04 11:09:51 +01001801 num_extents = count_max_extents(old_size);
Josef Bacikba117212015-03-13 15:01:24 -04001802 old_size = new->end - new->start + 1;
David Sterba823bb202017-01-04 11:09:51 +01001803 num_extents += count_max_extents(old_size);
1804 if (count_max_extents(new_size) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001805 return;
1806
Josef Bacik9e0baf62011-07-15 15:16:44 +00001807 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04001808 btrfs_mod_outstanding_extents(BTRFS_I(inode), -1);
Josef Bacik9e0baf62011-07-15 15:16:44 +00001809 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001810}
1811
Miao Xieeb73c1b2013-05-15 07:48:22 +00001812static void btrfs_add_delalloc_inodes(struct btrfs_root *root,
1813 struct inode *inode)
1814{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001815 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1816
Miao Xieeb73c1b2013-05-15 07:48:22 +00001817 spin_lock(&root->delalloc_lock);
1818 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1819 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1820 &root->delalloc_inodes);
1821 set_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1822 &BTRFS_I(inode)->runtime_flags);
1823 root->nr_delalloc_inodes++;
1824 if (root->nr_delalloc_inodes == 1) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001825 spin_lock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001826 BUG_ON(!list_empty(&root->delalloc_root));
1827 list_add_tail(&root->delalloc_root,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001828 &fs_info->delalloc_roots);
1829 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001830 }
1831 }
1832 spin_unlock(&root->delalloc_lock);
1833}
1834
Nikolay Borisov2b877332018-04-27 12:21:51 +03001835
1836void __btrfs_del_delalloc_inode(struct btrfs_root *root,
1837 struct btrfs_inode *inode)
Miao Xieeb73c1b2013-05-15 07:48:22 +00001838{
David Sterba3ffbd682018-06-29 10:56:42 +02001839 struct btrfs_fs_info *fs_info = root->fs_info;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001840
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001841 if (!list_empty(&inode->delalloc_inodes)) {
1842 list_del_init(&inode->delalloc_inodes);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001843 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001844 &inode->runtime_flags);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001845 root->nr_delalloc_inodes--;
1846 if (!root->nr_delalloc_inodes) {
Nikolay Borisov7c8a0d32018-04-27 12:21:52 +03001847 ASSERT(list_empty(&root->delalloc_inodes));
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001848 spin_lock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001849 BUG_ON(list_empty(&root->delalloc_root));
1850 list_del_init(&root->delalloc_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001851 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001852 }
1853 }
Nikolay Borisov2b877332018-04-27 12:21:51 +03001854}
1855
1856static void btrfs_del_delalloc_inode(struct btrfs_root *root,
1857 struct btrfs_inode *inode)
1858{
1859 spin_lock(&root->delalloc_lock);
1860 __btrfs_del_delalloc_inode(root, inode);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001861 spin_unlock(&root->delalloc_lock);
1862}
1863
Chris Masond352ac62008-09-29 15:18:18 -04001864/*
Nikolay Borisove06a1fc2018-11-01 14:09:50 +02001865 * Properly track delayed allocation bytes in the inode and to maintain the
1866 * list of inodes that have pending delalloc work to be done.
Chris Masond352ac62008-09-29 15:18:18 -04001867 */
Nikolay Borisove06a1fc2018-11-01 14:09:50 +02001868void btrfs_set_delalloc_extent(struct inode *inode, struct extent_state *state,
1869 unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001870{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001871 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1872
Wang Shilong47059d92014-07-03 18:22:07 +08001873 if ((*bits & EXTENT_DEFRAG) && !(*bits & EXTENT_DELALLOC))
1874 WARN_ON(1);
Chris Mason75eff682008-12-15 15:54:40 -05001875 /*
1876 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001877 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001878 * bit, which is only set or cleared with irqs on
1879 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001880 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001881 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001882 u64 len = state->end + 1 - state->start;
Josef Bacik8b62f872017-10-19 14:15:55 -04001883 u32 num_extents = count_max_extents(len);
Nikolay Borisov70ddc552017-02-20 13:50:35 +02001884 bool do_list = !btrfs_is_free_space_inode(BTRFS_I(inode));
Josef Bacik9ed74f22009-09-11 16:12:44 -04001885
Josef Bacik8b62f872017-10-19 14:15:55 -04001886 spin_lock(&BTRFS_I(inode)->lock);
1887 btrfs_mod_outstanding_extents(BTRFS_I(inode), num_extents);
1888 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik287a0ab2010-03-19 18:07:23 +00001889
Josef Bacik6a3891c2015-03-16 17:38:52 -04001890 /* For sanity tests */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001891 if (btrfs_is_testing(fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04001892 return;
1893
Nikolay Borisov104b4e52017-06-20 21:01:20 +03001894 percpu_counter_add_batch(&fs_info->delalloc_bytes, len,
1895 fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001896 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001897 BTRFS_I(inode)->delalloc_bytes += len;
Wang Shilong47059d92014-07-03 18:22:07 +08001898 if (*bits & EXTENT_DEFRAG)
1899 BTRFS_I(inode)->defrag_bytes += len;
Miao Xiedf0af1a2013-01-29 10:11:59 +00001900 if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00001901 &BTRFS_I(inode)->runtime_flags))
1902 btrfs_add_delalloc_inodes(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001903 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001904 }
Filipe Mananaa7e3b972017-04-03 10:45:46 +01001905
1906 if (!(state->state & EXTENT_DELALLOC_NEW) &&
1907 (*bits & EXTENT_DELALLOC_NEW)) {
1908 spin_lock(&BTRFS_I(inode)->lock);
1909 BTRFS_I(inode)->new_delalloc_bytes += state->end + 1 -
1910 state->start;
1911 spin_unlock(&BTRFS_I(inode)->lock);
1912 }
Chris Mason291d6732008-01-29 15:55:23 -05001913}
1914
Chris Masond352ac62008-09-29 15:18:18 -04001915/*
Nikolay Borisova36bb5f2018-11-01 14:09:51 +02001916 * Once a range is no longer delalloc this function ensures that proper
1917 * accounting happens.
Chris Masond352ac62008-09-29 15:18:18 -04001918 */
Nikolay Borisova36bb5f2018-11-01 14:09:51 +02001919void btrfs_clear_delalloc_extent(struct inode *vfs_inode,
1920 struct extent_state *state, unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001921{
Nikolay Borisova36bb5f2018-11-01 14:09:51 +02001922 struct btrfs_inode *inode = BTRFS_I(vfs_inode);
1923 struct btrfs_fs_info *fs_info = btrfs_sb(vfs_inode->i_sb);
Wang Shilong47059d92014-07-03 18:22:07 +08001924 u64 len = state->end + 1 - state->start;
David Sterba823bb202017-01-04 11:09:51 +01001925 u32 num_extents = count_max_extents(len);
Wang Shilong47059d92014-07-03 18:22:07 +08001926
Filipe Manana4a4b9642017-07-27 19:52:55 +01001927 if ((state->state & EXTENT_DEFRAG) && (*bits & EXTENT_DEFRAG)) {
1928 spin_lock(&inode->lock);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001929 inode->defrag_bytes -= len;
Filipe Manana4a4b9642017-07-27 19:52:55 +01001930 spin_unlock(&inode->lock);
1931 }
Wang Shilong47059d92014-07-03 18:22:07 +08001932
Chris Mason75eff682008-12-15 15:54:40 -05001933 /*
1934 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001935 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001936 * bit, which is only set or cleared with irqs on
1937 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001938 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001939 struct btrfs_root *root = inode->root;
Liu Bo83eea1f2012-07-10 05:28:39 -06001940 bool do_list = !btrfs_is_free_space_inode(inode);
Chris Masonbcbfce82008-04-22 13:26:47 -04001941
Josef Bacik8b62f872017-10-19 14:15:55 -04001942 spin_lock(&inode->lock);
1943 btrfs_mod_outstanding_extents(inode, -num_extents);
1944 spin_unlock(&inode->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001945
Josef Bacikb6d08f02013-09-27 14:57:43 -04001946 /*
1947 * We don't reserve metadata space for space cache inodes so we
Andrea Gelmini52042d82018-11-28 12:05:13 +01001948 * don't need to call delalloc_release_metadata if there is an
Josef Bacikb6d08f02013-09-27 14:57:43 -04001949 * error.
1950 */
Filipe Mananaa315e682017-03-06 23:04:20 +00001951 if (*bits & EXTENT_CLEAR_META_RESV &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001952 root != fs_info->tree_root)
Qu Wenruo43b18592017-12-12 15:34:32 +08001953 btrfs_delalloc_release_metadata(inode, len, false);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001954
Josef Bacik6a3891c2015-03-16 17:38:52 -04001955 /* For sanity tests. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001956 if (btrfs_is_testing(fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04001957 return;
1958
Filipe Mananaa315e682017-03-06 23:04:20 +00001959 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID &&
1960 do_list && !(state->state & EXTENT_NORESERVE) &&
1961 (*bits & EXTENT_CLEAR_DATA_RESV))
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001962 btrfs_free_reserved_data_space_noquota(
1963 &inode->vfs_inode,
Qu Wenruo51773be2015-10-08 18:19:37 +08001964 state->start, len);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001965
Nikolay Borisov104b4e52017-06-20 21:01:20 +03001966 percpu_counter_add_batch(&fs_info->delalloc_bytes, -len,
1967 fs_info->delalloc_batch);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001968 spin_lock(&inode->lock);
1969 inode->delalloc_bytes -= len;
1970 if (do_list && inode->delalloc_bytes == 0 &&
Miao Xiedf0af1a2013-01-29 10:11:59 +00001971 test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001972 &inode->runtime_flags))
Miao Xieeb73c1b2013-05-15 07:48:22 +00001973 btrfs_del_delalloc_inode(root, inode);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001974 spin_unlock(&inode->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001975 }
Filipe Mananaa7e3b972017-04-03 10:45:46 +01001976
1977 if ((state->state & EXTENT_DELALLOC_NEW) &&
1978 (*bits & EXTENT_DELALLOC_NEW)) {
1979 spin_lock(&inode->lock);
1980 ASSERT(inode->new_delalloc_bytes >= len);
1981 inode->new_delalloc_bytes -= len;
1982 spin_unlock(&inode->lock);
1983 }
Chris Mason291d6732008-01-29 15:55:23 -05001984}
1985
Chris Masond352ac62008-09-29 15:18:18 -04001986/*
Nikolay Borisovda12fe52018-11-27 20:57:58 +02001987 * btrfs_bio_fits_in_stripe - Checks whether the size of the given bio will fit
1988 * in a chunk's stripe. This function ensures that bios do not span a
1989 * stripe/chunk
Liu Bo6f034ec2016-06-22 18:31:49 -07001990 *
Nikolay Borisovda12fe52018-11-27 20:57:58 +02001991 * @page - The page we are about to add to the bio
1992 * @size - size we want to add to the bio
1993 * @bio - bio we want to ensure is smaller than a stripe
1994 * @bio_flags - flags of the bio
1995 *
1996 * return 1 if page cannot be added to the bio
1997 * return 0 if page can be added to the bio
Liu Bo6f034ec2016-06-22 18:31:49 -07001998 * return error otherwise
Chris Masond352ac62008-09-29 15:18:18 -04001999 */
Nikolay Borisovda12fe52018-11-27 20:57:58 +02002000int btrfs_bio_fits_in_stripe(struct page *page, size_t size, struct bio *bio,
2001 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04002002{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002003 struct inode *inode = page->mapping->host;
2004 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Kent Overstreet4f024f32013-10-11 15:44:27 -07002005 u64 logical = (u64)bio->bi_iter.bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04002006 u64 length = 0;
2007 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04002008 int ret;
Nikolay Borisov89b798a2019-06-03 12:05:05 +03002009 struct btrfs_io_geometry geom;
Chris Mason239b14b2008-03-24 15:02:07 -04002010
Chris Mason771ed682008-11-06 22:02:51 -05002011 if (bio_flags & EXTENT_BIO_COMPRESSED)
2012 return 0;
2013
Kent Overstreet4f024f32013-10-11 15:44:27 -07002014 length = bio->bi_iter.bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04002015 map_length = length;
Nikolay Borisov89b798a2019-06-03 12:05:05 +03002016 ret = btrfs_get_io_geometry(fs_info, btrfs_op(bio), logical, map_length,
2017 &geom);
Liu Bo6f034ec2016-06-22 18:31:49 -07002018 if (ret < 0)
2019 return ret;
Nikolay Borisov89b798a2019-06-03 12:05:05 +03002020
2021 if (geom.len < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04002022 return 1;
Jeff Mahoney3444a972011-10-03 23:23:13 -04002023 return 0;
Chris Mason239b14b2008-03-24 15:02:07 -04002024}
2025
Chris Masond352ac62008-09-29 15:18:18 -04002026/*
2027 * in order to insert checksums into the metadata in large chunks,
2028 * we wait until bio submission time. All the pages in the bio are
2029 * checksummed and sums are attached onto the ordered extent record.
2030 *
2031 * At IO completion time the cums attached on the ordered extent record
2032 * are inserted into the btree
2033 */
David Sterbad0ee3932018-03-08 14:35:48 +01002034static blk_status_t btrfs_submit_bio_start(void *private_data, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04002035 u64 bio_offset)
Chris Mason065631f2008-02-20 12:07:25 -05002036{
Josef Bacikc6100a42017-05-05 11:57:13 -04002037 struct inode *inode = private_data;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002038 blk_status_t ret = 0;
Chris Masone0156402008-04-16 11:15:20 -04002039
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002040 ret = btrfs_csum_one_bio(inode, bio, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002041 BUG_ON(ret); /* -ENOMEM */
Chris Mason4a69a412008-11-06 22:03:00 -05002042 return 0;
2043}
Chris Masone0156402008-04-16 11:15:20 -04002044
Chris Mason4a69a412008-11-06 22:03:00 -05002045/*
Chris Masoncad321a2008-12-17 14:51:42 -05002046 * extent_io.c submission hook. This does the right thing for csum calculation
Liu Bo4c274bc2017-11-01 17:19:27 -06002047 * on write, or reading the csums from the tree before a read.
2048 *
2049 * Rules about async/sync submit,
2050 * a) read: sync submit
2051 *
2052 * b) write without checksum: sync submit
2053 *
2054 * c) write with checksum:
2055 * c-1) if bio is issued by fsync: sync submit
2056 * (sync_writers != 0)
2057 *
2058 * c-2) if root is reloc root: sync submit
2059 * (only in case of buffered IO)
2060 *
2061 * c-3) otherwise: async submit
Chris Masond352ac62008-09-29 15:18:18 -04002062 */
Nikolay Borisova56b1c72019-04-10 17:24:39 +03002063static blk_status_t btrfs_submit_bio_hook(struct inode *inode, struct bio *bio,
Nikolay Borisov50489a52019-04-10 19:46:04 +03002064 int mirror_num,
2065 unsigned long bio_flags)
2066
Chris Mason44b8bd72008-04-16 11:14:51 -04002067{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002068 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason44b8bd72008-04-16 11:14:51 -04002069 struct btrfs_root *root = BTRFS_I(inode)->root;
Chandan Rajendra0d51e282015-07-27 15:26:43 +05302070 enum btrfs_wq_endio_type metadata = BTRFS_WQ_ENDIO_DATA;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002071 blk_status_t ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04002072 int skip_sum;
Josef Bacikb812ce22012-11-16 13:56:32 -05002073 int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
Chris Mason44b8bd72008-04-16 11:14:51 -04002074
Christoph Hellwig6cbff002009-04-17 10:37:41 +02002075 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masoncad321a2008-12-17 14:51:42 -05002076
Nikolay Borisov70ddc552017-02-20 13:50:35 +02002077 if (btrfs_is_free_space_inode(BTRFS_I(inode)))
Chandan Rajendra0d51e282015-07-27 15:26:43 +05302078 metadata = BTRFS_WQ_ENDIO_FREE_SPACE;
Jeff Mahoney04173412011-10-03 23:23:12 -04002079
Mike Christie37226b22016-06-05 14:31:52 -05002080 if (bio_op(bio) != REQ_OP_WRITE) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002081 ret = btrfs_bio_wq_end_io(fs_info, bio, metadata);
Josef Bacik5fd02042012-05-02 14:00:54 -04002082 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01002083 goto out;
Josef Bacik5fd02042012-05-02 14:00:54 -04002084
Chris Masond20f7042008-12-08 16:58:54 -05002085 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Stefan Behrens61891922012-11-05 18:51:52 +01002086 ret = btrfs_submit_compressed_read(inode, bio,
2087 mirror_num,
2088 bio_flags);
2089 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00002090 } else if (!skip_sum) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002091 ret = btrfs_lookup_bio_sums(inode, bio, NULL);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00002092 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01002093 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00002094 }
Chris Mason4d1b5fb2008-08-20 09:44:52 -04002095 goto mapit;
Josef Bacikb812ce22012-11-16 13:56:32 -05002096 } else if (async && !skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05002097 /* csum items have already been cloned */
2098 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
2099 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04002100 /* we're doing a write, do the async checksumming */
Josef Bacikc6100a42017-05-05 11:57:13 -04002101 ret = btrfs_wq_submit_bio(fs_info, bio, mirror_num, bio_flags,
Nikolay Borisove7681162019-04-10 17:24:41 +03002102 0, inode, btrfs_submit_bio_start);
Stefan Behrens61891922012-11-05 18:51:52 +01002103 goto out;
Josef Bacikb812ce22012-11-16 13:56:32 -05002104 } else if (!skip_sum) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002105 ret = btrfs_csum_one_bio(inode, bio, 0, 0);
Josef Bacikb812ce22012-11-16 13:56:32 -05002106 if (ret)
2107 goto out;
Chris Mason19b9bdb2008-10-30 14:23:13 -04002108 }
2109
Chris Mason0b86a832008-03-24 15:01:56 -04002110mapit:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002111 ret = btrfs_map_bio(fs_info, bio, mirror_num, 0);
Stefan Behrens61891922012-11-05 18:51:52 +01002112
2113out:
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002114 if (ret) {
2115 bio->bi_status = ret;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002116 bio_endio(bio);
2117 }
Stefan Behrens61891922012-11-05 18:51:52 +01002118 return ret;
Chris Mason065631f2008-02-20 12:07:25 -05002119}
Chris Mason6885f302008-02-20 16:11:05 -05002120
Chris Masond352ac62008-09-29 15:18:18 -04002121/*
2122 * given a list of ordered sums record them in the inode. This happens
2123 * at IO completion time based on sums calculated at bio submission time.
2124 */
Chris Masonba1da2f2008-07-17 12:54:15 -04002125static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
David Sterbadf9f6282017-02-10 19:35:37 +01002126 struct inode *inode, struct list_head *list)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002127{
Chris Masone6dcd2d2008-07-17 12:53:50 -04002128 struct btrfs_ordered_sum *sum;
Nikolay Borisovac01f262018-01-08 10:59:43 +02002129 int ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002130
Qinghuang Fengc6e30872009-01-21 10:59:08 -05002131 list_for_each_entry(sum, list, list) {
David Sterba7c2871a2017-11-08 01:07:43 +01002132 trans->adding_csums = true;
Nikolay Borisovac01f262018-01-08 10:59:43 +02002133 ret = btrfs_csum_file_blocks(trans,
Chris Masond20f7042008-12-08 16:58:54 -05002134 BTRFS_I(inode)->root->fs_info->csum_root, sum);
David Sterba7c2871a2017-11-08 01:07:43 +01002135 trans->adding_csums = false;
Nikolay Borisovac01f262018-01-08 10:59:43 +02002136 if (ret)
2137 return ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002138 }
2139 return 0;
2140}
2141
Josef Bacik2ac55d42010-02-03 19:33:23 +00002142int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
Filipe Mananae3b8a482017-11-04 00:16:59 +00002143 unsigned int extra_bits,
Nikolay Borisov330a5822019-07-17 16:18:17 +03002144 struct extent_state **cached_state)
Chris Masonea8c2812008-08-04 23:17:27 -04002145{
Johannes Thumshirnfdb1e122018-12-05 15:23:04 +01002146 WARN_ON(PAGE_ALIGNED(end));
Chris Masonea8c2812008-08-04 23:17:27 -04002147 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
Filipe Mananae3b8a482017-11-04 00:16:59 +00002148 extra_bits, cached_state);
Chris Masonea8c2812008-08-04 23:17:27 -04002149}
2150
Chris Masond352ac62008-09-29 15:18:18 -04002151/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04002152struct btrfs_writepage_fixup {
2153 struct page *page;
2154 struct btrfs_work work;
2155};
2156
Christoph Hellwigb2950862008-12-02 09:54:17 -05002157static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04002158{
2159 struct btrfs_writepage_fixup *fixup;
2160 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002161 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08002162 struct extent_changeset *data_reserved = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04002163 struct page *page;
2164 struct inode *inode;
2165 u64 page_start;
2166 u64 page_end;
Jeff Mahoney87826df2012-02-15 16:23:57 +01002167 int ret;
Chris Mason247e7432008-07-17 12:53:51 -04002168
2169 fixup = container_of(work, struct btrfs_writepage_fixup, work);
2170 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -04002171again:
Chris Mason247e7432008-07-17 12:53:51 -04002172 lock_page(page);
2173 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
2174 ClearPageChecked(page);
2175 goto out_page;
2176 }
2177
2178 inode = page->mapping->host;
2179 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002180 page_end = page_offset(page) + PAGE_SIZE - 1;
Chris Mason247e7432008-07-17 12:53:51 -04002181
David Sterbaff13db42015-12-03 14:30:40 +01002182 lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002183 &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04002184
2185 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04002186 if (PagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04002187 goto out;
Chris Mason4a096752008-07-21 10:29:44 -04002188
Nikolay Borisova776c6f2017-02-20 13:50:49 +02002189 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002190 PAGE_SIZE);
Chris Mason4a096752008-07-21 10:29:44 -04002191 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00002192 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
David Sterbae43bbe52017-12-12 21:43:52 +01002193 page_end, &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04002194 unlock_page(page);
2195 btrfs_start_ordered_extent(inode, ordered, 1);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002196 btrfs_put_ordered_extent(ordered);
Chris Mason4a096752008-07-21 10:29:44 -04002197 goto again;
2198 }
Chris Mason247e7432008-07-17 12:53:51 -04002199
Qu Wenruo364ecf32017-02-27 15:10:38 +08002200 ret = btrfs_delalloc_reserve_space(inode, &data_reserved, page_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002201 PAGE_SIZE);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002202 if (ret) {
2203 mapping_set_error(page->mapping, ret);
2204 end_extent_writepage(page, ret, page_start, page_end);
2205 ClearPageChecked(page);
2206 goto out;
2207 }
2208
Nikolay Borisovf3038ee2017-12-05 09:29:19 +02002209 ret = btrfs_set_extent_delalloc(inode, page_start, page_end, 0,
Nikolay Borisov330a5822019-07-17 16:18:17 +03002210 &cached_state);
Nikolay Borisovf3038ee2017-12-05 09:29:19 +02002211 if (ret) {
2212 mapping_set_error(page->mapping, ret);
2213 end_extent_writepage(page, ret, page_start, page_end);
2214 ClearPageChecked(page);
2215 goto out;
2216 }
2217
Chris Mason247e7432008-07-17 12:53:51 -04002218 ClearPageChecked(page);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002219 set_page_dirty(page);
Qu Wenruo8702ba92019-10-14 14:34:51 +08002220 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE);
Chris Mason247e7432008-07-17 12:53:51 -04002221out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00002222 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01002223 &cached_state);
Chris Mason247e7432008-07-17 12:53:51 -04002224out_page:
2225 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002226 put_page(page);
Miao Xieb897abe2011-01-26 16:19:22 +08002227 kfree(fixup);
Qu Wenruo364ecf32017-02-27 15:10:38 +08002228 extent_changeset_free(data_reserved);
Chris Mason247e7432008-07-17 12:53:51 -04002229}
2230
2231/*
2232 * There are a few paths in the higher layers of the kernel that directly
2233 * set the page dirty bit without asking the filesystem if it is a
2234 * good idea. This causes problems because we want to make sure COW
2235 * properly happens and the data=ordered rules are followed.
2236 *
Chris Masonc8b97812008-10-29 14:49:59 -04002237 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04002238 * hasn't been properly setup for IO. We kick off an async process
2239 * to fix it up. The async helper will wait for ordered extents, set
2240 * the delalloc bit and make it safe to write the page.
2241 */
Nikolay Borisovd75855b2018-11-01 14:09:47 +02002242int btrfs_writepage_cow_fixup(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04002243{
2244 struct inode *inode = page->mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002245 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason247e7432008-07-17 12:53:51 -04002246 struct btrfs_writepage_fixup *fixup;
Chris Mason247e7432008-07-17 12:53:51 -04002247
Chris Mason8b62b722009-09-02 16:53:46 -04002248 /* this page is properly in the ordered list */
2249 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04002250 return 0;
2251
2252 if (PageChecked(page))
2253 return -EAGAIN;
2254
2255 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
2256 if (!fixup)
2257 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04002258
Chris Mason247e7432008-07-17 12:53:51 -04002259 SetPageChecked(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002260 get_page(page);
Omar Sandovala0cac0e2019-09-16 11:30:57 -07002261 btrfs_init_work(&fixup->work, btrfs_writepage_fixup_worker, NULL, NULL);
Chris Mason247e7432008-07-17 12:53:51 -04002262 fixup->page = page;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002263 btrfs_queue_work(fs_info->fixup_workers, &fixup->work);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002264 return -EBUSY;
Chris Mason247e7432008-07-17 12:53:51 -04002265}
2266
Yan Zhengd899e052008-10-30 14:25:28 -04002267static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
2268 struct inode *inode, u64 file_pos,
2269 u64 disk_bytenr, u64 disk_num_bytes,
2270 u64 num_bytes, u64 ram_bytes,
2271 u8 compression, u8 encryption,
2272 u16 other_encoding, int extent_type)
2273{
2274 struct btrfs_root *root = BTRFS_I(inode)->root;
2275 struct btrfs_file_extent_item *fi;
2276 struct btrfs_path *path;
2277 struct extent_buffer *leaf;
2278 struct btrfs_key ins;
Qu Wenruoa12b8772017-02-27 15:10:37 +08002279 u64 qg_released;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002280 int extent_inserted = 0;
Yan Zhengd899e052008-10-30 14:25:28 -04002281 int ret;
2282
2283 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07002284 if (!path)
2285 return -ENOMEM;
Yan Zhengd899e052008-10-30 14:25:28 -04002286
Chris Masona1ed8352009-09-11 12:27:37 -04002287 /*
2288 * we may be replacing one extent in the tree with another.
2289 * The new extent is pinned in the extent map, and we don't want
2290 * to drop it from the cache until it is completely in the btree.
2291 *
2292 * So, tell btrfs_drop_extents to leave this extent in the cache.
2293 * the caller is expected to unpin it and allow it to be merged
2294 * with the others.
2295 */
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002296 ret = __btrfs_drop_extents(trans, root, inode, path, file_pos,
2297 file_pos + num_bytes, NULL, 0,
2298 1, sizeof(*fi), &extent_inserted);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002299 if (ret)
2300 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04002301
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002302 if (!extent_inserted) {
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002303 ins.objectid = btrfs_ino(BTRFS_I(inode));
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002304 ins.offset = file_pos;
2305 ins.type = BTRFS_EXTENT_DATA_KEY;
2306
2307 path->leave_spinning = 1;
2308 ret = btrfs_insert_empty_item(trans, root, path, &ins,
2309 sizeof(*fi));
2310 if (ret)
2311 goto out;
2312 }
Yan Zhengd899e052008-10-30 14:25:28 -04002313 leaf = path->nodes[0];
2314 fi = btrfs_item_ptr(leaf, path->slots[0],
2315 struct btrfs_file_extent_item);
2316 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
2317 btrfs_set_file_extent_type(leaf, fi, extent_type);
2318 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
2319 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
2320 btrfs_set_file_extent_offset(leaf, fi, 0);
2321 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
2322 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
2323 btrfs_set_file_extent_compression(leaf, fi, compression);
2324 btrfs_set_file_extent_encryption(leaf, fi, encryption);
2325 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
Chris Masonb9473432009-03-13 11:00:37 -04002326
Yan Zhengd899e052008-10-30 14:25:28 -04002327 btrfs_mark_buffer_dirty(leaf);
Josef Bacikce195332012-09-25 15:26:16 -04002328 btrfs_release_path(path);
Yan Zhengd899e052008-10-30 14:25:28 -04002329
2330 inode_add_bytes(inode, num_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -04002331
2332 ins.objectid = disk_bytenr;
2333 ins.offset = disk_num_bytes;
2334 ins.type = BTRFS_EXTENT_ITEM_KEY;
Qu Wenruoa12b8772017-02-27 15:10:37 +08002335
Qu Wenruo297d7502015-09-08 17:08:37 +08002336 /*
Qu Wenruo5846a3c2015-10-26 14:11:18 +08002337 * Release the reserved range from inode dirty range map, as it is
2338 * already moved into delayed_ref_head
Qu Wenruo297d7502015-09-08 17:08:37 +08002339 */
Qu Wenruoa12b8772017-02-27 15:10:37 +08002340 ret = btrfs_qgroup_release_data(inode, file_pos, ram_bytes);
2341 if (ret < 0)
2342 goto out;
2343 qg_released = ret;
Josef Bacik84f7d8e2017-09-29 15:43:49 -04002344 ret = btrfs_alloc_reserved_file_extent(trans, root,
2345 btrfs_ino(BTRFS_I(inode)),
2346 file_pos, qg_released, &ins);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002347out:
Yan Zhengd899e052008-10-30 14:25:28 -04002348 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04002349
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002350 return ret;
Yan Zhengd899e052008-10-30 14:25:28 -04002351}
2352
Liu Bo38c227d2013-01-29 03:18:40 +00002353/* snapshot-aware defrag */
2354struct sa_defrag_extent_backref {
2355 struct rb_node node;
2356 struct old_sa_defrag_extent *old;
2357 u64 root_id;
2358 u64 inum;
2359 u64 file_pos;
2360 u64 extent_offset;
2361 u64 num_bytes;
2362 u64 generation;
2363};
2364
2365struct old_sa_defrag_extent {
2366 struct list_head list;
2367 struct new_sa_defrag_extent *new;
2368
2369 u64 extent_offset;
2370 u64 bytenr;
2371 u64 offset;
2372 u64 len;
2373 int count;
2374};
2375
2376struct new_sa_defrag_extent {
2377 struct rb_root root;
2378 struct list_head head;
2379 struct btrfs_path *path;
2380 struct inode *inode;
2381 u64 file_pos;
2382 u64 len;
2383 u64 bytenr;
2384 u64 disk_len;
2385 u8 compress_type;
2386};
2387
2388static int backref_comp(struct sa_defrag_extent_backref *b1,
2389 struct sa_defrag_extent_backref *b2)
2390{
2391 if (b1->root_id < b2->root_id)
2392 return -1;
2393 else if (b1->root_id > b2->root_id)
2394 return 1;
2395
2396 if (b1->inum < b2->inum)
2397 return -1;
2398 else if (b1->inum > b2->inum)
2399 return 1;
2400
2401 if (b1->file_pos < b2->file_pos)
2402 return -1;
2403 else if (b1->file_pos > b2->file_pos)
2404 return 1;
2405
2406 /*
2407 * [------------------------------] ===> (a range of space)
2408 * |<--->| |<---->| =============> (fs/file tree A)
2409 * |<---------------------------->| ===> (fs/file tree B)
2410 *
2411 * A range of space can refer to two file extents in one tree while
2412 * refer to only one file extent in another tree.
2413 *
2414 * So we may process a disk offset more than one time(two extents in A)
2415 * and locate at the same extent(one extent in B), then insert two same
2416 * backrefs(both refer to the extent in B).
2417 */
2418 return 0;
2419}
2420
2421static void backref_insert(struct rb_root *root,
2422 struct sa_defrag_extent_backref *backref)
2423{
2424 struct rb_node **p = &root->rb_node;
2425 struct rb_node *parent = NULL;
2426 struct sa_defrag_extent_backref *entry;
2427 int ret;
2428
2429 while (*p) {
2430 parent = *p;
2431 entry = rb_entry(parent, struct sa_defrag_extent_backref, node);
2432
2433 ret = backref_comp(backref, entry);
2434 if (ret < 0)
2435 p = &(*p)->rb_left;
2436 else
2437 p = &(*p)->rb_right;
2438 }
2439
2440 rb_link_node(&backref->node, parent, p);
2441 rb_insert_color(&backref->node, root);
2442}
2443
2444/*
2445 * Note the backref might has changed, and in this case we just return 0.
2446 */
2447static noinline int record_one_backref(u64 inum, u64 offset, u64 root_id,
2448 void *ctx)
2449{
2450 struct btrfs_file_extent_item *extent;
Liu Bo38c227d2013-01-29 03:18:40 +00002451 struct old_sa_defrag_extent *old = ctx;
2452 struct new_sa_defrag_extent *new = old->new;
2453 struct btrfs_path *path = new->path;
2454 struct btrfs_key key;
2455 struct btrfs_root *root;
2456 struct sa_defrag_extent_backref *backref;
2457 struct extent_buffer *leaf;
2458 struct inode *inode = new->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002459 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002460 int slot;
2461 int ret;
2462 u64 extent_offset;
2463 u64 num_bytes;
2464
2465 if (BTRFS_I(inode)->root->root_key.objectid == root_id &&
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002466 inum == btrfs_ino(BTRFS_I(inode)))
Liu Bo38c227d2013-01-29 03:18:40 +00002467 return 0;
2468
2469 key.objectid = root_id;
2470 key.type = BTRFS_ROOT_ITEM_KEY;
2471 key.offset = (u64)-1;
2472
Liu Bo38c227d2013-01-29 03:18:40 +00002473 root = btrfs_read_fs_root_no_name(fs_info, &key);
2474 if (IS_ERR(root)) {
2475 if (PTR_ERR(root) == -ENOENT)
2476 return 0;
2477 WARN_ON(1);
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002478 btrfs_debug(fs_info, "inum=%llu, offset=%llu, root_id=%llu",
Liu Bo38c227d2013-01-29 03:18:40 +00002479 inum, offset, root_id);
2480 return PTR_ERR(root);
2481 }
2482
2483 key.objectid = inum;
2484 key.type = BTRFS_EXTENT_DATA_KEY;
2485 if (offset > (u64)-1 << 32)
2486 key.offset = 0;
2487 else
2488 key.offset = offset;
2489
2490 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05302491 if (WARN_ON(ret < 0))
Liu Bo38c227d2013-01-29 03:18:40 +00002492 return ret;
Josef Bacik50f13192013-07-22 12:50:37 -04002493 ret = 0;
Liu Bo38c227d2013-01-29 03:18:40 +00002494
2495 while (1) {
2496 cond_resched();
2497
2498 leaf = path->nodes[0];
2499 slot = path->slots[0];
2500
2501 if (slot >= btrfs_header_nritems(leaf)) {
2502 ret = btrfs_next_leaf(root, path);
2503 if (ret < 0) {
2504 goto out;
2505 } else if (ret > 0) {
2506 ret = 0;
2507 goto out;
2508 }
2509 continue;
2510 }
2511
2512 path->slots[0]++;
2513
2514 btrfs_item_key_to_cpu(leaf, &key, slot);
2515
2516 if (key.objectid > inum)
2517 goto out;
2518
2519 if (key.objectid < inum || key.type != BTRFS_EXTENT_DATA_KEY)
2520 continue;
2521
2522 extent = btrfs_item_ptr(leaf, slot,
2523 struct btrfs_file_extent_item);
2524
2525 if (btrfs_file_extent_disk_bytenr(leaf, extent) != old->bytenr)
2526 continue;
2527
Liu Boe68afa42013-07-01 22:13:26 +08002528 /*
2529 * 'offset' refers to the exact key.offset,
2530 * NOT the 'offset' field in btrfs_extent_data_ref, ie.
2531 * (key.offset - extent_offset).
2532 */
2533 if (key.offset != offset)
Liu Bo38c227d2013-01-29 03:18:40 +00002534 continue;
2535
Liu Boe68afa42013-07-01 22:13:26 +08002536 extent_offset = btrfs_file_extent_offset(leaf, extent);
Liu Bo38c227d2013-01-29 03:18:40 +00002537 num_bytes = btrfs_file_extent_num_bytes(leaf, extent);
Liu Boe68afa42013-07-01 22:13:26 +08002538
Liu Bo38c227d2013-01-29 03:18:40 +00002539 if (extent_offset >= old->extent_offset + old->offset +
2540 old->len || extent_offset + num_bytes <=
2541 old->extent_offset + old->offset)
2542 continue;
Liu Bo38c227d2013-01-29 03:18:40 +00002543 break;
2544 }
2545
2546 backref = kmalloc(sizeof(*backref), GFP_NOFS);
2547 if (!backref) {
2548 ret = -ENOENT;
2549 goto out;
2550 }
2551
2552 backref->root_id = root_id;
2553 backref->inum = inum;
Liu Boe68afa42013-07-01 22:13:26 +08002554 backref->file_pos = offset;
Liu Bo38c227d2013-01-29 03:18:40 +00002555 backref->num_bytes = num_bytes;
2556 backref->extent_offset = extent_offset;
2557 backref->generation = btrfs_file_extent_generation(leaf, extent);
2558 backref->old = old;
2559 backref_insert(&new->root, backref);
2560 old->count++;
2561out:
2562 btrfs_release_path(path);
2563 WARN_ON(ret);
2564 return ret;
2565}
2566
2567static noinline bool record_extent_backrefs(struct btrfs_path *path,
2568 struct new_sa_defrag_extent *new)
2569{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002570 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002571 struct old_sa_defrag_extent *old, *tmp;
2572 int ret;
2573
2574 new->path = path;
2575
2576 list_for_each_entry_safe(old, tmp, &new->head, list) {
Liu Boe68afa42013-07-01 22:13:26 +08002577 ret = iterate_inodes_from_logical(old->bytenr +
2578 old->extent_offset, fs_info,
Liu Bo38c227d2013-01-29 03:18:40 +00002579 path, record_one_backref,
Zygo Blaxellc995ab32017-09-22 13:58:45 -04002580 old, false);
Josef Bacik4724b102013-11-05 11:11:40 -05002581 if (ret < 0 && ret != -ENOENT)
2582 return false;
Liu Bo38c227d2013-01-29 03:18:40 +00002583
2584 /* no backref to be processed for this extent */
2585 if (!old->count) {
2586 list_del(&old->list);
2587 kfree(old);
2588 }
2589 }
2590
2591 if (list_empty(&new->head))
2592 return false;
2593
2594 return true;
2595}
2596
2597static int relink_is_mergable(struct extent_buffer *leaf,
2598 struct btrfs_file_extent_item *fi,
Liu Bo116e0022013-08-02 16:30:40 +08002599 struct new_sa_defrag_extent *new)
Liu Bo38c227d2013-01-29 03:18:40 +00002600{
Liu Bo116e0022013-08-02 16:30:40 +08002601 if (btrfs_file_extent_disk_bytenr(leaf, fi) != new->bytenr)
Liu Bo38c227d2013-01-29 03:18:40 +00002602 return 0;
2603
2604 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG)
2605 return 0;
2606
Liu Bo116e0022013-08-02 16:30:40 +08002607 if (btrfs_file_extent_compression(leaf, fi) != new->compress_type)
2608 return 0;
2609
2610 if (btrfs_file_extent_encryption(leaf, fi) ||
Liu Bo38c227d2013-01-29 03:18:40 +00002611 btrfs_file_extent_other_encoding(leaf, fi))
2612 return 0;
2613
2614 return 1;
2615}
2616
2617/*
2618 * Note the backref might has changed, and in this case we just return 0.
2619 */
2620static noinline int relink_extent_backref(struct btrfs_path *path,
2621 struct sa_defrag_extent_backref *prev,
2622 struct sa_defrag_extent_backref *backref)
2623{
2624 struct btrfs_file_extent_item *extent;
2625 struct btrfs_file_extent_item *item;
2626 struct btrfs_ordered_extent *ordered;
2627 struct btrfs_trans_handle *trans;
Qu Wenruo82fa1132019-04-04 14:45:35 +08002628 struct btrfs_ref ref = { 0 };
Liu Bo38c227d2013-01-29 03:18:40 +00002629 struct btrfs_root *root;
2630 struct btrfs_key key;
2631 struct extent_buffer *leaf;
2632 struct old_sa_defrag_extent *old = backref->old;
2633 struct new_sa_defrag_extent *new = old->new;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002634 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002635 struct inode *inode;
2636 struct extent_state *cached = NULL;
2637 int ret = 0;
2638 u64 start;
2639 u64 len;
2640 u64 lock_start;
2641 u64 lock_end;
2642 bool merge = false;
2643 int index;
2644
2645 if (prev && prev->root_id == backref->root_id &&
2646 prev->inum == backref->inum &&
2647 prev->file_pos + prev->num_bytes == backref->file_pos)
2648 merge = true;
2649
2650 /* step 1: get root */
2651 key.objectid = backref->root_id;
2652 key.type = BTRFS_ROOT_ITEM_KEY;
2653 key.offset = (u64)-1;
2654
Liu Bo38c227d2013-01-29 03:18:40 +00002655 index = srcu_read_lock(&fs_info->subvol_srcu);
2656
2657 root = btrfs_read_fs_root_no_name(fs_info, &key);
2658 if (IS_ERR(root)) {
2659 srcu_read_unlock(&fs_info->subvol_srcu, index);
2660 if (PTR_ERR(root) == -ENOENT)
2661 return 0;
2662 return PTR_ERR(root);
2663 }
Liu Bo38c227d2013-01-29 03:18:40 +00002664
Wang Shilongbcbba5e2014-02-08 23:46:35 +08002665 if (btrfs_root_readonly(root)) {
2666 srcu_read_unlock(&fs_info->subvol_srcu, index);
2667 return 0;
2668 }
2669
Liu Bo38c227d2013-01-29 03:18:40 +00002670 /* step 2: get inode */
2671 key.objectid = backref->inum;
2672 key.type = BTRFS_INODE_ITEM_KEY;
2673 key.offset = 0;
2674
2675 inode = btrfs_iget(fs_info->sb, &key, root, NULL);
2676 if (IS_ERR(inode)) {
2677 srcu_read_unlock(&fs_info->subvol_srcu, index);
2678 return 0;
2679 }
2680
2681 srcu_read_unlock(&fs_info->subvol_srcu, index);
2682
2683 /* step 3: relink backref */
2684 lock_start = backref->file_pos;
2685 lock_end = backref->file_pos + backref->num_bytes - 1;
2686 lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
David Sterbaff13db42015-12-03 14:30:40 +01002687 &cached);
Liu Bo38c227d2013-01-29 03:18:40 +00002688
2689 ordered = btrfs_lookup_first_ordered_extent(inode, lock_end);
2690 if (ordered) {
2691 btrfs_put_ordered_extent(ordered);
2692 goto out_unlock;
2693 }
2694
2695 trans = btrfs_join_transaction(root);
2696 if (IS_ERR(trans)) {
2697 ret = PTR_ERR(trans);
2698 goto out_unlock;
2699 }
2700
2701 key.objectid = backref->inum;
2702 key.type = BTRFS_EXTENT_DATA_KEY;
2703 key.offset = backref->file_pos;
2704
2705 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2706 if (ret < 0) {
2707 goto out_free_path;
2708 } else if (ret > 0) {
2709 ret = 0;
2710 goto out_free_path;
2711 }
2712
2713 extent = btrfs_item_ptr(path->nodes[0], path->slots[0],
2714 struct btrfs_file_extent_item);
2715
2716 if (btrfs_file_extent_generation(path->nodes[0], extent) !=
2717 backref->generation)
2718 goto out_free_path;
2719
2720 btrfs_release_path(path);
2721
2722 start = backref->file_pos;
2723 if (backref->extent_offset < old->extent_offset + old->offset)
2724 start += old->extent_offset + old->offset -
2725 backref->extent_offset;
2726
2727 len = min(backref->extent_offset + backref->num_bytes,
2728 old->extent_offset + old->offset + old->len);
2729 len -= max(backref->extent_offset, old->extent_offset + old->offset);
2730
2731 ret = btrfs_drop_extents(trans, root, inode, start,
2732 start + len, 1);
2733 if (ret)
2734 goto out_free_path;
2735again:
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002736 key.objectid = btrfs_ino(BTRFS_I(inode));
Liu Bo38c227d2013-01-29 03:18:40 +00002737 key.type = BTRFS_EXTENT_DATA_KEY;
2738 key.offset = start;
2739
Liu Boa09a0a72013-03-11 09:20:58 +00002740 path->leave_spinning = 1;
Liu Bo38c227d2013-01-29 03:18:40 +00002741 if (merge) {
2742 struct btrfs_file_extent_item *fi;
2743 u64 extent_len;
2744 struct btrfs_key found_key;
2745
Gui Hecheng3c9665d2014-01-23 13:41:09 +08002746 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
Liu Bo38c227d2013-01-29 03:18:40 +00002747 if (ret < 0)
2748 goto out_free_path;
2749
2750 path->slots[0]--;
2751 leaf = path->nodes[0];
2752 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2753
2754 fi = btrfs_item_ptr(leaf, path->slots[0],
2755 struct btrfs_file_extent_item);
2756 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
2757
Liu Bo116e0022013-08-02 16:30:40 +08002758 if (extent_len + found_key.offset == start &&
2759 relink_is_mergable(leaf, fi, new)) {
Liu Bo38c227d2013-01-29 03:18:40 +00002760 btrfs_set_file_extent_num_bytes(leaf, fi,
2761 extent_len + len);
2762 btrfs_mark_buffer_dirty(leaf);
2763 inode_add_bytes(inode, len);
2764
2765 ret = 1;
2766 goto out_free_path;
2767 } else {
2768 merge = false;
2769 btrfs_release_path(path);
2770 goto again;
2771 }
2772 }
2773
2774 ret = btrfs_insert_empty_item(trans, root, path, &key,
2775 sizeof(*extent));
2776 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002777 btrfs_abort_transaction(trans, ret);
Liu Bo38c227d2013-01-29 03:18:40 +00002778 goto out_free_path;
2779 }
2780
2781 leaf = path->nodes[0];
2782 item = btrfs_item_ptr(leaf, path->slots[0],
2783 struct btrfs_file_extent_item);
2784 btrfs_set_file_extent_disk_bytenr(leaf, item, new->bytenr);
2785 btrfs_set_file_extent_disk_num_bytes(leaf, item, new->disk_len);
2786 btrfs_set_file_extent_offset(leaf, item, start - new->file_pos);
2787 btrfs_set_file_extent_num_bytes(leaf, item, len);
2788 btrfs_set_file_extent_ram_bytes(leaf, item, new->len);
2789 btrfs_set_file_extent_generation(leaf, item, trans->transid);
2790 btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG);
2791 btrfs_set_file_extent_compression(leaf, item, new->compress_type);
2792 btrfs_set_file_extent_encryption(leaf, item, 0);
2793 btrfs_set_file_extent_other_encoding(leaf, item, 0);
2794
2795 btrfs_mark_buffer_dirty(leaf);
2796 inode_add_bytes(inode, len);
Liu Boa09a0a72013-03-11 09:20:58 +00002797 btrfs_release_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002798
Qu Wenruo82fa1132019-04-04 14:45:35 +08002799 btrfs_init_generic_ref(&ref, BTRFS_ADD_DELAYED_REF, new->bytenr,
2800 new->disk_len, 0);
2801 btrfs_init_data_ref(&ref, backref->root_id, backref->inum,
2802 new->file_pos); /* start - extent_offset */
2803 ret = btrfs_inc_extent_ref(trans, &ref);
Liu Bo38c227d2013-01-29 03:18:40 +00002804 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002805 btrfs_abort_transaction(trans, ret);
Liu Bo38c227d2013-01-29 03:18:40 +00002806 goto out_free_path;
2807 }
2808
2809 ret = 1;
2810out_free_path:
2811 btrfs_release_path(path);
Liu Boa09a0a72013-03-11 09:20:58 +00002812 path->leave_spinning = 0;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002813 btrfs_end_transaction(trans);
Liu Bo38c227d2013-01-29 03:18:40 +00002814out_unlock:
2815 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
David Sterbae43bbe52017-12-12 21:43:52 +01002816 &cached);
Liu Bo38c227d2013-01-29 03:18:40 +00002817 iput(inode);
2818 return ret;
2819}
2820
Liu Bo6f519562013-10-29 10:45:05 +08002821static void free_sa_defrag_extent(struct new_sa_defrag_extent *new)
2822{
2823 struct old_sa_defrag_extent *old, *tmp;
2824
2825 if (!new)
2826 return;
2827
2828 list_for_each_entry_safe(old, tmp, &new->head, list) {
Liu Bo6f519562013-10-29 10:45:05 +08002829 kfree(old);
2830 }
2831 kfree(new);
2832}
2833
Liu Bo38c227d2013-01-29 03:18:40 +00002834static void relink_file_extents(struct new_sa_defrag_extent *new)
2835{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002836 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002837 struct btrfs_path *path;
Liu Bo38c227d2013-01-29 03:18:40 +00002838 struct sa_defrag_extent_backref *backref;
2839 struct sa_defrag_extent_backref *prev = NULL;
Liu Bo38c227d2013-01-29 03:18:40 +00002840 struct rb_node *node;
2841 int ret;
2842
Liu Bo38c227d2013-01-29 03:18:40 +00002843 path = btrfs_alloc_path();
2844 if (!path)
2845 return;
2846
2847 if (!record_extent_backrefs(path, new)) {
2848 btrfs_free_path(path);
2849 goto out;
2850 }
2851 btrfs_release_path(path);
2852
2853 while (1) {
2854 node = rb_first(&new->root);
2855 if (!node)
2856 break;
2857 rb_erase(node, &new->root);
2858
2859 backref = rb_entry(node, struct sa_defrag_extent_backref, node);
2860
2861 ret = relink_extent_backref(path, prev, backref);
2862 WARN_ON(ret < 0);
2863
2864 kfree(prev);
2865
2866 if (ret == 1)
2867 prev = backref;
2868 else
2869 prev = NULL;
2870 cond_resched();
2871 }
2872 kfree(prev);
2873
2874 btrfs_free_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002875out:
Liu Bo6f519562013-10-29 10:45:05 +08002876 free_sa_defrag_extent(new);
2877
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002878 atomic_dec(&fs_info->defrag_running);
2879 wake_up(&fs_info->transaction_wait);
Liu Bo38c227d2013-01-29 03:18:40 +00002880}
2881
2882static struct new_sa_defrag_extent *
2883record_old_file_extents(struct inode *inode,
2884 struct btrfs_ordered_extent *ordered)
2885{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002886 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002887 struct btrfs_root *root = BTRFS_I(inode)->root;
2888 struct btrfs_path *path;
2889 struct btrfs_key key;
Liu Bo6f519562013-10-29 10:45:05 +08002890 struct old_sa_defrag_extent *old;
Liu Bo38c227d2013-01-29 03:18:40 +00002891 struct new_sa_defrag_extent *new;
2892 int ret;
2893
2894 new = kmalloc(sizeof(*new), GFP_NOFS);
2895 if (!new)
2896 return NULL;
2897
2898 new->inode = inode;
2899 new->file_pos = ordered->file_offset;
2900 new->len = ordered->len;
2901 new->bytenr = ordered->start;
2902 new->disk_len = ordered->disk_len;
2903 new->compress_type = ordered->compress_type;
2904 new->root = RB_ROOT;
2905 INIT_LIST_HEAD(&new->head);
2906
2907 path = btrfs_alloc_path();
2908 if (!path)
2909 goto out_kfree;
2910
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002911 key.objectid = btrfs_ino(BTRFS_I(inode));
Liu Bo38c227d2013-01-29 03:18:40 +00002912 key.type = BTRFS_EXTENT_DATA_KEY;
2913 key.offset = new->file_pos;
2914
2915 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2916 if (ret < 0)
2917 goto out_free_path;
2918 if (ret > 0 && path->slots[0] > 0)
2919 path->slots[0]--;
2920
2921 /* find out all the old extents for the file range */
2922 while (1) {
2923 struct btrfs_file_extent_item *extent;
2924 struct extent_buffer *l;
2925 int slot;
2926 u64 num_bytes;
2927 u64 offset;
2928 u64 end;
2929 u64 disk_bytenr;
2930 u64 extent_offset;
2931
2932 l = path->nodes[0];
2933 slot = path->slots[0];
2934
2935 if (slot >= btrfs_header_nritems(l)) {
2936 ret = btrfs_next_leaf(root, path);
2937 if (ret < 0)
Liu Bo6f519562013-10-29 10:45:05 +08002938 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002939 else if (ret > 0)
2940 break;
2941 continue;
2942 }
2943
2944 btrfs_item_key_to_cpu(l, &key, slot);
2945
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002946 if (key.objectid != btrfs_ino(BTRFS_I(inode)))
Liu Bo38c227d2013-01-29 03:18:40 +00002947 break;
2948 if (key.type != BTRFS_EXTENT_DATA_KEY)
2949 break;
2950 if (key.offset >= new->file_pos + new->len)
2951 break;
2952
2953 extent = btrfs_item_ptr(l, slot, struct btrfs_file_extent_item);
2954
2955 num_bytes = btrfs_file_extent_num_bytes(l, extent);
2956 if (key.offset + num_bytes < new->file_pos)
2957 goto next;
2958
2959 disk_bytenr = btrfs_file_extent_disk_bytenr(l, extent);
2960 if (!disk_bytenr)
2961 goto next;
2962
2963 extent_offset = btrfs_file_extent_offset(l, extent);
2964
2965 old = kmalloc(sizeof(*old), GFP_NOFS);
2966 if (!old)
Liu Bo6f519562013-10-29 10:45:05 +08002967 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002968
2969 offset = max(new->file_pos, key.offset);
2970 end = min(new->file_pos + new->len, key.offset + num_bytes);
2971
2972 old->bytenr = disk_bytenr;
2973 old->extent_offset = extent_offset;
2974 old->offset = offset - key.offset;
2975 old->len = end - offset;
2976 old->new = new;
2977 old->count = 0;
2978 list_add_tail(&old->list, &new->head);
2979next:
2980 path->slots[0]++;
2981 cond_resched();
2982 }
2983
2984 btrfs_free_path(path);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002985 atomic_inc(&fs_info->defrag_running);
Liu Bo38c227d2013-01-29 03:18:40 +00002986
2987 return new;
2988
Liu Bo38c227d2013-01-29 03:18:40 +00002989out_free_path:
2990 btrfs_free_path(path);
2991out_kfree:
Liu Bo6f519562013-10-29 10:45:05 +08002992 free_sa_defrag_extent(new);
Liu Bo38c227d2013-01-29 03:18:40 +00002993 return NULL;
2994}
2995
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002996static void btrfs_release_delalloc_bytes(struct btrfs_fs_info *fs_info,
Miao Xiee570fd22014-06-19 10:42:50 +08002997 u64 start, u64 len)
2998{
2999 struct btrfs_block_group_cache *cache;
3000
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003001 cache = btrfs_lookup_block_group(fs_info, start);
Miao Xiee570fd22014-06-19 10:42:50 +08003002 ASSERT(cache);
3003
3004 spin_lock(&cache->lock);
3005 cache->delalloc_bytes -= len;
3006 spin_unlock(&cache->lock);
3007
3008 btrfs_put_block_group(cache);
3009}
3010
Chris Masond352ac62008-09-29 15:18:18 -04003011/* as ordered data IO finishes, this gets called so we can finish
3012 * an ordered extent if the range of bytes in the file it covers are
3013 * fully written.
3014 */
Josef Bacik5fd02042012-05-02 14:00:54 -04003015static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
Chris Masone6dcd2d2008-07-17 12:53:50 -04003016{
Josef Bacik5fd02042012-05-02 14:00:54 -04003017 struct inode *inode = ordered_extent->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003018 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003019 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003020 struct btrfs_trans_handle *trans = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003021 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00003022 struct extent_state *cached_state = NULL;
Liu Bo38c227d2013-01-29 03:18:40 +00003023 struct new_sa_defrag_extent *new = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08003024 int compress_type = 0;
Josef Bacik77cef2e2013-08-29 13:57:21 -04003025 int ret = 0;
3026 u64 logical_len = ordered_extent->len;
Li Zefan82d59022011-04-20 10:33:24 +08003027 bool nolock;
Josef Bacik77cef2e2013-08-29 13:57:21 -04003028 bool truncated = false;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003029 bool range_locked = false;
3030 bool clear_new_delalloc_bytes = false;
Josef Bacik49940bd2018-10-11 15:54:21 -04003031 bool clear_reserved_extent = true;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003032
3033 if (!test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
3034 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags) &&
3035 !test_bit(BTRFS_ORDERED_DIRECT, &ordered_extent->flags))
3036 clear_new_delalloc_bytes = true;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003037
Nikolay Borisov70ddc552017-02-20 13:50:35 +02003038 nolock = btrfs_is_free_space_inode(BTRFS_I(inode));
Josef Bacik0cb59c92010-07-02 12:14:14 -04003039
Josef Bacik5fd02042012-05-02 14:00:54 -04003040 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
3041 ret = -EIO;
3042 goto out;
3043 }
3044
Nikolay Borisov7ab79562017-02-20 13:50:57 +02003045 btrfs_free_io_failure_record(BTRFS_I(inode),
3046 ordered_extent->file_offset,
3047 ordered_extent->file_offset +
3048 ordered_extent->len - 1);
Miao Xief6124962014-09-12 18:44:04 +08003049
Josef Bacik77cef2e2013-08-29 13:57:21 -04003050 if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) {
3051 truncated = true;
3052 logical_len = ordered_extent->truncated_len;
3053 /* Truncated the entire extent, don't bother adding */
3054 if (!logical_len)
3055 goto out;
3056 }
3057
Yan, Zhengc2167752009-11-12 09:34:21 +00003058 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003059 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
Qu Wenruo94ed9382015-09-08 17:25:56 +08003060
3061 /*
3062 * For mwrite(mmap + memset to write) case, we still reserve
3063 * space for NOCOW range.
3064 * As NOCOW won't cause a new delayed ref, just free the space
3065 */
Qu Wenruobc42bda2017-02-27 15:10:39 +08003066 btrfs_qgroup_free_data(inode, NULL, ordered_extent->file_offset,
Qu Wenruo94ed9382015-09-08 17:25:56 +08003067 ordered_extent->len);
Josef Bacik6c760c02012-11-09 10:53:21 -05003068 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
3069 if (nolock)
3070 trans = btrfs_join_transaction_nolock(root);
3071 else
3072 trans = btrfs_join_transaction(root);
3073 if (IS_ERR(trans)) {
3074 ret = PTR_ERR(trans);
3075 trans = NULL;
3076 goto out;
Yan, Zhengc2167752009-11-12 09:34:21 +00003077 }
Josef Bacik69fe2d72017-10-19 14:15:57 -04003078 trans->block_rsv = &BTRFS_I(inode)->block_rsv;
Josef Bacik6c760c02012-11-09 10:53:21 -05003079 ret = btrfs_update_inode_fallback(trans, root, inode);
3080 if (ret) /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04003081 btrfs_abort_transaction(trans, ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00003082 goto out;
3083 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04003084
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003085 range_locked = true;
Josef Bacik2ac55d42010-02-03 19:33:23 +00003086 lock_extent_bits(io_tree, ordered_extent->file_offset,
3087 ordered_extent->file_offset + ordered_extent->len - 1,
David Sterbaff13db42015-12-03 14:30:40 +01003088 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003089
Liu Bo38c227d2013-01-29 03:18:40 +00003090 ret = test_range_bit(io_tree, ordered_extent->file_offset,
3091 ordered_extent->file_offset + ordered_extent->len - 1,
Liu Bo452e62b2017-05-26 17:44:23 -06003092 EXTENT_DEFRAG, 0, cached_state);
Liu Bo38c227d2013-01-29 03:18:40 +00003093 if (ret) {
3094 u64 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
Josef Bacik8101c8d2014-01-29 16:05:30 -05003095 if (0 && last_snapshot >= BTRFS_I(inode)->generation)
Liu Bo38c227d2013-01-29 03:18:40 +00003096 /* the inode is shared */
3097 new = record_old_file_extents(inode, ordered_extent);
3098
3099 clear_extent_bit(io_tree, ordered_extent->file_offset,
3100 ordered_extent->file_offset + ordered_extent->len - 1,
David Sterbaae0f1622017-10-31 16:37:52 +01003101 EXTENT_DEFRAG, 0, 0, &cached_state);
Liu Bo38c227d2013-01-29 03:18:40 +00003102 }
3103
Josef Bacik0cb59c92010-07-02 12:14:14 -04003104 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003105 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04003106 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003107 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003108 if (IS_ERR(trans)) {
3109 ret = PTR_ERR(trans);
3110 trans = NULL;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003111 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003112 }
Chris Masona79b7d42014-05-22 16:18:52 -07003113
Josef Bacik69fe2d72017-10-19 14:15:57 -04003114 trans->block_rsv = &BTRFS_I(inode)->block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00003115
Chris Masonc8b97812008-10-29 14:49:59 -04003116 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08003117 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04003118 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08003119 BUG_ON(compress_type);
Justin Maggardb430b772017-10-30 15:29:10 -07003120 btrfs_qgroup_free_data(inode, NULL, ordered_extent->file_offset,
3121 ordered_extent->len);
Nikolay Borisov7a6d7062017-02-20 13:50:48 +02003122 ret = btrfs_mark_extent_written(trans, BTRFS_I(inode),
Yan Zhengd899e052008-10-30 14:25:28 -04003123 ordered_extent->file_offset,
3124 ordered_extent->file_offset +
Josef Bacik77cef2e2013-08-29 13:57:21 -04003125 logical_len);
Yan Zhengd899e052008-10-30 14:25:28 -04003126 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003127 BUG_ON(root == fs_info->tree_root);
Yan Zhengd899e052008-10-30 14:25:28 -04003128 ret = insert_reserved_file_extent(trans, inode,
3129 ordered_extent->file_offset,
3130 ordered_extent->start,
3131 ordered_extent->disk_len,
Josef Bacik77cef2e2013-08-29 13:57:21 -04003132 logical_len, logical_len,
Li Zefan261507a02010-12-17 14:21:50 +08003133 compress_type, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04003134 BTRFS_FILE_EXTENT_REG);
Josef Bacik49940bd2018-10-11 15:54:21 -04003135 if (!ret) {
3136 clear_reserved_extent = false;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003137 btrfs_release_delalloc_bytes(fs_info,
Miao Xiee570fd22014-06-19 10:42:50 +08003138 ordered_extent->start,
3139 ordered_extent->disk_len);
Josef Bacik49940bd2018-10-11 15:54:21 -04003140 }
Yan Zhengd899e052008-10-30 14:25:28 -04003141 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04003142 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
3143 ordered_extent->file_offset, ordered_extent->len,
3144 trans->transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003145 if (ret < 0) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003146 btrfs_abort_transaction(trans, ret);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003147 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003148 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00003149
Nikolay Borisovac01f262018-01-08 10:59:43 +02003150 ret = add_pending_csums(trans, inode, &ordered_extent->list);
3151 if (ret) {
3152 btrfs_abort_transaction(trans, ret);
3153 goto out;
3154 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04003155
Josef Bacik6c760c02012-11-09 10:53:21 -05003156 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
3157 ret = btrfs_update_inode_fallback(trans, root, inode);
3158 if (ret) { /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04003159 btrfs_abort_transaction(trans, ret);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003160 goto out;
Josef Bacik1ef30be2011-04-05 19:25:36 -04003161 }
3162 ret = 0;
Yan, Zhengc2167752009-11-12 09:34:21 +00003163out:
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003164 if (range_locked || clear_new_delalloc_bytes) {
3165 unsigned int clear_bits = 0;
3166
3167 if (range_locked)
3168 clear_bits |= EXTENT_LOCKED;
3169 if (clear_new_delalloc_bytes)
3170 clear_bits |= EXTENT_DELALLOC_NEW;
3171 clear_extent_bit(&BTRFS_I(inode)->io_tree,
3172 ordered_extent->file_offset,
3173 ordered_extent->file_offset +
3174 ordered_extent->len - 1,
3175 clear_bits,
3176 (clear_bits & EXTENT_LOCKED) ? 1 : 0,
David Sterbaae0f1622017-10-31 16:37:52 +01003177 0, &cached_state);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003178 }
3179
Miao Xiea698d0752012-09-20 01:51:59 -06003180 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003181 btrfs_end_transaction(trans);
Josef Bacik0cb59c92010-07-02 12:14:14 -04003182
Josef Bacik77cef2e2013-08-29 13:57:21 -04003183 if (ret || truncated) {
3184 u64 start, end;
3185
3186 if (truncated)
3187 start = ordered_extent->file_offset + logical_len;
3188 else
3189 start = ordered_extent->file_offset;
3190 end = ordered_extent->file_offset + ordered_extent->len - 1;
David Sterbaf08dc362017-10-31 17:02:39 +01003191 clear_extent_uptodate(io_tree, start, end, NULL);
Josef Bacik77cef2e2013-08-29 13:57:21 -04003192
3193 /* Drop the cache for the part of the extent we didn't write. */
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02003194 btrfs_drop_extent_cache(BTRFS_I(inode), start, end, 0);
Josef Bacik5fd02042012-05-02 14:00:54 -04003195
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003196 /*
3197 * If the ordered extent had an IOERR or something else went
3198 * wrong we need to return the space for this ordered extent
Josef Bacik77cef2e2013-08-29 13:57:21 -04003199 * back to the allocator. We only free the extent in the
3200 * truncated case if we didn't write out the extent at all.
Josef Bacik49940bd2018-10-11 15:54:21 -04003201 *
3202 * If we made it past insert_reserved_file_extent before we
3203 * errored out then we don't need to do this as the accounting
3204 * has already been done.
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003205 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04003206 if ((ret || !logical_len) &&
Josef Bacik49940bd2018-10-11 15:54:21 -04003207 clear_reserved_extent &&
Josef Bacik77cef2e2013-08-29 13:57:21 -04003208 !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003209 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003210 btrfs_free_reserved_extent(fs_info,
3211 ordered_extent->start,
Miao Xiee570fd22014-06-19 10:42:50 +08003212 ordered_extent->disk_len, 1);
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003213 }
3214
3215
Josef Bacik5fd02042012-05-02 14:00:54 -04003216 /*
Liu Bo8bad3c02012-06-18 12:14:23 +08003217 * This needs to be done to make sure anybody waiting knows we are done
3218 * updating everything for this ordered extent.
Josef Bacik5fd02042012-05-02 14:00:54 -04003219 */
3220 btrfs_remove_ordered_extent(inode, ordered_extent);
3221
Liu Bo38c227d2013-01-29 03:18:40 +00003222 /* for snapshot-aware defrag */
Liu Bo6f519562013-10-29 10:45:05 +08003223 if (new) {
3224 if (ret) {
3225 free_sa_defrag_extent(new);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003226 atomic_dec(&fs_info->defrag_running);
Liu Bo6f519562013-10-29 10:45:05 +08003227 } else {
3228 relink_file_extents(new);
3229 }
3230 }
Liu Bo38c227d2013-01-29 03:18:40 +00003231
Chris Masone6dcd2d2008-07-17 12:53:50 -04003232 /* once for us */
3233 btrfs_put_ordered_extent(ordered_extent);
3234 /* once for the tree */
3235 btrfs_put_ordered_extent(ordered_extent);
3236
Josef Bacik5fd02042012-05-02 14:00:54 -04003237 return ret;
3238}
3239
3240static void finish_ordered_fn(struct btrfs_work *work)
3241{
3242 struct btrfs_ordered_extent *ordered_extent;
3243 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
3244 btrfs_finish_ordered_io(ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003245}
3246
Nikolay Borisovc6297322018-11-08 10:18:08 +02003247void btrfs_writepage_endio_finish_ordered(struct page *page, u64 start,
3248 u64 end, int uptodate)
Chris Mason211f90e2008-07-18 11:56:15 -04003249{
Josef Bacik5fd02042012-05-02 14:00:54 -04003250 struct inode *inode = page->mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003251 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik5fd02042012-05-02 14:00:54 -04003252 struct btrfs_ordered_extent *ordered_extent = NULL;
Liu Bo9e0af232014-08-15 23:36:53 +08003253 struct btrfs_workqueue *wq;
Josef Bacik5fd02042012-05-02 14:00:54 -04003254
liubo1abe9b82011-03-24 11:18:59 +00003255 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
3256
Chris Mason8b62b722009-09-02 16:53:46 -04003257 ClearPagePrivate2(page);
Josef Bacik5fd02042012-05-02 14:00:54 -04003258 if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
3259 end - start + 1, uptodate))
David Sterbac3988d62017-02-17 15:18:32 +01003260 return;
Josef Bacik5fd02042012-05-02 14:00:54 -04003261
Omar Sandovala0cac0e2019-09-16 11:30:57 -07003262 if (btrfs_is_free_space_inode(BTRFS_I(inode)))
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003263 wq = fs_info->endio_freespace_worker;
Omar Sandovala0cac0e2019-09-16 11:30:57 -07003264 else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003265 wq = fs_info->endio_write_workers;
Josef Bacik5fd02042012-05-02 14:00:54 -04003266
Omar Sandovala0cac0e2019-09-16 11:30:57 -07003267 btrfs_init_work(&ordered_extent->work, finish_ordered_fn, NULL, NULL);
Liu Bo9e0af232014-08-15 23:36:53 +08003268 btrfs_queue_work(wq, &ordered_extent->work);
Chris Mason211f90e2008-07-18 11:56:15 -04003269}
3270
Miao Xiedc380ae2014-09-12 18:43:55 +08003271static int __readpage_endio_check(struct inode *inode,
3272 struct btrfs_io_bio *io_bio,
3273 int icsum, struct page *page,
3274 int pgoff, u64 start, size_t len)
3275{
Johannes Thumshirnd5178572019-06-03 16:58:57 +02003276 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
3277 SHASH_DESC_ON_STACK(shash, fs_info->csum_shash);
Miao Xiedc380ae2014-09-12 18:43:55 +08003278 char *kaddr;
Johannes Thumshirnd5178572019-06-03 16:58:57 +02003279 u16 csum_size = btrfs_super_csum_size(fs_info->super_copy);
3280 u8 *csum_expected;
3281 u8 csum[BTRFS_CSUM_SIZE];
Miao Xiedc380ae2014-09-12 18:43:55 +08003282
Johannes Thumshirnd5178572019-06-03 16:58:57 +02003283 csum_expected = ((u8 *)io_bio->csum) + icsum * csum_size;
Miao Xiedc380ae2014-09-12 18:43:55 +08003284
3285 kaddr = kmap_atomic(page);
Johannes Thumshirnd5178572019-06-03 16:58:57 +02003286 shash->tfm = fs_info->csum_shash;
3287
3288 crypto_shash_init(shash);
3289 crypto_shash_update(shash, kaddr + pgoff, len);
3290 crypto_shash_final(shash, csum);
3291
3292 if (memcmp(csum, csum_expected, csum_size))
Miao Xiedc380ae2014-09-12 18:43:55 +08003293 goto zeroit;
3294
3295 kunmap_atomic(kaddr);
3296 return 0;
3297zeroit:
Johannes Thumshirnea41d6b2019-06-03 16:58:58 +02003298 btrfs_print_data_csum_error(BTRFS_I(inode), start, csum, csum_expected,
3299 io_bio->mirror_num);
Miao Xiedc380ae2014-09-12 18:43:55 +08003300 memset(kaddr + pgoff, 1, len);
3301 flush_dcache_page(page);
3302 kunmap_atomic(kaddr);
Miao Xiedc380ae2014-09-12 18:43:55 +08003303 return -EIO;
3304}
3305
Chris Masond352ac62008-09-29 15:18:18 -04003306/*
Chris Masond352ac62008-09-29 15:18:18 -04003307 * when reads are done, we need to check csums to verify the data is correct
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02003308 * if there's a match, we allow the bio to finish. If not, the code in
3309 * extent_io.c will try to find good copies for us.
Chris Masond352ac62008-09-29 15:18:18 -04003310 */
Miao Xiefacc8a222013-07-25 19:22:34 +08003311static int btrfs_readpage_end_io_hook(struct btrfs_io_bio *io_bio,
3312 u64 phy_offset, struct page *page,
3313 u64 start, u64 end, int mirror)
Chris Mason07157aa2007-08-30 08:50:51 -04003314{
Miao Xie4eee4fa2012-12-21 09:17:45 +00003315 size_t offset = start - page_offset(page);
Chris Mason07157aa2007-08-30 08:50:51 -04003316 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05003317 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masonff79f812007-10-15 16:22:25 -04003318 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -05003319
Chris Masond20f7042008-12-08 16:58:54 -05003320 if (PageChecked(page)) {
3321 ClearPageChecked(page);
Miao Xiedc380ae2014-09-12 18:43:55 +08003322 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003323 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003324
3325 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Miao Xiedc380ae2014-09-12 18:43:55 +08003326 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003327
Yan Zheng17d217f2008-12-12 10:03:38 -05003328 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04003329 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
David Sterba91166212016-04-26 23:54:39 +02003330 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM);
Yan Zheng17d217f2008-12-12 10:03:38 -05003331 return 0;
3332 }
3333
Miao Xiefacc8a222013-07-25 19:22:34 +08003334 phy_offset >>= inode->i_sb->s_blocksize_bits;
Miao Xiedc380ae2014-09-12 18:43:55 +08003335 return __readpage_endio_check(inode, io_bio, phy_offset, page, offset,
3336 start, (size_t)(end - start + 1));
Chris Mason07157aa2007-08-30 08:50:51 -04003337}
Chris Masonb888db22007-08-27 16:49:44 -04003338
Nikolay Borisovc1c3fac2018-01-16 09:31:58 +02003339/*
3340 * btrfs_add_delayed_iput - perform a delayed iput on @inode
3341 *
3342 * @inode: The inode we want to perform iput on
3343 *
3344 * This function uses the generic vfs_inode::i_count to track whether we should
3345 * just decrement it (in case it's > 1) or if this is the last iput then link
3346 * the inode to the delayed iput machinery. Delayed iputs are processed at
3347 * transaction commit time/superblock commit/cleaner kthread.
3348 */
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003349void btrfs_add_delayed_iput(struct inode *inode)
3350{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003351 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
David Sterba8089fe62015-11-19 14:15:51 +01003352 struct btrfs_inode *binode = BTRFS_I(inode);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003353
3354 if (atomic_add_unless(&inode->i_count, -1, 1))
3355 return;
3356
Josef Bacik034f7842018-12-03 11:06:52 -05003357 atomic_inc(&fs_info->nr_delayed_iputs);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003358 spin_lock(&fs_info->delayed_iput_lock);
Nikolay Borisovc1c3fac2018-01-16 09:31:58 +02003359 ASSERT(list_empty(&binode->delayed_iput));
3360 list_add_tail(&binode->delayed_iput, &fs_info->delayed_iputs);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003361 spin_unlock(&fs_info->delayed_iput_lock);
Josef Bacikfd340d02019-01-11 10:21:02 -05003362 if (!test_bit(BTRFS_FS_CLEANER_RUNNING, &fs_info->flags))
3363 wake_up_process(fs_info->cleaner_kthread);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003364}
3365
Josef Bacik63611e72019-06-18 10:59:18 -04003366static void run_delayed_iput_locked(struct btrfs_fs_info *fs_info,
3367 struct btrfs_inode *inode)
3368{
3369 list_del_init(&inode->delayed_iput);
3370 spin_unlock(&fs_info->delayed_iput_lock);
3371 iput(&inode->vfs_inode);
3372 if (atomic_dec_and_test(&fs_info->nr_delayed_iputs))
3373 wake_up(&fs_info->delayed_iputs_wait);
3374 spin_lock(&fs_info->delayed_iput_lock);
3375}
3376
3377static void btrfs_run_delayed_iput(struct btrfs_fs_info *fs_info,
3378 struct btrfs_inode *inode)
3379{
3380 if (!list_empty(&inode->delayed_iput)) {
3381 spin_lock(&fs_info->delayed_iput_lock);
3382 if (!list_empty(&inode->delayed_iput))
3383 run_delayed_iput_locked(fs_info, inode);
3384 spin_unlock(&fs_info->delayed_iput_lock);
3385 }
3386}
3387
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003388void btrfs_run_delayed_iputs(struct btrfs_fs_info *fs_info)
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003389{
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003390
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003391 spin_lock(&fs_info->delayed_iput_lock);
David Sterba8089fe62015-11-19 14:15:51 +01003392 while (!list_empty(&fs_info->delayed_iputs)) {
3393 struct btrfs_inode *inode;
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003394
David Sterba8089fe62015-11-19 14:15:51 +01003395 inode = list_first_entry(&fs_info->delayed_iputs,
3396 struct btrfs_inode, delayed_iput);
Josef Bacik63611e72019-06-18 10:59:18 -04003397 run_delayed_iput_locked(fs_info, inode);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003398 }
David Sterba8089fe62015-11-19 14:15:51 +01003399 spin_unlock(&fs_info->delayed_iput_lock);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003400}
3401
Josef Bacik034f7842018-12-03 11:06:52 -05003402/**
3403 * btrfs_wait_on_delayed_iputs - wait on the delayed iputs to be done running
3404 * @fs_info - the fs_info for this fs
3405 * @return - EINTR if we were killed, 0 if nothing's pending
3406 *
3407 * This will wait on any delayed iputs that are currently running with KILLABLE
3408 * set. Once they are all done running we will return, unless we are killed in
3409 * which case we return EINTR. This helps in user operations like fallocate etc
3410 * that might get blocked on the iputs.
3411 */
3412int btrfs_wait_on_delayed_iputs(struct btrfs_fs_info *fs_info)
3413{
3414 int ret = wait_event_killable(fs_info->delayed_iputs_wait,
3415 atomic_read(&fs_info->nr_delayed_iputs) == 0);
3416 if (ret)
3417 return -EINTR;
3418 return 0;
3419}
3420
Yan, Zhengd68fc572010-05-16 10:49:58 -04003421/*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003422 * This creates an orphan entry for the given inode in case something goes wrong
3423 * in the middle of an unlink.
Josef Bacik7b128762008-07-24 12:17:14 -04003424 */
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003425int btrfs_orphan_add(struct btrfs_trans_handle *trans,
Omar Sandoval27919062018-05-11 13:13:37 -07003426 struct btrfs_inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003427{
Yan, Zhengd68fc572010-05-16 10:49:58 -04003428 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003429
Omar Sandoval27919062018-05-11 13:13:37 -07003430 ret = btrfs_insert_orphan_item(trans, inode->root, btrfs_ino(inode));
3431 if (ret && ret != -EEXIST) {
3432 btrfs_abort_transaction(trans, ret);
3433 return ret;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003434 }
3435
Yan, Zhengd68fc572010-05-16 10:49:58 -04003436 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003437}
3438
3439/*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003440 * We have done the delete so we can go ahead and remove the orphan item for
3441 * this particular inode.
Josef Bacik7b128762008-07-24 12:17:14 -04003442 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00003443static int btrfs_orphan_del(struct btrfs_trans_handle *trans,
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003444 struct btrfs_inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003445{
Omar Sandoval27919062018-05-11 13:13:37 -07003446 return btrfs_del_orphan_item(trans, inode->root, btrfs_ino(inode));
Josef Bacik7b128762008-07-24 12:17:14 -04003447}
3448
3449/*
3450 * this cleans up any orphans that may be left on the list from the last use
3451 * of this root.
3452 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003453int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04003454{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003455 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik7b128762008-07-24 12:17:14 -04003456 struct btrfs_path *path;
3457 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04003458 struct btrfs_key key, found_key;
3459 struct btrfs_trans_handle *trans;
3460 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003461 u64 last_objectid = 0;
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003462 int ret = 0, nr_unlink = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003463
Yan, Zhengd68fc572010-05-16 10:49:58 -04003464 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003465 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00003466
3467 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003468 if (!path) {
3469 ret = -ENOMEM;
3470 goto out;
3471 }
David Sterbae4058b52015-11-27 16:31:35 +01003472 path->reada = READA_BACK;
Josef Bacik7b128762008-07-24 12:17:14 -04003473
3474 key.objectid = BTRFS_ORPHAN_OBJECTID;
David Sterba962a2982014-06-04 18:41:45 +02003475 key.type = BTRFS_ORPHAN_ITEM_KEY;
Josef Bacik7b128762008-07-24 12:17:14 -04003476 key.offset = (u64)-1;
3477
Josef Bacik7b128762008-07-24 12:17:14 -04003478 while (1) {
3479 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003480 if (ret < 0)
3481 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003482
3483 /*
3484 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003485 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04003486 * find the key and see if we have stuff that matches
3487 */
3488 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003489 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003490 if (path->slots[0] == 0)
3491 break;
3492 path->slots[0]--;
3493 }
3494
3495 /* pull out the item */
3496 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04003497 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3498
3499 /* make sure the item matches what we want */
3500 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
3501 break;
David Sterba962a2982014-06-04 18:41:45 +02003502 if (found_key.type != BTRFS_ORPHAN_ITEM_KEY)
Josef Bacik7b128762008-07-24 12:17:14 -04003503 break;
3504
3505 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02003506 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04003507
3508 /*
3509 * this is where we are basically btrfs_lookup, without the
3510 * crossing root thing. we store the inode number in the
3511 * offset of the orphan item.
3512 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003513
3514 if (found_key.offset == last_objectid) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003515 btrfs_err(fs_info,
3516 "Error removing orphan entry, stopping orphan cleanup");
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003517 ret = -EINVAL;
3518 goto out;
3519 }
3520
3521 last_objectid = found_key.offset;
3522
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003523 found_key.objectid = found_key.offset;
3524 found_key.type = BTRFS_INODE_ITEM_KEY;
3525 found_key.offset = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003526 inode = btrfs_iget(fs_info->sb, &found_key, root, NULL);
Rusty Russell8c6ffba2013-07-15 11:20:32 +09303527 ret = PTR_ERR_OR_ZERO(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003528 if (ret && ret != -ENOENT)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003529 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003530
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003531 if (ret == -ENOENT && root == fs_info->tree_root) {
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003532 struct btrfs_root *dead_root;
3533 struct btrfs_fs_info *fs_info = root->fs_info;
3534 int is_dead_root = 0;
3535
3536 /*
3537 * this is an orphan in the tree root. Currently these
3538 * could come from 2 sources:
3539 * a) a snapshot deletion in progress
3540 * b) a free space cache inode
3541 * We need to distinguish those two, as the snapshot
3542 * orphan must not get deleted.
3543 * find_dead_roots already ran before us, so if this
3544 * is a snapshot deletion, we should find the root
3545 * in the dead_roots list
3546 */
3547 spin_lock(&fs_info->trans_lock);
3548 list_for_each_entry(dead_root, &fs_info->dead_roots,
3549 root_list) {
3550 if (dead_root->root_key.objectid ==
3551 found_key.objectid) {
3552 is_dead_root = 1;
3553 break;
3554 }
3555 }
3556 spin_unlock(&fs_info->trans_lock);
3557 if (is_dead_root) {
3558 /* prevent this orphan from being found again */
3559 key.offset = found_key.objectid - 1;
3560 continue;
3561 }
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003562
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003563 }
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003564
Josef Bacika8c9e572011-09-21 16:55:59 -04003565 /*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003566 * If we have an inode with links, there are a couple of
3567 * possibilities. Old kernels (before v3.12) used to create an
3568 * orphan item for truncate indicating that there were possibly
3569 * extent items past i_size that needed to be deleted. In v3.12,
3570 * truncate was changed to update i_size in sync with the extent
3571 * items, but the (useless) orphan item was still created. Since
3572 * v4.18, we don't create the orphan item for truncate at all.
3573 *
3574 * So, this item could mean that we need to do a truncate, but
3575 * only if this filesystem was last used on a pre-v3.12 kernel
3576 * and was not cleanly unmounted. The odds of that are quite
3577 * slim, and it's a pain to do the truncate now, so just delete
3578 * the orphan item.
3579 *
3580 * It's also possible that this orphan item was supposed to be
3581 * deleted but wasn't. The inode number may have been reused,
3582 * but either way, we can delete the orphan item.
Josef Bacika8c9e572011-09-21 16:55:59 -04003583 */
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003584 if (ret == -ENOENT || inode->i_nlink) {
3585 if (!ret)
3586 iput(inode);
Josef Bacika8c9e572011-09-21 16:55:59 -04003587 trans = btrfs_start_transaction(root, 1);
3588 if (IS_ERR(trans)) {
3589 ret = PTR_ERR(trans);
3590 goto out;
3591 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003592 btrfs_debug(fs_info, "auto deleting %Lu",
3593 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003594 ret = btrfs_del_orphan_item(trans, root,
3595 found_key.objectid);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003596 btrfs_end_transaction(trans);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003597 if (ret)
3598 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003599 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003600 }
Josef Bacik7b128762008-07-24 12:17:14 -04003601
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003602 nr_unlink++;
Josef Bacik7b128762008-07-24 12:17:14 -04003603
3604 /* this will do delete_inode and everything for us */
3605 iput(inode);
3606 }
Miao Xie3254c872011-11-10 20:45:05 -05003607 /* release the path since we're done with it */
3608 btrfs_release_path(path);
3609
Yan, Zhengd68fc572010-05-16 10:49:58 -04003610 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
3611
Omar Sandovala575cee2018-05-11 13:13:38 -07003612 if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003613 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003614 if (!IS_ERR(trans))
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003615 btrfs_end_transaction(trans);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003616 }
Josef Bacik7b128762008-07-24 12:17:14 -04003617
3618 if (nr_unlink)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003619 btrfs_debug(fs_info, "unlinked %d orphans", nr_unlink);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003620
3621out:
3622 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003623 btrfs_err(fs_info, "could not do orphan cleanup %d", ret);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003624 btrfs_free_path(path);
3625 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003626}
3627
Chris Masond352ac62008-09-29 15:18:18 -04003628/*
Chris Mason46a53cc2009-04-27 11:47:50 -04003629 * very simple check to peek ahead in the leaf looking for xattrs. If we
3630 * don't find any xattrs, we know there can't be any acls.
3631 *
3632 * slot is the slot the inode is in, objectid is the objectid of the inode
3633 */
3634static noinline int acls_after_inode_item(struct extent_buffer *leaf,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003635 int slot, u64 objectid,
3636 int *first_xattr_slot)
Chris Mason46a53cc2009-04-27 11:47:50 -04003637{
3638 u32 nritems = btrfs_header_nritems(leaf);
3639 struct btrfs_key found_key;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003640 static u64 xattr_access = 0;
3641 static u64 xattr_default = 0;
Chris Mason46a53cc2009-04-27 11:47:50 -04003642 int scanned = 0;
3643
Josef Bacikf23b5a52013-06-19 10:16:26 -04003644 if (!xattr_access) {
Andreas Gruenbacher97d79292015-12-02 14:44:35 +01003645 xattr_access = btrfs_name_hash(XATTR_NAME_POSIX_ACL_ACCESS,
3646 strlen(XATTR_NAME_POSIX_ACL_ACCESS));
3647 xattr_default = btrfs_name_hash(XATTR_NAME_POSIX_ACL_DEFAULT,
3648 strlen(XATTR_NAME_POSIX_ACL_DEFAULT));
Josef Bacikf23b5a52013-06-19 10:16:26 -04003649 }
3650
Chris Mason46a53cc2009-04-27 11:47:50 -04003651 slot++;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003652 *first_xattr_slot = -1;
Chris Mason46a53cc2009-04-27 11:47:50 -04003653 while (slot < nritems) {
3654 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3655
3656 /* we found a different objectid, there must not be acls */
3657 if (found_key.objectid != objectid)
3658 return 0;
3659
3660 /* we found an xattr, assume we've got an acl */
Josef Bacikf23b5a52013-06-19 10:16:26 -04003661 if (found_key.type == BTRFS_XATTR_ITEM_KEY) {
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003662 if (*first_xattr_slot == -1)
3663 *first_xattr_slot = slot;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003664 if (found_key.offset == xattr_access ||
3665 found_key.offset == xattr_default)
3666 return 1;
3667 }
Chris Mason46a53cc2009-04-27 11:47:50 -04003668
3669 /*
3670 * we found a key greater than an xattr key, there can't
3671 * be any acls later on
3672 */
3673 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
3674 return 0;
3675
3676 slot++;
3677 scanned++;
3678
3679 /*
3680 * it goes inode, inode backrefs, xattrs, extents,
3681 * so if there are a ton of hard links to an inode there can
3682 * be a lot of backrefs. Don't waste time searching too hard,
3683 * this is just an optimization
3684 */
3685 if (scanned >= 8)
3686 break;
3687 }
3688 /* we hit the end of the leaf before we found an xattr or
3689 * something larger than an xattr. We have to assume the inode
3690 * has acls
3691 */
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003692 if (*first_xattr_slot == -1)
3693 *first_xattr_slot = slot;
Chris Mason46a53cc2009-04-27 11:47:50 -04003694 return 1;
3695}
3696
3697/*
Chris Masond352ac62008-09-29 15:18:18 -04003698 * read an inode from the btree into the in-memory inode
3699 */
Filipe Manana4222ea72018-10-24 10:13:03 +01003700static int btrfs_read_locked_inode(struct inode *inode,
3701 struct btrfs_path *in_path)
Chris Mason39279cc2007-06-12 06:35:45 -04003702{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003703 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Filipe Manana4222ea72018-10-24 10:13:03 +01003704 struct btrfs_path *path = in_path;
Chris Mason5f39d392007-10-15 16:14:19 -04003705 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003706 struct btrfs_inode_item *inode_item;
3707 struct btrfs_root *root = BTRFS_I(inode)->root;
3708 struct btrfs_key location;
Miao Xie67de1172013-12-26 13:07:06 +08003709 unsigned long ptr;
Chris Mason46a53cc2009-04-27 11:47:50 -04003710 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04003711 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04003712 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003713 bool filled = false;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003714 int first_xattr_slot;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003715
3716 ret = btrfs_fill_inode(inode, &rdev);
3717 if (!ret)
3718 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04003719
Filipe Manana4222ea72018-10-24 10:13:03 +01003720 if (!path) {
3721 path = btrfs_alloc_path();
3722 if (!path)
3723 return -ENOMEM;
3724 }
Mark Fasheh1748f842011-07-12 11:25:31 -07003725
Chris Mason39279cc2007-06-12 06:35:45 -04003726 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05003727
Chris Mason39279cc2007-06-12 06:35:45 -04003728 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Filipe Manana67710892016-06-06 11:51:25 +01003729 if (ret) {
Filipe Manana4222ea72018-10-24 10:13:03 +01003730 if (path != in_path)
3731 btrfs_free_path(path);
Al Virof5b3a412018-07-29 23:04:51 +01003732 return ret;
Filipe Manana67710892016-06-06 11:51:25 +01003733 }
Chris Mason39279cc2007-06-12 06:35:45 -04003734
Chris Mason5f39d392007-10-15 16:14:19 -04003735 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00003736
3737 if (filled)
Miao Xie67de1172013-12-26 13:07:06 +08003738 goto cache_index;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003739
Chris Mason5f39d392007-10-15 16:14:19 -04003740 inode_item = btrfs_item_ptr(leaf, path->slots[0],
3741 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003742 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02003743 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08003744 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
3745 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02003746 btrfs_i_size_write(BTRFS_I(inode), btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04003747
David Sterbaa937b972014-12-12 17:39:12 +01003748 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->atime);
3749 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->atime);
Chris Mason5f39d392007-10-15 16:14:19 -04003750
David Sterbaa937b972014-12-12 17:39:12 +01003751 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->mtime);
3752 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->mtime);
Chris Mason5f39d392007-10-15 16:14:19 -04003753
David Sterbaa937b972014-12-12 17:39:12 +01003754 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->ctime);
3755 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->ctime);
Chris Mason5f39d392007-10-15 16:14:19 -04003756
chandan r9cc97d62012-07-04 12:48:07 +05303757 BTRFS_I(inode)->i_otime.tv_sec =
3758 btrfs_timespec_sec(leaf, &inode_item->otime);
3759 BTRFS_I(inode)->i_otime.tv_nsec =
3760 btrfs_timespec_nsec(leaf, &inode_item->otime);
Chris Mason5f39d392007-10-15 16:14:19 -04003761
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003762 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003763 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003764 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
3765
Jeff Laytonc7f88c42017-12-11 06:35:12 -05003766 inode_set_iversion_queried(inode,
3767 btrfs_inode_sequence(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003768 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04003769 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003770 rdev = btrfs_inode_rdev(leaf, inode_item);
3771
Josef Bacikaec74772008-07-24 12:12:38 -04003772 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05003773 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Miao Xie67de1172013-12-26 13:07:06 +08003774
3775cache_index:
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003776 /*
3777 * If we were modified in the current generation and evicted from memory
3778 * and then re-read we need to do a full sync since we don't have any
3779 * idea about which extents were modified before we were evicted from
3780 * cache.
3781 *
3782 * This is required for both inode re-read from disk and delayed inode
3783 * in delayed_nodes_tree.
3784 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003785 if (BTRFS_I(inode)->last_trans == fs_info->generation)
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003786 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3787 &BTRFS_I(inode)->runtime_flags);
3788
Filipe Mananabde6c242015-07-24 00:00:19 +01003789 /*
3790 * We don't persist the id of the transaction where an unlink operation
3791 * against the inode was last made. So here we assume the inode might
3792 * have been evicted, and therefore the exact value of last_unlink_trans
3793 * lost, and set it to last_trans to avoid metadata inconsistencies
3794 * between the inode and its parent if the inode is fsync'ed and the log
3795 * replayed. For example, in the scenario:
3796 *
3797 * touch mydir/foo
3798 * ln mydir/foo mydir/bar
3799 * sync
3800 * unlink mydir/bar
3801 * echo 2 > /proc/sys/vm/drop_caches # evicts inode
3802 * xfs_io -c fsync mydir/foo
3803 * <power failure>
3804 * mount fs, triggers fsync log replay
3805 *
3806 * We must make sure that when we fsync our inode foo we also log its
3807 * parent inode, otherwise after log replay the parent still has the
3808 * dentry with the "bar" name but our inode foo has a link count of 1
3809 * and doesn't have an inode ref with the name "bar" anymore.
3810 *
3811 * Setting last_unlink_trans to last_trans is a pessimistic approach,
Nicholas D Steeves01327612016-05-19 21:18:45 -04003812 * but it guarantees correctness at the expense of occasional full
Filipe Mananabde6c242015-07-24 00:00:19 +01003813 * transaction commits on fsync if our inode is a directory, or if our
3814 * inode is not a directory, logging its parent unnecessarily.
3815 */
3816 BTRFS_I(inode)->last_unlink_trans = BTRFS_I(inode)->last_trans;
3817
Miao Xie67de1172013-12-26 13:07:06 +08003818 path->slots[0]++;
3819 if (inode->i_nlink != 1 ||
3820 path->slots[0] >= btrfs_header_nritems(leaf))
3821 goto cache_acl;
3822
3823 btrfs_item_key_to_cpu(leaf, &location, path->slots[0]);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003824 if (location.objectid != btrfs_ino(BTRFS_I(inode)))
Miao Xie67de1172013-12-26 13:07:06 +08003825 goto cache_acl;
3826
3827 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3828 if (location.type == BTRFS_INODE_REF_KEY) {
3829 struct btrfs_inode_ref *ref;
3830
3831 ref = (struct btrfs_inode_ref *)ptr;
3832 BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref);
3833 } else if (location.type == BTRFS_INODE_EXTREF_KEY) {
3834 struct btrfs_inode_extref *extref;
3835
3836 extref = (struct btrfs_inode_extref *)ptr;
3837 BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf,
3838 extref);
3839 }
Miao Xie2f7e33d2011-06-23 07:27:13 +00003840cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04003841 /*
3842 * try to precache a NULL acl entry for files that don't have
3843 * any xattrs or acls
3844 */
Li Zefan33345d012011-04-20 10:31:50 +08003845 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
David Sterbaf85b7372017-01-20 14:54:07 +01003846 btrfs_ino(BTRFS_I(inode)), &first_xattr_slot);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003847 if (first_xattr_slot != -1) {
3848 path->slots[0] = first_xattr_slot;
3849 ret = btrfs_load_inode_props(inode, path);
3850 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003851 btrfs_err(fs_info,
David Sterba351fd352014-05-15 16:48:20 +02003852 "error loading props for ino %llu (root %llu): %d",
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003853 btrfs_ino(BTRFS_I(inode)),
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003854 root->root_key.objectid, ret);
3855 }
Filipe Manana4222ea72018-10-24 10:13:03 +01003856 if (path != in_path)
3857 btrfs_free_path(path);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003858
Al Viro72c04902009-06-24 16:58:48 -04003859 if (!maybe_acls)
3860 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04003861
Chris Mason39279cc2007-06-12 06:35:45 -04003862 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04003863 case S_IFREG:
3864 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masond1310b22008-01-24 16:13:08 -05003865 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04003866 inode->i_fop = &btrfs_file_operations;
3867 inode->i_op = &btrfs_file_inode_operations;
3868 break;
3869 case S_IFDIR:
3870 inode->i_fop = &btrfs_dir_file_operations;
Omar Sandoval67ade052017-01-25 17:06:38 -08003871 inode->i_op = &btrfs_dir_inode_operations;
Chris Mason39279cc2007-06-12 06:35:45 -04003872 break;
3873 case S_IFLNK:
3874 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -05003875 inode_nohighmem(inode);
Omar Sandoval4779cc02018-09-24 15:16:55 -07003876 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason39279cc2007-06-12 06:35:45 -04003877 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04003878 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05003879 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04003880 init_special_inode(inode, inode->i_mode, rdev);
3881 break;
Chris Mason39279cc2007-06-12 06:35:45 -04003882 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003883
David Sterba7b6a2212018-03-26 18:40:21 +02003884 btrfs_sync_inode_flags_to_i_flags(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003885 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003886}
3887
Chris Masond352ac62008-09-29 15:18:18 -04003888/*
3889 * given a leaf and an inode, copy the inode fields into the leaf
3890 */
Chris Masone02119d2008-09-05 16:13:11 -04003891static void fill_inode_item(struct btrfs_trans_handle *trans,
3892 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04003893 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04003894 struct inode *inode)
3895{
Liu Bo51fab692012-12-27 09:01:21 +00003896 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003897
David Sterbac82f8232019-08-09 17:48:21 +02003898 btrfs_init_map_token(&token, leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04003899
Liu Bo51fab692012-12-27 09:01:21 +00003900 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
3901 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
3902 btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size,
3903 &token);
3904 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3905 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003906
David Sterbaa937b972014-12-12 17:39:12 +01003907 btrfs_set_token_timespec_sec(leaf, &item->atime,
Liu Bo51fab692012-12-27 09:01:21 +00003908 inode->i_atime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003909 btrfs_set_token_timespec_nsec(leaf, &item->atime,
Liu Bo51fab692012-12-27 09:01:21 +00003910 inode->i_atime.tv_nsec, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003911
David Sterbaa937b972014-12-12 17:39:12 +01003912 btrfs_set_token_timespec_sec(leaf, &item->mtime,
Liu Bo51fab692012-12-27 09:01:21 +00003913 inode->i_mtime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003914 btrfs_set_token_timespec_nsec(leaf, &item->mtime,
Liu Bo51fab692012-12-27 09:01:21 +00003915 inode->i_mtime.tv_nsec, &token);
3916
David Sterbaa937b972014-12-12 17:39:12 +01003917 btrfs_set_token_timespec_sec(leaf, &item->ctime,
Liu Bo51fab692012-12-27 09:01:21 +00003918 inode->i_ctime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003919 btrfs_set_token_timespec_nsec(leaf, &item->ctime,
Liu Bo51fab692012-12-27 09:01:21 +00003920 inode->i_ctime.tv_nsec, &token);
3921
chandan r9cc97d62012-07-04 12:48:07 +05303922 btrfs_set_token_timespec_sec(leaf, &item->otime,
3923 BTRFS_I(inode)->i_otime.tv_sec, &token);
3924 btrfs_set_token_timespec_nsec(leaf, &item->otime,
3925 BTRFS_I(inode)->i_otime.tv_nsec, &token);
3926
Liu Bo51fab692012-12-27 09:01:21 +00003927 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3928 &token);
3929 btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation,
3930 &token);
Jeff Laytonc7f88c42017-12-11 06:35:12 -05003931 btrfs_set_token_inode_sequence(leaf, item, inode_peek_iversion(inode),
3932 &token);
Liu Bo51fab692012-12-27 09:01:21 +00003933 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
3934 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3935 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3936 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
Chris Mason39279cc2007-06-12 06:35:45 -04003937}
3938
Chris Masond352ac62008-09-29 15:18:18 -04003939/*
3940 * copy everything in the in-memory inode into the btree.
3941 */
Chris Mason21151332011-11-10 20:39:08 -05003942static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Chris Masond3977122009-01-05 21:25:51 -05003943 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003944{
3945 struct btrfs_inode_item *inode_item;
3946 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003947 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003948 int ret;
3949
3950 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08003951 if (!path)
3952 return -ENOMEM;
3953
Chris Masonb9473432009-03-13 11:00:37 -04003954 path->leave_spinning = 1;
Miao Xie16cdcec2011-04-22 18:12:22 +08003955 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
3956 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003957 if (ret) {
3958 if (ret > 0)
3959 ret = -ENOENT;
3960 goto failed;
3961 }
3962
Chris Mason5f39d392007-10-15 16:14:19 -04003963 leaf = path->nodes[0];
3964 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08003965 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04003966
Chris Masone02119d2008-09-05 16:13:11 -04003967 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04003968 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04003969 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003970 ret = 0;
3971failed:
Chris Mason39279cc2007-06-12 06:35:45 -04003972 btrfs_free_path(path);
3973 return ret;
3974}
3975
Chris Masond352ac62008-09-29 15:18:18 -04003976/*
Chris Mason21151332011-11-10 20:39:08 -05003977 * copy everything in the in-memory inode into the btree.
3978 */
3979noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
3980 struct btrfs_root *root, struct inode *inode)
3981{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003982 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason21151332011-11-10 20:39:08 -05003983 int ret;
3984
3985 /*
3986 * If the inode is a free space inode, we can deadlock during commit
3987 * if we put it into the delayed code.
3988 *
3989 * The data relocation inode should also be directly updated
3990 * without delay
3991 */
Nikolay Borisov70ddc552017-02-20 13:50:35 +02003992 if (!btrfs_is_free_space_inode(BTRFS_I(inode))
Josef Bacik1d52c782014-09-18 11:30:44 -04003993 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003994 && !test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) {
Alexander Block8ea05e32012-07-25 17:35:53 +02003995 btrfs_update_root_times(trans, root);
3996
Chris Mason21151332011-11-10 20:39:08 -05003997 ret = btrfs_delayed_update_inode(trans, root, inode);
3998 if (!ret)
3999 btrfs_set_inode_last_trans(trans, inode);
4000 return ret;
4001 }
4002
4003 return btrfs_update_inode_item(trans, root, inode);
4004}
4005
Josef Bacikbe6aef62012-10-22 15:43:12 -04004006noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
4007 struct btrfs_root *root,
4008 struct inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05004009{
4010 int ret;
4011
4012 ret = btrfs_update_inode(trans, root, inode);
4013 if (ret == -ENOSPC)
4014 return btrfs_update_inode_item(trans, root, inode);
4015 return ret;
4016}
4017
4018/*
Chris Masond352ac62008-09-29 15:18:18 -04004019 * unlink helper that gets used here in inode.c and in the tree logging
4020 * recovery code. It remove a link in a directory with a given name, and
4021 * also drops the back refs in the inode to the directory
4022 */
Al Viro92986792011-03-04 17:14:37 +00004023static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
4024 struct btrfs_root *root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004025 struct btrfs_inode *dir,
4026 struct btrfs_inode *inode,
Al Viro92986792011-03-04 17:14:37 +00004027 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04004028{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004029 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04004030 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04004031 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004032 struct btrfs_dir_item *di;
Josef Bacikaec74772008-07-24 12:12:38 -04004033 u64 index;
Li Zefan33345d012011-04-20 10:31:50 +08004034 u64 ino = btrfs_ino(inode);
4035 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04004036
4037 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04004038 if (!path) {
4039 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00004040 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04004041 }
4042
Chris Masonb9473432009-03-13 11:00:37 -04004043 path->leave_spinning = 1;
Li Zefan33345d012011-04-20 10:31:50 +08004044 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04004045 name, name_len, -1);
Liu Bo3cf50682018-09-12 06:06:26 +08004046 if (IS_ERR_OR_NULL(di)) {
4047 ret = di ? PTR_ERR(di) : -ENOENT;
Chris Mason39279cc2007-06-12 06:35:45 -04004048 goto err;
4049 }
Chris Mason39279cc2007-06-12 06:35:45 -04004050 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04004051 if (ret)
4052 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02004053 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04004054
Miao Xie67de1172013-12-26 13:07:06 +08004055 /*
4056 * If we don't have dir index, we have to get it by looking up
4057 * the inode ref, since we get the inode ref, remove it directly,
4058 * it is unnecessary to do delayed deletion.
4059 *
4060 * But if we have dir index, needn't search inode ref to get it.
4061 * Since the inode ref is close to the inode item, it is better
4062 * that we delay to delete it, and just do this deletion when
4063 * we update the inode item.
4064 */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004065 if (inode->dir_index) {
Miao Xie67de1172013-12-26 13:07:06 +08004066 ret = btrfs_delayed_delete_inode_ref(inode);
4067 if (!ret) {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004068 index = inode->dir_index;
Miao Xie67de1172013-12-26 13:07:06 +08004069 goto skip_backref;
4070 }
4071 }
4072
Li Zefan33345d012011-04-20 10:31:50 +08004073 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
4074 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04004075 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004076 btrfs_info(fs_info,
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00004077 "failed to delete reference to %.*s, inode %llu parent %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02004078 name_len, name, ino, dir_ino);
Jeff Mahoney66642832016-06-10 18:19:25 -04004079 btrfs_abort_transaction(trans, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04004080 goto err;
4081 }
Miao Xie67de1172013-12-26 13:07:06 +08004082skip_backref:
Lu Fengqi9add2942018-08-01 11:32:26 +08004083 ret = btrfs_delete_delayed_dir_index(trans, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004084 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004085 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04004086 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004087 }
Chris Mason39279cc2007-06-12 06:35:45 -04004088
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004089 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len, inode,
4090 dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004091 if (ret != 0 && ret != -ENOENT) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004092 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004093 goto err;
4094 }
Chris Masone02119d2008-09-05 16:13:11 -04004095
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004096 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len, dir,
4097 index);
Chris Mason6418c962010-10-30 07:34:24 -04004098 if (ret == -ENOENT)
4099 ret = 0;
Zach Brownd4e39912013-04-02 21:02:16 +00004100 else if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004101 btrfs_abort_transaction(trans, ret);
Josef Bacik63611e72019-06-18 10:59:18 -04004102
4103 /*
4104 * If we have a pending delayed iput we could end up with the final iput
4105 * being run in btrfs-cleaner context. If we have enough of these built
4106 * up we can end up burning a lot of time in btrfs-cleaner without any
4107 * way to throttle the unlinks. Since we're currently holding a ref on
4108 * the inode we can run the delayed iput here without any issues as the
4109 * final iput won't be done until after we drop the ref we're currently
4110 * holding.
4111 */
4112 btrfs_run_delayed_iput(fs_info, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004113err:
4114 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04004115 if (ret)
4116 goto out;
4117
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004118 btrfs_i_size_write(dir, dir->vfs_inode.i_size - name_len * 2);
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004119 inode_inc_iversion(&inode->vfs_inode);
4120 inode_inc_iversion(&dir->vfs_inode);
4121 inode->vfs_inode.i_ctime = dir->vfs_inode.i_mtime =
4122 dir->vfs_inode.i_ctime = current_time(&inode->vfs_inode);
4123 ret = btrfs_update_inode(trans, root, &dir->vfs_inode);
Chris Masone02119d2008-09-05 16:13:11 -04004124out:
Chris Mason39279cc2007-06-12 06:35:45 -04004125 return ret;
4126}
4127
Al Viro92986792011-03-04 17:14:37 +00004128int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
4129 struct btrfs_root *root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004130 struct btrfs_inode *dir, struct btrfs_inode *inode,
Al Viro92986792011-03-04 17:14:37 +00004131 const char *name, int name_len)
4132{
4133 int ret;
4134 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
4135 if (!ret) {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004136 drop_nlink(&inode->vfs_inode);
4137 ret = btrfs_update_inode(trans, root, &inode->vfs_inode);
Al Viro92986792011-03-04 17:14:37 +00004138 }
4139 return ret;
4140}
Yan, Zhenga22285a2010-05-16 10:48:46 -04004141
4142/*
4143 * helper to start transaction for unlink and rmdir.
4144 *
Josef Bacikd52be812013-05-29 14:54:47 -04004145 * unlink and rmdir are special in btrfs, they do not always free space, so
4146 * if we cannot make our reservations the normal way try and see if there is
4147 * plenty of slack room in the global reserve to migrate, otherwise we cannot
4148 * allow the unlink to occur.
Yan, Zhenga22285a2010-05-16 10:48:46 -04004149 */
Josef Bacikd52be812013-05-29 14:54:47 -04004150static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir)
Yan, Zhenga22285a2010-05-16 10:48:46 -04004151{
Yan, Zhenga22285a2010-05-16 10:48:46 -04004152 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004153
Josef Bacike70bea52011-10-11 14:18:24 -04004154 /*
4155 * 1 for the possible orphan item
4156 * 1 for the dir item
4157 * 1 for the dir index
4158 * 1 for the inode ref
Josef Bacike70bea52011-10-11 14:18:24 -04004159 * 1 for the inode
4160 */
Filipe Manana8eab77f2015-11-13 23:57:16 +00004161 return btrfs_start_transaction_fallback_global_rsv(root, 5, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004162}
4163
Chris Mason39279cc2007-06-12 06:35:45 -04004164static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
4165{
Yan, Zhenga22285a2010-05-16 10:48:46 -04004166 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004167 struct btrfs_trans_handle *trans;
David Howells2b0143b2015-03-17 22:25:59 +00004168 struct inode *inode = d_inode(dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04004169 int ret;
4170
Josef Bacikd52be812013-05-29 14:54:47 -04004171 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004172 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004173 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04004174
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004175 btrfs_record_unlink_dir(trans, BTRFS_I(dir), BTRFS_I(d_inode(dentry)),
4176 0);
Chris Mason12fcfd22009-03-24 10:24:20 -04004177
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004178 ret = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
4179 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
4180 dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00004181 if (ret)
4182 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004183
Yan, Zhenga22285a2010-05-16 10:48:46 -04004184 if (inode->i_nlink == 0) {
Nikolay Borisov73f2e542017-02-20 13:50:59 +02004185 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Tsutomu Itohb5324022011-07-19 07:27:20 +00004186 if (ret)
4187 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004188 }
Josef Bacik7b128762008-07-24 12:17:14 -04004189
Tsutomu Itohb5324022011-07-19 07:27:20 +00004190out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004191 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004192 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04004193 return ret;
4194}
4195
Misono Tomohirof60a2362018-04-18 11:34:52 +09004196static int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
Lu Fengqi401b3b12018-08-01 11:32:30 +08004197 struct inode *dir, u64 objectid,
4198 const char *name, int name_len)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004199{
Lu Fengqi401b3b12018-08-01 11:32:30 +08004200 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004201 struct btrfs_path *path;
4202 struct extent_buffer *leaf;
4203 struct btrfs_dir_item *di;
4204 struct btrfs_key key;
4205 u64 index;
4206 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004207 u64 dir_ino = btrfs_ino(BTRFS_I(dir));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004208
4209 path = btrfs_alloc_path();
4210 if (!path)
4211 return -ENOMEM;
4212
Li Zefan33345d012011-04-20 10:31:50 +08004213 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004214 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004215 if (IS_ERR_OR_NULL(di)) {
Liu Bo3cf50682018-09-12 06:06:26 +08004216 ret = di ? PTR_ERR(di) : -ENOENT;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004217 goto out;
4218 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004219
4220 leaf = path->nodes[0];
4221 btrfs_dir_item_key_to_cpu(leaf, di, &key);
4222 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
4223 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004224 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004225 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004226 goto out;
4227 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004228 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004229
Lu Fengqi3ee1c552018-08-01 11:32:28 +08004230 ret = btrfs_del_root_ref(trans, objectid, root->root_key.objectid,
4231 dir_ino, &index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004232 if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004233 if (ret != -ENOENT) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004234 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004235 goto out;
4236 }
Li Zefan33345d012011-04-20 10:31:50 +08004237 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004238 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004239 if (IS_ERR_OR_NULL(di)) {
4240 if (!di)
4241 ret = -ENOENT;
4242 else
4243 ret = PTR_ERR(di);
Jeff Mahoney66642832016-06-10 18:19:25 -04004244 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004245 goto out;
4246 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004247
4248 leaf = path->nodes[0];
4249 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004250 index = key.offset;
4251 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004252 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004253
Lu Fengqi9add2942018-08-01 11:32:26 +08004254 ret = btrfs_delete_delayed_dir_index(trans, BTRFS_I(dir), index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004255 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004256 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004257 goto out;
4258 }
Miao Xie16cdcec2011-04-22 18:12:22 +08004259
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004260 btrfs_i_size_write(BTRFS_I(dir), dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004261 inode_inc_iversion(dir);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07004262 dir->i_mtime = dir->i_ctime = current_time(dir);
Josef Bacik5a24e842012-08-08 10:12:59 -06004263 ret = btrfs_update_inode_fallback(trans, root, dir);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004264 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004265 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004266out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04004267 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004268 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004269}
4270
Misono Tomohiroec42f162018-04-18 11:34:13 +09004271/*
4272 * Helper to check if the subvolume references other subvolumes or if it's
4273 * default.
4274 */
Misono Tomohirof60a2362018-04-18 11:34:52 +09004275static noinline int may_destroy_subvol(struct btrfs_root *root)
Misono Tomohiroec42f162018-04-18 11:34:13 +09004276{
4277 struct btrfs_fs_info *fs_info = root->fs_info;
4278 struct btrfs_path *path;
4279 struct btrfs_dir_item *di;
4280 struct btrfs_key key;
4281 u64 dir_id;
4282 int ret;
4283
4284 path = btrfs_alloc_path();
4285 if (!path)
4286 return -ENOMEM;
4287
4288 /* Make sure this root isn't set as the default subvol */
4289 dir_id = btrfs_super_root_dir(fs_info->super_copy);
4290 di = btrfs_lookup_dir_item(NULL, fs_info->tree_root, path,
4291 dir_id, "default", 7, 0);
4292 if (di && !IS_ERR(di)) {
4293 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key);
4294 if (key.objectid == root->root_key.objectid) {
4295 ret = -EPERM;
4296 btrfs_err(fs_info,
4297 "deleting default subvolume %llu is not allowed",
4298 key.objectid);
4299 goto out;
4300 }
4301 btrfs_release_path(path);
4302 }
4303
4304 key.objectid = root->root_key.objectid;
4305 key.type = BTRFS_ROOT_REF_KEY;
4306 key.offset = (u64)-1;
4307
4308 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
4309 if (ret < 0)
4310 goto out;
4311 BUG_ON(ret == 0);
4312
4313 ret = 0;
4314 if (path->slots[0] > 0) {
4315 path->slots[0]--;
4316 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
4317 if (key.objectid == root->root_key.objectid &&
4318 key.type == BTRFS_ROOT_REF_KEY)
4319 ret = -ENOTEMPTY;
4320 }
4321out:
4322 btrfs_free_path(path);
4323 return ret;
4324}
4325
Nikolay Borisov20a68002018-04-27 14:36:24 +03004326/* Delete all dentries for inodes belonging to the root */
4327static void btrfs_prune_dentries(struct btrfs_root *root)
4328{
4329 struct btrfs_fs_info *fs_info = root->fs_info;
4330 struct rb_node *node;
4331 struct rb_node *prev;
4332 struct btrfs_inode *entry;
4333 struct inode *inode;
4334 u64 objectid = 0;
4335
4336 if (!test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
4337 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
4338
4339 spin_lock(&root->inode_lock);
4340again:
4341 node = root->inode_tree.rb_node;
4342 prev = NULL;
4343 while (node) {
4344 prev = node;
4345 entry = rb_entry(node, struct btrfs_inode, rb_node);
4346
David Sterba37508512018-06-29 10:56:40 +02004347 if (objectid < btrfs_ino(entry))
Nikolay Borisov20a68002018-04-27 14:36:24 +03004348 node = node->rb_left;
David Sterba37508512018-06-29 10:56:40 +02004349 else if (objectid > btrfs_ino(entry))
Nikolay Borisov20a68002018-04-27 14:36:24 +03004350 node = node->rb_right;
4351 else
4352 break;
4353 }
4354 if (!node) {
4355 while (prev) {
4356 entry = rb_entry(prev, struct btrfs_inode, rb_node);
David Sterba37508512018-06-29 10:56:40 +02004357 if (objectid <= btrfs_ino(entry)) {
Nikolay Borisov20a68002018-04-27 14:36:24 +03004358 node = prev;
4359 break;
4360 }
4361 prev = rb_next(prev);
4362 }
4363 }
4364 while (node) {
4365 entry = rb_entry(node, struct btrfs_inode, rb_node);
David Sterba37508512018-06-29 10:56:40 +02004366 objectid = btrfs_ino(entry) + 1;
Nikolay Borisov20a68002018-04-27 14:36:24 +03004367 inode = igrab(&entry->vfs_inode);
4368 if (inode) {
4369 spin_unlock(&root->inode_lock);
4370 if (atomic_read(&inode->i_count) > 1)
4371 d_prune_aliases(inode);
4372 /*
4373 * btrfs_drop_inode will have it removed from the inode
4374 * cache when its usage count hits zero.
4375 */
4376 iput(inode);
4377 cond_resched();
4378 spin_lock(&root->inode_lock);
4379 goto again;
4380 }
4381
4382 if (cond_resched_lock(&root->inode_lock))
4383 goto again;
4384
4385 node = rb_next(node);
4386 }
4387 spin_unlock(&root->inode_lock);
4388}
4389
Misono Tomohirof60a2362018-04-18 11:34:52 +09004390int btrfs_delete_subvolume(struct inode *dir, struct dentry *dentry)
4391{
4392 struct btrfs_fs_info *fs_info = btrfs_sb(dentry->d_sb);
4393 struct btrfs_root *root = BTRFS_I(dir)->root;
4394 struct inode *inode = d_inode(dentry);
4395 struct btrfs_root *dest = BTRFS_I(inode)->root;
4396 struct btrfs_trans_handle *trans;
4397 struct btrfs_block_rsv block_rsv;
4398 u64 root_flags;
Misono Tomohirof60a2362018-04-18 11:34:52 +09004399 int ret;
4400 int err;
4401
4402 /*
4403 * Don't allow to delete a subvolume with send in progress. This is
4404 * inside the inode lock so the error handling that has to drop the bit
4405 * again is not run concurrently.
4406 */
4407 spin_lock(&dest->root_item_lock);
Lu Fengqia7176f72018-08-04 21:10:53 +08004408 if (dest->send_in_progress) {
Misono Tomohirof60a2362018-04-18 11:34:52 +09004409 spin_unlock(&dest->root_item_lock);
4410 btrfs_warn(fs_info,
4411 "attempt to delete subvolume %llu during send",
4412 dest->root_key.objectid);
4413 return -EPERM;
4414 }
Lu Fengqia7176f72018-08-04 21:10:53 +08004415 root_flags = btrfs_root_flags(&dest->root_item);
4416 btrfs_set_root_flags(&dest->root_item,
4417 root_flags | BTRFS_ROOT_SUBVOL_DEAD);
4418 spin_unlock(&dest->root_item_lock);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004419
4420 down_write(&fs_info->subvol_sem);
4421
4422 err = may_destroy_subvol(dest);
4423 if (err)
4424 goto out_up_write;
4425
4426 btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP);
4427 /*
4428 * One for dir inode,
4429 * two for dir entries,
4430 * two for root ref/backref.
4431 */
Gu JinXiangc4c129d2018-05-30 11:00:38 +08004432 err = btrfs_subvolume_reserve_metadata(root, &block_rsv, 5, true);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004433 if (err)
4434 goto out_up_write;
4435
4436 trans = btrfs_start_transaction(root, 0);
4437 if (IS_ERR(trans)) {
4438 err = PTR_ERR(trans);
4439 goto out_release;
4440 }
4441 trans->block_rsv = &block_rsv;
4442 trans->bytes_reserved = block_rsv.size;
4443
4444 btrfs_record_snapshot_destroy(trans, BTRFS_I(dir));
4445
Lu Fengqi401b3b12018-08-01 11:32:30 +08004446 ret = btrfs_unlink_subvol(trans, dir, dest->root_key.objectid,
4447 dentry->d_name.name, dentry->d_name.len);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004448 if (ret) {
4449 err = ret;
4450 btrfs_abort_transaction(trans, ret);
4451 goto out_end_trans;
4452 }
4453
4454 btrfs_record_root_in_trans(trans, dest);
4455
4456 memset(&dest->root_item.drop_progress, 0,
4457 sizeof(dest->root_item.drop_progress));
4458 dest->root_item.drop_level = 0;
4459 btrfs_set_root_refs(&dest->root_item, 0);
4460
4461 if (!test_and_set_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &dest->state)) {
4462 ret = btrfs_insert_orphan_item(trans,
4463 fs_info->tree_root,
4464 dest->root_key.objectid);
4465 if (ret) {
4466 btrfs_abort_transaction(trans, ret);
4467 err = ret;
4468 goto out_end_trans;
4469 }
4470 }
4471
Lu Fengqid1957792018-05-29 15:01:54 +08004472 ret = btrfs_uuid_tree_remove(trans, dest->root_item.uuid,
Misono Tomohirof60a2362018-04-18 11:34:52 +09004473 BTRFS_UUID_KEY_SUBVOL,
4474 dest->root_key.objectid);
4475 if (ret && ret != -ENOENT) {
4476 btrfs_abort_transaction(trans, ret);
4477 err = ret;
4478 goto out_end_trans;
4479 }
4480 if (!btrfs_is_empty_uuid(dest->root_item.received_uuid)) {
Lu Fengqid1957792018-05-29 15:01:54 +08004481 ret = btrfs_uuid_tree_remove(trans,
Misono Tomohirof60a2362018-04-18 11:34:52 +09004482 dest->root_item.received_uuid,
4483 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
4484 dest->root_key.objectid);
4485 if (ret && ret != -ENOENT) {
4486 btrfs_abort_transaction(trans, ret);
4487 err = ret;
4488 goto out_end_trans;
4489 }
4490 }
4491
4492out_end_trans:
4493 trans->block_rsv = NULL;
4494 trans->bytes_reserved = 0;
4495 ret = btrfs_end_transaction(trans);
4496 if (ret && !err)
4497 err = ret;
4498 inode->i_flags |= S_DEAD;
4499out_release:
4500 btrfs_subvolume_release_metadata(fs_info, &block_rsv);
4501out_up_write:
4502 up_write(&fs_info->subvol_sem);
4503 if (err) {
4504 spin_lock(&dest->root_item_lock);
4505 root_flags = btrfs_root_flags(&dest->root_item);
4506 btrfs_set_root_flags(&dest->root_item,
4507 root_flags & ~BTRFS_ROOT_SUBVOL_DEAD);
4508 spin_unlock(&dest->root_item_lock);
4509 } else {
4510 d_invalidate(dentry);
Nikolay Borisov20a68002018-04-27 14:36:24 +03004511 btrfs_prune_dentries(dest);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004512 ASSERT(dest->send_in_progress == 0);
4513
4514 /* the last ref */
4515 if (dest->ino_cache_inode) {
4516 iput(dest->ino_cache_inode);
4517 dest->ino_cache_inode = NULL;
4518 }
4519 }
4520
4521 return err;
4522}
4523
Chris Mason39279cc2007-06-12 06:35:45 -04004524static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
4525{
David Howells2b0143b2015-03-17 22:25:59 +00004526 struct inode *inode = d_inode(dentry);
Chris Mason1832a6d2007-12-21 16:27:21 -05004527 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004528 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004529 struct btrfs_trans_handle *trans;
Filipe Manana44f714d2016-06-06 16:11:13 +01004530 u64 last_unlink_trans;
Chris Mason39279cc2007-06-12 06:35:45 -04004531
David Sterbab3ae2442012-09-13 16:04:34 -06004532 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Yan134d4512007-10-25 15:49:25 -04004533 return -ENOTEMPTY;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004534 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_FIRST_FREE_OBJECTID)
Misono Tomohiroa79a4642018-04-18 11:35:31 +09004535 return btrfs_delete_subvolume(dir, dentry);
Yan134d4512007-10-25 15:49:25 -04004536
Josef Bacikd52be812013-05-29 14:54:47 -04004537 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004538 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004539 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004540
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004541 if (unlikely(btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Lu Fengqi401b3b12018-08-01 11:32:30 +08004542 err = btrfs_unlink_subvol(trans, dir,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004543 BTRFS_I(inode)->location.objectid,
4544 dentry->d_name.name,
4545 dentry->d_name.len);
4546 goto out;
4547 }
4548
Nikolay Borisov73f2e542017-02-20 13:50:59 +02004549 err = btrfs_orphan_add(trans, BTRFS_I(inode));
Josef Bacik7b128762008-07-24 12:17:14 -04004550 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004551 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004552
Filipe Manana44f714d2016-06-06 16:11:13 +01004553 last_unlink_trans = BTRFS_I(inode)->last_unlink_trans;
4554
Chris Mason39279cc2007-06-12 06:35:45 -04004555 /* now the directory is empty */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004556 err = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
4557 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
4558 dentry->d_name.len);
Filipe Manana44f714d2016-06-06 16:11:13 +01004559 if (!err) {
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004560 btrfs_i_size_write(BTRFS_I(inode), 0);
Filipe Manana44f714d2016-06-06 16:11:13 +01004561 /*
4562 * Propagate the last_unlink_trans value of the deleted dir to
4563 * its parent directory. This is to prevent an unrecoverable
4564 * log tree in the case we do something like this:
4565 * 1) create dir foo
4566 * 2) create snapshot under dir foo
4567 * 3) delete the snapshot
4568 * 4) rmdir foo
4569 * 5) mkdir foo
4570 * 6) fsync foo or some file inside foo
4571 */
4572 if (last_unlink_trans >= trans->transid)
4573 BTRFS_I(dir)->last_unlink_trans = last_unlink_trans;
4574 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004575out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004576 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004577 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39544012007-12-12 14:38:19 -05004578
Chris Mason39279cc2007-06-12 06:35:45 -04004579 return err;
4580}
4581
Josef Bacikddfae632017-10-19 14:16:02 -04004582/*
4583 * Return this if we need to call truncate_block for the last bit of the
4584 * truncate.
4585 */
4586#define NEED_TRUNCATE_BLOCK 1
Filipe Manana0305cd52015-10-16 12:34:25 +01004587
Chris Mason323ac952008-10-01 19:05:46 -04004588/*
Chris Mason39279cc2007-06-12 06:35:45 -04004589 * this can truncate away extent items, csum items and directory items.
4590 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04004591 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04004592 *
4593 * csum items that cross the new i_size are truncated to the new size
4594 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04004595 *
4596 * min_type is the minimum key type to truncate down to. If set to 0, this
4597 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04004598 */
Yan, Zheng80825102009-11-12 09:35:36 +00004599int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
4600 struct btrfs_root *root,
4601 struct inode *inode,
4602 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004603{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004604 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04004605 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004606 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004607 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00004608 struct btrfs_key key;
4609 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04004610 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04004611 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004612 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004613 u64 item_end = 0;
Filipe Mananac1aa4572015-06-20 18:20:09 +01004614 u64 last_size = new_size;
Yan, Zheng80825102009-11-12 09:35:36 +00004615 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04004616 int found_extent;
4617 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05004618 int pending_del_nr = 0;
4619 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04004620 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00004621 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004622 u64 ino = btrfs_ino(BTRFS_I(inode));
Chris Mason28ed1342014-12-17 09:41:04 -08004623 u64 bytes_deleted = 0;
Thomas Meyer897ca812017-10-07 16:02:21 +02004624 bool be_nice = false;
4625 bool should_throttle = false;
Yan, Zheng80825102009-11-12 09:35:36 +00004626
4627 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04004628
Chris Mason28ed1342014-12-17 09:41:04 -08004629 /*
4630 * for non-free space inodes and ref cows, we want to back off from
4631 * time to time
4632 */
Nikolay Borisov70ddc552017-02-20 13:50:35 +02004633 if (!btrfs_is_free_space_inode(BTRFS_I(inode)) &&
Chris Mason28ed1342014-12-17 09:41:04 -08004634 test_bit(BTRFS_ROOT_REF_COWS, &root->state))
Thomas Meyer897ca812017-10-07 16:02:21 +02004635 be_nice = true;
Chris Mason28ed1342014-12-17 09:41:04 -08004636
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004637 path = btrfs_alloc_path();
4638 if (!path)
4639 return -ENOMEM;
David Sterbae4058b52015-11-27 16:31:35 +01004640 path->reada = READA_BACK;
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004641
Josef Bacik5dc562c2012-08-17 13:14:17 -04004642 /*
4643 * We want to drop from the next block forward in case this new size is
4644 * not block aligned since we will be keeping the last block of the
4645 * extent just the way it is.
4646 */
Miao Xie27cdeb72014-04-02 19:51:05 +08004647 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004648 root == fs_info->tree_root)
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02004649 btrfs_drop_extent_cache(BTRFS_I(inode), ALIGN(new_size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004650 fs_info->sectorsize),
Jeff Mahoneyda170662016-06-15 09:22:56 -04004651 (u64)-1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00004652
Miao Xie16cdcec2011-04-22 18:12:22 +08004653 /*
4654 * This function is also used to drop the items in the log tree before
4655 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
Andrea Gelmini52042d82018-11-28 12:05:13 +01004656 * it is used to drop the logged items. So we shouldn't kill the delayed
Miao Xie16cdcec2011-04-22 18:12:22 +08004657 * items.
4658 */
4659 if (min_type == 0 && root == BTRFS_I(inode)->root)
Nikolay Borisov4ccb5c72017-01-10 20:35:38 +02004660 btrfs_kill_delayed_inode_items(BTRFS_I(inode));
Miao Xie16cdcec2011-04-22 18:12:22 +08004661
Li Zefan33345d012011-04-20 10:31:50 +08004662 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04004663 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04004664 key.type = (u8)-1;
4665
Chris Mason85e21ba2008-01-29 15:11:36 -05004666search_again:
Chris Mason28ed1342014-12-17 09:41:04 -08004667 /*
4668 * with a 16K leaf size and 128MB extents, you can actually queue
4669 * up a huge file in a single leaf. Most of the time that
4670 * bytes_deleted is > 0, it will be huge by the time we get here
4671 */
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004672 if (be_nice && bytes_deleted > SZ_32M &&
4673 btrfs_should_end_transaction(trans)) {
4674 ret = -EAGAIN;
Yan, Zheng80825102009-11-12 09:35:36 +00004675 goto out;
4676 }
Chris Masond3977122009-01-05 21:25:51 -05004677
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004678 path->leave_spinning = 1;
4679 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
4680 if (ret < 0)
4681 goto out;
4682
Chris Mason85e21ba2008-01-29 15:11:36 -05004683 if (ret > 0) {
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004684 ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -04004685 /* there are no items in the tree for us to truncate, we're
4686 * done
4687 */
Yan, Zheng80825102009-11-12 09:35:36 +00004688 if (path->slots[0] == 0)
4689 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05004690 path->slots[0]--;
4691 }
4692
Chris Masond3977122009-01-05 21:25:51 -05004693 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04004694 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04004695 leaf = path->nodes[0];
4696 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02004697 found_type = found_key.type;
Chris Mason39279cc2007-06-12 06:35:45 -04004698
Li Zefan33345d012011-04-20 10:31:50 +08004699 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04004700 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004701
Chris Mason85e21ba2008-01-29 15:11:36 -05004702 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004703 break;
4704
Chris Mason5f39d392007-10-15 16:14:19 -04004705 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04004706 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04004707 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04004708 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04004709 extent_type = btrfs_file_extent_type(leaf, fi);
4710 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04004711 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04004712 btrfs_file_extent_num_bytes(leaf, fi);
Liu Bo09ed2f12017-03-10 11:09:48 -08004713
4714 trace_btrfs_truncate_show_fi_regular(
4715 BTRFS_I(inode), leaf, fi,
4716 found_key.offset);
Chris Mason179e29e2007-11-01 11:28:41 -04004717 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Qu Wenruoe41ca582018-06-06 15:41:49 +08004718 item_end += btrfs_file_extent_ram_bytes(leaf,
4719 fi);
Liu Bo09ed2f12017-03-10 11:09:48 -08004720
4721 trace_btrfs_truncate_show_fi_inline(
4722 BTRFS_I(inode), leaf, fi, path->slots[0],
4723 found_key.offset);
Chris Mason39279cc2007-06-12 06:35:45 -04004724 }
Yan008630c2007-11-07 13:31:09 -05004725 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04004726 }
Yan, Zheng80825102009-11-12 09:35:36 +00004727 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04004728 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00004729 } else {
Filipe Manana76b42ab2017-02-14 16:56:01 +00004730 if (item_end < new_size)
Yan, Zheng80825102009-11-12 09:35:36 +00004731 break;
4732 if (found_key.offset >= new_size)
4733 del_item = 1;
4734 else
4735 del_item = 0;
4736 }
Chris Mason39279cc2007-06-12 06:35:45 -04004737 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004738 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04004739 if (found_type != BTRFS_EXTENT_DATA_KEY)
4740 goto delete;
4741
4742 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04004743 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04004744 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05004745 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04004746 u64 orig_num_bytes =
4747 btrfs_file_extent_num_bytes(leaf, fi);
Qu Wenruofda28322013-02-26 08:10:22 +00004748 extent_num_bytes = ALIGN(new_size -
4749 found_key.offset,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004750 fs_info->sectorsize);
Chris Masondb945352007-10-15 16:15:53 -04004751 btrfs_set_file_extent_num_bytes(leaf, fi,
4752 extent_num_bytes);
4753 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05004754 extent_num_bytes);
Miao Xie27cdeb72014-04-02 19:51:05 +08004755 if (test_bit(BTRFS_ROOT_REF_COWS,
4756 &root->state) &&
4757 extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004758 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04004759 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004760 } else {
Chris Masondb945352007-10-15 16:15:53 -04004761 extent_num_bytes =
4762 btrfs_file_extent_disk_num_bytes(leaf,
4763 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004764 extent_offset = found_key.offset -
4765 btrfs_file_extent_offset(leaf, fi);
4766
Chris Mason39279cc2007-06-12 06:35:45 -04004767 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05004768 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004769 if (extent_start != 0) {
4770 found_extent = 1;
Miao Xie27cdeb72014-04-02 19:51:05 +08004771 if (test_bit(BTRFS_ROOT_REF_COWS,
4772 &root->state))
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004773 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04004774 }
4775 }
Chris Mason90692182008-02-08 13:49:28 -05004776 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04004777 /*
4778 * we can't truncate inline items that have had
4779 * special encodings
4780 */
4781 if (!del_item &&
Chris Masonc8b97812008-10-29 14:49:59 -04004782 btrfs_file_extent_encryption(leaf, fi) == 0 &&
Josef Bacikddfae632017-10-19 14:16:02 -04004783 btrfs_file_extent_other_encoding(leaf, fi) == 0 &&
4784 btrfs_file_extent_compression(leaf, fi) == 0) {
4785 u32 size = (u32)(new_size - found_key.offset);
Chris Mason514ac8a2014-01-03 21:07:00 -08004786
Josef Bacikddfae632017-10-19 14:16:02 -04004787 btrfs_set_file_extent_ram_bytes(leaf, fi, size);
4788 size = btrfs_file_extent_calc_inline_size(size);
David Sterba78ac4f92019-03-20 14:49:12 +01004789 btrfs_truncate_item(path, size, 1);
Josef Bacikddfae632017-10-19 14:16:02 -04004790 } else if (!del_item) {
Chris Mason514ac8a2014-01-03 21:07:00 -08004791 /*
Josef Bacikddfae632017-10-19 14:16:02 -04004792 * We have to bail so the last_size is set to
4793 * just before this extent.
Chris Mason514ac8a2014-01-03 21:07:00 -08004794 */
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004795 ret = NEED_TRUNCATE_BLOCK;
Josef Bacikddfae632017-10-19 14:16:02 -04004796 break;
Chris Mason90692182008-02-08 13:49:28 -05004797 }
Josef Bacikddfae632017-10-19 14:16:02 -04004798
4799 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state))
4800 inode_sub_bytes(inode, item_end + 1 - new_size);
Chris Mason39279cc2007-06-12 06:35:45 -04004801 }
Chris Mason179e29e2007-11-01 11:28:41 -04004802delete:
Josef Bacikddfae632017-10-19 14:16:02 -04004803 if (del_item)
4804 last_size = found_key.offset;
4805 else
4806 last_size = new_size;
Chris Mason39279cc2007-06-12 06:35:45 -04004807 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05004808 if (!pending_del_nr) {
4809 /* no pending yet, add ourselves */
4810 pending_del_slot = path->slots[0];
4811 pending_del_nr = 1;
4812 } else if (pending_del_nr &&
4813 path->slots[0] + 1 == pending_del_slot) {
4814 /* hop on the pending chunk */
4815 pending_del_nr++;
4816 pending_del_slot = path->slots[0];
4817 } else {
Chris Masond3977122009-01-05 21:25:51 -05004818 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05004819 }
Chris Mason39279cc2007-06-12 06:35:45 -04004820 } else {
4821 break;
4822 }
Thomas Meyer897ca812017-10-07 16:02:21 +02004823 should_throttle = false;
Chris Mason28f75a02015-02-04 06:59:29 -08004824
Miao Xie27cdeb72014-04-02 19:51:05 +08004825 if (found_extent &&
4826 (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004827 root == fs_info->tree_root)) {
Qu Wenruoffd4bb22019-04-04 14:45:36 +08004828 struct btrfs_ref ref = { 0 };
4829
Chris Masonb9473432009-03-13 11:00:37 -04004830 btrfs_set_path_blocking(path);
Chris Mason28ed1342014-12-17 09:41:04 -08004831 bytes_deleted += extent_num_bytes;
Qu Wenruoffd4bb22019-04-04 14:45:36 +08004832
4833 btrfs_init_generic_ref(&ref, BTRFS_DROP_DELAYED_REF,
4834 extent_start, extent_num_bytes, 0);
4835 ref.real_root = root->root_key.objectid;
4836 btrfs_init_data_ref(&ref, btrfs_header_owner(leaf),
4837 ino, extent_offset);
4838 ret = btrfs_free_extent(trans, &ref);
Omar Sandoval05522102018-05-11 13:13:31 -07004839 if (ret) {
4840 btrfs_abort_transaction(trans, ret);
4841 break;
4842 }
Chris Mason28f75a02015-02-04 06:59:29 -08004843 if (be_nice) {
Lu Fengqi7c861622018-10-11 13:40:36 +08004844 if (btrfs_should_throttle_delayed_refs(trans))
Thomas Meyer897ca812017-10-07 16:02:21 +02004845 should_throttle = true;
Chris Mason28f75a02015-02-04 06:59:29 -08004846 }
Chris Mason39279cc2007-06-12 06:35:45 -04004847 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004848
Yan, Zheng80825102009-11-12 09:35:36 +00004849 if (found_type == BTRFS_INODE_ITEM_KEY)
4850 break;
4851
4852 if (path->slots[0] == 0 ||
Josef Bacik12621332015-02-03 07:50:16 -08004853 path->slots[0] != pending_del_slot ||
Josef Bacik28bad212018-12-03 10:20:38 -05004854 should_throttle) {
Yan, Zheng80825102009-11-12 09:35:36 +00004855 if (pending_del_nr) {
4856 ret = btrfs_del_items(trans, root, path,
4857 pending_del_slot,
4858 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004859 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004860 btrfs_abort_transaction(trans, ret);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004861 break;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004862 }
Yan, Zheng80825102009-11-12 09:35:36 +00004863 pending_del_nr = 0;
4864 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004865 btrfs_release_path(path);
Josef Bacik28bad212018-12-03 10:20:38 -05004866
Chris Mason28f75a02015-02-04 06:59:29 -08004867 /*
Josef Bacik28bad212018-12-03 10:20:38 -05004868 * We can generate a lot of delayed refs, so we need to
4869 * throttle every once and a while and make sure we're
4870 * adding enough space to keep up with the work we are
4871 * generating. Since we hold a transaction here we
4872 * can't flush, and we don't want to FLUSH_LIMIT because
4873 * we could have generated too many delayed refs to
4874 * actually allocate, so just bail if we're short and
4875 * let the normal reservation dance happen higher up.
Chris Mason28f75a02015-02-04 06:59:29 -08004876 */
Josef Bacik28bad212018-12-03 10:20:38 -05004877 if (should_throttle) {
4878 ret = btrfs_delayed_refs_rsv_refill(fs_info,
4879 BTRFS_RESERVE_NO_FLUSH);
4880 if (ret) {
4881 ret = -EAGAIN;
4882 break;
4883 }
Chris Mason28f75a02015-02-04 06:59:29 -08004884 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004885 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00004886 } else {
4887 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05004888 }
Chris Mason39279cc2007-06-12 06:35:45 -04004889 }
Yan, Zheng80825102009-11-12 09:35:36 +00004890out:
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004891 if (ret >= 0 && pending_del_nr) {
4892 int err;
4893
4894 err = btrfs_del_items(trans, root, path, pending_del_slot,
Chris Mason85e21ba2008-01-29 15:11:36 -05004895 pending_del_nr);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004896 if (err) {
4897 btrfs_abort_transaction(trans, err);
4898 ret = err;
4899 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004900 }
Filipe Manana76b42ab2017-02-14 16:56:01 +00004901 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
4902 ASSERT(last_size >= new_size);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004903 if (!ret && last_size > new_size)
Filipe Manana76b42ab2017-02-14 16:56:01 +00004904 last_size = new_size;
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004905 btrfs_ordered_update_i_size(inode, last_size, NULL);
Filipe Manana76b42ab2017-02-14 16:56:01 +00004906 }
Chris Mason28ed1342014-12-17 09:41:04 -08004907
Chris Mason39279cc2007-06-12 06:35:45 -04004908 btrfs_free_path(path);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004909 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04004910}
4911
Chris Masona52d9a82007-08-27 16:49:44 -04004912/*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304913 * btrfs_truncate_block - read, zero a chunk and write a block
Josef Bacik2aaa6652012-08-29 14:27:18 -04004914 * @inode - inode that we're zeroing
4915 * @from - the offset to start zeroing
4916 * @len - the length to zero, 0 to zero the entire range respective to the
4917 * offset
4918 * @front - zero up to the offset instead of from the offset on
4919 *
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304920 * This will find the block for the "from" offset and cow the block and zero the
Josef Bacik2aaa6652012-08-29 14:27:18 -04004921 * part we want to zero. This is used with truncate and hole punching.
Chris Masona52d9a82007-08-27 16:49:44 -04004922 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304923int btrfs_truncate_block(struct inode *inode, loff_t from, loff_t len,
Josef Bacik2aaa6652012-08-29 14:27:18 -04004924 int front)
Chris Masona52d9a82007-08-27 16:49:44 -04004925{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004926 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004927 struct address_space *mapping = inode->i_mapping;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004928 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4929 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004930 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08004931 struct extent_changeset *data_reserved = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004932 char *kaddr;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004933 u32 blocksize = fs_info->sectorsize;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004934 pgoff_t index = from >> PAGE_SHIFT;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304935 unsigned offset = from & (blocksize - 1);
Chris Masona52d9a82007-08-27 16:49:44 -04004936 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004937 gfp_t mask = btrfs_alloc_write_mask(mapping);
Chris Masona52d9a82007-08-27 16:49:44 -04004938 int ret = 0;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304939 u64 block_start;
4940 u64 block_end;
Chris Masona52d9a82007-08-27 16:49:44 -04004941
Nikolay Borisovb03ebd92018-01-18 14:47:06 +02004942 if (IS_ALIGNED(offset, blocksize) &&
4943 (!len || IS_ALIGNED(len, blocksize)))
Chris Masona52d9a82007-08-27 16:49:44 -04004944 goto out;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304945
Josef Bacik8b62f872017-10-19 14:15:55 -04004946 block_start = round_down(from, blocksize);
4947 block_end = block_start + blocksize - 1;
4948
Qu Wenruo364ecf32017-02-27 15:10:38 +08004949 ret = btrfs_delalloc_reserve_space(inode, &data_reserved,
Josef Bacik8b62f872017-10-19 14:15:55 -04004950 block_start, blocksize);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004951 if (ret)
4952 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004953
Chris Mason211c17f2008-05-15 09:13:45 -04004954again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004955 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004956 if (!page) {
Qu Wenruobc42bda2017-02-27 15:10:39 +08004957 btrfs_delalloc_release_space(inode, data_reserved,
Qu Wenruo43b18592017-12-12 15:34:32 +08004958 block_start, blocksize, true);
Qu Wenruo8702ba92019-10-14 14:34:51 +08004959 btrfs_delalloc_release_extents(BTRFS_I(inode), blocksize);
Miao Xieac6a2b32012-12-05 10:56:13 +00004960 ret = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -04004961 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04004962 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004963
Chris Masona52d9a82007-08-27 16:49:44 -04004964 if (!PageUptodate(page)) {
4965 ret = btrfs_readpage(NULL, page);
4966 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004967 if (page->mapping != mapping) {
4968 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004969 put_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004970 goto again;
4971 }
Chris Masona52d9a82007-08-27 16:49:44 -04004972 if (!PageUptodate(page)) {
4973 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04004974 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04004975 }
4976 }
Chris Mason211c17f2008-05-15 09:13:45 -04004977 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004978
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304979 lock_extent_bits(io_tree, block_start, block_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004980 set_page_extent_mapped(page);
4981
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304982 ordered = btrfs_lookup_ordered_extent(inode, block_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004983 if (ordered) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304984 unlock_extent_cached(io_tree, block_start, block_end,
David Sterbae43bbe52017-12-12 21:43:52 +01004985 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004986 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004987 put_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04004988 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004989 btrfs_put_ordered_extent(ordered);
4990 goto again;
4991 }
4992
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304993 clear_extent_bit(&BTRFS_I(inode)->io_tree, block_start, block_end,
Omar Sandovale1821632019-08-15 14:04:04 -07004994 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
4995 0, 0, &cached_state);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004996
Filipe Mananae3b8a482017-11-04 00:16:59 +00004997 ret = btrfs_set_extent_delalloc(inode, block_start, block_end, 0,
Nikolay Borisov330a5822019-07-17 16:18:17 +03004998 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004999 if (ret) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05305000 unlock_extent_cached(io_tree, block_start, block_end,
David Sterbae43bbe52017-12-12 21:43:52 +01005001 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04005002 goto out_unlock;
5003 }
5004
Chandan Rajendra9703fef2016-01-21 15:55:56 +05305005 if (offset != blocksize) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04005006 if (!len)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05305007 len = blocksize - offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04005008 kaddr = kmap(page);
Josef Bacik2aaa6652012-08-29 14:27:18 -04005009 if (front)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05305010 memset(kaddr + (block_start - page_offset(page)),
5011 0, offset);
Josef Bacik2aaa6652012-08-29 14:27:18 -04005012 else
Chandan Rajendra9703fef2016-01-21 15:55:56 +05305013 memset(kaddr + (block_start - page_offset(page)) + offset,
5014 0, len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04005015 flush_dcache_page(page);
5016 kunmap(page);
5017 }
Chris Mason247e7432008-07-17 12:53:51 -04005018 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04005019 set_page_dirty(page);
David Sterbae43bbe52017-12-12 21:43:52 +01005020 unlock_extent_cached(io_tree, block_start, block_end, &cached_state);
Chris Mason39279cc2007-06-12 06:35:45 -04005021
Chris Mason89642222008-07-24 09:41:53 -04005022out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04005023 if (ret)
Qu Wenruobc42bda2017-02-27 15:10:39 +08005024 btrfs_delalloc_release_space(inode, data_reserved, block_start,
Qu Wenruo43b18592017-12-12 15:34:32 +08005025 blocksize, true);
Qu Wenruo8702ba92019-10-14 14:34:51 +08005026 btrfs_delalloc_release_extents(BTRFS_I(inode), blocksize);
Chris Mason39279cc2007-06-12 06:35:45 -04005027 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005028 put_page(page);
Chris Mason39279cc2007-06-12 06:35:45 -04005029out:
Qu Wenruo364ecf32017-02-27 15:10:38 +08005030 extent_changeset_free(data_reserved);
Chris Mason39279cc2007-06-12 06:35:45 -04005031 return ret;
5032}
5033
Josef Bacik16e75492013-10-22 12:18:51 -04005034static int maybe_insert_hole(struct btrfs_root *root, struct inode *inode,
5035 u64 offset, u64 len)
5036{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005037 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik16e75492013-10-22 12:18:51 -04005038 struct btrfs_trans_handle *trans;
5039 int ret;
5040
5041 /*
5042 * Still need to make sure the inode looks like it's been updated so
5043 * that any holes get logged if we fsync.
5044 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005045 if (btrfs_fs_incompat(fs_info, NO_HOLES)) {
5046 BTRFS_I(inode)->last_trans = fs_info->generation;
Josef Bacik16e75492013-10-22 12:18:51 -04005047 BTRFS_I(inode)->last_sub_trans = root->log_transid;
5048 BTRFS_I(inode)->last_log_commit = root->last_log_commit;
5049 return 0;
5050 }
5051
5052 /*
5053 * 1 - for the one we're dropping
5054 * 1 - for the one we're adding
5055 * 1 - for updating the inode.
5056 */
5057 trans = btrfs_start_transaction(root, 3);
5058 if (IS_ERR(trans))
5059 return PTR_ERR(trans);
5060
5061 ret = btrfs_drop_extents(trans, root, inode, offset, offset + len, 1);
5062 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04005063 btrfs_abort_transaction(trans, ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005064 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04005065 return ret;
5066 }
5067
David Sterbaf85b7372017-01-20 14:54:07 +01005068 ret = btrfs_insert_file_extent(trans, root, btrfs_ino(BTRFS_I(inode)),
5069 offset, 0, 0, len, 0, len, 0, 0, 0);
Josef Bacik16e75492013-10-22 12:18:51 -04005070 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04005071 btrfs_abort_transaction(trans, ret);
Josef Bacik16e75492013-10-22 12:18:51 -04005072 else
5073 btrfs_update_inode(trans, root, inode);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005074 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04005075 return ret;
5076}
5077
Josef Bacik695a0d02011-03-04 15:46:53 -05005078/*
5079 * This function puts in dummy file extents for the area we're creating a hole
5080 * for. So if we are truncating this file to a larger size we need to insert
5081 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
5082 * the range between oldsize and size
5083 */
Josef Bacika41ad392011-01-31 15:30:16 -05005084int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04005085{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005086 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Yan Zheng9036c102008-10-30 14:19:41 -04005087 struct btrfs_root *root = BTRFS_I(inode)->root;
5088 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04005089 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00005090 struct extent_state *cached_state = NULL;
Josef Bacik5dc562c2012-08-17 13:14:17 -04005091 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005092 u64 hole_start = ALIGN(oldsize, fs_info->sectorsize);
5093 u64 block_end = ALIGN(size, fs_info->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04005094 u64 last_byte;
5095 u64 cur_offset;
5096 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04005097 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04005098
Josef Bacika71754f2013-06-17 17:14:39 -04005099 /*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05305100 * If our size started in the middle of a block we need to zero out the
5101 * rest of the block before we expand the i_size, otherwise we could
Josef Bacika71754f2013-06-17 17:14:39 -04005102 * expose stale data.
5103 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05305104 err = btrfs_truncate_block(inode, oldsize, 0, 0);
Josef Bacika71754f2013-06-17 17:14:39 -04005105 if (err)
5106 return err;
5107
Yan Zheng9036c102008-10-30 14:19:41 -04005108 if (size <= hole_start)
5109 return 0;
5110
Nikolay Borisov23d31bd2019-05-07 10:19:23 +03005111 btrfs_lock_and_flush_ordered_range(io_tree, BTRFS_I(inode), hole_start,
5112 block_end - 1, &cached_state);
Yan Zheng9036c102008-10-30 14:19:41 -04005113 cur_offset = hole_start;
5114 while (1) {
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02005115 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, cur_offset,
Yan Zheng9036c102008-10-30 14:19:41 -04005116 block_end - cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005117 if (IS_ERR(em)) {
5118 err = PTR_ERR(em);
Zach Brownf2767952013-01-08 19:37:58 +00005119 em = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005120 break;
5121 }
Yan Zheng9036c102008-10-30 14:19:41 -04005122 last_byte = min(extent_map_end(em), block_end);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005123 last_byte = ALIGN(last_byte, fs_info->sectorsize);
Yan, Zheng80825102009-11-12 09:35:36 +00005124 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04005125 struct extent_map *hole_em;
Yan Zheng9036c102008-10-30 14:19:41 -04005126 hole_size = last_byte - cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00005127
Josef Bacik16e75492013-10-22 12:18:51 -04005128 err = maybe_insert_hole(root, inode, cur_offset,
5129 hole_size);
5130 if (err)
Chris Mason771ed682008-11-06 22:02:51 -05005131 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02005132 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04005133 cur_offset + hole_size - 1, 0);
5134 hole_em = alloc_extent_map();
5135 if (!hole_em) {
5136 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
5137 &BTRFS_I(inode)->runtime_flags);
5138 goto next;
5139 }
5140 hole_em->start = cur_offset;
5141 hole_em->len = hole_size;
5142 hole_em->orig_start = cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00005143
Josef Bacik5dc562c2012-08-17 13:14:17 -04005144 hole_em->block_start = EXTENT_MAP_HOLE;
5145 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05005146 hole_em->orig_block_len = 0;
Josef Bacikcc95bef2013-04-04 14:31:27 -04005147 hole_em->ram_bytes = hole_size;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005148 hole_em->bdev = fs_info->fs_devices->latest_bdev;
Josef Bacik5dc562c2012-08-17 13:14:17 -04005149 hole_em->compress_type = BTRFS_COMPRESS_NONE;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005150 hole_em->generation = fs_info->generation;
Josef Bacik5dc562c2012-08-17 13:14:17 -04005151
5152 while (1) {
5153 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04005154 err = add_extent_mapping(em_tree, hole_em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04005155 write_unlock(&em_tree->lock);
5156 if (err != -EEXIST)
5157 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02005158 btrfs_drop_extent_cache(BTRFS_I(inode),
5159 cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04005160 cur_offset +
5161 hole_size - 1, 0);
5162 }
5163 free_extent_map(hole_em);
Yan Zheng9036c102008-10-30 14:19:41 -04005164 }
Josef Bacik16e75492013-10-22 12:18:51 -04005165next:
Yan Zheng9036c102008-10-30 14:19:41 -04005166 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04005167 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04005168 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00005169 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04005170 break;
5171 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04005172 free_extent_map(em);
David Sterbae43bbe52017-12-12 21:43:52 +01005173 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state);
Yan Zheng9036c102008-10-30 14:19:41 -04005174 return err;
5175}
5176
Eric Sandeen3972f262013-01-12 02:57:22 +00005177static int btrfs_setsize(struct inode *inode, struct iattr *attr)
Yan, Zheng80825102009-11-12 09:35:36 +00005178{
Miao Xief4a2f4c2011-12-14 20:12:01 -05005179 struct btrfs_root *root = BTRFS_I(inode)->root;
5180 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05005181 loff_t oldsize = i_size_read(inode);
Eric Sandeen3972f262013-01-12 02:57:22 +00005182 loff_t newsize = attr->ia_size;
5183 int mask = attr->ia_valid;
Yan, Zheng80825102009-11-12 09:35:36 +00005184 int ret;
5185
Eric Sandeen3972f262013-01-12 02:57:22 +00005186 /*
5187 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
5188 * special case where we need to update the times despite not having
5189 * these flags set. For all other operations the VFS set these flags
5190 * explicitly if it wants a timestamp update.
5191 */
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08005192 if (newsize != oldsize) {
5193 inode_inc_iversion(inode);
5194 if (!(mask & (ATTR_CTIME | ATTR_MTIME)))
5195 inode->i_ctime = inode->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07005196 current_time(inode);
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08005197 }
Eric Sandeen3972f262013-01-12 02:57:22 +00005198
Josef Bacika41ad392011-01-31 15:30:16 -05005199 if (newsize > oldsize) {
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005200 /*
David Sterbaea14b57f2017-06-22 02:19:11 +02005201 * Don't do an expanding truncate while snapshotting is ongoing.
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005202 * This is to ensure the snapshot captures a fully consistent
5203 * state of this file - if the snapshot captures this expanding
5204 * truncation, it must capture all writes that happened before
5205 * this truncation.
5206 */
Zhao Lei0bc19f902016-01-06 18:56:36 +08005207 btrfs_wait_for_snapshot_creation(root);
Josef Bacika41ad392011-01-31 15:30:16 -05005208 ret = btrfs_cont_expand(inode, oldsize, newsize);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005209 if (ret) {
David Sterbaea14b57f2017-06-22 02:19:11 +02005210 btrfs_end_write_no_snapshotting(root);
Yan, Zheng80825102009-11-12 09:35:36 +00005211 return ret;
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005212 }
Yan, Zheng80825102009-11-12 09:35:36 +00005213
Miao Xief4a2f4c2011-12-14 20:12:01 -05005214 trans = btrfs_start_transaction(root, 1);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005215 if (IS_ERR(trans)) {
David Sterbaea14b57f2017-06-22 02:19:11 +02005216 btrfs_end_write_no_snapshotting(root);
Miao Xief4a2f4c2011-12-14 20:12:01 -05005217 return PTR_ERR(trans);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005218 }
Miao Xief4a2f4c2011-12-14 20:12:01 -05005219
5220 i_size_write(inode, newsize);
5221 btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
Chandan Rajendra27772b62016-01-21 15:56:03 +05305222 pagecache_isize_extended(inode, oldsize, newsize);
Miao Xief4a2f4c2011-12-14 20:12:01 -05005223 ret = btrfs_update_inode(trans, root, inode);
David Sterbaea14b57f2017-06-22 02:19:11 +02005224 btrfs_end_write_no_snapshotting(root);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005225 btrfs_end_transaction(trans);
Josef Bacika41ad392011-01-31 15:30:16 -05005226 } else {
Yan, Zheng80825102009-11-12 09:35:36 +00005227
Josef Bacika41ad392011-01-31 15:30:16 -05005228 /*
5229 * We're truncating a file that used to have good data down to
5230 * zero. Make sure it gets into the ordered flush list so that
5231 * any new writes get down to disk quickly.
5232 */
5233 if (newsize == 0)
Josef Bacik72ac3c02012-05-23 14:13:11 -04005234 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
5235 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00005236
Josef Bacika41ad392011-01-31 15:30:16 -05005237 truncate_setsize(inode, newsize);
Miao Xie2e60a512013-02-08 07:01:08 +00005238
Andrea Gelmini52042d82018-11-28 12:05:13 +01005239 /* Disable nonlocked read DIO to avoid the endless truncate */
Nikolay Borisovabcefb12017-02-20 13:51:10 +02005240 btrfs_inode_block_unlocked_dio(BTRFS_I(inode));
Miao Xie2e60a512013-02-08 07:01:08 +00005241 inode_dio_wait(inode);
Nikolay Borisov0b581702017-02-20 13:51:11 +02005242 btrfs_inode_resume_unlocked_dio(BTRFS_I(inode));
Miao Xie2e60a512013-02-08 07:01:08 +00005243
Filipe Manana213e8c52018-02-06 20:40:31 +00005244 ret = btrfs_truncate(inode, newsize == oldsize);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005245 if (ret && inode->i_nlink) {
5246 int err;
5247
5248 /*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07005249 * Truncate failed, so fix up the in-memory size. We
5250 * adjusted disk_i_size down as we removed extents, so
5251 * wait for disk_i_size to be stable and then update the
5252 * in-memory size to match.
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005253 */
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07005254 err = btrfs_wait_ordered_range(inode, 0, (u64)-1);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005255 if (err)
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07005256 return err;
5257 i_size_write(inode, BTRFS_I(inode)->disk_i_size);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005258 }
Yan, Zheng80825102009-11-12 09:35:36 +00005259 }
5260
Josef Bacika41ad392011-01-31 15:30:16 -05005261 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00005262}
5263
Chris Mason39279cc2007-06-12 06:35:45 -04005264static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
5265{
David Howells2b0143b2015-03-17 22:25:59 +00005266 struct inode *inode = d_inode(dentry);
Li Zefanb83cc962010-12-20 16:04:08 +08005267 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005268 int err;
5269
Li Zefanb83cc962010-12-20 16:04:08 +08005270 if (btrfs_root_readonly(root))
5271 return -EROFS;
5272
Jan Kara31051c82016-05-26 16:55:18 +02005273 err = setattr_prepare(dentry, attr);
Chris Mason39279cc2007-06-12 06:35:45 -04005274 if (err)
5275 return err;
5276
Chris Mason5a3f23d2009-03-31 13:27:11 -04005277 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Eric Sandeen3972f262013-01-12 02:57:22 +00005278 err = btrfs_setsize(inode, attr);
Yan, Zheng80825102009-11-12 09:35:36 +00005279 if (err)
5280 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005281 }
Yan Zheng9036c102008-10-30 14:19:41 -04005282
Christoph Hellwig10257742010-06-04 11:30:02 +02005283 if (attr->ia_valid) {
5284 setattr_copy(inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005285 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005286 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04005287
Josef Bacik22c44fe2011-11-30 10:45:38 -05005288 if (!err && attr->ia_valid & ATTR_MODE)
Christoph Hellwig996a7102013-12-20 05:16:43 -08005289 err = posix_acl_chmod(inode, inode->i_mode);
Christoph Hellwig10257742010-06-04 11:30:02 +02005290 }
5291
Chris Mason39279cc2007-06-12 06:35:45 -04005292 return err;
5293}
Chris Mason61295eb2008-01-14 16:24:38 -05005294
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005295/*
5296 * While truncating the inode pages during eviction, we get the VFS calling
5297 * btrfs_invalidatepage() against each page of the inode. This is slow because
5298 * the calls to btrfs_invalidatepage() result in a huge amount of calls to
5299 * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting
5300 * extent_state structures over and over, wasting lots of time.
5301 *
5302 * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all
5303 * those expensive operations on a per page basis and do only the ordered io
5304 * finishing, while we release here the extent_map and extent_state structures,
5305 * without the excessive merging and splitting.
5306 */
5307static void evict_inode_truncate_pages(struct inode *inode)
5308{
5309 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
5310 struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree;
5311 struct rb_node *node;
5312
5313 ASSERT(inode->i_state & I_FREEING);
Johannes Weiner91b0abe2014-04-03 14:47:49 -07005314 truncate_inode_pages_final(&inode->i_data);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005315
5316 write_lock(&map_tree->lock);
Liu Bo07e1ce02018-08-23 03:51:52 +08005317 while (!RB_EMPTY_ROOT(&map_tree->map.rb_root)) {
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005318 struct extent_map *em;
5319
Liu Bo07e1ce02018-08-23 03:51:52 +08005320 node = rb_first_cached(&map_tree->map);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005321 em = rb_entry(node, struct extent_map, rb_node);
Wang Shilong180589e2013-12-14 15:27:31 +08005322 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
5323 clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005324 remove_extent_mapping(map_tree, em);
5325 free_extent_map(em);
Filipe Manana7064dd52014-08-08 02:47:05 +01005326 if (need_resched()) {
5327 write_unlock(&map_tree->lock);
5328 cond_resched();
5329 write_lock(&map_tree->lock);
5330 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005331 }
5332 write_unlock(&map_tree->lock);
5333
Filipe Manana6ca07092015-05-26 00:55:42 +01005334 /*
5335 * Keep looping until we have no more ranges in the io tree.
5336 * We can have ongoing bios started by readpages (called from readahead)
Filipe Manana9c6429d2015-06-10 12:55:41 +01005337 * that have their endio callback (extent_io.c:end_bio_extent_readpage)
5338 * still in progress (unlocked the pages in the bio but did not yet
5339 * unlocked the ranges in the io tree). Therefore this means some
Filipe Manana6ca07092015-05-26 00:55:42 +01005340 * ranges can still be locked and eviction started because before
5341 * submitting those bios, which are executed by a separate task (work
5342 * queue kthread), inode references (inode->i_count) were not taken
5343 * (which would be dropped in the end io callback of each bio).
5344 * Therefore here we effectively end up waiting for those bios and
5345 * anyone else holding locked ranges without having bumped the inode's
5346 * reference count - if we don't do it, when they access the inode's
5347 * io_tree to unlock a range it may be too late, leading to an
5348 * use-after-free issue.
5349 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005350 spin_lock(&io_tree->lock);
5351 while (!RB_EMPTY_ROOT(&io_tree->state)) {
5352 struct extent_state *state;
5353 struct extent_state *cached_state = NULL;
Filipe Manana6ca07092015-05-26 00:55:42 +01005354 u64 start;
5355 u64 end;
Filipe Manana421f0922018-10-12 13:02:48 +01005356 unsigned state_flags;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005357
5358 node = rb_first(&io_tree->state);
5359 state = rb_entry(node, struct extent_state, rb_node);
Filipe Manana6ca07092015-05-26 00:55:42 +01005360 start = state->start;
5361 end = state->end;
Filipe Manana421f0922018-10-12 13:02:48 +01005362 state_flags = state->state;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005363 spin_unlock(&io_tree->lock);
5364
David Sterbaff13db42015-12-03 14:30:40 +01005365 lock_extent_bits(io_tree, start, end, &cached_state);
Qu Wenruob9d0b382015-09-29 10:35:16 +08005366
5367 /*
5368 * If still has DELALLOC flag, the extent didn't reach disk,
5369 * and its reserved space won't be freed by delayed_ref.
5370 * So we need to free its reserved space here.
5371 * (Refer to comment in btrfs_invalidatepage, case 2)
5372 *
5373 * Note, end is the bytenr of last byte, so we need + 1 here.
5374 */
Filipe Manana421f0922018-10-12 13:02:48 +01005375 if (state_flags & EXTENT_DELALLOC)
Qu Wenruobc42bda2017-02-27 15:10:39 +08005376 btrfs_qgroup_free_data(inode, NULL, start, end - start + 1);
Qu Wenruob9d0b382015-09-29 10:35:16 +08005377
Filipe Manana6ca07092015-05-26 00:55:42 +01005378 clear_extent_bit(io_tree, start, end,
Omar Sandovale1821632019-08-15 14:04:04 -07005379 EXTENT_LOCKED | EXTENT_DELALLOC |
5380 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1,
5381 &cached_state);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005382
Filipe Manana7064dd52014-08-08 02:47:05 +01005383 cond_resched();
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005384 spin_lock(&io_tree->lock);
5385 }
5386 spin_unlock(&io_tree->lock);
5387}
5388
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005389static struct btrfs_trans_handle *evict_refill_and_join(struct btrfs_root *root,
Josef Bacikad80cf52018-09-28 07:18:19 -04005390 struct btrfs_block_rsv *rsv)
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005391{
5392 struct btrfs_fs_info *fs_info = root->fs_info;
5393 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
Josef Bacikd3984c92019-08-01 18:19:37 -04005394 struct btrfs_trans_handle *trans;
Josef Bacik2bd36e72019-08-22 15:14:33 -04005395 u64 delayed_refs_extra = btrfs_calc_insert_metadata_size(fs_info, 1);
Josef Bacikd3984c92019-08-01 18:19:37 -04005396 int ret;
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005397
Josef Bacikd3984c92019-08-01 18:19:37 -04005398 /*
5399 * Eviction should be taking place at some place safe because of our
5400 * delayed iputs. However the normal flushing code will run delayed
5401 * iputs, so we cannot use FLUSH_ALL otherwise we'll deadlock.
5402 *
5403 * We reserve the delayed_refs_extra here again because we can't use
5404 * btrfs_start_transaction(root, 0) for the same deadlocky reason as
5405 * above. We reserve our extra bit here because we generate a ton of
5406 * delayed refs activity by truncating.
5407 *
5408 * If we cannot make our reservation we'll attempt to steal from the
5409 * global reserve, because we really want to be able to free up space.
5410 */
5411 ret = btrfs_block_rsv_refill(root, rsv, rsv->size + delayed_refs_extra,
5412 BTRFS_RESERVE_FLUSH_EVICT);
5413 if (ret) {
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005414 /*
5415 * Try to steal from the global reserve if there is space for
5416 * it.
5417 */
Josef Bacikd3984c92019-08-01 18:19:37 -04005418 if (btrfs_check_space_for_delayed_refs(fs_info) ||
5419 btrfs_block_rsv_migrate(global_rsv, rsv, rsv->size, 0)) {
5420 btrfs_warn(fs_info,
5421 "could not allocate space for delete; will truncate on mount");
5422 return ERR_PTR(-ENOSPC);
5423 }
5424 delayed_refs_extra = 0;
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005425 }
Josef Bacikd3984c92019-08-01 18:19:37 -04005426
5427 trans = btrfs_join_transaction(root);
5428 if (IS_ERR(trans))
5429 return trans;
5430
5431 if (delayed_refs_extra) {
5432 trans->block_rsv = &fs_info->trans_block_rsv;
5433 trans->bytes_reserved = delayed_refs_extra;
5434 btrfs_block_rsv_migrate(rsv, trans->block_rsv,
5435 delayed_refs_extra, 1);
5436 }
5437 return trans;
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005438}
5439
Al Virobd555972010-06-07 11:35:40 -04005440void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005441{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005442 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04005443 struct btrfs_trans_handle *trans;
5444 struct btrfs_root *root = BTRFS_I(inode)->root;
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005445 struct btrfs_block_rsv *rsv;
Chris Mason39279cc2007-06-12 06:35:45 -04005446 int ret;
5447
liubo1abe9b82011-03-24 11:18:59 +00005448 trace_btrfs_inode_evict(inode);
5449
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005450 if (!root) {
Liu Boe8f1bc12018-01-25 11:02:53 -07005451 clear_inode(inode);
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005452 return;
5453 }
5454
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005455 evict_inode_truncate_pages(inode);
5456
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005457 if (inode->i_nlink &&
5458 ((btrfs_root_refs(&root->root_item) != 0 &&
5459 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) ||
Nikolay Borisov70ddc552017-02-20 13:50:35 +02005460 btrfs_is_free_space_inode(BTRFS_I(inode))))
Al Virobd555972010-06-07 11:35:40 -04005461 goto no_delete;
5462
Omar Sandoval27919062018-05-11 13:13:37 -07005463 if (is_bad_inode(inode))
Chris Mason39279cc2007-06-12 06:35:45 -04005464 goto no_delete;
Chris Mason5f39d392007-10-15 16:14:19 -04005465
Nikolay Borisov7ab79562017-02-20 13:50:57 +02005466 btrfs_free_io_failure_record(BTRFS_I(inode), 0, (u64)-1);
Miao Xief6124962014-09-12 18:44:04 +08005467
Omar Sandoval7b40b692018-05-11 13:13:33 -07005468 if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags))
Yan, Zhengc71bf092009-11-12 09:34:40 +00005469 goto no_delete;
Yan, Zhengc71bf092009-11-12 09:34:40 +00005470
Yan, Zheng76dda932009-09-21 16:00:26 -04005471 if (inode->i_nlink > 0) {
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005472 BUG_ON(btrfs_root_refs(&root->root_item) != 0 &&
5473 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID);
Yan, Zheng76dda932009-09-21 16:00:26 -04005474 goto no_delete;
5475 }
5476
Nikolay Borisovaa790212017-01-10 20:35:40 +02005477 ret = btrfs_commit_inode_delayed_inode(BTRFS_I(inode));
Omar Sandoval27919062018-05-11 13:13:37 -07005478 if (ret)
Miao Xie0e8c36a2012-12-19 06:59:51 +00005479 goto no_delete;
Miao Xie0e8c36a2012-12-19 06:59:51 +00005480
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005481 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Omar Sandoval27919062018-05-11 13:13:37 -07005482 if (!rsv)
Josef Bacik4289a662011-08-05 13:22:24 -04005483 goto no_delete;
Josef Bacik2bd36e72019-08-22 15:14:33 -04005484 rsv->size = btrfs_calc_metadata_size(fs_info, 1);
Josef Bacikca7e70f2012-08-27 17:48:15 -04005485 rsv->failfast = 1;
Josef Bacik4289a662011-08-05 13:22:24 -04005486
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02005487 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Mason5f39d392007-10-15 16:14:19 -04005488
Yan, Zheng80825102009-11-12 09:35:36 +00005489 while (1) {
Josef Bacikad80cf52018-09-28 07:18:19 -04005490 trans = evict_refill_and_join(root, rsv);
Omar Sandoval27919062018-05-11 13:13:37 -07005491 if (IS_ERR(trans))
5492 goto free_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04005493
5494 trans->block_rsv = rsv;
5495
Yan, Zhengd68fc572010-05-16 10:49:58 -04005496 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Omar Sandoval27919062018-05-11 13:13:37 -07005497 trans->block_rsv = &fs_info->trans_block_rsv;
5498 btrfs_end_transaction(trans);
5499 btrfs_btree_balance_dirty(fs_info);
5500 if (ret && ret != -ENOSPC && ret != -EAGAIN)
5501 goto free_rsv;
5502 else if (!ret)
Yan, Zheng80825102009-11-12 09:35:36 +00005503 break;
Josef Bacik7b128762008-07-24 12:17:14 -04005504 }
5505
Josef Bacik4ef31a42013-08-13 14:10:08 -04005506 /*
Omar Sandoval27919062018-05-11 13:13:37 -07005507 * Errors here aren't a big deal, it just means we leave orphan items in
5508 * the tree. They will be cleaned up on the next mount. If the inode
5509 * number gets reused, cleanup deletes the orphan item without doing
5510 * anything, and unlink reuses the existing orphan item.
5511 *
5512 * If it turns out that we are dropping too many of these, we might want
5513 * to add a mechanism for retrying these after a commit.
Josef Bacik4ef31a42013-08-13 14:10:08 -04005514 */
Josef Bacikad80cf52018-09-28 07:18:19 -04005515 trans = evict_refill_and_join(root, rsv);
Omar Sandoval27919062018-05-11 13:13:37 -07005516 if (!IS_ERR(trans)) {
5517 trans->block_rsv = rsv;
5518 btrfs_orphan_del(trans, BTRFS_I(inode));
5519 trans->block_rsv = &fs_info->trans_block_rsv;
5520 btrfs_end_transaction(trans);
5521 }
Chris Mason85e21ba2008-01-29 15:11:36 -05005522
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005523 if (!(root == fs_info->tree_root ||
Li Zefan581bb052011-04-20 10:06:11 +08005524 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005525 btrfs_return_ino(root, btrfs_ino(BTRFS_I(inode)));
Li Zefan581bb052011-04-20 10:06:11 +08005526
Omar Sandoval27919062018-05-11 13:13:37 -07005527free_rsv:
5528 btrfs_free_block_rsv(fs_info, rsv);
Chris Mason39279cc2007-06-12 06:35:45 -04005529no_delete:
Omar Sandoval27919062018-05-11 13:13:37 -07005530 /*
5531 * If we didn't successfully delete, the orphan item will still be in
5532 * the tree and we'll retry on the next mount. Again, we might also want
5533 * to retry these periodically in the future.
5534 */
Nikolay Borisovf48d1cf2017-01-10 20:35:39 +02005535 btrfs_remove_delayed_node(BTRFS_I(inode));
Jan Karadbd57682012-05-03 14:48:02 +02005536 clear_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005537}
5538
5539/*
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005540 * Return the key found in the dir entry in the location pointer, fill @type
5541 * with BTRFS_FT_*, and return 0.
5542 *
Su Yue005d6712018-03-05 17:13:37 +08005543 * If no dir entries were found, returns -ENOENT.
5544 * If found a corrupted location in dir entry, returns -EUCLEAN.
Chris Mason39279cc2007-06-12 06:35:45 -04005545 */
5546static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005547 struct btrfs_key *location, u8 *type)
Chris Mason39279cc2007-06-12 06:35:45 -04005548{
5549 const char *name = dentry->d_name.name;
5550 int namelen = dentry->d_name.len;
5551 struct btrfs_dir_item *di;
5552 struct btrfs_path *path;
5553 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04005554 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005555
5556 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005557 if (!path)
5558 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05005559
David Sterbaf85b7372017-01-20 14:54:07 +01005560 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(BTRFS_I(dir)),
5561 name, namelen, 0);
Liu Bo3cf50682018-09-12 06:06:26 +08005562 if (IS_ERR_OR_NULL(di)) {
5563 ret = di ? PTR_ERR(di) : -ENOENT;
Su Yue005d6712018-03-05 17:13:37 +08005564 goto out;
5565 }
Chris Masond3977122009-01-05 21:25:51 -05005566
Chris Mason5f39d392007-10-15 16:14:19 -04005567 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Liu Bo56a0e702017-10-30 11:14:38 -06005568 if (location->type != BTRFS_INODE_ITEM_KEY &&
5569 location->type != BTRFS_ROOT_ITEM_KEY) {
Su Yue005d6712018-03-05 17:13:37 +08005570 ret = -EUCLEAN;
Liu Bo56a0e702017-10-30 11:14:38 -06005571 btrfs_warn(root->fs_info,
5572"%s gets something invalid in DIR_ITEM (name %s, directory ino %llu, location(%llu %u %llu))",
5573 __func__, name, btrfs_ino(BTRFS_I(dir)),
5574 location->objectid, location->type, location->offset);
Liu Bo56a0e702017-10-30 11:14:38 -06005575 }
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005576 if (!ret)
5577 *type = btrfs_dir_type(path->nodes[0], di);
Chris Mason39279cc2007-06-12 06:35:45 -04005578out:
Chris Mason39279cc2007-06-12 06:35:45 -04005579 btrfs_free_path(path);
5580 return ret;
5581}
5582
5583/*
5584 * when we hit a tree root in a directory, the btrfs part of the inode
5585 * needs to be changed to reflect the root directory of the tree root. This
5586 * is kind of like crossing a mount point.
5587 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005588static int fixup_tree_root_location(struct btrfs_fs_info *fs_info,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005589 struct inode *dir,
5590 struct dentry *dentry,
5591 struct btrfs_key *location,
5592 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04005593{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005594 struct btrfs_path *path;
5595 struct btrfs_root *new_root;
5596 struct btrfs_root_ref *ref;
5597 struct extent_buffer *leaf;
David Sterba1d4c08e2015-01-02 19:36:14 +01005598 struct btrfs_key key;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005599 int ret;
5600 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005601
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005602 path = btrfs_alloc_path();
5603 if (!path) {
5604 err = -ENOMEM;
5605 goto out;
5606 }
Chris Mason39279cc2007-06-12 06:35:45 -04005607
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005608 err = -ENOENT;
David Sterba1d4c08e2015-01-02 19:36:14 +01005609 key.objectid = BTRFS_I(dir)->root->root_key.objectid;
5610 key.type = BTRFS_ROOT_REF_KEY;
5611 key.offset = location->objectid;
5612
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005613 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005614 if (ret) {
5615 if (ret < 0)
5616 err = ret;
5617 goto out;
5618 }
Chris Mason39279cc2007-06-12 06:35:45 -04005619
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005620 leaf = path->nodes[0];
5621 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005622 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(BTRFS_I(dir)) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005623 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
5624 goto out;
5625
5626 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
5627 (unsigned long)(ref + 1),
5628 dentry->d_name.len);
5629 if (ret)
5630 goto out;
5631
David Sterbab3b4aa72011-04-21 01:20:15 +02005632 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005633
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005634 new_root = btrfs_read_fs_root_no_name(fs_info, location);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005635 if (IS_ERR(new_root)) {
5636 err = PTR_ERR(new_root);
5637 goto out;
5638 }
5639
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005640 *sub_root = new_root;
5641 location->objectid = btrfs_root_dirid(&new_root->root_item);
5642 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04005643 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005644 err = 0;
5645out:
5646 btrfs_free_path(path);
5647 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005648}
5649
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005650static void inode_tree_add(struct inode *inode)
5651{
5652 struct btrfs_root *root = BTRFS_I(inode)->root;
5653 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005654 struct rb_node **p;
5655 struct rb_node *parent;
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005656 struct rb_node *new = &BTRFS_I(inode)->rb_node;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005657 u64 ino = btrfs_ino(BTRFS_I(inode));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005658
Al Viro1d3382cb2010-10-23 15:19:20 -04005659 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005660 return;
Miao Xiee1409ce2013-05-15 07:48:16 +00005661 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005662 spin_lock(&root->inode_lock);
Miao Xiee1409ce2013-05-15 07:48:16 +00005663 p = &root->inode_tree.rb_node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005664 while (*p) {
5665 parent = *p;
5666 entry = rb_entry(parent, struct btrfs_inode, rb_node);
5667
David Sterba37508512018-06-29 10:56:40 +02005668 if (ino < btrfs_ino(entry))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005669 p = &parent->rb_left;
David Sterba37508512018-06-29 10:56:40 +02005670 else if (ino > btrfs_ino(entry))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005671 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005672 else {
5673 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04005674 (I_WILL_FREE | I_FREEING)));
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005675 rb_replace_node(parent, new, &root->inode_tree);
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005676 RB_CLEAR_NODE(parent);
5677 spin_unlock(&root->inode_lock);
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005678 return;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005679 }
5680 }
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005681 rb_link_node(new, parent, p);
5682 rb_insert_color(new, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005683 spin_unlock(&root->inode_lock);
5684}
5685
5686static void inode_tree_del(struct inode *inode)
5687{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005688 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005689 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04005690 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005691
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005692 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005693 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005694 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005695 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04005696 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005697 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005698 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04005699
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005700 if (empty && btrfs_root_refs(&root->root_item) == 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005701 synchronize_srcu(&fs_info->subvol_srcu);
Yan, Zheng76dda932009-09-21 16:00:26 -04005702 spin_lock(&root->inode_lock);
5703 empty = RB_EMPTY_ROOT(&root->inode_tree);
5704 spin_unlock(&root->inode_lock);
5705 if (empty)
5706 btrfs_add_dead_root(root);
5707 }
5708}
5709
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005710
Chris Masone02119d2008-09-05 16:13:11 -04005711static int btrfs_init_locked_inode(struct inode *inode, void *p)
5712{
5713 struct btrfs_iget_args *args = p;
Chris Mason90d3e592014-01-09 17:28:00 -08005714 inode->i_ino = args->location->objectid;
5715 memcpy(&BTRFS_I(inode)->location, args->location,
5716 sizeof(*args->location));
Chris Masone02119d2008-09-05 16:13:11 -04005717 BTRFS_I(inode)->root = args->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005718 return 0;
5719}
5720
5721static int btrfs_find_actor(struct inode *inode, void *opaque)
5722{
5723 struct btrfs_iget_args *args = opaque;
Chris Mason90d3e592014-01-09 17:28:00 -08005724 return args->location->objectid == BTRFS_I(inode)->location.objectid &&
Chris Masond3977122009-01-05 21:25:51 -05005725 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005726}
5727
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005728static struct inode *btrfs_iget_locked(struct super_block *s,
Chris Mason90d3e592014-01-09 17:28:00 -08005729 struct btrfs_key *location,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005730 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04005731{
5732 struct inode *inode;
5733 struct btrfs_iget_args args;
Chris Mason90d3e592014-01-09 17:28:00 -08005734 unsigned long hashval = btrfs_inode_hash(location->objectid, root);
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005735
Chris Mason90d3e592014-01-09 17:28:00 -08005736 args.location = location;
Chris Mason39279cc2007-06-12 06:35:45 -04005737 args.root = root;
5738
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005739 inode = iget5_locked(s, hashval, btrfs_find_actor,
Chris Mason39279cc2007-06-12 06:35:45 -04005740 btrfs_init_locked_inode,
5741 (void *)&args);
5742 return inode;
5743}
5744
Balaji Rao1a54ef82008-07-21 02:01:04 +05305745/* Get an inode object given its location and corresponding root.
5746 * Returns in *is_new if the inode was read from disk
5747 */
Filipe Manana4222ea72018-10-24 10:13:03 +01005748struct inode *btrfs_iget_path(struct super_block *s, struct btrfs_key *location,
5749 struct btrfs_root *root, int *new,
5750 struct btrfs_path *path)
Balaji Rao1a54ef82008-07-21 02:01:04 +05305751{
5752 struct inode *inode;
5753
Chris Mason90d3e592014-01-09 17:28:00 -08005754 inode = btrfs_iget_locked(s, location, root);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305755 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005756 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305757
5758 if (inode->i_state & I_NEW) {
Filipe Manana67710892016-06-06 11:51:25 +01005759 int ret;
5760
Filipe Manana4222ea72018-10-24 10:13:03 +01005761 ret = btrfs_read_locked_inode(inode, path);
Al Viro9bc2cef2018-07-29 23:04:50 +01005762 if (!ret) {
Mark Fasheh1748f842011-07-12 11:25:31 -07005763 inode_tree_add(inode);
5764 unlock_new_inode(inode);
5765 if (new)
5766 *new = 1;
5767 } else {
Al Virof5b3a412018-07-29 23:04:51 +01005768 iget_failed(inode);
5769 /*
5770 * ret > 0 can come from btrfs_search_slot called by
5771 * btrfs_read_locked_inode, this means the inode item
5772 * was not found.
5773 */
5774 if (ret > 0)
5775 ret = -ENOENT;
5776 inode = ERR_PTR(ret);
Mark Fasheh1748f842011-07-12 11:25:31 -07005777 }
5778 }
5779
Balaji Rao1a54ef82008-07-21 02:01:04 +05305780 return inode;
5781}
5782
Filipe Manana4222ea72018-10-24 10:13:03 +01005783struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
5784 struct btrfs_root *root, int *new)
5785{
5786 return btrfs_iget_path(s, location, root, new, NULL);
5787}
5788
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005789static struct inode *new_simple_dir(struct super_block *s,
5790 struct btrfs_key *key,
5791 struct btrfs_root *root)
5792{
5793 struct inode *inode = new_inode(s);
5794
5795 if (!inode)
5796 return ERR_PTR(-ENOMEM);
5797
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005798 BTRFS_I(inode)->root = root;
5799 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04005800 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005801
5802 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Li Zefan848cce02012-02-21 17:04:28 +08005803 inode->i_op = &btrfs_dir_ro_inode_operations;
Omar Sandoval1fdf4192017-01-25 17:06:39 -08005804 inode->i_opflags &= ~IOP_XATTR;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005805 inode->i_fop = &simple_dir_operations;
5806 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
Deepa Dinamanic2050a42016-09-14 07:48:06 -07005807 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05305808 inode->i_atime = inode->i_mtime;
5809 inode->i_ctime = inode->i_mtime;
Arnd Bergmannd3c6be6f2018-06-21 18:04:06 +02005810 BTRFS_I(inode)->i_otime = inode->i_mtime;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005811
5812 return inode;
5813}
5814
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005815static inline u8 btrfs_inode_type(struct inode *inode)
5816{
5817 /*
5818 * Compile-time asserts that generic FT_* types still match
5819 * BTRFS_FT_* types
5820 */
5821 BUILD_BUG_ON(BTRFS_FT_UNKNOWN != FT_UNKNOWN);
5822 BUILD_BUG_ON(BTRFS_FT_REG_FILE != FT_REG_FILE);
5823 BUILD_BUG_ON(BTRFS_FT_DIR != FT_DIR);
5824 BUILD_BUG_ON(BTRFS_FT_CHRDEV != FT_CHRDEV);
5825 BUILD_BUG_ON(BTRFS_FT_BLKDEV != FT_BLKDEV);
5826 BUILD_BUG_ON(BTRFS_FT_FIFO != FT_FIFO);
5827 BUILD_BUG_ON(BTRFS_FT_SOCK != FT_SOCK);
5828 BUILD_BUG_ON(BTRFS_FT_SYMLINK != FT_SYMLINK);
5829
5830 return fs_umode_to_ftype(inode->i_mode);
5831}
5832
Chris Mason3de45862008-11-17 21:02:50 -05005833struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04005834{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005835 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masond3977122009-01-05 21:25:51 -05005836 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005837 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005838 struct btrfs_root *sub_root = root;
5839 struct btrfs_key location;
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005840 u8 di_type = 0;
Yan, Zheng76dda932009-09-21 16:00:26 -04005841 int index;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005842 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005843
5844 if (dentry->d_name.len > BTRFS_NAME_LEN)
5845 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04005846
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005847 ret = btrfs_inode_by_name(dir, dentry, &location, &di_type);
Chris Mason39279cc2007-06-12 06:35:45 -04005848 if (ret < 0)
5849 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04005850
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005851 if (location.type == BTRFS_INODE_ITEM_KEY) {
Josef Bacik73f73412009-12-04 17:38:27 +00005852 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005853 if (IS_ERR(inode))
5854 return inode;
5855
5856 /* Do extra check against inode mode with di_type */
5857 if (btrfs_inode_type(inode) != di_type) {
5858 btrfs_crit(fs_info,
5859"inode mode mismatch with dir: inode mode=0%o btrfs type=%u dir type=%u",
5860 inode->i_mode, btrfs_inode_type(inode),
5861 di_type);
5862 iput(inode);
5863 return ERR_PTR(-EUCLEAN);
5864 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005865 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04005866 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005867
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005868 index = srcu_read_lock(&fs_info->subvol_srcu);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005869 ret = fixup_tree_root_location(fs_info, dir, dentry,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005870 &location, &sub_root);
5871 if (ret < 0) {
5872 if (ret != -ENOENT)
5873 inode = ERR_PTR(ret);
5874 else
5875 inode = new_simple_dir(dir->i_sb, &location, sub_root);
5876 } else {
Josef Bacik73f73412009-12-04 17:38:27 +00005877 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04005878 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005879 srcu_read_unlock(&fs_info->subvol_srcu, index);
Yan, Zheng76dda932009-09-21 16:00:26 -04005880
Julia Lawall34d19ba2011-01-24 19:55:19 +00005881 if (!IS_ERR(inode) && root != sub_root) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005882 down_read(&fs_info->cleanup_work_sem);
David Howellsbc98a422017-07-17 08:45:34 +01005883 if (!sb_rdonly(inode->i_sb))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005884 ret = btrfs_orphan_cleanup(sub_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005885 up_read(&fs_info->cleanup_work_sem);
Josef Bacik01cd3362013-06-03 21:39:49 -04005886 if (ret) {
5887 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005888 inode = ERR_PTR(ret);
Josef Bacik01cd3362013-06-03 21:39:49 -04005889 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00005890 }
5891
Chris Mason3de45862008-11-17 21:02:50 -05005892 return inode;
5893}
5894
Nick Pigginfe15ce42011-01-07 17:49:23 +11005895static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04005896{
5897 struct btrfs_root *root;
David Howells2b0143b2015-03-17 22:25:59 +00005898 struct inode *inode = d_inode(dentry);
Yan, Zheng76dda932009-09-21 16:00:26 -04005899
Li Zefan848cce02012-02-21 17:04:28 +08005900 if (!inode && !IS_ROOT(dentry))
David Howells2b0143b2015-03-17 22:25:59 +00005901 inode = d_inode(dentry->d_parent);
Yan, Zheng76dda932009-09-21 16:00:26 -04005902
Li Zefan848cce02012-02-21 17:04:28 +08005903 if (inode) {
5904 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04005905 if (btrfs_root_refs(&root->root_item) == 0)
5906 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08005907
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005908 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Li Zefan848cce02012-02-21 17:04:28 +08005909 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04005910 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005911 return 0;
5912}
5913
Chris Mason3de45862008-11-17 21:02:50 -05005914static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04005915 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05005916{
Al Viro3837d202018-10-10 16:38:27 -04005917 struct inode *inode = btrfs_lookup_dentry(dir, dentry);
Josef Bacika66e7cc2011-09-18 10:34:03 -04005918
Al Viro3837d202018-10-10 16:38:27 -04005919 if (inode == ERR_PTR(-ENOENT))
5920 inode = NULL;
Al Viro41d28bc2014-10-12 22:24:21 -04005921 return d_splice_alias(inode, dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04005922}
5923
Josef Bacik23b5ec72017-07-24 15:14:25 -04005924/*
5925 * All this infrastructure exists because dir_emit can fault, and we are holding
5926 * the tree lock when doing readdir. For now just allocate a buffer and copy
5927 * our information into that, and then dir_emit from the buffer. This is
5928 * similar to what NFS does, only we don't keep the buffer around in pagecache
5929 * because I'm afraid I'll mess that up. Long term we need to make filldir do
5930 * copy_to_user_inatomic so we don't have to worry about page faulting under the
5931 * tree lock.
5932 */
5933static int btrfs_opendir(struct inode *inode, struct file *file)
5934{
5935 struct btrfs_file_private *private;
5936
5937 private = kzalloc(sizeof(struct btrfs_file_private), GFP_KERNEL);
5938 if (!private)
5939 return -ENOMEM;
5940 private->filldir_buf = kzalloc(PAGE_SIZE, GFP_KERNEL);
5941 if (!private->filldir_buf) {
5942 kfree(private);
5943 return -ENOMEM;
5944 }
5945 file->private_data = private;
5946 return 0;
5947}
5948
5949struct dir_entry {
5950 u64 ino;
5951 u64 offset;
5952 unsigned type;
5953 int name_len;
5954};
5955
5956static int btrfs_filldir(void *addr, int entries, struct dir_context *ctx)
5957{
5958 while (entries--) {
5959 struct dir_entry *entry = addr;
5960 char *name = (char *)(entry + 1);
5961
David Sterba92d32172018-04-16 21:10:14 +02005962 ctx->pos = get_unaligned(&entry->offset);
5963 if (!dir_emit(ctx, name, get_unaligned(&entry->name_len),
5964 get_unaligned(&entry->ino),
5965 get_unaligned(&entry->type)))
Josef Bacik23b5ec72017-07-24 15:14:25 -04005966 return 1;
David Sterba92d32172018-04-16 21:10:14 +02005967 addr += sizeof(struct dir_entry) +
5968 get_unaligned(&entry->name_len);
Josef Bacik23b5ec72017-07-24 15:14:25 -04005969 ctx->pos++;
5970 }
5971 return 0;
5972}
5973
Al Viro9cdda8d2013-05-22 16:48:09 -04005974static int btrfs_real_readdir(struct file *file, struct dir_context *ctx)
Chris Mason39279cc2007-06-12 06:35:45 -04005975{
Al Viro9cdda8d2013-05-22 16:48:09 -04005976 struct inode *inode = file_inode(file);
Chris Mason39279cc2007-06-12 06:35:45 -04005977 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005978 struct btrfs_file_private *private = file->private_data;
Chris Mason39279cc2007-06-12 06:35:45 -04005979 struct btrfs_dir_item *di;
5980 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04005981 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04005982 struct btrfs_path *path;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005983 void *addr;
Miao Xie16cdcec2011-04-22 18:12:22 +08005984 struct list_head ins_list;
5985 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04005986 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04005987 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04005988 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04005989 char *name_ptr;
5990 int name_len;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005991 int entries = 0;
5992 int total_len = 0;
Omar Sandoval02dbfc92016-05-20 13:50:33 -07005993 bool put = false;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005994 struct btrfs_key location;
Chris Mason5f39d392007-10-15 16:14:19 -04005995
Al Viro9cdda8d2013-05-22 16:48:09 -04005996 if (!dir_emit_dots(file, ctx))
5997 return 0;
5998
David Woodhouse49593bf2008-08-17 17:08:36 +01005999 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08006000 if (!path)
6001 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04006002
Josef Bacik23b5ec72017-07-24 15:14:25 -04006003 addr = private->filldir_buf;
David Sterbae4058b52015-11-27 16:31:35 +01006004 path->reada = READA_FORWARD;
David Woodhouse49593bf2008-08-17 17:08:36 +01006005
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006006 INIT_LIST_HEAD(&ins_list);
6007 INIT_LIST_HEAD(&del_list);
6008 put = btrfs_readdir_get_delayed_items(inode, &ins_list, &del_list);
Miao Xie16cdcec2011-04-22 18:12:22 +08006009
Josef Bacik23b5ec72017-07-24 15:14:25 -04006010again:
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006011 key.type = BTRFS_DIR_INDEX_KEY;
Al Viro9cdda8d2013-05-22 16:48:09 -04006012 key.offset = ctx->pos;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02006013 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason5f39d392007-10-15 16:14:19 -04006014
Chris Mason39279cc2007-06-12 06:35:45 -04006015 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
6016 if (ret < 0)
6017 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01006018
6019 while (1) {
Josef Bacik23b5ec72017-07-24 15:14:25 -04006020 struct dir_entry *entry;
6021
Chris Mason5f39d392007-10-15 16:14:19 -04006022 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04006023 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08006024 if (slot >= btrfs_header_nritems(leaf)) {
6025 ret = btrfs_next_leaf(root, path);
6026 if (ret < 0)
6027 goto err;
6028 else if (ret > 0)
6029 break;
6030 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04006031 }
Chris Mason3de45862008-11-17 21:02:50 -05006032
Chris Mason5f39d392007-10-15 16:14:19 -04006033 btrfs_item_key_to_cpu(leaf, &found_key, slot);
6034
6035 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04006036 break;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006037 if (found_key.type != BTRFS_DIR_INDEX_KEY)
Chris Mason39279cc2007-06-12 06:35:45 -04006038 break;
Al Viro9cdda8d2013-05-22 16:48:09 -04006039 if (found_key.offset < ctx->pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08006040 goto next;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006041 if (btrfs_should_delete_dir_index(&del_list, found_key.offset))
Miao Xie16cdcec2011-04-22 18:12:22 +08006042 goto next;
Chris Mason39279cc2007-06-12 06:35:45 -04006043 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006044 name_len = btrfs_dir_name_len(leaf, di);
Josef Bacik23b5ec72017-07-24 15:14:25 -04006045 if ((total_len + sizeof(struct dir_entry) + name_len) >=
6046 PAGE_SIZE) {
6047 btrfs_release_path(path);
6048 ret = btrfs_filldir(private->filldir_buf, entries, ctx);
6049 if (ret)
6050 goto nopos;
6051 addr = private->filldir_buf;
6052 entries = 0;
6053 total_len = 0;
6054 goto again;
Chris Mason39279cc2007-06-12 06:35:45 -04006055 }
Josef Bacik23b5ec72017-07-24 15:14:25 -04006056
6057 entry = addr;
David Sterba92d32172018-04-16 21:10:14 +02006058 put_unaligned(name_len, &entry->name_len);
Josef Bacik23b5ec72017-07-24 15:14:25 -04006059 name_ptr = (char *)(entry + 1);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006060 read_extent_buffer(leaf, name_ptr, (unsigned long)(di + 1),
6061 name_len);
Phillip Potter7d157c32019-03-26 21:39:34 +00006062 put_unaligned(fs_ftype_to_dtype(btrfs_dir_type(leaf, di)),
David Sterba92d32172018-04-16 21:10:14 +02006063 &entry->type);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006064 btrfs_dir_item_key_to_cpu(leaf, di, &location);
David Sterba92d32172018-04-16 21:10:14 +02006065 put_unaligned(location.objectid, &entry->ino);
6066 put_unaligned(found_key.offset, &entry->offset);
Josef Bacik23b5ec72017-07-24 15:14:25 -04006067 entries++;
6068 addr += sizeof(struct dir_entry) + name_len;
6069 total_len += sizeof(struct dir_entry) + name_len;
Li Zefanb9e03af2011-03-23 10:43:58 +08006070next:
6071 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04006072 }
Josef Bacik23b5ec72017-07-24 15:14:25 -04006073 btrfs_release_path(path);
6074
6075 ret = btrfs_filldir(private->filldir_buf, entries, ctx);
6076 if (ret)
6077 goto nopos;
David Woodhouse49593bf2008-08-17 17:08:36 +01006078
Jeff Mahoneyd2fbb2b2016-11-05 13:26:35 -04006079 ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006080 if (ret)
David Sterbabc4ef752015-11-13 13:44:28 +01006081 goto nopos;
6082
Zach Browndb62efb2013-07-11 16:19:42 -07006083 /*
6084 * Stop new entries from being returned after we return the last
6085 * entry.
6086 *
6087 * New directory entries are assigned a strictly increasing
6088 * offset. This means that new entries created during readdir
6089 * are *guaranteed* to be seen in the future by that readdir.
6090 * This has broken buggy programs which operate on names as
6091 * they're returned by readdir. Until we re-use freed offsets
6092 * we have this hack to stop new entries from being returned
6093 * under the assumption that they'll never reach this huge
6094 * offset.
6095 *
6096 * This is being careful not to overflow 32bit loff_t unless the
6097 * last entry requires it because doing so has broken 32bit apps
6098 * in the past.
6099 */
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006100 if (ctx->pos >= INT_MAX)
6101 ctx->pos = LLONG_MAX;
6102 else
6103 ctx->pos = INT_MAX;
Chris Mason39279cc2007-06-12 06:35:45 -04006104nopos:
6105 ret = 0;
6106err:
Omar Sandoval02dbfc92016-05-20 13:50:33 -07006107 if (put)
6108 btrfs_readdir_put_delayed_items(inode, &ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04006109 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04006110 return ret;
6111}
6112
Chris Mason39279cc2007-06-12 06:35:45 -04006113/*
Chris Mason54aa1f42007-06-22 14:16:25 -04006114 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04006115 * inode changes. But, it is most likely to find the inode in cache.
6116 * FIXME, needs more benchmarking...there are no reasons other than performance
6117 * to keep or drop this code.
6118 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00006119static int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04006120{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006121 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04006122 struct btrfs_root *root = BTRFS_I(inode)->root;
6123 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006124 int ret;
6125
Josef Bacik72ac3c02012-05-23 14:13:11 -04006126 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05006127 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006128
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006129 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006130 if (IS_ERR(trans))
6131 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006132
6133 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04006134 if (ret && ret == -ENOSPC) {
6135 /* whoops, lets try again with the full transaction */
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006136 btrfs_end_transaction(trans);
Chris Mason94b60442010-05-26 11:02:00 -04006137 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006138 if (IS_ERR(trans))
6139 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006140
Chris Mason94b60442010-05-26 11:02:00 -04006141 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04006142 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006143 btrfs_end_transaction(trans);
Miao Xie16cdcec2011-04-22 18:12:22 +08006144 if (BTRFS_I(inode)->delayed_node)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006145 btrfs_balance_delayed_items(fs_info);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006146
6147 return ret;
6148}
6149
6150/*
6151 * This is a copy of file_update_time. We need this so we can return error on
6152 * ENOSPC for updating the inode in the case of file write and mmap writes.
6153 */
Deepa Dinamani95582b02018-05-08 19:36:02 -07006154static int btrfs_update_time(struct inode *inode, struct timespec64 *now,
Josef Bacike41f9412012-03-26 09:46:47 -04006155 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05006156{
Alexander Block2bc5565282012-06-15 09:49:33 +02006157 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Layton3a8c7232017-12-11 06:35:24 -05006158 bool dirty = flags & ~S_VERSION;
Alexander Block2bc5565282012-06-15 09:49:33 +02006159
6160 if (btrfs_root_readonly(root))
6161 return -EROFS;
6162
Josef Bacike41f9412012-03-26 09:46:47 -04006163 if (flags & S_VERSION)
Jeff Layton3a8c7232017-12-11 06:35:24 -05006164 dirty |= inode_maybe_inc_iversion(inode, dirty);
Josef Bacike41f9412012-03-26 09:46:47 -04006165 if (flags & S_CTIME)
6166 inode->i_ctime = *now;
6167 if (flags & S_MTIME)
6168 inode->i_mtime = *now;
6169 if (flags & S_ATIME)
6170 inode->i_atime = *now;
Jeff Layton3a8c7232017-12-11 06:35:24 -05006171 return dirty ? btrfs_dirty_inode(inode) : 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006172}
6173
Chris Masond352ac62008-09-29 15:18:18 -04006174/*
6175 * find the highest existing sequence number in a directory
6176 * and then set the in-memory index_cnt variable to reflect
6177 * free sequence numbers
6178 */
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006179static int btrfs_set_inode_index_count(struct btrfs_inode *inode)
Josef Bacikaec74772008-07-24 12:12:38 -04006180{
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006181 struct btrfs_root *root = inode->root;
Josef Bacikaec74772008-07-24 12:12:38 -04006182 struct btrfs_key key, found_key;
6183 struct btrfs_path *path;
6184 struct extent_buffer *leaf;
6185 int ret;
6186
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006187 key.objectid = btrfs_ino(inode);
David Sterba962a2982014-06-04 18:41:45 +02006188 key.type = BTRFS_DIR_INDEX_KEY;
Josef Bacikaec74772008-07-24 12:12:38 -04006189 key.offset = (u64)-1;
6190
6191 path = btrfs_alloc_path();
6192 if (!path)
6193 return -ENOMEM;
6194
6195 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
6196 if (ret < 0)
6197 goto out;
6198 /* FIXME: we should be able to handle this */
6199 if (ret == 0)
6200 goto out;
6201 ret = 0;
6202
6203 /*
6204 * MAGIC NUMBER EXPLANATION:
6205 * since we search a directory based on f_pos we have to start at 2
6206 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
6207 * else has to start at 2
6208 */
6209 if (path->slots[0] == 0) {
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006210 inode->index_cnt = 2;
Josef Bacikaec74772008-07-24 12:12:38 -04006211 goto out;
6212 }
6213
6214 path->slots[0]--;
6215
6216 leaf = path->nodes[0];
6217 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6218
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006219 if (found_key.objectid != btrfs_ino(inode) ||
David Sterba962a2982014-06-04 18:41:45 +02006220 found_key.type != BTRFS_DIR_INDEX_KEY) {
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006221 inode->index_cnt = 2;
Josef Bacikaec74772008-07-24 12:12:38 -04006222 goto out;
6223 }
6224
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006225 inode->index_cnt = found_key.offset + 1;
Josef Bacikaec74772008-07-24 12:12:38 -04006226out:
6227 btrfs_free_path(path);
6228 return ret;
6229}
6230
Chris Masond352ac62008-09-29 15:18:18 -04006231/*
6232 * helper to find a free sequence number in a given directory. This current
6233 * code is very simple, later versions will do smarter things in the btree
6234 */
Nikolay Borisov877574e2017-02-20 13:50:33 +02006235int btrfs_set_inode_index(struct btrfs_inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04006236{
6237 int ret = 0;
6238
Nikolay Borisov877574e2017-02-20 13:50:33 +02006239 if (dir->index_cnt == (u64)-1) {
6240 ret = btrfs_inode_delayed_dir_index_count(dir);
Miao Xie16cdcec2011-04-22 18:12:22 +08006241 if (ret) {
6242 ret = btrfs_set_inode_index_count(dir);
6243 if (ret)
6244 return ret;
6245 }
Josef Bacikaec74772008-07-24 12:12:38 -04006246 }
6247
Nikolay Borisov877574e2017-02-20 13:50:33 +02006248 *index = dir->index_cnt;
6249 dir->index_cnt++;
Josef Bacikaec74772008-07-24 12:12:38 -04006250
6251 return ret;
6252}
6253
Chris Masonb0d5d102014-09-08 13:08:51 -07006254static int btrfs_insert_inode_locked(struct inode *inode)
6255{
6256 struct btrfs_iget_args args;
6257 args.location = &BTRFS_I(inode)->location;
6258 args.root = BTRFS_I(inode)->root;
6259
6260 return insert_inode_locked4(inode,
6261 btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root),
6262 btrfs_find_actor, &args);
6263}
6264
Anand Jain19aee8d2017-07-18 17:37:05 +08006265/*
6266 * Inherit flags from the parent inode.
6267 *
6268 * Currently only the compression flags and the cow flags are inherited.
6269 */
6270static void btrfs_inherit_iflags(struct inode *inode, struct inode *dir)
6271{
6272 unsigned int flags;
6273
6274 if (!dir)
6275 return;
6276
6277 flags = BTRFS_I(dir)->flags;
6278
6279 if (flags & BTRFS_INODE_NOCOMPRESS) {
6280 BTRFS_I(inode)->flags &= ~BTRFS_INODE_COMPRESS;
6281 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
6282 } else if (flags & BTRFS_INODE_COMPRESS) {
6283 BTRFS_I(inode)->flags &= ~BTRFS_INODE_NOCOMPRESS;
6284 BTRFS_I(inode)->flags |= BTRFS_INODE_COMPRESS;
6285 }
6286
6287 if (flags & BTRFS_INODE_NODATACOW) {
6288 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
6289 if (S_ISREG(inode->i_mode))
6290 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
6291 }
6292
David Sterba7b6a2212018-03-26 18:40:21 +02006293 btrfs_sync_inode_flags_to_i_flags(inode);
Anand Jain19aee8d2017-07-18 17:37:05 +08006294}
6295
Chris Mason39279cc2007-06-12 06:35:45 -04006296static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
6297 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04006298 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05006299 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04006300 u64 ref_objectid, u64 objectid,
6301 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04006302{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006303 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04006304 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006305 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04006306 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04006307 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05006308 struct btrfs_inode_ref *ref;
6309 struct btrfs_key key[2];
6310 u32 sizes[2];
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006311 int nitems = name ? 2 : 1;
Chris Mason9c583092008-01-29 15:15:18 -05006312 unsigned long ptr;
Josef Bacik11a19a92019-09-09 10:12:04 -04006313 unsigned int nofs_flag;
Chris Mason39279cc2007-06-12 06:35:45 -04006314 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006315
Chris Mason5f39d392007-10-15 16:14:19 -04006316 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07006317 if (!path)
6318 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04006319
Josef Bacik11a19a92019-09-09 10:12:04 -04006320 nofs_flag = memalloc_nofs_save();
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006321 inode = new_inode(fs_info->sb);
Josef Bacik11a19a92019-09-09 10:12:04 -04006322 memalloc_nofs_restore(nofs_flag);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006323 if (!inode) {
6324 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04006325 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006326 }
Chris Mason39279cc2007-06-12 06:35:45 -04006327
Li Zefan581bb052011-04-20 10:06:11 +08006328 /*
Filipe Manana5762b5c2014-08-01 00:10:32 +01006329 * O_TMPFILE, set link count to 0, so that after this point,
6330 * we fill in an inode item with the correct link count.
6331 */
6332 if (!name)
6333 set_nlink(inode, 0);
6334
6335 /*
Li Zefan581bb052011-04-20 10:06:11 +08006336 * we have to initialize this early, so we can reclaim the inode
6337 * number if we fail afterwards in this function.
6338 */
6339 inode->i_ino = objectid;
6340
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006341 if (dir && name) {
liubo1abe9b82011-03-24 11:18:59 +00006342 trace_btrfs_inode_request(dir);
6343
Nikolay Borisov877574e2017-02-20 13:50:33 +02006344 ret = btrfs_set_inode_index(BTRFS_I(dir), index);
Shen Feng09771432009-04-02 16:46:06 -04006345 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006346 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04006347 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04006348 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04006349 }
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006350 } else if (dir) {
6351 *index = 0;
Josef Bacikaec74772008-07-24 12:12:38 -04006352 }
6353 /*
6354 * index_cnt is ignored for everything but a dir,
Su Yuedf6703e2018-01-12 11:08:02 +08006355 * btrfs_set_inode_index_count has an explanation for the magic
Josef Bacikaec74772008-07-24 12:12:38 -04006356 * number
6357 */
6358 BTRFS_I(inode)->index_cnt = 2;
Miao Xie67de1172013-12-26 13:07:06 +08006359 BTRFS_I(inode)->dir_index = *index;
Chris Mason39279cc2007-06-12 06:35:45 -04006360 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04006361 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00006362 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db22007-08-27 16:49:44 -04006363
Josef Bacik5dc562c2012-08-17 13:14:17 -04006364 /*
6365 * We could have gotten an inode number from somebody who was fsynced
6366 * and then removed in this same transaction, so let's just set full
6367 * sync since it will be a full sync anyway and this will blow away the
6368 * old info in the log.
6369 */
6370 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
6371
Chris Mason9c583092008-01-29 15:15:18 -05006372 key[0].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006373 key[0].type = BTRFS_INODE_ITEM_KEY;
Chris Mason9c583092008-01-29 15:15:18 -05006374 key[0].offset = 0;
6375
Chris Mason9c583092008-01-29 15:15:18 -05006376 sizes[0] = sizeof(struct btrfs_inode_item);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006377
6378 if (name) {
6379 /*
6380 * Start new inodes with an inode_ref. This is slightly more
6381 * efficient for small numbers of hard links since they will
6382 * be packed into one item. Extended refs will kick in if we
6383 * add more hard links than can fit in the ref item.
6384 */
6385 key[1].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006386 key[1].type = BTRFS_INODE_REF_KEY;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006387 key[1].offset = ref_objectid;
6388
6389 sizes[1] = name_len + sizeof(*ref);
6390 }
Chris Mason9c583092008-01-29 15:15:18 -05006391
Chris Masonb0d5d102014-09-08 13:08:51 -07006392 location = &BTRFS_I(inode)->location;
6393 location->objectid = objectid;
6394 location->offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02006395 location->type = BTRFS_INODE_ITEM_KEY;
Chris Masonb0d5d102014-09-08 13:08:51 -07006396
6397 ret = btrfs_insert_inode_locked(inode);
Al Viro32955c52018-05-16 12:20:05 -04006398 if (ret < 0) {
6399 iput(inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006400 goto fail;
Al Viro32955c52018-05-16 12:20:05 -04006401 }
Chris Masonb0d5d102014-09-08 13:08:51 -07006402
Chris Masonb9473432009-03-13 11:00:37 -04006403 path->leave_spinning = 1;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006404 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems);
Chris Mason9c583092008-01-29 15:15:18 -05006405 if (ret != 0)
Chris Masonb0d5d102014-09-08 13:08:51 -07006406 goto fail_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04006407
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03006408 inode_init_owner(inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04006409 inode_set_bytes(inode, 0);
chandan r9cc97d62012-07-04 12:48:07 +05306410
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006411 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05306412 inode->i_atime = inode->i_mtime;
6413 inode->i_ctime = inode->i_mtime;
Arnd Bergmannd3c6be6f2018-06-21 18:04:06 +02006414 BTRFS_I(inode)->i_otime = inode->i_mtime;
chandan r9cc97d62012-07-04 12:48:07 +05306415
Chris Mason5f39d392007-10-15 16:14:19 -04006416 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
6417 struct btrfs_inode_item);
David Sterbab159fa22016-11-08 18:09:03 +01006418 memzero_extent_buffer(path->nodes[0], (unsigned long)inode_item,
Li Zefan293f7e02012-07-10 00:58:58 -06006419 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04006420 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05006421
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006422 if (name) {
6423 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
6424 struct btrfs_inode_ref);
6425 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
6426 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
6427 ptr = (unsigned long)(ref + 1);
6428 write_extent_buffer(path->nodes[0], name, ptr, name_len);
6429 }
Chris Mason9c583092008-01-29 15:15:18 -05006430
Chris Mason5f39d392007-10-15 16:14:19 -04006431 btrfs_mark_buffer_dirty(path->nodes[0]);
6432 btrfs_free_path(path);
6433
Christoph Hellwig6cbff002009-04-17 10:37:41 +02006434 btrfs_inherit_iflags(inode, dir);
6435
Al Viro569254b2011-07-24 17:08:40 -04006436 if (S_ISREG(mode)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006437 if (btrfs_test_opt(fs_info, NODATASUM))
Chris Mason94272162009-07-02 12:26:06 -04006438 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006439 if (btrfs_test_opt(fs_info, NODATACOW))
Josef Bacikf2bdf9a2013-02-21 15:28:28 -05006440 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
6441 BTRFS_INODE_NODATASUM;
Chris Mason94272162009-07-02 12:26:06 -04006442 }
6443
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006444 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00006445
6446 trace_btrfs_inode_new(inode);
Chris Mason1973f0f2011-06-24 13:13:29 -04006447 btrfs_set_inode_last_trans(trans, inode);
liubo1abe9b82011-03-24 11:18:59 +00006448
Alexander Block8ea05e32012-07-25 17:35:53 +02006449 btrfs_update_root_times(trans, root);
6450
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006451 ret = btrfs_inode_inherit_props(trans, inode, dir);
6452 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006453 btrfs_err(fs_info,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006454 "error inheriting props for ino %llu (root %llu): %d",
David Sterbaf85b7372017-01-20 14:54:07 +01006455 btrfs_ino(BTRFS_I(inode)), root->root_key.objectid, ret);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006456
Chris Mason39279cc2007-06-12 06:35:45 -04006457 return inode;
Chris Masonb0d5d102014-09-08 13:08:51 -07006458
6459fail_unlock:
Al Viro32955c52018-05-16 12:20:05 -04006460 discard_new_inode(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006461fail:
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006462 if (dir && name)
Josef Bacikaec74772008-07-24 12:12:38 -04006463 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04006464 btrfs_free_path(path);
6465 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006466}
6467
Chris Masond352ac62008-09-29 15:18:18 -04006468/*
6469 * utility function to add 'inode' into 'parent_inode' with
6470 * a give name and a given sequence number.
6471 * if 'add_backref' is true, also insert a backref from the
6472 * inode to the parent directory.
6473 */
Chris Masone02119d2008-09-05 16:13:11 -04006474int btrfs_add_link(struct btrfs_trans_handle *trans,
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006475 struct btrfs_inode *parent_inode, struct btrfs_inode *inode,
Chris Masone02119d2008-09-05 16:13:11 -04006476 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006477{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006478 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006479 struct btrfs_key key;
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006480 struct btrfs_root *root = parent_inode->root;
6481 u64 ino = btrfs_ino(inode);
6482 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006483
Li Zefan33345d012011-04-20 10:31:50 +08006484 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006485 memcpy(&key, &inode->root->root_key, sizeof(key));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006486 } else {
Li Zefan33345d012011-04-20 10:31:50 +08006487 key.objectid = ino;
David Sterba962a2982014-06-04 18:41:45 +02006488 key.type = BTRFS_INODE_ITEM_KEY;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006489 key.offset = 0;
6490 }
Chris Mason39279cc2007-06-12 06:35:45 -04006491
Li Zefan33345d012011-04-20 10:31:50 +08006492 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Lu Fengqi6025c192018-08-01 11:32:29 +08006493 ret = btrfs_add_root_ref(trans, key.objectid,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006494 root->root_key.objectid, parent_ino,
6495 index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006496 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08006497 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
6498 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006499 }
6500
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006501 /* Nothing to clean up yet */
6502 if (ret)
6503 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006504
Lu Fengqi684572d2018-08-04 21:10:57 +08006505 ret = btrfs_insert_dir_item(trans, name, name_len, parent_inode, &key,
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006506 btrfs_inode_type(&inode->vfs_inode), index);
Chris Mason9c520572012-12-17 14:26:57 -05006507 if (ret == -EEXIST || ret == -EOVERFLOW)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006508 goto fail_dir_item;
6509 else if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04006510 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006511 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006512 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006513
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006514 btrfs_i_size_write(parent_inode, parent_inode->vfs_inode.i_size +
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006515 name_len * 2);
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006516 inode_inc_iversion(&parent_inode->vfs_inode);
Filipe Manana5338e432019-05-15 16:02:47 +01006517 /*
6518 * If we are replaying a log tree, we do not want to update the mtime
6519 * and ctime of the parent directory with the current time, since the
6520 * log replay procedure is responsible for setting them to their correct
6521 * values (the ones it had when the fsync was done).
6522 */
6523 if (!test_bit(BTRFS_FS_LOG_RECOVERING, &root->fs_info->flags)) {
6524 struct timespec64 now = current_time(&parent_inode->vfs_inode);
6525
6526 parent_inode->vfs_inode.i_mtime = now;
6527 parent_inode->vfs_inode.i_ctime = now;
6528 }
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006529 ret = btrfs_update_inode(trans, root, &parent_inode->vfs_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006530 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04006531 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006532 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05006533
6534fail_dir_item:
6535 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
6536 u64 local_index;
6537 int err;
Lu Fengqi3ee1c552018-08-01 11:32:28 +08006538 err = btrfs_del_root_ref(trans, key.objectid,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006539 root->root_key.objectid, parent_ino,
6540 &local_index, name, name_len);
Johannes Thumshirn1690dd412018-12-12 15:14:17 +01006541 if (err)
6542 btrfs_abort_transaction(trans, err);
Chris Masonfe66a052012-02-20 08:40:56 -05006543 } else if (add_backref) {
6544 u64 local_index;
6545 int err;
6546
6547 err = btrfs_del_inode_ref(trans, root, name, name_len,
6548 ino, parent_ino, &local_index);
Johannes Thumshirn1690dd412018-12-12 15:14:17 +01006549 if (err)
6550 btrfs_abort_transaction(trans, err);
Chris Masonfe66a052012-02-20 08:40:56 -05006551 }
Johannes Thumshirn1690dd412018-12-12 15:14:17 +01006552
6553 /* Return the original error code */
Chris Masonfe66a052012-02-20 08:40:56 -05006554 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006555}
6556
6557static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006558 struct btrfs_inode *dir, struct dentry *dentry,
6559 struct btrfs_inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006560{
Josef Bacika1b075d2010-11-19 20:36:11 +00006561 int err = btrfs_add_link(trans, dir, inode,
6562 dentry->d_name.name, dentry->d_name.len,
6563 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006564 if (err > 0)
6565 err = -EEXIST;
6566 return err;
6567}
6568
Josef Bacik618e21d2007-07-11 10:18:17 -04006569static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04006570 umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04006571{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006572 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Josef Bacik618e21d2007-07-11 10:18:17 -04006573 struct btrfs_trans_handle *trans;
6574 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006575 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04006576 int err;
Josef Bacik618e21d2007-07-11 10:18:17 -04006577 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006578 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04006579
Josef Bacik9ed74f22009-09-11 16:12:44 -04006580 /*
6581 * 2 for inode item and ref
6582 * 2 for dir items
6583 * 1 for xattr if selinux is on
6584 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006585 trans = btrfs_start_transaction(root, 5);
6586 if (IS_ERR(trans))
6587 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05006588
Li Zefan581bb052011-04-20 10:06:11 +08006589 err = btrfs_find_free_ino(root, &objectid);
6590 if (err)
6591 goto out_unlock;
6592
Josef Bacikaec74772008-07-24 12:12:38 -04006593 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006594 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6595 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006596 if (IS_ERR(inode)) {
6597 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -04006598 inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04006599 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006600 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006601
Casey Schauflerad19db72011-12-15 10:09:07 -05006602 /*
6603 * If the active LSM wants to access the inode during
6604 * d_instantiate it needs these. Smack checks to see
6605 * if the filesystem supports xattrs by looking at the
6606 * ops vector.
6607 */
Casey Schauflerad19db72011-12-15 10:09:07 -05006608 inode->i_op = &btrfs_special_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006609 init_special_inode(inode, inode->i_mode, rdev);
6610
6611 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik618e21d2007-07-11 10:18:17 -04006612 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006613 goto out_unlock;
Chris Masonb0d5d102014-09-08 13:08:51 -07006614
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006615 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6616 0, index);
Al Viro32955c52018-05-16 12:20:05 -04006617 if (err)
6618 goto out_unlock;
6619
6620 btrfs_update_inode(trans, root, inode);
6621 d_instantiate_new(dentry, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006622
Josef Bacik618e21d2007-07-11 10:18:17 -04006623out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006624 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006625 btrfs_btree_balance_dirty(fs_info);
Al Viro32955c52018-05-16 12:20:05 -04006626 if (err && inode) {
Josef Bacik618e21d2007-07-11 10:18:17 -04006627 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -04006628 discard_new_inode(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006629 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006630 return err;
6631}
6632
Chris Mason39279cc2007-06-12 06:35:45 -04006633static int btrfs_create(struct inode *dir, struct dentry *dentry,
Al Viroebfc3b42012-06-10 18:05:36 -04006634 umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -04006635{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006636 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04006637 struct btrfs_trans_handle *trans;
6638 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006639 struct inode *inode = NULL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006640 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04006641 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006642 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006643
Josef Bacik9ed74f22009-09-11 16:12:44 -04006644 /*
6645 * 2 for inode item and ref
6646 * 2 for dir items
6647 * 1 for xattr if selinux is on
6648 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006649 trans = btrfs_start_transaction(root, 5);
6650 if (IS_ERR(trans))
6651 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006652
Li Zefan581bb052011-04-20 10:06:11 +08006653 err = btrfs_find_free_ino(root, &objectid);
6654 if (err)
6655 goto out_unlock;
6656
Josef Bacikaec74772008-07-24 12:12:38 -04006657 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006658 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6659 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006660 if (IS_ERR(inode)) {
6661 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -04006662 inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006663 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006664 }
Casey Schauflerad19db72011-12-15 10:09:07 -05006665 /*
6666 * If the active LSM wants to access the inode during
6667 * d_instantiate it needs these. Smack checks to see
6668 * if the filesystem supports xattrs by looking at the
6669 * ops vector.
6670 */
6671 inode->i_fop = &btrfs_file_operations;
6672 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006673 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006674
6675 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
6676 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006677 goto out_unlock;
Chris Masonb0d5d102014-09-08 13:08:51 -07006678
6679 err = btrfs_update_inode(trans, root, inode);
6680 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006681 goto out_unlock;
Casey Schauflerad19db72011-12-15 10:09:07 -05006682
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006683 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6684 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006685 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006686 goto out_unlock;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006687
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006688 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Al Viro1e2e5472018-05-04 08:23:01 -04006689 d_instantiate_new(dentry, inode);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006690
Chris Mason39279cc2007-06-12 06:35:45 -04006691out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006692 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -04006693 if (err && inode) {
Chris Mason39279cc2007-06-12 06:35:45 -04006694 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -04006695 discard_new_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006696 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006697 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006698 return err;
6699}
6700
6701static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
6702 struct dentry *dentry)
6703{
Filipe Manana271dba452016-01-05 16:24:05 +00006704 struct btrfs_trans_handle *trans = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006705 struct btrfs_root *root = BTRFS_I(dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00006706 struct inode *inode = d_inode(old_dentry);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006707 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason00e4e6b2008-08-05 11:18:09 -04006708 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04006709 int err;
6710 int drop_inode = 0;
6711
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006712 /* do not allow sys_link's with other subvols of the same device */
Misono Tomohiro4fd786e2018-08-06 14:25:24 +09006713 if (root->root_key.objectid != BTRFS_I(inode)->root->root_key.objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00006714 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006715
Mark Fashehf1863732012-08-08 11:32:27 -07006716 if (inode->i_nlink >= BTRFS_LINK_MAX)
Al Viroc055e992011-03-04 17:15:18 +00006717 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04006718
Nikolay Borisov877574e2017-02-20 13:50:33 +02006719 err = btrfs_set_inode_index(BTRFS_I(dir), &index);
Josef Bacikaec74772008-07-24 12:12:38 -04006720 if (err)
6721 goto fail;
6722
Yan, Zhenga22285a2010-05-16 10:48:46 -04006723 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00006724 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04006725 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00006726 * 1 item for parent inode
Omar Sandoval399b0bb2018-05-11 13:13:40 -07006727 * 1 item for orphan item deletion if O_TMPFILE
Yan, Zhenga22285a2010-05-16 10:48:46 -04006728 */
Omar Sandoval399b0bb2018-05-11 13:13:40 -07006729 trans = btrfs_start_transaction(root, inode->i_nlink ? 5 : 6);
Yan, Zhenga22285a2010-05-16 10:48:46 -04006730 if (IS_ERR(trans)) {
6731 err = PTR_ERR(trans);
Filipe Manana271dba452016-01-05 16:24:05 +00006732 trans = NULL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006733 goto fail;
6734 }
Chris Mason5f39d392007-10-15 16:14:19 -04006735
Miao Xie67de1172013-12-26 13:07:06 +08006736 /* There are several dir indexes for this inode, clear the cache. */
6737 BTRFS_I(inode)->dir_index = 0ULL;
Zach Brown8b558c52013-10-16 12:10:34 -07006738 inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04006739 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006740 inode->i_ctime = current_time(inode);
Al Viro7de9c6ee2010-10-23 11:11:40 -04006741 ihold(inode);
Josef Bacike9976152012-10-11 15:53:56 -04006742 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
Josef Bacikaec74772008-07-24 12:12:38 -04006743
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006744 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6745 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04006746
Yan, Zhenga5719522009-09-24 09:17:31 -04006747 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006748 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04006749 } else {
Al Viro10d9f302011-07-16 23:09:10 -04006750 struct dentry *parent = dentry->d_parent;
Filipe Mananad4682ba2018-06-11 19:24:28 +01006751 int ret;
6752
Yan, Zhenga5719522009-09-24 09:17:31 -04006753 err = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006754 if (err)
6755 goto fail;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006756 if (inode->i_nlink == 1) {
6757 /*
6758 * If new hard link count is 1, it's a file created
6759 * with open(2) O_TMPFILE flag.
6760 */
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02006761 err = btrfs_orphan_del(trans, BTRFS_I(inode));
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006762 if (err)
6763 goto fail;
6764 }
Al Viro08c422c2011-12-23 07:58:13 -05006765 d_instantiate(dentry, inode);
Filipe Mananad4682ba2018-06-11 19:24:28 +01006766 ret = btrfs_log_new_name(trans, BTRFS_I(inode), NULL, parent,
6767 true, NULL);
6768 if (ret == BTRFS_NEED_TRANS_COMMIT) {
6769 err = btrfs_commit_transaction(trans);
6770 trans = NULL;
6771 }
Yan, Zhenga5719522009-09-24 09:17:31 -04006772 }
Chris Mason39279cc2007-06-12 06:35:45 -04006773
Chris Mason1832a6d2007-12-21 16:27:21 -05006774fail:
Filipe Manana271dba452016-01-05 16:24:05 +00006775 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006776 btrfs_end_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006777 if (drop_inode) {
6778 inode_dec_link_count(inode);
6779 iput(inode);
6780 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006781 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006782 return err;
6783}
6784
Al Viro18bb1db2011-07-26 01:41:39 -04006785static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04006786{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006787 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masonb9d86662008-05-02 16:13:49 -04006788 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006789 struct btrfs_trans_handle *trans;
6790 struct btrfs_root *root = BTRFS_I(dir)->root;
6791 int err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04006792 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006793 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006794
Josef Bacik9ed74f22009-09-11 16:12:44 -04006795 /*
6796 * 2 items for inode and ref
6797 * 2 items for dir items
6798 * 1 for xattr if selinux is on
6799 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006800 trans = btrfs_start_transaction(root, 5);
6801 if (IS_ERR(trans))
6802 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006803
Li Zefan581bb052011-04-20 10:06:11 +08006804 err = btrfs_find_free_ino(root, &objectid);
6805 if (err)
6806 goto out_fail;
6807
Josef Bacikaec74772008-07-24 12:12:38 -04006808 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006809 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6810 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04006811 if (IS_ERR(inode)) {
6812 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -04006813 inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006814 goto out_fail;
6815 }
Chris Mason5f39d392007-10-15 16:14:19 -04006816
Chris Masonb0d5d102014-09-08 13:08:51 -07006817 /* these must be set before we unlock the inode */
6818 inode->i_op = &btrfs_dir_inode_operations;
6819 inode->i_fop = &btrfs_dir_file_operations;
Josef Bacik33268ea2008-07-24 12:16:36 -04006820
Eric Paris2a7dba32011-02-01 11:05:39 -05006821 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04006822 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006823 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04006824
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02006825 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Mason39279cc2007-06-12 06:35:45 -04006826 err = btrfs_update_inode(trans, root, inode);
6827 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006828 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04006829
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006830 err = btrfs_add_link(trans, BTRFS_I(dir), BTRFS_I(inode),
6831 dentry->d_name.name,
6832 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006833 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006834 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04006835
Al Viro1e2e5472018-05-04 08:23:01 -04006836 d_instantiate_new(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006837
6838out_fail:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006839 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -04006840 if (err && inode) {
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006841 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -04006842 discard_new_inode(inode);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006843 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006844 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006845 return err;
6846}
6847
Chris Masonc8b97812008-10-29 14:49:59 -04006848static noinline int uncompress_inline(struct btrfs_path *path,
Byongho Leee40da0e2015-05-19 23:46:45 +09006849 struct page *page,
Chris Masonc8b97812008-10-29 14:49:59 -04006850 size_t pg_offset, u64 extent_offset,
6851 struct btrfs_file_extent_item *item)
6852{
6853 int ret;
6854 struct extent_buffer *leaf = path->nodes[0];
6855 char *tmp;
6856 size_t max_size;
6857 unsigned long inline_size;
6858 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08006859 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04006860
6861 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08006862 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04006863 max_size = btrfs_file_extent_ram_bytes(leaf, item);
6864 inline_size = btrfs_file_extent_inline_item_len(leaf,
Ross Kirkdd3cc162013-09-16 15:58:09 +01006865 btrfs_item_nr(path->slots[0]));
Chris Masonc8b97812008-10-29 14:49:59 -04006866 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04006867 if (!tmp)
6868 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04006869 ptr = btrfs_file_extent_inline_start(item);
6870
6871 read_extent_buffer(leaf, tmp, ptr, inline_size);
6872
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006873 max_size = min_t(unsigned long, PAGE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08006874 ret = btrfs_decompress(compress_type, tmp, page,
6875 extent_offset, inline_size, max_size);
Zygo Blaxelle1699d22017-03-10 16:45:44 -05006876
6877 /*
6878 * decompression code contains a memset to fill in any space between the end
6879 * of the uncompressed data and the end of max_size in case the decompressed
6880 * data ends up shorter than ram_bytes. That doesn't cover the hole between
6881 * the end of an inline extent and the beginning of the next block, so we
6882 * cover that region here.
6883 */
6884
6885 if (max_size + pg_offset < PAGE_SIZE) {
6886 char *map = kmap(page);
6887 memset(map + pg_offset + max_size, 0, PAGE_SIZE - max_size - pg_offset);
6888 kunmap(page);
6889 }
Chris Masonc8b97812008-10-29 14:49:59 -04006890 kfree(tmp);
Zach Brown166ae5a2014-05-09 17:15:10 -04006891 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -04006892}
6893
Chris Masond352ac62008-09-29 15:18:18 -04006894/*
6895 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05006896 * the ugly parts come from merging extents from the disk with the in-ram
6897 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04006898 * where the in-ram extents might be locked pending data=ordered completion.
6899 *
6900 * This also copies inline extents directly into the page.
6901 */
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006902struct extent_map *btrfs_get_extent(struct btrfs_inode *inode,
Liu Bode2c6612018-08-25 13:47:59 +08006903 struct page *page,
6904 size_t pg_offset, u64 start, u64 len,
6905 int create)
Chris Masona52d9a82007-08-27 16:49:44 -04006906{
David Sterba3ffbd682018-06-29 10:56:42 +02006907 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Chris Masona52d9a82007-08-27 16:49:44 -04006908 int ret;
6909 int err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006910 u64 extent_start = 0;
6911 u64 extent_end = 0;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006912 u64 objectid = btrfs_ino(inode);
Linus Torvalds7e74e232019-05-01 12:19:20 -07006913 int extent_type = -1;
Chris Masonf4219502008-07-22 11:18:09 -04006914 struct btrfs_path *path = NULL;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006915 struct btrfs_root *root = inode->root;
Chris Masona52d9a82007-08-27 16:49:44 -04006916 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04006917 struct extent_buffer *leaf;
6918 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04006919 struct extent_map *em = NULL;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006920 struct extent_map_tree *em_tree = &inode->extent_tree;
6921 struct extent_io_tree *io_tree = &inode->io_tree;
Filipe Manana7ffbb592014-06-09 03:48:05 +01006922 const bool new_inline = !page || create;
Chris Masona52d9a82007-08-27 16:49:44 -04006923
Chris Mason890871b2009-09-02 16:24:52 -04006924 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006925 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04006926 if (em)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006927 em->bdev = fs_info->fs_devices->latest_bdev;
Chris Mason890871b2009-09-02 16:24:52 -04006928 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006929
Chris Masona52d9a82007-08-27 16:49:44 -04006930 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04006931 if (em->start > start || em->start + em->len <= start)
6932 free_extent_map(em);
6933 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05006934 free_extent_map(em);
6935 else
6936 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006937 }
David Sterba172ddd62011-04-21 00:48:27 +02006938 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04006939 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05006940 err = -ENOMEM;
6941 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006942 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006943 em->bdev = fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05006944 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05006945 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05006946 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04006947 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04006948
Liu Bobee6ec82018-08-17 05:05:28 +08006949 path = btrfs_alloc_path();
Chris Masonf4219502008-07-22 11:18:09 -04006950 if (!path) {
Liu Bobee6ec82018-08-17 05:05:28 +08006951 err = -ENOMEM;
6952 goto out;
Chris Masonf4219502008-07-22 11:18:09 -04006953 }
6954
Liu Bobee6ec82018-08-17 05:05:28 +08006955 /* Chances are we'll be called again, so go ahead and do readahead */
6956 path->reada = READA_FORWARD;
6957
Liu Boe49aabd2018-08-25 13:47:09 +08006958 /*
6959 * Unless we're going to uncompress the inline extent, no sleep would
6960 * happen.
6961 */
6962 path->leave_spinning = 1;
6963
Nikolay Borisov5c9a7022017-12-01 11:19:40 +02006964 ret = btrfs_lookup_file_extent(NULL, root, path, objectid, start, 0);
Chris Masona52d9a82007-08-27 16:49:44 -04006965 if (ret < 0) {
6966 err = ret;
6967 goto out;
Nikolay Borisovb8eeab72018-12-17 11:49:00 +02006968 } else if (ret > 0) {
Chris Masona52d9a82007-08-27 16:49:44 -04006969 if (path->slots[0] == 0)
6970 goto not_found;
6971 path->slots[0]--;
6972 }
6973
Chris Mason5f39d392007-10-15 16:14:19 -04006974 leaf = path->nodes[0];
6975 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04006976 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04006977 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04006978 if (found_key.objectid != objectid ||
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006979 found_key.type != BTRFS_EXTENT_DATA_KEY) {
Josef Bacik25a50342013-10-14 12:08:38 -04006980 /*
6981 * If we backup past the first extent we want to move forward
6982 * and see if there is an extent in front of us, otherwise we'll
6983 * say there is a hole for our whole search range which can
6984 * cause problems.
6985 */
6986 extent_end = start;
6987 goto next;
Chris Masona52d9a82007-08-27 16:49:44 -04006988 }
6989
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006990 extent_type = btrfs_file_extent_type(leaf, item);
Chris Mason5f39d392007-10-15 16:14:19 -04006991 extent_start = found_key.offset;
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006992 if (extent_type == BTRFS_FILE_EXTENT_REG ||
6993 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08006994 /* Only regular file could have regular/prealloc extent */
6995 if (!S_ISREG(inode->vfs_inode.i_mode)) {
6996 ret = -EUCLEAN;
6997 btrfs_crit(fs_info,
6998 "regular/prealloc extent found for non-regular inode %llu",
6999 btrfs_ino(inode));
7000 goto out;
7001 }
Chris Masona52d9a82007-08-27 16:49:44 -04007002 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04007003 btrfs_file_extent_num_bytes(leaf, item);
Liu Bo09ed2f12017-03-10 11:09:48 -08007004
7005 trace_btrfs_get_extent_show_fi_regular(inode, leaf, item,
7006 extent_start);
Nikolay Borisov694c12e2018-12-17 10:35:59 +02007007 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Yan Zheng9036c102008-10-30 14:19:41 -04007008 size_t size;
Qu Wenruoe41ca582018-06-06 15:41:49 +08007009
7010 size = btrfs_file_extent_ram_bytes(leaf, item);
Jeff Mahoneyda170662016-06-15 09:22:56 -04007011 extent_end = ALIGN(extent_start + size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007012 fs_info->sectorsize);
Liu Bo09ed2f12017-03-10 11:09:48 -08007013
7014 trace_btrfs_get_extent_show_fi_inline(inode, leaf, item,
7015 path->slots[0],
7016 extent_start);
Yan Zheng9036c102008-10-30 14:19:41 -04007017 }
Josef Bacik25a50342013-10-14 12:08:38 -04007018next:
Yan Zheng9036c102008-10-30 14:19:41 -04007019 if (start >= extent_end) {
7020 path->slots[0]++;
7021 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
7022 ret = btrfs_next_leaf(root, path);
7023 if (ret < 0) {
7024 err = ret;
7025 goto out;
Nikolay Borisovb8eeab72018-12-17 11:49:00 +02007026 } else if (ret > 0) {
Yan Zheng9036c102008-10-30 14:19:41 -04007027 goto not_found;
Nikolay Borisovb8eeab72018-12-17 11:49:00 +02007028 }
Yan Zheng9036c102008-10-30 14:19:41 -04007029 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04007030 }
Yan Zheng9036c102008-10-30 14:19:41 -04007031 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
7032 if (found_key.objectid != objectid ||
7033 found_key.type != BTRFS_EXTENT_DATA_KEY)
7034 goto not_found;
7035 if (start + len <= found_key.offset)
7036 goto not_found;
Wang Shilonge2eca692014-07-17 11:44:14 +08007037 if (start > found_key.offset)
7038 goto next;
Nikolay Borisov02a033d2018-12-17 10:36:02 +02007039
7040 /* New extent overlaps with existing one */
Yan Zheng9036c102008-10-30 14:19:41 -04007041 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04007042 em->orig_start = start;
Yan Zheng9036c102008-10-30 14:19:41 -04007043 em->len = found_key.offset - start;
Nikolay Borisov02a033d2018-12-17 10:36:02 +02007044 em->block_start = EXTENT_MAP_HOLE;
7045 goto insert;
Yan Zheng9036c102008-10-30 14:19:41 -04007046 }
7047
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007048 btrfs_extent_item_to_extent_map(inode, path, item,
Nikolay Borisov9cdc5122017-02-20 13:51:02 +02007049 new_inline, em);
Filipe Manana7ffbb592014-06-09 03:48:05 +01007050
Nikolay Borisov694c12e2018-12-17 10:35:59 +02007051 if (extent_type == BTRFS_FILE_EXTENT_REG ||
7052 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04007053 goto insert;
Nikolay Borisov694c12e2018-12-17 10:35:59 +02007054 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04007055 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04007056 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04007057 size_t size;
7058 size_t extent_offset;
7059 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04007060
Filipe Manana7ffbb592014-06-09 03:48:05 +01007061 if (new_inline)
Yan689f9342007-10-29 11:41:07 -04007062 goto out;
Yan689f9342007-10-29 11:41:07 -04007063
Qu Wenruoe41ca582018-06-06 15:41:49 +08007064 size = btrfs_file_extent_ram_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04007065 extent_offset = page_offset(page) + pg_offset - extent_start;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007066 copy_size = min_t(u64, PAGE_SIZE - pg_offset,
7067 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04007068 em->start = extent_start + extent_offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007069 em->len = ALIGN(copy_size, fs_info->sectorsize);
Josef Bacikb4939682012-12-03 10:31:19 -05007070 em->orig_block_len = em->len;
Josef Bacik70c8a912012-10-11 16:54:30 -04007071 em->orig_start = em->start;
Yan689f9342007-10-29 11:41:07 -04007072 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Liu Boe49aabd2018-08-25 13:47:09 +08007073
7074 btrfs_set_path_blocking(path);
Edmund Nadolskibf46f522017-11-20 13:24:49 -07007075 if (!PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08007076 if (btrfs_file_extent_compression(leaf, item) !=
7077 BTRFS_COMPRESS_NONE) {
Byongho Leee40da0e2015-05-19 23:46:45 +09007078 ret = uncompress_inline(path, page, pg_offset,
Chris Masonc8b97812008-10-29 14:49:59 -04007079 extent_offset, item);
Zach Brown166ae5a2014-05-09 17:15:10 -04007080 if (ret) {
7081 err = ret;
7082 goto out;
7083 }
Chris Masonc8b97812008-10-29 14:49:59 -04007084 } else {
7085 map = kmap(page);
7086 read_extent_buffer(leaf, map + pg_offset, ptr,
7087 copy_size);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007088 if (pg_offset + copy_size < PAGE_SIZE) {
Chris Mason93c82d52009-09-11 12:36:29 -04007089 memset(map + pg_offset + copy_size, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007090 PAGE_SIZE - pg_offset -
Chris Mason93c82d52009-09-11 12:36:29 -04007091 copy_size);
7092 }
Chris Masonc8b97812008-10-29 14:49:59 -04007093 kunmap(page);
7094 }
Chris Mason179e29e2007-11-01 11:28:41 -04007095 flush_dcache_page(page);
Chris Masona52d9a82007-08-27 16:49:44 -04007096 }
Chris Masond1310b22008-01-24 16:13:08 -05007097 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00007098 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04007099 goto insert;
Chris Masona52d9a82007-08-27 16:49:44 -04007100 }
7101not_found:
7102 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04007103 em->orig_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05007104 em->len = len;
Chris Mason5f39d392007-10-15 16:14:19 -04007105 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04007106insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02007107 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05007108 if (em->start > start || extent_map_end(em) <= start) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007109 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04007110 "bad extent! em: [%llu %llu] passed [%llu %llu]",
7111 em->start, em->len, start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04007112 err = -EIO;
7113 goto out;
7114 }
Chris Masond1310b22008-01-24 16:13:08 -05007115
7116 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04007117 write_lock(&em_tree->lock);
David Sterbaf46b24c2018-04-03 21:45:57 +02007118 err = btrfs_add_extent_mapping(fs_info, em_tree, &em, start, len);
Chris Mason890871b2009-09-02 16:24:52 -04007119 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04007120out:
Liu Boc6414282018-08-23 07:36:17 +08007121 btrfs_free_path(path);
liubo1abe9b82011-03-24 11:18:59 +00007122
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007123 trace_btrfs_get_extent(root, inode, em);
liubo1abe9b82011-03-24 11:18:59 +00007124
Chris Masona52d9a82007-08-27 16:49:44 -04007125 if (err) {
7126 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04007127 return ERR_PTR(err);
7128 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007129 BUG_ON(!em); /* Error is always set */
Chris Masona52d9a82007-08-27 16:49:44 -04007130 return em;
7131}
7132
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007133struct extent_map *btrfs_get_extent_fiemap(struct btrfs_inode *inode,
Nikolay Borisov4ab47a82018-12-12 09:42:32 +02007134 u64 start, u64 len)
Chris Masonec29ed52011-02-23 16:23:20 -05007135{
7136 struct extent_map *em;
7137 struct extent_map *hole_em = NULL;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007138 u64 delalloc_start = start;
Chris Masonec29ed52011-02-23 16:23:20 -05007139 u64 end;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007140 u64 delalloc_len;
7141 u64 delalloc_end;
Chris Masonec29ed52011-02-23 16:23:20 -05007142 int err = 0;
7143
Nikolay Borisov4ab47a82018-12-12 09:42:32 +02007144 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
Chris Masonec29ed52011-02-23 16:23:20 -05007145 if (IS_ERR(em))
7146 return em;
Dan Carpenter99862772017-04-11 11:57:15 +03007147 /*
7148 * If our em maps to:
7149 * - a hole or
7150 * - a pre-alloc extent,
7151 * there might actually be delalloc bytes behind it.
7152 */
7153 if (em->block_start != EXTENT_MAP_HOLE &&
7154 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7155 return em;
7156 else
7157 hole_em = em;
Chris Masonec29ed52011-02-23 16:23:20 -05007158
7159 /* check to see if we've wrapped (len == -1 or similar) */
7160 end = start + len;
7161 if (end < start)
7162 end = (u64)-1;
7163 else
7164 end -= 1;
7165
7166 em = NULL;
7167
7168 /* ok, we didn't find anything, lets look for delalloc */
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007169 delalloc_len = count_range_bits(&inode->io_tree, &delalloc_start,
Chris Masonec29ed52011-02-23 16:23:20 -05007170 end, len, EXTENT_DELALLOC, 1);
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007171 delalloc_end = delalloc_start + delalloc_len;
7172 if (delalloc_end < delalloc_start)
7173 delalloc_end = (u64)-1;
Chris Masonec29ed52011-02-23 16:23:20 -05007174
7175 /*
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007176 * We didn't find anything useful, return the original results from
7177 * get_extent()
Chris Masonec29ed52011-02-23 16:23:20 -05007178 */
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007179 if (delalloc_start > end || delalloc_end <= start) {
Chris Masonec29ed52011-02-23 16:23:20 -05007180 em = hole_em;
7181 hole_em = NULL;
7182 goto out;
7183 }
7184
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007185 /*
7186 * Adjust the delalloc_start to make sure it doesn't go backwards from
7187 * the start they passed in
Chris Masonec29ed52011-02-23 16:23:20 -05007188 */
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007189 delalloc_start = max(start, delalloc_start);
7190 delalloc_len = delalloc_end - delalloc_start;
Chris Masonec29ed52011-02-23 16:23:20 -05007191
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007192 if (delalloc_len > 0) {
7193 u64 hole_start;
Nikolay Borisov02950af2018-12-12 09:42:34 +02007194 u64 hole_len;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007195 const u64 hole_end = extent_map_end(hole_em);
Chris Masonec29ed52011-02-23 16:23:20 -05007196
David Sterba172ddd62011-04-21 00:48:27 +02007197 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05007198 if (!em) {
7199 err = -ENOMEM;
7200 goto out;
7201 }
Chris Masonec29ed52011-02-23 16:23:20 -05007202 em->bdev = NULL;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007203
7204 ASSERT(hole_em);
7205 /*
7206 * When btrfs_get_extent can't find anything it returns one
7207 * huge hole
7208 *
7209 * Make sure what it found really fits our range, and adjust to
7210 * make sure it is based on the start from the caller
7211 */
7212 if (hole_end <= start || hole_em->start > end) {
7213 free_extent_map(hole_em);
7214 hole_em = NULL;
7215 } else {
7216 hole_start = max(hole_em->start, start);
7217 hole_len = hole_end - hole_start;
7218 }
7219
7220 if (hole_em && delalloc_start > hole_start) {
7221 /*
7222 * Our hole starts before our delalloc, so we have to
7223 * return just the parts of the hole that go until the
7224 * delalloc starts
Chris Masonec29ed52011-02-23 16:23:20 -05007225 */
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007226 em->len = min(hole_len, delalloc_start - hole_start);
Chris Masonec29ed52011-02-23 16:23:20 -05007227 em->start = hole_start;
7228 em->orig_start = hole_start;
7229 /*
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007230 * Don't adjust block start at all, it is fixed at
7231 * EXTENT_MAP_HOLE
Chris Masonec29ed52011-02-23 16:23:20 -05007232 */
7233 em->block_start = hole_em->block_start;
7234 em->block_len = hole_len;
Liu Bof9e4fb52013-01-07 10:10:12 +00007235 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
7236 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonec29ed52011-02-23 16:23:20 -05007237 } else {
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007238 /*
7239 * Hole is out of passed range or it starts after
7240 * delalloc range
7241 */
7242 em->start = delalloc_start;
7243 em->len = delalloc_len;
7244 em->orig_start = delalloc_start;
Chris Masonec29ed52011-02-23 16:23:20 -05007245 em->block_start = EXTENT_MAP_DELALLOC;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007246 em->block_len = delalloc_len;
Chris Masonec29ed52011-02-23 16:23:20 -05007247 }
Nikolay Borisovbf8d32b2017-12-01 11:19:43 +02007248 } else {
Chris Masonec29ed52011-02-23 16:23:20 -05007249 return hole_em;
7250 }
7251out:
7252
7253 free_extent_map(hole_em);
7254 if (err) {
7255 free_extent_map(em);
7256 return ERR_PTR(err);
7257 }
7258 return em;
7259}
7260
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007261static struct extent_map *btrfs_create_dio_extent(struct inode *inode,
7262 const u64 start,
7263 const u64 len,
7264 const u64 orig_start,
7265 const u64 block_start,
7266 const u64 block_len,
7267 const u64 orig_block_len,
7268 const u64 ram_bytes,
7269 const int type)
7270{
7271 struct extent_map *em = NULL;
7272 int ret;
7273
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007274 if (type != BTRFS_ORDERED_NOCOW) {
Liu Bo6f9994d2017-01-31 07:50:22 -08007275 em = create_io_em(inode, start, len, orig_start,
7276 block_start, block_len, orig_block_len,
7277 ram_bytes,
7278 BTRFS_COMPRESS_NONE, /* compress_type */
7279 type);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007280 if (IS_ERR(em))
7281 goto out;
7282 }
7283 ret = btrfs_add_ordered_extent_dio(inode, start, block_start,
7284 len, block_len, type);
7285 if (ret) {
7286 if (em) {
7287 free_extent_map(em);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02007288 btrfs_drop_extent_cache(BTRFS_I(inode), start,
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007289 start + len - 1, 0);
7290 }
7291 em = ERR_PTR(ret);
7292 }
7293 out:
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007294
7295 return em;
7296}
7297
Josef Bacik4b46fce2010-05-23 11:00:55 -04007298static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
7299 u64 start, u64 len)
7300{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007301 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007302 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik70c8a912012-10-11 16:54:30 -04007303 struct extent_map *em;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007304 struct btrfs_key ins;
7305 u64 alloc_hint;
7306 int ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007307
Josef Bacik4b46fce2010-05-23 11:00:55 -04007308 alloc_hint = get_extent_allocation_hint(inode, start, len);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007309 ret = btrfs_reserve_extent(root, len, len, fs_info->sectorsize,
Jeff Mahoneyda170662016-06-15 09:22:56 -04007310 0, alloc_hint, &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007311 if (ret)
7312 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007313
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007314 em = btrfs_create_dio_extent(inode, start, ins.offset, start,
7315 ins.objectid, ins.offset, ins.offset,
Liu Bo6288d6e2017-02-21 12:12:58 -08007316 ins.offset, BTRFS_ORDERED_REGULAR);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007317 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007318 if (IS_ERR(em))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007319 btrfs_free_reserved_extent(fs_info, ins.objectid,
7320 ins.offset, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007321
Josef Bacik4b46fce2010-05-23 11:00:55 -04007322 return em;
7323}
7324
Chris Mason46bfbb52010-05-26 11:04:10 -04007325/*
7326 * returns 1 when the nocow is safe, < 1 on error, 0 if the
7327 * block must be cow'd
7328 */
Josef Bacik00361582013-08-14 14:02:47 -04007329noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
Josef Bacik7ee9e442013-06-21 16:37:03 -04007330 u64 *orig_start, u64 *orig_block_len,
7331 u64 *ram_bytes)
Chris Mason46bfbb52010-05-26 11:04:10 -04007332{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007333 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason46bfbb52010-05-26 11:04:10 -04007334 struct btrfs_path *path;
7335 int ret;
7336 struct extent_buffer *leaf;
7337 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xie7b2b7082014-02-27 13:58:05 +08007338 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason46bfbb52010-05-26 11:04:10 -04007339 struct btrfs_file_extent_item *fi;
7340 struct btrfs_key key;
7341 u64 disk_bytenr;
7342 u64 backref_offset;
7343 u64 extent_end;
7344 u64 num_bytes;
7345 int slot;
7346 int found_type;
Josef Bacik7ee9e442013-06-21 16:37:03 -04007347 bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW);
Miao Xiee77751a2013-12-27 21:11:50 +08007348
Chris Mason46bfbb52010-05-26 11:04:10 -04007349 path = btrfs_alloc_path();
7350 if (!path)
7351 return -ENOMEM;
7352
David Sterbaf85b7372017-01-20 14:54:07 +01007353 ret = btrfs_lookup_file_extent(NULL, root, path,
7354 btrfs_ino(BTRFS_I(inode)), offset, 0);
Chris Mason46bfbb52010-05-26 11:04:10 -04007355 if (ret < 0)
7356 goto out;
7357
7358 slot = path->slots[0];
7359 if (ret == 1) {
7360 if (slot == 0) {
7361 /* can't find the item, must cow */
7362 ret = 0;
7363 goto out;
7364 }
7365 slot--;
7366 }
7367 ret = 0;
7368 leaf = path->nodes[0];
7369 btrfs_item_key_to_cpu(leaf, &key, slot);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02007370 if (key.objectid != btrfs_ino(BTRFS_I(inode)) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04007371 key.type != BTRFS_EXTENT_DATA_KEY) {
7372 /* not our file or wrong item type, must cow */
7373 goto out;
7374 }
7375
7376 if (key.offset > offset) {
7377 /* Wrong offset, must cow */
7378 goto out;
7379 }
7380
7381 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
7382 found_type = btrfs_file_extent_type(leaf, fi);
7383 if (found_type != BTRFS_FILE_EXTENT_REG &&
7384 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
7385 /* not a regular extent, must cow */
7386 goto out;
7387 }
Josef Bacik7ee9e442013-06-21 16:37:03 -04007388
7389 if (!nocow && found_type == BTRFS_FILE_EXTENT_REG)
7390 goto out;
7391
Miao Xiee77751a2013-12-27 21:11:50 +08007392 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
7393 if (extent_end <= offset)
7394 goto out;
7395
Chris Mason46bfbb52010-05-26 11:04:10 -04007396 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Josef Bacik7ee9e442013-06-21 16:37:03 -04007397 if (disk_bytenr == 0)
7398 goto out;
7399
7400 if (btrfs_file_extent_compression(leaf, fi) ||
7401 btrfs_file_extent_encryption(leaf, fi) ||
7402 btrfs_file_extent_other_encoding(leaf, fi))
7403 goto out;
7404
Ethan Lien78d42952018-05-17 14:58:29 +08007405 /*
7406 * Do the same check as in btrfs_cross_ref_exist but without the
7407 * unnecessary search.
7408 */
7409 if (btrfs_file_extent_generation(leaf, fi) <=
7410 btrfs_root_last_snapshot(&root->root_item))
7411 goto out;
7412
Chris Mason46bfbb52010-05-26 11:04:10 -04007413 backref_offset = btrfs_file_extent_offset(leaf, fi);
7414
Josef Bacik7ee9e442013-06-21 16:37:03 -04007415 if (orig_start) {
7416 *orig_start = key.offset - backref_offset;
7417 *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
7418 *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
7419 }
Josef Bacikeb384b52013-04-24 16:32:55 -04007420
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007421 if (btrfs_extent_readonly(fs_info, disk_bytenr))
Chris Mason46bfbb52010-05-26 11:04:10 -04007422 goto out;
Miao Xie7b2b7082014-02-27 13:58:05 +08007423
7424 num_bytes = min(offset + *len, extent_end) - offset;
7425 if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) {
7426 u64 range_end;
7427
Jeff Mahoneyda170662016-06-15 09:22:56 -04007428 range_end = round_up(offset + num_bytes,
7429 root->fs_info->sectorsize) - 1;
Miao Xie7b2b7082014-02-27 13:58:05 +08007430 ret = test_range_bit(io_tree, offset, range_end,
7431 EXTENT_DELALLOC, 0, NULL);
7432 if (ret) {
7433 ret = -EAGAIN;
7434 goto out;
7435 }
7436 }
7437
Josef Bacik1bda19e2013-10-18 12:10:36 -04007438 btrfs_release_path(path);
Chris Mason46bfbb52010-05-26 11:04:10 -04007439
7440 /*
7441 * look for other files referencing this extent, if we
7442 * find any we must cow
7443 */
Josef Bacik00361582013-08-14 14:02:47 -04007444
Liu Boe4c3b2d2017-01-30 12:25:28 -08007445 ret = btrfs_cross_ref_exist(root, btrfs_ino(BTRFS_I(inode)),
Josef Bacik00361582013-08-14 14:02:47 -04007446 key.offset - backref_offset, disk_bytenr);
Josef Bacik00361582013-08-14 14:02:47 -04007447 if (ret) {
7448 ret = 0;
7449 goto out;
7450 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007451
7452 /*
7453 * adjust disk_bytenr and num_bytes to cover just the bytes
7454 * in this extent we are about to write. If there
7455 * are any csums in that range we have to cow in order
7456 * to keep the csums correct
7457 */
7458 disk_bytenr += backref_offset;
7459 disk_bytenr += offset - key.offset;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007460 if (csum_exist_in_range(fs_info, disk_bytenr, num_bytes))
7461 goto out;
Chris Mason46bfbb52010-05-26 11:04:10 -04007462 /*
7463 * all of the above have passed, it is safe to overwrite this extent
7464 * without cow
7465 */
Josef Bacikeb384b52013-04-24 16:32:55 -04007466 *len = num_bytes;
Chris Mason46bfbb52010-05-26 11:04:10 -04007467 ret = 1;
7468out:
7469 btrfs_free_path(path);
7470 return ret;
7471}
7472
Josef Bacikeb838e72012-07-31 16:28:48 -04007473static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
7474 struct extent_state **cached_state, int writing)
7475{
7476 struct btrfs_ordered_extent *ordered;
7477 int ret = 0;
7478
7479 while (1) {
7480 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbaff13db42015-12-03 14:30:40 +01007481 cached_state);
Josef Bacikeb838e72012-07-31 16:28:48 -04007482 /*
7483 * We're concerned with the entire range that we're going to be
Nicholas D Steeves01327612016-05-19 21:18:45 -04007484 * doing DIO to, so we need to make sure there's no ordered
Josef Bacikeb838e72012-07-31 16:28:48 -04007485 * extents in this range.
7486 */
Nikolay Borisova776c6f2017-02-20 13:50:49 +02007487 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), lockstart,
Josef Bacikeb838e72012-07-31 16:28:48 -04007488 lockend - lockstart + 1);
7489
7490 /*
7491 * We need to make sure there are no buffered pages in this
7492 * range either, we could have raced between the invalidate in
7493 * generic_file_direct_write and locking the extent. The
7494 * invalidate needs to happen so that reads after a write do not
7495 * get stale data.
7496 */
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007497 if (!ordered &&
David Sterba051c98e2018-03-07 15:33:22 +01007498 (!writing || !filemap_range_has_page(inode->i_mapping,
7499 lockstart, lockend)))
Josef Bacikeb838e72012-07-31 16:28:48 -04007500 break;
7501
7502 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbae43bbe52017-12-12 21:43:52 +01007503 cached_state);
Josef Bacikeb838e72012-07-31 16:28:48 -04007504
7505 if (ordered) {
Filipe Mananaade77022016-02-18 14:28:55 +00007506 /*
7507 * If we are doing a DIO read and the ordered extent we
7508 * found is for a buffered write, we can not wait for it
7509 * to complete and retry, because if we do so we can
7510 * deadlock with concurrent buffered writes on page
7511 * locks. This happens only if our DIO read covers more
7512 * than one extent map, if at this point has already
7513 * created an ordered extent for a previous extent map
7514 * and locked its range in the inode's io tree, and a
7515 * concurrent write against that previous extent map's
7516 * range and this range started (we unlock the ranges
7517 * in the io tree only when the bios complete and
7518 * buffered writes always lock pages before attempting
7519 * to lock range in the io tree).
7520 */
7521 if (writing ||
7522 test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags))
7523 btrfs_start_ordered_extent(inode, ordered, 1);
7524 else
7525 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007526 btrfs_put_ordered_extent(ordered);
7527 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007528 /*
Filipe Mananab850ae12015-12-08 16:23:16 +00007529 * We could trigger writeback for this range (and wait
7530 * for it to complete) and then invalidate the pages for
7531 * this range (through invalidate_inode_pages2_range()),
7532 * but that can lead us to a deadlock with a concurrent
7533 * call to readpages() (a buffered read or a defrag call
7534 * triggered a readahead) on a page lock due to an
7535 * ordered dio extent we created before but did not have
7536 * yet a corresponding bio submitted (whence it can not
7537 * complete), which makes readpages() wait for that
7538 * ordered extent to complete while holding a lock on
7539 * that page.
Josef Bacikeb838e72012-07-31 16:28:48 -04007540 */
Filipe Mananab850ae12015-12-08 16:23:16 +00007541 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007542 }
7543
Filipe Mananaade77022016-02-18 14:28:55 +00007544 if (ret)
7545 break;
7546
Josef Bacikeb838e72012-07-31 16:28:48 -04007547 cond_resched();
7548 }
7549
7550 return ret;
7551}
7552
Liu Bo6f9994d2017-01-31 07:50:22 -08007553/* The callers of this must take lock_extent() */
7554static struct extent_map *create_io_em(struct inode *inode, u64 start, u64 len,
7555 u64 orig_start, u64 block_start,
7556 u64 block_len, u64 orig_block_len,
7557 u64 ram_bytes, int compress_type,
7558 int type)
Josef Bacik69ffb542012-09-11 15:40:07 -04007559{
7560 struct extent_map_tree *em_tree;
7561 struct extent_map *em;
7562 struct btrfs_root *root = BTRFS_I(inode)->root;
7563 int ret;
7564
Liu Bo6f9994d2017-01-31 07:50:22 -08007565 ASSERT(type == BTRFS_ORDERED_PREALLOC ||
7566 type == BTRFS_ORDERED_COMPRESSED ||
7567 type == BTRFS_ORDERED_NOCOW ||
Liu Bo1af4a0a2017-02-13 15:35:09 -08007568 type == BTRFS_ORDERED_REGULAR);
Liu Bo6f9994d2017-01-31 07:50:22 -08007569
Josef Bacik69ffb542012-09-11 15:40:07 -04007570 em_tree = &BTRFS_I(inode)->extent_tree;
7571 em = alloc_extent_map();
7572 if (!em)
7573 return ERR_PTR(-ENOMEM);
7574
7575 em->start = start;
7576 em->orig_start = orig_start;
7577 em->len = len;
7578 em->block_len = block_len;
7579 em->block_start = block_start;
7580 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacikb4939682012-12-03 10:31:19 -05007581 em->orig_block_len = orig_block_len;
Josef Bacikcc95bef2013-04-04 14:31:27 -04007582 em->ram_bytes = ram_bytes;
Josef Bacik70c8a912012-10-11 16:54:30 -04007583 em->generation = -1;
Josef Bacik69ffb542012-09-11 15:40:07 -04007584 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Liu Bo1af4a0a2017-02-13 15:35:09 -08007585 if (type == BTRFS_ORDERED_PREALLOC) {
Josef Bacikb11e2342012-12-03 10:58:15 -05007586 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Liu Bo1af4a0a2017-02-13 15:35:09 -08007587 } else if (type == BTRFS_ORDERED_COMPRESSED) {
Liu Bo6f9994d2017-01-31 07:50:22 -08007588 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
7589 em->compress_type = compress_type;
7590 }
Josef Bacik69ffb542012-09-11 15:40:07 -04007591
7592 do {
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02007593 btrfs_drop_extent_cache(BTRFS_I(inode), em->start,
Josef Bacik69ffb542012-09-11 15:40:07 -04007594 em->start + em->len - 1, 0);
7595 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04007596 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik69ffb542012-09-11 15:40:07 -04007597 write_unlock(&em_tree->lock);
Liu Bo6f9994d2017-01-31 07:50:22 -08007598 /*
7599 * The caller has taken lock_extent(), who could race with us
7600 * to add em?
7601 */
Josef Bacik69ffb542012-09-11 15:40:07 -04007602 } while (ret == -EEXIST);
7603
7604 if (ret) {
7605 free_extent_map(em);
7606 return ERR_PTR(ret);
7607 }
7608
Liu Bo6f9994d2017-01-31 07:50:22 -08007609 /* em got 2 refs now, callers needs to do free_extent_map once. */
Josef Bacik69ffb542012-09-11 15:40:07 -04007610 return em;
7611}
7612
Nikolay Borisov1c8d0172018-05-02 15:19:32 +03007613
7614static int btrfs_get_blocks_direct_read(struct extent_map *em,
7615 struct buffer_head *bh_result,
7616 struct inode *inode,
7617 u64 start, u64 len)
7618{
7619 if (em->block_start == EXTENT_MAP_HOLE ||
7620 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7621 return -ENOENT;
7622
7623 len = min(len, em->len - (start - em->start));
7624
7625 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
7626 inode->i_blkbits;
7627 bh_result->b_size = len;
7628 bh_result->b_bdev = em->bdev;
7629 set_buffer_mapped(bh_result);
7630
7631 return 0;
7632}
7633
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007634static int btrfs_get_blocks_direct_write(struct extent_map **map,
7635 struct buffer_head *bh_result,
7636 struct inode *inode,
7637 struct btrfs_dio_data *dio_data,
7638 u64 start, u64 len)
7639{
7640 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
7641 struct extent_map *em = *map;
7642 int ret = 0;
7643
7644 /*
7645 * We don't allocate a new extent in the following cases
7646 *
7647 * 1) The inode is marked as NODATACOW. In this case we'll just use the
7648 * existing extent.
7649 * 2) The extent is marked as PREALLOC. We're good to go here and can
7650 * just use the extent.
7651 *
7652 */
7653 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
7654 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
7655 em->block_start != EXTENT_MAP_HOLE)) {
7656 int type;
7657 u64 block_start, orig_start, orig_block_len, ram_bytes;
7658
7659 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7660 type = BTRFS_ORDERED_PREALLOC;
7661 else
7662 type = BTRFS_ORDERED_NOCOW;
7663 len = min(len, em->len - (start - em->start));
7664 block_start = em->block_start + (start - em->start);
7665
7666 if (can_nocow_extent(inode, start, &len, &orig_start,
7667 &orig_block_len, &ram_bytes) == 1 &&
7668 btrfs_inc_nocow_writers(fs_info, block_start)) {
7669 struct extent_map *em2;
7670
7671 em2 = btrfs_create_dio_extent(inode, start, len,
7672 orig_start, block_start,
7673 len, orig_block_len,
7674 ram_bytes, type);
7675 btrfs_dec_nocow_writers(fs_info, block_start);
7676 if (type == BTRFS_ORDERED_PREALLOC) {
7677 free_extent_map(em);
7678 *map = em = em2;
7679 }
7680
7681 if (em2 && IS_ERR(em2)) {
7682 ret = PTR_ERR(em2);
7683 goto out;
7684 }
7685 /*
7686 * For inode marked NODATACOW or extent marked PREALLOC,
7687 * use the existing or preallocated extent, so does not
7688 * need to adjust btrfs_space_info's bytes_may_use.
7689 */
7690 btrfs_free_reserved_data_space_noquota(inode, start,
7691 len);
7692 goto skip_cow;
7693 }
7694 }
7695
7696 /* this will cow the extent */
7697 len = bh_result->b_size;
7698 free_extent_map(em);
7699 *map = em = btrfs_new_extent_direct(inode, start, len);
7700 if (IS_ERR(em)) {
7701 ret = PTR_ERR(em);
7702 goto out;
7703 }
7704
7705 len = min(len, em->len - (start - em->start));
7706
7707skip_cow:
7708 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
7709 inode->i_blkbits;
7710 bh_result->b_size = len;
7711 bh_result->b_bdev = em->bdev;
7712 set_buffer_mapped(bh_result);
7713
7714 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7715 set_buffer_new(bh_result);
7716
7717 /*
7718 * Need to update the i_size under the extent lock so buffered
7719 * readers will get the updated i_size when we unlock.
7720 */
7721 if (!dio_data->overwrite && start + len > i_size_read(inode))
7722 i_size_write(inode, start + len);
7723
7724 WARN_ON(dio_data->reserve < len);
7725 dio_data->reserve -= len;
7726 dio_data->unsubmitted_oe_range_end = start + len;
7727 current->journal_info = dio_data;
7728out:
7729 return ret;
7730}
7731
Josef Bacik4b46fce2010-05-23 11:00:55 -04007732static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
7733 struct buffer_head *bh_result, int create)
7734{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007735 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007736 struct extent_map *em;
Josef Bacikeb838e72012-07-31 16:28:48 -04007737 struct extent_state *cached_state = NULL;
chandan50745b02015-08-28 21:10:13 +05307738 struct btrfs_dio_data *dio_data = NULL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007739 u64 start = iblock << inode->i_blkbits;
Josef Bacikeb838e72012-07-31 16:28:48 -04007740 u64 lockstart, lockend;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007741 u64 len = bh_result->b_size;
Miao Xie09348562013-02-07 10:12:07 +00007742 int ret = 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007743
Omar Sandovale1821632019-08-15 14:04:04 -07007744 if (!create)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007745 len = min_t(u64, len, fs_info->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04007746
Josef Bacikc3298612012-08-03 16:49:19 -04007747 lockstart = start;
7748 lockend = start + len - 1;
7749
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007750 if (current->journal_info) {
7751 /*
7752 * Need to pull our outstanding extents and set journal_info to NULL so
Nicholas D Steeves01327612016-05-19 21:18:45 -04007753 * that anything that needs to check if there's a transaction doesn't get
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007754 * confused.
7755 */
chandan50745b02015-08-28 21:10:13 +05307756 dio_data = current->journal_info;
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007757 current->journal_info = NULL;
7758 }
7759
Josef Bacikeb838e72012-07-31 16:28:48 -04007760 /*
7761 * If this errors out it's because we couldn't invalidate pagecache for
7762 * this range and we need to fallback to buffered.
7763 */
Filipe Manana9c9464c2015-11-04 09:52:04 +00007764 if (lock_extent_direct(inode, lockstart, lockend, &cached_state,
7765 create)) {
7766 ret = -ENOTBLK;
7767 goto err;
7768 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007769
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007770 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len, 0);
Josef Bacikeb838e72012-07-31 16:28:48 -04007771 if (IS_ERR(em)) {
7772 ret = PTR_ERR(em);
7773 goto unlock_err;
7774 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007775
7776 /*
7777 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
7778 * io. INLINE is special, and we could probably kludge it in here, but
7779 * it's still buffered so for safety lets just fall back to the generic
7780 * buffered path.
7781 *
7782 * For COMPRESSED we _have_ to read the entire extent in so we can
7783 * decompress it, so there will be buffering required no matter what we
7784 * do, so go ahead and fallback to buffered.
7785 *
Nicholas D Steeves01327612016-05-19 21:18:45 -04007786 * We return -ENOTBLK because that's what makes DIO go ahead and go back
Josef Bacik4b46fce2010-05-23 11:00:55 -04007787 * to buffered IO. Don't blame me, this is the price we pay for using
7788 * the generic code.
7789 */
7790 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
7791 em->block_start == EXTENT_MAP_INLINE) {
7792 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007793 ret = -ENOTBLK;
7794 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007795 }
7796
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007797 if (create) {
7798 ret = btrfs_get_blocks_direct_write(&em, bh_result, inode,
7799 dio_data, start, len);
7800 if (ret < 0)
7801 goto unlock_err;
7802
Omar Sandovale1821632019-08-15 14:04:04 -07007803 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart,
7804 lockend, &cached_state);
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007805 } else {
Nikolay Borisov1c8d0172018-05-02 15:19:32 +03007806 ret = btrfs_get_blocks_direct_read(em, bh_result, inode,
7807 start, len);
7808 /* Can be negative only if we read from a hole */
7809 if (ret < 0) {
7810 ret = 0;
7811 free_extent_map(em);
7812 goto unlock_err;
7813 }
7814 /*
7815 * We need to unlock only the end area that we aren't using.
7816 * The rest is going to be unlocked by the endio routine.
7817 */
7818 lockstart = start + bh_result->b_size;
7819 if (lockstart < lockend) {
Omar Sandovale1821632019-08-15 14:04:04 -07007820 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
7821 lockstart, lockend, &cached_state);
Nikolay Borisov1c8d0172018-05-02 15:19:32 +03007822 } else {
7823 free_extent_state(cached_state);
7824 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007825 }
7826
Josef Bacik4b46fce2010-05-23 11:00:55 -04007827 free_extent_map(em);
7828
7829 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007830
7831unlock_err:
Omar Sandovale1821632019-08-15 14:04:04 -07007832 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7833 &cached_state);
Filipe Manana9c9464c2015-11-04 09:52:04 +00007834err:
chandan50745b02015-08-28 21:10:13 +05307835 if (dio_data)
7836 current->journal_info = dio_data;
Josef Bacikeb838e72012-07-31 16:28:48 -04007837 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007838}
7839
Omar Sandoval58efbc92017-08-22 23:45:59 -07007840static inline blk_status_t submit_dio_repair_bio(struct inode *inode,
7841 struct bio *bio,
7842 int mirror_num)
Miao Xie8b110e32014-09-12 18:44:03 +08007843{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007844 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Omar Sandoval58efbc92017-08-22 23:45:59 -07007845 blk_status_t ret;
Miao Xie8b110e32014-09-12 18:44:03 +08007846
Mike Christie37226b22016-06-05 14:31:52 -05007847 BUG_ON(bio_op(bio) == REQ_OP_WRITE);
Miao Xie8b110e32014-09-12 18:44:03 +08007848
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007849 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DIO_REPAIR);
Miao Xie8b110e32014-09-12 18:44:03 +08007850 if (ret)
Nikolay Borisovea057f62017-12-13 10:25:38 +02007851 return ret;
Miao Xie8b110e32014-09-12 18:44:03 +08007852
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007853 ret = btrfs_map_bio(fs_info, bio, mirror_num, 0);
Nikolay Borisovea057f62017-12-13 10:25:38 +02007854
Miao Xie8b110e32014-09-12 18:44:03 +08007855 return ret;
7856}
7857
7858static int btrfs_check_dio_repairable(struct inode *inode,
7859 struct bio *failed_bio,
7860 struct io_failure_record *failrec,
7861 int failed_mirror)
7862{
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007863 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xie8b110e32014-09-12 18:44:03 +08007864 int num_copies;
7865
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007866 num_copies = btrfs_num_copies(fs_info, failrec->logical, failrec->len);
Miao Xie8b110e32014-09-12 18:44:03 +08007867 if (num_copies == 1) {
7868 /*
7869 * we only have a single copy of the data, so don't bother with
7870 * all the retry and error correction code that follows. no
7871 * matter what the error is, it is very likely to persist.
7872 */
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007873 btrfs_debug(fs_info,
7874 "Check DIO Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d",
7875 num_copies, failrec->this_mirror, failed_mirror);
Miao Xie8b110e32014-09-12 18:44:03 +08007876 return 0;
7877 }
7878
7879 failrec->failed_mirror = failed_mirror;
7880 failrec->this_mirror++;
7881 if (failrec->this_mirror == failed_mirror)
7882 failrec->this_mirror++;
7883
7884 if (failrec->this_mirror > num_copies) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007885 btrfs_debug(fs_info,
7886 "Check DIO Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d",
7887 num_copies, failrec->this_mirror, failed_mirror);
Miao Xie8b110e32014-09-12 18:44:03 +08007888 return 0;
7889 }
7890
7891 return 1;
7892}
7893
Omar Sandoval58efbc92017-08-22 23:45:59 -07007894static blk_status_t dio_read_error(struct inode *inode, struct bio *failed_bio,
7895 struct page *page, unsigned int pgoff,
7896 u64 start, u64 end, int failed_mirror,
7897 bio_end_io_t *repair_endio, void *repair_arg)
Miao Xie8b110e32014-09-12 18:44:03 +08007898{
7899 struct io_failure_record *failrec;
Josef Bacik7870d082017-05-05 11:57:15 -04007900 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
7901 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
Miao Xie8b110e32014-09-12 18:44:03 +08007902 struct bio *bio;
7903 int isector;
David Sterbaf1c77c52017-06-06 19:03:49 +02007904 unsigned int read_mode = 0;
Liu Bo17347ce2017-05-15 15:33:27 -07007905 int segs;
Miao Xie8b110e32014-09-12 18:44:03 +08007906 int ret;
Omar Sandoval58efbc92017-08-22 23:45:59 -07007907 blk_status_t status;
Ming Leic16a8ac2017-12-18 20:22:12 +08007908 struct bio_vec bvec;
Miao Xie8b110e32014-09-12 18:44:03 +08007909
Mike Christie37226b22016-06-05 14:31:52 -05007910 BUG_ON(bio_op(failed_bio) == REQ_OP_WRITE);
Miao Xie8b110e32014-09-12 18:44:03 +08007911
7912 ret = btrfs_get_io_failure_record(inode, start, end, &failrec);
7913 if (ret)
Omar Sandoval58efbc92017-08-22 23:45:59 -07007914 return errno_to_blk_status(ret);
Miao Xie8b110e32014-09-12 18:44:03 +08007915
7916 ret = btrfs_check_dio_repairable(inode, failed_bio, failrec,
7917 failed_mirror);
7918 if (!ret) {
Josef Bacik7870d082017-05-05 11:57:15 -04007919 free_io_failure(failure_tree, io_tree, failrec);
Omar Sandoval58efbc92017-08-22 23:45:59 -07007920 return BLK_STS_IOERR;
Miao Xie8b110e32014-09-12 18:44:03 +08007921 }
7922
Liu Bo17347ce2017-05-15 15:33:27 -07007923 segs = bio_segments(failed_bio);
Ming Leic16a8ac2017-12-18 20:22:12 +08007924 bio_get_first_bvec(failed_bio, &bvec);
Liu Bo17347ce2017-05-15 15:33:27 -07007925 if (segs > 1 ||
Ming Leic16a8ac2017-12-18 20:22:12 +08007926 (bvec.bv_len > btrfs_inode_sectorsize(inode)))
Christoph Hellwig70fd7612016-11-01 07:40:10 -06007927 read_mode |= REQ_FAILFAST_DEV;
Miao Xie8b110e32014-09-12 18:44:03 +08007928
7929 isector = start - btrfs_io_bio(failed_bio)->logical;
7930 isector >>= inode->i_sb->s_blocksize_bits;
7931 bio = btrfs_create_repair_bio(inode, failed_bio, failrec, page,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307932 pgoff, isector, repair_endio, repair_arg);
David Sterbaebcc3262018-06-29 10:56:53 +02007933 bio->bi_opf = REQ_OP_READ | read_mode;
Miao Xie8b110e32014-09-12 18:44:03 +08007934
7935 btrfs_debug(BTRFS_I(inode)->root->fs_info,
David Sterba913e1532017-07-13 15:32:18 +02007936 "repair DIO read error: submitting new dio read[%#x] to this_mirror=%d, in_validation=%d",
Miao Xie8b110e32014-09-12 18:44:03 +08007937 read_mode, failrec->this_mirror, failrec->in_validation);
7938
Omar Sandoval58efbc92017-08-22 23:45:59 -07007939 status = submit_dio_repair_bio(inode, bio, failrec->this_mirror);
7940 if (status) {
Josef Bacik7870d082017-05-05 11:57:15 -04007941 free_io_failure(failure_tree, io_tree, failrec);
Miao Xie8b110e32014-09-12 18:44:03 +08007942 bio_put(bio);
7943 }
7944
Omar Sandoval58efbc92017-08-22 23:45:59 -07007945 return status;
Miao Xie8b110e32014-09-12 18:44:03 +08007946}
7947
7948struct btrfs_retry_complete {
7949 struct completion done;
7950 struct inode *inode;
7951 u64 start;
7952 int uptodate;
7953};
7954
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007955static void btrfs_retry_endio_nocsum(struct bio *bio)
Miao Xie8b110e32014-09-12 18:44:03 +08007956{
7957 struct btrfs_retry_complete *done = bio->bi_private;
Josef Bacik7870d082017-05-05 11:57:15 -04007958 struct inode *inode = done->inode;
Miao Xie8b110e32014-09-12 18:44:03 +08007959 struct bio_vec *bvec;
Josef Bacik7870d082017-05-05 11:57:15 -04007960 struct extent_io_tree *io_tree, *failure_tree;
Ming Lei6dc4f102019-02-15 19:13:19 +08007961 struct bvec_iter_all iter_all;
Miao Xie8b110e32014-09-12 18:44:03 +08007962
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02007963 if (bio->bi_status)
Miao Xie8b110e32014-09-12 18:44:03 +08007964 goto end;
7965
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307966 ASSERT(bio->bi_vcnt == 1);
Josef Bacik7870d082017-05-05 11:57:15 -04007967 io_tree = &BTRFS_I(inode)->io_tree;
7968 failure_tree = &BTRFS_I(inode)->io_failure_tree;
Ming Lei263663c2017-12-18 20:22:04 +08007969 ASSERT(bio_first_bvec_all(bio)->bv_len == btrfs_inode_sectorsize(inode));
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307970
Miao Xie8b110e32014-09-12 18:44:03 +08007971 done->uptodate = 1;
David Sterbac09abff2017-07-13 18:10:07 +02007972 ASSERT(!bio_flagged(bio, BIO_CLONED));
Christoph Hellwig2b070cf2019-04-25 09:03:00 +02007973 bio_for_each_segment_all(bvec, bio, iter_all)
Josef Bacik7870d082017-05-05 11:57:15 -04007974 clean_io_failure(BTRFS_I(inode)->root->fs_info, failure_tree,
7975 io_tree, done->start, bvec->bv_page,
7976 btrfs_ino(BTRFS_I(inode)), 0);
Miao Xie8b110e32014-09-12 18:44:03 +08007977end:
7978 complete(&done->done);
7979 bio_put(bio);
7980}
7981
Omar Sandoval58efbc92017-08-22 23:45:59 -07007982static blk_status_t __btrfs_correct_data_nocsum(struct inode *inode,
7983 struct btrfs_io_bio *io_bio)
Josef Bacik4b46fce2010-05-23 11:00:55 -04007984{
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307985 struct btrfs_fs_info *fs_info;
Liu Bo17347ce2017-05-15 15:33:27 -07007986 struct bio_vec bvec;
7987 struct bvec_iter iter;
Miao Xie8b110e32014-09-12 18:44:03 +08007988 struct btrfs_retry_complete done;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007989 u64 start;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307990 unsigned int pgoff;
7991 u32 sectorsize;
7992 int nr_sectors;
Omar Sandoval58efbc92017-08-22 23:45:59 -07007993 blk_status_t ret;
7994 blk_status_t err = BLK_STS_OK;
Miao Xiedc380ae2014-09-12 18:43:55 +08007995
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307996 fs_info = BTRFS_I(inode)->root->fs_info;
Jeff Mahoneyda170662016-06-15 09:22:56 -04007997 sectorsize = fs_info->sectorsize;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307998
Miao Xiec1dc0892014-09-12 18:43:56 +08007999 start = io_bio->logical;
Miao Xie8b110e32014-09-12 18:44:03 +08008000 done.inode = inode;
Liu Bo17347ce2017-05-15 15:33:27 -07008001 io_bio->bio.bi_iter = io_bio->iter;
Miao Xie8b110e32014-09-12 18:44:03 +08008002
Liu Bo17347ce2017-05-15 15:33:27 -07008003 bio_for_each_segment(bvec, &io_bio->bio, iter) {
8004 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec.bv_len);
8005 pgoff = bvec.bv_offset;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308006
8007next_block_or_try_again:
Miao Xie8b110e32014-09-12 18:44:03 +08008008 done.uptodate = 0;
8009 done.start = start;
8010 init_completion(&done.done);
8011
Liu Bo17347ce2017-05-15 15:33:27 -07008012 ret = dio_read_error(inode, &io_bio->bio, bvec.bv_page,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308013 pgoff, start, start + sectorsize - 1,
8014 io_bio->mirror_num,
8015 btrfs_retry_endio_nocsum, &done);
Liu Bo629ebf42017-05-15 17:20:07 -07008016 if (ret) {
8017 err = ret;
8018 goto next;
8019 }
Miao Xie8b110e32014-09-12 18:44:03 +08008020
David Sterba9c17f6c2017-07-19 19:26:45 +02008021 wait_for_completion_io(&done.done);
Miao Xie8b110e32014-09-12 18:44:03 +08008022
8023 if (!done.uptodate) {
8024 /* We might have another mirror, so try again */
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308025 goto next_block_or_try_again;
Miao Xie8b110e32014-09-12 18:44:03 +08008026 }
8027
Liu Bo629ebf42017-05-15 17:20:07 -07008028next:
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308029 start += sectorsize;
8030
Liu Bo97bf5a52017-04-07 13:11:10 -07008031 nr_sectors--;
8032 if (nr_sectors) {
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308033 pgoff += sectorsize;
Liu Bo97bf5a52017-04-07 13:11:10 -07008034 ASSERT(pgoff < PAGE_SIZE);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308035 goto next_block_or_try_again;
8036 }
Miao Xie8b110e32014-09-12 18:44:03 +08008037 }
8038
Liu Bo629ebf42017-05-15 17:20:07 -07008039 return err;
Miao Xie8b110e32014-09-12 18:44:03 +08008040}
8041
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008042static void btrfs_retry_endio(struct bio *bio)
Miao Xie8b110e32014-09-12 18:44:03 +08008043{
8044 struct btrfs_retry_complete *done = bio->bi_private;
8045 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
Josef Bacik7870d082017-05-05 11:57:15 -04008046 struct extent_io_tree *io_tree, *failure_tree;
8047 struct inode *inode = done->inode;
Miao Xie8b110e32014-09-12 18:44:03 +08008048 struct bio_vec *bvec;
8049 int uptodate;
8050 int ret;
Christoph Hellwig2b070cf2019-04-25 09:03:00 +02008051 int i = 0;
Ming Lei6dc4f102019-02-15 19:13:19 +08008052 struct bvec_iter_all iter_all;
Miao Xie8b110e32014-09-12 18:44:03 +08008053
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008054 if (bio->bi_status)
Miao Xie8b110e32014-09-12 18:44:03 +08008055 goto end;
8056
8057 uptodate = 1;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308058
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308059 ASSERT(bio->bi_vcnt == 1);
Ming Lei263663c2017-12-18 20:22:04 +08008060 ASSERT(bio_first_bvec_all(bio)->bv_len == btrfs_inode_sectorsize(done->inode));
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308061
Josef Bacik7870d082017-05-05 11:57:15 -04008062 io_tree = &BTRFS_I(inode)->io_tree;
8063 failure_tree = &BTRFS_I(inode)->io_failure_tree;
8064
David Sterbac09abff2017-07-13 18:10:07 +02008065 ASSERT(!bio_flagged(bio, BIO_CLONED));
Christoph Hellwig2b070cf2019-04-25 09:03:00 +02008066 bio_for_each_segment_all(bvec, bio, iter_all) {
Josef Bacik7870d082017-05-05 11:57:15 -04008067 ret = __readpage_endio_check(inode, io_bio, i, bvec->bv_page,
8068 bvec->bv_offset, done->start,
8069 bvec->bv_len);
Miao Xie8b110e32014-09-12 18:44:03 +08008070 if (!ret)
Josef Bacik7870d082017-05-05 11:57:15 -04008071 clean_io_failure(BTRFS_I(inode)->root->fs_info,
8072 failure_tree, io_tree, done->start,
8073 bvec->bv_page,
8074 btrfs_ino(BTRFS_I(inode)),
8075 bvec->bv_offset);
Miao Xie8b110e32014-09-12 18:44:03 +08008076 else
8077 uptodate = 0;
Christoph Hellwig2b070cf2019-04-25 09:03:00 +02008078 i++;
Miao Xie8b110e32014-09-12 18:44:03 +08008079 }
8080
8081 done->uptodate = uptodate;
8082end:
8083 complete(&done->done);
8084 bio_put(bio);
8085}
8086
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008087static blk_status_t __btrfs_subio_endio_read(struct inode *inode,
8088 struct btrfs_io_bio *io_bio, blk_status_t err)
Miao Xie8b110e32014-09-12 18:44:03 +08008089{
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308090 struct btrfs_fs_info *fs_info;
Liu Bo17347ce2017-05-15 15:33:27 -07008091 struct bio_vec bvec;
8092 struct bvec_iter iter;
Miao Xie8b110e32014-09-12 18:44:03 +08008093 struct btrfs_retry_complete done;
8094 u64 start;
8095 u64 offset = 0;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308096 u32 sectorsize;
8097 int nr_sectors;
8098 unsigned int pgoff;
8099 int csum_pos;
Liu Boef7cdac12017-04-13 18:11:48 -07008100 bool uptodate = (err == 0);
Miao Xie8b110e32014-09-12 18:44:03 +08008101 int ret;
Omar Sandoval58efbc92017-08-22 23:45:59 -07008102 blk_status_t status;
Miao Xie8b110e32014-09-12 18:44:03 +08008103
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308104 fs_info = BTRFS_I(inode)->root->fs_info;
Jeff Mahoneyda170662016-06-15 09:22:56 -04008105 sectorsize = fs_info->sectorsize;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308106
Omar Sandoval58efbc92017-08-22 23:45:59 -07008107 err = BLK_STS_OK;
Miao Xie8b110e32014-09-12 18:44:03 +08008108 start = io_bio->logical;
8109 done.inode = inode;
Liu Bo17347ce2017-05-15 15:33:27 -07008110 io_bio->bio.bi_iter = io_bio->iter;
Miao Xie8b110e32014-09-12 18:44:03 +08008111
Liu Bo17347ce2017-05-15 15:33:27 -07008112 bio_for_each_segment(bvec, &io_bio->bio, iter) {
8113 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec.bv_len);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308114
Liu Bo17347ce2017-05-15 15:33:27 -07008115 pgoff = bvec.bv_offset;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308116next_block:
Liu Boef7cdac12017-04-13 18:11:48 -07008117 if (uptodate) {
8118 csum_pos = BTRFS_BYTES_TO_BLKS(fs_info, offset);
8119 ret = __readpage_endio_check(inode, io_bio, csum_pos,
8120 bvec.bv_page, pgoff, start, sectorsize);
8121 if (likely(!ret))
8122 goto next;
8123 }
Miao Xie8b110e32014-09-12 18:44:03 +08008124try_again:
8125 done.uptodate = 0;
8126 done.start = start;
8127 init_completion(&done.done);
8128
Omar Sandoval58efbc92017-08-22 23:45:59 -07008129 status = dio_read_error(inode, &io_bio->bio, bvec.bv_page,
8130 pgoff, start, start + sectorsize - 1,
8131 io_bio->mirror_num, btrfs_retry_endio,
8132 &done);
8133 if (status) {
8134 err = status;
Miao Xie8b110e32014-09-12 18:44:03 +08008135 goto next;
8136 }
8137
David Sterba9c17f6c2017-07-19 19:26:45 +02008138 wait_for_completion_io(&done.done);
Miao Xie8b110e32014-09-12 18:44:03 +08008139
8140 if (!done.uptodate) {
8141 /* We might have another mirror, so try again */
8142 goto try_again;
8143 }
8144next:
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308145 offset += sectorsize;
8146 start += sectorsize;
8147
8148 ASSERT(nr_sectors);
8149
Liu Bo97bf5a52017-04-07 13:11:10 -07008150 nr_sectors--;
8151 if (nr_sectors) {
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308152 pgoff += sectorsize;
Liu Bo97bf5a52017-04-07 13:11:10 -07008153 ASSERT(pgoff < PAGE_SIZE);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308154 goto next_block;
8155 }
Kent Overstreet2c30c712013-11-07 12:20:26 -08008156 }
Miao Xiec1dc0892014-09-12 18:43:56 +08008157
8158 return err;
8159}
8160
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008161static blk_status_t btrfs_subio_endio_read(struct inode *inode,
8162 struct btrfs_io_bio *io_bio, blk_status_t err)
Miao Xie8b110e32014-09-12 18:44:03 +08008163{
8164 bool skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
8165
8166 if (skip_csum) {
8167 if (unlikely(err))
8168 return __btrfs_correct_data_nocsum(inode, io_bio);
8169 else
Omar Sandoval58efbc92017-08-22 23:45:59 -07008170 return BLK_STS_OK;
Miao Xie8b110e32014-09-12 18:44:03 +08008171 } else {
8172 return __btrfs_subio_endio_read(inode, io_bio, err);
8173 }
8174}
8175
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008176static void btrfs_endio_direct_read(struct bio *bio)
Miao Xiec1dc0892014-09-12 18:43:56 +08008177{
8178 struct btrfs_dio_private *dip = bio->bi_private;
8179 struct inode *inode = dip->inode;
8180 struct bio *dio_bio;
8181 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008182 blk_status_t err = bio->bi_status;
Miao Xiec1dc0892014-09-12 18:43:56 +08008183
Liu Bo99c4e3b92017-09-15 15:06:51 -06008184 if (dip->flags & BTRFS_DIO_ORIG_BIO_SUBMITTED)
Miao Xie8b110e32014-09-12 18:44:03 +08008185 err = btrfs_subio_endio_read(inode, io_bio, err);
Miao Xiec1dc0892014-09-12 18:43:56 +08008186
Josef Bacik4b46fce2010-05-23 11:00:55 -04008187 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01008188 dip->logical_offset + dip->bytes - 1);
Chris Mason9be33952013-05-17 18:30:14 -04008189 dio_bio = dip->dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008190
Josef Bacik4b46fce2010-05-23 11:00:55 -04008191 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04008192
Liu Bo99c4e3b92017-09-15 15:06:51 -06008193 dio_bio->bi_status = err;
Christoph Hellwig40553512017-06-03 09:37:58 +02008194 dio_end_io(dio_bio);
David Sterbab3a0dd52018-11-22 17:16:49 +01008195 btrfs_io_bio_free_csum(io_bio);
Chris Mason9be33952013-05-17 18:30:14 -04008196 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008197}
8198
Qu Wenruo524272602017-03-08 10:25:52 +08008199static void __endio_write_update_ordered(struct inode *inode,
8200 const u64 offset, const u64 bytes,
8201 const bool uptodate)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008202{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008203 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008204 struct btrfs_ordered_extent *ordered = NULL;
Qu Wenruo524272602017-03-08 10:25:52 +08008205 struct btrfs_workqueue *wq;
Filipe Manana14543772015-11-24 16:23:54 +00008206 u64 ordered_offset = offset;
8207 u64 ordered_bytes = bytes;
Naohiro Aota67c003f2017-09-01 17:59:07 +09008208 u64 last_offset;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008209
Omar Sandovala0cac0e2019-09-16 11:30:57 -07008210 if (btrfs_is_free_space_inode(BTRFS_I(inode)))
Qu Wenruo524272602017-03-08 10:25:52 +08008211 wq = fs_info->endio_freespace_worker;
Omar Sandovala0cac0e2019-09-16 11:30:57 -07008212 else
Qu Wenruo524272602017-03-08 10:25:52 +08008213 wq = fs_info->endio_write_workers;
Qu Wenruo524272602017-03-08 10:25:52 +08008214
Nikolay Borisovb25f0d02018-04-11 11:21:17 +03008215 while (ordered_offset < offset + bytes) {
8216 last_offset = ordered_offset;
8217 if (btrfs_dec_test_first_ordered_pending(inode, &ordered,
8218 &ordered_offset,
8219 ordered_bytes,
8220 uptodate)) {
Omar Sandovala0cac0e2019-09-16 11:30:57 -07008221 btrfs_init_work(&ordered->work, finish_ordered_fn, NULL,
8222 NULL);
Nikolay Borisovb25f0d02018-04-11 11:21:17 +03008223 btrfs_queue_work(wq, &ordered->work);
8224 }
8225 /*
8226 * If btrfs_dec_test_ordered_pending does not find any ordered
8227 * extent in the range, we can exit.
8228 */
8229 if (ordered_offset == last_offset)
8230 return;
8231 /*
8232 * Our bio might span multiple ordered extents. In this case
Andrea Gelmini52042d82018-11-28 12:05:13 +01008233 * we keep going until we have accounted the whole dio.
Nikolay Borisovb25f0d02018-04-11 11:21:17 +03008234 */
8235 if (ordered_offset < offset + bytes) {
8236 ordered_bytes = offset + bytes - ordered_offset;
8237 ordered = NULL;
8238 }
Chris Mason163cf092010-11-28 19:56:33 -05008239 }
Filipe Manana14543772015-11-24 16:23:54 +00008240}
8241
8242static void btrfs_endio_direct_write(struct bio *bio)
8243{
8244 struct btrfs_dio_private *dip = bio->bi_private;
8245 struct bio *dio_bio = dip->dio_bio;
8246
Qu Wenruo524272602017-03-08 10:25:52 +08008247 __endio_write_update_ordered(dip->inode, dip->logical_offset,
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008248 dip->bytes, !bio->bi_status);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008249
Josef Bacik4b46fce2010-05-23 11:00:55 -04008250 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04008251
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008252 dio_bio->bi_status = bio->bi_status;
Christoph Hellwig40553512017-06-03 09:37:58 +02008253 dio_end_io(dio_bio);
Chris Mason9be33952013-05-17 18:30:14 -04008254 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008255}
8256
David Sterbad0ee3932018-03-08 14:35:48 +01008257static blk_status_t btrfs_submit_bio_start_direct_io(void *private_data,
David Sterbad0779292018-03-08 13:47:33 +01008258 struct bio *bio, u64 offset)
Chris Masoneaf25d92010-05-25 09:48:28 -04008259{
Josef Bacikc6100a42017-05-05 11:57:13 -04008260 struct inode *inode = private_data;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008261 blk_status_t ret;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008262 ret = btrfs_csum_one_bio(inode, bio, offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008263 BUG_ON(ret); /* -ENOMEM */
Chris Masoneaf25d92010-05-25 09:48:28 -04008264 return 0;
8265}
8266
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008267static void btrfs_end_dio_bio(struct bio *bio)
Miao Xiee65e1532010-11-22 03:04:43 +00008268{
8269 struct btrfs_dio_private *dip = bio->bi_private;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008270 blk_status_t err = bio->bi_status;
Miao Xiee65e1532010-11-22 03:04:43 +00008271
Miao Xie8b110e32014-09-12 18:44:03 +08008272 if (err)
8273 btrfs_warn(BTRFS_I(dip->inode)->root->fs_info,
Mike Christie6296b962016-06-05 14:32:21 -05008274 "direct IO failed ino %llu rw %d,%u sector %#Lx len %u err no %d",
David Sterbaf85b7372017-01-20 14:54:07 +01008275 btrfs_ino(BTRFS_I(dip->inode)), bio_op(bio),
8276 bio->bi_opf,
Miao Xie8b110e32014-09-12 18:44:03 +08008277 (unsigned long long)bio->bi_iter.bi_sector,
8278 bio->bi_iter.bi_size, err);
8279
8280 if (dip->subio_endio)
8281 err = dip->subio_endio(dip->inode, btrfs_io_bio(bio), err);
Miao Xiec1dc0892014-09-12 18:43:56 +08008282
8283 if (err) {
Miao Xiee65e1532010-11-22 03:04:43 +00008284 /*
Nikolay Borisovde224b72018-02-14 10:53:36 +02008285 * We want to perceive the errors flag being set before
8286 * decrementing the reference count. We don't need a barrier
8287 * since atomic operations with a return value are fully
8288 * ordered as per atomic_t.txt
Miao Xiee65e1532010-11-22 03:04:43 +00008289 */
Nikolay Borisovde224b72018-02-14 10:53:36 +02008290 dip->errors = 1;
Miao Xiee65e1532010-11-22 03:04:43 +00008291 }
8292
8293 /* if there are more bios still pending for this dio, just exit */
8294 if (!atomic_dec_and_test(&dip->pending_bios))
8295 goto out;
8296
Chris Mason9be33952013-05-17 18:30:14 -04008297 if (dip->errors) {
Miao Xiee65e1532010-11-22 03:04:43 +00008298 bio_io_error(dip->orig_bio);
Chris Mason9be33952013-05-17 18:30:14 -04008299 } else {
Anand Jain2dbe0c72017-10-14 08:35:56 +08008300 dip->dio_bio->bi_status = BLK_STS_OK;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008301 bio_endio(dip->orig_bio);
Miao Xiee65e1532010-11-22 03:04:43 +00008302 }
8303out:
8304 bio_put(bio);
8305}
8306
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008307static inline blk_status_t btrfs_lookup_and_bind_dio_csum(struct inode *inode,
Miao Xiec1dc0892014-09-12 18:43:56 +08008308 struct btrfs_dio_private *dip,
8309 struct bio *bio,
8310 u64 file_offset)
8311{
8312 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
8313 struct btrfs_io_bio *orig_io_bio = btrfs_io_bio(dip->orig_bio);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008314 blk_status_t ret;
Miao Xiec1dc0892014-09-12 18:43:56 +08008315
8316 /*
8317 * We load all the csum data we need when we submit
8318 * the first bio to reduce the csum tree search and
8319 * contention.
8320 */
8321 if (dip->logical_offset == file_offset) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008322 ret = btrfs_lookup_bio_sums_dio(inode, dip->orig_bio,
Miao Xiec1dc0892014-09-12 18:43:56 +08008323 file_offset);
8324 if (ret)
8325 return ret;
8326 }
8327
8328 if (bio == dip->orig_bio)
8329 return 0;
8330
8331 file_offset -= dip->logical_offset;
8332 file_offset >>= inode->i_sb->s_blocksize_bits;
8333 io_bio->csum = (u8 *)(((u32 *)orig_io_bio->csum) + file_offset);
8334
8335 return 0;
8336}
8337
David Sterbad0ee3932018-03-08 14:35:48 +01008338static inline blk_status_t btrfs_submit_dio_bio(struct bio *bio,
8339 struct inode *inode, u64 file_offset, int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00008340{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008341 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xiefacc8a222013-07-25 19:22:34 +08008342 struct btrfs_dio_private *dip = bio->bi_private;
Mike Christie37226b22016-06-05 14:31:52 -05008343 bool write = bio_op(bio) == REQ_OP_WRITE;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008344 blk_status_t ret;
Miao Xiee65e1532010-11-22 03:04:43 +00008345
Liu Bo4c274bc2017-11-01 17:19:27 -06008346 /* Check btrfs_submit_bio_hook() for rules about async submit. */
Josef Bacikb812ce22012-11-16 13:56:32 -05008347 if (async_submit)
8348 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
8349
Josef Bacik5fd02042012-05-02 14:00:54 -04008350 if (!write) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008351 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DATA);
Josef Bacik5fd02042012-05-02 14:00:54 -04008352 if (ret)
8353 goto err;
8354 }
Miao Xiee65e1532010-11-22 03:04:43 +00008355
Nikolay Borisove6961ca2017-08-03 15:44:58 +03008356 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Josef Bacik1ae39932011-04-06 14:41:34 -04008357 goto map;
8358
8359 if (write && async_submit) {
Josef Bacikc6100a42017-05-05 11:57:13 -04008360 ret = btrfs_wq_submit_bio(fs_info, bio, 0, 0,
8361 file_offset, inode,
David Sterbae288c082018-07-18 17:36:24 +02008362 btrfs_submit_bio_start_direct_io);
Miao Xiee65e1532010-11-22 03:04:43 +00008363 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04008364 } else if (write) {
8365 /*
8366 * If we aren't doing async submit, calculate the csum of the
8367 * bio now.
8368 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008369 ret = btrfs_csum_one_bio(inode, bio, file_offset, 1);
Josef Bacik1ae39932011-04-06 14:41:34 -04008370 if (ret)
8371 goto err;
Miao Xie23ea8e52014-09-12 18:43:54 +08008372 } else {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008373 ret = btrfs_lookup_and_bind_dio_csum(inode, dip, bio,
Miao Xiec1dc0892014-09-12 18:43:56 +08008374 file_offset);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00008375 if (ret)
8376 goto err;
8377 }
Josef Bacik1ae39932011-04-06 14:41:34 -04008378map:
Liu Bo9b4a9b22017-08-18 11:54:02 -06008379 ret = btrfs_map_bio(fs_info, bio, 0, 0);
Miao Xiee65e1532010-11-22 03:04:43 +00008380err:
Miao Xiee65e1532010-11-22 03:04:43 +00008381 return ret;
8382}
8383
Nikolay Borisove6961ca2017-08-03 15:44:58 +03008384static int btrfs_submit_direct_hook(struct btrfs_dio_private *dip)
Miao Xiee65e1532010-11-22 03:04:43 +00008385{
8386 struct inode *inode = dip->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008387 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xiee65e1532010-11-22 03:04:43 +00008388 struct bio *bio;
8389 struct bio *orig_bio = dip->orig_bio;
Kent Overstreet4f024f32013-10-11 15:44:27 -07008390 u64 start_sector = orig_bio->bi_iter.bi_sector;
Miao Xiee65e1532010-11-22 03:04:43 +00008391 u64 file_offset = dip->logical_offset;
Josef Bacik1ae39932011-04-06 14:41:34 -04008392 int async_submit = 0;
Liu Bo725130b2017-05-16 09:51:39 -07008393 u64 submit_len;
8394 int clone_offset = 0;
8395 int clone_len;
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05308396 int ret;
Omar Sandoval58efbc92017-08-22 23:45:59 -07008397 blk_status_t status;
Nikolay Borisov89b798a2019-06-03 12:05:05 +03008398 struct btrfs_io_geometry geom;
Miao Xiee65e1532010-11-22 03:04:43 +00008399
Nikolay Borisov89b798a2019-06-03 12:05:05 +03008400 submit_len = orig_bio->bi_iter.bi_size;
8401 ret = btrfs_get_io_geometry(fs_info, btrfs_op(orig_bio),
8402 start_sector << 9, submit_len, &geom);
Miao Xie7a5c3c92014-06-17 18:58:59 +08008403 if (ret)
Miao Xiee65e1532010-11-22 03:04:43 +00008404 return -EIO;
Miao Xiefacc8a222013-07-25 19:22:34 +08008405
Nikolay Borisov89b798a2019-06-03 12:05:05 +03008406 if (geom.len >= submit_len) {
Josef Bacik02f57c72011-04-06 14:25:44 -04008407 bio = orig_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08008408 dip->flags |= BTRFS_DIO_ORIG_BIO_SUBMITTED;
Josef Bacik02f57c72011-04-06 14:25:44 -04008409 goto submit;
8410 }
8411
David Woodhouse53b381b2013-01-29 18:40:14 -05008412 /* async crcs make it difficult to collect full stripe writes. */
Jeff Mahoney1b868262017-05-17 11:38:35 -04008413 if (btrfs_data_alloc_profile(fs_info) & BTRFS_BLOCK_GROUP_RAID56_MASK)
David Woodhouse53b381b2013-01-29 18:40:14 -05008414 async_submit = 0;
8415 else
8416 async_submit = 1;
8417
Liu Bo725130b2017-05-16 09:51:39 -07008418 /* bio split */
Nikolay Borisov89b798a2019-06-03 12:05:05 +03008419 ASSERT(geom.len <= INT_MAX);
Josef Bacik02f57c72011-04-06 14:25:44 -04008420 atomic_inc(&dip->pending_bios);
Arnd Bergmann3c91ee62017-05-18 15:33:29 +02008421 do {
Nikolay Borisov89b798a2019-06-03 12:05:05 +03008422 clone_len = min_t(int, submit_len, geom.len);
Josef Bacik02f57c72011-04-06 14:25:44 -04008423
Liu Bo725130b2017-05-16 09:51:39 -07008424 /*
8425 * This will never fail as it's passing GPF_NOFS and
8426 * the allocation is backed by btrfs_bioset.
8427 */
Liu Boe4770942017-05-16 10:57:14 -07008428 bio = btrfs_bio_clone_partial(orig_bio, clone_offset,
Liu Bo725130b2017-05-16 09:51:39 -07008429 clone_len);
8430 bio->bi_private = dip;
8431 bio->bi_end_io = btrfs_end_dio_bio;
8432 btrfs_io_bio(bio)->logical = file_offset;
Miao Xiee65e1532010-11-22 03:04:43 +00008433
Liu Bo725130b2017-05-16 09:51:39 -07008434 ASSERT(submit_len >= clone_len);
8435 submit_len -= clone_len;
8436 if (submit_len == 0)
8437 break;
Miao Xiee65e1532010-11-22 03:04:43 +00008438
Liu Bo725130b2017-05-16 09:51:39 -07008439 /*
8440 * Increase the count before we submit the bio so we know
8441 * the end IO handler won't happen before we increase the
8442 * count. Otherwise, the dip might get freed before we're
8443 * done setting it up.
8444 */
8445 atomic_inc(&dip->pending_bios);
Miao Xiee65e1532010-11-22 03:04:43 +00008446
David Sterbad0ee3932018-03-08 14:35:48 +01008447 status = btrfs_submit_dio_bio(bio, inode, file_offset,
Omar Sandoval58efbc92017-08-22 23:45:59 -07008448 async_submit);
8449 if (status) {
Liu Bo725130b2017-05-16 09:51:39 -07008450 bio_put(bio);
8451 atomic_dec(&dip->pending_bios);
8452 goto out_err;
Miao Xiee65e1532010-11-22 03:04:43 +00008453 }
Liu Bo725130b2017-05-16 09:51:39 -07008454
8455 clone_offset += clone_len;
8456 start_sector += clone_len >> 9;
8457 file_offset += clone_len;
8458
Nikolay Borisov89b798a2019-06-03 12:05:05 +03008459 ret = btrfs_get_io_geometry(fs_info, btrfs_op(orig_bio),
8460 start_sector << 9, submit_len, &geom);
Liu Bo725130b2017-05-16 09:51:39 -07008461 if (ret)
8462 goto out_err;
Arnd Bergmann3c91ee62017-05-18 15:33:29 +02008463 } while (submit_len > 0);
Miao Xiee65e1532010-11-22 03:04:43 +00008464
Josef Bacik02f57c72011-04-06 14:25:44 -04008465submit:
David Sterbad0ee3932018-03-08 14:35:48 +01008466 status = btrfs_submit_dio_bio(bio, inode, file_offset, async_submit);
Omar Sandoval58efbc92017-08-22 23:45:59 -07008467 if (!status)
Miao Xiee65e1532010-11-22 03:04:43 +00008468 return 0;
8469
8470 bio_put(bio);
8471out_err:
8472 dip->errors = 1;
8473 /*
Nikolay Borisovde224b72018-02-14 10:53:36 +02008474 * Before atomic variable goto zero, we must make sure dip->errors is
8475 * perceived to be set. This ordering is ensured by the fact that an
8476 * atomic operations with a return value are fully ordered as per
8477 * atomic_t.txt
Miao Xiee65e1532010-11-22 03:04:43 +00008478 */
Miao Xiee65e1532010-11-22 03:04:43 +00008479 if (atomic_dec_and_test(&dip->pending_bios))
8480 bio_io_error(dip->orig_bio);
8481
8482 /* bio_end_io() will handle error, so we needn't return it */
8483 return 0;
8484}
8485
Mike Christie8a4c1e42016-06-05 14:31:50 -05008486static void btrfs_submit_direct(struct bio *dio_bio, struct inode *inode,
8487 loff_t file_offset)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008488{
Filipe Manana61de7182015-07-01 12:13:10 +01008489 struct btrfs_dio_private *dip = NULL;
Liu Bo3892ac92017-04-17 15:00:28 -07008490 struct bio *bio = NULL;
8491 struct btrfs_io_bio *io_bio;
Mike Christie8a4c1e42016-06-05 14:31:50 -05008492 bool write = (bio_op(dio_bio) == REQ_OP_WRITE);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008493 int ret = 0;
8494
David Sterba8b6c1d52017-06-02 17:48:13 +02008495 bio = btrfs_bio_clone(dio_bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008496
Miao Xiec1dc0892014-09-12 18:43:56 +08008497 dip = kzalloc(sizeof(*dip), GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04008498 if (!dip) {
8499 ret = -ENOMEM;
Filipe Manana61de7182015-07-01 12:13:10 +01008500 goto free_ordered;
Chris Mason9be33952013-05-17 18:30:14 -04008501 }
8502
8503 dip->private = dio_bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008504 dip->inode = inode;
8505 dip->logical_offset = file_offset;
Kent Overstreet4f024f32013-10-11 15:44:27 -07008506 dip->bytes = dio_bio->bi_iter.bi_size;
8507 dip->disk_bytenr = (u64)dio_bio->bi_iter.bi_sector << 9;
Liu Bo3892ac92017-04-17 15:00:28 -07008508 bio->bi_private = dip;
8509 dip->orig_bio = bio;
Chris Mason9be33952013-05-17 18:30:14 -04008510 dip->dio_bio = dio_bio;
Miao Xiee65e1532010-11-22 03:04:43 +00008511 atomic_set(&dip->pending_bios, 0);
Liu Bo3892ac92017-04-17 15:00:28 -07008512 io_bio = btrfs_io_bio(bio);
8513 io_bio->logical = file_offset;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008514
Miao Xiec1dc0892014-09-12 18:43:56 +08008515 if (write) {
Liu Bo3892ac92017-04-17 15:00:28 -07008516 bio->bi_end_io = btrfs_endio_direct_write;
Miao Xiec1dc0892014-09-12 18:43:56 +08008517 } else {
Liu Bo3892ac92017-04-17 15:00:28 -07008518 bio->bi_end_io = btrfs_endio_direct_read;
Miao Xiec1dc0892014-09-12 18:43:56 +08008519 dip->subio_endio = btrfs_subio_endio_read;
8520 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04008521
Filipe Mananaf28a4922015-12-08 19:23:20 +00008522 /*
8523 * Reset the range for unsubmitted ordered extents (to a 0 length range)
8524 * even if we fail to submit a bio, because in such case we do the
8525 * corresponding error handling below and it must not be done a second
8526 * time by btrfs_direct_IO().
8527 */
8528 if (write) {
8529 struct btrfs_dio_data *dio_data = current->journal_info;
8530
8531 dio_data->unsubmitted_oe_range_end = dip->logical_offset +
8532 dip->bytes;
8533 dio_data->unsubmitted_oe_range_start =
8534 dio_data->unsubmitted_oe_range_end;
8535 }
8536
Nikolay Borisove6961ca2017-08-03 15:44:58 +03008537 ret = btrfs_submit_direct_hook(dip);
Miao Xiee65e1532010-11-22 03:04:43 +00008538 if (!ret)
Chris Masoneaf25d92010-05-25 09:48:28 -04008539 return;
Chris Mason9be33952013-05-17 18:30:14 -04008540
David Sterbab3a0dd52018-11-22 17:16:49 +01008541 btrfs_io_bio_free_csum(io_bio);
Chris Mason9be33952013-05-17 18:30:14 -04008542
Josef Bacik4b46fce2010-05-23 11:00:55 -04008543free_ordered:
8544 /*
Filipe Manana61de7182015-07-01 12:13:10 +01008545 * If we arrived here it means either we failed to submit the dip
8546 * or we either failed to clone the dio_bio or failed to allocate the
8547 * dip. If we cloned the dio_bio and allocated the dip, we can just
8548 * call bio_endio against our io_bio so that we get proper resource
8549 * cleanup if we fail to submit the dip, otherwise, we must do the
8550 * same as btrfs_endio_direct_[write|read] because we can't call these
8551 * callbacks - they require an allocated dip and a clone of dio_bio.
Josef Bacik4b46fce2010-05-23 11:00:55 -04008552 */
Liu Bo3892ac92017-04-17 15:00:28 -07008553 if (bio && dip) {
Guoqing Jiang054ec2f2017-06-02 16:08:50 +08008554 bio_io_error(bio);
Filipe Manana61de7182015-07-01 12:13:10 +01008555 /*
Liu Bo3892ac92017-04-17 15:00:28 -07008556 * The end io callbacks free our dip, do the final put on bio
Filipe Manana61de7182015-07-01 12:13:10 +01008557 * and all the cleanup and final put for dio_bio (through
8558 * dio_end_io()).
8559 */
8560 dip = NULL;
Liu Bo3892ac92017-04-17 15:00:28 -07008561 bio = NULL;
Filipe Manana61de7182015-07-01 12:13:10 +01008562 } else {
Filipe Manana14543772015-11-24 16:23:54 +00008563 if (write)
Qu Wenruo524272602017-03-08 10:25:52 +08008564 __endio_write_update_ordered(inode,
Filipe Manana14543772015-11-24 16:23:54 +00008565 file_offset,
8566 dio_bio->bi_iter.bi_size,
Qu Wenruo524272602017-03-08 10:25:52 +08008567 false);
Filipe Manana14543772015-11-24 16:23:54 +00008568 else
Filipe Manana61de7182015-07-01 12:13:10 +01008569 unlock_extent(&BTRFS_I(inode)->io_tree, file_offset,
8570 file_offset + dio_bio->bi_iter.bi_size - 1);
Filipe Manana14543772015-11-24 16:23:54 +00008571
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008572 dio_bio->bi_status = BLK_STS_IOERR;
Filipe Manana61de7182015-07-01 12:13:10 +01008573 /*
8574 * Releases and cleans up our dio_bio, no need to bio_put()
8575 * nor bio_endio()/bio_io_error() against dio_bio.
8576 */
Christoph Hellwig40553512017-06-03 09:37:58 +02008577 dio_end_io(dio_bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008578 }
Liu Bo3892ac92017-04-17 15:00:28 -07008579 if (bio)
8580 bio_put(bio);
Filipe Manana61de7182015-07-01 12:13:10 +01008581 kfree(dip);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008582}
8583
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008584static ssize_t check_direct_IO(struct btrfs_fs_info *fs_info,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008585 const struct iov_iter *iter, loff_t offset)
Chris Mason5a5f79b2010-05-26 21:33:37 -04008586{
8587 int seg;
Josef Bacika1b75f72011-04-08 15:51:18 +00008588 int i;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008589 unsigned int blocksize_mask = fs_info->sectorsize - 1;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008590 ssize_t retval = -EINVAL;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008591
8592 if (offset & blocksize_mask)
8593 goto out;
8594
Al Viro28060d52014-03-22 05:15:17 -04008595 if (iov_iter_alignment(iter) & blocksize_mask)
8596 goto out;
Josef Bacika1b75f72011-04-08 15:51:18 +00008597
Al Viro28060d52014-03-22 05:15:17 -04008598 /* If this is a write we don't need to check anymore */
Al Virocd27e452016-10-10 13:39:05 -04008599 if (iov_iter_rw(iter) != READ || !iter_is_iovec(iter))
Al Viro28060d52014-03-22 05:15:17 -04008600 return 0;
8601 /*
8602 * Check to make sure we don't have duplicate iov_base's in this
8603 * iovec, if so return EINVAL, otherwise we'll get csum errors
8604 * when reading back.
8605 */
8606 for (seg = 0; seg < iter->nr_segs; seg++) {
8607 for (i = seg + 1; i < iter->nr_segs; i++) {
8608 if (iter->iov[seg].iov_base == iter->iov[i].iov_base)
Josef Bacika1b75f72011-04-08 15:51:18 +00008609 goto out;
8610 }
Chris Mason5a5f79b2010-05-26 21:33:37 -04008611 }
8612 retval = 0;
8613out:
8614 return retval;
8615}
Josef Bacikeb838e72012-07-31 16:28:48 -04008616
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008617static ssize_t btrfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
Chris Mason16432982008-04-10 10:23:21 -04008618{
Josef Bacik4b46fce2010-05-23 11:00:55 -04008619 struct file *file = iocb->ki_filp;
8620 struct inode *inode = file->f_mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008621 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
chandan50745b02015-08-28 21:10:13 +05308622 struct btrfs_dio_data dio_data = { 0 };
Qu Wenruo364ecf32017-02-27 15:10:38 +08008623 struct extent_changeset *data_reserved = NULL;
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008624 loff_t offset = iocb->ki_pos;
Miao Xie09348562013-02-07 10:12:07 +00008625 size_t count = 0;
Miao Xie2e60a512013-02-08 07:01:08 +00008626 int flags = 0;
Miao Xie38851cc2013-02-08 07:04:11 +00008627 bool wakeup = true;
8628 bool relock = false;
Miao Xie09348562013-02-07 10:12:07 +00008629 ssize_t ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008630
Nikolay Borisov8c70c9f2017-08-21 12:43:46 +03008631 if (check_direct_IO(fs_info, iter, offset))
Chris Mason5a5f79b2010-05-26 21:33:37 -04008632 return 0;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008633
Jens Axboefe0f07d2015-04-15 17:05:48 -06008634 inode_dio_begin(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008635
Josef Bacik0e267c42013-07-02 10:38:02 -04008636 /*
Miao Xie41bd9ca2014-03-06 13:54:57 +08008637 * The generic stuff only does filemap_write_and_wait_range, which
8638 * isn't enough if we've written compressed pages to this area, so
8639 * we need to flush the dirty pages again to make absolutely sure
8640 * that any outstanding dirty pages are on disk.
Josef Bacik0e267c42013-07-02 10:38:02 -04008641 */
Al Viroa6cbcd42014-03-04 22:38:00 -05008642 count = iov_iter_count(iter);
Miao Xie41bd9ca2014-03-06 13:54:57 +08008643 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
8644 &BTRFS_I(inode)->runtime_flags))
Wang Shilong9a025a02014-07-17 11:44:13 +08008645 filemap_fdatawrite_range(inode->i_mapping, offset,
8646 offset + count - 1);
Josef Bacik0e267c42013-07-02 10:38:02 -04008647
Omar Sandoval6f673762015-03-16 04:33:52 -07008648 if (iov_iter_rw(iter) == WRITE) {
Miao Xie38851cc2013-02-08 07:04:11 +00008649 /*
8650 * If the write DIO is beyond the EOF, we need update
8651 * the isize, but it is protected by i_mutex. So we can
8652 * not unlock the i_mutex at this case.
8653 */
8654 if (offset + count <= inode->i_size) {
Liu Bo4aaedfb2016-12-14 22:36:05 -08008655 dio_data.overwrite = 1;
Al Viro59551022016-01-22 15:40:57 -05008656 inode_unlock(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008657 relock = true;
Goldwyn Rodriguesedf064e2017-06-20 07:05:49 -05008658 } else if (iocb->ki_flags & IOCB_NOWAIT) {
8659 ret = -EAGAIN;
8660 goto out;
Miao Xie38851cc2013-02-08 07:04:11 +00008661 }
Qu Wenruo364ecf32017-02-27 15:10:38 +08008662 ret = btrfs_delalloc_reserve_space(inode, &data_reserved,
8663 offset, count);
Miao Xie09348562013-02-07 10:12:07 +00008664 if (ret)
Miao Xie38851cc2013-02-08 07:04:11 +00008665 goto out;
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008666
8667 /*
8668 * We need to know how many extents we reserved so that we can
8669 * do the accounting properly if we go over the number we
8670 * originally calculated. Abuse current->journal_info for this.
8671 */
Jeff Mahoneyda170662016-06-15 09:22:56 -04008672 dio_data.reserve = round_up(count,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008673 fs_info->sectorsize);
Filipe Mananaf28a4922015-12-08 19:23:20 +00008674 dio_data.unsubmitted_oe_range_start = (u64)offset;
8675 dio_data.unsubmitted_oe_range_end = (u64)offset;
chandan50745b02015-08-28 21:10:13 +05308676 current->journal_info = &dio_data;
Chandan Rajendra97dcdea2016-12-23 15:00:18 +05308677 down_read(&BTRFS_I(inode)->dio_sem);
David Sterbaee39b432014-09-30 01:33:33 +02008678 } else if (test_bit(BTRFS_INODE_READDIO_NEED_LOCK,
8679 &BTRFS_I(inode)->runtime_flags)) {
Jens Axboefe0f07d2015-04-15 17:05:48 -06008680 inode_dio_end(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008681 flags = DIO_LOCKING | DIO_SKIP_HOLES;
8682 wakeup = false;
Miao Xie09348562013-02-07 10:12:07 +00008683 }
8684
Omar Sandoval17f8c842015-03-16 04:33:50 -07008685 ret = __blockdev_direct_IO(iocb, inode,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008686 fs_info->fs_devices->latest_bdev,
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008687 iter, btrfs_get_blocks_direct, NULL,
Omar Sandoval17f8c842015-03-16 04:33:50 -07008688 btrfs_submit_direct, flags);
Omar Sandoval6f673762015-03-16 04:33:52 -07008689 if (iov_iter_rw(iter) == WRITE) {
Chandan Rajendra97dcdea2016-12-23 15:00:18 +05308690 up_read(&BTRFS_I(inode)->dio_sem);
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008691 current->journal_info = NULL;
Liu Boddba1bf2015-06-17 16:59:58 +08008692 if (ret < 0 && ret != -EIOCBQUEUED) {
chandan50745b02015-08-28 21:10:13 +05308693 if (dio_data.reserve)
Qu Wenruobc42bda2017-02-27 15:10:39 +08008694 btrfs_delalloc_release_space(inode, data_reserved,
Qu Wenruo43b18592017-12-12 15:34:32 +08008695 offset, dio_data.reserve, true);
Filipe Mananaf28a4922015-12-08 19:23:20 +00008696 /*
8697 * On error we might have left some ordered extents
8698 * without submitting corresponding bios for them, so
8699 * cleanup them up to avoid other tasks getting them
8700 * and waiting for them to complete forever.
8701 */
8702 if (dio_data.unsubmitted_oe_range_start <
8703 dio_data.unsubmitted_oe_range_end)
Qu Wenruo524272602017-03-08 10:25:52 +08008704 __endio_write_update_ordered(inode,
Filipe Mananaf28a4922015-12-08 19:23:20 +00008705 dio_data.unsubmitted_oe_range_start,
8706 dio_data.unsubmitted_oe_range_end -
8707 dio_data.unsubmitted_oe_range_start,
Qu Wenruo524272602017-03-08 10:25:52 +08008708 false);
Liu Boddba1bf2015-06-17 16:59:58 +08008709 } else if (ret >= 0 && (size_t)ret < count)
Qu Wenruobc42bda2017-02-27 15:10:39 +08008710 btrfs_delalloc_release_space(inode, data_reserved,
Qu Wenruo43b18592017-12-12 15:34:32 +08008711 offset, count - (size_t)ret, true);
Qu Wenruo8702ba92019-10-14 14:34:51 +08008712 btrfs_delalloc_release_extents(BTRFS_I(inode), count);
Miao Xie09348562013-02-07 10:12:07 +00008713 }
Miao Xie38851cc2013-02-08 07:04:11 +00008714out:
Miao Xie2e60a512013-02-08 07:01:08 +00008715 if (wakeup)
Jens Axboefe0f07d2015-04-15 17:05:48 -06008716 inode_dio_end(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008717 if (relock)
Al Viro59551022016-01-22 15:40:57 -05008718 inode_lock(inode);
Miao Xie09348562013-02-07 10:12:07 +00008719
Qu Wenruo364ecf32017-02-27 15:10:38 +08008720 extent_changeset_free(data_reserved);
Miao Xie09348562013-02-07 10:12:07 +00008721 return ret;
Chris Mason16432982008-04-10 10:23:21 -04008722}
8723
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008724#define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
8725
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008726static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
8727 __u64 start, __u64 len)
8728{
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008729 int ret;
8730
8731 ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS);
8732 if (ret)
8733 return ret;
8734
David Sterba2135fb92017-06-23 04:09:57 +02008735 return extent_fiemap(inode, fieinfo, start, len);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008736}
8737
Chris Mason9ebefb182007-06-15 13:50:00 -04008738int btrfs_readpage(struct file *file, struct page *page)
8739{
Chris Masond1310b22008-01-24 16:13:08 -05008740 struct extent_io_tree *tree;
8741 tree = &BTRFS_I(page->mapping->host)->io_tree;
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02008742 return extent_read_full_page(tree, page, btrfs_get_extent, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04008743}
Chris Mason1832a6d2007-12-21 16:27:21 -05008744
Chris Mason39279cc2007-06-12 06:35:45 -04008745static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
8746{
Josef Bacikbe7bd732015-10-22 15:05:09 -04008747 struct inode *inode = page->mapping->host;
8748 int ret;
Chris Masonb888db22007-08-27 16:49:44 -04008749
8750 if (current->flags & PF_MEMALLOC) {
8751 redirty_page_for_writepage(wbc, page);
8752 unlock_page(page);
8753 return 0;
8754 }
Josef Bacikbe7bd732015-10-22 15:05:09 -04008755
8756 /*
8757 * If we are under memory pressure we will call this directly from the
8758 * VM, we need to make sure we have the inode referenced for the ordered
8759 * extent. If not just return like we didn't do anything.
8760 */
8761 if (!igrab(inode)) {
8762 redirty_page_for_writepage(wbc, page);
8763 return AOP_WRITEPAGE_ACTIVATE;
8764 }
Nikolay Borisov0a9b0e52017-12-08 15:55:59 +02008765 ret = extent_write_full_page(page, wbc);
Josef Bacikbe7bd732015-10-22 15:05:09 -04008766 btrfs_add_delayed_iput(inode);
8767 return ret;
Chris Masona52d9a82007-08-27 16:49:44 -04008768}
Chris Mason39279cc2007-06-12 06:35:45 -04008769
Eric Sandeen48a3b632013-04-25 20:41:01 +00008770static int btrfs_writepages(struct address_space *mapping,
8771 struct writeback_control *wbc)
Chris Masonb293f02e2007-11-01 19:45:34 -04008772{
Nikolay Borisov8ae225a2018-04-19 10:46:38 +03008773 return extent_writepages(mapping, wbc);
Chris Masonb293f02e2007-11-01 19:45:34 -04008774}
8775
Chris Mason3ab2fb52007-11-08 10:59:22 -05008776static int
8777btrfs_readpages(struct file *file, struct address_space *mapping,
8778 struct list_head *pages, unsigned nr_pages)
8779{
Nikolay Borisov2a3ff0a2018-04-19 10:46:36 +03008780 return extent_readpages(mapping, pages, nr_pages);
Chris Mason3ab2fb52007-11-08 10:59:22 -05008781}
Nikolay Borisov2a3ff0a2018-04-19 10:46:36 +03008782
Chris Masone6dcd2d2008-07-17 12:53:50 -04008783static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04008784{
Nikolay Borisov477a30b2018-04-19 10:46:34 +03008785 int ret = try_release_extent_mapping(page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04008786 if (ret == 1) {
8787 ClearPagePrivate(page);
8788 set_page_private(page, 0);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008789 put_page(page);
Chris Masona52d9a82007-08-27 16:49:44 -04008790 }
8791 return ret;
8792}
Chris Mason39279cc2007-06-12 06:35:45 -04008793
Chris Masone6dcd2d2008-07-17 12:53:50 -04008794static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
8795{
Chris Mason98509cf2008-09-11 15:51:43 -04008796 if (PageWriteback(page) || PageDirty(page))
8797 return 0;
Michal Hocko3ba7ab22017-01-09 15:39:02 +01008798 return __btrfs_releasepage(page, gfp_flags);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008799}
8800
Lukas Czernerd47992f2013-05-21 23:17:23 -04008801static void btrfs_invalidatepage(struct page *page, unsigned int offset,
8802 unsigned int length)
Chris Masona52d9a82007-08-27 16:49:44 -04008803{
Josef Bacik5fd02042012-05-02 14:00:54 -04008804 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05008805 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008806 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008807 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008808 u64 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008809 u64 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308810 u64 start;
8811 u64 end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008812 int inode_evicting = inode->i_state & I_FREEING;
Chris Masona52d9a82007-08-27 16:49:44 -04008813
Chris Mason8b62b722009-09-02 16:53:46 -04008814 /*
8815 * we have the page locked, so new writeback can't start,
8816 * and the dirty bit won't be cleared while we are here.
8817 *
8818 * Wait for IO on this page so that we can safely clear
8819 * the PagePrivate2 bit and do ordered accounting
8820 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008821 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04008822
Josef Bacik5fd02042012-05-02 14:00:54 -04008823 tree = &BTRFS_I(inode)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008824 if (offset) {
8825 btrfs_releasepage(page, GFP_NOFS);
8826 return;
8827 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008828
8829 if (!inode_evicting)
David Sterbaff13db42015-12-03 14:30:40 +01008830 lock_extent_bits(tree, page_start, page_end, &cached_state);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308831again:
8832 start = page_start;
Nikolay Borisova776c6f2017-02-20 13:50:49 +02008833 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), start,
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308834 page_end - start + 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008835 if (ordered) {
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308836 end = min(page_end, ordered->file_offset + ordered->len - 1);
Chris Masoneb84ae02008-07-17 13:53:27 -04008837 /*
8838 * IO on this page will never be started, so we need
8839 * to account for any ordered extents now
8840 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008841 if (!inode_evicting)
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308842 clear_extent_bit(tree, start, end,
Omar Sandovale1821632019-08-15 14:04:04 -07008843 EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008844 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
David Sterbaae0f1622017-10-31 16:37:52 +01008845 EXTENT_DEFRAG, 1, 0, &cached_state);
Chris Mason8b62b722009-09-02 16:53:46 -04008846 /*
8847 * whoever cleared the private bit is responsible
8848 * for the finish_ordered_io
8849 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04008850 if (TestClearPagePrivate2(page)) {
8851 struct btrfs_ordered_inode_tree *tree;
8852 u64 new_len;
8853
8854 tree = &BTRFS_I(inode)->ordered_tree;
8855
8856 spin_lock_irq(&tree->lock);
8857 set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308858 new_len = start - ordered->file_offset;
Josef Bacik77cef2e2013-08-29 13:57:21 -04008859 if (new_len < ordered->truncated_len)
8860 ordered->truncated_len = new_len;
8861 spin_unlock_irq(&tree->lock);
8862
8863 if (btrfs_dec_test_ordered_pending(inode, &ordered,
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308864 start,
8865 end - start + 1, 1))
Josef Bacik77cef2e2013-08-29 13:57:21 -04008866 btrfs_finish_ordered_io(ordered);
Chris Mason8b62b722009-09-02 16:53:46 -04008867 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008868 btrfs_put_ordered_extent(ordered);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008869 if (!inode_evicting) {
8870 cached_state = NULL;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308871 lock_extent_bits(tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008872 &cached_state);
8873 }
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308874
8875 start = end + 1;
8876 if (start < page_end)
8877 goto again;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008878 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008879
Qu Wenruob9d0b382015-09-29 10:35:16 +08008880 /*
8881 * Qgroup reserved space handler
8882 * Page here will be either
8883 * 1) Already written to disk
8884 * In this case, its reserved space is released from data rsv map
8885 * and will be freed by delayed_ref handler finally.
8886 * So even we call qgroup_free_data(), it won't decrease reserved
8887 * space.
8888 * 2) Not written to disk
Goldwyn Rodrigues0b34c262016-09-30 10:40:52 -05008889 * This means the reserved space should be freed here. However,
8890 * if a truncate invalidates the page (by clearing PageDirty)
8891 * and the page is accounted for while allocating extent
8892 * in btrfs_check_data_free_space() we let delayed_ref to
8893 * free the entire extent.
Qu Wenruob9d0b382015-09-29 10:35:16 +08008894 */
Goldwyn Rodrigues0b34c262016-09-30 10:40:52 -05008895 if (PageDirty(page))
Qu Wenruobc42bda2017-02-27 15:10:39 +08008896 btrfs_qgroup_free_data(inode, NULL, page_start, PAGE_SIZE);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008897 if (!inode_evicting) {
Omar Sandovale1821632019-08-15 14:04:04 -07008898 clear_extent_bit(tree, page_start, page_end, EXTENT_LOCKED |
Filipe Mananaa7e3b972017-04-03 10:45:46 +01008899 EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
8900 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1,
David Sterbaae0f1622017-10-31 16:37:52 +01008901 &cached_state);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008902
8903 __btrfs_releasepage(page, GFP_NOFS);
8904 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008905
Chris Mason4a096752008-07-21 10:29:44 -04008906 ClearPageChecked(page);
Chris Mason9ad6b7bc2008-04-18 16:11:30 -04008907 if (PagePrivate(page)) {
Chris Mason9ad6b7bc2008-04-18 16:11:30 -04008908 ClearPagePrivate(page);
8909 set_page_private(page, 0);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008910 put_page(page);
Chris Mason9ad6b7bc2008-04-18 16:11:30 -04008911 }
Chris Mason39279cc2007-06-12 06:35:45 -04008912}
8913
Chris Mason9ebefb182007-06-15 13:50:00 -04008914/*
8915 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
8916 * called from a page fault handler when a page is first dirtied. Hence we must
8917 * be careful to check for EOF conditions here. We set the page up correctly
8918 * for a written page which means we get ENOSPC checking when writing into
8919 * holes and correct delalloc and unwritten extent mapping on filesystems that
8920 * support these features.
8921 *
8922 * We are not allowed to take the i_mutex here so we have to play games to
8923 * protect against truncate races as the page could now be beyond EOF. Because
Omar Sandovald1342aa2018-05-11 13:13:29 -07008924 * truncate_setsize() writes the inode size before removing pages, once we have
8925 * the page lock we can determine safely if the page is beyond EOF. If it is not
Chris Mason9ebefb182007-06-15 13:50:00 -04008926 * beyond EOF, then the page is guaranteed safe against truncation until we
8927 * unlock the page.
8928 */
Souptick Joardera528a242018-06-06 19:54:44 +05308929vm_fault_t btrfs_page_mkwrite(struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04008930{
Nick Pigginc2ec1752009-03-31 15:23:21 -07008931 struct page *page = vmf->page;
Dave Jiang11bac802017-02-24 14:56:41 -08008932 struct inode *inode = file_inode(vmf->vma->vm_file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008933 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008934 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
8935 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008936 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08008937 struct extent_changeset *data_reserved = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008938 char *kaddr;
8939 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04008940 loff_t size;
Souptick Joardera528a242018-06-06 19:54:44 +05308941 vm_fault_t ret;
8942 int ret2;
Chris Mason9998eb72012-01-25 13:47:40 -05008943 int reserved = 0;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308944 u64 reserved_space;
Chris Masona52d9a82007-08-27 16:49:44 -04008945 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008946 u64 page_end;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308947 u64 end;
8948
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008949 reserved_space = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04008950
Jan Karab2b5ef52012-06-12 16:20:45 +02008951 sb_start_pagefault(inode->i_sb);
Qu Wenruodf480632015-09-08 17:25:54 +08008952 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008953 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308954 end = page_end;
Qu Wenruodf480632015-09-08 17:25:54 +08008955
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308956 /*
8957 * Reserving delalloc space after obtaining the page lock can lead to
8958 * deadlock. For example, if a dirty page is locked by this function
8959 * and the call to btrfs_delalloc_reserve_space() ends up triggering
8960 * dirty page write out, then the btrfs_writepage() function could
8961 * end up waiting indefinitely to get a lock on the page currently
8962 * being processed by btrfs_page_mkwrite() function.
8963 */
Souptick Joardera528a242018-06-06 19:54:44 +05308964 ret2 = btrfs_delalloc_reserve_space(inode, &data_reserved, page_start,
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308965 reserved_space);
Souptick Joardera528a242018-06-06 19:54:44 +05308966 if (!ret2) {
8967 ret2 = file_update_time(vmf->vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05008968 reserved = 1;
8969 }
Souptick Joardera528a242018-06-06 19:54:44 +05308970 if (ret2) {
8971 ret = vmf_error(ret2);
Chris Mason9998eb72012-01-25 13:47:40 -05008972 if (reserved)
8973 goto out;
8974 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07008975 }
Chris Mason1832a6d2007-12-21 16:27:21 -05008976
Nick Piggin56a76f82009-03-31 15:23:23 -07008977 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008978again:
Chris Mason9ebefb182007-06-15 13:50:00 -04008979 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04008980 size = i_size_read(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04008981
Chris Mason9ebefb182007-06-15 13:50:00 -04008982 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04008983 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04008984 /* page got truncated out from underneath us */
8985 goto out_unlock;
8986 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008987 wait_on_page_writeback(page);
8988
David Sterbaff13db42015-12-03 14:30:40 +01008989 lock_extent_bits(io_tree, page_start, page_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008990 set_page_extent_mapped(page);
8991
Chris Masoneb84ae02008-07-17 13:53:27 -04008992 /*
8993 * we can't set the delalloc bits if there are pending ordered
8994 * extents. Drop our locks and wait for them to finish
8995 */
Nikolay Borisova776c6f2017-02-20 13:50:49 +02008996 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start,
8997 PAGE_SIZE);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008998 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00008999 unlock_extent_cached(io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01009000 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009001 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04009002 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009003 btrfs_put_ordered_extent(ordered);
9004 goto again;
9005 }
9006
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009007 if (page->index == ((size - 1) >> PAGE_SHIFT)) {
Jeff Mahoneyda170662016-06-15 09:22:56 -04009008 reserved_space = round_up(size - page_start,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009009 fs_info->sectorsize);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009010 if (reserved_space < PAGE_SIZE) {
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309011 end = page_start + reserved_space - 1;
Qu Wenruobc42bda2017-02-27 15:10:39 +08009012 btrfs_delalloc_release_space(inode, data_reserved,
Qu Wenruo43b18592017-12-12 15:34:32 +08009013 page_start, PAGE_SIZE - reserved_space,
9014 true);
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309015 }
9016 }
9017
Josef Bacikfbf19082009-10-01 17:10:23 -04009018 /*
Liu Bo54160342016-12-13 12:15:19 -08009019 * page_mkwrite gets called when the page is firstly dirtied after it's
9020 * faulted in, but write(2) could also dirty a page and set delalloc
9021 * bits, thus in this case for space account reason, we still need to
9022 * clear any delalloc bits within this page range since we have to
9023 * reserve data&meta space before lock_page() (see above comments).
Josef Bacikfbf19082009-10-01 17:10:23 -04009024 */
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309025 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, end,
Omar Sandovale1821632019-08-15 14:04:04 -07009026 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
9027 EXTENT_DEFRAG, 0, 0, &cached_state);
Josef Bacikfbf19082009-10-01 17:10:23 -04009028
Souptick Joardera528a242018-06-06 19:54:44 +05309029 ret2 = btrfs_set_extent_delalloc(inode, page_start, end, 0,
Nikolay Borisov330a5822019-07-17 16:18:17 +03009030 &cached_state);
Souptick Joardera528a242018-06-06 19:54:44 +05309031 if (ret2) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00009032 unlock_extent_cached(io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01009033 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009034 ret = VM_FAULT_SIGBUS;
9035 goto out_unlock;
9036 }
Souptick Joardera528a242018-06-06 19:54:44 +05309037 ret2 = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04009038
9039 /* page is wholly or partially inside EOF */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009040 if (page_start + PAGE_SIZE > size)
Johannes Thumshirn70730172018-12-05 15:23:03 +01009041 zero_start = offset_in_page(size);
Chris Mason9ebefb182007-06-15 13:50:00 -04009042 else
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009043 zero_start = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04009044
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009045 if (zero_start != PAGE_SIZE) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04009046 kaddr = kmap(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009047 memset(kaddr + zero_start, 0, PAGE_SIZE - zero_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009048 flush_dcache_page(page);
9049 kunmap(page);
9050 }
Chris Mason247e7432008-07-17 12:53:51 -04009051 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009052 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04009053 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04009054
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009055 BTRFS_I(inode)->last_trans = fs_info->generation;
Chris Mason257c62e2009-10-13 13:21:08 -04009056 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06009057 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
Chris Mason257c62e2009-10-13 13:21:08 -04009058
David Sterbae43bbe52017-12-12 21:43:52 +01009059 unlock_extent_cached(io_tree, page_start, page_end, &cached_state);
Chris Mason9ebefb182007-06-15 13:50:00 -04009060
Souptick Joardera528a242018-06-06 19:54:44 +05309061 if (!ret2) {
Qu Wenruo8702ba92019-10-14 14:34:51 +08009062 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE);
Jan Karab2b5ef52012-06-12 16:20:45 +02009063 sb_end_pagefault(inode->i_sb);
Qu Wenruo364ecf32017-02-27 15:10:38 +08009064 extent_changeset_free(data_reserved);
Chris Mason50a9b212009-09-11 12:33:12 -04009065 return VM_FAULT_LOCKED;
Jan Karab2b5ef52012-06-12 16:20:45 +02009066 }
Chris Mason717beb92018-06-25 10:03:41 -07009067
9068out_unlock:
Chris Mason9ebefb182007-06-15 13:50:00 -04009069 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05009070out:
Qu Wenruo8702ba92019-10-14 14:34:51 +08009071 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE);
Qu Wenruobc42bda2017-02-27 15:10:39 +08009072 btrfs_delalloc_release_space(inode, data_reserved, page_start,
Qu Wenruo43b18592017-12-12 15:34:32 +08009073 reserved_space, (ret != 0));
Chris Mason9998eb72012-01-25 13:47:40 -05009074out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02009075 sb_end_pagefault(inode->i_sb);
Qu Wenruo364ecf32017-02-27 15:10:38 +08009076 extent_changeset_free(data_reserved);
Chris Mason9ebefb182007-06-15 13:50:00 -04009077 return ret;
9078}
9079
Filipe Manana213e8c52018-02-06 20:40:31 +00009080static int btrfs_truncate(struct inode *inode, bool skip_writeback)
Chris Mason39279cc2007-06-12 06:35:45 -04009081{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009082 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009083 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04009084 struct btrfs_block_rsv *rsv;
Omar Sandovalad7e1a72018-05-22 09:59:50 -07009085 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04009086 struct btrfs_trans_handle *trans;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009087 u64 mask = fs_info->sectorsize - 1;
Josef Bacik2bd36e72019-08-22 15:14:33 -04009088 u64 min_size = btrfs_calc_metadata_size(fs_info, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04009089
Filipe Manana213e8c52018-02-06 20:40:31 +00009090 if (!skip_writeback) {
9091 ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask),
9092 (u64)-1);
9093 if (ret)
9094 return ret;
9095 }
Chris Mason39279cc2007-06-12 06:35:45 -04009096
Josef Bacikfcb80c22011-05-03 10:40:22 -04009097 /*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07009098 * Yes ladies and gentlemen, this is indeed ugly. We have a couple of
9099 * things going on here:
Josef Bacikfcb80c22011-05-03 10:40:22 -04009100 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07009101 * 1) We need to reserve space to update our inode.
Josef Bacikfcb80c22011-05-03 10:40:22 -04009102 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07009103 * 2) We need to have something to cache all the space that is going to
Josef Bacikfcb80c22011-05-03 10:40:22 -04009104 * be free'd up by the truncate operation, but also have some slack
9105 * space reserved in case it uses space during the truncate (thank you
9106 * very much snapshotting).
9107 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07009108 * And we need these to be separate. The fact is we can use a lot of
Josef Bacikfcb80c22011-05-03 10:40:22 -04009109 * space doing the truncate, and we have no earthly idea how much space
Nicholas D Steeves01327612016-05-19 21:18:45 -04009110 * we will use, so we need the truncate reservation to be separate so it
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07009111 * doesn't end up using space reserved for updating the inode. We also
9112 * need to be able to stop the transaction and start a new one, which
9113 * means we need to be able to update the inode several times, and we
9114 * have no idea of knowing how many times that will be, so we can't just
9115 * reserve 1 item for the entirety of the operation, so that has to be
9116 * done separately as well.
Josef Bacikfcb80c22011-05-03 10:40:22 -04009117 *
9118 * So that leaves us with
9119 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07009120 * 1) rsv - for the truncate reservation, which we will steal from the
Josef Bacikfcb80c22011-05-03 10:40:22 -04009121 * transaction reservation.
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07009122 * 2) fs_info->trans_block_rsv - this will have 1 items worth left for
Josef Bacikfcb80c22011-05-03 10:40:22 -04009123 * updating the inode.
9124 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009125 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009126 if (!rsv)
9127 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04009128 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04009129 rsv->failfast = 1;
Josef Bacikf0cd8462011-03-04 14:37:08 -05009130
Josef Bacik907cbce2011-08-08 13:46:15 -04009131 /*
Josef Bacik07127182011-08-19 10:29:59 -04009132 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04009133 * 1 for updating the inode.
9134 */
Josef Bacikf3fe8202013-01-07 17:03:21 -05009135 trans = btrfs_start_transaction(root, 2);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009136 if (IS_ERR(trans)) {
Omar Sandovalad7e1a72018-05-22 09:59:50 -07009137 ret = PTR_ERR(trans);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009138 goto out;
9139 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05009140
Josef Bacik907cbce2011-08-08 13:46:15 -04009141 /* Migrate the slack space for the truncate to our reserve */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009142 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, rsv,
Lu Fengqi3a584172018-08-04 21:10:55 +08009143 min_size, false);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009144 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05009145
Chris Mason5a3f23d2009-03-31 13:27:11 -04009146 /*
Josef Bacik5dc562c2012-08-17 13:14:17 -04009147 * So if we truncate and then write and fsync we normally would just
9148 * write the extents that changed, which is a problem if we need to
9149 * first truncate that entire inode. So set this flag so we write out
9150 * all of the extents in the inode to the sync log so we're completely
9151 * safe.
9152 */
9153 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009154 trans->block_rsv = rsv;
Josef Bacik5dc562c2012-08-17 13:14:17 -04009155
Yan, Zheng80825102009-11-12 09:35:36 +00009156 while (1) {
9157 ret = btrfs_truncate_inode_items(trans, root, inode,
9158 inode->i_size,
9159 BTRFS_EXTENT_DATA_KEY);
Josef Bacikddfae632017-10-19 14:16:02 -04009160 trans->block_rsv = &fs_info->trans_block_rsv;
Omar Sandovalad7e1a72018-05-22 09:59:50 -07009161 if (ret != -ENOSPC && ret != -EAGAIN)
Yan, Zheng80825102009-11-12 09:35:36 +00009162 break;
Chris Mason39279cc2007-06-12 06:35:45 -04009163
Yan, Zheng80825102009-11-12 09:35:36 +00009164 ret = btrfs_update_inode(trans, root, inode);
Omar Sandovalad7e1a72018-05-22 09:59:50 -07009165 if (ret)
Josef Bacik3893e332011-01-31 16:03:11 -05009166 break;
Josef Bacikca7e70f2012-08-27 17:48:15 -04009167
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009168 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009169 btrfs_btree_balance_dirty(fs_info);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009170
9171 trans = btrfs_start_transaction(root, 2);
9172 if (IS_ERR(trans)) {
Omar Sandovalad7e1a72018-05-22 09:59:50 -07009173 ret = PTR_ERR(trans);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009174 trans = NULL;
9175 break;
9176 }
9177
Wang Xiaoguang47b5d642016-09-07 20:17:38 +08009178 btrfs_block_rsv_release(fs_info, rsv, -1);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009179 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv,
Lu Fengqi3a584172018-08-04 21:10:55 +08009180 rsv, min_size, false);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009181 BUG_ON(ret); /* shouldn't happen */
9182 trans->block_rsv = rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00009183 }
9184
Josef Bacikddfae632017-10-19 14:16:02 -04009185 /*
9186 * We can't call btrfs_truncate_block inside a trans handle as we could
9187 * deadlock with freeze, if we got NEED_TRUNCATE_BLOCK then we know
9188 * we've truncated everything except the last little bit, and can do
9189 * btrfs_truncate_block and then update the disk_i_size.
9190 */
9191 if (ret == NEED_TRUNCATE_BLOCK) {
9192 btrfs_end_transaction(trans);
9193 btrfs_btree_balance_dirty(fs_info);
9194
9195 ret = btrfs_truncate_block(inode, inode->i_size, 0, 0);
9196 if (ret)
9197 goto out;
9198 trans = btrfs_start_transaction(root, 1);
9199 if (IS_ERR(trans)) {
9200 ret = PTR_ERR(trans);
9201 goto out;
9202 }
9203 btrfs_ordered_update_i_size(inode, inode->i_size, NULL);
9204 }
9205
Chris Mason917c16b2011-11-08 14:49:59 -05009206 if (trans) {
Omar Sandovalad7e1a72018-05-22 09:59:50 -07009207 int ret2;
Josef Bacik7b128762008-07-24 12:17:14 -04009208
Omar Sandovalad7e1a72018-05-22 09:59:50 -07009209 trans->block_rsv = &fs_info->trans_block_rsv;
9210 ret2 = btrfs_update_inode(trans, root, inode);
9211 if (ret2 && !ret)
9212 ret = ret2;
9213
9214 ret2 = btrfs_end_transaction(trans);
9215 if (ret2 && !ret)
9216 ret = ret2;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009217 btrfs_btree_balance_dirty(fs_info);
Chris Mason917c16b2011-11-08 14:49:59 -05009218 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04009219out:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009220 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009221
Omar Sandovalad7e1a72018-05-22 09:59:50 -07009222 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04009223}
9224
Sven Wegener3b963622008-06-09 21:57:42 -04009225/*
Chris Masond352ac62008-09-29 15:18:18 -04009226 * create a new subvolume directory/inode (helper for the ioctl).
9227 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05009228int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009229 struct btrfs_root *new_root,
9230 struct btrfs_root *parent_root,
9231 u64 new_dirid)
Chris Mason39279cc2007-06-12 06:35:45 -04009232{
Chris Mason39279cc2007-06-12 06:35:45 -04009233 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04009234 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04009235 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009236
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01009237 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
9238 new_dirid, new_dirid,
9239 S_IFDIR | (~current_umask() & S_IRWXUGO),
9240 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04009241 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04009242 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009243 inode->i_op = &btrfs_dir_inode_operations;
9244 inode->i_fop = &btrfs_dir_file_operations;
9245
Miklos Szeredibfe86842011-10-28 14:13:29 +02009246 set_nlink(inode, 1);
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02009247 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Masonb0d5d102014-09-08 13:08:51 -07009248 unlock_new_inode(inode);
Sven Wegener3b963622008-06-09 21:57:42 -04009249
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009250 err = btrfs_subvol_inherit_props(trans, new_root, parent_root);
9251 if (err)
9252 btrfs_err(new_root->fs_info,
David Sterba351fd352014-05-15 16:48:20 +02009253 "error inheriting subvolume %llu properties: %d",
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009254 new_root->root_key.objectid, err);
9255
Yan, Zheng76dda932009-09-21 16:00:26 -04009256 err = btrfs_update_inode(trans, new_root, inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04009257
Yan, Zheng76dda932009-09-21 16:00:26 -04009258 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07009259 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04009260}
9261
Chris Mason39279cc2007-06-12 06:35:45 -04009262struct inode *btrfs_alloc_inode(struct super_block *sb)
9263{
Josef Bacik69fe2d72017-10-19 14:15:57 -04009264 struct btrfs_fs_info *fs_info = btrfs_sb(sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009265 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009266 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009267
David Sterba712e36c2017-10-31 17:08:27 +01009268 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_KERNEL);
Chris Mason39279cc2007-06-12 06:35:45 -04009269 if (!ei)
9270 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009271
9272 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009273 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04009274 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04009275 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04009276 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009277 ei->delalloc_bytes = 0;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01009278 ei->new_delalloc_bytes = 0;
Wang Shilong47059d92014-07-03 18:22:07 +08009279 ei->defrag_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009280 ei->disk_i_size = 0;
9281 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04009282 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009283 ei->index_cnt = (u64)-1;
Miao Xie67de1172013-12-26 13:07:06 +08009284 ei->dir_index = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009285 ei->last_unlink_trans = 0;
Liu Bo46d8bc32012-08-29 01:07:55 -06009286 ei->last_log_commit = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009287
Josef Bacik9e0baf62011-07-15 15:16:44 +00009288 spin_lock_init(&ei->lock);
9289 ei->outstanding_extents = 0;
Josef Bacik69fe2d72017-10-19 14:15:57 -04009290 if (sb->s_magic != BTRFS_TEST_MAGIC)
9291 btrfs_init_metadata_block_rsv(fs_info, &ei->block_rsv,
9292 BTRFS_BLOCK_RSV_DELALLOC);
Josef Bacik72ac3c02012-05-23 14:13:11 -04009293 ei->runtime_flags = 0;
David Sterbab52aa8c2017-07-17 19:17:20 +02009294 ei->prop_compress = BTRFS_COMPRESS_NONE;
David Sterbaeec63c62017-07-17 19:41:31 +02009295 ei->defrag_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009296
Miao Xie16cdcec2011-04-22 18:12:22 +08009297 ei->delayed_node = NULL;
9298
chandan r9cc97d62012-07-04 12:48:07 +05309299 ei->i_otime.tv_sec = 0;
9300 ei->i_otime.tv_nsec = 0;
9301
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009302 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02009303 extent_map_tree_init(&ei->extent_tree);
Qu Wenruo43eb5f22019-03-01 10:47:59 +08009304 extent_io_tree_init(fs_info, &ei->io_tree, IO_TREE_INODE_IO, inode);
9305 extent_io_tree_init(fs_info, &ei->io_failure_tree,
9306 IO_TREE_INODE_IO_FAILURE, inode);
David Sterba7b439732019-03-11 15:58:30 +01009307 ei->io_tree.track_uptodate = true;
9308 ei->io_failure_tree.track_uptodate = true;
Josef Bacikb812ce22012-11-16 13:56:32 -05009309 atomic_set(&ei->sync_writers, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009310 mutex_init(&ei->log_mutex);
Josef Bacikf2486792012-01-13 12:09:22 -05009311 mutex_init(&ei->delalloc_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009312 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009313 INIT_LIST_HEAD(&ei->delalloc_inodes);
David Sterba8089fe62015-11-19 14:15:51 +01009314 INIT_LIST_HEAD(&ei->delayed_iput);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009315 RB_CLEAR_NODE(&ei->rb_node);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01009316 init_rwsem(&ei->dio_sem);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009317
9318 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009319}
9320
Josef Bacikaaedb552013-10-11 14:44:09 -04009321#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
9322void btrfs_test_destroy_inode(struct inode *inode)
9323{
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02009324 btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0);
Josef Bacikaaedb552013-10-11 14:44:09 -04009325 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
9326}
9327#endif
9328
Al Viro26602ca2019-04-10 15:14:41 -04009329void btrfs_free_inode(struct inode *inode)
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009330{
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009331 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
9332}
9333
Chris Mason39279cc2007-06-12 06:35:45 -04009334void btrfs_destroy_inode(struct inode *inode)
9335{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009336 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009337 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009338 struct btrfs_root *root = BTRFS_I(inode)->root;
9339
Al Virob3d9b7a2012-06-09 13:51:19 -04009340 WARN_ON(!hlist_empty(&inode->i_dentry));
Chris Mason39279cc2007-06-12 06:35:45 -04009341 WARN_ON(inode->i_data.nrpages);
Josef Bacik69fe2d72017-10-19 14:15:57 -04009342 WARN_ON(BTRFS_I(inode)->block_rsv.reserved);
9343 WARN_ON(BTRFS_I(inode)->block_rsv.size);
Josef Bacik9e0baf62011-07-15 15:16:44 +00009344 WARN_ON(BTRFS_I(inode)->outstanding_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04009345 WARN_ON(BTRFS_I(inode)->delalloc_bytes);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01009346 WARN_ON(BTRFS_I(inode)->new_delalloc_bytes);
Josef Bacik7709cde2011-08-04 10:25:02 -04009347 WARN_ON(BTRFS_I(inode)->csum_bytes);
Wang Shilong47059d92014-07-03 18:22:07 +08009348 WARN_ON(BTRFS_I(inode)->defrag_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04009349
Chris Mason5a3f23d2009-03-31 13:27:11 -04009350 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05009351 * This can happen where we create an inode, but somebody else also
9352 * created the same inode and we need to destroy the one we already
9353 * created.
9354 */
9355 if (!root)
Al Viro26602ca2019-04-10 15:14:41 -04009356 return;
Josef Bacika6dbd422009-11-11 15:53:34 -05009357
Chris Masond3977122009-01-05 21:25:51 -05009358 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04009359 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
9360 if (!ordered)
9361 break;
9362 else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009363 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04009364 "found ordered extent %llu %llu on inode cleanup",
9365 ordered->file_offset, ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009366 btrfs_remove_ordered_extent(inode, ordered);
9367 btrfs_put_ordered_extent(ordered);
9368 btrfs_put_ordered_extent(ordered);
9369 }
9370 }
Qu Wenruo56fa9d02015-10-13 09:53:10 +08009371 btrfs_qgroup_check_reserved_leak(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04009372 inode_tree_del(inode);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02009373 btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04009374}
9375
Al Viro45321ac2010-06-07 13:43:19 -04009376int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04009377{
9378 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04009379
Naohiro Aota6379ef92013-06-06 09:56:34 +00009380 if (root == NULL)
9381 return 1;
9382
Liu Bofa6ac872013-02-20 14:10:23 +00009383 /* the snap/subvol tree is on deleting */
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02009384 if (btrfs_root_refs(&root->root_item) == 0)
Al Viro45321ac2010-06-07 13:43:19 -04009385 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04009386 else
Al Viro45321ac2010-06-07 13:43:19 -04009387 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04009388}
9389
Sven Wegener0ee0fda2008-07-30 16:54:26 -04009390static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04009391{
9392 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
9393
9394 inode_init_once(&ei->vfs_inode);
9395}
9396
David Sterbae67c7182018-02-19 17:24:18 +01009397void __cold btrfs_destroy_cachep(void)
Chris Mason39279cc2007-06-12 06:35:45 -04009398{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10009399 /*
9400 * Make sure all delayed rcu free inodes are flushed before we
9401 * destroy cache.
9402 */
9403 rcu_barrier();
Kinglong Mee5598e902016-01-29 21:36:35 +08009404 kmem_cache_destroy(btrfs_inode_cachep);
9405 kmem_cache_destroy(btrfs_trans_handle_cachep);
Kinglong Mee5598e902016-01-29 21:36:35 +08009406 kmem_cache_destroy(btrfs_path_cachep);
9407 kmem_cache_destroy(btrfs_free_space_cachep);
Christophe Leroy3acd4852019-08-21 15:05:55 +00009408 kmem_cache_destroy(btrfs_free_space_bitmap_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04009409}
9410
Liu Bof5c29bd2017-11-02 17:21:50 -06009411int __init btrfs_init_cachep(void)
Chris Mason39279cc2007-06-12 06:35:45 -04009412{
David Sterba837e1972012-09-07 03:00:48 -06009413 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009414 sizeof(struct btrfs_inode), 0,
Vladimir Davydov5d097052016-01-14 15:18:21 -08009415 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD | SLAB_ACCOUNT,
9416 init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04009417 if (!btrfs_inode_cachep)
9418 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009419
David Sterba837e1972012-09-07 03:00:48 -06009420 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009421 sizeof(struct btrfs_trans_handle), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009422 SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009423 if (!btrfs_trans_handle_cachep)
9424 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009425
David Sterba837e1972012-09-07 03:00:48 -06009426 btrfs_path_cachep = kmem_cache_create("btrfs_path",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009427 sizeof(struct btrfs_path), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009428 SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009429 if (!btrfs_path_cachep)
9430 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009431
David Sterba837e1972012-09-07 03:00:48 -06009432 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
Josef Bacikdc89e982011-01-28 17:05:48 -05009433 sizeof(struct btrfs_free_space), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009434 SLAB_MEM_SPREAD, NULL);
Josef Bacikdc89e982011-01-28 17:05:48 -05009435 if (!btrfs_free_space_cachep)
9436 goto fail;
9437
Christophe Leroy3acd4852019-08-21 15:05:55 +00009438 btrfs_free_space_bitmap_cachep = kmem_cache_create("btrfs_free_space_bitmap",
9439 PAGE_SIZE, PAGE_SIZE,
9440 SLAB_RED_ZONE, NULL);
9441 if (!btrfs_free_space_bitmap_cachep)
9442 goto fail;
9443
Chris Mason39279cc2007-06-12 06:35:45 -04009444 return 0;
9445fail:
9446 btrfs_destroy_cachep();
9447 return -ENOMEM;
9448}
9449
David Howellsa528d352017-01-31 16:46:22 +00009450static int btrfs_getattr(const struct path *path, struct kstat *stat,
9451 u32 request_mask, unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04009452{
Miao Xiedf0af1a2013-01-29 10:11:59 +00009453 u64 delalloc_bytes;
David Howellsa528d352017-01-31 16:46:22 +00009454 struct inode *inode = d_inode(path->dentry);
David Sterbafadc0d82011-11-20 07:33:38 -05009455 u32 blocksize = inode->i_sb->s_blocksize;
Yonghong Song04a87e32017-05-12 15:07:43 -07009456 u32 bi_flags = BTRFS_I(inode)->flags;
9457
9458 stat->result_mask |= STATX_BTIME;
9459 stat->btime.tv_sec = BTRFS_I(inode)->i_otime.tv_sec;
9460 stat->btime.tv_nsec = BTRFS_I(inode)->i_otime.tv_nsec;
9461 if (bi_flags & BTRFS_INODE_APPEND)
9462 stat->attributes |= STATX_ATTR_APPEND;
9463 if (bi_flags & BTRFS_INODE_COMPRESS)
9464 stat->attributes |= STATX_ATTR_COMPRESSED;
9465 if (bi_flags & BTRFS_INODE_IMMUTABLE)
9466 stat->attributes |= STATX_ATTR_IMMUTABLE;
9467 if (bi_flags & BTRFS_INODE_NODUMP)
9468 stat->attributes |= STATX_ATTR_NODUMP;
9469
9470 stat->attributes_mask |= (STATX_ATTR_APPEND |
9471 STATX_ATTR_COMPRESSED |
9472 STATX_ATTR_IMMUTABLE |
9473 STATX_ATTR_NODUMP);
David Sterbafadc0d82011-11-20 07:33:38 -05009474
Chris Mason39279cc2007-06-12 06:35:45 -04009475 generic_fillattr(inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04009476 stat->dev = BTRFS_I(inode)->root->anon_dev;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009477
9478 spin_lock(&BTRFS_I(inode)->lock);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01009479 delalloc_bytes = BTRFS_I(inode)->new_delalloc_bytes;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009480 spin_unlock(&BTRFS_I(inode)->lock);
David Sterbafadc0d82011-11-20 07:33:38 -05009481 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
Miao Xiedf0af1a2013-01-29 10:11:59 +00009482 ALIGN(delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04009483 return 0;
9484}
9485
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009486static int btrfs_rename_exchange(struct inode *old_dir,
9487 struct dentry *old_dentry,
9488 struct inode *new_dir,
9489 struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04009490{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009491 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009492 struct btrfs_trans_handle *trans;
9493 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009494 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009495 struct inode *new_inode = new_dentry->d_inode;
9496 struct inode *old_inode = old_dentry->d_inode;
Deepa Dinamani95582b02018-05-08 19:36:02 -07009497 struct timespec64 ctime = current_time(old_inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009498 struct dentry *parent;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009499 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
9500 u64 new_ino = btrfs_ino(BTRFS_I(new_inode));
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009501 u64 old_idx = 0;
9502 u64 new_idx = 0;
9503 u64 root_objectid;
9504 int ret;
Filipe Manana86e8aa02016-05-05 02:02:27 +01009505 bool root_log_pinned = false;
9506 bool dest_log_pinned = false;
Filipe Mananad4682ba2018-06-11 19:24:28 +01009507 struct btrfs_log_ctx ctx_root;
9508 struct btrfs_log_ctx ctx_dest;
9509 bool sync_log_root = false;
9510 bool sync_log_dest = false;
9511 bool commit_transaction = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009512
9513 /* we only allow rename subvolume link between subvolumes */
9514 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
9515 return -EXDEV;
9516
Filipe Mananad4682ba2018-06-11 19:24:28 +01009517 btrfs_init_log_ctx(&ctx_root, old_inode);
9518 btrfs_init_log_ctx(&ctx_dest, new_inode);
9519
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009520 /* close the race window with snapshot create/destroy ioctl */
9521 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009522 down_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009523 if (new_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009524 down_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009525
9526 /*
9527 * We want to reserve the absolute worst case amount of items. So if
9528 * both inodes are subvols and we need to unlink them then that would
9529 * require 4 item modifications, but if they are both normal inodes it
9530 * would require 5 item modifications, so we'll assume their normal
9531 * inodes. So 5 * 2 is 10, plus 2 for the new links, so 12 total items
9532 * should cover the worst case number of items we'll modify.
9533 */
9534 trans = btrfs_start_transaction(root, 12);
9535 if (IS_ERR(trans)) {
9536 ret = PTR_ERR(trans);
9537 goto out_notrans;
9538 }
9539
9540 /*
9541 * We need to find a free sequence number both in the source and
9542 * in the destination directory for the exchange.
9543 */
Nikolay Borisov877574e2017-02-20 13:50:33 +02009544 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009545 if (ret)
9546 goto out_fail;
Nikolay Borisov877574e2017-02-20 13:50:33 +02009547 ret = btrfs_set_inode_index(BTRFS_I(old_dir), &new_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009548 if (ret)
9549 goto out_fail;
9550
9551 BTRFS_I(old_inode)->dir_index = 0ULL;
9552 BTRFS_I(new_inode)->dir_index = 0ULL;
9553
9554 /* Reference for the source. */
9555 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
9556 /* force full log commit if subvolume involved. */
David Sterba90787762019-03-20 13:28:05 +01009557 btrfs_set_log_full_commit(trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009558 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01009559 btrfs_pin_log_trans(root);
9560 root_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009561 ret = btrfs_insert_inode_ref(trans, dest,
9562 new_dentry->d_name.name,
9563 new_dentry->d_name.len,
9564 old_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01009565 btrfs_ino(BTRFS_I(new_dir)),
9566 old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009567 if (ret)
9568 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009569 }
9570
9571 /* And now for the dest. */
9572 if (new_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(dest);
9577 dest_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009578 ret = btrfs_insert_inode_ref(trans, root,
9579 old_dentry->d_name.name,
9580 old_dentry->d_name.len,
9581 new_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01009582 btrfs_ino(BTRFS_I(old_dir)),
9583 new_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 /* Update inode version and ctime/mtime. */
9589 inode_inc_iversion(old_dir);
9590 inode_inc_iversion(new_dir);
9591 inode_inc_iversion(old_inode);
9592 inode_inc_iversion(new_inode);
9593 old_dir->i_ctime = old_dir->i_mtime = ctime;
9594 new_dir->i_ctime = new_dir->i_mtime = ctime;
9595 old_inode->i_ctime = ctime;
9596 new_inode->i_ctime = ctime;
9597
9598 if (old_dentry->d_parent != new_dentry->d_parent) {
David Sterbaf85b7372017-01-20 14:54:07 +01009599 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
9600 BTRFS_I(old_inode), 1);
9601 btrfs_record_unlink_dir(trans, BTRFS_I(new_dir),
9602 BTRFS_I(new_inode), 1);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009603 }
9604
9605 /* src is a subvolume */
9606 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
9607 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
Lu Fengqi401b3b12018-08-01 11:32:30 +08009608 ret = btrfs_unlink_subvol(trans, old_dir, root_objectid,
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009609 old_dentry->d_name.name,
9610 old_dentry->d_name.len);
9611 } else { /* src is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009612 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
9613 BTRFS_I(old_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009614 old_dentry->d_name.name,
9615 old_dentry->d_name.len);
9616 if (!ret)
9617 ret = btrfs_update_inode(trans, root, old_inode);
9618 }
9619 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009620 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009621 goto out_fail;
9622 }
9623
9624 /* dest is a subvolume */
9625 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
9626 root_objectid = BTRFS_I(new_inode)->root->root_key.objectid;
Lu Fengqi401b3b12018-08-01 11:32:30 +08009627 ret = btrfs_unlink_subvol(trans, new_dir, root_objectid,
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009628 new_dentry->d_name.name,
9629 new_dentry->d_name.len);
9630 } else { /* dest is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009631 ret = __btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
9632 BTRFS_I(new_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009633 new_dentry->d_name.name,
9634 new_dentry->d_name.len);
9635 if (!ret)
9636 ret = btrfs_update_inode(trans, dest, new_inode);
9637 }
9638 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009639 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009640 goto out_fail;
9641 }
9642
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009643 ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009644 new_dentry->d_name.name,
9645 new_dentry->d_name.len, 0, old_idx);
9646 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009647 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009648 goto out_fail;
9649 }
9650
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009651 ret = btrfs_add_link(trans, BTRFS_I(old_dir), BTRFS_I(new_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009652 old_dentry->d_name.name,
9653 old_dentry->d_name.len, 0, new_idx);
9654 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009655 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009656 goto out_fail;
9657 }
9658
9659 if (old_inode->i_nlink == 1)
9660 BTRFS_I(old_inode)->dir_index = old_idx;
9661 if (new_inode->i_nlink == 1)
9662 BTRFS_I(new_inode)->dir_index = new_idx;
9663
Filipe Manana86e8aa02016-05-05 02:02:27 +01009664 if (root_log_pinned) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009665 parent = new_dentry->d_parent;
Filipe Mananad4682ba2018-06-11 19:24:28 +01009666 ret = btrfs_log_new_name(trans, BTRFS_I(old_inode),
9667 BTRFS_I(old_dir), parent,
9668 false, &ctx_root);
9669 if (ret == BTRFS_NEED_LOG_SYNC)
9670 sync_log_root = true;
9671 else if (ret == BTRFS_NEED_TRANS_COMMIT)
9672 commit_transaction = true;
9673 ret = 0;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009674 btrfs_end_log_trans(root);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009675 root_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009676 }
Filipe Manana86e8aa02016-05-05 02:02:27 +01009677 if (dest_log_pinned) {
Filipe Mananad4682ba2018-06-11 19:24:28 +01009678 if (!commit_transaction) {
9679 parent = old_dentry->d_parent;
9680 ret = btrfs_log_new_name(trans, BTRFS_I(new_inode),
9681 BTRFS_I(new_dir), parent,
9682 false, &ctx_dest);
9683 if (ret == BTRFS_NEED_LOG_SYNC)
9684 sync_log_dest = true;
9685 else if (ret == BTRFS_NEED_TRANS_COMMIT)
9686 commit_transaction = true;
9687 ret = 0;
9688 }
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009689 btrfs_end_log_trans(dest);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009690 dest_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009691 }
9692out_fail:
Filipe Manana86e8aa02016-05-05 02:02:27 +01009693 /*
9694 * If we have pinned a log and an error happened, we unpin tasks
9695 * trying to sync the log and force them to fallback to a transaction
9696 * commit if the log currently contains any of the inodes involved in
9697 * this rename operation (to ensure we do not persist a log with an
9698 * inconsistent state for any of these inodes or leading to any
9699 * inconsistencies when replayed). If the transaction was aborted, the
9700 * abortion reason is propagated to userspace when attempting to commit
9701 * the transaction. If the log does not contain any of these inodes, we
9702 * allow the tasks to sync it.
9703 */
9704 if (ret && (root_log_pinned || dest_log_pinned)) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009705 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
9706 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
9707 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana86e8aa02016-05-05 02:02:27 +01009708 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009709 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
David Sterba90787762019-03-20 13:28:05 +01009710 btrfs_set_log_full_commit(trans);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009711
9712 if (root_log_pinned) {
9713 btrfs_end_log_trans(root);
9714 root_log_pinned = false;
9715 }
9716 if (dest_log_pinned) {
9717 btrfs_end_log_trans(dest);
9718 dest_log_pinned = false;
9719 }
9720 }
Filipe Mananad4682ba2018-06-11 19:24:28 +01009721 if (!ret && sync_log_root && !commit_transaction) {
9722 ret = btrfs_sync_log(trans, BTRFS_I(old_inode)->root,
9723 &ctx_root);
9724 if (ret)
9725 commit_transaction = true;
9726 }
9727 if (!ret && sync_log_dest && !commit_transaction) {
9728 ret = btrfs_sync_log(trans, BTRFS_I(new_inode)->root,
9729 &ctx_dest);
9730 if (ret)
9731 commit_transaction = true;
9732 }
9733 if (commit_transaction) {
Filipe Mananae6c61712019-11-08 16:11:56 +00009734 /*
9735 * We may have set commit_transaction when logging the new name
9736 * in the destination root, in which case we left the source
9737 * root context in the list of log contextes. So make sure we
9738 * remove it to avoid invalid memory accesses, since the context
9739 * was allocated in our stack frame.
9740 */
9741 if (sync_log_root) {
9742 mutex_lock(&root->log_mutex);
9743 list_del_init(&ctx_root.list);
9744 mutex_unlock(&root->log_mutex);
9745 }
Filipe Mananad4682ba2018-06-11 19:24:28 +01009746 ret = btrfs_commit_transaction(trans);
9747 } else {
9748 int ret2;
9749
9750 ret2 = btrfs_end_transaction(trans);
9751 ret = ret ? ret : ret2;
9752 }
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009753out_notrans:
9754 if (new_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009755 up_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009756 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009757 up_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009758
Filipe Mananae6c61712019-11-08 16:11:56 +00009759 ASSERT(list_empty(&ctx_root.list));
9760 ASSERT(list_empty(&ctx_dest.list));
9761
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009762 return ret;
9763}
9764
9765static int btrfs_whiteout_for_rename(struct btrfs_trans_handle *trans,
9766 struct btrfs_root *root,
9767 struct inode *dir,
9768 struct dentry *dentry)
9769{
9770 int ret;
9771 struct inode *inode;
9772 u64 objectid;
9773 u64 index;
9774
9775 ret = btrfs_find_free_ino(root, &objectid);
9776 if (ret)
9777 return ret;
9778
9779 inode = btrfs_new_inode(trans, root, dir,
9780 dentry->d_name.name,
9781 dentry->d_name.len,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009782 btrfs_ino(BTRFS_I(dir)),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009783 objectid,
9784 S_IFCHR | WHITEOUT_MODE,
9785 &index);
9786
9787 if (IS_ERR(inode)) {
9788 ret = PTR_ERR(inode);
9789 return ret;
9790 }
9791
9792 inode->i_op = &btrfs_special_inode_operations;
9793 init_special_inode(inode, inode->i_mode,
9794 WHITEOUT_DEV);
9795
9796 ret = btrfs_init_inode_security(trans, inode, dir,
9797 &dentry->d_name);
9798 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009799 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009800
Nikolay Borisovcef415a2017-02-20 13:51:09 +02009801 ret = btrfs_add_nondir(trans, BTRFS_I(dir), dentry,
9802 BTRFS_I(inode), 0, index);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009803 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009804 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009805
9806 ret = btrfs_update_inode(trans, root, inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009807out:
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009808 unlock_new_inode(inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009809 if (ret)
9810 inode_dec_link_count(inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009811 iput(inode);
9812
Filipe Mananac9901612016-05-05 01:41:57 +01009813 return ret;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009814}
9815
Chris Mason39279cc2007-06-12 06:35:45 -04009816static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009817 struct inode *new_dir, struct dentry *new_dentry,
9818 unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04009819{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009820 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009821 struct btrfs_trans_handle *trans;
Filipe Manana5062af32016-05-05 10:26:26 +01009822 unsigned int trans_num_items;
Chris Mason39279cc2007-06-12 06:35:45 -04009823 struct btrfs_root *root = BTRFS_I(old_dir)->root;
9824 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00009825 struct inode *new_inode = d_inode(new_dentry);
9826 struct inode *old_inode = d_inode(old_dentry);
Chris Mason00e4e6b2008-08-05 11:18:09 -04009827 u64 index = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009828 u64 root_objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04009829 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009830 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009831 bool log_pinned = false;
Filipe Mananad4682ba2018-06-11 19:24:28 +01009832 struct btrfs_log_ctx ctx;
9833 bool sync_log = false;
9834 bool commit_transaction = false;
Chris Mason39279cc2007-06-12 06:35:45 -04009835
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009836 if (btrfs_ino(BTRFS_I(new_dir)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04009837 return -EPERM;
9838
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009839 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08009840 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05009841 return -EXDEV;
9842
Li Zefan33345d012011-04-20 10:31:50 +08009843 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009844 (new_inode && btrfs_ino(BTRFS_I(new_inode)) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009845 return -ENOTEMPTY;
9846
Chris Mason39279cc2007-06-12 06:35:45 -04009847 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009848 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04009849 return -ENOTEMPTY;
Chris Mason9c520572012-12-17 14:26:57 -05009850
9851
9852 /* check for collisions, even if the name isn't there */
Josef Bacik4871c152013-10-09 12:24:04 -04009853 ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino,
Chris Mason9c520572012-12-17 14:26:57 -05009854 new_dentry->d_name.name,
9855 new_dentry->d_name.len);
9856
9857 if (ret) {
9858 if (ret == -EEXIST) {
9859 /* we shouldn't get
9860 * eexist without a new_inode */
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05309861 if (WARN_ON(!new_inode)) {
Chris Mason9c520572012-12-17 14:26:57 -05009862 return ret;
9863 }
9864 } else {
9865 /* maybe -EOVERFLOW */
9866 return ret;
9867 }
9868 }
9869 ret = 0;
9870
Chris Mason5a3f23d2009-03-31 13:27:11 -04009871 /*
Chris Mason8d875f92014-08-12 10:47:42 -07009872 * we're using rename to replace one file with another. Start IO on it
9873 * now so we don't add too much work to the end of the transaction
Chris Mason5a3f23d2009-03-31 13:27:11 -04009874 */
Chris Mason8d875f92014-08-12 10:47:42 -07009875 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size)
Chris Mason5a3f23d2009-03-31 13:27:11 -04009876 filemap_flush(old_inode->i_mapping);
9877
Yan, Zheng76dda932009-09-21 16:00:26 -04009878 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08009879 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009880 down_read(&fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04009881 /*
9882 * We want to reserve the absolute worst case amount of items. So if
9883 * both inodes are subvols and we need to unlink them then that would
9884 * require 4 item modifications, but if they are both normal inodes it
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009885 * would require 5 item modifications, so we'll assume they are normal
Yan, Zhenga22285a2010-05-16 10:48:46 -04009886 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
9887 * should cover the worst case number of items we'll modify.
Filipe Manana5062af32016-05-05 10:26:26 +01009888 * If our rename has the whiteout flag, we need more 5 units for the
9889 * new inode (1 inode item, 1 inode ref, 2 dir items and 1 xattr item
9890 * when selinux is enabled).
Yan, Zhenga22285a2010-05-16 10:48:46 -04009891 */
Filipe Manana5062af32016-05-05 10:26:26 +01009892 trans_num_items = 11;
9893 if (flags & RENAME_WHITEOUT)
9894 trans_num_items += 5;
9895 trans = btrfs_start_transaction(root, trans_num_items);
Johann Lombardib44c59a2011-03-31 13:23:47 +00009896 if (IS_ERR(trans)) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009897 ret = PTR_ERR(trans);
9898 goto out_notrans;
9899 }
Chris Mason5f39d392007-10-15 16:14:19 -04009900
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009901 if (dest != root)
9902 btrfs_record_root_in_trans(trans, dest);
9903
Nikolay Borisov877574e2017-02-20 13:50:33 +02009904 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009905 if (ret)
9906 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009907
Miao Xie67de1172013-12-26 13:07:06 +08009908 BTRFS_I(old_inode)->dir_index = 0ULL;
Li Zefan33345d012011-04-20 10:31:50 +08009909 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009910 /* force full log commit if subvolume involved. */
David Sterba90787762019-03-20 13:28:05 +01009911 btrfs_set_log_full_commit(trans);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009912 } else {
Filipe Mananac4aba952016-04-29 13:14:42 +01009913 btrfs_pin_log_trans(root);
9914 log_pinned = true;
Yan, Zhenga5719522009-09-24 09:17:31 -04009915 ret = btrfs_insert_inode_ref(trans, dest,
9916 new_dentry->d_name.name,
9917 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08009918 old_ino,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009919 btrfs_ino(BTRFS_I(new_dir)), index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009920 if (ret)
9921 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009922 }
Chris Mason5a3f23d2009-03-31 13:27:11 -04009923
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009924 inode_inc_iversion(old_dir);
9925 inode_inc_iversion(new_dir);
9926 inode_inc_iversion(old_inode);
Deepa Dinamani04b285f2016-02-06 23:57:21 -08009927 old_dir->i_ctime = old_dir->i_mtime =
9928 new_dir->i_ctime = new_dir->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009929 old_inode->i_ctime = current_time(old_dir);
Chris Mason5f39d392007-10-15 16:14:19 -04009930
Chris Mason12fcfd22009-03-24 10:24:20 -04009931 if (old_dentry->d_parent != new_dentry->d_parent)
David Sterbaf85b7372017-01-20 14:54:07 +01009932 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
9933 BTRFS_I(old_inode), 1);
Chris Mason12fcfd22009-03-24 10:24:20 -04009934
Li Zefan33345d012011-04-20 10:31:50 +08009935 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009936 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
Lu Fengqi401b3b12018-08-01 11:32:30 +08009937 ret = btrfs_unlink_subvol(trans, old_dir, root_objectid,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009938 old_dentry->d_name.name,
9939 old_dentry->d_name.len);
9940 } else {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009941 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
9942 BTRFS_I(d_inode(old_dentry)),
Al Viro92986792011-03-04 17:14:37 +00009943 old_dentry->d_name.name,
9944 old_dentry->d_name.len);
9945 if (!ret)
9946 ret = btrfs_update_inode(trans, root, old_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009947 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009948 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009949 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009950 goto out_fail;
9951 }
Chris Mason39279cc2007-06-12 06:35:45 -04009952
9953 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009954 inode_inc_iversion(new_inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009955 new_inode->i_ctime = current_time(new_inode);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009956 if (unlikely(btrfs_ino(BTRFS_I(new_inode)) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009957 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
9958 root_objectid = BTRFS_I(new_inode)->location.objectid;
Lu Fengqi401b3b12018-08-01 11:32:30 +08009959 ret = btrfs_unlink_subvol(trans, new_dir, root_objectid,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009960 new_dentry->d_name.name,
9961 new_dentry->d_name.len);
9962 BUG_ON(new_inode->i_nlink == 0);
9963 } else {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009964 ret = btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
9965 BTRFS_I(d_inode(new_dentry)),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009966 new_dentry->d_name.name,
9967 new_dentry->d_name.len);
9968 }
Josef Bacik4ef31a42013-08-13 14:10:08 -04009969 if (!ret && new_inode->i_nlink == 0)
Nikolay Borisov73f2e542017-02-20 13:50:59 +02009970 ret = btrfs_orphan_add(trans,
9971 BTRFS_I(d_inode(new_dentry)));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009972 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009973 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009974 goto out_fail;
9975 }
Chris Mason39279cc2007-06-12 06:35:45 -04009976 }
Josef Bacikaec74772008-07-24 12:12:38 -04009977
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009978 ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009979 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04009980 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009981 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009982 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009983 goto out_fail;
9984 }
Chris Mason39279cc2007-06-12 06:35:45 -04009985
Miao Xie67de1172013-12-26 13:07:06 +08009986 if (old_inode->i_nlink == 1)
9987 BTRFS_I(old_inode)->dir_index = index;
9988
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009989 if (log_pinned) {
Al Viro10d9f302011-07-16 23:09:10 -04009990 struct dentry *parent = new_dentry->d_parent;
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009991
Filipe Mananad4682ba2018-06-11 19:24:28 +01009992 btrfs_init_log_ctx(&ctx, old_inode);
9993 ret = btrfs_log_new_name(trans, BTRFS_I(old_inode),
9994 BTRFS_I(old_dir), parent,
9995 false, &ctx);
9996 if (ret == BTRFS_NEED_LOG_SYNC)
9997 sync_log = true;
9998 else if (ret == BTRFS_NEED_TRANS_COMMIT)
9999 commit_transaction = true;
10000 ret = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010001 btrfs_end_log_trans(root);
Filipe Manana3dc9e8f72016-04-29 11:34:22 +010010002 log_pinned = false;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010003 }
Dan Fuhrycdd1fed2016-03-17 15:23:38 +010010004
10005 if (flags & RENAME_WHITEOUT) {
10006 ret = btrfs_whiteout_for_rename(trans, root, old_dir,
10007 old_dentry);
10008
10009 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -040010010 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +010010011 goto out_fail;
10012 }
Chris Mason12fcfd22009-03-24 10:24:20 -040010013 }
Chris Mason39279cc2007-06-12 06:35:45 -040010014out_fail:
Filipe Manana3dc9e8f72016-04-29 11:34:22 +010010015 /*
10016 * If we have pinned the log and an error happened, we unpin tasks
10017 * trying to sync the log and force them to fallback to a transaction
10018 * commit if the log currently contains any of the inodes involved in
10019 * this rename operation (to ensure we do not persist a log with an
10020 * inconsistent state for any of these inodes or leading to any
10021 * inconsistencies when replayed). If the transaction was aborted, the
10022 * abortion reason is propagated to userspace when attempting to commit
10023 * the transaction. If the log does not contain any of these inodes, we
10024 * allow the tasks to sync it.
10025 */
10026 if (ret && log_pinned) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +020010027 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
10028 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
10029 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana3dc9e8f72016-04-29 11:34:22 +010010030 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +020010031 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
David Sterba90787762019-03-20 13:28:05 +010010032 btrfs_set_log_full_commit(trans);
Filipe Manana3dc9e8f72016-04-29 11:34:22 +010010033
10034 btrfs_end_log_trans(root);
10035 log_pinned = false;
10036 }
Filipe Mananad4682ba2018-06-11 19:24:28 +010010037 if (!ret && sync_log) {
10038 ret = btrfs_sync_log(trans, BTRFS_I(old_inode)->root, &ctx);
10039 if (ret)
10040 commit_transaction = true;
10041 }
10042 if (commit_transaction) {
10043 ret = btrfs_commit_transaction(trans);
10044 } else {
10045 int ret2;
10046
10047 ret2 = btrfs_end_transaction(trans);
10048 ret = ret ? ret : ret2;
10049 }
Johann Lombardib44c59a2011-03-31 13:23:47 +000010050out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +080010051 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010052 up_read(&fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -040010053
Chris Mason39279cc2007-06-12 06:35:45 -040010054 return ret;
10055}
10056
Miklos Szeredi80ace852014-07-23 15:15:32 +020010057static int btrfs_rename2(struct inode *old_dir, struct dentry *old_dentry,
10058 struct inode *new_dir, struct dentry *new_dentry,
10059 unsigned int flags)
10060{
Dan Fuhrycdd1fed2016-03-17 15:23:38 +010010061 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
Miklos Szeredi80ace852014-07-23 15:15:32 +020010062 return -EINVAL;
10063
Dan Fuhrycdd1fed2016-03-17 15:23:38 +010010064 if (flags & RENAME_EXCHANGE)
10065 return btrfs_rename_exchange(old_dir, old_dentry, new_dir,
10066 new_dentry);
10067
10068 return btrfs_rename(old_dir, old_dentry, new_dir, new_dentry, flags);
Miklos Szeredi80ace852014-07-23 15:15:32 +020010069}
10070
Nikolay Borisov3a2f8c02018-04-24 17:23:59 +030010071struct btrfs_delalloc_work {
10072 struct inode *inode;
10073 struct completion completion;
10074 struct list_head list;
10075 struct btrfs_work work;
10076};
10077
Miao Xie8ccf6f192012-10-25 09:28:04 +000010078static void btrfs_run_delalloc_work(struct btrfs_work *work)
10079{
10080 struct btrfs_delalloc_work *delalloc_work;
Josef Bacik9f23e282013-10-28 15:03:41 -040010081 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010082
10083 delalloc_work = container_of(work, struct btrfs_delalloc_work,
10084 work);
Josef Bacik9f23e282013-10-28 15:03:41 -040010085 inode = delalloc_work->inode;
David Sterba30424602015-11-27 19:27:11 +010010086 filemap_flush(inode->i_mapping);
10087 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
10088 &BTRFS_I(inode)->runtime_flags))
Josef Bacik9f23e282013-10-28 15:03:41 -040010089 filemap_flush(inode->i_mapping);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010090
Nikolay Borisov076da912018-04-23 10:54:16 +030010091 iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010092 complete(&delalloc_work->completion);
10093}
10094
Nikolay Borisov3a2f8c02018-04-24 17:23:59 +030010095static struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode)
Miao Xie8ccf6f192012-10-25 09:28:04 +000010096{
10097 struct btrfs_delalloc_work *work;
10098
David Sterba100d5702015-12-08 14:39:32 +010010099 work = kmalloc(sizeof(*work), GFP_NOFS);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010100 if (!work)
10101 return NULL;
10102
10103 init_completion(&work->completion);
10104 INIT_LIST_HEAD(&work->list);
10105 work->inode = inode;
Omar Sandovala0cac0e2019-09-16 11:30:57 -070010106 btrfs_init_work(&work->work, btrfs_run_delalloc_work, NULL, NULL);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010107
10108 return work;
10109}
10110
Chris Masond352ac62008-09-29 15:18:18 -040010111/*
10112 * some fairly slow code that needs optimization. This walks the list
10113 * of all the inodes with pending delalloc and forces them to disk.
10114 */
Ethan Lien3cd24c62018-11-01 14:49:03 +080010115static int start_delalloc_inodes(struct btrfs_root *root, int nr, bool snapshot)
Chris Masonea8c2812008-08-04 23:17:27 -040010116{
Chris Masonea8c2812008-08-04 23:17:27 -040010117 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010118 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010119 struct btrfs_delalloc_work *work, *next;
10120 struct list_head works;
Miao Xie1eafa6c2013-01-22 10:49:00 +000010121 struct list_head splice;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010122 int ret = 0;
Chris Masonea8c2812008-08-04 23:17:27 -040010123
Miao Xie8ccf6f192012-10-25 09:28:04 +000010124 INIT_LIST_HEAD(&works);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010125 INIT_LIST_HEAD(&splice);
Miao Xie63607cc2013-01-22 10:50:35 +000010126
Miao Xie573bfb72014-03-06 13:55:03 +080010127 mutex_lock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010128 spin_lock(&root->delalloc_lock);
10129 list_splice_init(&root->delalloc_inodes, &splice);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010130 while (!list_empty(&splice)) {
10131 binode = list_entry(splice.next, struct btrfs_inode,
Chris Masonea8c2812008-08-04 23:17:27 -040010132 delalloc_inodes);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010133
Miao Xieeb73c1b2013-05-15 07:48:22 +000010134 list_move_tail(&binode->delalloc_inodes,
10135 &root->delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010136 inode = igrab(&binode->vfs_inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +000010137 if (!inode) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010138 cond_resched_lock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010139 continue;
Miao Xiedf0af1a2013-01-29 10:11:59 +000010140 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010141 spin_unlock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010142
Ethan Lien3cd24c62018-11-01 14:49:03 +080010143 if (snapshot)
10144 set_bit(BTRFS_INODE_SNAPSHOT_FLUSH,
10145 &binode->runtime_flags);
Nikolay Borisov076da912018-04-23 10:54:16 +030010146 work = btrfs_alloc_delalloc_work(inode);
David Sterba5d99a9982014-09-29 19:20:37 +020010147 if (!work) {
Nikolay Borisov4fbb5142018-04-23 10:54:15 +030010148 iput(inode);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010149 ret = -ENOMEM;
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010150 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010151 }
Miao Xie1eafa6c2013-01-22 10:49:00 +000010152 list_add_tail(&work->list, &works);
Qu Wenruoa44903a2014-02-28 10:46:09 +080010153 btrfs_queue_work(root->fs_info->flush_workers,
10154 &work->work);
Miao Xie6c255e62014-03-06 13:55:01 +080010155 ret++;
10156 if (nr != -1 && ret >= nr)
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010157 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010158 cond_resched();
Miao Xieeb73c1b2013-05-15 07:48:22 +000010159 spin_lock(&root->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -040010160 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010161 spin_unlock(&root->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -040010162
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010163out:
Miao Xie1eafa6c2013-01-22 10:49:00 +000010164 list_for_each_entry_safe(work, next, &works, list) {
10165 list_del_init(&work->list);
Nikolay Borisov40012f92018-04-19 10:46:39 +030010166 wait_for_completion(&work->completion);
10167 kfree(work);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010168 }
10169
Nikolay Borisov81f1d392018-04-19 10:46:37 +030010170 if (!list_empty(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010171 spin_lock(&root->delalloc_lock);
10172 list_splice_tail(&splice, &root->delalloc_inodes);
10173 spin_unlock(&root->delalloc_lock);
10174 }
Miao Xie573bfb72014-03-06 13:55:03 +080010175 mutex_unlock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010176 return ret;
10177}
10178
Ethan Lien3cd24c62018-11-01 14:49:03 +080010179int btrfs_start_delalloc_snapshot(struct btrfs_root *root)
Miao Xieeb73c1b2013-05-15 07:48:22 +000010180{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010181 struct btrfs_fs_info *fs_info = root->fs_info;
Miao Xieeb73c1b2013-05-15 07:48:22 +000010182 int ret;
10183
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010184 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +000010185 return -EROFS;
10186
Ethan Lien3cd24c62018-11-01 14:49:03 +080010187 ret = start_delalloc_inodes(root, -1, true);
Miao Xie6c255e62014-03-06 13:55:01 +080010188 if (ret > 0)
10189 ret = 0;
Miao Xieeb73c1b2013-05-15 07:48:22 +000010190 return ret;
10191}
10192
Nikolay Borisov82b3e532018-04-23 10:54:13 +030010193int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, int nr)
Miao Xieeb73c1b2013-05-15 07:48:22 +000010194{
10195 struct btrfs_root *root;
10196 struct list_head splice;
10197 int ret;
10198
Wang Shilong2c21b4d2014-01-14 19:42:20 +080010199 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +000010200 return -EROFS;
10201
10202 INIT_LIST_HEAD(&splice);
10203
Miao Xie573bfb72014-03-06 13:55:03 +080010204 mutex_lock(&fs_info->delalloc_root_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010205 spin_lock(&fs_info->delalloc_root_lock);
10206 list_splice_init(&fs_info->delalloc_roots, &splice);
Miao Xie6c255e62014-03-06 13:55:01 +080010207 while (!list_empty(&splice) && nr) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010208 root = list_first_entry(&splice, struct btrfs_root,
10209 delalloc_root);
10210 root = btrfs_grab_fs_root(root);
10211 BUG_ON(!root);
10212 list_move_tail(&root->delalloc_root,
10213 &fs_info->delalloc_roots);
10214 spin_unlock(&fs_info->delalloc_root_lock);
10215
Ethan Lien3cd24c62018-11-01 14:49:03 +080010216 ret = start_delalloc_inodes(root, nr, false);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010217 btrfs_put_fs_root(root);
Miao Xie6c255e62014-03-06 13:55:01 +080010218 if (ret < 0)
Miao Xieeb73c1b2013-05-15 07:48:22 +000010219 goto out;
10220
Miao Xie6c255e62014-03-06 13:55:01 +080010221 if (nr != -1) {
10222 nr -= ret;
10223 WARN_ON(nr < 0);
10224 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010225 spin_lock(&fs_info->delalloc_root_lock);
10226 }
10227 spin_unlock(&fs_info->delalloc_root_lock);
10228
Miao Xie6c255e62014-03-06 13:55:01 +080010229 ret = 0;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010230out:
Nikolay Borisov81f1d392018-04-19 10:46:37 +030010231 if (!list_empty(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010232 spin_lock(&fs_info->delalloc_root_lock);
10233 list_splice_tail(&splice, &fs_info->delalloc_roots);
10234 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010235 }
Miao Xie573bfb72014-03-06 13:55:03 +080010236 mutex_unlock(&fs_info->delalloc_root_mutex);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010237 return ret;
Chris Masonea8c2812008-08-04 23:17:27 -040010238}
10239
Chris Mason39279cc2007-06-12 06:35:45 -040010240static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
10241 const char *symname)
10242{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010243 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -040010244 struct btrfs_trans_handle *trans;
10245 struct btrfs_root *root = BTRFS_I(dir)->root;
10246 struct btrfs_path *path;
10247 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -050010248 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -040010249 int err;
Chris Mason39279cc2007-06-12 06:35:45 -040010250 u64 objectid;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +053010251 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -040010252 int name_len;
10253 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -040010254 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -040010255 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -040010256 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -040010257
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +010010258 name_len = strlen(symname);
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010259 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info))
Chris Mason39279cc2007-06-12 06:35:45 -040010260 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -050010261
Josef Bacik9ed74f22009-09-11 16:12:44 -040010262 /*
10263 * 2 items for inode item and ref
10264 * 2 items for dir items
Filipe Manana9269d122015-12-31 18:16:29 +000010265 * 1 item for updating parent inode item
10266 * 1 item for the inline extent item
Josef Bacik9ed74f22009-09-11 16:12:44 -040010267 * 1 item for xattr if selinux is on
10268 */
Filipe Manana9269d122015-12-31 18:16:29 +000010269 trans = btrfs_start_transaction(root, 7);
Yan, Zhenga22285a2010-05-16 10:48:46 -040010270 if (IS_ERR(trans))
10271 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -050010272
Li Zefan581bb052011-04-20 10:06:11 +080010273 err = btrfs_find_free_ino(root, &objectid);
10274 if (err)
10275 goto out_unlock;
10276
Josef Bacikaec74772008-07-24 12:12:38 -040010277 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +010010278 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)),
10279 objectid, S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -040010280 if (IS_ERR(inode)) {
10281 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -040010282 inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -040010283 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -040010284 }
Chris Mason39279cc2007-06-12 06:35:45 -040010285
Casey Schauflerad19db72011-12-15 10:09:07 -050010286 /*
10287 * If the active LSM wants to access the inode during
10288 * d_instantiate it needs these. Smack checks to see
10289 * if the filesystem supports xattrs by looking at the
10290 * ops vector.
10291 */
10292 inode->i_fop = &btrfs_file_operations;
10293 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -070010294 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -070010295 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
10296
10297 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
10298 if (err)
Al Viro32955c52018-05-16 12:20:05 -040010299 goto out_unlock;
Casey Schauflerad19db72011-12-15 10:09:07 -050010300
Chris Mason39279cc2007-06-12 06:35:45 -040010301 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -070010302 if (!path) {
10303 err = -ENOMEM;
Al Viro32955c52018-05-16 12:20:05 -040010304 goto out_unlock;
Mark Fashehd8926bb2011-07-13 10:38:47 -070010305 }
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +020010306 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason39279cc2007-06-12 06:35:45 -040010307 key.offset = 0;
David Sterba962a2982014-06-04 18:41:45 +020010308 key.type = BTRFS_EXTENT_DATA_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -040010309 datasize = btrfs_file_extent_calc_inline_size(name_len);
10310 err = btrfs_insert_empty_item(trans, root, path, &key,
10311 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -040010312 if (err) {
Julia Lawallb0839162011-05-14 07:10:51 +000010313 btrfs_free_path(path);
Al Viro32955c52018-05-16 12:20:05 -040010314 goto out_unlock;
Chris Mason54aa1f42007-06-22 14:16:25 -040010315 }
Chris Mason5f39d392007-10-15 16:14:19 -040010316 leaf = path->nodes[0];
10317 ei = btrfs_item_ptr(leaf, path->slots[0],
10318 struct btrfs_file_extent_item);
10319 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
10320 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -040010321 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -040010322 btrfs_set_file_extent_encryption(leaf, ei, 0);
10323 btrfs_set_file_extent_compression(leaf, ei, 0);
10324 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
10325 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
10326
Chris Mason39279cc2007-06-12 06:35:45 -040010327 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -040010328 write_extent_buffer(leaf, symname, ptr, name_len);
10329 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -040010330 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -040010331
Chris Mason39279cc2007-06-12 06:35:45 -040010332 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -050010333 inode_nohighmem(inode);
Yan Zhengd899e052008-10-30 14:25:28 -040010334 inode_set_bytes(inode, name_len);
Nikolay Borisov6ef06d22017-02-20 13:50:34 +020010335 btrfs_i_size_write(BTRFS_I(inode), name_len);
Chris Mason54aa1f42007-06-22 14:16:25 -040010336 err = btrfs_update_inode(trans, root, inode);
Filipe Mananad50866d2015-12-31 18:08:24 +000010337 /*
10338 * Last step, add directory indexes for our symlink inode. This is the
10339 * last step to avoid extra cleanup of these indexes if an error happens
10340 * elsewhere above.
10341 */
10342 if (!err)
Nikolay Borisovcef415a2017-02-20 13:51:09 +020010343 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry,
10344 BTRFS_I(inode), 0, index);
Al Viro32955c52018-05-16 12:20:05 -040010345 if (err)
10346 goto out_unlock;
Chris Masonb0d5d102014-09-08 13:08:51 -070010347
Al Viro1e2e5472018-05-04 08:23:01 -040010348 d_instantiate_new(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010349
10350out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010351 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -040010352 if (err && inode) {
Chris Mason39279cc2007-06-12 06:35:45 -040010353 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -040010354 discard_new_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010355 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010356 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -040010357 return err;
10358}
Chris Mason16432982008-04-10 10:23:21 -040010359
Josef Bacik0af3d002010-06-21 14:48:16 -040010360static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
10361 u64 start, u64 num_bytes, u64 min_size,
10362 loff_t actual_len, u64 *alloc_hint,
10363 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -040010364{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010365 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik5dc562c2012-08-17 13:14:17 -040010366 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
10367 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -040010368 struct btrfs_root *root = BTRFS_I(inode)->root;
10369 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -040010370 u64 cur_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +000010371 u64 i_size;
Chris Mason154ea282013-03-05 11:11:26 -050010372 u64 cur_bytes;
Josef Bacik0b670dc2015-09-23 17:11:16 -040010373 u64 last_alloc = (u64)-1;
Yan Zhengd899e052008-10-30 14:25:28 -040010374 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -040010375 bool own_trans = true;
Wang Xiaoguang18513092016-07-25 15:51:40 +080010376 u64 end = start + num_bytes - 1;
Yan Zhengd899e052008-10-30 14:25:28 -040010377
Josef Bacik0af3d002010-06-21 14:48:16 -040010378 if (trans)
10379 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -040010380 while (num_bytes > 0) {
Josef Bacik0af3d002010-06-21 14:48:16 -040010381 if (own_trans) {
10382 trans = btrfs_start_transaction(root, 3);
10383 if (IS_ERR(trans)) {
10384 ret = PTR_ERR(trans);
10385 break;
10386 }
Yan Zhengd899e052008-10-30 14:25:28 -040010387 }
Yan, Zheng5a303d52009-11-12 09:34:52 +000010388
Byongho Leeee221842015-12-15 01:42:10 +090010389 cur_bytes = min_t(u64, num_bytes, SZ_256M);
Chris Mason154ea282013-03-05 11:11:26 -050010390 cur_bytes = max(cur_bytes, min_size);
Josef Bacik0b670dc2015-09-23 17:11:16 -040010391 /*
10392 * If we are severely fragmented we could end up with really
10393 * small allocations, so if the allocator is returning small
10394 * chunks lets make its job easier by only searching for those
10395 * sized chunks.
10396 */
10397 cur_bytes = min(cur_bytes, last_alloc);
Wang Xiaoguang18513092016-07-25 15:51:40 +080010398 ret = btrfs_reserve_extent(root, cur_bytes, cur_bytes,
10399 min_size, 0, *alloc_hint, &ins, 1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010400 if (ret) {
Josef Bacik0af3d002010-06-21 14:48:16 -040010401 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010402 btrfs_end_transaction(trans);
Yan, Zhenga22285a2010-05-16 10:48:46 -040010403 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +000010404 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010405 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010406
Josef Bacik0b670dc2015-09-23 17:11:16 -040010407 last_alloc = ins.offset;
Yan Zhengd899e052008-10-30 14:25:28 -040010408 ret = insert_reserved_file_extent(trans, inode,
10409 cur_offset, ins.objectid,
10410 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +000010411 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -040010412 BTRFS_FILE_EXTENT_PREALLOC);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010413 if (ret) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010414 btrfs_free_reserved_extent(fs_info, ins.objectid,
Miao Xiee570fd22014-06-19 10:42:50 +080010415 ins.offset, 0);
Jeff Mahoney66642832016-06-10 18:19:25 -040010416 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010417 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010418 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010419 break;
10420 }
Dongsheng Yang31193212014-12-12 16:44:35 +080010421
Nikolay Borisovdcdbc052017-02-20 13:50:45 +020010422 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Chris Masona1ed8352009-09-11 12:27:37 -040010423 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010424
Josef Bacik5dc562c2012-08-17 13:14:17 -040010425 em = alloc_extent_map();
10426 if (!em) {
10427 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
10428 &BTRFS_I(inode)->runtime_flags);
10429 goto next;
10430 }
10431
10432 em->start = cur_offset;
10433 em->orig_start = cur_offset;
10434 em->len = ins.offset;
10435 em->block_start = ins.objectid;
10436 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -050010437 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -040010438 em->ram_bytes = ins.offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010439 em->bdev = fs_info->fs_devices->latest_bdev;
Josef Bacik5dc562c2012-08-17 13:14:17 -040010440 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
10441 em->generation = trans->transid;
10442
10443 while (1) {
10444 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -040010445 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -040010446 write_unlock(&em_tree->lock);
10447 if (ret != -EEXIST)
10448 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +020010449 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -040010450 cur_offset + ins.offset - 1,
10451 0);
10452 }
10453 free_extent_map(em);
10454next:
Yan Zhengd899e052008-10-30 14:25:28 -040010455 num_bytes -= ins.offset;
10456 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -040010457 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +000010458
Josef Bacik0c4d2d92012-04-05 15:03:02 -040010459 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -070010460 inode->i_ctime = current_time(inode);
Christoph Hellwig6cbff002009-04-17 10:37:41 +020010461 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -040010462 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -040010463 (actual_len > inode->i_size) &&
10464 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +000010465 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +000010466 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +000010467 else
Josef Bacik55a61d12010-11-22 18:50:32 +000010468 i_size = cur_offset;
10469 i_size_write(inode, i_size);
10470 btrfs_ordered_update_i_size(inode, i_size, NULL);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010471 }
10472
Yan Zhengd899e052008-10-30 14:25:28 -040010473 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010474
10475 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -040010476 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010477 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010478 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010479 break;
10480 }
Yan Zhengd899e052008-10-30 14:25:28 -040010481
Josef Bacik0af3d002010-06-21 14:48:16 -040010482 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010483 btrfs_end_transaction(trans);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010484 }
Wang Xiaoguang18513092016-07-25 15:51:40 +080010485 if (cur_offset < end)
Qu Wenruobc42bda2017-02-27 15:10:39 +080010486 btrfs_free_reserved_data_space(inode, NULL, cur_offset,
Wang Xiaoguang18513092016-07-25 15:51:40 +080010487 end - cur_offset + 1);
Yan Zhengd899e052008-10-30 14:25:28 -040010488 return ret;
10489}
10490
Josef Bacik0af3d002010-06-21 14:48:16 -040010491int btrfs_prealloc_file_range(struct inode *inode, int mode,
10492 u64 start, u64 num_bytes, u64 min_size,
10493 loff_t actual_len, u64 *alloc_hint)
10494{
10495 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
10496 min_size, actual_len, alloc_hint,
10497 NULL);
10498}
10499
10500int btrfs_prealloc_file_range_trans(struct inode *inode,
10501 struct btrfs_trans_handle *trans, int mode,
10502 u64 start, u64 num_bytes, u64 min_size,
10503 loff_t actual_len, u64 *alloc_hint)
10504{
10505 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
10506 min_size, actual_len, alloc_hint, trans);
10507}
10508
Chris Masone6dcd2d2008-07-17 12:53:50 -040010509static int btrfs_set_page_dirty(struct page *page)
10510{
Chris Masone6dcd2d2008-07-17 12:53:50 -040010511 return __set_page_dirty_nobuffers(page);
10512}
10513
Al Viro10556cb22011-06-20 19:28:19 -040010514static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -050010515{
Li Zefanb83cc962010-12-20 16:04:08 +080010516 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +000010517 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +080010518
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +000010519 if (mask & MAY_WRITE &&
10520 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
10521 if (btrfs_root_readonly(root))
10522 return -EROFS;
10523 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
10524 return -EACCES;
10525 }
Al Viro2830ba72011-06-20 19:16:29 -040010526 return generic_permission(inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -050010527}
Chris Mason39279cc2007-06-12 06:35:45 -040010528
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010529static int btrfs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
10530{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010531 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010532 struct btrfs_trans_handle *trans;
10533 struct btrfs_root *root = BTRFS_I(dir)->root;
10534 struct inode *inode = NULL;
10535 u64 objectid;
10536 u64 index;
10537 int ret = 0;
10538
10539 /*
10540 * 5 units required for adding orphan entry
10541 */
10542 trans = btrfs_start_transaction(root, 5);
10543 if (IS_ERR(trans))
10544 return PTR_ERR(trans);
10545
10546 ret = btrfs_find_free_ino(root, &objectid);
10547 if (ret)
10548 goto out;
10549
10550 inode = btrfs_new_inode(trans, root, dir, NULL, 0,
David Sterbaf85b7372017-01-20 14:54:07 +010010551 btrfs_ino(BTRFS_I(dir)), objectid, mode, &index);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010552 if (IS_ERR(inode)) {
10553 ret = PTR_ERR(inode);
10554 inode = NULL;
10555 goto out;
10556 }
10557
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010558 inode->i_fop = &btrfs_file_operations;
10559 inode->i_op = &btrfs_file_inode_operations;
10560
10561 inode->i_mapping->a_ops = &btrfs_aops;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010562 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
10563
Chris Masonb0d5d102014-09-08 13:08:51 -070010564 ret = btrfs_init_inode_security(trans, inode, dir, NULL);
10565 if (ret)
Al Viro32955c52018-05-16 12:20:05 -040010566 goto out;
Chris Masonb0d5d102014-09-08 13:08:51 -070010567
10568 ret = btrfs_update_inode(trans, root, inode);
10569 if (ret)
Al Viro32955c52018-05-16 12:20:05 -040010570 goto out;
Nikolay Borisov73f2e542017-02-20 13:50:59 +020010571 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010572 if (ret)
Al Viro32955c52018-05-16 12:20:05 -040010573 goto out;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010574
Filipe Manana5762b5c2014-08-01 00:10:32 +010010575 /*
10576 * We set number of links to 0 in btrfs_new_inode(), and here we set
10577 * it to 1 because d_tmpfile() will issue a warning if the count is 0,
10578 * through:
10579 *
10580 * d_tmpfile() -> inode_dec_link_count() -> drop_nlink()
10581 */
10582 set_nlink(inode, 1);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010583 d_tmpfile(dentry, inode);
Al Viro32955c52018-05-16 12:20:05 -040010584 unlock_new_inode(inode);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010585 mark_inode_dirty(inode);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010586out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010587 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -040010588 if (ret && inode)
10589 discard_new_inode(inode);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010590 btrfs_btree_balance_dirty(fs_info);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010591 return ret;
10592}
10593
David Sterba5cdc84b2018-07-18 20:32:52 +020010594void btrfs_set_range_writeback(struct extent_io_tree *tree, u64 start, u64 end)
Josef Bacikc6100a42017-05-05 11:57:13 -040010595{
David Sterba5cdc84b2018-07-18 20:32:52 +020010596 struct inode *inode = tree->private_data;
Josef Bacikc6100a42017-05-05 11:57:13 -040010597 unsigned long index = start >> PAGE_SHIFT;
10598 unsigned long end_index = end >> PAGE_SHIFT;
10599 struct page *page;
10600
10601 while (index <= end_index) {
10602 page = find_get_page(inode->i_mapping, index);
10603 ASSERT(page); /* Pages should be in the extent_io_tree */
10604 set_page_writeback(page);
10605 put_page(page);
10606 index++;
10607 }
10608}
10609
Omar Sandovaled46ff32016-11-03 10:28:14 -070010610#ifdef CONFIG_SWAP
10611/*
10612 * Add an entry indicating a block group or device which is pinned by a
10613 * swapfile. Returns 0 on success, 1 if there is already an entry for it, or a
10614 * negative errno on failure.
10615 */
10616static int btrfs_add_swapfile_pin(struct inode *inode, void *ptr,
10617 bool is_block_group)
10618{
10619 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
10620 struct btrfs_swapfile_pin *sp, *entry;
10621 struct rb_node **p;
10622 struct rb_node *parent = NULL;
10623
10624 sp = kmalloc(sizeof(*sp), GFP_NOFS);
10625 if (!sp)
10626 return -ENOMEM;
10627 sp->ptr = ptr;
10628 sp->inode = inode;
10629 sp->is_block_group = is_block_group;
10630
10631 spin_lock(&fs_info->swapfile_pins_lock);
10632 p = &fs_info->swapfile_pins.rb_node;
10633 while (*p) {
10634 parent = *p;
10635 entry = rb_entry(parent, struct btrfs_swapfile_pin, node);
10636 if (sp->ptr < entry->ptr ||
10637 (sp->ptr == entry->ptr && sp->inode < entry->inode)) {
10638 p = &(*p)->rb_left;
10639 } else if (sp->ptr > entry->ptr ||
10640 (sp->ptr == entry->ptr && sp->inode > entry->inode)) {
10641 p = &(*p)->rb_right;
10642 } else {
10643 spin_unlock(&fs_info->swapfile_pins_lock);
10644 kfree(sp);
10645 return 1;
10646 }
10647 }
10648 rb_link_node(&sp->node, parent, p);
10649 rb_insert_color(&sp->node, &fs_info->swapfile_pins);
10650 spin_unlock(&fs_info->swapfile_pins_lock);
10651 return 0;
10652}
10653
10654/* Free all of the entries pinned by this swapfile. */
10655static void btrfs_free_swapfile_pins(struct inode *inode)
10656{
10657 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
10658 struct btrfs_swapfile_pin *sp;
10659 struct rb_node *node, *next;
10660
10661 spin_lock(&fs_info->swapfile_pins_lock);
10662 node = rb_first(&fs_info->swapfile_pins);
10663 while (node) {
10664 next = rb_next(node);
10665 sp = rb_entry(node, struct btrfs_swapfile_pin, node);
10666 if (sp->inode == inode) {
10667 rb_erase(&sp->node, &fs_info->swapfile_pins);
10668 if (sp->is_block_group)
10669 btrfs_put_block_group(sp->ptr);
10670 kfree(sp);
10671 }
10672 node = next;
10673 }
10674 spin_unlock(&fs_info->swapfile_pins_lock);
10675}
10676
10677struct btrfs_swap_info {
10678 u64 start;
10679 u64 block_start;
10680 u64 block_len;
10681 u64 lowest_ppage;
10682 u64 highest_ppage;
10683 unsigned long nr_pages;
10684 int nr_extents;
10685};
10686
10687static int btrfs_add_swap_extent(struct swap_info_struct *sis,
10688 struct btrfs_swap_info *bsi)
10689{
10690 unsigned long nr_pages;
10691 u64 first_ppage, first_ppage_reported, next_ppage;
10692 int ret;
10693
10694 first_ppage = ALIGN(bsi->block_start, PAGE_SIZE) >> PAGE_SHIFT;
10695 next_ppage = ALIGN_DOWN(bsi->block_start + bsi->block_len,
10696 PAGE_SIZE) >> PAGE_SHIFT;
10697
10698 if (first_ppage >= next_ppage)
10699 return 0;
10700 nr_pages = next_ppage - first_ppage;
10701
10702 first_ppage_reported = first_ppage;
10703 if (bsi->start == 0)
10704 first_ppage_reported++;
10705 if (bsi->lowest_ppage > first_ppage_reported)
10706 bsi->lowest_ppage = first_ppage_reported;
10707 if (bsi->highest_ppage < (next_ppage - 1))
10708 bsi->highest_ppage = next_ppage - 1;
10709
10710 ret = add_swap_extent(sis, bsi->nr_pages, nr_pages, first_ppage);
10711 if (ret < 0)
10712 return ret;
10713 bsi->nr_extents += ret;
10714 bsi->nr_pages += nr_pages;
10715 return 0;
10716}
10717
10718static void btrfs_swap_deactivate(struct file *file)
10719{
10720 struct inode *inode = file_inode(file);
10721
10722 btrfs_free_swapfile_pins(inode);
10723 atomic_dec(&BTRFS_I(inode)->root->nr_swapfiles);
10724}
10725
10726static int btrfs_swap_activate(struct swap_info_struct *sis, struct file *file,
10727 sector_t *span)
10728{
10729 struct inode *inode = file_inode(file);
10730 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
10731 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
10732 struct extent_state *cached_state = NULL;
10733 struct extent_map *em = NULL;
10734 struct btrfs_device *device = NULL;
10735 struct btrfs_swap_info bsi = {
10736 .lowest_ppage = (sector_t)-1ULL,
10737 };
10738 int ret = 0;
10739 u64 isize;
10740 u64 start;
10741
10742 /*
10743 * If the swap file was just created, make sure delalloc is done. If the
10744 * file changes again after this, the user is doing something stupid and
10745 * we don't really care.
10746 */
10747 ret = btrfs_wait_ordered_range(inode, 0, (u64)-1);
10748 if (ret)
10749 return ret;
10750
10751 /*
10752 * The inode is locked, so these flags won't change after we check them.
10753 */
10754 if (BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS) {
10755 btrfs_warn(fs_info, "swapfile must not be compressed");
10756 return -EINVAL;
10757 }
10758 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW)) {
10759 btrfs_warn(fs_info, "swapfile must not be copy-on-write");
10760 return -EINVAL;
10761 }
10762 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
10763 btrfs_warn(fs_info, "swapfile must not be checksummed");
10764 return -EINVAL;
10765 }
10766
10767 /*
10768 * Balance or device remove/replace/resize can move stuff around from
10769 * under us. The EXCL_OP flag makes sure they aren't running/won't run
10770 * concurrently while we are mapping the swap extents, and
10771 * fs_info->swapfile_pins prevents them from running while the swap file
10772 * is active and moving the extents. Note that this also prevents a
10773 * concurrent device add which isn't actually necessary, but it's not
10774 * really worth the trouble to allow it.
10775 */
10776 if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) {
10777 btrfs_warn(fs_info,
10778 "cannot activate swapfile while exclusive operation is running");
10779 return -EBUSY;
10780 }
10781 /*
10782 * Snapshots can create extents which require COW even if NODATACOW is
10783 * set. We use this counter to prevent snapshots. We must increment it
10784 * before walking the extents because we don't want a concurrent
10785 * snapshot to run after we've already checked the extents.
10786 */
10787 atomic_inc(&BTRFS_I(inode)->root->nr_swapfiles);
10788
10789 isize = ALIGN_DOWN(inode->i_size, fs_info->sectorsize);
10790
10791 lock_extent_bits(io_tree, 0, isize - 1, &cached_state);
10792 start = 0;
10793 while (start < isize) {
10794 u64 logical_block_start, physical_block_start;
10795 struct btrfs_block_group_cache *bg;
10796 u64 len = isize - start;
10797
10798 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len, 0);
10799 if (IS_ERR(em)) {
10800 ret = PTR_ERR(em);
10801 goto out;
10802 }
10803
10804 if (em->block_start == EXTENT_MAP_HOLE) {
10805 btrfs_warn(fs_info, "swapfile must not have holes");
10806 ret = -EINVAL;
10807 goto out;
10808 }
10809 if (em->block_start == EXTENT_MAP_INLINE) {
10810 /*
10811 * It's unlikely we'll ever actually find ourselves
10812 * here, as a file small enough to fit inline won't be
10813 * big enough to store more than the swap header, but in
10814 * case something changes in the future, let's catch it
10815 * here rather than later.
10816 */
10817 btrfs_warn(fs_info, "swapfile must not be inline");
10818 ret = -EINVAL;
10819 goto out;
10820 }
10821 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
10822 btrfs_warn(fs_info, "swapfile must not be compressed");
10823 ret = -EINVAL;
10824 goto out;
10825 }
10826
10827 logical_block_start = em->block_start + (start - em->start);
10828 len = min(len, em->len - (start - em->start));
10829 free_extent_map(em);
10830 em = NULL;
10831
10832 ret = can_nocow_extent(inode, start, &len, NULL, NULL, NULL);
10833 if (ret < 0) {
10834 goto out;
10835 } else if (ret) {
10836 ret = 0;
10837 } else {
10838 btrfs_warn(fs_info,
10839 "swapfile must not be copy-on-write");
10840 ret = -EINVAL;
10841 goto out;
10842 }
10843
10844 em = btrfs_get_chunk_map(fs_info, logical_block_start, len);
10845 if (IS_ERR(em)) {
10846 ret = PTR_ERR(em);
10847 goto out;
10848 }
10849
10850 if (em->map_lookup->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
10851 btrfs_warn(fs_info,
10852 "swapfile must have single data profile");
10853 ret = -EINVAL;
10854 goto out;
10855 }
10856
10857 if (device == NULL) {
10858 device = em->map_lookup->stripes[0].dev;
10859 ret = btrfs_add_swapfile_pin(inode, device, false);
10860 if (ret == 1)
10861 ret = 0;
10862 else if (ret)
10863 goto out;
10864 } else if (device != em->map_lookup->stripes[0].dev) {
10865 btrfs_warn(fs_info, "swapfile must be on one device");
10866 ret = -EINVAL;
10867 goto out;
10868 }
10869
10870 physical_block_start = (em->map_lookup->stripes[0].physical +
10871 (logical_block_start - em->start));
10872 len = min(len, em->len - (logical_block_start - em->start));
10873 free_extent_map(em);
10874 em = NULL;
10875
10876 bg = btrfs_lookup_block_group(fs_info, logical_block_start);
10877 if (!bg) {
10878 btrfs_warn(fs_info,
10879 "could not find block group containing swapfile");
10880 ret = -EINVAL;
10881 goto out;
10882 }
10883
10884 ret = btrfs_add_swapfile_pin(inode, bg, true);
10885 if (ret) {
10886 btrfs_put_block_group(bg);
10887 if (ret == 1)
10888 ret = 0;
10889 else
10890 goto out;
10891 }
10892
10893 if (bsi.block_len &&
10894 bsi.block_start + bsi.block_len == physical_block_start) {
10895 bsi.block_len += len;
10896 } else {
10897 if (bsi.block_len) {
10898 ret = btrfs_add_swap_extent(sis, &bsi);
10899 if (ret)
10900 goto out;
10901 }
10902 bsi.start = start;
10903 bsi.block_start = physical_block_start;
10904 bsi.block_len = len;
10905 }
10906
10907 start += len;
10908 }
10909
10910 if (bsi.block_len)
10911 ret = btrfs_add_swap_extent(sis, &bsi);
10912
10913out:
10914 if (!IS_ERR_OR_NULL(em))
10915 free_extent_map(em);
10916
10917 unlock_extent_cached(io_tree, 0, isize - 1, &cached_state);
10918
10919 if (ret)
10920 btrfs_swap_deactivate(file);
10921
10922 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
10923
10924 if (ret)
10925 return ret;
10926
10927 if (device)
10928 sis->bdev = device->bdev;
10929 *span = bsi.highest_ppage - bsi.lowest_ppage + 1;
10930 sis->max = bsi.nr_pages;
10931 sis->pages = bsi.nr_pages - 1;
10932 sis->highest_bit = bsi.nr_pages - 1;
10933 return bsi.nr_extents;
10934}
10935#else
10936static void btrfs_swap_deactivate(struct file *file)
10937{
10938}
10939
10940static int btrfs_swap_activate(struct swap_info_struct *sis, struct file *file,
10941 sector_t *span)
10942{
10943 return -EOPNOTSUPP;
10944}
10945#endif
10946
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010947static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -050010948 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -040010949 .lookup = btrfs_lookup,
10950 .create = btrfs_create,
10951 .unlink = btrfs_unlink,
10952 .link = btrfs_link,
10953 .mkdir = btrfs_mkdir,
10954 .rmdir = btrfs_rmdir,
Miklos Szeredi2773bf02016-09-27 11:03:58 +020010955 .rename = btrfs_rename2,
Chris Mason39279cc2007-06-12 06:35:45 -040010956 .symlink = btrfs_symlink,
10957 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -040010958 .mknod = btrfs_mknod,
Josef Bacik5103e942007-11-16 11:45:54 -050010959 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010960 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010961 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010962 .set_acl = btrfs_set_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070010963 .update_time = btrfs_update_time,
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010964 .tmpfile = btrfs_tmpfile,
Chris Mason39279cc2007-06-12 06:35:45 -040010965};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010966static const struct inode_operations btrfs_dir_ro_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010967 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -050010968 .permission = btrfs_permission,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070010969 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010970};
Yan, Zheng76dda932009-09-21 16:00:26 -040010971
Alexey Dobriyan828c0952009-10-01 15:43:56 -070010972static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010973 .llseek = generic_file_llseek,
10974 .read = generic_read_dir,
Omar Sandoval02dbfc92016-05-20 13:50:33 -070010975 .iterate_shared = btrfs_real_readdir,
Josef Bacik23b5ec72017-07-24 15:14:25 -040010976 .open = btrfs_opendir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -040010977 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010978#ifdef CONFIG_COMPAT
Luke Dashjr4c63c242015-10-29 08:22:21 +000010979 .compat_ioctl = btrfs_compat_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010980#endif
Sage Weil6bf13c02008-06-10 10:07:39 -040010981 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -040010982 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -040010983};
10984
David Sterba20e55062015-11-19 11:42:28 +010010985static const struct extent_io_ops btrfs_extent_io_ops = {
David Sterba4d53ddd2017-02-17 15:27:44 +010010986 /* mandatory callbacks */
Chris Mason065631f2008-02-20 12:07:25 -050010987 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -040010988 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
10989};
10990
Chris Mason35054392009-01-21 13:11:13 -050010991/*
10992 * btrfs doesn't support the bmap operation because swapfiles
10993 * use bmap to make a mapping of extents in the file. They assume
10994 * these extents won't change over the life of the file and they
10995 * use the bmap result to do IO directly to the drive.
10996 *
10997 * the btrfs bmap call would return logical addresses that aren't
10998 * suitable for IO and they also will change frequently as COW
10999 * operations happen. So, swapfile + btrfs == corruption.
11000 *
11001 * For now we're avoiding this by dropping bmap.
11002 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -070011003static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -040011004 .readpage = btrfs_readpage,
11005 .writepage = btrfs_writepage,
Chris Masonb293f02e2007-11-01 19:45:34 -040011006 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -050011007 .readpages = btrfs_readpages,
Chris Mason16432982008-04-10 10:23:21 -040011008 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -040011009 .invalidatepage = btrfs_invalidatepage,
11010 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -040011011 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +020011012 .error_remove_page = generic_error_remove_page,
Omar Sandovaled46ff32016-11-03 10:28:14 -070011013 .swap_activate = btrfs_swap_activate,
11014 .swap_deactivate = btrfs_swap_deactivate,
Chris Mason39279cc2007-06-12 06:35:45 -040011015};
11016
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070011017static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040011018 .getattr = btrfs_getattr,
11019 .setattr = btrfs_setattr,
Josef Bacik5103e942007-11-16 11:45:54 -050011020 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050011021 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -050011022 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020011023 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080011024 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040011025 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040011026};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070011027static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -040011028 .getattr = btrfs_getattr,
11029 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050011030 .permission = btrfs_permission,
Josef Bacik33268ea2008-07-24 12:16:36 -040011031 .listxattr = btrfs_listxattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020011032 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080011033 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040011034 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -040011035};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070011036static const struct inode_operations btrfs_symlink_inode_operations = {
Al Viro6b255392015-11-17 10:20:54 -050011037 .get_link = page_get_link,
Li Zefanf2095612010-11-19 02:05:24 +000011038 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -050011039 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050011040 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -050011041 .listxattr = btrfs_listxattr,
Josef Bacike41f9412012-03-26 09:46:47 -040011042 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040011043};
Yan, Zheng76dda932009-09-21 16:00:26 -040011044
Alexey Dobriyan82d339d2009-10-09 09:54:36 -040011045const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -040011046 .d_delete = btrfs_dentry_delete,
11047};