blob: 7ed0fe2dd5bafdc81f690edbfe3c9262c91618ec [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;
Chris Masonec39f762019-07-10 12:28:17 -0700371 struct cgroup_subsys_state *blkcg_css;
Chris Mason771ed682008-11-06 22:02:51 -0500372 struct btrfs_work work;
Nikolay Borisov97db1202019-03-12 17:20:24 +0200373 atomic_t *pending;
Chris Mason771ed682008-11-06 22:02:51 -0500374};
375
Nikolay Borisov97db1202019-03-12 17:20:24 +0200376struct async_cow {
377 /* Number of chunks in flight; must be first in the structure */
378 atomic_t num_chunks;
379 struct async_chunk chunks[];
380};
381
382static noinline int add_async_extent(struct async_chunk *cow,
Chris Mason771ed682008-11-06 22:02:51 -0500383 u64 start, u64 ram_size,
384 u64 compressed_size,
385 struct page **pages,
Li Zefan261507a02010-12-17 14:21:50 +0800386 unsigned long nr_pages,
387 int compress_type)
Chris Mason771ed682008-11-06 22:02:51 -0500388{
389 struct async_extent *async_extent;
390
391 async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100392 BUG_ON(!async_extent); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -0500393 async_extent->start = start;
394 async_extent->ram_size = ram_size;
395 async_extent->compressed_size = compressed_size;
396 async_extent->pages = pages;
397 async_extent->nr_pages = nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800398 async_extent->compress_type = compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500399 list_add_tail(&async_extent->list, &cow->extents);
400 return 0;
401}
402
Qu Wenruo42c16da2019-07-01 05:12:46 +0000403/*
404 * Check if the inode has flags compatible with compression
405 */
406static inline bool inode_can_compress(struct inode *inode)
407{
408 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW ||
409 BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
410 return false;
411 return true;
412}
413
414/*
415 * Check if the inode needs to be submitted to compression, based on mount
416 * options, defragmentation, properties or heuristics.
417 */
Timofey Titovetsc2fcdcd2017-07-17 16:52:58 +0300418static inline int inode_need_compress(struct inode *inode, u64 start, u64 end)
Wang Shilongf79707b2014-07-17 11:44:09 +0800419{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400420 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Wang Shilongf79707b2014-07-17 11:44:09 +0800421
Qu Wenruo42c16da2019-07-01 05:12:46 +0000422 if (!inode_can_compress(inode)) {
423 WARN(IS_ENABLED(CONFIG_BTRFS_DEBUG),
424 KERN_ERR "BTRFS: unexpected compression for ino %llu\n",
425 btrfs_ino(BTRFS_I(inode)));
426 return 0;
427 }
Wang Shilongf79707b2014-07-17 11:44:09 +0800428 /* force compress */
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400429 if (btrfs_test_opt(fs_info, FORCE_COMPRESS))
Wang Shilongf79707b2014-07-17 11:44:09 +0800430 return 1;
David Sterbaeec63c62017-07-17 19:41:31 +0200431 /* defrag ioctl */
432 if (BTRFS_I(inode)->defrag_compress)
433 return 1;
Wang Shilongf79707b2014-07-17 11:44:09 +0800434 /* bad compression ratios */
435 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS)
436 return 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400437 if (btrfs_test_opt(fs_info, COMPRESS) ||
Wang Shilongf79707b2014-07-17 11:44:09 +0800438 BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS ||
David Sterbab52aa8c2017-07-17 19:17:20 +0200439 BTRFS_I(inode)->prop_compress)
Timofey Titovetsc2fcdcd2017-07-17 16:52:58 +0300440 return btrfs_compress_heuristic(inode, start, end);
Wang Shilongf79707b2014-07-17 11:44:09 +0800441 return 0;
442}
443
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200444static inline void inode_should_defrag(struct btrfs_inode *inode,
Anand Jain26d30f82016-12-19 19:09:06 +0800445 u64 start, u64 end, u64 num_bytes, u64 small_write)
446{
447 /* If this is a small write inside eof, kick off a defrag */
448 if (num_bytes < small_write &&
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200449 (start > 0 || end + 1 < inode->disk_i_size))
Anand Jain26d30f82016-12-19 19:09:06 +0800450 btrfs_add_inode_defrag(NULL, inode);
451}
452
Chris Masonc8b97812008-10-29 14:49:59 -0400453/*
Chris Mason771ed682008-11-06 22:02:51 -0500454 * we create compressed extents in two phases. The first
455 * phase compresses a range of pages that have already been
456 * locked (both pages and state bits are locked).
Chris Masonc8b97812008-10-29 14:49:59 -0400457 *
Chris Mason771ed682008-11-06 22:02:51 -0500458 * This is done inside an ordered work queue, and the compression
459 * is spread across many cpus. The actual IO submission is step
460 * two, and the ordered work queue takes care of making sure that
461 * happens in the same order things were put onto the queue by
462 * writepages and friends.
Chris Masonc8b97812008-10-29 14:49:59 -0400463 *
Chris Mason771ed682008-11-06 22:02:51 -0500464 * If this code finds it can't get good compression, it puts an
465 * entry onto the work queue to write the uncompressed bytes. This
466 * makes sure that both compressed inodes and uncompressed inodes
Artem Bityutskiyb2570312012-07-25 18:12:06 +0300467 * are written in the same order that the flusher thread sent them
468 * down.
Chris Masond352ac62008-09-29 15:18:18 -0400469 */
Nikolay Borisovac3e9932019-07-17 14:41:44 +0300470static noinline int compress_file_range(struct async_chunk *async_chunk)
Chris Masonb888db22007-08-27 16:49:44 -0400471{
Nikolay Borisov1368c6d2019-03-12 17:20:27 +0200472 struct inode *inode = async_chunk->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400473 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400474 u64 blocksize = fs_info->sectorsize;
Nikolay Borisov1368c6d2019-03-12 17:20:27 +0200475 u64 start = async_chunk->start;
476 u64 end = async_chunk->end;
Chris Masonc8b97812008-10-29 14:49:59 -0400477 u64 actual_end;
Josef Bacikd98da492019-10-11 09:03:54 -0400478 u64 i_size;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400479 int ret = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400480 struct page **pages = NULL;
481 unsigned long nr_pages;
Chris Masonc8b97812008-10-29 14:49:59 -0400482 unsigned long total_compressed = 0;
483 unsigned long total_in = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400484 int i;
485 int will_compress;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400486 int compress_type = fs_info->compress_type;
Nikolay Borisovac3e9932019-07-17 14:41:44 +0300487 int compressed_extents = 0;
Chris Mason4adaa612013-03-26 13:07:00 -0400488 int redirty = 0;
Chris Masonb888db22007-08-27 16:49:44 -0400489
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200490 inode_should_defrag(BTRFS_I(inode), start, end, end - start + 1,
491 SZ_16K);
Chris Mason4cb53002011-05-24 15:35:30 -0400492
Josef Bacikd98da492019-10-11 09:03:54 -0400493 /*
494 * We need to save i_size before now because it could change in between
495 * us evaluating the size and assigning it. This is because we lock and
496 * unlock the page in truncate and fallocate, and then modify the i_size
497 * later on.
498 *
499 * The barriers are to emulate READ_ONCE, remove that once i_size_read
500 * does that for us.
501 */
502 barrier();
503 i_size = i_size_read(inode);
504 barrier();
505 actual_end = min_t(u64, i_size, end + 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400506again:
507 will_compress = 0;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300508 nr_pages = (end >> PAGE_SHIFT) - (start >> PAGE_SHIFT) + 1;
David Sterba069eac72017-02-14 19:36:54 +0100509 BUILD_BUG_ON((BTRFS_MAX_COMPRESSED % PAGE_SIZE) != 0);
510 nr_pages = min_t(unsigned long, nr_pages,
511 BTRFS_MAX_COMPRESSED / PAGE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400512
Chris Masonf03d9301f2009-02-04 09:31:06 -0500513 /*
514 * we don't want to send crud past the end of i_size through
515 * compression, that's just a waste of CPU time. So, if the
516 * end of the file is before the start of our current
517 * requested range of bytes, we bail out to the uncompressed
518 * cleanup code that can deal with all of this.
519 *
520 * It isn't really the fastest way to fix things, but this is a
521 * very uncommon corner.
522 */
523 if (actual_end <= start)
524 goto cleanup_and_bail_uncompressed;
525
Chris Masonc8b97812008-10-29 14:49:59 -0400526 total_compressed = actual_end - start;
527
Shilong Wang4bcbb332014-10-07 18:44:35 -0400528 /*
529 * skip compression for a small file range(<=blocksize) that
Nicholas D Steeves01327612016-05-19 21:18:45 -0400530 * isn't an inline extent, since it doesn't save disk space at all.
Shilong Wang4bcbb332014-10-07 18:44:35 -0400531 */
532 if (total_compressed <= blocksize &&
533 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
534 goto cleanup_and_bail_uncompressed;
535
David Sterba069eac72017-02-14 19:36:54 +0100536 total_compressed = min_t(unsigned long, total_compressed,
537 BTRFS_MAX_UNCOMPRESSED);
Chris Masonc8b97812008-10-29 14:49:59 -0400538 total_in = 0;
539 ret = 0;
Chris Masondb945352007-10-15 16:15:53 -0400540
Chris Mason771ed682008-11-06 22:02:51 -0500541 /*
542 * we do compression for mount -o compress and when the
543 * inode has not been flagged as nocompress. This flag can
544 * change at any time if we discover bad compression ratios.
Chris Masonc8b97812008-10-29 14:49:59 -0400545 */
Timofey Titovetsc2fcdcd2017-07-17 16:52:58 +0300546 if (inode_need_compress(inode, start, end)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400547 WARN_ON(pages);
David Sterba31e818f2015-02-20 18:00:26 +0100548 pages = kcalloc(nr_pages, sizeof(struct page *), GFP_NOFS);
Li Zefan560f7d72011-09-08 10:22:01 +0800549 if (!pages) {
550 /* just bail out to the uncompressed code */
Filipe Manana3527a012018-10-13 00:37:25 +0100551 nr_pages = 0;
Li Zefan560f7d72011-09-08 10:22:01 +0800552 goto cont;
553 }
Chris Mason179e29e2007-11-01 11:28:41 -0400554
David Sterbaeec63c62017-07-17 19:41:31 +0200555 if (BTRFS_I(inode)->defrag_compress)
556 compress_type = BTRFS_I(inode)->defrag_compress;
557 else if (BTRFS_I(inode)->prop_compress)
David Sterbab52aa8c2017-07-17 19:17:20 +0200558 compress_type = BTRFS_I(inode)->prop_compress;
Li Zefan261507a02010-12-17 14:21:50 +0800559
Chris Mason4adaa612013-03-26 13:07:00 -0400560 /*
561 * we need to call clear_page_dirty_for_io on each
562 * page in the range. Otherwise applications with the file
563 * mmap'd can wander in and change the page contents while
564 * we are compressing them.
565 *
566 * If the compression fails for any reason, we set the pages
567 * dirty again later on.
Timofey Titovetse9679de2017-10-24 01:29:48 +0300568 *
569 * Note that the remaining part is redirtied, the start pointer
570 * has moved, the end is the original one.
Chris Mason4adaa612013-03-26 13:07:00 -0400571 */
Timofey Titovetse9679de2017-10-24 01:29:48 +0300572 if (!redirty) {
573 extent_range_clear_dirty_for_io(inode, start, end);
574 redirty = 1;
575 }
David Sterbaf51d2b52017-09-15 17:36:57 +0200576
577 /* Compression level is applied here and only here */
578 ret = btrfs_compress_pages(
579 compress_type | (fs_info->compress_level << 4),
Li Zefan261507a02010-12-17 14:21:50 +0800580 inode->i_mapping, start,
David Sterba38c31462017-02-14 19:04:07 +0100581 pages,
David Sterba4d3a8002017-02-14 19:04:07 +0100582 &nr_pages,
Li Zefan261507a02010-12-17 14:21:50 +0800583 &total_in,
David Sterbae5d74902017-02-14 19:45:05 +0100584 &total_compressed);
Chris Masonc8b97812008-10-29 14:49:59 -0400585
586 if (!ret) {
Johannes Thumshirn70730172018-12-05 15:23:03 +0100587 unsigned long offset = offset_in_page(total_compressed);
David Sterba4d3a8002017-02-14 19:04:07 +0100588 struct page *page = pages[nr_pages - 1];
Chris Masonc8b97812008-10-29 14:49:59 -0400589 char *kaddr;
590
591 /* zero the tail end of the last page, we might be
592 * sending it down to disk
593 */
594 if (offset) {
Cong Wang7ac687d2011-11-25 23:14:28 +0800595 kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400596 memset(kaddr + offset, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300597 PAGE_SIZE - offset);
Cong Wang7ac687d2011-11-25 23:14:28 +0800598 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400599 }
600 will_compress = 1;
601 }
602 }
Li Zefan560f7d72011-09-08 10:22:01 +0800603cont:
Chris Masonc8b97812008-10-29 14:49:59 -0400604 if (start == 0) {
605 /* lets try to make an inline extent */
Timofey Titovets6018ba02017-09-15 01:57:26 +0300606 if (ret || total_in < actual_end) {
Chris Masonc8b97812008-10-29 14:49:59 -0400607 /* we didn't compress the entire range, try
Chris Mason771ed682008-11-06 22:02:51 -0500608 * to make an uncompressed inline extent.
Chris Masonc8b97812008-10-29 14:49:59 -0400609 */
Nikolay Borisovd02c0e22018-03-02 09:43:15 +0200610 ret = cow_file_range_inline(inode, start, end, 0,
611 BTRFS_COMPRESS_NONE, NULL);
Chris Masonc8b97812008-10-29 14:49:59 -0400612 } else {
Chris Mason771ed682008-11-06 22:02:51 -0500613 /* try making a compressed inline extent */
Nikolay Borisovd02c0e22018-03-02 09:43:15 +0200614 ret = cow_file_range_inline(inode, start, end,
Li Zefanfe3f5662011-03-28 08:30:38 +0000615 total_compressed,
616 compress_type, pages);
Chris Masonc8b97812008-10-29 14:49:59 -0400617 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100618 if (ret <= 0) {
Josef Bacik151a41b2013-07-29 13:22:24 -0400619 unsigned long clear_flags = EXTENT_DELALLOC |
Josef Bacik8b62f872017-10-19 14:15:55 -0400620 EXTENT_DELALLOC_NEW | EXTENT_DEFRAG |
621 EXTENT_DO_ACCOUNTING;
Filipe Mananae6eb4312014-10-10 10:45:12 +0100622 unsigned long page_error_op;
623
Filipe Mananae6eb4312014-10-10 10:45:12 +0100624 page_error_op = ret < 0 ? PAGE_SET_ERROR : 0;
Josef Bacik151a41b2013-07-29 13:22:24 -0400625
Chris Mason771ed682008-11-06 22:02:51 -0500626 /*
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100627 * inline extent creation worked or returned error,
628 * we don't need to create any more async work items.
629 * Unlock and free up our temp pages.
Josef Bacik8b62f872017-10-19 14:15:55 -0400630 *
631 * We use DO_ACCOUNTING here because we need the
632 * delalloc_release_metadata to be done _after_ we drop
633 * our outstanding extent for clearing delalloc for this
634 * range.
Chris Mason771ed682008-11-06 22:02:51 -0500635 */
Nikolay Borisov74e91942019-07-17 16:18:16 +0300636 extent_clear_unlock_delalloc(inode, start, end, NULL,
637 clear_flags,
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800638 PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -0400639 PAGE_CLEAR_DIRTY |
640 PAGE_SET_WRITEBACK |
Filipe Mananae6eb4312014-10-10 10:45:12 +0100641 page_error_op |
Josef Bacikc2790a22013-07-29 11:20:47 -0400642 PAGE_END_WRITEBACK);
Nikolay Borisovcecc8d92019-07-17 14:41:45 +0300643
644 for (i = 0; i < nr_pages; i++) {
645 WARN_ON(pages[i]->mapping);
646 put_page(pages[i]);
647 }
648 kfree(pages);
649
650 return 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400651 }
652 }
653
654 if (will_compress) {
655 /*
656 * we aren't doing an inline extent round the compressed size
657 * up to a block size boundary so the allocator does sane
658 * things
659 */
Qu Wenruofda28322013-02-26 08:10:22 +0000660 total_compressed = ALIGN(total_compressed, blocksize);
Chris Masonc8b97812008-10-29 14:49:59 -0400661
662 /*
663 * one last check to make sure the compression is really a
Timofey Titovets170607e2017-06-06 14:41:15 +0300664 * win, compare the page count read with the blocks on disk,
665 * compression must free at least one sector size
Chris Masonc8b97812008-10-29 14:49:59 -0400666 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300667 total_in = ALIGN(total_in, PAGE_SIZE);
Timofey Titovets170607e2017-06-06 14:41:15 +0300668 if (total_compressed + blocksize <= total_in) {
Nikolay Borisovac3e9932019-07-17 14:41:44 +0300669 compressed_extents++;
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700670
671 /*
672 * The async work queues will take care of doing actual
673 * allocation on disk for these compressed pages, and
674 * will submit them to the elevator.
675 */
Nikolay Borisovb5326272019-03-12 17:20:25 +0200676 add_async_extent(async_chunk, start, total_in,
David Sterba4d3a8002017-02-14 19:04:07 +0100677 total_compressed, pages, nr_pages,
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700678 compress_type);
679
Timofey Titovets11708622017-10-03 18:06:01 +0300680 if (start + total_in < end) {
681 start += total_in;
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700682 pages = NULL;
683 cond_resched();
684 goto again;
685 }
Nikolay Borisovac3e9932019-07-17 14:41:44 +0300686 return compressed_extents;
Chris Masonc8b97812008-10-29 14:49:59 -0400687 }
688 }
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700689 if (pages) {
Chris Masonc8b97812008-10-29 14:49:59 -0400690 /*
691 * the compression code ran but failed to make things smaller,
692 * free any pages it allocated and our page pointer array
693 */
David Sterba4d3a8002017-02-14 19:04:07 +0100694 for (i = 0; i < nr_pages; i++) {
Chris Mason70b99e62008-10-31 12:46:39 -0400695 WARN_ON(pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300696 put_page(pages[i]);
Chris Masonc8b97812008-10-29 14:49:59 -0400697 }
698 kfree(pages);
699 pages = NULL;
700 total_compressed = 0;
David Sterba4d3a8002017-02-14 19:04:07 +0100701 nr_pages = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400702
703 /* flag the file so we don't compress in the future */
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400704 if (!btrfs_test_opt(fs_info, FORCE_COMPRESS) &&
David Sterbab52aa8c2017-07-17 19:17:20 +0200705 !(BTRFS_I(inode)->prop_compress)) {
Chris Masona555f812010-01-28 16:18:15 -0500706 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
Chris Mason1e701a32010-03-11 09:42:04 -0500707 }
Chris Masonc8b97812008-10-29 14:49:59 -0400708 }
Chris Masonf03d9301f2009-02-04 09:31:06 -0500709cleanup_and_bail_uncompressed:
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700710 /*
711 * No compression, but we still need to write the pages in the file
712 * we've been given so far. redirty the locked page if it corresponds
713 * to our extent and set things up for the async work queue to run
714 * cow_file_range to do the normal delalloc dance.
715 */
Chris Mason1d53c9e2019-07-10 12:28:16 -0700716 if (async_chunk->locked_page &&
717 (page_offset(async_chunk->locked_page) >= start &&
718 page_offset(async_chunk->locked_page)) <= end) {
Nikolay Borisov1368c6d2019-03-12 17:20:27 +0200719 __set_page_dirty_nobuffers(async_chunk->locked_page);
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700720 /* unlocked later on in the async handlers */
Chris Mason1d53c9e2019-07-10 12:28:16 -0700721 }
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700722
723 if (redirty)
724 extent_range_redirty_for_io(inode, start, end);
Nikolay Borisovb5326272019-03-12 17:20:25 +0200725 add_async_extent(async_chunk, start, end - start + 1, 0, NULL, 0,
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700726 BTRFS_COMPRESS_NONE);
Nikolay Borisovac3e9932019-07-17 14:41:44 +0300727 compressed_extents++;
Chris Mason771ed682008-11-06 22:02:51 -0500728
Nikolay Borisovac3e9932019-07-17 14:41:44 +0300729 return compressed_extents;
Chris Mason771ed682008-11-06 22:02:51 -0500730}
Chris Mason771ed682008-11-06 22:02:51 -0500731
Filipe Manana40ae8372014-10-06 22:14:24 +0100732static void free_async_extent_pages(struct async_extent *async_extent)
733{
734 int i;
735
736 if (!async_extent->pages)
737 return;
738
739 for (i = 0; i < async_extent->nr_pages; i++) {
740 WARN_ON(async_extent->pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300741 put_page(async_extent->pages[i]);
Filipe Manana40ae8372014-10-06 22:14:24 +0100742 }
743 kfree(async_extent->pages);
744 async_extent->nr_pages = 0;
745 async_extent->pages = NULL;
Chris Mason771ed682008-11-06 22:02:51 -0500746}
747
748/*
749 * phase two of compressed writeback. This is the ordered portion
750 * of the code, which only gets called in the order the work was
751 * queued. We walk all the async extents created by compress_file_range
752 * and send them down to the disk.
753 */
Nikolay Borisovb5326272019-03-12 17:20:25 +0200754static noinline void submit_compressed_extents(struct async_chunk *async_chunk)
Chris Mason771ed682008-11-06 22:02:51 -0500755{
Nikolay Borisovb5326272019-03-12 17:20:25 +0200756 struct inode *inode = async_chunk->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400757 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason771ed682008-11-06 22:02:51 -0500758 struct async_extent *async_extent;
759 u64 alloc_hint = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500760 struct btrfs_key ins;
761 struct extent_map *em;
762 struct btrfs_root *root = BTRFS_I(inode)->root;
Nikolay Borisov43366502019-03-12 17:20:29 +0200763 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500764 int ret = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500765
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500766again:
Nikolay Borisovb5326272019-03-12 17:20:25 +0200767 while (!list_empty(&async_chunk->extents)) {
768 async_extent = list_entry(async_chunk->extents.next,
Chris Mason771ed682008-11-06 22:02:51 -0500769 struct async_extent, list);
770 list_del(&async_extent->list);
771
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500772retry:
Nikolay Borisov74475552019-03-12 17:20:30 +0200773 lock_extent(io_tree, async_extent->start,
774 async_extent->start + async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500775 /* did the compression code fall back to uncompressed IO? */
776 if (!async_extent->pages) {
777 int page_started = 0;
778 unsigned long nr_written = 0;
779
Chris Mason771ed682008-11-06 22:02:51 -0500780 /* allocate blocks */
Nikolay Borisovb5326272019-03-12 17:20:25 +0200781 ret = cow_file_range(inode, async_chunk->locked_page,
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500782 async_extent->start,
783 async_extent->start +
784 async_extent->ram_size - 1,
Nikolay Borisov330a5822019-07-17 16:18:17 +0300785 &page_started, &nr_written, 0);
Chris Mason771ed682008-11-06 22:02:51 -0500786
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100787 /* JDM XXX */
788
Chris Mason771ed682008-11-06 22:02:51 -0500789 /*
790 * if page_started, cow_file_range inserted an
791 * inline extent and took care of all the unlocking
792 * and IO for us. Otherwise, we need to submit
793 * all those pages down to the drive.
794 */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500795 if (!page_started && !ret)
Nikolay Borisov5e3ee232017-12-08 15:55:58 +0200796 extent_write_locked_range(inode,
797 async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500798 async_extent->start +
Chris Mason771ed682008-11-06 22:02:51 -0500799 async_extent->ram_size - 1,
Chris Mason771ed682008-11-06 22:02:51 -0500800 WB_SYNC_ALL);
Chris Mason1d53c9e2019-07-10 12:28:16 -0700801 else if (ret && async_chunk->locked_page)
Nikolay Borisovb5326272019-03-12 17:20:25 +0200802 unlock_page(async_chunk->locked_page);
Chris Mason771ed682008-11-06 22:02:51 -0500803 kfree(async_extent);
804 cond_resched();
805 continue;
806 }
807
Wang Xiaoguang18513092016-07-25 15:51:40 +0800808 ret = btrfs_reserve_extent(root, async_extent->ram_size,
Chris Mason771ed682008-11-06 22:02:51 -0500809 async_extent->compressed_size,
810 async_extent->compressed_size,
Miao Xiee570fd22014-06-19 10:42:50 +0800811 0, alloc_hint, &ins, 1, 1);
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500812 if (ret) {
Filipe Manana40ae8372014-10-06 22:14:24 +0100813 free_async_extent_pages(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500814
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400815 if (ret == -ENOSPC) {
816 unlock_extent(io_tree, async_extent->start,
817 async_extent->start +
818 async_extent->ram_size - 1);
Liu Boce620032014-07-24 22:48:05 +0800819
820 /*
821 * we need to redirty the pages if we decide to
822 * fallback to uncompressed IO, otherwise we
823 * will not submit these pages down to lower
824 * layers.
825 */
826 extent_range_redirty_for_io(inode,
827 async_extent->start,
828 async_extent->start +
829 async_extent->ram_size - 1);
830
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100831 goto retry;
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400832 }
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500833 goto out_free;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500834 }
Yan, Zhengc2167752009-11-12 09:34:21 +0000835 /*
836 * here we're doing allocation and writeback of the
837 * compressed pages
838 */
Liu Bo6f9994d2017-01-31 07:50:22 -0800839 em = create_io_em(inode, async_extent->start,
840 async_extent->ram_size, /* len */
841 async_extent->start, /* orig_start */
842 ins.objectid, /* block_start */
843 ins.offset, /* block_len */
844 ins.offset, /* orig_block_len */
845 async_extent->ram_size, /* ram_bytes */
846 async_extent->compress_type,
847 BTRFS_ORDERED_COMPRESSED);
848 if (IS_ERR(em))
849 /* ret value is not necessary due to void function */
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500850 goto out_free_reserve;
Liu Bo6f9994d2017-01-31 07:50:22 -0800851 free_extent_map(em);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500852
Li Zefan261507a02010-12-17 14:21:50 +0800853 ret = btrfs_add_ordered_extent_compress(inode,
854 async_extent->start,
855 ins.objectid,
856 async_extent->ram_size,
857 ins.offset,
858 BTRFS_ORDERED_COMPRESSED,
859 async_extent->compress_type);
Filipe Mananad9f85962014-08-25 10:43:00 +0100860 if (ret) {
Nikolay Borisovdcdbc052017-02-20 13:50:45 +0200861 btrfs_drop_extent_cache(BTRFS_I(inode),
862 async_extent->start,
Filipe Mananad9f85962014-08-25 10:43:00 +0100863 async_extent->start +
864 async_extent->ram_size - 1, 0);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500865 goto out_free_reserve;
Filipe Mananad9f85962014-08-25 10:43:00 +0100866 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400867 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Chris Mason771ed682008-11-06 22:02:51 -0500868
Chris Mason771ed682008-11-06 22:02:51 -0500869 /*
870 * clear dirty, set writeback and unlock the pages.
871 */
Josef Bacikc2790a22013-07-29 11:20:47 -0400872 extent_clear_unlock_delalloc(inode, async_extent->start,
Chris Masona791e352009-10-08 11:27:10 -0400873 async_extent->start +
874 async_extent->ram_size - 1,
Josef Bacik151a41b2013-07-29 13:22:24 -0400875 NULL, EXTENT_LOCKED | EXTENT_DELALLOC,
876 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -0400877 PAGE_SET_WRITEBACK);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200878 if (btrfs_submit_compressed_write(inode,
Chris Masond3977122009-01-05 21:25:51 -0500879 async_extent->start,
880 async_extent->ram_size,
881 ins.objectid,
882 ins.offset, async_extent->pages,
Liu Bof82b7352017-10-23 23:18:16 -0600883 async_extent->nr_pages,
Chris Masonec39f762019-07-10 12:28:17 -0700884 async_chunk->write_flags,
885 async_chunk->blkcg_css)) {
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100886 struct page *p = async_extent->pages[0];
887 const u64 start = async_extent->start;
888 const u64 end = start + async_extent->ram_size - 1;
889
890 p->mapping = inode->i_mapping;
Nikolay Borisovc6297322018-11-08 10:18:08 +0200891 btrfs_writepage_endio_finish_ordered(p, start, end, 0);
Nikolay Borisov7087a9d2018-11-01 14:09:48 +0200892
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100893 p->mapping = NULL;
Nikolay Borisov74e91942019-07-17 16:18:16 +0300894 extent_clear_unlock_delalloc(inode, start, end,
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800895 NULL, 0,
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100896 PAGE_END_WRITEBACK |
897 PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +0100898 free_async_extent_pages(async_extent);
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100899 }
Chris Mason771ed682008-11-06 22:02:51 -0500900 alloc_hint = ins.objectid + ins.offset;
901 kfree(async_extent);
902 cond_resched();
903 }
Filipe Mananadec8f172014-10-06 22:14:26 +0100904 return;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500905out_free_reserve:
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400906 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400907 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100908out_free:
Josef Bacikc2790a22013-07-29 11:20:47 -0400909 extent_clear_unlock_delalloc(inode, async_extent->start,
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500910 async_extent->start +
911 async_extent->ram_size - 1,
Josef Bacikc2790a22013-07-29 11:20:47 -0400912 NULL, EXTENT_LOCKED | EXTENT_DELALLOC |
Filipe Mananaa7e3b972017-04-03 10:45:46 +0100913 EXTENT_DELALLOC_NEW |
Josef Bacik151a41b2013-07-29 13:22:24 -0400914 EXTENT_DEFRAG | EXTENT_DO_ACCOUNTING,
915 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Filipe Manana704de492014-10-06 22:14:22 +0100916 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK |
917 PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +0100918 free_async_extent_pages(async_extent);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100919 kfree(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500920 goto again;
Chris Mason771ed682008-11-06 22:02:51 -0500921}
922
Josef Bacik4b46fce2010-05-23 11:00:55 -0400923static u64 get_extent_allocation_hint(struct inode *inode, u64 start,
924 u64 num_bytes)
925{
926 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
927 struct extent_map *em;
928 u64 alloc_hint = 0;
929
930 read_lock(&em_tree->lock);
931 em = search_extent_mapping(em_tree, start, num_bytes);
932 if (em) {
933 /*
934 * if block start isn't an actual block number then find the
935 * first block in this inode and use that as a hint. If that
936 * block is also bogus then just don't worry about it.
937 */
938 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
939 free_extent_map(em);
940 em = search_extent_mapping(em_tree, 0, 0);
941 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
942 alloc_hint = em->block_start;
943 if (em)
944 free_extent_map(em);
945 } else {
946 alloc_hint = em->block_start;
947 free_extent_map(em);
948 }
949 }
950 read_unlock(&em_tree->lock);
951
952 return alloc_hint;
953}
954
Chris Mason771ed682008-11-06 22:02:51 -0500955/*
956 * when extent_io.c finds a delayed allocation range in the file,
957 * the call backs end up in this code. The basic idea is to
958 * allocate extents on disk for the range, and create ordered data structs
959 * in ram to track those extents.
960 *
961 * locked_page is the page that writepage had locked already. We use
962 * it to make sure we don't do extra locks or unlocks.
963 *
964 * *page_started is set to one if we unlock locked_page and do everything
965 * required to start IO on it. It may be clean and already done with
966 * IO when we return.
967 */
Josef Bacik00361582013-08-14 14:02:47 -0400968static noinline int cow_file_range(struct inode *inode,
969 struct page *locked_page,
Nikolay Borisov74e91942019-07-17 16:18:16 +0300970 u64 start, u64 end, int *page_started,
Nikolay Borisov330a5822019-07-17 16:18:17 +0300971 unsigned long *nr_written, int unlock)
Chris Mason771ed682008-11-06 22:02:51 -0500972{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400973 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik00361582013-08-14 14:02:47 -0400974 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason771ed682008-11-06 22:02:51 -0500975 u64 alloc_hint = 0;
976 u64 num_bytes;
977 unsigned long ram_size;
Filipe Mananaa315e682017-03-06 23:04:20 +0000978 u64 cur_alloc_size = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400979 u64 blocksize = fs_info->sectorsize;
Chris Mason771ed682008-11-06 22:02:51 -0500980 struct btrfs_key ins;
981 struct extent_map *em;
Filipe Mananaa315e682017-03-06 23:04:20 +0000982 unsigned clear_bits;
983 unsigned long page_ops;
984 bool extent_reserved = false;
Chris Mason771ed682008-11-06 22:02:51 -0500985 int ret = 0;
986
Nikolay Borisov70ddc552017-02-20 13:50:35 +0200987 if (btrfs_is_free_space_inode(BTRFS_I(inode))) {
Josef Bacik02ecd2c2013-10-25 16:19:08 -0400988 WARN_ON_ONCE(1);
Josef Bacik29bce2f2014-02-07 12:21:23 -0500989 ret = -EINVAL;
990 goto out_unlock;
Josef Bacik02ecd2c2013-10-25 16:19:08 -0400991 }
Chris Mason771ed682008-11-06 22:02:51 -0500992
Qu Wenruofda28322013-02-26 08:10:22 +0000993 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Mason771ed682008-11-06 22:02:51 -0500994 num_bytes = max(blocksize, num_bytes);
Anand Jain566b1762018-02-15 18:07:59 +0800995 ASSERT(num_bytes <= btrfs_super_total_bytes(fs_info->super_copy));
Chris Mason771ed682008-11-06 22:02:51 -0500996
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200997 inode_should_defrag(BTRFS_I(inode), start, end, num_bytes, SZ_64K);
Chris Mason4cb53002011-05-24 15:35:30 -0400998
Chris Mason771ed682008-11-06 22:02:51 -0500999 if (start == 0) {
1000 /* lets try to make an inline extent */
Nikolay Borisovd02c0e22018-03-02 09:43:15 +02001001 ret = cow_file_range_inline(inode, start, end, 0,
1002 BTRFS_COMPRESS_NONE, NULL);
Chris Mason771ed682008-11-06 22:02:51 -05001003 if (ret == 0) {
Josef Bacik8b62f872017-10-19 14:15:55 -04001004 /*
1005 * We use DO_ACCOUNTING here because we need the
1006 * delalloc_release_metadata to be run _after_ we drop
1007 * our outstanding extent for clearing delalloc for this
1008 * range.
1009 */
Nikolay Borisov74e91942019-07-17 16:18:16 +03001010 extent_clear_unlock_delalloc(inode, start, end, NULL,
Josef Bacikc2790a22013-07-29 11:20:47 -04001011 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik8b62f872017-10-19 14:15:55 -04001012 EXTENT_DELALLOC_NEW | EXTENT_DEFRAG |
1013 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001014 PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
1015 PAGE_END_WRITEBACK);
Chris Mason771ed682008-11-06 22:02:51 -05001016 *nr_written = *nr_written +
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001017 (end - start + PAGE_SIZE) / PAGE_SIZE;
Chris Mason771ed682008-11-06 22:02:51 -05001018 *page_started = 1;
Chris Mason771ed682008-11-06 22:02:51 -05001019 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001020 } else if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001021 goto out_unlock;
Chris Mason771ed682008-11-06 22:02:51 -05001022 }
1023 }
Chris Masonc8b97812008-10-29 14:49:59 -04001024
Josef Bacik4b46fce2010-05-23 11:00:55 -04001025 alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02001026 btrfs_drop_extent_cache(BTRFS_I(inode), start,
1027 start + num_bytes - 1, 0);
Chris Mason3b951512008-04-17 11:29:12 -04001028
Anand Jain3752d222018-02-15 12:29:38 +08001029 while (num_bytes > 0) {
1030 cur_alloc_size = num_bytes;
Wang Xiaoguang18513092016-07-25 15:51:40 +08001031 ret = btrfs_reserve_extent(root, cur_alloc_size, cur_alloc_size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001032 fs_info->sectorsize, 0, alloc_hint,
Miao Xiee570fd22014-06-19 10:42:50 +08001033 &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04001034 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001035 goto out_unlock;
Filipe Mananaa315e682017-03-06 23:04:20 +00001036 cur_alloc_size = ins.offset;
1037 extent_reserved = true;
Chris Masond3977122009-01-05 21:25:51 -05001038
Chris Mason771ed682008-11-06 22:02:51 -05001039 ram_size = ins.offset;
Liu Bo6f9994d2017-01-31 07:50:22 -08001040 em = create_io_em(inode, start, ins.offset, /* len */
1041 start, /* orig_start */
1042 ins.objectid, /* block_start */
1043 ins.offset, /* block_len */
1044 ins.offset, /* orig_block_len */
1045 ram_size, /* ram_bytes */
1046 BTRFS_COMPRESS_NONE, /* compress_type */
Liu Bo1af4a0a2017-02-13 15:35:09 -08001047 BTRFS_ORDERED_REGULAR /* type */);
Su Yue090a127a2018-05-30 16:48:56 +08001048 if (IS_ERR(em)) {
1049 ret = PTR_ERR(em);
Liu Boace68ba2013-04-22 10:53:47 +00001050 goto out_reserve;
Su Yue090a127a2018-05-30 16:48:56 +08001051 }
Liu Bo6f9994d2017-01-31 07:50:22 -08001052 free_extent_map(em);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001053
Chris Masone6dcd2d2008-07-17 12:53:50 -04001054 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
Chris Mason771ed682008-11-06 22:02:51 -05001055 ram_size, cur_alloc_size, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001056 if (ret)
Filipe Mananad9f85962014-08-25 10:43:00 +01001057 goto out_drop_extent_cache;
Chris Masonc8b97812008-10-29 14:49:59 -04001058
Yan Zheng17d217f2008-12-12 10:03:38 -05001059 if (root->root_key.objectid ==
1060 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1061 ret = btrfs_reloc_clone_csums(inode, start,
1062 cur_alloc_size);
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001063 /*
1064 * Only drop cache here, and process as normal.
1065 *
1066 * We must not allow extent_clear_unlock_delalloc()
1067 * at out_unlock label to free meta of this ordered
1068 * extent, as its meta should be freed by
1069 * btrfs_finish_ordered_io().
1070 *
1071 * So we must continue until @start is increased to
1072 * skip current ordered extent.
1073 */
Josef Bacik00361582013-08-14 14:02:47 -04001074 if (ret)
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001075 btrfs_drop_extent_cache(BTRFS_I(inode), start,
1076 start + ram_size - 1, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001077 }
1078
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001079 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana9cfa3e32016-04-26 15:39:32 +01001080
Chris Masonc8b97812008-10-29 14:49:59 -04001081 /* we're not doing compressed IO, don't unlock the first
1082 * page (which the caller expects to stay locked), don't
1083 * clear any dirty bits and don't set any writeback bits
Chris Mason8b62b722009-09-02 16:53:46 -04001084 *
1085 * Do set the Private2 bit so we know this page was properly
1086 * setup for writepage
Chris Masonc8b97812008-10-29 14:49:59 -04001087 */
Filipe Mananaa315e682017-03-06 23:04:20 +00001088 page_ops = unlock ? PAGE_UNLOCK : 0;
1089 page_ops |= PAGE_SET_PRIVATE2;
Chris Masona791e352009-10-08 11:27:10 -04001090
Josef Bacikc2790a22013-07-29 11:20:47 -04001091 extent_clear_unlock_delalloc(inode, start,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001092 start + ram_size - 1,
Nikolay Borisov74e91942019-07-17 16:18:16 +03001093 locked_page,
Josef Bacikc2790a22013-07-29 11:20:47 -04001094 EXTENT_LOCKED | EXTENT_DELALLOC,
Filipe Mananaa315e682017-03-06 23:04:20 +00001095 page_ops);
Anand Jain3752d222018-02-15 12:29:38 +08001096 if (num_bytes < cur_alloc_size)
1097 num_bytes = 0;
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001098 else
Anand Jain3752d222018-02-15 12:29:38 +08001099 num_bytes -= cur_alloc_size;
Chris Masonc59f8952007-12-17 20:14:04 -05001100 alloc_hint = ins.objectid + ins.offset;
1101 start += cur_alloc_size;
Filipe Mananaa315e682017-03-06 23:04:20 +00001102 extent_reserved = false;
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001103
1104 /*
1105 * btrfs_reloc_clone_csums() error, since start is increased
1106 * extent_clear_unlock_delalloc() at out_unlock label won't
1107 * free metadata of current ordered extent, we're OK to exit.
1108 */
1109 if (ret)
1110 goto out_unlock;
Chris Masonb888db22007-08-27 16:49:44 -04001111 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001112out:
Chris Masonbe20aa92007-12-17 20:14:01 -05001113 return ret;
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001114
Filipe Mananad9f85962014-08-25 10:43:00 +01001115out_drop_extent_cache:
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02001116 btrfs_drop_extent_cache(BTRFS_I(inode), start, start + ram_size - 1, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001117out_reserve:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001118 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001119 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001120out_unlock:
Filipe Mananaa7e3b972017-04-03 10:45:46 +01001121 clear_bits = EXTENT_LOCKED | EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
1122 EXTENT_DEFRAG | EXTENT_CLEAR_META_RESV;
Filipe Mananaa315e682017-03-06 23:04:20 +00001123 page_ops = PAGE_UNLOCK | PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
1124 PAGE_END_WRITEBACK;
1125 /*
1126 * If we reserved an extent for our delalloc range (or a subrange) and
1127 * failed to create the respective ordered extent, then it means that
1128 * when we reserved the extent we decremented the extent's size from
1129 * the data space_info's bytes_may_use counter and incremented the
1130 * space_info's bytes_reserved counter by the same amount. We must make
1131 * sure extent_clear_unlock_delalloc() does not try to decrement again
1132 * the data space_info's bytes_may_use counter, therefore we do not pass
1133 * it the flag EXTENT_CLEAR_DATA_RESV.
1134 */
1135 if (extent_reserved) {
1136 extent_clear_unlock_delalloc(inode, start,
1137 start + cur_alloc_size,
Filipe Mananaa315e682017-03-06 23:04:20 +00001138 locked_page,
1139 clear_bits,
1140 page_ops);
1141 start += cur_alloc_size;
1142 if (start >= end)
1143 goto out;
1144 }
Nikolay Borisov74e91942019-07-17 16:18:16 +03001145 extent_clear_unlock_delalloc(inode, start, end, locked_page,
Filipe Mananaa315e682017-03-06 23:04:20 +00001146 clear_bits | EXTENT_CLEAR_DATA_RESV,
1147 page_ops);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001148 goto out;
Chris Mason771ed682008-11-06 22:02:51 -05001149}
Chris Masonc8b97812008-10-29 14:49:59 -04001150
Chris Mason771ed682008-11-06 22:02:51 -05001151/*
1152 * work queue call back to started compression on a file and pages
1153 */
1154static noinline void async_cow_start(struct btrfs_work *work)
1155{
Nikolay Borisovb5326272019-03-12 17:20:25 +02001156 struct async_chunk *async_chunk;
Nikolay Borisovac3e9932019-07-17 14:41:44 +03001157 int compressed_extents;
Chris Mason771ed682008-11-06 22:02:51 -05001158
Nikolay Borisovb5326272019-03-12 17:20:25 +02001159 async_chunk = container_of(work, struct async_chunk, work);
Chris Mason771ed682008-11-06 22:02:51 -05001160
Nikolay Borisovac3e9932019-07-17 14:41:44 +03001161 compressed_extents = compress_file_range(async_chunk);
1162 if (compressed_extents == 0) {
Nikolay Borisovb5326272019-03-12 17:20:25 +02001163 btrfs_add_delayed_iput(async_chunk->inode);
1164 async_chunk->inode = NULL;
Josef Bacik8180ef82012-06-08 15:16:12 -04001165 }
Chris Mason771ed682008-11-06 22:02:51 -05001166}
1167
1168/*
1169 * work queue call back to submit previously compressed pages
1170 */
1171static noinline void async_cow_submit(struct btrfs_work *work)
1172{
Nikolay Borisovc5a68ae2019-03-12 17:20:26 +02001173 struct async_chunk *async_chunk = container_of(work, struct async_chunk,
1174 work);
1175 struct btrfs_fs_info *fs_info = btrfs_work_owner(work);
Chris Mason771ed682008-11-06 22:02:51 -05001176 unsigned long nr_pages;
1177
Nikolay Borisovb5326272019-03-12 17:20:25 +02001178 nr_pages = (async_chunk->end - async_chunk->start + PAGE_SIZE) >>
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001179 PAGE_SHIFT;
Chris Mason771ed682008-11-06 22:02:51 -05001180
David Sterba093258e2018-02-26 16:15:17 +01001181 /* atomic_sub_return implies a barrier */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001182 if (atomic_sub_return(nr_pages, &fs_info->async_delalloc_pages) <
David Sterba093258e2018-02-26 16:15:17 +01001183 5 * SZ_1M)
1184 cond_wake_up_nomb(&fs_info->async_submit_wait);
Chris Mason771ed682008-11-06 22:02:51 -05001185
Nikolay Borisov4546d172019-01-03 10:50:03 +02001186 /*
Nikolay Borisovb5326272019-03-12 17:20:25 +02001187 * ->inode could be NULL if async_chunk_start has failed to compress,
Nikolay Borisov4546d172019-01-03 10:50:03 +02001188 * in which case we don't have anything to submit, yet we need to
1189 * always adjust ->async_delalloc_pages as its paired with the init
1190 * happening in cow_file_range_async
1191 */
Nikolay Borisovb5326272019-03-12 17:20:25 +02001192 if (async_chunk->inode)
1193 submit_compressed_extents(async_chunk);
Chris Mason771ed682008-11-06 22:02:51 -05001194}
Chris Masonc8b97812008-10-29 14:49:59 -04001195
Chris Mason771ed682008-11-06 22:02:51 -05001196static noinline void async_cow_free(struct btrfs_work *work)
1197{
Nikolay Borisovb5326272019-03-12 17:20:25 +02001198 struct async_chunk *async_chunk;
Nikolay Borisov97db1202019-03-12 17:20:24 +02001199
Nikolay Borisovb5326272019-03-12 17:20:25 +02001200 async_chunk = container_of(work, struct async_chunk, work);
1201 if (async_chunk->inode)
1202 btrfs_add_delayed_iput(async_chunk->inode);
Chris Masonec39f762019-07-10 12:28:17 -07001203 if (async_chunk->blkcg_css)
1204 css_put(async_chunk->blkcg_css);
Nikolay Borisov97db1202019-03-12 17:20:24 +02001205 /*
1206 * Since the pointer to 'pending' is at the beginning of the array of
Nikolay Borisovb5326272019-03-12 17:20:25 +02001207 * async_chunk's, freeing it ensures the whole array has been freed.
Nikolay Borisov97db1202019-03-12 17:20:24 +02001208 */
Nikolay Borisovb5326272019-03-12 17:20:25 +02001209 if (atomic_dec_and_test(async_chunk->pending))
Nikolay Borisovb1c16ac2019-04-01 11:29:57 +03001210 kvfree(async_chunk->pending);
Chris Mason771ed682008-11-06 22:02:51 -05001211}
1212
Chris Masonec39f762019-07-10 12:28:17 -07001213static int cow_file_range_async(struct inode *inode,
1214 struct writeback_control *wbc,
1215 struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001216 u64 start, u64 end, int *page_started,
Liu Bof82b7352017-10-23 23:18:16 -06001217 unsigned long *nr_written,
1218 unsigned int write_flags)
Chris Mason771ed682008-11-06 22:02:51 -05001219{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001220 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masonec39f762019-07-10 12:28:17 -07001221 struct cgroup_subsys_state *blkcg_css = wbc_blkcg_css(wbc);
Nikolay Borisov97db1202019-03-12 17:20:24 +02001222 struct async_cow *ctx;
1223 struct async_chunk *async_chunk;
Chris Mason771ed682008-11-06 22:02:51 -05001224 unsigned long nr_pages;
1225 u64 cur_end;
Nikolay Borisov97db1202019-03-12 17:20:24 +02001226 u64 num_chunks = DIV_ROUND_UP(end - start, SZ_512K);
1227 int i;
1228 bool should_compress;
Nikolay Borisovb1c16ac2019-04-01 11:29:57 +03001229 unsigned nofs_flag;
Chris Mason771ed682008-11-06 22:02:51 -05001230
Nikolay Borisov69684c52019-03-12 17:20:28 +02001231 unlock_extent(&BTRFS_I(inode)->io_tree, start, end);
Nikolay Borisov97db1202019-03-12 17:20:24 +02001232
1233 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS &&
1234 !btrfs_test_opt(fs_info, FORCE_COMPRESS)) {
1235 num_chunks = 1;
1236 should_compress = false;
1237 } else {
1238 should_compress = true;
1239 }
1240
Nikolay Borisovb1c16ac2019-04-01 11:29:57 +03001241 nofs_flag = memalloc_nofs_save();
1242 ctx = kvmalloc(struct_size(ctx, chunks, num_chunks), GFP_KERNEL);
1243 memalloc_nofs_restore(nofs_flag);
1244
Nikolay Borisov97db1202019-03-12 17:20:24 +02001245 if (!ctx) {
1246 unsigned clear_bits = EXTENT_LOCKED | EXTENT_DELALLOC |
1247 EXTENT_DELALLOC_NEW | EXTENT_DEFRAG |
1248 EXTENT_DO_ACCOUNTING;
1249 unsigned long page_ops = PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
1250 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK |
1251 PAGE_SET_ERROR;
1252
Nikolay Borisov74e91942019-07-17 16:18:16 +03001253 extent_clear_unlock_delalloc(inode, start, end, locked_page,
Nikolay Borisov97db1202019-03-12 17:20:24 +02001254 clear_bits, page_ops);
1255 return -ENOMEM;
1256 }
1257
1258 async_chunk = ctx->chunks;
1259 atomic_set(&ctx->num_chunks, num_chunks);
1260
1261 for (i = 0; i < num_chunks; i++) {
1262 if (should_compress)
1263 cur_end = min(end, start + SZ_512K - 1);
1264 else
1265 cur_end = end;
1266
Nikolay Borisovbd4691a2019-01-03 10:50:01 +02001267 /*
1268 * igrab is called higher up in the call chain, take only the
1269 * lightweight reference for the callback lifetime
1270 */
1271 ihold(inode);
Nikolay Borisov97db1202019-03-12 17:20:24 +02001272 async_chunk[i].pending = &ctx->num_chunks;
1273 async_chunk[i].inode = inode;
1274 async_chunk[i].start = start;
1275 async_chunk[i].end = cur_end;
Nikolay Borisov97db1202019-03-12 17:20:24 +02001276 async_chunk[i].write_flags = write_flags;
1277 INIT_LIST_HEAD(&async_chunk[i].extents);
Chris Mason771ed682008-11-06 22:02:51 -05001278
Chris Mason1d53c9e2019-07-10 12:28:16 -07001279 /*
1280 * The locked_page comes all the way from writepage and its
1281 * the original page we were actually given. As we spread
1282 * this large delalloc region across multiple async_chunk
1283 * structs, only the first struct needs a pointer to locked_page
1284 *
1285 * This way we don't need racey decisions about who is supposed
1286 * to unlock it.
1287 */
1288 if (locked_page) {
Chris Masonec39f762019-07-10 12:28:17 -07001289 /*
1290 * Depending on the compressibility, the pages might or
1291 * might not go through async. We want all of them to
1292 * be accounted against wbc once. Let's do it here
1293 * before the paths diverge. wbc accounting is used
1294 * only for foreign writeback detection and doesn't
1295 * need full accuracy. Just account the whole thing
1296 * against the first page.
1297 */
1298 wbc_account_cgroup_owner(wbc, locked_page,
1299 cur_end - start);
Chris Mason1d53c9e2019-07-10 12:28:16 -07001300 async_chunk[i].locked_page = locked_page;
1301 locked_page = NULL;
1302 } else {
1303 async_chunk[i].locked_page = NULL;
1304 }
1305
Chris Masonec39f762019-07-10 12:28:17 -07001306 if (blkcg_css != blkcg_root_css) {
1307 css_get(blkcg_css);
1308 async_chunk[i].blkcg_css = blkcg_css;
1309 } else {
1310 async_chunk[i].blkcg_css = NULL;
1311 }
1312
Omar Sandovala0cac0e2019-09-16 11:30:57 -07001313 btrfs_init_work(&async_chunk[i].work, async_cow_start,
1314 async_cow_submit, async_cow_free);
Chris Mason771ed682008-11-06 22:02:51 -05001315
Nikolay Borisov97db1202019-03-12 17:20:24 +02001316 nr_pages = DIV_ROUND_UP(cur_end - start, PAGE_SIZE);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001317 atomic_add(nr_pages, &fs_info->async_delalloc_pages);
Chris Mason771ed682008-11-06 22:02:51 -05001318
Nikolay Borisov97db1202019-03-12 17:20:24 +02001319 btrfs_queue_work(fs_info->delalloc_workers, &async_chunk[i].work);
Chris Mason771ed682008-11-06 22:02:51 -05001320
Chris Mason771ed682008-11-06 22:02:51 -05001321 *nr_written += nr_pages;
1322 start = cur_end + 1;
1323 }
1324 *page_started = 1;
1325 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001326}
1327
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001328static noinline int csum_exist_in_range(struct btrfs_fs_info *fs_info,
Yan Zheng17d217f2008-12-12 10:03:38 -05001329 u64 bytenr, u64 num_bytes)
1330{
1331 int ret;
1332 struct btrfs_ordered_sum *sums;
1333 LIST_HEAD(list);
1334
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001335 ret = btrfs_lookup_csums_range(fs_info->csum_root, bytenr,
Arne Jansena2de7332011-03-08 14:14:00 +01001336 bytenr + num_bytes - 1, &list, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001337 if (ret == 0 && list_empty(&list))
1338 return 0;
1339
1340 while (!list_empty(&list)) {
1341 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1342 list_del(&sums->list);
1343 kfree(sums);
1344 }
Liu Bo58113752018-01-31 17:09:13 -07001345 if (ret < 0)
1346 return ret;
Yan Zheng17d217f2008-12-12 10:03:38 -05001347 return 1;
1348}
1349
Chris Masond352ac62008-09-29 15:18:18 -04001350/*
1351 * when nowcow writeback call back. This checks for snapshots or COW copies
1352 * of the extents that exist in the file, and COWs the file as required.
1353 *
1354 * If no cow copies or snapshots exist, we write directly to the existing
1355 * blocks on disk
1356 */
Chris Mason7f366cf2009-03-12 20:12:45 -04001357static noinline int run_delalloc_nocow(struct inode *inode,
1358 struct page *locked_page,
Nikolay Borisov3e024842019-08-21 10:42:03 +03001359 const u64 start, const u64 end,
1360 int *page_started, int force,
1361 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001362{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001363 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masonbe20aa92007-12-17 20:14:01 -05001364 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masonbe20aa92007-12-17 20:14:01 -05001365 struct btrfs_path *path;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001366 u64 cow_start = (u64)-1;
1367 u64 cur_offset = start;
Robbie Ko8ecebf4d2018-08-06 10:30:30 +08001368 int ret;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001369 bool check_prev = true;
1370 const bool freespace_inode = btrfs_is_free_space_inode(BTRFS_I(inode));
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02001371 u64 ino = btrfs_ino(BTRFS_I(inode));
Nikolay Borisov762bf092019-08-22 17:24:20 +03001372 bool nocow = false;
1373 u64 disk_bytenr = 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001374
1375 path = btrfs_alloc_path();
Josef Bacik17ca04a2012-05-31 15:58:55 -04001376 if (!path) {
Nikolay Borisov74e91942019-07-17 16:18:16 +03001377 extent_clear_unlock_delalloc(inode, start, end, locked_page,
Josef Bacikc2790a22013-07-29 11:20:47 -04001378 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -04001379 EXTENT_DO_ACCOUNTING |
1380 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001381 PAGE_CLEAR_DIRTY |
1382 PAGE_SET_WRITEBACK |
1383 PAGE_END_WRITEBACK);
Mark Fashehd8926bb2011-07-13 10:38:47 -07001384 return -ENOMEM;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001385 }
Li Zefan82d59022011-04-20 10:33:24 +08001386
Yan Zheng80ff3852008-10-30 14:20:02 -04001387 while (1) {
Nikolay Borisov3e024842019-08-21 10:42:03 +03001388 struct btrfs_key found_key;
1389 struct btrfs_file_extent_item *fi;
1390 struct extent_buffer *leaf;
1391 u64 extent_end;
1392 u64 extent_offset;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001393 u64 num_bytes = 0;
1394 u64 disk_num_bytes;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001395 u64 ram_bytes;
1396 int extent_type;
Nikolay Borisov762bf092019-08-22 17:24:20 +03001397
1398 nocow = false;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001399
Liu Boe4c3b2d2017-01-30 12:25:28 -08001400 ret = btrfs_lookup_file_extent(NULL, root, path, ino,
Yan Zheng80ff3852008-10-30 14:20:02 -04001401 cur_offset, 0);
Josef Bacikd788a342013-10-25 16:55:08 -04001402 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001403 goto error;
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001404
1405 /*
1406 * If there is no extent for our range when doing the initial
1407 * search, then go back to the previous slot as it will be the
1408 * one containing the search offset
1409 */
Yan Zheng80ff3852008-10-30 14:20:02 -04001410 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1411 leaf = path->nodes[0];
1412 btrfs_item_key_to_cpu(leaf, &found_key,
1413 path->slots[0] - 1);
Li Zefan33345d012011-04-20 10:31:50 +08001414 if (found_key.objectid == ino &&
Yan Zheng80ff3852008-10-30 14:20:02 -04001415 found_key.type == BTRFS_EXTENT_DATA_KEY)
1416 path->slots[0]--;
1417 }
Nikolay Borisov3e024842019-08-21 10:42:03 +03001418 check_prev = false;
Yan Zheng80ff3852008-10-30 14:20:02 -04001419next_slot:
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001420 /* Go to next leaf if we have exhausted the current one */
Yan Zheng80ff3852008-10-30 14:20:02 -04001421 leaf = path->nodes[0];
1422 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1423 ret = btrfs_next_leaf(root, path);
Liu Boe8916692018-01-25 11:02:50 -07001424 if (ret < 0) {
1425 if (cow_start != (u64)-1)
1426 cur_offset = cow_start;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001427 goto error;
Liu Boe8916692018-01-25 11:02:50 -07001428 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001429 if (ret > 0)
1430 break;
1431 leaf = path->nodes[0];
1432 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001433
Yan Zheng80ff3852008-10-30 14:20:02 -04001434 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001435
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001436 /* Didn't find anything for our INO */
Filipe Manana1d512cb2015-11-09 00:33:58 +00001437 if (found_key.objectid > ino)
1438 break;
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001439 /*
1440 * Keep searching until we find an EXTENT_ITEM or there are no
1441 * more extents for this inode
1442 */
Filipe Manana1d512cb2015-11-09 00:33:58 +00001443 if (WARN_ON_ONCE(found_key.objectid < ino) ||
1444 found_key.type < BTRFS_EXTENT_DATA_KEY) {
1445 path->slots[0]++;
1446 goto next_slot;
1447 }
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001448
1449 /* Found key is not EXTENT_DATA_KEY or starts after req range */
Filipe Manana1d512cb2015-11-09 00:33:58 +00001450 if (found_key.type > BTRFS_EXTENT_DATA_KEY ||
Yan Zheng80ff3852008-10-30 14:20:02 -04001451 found_key.offset > end)
1452 break;
Chris Masonbe20aa92007-12-17 20:14:01 -05001453
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001454 /*
1455 * If the found extent starts after requested offset, then
1456 * adjust extent_end to be right before this extent begins
1457 */
Yan Zheng80ff3852008-10-30 14:20:02 -04001458 if (found_key.offset > cur_offset) {
1459 extent_end = found_key.offset;
Chris Masone9061e22009-10-09 09:57:45 -04001460 extent_type = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001461 goto out_check;
1462 }
Chris Masonc31f8832008-01-08 15:46:31 -05001463
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001464 /*
1465 * Found extent which begins before our range and potentially
1466 * intersect it
1467 */
Yan Zheng80ff3852008-10-30 14:20:02 -04001468 fi = btrfs_item_ptr(leaf, path->slots[0],
1469 struct btrfs_file_extent_item);
1470 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001471
Josef Bacikcc95bef2013-04-04 14:31:27 -04001472 ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
Yan Zhengd899e052008-10-30 14:25:28 -04001473 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1474 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001475 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001476 extent_offset = btrfs_file_extent_offset(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001477 extent_end = found_key.offset +
1478 btrfs_file_extent_num_bytes(leaf, fi);
Josef Bacikb4939682012-12-03 10:31:19 -05001479 disk_num_bytes =
1480 btrfs_file_extent_disk_num_bytes(leaf, fi);
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001481 /*
1482 * If extent we got ends before our range starts, skip
1483 * to next extent
1484 */
Yan Zheng80ff3852008-10-30 14:20:02 -04001485 if (extent_end <= start) {
1486 path->slots[0]++;
1487 goto next_slot;
1488 }
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001489 /* Skip holes */
Yan Zheng17d217f2008-12-12 10:03:38 -05001490 if (disk_bytenr == 0)
1491 goto out_check;
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001492 /* Skip compressed/encrypted/encoded extents */
Yan Zheng80ff3852008-10-30 14:20:02 -04001493 if (btrfs_file_extent_compression(leaf, fi) ||
1494 btrfs_file_extent_encryption(leaf, fi) ||
1495 btrfs_file_extent_other_encoding(leaf, fi))
1496 goto out_check;
Ethan Lien78d42952018-05-17 14:58:29 +08001497 /*
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001498 * If extent is created before the last volume's snapshot
1499 * this implies the extent is shared, hence we can't do
1500 * nocow. This is the same check as in
1501 * btrfs_cross_ref_exist but without calling
1502 * btrfs_search_slot.
Ethan Lien78d42952018-05-17 14:58:29 +08001503 */
Nikolay Borisov3e024842019-08-21 10:42:03 +03001504 if (!freespace_inode &&
Lu Fengqi27a7ff52018-11-29 17:31:32 +08001505 btrfs_file_extent_generation(leaf, fi) <=
Ethan Lien78d42952018-05-17 14:58:29 +08001506 btrfs_root_last_snapshot(&root->root_item))
1507 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001508 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1509 goto out_check;
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001510 /* If extent is RO, we must COW it */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001511 if (btrfs_extent_readonly(fs_info, disk_bytenr))
Yan Zheng80ff3852008-10-30 14:20:02 -04001512 goto out_check;
Liu Bo58113752018-01-31 17:09:13 -07001513 ret = btrfs_cross_ref_exist(root, ino,
1514 found_key.offset -
1515 extent_offset, disk_bytenr);
1516 if (ret) {
1517 /*
1518 * ret could be -EIO if the above fails to read
1519 * metadata.
1520 */
1521 if (ret < 0) {
1522 if (cow_start != (u64)-1)
1523 cur_offset = cow_start;
1524 goto error;
1525 }
1526
Nikolay Borisov3e024842019-08-21 10:42:03 +03001527 WARN_ON_ONCE(freespace_inode);
Yan Zheng17d217f2008-12-12 10:03:38 -05001528 goto out_check;
Liu Bo58113752018-01-31 17:09:13 -07001529 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001530 disk_bytenr += extent_offset;
Yan Zheng17d217f2008-12-12 10:03:38 -05001531 disk_bytenr += cur_offset - found_key.offset;
1532 num_bytes = min(end + 1, extent_end) - cur_offset;
1533 /*
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001534 * If there are pending snapshots for this root, we
1535 * fall into common COW way
Wang Shilonge9894fd2014-03-27 11:12:25 +08001536 */
Nikolay Borisov3e024842019-08-21 10:42:03 +03001537 if (!freespace_inode && atomic_read(&root->snapshot_force_cow))
Robbie Ko8ecebf4d2018-08-06 10:30:30 +08001538 goto out_check;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001539 /*
Yan Zheng17d217f2008-12-12 10:03:38 -05001540 * force cow if csum exists in the range.
1541 * this ensure that csum for a given extent are
1542 * either valid or do not exist.
1543 */
Liu Bo58113752018-01-31 17:09:13 -07001544 ret = csum_exist_in_range(fs_info, disk_bytenr,
1545 num_bytes);
1546 if (ret) {
Liu Bo58113752018-01-31 17:09:13 -07001547 /*
1548 * ret could be -EIO if the above fails to read
1549 * metadata.
1550 */
1551 if (ret < 0) {
1552 if (cow_start != (u64)-1)
1553 cur_offset = cow_start;
1554 goto error;
1555 }
Nikolay Borisov3e024842019-08-21 10:42:03 +03001556 WARN_ON_ONCE(freespace_inode);
Yan Zheng17d217f2008-12-12 10:03:38 -05001557 goto out_check;
Robbie Ko91e1f562016-10-07 10:01:29 +08001558 }
Robbie Ko8ecebf4d2018-08-06 10:30:30 +08001559 if (!btrfs_inc_nocow_writers(fs_info, disk_bytenr))
Filipe Mananaf78c4362016-05-09 13:15:41 +01001560 goto out_check;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001561 nocow = true;
Yan Zheng80ff3852008-10-30 14:20:02 -04001562 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Nikolay Borisove8e21002019-08-22 17:25:23 +03001563 extent_end = found_key.offset + ram_bytes;
1564 extent_end = ALIGN(extent_end, fs_info->sectorsize);
Nikolay Borisov922f0512019-08-05 17:47:06 +03001565 /* Skip extents outside of our requested range */
1566 if (extent_end <= start) {
1567 path->slots[0]++;
1568 goto next_slot;
1569 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001570 } else {
Nikolay Borisove8e21002019-08-22 17:25:23 +03001571 /* If this triggers then we have a memory corruption */
Arnd Bergmann290342f2019-03-25 14:02:25 +01001572 BUG();
Yan Zheng80ff3852008-10-30 14:20:02 -04001573 }
1574out_check:
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001575 /*
1576 * If nocow is false then record the beginning of the range
1577 * that needs to be COWed
1578 */
Yan Zheng80ff3852008-10-30 14:20:02 -04001579 if (!nocow) {
1580 if (cow_start == (u64)-1)
1581 cow_start = cur_offset;
1582 cur_offset = extent_end;
1583 if (cur_offset > end)
1584 break;
1585 path->slots[0]++;
1586 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001587 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001588
David Sterbab3b4aa72011-04-21 01:20:15 +02001589 btrfs_release_path(path);
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001590
1591 /*
1592 * COW range from cow_start to found_key.offset - 1. As the key
1593 * will contain the beginning of the first extent that can be
1594 * NOCOW, following one which needs to be COW'ed
1595 */
Yan Zheng80ff3852008-10-30 14:20:02 -04001596 if (cow_start != (u64)-1) {
Josef Bacik00361582013-08-14 14:02:47 -04001597 ret = cow_file_range(inode, locked_page,
1598 cow_start, found_key.offset - 1,
Nikolay Borisov330a5822019-07-17 16:18:17 +03001599 page_started, nr_written, 1);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001600 if (ret) {
Filipe Mananaf78c4362016-05-09 13:15:41 +01001601 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001602 btrfs_dec_nocow_writers(fs_info,
Filipe Mananaf78c4362016-05-09 13:15:41 +01001603 disk_bytenr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001604 goto error;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001605 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001606 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001607 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001608
Yan Zhengd899e052008-10-30 14:25:28 -04001609 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Liu Bo6f9994d2017-01-31 07:50:22 -08001610 u64 orig_start = found_key.offset - extent_offset;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001611 struct extent_map *em;
Liu Bo6f9994d2017-01-31 07:50:22 -08001612
1613 em = create_io_em(inode, cur_offset, num_bytes,
1614 orig_start,
1615 disk_bytenr, /* block_start */
1616 num_bytes, /* block_len */
1617 disk_num_bytes, /* orig_block_len */
1618 ram_bytes, BTRFS_COMPRESS_NONE,
1619 BTRFS_ORDERED_PREALLOC);
1620 if (IS_ERR(em)) {
Liu Bo6f9994d2017-01-31 07:50:22 -08001621 if (nocow)
1622 btrfs_dec_nocow_writers(fs_info,
1623 disk_bytenr);
1624 ret = PTR_ERR(em);
1625 goto error;
Yan Zhengd899e052008-10-30 14:25:28 -04001626 }
Liu Bo6f9994d2017-01-31 07:50:22 -08001627 free_extent_map(em);
Nikolay Borisovbb55f622019-08-05 17:47:05 +03001628 ret = btrfs_add_ordered_extent(inode, cur_offset,
1629 disk_bytenr, num_bytes,
1630 num_bytes,
1631 BTRFS_ORDERED_PREALLOC);
Nikolay Borisov762bf092019-08-22 17:24:20 +03001632 if (ret) {
1633 btrfs_drop_extent_cache(BTRFS_I(inode),
1634 cur_offset,
1635 cur_offset + num_bytes - 1,
1636 0);
1637 goto error;
1638 }
Yan Zhengd899e052008-10-30 14:25:28 -04001639 } else {
Nikolay Borisovbb55f622019-08-05 17:47:05 +03001640 ret = btrfs_add_ordered_extent(inode, cur_offset,
1641 disk_bytenr, num_bytes,
1642 num_bytes,
1643 BTRFS_ORDERED_NOCOW);
Nikolay Borisov762bf092019-08-22 17:24:20 +03001644 if (ret)
1645 goto error;
Yan Zhengd899e052008-10-30 14:25:28 -04001646 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001647
Filipe Mananaf78c4362016-05-09 13:15:41 +01001648 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001649 btrfs_dec_nocow_writers(fs_info, disk_bytenr);
Nikolay Borisov762bf092019-08-22 17:24:20 +03001650 nocow = false;
Chris Mason771ed682008-11-06 22:02:51 -05001651
Yan, Zhengefa56462010-05-16 10:49:59 -04001652 if (root->root_key.objectid ==
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001653 BTRFS_DATA_RELOC_TREE_OBJECTID)
1654 /*
1655 * Error handled later, as we must prevent
1656 * extent_clear_unlock_delalloc() in error handler
1657 * from freeing metadata of created ordered extent.
1658 */
Yan, Zhengefa56462010-05-16 10:49:59 -04001659 ret = btrfs_reloc_clone_csums(inode, cur_offset,
1660 num_bytes);
Yan, Zhengefa56462010-05-16 10:49:59 -04001661
Josef Bacikc2790a22013-07-29 11:20:47 -04001662 extent_clear_unlock_delalloc(inode, cur_offset,
Nikolay Borisov74e91942019-07-17 16:18:16 +03001663 cur_offset + num_bytes - 1,
Josef Bacikc2790a22013-07-29 11:20:47 -04001664 locked_page, EXTENT_LOCKED |
Wang Xiaoguang18513092016-07-25 15:51:40 +08001665 EXTENT_DELALLOC |
1666 EXTENT_CLEAR_DATA_RESV,
1667 PAGE_UNLOCK | PAGE_SET_PRIVATE2);
1668
Yan Zheng80ff3852008-10-30 14:20:02 -04001669 cur_offset = extent_end;
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001670
1671 /*
1672 * btrfs_reloc_clone_csums() error, now we're OK to call error
1673 * handler, as metadata for created ordered extent will only
1674 * be freed by btrfs_finish_ordered_io().
1675 */
1676 if (ret)
1677 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001678 if (cur_offset > end)
1679 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001680 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001681 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001682
Robbie Ko506481b2018-10-30 18:04:04 +08001683 if (cur_offset <= end && cow_start == (u64)-1)
Yan Zheng80ff3852008-10-30 14:20:02 -04001684 cow_start = cur_offset;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001685
Yan Zheng80ff3852008-10-30 14:20:02 -04001686 if (cow_start != (u64)-1) {
Robbie Ko506481b2018-10-30 18:04:04 +08001687 cur_offset = end;
Nikolay Borisov74e91942019-07-17 16:18:16 +03001688 ret = cow_file_range(inode, locked_page, cow_start, end,
Nikolay Borisov330a5822019-07-17 16:18:17 +03001689 page_started, nr_written, 1);
Josef Bacikd788a342013-10-25 16:55:08 -04001690 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001691 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001692 }
1693
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001694error:
Nikolay Borisov762bf092019-08-22 17:24:20 +03001695 if (nocow)
1696 btrfs_dec_nocow_writers(fs_info, disk_bytenr);
1697
Josef Bacik17ca04a2012-05-31 15:58:55 -04001698 if (ret && cur_offset < end)
Nikolay Borisov74e91942019-07-17 16:18:16 +03001699 extent_clear_unlock_delalloc(inode, cur_offset, end,
Josef Bacikc2790a22013-07-29 11:20:47 -04001700 locked_page, EXTENT_LOCKED |
Josef Bacik151a41b2013-07-29 13:22:24 -04001701 EXTENT_DELALLOC | EXTENT_DEFRAG |
1702 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
1703 PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -04001704 PAGE_SET_WRITEBACK |
1705 PAGE_END_WRITEBACK);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001706 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001707 return ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001708}
1709
Wang Shilong47059d92014-07-03 18:22:07 +08001710static inline int need_force_cow(struct inode *inode, u64 start, u64 end)
1711{
1712
1713 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
1714 !(BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC))
1715 return 0;
1716
1717 /*
1718 * @defrag_bytes is a hint value, no spinlock held here,
1719 * if is not zero, it means the file is defragging.
1720 * Force cow if given extent needs to be defragged.
1721 */
1722 if (BTRFS_I(inode)->defrag_bytes &&
1723 test_range_bit(&BTRFS_I(inode)->io_tree, start, end,
1724 EXTENT_DEFRAG, 0, NULL))
1725 return 1;
1726
1727 return 0;
1728}
1729
Chris Masond352ac62008-09-29 15:18:18 -04001730/*
Nikolay Borisov5eaad972018-11-01 14:09:46 +02001731 * Function to process delayed allocation (create CoW) for ranges which are
1732 * being touched for the first time.
Chris Masond352ac62008-09-29 15:18:18 -04001733 */
Nikolay Borisovbc9a8bf2018-12-19 09:50:20 +02001734int btrfs_run_delalloc_range(struct inode *inode, struct page *locked_page,
Nikolay Borisov5eaad972018-11-01 14:09:46 +02001735 u64 start, u64 end, int *page_started, unsigned long *nr_written,
1736 struct writeback_control *wbc)
Chris Masonbe20aa92007-12-17 20:14:01 -05001737{
Chris Masonbe20aa92007-12-17 20:14:01 -05001738 int ret;
Wang Shilong47059d92014-07-03 18:22:07 +08001739 int force_cow = need_force_cow(inode, start, end);
Liu Bof82b7352017-10-23 23:18:16 -06001740 unsigned int write_flags = wbc_to_write_flags(wbc);
Chris Masona2135012008-06-25 16:01:30 -04001741
Wang Shilong47059d92014-07-03 18:22:07 +08001742 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW && !force_cow) {
Chris Masonc8b97812008-10-29 14:49:59 -04001743 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001744 page_started, 1, nr_written);
Wang Shilong47059d92014-07-03 18:22:07 +08001745 } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC && !force_cow) {
Yan Zhengd899e052008-10-30 14:25:28 -04001746 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001747 page_started, 0, nr_written);
Qu Wenruo42c16da2019-07-01 05:12:46 +00001748 } else if (!inode_can_compress(inode) ||
1749 !inode_need_compress(inode, start, end)) {
Nikolay Borisov74e91942019-07-17 16:18:16 +03001750 ret = cow_file_range(inode, locked_page, start, end,
Nikolay Borisov330a5822019-07-17 16:18:17 +03001751 page_started, nr_written, 1);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001752 } else {
1753 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1754 &BTRFS_I(inode)->runtime_flags);
Chris Masonec39f762019-07-10 12:28:17 -07001755 ret = cow_file_range_async(inode, wbc, locked_page, start, end,
Liu Bof82b7352017-10-23 23:18:16 -06001756 page_started, nr_written,
1757 write_flags);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001758 }
Qu Wenruo524272602017-03-08 10:25:52 +08001759 if (ret)
Nikolay Borisovd1051d62018-11-21 17:10:52 +02001760 btrfs_cleanup_ordered_extents(inode, locked_page, start,
1761 end - start + 1);
Chris Masonb888db22007-08-27 16:49:44 -04001762 return ret;
1763}
1764
Nikolay Borisovabbb55f2018-11-01 14:09:53 +02001765void btrfs_split_delalloc_extent(struct inode *inode,
1766 struct extent_state *orig, u64 split)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001767{
Josef Bacikdcab6a32015-02-11 15:08:59 -05001768 u64 size;
1769
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001770 /* not delalloc, ignore it */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001771 if (!(orig->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001772 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001773
Josef Bacikdcab6a32015-02-11 15:08:59 -05001774 size = orig->end - orig->start + 1;
1775 if (size > BTRFS_MAX_EXTENT_SIZE) {
David Sterba823bb202017-01-04 11:09:51 +01001776 u32 num_extents;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001777 u64 new_size;
1778
1779 /*
Nikolay Borisov5c848192018-11-01 14:09:52 +02001780 * See the explanation in btrfs_merge_delalloc_extent, the same
Josef Bacikba117212015-03-13 15:01:24 -04001781 * applies here, just in reverse.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001782 */
1783 new_size = orig->end - split + 1;
David Sterba823bb202017-01-04 11:09:51 +01001784 num_extents = count_max_extents(new_size);
Josef Bacikba117212015-03-13 15:01:24 -04001785 new_size = split - orig->start;
David Sterba823bb202017-01-04 11:09:51 +01001786 num_extents += count_max_extents(new_size);
1787 if (count_max_extents(size) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001788 return;
1789 }
1790
Josef Bacik9e0baf62011-07-15 15:16:44 +00001791 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04001792 btrfs_mod_outstanding_extents(BTRFS_I(inode), 1);
Josef Bacik9e0baf62011-07-15 15:16:44 +00001793 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001794}
1795
1796/*
Nikolay Borisov5c848192018-11-01 14:09:52 +02001797 * Handle merged delayed allocation extents so we can keep track of new extents
1798 * that are just merged onto old extents, such as when we are doing sequential
1799 * writes, so we can properly account for the metadata space we'll need.
Josef Bacik9ed74f22009-09-11 16:12:44 -04001800 */
Nikolay Borisov5c848192018-11-01 14:09:52 +02001801void btrfs_merge_delalloc_extent(struct inode *inode, struct extent_state *new,
1802 struct extent_state *other)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001803{
Josef Bacikdcab6a32015-02-11 15:08:59 -05001804 u64 new_size, old_size;
David Sterba823bb202017-01-04 11:09:51 +01001805 u32 num_extents;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001806
Josef Bacik9ed74f22009-09-11 16:12:44 -04001807 /* not delalloc, ignore it */
1808 if (!(other->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001809 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001810
Josef Bacik8461a3d2015-03-13 15:12:08 -04001811 if (new->start > other->start)
1812 new_size = new->end - other->start + 1;
1813 else
1814 new_size = other->end - new->start + 1;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001815
1816 /* we're not bigger than the max, unreserve the space and go */
1817 if (new_size <= BTRFS_MAX_EXTENT_SIZE) {
1818 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04001819 btrfs_mod_outstanding_extents(BTRFS_I(inode), -1);
Josef Bacikdcab6a32015-02-11 15:08:59 -05001820 spin_unlock(&BTRFS_I(inode)->lock);
1821 return;
1822 }
1823
1824 /*
Josef Bacikba117212015-03-13 15:01:24 -04001825 * We have to add up either side to figure out how many extents were
1826 * accounted for before we merged into one big extent. If the number of
1827 * extents we accounted for is <= the amount we need for the new range
1828 * then we can return, otherwise drop. Think of it like this
1829 *
1830 * [ 4k][MAX_SIZE]
1831 *
1832 * So we've grown the extent by a MAX_SIZE extent, this would mean we
1833 * need 2 outstanding extents, on one side we have 1 and the other side
1834 * we have 1 so they are == and we can return. But in this case
1835 *
1836 * [MAX_SIZE+4k][MAX_SIZE+4k]
1837 *
1838 * Each range on their own accounts for 2 extents, but merged together
1839 * they are only 3 extents worth of accounting, so we need to drop in
1840 * this case.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001841 */
Josef Bacikba117212015-03-13 15:01:24 -04001842 old_size = other->end - other->start + 1;
David Sterba823bb202017-01-04 11:09:51 +01001843 num_extents = count_max_extents(old_size);
Josef Bacikba117212015-03-13 15:01:24 -04001844 old_size = new->end - new->start + 1;
David Sterba823bb202017-01-04 11:09:51 +01001845 num_extents += count_max_extents(old_size);
1846 if (count_max_extents(new_size) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001847 return;
1848
Josef Bacik9e0baf62011-07-15 15:16:44 +00001849 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04001850 btrfs_mod_outstanding_extents(BTRFS_I(inode), -1);
Josef Bacik9e0baf62011-07-15 15:16:44 +00001851 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001852}
1853
Miao Xieeb73c1b2013-05-15 07:48:22 +00001854static void btrfs_add_delalloc_inodes(struct btrfs_root *root,
1855 struct inode *inode)
1856{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001857 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1858
Miao Xieeb73c1b2013-05-15 07:48:22 +00001859 spin_lock(&root->delalloc_lock);
1860 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1861 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1862 &root->delalloc_inodes);
1863 set_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1864 &BTRFS_I(inode)->runtime_flags);
1865 root->nr_delalloc_inodes++;
1866 if (root->nr_delalloc_inodes == 1) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001867 spin_lock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001868 BUG_ON(!list_empty(&root->delalloc_root));
1869 list_add_tail(&root->delalloc_root,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001870 &fs_info->delalloc_roots);
1871 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001872 }
1873 }
1874 spin_unlock(&root->delalloc_lock);
1875}
1876
Nikolay Borisov2b877332018-04-27 12:21:51 +03001877
1878void __btrfs_del_delalloc_inode(struct btrfs_root *root,
1879 struct btrfs_inode *inode)
Miao Xieeb73c1b2013-05-15 07:48:22 +00001880{
David Sterba3ffbd682018-06-29 10:56:42 +02001881 struct btrfs_fs_info *fs_info = root->fs_info;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001882
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001883 if (!list_empty(&inode->delalloc_inodes)) {
1884 list_del_init(&inode->delalloc_inodes);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001885 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001886 &inode->runtime_flags);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001887 root->nr_delalloc_inodes--;
1888 if (!root->nr_delalloc_inodes) {
Nikolay Borisov7c8a0d32018-04-27 12:21:52 +03001889 ASSERT(list_empty(&root->delalloc_inodes));
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001890 spin_lock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001891 BUG_ON(list_empty(&root->delalloc_root));
1892 list_del_init(&root->delalloc_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001893 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001894 }
1895 }
Nikolay Borisov2b877332018-04-27 12:21:51 +03001896}
1897
1898static void btrfs_del_delalloc_inode(struct btrfs_root *root,
1899 struct btrfs_inode *inode)
1900{
1901 spin_lock(&root->delalloc_lock);
1902 __btrfs_del_delalloc_inode(root, inode);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001903 spin_unlock(&root->delalloc_lock);
1904}
1905
Chris Masond352ac62008-09-29 15:18:18 -04001906/*
Nikolay Borisove06a1fc2018-11-01 14:09:50 +02001907 * Properly track delayed allocation bytes in the inode and to maintain the
1908 * list of inodes that have pending delalloc work to be done.
Chris Masond352ac62008-09-29 15:18:18 -04001909 */
Nikolay Borisove06a1fc2018-11-01 14:09:50 +02001910void btrfs_set_delalloc_extent(struct inode *inode, struct extent_state *state,
1911 unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001912{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001913 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1914
Wang Shilong47059d92014-07-03 18:22:07 +08001915 if ((*bits & EXTENT_DEFRAG) && !(*bits & EXTENT_DELALLOC))
1916 WARN_ON(1);
Chris Mason75eff682008-12-15 15:54:40 -05001917 /*
1918 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001919 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001920 * bit, which is only set or cleared with irqs on
1921 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001922 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001923 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001924 u64 len = state->end + 1 - state->start;
Josef Bacik8b62f872017-10-19 14:15:55 -04001925 u32 num_extents = count_max_extents(len);
Nikolay Borisov70ddc552017-02-20 13:50:35 +02001926 bool do_list = !btrfs_is_free_space_inode(BTRFS_I(inode));
Josef Bacik9ed74f22009-09-11 16:12:44 -04001927
Josef Bacik8b62f872017-10-19 14:15:55 -04001928 spin_lock(&BTRFS_I(inode)->lock);
1929 btrfs_mod_outstanding_extents(BTRFS_I(inode), num_extents);
1930 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik287a0ab2010-03-19 18:07:23 +00001931
Josef Bacik6a3891c2015-03-16 17:38:52 -04001932 /* For sanity tests */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001933 if (btrfs_is_testing(fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04001934 return;
1935
Nikolay Borisov104b4e52017-06-20 21:01:20 +03001936 percpu_counter_add_batch(&fs_info->delalloc_bytes, len,
1937 fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001938 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001939 BTRFS_I(inode)->delalloc_bytes += len;
Wang Shilong47059d92014-07-03 18:22:07 +08001940 if (*bits & EXTENT_DEFRAG)
1941 BTRFS_I(inode)->defrag_bytes += len;
Miao Xiedf0af1a2013-01-29 10:11:59 +00001942 if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00001943 &BTRFS_I(inode)->runtime_flags))
1944 btrfs_add_delalloc_inodes(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001945 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001946 }
Filipe Mananaa7e3b972017-04-03 10:45:46 +01001947
1948 if (!(state->state & EXTENT_DELALLOC_NEW) &&
1949 (*bits & EXTENT_DELALLOC_NEW)) {
1950 spin_lock(&BTRFS_I(inode)->lock);
1951 BTRFS_I(inode)->new_delalloc_bytes += state->end + 1 -
1952 state->start;
1953 spin_unlock(&BTRFS_I(inode)->lock);
1954 }
Chris Mason291d6732008-01-29 15:55:23 -05001955}
1956
Chris Masond352ac62008-09-29 15:18:18 -04001957/*
Nikolay Borisova36bb5f2018-11-01 14:09:51 +02001958 * Once a range is no longer delalloc this function ensures that proper
1959 * accounting happens.
Chris Masond352ac62008-09-29 15:18:18 -04001960 */
Nikolay Borisova36bb5f2018-11-01 14:09:51 +02001961void btrfs_clear_delalloc_extent(struct inode *vfs_inode,
1962 struct extent_state *state, unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001963{
Nikolay Borisova36bb5f2018-11-01 14:09:51 +02001964 struct btrfs_inode *inode = BTRFS_I(vfs_inode);
1965 struct btrfs_fs_info *fs_info = btrfs_sb(vfs_inode->i_sb);
Wang Shilong47059d92014-07-03 18:22:07 +08001966 u64 len = state->end + 1 - state->start;
David Sterba823bb202017-01-04 11:09:51 +01001967 u32 num_extents = count_max_extents(len);
Wang Shilong47059d92014-07-03 18:22:07 +08001968
Filipe Manana4a4b9642017-07-27 19:52:55 +01001969 if ((state->state & EXTENT_DEFRAG) && (*bits & EXTENT_DEFRAG)) {
1970 spin_lock(&inode->lock);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001971 inode->defrag_bytes -= len;
Filipe Manana4a4b9642017-07-27 19:52:55 +01001972 spin_unlock(&inode->lock);
1973 }
Wang Shilong47059d92014-07-03 18:22:07 +08001974
Chris Mason75eff682008-12-15 15:54:40 -05001975 /*
1976 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001977 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001978 * bit, which is only set or cleared with irqs on
1979 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001980 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001981 struct btrfs_root *root = inode->root;
Liu Bo83eea1f2012-07-10 05:28:39 -06001982 bool do_list = !btrfs_is_free_space_inode(inode);
Chris Masonbcbfce82008-04-22 13:26:47 -04001983
Josef Bacik8b62f872017-10-19 14:15:55 -04001984 spin_lock(&inode->lock);
1985 btrfs_mod_outstanding_extents(inode, -num_extents);
1986 spin_unlock(&inode->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001987
Josef Bacikb6d08f02013-09-27 14:57:43 -04001988 /*
1989 * We don't reserve metadata space for space cache inodes so we
Andrea Gelmini52042d82018-11-28 12:05:13 +01001990 * don't need to call delalloc_release_metadata if there is an
Josef Bacikb6d08f02013-09-27 14:57:43 -04001991 * error.
1992 */
Filipe Mananaa315e682017-03-06 23:04:20 +00001993 if (*bits & EXTENT_CLEAR_META_RESV &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001994 root != fs_info->tree_root)
Qu Wenruo43b18592017-12-12 15:34:32 +08001995 btrfs_delalloc_release_metadata(inode, len, false);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001996
Josef Bacik6a3891c2015-03-16 17:38:52 -04001997 /* For sanity tests. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001998 if (btrfs_is_testing(fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04001999 return;
2000
Filipe Mananaa315e682017-03-06 23:04:20 +00002001 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID &&
2002 do_list && !(state->state & EXTENT_NORESERVE) &&
2003 (*bits & EXTENT_CLEAR_DATA_RESV))
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02002004 btrfs_free_reserved_data_space_noquota(
2005 &inode->vfs_inode,
Qu Wenruo51773be2015-10-08 18:19:37 +08002006 state->start, len);
Josef Bacik9ed74f22009-09-11 16:12:44 -04002007
Nikolay Borisov104b4e52017-06-20 21:01:20 +03002008 percpu_counter_add_batch(&fs_info->delalloc_bytes, -len,
2009 fs_info->delalloc_batch);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02002010 spin_lock(&inode->lock);
2011 inode->delalloc_bytes -= len;
2012 if (do_list && inode->delalloc_bytes == 0 &&
Miao Xiedf0af1a2013-01-29 10:11:59 +00002013 test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02002014 &inode->runtime_flags))
Miao Xieeb73c1b2013-05-15 07:48:22 +00002015 btrfs_del_delalloc_inode(root, inode);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02002016 spin_unlock(&inode->lock);
Chris Mason291d6732008-01-29 15:55:23 -05002017 }
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002018
2019 if ((state->state & EXTENT_DELALLOC_NEW) &&
2020 (*bits & EXTENT_DELALLOC_NEW)) {
2021 spin_lock(&inode->lock);
2022 ASSERT(inode->new_delalloc_bytes >= len);
2023 inode->new_delalloc_bytes -= len;
2024 spin_unlock(&inode->lock);
2025 }
Chris Mason291d6732008-01-29 15:55:23 -05002026}
2027
Chris Masond352ac62008-09-29 15:18:18 -04002028/*
Nikolay Borisovda12fe52018-11-27 20:57:58 +02002029 * btrfs_bio_fits_in_stripe - Checks whether the size of the given bio will fit
2030 * in a chunk's stripe. This function ensures that bios do not span a
2031 * stripe/chunk
Liu Bo6f034ec2016-06-22 18:31:49 -07002032 *
Nikolay Borisovda12fe52018-11-27 20:57:58 +02002033 * @page - The page we are about to add to the bio
2034 * @size - size we want to add to the bio
2035 * @bio - bio we want to ensure is smaller than a stripe
2036 * @bio_flags - flags of the bio
2037 *
2038 * return 1 if page cannot be added to the bio
2039 * return 0 if page can be added to the bio
Liu Bo6f034ec2016-06-22 18:31:49 -07002040 * return error otherwise
Chris Masond352ac62008-09-29 15:18:18 -04002041 */
Nikolay Borisovda12fe52018-11-27 20:57:58 +02002042int btrfs_bio_fits_in_stripe(struct page *page, size_t size, struct bio *bio,
2043 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04002044{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002045 struct inode *inode = page->mapping->host;
2046 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Kent Overstreet4f024f32013-10-11 15:44:27 -07002047 u64 logical = (u64)bio->bi_iter.bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04002048 u64 length = 0;
2049 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04002050 int ret;
Nikolay Borisov89b798a2019-06-03 12:05:05 +03002051 struct btrfs_io_geometry geom;
Chris Mason239b14b2008-03-24 15:02:07 -04002052
Chris Mason771ed682008-11-06 22:02:51 -05002053 if (bio_flags & EXTENT_BIO_COMPRESSED)
2054 return 0;
2055
Kent Overstreet4f024f32013-10-11 15:44:27 -07002056 length = bio->bi_iter.bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04002057 map_length = length;
Nikolay Borisov89b798a2019-06-03 12:05:05 +03002058 ret = btrfs_get_io_geometry(fs_info, btrfs_op(bio), logical, map_length,
2059 &geom);
Liu Bo6f034ec2016-06-22 18:31:49 -07002060 if (ret < 0)
2061 return ret;
Nikolay Borisov89b798a2019-06-03 12:05:05 +03002062
2063 if (geom.len < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04002064 return 1;
Jeff Mahoney3444a972011-10-03 23:23:13 -04002065 return 0;
Chris Mason239b14b2008-03-24 15:02:07 -04002066}
2067
Chris Masond352ac62008-09-29 15:18:18 -04002068/*
2069 * in order to insert checksums into the metadata in large chunks,
2070 * we wait until bio submission time. All the pages in the bio are
2071 * checksummed and sums are attached onto the ordered extent record.
2072 *
2073 * At IO completion time the cums attached on the ordered extent record
2074 * are inserted into the btree
2075 */
David Sterbad0ee3932018-03-08 14:35:48 +01002076static blk_status_t btrfs_submit_bio_start(void *private_data, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04002077 u64 bio_offset)
Chris Mason065631f2008-02-20 12:07:25 -05002078{
Josef Bacikc6100a42017-05-05 11:57:13 -04002079 struct inode *inode = private_data;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002080 blk_status_t ret = 0;
Chris Masone0156402008-04-16 11:15:20 -04002081
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002082 ret = btrfs_csum_one_bio(inode, bio, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002083 BUG_ON(ret); /* -ENOMEM */
Chris Mason4a69a412008-11-06 22:03:00 -05002084 return 0;
2085}
Chris Masone0156402008-04-16 11:15:20 -04002086
Chris Mason4a69a412008-11-06 22:03:00 -05002087/*
Chris Masoncad321a2008-12-17 14:51:42 -05002088 * extent_io.c submission hook. This does the right thing for csum calculation
Liu Bo4c274bc2017-11-01 17:19:27 -06002089 * on write, or reading the csums from the tree before a read.
2090 *
2091 * Rules about async/sync submit,
2092 * a) read: sync submit
2093 *
2094 * b) write without checksum: sync submit
2095 *
2096 * c) write with checksum:
2097 * c-1) if bio is issued by fsync: sync submit
2098 * (sync_writers != 0)
2099 *
2100 * c-2) if root is reloc root: sync submit
2101 * (only in case of buffered IO)
2102 *
2103 * c-3) otherwise: async submit
Chris Masond352ac62008-09-29 15:18:18 -04002104 */
Nikolay Borisova56b1c72019-04-10 17:24:39 +03002105static blk_status_t btrfs_submit_bio_hook(struct inode *inode, struct bio *bio,
Nikolay Borisov50489a52019-04-10 19:46:04 +03002106 int mirror_num,
2107 unsigned long bio_flags)
2108
Chris Mason44b8bd72008-04-16 11:14:51 -04002109{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002110 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason44b8bd72008-04-16 11:14:51 -04002111 struct btrfs_root *root = BTRFS_I(inode)->root;
Chandan Rajendra0d51e282015-07-27 15:26:43 +05302112 enum btrfs_wq_endio_type metadata = BTRFS_WQ_ENDIO_DATA;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002113 blk_status_t ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04002114 int skip_sum;
Josef Bacikb812ce22012-11-16 13:56:32 -05002115 int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
Chris Mason44b8bd72008-04-16 11:14:51 -04002116
Christoph Hellwig6cbff002009-04-17 10:37:41 +02002117 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masoncad321a2008-12-17 14:51:42 -05002118
Nikolay Borisov70ddc552017-02-20 13:50:35 +02002119 if (btrfs_is_free_space_inode(BTRFS_I(inode)))
Chandan Rajendra0d51e282015-07-27 15:26:43 +05302120 metadata = BTRFS_WQ_ENDIO_FREE_SPACE;
Jeff Mahoney04173412011-10-03 23:23:12 -04002121
Mike Christie37226b22016-06-05 14:31:52 -05002122 if (bio_op(bio) != REQ_OP_WRITE) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002123 ret = btrfs_bio_wq_end_io(fs_info, bio, metadata);
Josef Bacik5fd02042012-05-02 14:00:54 -04002124 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01002125 goto out;
Josef Bacik5fd02042012-05-02 14:00:54 -04002126
Chris Masond20f7042008-12-08 16:58:54 -05002127 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Stefan Behrens61891922012-11-05 18:51:52 +01002128 ret = btrfs_submit_compressed_read(inode, bio,
2129 mirror_num,
2130 bio_flags);
2131 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00002132 } else if (!skip_sum) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002133 ret = btrfs_lookup_bio_sums(inode, bio, NULL);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00002134 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01002135 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00002136 }
Chris Mason4d1b5fb2008-08-20 09:44:52 -04002137 goto mapit;
Josef Bacikb812ce22012-11-16 13:56:32 -05002138 } else if (async && !skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05002139 /* csum items have already been cloned */
2140 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
2141 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04002142 /* we're doing a write, do the async checksumming */
Josef Bacikc6100a42017-05-05 11:57:13 -04002143 ret = btrfs_wq_submit_bio(fs_info, bio, mirror_num, bio_flags,
Nikolay Borisove7681162019-04-10 17:24:41 +03002144 0, inode, btrfs_submit_bio_start);
Stefan Behrens61891922012-11-05 18:51:52 +01002145 goto out;
Josef Bacikb812ce22012-11-16 13:56:32 -05002146 } else if (!skip_sum) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002147 ret = btrfs_csum_one_bio(inode, bio, 0, 0);
Josef Bacikb812ce22012-11-16 13:56:32 -05002148 if (ret)
2149 goto out;
Chris Mason19b9bdb2008-10-30 14:23:13 -04002150 }
2151
Chris Mason0b86a832008-03-24 15:01:56 -04002152mapit:
Chris Mason08635ba2019-07-10 12:28:14 -07002153 ret = btrfs_map_bio(fs_info, bio, mirror_num);
Stefan Behrens61891922012-11-05 18:51:52 +01002154
2155out:
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002156 if (ret) {
2157 bio->bi_status = ret;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002158 bio_endio(bio);
2159 }
Stefan Behrens61891922012-11-05 18:51:52 +01002160 return ret;
Chris Mason065631f2008-02-20 12:07:25 -05002161}
Chris Mason6885f302008-02-20 16:11:05 -05002162
Chris Masond352ac62008-09-29 15:18:18 -04002163/*
2164 * given a list of ordered sums record them in the inode. This happens
2165 * at IO completion time based on sums calculated at bio submission time.
2166 */
Chris Masonba1da2f2008-07-17 12:54:15 -04002167static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
David Sterbadf9f6282017-02-10 19:35:37 +01002168 struct inode *inode, struct list_head *list)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002169{
Chris Masone6dcd2d2008-07-17 12:53:50 -04002170 struct btrfs_ordered_sum *sum;
Nikolay Borisovac01f262018-01-08 10:59:43 +02002171 int ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002172
Qinghuang Fengc6e30872009-01-21 10:59:08 -05002173 list_for_each_entry(sum, list, list) {
David Sterba7c2871a2017-11-08 01:07:43 +01002174 trans->adding_csums = true;
Nikolay Borisovac01f262018-01-08 10:59:43 +02002175 ret = btrfs_csum_file_blocks(trans,
Chris Masond20f7042008-12-08 16:58:54 -05002176 BTRFS_I(inode)->root->fs_info->csum_root, sum);
David Sterba7c2871a2017-11-08 01:07:43 +01002177 trans->adding_csums = false;
Nikolay Borisovac01f262018-01-08 10:59:43 +02002178 if (ret)
2179 return ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002180 }
2181 return 0;
2182}
2183
Josef Bacik2ac55d42010-02-03 19:33:23 +00002184int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
Filipe Mananae3b8a482017-11-04 00:16:59 +00002185 unsigned int extra_bits,
Nikolay Borisov330a5822019-07-17 16:18:17 +03002186 struct extent_state **cached_state)
Chris Masonea8c2812008-08-04 23:17:27 -04002187{
Johannes Thumshirnfdb1e122018-12-05 15:23:04 +01002188 WARN_ON(PAGE_ALIGNED(end));
Chris Masonea8c2812008-08-04 23:17:27 -04002189 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
Filipe Mananae3b8a482017-11-04 00:16:59 +00002190 extra_bits, cached_state);
Chris Masonea8c2812008-08-04 23:17:27 -04002191}
2192
Chris Masond352ac62008-09-29 15:18:18 -04002193/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04002194struct btrfs_writepage_fixup {
2195 struct page *page;
2196 struct btrfs_work work;
2197};
2198
Christoph Hellwigb2950862008-12-02 09:54:17 -05002199static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04002200{
2201 struct btrfs_writepage_fixup *fixup;
2202 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002203 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08002204 struct extent_changeset *data_reserved = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04002205 struct page *page;
2206 struct inode *inode;
2207 u64 page_start;
2208 u64 page_end;
Jeff Mahoney87826df2012-02-15 16:23:57 +01002209 int ret;
Chris Mason247e7432008-07-17 12:53:51 -04002210
2211 fixup = container_of(work, struct btrfs_writepage_fixup, work);
2212 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -04002213again:
Chris Mason247e7432008-07-17 12:53:51 -04002214 lock_page(page);
2215 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
2216 ClearPageChecked(page);
2217 goto out_page;
2218 }
2219
2220 inode = page->mapping->host;
2221 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002222 page_end = page_offset(page) + PAGE_SIZE - 1;
Chris Mason247e7432008-07-17 12:53:51 -04002223
David Sterbaff13db42015-12-03 14:30:40 +01002224 lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002225 &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04002226
2227 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04002228 if (PagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04002229 goto out;
Chris Mason4a096752008-07-21 10:29:44 -04002230
Nikolay Borisova776c6f2017-02-20 13:50:49 +02002231 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002232 PAGE_SIZE);
Chris Mason4a096752008-07-21 10:29:44 -04002233 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00002234 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
David Sterbae43bbe52017-12-12 21:43:52 +01002235 page_end, &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04002236 unlock_page(page);
2237 btrfs_start_ordered_extent(inode, ordered, 1);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002238 btrfs_put_ordered_extent(ordered);
Chris Mason4a096752008-07-21 10:29:44 -04002239 goto again;
2240 }
Chris Mason247e7432008-07-17 12:53:51 -04002241
Qu Wenruo364ecf32017-02-27 15:10:38 +08002242 ret = btrfs_delalloc_reserve_space(inode, &data_reserved, page_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002243 PAGE_SIZE);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002244 if (ret) {
2245 mapping_set_error(page->mapping, ret);
2246 end_extent_writepage(page, ret, page_start, page_end);
2247 ClearPageChecked(page);
2248 goto out;
2249 }
2250
Nikolay Borisovf3038ee2017-12-05 09:29:19 +02002251 ret = btrfs_set_extent_delalloc(inode, page_start, page_end, 0,
Nikolay Borisov330a5822019-07-17 16:18:17 +03002252 &cached_state);
Nikolay Borisovf3038ee2017-12-05 09:29:19 +02002253 if (ret) {
2254 mapping_set_error(page->mapping, ret);
2255 end_extent_writepage(page, ret, page_start, page_end);
2256 ClearPageChecked(page);
2257 goto out;
2258 }
2259
Chris Mason247e7432008-07-17 12:53:51 -04002260 ClearPageChecked(page);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002261 set_page_dirty(page);
Qu Wenruo8702ba92019-10-14 14:34:51 +08002262 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE);
Chris Mason247e7432008-07-17 12:53:51 -04002263out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00002264 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01002265 &cached_state);
Chris Mason247e7432008-07-17 12:53:51 -04002266out_page:
2267 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002268 put_page(page);
Miao Xieb897abe2011-01-26 16:19:22 +08002269 kfree(fixup);
Qu Wenruo364ecf32017-02-27 15:10:38 +08002270 extent_changeset_free(data_reserved);
Chris Mason247e7432008-07-17 12:53:51 -04002271}
2272
2273/*
2274 * There are a few paths in the higher layers of the kernel that directly
2275 * set the page dirty bit without asking the filesystem if it is a
2276 * good idea. This causes problems because we want to make sure COW
2277 * properly happens and the data=ordered rules are followed.
2278 *
Chris Masonc8b97812008-10-29 14:49:59 -04002279 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04002280 * hasn't been properly setup for IO. We kick off an async process
2281 * to fix it up. The async helper will wait for ordered extents, set
2282 * the delalloc bit and make it safe to write the page.
2283 */
Nikolay Borisovd75855b2018-11-01 14:09:47 +02002284int btrfs_writepage_cow_fixup(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04002285{
2286 struct inode *inode = page->mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002287 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason247e7432008-07-17 12:53:51 -04002288 struct btrfs_writepage_fixup *fixup;
Chris Mason247e7432008-07-17 12:53:51 -04002289
Chris Mason8b62b722009-09-02 16:53:46 -04002290 /* this page is properly in the ordered list */
2291 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04002292 return 0;
2293
2294 if (PageChecked(page))
2295 return -EAGAIN;
2296
2297 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
2298 if (!fixup)
2299 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04002300
Chris Mason247e7432008-07-17 12:53:51 -04002301 SetPageChecked(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002302 get_page(page);
Omar Sandovala0cac0e2019-09-16 11:30:57 -07002303 btrfs_init_work(&fixup->work, btrfs_writepage_fixup_worker, NULL, NULL);
Chris Mason247e7432008-07-17 12:53:51 -04002304 fixup->page = page;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002305 btrfs_queue_work(fs_info->fixup_workers, &fixup->work);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002306 return -EBUSY;
Chris Mason247e7432008-07-17 12:53:51 -04002307}
2308
Yan Zhengd899e052008-10-30 14:25:28 -04002309static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
2310 struct inode *inode, u64 file_pos,
2311 u64 disk_bytenr, u64 disk_num_bytes,
2312 u64 num_bytes, u64 ram_bytes,
2313 u8 compression, u8 encryption,
2314 u16 other_encoding, int extent_type)
2315{
2316 struct btrfs_root *root = BTRFS_I(inode)->root;
2317 struct btrfs_file_extent_item *fi;
2318 struct btrfs_path *path;
2319 struct extent_buffer *leaf;
2320 struct btrfs_key ins;
Qu Wenruoa12b8772017-02-27 15:10:37 +08002321 u64 qg_released;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002322 int extent_inserted = 0;
Yan Zhengd899e052008-10-30 14:25:28 -04002323 int ret;
2324
2325 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07002326 if (!path)
2327 return -ENOMEM;
Yan Zhengd899e052008-10-30 14:25:28 -04002328
Chris Masona1ed8352009-09-11 12:27:37 -04002329 /*
2330 * we may be replacing one extent in the tree with another.
2331 * The new extent is pinned in the extent map, and we don't want
2332 * to drop it from the cache until it is completely in the btree.
2333 *
2334 * So, tell btrfs_drop_extents to leave this extent in the cache.
2335 * the caller is expected to unpin it and allow it to be merged
2336 * with the others.
2337 */
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002338 ret = __btrfs_drop_extents(trans, root, inode, path, file_pos,
2339 file_pos + num_bytes, NULL, 0,
2340 1, sizeof(*fi), &extent_inserted);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002341 if (ret)
2342 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04002343
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002344 if (!extent_inserted) {
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002345 ins.objectid = btrfs_ino(BTRFS_I(inode));
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002346 ins.offset = file_pos;
2347 ins.type = BTRFS_EXTENT_DATA_KEY;
2348
2349 path->leave_spinning = 1;
2350 ret = btrfs_insert_empty_item(trans, root, path, &ins,
2351 sizeof(*fi));
2352 if (ret)
2353 goto out;
2354 }
Yan Zhengd899e052008-10-30 14:25:28 -04002355 leaf = path->nodes[0];
2356 fi = btrfs_item_ptr(leaf, path->slots[0],
2357 struct btrfs_file_extent_item);
2358 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
2359 btrfs_set_file_extent_type(leaf, fi, extent_type);
2360 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
2361 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
2362 btrfs_set_file_extent_offset(leaf, fi, 0);
2363 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
2364 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
2365 btrfs_set_file_extent_compression(leaf, fi, compression);
2366 btrfs_set_file_extent_encryption(leaf, fi, encryption);
2367 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
Chris Masonb9473432009-03-13 11:00:37 -04002368
Yan Zhengd899e052008-10-30 14:25:28 -04002369 btrfs_mark_buffer_dirty(leaf);
Josef Bacikce195332012-09-25 15:26:16 -04002370 btrfs_release_path(path);
Yan Zhengd899e052008-10-30 14:25:28 -04002371
2372 inode_add_bytes(inode, num_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -04002373
2374 ins.objectid = disk_bytenr;
2375 ins.offset = disk_num_bytes;
2376 ins.type = BTRFS_EXTENT_ITEM_KEY;
Qu Wenruoa12b8772017-02-27 15:10:37 +08002377
Qu Wenruo297d7502015-09-08 17:08:37 +08002378 /*
Qu Wenruo5846a3c2015-10-26 14:11:18 +08002379 * Release the reserved range from inode dirty range map, as it is
2380 * already moved into delayed_ref_head
Qu Wenruo297d7502015-09-08 17:08:37 +08002381 */
Qu Wenruoa12b8772017-02-27 15:10:37 +08002382 ret = btrfs_qgroup_release_data(inode, file_pos, ram_bytes);
2383 if (ret < 0)
2384 goto out;
2385 qg_released = ret;
Josef Bacik84f7d8e2017-09-29 15:43:49 -04002386 ret = btrfs_alloc_reserved_file_extent(trans, root,
2387 btrfs_ino(BTRFS_I(inode)),
2388 file_pos, qg_released, &ins);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002389out:
Yan Zhengd899e052008-10-30 14:25:28 -04002390 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04002391
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002392 return ret;
Yan Zhengd899e052008-10-30 14:25:28 -04002393}
2394
Liu Bo38c227d2013-01-29 03:18:40 +00002395/* snapshot-aware defrag */
2396struct sa_defrag_extent_backref {
2397 struct rb_node node;
2398 struct old_sa_defrag_extent *old;
2399 u64 root_id;
2400 u64 inum;
2401 u64 file_pos;
2402 u64 extent_offset;
2403 u64 num_bytes;
2404 u64 generation;
2405};
2406
2407struct old_sa_defrag_extent {
2408 struct list_head list;
2409 struct new_sa_defrag_extent *new;
2410
2411 u64 extent_offset;
2412 u64 bytenr;
2413 u64 offset;
2414 u64 len;
2415 int count;
2416};
2417
2418struct new_sa_defrag_extent {
2419 struct rb_root root;
2420 struct list_head head;
2421 struct btrfs_path *path;
2422 struct inode *inode;
2423 u64 file_pos;
2424 u64 len;
2425 u64 bytenr;
2426 u64 disk_len;
2427 u8 compress_type;
2428};
2429
2430static int backref_comp(struct sa_defrag_extent_backref *b1,
2431 struct sa_defrag_extent_backref *b2)
2432{
2433 if (b1->root_id < b2->root_id)
2434 return -1;
2435 else if (b1->root_id > b2->root_id)
2436 return 1;
2437
2438 if (b1->inum < b2->inum)
2439 return -1;
2440 else if (b1->inum > b2->inum)
2441 return 1;
2442
2443 if (b1->file_pos < b2->file_pos)
2444 return -1;
2445 else if (b1->file_pos > b2->file_pos)
2446 return 1;
2447
2448 /*
2449 * [------------------------------] ===> (a range of space)
2450 * |<--->| |<---->| =============> (fs/file tree A)
2451 * |<---------------------------->| ===> (fs/file tree B)
2452 *
2453 * A range of space can refer to two file extents in one tree while
2454 * refer to only one file extent in another tree.
2455 *
2456 * So we may process a disk offset more than one time(two extents in A)
2457 * and locate at the same extent(one extent in B), then insert two same
2458 * backrefs(both refer to the extent in B).
2459 */
2460 return 0;
2461}
2462
2463static void backref_insert(struct rb_root *root,
2464 struct sa_defrag_extent_backref *backref)
2465{
2466 struct rb_node **p = &root->rb_node;
2467 struct rb_node *parent = NULL;
2468 struct sa_defrag_extent_backref *entry;
2469 int ret;
2470
2471 while (*p) {
2472 parent = *p;
2473 entry = rb_entry(parent, struct sa_defrag_extent_backref, node);
2474
2475 ret = backref_comp(backref, entry);
2476 if (ret < 0)
2477 p = &(*p)->rb_left;
2478 else
2479 p = &(*p)->rb_right;
2480 }
2481
2482 rb_link_node(&backref->node, parent, p);
2483 rb_insert_color(&backref->node, root);
2484}
2485
2486/*
2487 * Note the backref might has changed, and in this case we just return 0.
2488 */
2489static noinline int record_one_backref(u64 inum, u64 offset, u64 root_id,
2490 void *ctx)
2491{
2492 struct btrfs_file_extent_item *extent;
Liu Bo38c227d2013-01-29 03:18:40 +00002493 struct old_sa_defrag_extent *old = ctx;
2494 struct new_sa_defrag_extent *new = old->new;
2495 struct btrfs_path *path = new->path;
2496 struct btrfs_key key;
2497 struct btrfs_root *root;
2498 struct sa_defrag_extent_backref *backref;
2499 struct extent_buffer *leaf;
2500 struct inode *inode = new->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002501 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002502 int slot;
2503 int ret;
2504 u64 extent_offset;
2505 u64 num_bytes;
2506
2507 if (BTRFS_I(inode)->root->root_key.objectid == root_id &&
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002508 inum == btrfs_ino(BTRFS_I(inode)))
Liu Bo38c227d2013-01-29 03:18:40 +00002509 return 0;
2510
2511 key.objectid = root_id;
2512 key.type = BTRFS_ROOT_ITEM_KEY;
2513 key.offset = (u64)-1;
2514
Liu Bo38c227d2013-01-29 03:18:40 +00002515 root = btrfs_read_fs_root_no_name(fs_info, &key);
2516 if (IS_ERR(root)) {
2517 if (PTR_ERR(root) == -ENOENT)
2518 return 0;
2519 WARN_ON(1);
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002520 btrfs_debug(fs_info, "inum=%llu, offset=%llu, root_id=%llu",
Liu Bo38c227d2013-01-29 03:18:40 +00002521 inum, offset, root_id);
2522 return PTR_ERR(root);
2523 }
2524
2525 key.objectid = inum;
2526 key.type = BTRFS_EXTENT_DATA_KEY;
2527 if (offset > (u64)-1 << 32)
2528 key.offset = 0;
2529 else
2530 key.offset = offset;
2531
2532 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05302533 if (WARN_ON(ret < 0))
Liu Bo38c227d2013-01-29 03:18:40 +00002534 return ret;
Josef Bacik50f13192013-07-22 12:50:37 -04002535 ret = 0;
Liu Bo38c227d2013-01-29 03:18:40 +00002536
2537 while (1) {
2538 cond_resched();
2539
2540 leaf = path->nodes[0];
2541 slot = path->slots[0];
2542
2543 if (slot >= btrfs_header_nritems(leaf)) {
2544 ret = btrfs_next_leaf(root, path);
2545 if (ret < 0) {
2546 goto out;
2547 } else if (ret > 0) {
2548 ret = 0;
2549 goto out;
2550 }
2551 continue;
2552 }
2553
2554 path->slots[0]++;
2555
2556 btrfs_item_key_to_cpu(leaf, &key, slot);
2557
2558 if (key.objectid > inum)
2559 goto out;
2560
2561 if (key.objectid < inum || key.type != BTRFS_EXTENT_DATA_KEY)
2562 continue;
2563
2564 extent = btrfs_item_ptr(leaf, slot,
2565 struct btrfs_file_extent_item);
2566
2567 if (btrfs_file_extent_disk_bytenr(leaf, extent) != old->bytenr)
2568 continue;
2569
Liu Boe68afa42013-07-01 22:13:26 +08002570 /*
2571 * 'offset' refers to the exact key.offset,
2572 * NOT the 'offset' field in btrfs_extent_data_ref, ie.
2573 * (key.offset - extent_offset).
2574 */
2575 if (key.offset != offset)
Liu Bo38c227d2013-01-29 03:18:40 +00002576 continue;
2577
Liu Boe68afa42013-07-01 22:13:26 +08002578 extent_offset = btrfs_file_extent_offset(leaf, extent);
Liu Bo38c227d2013-01-29 03:18:40 +00002579 num_bytes = btrfs_file_extent_num_bytes(leaf, extent);
Liu Boe68afa42013-07-01 22:13:26 +08002580
Liu Bo38c227d2013-01-29 03:18:40 +00002581 if (extent_offset >= old->extent_offset + old->offset +
2582 old->len || extent_offset + num_bytes <=
2583 old->extent_offset + old->offset)
2584 continue;
Liu Bo38c227d2013-01-29 03:18:40 +00002585 break;
2586 }
2587
2588 backref = kmalloc(sizeof(*backref), GFP_NOFS);
2589 if (!backref) {
2590 ret = -ENOENT;
2591 goto out;
2592 }
2593
2594 backref->root_id = root_id;
2595 backref->inum = inum;
Liu Boe68afa42013-07-01 22:13:26 +08002596 backref->file_pos = offset;
Liu Bo38c227d2013-01-29 03:18:40 +00002597 backref->num_bytes = num_bytes;
2598 backref->extent_offset = extent_offset;
2599 backref->generation = btrfs_file_extent_generation(leaf, extent);
2600 backref->old = old;
2601 backref_insert(&new->root, backref);
2602 old->count++;
2603out:
2604 btrfs_release_path(path);
2605 WARN_ON(ret);
2606 return ret;
2607}
2608
2609static noinline bool record_extent_backrefs(struct btrfs_path *path,
2610 struct new_sa_defrag_extent *new)
2611{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002612 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002613 struct old_sa_defrag_extent *old, *tmp;
2614 int ret;
2615
2616 new->path = path;
2617
2618 list_for_each_entry_safe(old, tmp, &new->head, list) {
Liu Boe68afa42013-07-01 22:13:26 +08002619 ret = iterate_inodes_from_logical(old->bytenr +
2620 old->extent_offset, fs_info,
Liu Bo38c227d2013-01-29 03:18:40 +00002621 path, record_one_backref,
Zygo Blaxellc995ab32017-09-22 13:58:45 -04002622 old, false);
Josef Bacik4724b102013-11-05 11:11:40 -05002623 if (ret < 0 && ret != -ENOENT)
2624 return false;
Liu Bo38c227d2013-01-29 03:18:40 +00002625
2626 /* no backref to be processed for this extent */
2627 if (!old->count) {
2628 list_del(&old->list);
2629 kfree(old);
2630 }
2631 }
2632
2633 if (list_empty(&new->head))
2634 return false;
2635
2636 return true;
2637}
2638
2639static int relink_is_mergable(struct extent_buffer *leaf,
2640 struct btrfs_file_extent_item *fi,
Liu Bo116e0022013-08-02 16:30:40 +08002641 struct new_sa_defrag_extent *new)
Liu Bo38c227d2013-01-29 03:18:40 +00002642{
Liu Bo116e0022013-08-02 16:30:40 +08002643 if (btrfs_file_extent_disk_bytenr(leaf, fi) != new->bytenr)
Liu Bo38c227d2013-01-29 03:18:40 +00002644 return 0;
2645
2646 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG)
2647 return 0;
2648
Liu Bo116e0022013-08-02 16:30:40 +08002649 if (btrfs_file_extent_compression(leaf, fi) != new->compress_type)
2650 return 0;
2651
2652 if (btrfs_file_extent_encryption(leaf, fi) ||
Liu Bo38c227d2013-01-29 03:18:40 +00002653 btrfs_file_extent_other_encoding(leaf, fi))
2654 return 0;
2655
2656 return 1;
2657}
2658
2659/*
2660 * Note the backref might has changed, and in this case we just return 0.
2661 */
2662static noinline int relink_extent_backref(struct btrfs_path *path,
2663 struct sa_defrag_extent_backref *prev,
2664 struct sa_defrag_extent_backref *backref)
2665{
2666 struct btrfs_file_extent_item *extent;
2667 struct btrfs_file_extent_item *item;
2668 struct btrfs_ordered_extent *ordered;
2669 struct btrfs_trans_handle *trans;
Qu Wenruo82fa1132019-04-04 14:45:35 +08002670 struct btrfs_ref ref = { 0 };
Liu Bo38c227d2013-01-29 03:18:40 +00002671 struct btrfs_root *root;
2672 struct btrfs_key key;
2673 struct extent_buffer *leaf;
2674 struct old_sa_defrag_extent *old = backref->old;
2675 struct new_sa_defrag_extent *new = old->new;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002676 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002677 struct inode *inode;
2678 struct extent_state *cached = NULL;
2679 int ret = 0;
2680 u64 start;
2681 u64 len;
2682 u64 lock_start;
2683 u64 lock_end;
2684 bool merge = false;
2685 int index;
2686
2687 if (prev && prev->root_id == backref->root_id &&
2688 prev->inum == backref->inum &&
2689 prev->file_pos + prev->num_bytes == backref->file_pos)
2690 merge = true;
2691
2692 /* step 1: get root */
2693 key.objectid = backref->root_id;
2694 key.type = BTRFS_ROOT_ITEM_KEY;
2695 key.offset = (u64)-1;
2696
Liu Bo38c227d2013-01-29 03:18:40 +00002697 index = srcu_read_lock(&fs_info->subvol_srcu);
2698
2699 root = btrfs_read_fs_root_no_name(fs_info, &key);
2700 if (IS_ERR(root)) {
2701 srcu_read_unlock(&fs_info->subvol_srcu, index);
2702 if (PTR_ERR(root) == -ENOENT)
2703 return 0;
2704 return PTR_ERR(root);
2705 }
Liu Bo38c227d2013-01-29 03:18:40 +00002706
Wang Shilongbcbba5e2014-02-08 23:46:35 +08002707 if (btrfs_root_readonly(root)) {
2708 srcu_read_unlock(&fs_info->subvol_srcu, index);
2709 return 0;
2710 }
2711
Liu Bo38c227d2013-01-29 03:18:40 +00002712 /* step 2: get inode */
2713 key.objectid = backref->inum;
2714 key.type = BTRFS_INODE_ITEM_KEY;
2715 key.offset = 0;
2716
David Sterba4c66e0d2019-10-03 19:09:35 +02002717 inode = btrfs_iget(fs_info->sb, &key, root);
Liu Bo38c227d2013-01-29 03:18:40 +00002718 if (IS_ERR(inode)) {
2719 srcu_read_unlock(&fs_info->subvol_srcu, index);
2720 return 0;
2721 }
2722
2723 srcu_read_unlock(&fs_info->subvol_srcu, index);
2724
2725 /* step 3: relink backref */
2726 lock_start = backref->file_pos;
2727 lock_end = backref->file_pos + backref->num_bytes - 1;
2728 lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
David Sterbaff13db42015-12-03 14:30:40 +01002729 &cached);
Liu Bo38c227d2013-01-29 03:18:40 +00002730
2731 ordered = btrfs_lookup_first_ordered_extent(inode, lock_end);
2732 if (ordered) {
2733 btrfs_put_ordered_extent(ordered);
2734 goto out_unlock;
2735 }
2736
2737 trans = btrfs_join_transaction(root);
2738 if (IS_ERR(trans)) {
2739 ret = PTR_ERR(trans);
2740 goto out_unlock;
2741 }
2742
2743 key.objectid = backref->inum;
2744 key.type = BTRFS_EXTENT_DATA_KEY;
2745 key.offset = backref->file_pos;
2746
2747 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2748 if (ret < 0) {
2749 goto out_free_path;
2750 } else if (ret > 0) {
2751 ret = 0;
2752 goto out_free_path;
2753 }
2754
2755 extent = btrfs_item_ptr(path->nodes[0], path->slots[0],
2756 struct btrfs_file_extent_item);
2757
2758 if (btrfs_file_extent_generation(path->nodes[0], extent) !=
2759 backref->generation)
2760 goto out_free_path;
2761
2762 btrfs_release_path(path);
2763
2764 start = backref->file_pos;
2765 if (backref->extent_offset < old->extent_offset + old->offset)
2766 start += old->extent_offset + old->offset -
2767 backref->extent_offset;
2768
2769 len = min(backref->extent_offset + backref->num_bytes,
2770 old->extent_offset + old->offset + old->len);
2771 len -= max(backref->extent_offset, old->extent_offset + old->offset);
2772
2773 ret = btrfs_drop_extents(trans, root, inode, start,
2774 start + len, 1);
2775 if (ret)
2776 goto out_free_path;
2777again:
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002778 key.objectid = btrfs_ino(BTRFS_I(inode));
Liu Bo38c227d2013-01-29 03:18:40 +00002779 key.type = BTRFS_EXTENT_DATA_KEY;
2780 key.offset = start;
2781
Liu Boa09a0a72013-03-11 09:20:58 +00002782 path->leave_spinning = 1;
Liu Bo38c227d2013-01-29 03:18:40 +00002783 if (merge) {
2784 struct btrfs_file_extent_item *fi;
2785 u64 extent_len;
2786 struct btrfs_key found_key;
2787
Gui Hecheng3c9665d2014-01-23 13:41:09 +08002788 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
Liu Bo38c227d2013-01-29 03:18:40 +00002789 if (ret < 0)
2790 goto out_free_path;
2791
2792 path->slots[0]--;
2793 leaf = path->nodes[0];
2794 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2795
2796 fi = btrfs_item_ptr(leaf, path->slots[0],
2797 struct btrfs_file_extent_item);
2798 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
2799
Liu Bo116e0022013-08-02 16:30:40 +08002800 if (extent_len + found_key.offset == start &&
2801 relink_is_mergable(leaf, fi, new)) {
Liu Bo38c227d2013-01-29 03:18:40 +00002802 btrfs_set_file_extent_num_bytes(leaf, fi,
2803 extent_len + len);
2804 btrfs_mark_buffer_dirty(leaf);
2805 inode_add_bytes(inode, len);
2806
2807 ret = 1;
2808 goto out_free_path;
2809 } else {
2810 merge = false;
2811 btrfs_release_path(path);
2812 goto again;
2813 }
2814 }
2815
2816 ret = btrfs_insert_empty_item(trans, root, path, &key,
2817 sizeof(*extent));
2818 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002819 btrfs_abort_transaction(trans, ret);
Liu Bo38c227d2013-01-29 03:18:40 +00002820 goto out_free_path;
2821 }
2822
2823 leaf = path->nodes[0];
2824 item = btrfs_item_ptr(leaf, path->slots[0],
2825 struct btrfs_file_extent_item);
2826 btrfs_set_file_extent_disk_bytenr(leaf, item, new->bytenr);
2827 btrfs_set_file_extent_disk_num_bytes(leaf, item, new->disk_len);
2828 btrfs_set_file_extent_offset(leaf, item, start - new->file_pos);
2829 btrfs_set_file_extent_num_bytes(leaf, item, len);
2830 btrfs_set_file_extent_ram_bytes(leaf, item, new->len);
2831 btrfs_set_file_extent_generation(leaf, item, trans->transid);
2832 btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG);
2833 btrfs_set_file_extent_compression(leaf, item, new->compress_type);
2834 btrfs_set_file_extent_encryption(leaf, item, 0);
2835 btrfs_set_file_extent_other_encoding(leaf, item, 0);
2836
2837 btrfs_mark_buffer_dirty(leaf);
2838 inode_add_bytes(inode, len);
Liu Boa09a0a72013-03-11 09:20:58 +00002839 btrfs_release_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002840
Qu Wenruo82fa1132019-04-04 14:45:35 +08002841 btrfs_init_generic_ref(&ref, BTRFS_ADD_DELAYED_REF, new->bytenr,
2842 new->disk_len, 0);
2843 btrfs_init_data_ref(&ref, backref->root_id, backref->inum,
2844 new->file_pos); /* start - extent_offset */
2845 ret = btrfs_inc_extent_ref(trans, &ref);
Liu Bo38c227d2013-01-29 03:18:40 +00002846 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002847 btrfs_abort_transaction(trans, ret);
Liu Bo38c227d2013-01-29 03:18:40 +00002848 goto out_free_path;
2849 }
2850
2851 ret = 1;
2852out_free_path:
2853 btrfs_release_path(path);
Liu Boa09a0a72013-03-11 09:20:58 +00002854 path->leave_spinning = 0;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002855 btrfs_end_transaction(trans);
Liu Bo38c227d2013-01-29 03:18:40 +00002856out_unlock:
2857 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
David Sterbae43bbe52017-12-12 21:43:52 +01002858 &cached);
Liu Bo38c227d2013-01-29 03:18:40 +00002859 iput(inode);
2860 return ret;
2861}
2862
Liu Bo6f519562013-10-29 10:45:05 +08002863static void free_sa_defrag_extent(struct new_sa_defrag_extent *new)
2864{
2865 struct old_sa_defrag_extent *old, *tmp;
2866
2867 if (!new)
2868 return;
2869
2870 list_for_each_entry_safe(old, tmp, &new->head, list) {
Liu Bo6f519562013-10-29 10:45:05 +08002871 kfree(old);
2872 }
2873 kfree(new);
2874}
2875
Liu Bo38c227d2013-01-29 03:18:40 +00002876static void relink_file_extents(struct new_sa_defrag_extent *new)
2877{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002878 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002879 struct btrfs_path *path;
Liu Bo38c227d2013-01-29 03:18:40 +00002880 struct sa_defrag_extent_backref *backref;
2881 struct sa_defrag_extent_backref *prev = NULL;
Liu Bo38c227d2013-01-29 03:18:40 +00002882 struct rb_node *node;
2883 int ret;
2884
Liu Bo38c227d2013-01-29 03:18:40 +00002885 path = btrfs_alloc_path();
2886 if (!path)
2887 return;
2888
2889 if (!record_extent_backrefs(path, new)) {
2890 btrfs_free_path(path);
2891 goto out;
2892 }
2893 btrfs_release_path(path);
2894
2895 while (1) {
2896 node = rb_first(&new->root);
2897 if (!node)
2898 break;
2899 rb_erase(node, &new->root);
2900
2901 backref = rb_entry(node, struct sa_defrag_extent_backref, node);
2902
2903 ret = relink_extent_backref(path, prev, backref);
2904 WARN_ON(ret < 0);
2905
2906 kfree(prev);
2907
2908 if (ret == 1)
2909 prev = backref;
2910 else
2911 prev = NULL;
2912 cond_resched();
2913 }
2914 kfree(prev);
2915
2916 btrfs_free_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002917out:
Liu Bo6f519562013-10-29 10:45:05 +08002918 free_sa_defrag_extent(new);
2919
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002920 atomic_dec(&fs_info->defrag_running);
2921 wake_up(&fs_info->transaction_wait);
Liu Bo38c227d2013-01-29 03:18:40 +00002922}
2923
2924static struct new_sa_defrag_extent *
2925record_old_file_extents(struct inode *inode,
2926 struct btrfs_ordered_extent *ordered)
2927{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002928 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002929 struct btrfs_root *root = BTRFS_I(inode)->root;
2930 struct btrfs_path *path;
2931 struct btrfs_key key;
Liu Bo6f519562013-10-29 10:45:05 +08002932 struct old_sa_defrag_extent *old;
Liu Bo38c227d2013-01-29 03:18:40 +00002933 struct new_sa_defrag_extent *new;
2934 int ret;
2935
2936 new = kmalloc(sizeof(*new), GFP_NOFS);
2937 if (!new)
2938 return NULL;
2939
2940 new->inode = inode;
2941 new->file_pos = ordered->file_offset;
2942 new->len = ordered->len;
2943 new->bytenr = ordered->start;
2944 new->disk_len = ordered->disk_len;
2945 new->compress_type = ordered->compress_type;
2946 new->root = RB_ROOT;
2947 INIT_LIST_HEAD(&new->head);
2948
2949 path = btrfs_alloc_path();
2950 if (!path)
2951 goto out_kfree;
2952
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002953 key.objectid = btrfs_ino(BTRFS_I(inode));
Liu Bo38c227d2013-01-29 03:18:40 +00002954 key.type = BTRFS_EXTENT_DATA_KEY;
2955 key.offset = new->file_pos;
2956
2957 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2958 if (ret < 0)
2959 goto out_free_path;
2960 if (ret > 0 && path->slots[0] > 0)
2961 path->slots[0]--;
2962
2963 /* find out all the old extents for the file range */
2964 while (1) {
2965 struct btrfs_file_extent_item *extent;
2966 struct extent_buffer *l;
2967 int slot;
2968 u64 num_bytes;
2969 u64 offset;
2970 u64 end;
2971 u64 disk_bytenr;
2972 u64 extent_offset;
2973
2974 l = path->nodes[0];
2975 slot = path->slots[0];
2976
2977 if (slot >= btrfs_header_nritems(l)) {
2978 ret = btrfs_next_leaf(root, path);
2979 if (ret < 0)
Liu Bo6f519562013-10-29 10:45:05 +08002980 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002981 else if (ret > 0)
2982 break;
2983 continue;
2984 }
2985
2986 btrfs_item_key_to_cpu(l, &key, slot);
2987
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002988 if (key.objectid != btrfs_ino(BTRFS_I(inode)))
Liu Bo38c227d2013-01-29 03:18:40 +00002989 break;
2990 if (key.type != BTRFS_EXTENT_DATA_KEY)
2991 break;
2992 if (key.offset >= new->file_pos + new->len)
2993 break;
2994
2995 extent = btrfs_item_ptr(l, slot, struct btrfs_file_extent_item);
2996
2997 num_bytes = btrfs_file_extent_num_bytes(l, extent);
2998 if (key.offset + num_bytes < new->file_pos)
2999 goto next;
3000
3001 disk_bytenr = btrfs_file_extent_disk_bytenr(l, extent);
3002 if (!disk_bytenr)
3003 goto next;
3004
3005 extent_offset = btrfs_file_extent_offset(l, extent);
3006
3007 old = kmalloc(sizeof(*old), GFP_NOFS);
3008 if (!old)
Liu Bo6f519562013-10-29 10:45:05 +08003009 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00003010
3011 offset = max(new->file_pos, key.offset);
3012 end = min(new->file_pos + new->len, key.offset + num_bytes);
3013
3014 old->bytenr = disk_bytenr;
3015 old->extent_offset = extent_offset;
3016 old->offset = offset - key.offset;
3017 old->len = end - offset;
3018 old->new = new;
3019 old->count = 0;
3020 list_add_tail(&old->list, &new->head);
3021next:
3022 path->slots[0]++;
3023 cond_resched();
3024 }
3025
3026 btrfs_free_path(path);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003027 atomic_inc(&fs_info->defrag_running);
Liu Bo38c227d2013-01-29 03:18:40 +00003028
3029 return new;
3030
Liu Bo38c227d2013-01-29 03:18:40 +00003031out_free_path:
3032 btrfs_free_path(path);
3033out_kfree:
Liu Bo6f519562013-10-29 10:45:05 +08003034 free_sa_defrag_extent(new);
Liu Bo38c227d2013-01-29 03:18:40 +00003035 return NULL;
3036}
3037
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003038static void btrfs_release_delalloc_bytes(struct btrfs_fs_info *fs_info,
Miao Xiee570fd22014-06-19 10:42:50 +08003039 u64 start, u64 len)
3040{
3041 struct btrfs_block_group_cache *cache;
3042
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003043 cache = btrfs_lookup_block_group(fs_info, start);
Miao Xiee570fd22014-06-19 10:42:50 +08003044 ASSERT(cache);
3045
3046 spin_lock(&cache->lock);
3047 cache->delalloc_bytes -= len;
3048 spin_unlock(&cache->lock);
3049
3050 btrfs_put_block_group(cache);
3051}
3052
Chris Masond352ac62008-09-29 15:18:18 -04003053/* as ordered data IO finishes, this gets called so we can finish
3054 * an ordered extent if the range of bytes in the file it covers are
3055 * fully written.
3056 */
Josef Bacik5fd02042012-05-02 14:00:54 -04003057static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
Chris Masone6dcd2d2008-07-17 12:53:50 -04003058{
Josef Bacik5fd02042012-05-02 14:00:54 -04003059 struct inode *inode = ordered_extent->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003060 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003061 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003062 struct btrfs_trans_handle *trans = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003063 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00003064 struct extent_state *cached_state = NULL;
Liu Bo38c227d2013-01-29 03:18:40 +00003065 struct new_sa_defrag_extent *new = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08003066 int compress_type = 0;
Josef Bacik77cef2e2013-08-29 13:57:21 -04003067 int ret = 0;
3068 u64 logical_len = ordered_extent->len;
Li Zefan82d59022011-04-20 10:33:24 +08003069 bool nolock;
Josef Bacik77cef2e2013-08-29 13:57:21 -04003070 bool truncated = false;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003071 bool range_locked = false;
3072 bool clear_new_delalloc_bytes = false;
Josef Bacik49940bd2018-10-11 15:54:21 -04003073 bool clear_reserved_extent = true;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003074
3075 if (!test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
3076 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags) &&
3077 !test_bit(BTRFS_ORDERED_DIRECT, &ordered_extent->flags))
3078 clear_new_delalloc_bytes = true;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003079
Nikolay Borisov70ddc552017-02-20 13:50:35 +02003080 nolock = btrfs_is_free_space_inode(BTRFS_I(inode));
Josef Bacik0cb59c92010-07-02 12:14:14 -04003081
Josef Bacik5fd02042012-05-02 14:00:54 -04003082 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
3083 ret = -EIO;
3084 goto out;
3085 }
3086
Nikolay Borisov7ab79562017-02-20 13:50:57 +02003087 btrfs_free_io_failure_record(BTRFS_I(inode),
3088 ordered_extent->file_offset,
3089 ordered_extent->file_offset +
3090 ordered_extent->len - 1);
Miao Xief6124962014-09-12 18:44:04 +08003091
Josef Bacik77cef2e2013-08-29 13:57:21 -04003092 if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) {
3093 truncated = true;
3094 logical_len = ordered_extent->truncated_len;
3095 /* Truncated the entire extent, don't bother adding */
3096 if (!logical_len)
3097 goto out;
3098 }
3099
Yan, Zhengc2167752009-11-12 09:34:21 +00003100 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003101 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
Qu Wenruo94ed9382015-09-08 17:25:56 +08003102
3103 /*
3104 * For mwrite(mmap + memset to write) case, we still reserve
3105 * space for NOCOW range.
3106 * As NOCOW won't cause a new delayed ref, just free the space
3107 */
Qu Wenruobc42bda2017-02-27 15:10:39 +08003108 btrfs_qgroup_free_data(inode, NULL, ordered_extent->file_offset,
Qu Wenruo94ed9382015-09-08 17:25:56 +08003109 ordered_extent->len);
Josef Bacik6c760c02012-11-09 10:53:21 -05003110 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
3111 if (nolock)
3112 trans = btrfs_join_transaction_nolock(root);
3113 else
3114 trans = btrfs_join_transaction(root);
3115 if (IS_ERR(trans)) {
3116 ret = PTR_ERR(trans);
3117 trans = NULL;
3118 goto out;
Yan, Zhengc2167752009-11-12 09:34:21 +00003119 }
Josef Bacik69fe2d72017-10-19 14:15:57 -04003120 trans->block_rsv = &BTRFS_I(inode)->block_rsv;
Josef Bacik6c760c02012-11-09 10:53:21 -05003121 ret = btrfs_update_inode_fallback(trans, root, inode);
3122 if (ret) /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04003123 btrfs_abort_transaction(trans, ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00003124 goto out;
3125 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04003126
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003127 range_locked = true;
Josef Bacik2ac55d42010-02-03 19:33:23 +00003128 lock_extent_bits(io_tree, ordered_extent->file_offset,
3129 ordered_extent->file_offset + ordered_extent->len - 1,
David Sterbaff13db42015-12-03 14:30:40 +01003130 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003131
Liu Bo38c227d2013-01-29 03:18:40 +00003132 ret = test_range_bit(io_tree, ordered_extent->file_offset,
3133 ordered_extent->file_offset + ordered_extent->len - 1,
Liu Bo452e62b2017-05-26 17:44:23 -06003134 EXTENT_DEFRAG, 0, cached_state);
Liu Bo38c227d2013-01-29 03:18:40 +00003135 if (ret) {
3136 u64 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
Josef Bacik8101c8d2014-01-29 16:05:30 -05003137 if (0 && last_snapshot >= BTRFS_I(inode)->generation)
Liu Bo38c227d2013-01-29 03:18:40 +00003138 /* the inode is shared */
3139 new = record_old_file_extents(inode, ordered_extent);
3140
3141 clear_extent_bit(io_tree, ordered_extent->file_offset,
3142 ordered_extent->file_offset + ordered_extent->len - 1,
David Sterbaae0f1622017-10-31 16:37:52 +01003143 EXTENT_DEFRAG, 0, 0, &cached_state);
Liu Bo38c227d2013-01-29 03:18:40 +00003144 }
3145
Josef Bacik0cb59c92010-07-02 12:14:14 -04003146 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003147 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04003148 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003149 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003150 if (IS_ERR(trans)) {
3151 ret = PTR_ERR(trans);
3152 trans = NULL;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003153 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003154 }
Chris Masona79b7d42014-05-22 16:18:52 -07003155
Josef Bacik69fe2d72017-10-19 14:15:57 -04003156 trans->block_rsv = &BTRFS_I(inode)->block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00003157
Chris Masonc8b97812008-10-29 14:49:59 -04003158 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08003159 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04003160 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08003161 BUG_ON(compress_type);
Justin Maggardb430b772017-10-30 15:29:10 -07003162 btrfs_qgroup_free_data(inode, NULL, ordered_extent->file_offset,
3163 ordered_extent->len);
Nikolay Borisov7a6d7062017-02-20 13:50:48 +02003164 ret = btrfs_mark_extent_written(trans, BTRFS_I(inode),
Yan Zhengd899e052008-10-30 14:25:28 -04003165 ordered_extent->file_offset,
3166 ordered_extent->file_offset +
Josef Bacik77cef2e2013-08-29 13:57:21 -04003167 logical_len);
Yan Zhengd899e052008-10-30 14:25:28 -04003168 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003169 BUG_ON(root == fs_info->tree_root);
Yan Zhengd899e052008-10-30 14:25:28 -04003170 ret = insert_reserved_file_extent(trans, inode,
3171 ordered_extent->file_offset,
3172 ordered_extent->start,
3173 ordered_extent->disk_len,
Josef Bacik77cef2e2013-08-29 13:57:21 -04003174 logical_len, logical_len,
Li Zefan261507a02010-12-17 14:21:50 +08003175 compress_type, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04003176 BTRFS_FILE_EXTENT_REG);
Josef Bacik49940bd2018-10-11 15:54:21 -04003177 if (!ret) {
3178 clear_reserved_extent = false;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003179 btrfs_release_delalloc_bytes(fs_info,
Miao Xiee570fd22014-06-19 10:42:50 +08003180 ordered_extent->start,
3181 ordered_extent->disk_len);
Josef Bacik49940bd2018-10-11 15:54:21 -04003182 }
Yan Zhengd899e052008-10-30 14:25:28 -04003183 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04003184 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
3185 ordered_extent->file_offset, ordered_extent->len,
3186 trans->transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003187 if (ret < 0) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003188 btrfs_abort_transaction(trans, ret);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003189 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003190 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00003191
Nikolay Borisovac01f262018-01-08 10:59:43 +02003192 ret = add_pending_csums(trans, inode, &ordered_extent->list);
3193 if (ret) {
3194 btrfs_abort_transaction(trans, ret);
3195 goto out;
3196 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04003197
Josef Bacik6c760c02012-11-09 10:53:21 -05003198 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
3199 ret = btrfs_update_inode_fallback(trans, root, inode);
3200 if (ret) { /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04003201 btrfs_abort_transaction(trans, ret);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003202 goto out;
Josef Bacik1ef30be2011-04-05 19:25:36 -04003203 }
3204 ret = 0;
Yan, Zhengc2167752009-11-12 09:34:21 +00003205out:
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003206 if (range_locked || clear_new_delalloc_bytes) {
3207 unsigned int clear_bits = 0;
3208
3209 if (range_locked)
3210 clear_bits |= EXTENT_LOCKED;
3211 if (clear_new_delalloc_bytes)
3212 clear_bits |= EXTENT_DELALLOC_NEW;
3213 clear_extent_bit(&BTRFS_I(inode)->io_tree,
3214 ordered_extent->file_offset,
3215 ordered_extent->file_offset +
3216 ordered_extent->len - 1,
3217 clear_bits,
3218 (clear_bits & EXTENT_LOCKED) ? 1 : 0,
David Sterbaae0f1622017-10-31 16:37:52 +01003219 0, &cached_state);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003220 }
3221
Miao Xiea698d0752012-09-20 01:51:59 -06003222 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003223 btrfs_end_transaction(trans);
Josef Bacik0cb59c92010-07-02 12:14:14 -04003224
Josef Bacik77cef2e2013-08-29 13:57:21 -04003225 if (ret || truncated) {
3226 u64 start, end;
3227
3228 if (truncated)
3229 start = ordered_extent->file_offset + logical_len;
3230 else
3231 start = ordered_extent->file_offset;
3232 end = ordered_extent->file_offset + ordered_extent->len - 1;
David Sterbaf08dc362017-10-31 17:02:39 +01003233 clear_extent_uptodate(io_tree, start, end, NULL);
Josef Bacik77cef2e2013-08-29 13:57:21 -04003234
3235 /* Drop the cache for the part of the extent we didn't write. */
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02003236 btrfs_drop_extent_cache(BTRFS_I(inode), start, end, 0);
Josef Bacik5fd02042012-05-02 14:00:54 -04003237
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003238 /*
3239 * If the ordered extent had an IOERR or something else went
3240 * wrong we need to return the space for this ordered extent
Josef Bacik77cef2e2013-08-29 13:57:21 -04003241 * back to the allocator. We only free the extent in the
3242 * truncated case if we didn't write out the extent at all.
Josef Bacik49940bd2018-10-11 15:54:21 -04003243 *
3244 * If we made it past insert_reserved_file_extent before we
3245 * errored out then we don't need to do this as the accounting
3246 * has already been done.
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003247 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04003248 if ((ret || !logical_len) &&
Josef Bacik49940bd2018-10-11 15:54:21 -04003249 clear_reserved_extent &&
Josef Bacik77cef2e2013-08-29 13:57:21 -04003250 !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003251 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003252 btrfs_free_reserved_extent(fs_info,
3253 ordered_extent->start,
Miao Xiee570fd22014-06-19 10:42:50 +08003254 ordered_extent->disk_len, 1);
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003255 }
3256
3257
Josef Bacik5fd02042012-05-02 14:00:54 -04003258 /*
Liu Bo8bad3c02012-06-18 12:14:23 +08003259 * This needs to be done to make sure anybody waiting knows we are done
3260 * updating everything for this ordered extent.
Josef Bacik5fd02042012-05-02 14:00:54 -04003261 */
3262 btrfs_remove_ordered_extent(inode, ordered_extent);
3263
Liu Bo38c227d2013-01-29 03:18:40 +00003264 /* for snapshot-aware defrag */
Liu Bo6f519562013-10-29 10:45:05 +08003265 if (new) {
3266 if (ret) {
3267 free_sa_defrag_extent(new);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003268 atomic_dec(&fs_info->defrag_running);
Liu Bo6f519562013-10-29 10:45:05 +08003269 } else {
3270 relink_file_extents(new);
3271 }
3272 }
Liu Bo38c227d2013-01-29 03:18:40 +00003273
Chris Masone6dcd2d2008-07-17 12:53:50 -04003274 /* once for us */
3275 btrfs_put_ordered_extent(ordered_extent);
3276 /* once for the tree */
3277 btrfs_put_ordered_extent(ordered_extent);
3278
Josef Bacik5fd02042012-05-02 14:00:54 -04003279 return ret;
3280}
3281
3282static void finish_ordered_fn(struct btrfs_work *work)
3283{
3284 struct btrfs_ordered_extent *ordered_extent;
3285 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
3286 btrfs_finish_ordered_io(ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003287}
3288
Nikolay Borisovc6297322018-11-08 10:18:08 +02003289void btrfs_writepage_endio_finish_ordered(struct page *page, u64 start,
3290 u64 end, int uptodate)
Chris Mason211f90e2008-07-18 11:56:15 -04003291{
Josef Bacik5fd02042012-05-02 14:00:54 -04003292 struct inode *inode = page->mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003293 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik5fd02042012-05-02 14:00:54 -04003294 struct btrfs_ordered_extent *ordered_extent = NULL;
Liu Bo9e0af232014-08-15 23:36:53 +08003295 struct btrfs_workqueue *wq;
Josef Bacik5fd02042012-05-02 14:00:54 -04003296
liubo1abe9b82011-03-24 11:18:59 +00003297 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
3298
Chris Mason8b62b722009-09-02 16:53:46 -04003299 ClearPagePrivate2(page);
Josef Bacik5fd02042012-05-02 14:00:54 -04003300 if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
3301 end - start + 1, uptodate))
David Sterbac3988d62017-02-17 15:18:32 +01003302 return;
Josef Bacik5fd02042012-05-02 14:00:54 -04003303
Omar Sandovala0cac0e2019-09-16 11:30:57 -07003304 if (btrfs_is_free_space_inode(BTRFS_I(inode)))
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003305 wq = fs_info->endio_freespace_worker;
Omar Sandovala0cac0e2019-09-16 11:30:57 -07003306 else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003307 wq = fs_info->endio_write_workers;
Josef Bacik5fd02042012-05-02 14:00:54 -04003308
Omar Sandovala0cac0e2019-09-16 11:30:57 -07003309 btrfs_init_work(&ordered_extent->work, finish_ordered_fn, NULL, NULL);
Liu Bo9e0af232014-08-15 23:36:53 +08003310 btrfs_queue_work(wq, &ordered_extent->work);
Chris Mason211f90e2008-07-18 11:56:15 -04003311}
3312
Miao Xiedc380ae2014-09-12 18:43:55 +08003313static int __readpage_endio_check(struct inode *inode,
3314 struct btrfs_io_bio *io_bio,
3315 int icsum, struct page *page,
3316 int pgoff, u64 start, size_t len)
3317{
Johannes Thumshirnd5178572019-06-03 16:58:57 +02003318 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
3319 SHASH_DESC_ON_STACK(shash, fs_info->csum_shash);
Miao Xiedc380ae2014-09-12 18:43:55 +08003320 char *kaddr;
Johannes Thumshirnd5178572019-06-03 16:58:57 +02003321 u16 csum_size = btrfs_super_csum_size(fs_info->super_copy);
3322 u8 *csum_expected;
3323 u8 csum[BTRFS_CSUM_SIZE];
Miao Xiedc380ae2014-09-12 18:43:55 +08003324
Johannes Thumshirnd5178572019-06-03 16:58:57 +02003325 csum_expected = ((u8 *)io_bio->csum) + icsum * csum_size;
Miao Xiedc380ae2014-09-12 18:43:55 +08003326
3327 kaddr = kmap_atomic(page);
Johannes Thumshirnd5178572019-06-03 16:58:57 +02003328 shash->tfm = fs_info->csum_shash;
3329
3330 crypto_shash_init(shash);
3331 crypto_shash_update(shash, kaddr + pgoff, len);
3332 crypto_shash_final(shash, csum);
3333
3334 if (memcmp(csum, csum_expected, csum_size))
Miao Xiedc380ae2014-09-12 18:43:55 +08003335 goto zeroit;
3336
3337 kunmap_atomic(kaddr);
3338 return 0;
3339zeroit:
Johannes Thumshirnea41d6b2019-06-03 16:58:58 +02003340 btrfs_print_data_csum_error(BTRFS_I(inode), start, csum, csum_expected,
3341 io_bio->mirror_num);
Miao Xiedc380ae2014-09-12 18:43:55 +08003342 memset(kaddr + pgoff, 1, len);
3343 flush_dcache_page(page);
3344 kunmap_atomic(kaddr);
Miao Xiedc380ae2014-09-12 18:43:55 +08003345 return -EIO;
3346}
3347
Chris Masond352ac62008-09-29 15:18:18 -04003348/*
Chris Masond352ac62008-09-29 15:18:18 -04003349 * when reads are done, we need to check csums to verify the data is correct
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02003350 * if there's a match, we allow the bio to finish. If not, the code in
3351 * extent_io.c will try to find good copies for us.
Chris Masond352ac62008-09-29 15:18:18 -04003352 */
Miao Xiefacc8a222013-07-25 19:22:34 +08003353static int btrfs_readpage_end_io_hook(struct btrfs_io_bio *io_bio,
3354 u64 phy_offset, struct page *page,
3355 u64 start, u64 end, int mirror)
Chris Mason07157aa2007-08-30 08:50:51 -04003356{
Miao Xie4eee4fa2012-12-21 09:17:45 +00003357 size_t offset = start - page_offset(page);
Chris Mason07157aa2007-08-30 08:50:51 -04003358 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05003359 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masonff79f812007-10-15 16:22:25 -04003360 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -05003361
Chris Masond20f7042008-12-08 16:58:54 -05003362 if (PageChecked(page)) {
3363 ClearPageChecked(page);
Miao Xiedc380ae2014-09-12 18:43:55 +08003364 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003365 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003366
3367 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Miao Xiedc380ae2014-09-12 18:43:55 +08003368 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003369
Yan Zheng17d217f2008-12-12 10:03:38 -05003370 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04003371 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
David Sterba91166212016-04-26 23:54:39 +02003372 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM);
Yan Zheng17d217f2008-12-12 10:03:38 -05003373 return 0;
3374 }
3375
Miao Xiefacc8a222013-07-25 19:22:34 +08003376 phy_offset >>= inode->i_sb->s_blocksize_bits;
Miao Xiedc380ae2014-09-12 18:43:55 +08003377 return __readpage_endio_check(inode, io_bio, phy_offset, page, offset,
3378 start, (size_t)(end - start + 1));
Chris Mason07157aa2007-08-30 08:50:51 -04003379}
Chris Masonb888db22007-08-27 16:49:44 -04003380
Nikolay Borisovc1c3fac2018-01-16 09:31:58 +02003381/*
3382 * btrfs_add_delayed_iput - perform a delayed iput on @inode
3383 *
3384 * @inode: The inode we want to perform iput on
3385 *
3386 * This function uses the generic vfs_inode::i_count to track whether we should
3387 * just decrement it (in case it's > 1) or if this is the last iput then link
3388 * the inode to the delayed iput machinery. Delayed iputs are processed at
3389 * transaction commit time/superblock commit/cleaner kthread.
3390 */
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003391void btrfs_add_delayed_iput(struct inode *inode)
3392{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003393 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
David Sterba8089fe62015-11-19 14:15:51 +01003394 struct btrfs_inode *binode = BTRFS_I(inode);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003395
3396 if (atomic_add_unless(&inode->i_count, -1, 1))
3397 return;
3398
Josef Bacik034f7842018-12-03 11:06:52 -05003399 atomic_inc(&fs_info->nr_delayed_iputs);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003400 spin_lock(&fs_info->delayed_iput_lock);
Nikolay Borisovc1c3fac2018-01-16 09:31:58 +02003401 ASSERT(list_empty(&binode->delayed_iput));
3402 list_add_tail(&binode->delayed_iput, &fs_info->delayed_iputs);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003403 spin_unlock(&fs_info->delayed_iput_lock);
Josef Bacikfd340d02019-01-11 10:21:02 -05003404 if (!test_bit(BTRFS_FS_CLEANER_RUNNING, &fs_info->flags))
3405 wake_up_process(fs_info->cleaner_kthread);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003406}
3407
Josef Bacik63611e72019-06-18 10:59:18 -04003408static void run_delayed_iput_locked(struct btrfs_fs_info *fs_info,
3409 struct btrfs_inode *inode)
3410{
3411 list_del_init(&inode->delayed_iput);
3412 spin_unlock(&fs_info->delayed_iput_lock);
3413 iput(&inode->vfs_inode);
3414 if (atomic_dec_and_test(&fs_info->nr_delayed_iputs))
3415 wake_up(&fs_info->delayed_iputs_wait);
3416 spin_lock(&fs_info->delayed_iput_lock);
3417}
3418
3419static void btrfs_run_delayed_iput(struct btrfs_fs_info *fs_info,
3420 struct btrfs_inode *inode)
3421{
3422 if (!list_empty(&inode->delayed_iput)) {
3423 spin_lock(&fs_info->delayed_iput_lock);
3424 if (!list_empty(&inode->delayed_iput))
3425 run_delayed_iput_locked(fs_info, inode);
3426 spin_unlock(&fs_info->delayed_iput_lock);
3427 }
3428}
3429
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003430void btrfs_run_delayed_iputs(struct btrfs_fs_info *fs_info)
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003431{
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003432
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003433 spin_lock(&fs_info->delayed_iput_lock);
David Sterba8089fe62015-11-19 14:15:51 +01003434 while (!list_empty(&fs_info->delayed_iputs)) {
3435 struct btrfs_inode *inode;
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003436
David Sterba8089fe62015-11-19 14:15:51 +01003437 inode = list_first_entry(&fs_info->delayed_iputs,
3438 struct btrfs_inode, delayed_iput);
Josef Bacik63611e72019-06-18 10:59:18 -04003439 run_delayed_iput_locked(fs_info, inode);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003440 }
David Sterba8089fe62015-11-19 14:15:51 +01003441 spin_unlock(&fs_info->delayed_iput_lock);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003442}
3443
Josef Bacik034f7842018-12-03 11:06:52 -05003444/**
3445 * btrfs_wait_on_delayed_iputs - wait on the delayed iputs to be done running
3446 * @fs_info - the fs_info for this fs
3447 * @return - EINTR if we were killed, 0 if nothing's pending
3448 *
3449 * This will wait on any delayed iputs that are currently running with KILLABLE
3450 * set. Once they are all done running we will return, unless we are killed in
3451 * which case we return EINTR. This helps in user operations like fallocate etc
3452 * that might get blocked on the iputs.
3453 */
3454int btrfs_wait_on_delayed_iputs(struct btrfs_fs_info *fs_info)
3455{
3456 int ret = wait_event_killable(fs_info->delayed_iputs_wait,
3457 atomic_read(&fs_info->nr_delayed_iputs) == 0);
3458 if (ret)
3459 return -EINTR;
3460 return 0;
3461}
3462
Yan, Zhengd68fc572010-05-16 10:49:58 -04003463/*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003464 * This creates an orphan entry for the given inode in case something goes wrong
3465 * in the middle of an unlink.
Josef Bacik7b128762008-07-24 12:17:14 -04003466 */
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003467int btrfs_orphan_add(struct btrfs_trans_handle *trans,
Omar Sandoval27919062018-05-11 13:13:37 -07003468 struct btrfs_inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003469{
Yan, Zhengd68fc572010-05-16 10:49:58 -04003470 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003471
Omar Sandoval27919062018-05-11 13:13:37 -07003472 ret = btrfs_insert_orphan_item(trans, inode->root, btrfs_ino(inode));
3473 if (ret && ret != -EEXIST) {
3474 btrfs_abort_transaction(trans, ret);
3475 return ret;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003476 }
3477
Yan, Zhengd68fc572010-05-16 10:49:58 -04003478 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003479}
3480
3481/*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003482 * We have done the delete so we can go ahead and remove the orphan item for
3483 * this particular inode.
Josef Bacik7b128762008-07-24 12:17:14 -04003484 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00003485static int btrfs_orphan_del(struct btrfs_trans_handle *trans,
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003486 struct btrfs_inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003487{
Omar Sandoval27919062018-05-11 13:13:37 -07003488 return btrfs_del_orphan_item(trans, inode->root, btrfs_ino(inode));
Josef Bacik7b128762008-07-24 12:17:14 -04003489}
3490
3491/*
3492 * this cleans up any orphans that may be left on the list from the last use
3493 * of this root.
3494 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003495int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04003496{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003497 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik7b128762008-07-24 12:17:14 -04003498 struct btrfs_path *path;
3499 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04003500 struct btrfs_key key, found_key;
3501 struct btrfs_trans_handle *trans;
3502 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003503 u64 last_objectid = 0;
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003504 int ret = 0, nr_unlink = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003505
Yan, Zhengd68fc572010-05-16 10:49:58 -04003506 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003507 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00003508
3509 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003510 if (!path) {
3511 ret = -ENOMEM;
3512 goto out;
3513 }
David Sterbae4058b52015-11-27 16:31:35 +01003514 path->reada = READA_BACK;
Josef Bacik7b128762008-07-24 12:17:14 -04003515
3516 key.objectid = BTRFS_ORPHAN_OBJECTID;
David Sterba962a2982014-06-04 18:41:45 +02003517 key.type = BTRFS_ORPHAN_ITEM_KEY;
Josef Bacik7b128762008-07-24 12:17:14 -04003518 key.offset = (u64)-1;
3519
Josef Bacik7b128762008-07-24 12:17:14 -04003520 while (1) {
3521 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003522 if (ret < 0)
3523 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003524
3525 /*
3526 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003527 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04003528 * find the key and see if we have stuff that matches
3529 */
3530 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003531 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003532 if (path->slots[0] == 0)
3533 break;
3534 path->slots[0]--;
3535 }
3536
3537 /* pull out the item */
3538 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04003539 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3540
3541 /* make sure the item matches what we want */
3542 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
3543 break;
David Sterba962a2982014-06-04 18:41:45 +02003544 if (found_key.type != BTRFS_ORPHAN_ITEM_KEY)
Josef Bacik7b128762008-07-24 12:17:14 -04003545 break;
3546
3547 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02003548 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04003549
3550 /*
3551 * this is where we are basically btrfs_lookup, without the
3552 * crossing root thing. we store the inode number in the
3553 * offset of the orphan item.
3554 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003555
3556 if (found_key.offset == last_objectid) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003557 btrfs_err(fs_info,
3558 "Error removing orphan entry, stopping orphan cleanup");
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003559 ret = -EINVAL;
3560 goto out;
3561 }
3562
3563 last_objectid = found_key.offset;
3564
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003565 found_key.objectid = found_key.offset;
3566 found_key.type = BTRFS_INODE_ITEM_KEY;
3567 found_key.offset = 0;
David Sterba4c66e0d2019-10-03 19:09:35 +02003568 inode = btrfs_iget(fs_info->sb, &found_key, root);
Rusty Russell8c6ffba2013-07-15 11:20:32 +09303569 ret = PTR_ERR_OR_ZERO(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003570 if (ret && ret != -ENOENT)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003571 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003572
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003573 if (ret == -ENOENT && root == fs_info->tree_root) {
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003574 struct btrfs_root *dead_root;
3575 struct btrfs_fs_info *fs_info = root->fs_info;
3576 int is_dead_root = 0;
3577
3578 /*
3579 * this is an orphan in the tree root. Currently these
3580 * could come from 2 sources:
3581 * a) a snapshot deletion in progress
3582 * b) a free space cache inode
3583 * We need to distinguish those two, as the snapshot
3584 * orphan must not get deleted.
3585 * find_dead_roots already ran before us, so if this
3586 * is a snapshot deletion, we should find the root
3587 * in the dead_roots list
3588 */
3589 spin_lock(&fs_info->trans_lock);
3590 list_for_each_entry(dead_root, &fs_info->dead_roots,
3591 root_list) {
3592 if (dead_root->root_key.objectid ==
3593 found_key.objectid) {
3594 is_dead_root = 1;
3595 break;
3596 }
3597 }
3598 spin_unlock(&fs_info->trans_lock);
3599 if (is_dead_root) {
3600 /* prevent this orphan from being found again */
3601 key.offset = found_key.objectid - 1;
3602 continue;
3603 }
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003604
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003605 }
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003606
Josef Bacika8c9e572011-09-21 16:55:59 -04003607 /*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003608 * If we have an inode with links, there are a couple of
3609 * possibilities. Old kernels (before v3.12) used to create an
3610 * orphan item for truncate indicating that there were possibly
3611 * extent items past i_size that needed to be deleted. In v3.12,
3612 * truncate was changed to update i_size in sync with the extent
3613 * items, but the (useless) orphan item was still created. Since
3614 * v4.18, we don't create the orphan item for truncate at all.
3615 *
3616 * So, this item could mean that we need to do a truncate, but
3617 * only if this filesystem was last used on a pre-v3.12 kernel
3618 * and was not cleanly unmounted. The odds of that are quite
3619 * slim, and it's a pain to do the truncate now, so just delete
3620 * the orphan item.
3621 *
3622 * It's also possible that this orphan item was supposed to be
3623 * deleted but wasn't. The inode number may have been reused,
3624 * but either way, we can delete the orphan item.
Josef Bacika8c9e572011-09-21 16:55:59 -04003625 */
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003626 if (ret == -ENOENT || inode->i_nlink) {
3627 if (!ret)
3628 iput(inode);
Josef Bacika8c9e572011-09-21 16:55:59 -04003629 trans = btrfs_start_transaction(root, 1);
3630 if (IS_ERR(trans)) {
3631 ret = PTR_ERR(trans);
3632 goto out;
3633 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003634 btrfs_debug(fs_info, "auto deleting %Lu",
3635 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003636 ret = btrfs_del_orphan_item(trans, root,
3637 found_key.objectid);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003638 btrfs_end_transaction(trans);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003639 if (ret)
3640 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003641 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003642 }
Josef Bacik7b128762008-07-24 12:17:14 -04003643
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003644 nr_unlink++;
Josef Bacik7b128762008-07-24 12:17:14 -04003645
3646 /* this will do delete_inode and everything for us */
3647 iput(inode);
3648 }
Miao Xie3254c872011-11-10 20:45:05 -05003649 /* release the path since we're done with it */
3650 btrfs_release_path(path);
3651
Yan, Zhengd68fc572010-05-16 10:49:58 -04003652 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
3653
Omar Sandovala575cee2018-05-11 13:13:38 -07003654 if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003655 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003656 if (!IS_ERR(trans))
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003657 btrfs_end_transaction(trans);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003658 }
Josef Bacik7b128762008-07-24 12:17:14 -04003659
3660 if (nr_unlink)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003661 btrfs_debug(fs_info, "unlinked %d orphans", nr_unlink);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003662
3663out:
3664 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003665 btrfs_err(fs_info, "could not do orphan cleanup %d", ret);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003666 btrfs_free_path(path);
3667 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003668}
3669
Chris Masond352ac62008-09-29 15:18:18 -04003670/*
Chris Mason46a53cc2009-04-27 11:47:50 -04003671 * very simple check to peek ahead in the leaf looking for xattrs. If we
3672 * don't find any xattrs, we know there can't be any acls.
3673 *
3674 * slot is the slot the inode is in, objectid is the objectid of the inode
3675 */
3676static noinline int acls_after_inode_item(struct extent_buffer *leaf,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003677 int slot, u64 objectid,
3678 int *first_xattr_slot)
Chris Mason46a53cc2009-04-27 11:47:50 -04003679{
3680 u32 nritems = btrfs_header_nritems(leaf);
3681 struct btrfs_key found_key;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003682 static u64 xattr_access = 0;
3683 static u64 xattr_default = 0;
Chris Mason46a53cc2009-04-27 11:47:50 -04003684 int scanned = 0;
3685
Josef Bacikf23b5a52013-06-19 10:16:26 -04003686 if (!xattr_access) {
Andreas Gruenbacher97d79292015-12-02 14:44:35 +01003687 xattr_access = btrfs_name_hash(XATTR_NAME_POSIX_ACL_ACCESS,
3688 strlen(XATTR_NAME_POSIX_ACL_ACCESS));
3689 xattr_default = btrfs_name_hash(XATTR_NAME_POSIX_ACL_DEFAULT,
3690 strlen(XATTR_NAME_POSIX_ACL_DEFAULT));
Josef Bacikf23b5a52013-06-19 10:16:26 -04003691 }
3692
Chris Mason46a53cc2009-04-27 11:47:50 -04003693 slot++;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003694 *first_xattr_slot = -1;
Chris Mason46a53cc2009-04-27 11:47:50 -04003695 while (slot < nritems) {
3696 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3697
3698 /* we found a different objectid, there must not be acls */
3699 if (found_key.objectid != objectid)
3700 return 0;
3701
3702 /* we found an xattr, assume we've got an acl */
Josef Bacikf23b5a52013-06-19 10:16:26 -04003703 if (found_key.type == BTRFS_XATTR_ITEM_KEY) {
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003704 if (*first_xattr_slot == -1)
3705 *first_xattr_slot = slot;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003706 if (found_key.offset == xattr_access ||
3707 found_key.offset == xattr_default)
3708 return 1;
3709 }
Chris Mason46a53cc2009-04-27 11:47:50 -04003710
3711 /*
3712 * we found a key greater than an xattr key, there can't
3713 * be any acls later on
3714 */
3715 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
3716 return 0;
3717
3718 slot++;
3719 scanned++;
3720
3721 /*
3722 * it goes inode, inode backrefs, xattrs, extents,
3723 * so if there are a ton of hard links to an inode there can
3724 * be a lot of backrefs. Don't waste time searching too hard,
3725 * this is just an optimization
3726 */
3727 if (scanned >= 8)
3728 break;
3729 }
3730 /* we hit the end of the leaf before we found an xattr or
3731 * something larger than an xattr. We have to assume the inode
3732 * has acls
3733 */
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003734 if (*first_xattr_slot == -1)
3735 *first_xattr_slot = slot;
Chris Mason46a53cc2009-04-27 11:47:50 -04003736 return 1;
3737}
3738
3739/*
Chris Masond352ac62008-09-29 15:18:18 -04003740 * read an inode from the btree into the in-memory inode
3741 */
Filipe Manana4222ea72018-10-24 10:13:03 +01003742static int btrfs_read_locked_inode(struct inode *inode,
3743 struct btrfs_path *in_path)
Chris Mason39279cc2007-06-12 06:35:45 -04003744{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003745 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Filipe Manana4222ea72018-10-24 10:13:03 +01003746 struct btrfs_path *path = in_path;
Chris Mason5f39d392007-10-15 16:14:19 -04003747 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003748 struct btrfs_inode_item *inode_item;
3749 struct btrfs_root *root = BTRFS_I(inode)->root;
3750 struct btrfs_key location;
Miao Xie67de1172013-12-26 13:07:06 +08003751 unsigned long ptr;
Chris Mason46a53cc2009-04-27 11:47:50 -04003752 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04003753 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04003754 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003755 bool filled = false;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003756 int first_xattr_slot;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003757
3758 ret = btrfs_fill_inode(inode, &rdev);
3759 if (!ret)
3760 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04003761
Filipe Manana4222ea72018-10-24 10:13:03 +01003762 if (!path) {
3763 path = btrfs_alloc_path();
3764 if (!path)
3765 return -ENOMEM;
3766 }
Mark Fasheh1748f842011-07-12 11:25:31 -07003767
Chris Mason39279cc2007-06-12 06:35:45 -04003768 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05003769
Chris Mason39279cc2007-06-12 06:35:45 -04003770 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Filipe Manana67710892016-06-06 11:51:25 +01003771 if (ret) {
Filipe Manana4222ea72018-10-24 10:13:03 +01003772 if (path != in_path)
3773 btrfs_free_path(path);
Al Virof5b3a412018-07-29 23:04:51 +01003774 return ret;
Filipe Manana67710892016-06-06 11:51:25 +01003775 }
Chris Mason39279cc2007-06-12 06:35:45 -04003776
Chris Mason5f39d392007-10-15 16:14:19 -04003777 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00003778
3779 if (filled)
Miao Xie67de1172013-12-26 13:07:06 +08003780 goto cache_index;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003781
Chris Mason5f39d392007-10-15 16:14:19 -04003782 inode_item = btrfs_item_ptr(leaf, path->slots[0],
3783 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003784 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02003785 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08003786 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
3787 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02003788 btrfs_i_size_write(BTRFS_I(inode), btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04003789
David Sterbaa937b972014-12-12 17:39:12 +01003790 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->atime);
3791 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->atime);
Chris Mason5f39d392007-10-15 16:14:19 -04003792
David Sterbaa937b972014-12-12 17:39:12 +01003793 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->mtime);
3794 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->mtime);
Chris Mason5f39d392007-10-15 16:14:19 -04003795
David Sterbaa937b972014-12-12 17:39:12 +01003796 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->ctime);
3797 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->ctime);
Chris Mason5f39d392007-10-15 16:14:19 -04003798
chandan r9cc97d62012-07-04 12:48:07 +05303799 BTRFS_I(inode)->i_otime.tv_sec =
3800 btrfs_timespec_sec(leaf, &inode_item->otime);
3801 BTRFS_I(inode)->i_otime.tv_nsec =
3802 btrfs_timespec_nsec(leaf, &inode_item->otime);
Chris Mason5f39d392007-10-15 16:14:19 -04003803
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003804 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003805 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003806 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
3807
Jeff Laytonc7f88c42017-12-11 06:35:12 -05003808 inode_set_iversion_queried(inode,
3809 btrfs_inode_sequence(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003810 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04003811 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003812 rdev = btrfs_inode_rdev(leaf, inode_item);
3813
Josef Bacikaec74772008-07-24 12:12:38 -04003814 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05003815 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Miao Xie67de1172013-12-26 13:07:06 +08003816
3817cache_index:
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003818 /*
3819 * If we were modified in the current generation and evicted from memory
3820 * and then re-read we need to do a full sync since we don't have any
3821 * idea about which extents were modified before we were evicted from
3822 * cache.
3823 *
3824 * This is required for both inode re-read from disk and delayed inode
3825 * in delayed_nodes_tree.
3826 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003827 if (BTRFS_I(inode)->last_trans == fs_info->generation)
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003828 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3829 &BTRFS_I(inode)->runtime_flags);
3830
Filipe Mananabde6c242015-07-24 00:00:19 +01003831 /*
3832 * We don't persist the id of the transaction where an unlink operation
3833 * against the inode was last made. So here we assume the inode might
3834 * have been evicted, and therefore the exact value of last_unlink_trans
3835 * lost, and set it to last_trans to avoid metadata inconsistencies
3836 * between the inode and its parent if the inode is fsync'ed and the log
3837 * replayed. For example, in the scenario:
3838 *
3839 * touch mydir/foo
3840 * ln mydir/foo mydir/bar
3841 * sync
3842 * unlink mydir/bar
3843 * echo 2 > /proc/sys/vm/drop_caches # evicts inode
3844 * xfs_io -c fsync mydir/foo
3845 * <power failure>
3846 * mount fs, triggers fsync log replay
3847 *
3848 * We must make sure that when we fsync our inode foo we also log its
3849 * parent inode, otherwise after log replay the parent still has the
3850 * dentry with the "bar" name but our inode foo has a link count of 1
3851 * and doesn't have an inode ref with the name "bar" anymore.
3852 *
3853 * Setting last_unlink_trans to last_trans is a pessimistic approach,
Nicholas D Steeves01327612016-05-19 21:18:45 -04003854 * but it guarantees correctness at the expense of occasional full
Filipe Mananabde6c242015-07-24 00:00:19 +01003855 * transaction commits on fsync if our inode is a directory, or if our
3856 * inode is not a directory, logging its parent unnecessarily.
3857 */
3858 BTRFS_I(inode)->last_unlink_trans = BTRFS_I(inode)->last_trans;
3859
Miao Xie67de1172013-12-26 13:07:06 +08003860 path->slots[0]++;
3861 if (inode->i_nlink != 1 ||
3862 path->slots[0] >= btrfs_header_nritems(leaf))
3863 goto cache_acl;
3864
3865 btrfs_item_key_to_cpu(leaf, &location, path->slots[0]);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003866 if (location.objectid != btrfs_ino(BTRFS_I(inode)))
Miao Xie67de1172013-12-26 13:07:06 +08003867 goto cache_acl;
3868
3869 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3870 if (location.type == BTRFS_INODE_REF_KEY) {
3871 struct btrfs_inode_ref *ref;
3872
3873 ref = (struct btrfs_inode_ref *)ptr;
3874 BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref);
3875 } else if (location.type == BTRFS_INODE_EXTREF_KEY) {
3876 struct btrfs_inode_extref *extref;
3877
3878 extref = (struct btrfs_inode_extref *)ptr;
3879 BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf,
3880 extref);
3881 }
Miao Xie2f7e33d2011-06-23 07:27:13 +00003882cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04003883 /*
3884 * try to precache a NULL acl entry for files that don't have
3885 * any xattrs or acls
3886 */
Li Zefan33345d012011-04-20 10:31:50 +08003887 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
David Sterbaf85b7372017-01-20 14:54:07 +01003888 btrfs_ino(BTRFS_I(inode)), &first_xattr_slot);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003889 if (first_xattr_slot != -1) {
3890 path->slots[0] = first_xattr_slot;
3891 ret = btrfs_load_inode_props(inode, path);
3892 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003893 btrfs_err(fs_info,
David Sterba351fd352014-05-15 16:48:20 +02003894 "error loading props for ino %llu (root %llu): %d",
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003895 btrfs_ino(BTRFS_I(inode)),
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003896 root->root_key.objectid, ret);
3897 }
Filipe Manana4222ea72018-10-24 10:13:03 +01003898 if (path != in_path)
3899 btrfs_free_path(path);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003900
Al Viro72c04902009-06-24 16:58:48 -04003901 if (!maybe_acls)
3902 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04003903
Chris Mason39279cc2007-06-12 06:35:45 -04003904 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04003905 case S_IFREG:
3906 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masond1310b22008-01-24 16:13:08 -05003907 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04003908 inode->i_fop = &btrfs_file_operations;
3909 inode->i_op = &btrfs_file_inode_operations;
3910 break;
3911 case S_IFDIR:
3912 inode->i_fop = &btrfs_dir_file_operations;
Omar Sandoval67ade052017-01-25 17:06:38 -08003913 inode->i_op = &btrfs_dir_inode_operations;
Chris Mason39279cc2007-06-12 06:35:45 -04003914 break;
3915 case S_IFLNK:
3916 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -05003917 inode_nohighmem(inode);
Omar Sandoval4779cc02018-09-24 15:16:55 -07003918 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason39279cc2007-06-12 06:35:45 -04003919 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04003920 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05003921 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04003922 init_special_inode(inode, inode->i_mode, rdev);
3923 break;
Chris Mason39279cc2007-06-12 06:35:45 -04003924 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003925
David Sterba7b6a2212018-03-26 18:40:21 +02003926 btrfs_sync_inode_flags_to_i_flags(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003927 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003928}
3929
Chris Masond352ac62008-09-29 15:18:18 -04003930/*
3931 * given a leaf and an inode, copy the inode fields into the leaf
3932 */
Chris Masone02119d2008-09-05 16:13:11 -04003933static void fill_inode_item(struct btrfs_trans_handle *trans,
3934 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04003935 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04003936 struct inode *inode)
3937{
Liu Bo51fab692012-12-27 09:01:21 +00003938 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003939
David Sterbac82f8232019-08-09 17:48:21 +02003940 btrfs_init_map_token(&token, leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04003941
Liu Bo51fab692012-12-27 09:01:21 +00003942 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
3943 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
3944 btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size,
3945 &token);
3946 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3947 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003948
David Sterbaa937b972014-12-12 17:39:12 +01003949 btrfs_set_token_timespec_sec(leaf, &item->atime,
Liu Bo51fab692012-12-27 09:01:21 +00003950 inode->i_atime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003951 btrfs_set_token_timespec_nsec(leaf, &item->atime,
Liu Bo51fab692012-12-27 09:01:21 +00003952 inode->i_atime.tv_nsec, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003953
David Sterbaa937b972014-12-12 17:39:12 +01003954 btrfs_set_token_timespec_sec(leaf, &item->mtime,
Liu Bo51fab692012-12-27 09:01:21 +00003955 inode->i_mtime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003956 btrfs_set_token_timespec_nsec(leaf, &item->mtime,
Liu Bo51fab692012-12-27 09:01:21 +00003957 inode->i_mtime.tv_nsec, &token);
3958
David Sterbaa937b972014-12-12 17:39:12 +01003959 btrfs_set_token_timespec_sec(leaf, &item->ctime,
Liu Bo51fab692012-12-27 09:01:21 +00003960 inode->i_ctime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003961 btrfs_set_token_timespec_nsec(leaf, &item->ctime,
Liu Bo51fab692012-12-27 09:01:21 +00003962 inode->i_ctime.tv_nsec, &token);
3963
chandan r9cc97d62012-07-04 12:48:07 +05303964 btrfs_set_token_timespec_sec(leaf, &item->otime,
3965 BTRFS_I(inode)->i_otime.tv_sec, &token);
3966 btrfs_set_token_timespec_nsec(leaf, &item->otime,
3967 BTRFS_I(inode)->i_otime.tv_nsec, &token);
3968
Liu Bo51fab692012-12-27 09:01:21 +00003969 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3970 &token);
3971 btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation,
3972 &token);
Jeff Laytonc7f88c42017-12-11 06:35:12 -05003973 btrfs_set_token_inode_sequence(leaf, item, inode_peek_iversion(inode),
3974 &token);
Liu Bo51fab692012-12-27 09:01:21 +00003975 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
3976 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3977 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3978 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
Chris Mason39279cc2007-06-12 06:35:45 -04003979}
3980
Chris Masond352ac62008-09-29 15:18:18 -04003981/*
3982 * copy everything in the in-memory inode into the btree.
3983 */
Chris Mason21151332011-11-10 20:39:08 -05003984static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Chris Masond3977122009-01-05 21:25:51 -05003985 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003986{
3987 struct btrfs_inode_item *inode_item;
3988 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003989 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003990 int ret;
3991
3992 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08003993 if (!path)
3994 return -ENOMEM;
3995
Chris Masonb9473432009-03-13 11:00:37 -04003996 path->leave_spinning = 1;
Miao Xie16cdcec2011-04-22 18:12:22 +08003997 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
3998 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003999 if (ret) {
4000 if (ret > 0)
4001 ret = -ENOENT;
4002 goto failed;
4003 }
4004
Chris Mason5f39d392007-10-15 16:14:19 -04004005 leaf = path->nodes[0];
4006 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08004007 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04004008
Chris Masone02119d2008-09-05 16:13:11 -04004009 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04004010 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04004011 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004012 ret = 0;
4013failed:
Chris Mason39279cc2007-06-12 06:35:45 -04004014 btrfs_free_path(path);
4015 return ret;
4016}
4017
Chris Masond352ac62008-09-29 15:18:18 -04004018/*
Chris Mason21151332011-11-10 20:39:08 -05004019 * copy everything in the in-memory inode into the btree.
4020 */
4021noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
4022 struct btrfs_root *root, struct inode *inode)
4023{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004024 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason21151332011-11-10 20:39:08 -05004025 int ret;
4026
4027 /*
4028 * If the inode is a free space inode, we can deadlock during commit
4029 * if we put it into the delayed code.
4030 *
4031 * The data relocation inode should also be directly updated
4032 * without delay
4033 */
Nikolay Borisov70ddc552017-02-20 13:50:35 +02004034 if (!btrfs_is_free_space_inode(BTRFS_I(inode))
Josef Bacik1d52c782014-09-18 11:30:44 -04004035 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004036 && !test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) {
Alexander Block8ea05e32012-07-25 17:35:53 +02004037 btrfs_update_root_times(trans, root);
4038
Chris Mason21151332011-11-10 20:39:08 -05004039 ret = btrfs_delayed_update_inode(trans, root, inode);
4040 if (!ret)
4041 btrfs_set_inode_last_trans(trans, inode);
4042 return ret;
4043 }
4044
4045 return btrfs_update_inode_item(trans, root, inode);
4046}
4047
Josef Bacikbe6aef62012-10-22 15:43:12 -04004048noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
4049 struct btrfs_root *root,
4050 struct inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05004051{
4052 int ret;
4053
4054 ret = btrfs_update_inode(trans, root, inode);
4055 if (ret == -ENOSPC)
4056 return btrfs_update_inode_item(trans, root, inode);
4057 return ret;
4058}
4059
4060/*
Chris Masond352ac62008-09-29 15:18:18 -04004061 * unlink helper that gets used here in inode.c and in the tree logging
4062 * recovery code. It remove a link in a directory with a given name, and
4063 * also drops the back refs in the inode to the directory
4064 */
Al Viro92986792011-03-04 17:14:37 +00004065static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
4066 struct btrfs_root *root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004067 struct btrfs_inode *dir,
4068 struct btrfs_inode *inode,
Al Viro92986792011-03-04 17:14:37 +00004069 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04004070{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004071 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04004072 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04004073 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004074 struct btrfs_dir_item *di;
Josef Bacikaec74772008-07-24 12:12:38 -04004075 u64 index;
Li Zefan33345d012011-04-20 10:31:50 +08004076 u64 ino = btrfs_ino(inode);
4077 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04004078
4079 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04004080 if (!path) {
4081 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00004082 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04004083 }
4084
Chris Masonb9473432009-03-13 11:00:37 -04004085 path->leave_spinning = 1;
Li Zefan33345d012011-04-20 10:31:50 +08004086 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04004087 name, name_len, -1);
Liu Bo3cf50682018-09-12 06:06:26 +08004088 if (IS_ERR_OR_NULL(di)) {
4089 ret = di ? PTR_ERR(di) : -ENOENT;
Chris Mason39279cc2007-06-12 06:35:45 -04004090 goto err;
4091 }
Chris Mason39279cc2007-06-12 06:35:45 -04004092 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04004093 if (ret)
4094 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02004095 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04004096
Miao Xie67de1172013-12-26 13:07:06 +08004097 /*
4098 * If we don't have dir index, we have to get it by looking up
4099 * the inode ref, since we get the inode ref, remove it directly,
4100 * it is unnecessary to do delayed deletion.
4101 *
4102 * But if we have dir index, needn't search inode ref to get it.
4103 * Since the inode ref is close to the inode item, it is better
4104 * that we delay to delete it, and just do this deletion when
4105 * we update the inode item.
4106 */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004107 if (inode->dir_index) {
Miao Xie67de1172013-12-26 13:07:06 +08004108 ret = btrfs_delayed_delete_inode_ref(inode);
4109 if (!ret) {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004110 index = inode->dir_index;
Miao Xie67de1172013-12-26 13:07:06 +08004111 goto skip_backref;
4112 }
4113 }
4114
Li Zefan33345d012011-04-20 10:31:50 +08004115 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
4116 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04004117 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004118 btrfs_info(fs_info,
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00004119 "failed to delete reference to %.*s, inode %llu parent %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02004120 name_len, name, ino, dir_ino);
Jeff Mahoney66642832016-06-10 18:19:25 -04004121 btrfs_abort_transaction(trans, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04004122 goto err;
4123 }
Miao Xie67de1172013-12-26 13:07:06 +08004124skip_backref:
Lu Fengqi9add2942018-08-01 11:32:26 +08004125 ret = btrfs_delete_delayed_dir_index(trans, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004126 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004127 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04004128 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004129 }
Chris Mason39279cc2007-06-12 06:35:45 -04004130
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004131 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len, inode,
4132 dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004133 if (ret != 0 && ret != -ENOENT) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004134 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004135 goto err;
4136 }
Chris Masone02119d2008-09-05 16:13:11 -04004137
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004138 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len, dir,
4139 index);
Chris Mason6418c962010-10-30 07:34:24 -04004140 if (ret == -ENOENT)
4141 ret = 0;
Zach Brownd4e39912013-04-02 21:02:16 +00004142 else if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004143 btrfs_abort_transaction(trans, ret);
Josef Bacik63611e72019-06-18 10:59:18 -04004144
4145 /*
4146 * If we have a pending delayed iput we could end up with the final iput
4147 * being run in btrfs-cleaner context. If we have enough of these built
4148 * up we can end up burning a lot of time in btrfs-cleaner without any
4149 * way to throttle the unlinks. Since we're currently holding a ref on
4150 * the inode we can run the delayed iput here without any issues as the
4151 * final iput won't be done until after we drop the ref we're currently
4152 * holding.
4153 */
4154 btrfs_run_delayed_iput(fs_info, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004155err:
4156 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04004157 if (ret)
4158 goto out;
4159
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004160 btrfs_i_size_write(dir, dir->vfs_inode.i_size - name_len * 2);
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004161 inode_inc_iversion(&inode->vfs_inode);
4162 inode_inc_iversion(&dir->vfs_inode);
4163 inode->vfs_inode.i_ctime = dir->vfs_inode.i_mtime =
4164 dir->vfs_inode.i_ctime = current_time(&inode->vfs_inode);
4165 ret = btrfs_update_inode(trans, root, &dir->vfs_inode);
Chris Masone02119d2008-09-05 16:13:11 -04004166out:
Chris Mason39279cc2007-06-12 06:35:45 -04004167 return ret;
4168}
4169
Al Viro92986792011-03-04 17:14:37 +00004170int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
4171 struct btrfs_root *root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004172 struct btrfs_inode *dir, struct btrfs_inode *inode,
Al Viro92986792011-03-04 17:14:37 +00004173 const char *name, int name_len)
4174{
4175 int ret;
4176 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
4177 if (!ret) {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004178 drop_nlink(&inode->vfs_inode);
4179 ret = btrfs_update_inode(trans, root, &inode->vfs_inode);
Al Viro92986792011-03-04 17:14:37 +00004180 }
4181 return ret;
4182}
Yan, Zhenga22285a2010-05-16 10:48:46 -04004183
4184/*
4185 * helper to start transaction for unlink and rmdir.
4186 *
Josef Bacikd52be812013-05-29 14:54:47 -04004187 * unlink and rmdir are special in btrfs, they do not always free space, so
4188 * if we cannot make our reservations the normal way try and see if there is
4189 * plenty of slack room in the global reserve to migrate, otherwise we cannot
4190 * allow the unlink to occur.
Yan, Zhenga22285a2010-05-16 10:48:46 -04004191 */
Josef Bacikd52be812013-05-29 14:54:47 -04004192static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir)
Yan, Zhenga22285a2010-05-16 10:48:46 -04004193{
Yan, Zhenga22285a2010-05-16 10:48:46 -04004194 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004195
Josef Bacike70bea52011-10-11 14:18:24 -04004196 /*
4197 * 1 for the possible orphan item
4198 * 1 for the dir item
4199 * 1 for the dir index
4200 * 1 for the inode ref
Josef Bacike70bea52011-10-11 14:18:24 -04004201 * 1 for the inode
4202 */
Filipe Manana8eab77f2015-11-13 23:57:16 +00004203 return btrfs_start_transaction_fallback_global_rsv(root, 5, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004204}
4205
Chris Mason39279cc2007-06-12 06:35:45 -04004206static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
4207{
Yan, Zhenga22285a2010-05-16 10:48:46 -04004208 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004209 struct btrfs_trans_handle *trans;
David Howells2b0143b2015-03-17 22:25:59 +00004210 struct inode *inode = d_inode(dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04004211 int ret;
4212
Josef Bacikd52be812013-05-29 14:54:47 -04004213 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004214 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004215 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04004216
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004217 btrfs_record_unlink_dir(trans, BTRFS_I(dir), BTRFS_I(d_inode(dentry)),
4218 0);
Chris Mason12fcfd22009-03-24 10:24:20 -04004219
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004220 ret = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
4221 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
4222 dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00004223 if (ret)
4224 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004225
Yan, Zhenga22285a2010-05-16 10:48:46 -04004226 if (inode->i_nlink == 0) {
Nikolay Borisov73f2e542017-02-20 13:50:59 +02004227 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Tsutomu Itohb5324022011-07-19 07:27:20 +00004228 if (ret)
4229 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004230 }
Josef Bacik7b128762008-07-24 12:17:14 -04004231
Tsutomu Itohb5324022011-07-19 07:27:20 +00004232out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004233 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004234 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04004235 return ret;
4236}
4237
Misono Tomohirof60a2362018-04-18 11:34:52 +09004238static int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
Lu Fengqi401b3b12018-08-01 11:32:30 +08004239 struct inode *dir, u64 objectid,
4240 const char *name, int name_len)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004241{
Lu Fengqi401b3b12018-08-01 11:32:30 +08004242 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004243 struct btrfs_path *path;
4244 struct extent_buffer *leaf;
4245 struct btrfs_dir_item *di;
4246 struct btrfs_key key;
4247 u64 index;
4248 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004249 u64 dir_ino = btrfs_ino(BTRFS_I(dir));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004250
4251 path = btrfs_alloc_path();
4252 if (!path)
4253 return -ENOMEM;
4254
Li Zefan33345d012011-04-20 10:31:50 +08004255 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004256 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004257 if (IS_ERR_OR_NULL(di)) {
Liu Bo3cf50682018-09-12 06:06:26 +08004258 ret = di ? PTR_ERR(di) : -ENOENT;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004259 goto out;
4260 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004261
4262 leaf = path->nodes[0];
4263 btrfs_dir_item_key_to_cpu(leaf, di, &key);
4264 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
4265 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004266 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004267 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004268 goto out;
4269 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004270 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004271
Lu Fengqi3ee1c552018-08-01 11:32:28 +08004272 ret = btrfs_del_root_ref(trans, objectid, root->root_key.objectid,
4273 dir_ino, &index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004274 if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004275 if (ret != -ENOENT) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004276 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004277 goto out;
4278 }
Li Zefan33345d012011-04-20 10:31:50 +08004279 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004280 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004281 if (IS_ERR_OR_NULL(di)) {
4282 if (!di)
4283 ret = -ENOENT;
4284 else
4285 ret = PTR_ERR(di);
Jeff Mahoney66642832016-06-10 18:19:25 -04004286 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004287 goto out;
4288 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004289
4290 leaf = path->nodes[0];
4291 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004292 index = key.offset;
4293 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004294 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004295
Lu Fengqi9add2942018-08-01 11:32:26 +08004296 ret = btrfs_delete_delayed_dir_index(trans, BTRFS_I(dir), index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004297 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004298 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004299 goto out;
4300 }
Miao Xie16cdcec2011-04-22 18:12:22 +08004301
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004302 btrfs_i_size_write(BTRFS_I(dir), dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004303 inode_inc_iversion(dir);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07004304 dir->i_mtime = dir->i_ctime = current_time(dir);
Josef Bacik5a24e842012-08-08 10:12:59 -06004305 ret = btrfs_update_inode_fallback(trans, root, dir);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004306 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004307 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004308out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04004309 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004310 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004311}
4312
Misono Tomohiroec42f162018-04-18 11:34:13 +09004313/*
4314 * Helper to check if the subvolume references other subvolumes or if it's
4315 * default.
4316 */
Misono Tomohirof60a2362018-04-18 11:34:52 +09004317static noinline int may_destroy_subvol(struct btrfs_root *root)
Misono Tomohiroec42f162018-04-18 11:34:13 +09004318{
4319 struct btrfs_fs_info *fs_info = root->fs_info;
4320 struct btrfs_path *path;
4321 struct btrfs_dir_item *di;
4322 struct btrfs_key key;
4323 u64 dir_id;
4324 int ret;
4325
4326 path = btrfs_alloc_path();
4327 if (!path)
4328 return -ENOMEM;
4329
4330 /* Make sure this root isn't set as the default subvol */
4331 dir_id = btrfs_super_root_dir(fs_info->super_copy);
4332 di = btrfs_lookup_dir_item(NULL, fs_info->tree_root, path,
4333 dir_id, "default", 7, 0);
4334 if (di && !IS_ERR(di)) {
4335 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key);
4336 if (key.objectid == root->root_key.objectid) {
4337 ret = -EPERM;
4338 btrfs_err(fs_info,
4339 "deleting default subvolume %llu is not allowed",
4340 key.objectid);
4341 goto out;
4342 }
4343 btrfs_release_path(path);
4344 }
4345
4346 key.objectid = root->root_key.objectid;
4347 key.type = BTRFS_ROOT_REF_KEY;
4348 key.offset = (u64)-1;
4349
4350 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
4351 if (ret < 0)
4352 goto out;
4353 BUG_ON(ret == 0);
4354
4355 ret = 0;
4356 if (path->slots[0] > 0) {
4357 path->slots[0]--;
4358 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
4359 if (key.objectid == root->root_key.objectid &&
4360 key.type == BTRFS_ROOT_REF_KEY)
4361 ret = -ENOTEMPTY;
4362 }
4363out:
4364 btrfs_free_path(path);
4365 return ret;
4366}
4367
Nikolay Borisov20a68002018-04-27 14:36:24 +03004368/* Delete all dentries for inodes belonging to the root */
4369static void btrfs_prune_dentries(struct btrfs_root *root)
4370{
4371 struct btrfs_fs_info *fs_info = root->fs_info;
4372 struct rb_node *node;
4373 struct rb_node *prev;
4374 struct btrfs_inode *entry;
4375 struct inode *inode;
4376 u64 objectid = 0;
4377
4378 if (!test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
4379 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
4380
4381 spin_lock(&root->inode_lock);
4382again:
4383 node = root->inode_tree.rb_node;
4384 prev = NULL;
4385 while (node) {
4386 prev = node;
4387 entry = rb_entry(node, struct btrfs_inode, rb_node);
4388
David Sterba37508512018-06-29 10:56:40 +02004389 if (objectid < btrfs_ino(entry))
Nikolay Borisov20a68002018-04-27 14:36:24 +03004390 node = node->rb_left;
David Sterba37508512018-06-29 10:56:40 +02004391 else if (objectid > btrfs_ino(entry))
Nikolay Borisov20a68002018-04-27 14:36:24 +03004392 node = node->rb_right;
4393 else
4394 break;
4395 }
4396 if (!node) {
4397 while (prev) {
4398 entry = rb_entry(prev, struct btrfs_inode, rb_node);
David Sterba37508512018-06-29 10:56:40 +02004399 if (objectid <= btrfs_ino(entry)) {
Nikolay Borisov20a68002018-04-27 14:36:24 +03004400 node = prev;
4401 break;
4402 }
4403 prev = rb_next(prev);
4404 }
4405 }
4406 while (node) {
4407 entry = rb_entry(node, struct btrfs_inode, rb_node);
David Sterba37508512018-06-29 10:56:40 +02004408 objectid = btrfs_ino(entry) + 1;
Nikolay Borisov20a68002018-04-27 14:36:24 +03004409 inode = igrab(&entry->vfs_inode);
4410 if (inode) {
4411 spin_unlock(&root->inode_lock);
4412 if (atomic_read(&inode->i_count) > 1)
4413 d_prune_aliases(inode);
4414 /*
4415 * btrfs_drop_inode will have it removed from the inode
4416 * cache when its usage count hits zero.
4417 */
4418 iput(inode);
4419 cond_resched();
4420 spin_lock(&root->inode_lock);
4421 goto again;
4422 }
4423
4424 if (cond_resched_lock(&root->inode_lock))
4425 goto again;
4426
4427 node = rb_next(node);
4428 }
4429 spin_unlock(&root->inode_lock);
4430}
4431
Misono Tomohirof60a2362018-04-18 11:34:52 +09004432int btrfs_delete_subvolume(struct inode *dir, struct dentry *dentry)
4433{
4434 struct btrfs_fs_info *fs_info = btrfs_sb(dentry->d_sb);
4435 struct btrfs_root *root = BTRFS_I(dir)->root;
4436 struct inode *inode = d_inode(dentry);
4437 struct btrfs_root *dest = BTRFS_I(inode)->root;
4438 struct btrfs_trans_handle *trans;
4439 struct btrfs_block_rsv block_rsv;
4440 u64 root_flags;
Misono Tomohirof60a2362018-04-18 11:34:52 +09004441 int ret;
4442 int err;
4443
4444 /*
4445 * Don't allow to delete a subvolume with send in progress. This is
4446 * inside the inode lock so the error handling that has to drop the bit
4447 * again is not run concurrently.
4448 */
4449 spin_lock(&dest->root_item_lock);
Lu Fengqia7176f72018-08-04 21:10:53 +08004450 if (dest->send_in_progress) {
Misono Tomohirof60a2362018-04-18 11:34:52 +09004451 spin_unlock(&dest->root_item_lock);
4452 btrfs_warn(fs_info,
4453 "attempt to delete subvolume %llu during send",
4454 dest->root_key.objectid);
4455 return -EPERM;
4456 }
Lu Fengqia7176f72018-08-04 21:10:53 +08004457 root_flags = btrfs_root_flags(&dest->root_item);
4458 btrfs_set_root_flags(&dest->root_item,
4459 root_flags | BTRFS_ROOT_SUBVOL_DEAD);
4460 spin_unlock(&dest->root_item_lock);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004461
4462 down_write(&fs_info->subvol_sem);
4463
4464 err = may_destroy_subvol(dest);
4465 if (err)
4466 goto out_up_write;
4467
4468 btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP);
4469 /*
4470 * One for dir inode,
4471 * two for dir entries,
4472 * two for root ref/backref.
4473 */
Gu JinXiangc4c129d2018-05-30 11:00:38 +08004474 err = btrfs_subvolume_reserve_metadata(root, &block_rsv, 5, true);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004475 if (err)
4476 goto out_up_write;
4477
4478 trans = btrfs_start_transaction(root, 0);
4479 if (IS_ERR(trans)) {
4480 err = PTR_ERR(trans);
4481 goto out_release;
4482 }
4483 trans->block_rsv = &block_rsv;
4484 trans->bytes_reserved = block_rsv.size;
4485
4486 btrfs_record_snapshot_destroy(trans, BTRFS_I(dir));
4487
Lu Fengqi401b3b12018-08-01 11:32:30 +08004488 ret = btrfs_unlink_subvol(trans, dir, dest->root_key.objectid,
4489 dentry->d_name.name, dentry->d_name.len);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004490 if (ret) {
4491 err = ret;
4492 btrfs_abort_transaction(trans, ret);
4493 goto out_end_trans;
4494 }
4495
4496 btrfs_record_root_in_trans(trans, dest);
4497
4498 memset(&dest->root_item.drop_progress, 0,
4499 sizeof(dest->root_item.drop_progress));
4500 dest->root_item.drop_level = 0;
4501 btrfs_set_root_refs(&dest->root_item, 0);
4502
4503 if (!test_and_set_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &dest->state)) {
4504 ret = btrfs_insert_orphan_item(trans,
4505 fs_info->tree_root,
4506 dest->root_key.objectid);
4507 if (ret) {
4508 btrfs_abort_transaction(trans, ret);
4509 err = ret;
4510 goto out_end_trans;
4511 }
4512 }
4513
Lu Fengqid1957792018-05-29 15:01:54 +08004514 ret = btrfs_uuid_tree_remove(trans, dest->root_item.uuid,
Misono Tomohirof60a2362018-04-18 11:34:52 +09004515 BTRFS_UUID_KEY_SUBVOL,
4516 dest->root_key.objectid);
4517 if (ret && ret != -ENOENT) {
4518 btrfs_abort_transaction(trans, ret);
4519 err = ret;
4520 goto out_end_trans;
4521 }
4522 if (!btrfs_is_empty_uuid(dest->root_item.received_uuid)) {
Lu Fengqid1957792018-05-29 15:01:54 +08004523 ret = btrfs_uuid_tree_remove(trans,
Misono Tomohirof60a2362018-04-18 11:34:52 +09004524 dest->root_item.received_uuid,
4525 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
4526 dest->root_key.objectid);
4527 if (ret && ret != -ENOENT) {
4528 btrfs_abort_transaction(trans, ret);
4529 err = ret;
4530 goto out_end_trans;
4531 }
4532 }
4533
4534out_end_trans:
4535 trans->block_rsv = NULL;
4536 trans->bytes_reserved = 0;
4537 ret = btrfs_end_transaction(trans);
4538 if (ret && !err)
4539 err = ret;
4540 inode->i_flags |= S_DEAD;
4541out_release:
4542 btrfs_subvolume_release_metadata(fs_info, &block_rsv);
4543out_up_write:
4544 up_write(&fs_info->subvol_sem);
4545 if (err) {
4546 spin_lock(&dest->root_item_lock);
4547 root_flags = btrfs_root_flags(&dest->root_item);
4548 btrfs_set_root_flags(&dest->root_item,
4549 root_flags & ~BTRFS_ROOT_SUBVOL_DEAD);
4550 spin_unlock(&dest->root_item_lock);
4551 } else {
4552 d_invalidate(dentry);
Nikolay Borisov20a68002018-04-27 14:36:24 +03004553 btrfs_prune_dentries(dest);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004554 ASSERT(dest->send_in_progress == 0);
4555
4556 /* the last ref */
4557 if (dest->ino_cache_inode) {
4558 iput(dest->ino_cache_inode);
4559 dest->ino_cache_inode = NULL;
4560 }
4561 }
4562
4563 return err;
4564}
4565
Chris Mason39279cc2007-06-12 06:35:45 -04004566static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
4567{
David Howells2b0143b2015-03-17 22:25:59 +00004568 struct inode *inode = d_inode(dentry);
Chris Mason1832a6d2007-12-21 16:27:21 -05004569 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004570 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004571 struct btrfs_trans_handle *trans;
Filipe Manana44f714d2016-06-06 16:11:13 +01004572 u64 last_unlink_trans;
Chris Mason39279cc2007-06-12 06:35:45 -04004573
David Sterbab3ae2442012-09-13 16:04:34 -06004574 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Yan134d4512007-10-25 15:49:25 -04004575 return -ENOTEMPTY;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004576 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_FIRST_FREE_OBJECTID)
Misono Tomohiroa79a4642018-04-18 11:35:31 +09004577 return btrfs_delete_subvolume(dir, dentry);
Yan134d4512007-10-25 15:49:25 -04004578
Josef Bacikd52be812013-05-29 14:54:47 -04004579 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004580 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004581 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004582
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004583 if (unlikely(btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Lu Fengqi401b3b12018-08-01 11:32:30 +08004584 err = btrfs_unlink_subvol(trans, dir,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004585 BTRFS_I(inode)->location.objectid,
4586 dentry->d_name.name,
4587 dentry->d_name.len);
4588 goto out;
4589 }
4590
Nikolay Borisov73f2e542017-02-20 13:50:59 +02004591 err = btrfs_orphan_add(trans, BTRFS_I(inode));
Josef Bacik7b128762008-07-24 12:17:14 -04004592 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004593 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004594
Filipe Manana44f714d2016-06-06 16:11:13 +01004595 last_unlink_trans = BTRFS_I(inode)->last_unlink_trans;
4596
Chris Mason39279cc2007-06-12 06:35:45 -04004597 /* now the directory is empty */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004598 err = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
4599 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
4600 dentry->d_name.len);
Filipe Manana44f714d2016-06-06 16:11:13 +01004601 if (!err) {
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004602 btrfs_i_size_write(BTRFS_I(inode), 0);
Filipe Manana44f714d2016-06-06 16:11:13 +01004603 /*
4604 * Propagate the last_unlink_trans value of the deleted dir to
4605 * its parent directory. This is to prevent an unrecoverable
4606 * log tree in the case we do something like this:
4607 * 1) create dir foo
4608 * 2) create snapshot under dir foo
4609 * 3) delete the snapshot
4610 * 4) rmdir foo
4611 * 5) mkdir foo
4612 * 6) fsync foo or some file inside foo
4613 */
4614 if (last_unlink_trans >= trans->transid)
4615 BTRFS_I(dir)->last_unlink_trans = last_unlink_trans;
4616 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004617out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004618 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004619 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39544012007-12-12 14:38:19 -05004620
Chris Mason39279cc2007-06-12 06:35:45 -04004621 return err;
4622}
4623
Josef Bacikddfae632017-10-19 14:16:02 -04004624/*
4625 * Return this if we need to call truncate_block for the last bit of the
4626 * truncate.
4627 */
4628#define NEED_TRUNCATE_BLOCK 1
Filipe Manana0305cd52015-10-16 12:34:25 +01004629
Chris Mason323ac952008-10-01 19:05:46 -04004630/*
Chris Mason39279cc2007-06-12 06:35:45 -04004631 * this can truncate away extent items, csum items and directory items.
4632 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04004633 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04004634 *
4635 * csum items that cross the new i_size are truncated to the new size
4636 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04004637 *
4638 * min_type is the minimum key type to truncate down to. If set to 0, this
4639 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04004640 */
Yan, Zheng80825102009-11-12 09:35:36 +00004641int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
4642 struct btrfs_root *root,
4643 struct inode *inode,
4644 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004645{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004646 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04004647 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004648 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004649 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00004650 struct btrfs_key key;
4651 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04004652 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04004653 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004654 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004655 u64 item_end = 0;
Filipe Mananac1aa4572015-06-20 18:20:09 +01004656 u64 last_size = new_size;
Yan, Zheng80825102009-11-12 09:35:36 +00004657 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04004658 int found_extent;
4659 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05004660 int pending_del_nr = 0;
4661 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04004662 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00004663 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004664 u64 ino = btrfs_ino(BTRFS_I(inode));
Chris Mason28ed1342014-12-17 09:41:04 -08004665 u64 bytes_deleted = 0;
Thomas Meyer897ca812017-10-07 16:02:21 +02004666 bool be_nice = false;
4667 bool should_throttle = false;
Yan, Zheng80825102009-11-12 09:35:36 +00004668
4669 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04004670
Chris Mason28ed1342014-12-17 09:41:04 -08004671 /*
4672 * for non-free space inodes and ref cows, we want to back off from
4673 * time to time
4674 */
Nikolay Borisov70ddc552017-02-20 13:50:35 +02004675 if (!btrfs_is_free_space_inode(BTRFS_I(inode)) &&
Chris Mason28ed1342014-12-17 09:41:04 -08004676 test_bit(BTRFS_ROOT_REF_COWS, &root->state))
Thomas Meyer897ca812017-10-07 16:02:21 +02004677 be_nice = true;
Chris Mason28ed1342014-12-17 09:41:04 -08004678
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004679 path = btrfs_alloc_path();
4680 if (!path)
4681 return -ENOMEM;
David Sterbae4058b52015-11-27 16:31:35 +01004682 path->reada = READA_BACK;
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004683
Josef Bacik5dc562c2012-08-17 13:14:17 -04004684 /*
4685 * We want to drop from the next block forward in case this new size is
4686 * not block aligned since we will be keeping the last block of the
4687 * extent just the way it is.
4688 */
Miao Xie27cdeb72014-04-02 19:51:05 +08004689 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004690 root == fs_info->tree_root)
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02004691 btrfs_drop_extent_cache(BTRFS_I(inode), ALIGN(new_size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004692 fs_info->sectorsize),
Jeff Mahoneyda170662016-06-15 09:22:56 -04004693 (u64)-1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00004694
Miao Xie16cdcec2011-04-22 18:12:22 +08004695 /*
4696 * This function is also used to drop the items in the log tree before
4697 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
Andrea Gelmini52042d82018-11-28 12:05:13 +01004698 * it is used to drop the logged items. So we shouldn't kill the delayed
Miao Xie16cdcec2011-04-22 18:12:22 +08004699 * items.
4700 */
4701 if (min_type == 0 && root == BTRFS_I(inode)->root)
Nikolay Borisov4ccb5c72017-01-10 20:35:38 +02004702 btrfs_kill_delayed_inode_items(BTRFS_I(inode));
Miao Xie16cdcec2011-04-22 18:12:22 +08004703
Li Zefan33345d012011-04-20 10:31:50 +08004704 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04004705 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04004706 key.type = (u8)-1;
4707
Chris Mason85e21ba2008-01-29 15:11:36 -05004708search_again:
Chris Mason28ed1342014-12-17 09:41:04 -08004709 /*
4710 * with a 16K leaf size and 128MB extents, you can actually queue
4711 * up a huge file in a single leaf. Most of the time that
4712 * bytes_deleted is > 0, it will be huge by the time we get here
4713 */
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004714 if (be_nice && bytes_deleted > SZ_32M &&
4715 btrfs_should_end_transaction(trans)) {
4716 ret = -EAGAIN;
Yan, Zheng80825102009-11-12 09:35:36 +00004717 goto out;
4718 }
Chris Masond3977122009-01-05 21:25:51 -05004719
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004720 path->leave_spinning = 1;
4721 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
4722 if (ret < 0)
4723 goto out;
4724
Chris Mason85e21ba2008-01-29 15:11:36 -05004725 if (ret > 0) {
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004726 ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -04004727 /* there are no items in the tree for us to truncate, we're
4728 * done
4729 */
Yan, Zheng80825102009-11-12 09:35:36 +00004730 if (path->slots[0] == 0)
4731 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05004732 path->slots[0]--;
4733 }
4734
Chris Masond3977122009-01-05 21:25:51 -05004735 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04004736 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04004737 leaf = path->nodes[0];
4738 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02004739 found_type = found_key.type;
Chris Mason39279cc2007-06-12 06:35:45 -04004740
Li Zefan33345d012011-04-20 10:31:50 +08004741 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04004742 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004743
Chris Mason85e21ba2008-01-29 15:11:36 -05004744 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004745 break;
4746
Chris Mason5f39d392007-10-15 16:14:19 -04004747 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04004748 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04004749 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04004750 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04004751 extent_type = btrfs_file_extent_type(leaf, fi);
4752 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04004753 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04004754 btrfs_file_extent_num_bytes(leaf, fi);
Liu Bo09ed2f12017-03-10 11:09:48 -08004755
4756 trace_btrfs_truncate_show_fi_regular(
4757 BTRFS_I(inode), leaf, fi,
4758 found_key.offset);
Chris Mason179e29e2007-11-01 11:28:41 -04004759 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Qu Wenruoe41ca582018-06-06 15:41:49 +08004760 item_end += btrfs_file_extent_ram_bytes(leaf,
4761 fi);
Liu Bo09ed2f12017-03-10 11:09:48 -08004762
4763 trace_btrfs_truncate_show_fi_inline(
4764 BTRFS_I(inode), leaf, fi, path->slots[0],
4765 found_key.offset);
Chris Mason39279cc2007-06-12 06:35:45 -04004766 }
Yan008630c2007-11-07 13:31:09 -05004767 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04004768 }
Yan, Zheng80825102009-11-12 09:35:36 +00004769 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04004770 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00004771 } else {
Filipe Manana76b42ab2017-02-14 16:56:01 +00004772 if (item_end < new_size)
Yan, Zheng80825102009-11-12 09:35:36 +00004773 break;
4774 if (found_key.offset >= new_size)
4775 del_item = 1;
4776 else
4777 del_item = 0;
4778 }
Chris Mason39279cc2007-06-12 06:35:45 -04004779 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004780 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04004781 if (found_type != BTRFS_EXTENT_DATA_KEY)
4782 goto delete;
4783
4784 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04004785 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04004786 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05004787 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04004788 u64 orig_num_bytes =
4789 btrfs_file_extent_num_bytes(leaf, fi);
Qu Wenruofda28322013-02-26 08:10:22 +00004790 extent_num_bytes = ALIGN(new_size -
4791 found_key.offset,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004792 fs_info->sectorsize);
Chris Masondb945352007-10-15 16:15:53 -04004793 btrfs_set_file_extent_num_bytes(leaf, fi,
4794 extent_num_bytes);
4795 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05004796 extent_num_bytes);
Miao Xie27cdeb72014-04-02 19:51:05 +08004797 if (test_bit(BTRFS_ROOT_REF_COWS,
4798 &root->state) &&
4799 extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004800 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04004801 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004802 } else {
Chris Masondb945352007-10-15 16:15:53 -04004803 extent_num_bytes =
4804 btrfs_file_extent_disk_num_bytes(leaf,
4805 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004806 extent_offset = found_key.offset -
4807 btrfs_file_extent_offset(leaf, fi);
4808
Chris Mason39279cc2007-06-12 06:35:45 -04004809 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05004810 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004811 if (extent_start != 0) {
4812 found_extent = 1;
Miao Xie27cdeb72014-04-02 19:51:05 +08004813 if (test_bit(BTRFS_ROOT_REF_COWS,
4814 &root->state))
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004815 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04004816 }
4817 }
Chris Mason90692182008-02-08 13:49:28 -05004818 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04004819 /*
4820 * we can't truncate inline items that have had
4821 * special encodings
4822 */
4823 if (!del_item &&
Chris Masonc8b97812008-10-29 14:49:59 -04004824 btrfs_file_extent_encryption(leaf, fi) == 0 &&
Josef Bacikddfae632017-10-19 14:16:02 -04004825 btrfs_file_extent_other_encoding(leaf, fi) == 0 &&
4826 btrfs_file_extent_compression(leaf, fi) == 0) {
4827 u32 size = (u32)(new_size - found_key.offset);
Chris Mason514ac8a2014-01-03 21:07:00 -08004828
Josef Bacikddfae632017-10-19 14:16:02 -04004829 btrfs_set_file_extent_ram_bytes(leaf, fi, size);
4830 size = btrfs_file_extent_calc_inline_size(size);
David Sterba78ac4f92019-03-20 14:49:12 +01004831 btrfs_truncate_item(path, size, 1);
Josef Bacikddfae632017-10-19 14:16:02 -04004832 } else if (!del_item) {
Chris Mason514ac8a2014-01-03 21:07:00 -08004833 /*
Josef Bacikddfae632017-10-19 14:16:02 -04004834 * We have to bail so the last_size is set to
4835 * just before this extent.
Chris Mason514ac8a2014-01-03 21:07:00 -08004836 */
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004837 ret = NEED_TRUNCATE_BLOCK;
Josef Bacikddfae632017-10-19 14:16:02 -04004838 break;
Chris Mason90692182008-02-08 13:49:28 -05004839 }
Josef Bacikddfae632017-10-19 14:16:02 -04004840
4841 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state))
4842 inode_sub_bytes(inode, item_end + 1 - new_size);
Chris Mason39279cc2007-06-12 06:35:45 -04004843 }
Chris Mason179e29e2007-11-01 11:28:41 -04004844delete:
Josef Bacikddfae632017-10-19 14:16:02 -04004845 if (del_item)
4846 last_size = found_key.offset;
4847 else
4848 last_size = new_size;
Chris Mason39279cc2007-06-12 06:35:45 -04004849 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05004850 if (!pending_del_nr) {
4851 /* no pending yet, add ourselves */
4852 pending_del_slot = path->slots[0];
4853 pending_del_nr = 1;
4854 } else if (pending_del_nr &&
4855 path->slots[0] + 1 == pending_del_slot) {
4856 /* hop on the pending chunk */
4857 pending_del_nr++;
4858 pending_del_slot = path->slots[0];
4859 } else {
Chris Masond3977122009-01-05 21:25:51 -05004860 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05004861 }
Chris Mason39279cc2007-06-12 06:35:45 -04004862 } else {
4863 break;
4864 }
Thomas Meyer897ca812017-10-07 16:02:21 +02004865 should_throttle = false;
Chris Mason28f75a02015-02-04 06:59:29 -08004866
Miao Xie27cdeb72014-04-02 19:51:05 +08004867 if (found_extent &&
4868 (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004869 root == fs_info->tree_root)) {
Qu Wenruoffd4bb22019-04-04 14:45:36 +08004870 struct btrfs_ref ref = { 0 };
4871
Chris Masonb9473432009-03-13 11:00:37 -04004872 btrfs_set_path_blocking(path);
Chris Mason28ed1342014-12-17 09:41:04 -08004873 bytes_deleted += extent_num_bytes;
Qu Wenruoffd4bb22019-04-04 14:45:36 +08004874
4875 btrfs_init_generic_ref(&ref, BTRFS_DROP_DELAYED_REF,
4876 extent_start, extent_num_bytes, 0);
4877 ref.real_root = root->root_key.objectid;
4878 btrfs_init_data_ref(&ref, btrfs_header_owner(leaf),
4879 ino, extent_offset);
4880 ret = btrfs_free_extent(trans, &ref);
Omar Sandoval05522102018-05-11 13:13:31 -07004881 if (ret) {
4882 btrfs_abort_transaction(trans, ret);
4883 break;
4884 }
Chris Mason28f75a02015-02-04 06:59:29 -08004885 if (be_nice) {
Lu Fengqi7c861622018-10-11 13:40:36 +08004886 if (btrfs_should_throttle_delayed_refs(trans))
Thomas Meyer897ca812017-10-07 16:02:21 +02004887 should_throttle = true;
Chris Mason28f75a02015-02-04 06:59:29 -08004888 }
Chris Mason39279cc2007-06-12 06:35:45 -04004889 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004890
Yan, Zheng80825102009-11-12 09:35:36 +00004891 if (found_type == BTRFS_INODE_ITEM_KEY)
4892 break;
4893
4894 if (path->slots[0] == 0 ||
Josef Bacik12621332015-02-03 07:50:16 -08004895 path->slots[0] != pending_del_slot ||
Josef Bacik28bad212018-12-03 10:20:38 -05004896 should_throttle) {
Yan, Zheng80825102009-11-12 09:35:36 +00004897 if (pending_del_nr) {
4898 ret = btrfs_del_items(trans, root, path,
4899 pending_del_slot,
4900 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004901 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004902 btrfs_abort_transaction(trans, ret);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004903 break;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004904 }
Yan, Zheng80825102009-11-12 09:35:36 +00004905 pending_del_nr = 0;
4906 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004907 btrfs_release_path(path);
Josef Bacik28bad212018-12-03 10:20:38 -05004908
Chris Mason28f75a02015-02-04 06:59:29 -08004909 /*
Josef Bacik28bad212018-12-03 10:20:38 -05004910 * We can generate a lot of delayed refs, so we need to
4911 * throttle every once and a while and make sure we're
4912 * adding enough space to keep up with the work we are
4913 * generating. Since we hold a transaction here we
4914 * can't flush, and we don't want to FLUSH_LIMIT because
4915 * we could have generated too many delayed refs to
4916 * actually allocate, so just bail if we're short and
4917 * let the normal reservation dance happen higher up.
Chris Mason28f75a02015-02-04 06:59:29 -08004918 */
Josef Bacik28bad212018-12-03 10:20:38 -05004919 if (should_throttle) {
4920 ret = btrfs_delayed_refs_rsv_refill(fs_info,
4921 BTRFS_RESERVE_NO_FLUSH);
4922 if (ret) {
4923 ret = -EAGAIN;
4924 break;
4925 }
Chris Mason28f75a02015-02-04 06:59:29 -08004926 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004927 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00004928 } else {
4929 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05004930 }
Chris Mason39279cc2007-06-12 06:35:45 -04004931 }
Yan, Zheng80825102009-11-12 09:35:36 +00004932out:
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004933 if (ret >= 0 && pending_del_nr) {
4934 int err;
4935
4936 err = btrfs_del_items(trans, root, path, pending_del_slot,
Chris Mason85e21ba2008-01-29 15:11:36 -05004937 pending_del_nr);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004938 if (err) {
4939 btrfs_abort_transaction(trans, err);
4940 ret = err;
4941 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004942 }
Filipe Manana76b42ab2017-02-14 16:56:01 +00004943 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
4944 ASSERT(last_size >= new_size);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004945 if (!ret && last_size > new_size)
Filipe Manana76b42ab2017-02-14 16:56:01 +00004946 last_size = new_size;
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004947 btrfs_ordered_update_i_size(inode, last_size, NULL);
Filipe Manana76b42ab2017-02-14 16:56:01 +00004948 }
Chris Mason28ed1342014-12-17 09:41:04 -08004949
Chris Mason39279cc2007-06-12 06:35:45 -04004950 btrfs_free_path(path);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004951 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04004952}
4953
Chris Masona52d9a82007-08-27 16:49:44 -04004954/*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304955 * btrfs_truncate_block - read, zero a chunk and write a block
Josef Bacik2aaa6652012-08-29 14:27:18 -04004956 * @inode - inode that we're zeroing
4957 * @from - the offset to start zeroing
4958 * @len - the length to zero, 0 to zero the entire range respective to the
4959 * offset
4960 * @front - zero up to the offset instead of from the offset on
4961 *
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304962 * This will find the block for the "from" offset and cow the block and zero the
Josef Bacik2aaa6652012-08-29 14:27:18 -04004963 * part we want to zero. This is used with truncate and hole punching.
Chris Masona52d9a82007-08-27 16:49:44 -04004964 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304965int btrfs_truncate_block(struct inode *inode, loff_t from, loff_t len,
Josef Bacik2aaa6652012-08-29 14:27:18 -04004966 int front)
Chris Masona52d9a82007-08-27 16:49:44 -04004967{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004968 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004969 struct address_space *mapping = inode->i_mapping;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004970 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4971 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004972 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08004973 struct extent_changeset *data_reserved = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004974 char *kaddr;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004975 u32 blocksize = fs_info->sectorsize;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004976 pgoff_t index = from >> PAGE_SHIFT;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304977 unsigned offset = from & (blocksize - 1);
Chris Masona52d9a82007-08-27 16:49:44 -04004978 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004979 gfp_t mask = btrfs_alloc_write_mask(mapping);
Chris Masona52d9a82007-08-27 16:49:44 -04004980 int ret = 0;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304981 u64 block_start;
4982 u64 block_end;
Chris Masona52d9a82007-08-27 16:49:44 -04004983
Nikolay Borisovb03ebd92018-01-18 14:47:06 +02004984 if (IS_ALIGNED(offset, blocksize) &&
4985 (!len || IS_ALIGNED(len, blocksize)))
Chris Masona52d9a82007-08-27 16:49:44 -04004986 goto out;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304987
Josef Bacik8b62f872017-10-19 14:15:55 -04004988 block_start = round_down(from, blocksize);
4989 block_end = block_start + blocksize - 1;
4990
Qu Wenruo364ecf32017-02-27 15:10:38 +08004991 ret = btrfs_delalloc_reserve_space(inode, &data_reserved,
Josef Bacik8b62f872017-10-19 14:15:55 -04004992 block_start, blocksize);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004993 if (ret)
4994 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004995
Chris Mason211c17f2008-05-15 09:13:45 -04004996again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004997 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004998 if (!page) {
Qu Wenruobc42bda2017-02-27 15:10:39 +08004999 btrfs_delalloc_release_space(inode, data_reserved,
Qu Wenruo43b18592017-12-12 15:34:32 +08005000 block_start, blocksize, true);
Qu Wenruo8702ba92019-10-14 14:34:51 +08005001 btrfs_delalloc_release_extents(BTRFS_I(inode), blocksize);
Miao Xieac6a2b32012-12-05 10:56:13 +00005002 ret = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -04005003 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04005004 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04005005
Chris Masona52d9a82007-08-27 16:49:44 -04005006 if (!PageUptodate(page)) {
5007 ret = btrfs_readpage(NULL, page);
5008 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04005009 if (page->mapping != mapping) {
5010 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005011 put_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04005012 goto again;
5013 }
Chris Masona52d9a82007-08-27 16:49:44 -04005014 if (!PageUptodate(page)) {
5015 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04005016 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04005017 }
5018 }
Chris Mason211c17f2008-05-15 09:13:45 -04005019 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04005020
Chandan Rajendra9703fef2016-01-21 15:55:56 +05305021 lock_extent_bits(io_tree, block_start, block_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04005022 set_page_extent_mapped(page);
5023
Chandan Rajendra9703fef2016-01-21 15:55:56 +05305024 ordered = btrfs_lookup_ordered_extent(inode, block_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04005025 if (ordered) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05305026 unlock_extent_cached(io_tree, block_start, block_end,
David Sterbae43bbe52017-12-12 21:43:52 +01005027 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04005028 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005029 put_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04005030 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04005031 btrfs_put_ordered_extent(ordered);
5032 goto again;
5033 }
5034
Chandan Rajendra9703fef2016-01-21 15:55:56 +05305035 clear_extent_bit(&BTRFS_I(inode)->io_tree, block_start, block_end,
Omar Sandovale1821632019-08-15 14:04:04 -07005036 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
5037 0, 0, &cached_state);
Josef Bacik5d5e1032009-10-13 16:46:49 -04005038
Filipe Mananae3b8a482017-11-04 00:16:59 +00005039 ret = btrfs_set_extent_delalloc(inode, block_start, block_end, 0,
Nikolay Borisov330a5822019-07-17 16:18:17 +03005040 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04005041 if (ret) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05305042 unlock_extent_cached(io_tree, block_start, block_end,
David Sterbae43bbe52017-12-12 21:43:52 +01005043 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04005044 goto out_unlock;
5045 }
5046
Chandan Rajendra9703fef2016-01-21 15:55:56 +05305047 if (offset != blocksize) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04005048 if (!len)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05305049 len = blocksize - offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04005050 kaddr = kmap(page);
Josef Bacik2aaa6652012-08-29 14:27:18 -04005051 if (front)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05305052 memset(kaddr + (block_start - page_offset(page)),
5053 0, offset);
Josef Bacik2aaa6652012-08-29 14:27:18 -04005054 else
Chandan Rajendra9703fef2016-01-21 15:55:56 +05305055 memset(kaddr + (block_start - page_offset(page)) + offset,
5056 0, len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04005057 flush_dcache_page(page);
5058 kunmap(page);
5059 }
Chris Mason247e7432008-07-17 12:53:51 -04005060 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04005061 set_page_dirty(page);
David Sterbae43bbe52017-12-12 21:43:52 +01005062 unlock_extent_cached(io_tree, block_start, block_end, &cached_state);
Chris Mason39279cc2007-06-12 06:35:45 -04005063
Chris Mason89642222008-07-24 09:41:53 -04005064out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04005065 if (ret)
Qu Wenruobc42bda2017-02-27 15:10:39 +08005066 btrfs_delalloc_release_space(inode, data_reserved, block_start,
Qu Wenruo43b18592017-12-12 15:34:32 +08005067 blocksize, true);
Qu Wenruo8702ba92019-10-14 14:34:51 +08005068 btrfs_delalloc_release_extents(BTRFS_I(inode), blocksize);
Chris Mason39279cc2007-06-12 06:35:45 -04005069 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005070 put_page(page);
Chris Mason39279cc2007-06-12 06:35:45 -04005071out:
Qu Wenruo364ecf32017-02-27 15:10:38 +08005072 extent_changeset_free(data_reserved);
Chris Mason39279cc2007-06-12 06:35:45 -04005073 return ret;
5074}
5075
Josef Bacik16e75492013-10-22 12:18:51 -04005076static int maybe_insert_hole(struct btrfs_root *root, struct inode *inode,
5077 u64 offset, u64 len)
5078{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005079 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik16e75492013-10-22 12:18:51 -04005080 struct btrfs_trans_handle *trans;
5081 int ret;
5082
5083 /*
5084 * Still need to make sure the inode looks like it's been updated so
5085 * that any holes get logged if we fsync.
5086 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005087 if (btrfs_fs_incompat(fs_info, NO_HOLES)) {
5088 BTRFS_I(inode)->last_trans = fs_info->generation;
Josef Bacik16e75492013-10-22 12:18:51 -04005089 BTRFS_I(inode)->last_sub_trans = root->log_transid;
5090 BTRFS_I(inode)->last_log_commit = root->last_log_commit;
5091 return 0;
5092 }
5093
5094 /*
5095 * 1 - for the one we're dropping
5096 * 1 - for the one we're adding
5097 * 1 - for updating the inode.
5098 */
5099 trans = btrfs_start_transaction(root, 3);
5100 if (IS_ERR(trans))
5101 return PTR_ERR(trans);
5102
5103 ret = btrfs_drop_extents(trans, root, inode, offset, offset + len, 1);
5104 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04005105 btrfs_abort_transaction(trans, ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005106 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04005107 return ret;
5108 }
5109
David Sterbaf85b7372017-01-20 14:54:07 +01005110 ret = btrfs_insert_file_extent(trans, root, btrfs_ino(BTRFS_I(inode)),
5111 offset, 0, 0, len, 0, len, 0, 0, 0);
Josef Bacik16e75492013-10-22 12:18:51 -04005112 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04005113 btrfs_abort_transaction(trans, ret);
Josef Bacik16e75492013-10-22 12:18:51 -04005114 else
5115 btrfs_update_inode(trans, root, inode);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005116 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04005117 return ret;
5118}
5119
Josef Bacik695a0d02011-03-04 15:46:53 -05005120/*
5121 * This function puts in dummy file extents for the area we're creating a hole
5122 * for. So if we are truncating this file to a larger size we need to insert
5123 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
5124 * the range between oldsize and size
5125 */
Josef Bacika41ad392011-01-31 15:30:16 -05005126int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04005127{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005128 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Yan Zheng9036c102008-10-30 14:19:41 -04005129 struct btrfs_root *root = BTRFS_I(inode)->root;
5130 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04005131 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00005132 struct extent_state *cached_state = NULL;
Josef Bacik5dc562c2012-08-17 13:14:17 -04005133 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005134 u64 hole_start = ALIGN(oldsize, fs_info->sectorsize);
5135 u64 block_end = ALIGN(size, fs_info->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04005136 u64 last_byte;
5137 u64 cur_offset;
5138 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04005139 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04005140
Josef Bacika71754f2013-06-17 17:14:39 -04005141 /*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05305142 * If our size started in the middle of a block we need to zero out the
5143 * rest of the block before we expand the i_size, otherwise we could
Josef Bacika71754f2013-06-17 17:14:39 -04005144 * expose stale data.
5145 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05305146 err = btrfs_truncate_block(inode, oldsize, 0, 0);
Josef Bacika71754f2013-06-17 17:14:39 -04005147 if (err)
5148 return err;
5149
Yan Zheng9036c102008-10-30 14:19:41 -04005150 if (size <= hole_start)
5151 return 0;
5152
Nikolay Borisov23d31bd2019-05-07 10:19:23 +03005153 btrfs_lock_and_flush_ordered_range(io_tree, BTRFS_I(inode), hole_start,
5154 block_end - 1, &cached_state);
Yan Zheng9036c102008-10-30 14:19:41 -04005155 cur_offset = hole_start;
5156 while (1) {
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02005157 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, cur_offset,
Yan Zheng9036c102008-10-30 14:19:41 -04005158 block_end - cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005159 if (IS_ERR(em)) {
5160 err = PTR_ERR(em);
Zach Brownf2767952013-01-08 19:37:58 +00005161 em = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005162 break;
5163 }
Yan Zheng9036c102008-10-30 14:19:41 -04005164 last_byte = min(extent_map_end(em), block_end);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005165 last_byte = ALIGN(last_byte, fs_info->sectorsize);
Yan, Zheng80825102009-11-12 09:35:36 +00005166 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04005167 struct extent_map *hole_em;
Yan Zheng9036c102008-10-30 14:19:41 -04005168 hole_size = last_byte - cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00005169
Josef Bacik16e75492013-10-22 12:18:51 -04005170 err = maybe_insert_hole(root, inode, cur_offset,
5171 hole_size);
5172 if (err)
Chris Mason771ed682008-11-06 22:02:51 -05005173 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02005174 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04005175 cur_offset + hole_size - 1, 0);
5176 hole_em = alloc_extent_map();
5177 if (!hole_em) {
5178 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
5179 &BTRFS_I(inode)->runtime_flags);
5180 goto next;
5181 }
5182 hole_em->start = cur_offset;
5183 hole_em->len = hole_size;
5184 hole_em->orig_start = cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00005185
Josef Bacik5dc562c2012-08-17 13:14:17 -04005186 hole_em->block_start = EXTENT_MAP_HOLE;
5187 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05005188 hole_em->orig_block_len = 0;
Josef Bacikcc95bef2013-04-04 14:31:27 -04005189 hole_em->ram_bytes = hole_size;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005190 hole_em->bdev = fs_info->fs_devices->latest_bdev;
Josef Bacik5dc562c2012-08-17 13:14:17 -04005191 hole_em->compress_type = BTRFS_COMPRESS_NONE;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005192 hole_em->generation = fs_info->generation;
Josef Bacik5dc562c2012-08-17 13:14:17 -04005193
5194 while (1) {
5195 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04005196 err = add_extent_mapping(em_tree, hole_em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04005197 write_unlock(&em_tree->lock);
5198 if (err != -EEXIST)
5199 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02005200 btrfs_drop_extent_cache(BTRFS_I(inode),
5201 cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04005202 cur_offset +
5203 hole_size - 1, 0);
5204 }
5205 free_extent_map(hole_em);
Yan Zheng9036c102008-10-30 14:19:41 -04005206 }
Josef Bacik16e75492013-10-22 12:18:51 -04005207next:
Yan Zheng9036c102008-10-30 14:19:41 -04005208 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04005209 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04005210 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00005211 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04005212 break;
5213 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04005214 free_extent_map(em);
David Sterbae43bbe52017-12-12 21:43:52 +01005215 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state);
Yan Zheng9036c102008-10-30 14:19:41 -04005216 return err;
5217}
5218
Eric Sandeen3972f262013-01-12 02:57:22 +00005219static int btrfs_setsize(struct inode *inode, struct iattr *attr)
Yan, Zheng80825102009-11-12 09:35:36 +00005220{
Miao Xief4a2f4c2011-12-14 20:12:01 -05005221 struct btrfs_root *root = BTRFS_I(inode)->root;
5222 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05005223 loff_t oldsize = i_size_read(inode);
Eric Sandeen3972f262013-01-12 02:57:22 +00005224 loff_t newsize = attr->ia_size;
5225 int mask = attr->ia_valid;
Yan, Zheng80825102009-11-12 09:35:36 +00005226 int ret;
5227
Eric Sandeen3972f262013-01-12 02:57:22 +00005228 /*
5229 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
5230 * special case where we need to update the times despite not having
5231 * these flags set. For all other operations the VFS set these flags
5232 * explicitly if it wants a timestamp update.
5233 */
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08005234 if (newsize != oldsize) {
5235 inode_inc_iversion(inode);
5236 if (!(mask & (ATTR_CTIME | ATTR_MTIME)))
5237 inode->i_ctime = inode->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07005238 current_time(inode);
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08005239 }
Eric Sandeen3972f262013-01-12 02:57:22 +00005240
Josef Bacika41ad392011-01-31 15:30:16 -05005241 if (newsize > oldsize) {
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005242 /*
David Sterbaea14b57f2017-06-22 02:19:11 +02005243 * Don't do an expanding truncate while snapshotting is ongoing.
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005244 * This is to ensure the snapshot captures a fully consistent
5245 * state of this file - if the snapshot captures this expanding
5246 * truncation, it must capture all writes that happened before
5247 * this truncation.
5248 */
Zhao Lei0bc19f902016-01-06 18:56:36 +08005249 btrfs_wait_for_snapshot_creation(root);
Josef Bacika41ad392011-01-31 15:30:16 -05005250 ret = btrfs_cont_expand(inode, oldsize, newsize);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005251 if (ret) {
David Sterbaea14b57f2017-06-22 02:19:11 +02005252 btrfs_end_write_no_snapshotting(root);
Yan, Zheng80825102009-11-12 09:35:36 +00005253 return ret;
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005254 }
Yan, Zheng80825102009-11-12 09:35:36 +00005255
Miao Xief4a2f4c2011-12-14 20:12:01 -05005256 trans = btrfs_start_transaction(root, 1);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005257 if (IS_ERR(trans)) {
David Sterbaea14b57f2017-06-22 02:19:11 +02005258 btrfs_end_write_no_snapshotting(root);
Miao Xief4a2f4c2011-12-14 20:12:01 -05005259 return PTR_ERR(trans);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005260 }
Miao Xief4a2f4c2011-12-14 20:12:01 -05005261
5262 i_size_write(inode, newsize);
5263 btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
Chandan Rajendra27772b62016-01-21 15:56:03 +05305264 pagecache_isize_extended(inode, oldsize, newsize);
Miao Xief4a2f4c2011-12-14 20:12:01 -05005265 ret = btrfs_update_inode(trans, root, inode);
David Sterbaea14b57f2017-06-22 02:19:11 +02005266 btrfs_end_write_no_snapshotting(root);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005267 btrfs_end_transaction(trans);
Josef Bacika41ad392011-01-31 15:30:16 -05005268 } else {
Yan, Zheng80825102009-11-12 09:35:36 +00005269
Josef Bacika41ad392011-01-31 15:30:16 -05005270 /*
5271 * We're truncating a file that used to have good data down to
5272 * zero. Make sure it gets into the ordered flush list so that
5273 * any new writes get down to disk quickly.
5274 */
5275 if (newsize == 0)
Josef Bacik72ac3c02012-05-23 14:13:11 -04005276 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
5277 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00005278
Josef Bacika41ad392011-01-31 15:30:16 -05005279 truncate_setsize(inode, newsize);
Miao Xie2e60a512013-02-08 07:01:08 +00005280
Andrea Gelmini52042d82018-11-28 12:05:13 +01005281 /* Disable nonlocked read DIO to avoid the endless truncate */
Nikolay Borisovabcefb12017-02-20 13:51:10 +02005282 btrfs_inode_block_unlocked_dio(BTRFS_I(inode));
Miao Xie2e60a512013-02-08 07:01:08 +00005283 inode_dio_wait(inode);
Nikolay Borisov0b581702017-02-20 13:51:11 +02005284 btrfs_inode_resume_unlocked_dio(BTRFS_I(inode));
Miao Xie2e60a512013-02-08 07:01:08 +00005285
Filipe Manana213e8c52018-02-06 20:40:31 +00005286 ret = btrfs_truncate(inode, newsize == oldsize);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005287 if (ret && inode->i_nlink) {
5288 int err;
5289
5290 /*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07005291 * Truncate failed, so fix up the in-memory size. We
5292 * adjusted disk_i_size down as we removed extents, so
5293 * wait for disk_i_size to be stable and then update the
5294 * in-memory size to match.
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005295 */
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07005296 err = btrfs_wait_ordered_range(inode, 0, (u64)-1);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005297 if (err)
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07005298 return err;
5299 i_size_write(inode, BTRFS_I(inode)->disk_i_size);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005300 }
Yan, Zheng80825102009-11-12 09:35:36 +00005301 }
5302
Josef Bacika41ad392011-01-31 15:30:16 -05005303 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00005304}
5305
Chris Mason39279cc2007-06-12 06:35:45 -04005306static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
5307{
David Howells2b0143b2015-03-17 22:25:59 +00005308 struct inode *inode = d_inode(dentry);
Li Zefanb83cc962010-12-20 16:04:08 +08005309 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005310 int err;
5311
Li Zefanb83cc962010-12-20 16:04:08 +08005312 if (btrfs_root_readonly(root))
5313 return -EROFS;
5314
Jan Kara31051c82016-05-26 16:55:18 +02005315 err = setattr_prepare(dentry, attr);
Chris Mason39279cc2007-06-12 06:35:45 -04005316 if (err)
5317 return err;
5318
Chris Mason5a3f23d2009-03-31 13:27:11 -04005319 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Eric Sandeen3972f262013-01-12 02:57:22 +00005320 err = btrfs_setsize(inode, attr);
Yan, Zheng80825102009-11-12 09:35:36 +00005321 if (err)
5322 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005323 }
Yan Zheng9036c102008-10-30 14:19:41 -04005324
Christoph Hellwig10257742010-06-04 11:30:02 +02005325 if (attr->ia_valid) {
5326 setattr_copy(inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005327 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005328 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04005329
Josef Bacik22c44fe2011-11-30 10:45:38 -05005330 if (!err && attr->ia_valid & ATTR_MODE)
Christoph Hellwig996a7102013-12-20 05:16:43 -08005331 err = posix_acl_chmod(inode, inode->i_mode);
Christoph Hellwig10257742010-06-04 11:30:02 +02005332 }
5333
Chris Mason39279cc2007-06-12 06:35:45 -04005334 return err;
5335}
Chris Mason61295eb2008-01-14 16:24:38 -05005336
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005337/*
5338 * While truncating the inode pages during eviction, we get the VFS calling
5339 * btrfs_invalidatepage() against each page of the inode. This is slow because
5340 * the calls to btrfs_invalidatepage() result in a huge amount of calls to
5341 * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting
5342 * extent_state structures over and over, wasting lots of time.
5343 *
5344 * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all
5345 * those expensive operations on a per page basis and do only the ordered io
5346 * finishing, while we release here the extent_map and extent_state structures,
5347 * without the excessive merging and splitting.
5348 */
5349static void evict_inode_truncate_pages(struct inode *inode)
5350{
5351 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
5352 struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree;
5353 struct rb_node *node;
5354
5355 ASSERT(inode->i_state & I_FREEING);
Johannes Weiner91b0abe2014-04-03 14:47:49 -07005356 truncate_inode_pages_final(&inode->i_data);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005357
5358 write_lock(&map_tree->lock);
Liu Bo07e1ce02018-08-23 03:51:52 +08005359 while (!RB_EMPTY_ROOT(&map_tree->map.rb_root)) {
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005360 struct extent_map *em;
5361
Liu Bo07e1ce02018-08-23 03:51:52 +08005362 node = rb_first_cached(&map_tree->map);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005363 em = rb_entry(node, struct extent_map, rb_node);
Wang Shilong180589e2013-12-14 15:27:31 +08005364 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
5365 clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005366 remove_extent_mapping(map_tree, em);
5367 free_extent_map(em);
Filipe Manana7064dd52014-08-08 02:47:05 +01005368 if (need_resched()) {
5369 write_unlock(&map_tree->lock);
5370 cond_resched();
5371 write_lock(&map_tree->lock);
5372 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005373 }
5374 write_unlock(&map_tree->lock);
5375
Filipe Manana6ca07092015-05-26 00:55:42 +01005376 /*
5377 * Keep looping until we have no more ranges in the io tree.
5378 * We can have ongoing bios started by readpages (called from readahead)
Filipe Manana9c6429d2015-06-10 12:55:41 +01005379 * that have their endio callback (extent_io.c:end_bio_extent_readpage)
5380 * still in progress (unlocked the pages in the bio but did not yet
5381 * unlocked the ranges in the io tree). Therefore this means some
Filipe Manana6ca07092015-05-26 00:55:42 +01005382 * ranges can still be locked and eviction started because before
5383 * submitting those bios, which are executed by a separate task (work
5384 * queue kthread), inode references (inode->i_count) were not taken
5385 * (which would be dropped in the end io callback of each bio).
5386 * Therefore here we effectively end up waiting for those bios and
5387 * anyone else holding locked ranges without having bumped the inode's
5388 * reference count - if we don't do it, when they access the inode's
5389 * io_tree to unlock a range it may be too late, leading to an
5390 * use-after-free issue.
5391 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005392 spin_lock(&io_tree->lock);
5393 while (!RB_EMPTY_ROOT(&io_tree->state)) {
5394 struct extent_state *state;
5395 struct extent_state *cached_state = NULL;
Filipe Manana6ca07092015-05-26 00:55:42 +01005396 u64 start;
5397 u64 end;
Filipe Manana421f0922018-10-12 13:02:48 +01005398 unsigned state_flags;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005399
5400 node = rb_first(&io_tree->state);
5401 state = rb_entry(node, struct extent_state, rb_node);
Filipe Manana6ca07092015-05-26 00:55:42 +01005402 start = state->start;
5403 end = state->end;
Filipe Manana421f0922018-10-12 13:02:48 +01005404 state_flags = state->state;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005405 spin_unlock(&io_tree->lock);
5406
David Sterbaff13db42015-12-03 14:30:40 +01005407 lock_extent_bits(io_tree, start, end, &cached_state);
Qu Wenruob9d0b382015-09-29 10:35:16 +08005408
5409 /*
5410 * If still has DELALLOC flag, the extent didn't reach disk,
5411 * and its reserved space won't be freed by delayed_ref.
5412 * So we need to free its reserved space here.
5413 * (Refer to comment in btrfs_invalidatepage, case 2)
5414 *
5415 * Note, end is the bytenr of last byte, so we need + 1 here.
5416 */
Filipe Manana421f0922018-10-12 13:02:48 +01005417 if (state_flags & EXTENT_DELALLOC)
Qu Wenruobc42bda2017-02-27 15:10:39 +08005418 btrfs_qgroup_free_data(inode, NULL, start, end - start + 1);
Qu Wenruob9d0b382015-09-29 10:35:16 +08005419
Filipe Manana6ca07092015-05-26 00:55:42 +01005420 clear_extent_bit(io_tree, start, end,
Omar Sandovale1821632019-08-15 14:04:04 -07005421 EXTENT_LOCKED | EXTENT_DELALLOC |
5422 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1,
5423 &cached_state);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005424
Filipe Manana7064dd52014-08-08 02:47:05 +01005425 cond_resched();
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005426 spin_lock(&io_tree->lock);
5427 }
5428 spin_unlock(&io_tree->lock);
5429}
5430
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005431static struct btrfs_trans_handle *evict_refill_and_join(struct btrfs_root *root,
Josef Bacikad80cf52018-09-28 07:18:19 -04005432 struct btrfs_block_rsv *rsv)
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005433{
5434 struct btrfs_fs_info *fs_info = root->fs_info;
5435 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
Josef Bacikd3984c92019-08-01 18:19:37 -04005436 struct btrfs_trans_handle *trans;
Josef Bacik2bd36e72019-08-22 15:14:33 -04005437 u64 delayed_refs_extra = btrfs_calc_insert_metadata_size(fs_info, 1);
Josef Bacikd3984c92019-08-01 18:19:37 -04005438 int ret;
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005439
Josef Bacikd3984c92019-08-01 18:19:37 -04005440 /*
5441 * Eviction should be taking place at some place safe because of our
5442 * delayed iputs. However the normal flushing code will run delayed
5443 * iputs, so we cannot use FLUSH_ALL otherwise we'll deadlock.
5444 *
5445 * We reserve the delayed_refs_extra here again because we can't use
5446 * btrfs_start_transaction(root, 0) for the same deadlocky reason as
5447 * above. We reserve our extra bit here because we generate a ton of
5448 * delayed refs activity by truncating.
5449 *
5450 * If we cannot make our reservation we'll attempt to steal from the
5451 * global reserve, because we really want to be able to free up space.
5452 */
5453 ret = btrfs_block_rsv_refill(root, rsv, rsv->size + delayed_refs_extra,
5454 BTRFS_RESERVE_FLUSH_EVICT);
5455 if (ret) {
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005456 /*
5457 * Try to steal from the global reserve if there is space for
5458 * it.
5459 */
Josef Bacikd3984c92019-08-01 18:19:37 -04005460 if (btrfs_check_space_for_delayed_refs(fs_info) ||
5461 btrfs_block_rsv_migrate(global_rsv, rsv, rsv->size, 0)) {
5462 btrfs_warn(fs_info,
5463 "could not allocate space for delete; will truncate on mount");
5464 return ERR_PTR(-ENOSPC);
5465 }
5466 delayed_refs_extra = 0;
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005467 }
Josef Bacikd3984c92019-08-01 18:19:37 -04005468
5469 trans = btrfs_join_transaction(root);
5470 if (IS_ERR(trans))
5471 return trans;
5472
5473 if (delayed_refs_extra) {
5474 trans->block_rsv = &fs_info->trans_block_rsv;
5475 trans->bytes_reserved = delayed_refs_extra;
5476 btrfs_block_rsv_migrate(rsv, trans->block_rsv,
5477 delayed_refs_extra, 1);
5478 }
5479 return trans;
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005480}
5481
Al Virobd555972010-06-07 11:35:40 -04005482void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005483{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005484 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04005485 struct btrfs_trans_handle *trans;
5486 struct btrfs_root *root = BTRFS_I(inode)->root;
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005487 struct btrfs_block_rsv *rsv;
Chris Mason39279cc2007-06-12 06:35:45 -04005488 int ret;
5489
liubo1abe9b82011-03-24 11:18:59 +00005490 trace_btrfs_inode_evict(inode);
5491
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005492 if (!root) {
Liu Boe8f1bc12018-01-25 11:02:53 -07005493 clear_inode(inode);
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005494 return;
5495 }
5496
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005497 evict_inode_truncate_pages(inode);
5498
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005499 if (inode->i_nlink &&
5500 ((btrfs_root_refs(&root->root_item) != 0 &&
5501 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) ||
Nikolay Borisov70ddc552017-02-20 13:50:35 +02005502 btrfs_is_free_space_inode(BTRFS_I(inode))))
Al Virobd555972010-06-07 11:35:40 -04005503 goto no_delete;
5504
Omar Sandoval27919062018-05-11 13:13:37 -07005505 if (is_bad_inode(inode))
Chris Mason39279cc2007-06-12 06:35:45 -04005506 goto no_delete;
Chris Mason5f39d392007-10-15 16:14:19 -04005507
Nikolay Borisov7ab79562017-02-20 13:50:57 +02005508 btrfs_free_io_failure_record(BTRFS_I(inode), 0, (u64)-1);
Miao Xief6124962014-09-12 18:44:04 +08005509
Omar Sandoval7b40b692018-05-11 13:13:33 -07005510 if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags))
Yan, Zhengc71bf092009-11-12 09:34:40 +00005511 goto no_delete;
Yan, Zhengc71bf092009-11-12 09:34:40 +00005512
Yan, Zheng76dda932009-09-21 16:00:26 -04005513 if (inode->i_nlink > 0) {
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005514 BUG_ON(btrfs_root_refs(&root->root_item) != 0 &&
5515 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID);
Yan, Zheng76dda932009-09-21 16:00:26 -04005516 goto no_delete;
5517 }
5518
Nikolay Borisovaa790212017-01-10 20:35:40 +02005519 ret = btrfs_commit_inode_delayed_inode(BTRFS_I(inode));
Omar Sandoval27919062018-05-11 13:13:37 -07005520 if (ret)
Miao Xie0e8c36a2012-12-19 06:59:51 +00005521 goto no_delete;
Miao Xie0e8c36a2012-12-19 06:59:51 +00005522
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005523 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Omar Sandoval27919062018-05-11 13:13:37 -07005524 if (!rsv)
Josef Bacik4289a662011-08-05 13:22:24 -04005525 goto no_delete;
Josef Bacik2bd36e72019-08-22 15:14:33 -04005526 rsv->size = btrfs_calc_metadata_size(fs_info, 1);
Josef Bacikca7e70f2012-08-27 17:48:15 -04005527 rsv->failfast = 1;
Josef Bacik4289a662011-08-05 13:22:24 -04005528
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02005529 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Mason5f39d392007-10-15 16:14:19 -04005530
Yan, Zheng80825102009-11-12 09:35:36 +00005531 while (1) {
Josef Bacikad80cf52018-09-28 07:18:19 -04005532 trans = evict_refill_and_join(root, rsv);
Omar Sandoval27919062018-05-11 13:13:37 -07005533 if (IS_ERR(trans))
5534 goto free_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04005535
5536 trans->block_rsv = rsv;
5537
Yan, Zhengd68fc572010-05-16 10:49:58 -04005538 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Omar Sandoval27919062018-05-11 13:13:37 -07005539 trans->block_rsv = &fs_info->trans_block_rsv;
5540 btrfs_end_transaction(trans);
5541 btrfs_btree_balance_dirty(fs_info);
5542 if (ret && ret != -ENOSPC && ret != -EAGAIN)
5543 goto free_rsv;
5544 else if (!ret)
Yan, Zheng80825102009-11-12 09:35:36 +00005545 break;
Josef Bacik7b128762008-07-24 12:17:14 -04005546 }
5547
Josef Bacik4ef31a42013-08-13 14:10:08 -04005548 /*
Omar Sandoval27919062018-05-11 13:13:37 -07005549 * Errors here aren't a big deal, it just means we leave orphan items in
5550 * the tree. They will be cleaned up on the next mount. If the inode
5551 * number gets reused, cleanup deletes the orphan item without doing
5552 * anything, and unlink reuses the existing orphan item.
5553 *
5554 * If it turns out that we are dropping too many of these, we might want
5555 * to add a mechanism for retrying these after a commit.
Josef Bacik4ef31a42013-08-13 14:10:08 -04005556 */
Josef Bacikad80cf52018-09-28 07:18:19 -04005557 trans = evict_refill_and_join(root, rsv);
Omar Sandoval27919062018-05-11 13:13:37 -07005558 if (!IS_ERR(trans)) {
5559 trans->block_rsv = rsv;
5560 btrfs_orphan_del(trans, BTRFS_I(inode));
5561 trans->block_rsv = &fs_info->trans_block_rsv;
5562 btrfs_end_transaction(trans);
5563 }
Chris Mason85e21ba2008-01-29 15:11:36 -05005564
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005565 if (!(root == fs_info->tree_root ||
Li Zefan581bb052011-04-20 10:06:11 +08005566 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005567 btrfs_return_ino(root, btrfs_ino(BTRFS_I(inode)));
Li Zefan581bb052011-04-20 10:06:11 +08005568
Omar Sandoval27919062018-05-11 13:13:37 -07005569free_rsv:
5570 btrfs_free_block_rsv(fs_info, rsv);
Chris Mason39279cc2007-06-12 06:35:45 -04005571no_delete:
Omar Sandoval27919062018-05-11 13:13:37 -07005572 /*
5573 * If we didn't successfully delete, the orphan item will still be in
5574 * the tree and we'll retry on the next mount. Again, we might also want
5575 * to retry these periodically in the future.
5576 */
Nikolay Borisovf48d1cf2017-01-10 20:35:39 +02005577 btrfs_remove_delayed_node(BTRFS_I(inode));
Jan Karadbd57682012-05-03 14:48:02 +02005578 clear_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005579}
5580
5581/*
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005582 * Return the key found in the dir entry in the location pointer, fill @type
5583 * with BTRFS_FT_*, and return 0.
5584 *
Su Yue005d6712018-03-05 17:13:37 +08005585 * If no dir entries were found, returns -ENOENT.
5586 * If found a corrupted location in dir entry, returns -EUCLEAN.
Chris Mason39279cc2007-06-12 06:35:45 -04005587 */
5588static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005589 struct btrfs_key *location, u8 *type)
Chris Mason39279cc2007-06-12 06:35:45 -04005590{
5591 const char *name = dentry->d_name.name;
5592 int namelen = dentry->d_name.len;
5593 struct btrfs_dir_item *di;
5594 struct btrfs_path *path;
5595 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04005596 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005597
5598 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005599 if (!path)
5600 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05005601
David Sterbaf85b7372017-01-20 14:54:07 +01005602 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(BTRFS_I(dir)),
5603 name, namelen, 0);
Liu Bo3cf50682018-09-12 06:06:26 +08005604 if (IS_ERR_OR_NULL(di)) {
5605 ret = di ? PTR_ERR(di) : -ENOENT;
Su Yue005d6712018-03-05 17:13:37 +08005606 goto out;
5607 }
Chris Masond3977122009-01-05 21:25:51 -05005608
Chris Mason5f39d392007-10-15 16:14:19 -04005609 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Liu Bo56a0e702017-10-30 11:14:38 -06005610 if (location->type != BTRFS_INODE_ITEM_KEY &&
5611 location->type != BTRFS_ROOT_ITEM_KEY) {
Su Yue005d6712018-03-05 17:13:37 +08005612 ret = -EUCLEAN;
Liu Bo56a0e702017-10-30 11:14:38 -06005613 btrfs_warn(root->fs_info,
5614"%s gets something invalid in DIR_ITEM (name %s, directory ino %llu, location(%llu %u %llu))",
5615 __func__, name, btrfs_ino(BTRFS_I(dir)),
5616 location->objectid, location->type, location->offset);
Liu Bo56a0e702017-10-30 11:14:38 -06005617 }
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005618 if (!ret)
5619 *type = btrfs_dir_type(path->nodes[0], di);
Chris Mason39279cc2007-06-12 06:35:45 -04005620out:
Chris Mason39279cc2007-06-12 06:35:45 -04005621 btrfs_free_path(path);
5622 return ret;
5623}
5624
5625/*
5626 * when we hit a tree root in a directory, the btrfs part of the inode
5627 * needs to be changed to reflect the root directory of the tree root. This
5628 * is kind of like crossing a mount point.
5629 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005630static int fixup_tree_root_location(struct btrfs_fs_info *fs_info,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005631 struct inode *dir,
5632 struct dentry *dentry,
5633 struct btrfs_key *location,
5634 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04005635{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005636 struct btrfs_path *path;
5637 struct btrfs_root *new_root;
5638 struct btrfs_root_ref *ref;
5639 struct extent_buffer *leaf;
David Sterba1d4c08e2015-01-02 19:36:14 +01005640 struct btrfs_key key;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005641 int ret;
5642 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005643
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005644 path = btrfs_alloc_path();
5645 if (!path) {
5646 err = -ENOMEM;
5647 goto out;
5648 }
Chris Mason39279cc2007-06-12 06:35:45 -04005649
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005650 err = -ENOENT;
David Sterba1d4c08e2015-01-02 19:36:14 +01005651 key.objectid = BTRFS_I(dir)->root->root_key.objectid;
5652 key.type = BTRFS_ROOT_REF_KEY;
5653 key.offset = location->objectid;
5654
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005655 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005656 if (ret) {
5657 if (ret < 0)
5658 err = ret;
5659 goto out;
5660 }
Chris Mason39279cc2007-06-12 06:35:45 -04005661
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005662 leaf = path->nodes[0];
5663 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005664 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(BTRFS_I(dir)) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005665 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
5666 goto out;
5667
5668 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
5669 (unsigned long)(ref + 1),
5670 dentry->d_name.len);
5671 if (ret)
5672 goto out;
5673
David Sterbab3b4aa72011-04-21 01:20:15 +02005674 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005675
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005676 new_root = btrfs_read_fs_root_no_name(fs_info, location);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005677 if (IS_ERR(new_root)) {
5678 err = PTR_ERR(new_root);
5679 goto out;
5680 }
5681
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005682 *sub_root = new_root;
5683 location->objectid = btrfs_root_dirid(&new_root->root_item);
5684 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04005685 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005686 err = 0;
5687out:
5688 btrfs_free_path(path);
5689 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005690}
5691
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005692static void inode_tree_add(struct inode *inode)
5693{
5694 struct btrfs_root *root = BTRFS_I(inode)->root;
5695 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005696 struct rb_node **p;
5697 struct rb_node *parent;
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005698 struct rb_node *new = &BTRFS_I(inode)->rb_node;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005699 u64 ino = btrfs_ino(BTRFS_I(inode));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005700
Al Viro1d3382cb2010-10-23 15:19:20 -04005701 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005702 return;
Miao Xiee1409ce2013-05-15 07:48:16 +00005703 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005704 spin_lock(&root->inode_lock);
Miao Xiee1409ce2013-05-15 07:48:16 +00005705 p = &root->inode_tree.rb_node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005706 while (*p) {
5707 parent = *p;
5708 entry = rb_entry(parent, struct btrfs_inode, rb_node);
5709
David Sterba37508512018-06-29 10:56:40 +02005710 if (ino < btrfs_ino(entry))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005711 p = &parent->rb_left;
David Sterba37508512018-06-29 10:56:40 +02005712 else if (ino > btrfs_ino(entry))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005713 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005714 else {
5715 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04005716 (I_WILL_FREE | I_FREEING)));
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005717 rb_replace_node(parent, new, &root->inode_tree);
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005718 RB_CLEAR_NODE(parent);
5719 spin_unlock(&root->inode_lock);
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005720 return;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005721 }
5722 }
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005723 rb_link_node(new, parent, p);
5724 rb_insert_color(new, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005725 spin_unlock(&root->inode_lock);
5726}
5727
5728static void inode_tree_del(struct inode *inode)
5729{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005730 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005731 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04005732 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005733
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005734 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005735 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005736 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005737 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04005738 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005739 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005740 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04005741
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005742 if (empty && btrfs_root_refs(&root->root_item) == 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005743 synchronize_srcu(&fs_info->subvol_srcu);
Yan, Zheng76dda932009-09-21 16:00:26 -04005744 spin_lock(&root->inode_lock);
5745 empty = RB_EMPTY_ROOT(&root->inode_tree);
5746 spin_unlock(&root->inode_lock);
5747 if (empty)
5748 btrfs_add_dead_root(root);
5749 }
5750}
5751
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005752
Chris Masone02119d2008-09-05 16:13:11 -04005753static int btrfs_init_locked_inode(struct inode *inode, void *p)
5754{
5755 struct btrfs_iget_args *args = p;
Chris Mason90d3e592014-01-09 17:28:00 -08005756 inode->i_ino = args->location->objectid;
5757 memcpy(&BTRFS_I(inode)->location, args->location,
5758 sizeof(*args->location));
Chris Masone02119d2008-09-05 16:13:11 -04005759 BTRFS_I(inode)->root = args->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005760 return 0;
5761}
5762
5763static int btrfs_find_actor(struct inode *inode, void *opaque)
5764{
5765 struct btrfs_iget_args *args = opaque;
Chris Mason90d3e592014-01-09 17:28:00 -08005766 return args->location->objectid == BTRFS_I(inode)->location.objectid &&
Chris Masond3977122009-01-05 21:25:51 -05005767 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005768}
5769
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005770static struct inode *btrfs_iget_locked(struct super_block *s,
Chris Mason90d3e592014-01-09 17:28:00 -08005771 struct btrfs_key *location,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005772 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04005773{
5774 struct inode *inode;
5775 struct btrfs_iget_args args;
Chris Mason90d3e592014-01-09 17:28:00 -08005776 unsigned long hashval = btrfs_inode_hash(location->objectid, root);
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005777
Chris Mason90d3e592014-01-09 17:28:00 -08005778 args.location = location;
Chris Mason39279cc2007-06-12 06:35:45 -04005779 args.root = root;
5780
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005781 inode = iget5_locked(s, hashval, btrfs_find_actor,
Chris Mason39279cc2007-06-12 06:35:45 -04005782 btrfs_init_locked_inode,
5783 (void *)&args);
5784 return inode;
5785}
5786
David Sterba4c66e0d2019-10-03 19:09:35 +02005787/*
5788 * Get an inode object given its location and corresponding root.
5789 * Path can be preallocated to prevent recursing back to iget through
5790 * allocator. NULL is also valid but may require an additional allocation
5791 * later.
Balaji Rao1a54ef82008-07-21 02:01:04 +05305792 */
Filipe Manana4222ea72018-10-24 10:13:03 +01005793struct inode *btrfs_iget_path(struct super_block *s, struct btrfs_key *location,
David Sterba4c66e0d2019-10-03 19:09:35 +02005794 struct btrfs_root *root, struct btrfs_path *path)
Balaji Rao1a54ef82008-07-21 02:01:04 +05305795{
5796 struct inode *inode;
5797
Chris Mason90d3e592014-01-09 17:28:00 -08005798 inode = btrfs_iget_locked(s, location, root);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305799 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005800 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305801
5802 if (inode->i_state & I_NEW) {
Filipe Manana67710892016-06-06 11:51:25 +01005803 int ret;
5804
Filipe Manana4222ea72018-10-24 10:13:03 +01005805 ret = btrfs_read_locked_inode(inode, path);
Al Viro9bc2cef2018-07-29 23:04:50 +01005806 if (!ret) {
Mark Fasheh1748f842011-07-12 11:25:31 -07005807 inode_tree_add(inode);
5808 unlock_new_inode(inode);
Mark Fasheh1748f842011-07-12 11:25:31 -07005809 } else {
Al Virof5b3a412018-07-29 23:04:51 +01005810 iget_failed(inode);
5811 /*
5812 * ret > 0 can come from btrfs_search_slot called by
5813 * btrfs_read_locked_inode, this means the inode item
5814 * was not found.
5815 */
5816 if (ret > 0)
5817 ret = -ENOENT;
5818 inode = ERR_PTR(ret);
Mark Fasheh1748f842011-07-12 11:25:31 -07005819 }
5820 }
5821
Balaji Rao1a54ef82008-07-21 02:01:04 +05305822 return inode;
5823}
5824
Filipe Manana4222ea72018-10-24 10:13:03 +01005825struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
David Sterba4c66e0d2019-10-03 19:09:35 +02005826 struct btrfs_root *root)
Filipe Manana4222ea72018-10-24 10:13:03 +01005827{
David Sterba4c66e0d2019-10-03 19:09:35 +02005828 return btrfs_iget_path(s, location, root, NULL);
Filipe Manana4222ea72018-10-24 10:13:03 +01005829}
5830
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005831static struct inode *new_simple_dir(struct super_block *s,
5832 struct btrfs_key *key,
5833 struct btrfs_root *root)
5834{
5835 struct inode *inode = new_inode(s);
5836
5837 if (!inode)
5838 return ERR_PTR(-ENOMEM);
5839
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005840 BTRFS_I(inode)->root = root;
5841 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04005842 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005843
5844 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Li Zefan848cce02012-02-21 17:04:28 +08005845 inode->i_op = &btrfs_dir_ro_inode_operations;
Omar Sandoval1fdf4192017-01-25 17:06:39 -08005846 inode->i_opflags &= ~IOP_XATTR;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005847 inode->i_fop = &simple_dir_operations;
5848 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
Deepa Dinamanic2050a42016-09-14 07:48:06 -07005849 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05305850 inode->i_atime = inode->i_mtime;
5851 inode->i_ctime = inode->i_mtime;
Arnd Bergmannd3c6be6f2018-06-21 18:04:06 +02005852 BTRFS_I(inode)->i_otime = inode->i_mtime;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005853
5854 return inode;
5855}
5856
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005857static inline u8 btrfs_inode_type(struct inode *inode)
5858{
5859 /*
5860 * Compile-time asserts that generic FT_* types still match
5861 * BTRFS_FT_* types
5862 */
5863 BUILD_BUG_ON(BTRFS_FT_UNKNOWN != FT_UNKNOWN);
5864 BUILD_BUG_ON(BTRFS_FT_REG_FILE != FT_REG_FILE);
5865 BUILD_BUG_ON(BTRFS_FT_DIR != FT_DIR);
5866 BUILD_BUG_ON(BTRFS_FT_CHRDEV != FT_CHRDEV);
5867 BUILD_BUG_ON(BTRFS_FT_BLKDEV != FT_BLKDEV);
5868 BUILD_BUG_ON(BTRFS_FT_FIFO != FT_FIFO);
5869 BUILD_BUG_ON(BTRFS_FT_SOCK != FT_SOCK);
5870 BUILD_BUG_ON(BTRFS_FT_SYMLINK != FT_SYMLINK);
5871
5872 return fs_umode_to_ftype(inode->i_mode);
5873}
5874
Chris Mason3de45862008-11-17 21:02:50 -05005875struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04005876{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005877 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masond3977122009-01-05 21:25:51 -05005878 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005879 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005880 struct btrfs_root *sub_root = root;
5881 struct btrfs_key location;
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005882 u8 di_type = 0;
Yan, Zheng76dda932009-09-21 16:00:26 -04005883 int index;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005884 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005885
5886 if (dentry->d_name.len > BTRFS_NAME_LEN)
5887 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04005888
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005889 ret = btrfs_inode_by_name(dir, dentry, &location, &di_type);
Chris Mason39279cc2007-06-12 06:35:45 -04005890 if (ret < 0)
5891 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04005892
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005893 if (location.type == BTRFS_INODE_ITEM_KEY) {
David Sterba4c66e0d2019-10-03 19:09:35 +02005894 inode = btrfs_iget(dir->i_sb, &location, root);
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005895 if (IS_ERR(inode))
5896 return inode;
5897
5898 /* Do extra check against inode mode with di_type */
5899 if (btrfs_inode_type(inode) != di_type) {
5900 btrfs_crit(fs_info,
5901"inode mode mismatch with dir: inode mode=0%o btrfs type=%u dir type=%u",
5902 inode->i_mode, btrfs_inode_type(inode),
5903 di_type);
5904 iput(inode);
5905 return ERR_PTR(-EUCLEAN);
5906 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005907 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04005908 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005909
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005910 index = srcu_read_lock(&fs_info->subvol_srcu);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005911 ret = fixup_tree_root_location(fs_info, dir, dentry,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005912 &location, &sub_root);
5913 if (ret < 0) {
5914 if (ret != -ENOENT)
5915 inode = ERR_PTR(ret);
5916 else
5917 inode = new_simple_dir(dir->i_sb, &location, sub_root);
5918 } else {
David Sterba4c66e0d2019-10-03 19:09:35 +02005919 inode = btrfs_iget(dir->i_sb, &location, sub_root);
Chris Mason39279cc2007-06-12 06:35:45 -04005920 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005921 srcu_read_unlock(&fs_info->subvol_srcu, index);
Yan, Zheng76dda932009-09-21 16:00:26 -04005922
Julia Lawall34d19ba2011-01-24 19:55:19 +00005923 if (!IS_ERR(inode) && root != sub_root) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005924 down_read(&fs_info->cleanup_work_sem);
David Howellsbc98a422017-07-17 08:45:34 +01005925 if (!sb_rdonly(inode->i_sb))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005926 ret = btrfs_orphan_cleanup(sub_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005927 up_read(&fs_info->cleanup_work_sem);
Josef Bacik01cd3362013-06-03 21:39:49 -04005928 if (ret) {
5929 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005930 inode = ERR_PTR(ret);
Josef Bacik01cd3362013-06-03 21:39:49 -04005931 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00005932 }
5933
Chris Mason3de45862008-11-17 21:02:50 -05005934 return inode;
5935}
5936
Nick Pigginfe15ce42011-01-07 17:49:23 +11005937static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04005938{
5939 struct btrfs_root *root;
David Howells2b0143b2015-03-17 22:25:59 +00005940 struct inode *inode = d_inode(dentry);
Yan, Zheng76dda932009-09-21 16:00:26 -04005941
Li Zefan848cce02012-02-21 17:04:28 +08005942 if (!inode && !IS_ROOT(dentry))
David Howells2b0143b2015-03-17 22:25:59 +00005943 inode = d_inode(dentry->d_parent);
Yan, Zheng76dda932009-09-21 16:00:26 -04005944
Li Zefan848cce02012-02-21 17:04:28 +08005945 if (inode) {
5946 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04005947 if (btrfs_root_refs(&root->root_item) == 0)
5948 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08005949
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005950 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Li Zefan848cce02012-02-21 17:04:28 +08005951 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04005952 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005953 return 0;
5954}
5955
Chris Mason3de45862008-11-17 21:02:50 -05005956static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04005957 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05005958{
Al Viro3837d202018-10-10 16:38:27 -04005959 struct inode *inode = btrfs_lookup_dentry(dir, dentry);
Josef Bacika66e7cc2011-09-18 10:34:03 -04005960
Al Viro3837d202018-10-10 16:38:27 -04005961 if (inode == ERR_PTR(-ENOENT))
5962 inode = NULL;
Al Viro41d28bc2014-10-12 22:24:21 -04005963 return d_splice_alias(inode, dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04005964}
5965
Josef Bacik23b5ec72017-07-24 15:14:25 -04005966/*
5967 * All this infrastructure exists because dir_emit can fault, and we are holding
5968 * the tree lock when doing readdir. For now just allocate a buffer and copy
5969 * our information into that, and then dir_emit from the buffer. This is
5970 * similar to what NFS does, only we don't keep the buffer around in pagecache
5971 * because I'm afraid I'll mess that up. Long term we need to make filldir do
5972 * copy_to_user_inatomic so we don't have to worry about page faulting under the
5973 * tree lock.
5974 */
5975static int btrfs_opendir(struct inode *inode, struct file *file)
5976{
5977 struct btrfs_file_private *private;
5978
5979 private = kzalloc(sizeof(struct btrfs_file_private), GFP_KERNEL);
5980 if (!private)
5981 return -ENOMEM;
5982 private->filldir_buf = kzalloc(PAGE_SIZE, GFP_KERNEL);
5983 if (!private->filldir_buf) {
5984 kfree(private);
5985 return -ENOMEM;
5986 }
5987 file->private_data = private;
5988 return 0;
5989}
5990
5991struct dir_entry {
5992 u64 ino;
5993 u64 offset;
5994 unsigned type;
5995 int name_len;
5996};
5997
5998static int btrfs_filldir(void *addr, int entries, struct dir_context *ctx)
5999{
6000 while (entries--) {
6001 struct dir_entry *entry = addr;
6002 char *name = (char *)(entry + 1);
6003
David Sterba92d32172018-04-16 21:10:14 +02006004 ctx->pos = get_unaligned(&entry->offset);
6005 if (!dir_emit(ctx, name, get_unaligned(&entry->name_len),
6006 get_unaligned(&entry->ino),
6007 get_unaligned(&entry->type)))
Josef Bacik23b5ec72017-07-24 15:14:25 -04006008 return 1;
David Sterba92d32172018-04-16 21:10:14 +02006009 addr += sizeof(struct dir_entry) +
6010 get_unaligned(&entry->name_len);
Josef Bacik23b5ec72017-07-24 15:14:25 -04006011 ctx->pos++;
6012 }
6013 return 0;
6014}
6015
Al Viro9cdda8d2013-05-22 16:48:09 -04006016static int btrfs_real_readdir(struct file *file, struct dir_context *ctx)
Chris Mason39279cc2007-06-12 06:35:45 -04006017{
Al Viro9cdda8d2013-05-22 16:48:09 -04006018 struct inode *inode = file_inode(file);
Chris Mason39279cc2007-06-12 06:35:45 -04006019 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik23b5ec72017-07-24 15:14:25 -04006020 struct btrfs_file_private *private = file->private_data;
Chris Mason39279cc2007-06-12 06:35:45 -04006021 struct btrfs_dir_item *di;
6022 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04006023 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04006024 struct btrfs_path *path;
Josef Bacik23b5ec72017-07-24 15:14:25 -04006025 void *addr;
Miao Xie16cdcec2011-04-22 18:12:22 +08006026 struct list_head ins_list;
6027 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04006028 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04006029 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04006030 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04006031 char *name_ptr;
6032 int name_len;
Josef Bacik23b5ec72017-07-24 15:14:25 -04006033 int entries = 0;
6034 int total_len = 0;
Omar Sandoval02dbfc92016-05-20 13:50:33 -07006035 bool put = false;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006036 struct btrfs_key location;
Chris Mason5f39d392007-10-15 16:14:19 -04006037
Al Viro9cdda8d2013-05-22 16:48:09 -04006038 if (!dir_emit_dots(file, ctx))
6039 return 0;
6040
David Woodhouse49593bf2008-08-17 17:08:36 +01006041 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08006042 if (!path)
6043 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04006044
Josef Bacik23b5ec72017-07-24 15:14:25 -04006045 addr = private->filldir_buf;
David Sterbae4058b52015-11-27 16:31:35 +01006046 path->reada = READA_FORWARD;
David Woodhouse49593bf2008-08-17 17:08:36 +01006047
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006048 INIT_LIST_HEAD(&ins_list);
6049 INIT_LIST_HEAD(&del_list);
6050 put = btrfs_readdir_get_delayed_items(inode, &ins_list, &del_list);
Miao Xie16cdcec2011-04-22 18:12:22 +08006051
Josef Bacik23b5ec72017-07-24 15:14:25 -04006052again:
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006053 key.type = BTRFS_DIR_INDEX_KEY;
Al Viro9cdda8d2013-05-22 16:48:09 -04006054 key.offset = ctx->pos;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02006055 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason5f39d392007-10-15 16:14:19 -04006056
Chris Mason39279cc2007-06-12 06:35:45 -04006057 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
6058 if (ret < 0)
6059 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01006060
6061 while (1) {
Josef Bacik23b5ec72017-07-24 15:14:25 -04006062 struct dir_entry *entry;
6063
Chris Mason5f39d392007-10-15 16:14:19 -04006064 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04006065 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08006066 if (slot >= btrfs_header_nritems(leaf)) {
6067 ret = btrfs_next_leaf(root, path);
6068 if (ret < 0)
6069 goto err;
6070 else if (ret > 0)
6071 break;
6072 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04006073 }
Chris Mason3de45862008-11-17 21:02:50 -05006074
Chris Mason5f39d392007-10-15 16:14:19 -04006075 btrfs_item_key_to_cpu(leaf, &found_key, slot);
6076
6077 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04006078 break;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006079 if (found_key.type != BTRFS_DIR_INDEX_KEY)
Chris Mason39279cc2007-06-12 06:35:45 -04006080 break;
Al Viro9cdda8d2013-05-22 16:48:09 -04006081 if (found_key.offset < ctx->pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08006082 goto next;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006083 if (btrfs_should_delete_dir_index(&del_list, found_key.offset))
Miao Xie16cdcec2011-04-22 18:12:22 +08006084 goto next;
Chris Mason39279cc2007-06-12 06:35:45 -04006085 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006086 name_len = btrfs_dir_name_len(leaf, di);
Josef Bacik23b5ec72017-07-24 15:14:25 -04006087 if ((total_len + sizeof(struct dir_entry) + name_len) >=
6088 PAGE_SIZE) {
6089 btrfs_release_path(path);
6090 ret = btrfs_filldir(private->filldir_buf, entries, ctx);
6091 if (ret)
6092 goto nopos;
6093 addr = private->filldir_buf;
6094 entries = 0;
6095 total_len = 0;
6096 goto again;
Chris Mason39279cc2007-06-12 06:35:45 -04006097 }
Josef Bacik23b5ec72017-07-24 15:14:25 -04006098
6099 entry = addr;
David Sterba92d32172018-04-16 21:10:14 +02006100 put_unaligned(name_len, &entry->name_len);
Josef Bacik23b5ec72017-07-24 15:14:25 -04006101 name_ptr = (char *)(entry + 1);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006102 read_extent_buffer(leaf, name_ptr, (unsigned long)(di + 1),
6103 name_len);
Phillip Potter7d157c32019-03-26 21:39:34 +00006104 put_unaligned(fs_ftype_to_dtype(btrfs_dir_type(leaf, di)),
David Sterba92d32172018-04-16 21:10:14 +02006105 &entry->type);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006106 btrfs_dir_item_key_to_cpu(leaf, di, &location);
David Sterba92d32172018-04-16 21:10:14 +02006107 put_unaligned(location.objectid, &entry->ino);
6108 put_unaligned(found_key.offset, &entry->offset);
Josef Bacik23b5ec72017-07-24 15:14:25 -04006109 entries++;
6110 addr += sizeof(struct dir_entry) + name_len;
6111 total_len += sizeof(struct dir_entry) + name_len;
Li Zefanb9e03af2011-03-23 10:43:58 +08006112next:
6113 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04006114 }
Josef Bacik23b5ec72017-07-24 15:14:25 -04006115 btrfs_release_path(path);
6116
6117 ret = btrfs_filldir(private->filldir_buf, entries, ctx);
6118 if (ret)
6119 goto nopos;
David Woodhouse49593bf2008-08-17 17:08:36 +01006120
Jeff Mahoneyd2fbb2b2016-11-05 13:26:35 -04006121 ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006122 if (ret)
David Sterbabc4ef752015-11-13 13:44:28 +01006123 goto nopos;
6124
Zach Browndb62efb2013-07-11 16:19:42 -07006125 /*
6126 * Stop new entries from being returned after we return the last
6127 * entry.
6128 *
6129 * New directory entries are assigned a strictly increasing
6130 * offset. This means that new entries created during readdir
6131 * are *guaranteed* to be seen in the future by that readdir.
6132 * This has broken buggy programs which operate on names as
6133 * they're returned by readdir. Until we re-use freed offsets
6134 * we have this hack to stop new entries from being returned
6135 * under the assumption that they'll never reach this huge
6136 * offset.
6137 *
6138 * This is being careful not to overflow 32bit loff_t unless the
6139 * last entry requires it because doing so has broken 32bit apps
6140 * in the past.
6141 */
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006142 if (ctx->pos >= INT_MAX)
6143 ctx->pos = LLONG_MAX;
6144 else
6145 ctx->pos = INT_MAX;
Chris Mason39279cc2007-06-12 06:35:45 -04006146nopos:
6147 ret = 0;
6148err:
Omar Sandoval02dbfc92016-05-20 13:50:33 -07006149 if (put)
6150 btrfs_readdir_put_delayed_items(inode, &ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04006151 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04006152 return ret;
6153}
6154
Chris Mason39279cc2007-06-12 06:35:45 -04006155/*
Chris Mason54aa1f42007-06-22 14:16:25 -04006156 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04006157 * inode changes. But, it is most likely to find the inode in cache.
6158 * FIXME, needs more benchmarking...there are no reasons other than performance
6159 * to keep or drop this code.
6160 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00006161static int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04006162{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006163 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04006164 struct btrfs_root *root = BTRFS_I(inode)->root;
6165 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006166 int ret;
6167
Josef Bacik72ac3c02012-05-23 14:13:11 -04006168 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05006169 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006170
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006171 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006172 if (IS_ERR(trans))
6173 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006174
6175 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04006176 if (ret && ret == -ENOSPC) {
6177 /* whoops, lets try again with the full transaction */
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006178 btrfs_end_transaction(trans);
Chris Mason94b60442010-05-26 11:02:00 -04006179 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006180 if (IS_ERR(trans))
6181 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006182
Chris Mason94b60442010-05-26 11:02:00 -04006183 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04006184 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006185 btrfs_end_transaction(trans);
Miao Xie16cdcec2011-04-22 18:12:22 +08006186 if (BTRFS_I(inode)->delayed_node)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006187 btrfs_balance_delayed_items(fs_info);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006188
6189 return ret;
6190}
6191
6192/*
6193 * This is a copy of file_update_time. We need this so we can return error on
6194 * ENOSPC for updating the inode in the case of file write and mmap writes.
6195 */
Deepa Dinamani95582b02018-05-08 19:36:02 -07006196static int btrfs_update_time(struct inode *inode, struct timespec64 *now,
Josef Bacike41f9412012-03-26 09:46:47 -04006197 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05006198{
Alexander Block2bc5565282012-06-15 09:49:33 +02006199 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Layton3a8c7232017-12-11 06:35:24 -05006200 bool dirty = flags & ~S_VERSION;
Alexander Block2bc5565282012-06-15 09:49:33 +02006201
6202 if (btrfs_root_readonly(root))
6203 return -EROFS;
6204
Josef Bacike41f9412012-03-26 09:46:47 -04006205 if (flags & S_VERSION)
Jeff Layton3a8c7232017-12-11 06:35:24 -05006206 dirty |= inode_maybe_inc_iversion(inode, dirty);
Josef Bacike41f9412012-03-26 09:46:47 -04006207 if (flags & S_CTIME)
6208 inode->i_ctime = *now;
6209 if (flags & S_MTIME)
6210 inode->i_mtime = *now;
6211 if (flags & S_ATIME)
6212 inode->i_atime = *now;
Jeff Layton3a8c7232017-12-11 06:35:24 -05006213 return dirty ? btrfs_dirty_inode(inode) : 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006214}
6215
Chris Masond352ac62008-09-29 15:18:18 -04006216/*
6217 * find the highest existing sequence number in a directory
6218 * and then set the in-memory index_cnt variable to reflect
6219 * free sequence numbers
6220 */
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006221static int btrfs_set_inode_index_count(struct btrfs_inode *inode)
Josef Bacikaec74772008-07-24 12:12:38 -04006222{
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006223 struct btrfs_root *root = inode->root;
Josef Bacikaec74772008-07-24 12:12:38 -04006224 struct btrfs_key key, found_key;
6225 struct btrfs_path *path;
6226 struct extent_buffer *leaf;
6227 int ret;
6228
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006229 key.objectid = btrfs_ino(inode);
David Sterba962a2982014-06-04 18:41:45 +02006230 key.type = BTRFS_DIR_INDEX_KEY;
Josef Bacikaec74772008-07-24 12:12:38 -04006231 key.offset = (u64)-1;
6232
6233 path = btrfs_alloc_path();
6234 if (!path)
6235 return -ENOMEM;
6236
6237 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
6238 if (ret < 0)
6239 goto out;
6240 /* FIXME: we should be able to handle this */
6241 if (ret == 0)
6242 goto out;
6243 ret = 0;
6244
6245 /*
6246 * MAGIC NUMBER EXPLANATION:
6247 * since we search a directory based on f_pos we have to start at 2
6248 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
6249 * else has to start at 2
6250 */
6251 if (path->slots[0] == 0) {
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006252 inode->index_cnt = 2;
Josef Bacikaec74772008-07-24 12:12:38 -04006253 goto out;
6254 }
6255
6256 path->slots[0]--;
6257
6258 leaf = path->nodes[0];
6259 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6260
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006261 if (found_key.objectid != btrfs_ino(inode) ||
David Sterba962a2982014-06-04 18:41:45 +02006262 found_key.type != BTRFS_DIR_INDEX_KEY) {
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006263 inode->index_cnt = 2;
Josef Bacikaec74772008-07-24 12:12:38 -04006264 goto out;
6265 }
6266
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006267 inode->index_cnt = found_key.offset + 1;
Josef Bacikaec74772008-07-24 12:12:38 -04006268out:
6269 btrfs_free_path(path);
6270 return ret;
6271}
6272
Chris Masond352ac62008-09-29 15:18:18 -04006273/*
6274 * helper to find a free sequence number in a given directory. This current
6275 * code is very simple, later versions will do smarter things in the btree
6276 */
Nikolay Borisov877574e2017-02-20 13:50:33 +02006277int btrfs_set_inode_index(struct btrfs_inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04006278{
6279 int ret = 0;
6280
Nikolay Borisov877574e2017-02-20 13:50:33 +02006281 if (dir->index_cnt == (u64)-1) {
6282 ret = btrfs_inode_delayed_dir_index_count(dir);
Miao Xie16cdcec2011-04-22 18:12:22 +08006283 if (ret) {
6284 ret = btrfs_set_inode_index_count(dir);
6285 if (ret)
6286 return ret;
6287 }
Josef Bacikaec74772008-07-24 12:12:38 -04006288 }
6289
Nikolay Borisov877574e2017-02-20 13:50:33 +02006290 *index = dir->index_cnt;
6291 dir->index_cnt++;
Josef Bacikaec74772008-07-24 12:12:38 -04006292
6293 return ret;
6294}
6295
Chris Masonb0d5d102014-09-08 13:08:51 -07006296static int btrfs_insert_inode_locked(struct inode *inode)
6297{
6298 struct btrfs_iget_args args;
6299 args.location = &BTRFS_I(inode)->location;
6300 args.root = BTRFS_I(inode)->root;
6301
6302 return insert_inode_locked4(inode,
6303 btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root),
6304 btrfs_find_actor, &args);
6305}
6306
Anand Jain19aee8d2017-07-18 17:37:05 +08006307/*
6308 * Inherit flags from the parent inode.
6309 *
6310 * Currently only the compression flags and the cow flags are inherited.
6311 */
6312static void btrfs_inherit_iflags(struct inode *inode, struct inode *dir)
6313{
6314 unsigned int flags;
6315
6316 if (!dir)
6317 return;
6318
6319 flags = BTRFS_I(dir)->flags;
6320
6321 if (flags & BTRFS_INODE_NOCOMPRESS) {
6322 BTRFS_I(inode)->flags &= ~BTRFS_INODE_COMPRESS;
6323 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
6324 } else if (flags & BTRFS_INODE_COMPRESS) {
6325 BTRFS_I(inode)->flags &= ~BTRFS_INODE_NOCOMPRESS;
6326 BTRFS_I(inode)->flags |= BTRFS_INODE_COMPRESS;
6327 }
6328
6329 if (flags & BTRFS_INODE_NODATACOW) {
6330 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
6331 if (S_ISREG(inode->i_mode))
6332 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
6333 }
6334
David Sterba7b6a2212018-03-26 18:40:21 +02006335 btrfs_sync_inode_flags_to_i_flags(inode);
Anand Jain19aee8d2017-07-18 17:37:05 +08006336}
6337
Chris Mason39279cc2007-06-12 06:35:45 -04006338static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
6339 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04006340 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05006341 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04006342 u64 ref_objectid, u64 objectid,
6343 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04006344{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006345 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04006346 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006347 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04006348 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04006349 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05006350 struct btrfs_inode_ref *ref;
6351 struct btrfs_key key[2];
6352 u32 sizes[2];
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006353 int nitems = name ? 2 : 1;
Chris Mason9c583092008-01-29 15:15:18 -05006354 unsigned long ptr;
Josef Bacik11a19a92019-09-09 10:12:04 -04006355 unsigned int nofs_flag;
Chris Mason39279cc2007-06-12 06:35:45 -04006356 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006357
Chris Mason5f39d392007-10-15 16:14:19 -04006358 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07006359 if (!path)
6360 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04006361
Josef Bacik11a19a92019-09-09 10:12:04 -04006362 nofs_flag = memalloc_nofs_save();
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006363 inode = new_inode(fs_info->sb);
Josef Bacik11a19a92019-09-09 10:12:04 -04006364 memalloc_nofs_restore(nofs_flag);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006365 if (!inode) {
6366 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04006367 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006368 }
Chris Mason39279cc2007-06-12 06:35:45 -04006369
Li Zefan581bb052011-04-20 10:06:11 +08006370 /*
Filipe Manana5762b5c2014-08-01 00:10:32 +01006371 * O_TMPFILE, set link count to 0, so that after this point,
6372 * we fill in an inode item with the correct link count.
6373 */
6374 if (!name)
6375 set_nlink(inode, 0);
6376
6377 /*
Li Zefan581bb052011-04-20 10:06:11 +08006378 * we have to initialize this early, so we can reclaim the inode
6379 * number if we fail afterwards in this function.
6380 */
6381 inode->i_ino = objectid;
6382
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006383 if (dir && name) {
liubo1abe9b82011-03-24 11:18:59 +00006384 trace_btrfs_inode_request(dir);
6385
Nikolay Borisov877574e2017-02-20 13:50:33 +02006386 ret = btrfs_set_inode_index(BTRFS_I(dir), index);
Shen Feng09771432009-04-02 16:46:06 -04006387 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006388 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04006389 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04006390 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04006391 }
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006392 } else if (dir) {
6393 *index = 0;
Josef Bacikaec74772008-07-24 12:12:38 -04006394 }
6395 /*
6396 * index_cnt is ignored for everything but a dir,
Su Yuedf6703e2018-01-12 11:08:02 +08006397 * btrfs_set_inode_index_count has an explanation for the magic
Josef Bacikaec74772008-07-24 12:12:38 -04006398 * number
6399 */
6400 BTRFS_I(inode)->index_cnt = 2;
Miao Xie67de1172013-12-26 13:07:06 +08006401 BTRFS_I(inode)->dir_index = *index;
Chris Mason39279cc2007-06-12 06:35:45 -04006402 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04006403 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00006404 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db22007-08-27 16:49:44 -04006405
Josef Bacik5dc562c2012-08-17 13:14:17 -04006406 /*
6407 * We could have gotten an inode number from somebody who was fsynced
6408 * and then removed in this same transaction, so let's just set full
6409 * sync since it will be a full sync anyway and this will blow away the
6410 * old info in the log.
6411 */
6412 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
6413
Chris Mason9c583092008-01-29 15:15:18 -05006414 key[0].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006415 key[0].type = BTRFS_INODE_ITEM_KEY;
Chris Mason9c583092008-01-29 15:15:18 -05006416 key[0].offset = 0;
6417
Chris Mason9c583092008-01-29 15:15:18 -05006418 sizes[0] = sizeof(struct btrfs_inode_item);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006419
6420 if (name) {
6421 /*
6422 * Start new inodes with an inode_ref. This is slightly more
6423 * efficient for small numbers of hard links since they will
6424 * be packed into one item. Extended refs will kick in if we
6425 * add more hard links than can fit in the ref item.
6426 */
6427 key[1].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006428 key[1].type = BTRFS_INODE_REF_KEY;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006429 key[1].offset = ref_objectid;
6430
6431 sizes[1] = name_len + sizeof(*ref);
6432 }
Chris Mason9c583092008-01-29 15:15:18 -05006433
Chris Masonb0d5d102014-09-08 13:08:51 -07006434 location = &BTRFS_I(inode)->location;
6435 location->objectid = objectid;
6436 location->offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02006437 location->type = BTRFS_INODE_ITEM_KEY;
Chris Masonb0d5d102014-09-08 13:08:51 -07006438
6439 ret = btrfs_insert_inode_locked(inode);
Al Viro32955c52018-05-16 12:20:05 -04006440 if (ret < 0) {
6441 iput(inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006442 goto fail;
Al Viro32955c52018-05-16 12:20:05 -04006443 }
Chris Masonb0d5d102014-09-08 13:08:51 -07006444
Chris Masonb9473432009-03-13 11:00:37 -04006445 path->leave_spinning = 1;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006446 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems);
Chris Mason9c583092008-01-29 15:15:18 -05006447 if (ret != 0)
Chris Masonb0d5d102014-09-08 13:08:51 -07006448 goto fail_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04006449
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03006450 inode_init_owner(inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04006451 inode_set_bytes(inode, 0);
chandan r9cc97d62012-07-04 12:48:07 +05306452
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006453 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05306454 inode->i_atime = inode->i_mtime;
6455 inode->i_ctime = inode->i_mtime;
Arnd Bergmannd3c6be6f2018-06-21 18:04:06 +02006456 BTRFS_I(inode)->i_otime = inode->i_mtime;
chandan r9cc97d62012-07-04 12:48:07 +05306457
Chris Mason5f39d392007-10-15 16:14:19 -04006458 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
6459 struct btrfs_inode_item);
David Sterbab159fa22016-11-08 18:09:03 +01006460 memzero_extent_buffer(path->nodes[0], (unsigned long)inode_item,
Li Zefan293f7e02012-07-10 00:58:58 -06006461 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04006462 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05006463
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006464 if (name) {
6465 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
6466 struct btrfs_inode_ref);
6467 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
6468 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
6469 ptr = (unsigned long)(ref + 1);
6470 write_extent_buffer(path->nodes[0], name, ptr, name_len);
6471 }
Chris Mason9c583092008-01-29 15:15:18 -05006472
Chris Mason5f39d392007-10-15 16:14:19 -04006473 btrfs_mark_buffer_dirty(path->nodes[0]);
6474 btrfs_free_path(path);
6475
Christoph Hellwig6cbff002009-04-17 10:37:41 +02006476 btrfs_inherit_iflags(inode, dir);
6477
Al Viro569254b2011-07-24 17:08:40 -04006478 if (S_ISREG(mode)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006479 if (btrfs_test_opt(fs_info, NODATASUM))
Chris Mason94272162009-07-02 12:26:06 -04006480 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006481 if (btrfs_test_opt(fs_info, NODATACOW))
Josef Bacikf2bdf9a2013-02-21 15:28:28 -05006482 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
6483 BTRFS_INODE_NODATASUM;
Chris Mason94272162009-07-02 12:26:06 -04006484 }
6485
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006486 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00006487
6488 trace_btrfs_inode_new(inode);
Chris Mason1973f0f2011-06-24 13:13:29 -04006489 btrfs_set_inode_last_trans(trans, inode);
liubo1abe9b82011-03-24 11:18:59 +00006490
Alexander Block8ea05e32012-07-25 17:35:53 +02006491 btrfs_update_root_times(trans, root);
6492
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006493 ret = btrfs_inode_inherit_props(trans, inode, dir);
6494 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006495 btrfs_err(fs_info,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006496 "error inheriting props for ino %llu (root %llu): %d",
David Sterbaf85b7372017-01-20 14:54:07 +01006497 btrfs_ino(BTRFS_I(inode)), root->root_key.objectid, ret);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006498
Chris Mason39279cc2007-06-12 06:35:45 -04006499 return inode;
Chris Masonb0d5d102014-09-08 13:08:51 -07006500
6501fail_unlock:
Al Viro32955c52018-05-16 12:20:05 -04006502 discard_new_inode(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006503fail:
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006504 if (dir && name)
Josef Bacikaec74772008-07-24 12:12:38 -04006505 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04006506 btrfs_free_path(path);
6507 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006508}
6509
Chris Masond352ac62008-09-29 15:18:18 -04006510/*
6511 * utility function to add 'inode' into 'parent_inode' with
6512 * a give name and a given sequence number.
6513 * if 'add_backref' is true, also insert a backref from the
6514 * inode to the parent directory.
6515 */
Chris Masone02119d2008-09-05 16:13:11 -04006516int btrfs_add_link(struct btrfs_trans_handle *trans,
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006517 struct btrfs_inode *parent_inode, struct btrfs_inode *inode,
Chris Masone02119d2008-09-05 16:13:11 -04006518 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006519{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006520 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006521 struct btrfs_key key;
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006522 struct btrfs_root *root = parent_inode->root;
6523 u64 ino = btrfs_ino(inode);
6524 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006525
Li Zefan33345d012011-04-20 10:31:50 +08006526 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006527 memcpy(&key, &inode->root->root_key, sizeof(key));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006528 } else {
Li Zefan33345d012011-04-20 10:31:50 +08006529 key.objectid = ino;
David Sterba962a2982014-06-04 18:41:45 +02006530 key.type = BTRFS_INODE_ITEM_KEY;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006531 key.offset = 0;
6532 }
Chris Mason39279cc2007-06-12 06:35:45 -04006533
Li Zefan33345d012011-04-20 10:31:50 +08006534 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Lu Fengqi6025c192018-08-01 11:32:29 +08006535 ret = btrfs_add_root_ref(trans, key.objectid,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006536 root->root_key.objectid, parent_ino,
6537 index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006538 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08006539 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
6540 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006541 }
6542
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006543 /* Nothing to clean up yet */
6544 if (ret)
6545 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006546
Lu Fengqi684572d2018-08-04 21:10:57 +08006547 ret = btrfs_insert_dir_item(trans, name, name_len, parent_inode, &key,
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006548 btrfs_inode_type(&inode->vfs_inode), index);
Chris Mason9c520572012-12-17 14:26:57 -05006549 if (ret == -EEXIST || ret == -EOVERFLOW)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006550 goto fail_dir_item;
6551 else if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04006552 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006553 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006554 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006555
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006556 btrfs_i_size_write(parent_inode, parent_inode->vfs_inode.i_size +
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006557 name_len * 2);
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006558 inode_inc_iversion(&parent_inode->vfs_inode);
Filipe Manana5338e432019-05-15 16:02:47 +01006559 /*
6560 * If we are replaying a log tree, we do not want to update the mtime
6561 * and ctime of the parent directory with the current time, since the
6562 * log replay procedure is responsible for setting them to their correct
6563 * values (the ones it had when the fsync was done).
6564 */
6565 if (!test_bit(BTRFS_FS_LOG_RECOVERING, &root->fs_info->flags)) {
6566 struct timespec64 now = current_time(&parent_inode->vfs_inode);
6567
6568 parent_inode->vfs_inode.i_mtime = now;
6569 parent_inode->vfs_inode.i_ctime = now;
6570 }
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006571 ret = btrfs_update_inode(trans, root, &parent_inode->vfs_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006572 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04006573 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006574 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05006575
6576fail_dir_item:
6577 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
6578 u64 local_index;
6579 int err;
Lu Fengqi3ee1c552018-08-01 11:32:28 +08006580 err = btrfs_del_root_ref(trans, key.objectid,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006581 root->root_key.objectid, parent_ino,
6582 &local_index, name, name_len);
Johannes Thumshirn1690dd412018-12-12 15:14:17 +01006583 if (err)
6584 btrfs_abort_transaction(trans, err);
Chris Masonfe66a052012-02-20 08:40:56 -05006585 } else if (add_backref) {
6586 u64 local_index;
6587 int err;
6588
6589 err = btrfs_del_inode_ref(trans, root, name, name_len,
6590 ino, parent_ino, &local_index);
Johannes Thumshirn1690dd412018-12-12 15:14:17 +01006591 if (err)
6592 btrfs_abort_transaction(trans, err);
Chris Masonfe66a052012-02-20 08:40:56 -05006593 }
Johannes Thumshirn1690dd412018-12-12 15:14:17 +01006594
6595 /* Return the original error code */
Chris Masonfe66a052012-02-20 08:40:56 -05006596 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006597}
6598
6599static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006600 struct btrfs_inode *dir, struct dentry *dentry,
6601 struct btrfs_inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006602{
Josef Bacika1b075d2010-11-19 20:36:11 +00006603 int err = btrfs_add_link(trans, dir, inode,
6604 dentry->d_name.name, dentry->d_name.len,
6605 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006606 if (err > 0)
6607 err = -EEXIST;
6608 return err;
6609}
6610
Josef Bacik618e21d2007-07-11 10:18:17 -04006611static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04006612 umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04006613{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006614 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Josef Bacik618e21d2007-07-11 10:18:17 -04006615 struct btrfs_trans_handle *trans;
6616 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006617 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04006618 int err;
Josef Bacik618e21d2007-07-11 10:18:17 -04006619 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006620 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04006621
Josef Bacik9ed74f22009-09-11 16:12:44 -04006622 /*
6623 * 2 for inode item and ref
6624 * 2 for dir items
6625 * 1 for xattr if selinux is on
6626 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006627 trans = btrfs_start_transaction(root, 5);
6628 if (IS_ERR(trans))
6629 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05006630
Li Zefan581bb052011-04-20 10:06:11 +08006631 err = btrfs_find_free_ino(root, &objectid);
6632 if (err)
6633 goto out_unlock;
6634
Josef Bacikaec74772008-07-24 12:12:38 -04006635 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006636 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6637 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006638 if (IS_ERR(inode)) {
6639 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -04006640 inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04006641 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006642 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006643
Casey Schauflerad19db72011-12-15 10:09:07 -05006644 /*
6645 * If the active LSM wants to access the inode during
6646 * d_instantiate it needs these. Smack checks to see
6647 * if the filesystem supports xattrs by looking at the
6648 * ops vector.
6649 */
Casey Schauflerad19db72011-12-15 10:09:07 -05006650 inode->i_op = &btrfs_special_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006651 init_special_inode(inode, inode->i_mode, rdev);
6652
6653 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik618e21d2007-07-11 10:18:17 -04006654 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006655 goto out_unlock;
Chris Masonb0d5d102014-09-08 13:08:51 -07006656
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006657 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6658 0, index);
Al Viro32955c52018-05-16 12:20:05 -04006659 if (err)
6660 goto out_unlock;
6661
6662 btrfs_update_inode(trans, root, inode);
6663 d_instantiate_new(dentry, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006664
Josef Bacik618e21d2007-07-11 10:18:17 -04006665out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006666 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006667 btrfs_btree_balance_dirty(fs_info);
Al Viro32955c52018-05-16 12:20:05 -04006668 if (err && inode) {
Josef Bacik618e21d2007-07-11 10:18:17 -04006669 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -04006670 discard_new_inode(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006671 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006672 return err;
6673}
6674
Chris Mason39279cc2007-06-12 06:35:45 -04006675static int btrfs_create(struct inode *dir, struct dentry *dentry,
Al Viroebfc3b42012-06-10 18:05:36 -04006676 umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -04006677{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006678 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04006679 struct btrfs_trans_handle *trans;
6680 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006681 struct inode *inode = NULL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006682 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04006683 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006684 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006685
Josef Bacik9ed74f22009-09-11 16:12:44 -04006686 /*
6687 * 2 for inode item and ref
6688 * 2 for dir items
6689 * 1 for xattr if selinux is on
6690 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006691 trans = btrfs_start_transaction(root, 5);
6692 if (IS_ERR(trans))
6693 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006694
Li Zefan581bb052011-04-20 10:06:11 +08006695 err = btrfs_find_free_ino(root, &objectid);
6696 if (err)
6697 goto out_unlock;
6698
Josef Bacikaec74772008-07-24 12:12:38 -04006699 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006700 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6701 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006702 if (IS_ERR(inode)) {
6703 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -04006704 inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006705 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006706 }
Casey Schauflerad19db72011-12-15 10:09:07 -05006707 /*
6708 * If the active LSM wants to access the inode during
6709 * d_instantiate it needs these. Smack checks to see
6710 * if the filesystem supports xattrs by looking at the
6711 * ops vector.
6712 */
6713 inode->i_fop = &btrfs_file_operations;
6714 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006715 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006716
6717 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
6718 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006719 goto out_unlock;
Chris Masonb0d5d102014-09-08 13:08:51 -07006720
6721 err = btrfs_update_inode(trans, root, inode);
6722 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006723 goto out_unlock;
Casey Schauflerad19db72011-12-15 10:09:07 -05006724
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006725 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6726 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006727 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006728 goto out_unlock;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006729
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006730 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Al Viro1e2e5472018-05-04 08:23:01 -04006731 d_instantiate_new(dentry, inode);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006732
Chris Mason39279cc2007-06-12 06:35:45 -04006733out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006734 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -04006735 if (err && inode) {
Chris Mason39279cc2007-06-12 06:35:45 -04006736 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -04006737 discard_new_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006738 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006739 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006740 return err;
6741}
6742
6743static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
6744 struct dentry *dentry)
6745{
Filipe Manana271dba452016-01-05 16:24:05 +00006746 struct btrfs_trans_handle *trans = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006747 struct btrfs_root *root = BTRFS_I(dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00006748 struct inode *inode = d_inode(old_dentry);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006749 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason00e4e6b2008-08-05 11:18:09 -04006750 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04006751 int err;
6752 int drop_inode = 0;
6753
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006754 /* do not allow sys_link's with other subvols of the same device */
Misono Tomohiro4fd786e2018-08-06 14:25:24 +09006755 if (root->root_key.objectid != BTRFS_I(inode)->root->root_key.objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00006756 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006757
Mark Fashehf1863732012-08-08 11:32:27 -07006758 if (inode->i_nlink >= BTRFS_LINK_MAX)
Al Viroc055e992011-03-04 17:15:18 +00006759 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04006760
Nikolay Borisov877574e2017-02-20 13:50:33 +02006761 err = btrfs_set_inode_index(BTRFS_I(dir), &index);
Josef Bacikaec74772008-07-24 12:12:38 -04006762 if (err)
6763 goto fail;
6764
Yan, Zhenga22285a2010-05-16 10:48:46 -04006765 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00006766 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04006767 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00006768 * 1 item for parent inode
Omar Sandoval399b0bb2018-05-11 13:13:40 -07006769 * 1 item for orphan item deletion if O_TMPFILE
Yan, Zhenga22285a2010-05-16 10:48:46 -04006770 */
Omar Sandoval399b0bb2018-05-11 13:13:40 -07006771 trans = btrfs_start_transaction(root, inode->i_nlink ? 5 : 6);
Yan, Zhenga22285a2010-05-16 10:48:46 -04006772 if (IS_ERR(trans)) {
6773 err = PTR_ERR(trans);
Filipe Manana271dba452016-01-05 16:24:05 +00006774 trans = NULL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006775 goto fail;
6776 }
Chris Mason5f39d392007-10-15 16:14:19 -04006777
Miao Xie67de1172013-12-26 13:07:06 +08006778 /* There are several dir indexes for this inode, clear the cache. */
6779 BTRFS_I(inode)->dir_index = 0ULL;
Zach Brown8b558c52013-10-16 12:10:34 -07006780 inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04006781 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006782 inode->i_ctime = current_time(inode);
Al Viro7de9c6ee2010-10-23 11:11:40 -04006783 ihold(inode);
Josef Bacike9976152012-10-11 15:53:56 -04006784 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
Josef Bacikaec74772008-07-24 12:12:38 -04006785
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006786 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6787 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04006788
Yan, Zhenga5719522009-09-24 09:17:31 -04006789 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006790 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04006791 } else {
Al Viro10d9f302011-07-16 23:09:10 -04006792 struct dentry *parent = dentry->d_parent;
Filipe Mananad4682ba2018-06-11 19:24:28 +01006793 int ret;
6794
Yan, Zhenga5719522009-09-24 09:17:31 -04006795 err = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006796 if (err)
6797 goto fail;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006798 if (inode->i_nlink == 1) {
6799 /*
6800 * If new hard link count is 1, it's a file created
6801 * with open(2) O_TMPFILE flag.
6802 */
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02006803 err = btrfs_orphan_del(trans, BTRFS_I(inode));
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006804 if (err)
6805 goto fail;
6806 }
Al Viro08c422c2011-12-23 07:58:13 -05006807 d_instantiate(dentry, inode);
Filipe Mananad4682ba2018-06-11 19:24:28 +01006808 ret = btrfs_log_new_name(trans, BTRFS_I(inode), NULL, parent,
6809 true, NULL);
6810 if (ret == BTRFS_NEED_TRANS_COMMIT) {
6811 err = btrfs_commit_transaction(trans);
6812 trans = NULL;
6813 }
Yan, Zhenga5719522009-09-24 09:17:31 -04006814 }
Chris Mason39279cc2007-06-12 06:35:45 -04006815
Chris Mason1832a6d2007-12-21 16:27:21 -05006816fail:
Filipe Manana271dba452016-01-05 16:24:05 +00006817 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006818 btrfs_end_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006819 if (drop_inode) {
6820 inode_dec_link_count(inode);
6821 iput(inode);
6822 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006823 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006824 return err;
6825}
6826
Al Viro18bb1db2011-07-26 01:41:39 -04006827static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04006828{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006829 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masonb9d86662008-05-02 16:13:49 -04006830 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006831 struct btrfs_trans_handle *trans;
6832 struct btrfs_root *root = BTRFS_I(dir)->root;
6833 int err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04006834 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006835 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006836
Josef Bacik9ed74f22009-09-11 16:12:44 -04006837 /*
6838 * 2 items for inode and ref
6839 * 2 items for dir items
6840 * 1 for xattr if selinux is on
6841 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006842 trans = btrfs_start_transaction(root, 5);
6843 if (IS_ERR(trans))
6844 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006845
Li Zefan581bb052011-04-20 10:06:11 +08006846 err = btrfs_find_free_ino(root, &objectid);
6847 if (err)
6848 goto out_fail;
6849
Josef Bacikaec74772008-07-24 12:12:38 -04006850 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006851 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6852 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04006853 if (IS_ERR(inode)) {
6854 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -04006855 inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006856 goto out_fail;
6857 }
Chris Mason5f39d392007-10-15 16:14:19 -04006858
Chris Masonb0d5d102014-09-08 13:08:51 -07006859 /* these must be set before we unlock the inode */
6860 inode->i_op = &btrfs_dir_inode_operations;
6861 inode->i_fop = &btrfs_dir_file_operations;
Josef Bacik33268ea2008-07-24 12:16:36 -04006862
Eric Paris2a7dba32011-02-01 11:05:39 -05006863 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04006864 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006865 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04006866
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02006867 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Mason39279cc2007-06-12 06:35:45 -04006868 err = btrfs_update_inode(trans, root, inode);
6869 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006870 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04006871
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006872 err = btrfs_add_link(trans, BTRFS_I(dir), BTRFS_I(inode),
6873 dentry->d_name.name,
6874 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006875 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006876 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04006877
Al Viro1e2e5472018-05-04 08:23:01 -04006878 d_instantiate_new(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006879
6880out_fail:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006881 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -04006882 if (err && inode) {
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006883 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -04006884 discard_new_inode(inode);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006885 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006886 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006887 return err;
6888}
6889
Chris Masonc8b97812008-10-29 14:49:59 -04006890static noinline int uncompress_inline(struct btrfs_path *path,
Byongho Leee40da0e2015-05-19 23:46:45 +09006891 struct page *page,
Chris Masonc8b97812008-10-29 14:49:59 -04006892 size_t pg_offset, u64 extent_offset,
6893 struct btrfs_file_extent_item *item)
6894{
6895 int ret;
6896 struct extent_buffer *leaf = path->nodes[0];
6897 char *tmp;
6898 size_t max_size;
6899 unsigned long inline_size;
6900 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08006901 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04006902
6903 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08006904 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04006905 max_size = btrfs_file_extent_ram_bytes(leaf, item);
6906 inline_size = btrfs_file_extent_inline_item_len(leaf,
Ross Kirkdd3cc162013-09-16 15:58:09 +01006907 btrfs_item_nr(path->slots[0]));
Chris Masonc8b97812008-10-29 14:49:59 -04006908 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04006909 if (!tmp)
6910 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04006911 ptr = btrfs_file_extent_inline_start(item);
6912
6913 read_extent_buffer(leaf, tmp, ptr, inline_size);
6914
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006915 max_size = min_t(unsigned long, PAGE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08006916 ret = btrfs_decompress(compress_type, tmp, page,
6917 extent_offset, inline_size, max_size);
Zygo Blaxelle1699d22017-03-10 16:45:44 -05006918
6919 /*
6920 * decompression code contains a memset to fill in any space between the end
6921 * of the uncompressed data and the end of max_size in case the decompressed
6922 * data ends up shorter than ram_bytes. That doesn't cover the hole between
6923 * the end of an inline extent and the beginning of the next block, so we
6924 * cover that region here.
6925 */
6926
6927 if (max_size + pg_offset < PAGE_SIZE) {
6928 char *map = kmap(page);
6929 memset(map + pg_offset + max_size, 0, PAGE_SIZE - max_size - pg_offset);
6930 kunmap(page);
6931 }
Chris Masonc8b97812008-10-29 14:49:59 -04006932 kfree(tmp);
Zach Brown166ae5a2014-05-09 17:15:10 -04006933 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -04006934}
6935
Chris Masond352ac62008-09-29 15:18:18 -04006936/*
6937 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05006938 * the ugly parts come from merging extents from the disk with the in-ram
6939 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04006940 * where the in-ram extents might be locked pending data=ordered completion.
6941 *
6942 * This also copies inline extents directly into the page.
6943 */
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006944struct extent_map *btrfs_get_extent(struct btrfs_inode *inode,
Liu Bode2c6612018-08-25 13:47:59 +08006945 struct page *page,
6946 size_t pg_offset, u64 start, u64 len,
6947 int create)
Chris Masona52d9a82007-08-27 16:49:44 -04006948{
David Sterba3ffbd682018-06-29 10:56:42 +02006949 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Chris Masona52d9a82007-08-27 16:49:44 -04006950 int ret;
6951 int err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006952 u64 extent_start = 0;
6953 u64 extent_end = 0;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006954 u64 objectid = btrfs_ino(inode);
Linus Torvalds7e74e232019-05-01 12:19:20 -07006955 int extent_type = -1;
Chris Masonf4219502008-07-22 11:18:09 -04006956 struct btrfs_path *path = NULL;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006957 struct btrfs_root *root = inode->root;
Chris Masona52d9a82007-08-27 16:49:44 -04006958 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04006959 struct extent_buffer *leaf;
6960 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04006961 struct extent_map *em = NULL;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006962 struct extent_map_tree *em_tree = &inode->extent_tree;
6963 struct extent_io_tree *io_tree = &inode->io_tree;
Filipe Manana7ffbb592014-06-09 03:48:05 +01006964 const bool new_inline = !page || create;
Chris Masona52d9a82007-08-27 16:49:44 -04006965
Chris Mason890871b2009-09-02 16:24:52 -04006966 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006967 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04006968 if (em)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006969 em->bdev = fs_info->fs_devices->latest_bdev;
Chris Mason890871b2009-09-02 16:24:52 -04006970 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006971
Chris Masona52d9a82007-08-27 16:49:44 -04006972 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04006973 if (em->start > start || em->start + em->len <= start)
6974 free_extent_map(em);
6975 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05006976 free_extent_map(em);
6977 else
6978 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006979 }
David Sterba172ddd62011-04-21 00:48:27 +02006980 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04006981 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05006982 err = -ENOMEM;
6983 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006984 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006985 em->bdev = fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05006986 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05006987 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05006988 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04006989 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04006990
Liu Bobee6ec82018-08-17 05:05:28 +08006991 path = btrfs_alloc_path();
Chris Masonf4219502008-07-22 11:18:09 -04006992 if (!path) {
Liu Bobee6ec82018-08-17 05:05:28 +08006993 err = -ENOMEM;
6994 goto out;
Chris Masonf4219502008-07-22 11:18:09 -04006995 }
6996
Liu Bobee6ec82018-08-17 05:05:28 +08006997 /* Chances are we'll be called again, so go ahead and do readahead */
6998 path->reada = READA_FORWARD;
6999
Liu Boe49aabd2018-08-25 13:47:09 +08007000 /*
7001 * Unless we're going to uncompress the inline extent, no sleep would
7002 * happen.
7003 */
7004 path->leave_spinning = 1;
7005
Nikolay Borisov5c9a7022017-12-01 11:19:40 +02007006 ret = btrfs_lookup_file_extent(NULL, root, path, objectid, start, 0);
Chris Masona52d9a82007-08-27 16:49:44 -04007007 if (ret < 0) {
7008 err = ret;
7009 goto out;
Nikolay Borisovb8eeab72018-12-17 11:49:00 +02007010 } else if (ret > 0) {
Chris Masona52d9a82007-08-27 16:49:44 -04007011 if (path->slots[0] == 0)
7012 goto not_found;
7013 path->slots[0]--;
7014 }
7015
Chris Mason5f39d392007-10-15 16:14:19 -04007016 leaf = path->nodes[0];
7017 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04007018 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04007019 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04007020 if (found_key.objectid != objectid ||
Nikolay Borisov694c12e2018-12-17 10:35:59 +02007021 found_key.type != BTRFS_EXTENT_DATA_KEY) {
Josef Bacik25a50342013-10-14 12:08:38 -04007022 /*
7023 * If we backup past the first extent we want to move forward
7024 * and see if there is an extent in front of us, otherwise we'll
7025 * say there is a hole for our whole search range which can
7026 * cause problems.
7027 */
7028 extent_end = start;
7029 goto next;
Chris Masona52d9a82007-08-27 16:49:44 -04007030 }
7031
Nikolay Borisov694c12e2018-12-17 10:35:59 +02007032 extent_type = btrfs_file_extent_type(leaf, item);
Chris Mason5f39d392007-10-15 16:14:19 -04007033 extent_start = found_key.offset;
Nikolay Borisov694c12e2018-12-17 10:35:59 +02007034 if (extent_type == BTRFS_FILE_EXTENT_REG ||
7035 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08007036 /* Only regular file could have regular/prealloc extent */
7037 if (!S_ISREG(inode->vfs_inode.i_mode)) {
7038 ret = -EUCLEAN;
7039 btrfs_crit(fs_info,
7040 "regular/prealloc extent found for non-regular inode %llu",
7041 btrfs_ino(inode));
7042 goto out;
7043 }
Chris Masona52d9a82007-08-27 16:49:44 -04007044 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04007045 btrfs_file_extent_num_bytes(leaf, item);
Liu Bo09ed2f12017-03-10 11:09:48 -08007046
7047 trace_btrfs_get_extent_show_fi_regular(inode, leaf, item,
7048 extent_start);
Nikolay Borisov694c12e2018-12-17 10:35:59 +02007049 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Yan Zheng9036c102008-10-30 14:19:41 -04007050 size_t size;
Qu Wenruoe41ca582018-06-06 15:41:49 +08007051
7052 size = btrfs_file_extent_ram_bytes(leaf, item);
Jeff Mahoneyda170662016-06-15 09:22:56 -04007053 extent_end = ALIGN(extent_start + size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007054 fs_info->sectorsize);
Liu Bo09ed2f12017-03-10 11:09:48 -08007055
7056 trace_btrfs_get_extent_show_fi_inline(inode, leaf, item,
7057 path->slots[0],
7058 extent_start);
Yan Zheng9036c102008-10-30 14:19:41 -04007059 }
Josef Bacik25a50342013-10-14 12:08:38 -04007060next:
Yan Zheng9036c102008-10-30 14:19:41 -04007061 if (start >= extent_end) {
7062 path->slots[0]++;
7063 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
7064 ret = btrfs_next_leaf(root, path);
7065 if (ret < 0) {
7066 err = ret;
7067 goto out;
Nikolay Borisovb8eeab72018-12-17 11:49:00 +02007068 } else if (ret > 0) {
Yan Zheng9036c102008-10-30 14:19:41 -04007069 goto not_found;
Nikolay Borisovb8eeab72018-12-17 11:49:00 +02007070 }
Yan Zheng9036c102008-10-30 14:19:41 -04007071 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04007072 }
Yan Zheng9036c102008-10-30 14:19:41 -04007073 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
7074 if (found_key.objectid != objectid ||
7075 found_key.type != BTRFS_EXTENT_DATA_KEY)
7076 goto not_found;
7077 if (start + len <= found_key.offset)
7078 goto not_found;
Wang Shilonge2eca692014-07-17 11:44:14 +08007079 if (start > found_key.offset)
7080 goto next;
Nikolay Borisov02a033d2018-12-17 10:36:02 +02007081
7082 /* New extent overlaps with existing one */
Yan Zheng9036c102008-10-30 14:19:41 -04007083 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04007084 em->orig_start = start;
Yan Zheng9036c102008-10-30 14:19:41 -04007085 em->len = found_key.offset - start;
Nikolay Borisov02a033d2018-12-17 10:36:02 +02007086 em->block_start = EXTENT_MAP_HOLE;
7087 goto insert;
Yan Zheng9036c102008-10-30 14:19:41 -04007088 }
7089
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007090 btrfs_extent_item_to_extent_map(inode, path, item,
Nikolay Borisov9cdc5122017-02-20 13:51:02 +02007091 new_inline, em);
Filipe Manana7ffbb592014-06-09 03:48:05 +01007092
Nikolay Borisov694c12e2018-12-17 10:35:59 +02007093 if (extent_type == BTRFS_FILE_EXTENT_REG ||
7094 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04007095 goto insert;
Nikolay Borisov694c12e2018-12-17 10:35:59 +02007096 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04007097 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04007098 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04007099 size_t size;
7100 size_t extent_offset;
7101 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04007102
Filipe Manana7ffbb592014-06-09 03:48:05 +01007103 if (new_inline)
Yan689f9342007-10-29 11:41:07 -04007104 goto out;
Yan689f9342007-10-29 11:41:07 -04007105
Qu Wenruoe41ca582018-06-06 15:41:49 +08007106 size = btrfs_file_extent_ram_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04007107 extent_offset = page_offset(page) + pg_offset - extent_start;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007108 copy_size = min_t(u64, PAGE_SIZE - pg_offset,
7109 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04007110 em->start = extent_start + extent_offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007111 em->len = ALIGN(copy_size, fs_info->sectorsize);
Josef Bacikb4939682012-12-03 10:31:19 -05007112 em->orig_block_len = em->len;
Josef Bacik70c8a912012-10-11 16:54:30 -04007113 em->orig_start = em->start;
Yan689f9342007-10-29 11:41:07 -04007114 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Liu Boe49aabd2018-08-25 13:47:09 +08007115
7116 btrfs_set_path_blocking(path);
Edmund Nadolskibf46f522017-11-20 13:24:49 -07007117 if (!PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08007118 if (btrfs_file_extent_compression(leaf, item) !=
7119 BTRFS_COMPRESS_NONE) {
Byongho Leee40da0e2015-05-19 23:46:45 +09007120 ret = uncompress_inline(path, page, pg_offset,
Chris Masonc8b97812008-10-29 14:49:59 -04007121 extent_offset, item);
Zach Brown166ae5a2014-05-09 17:15:10 -04007122 if (ret) {
7123 err = ret;
7124 goto out;
7125 }
Chris Masonc8b97812008-10-29 14:49:59 -04007126 } else {
7127 map = kmap(page);
7128 read_extent_buffer(leaf, map + pg_offset, ptr,
7129 copy_size);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007130 if (pg_offset + copy_size < PAGE_SIZE) {
Chris Mason93c82d52009-09-11 12:36:29 -04007131 memset(map + pg_offset + copy_size, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007132 PAGE_SIZE - pg_offset -
Chris Mason93c82d52009-09-11 12:36:29 -04007133 copy_size);
7134 }
Chris Masonc8b97812008-10-29 14:49:59 -04007135 kunmap(page);
7136 }
Chris Mason179e29e2007-11-01 11:28:41 -04007137 flush_dcache_page(page);
Chris Masona52d9a82007-08-27 16:49:44 -04007138 }
Chris Masond1310b22008-01-24 16:13:08 -05007139 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00007140 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04007141 goto insert;
Chris Masona52d9a82007-08-27 16:49:44 -04007142 }
7143not_found:
7144 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04007145 em->orig_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05007146 em->len = len;
Chris Mason5f39d392007-10-15 16:14:19 -04007147 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04007148insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02007149 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05007150 if (em->start > start || extent_map_end(em) <= start) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007151 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04007152 "bad extent! em: [%llu %llu] passed [%llu %llu]",
7153 em->start, em->len, start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04007154 err = -EIO;
7155 goto out;
7156 }
Chris Masond1310b22008-01-24 16:13:08 -05007157
7158 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04007159 write_lock(&em_tree->lock);
David Sterbaf46b24c2018-04-03 21:45:57 +02007160 err = btrfs_add_extent_mapping(fs_info, em_tree, &em, start, len);
Chris Mason890871b2009-09-02 16:24:52 -04007161 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04007162out:
Liu Boc6414282018-08-23 07:36:17 +08007163 btrfs_free_path(path);
liubo1abe9b82011-03-24 11:18:59 +00007164
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007165 trace_btrfs_get_extent(root, inode, em);
liubo1abe9b82011-03-24 11:18:59 +00007166
Chris Masona52d9a82007-08-27 16:49:44 -04007167 if (err) {
7168 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04007169 return ERR_PTR(err);
7170 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007171 BUG_ON(!em); /* Error is always set */
Chris Masona52d9a82007-08-27 16:49:44 -04007172 return em;
7173}
7174
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007175struct extent_map *btrfs_get_extent_fiemap(struct btrfs_inode *inode,
Nikolay Borisov4ab47a82018-12-12 09:42:32 +02007176 u64 start, u64 len)
Chris Masonec29ed52011-02-23 16:23:20 -05007177{
7178 struct extent_map *em;
7179 struct extent_map *hole_em = NULL;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007180 u64 delalloc_start = start;
Chris Masonec29ed52011-02-23 16:23:20 -05007181 u64 end;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007182 u64 delalloc_len;
7183 u64 delalloc_end;
Chris Masonec29ed52011-02-23 16:23:20 -05007184 int err = 0;
7185
Nikolay Borisov4ab47a82018-12-12 09:42:32 +02007186 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
Chris Masonec29ed52011-02-23 16:23:20 -05007187 if (IS_ERR(em))
7188 return em;
Dan Carpenter99862772017-04-11 11:57:15 +03007189 /*
7190 * If our em maps to:
7191 * - a hole or
7192 * - a pre-alloc extent,
7193 * there might actually be delalloc bytes behind it.
7194 */
7195 if (em->block_start != EXTENT_MAP_HOLE &&
7196 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7197 return em;
7198 else
7199 hole_em = em;
Chris Masonec29ed52011-02-23 16:23:20 -05007200
7201 /* check to see if we've wrapped (len == -1 or similar) */
7202 end = start + len;
7203 if (end < start)
7204 end = (u64)-1;
7205 else
7206 end -= 1;
7207
7208 em = NULL;
7209
7210 /* ok, we didn't find anything, lets look for delalloc */
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007211 delalloc_len = count_range_bits(&inode->io_tree, &delalloc_start,
Chris Masonec29ed52011-02-23 16:23:20 -05007212 end, len, EXTENT_DELALLOC, 1);
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007213 delalloc_end = delalloc_start + delalloc_len;
7214 if (delalloc_end < delalloc_start)
7215 delalloc_end = (u64)-1;
Chris Masonec29ed52011-02-23 16:23:20 -05007216
7217 /*
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007218 * We didn't find anything useful, return the original results from
7219 * get_extent()
Chris Masonec29ed52011-02-23 16:23:20 -05007220 */
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007221 if (delalloc_start > end || delalloc_end <= start) {
Chris Masonec29ed52011-02-23 16:23:20 -05007222 em = hole_em;
7223 hole_em = NULL;
7224 goto out;
7225 }
7226
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007227 /*
7228 * Adjust the delalloc_start to make sure it doesn't go backwards from
7229 * the start they passed in
Chris Masonec29ed52011-02-23 16:23:20 -05007230 */
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007231 delalloc_start = max(start, delalloc_start);
7232 delalloc_len = delalloc_end - delalloc_start;
Chris Masonec29ed52011-02-23 16:23:20 -05007233
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007234 if (delalloc_len > 0) {
7235 u64 hole_start;
Nikolay Borisov02950af2018-12-12 09:42:34 +02007236 u64 hole_len;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007237 const u64 hole_end = extent_map_end(hole_em);
Chris Masonec29ed52011-02-23 16:23:20 -05007238
David Sterba172ddd62011-04-21 00:48:27 +02007239 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05007240 if (!em) {
7241 err = -ENOMEM;
7242 goto out;
7243 }
Chris Masonec29ed52011-02-23 16:23:20 -05007244 em->bdev = NULL;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007245
7246 ASSERT(hole_em);
7247 /*
7248 * When btrfs_get_extent can't find anything it returns one
7249 * huge hole
7250 *
7251 * Make sure what it found really fits our range, and adjust to
7252 * make sure it is based on the start from the caller
7253 */
7254 if (hole_end <= start || hole_em->start > end) {
7255 free_extent_map(hole_em);
7256 hole_em = NULL;
7257 } else {
7258 hole_start = max(hole_em->start, start);
7259 hole_len = hole_end - hole_start;
7260 }
7261
7262 if (hole_em && delalloc_start > hole_start) {
7263 /*
7264 * Our hole starts before our delalloc, so we have to
7265 * return just the parts of the hole that go until the
7266 * delalloc starts
Chris Masonec29ed52011-02-23 16:23:20 -05007267 */
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007268 em->len = min(hole_len, delalloc_start - hole_start);
Chris Masonec29ed52011-02-23 16:23:20 -05007269 em->start = hole_start;
7270 em->orig_start = hole_start;
7271 /*
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007272 * Don't adjust block start at all, it is fixed at
7273 * EXTENT_MAP_HOLE
Chris Masonec29ed52011-02-23 16:23:20 -05007274 */
7275 em->block_start = hole_em->block_start;
7276 em->block_len = hole_len;
Liu Bof9e4fb52013-01-07 10:10:12 +00007277 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
7278 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonec29ed52011-02-23 16:23:20 -05007279 } else {
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007280 /*
7281 * Hole is out of passed range or it starts after
7282 * delalloc range
7283 */
7284 em->start = delalloc_start;
7285 em->len = delalloc_len;
7286 em->orig_start = delalloc_start;
Chris Masonec29ed52011-02-23 16:23:20 -05007287 em->block_start = EXTENT_MAP_DELALLOC;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007288 em->block_len = delalloc_len;
Chris Masonec29ed52011-02-23 16:23:20 -05007289 }
Nikolay Borisovbf8d32b2017-12-01 11:19:43 +02007290 } else {
Chris Masonec29ed52011-02-23 16:23:20 -05007291 return hole_em;
7292 }
7293out:
7294
7295 free_extent_map(hole_em);
7296 if (err) {
7297 free_extent_map(em);
7298 return ERR_PTR(err);
7299 }
7300 return em;
7301}
7302
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007303static struct extent_map *btrfs_create_dio_extent(struct inode *inode,
7304 const u64 start,
7305 const u64 len,
7306 const u64 orig_start,
7307 const u64 block_start,
7308 const u64 block_len,
7309 const u64 orig_block_len,
7310 const u64 ram_bytes,
7311 const int type)
7312{
7313 struct extent_map *em = NULL;
7314 int ret;
7315
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007316 if (type != BTRFS_ORDERED_NOCOW) {
Liu Bo6f9994d2017-01-31 07:50:22 -08007317 em = create_io_em(inode, start, len, orig_start,
7318 block_start, block_len, orig_block_len,
7319 ram_bytes,
7320 BTRFS_COMPRESS_NONE, /* compress_type */
7321 type);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007322 if (IS_ERR(em))
7323 goto out;
7324 }
7325 ret = btrfs_add_ordered_extent_dio(inode, start, block_start,
7326 len, block_len, type);
7327 if (ret) {
7328 if (em) {
7329 free_extent_map(em);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02007330 btrfs_drop_extent_cache(BTRFS_I(inode), start,
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007331 start + len - 1, 0);
7332 }
7333 em = ERR_PTR(ret);
7334 }
7335 out:
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007336
7337 return em;
7338}
7339
Josef Bacik4b46fce2010-05-23 11:00:55 -04007340static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
7341 u64 start, u64 len)
7342{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007343 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007344 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik70c8a912012-10-11 16:54:30 -04007345 struct extent_map *em;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007346 struct btrfs_key ins;
7347 u64 alloc_hint;
7348 int ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007349
Josef Bacik4b46fce2010-05-23 11:00:55 -04007350 alloc_hint = get_extent_allocation_hint(inode, start, len);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007351 ret = btrfs_reserve_extent(root, len, len, fs_info->sectorsize,
Jeff Mahoneyda170662016-06-15 09:22:56 -04007352 0, alloc_hint, &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007353 if (ret)
7354 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007355
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007356 em = btrfs_create_dio_extent(inode, start, ins.offset, start,
7357 ins.objectid, ins.offset, ins.offset,
Liu Bo6288d6e2017-02-21 12:12:58 -08007358 ins.offset, BTRFS_ORDERED_REGULAR);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007359 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007360 if (IS_ERR(em))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007361 btrfs_free_reserved_extent(fs_info, ins.objectid,
7362 ins.offset, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007363
Josef Bacik4b46fce2010-05-23 11:00:55 -04007364 return em;
7365}
7366
Chris Mason46bfbb52010-05-26 11:04:10 -04007367/*
7368 * returns 1 when the nocow is safe, < 1 on error, 0 if the
7369 * block must be cow'd
7370 */
Josef Bacik00361582013-08-14 14:02:47 -04007371noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
Josef Bacik7ee9e442013-06-21 16:37:03 -04007372 u64 *orig_start, u64 *orig_block_len,
7373 u64 *ram_bytes)
Chris Mason46bfbb52010-05-26 11:04:10 -04007374{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007375 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason46bfbb52010-05-26 11:04:10 -04007376 struct btrfs_path *path;
7377 int ret;
7378 struct extent_buffer *leaf;
7379 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xie7b2b7082014-02-27 13:58:05 +08007380 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason46bfbb52010-05-26 11:04:10 -04007381 struct btrfs_file_extent_item *fi;
7382 struct btrfs_key key;
7383 u64 disk_bytenr;
7384 u64 backref_offset;
7385 u64 extent_end;
7386 u64 num_bytes;
7387 int slot;
7388 int found_type;
Josef Bacik7ee9e442013-06-21 16:37:03 -04007389 bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW);
Miao Xiee77751a2013-12-27 21:11:50 +08007390
Chris Mason46bfbb52010-05-26 11:04:10 -04007391 path = btrfs_alloc_path();
7392 if (!path)
7393 return -ENOMEM;
7394
David Sterbaf85b7372017-01-20 14:54:07 +01007395 ret = btrfs_lookup_file_extent(NULL, root, path,
7396 btrfs_ino(BTRFS_I(inode)), offset, 0);
Chris Mason46bfbb52010-05-26 11:04:10 -04007397 if (ret < 0)
7398 goto out;
7399
7400 slot = path->slots[0];
7401 if (ret == 1) {
7402 if (slot == 0) {
7403 /* can't find the item, must cow */
7404 ret = 0;
7405 goto out;
7406 }
7407 slot--;
7408 }
7409 ret = 0;
7410 leaf = path->nodes[0];
7411 btrfs_item_key_to_cpu(leaf, &key, slot);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02007412 if (key.objectid != btrfs_ino(BTRFS_I(inode)) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04007413 key.type != BTRFS_EXTENT_DATA_KEY) {
7414 /* not our file or wrong item type, must cow */
7415 goto out;
7416 }
7417
7418 if (key.offset > offset) {
7419 /* Wrong offset, must cow */
7420 goto out;
7421 }
7422
7423 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
7424 found_type = btrfs_file_extent_type(leaf, fi);
7425 if (found_type != BTRFS_FILE_EXTENT_REG &&
7426 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
7427 /* not a regular extent, must cow */
7428 goto out;
7429 }
Josef Bacik7ee9e442013-06-21 16:37:03 -04007430
7431 if (!nocow && found_type == BTRFS_FILE_EXTENT_REG)
7432 goto out;
7433
Miao Xiee77751a2013-12-27 21:11:50 +08007434 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
7435 if (extent_end <= offset)
7436 goto out;
7437
Chris Mason46bfbb52010-05-26 11:04:10 -04007438 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Josef Bacik7ee9e442013-06-21 16:37:03 -04007439 if (disk_bytenr == 0)
7440 goto out;
7441
7442 if (btrfs_file_extent_compression(leaf, fi) ||
7443 btrfs_file_extent_encryption(leaf, fi) ||
7444 btrfs_file_extent_other_encoding(leaf, fi))
7445 goto out;
7446
Ethan Lien78d42952018-05-17 14:58:29 +08007447 /*
7448 * Do the same check as in btrfs_cross_ref_exist but without the
7449 * unnecessary search.
7450 */
7451 if (btrfs_file_extent_generation(leaf, fi) <=
7452 btrfs_root_last_snapshot(&root->root_item))
7453 goto out;
7454
Chris Mason46bfbb52010-05-26 11:04:10 -04007455 backref_offset = btrfs_file_extent_offset(leaf, fi);
7456
Josef Bacik7ee9e442013-06-21 16:37:03 -04007457 if (orig_start) {
7458 *orig_start = key.offset - backref_offset;
7459 *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
7460 *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
7461 }
Josef Bacikeb384b52013-04-24 16:32:55 -04007462
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007463 if (btrfs_extent_readonly(fs_info, disk_bytenr))
Chris Mason46bfbb52010-05-26 11:04:10 -04007464 goto out;
Miao Xie7b2b7082014-02-27 13:58:05 +08007465
7466 num_bytes = min(offset + *len, extent_end) - offset;
7467 if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) {
7468 u64 range_end;
7469
Jeff Mahoneyda170662016-06-15 09:22:56 -04007470 range_end = round_up(offset + num_bytes,
7471 root->fs_info->sectorsize) - 1;
Miao Xie7b2b7082014-02-27 13:58:05 +08007472 ret = test_range_bit(io_tree, offset, range_end,
7473 EXTENT_DELALLOC, 0, NULL);
7474 if (ret) {
7475 ret = -EAGAIN;
7476 goto out;
7477 }
7478 }
7479
Josef Bacik1bda19e2013-10-18 12:10:36 -04007480 btrfs_release_path(path);
Chris Mason46bfbb52010-05-26 11:04:10 -04007481
7482 /*
7483 * look for other files referencing this extent, if we
7484 * find any we must cow
7485 */
Josef Bacik00361582013-08-14 14:02:47 -04007486
Liu Boe4c3b2d2017-01-30 12:25:28 -08007487 ret = btrfs_cross_ref_exist(root, btrfs_ino(BTRFS_I(inode)),
Josef Bacik00361582013-08-14 14:02:47 -04007488 key.offset - backref_offset, disk_bytenr);
Josef Bacik00361582013-08-14 14:02:47 -04007489 if (ret) {
7490 ret = 0;
7491 goto out;
7492 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007493
7494 /*
7495 * adjust disk_bytenr and num_bytes to cover just the bytes
7496 * in this extent we are about to write. If there
7497 * are any csums in that range we have to cow in order
7498 * to keep the csums correct
7499 */
7500 disk_bytenr += backref_offset;
7501 disk_bytenr += offset - key.offset;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007502 if (csum_exist_in_range(fs_info, disk_bytenr, num_bytes))
7503 goto out;
Chris Mason46bfbb52010-05-26 11:04:10 -04007504 /*
7505 * all of the above have passed, it is safe to overwrite this extent
7506 * without cow
7507 */
Josef Bacikeb384b52013-04-24 16:32:55 -04007508 *len = num_bytes;
Chris Mason46bfbb52010-05-26 11:04:10 -04007509 ret = 1;
7510out:
7511 btrfs_free_path(path);
7512 return ret;
7513}
7514
Josef Bacikeb838e72012-07-31 16:28:48 -04007515static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
7516 struct extent_state **cached_state, int writing)
7517{
7518 struct btrfs_ordered_extent *ordered;
7519 int ret = 0;
7520
7521 while (1) {
7522 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbaff13db42015-12-03 14:30:40 +01007523 cached_state);
Josef Bacikeb838e72012-07-31 16:28:48 -04007524 /*
7525 * We're concerned with the entire range that we're going to be
Nicholas D Steeves01327612016-05-19 21:18:45 -04007526 * doing DIO to, so we need to make sure there's no ordered
Josef Bacikeb838e72012-07-31 16:28:48 -04007527 * extents in this range.
7528 */
Nikolay Borisova776c6f2017-02-20 13:50:49 +02007529 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), lockstart,
Josef Bacikeb838e72012-07-31 16:28:48 -04007530 lockend - lockstart + 1);
7531
7532 /*
7533 * We need to make sure there are no buffered pages in this
7534 * range either, we could have raced between the invalidate in
7535 * generic_file_direct_write and locking the extent. The
7536 * invalidate needs to happen so that reads after a write do not
7537 * get stale data.
7538 */
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007539 if (!ordered &&
David Sterba051c98e2018-03-07 15:33:22 +01007540 (!writing || !filemap_range_has_page(inode->i_mapping,
7541 lockstart, lockend)))
Josef Bacikeb838e72012-07-31 16:28:48 -04007542 break;
7543
7544 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbae43bbe52017-12-12 21:43:52 +01007545 cached_state);
Josef Bacikeb838e72012-07-31 16:28:48 -04007546
7547 if (ordered) {
Filipe Mananaade77022016-02-18 14:28:55 +00007548 /*
7549 * If we are doing a DIO read and the ordered extent we
7550 * found is for a buffered write, we can not wait for it
7551 * to complete and retry, because if we do so we can
7552 * deadlock with concurrent buffered writes on page
7553 * locks. This happens only if our DIO read covers more
7554 * than one extent map, if at this point has already
7555 * created an ordered extent for a previous extent map
7556 * and locked its range in the inode's io tree, and a
7557 * concurrent write against that previous extent map's
7558 * range and this range started (we unlock the ranges
7559 * in the io tree only when the bios complete and
7560 * buffered writes always lock pages before attempting
7561 * to lock range in the io tree).
7562 */
7563 if (writing ||
7564 test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags))
7565 btrfs_start_ordered_extent(inode, ordered, 1);
7566 else
7567 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007568 btrfs_put_ordered_extent(ordered);
7569 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007570 /*
Filipe Mananab850ae12015-12-08 16:23:16 +00007571 * We could trigger writeback for this range (and wait
7572 * for it to complete) and then invalidate the pages for
7573 * this range (through invalidate_inode_pages2_range()),
7574 * but that can lead us to a deadlock with a concurrent
7575 * call to readpages() (a buffered read or a defrag call
7576 * triggered a readahead) on a page lock due to an
7577 * ordered dio extent we created before but did not have
7578 * yet a corresponding bio submitted (whence it can not
7579 * complete), which makes readpages() wait for that
7580 * ordered extent to complete while holding a lock on
7581 * that page.
Josef Bacikeb838e72012-07-31 16:28:48 -04007582 */
Filipe Mananab850ae12015-12-08 16:23:16 +00007583 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007584 }
7585
Filipe Mananaade77022016-02-18 14:28:55 +00007586 if (ret)
7587 break;
7588
Josef Bacikeb838e72012-07-31 16:28:48 -04007589 cond_resched();
7590 }
7591
7592 return ret;
7593}
7594
Liu Bo6f9994d2017-01-31 07:50:22 -08007595/* The callers of this must take lock_extent() */
7596static struct extent_map *create_io_em(struct inode *inode, u64 start, u64 len,
7597 u64 orig_start, u64 block_start,
7598 u64 block_len, u64 orig_block_len,
7599 u64 ram_bytes, int compress_type,
7600 int type)
Josef Bacik69ffb542012-09-11 15:40:07 -04007601{
7602 struct extent_map_tree *em_tree;
7603 struct extent_map *em;
7604 struct btrfs_root *root = BTRFS_I(inode)->root;
7605 int ret;
7606
Liu Bo6f9994d2017-01-31 07:50:22 -08007607 ASSERT(type == BTRFS_ORDERED_PREALLOC ||
7608 type == BTRFS_ORDERED_COMPRESSED ||
7609 type == BTRFS_ORDERED_NOCOW ||
Liu Bo1af4a0a2017-02-13 15:35:09 -08007610 type == BTRFS_ORDERED_REGULAR);
Liu Bo6f9994d2017-01-31 07:50:22 -08007611
Josef Bacik69ffb542012-09-11 15:40:07 -04007612 em_tree = &BTRFS_I(inode)->extent_tree;
7613 em = alloc_extent_map();
7614 if (!em)
7615 return ERR_PTR(-ENOMEM);
7616
7617 em->start = start;
7618 em->orig_start = orig_start;
7619 em->len = len;
7620 em->block_len = block_len;
7621 em->block_start = block_start;
7622 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacikb4939682012-12-03 10:31:19 -05007623 em->orig_block_len = orig_block_len;
Josef Bacikcc95bef2013-04-04 14:31:27 -04007624 em->ram_bytes = ram_bytes;
Josef Bacik70c8a912012-10-11 16:54:30 -04007625 em->generation = -1;
Josef Bacik69ffb542012-09-11 15:40:07 -04007626 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Liu Bo1af4a0a2017-02-13 15:35:09 -08007627 if (type == BTRFS_ORDERED_PREALLOC) {
Josef Bacikb11e2342012-12-03 10:58:15 -05007628 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Liu Bo1af4a0a2017-02-13 15:35:09 -08007629 } else if (type == BTRFS_ORDERED_COMPRESSED) {
Liu Bo6f9994d2017-01-31 07:50:22 -08007630 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
7631 em->compress_type = compress_type;
7632 }
Josef Bacik69ffb542012-09-11 15:40:07 -04007633
7634 do {
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02007635 btrfs_drop_extent_cache(BTRFS_I(inode), em->start,
Josef Bacik69ffb542012-09-11 15:40:07 -04007636 em->start + em->len - 1, 0);
7637 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04007638 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik69ffb542012-09-11 15:40:07 -04007639 write_unlock(&em_tree->lock);
Liu Bo6f9994d2017-01-31 07:50:22 -08007640 /*
7641 * The caller has taken lock_extent(), who could race with us
7642 * to add em?
7643 */
Josef Bacik69ffb542012-09-11 15:40:07 -04007644 } while (ret == -EEXIST);
7645
7646 if (ret) {
7647 free_extent_map(em);
7648 return ERR_PTR(ret);
7649 }
7650
Liu Bo6f9994d2017-01-31 07:50:22 -08007651 /* em got 2 refs now, callers needs to do free_extent_map once. */
Josef Bacik69ffb542012-09-11 15:40:07 -04007652 return em;
7653}
7654
Nikolay Borisov1c8d0172018-05-02 15:19:32 +03007655
7656static int btrfs_get_blocks_direct_read(struct extent_map *em,
7657 struct buffer_head *bh_result,
7658 struct inode *inode,
7659 u64 start, u64 len)
7660{
7661 if (em->block_start == EXTENT_MAP_HOLE ||
7662 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7663 return -ENOENT;
7664
7665 len = min(len, em->len - (start - em->start));
7666
7667 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
7668 inode->i_blkbits;
7669 bh_result->b_size = len;
7670 bh_result->b_bdev = em->bdev;
7671 set_buffer_mapped(bh_result);
7672
7673 return 0;
7674}
7675
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007676static int btrfs_get_blocks_direct_write(struct extent_map **map,
7677 struct buffer_head *bh_result,
7678 struct inode *inode,
7679 struct btrfs_dio_data *dio_data,
7680 u64 start, u64 len)
7681{
7682 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
7683 struct extent_map *em = *map;
7684 int ret = 0;
7685
7686 /*
7687 * We don't allocate a new extent in the following cases
7688 *
7689 * 1) The inode is marked as NODATACOW. In this case we'll just use the
7690 * existing extent.
7691 * 2) The extent is marked as PREALLOC. We're good to go here and can
7692 * just use the extent.
7693 *
7694 */
7695 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
7696 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
7697 em->block_start != EXTENT_MAP_HOLE)) {
7698 int type;
7699 u64 block_start, orig_start, orig_block_len, ram_bytes;
7700
7701 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7702 type = BTRFS_ORDERED_PREALLOC;
7703 else
7704 type = BTRFS_ORDERED_NOCOW;
7705 len = min(len, em->len - (start - em->start));
7706 block_start = em->block_start + (start - em->start);
7707
7708 if (can_nocow_extent(inode, start, &len, &orig_start,
7709 &orig_block_len, &ram_bytes) == 1 &&
7710 btrfs_inc_nocow_writers(fs_info, block_start)) {
7711 struct extent_map *em2;
7712
7713 em2 = btrfs_create_dio_extent(inode, start, len,
7714 orig_start, block_start,
7715 len, orig_block_len,
7716 ram_bytes, type);
7717 btrfs_dec_nocow_writers(fs_info, block_start);
7718 if (type == BTRFS_ORDERED_PREALLOC) {
7719 free_extent_map(em);
7720 *map = em = em2;
7721 }
7722
7723 if (em2 && IS_ERR(em2)) {
7724 ret = PTR_ERR(em2);
7725 goto out;
7726 }
7727 /*
7728 * For inode marked NODATACOW or extent marked PREALLOC,
7729 * use the existing or preallocated extent, so does not
7730 * need to adjust btrfs_space_info's bytes_may_use.
7731 */
7732 btrfs_free_reserved_data_space_noquota(inode, start,
7733 len);
7734 goto skip_cow;
7735 }
7736 }
7737
7738 /* this will cow the extent */
7739 len = bh_result->b_size;
7740 free_extent_map(em);
7741 *map = em = btrfs_new_extent_direct(inode, start, len);
7742 if (IS_ERR(em)) {
7743 ret = PTR_ERR(em);
7744 goto out;
7745 }
7746
7747 len = min(len, em->len - (start - em->start));
7748
7749skip_cow:
7750 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
7751 inode->i_blkbits;
7752 bh_result->b_size = len;
7753 bh_result->b_bdev = em->bdev;
7754 set_buffer_mapped(bh_result);
7755
7756 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7757 set_buffer_new(bh_result);
7758
7759 /*
7760 * Need to update the i_size under the extent lock so buffered
7761 * readers will get the updated i_size when we unlock.
7762 */
7763 if (!dio_data->overwrite && start + len > i_size_read(inode))
7764 i_size_write(inode, start + len);
7765
7766 WARN_ON(dio_data->reserve < len);
7767 dio_data->reserve -= len;
7768 dio_data->unsubmitted_oe_range_end = start + len;
7769 current->journal_info = dio_data;
7770out:
7771 return ret;
7772}
7773
Josef Bacik4b46fce2010-05-23 11:00:55 -04007774static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
7775 struct buffer_head *bh_result, int create)
7776{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007777 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007778 struct extent_map *em;
Josef Bacikeb838e72012-07-31 16:28:48 -04007779 struct extent_state *cached_state = NULL;
chandan50745b02015-08-28 21:10:13 +05307780 struct btrfs_dio_data *dio_data = NULL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007781 u64 start = iblock << inode->i_blkbits;
Josef Bacikeb838e72012-07-31 16:28:48 -04007782 u64 lockstart, lockend;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007783 u64 len = bh_result->b_size;
Miao Xie09348562013-02-07 10:12:07 +00007784 int ret = 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007785
Omar Sandovale1821632019-08-15 14:04:04 -07007786 if (!create)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007787 len = min_t(u64, len, fs_info->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04007788
Josef Bacikc3298612012-08-03 16:49:19 -04007789 lockstart = start;
7790 lockend = start + len - 1;
7791
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007792 if (current->journal_info) {
7793 /*
7794 * Need to pull our outstanding extents and set journal_info to NULL so
Nicholas D Steeves01327612016-05-19 21:18:45 -04007795 * that anything that needs to check if there's a transaction doesn't get
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007796 * confused.
7797 */
chandan50745b02015-08-28 21:10:13 +05307798 dio_data = current->journal_info;
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007799 current->journal_info = NULL;
7800 }
7801
Josef Bacikeb838e72012-07-31 16:28:48 -04007802 /*
7803 * If this errors out it's because we couldn't invalidate pagecache for
7804 * this range and we need to fallback to buffered.
7805 */
Filipe Manana9c9464c2015-11-04 09:52:04 +00007806 if (lock_extent_direct(inode, lockstart, lockend, &cached_state,
7807 create)) {
7808 ret = -ENOTBLK;
7809 goto err;
7810 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007811
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007812 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len, 0);
Josef Bacikeb838e72012-07-31 16:28:48 -04007813 if (IS_ERR(em)) {
7814 ret = PTR_ERR(em);
7815 goto unlock_err;
7816 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007817
7818 /*
7819 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
7820 * io. INLINE is special, and we could probably kludge it in here, but
7821 * it's still buffered so for safety lets just fall back to the generic
7822 * buffered path.
7823 *
7824 * For COMPRESSED we _have_ to read the entire extent in so we can
7825 * decompress it, so there will be buffering required no matter what we
7826 * do, so go ahead and fallback to buffered.
7827 *
Nicholas D Steeves01327612016-05-19 21:18:45 -04007828 * We return -ENOTBLK because that's what makes DIO go ahead and go back
Josef Bacik4b46fce2010-05-23 11:00:55 -04007829 * to buffered IO. Don't blame me, this is the price we pay for using
7830 * the generic code.
7831 */
7832 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
7833 em->block_start == EXTENT_MAP_INLINE) {
7834 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007835 ret = -ENOTBLK;
7836 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007837 }
7838
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007839 if (create) {
7840 ret = btrfs_get_blocks_direct_write(&em, bh_result, inode,
7841 dio_data, start, len);
7842 if (ret < 0)
7843 goto unlock_err;
7844
Omar Sandovale1821632019-08-15 14:04:04 -07007845 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart,
7846 lockend, &cached_state);
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007847 } else {
Nikolay Borisov1c8d0172018-05-02 15:19:32 +03007848 ret = btrfs_get_blocks_direct_read(em, bh_result, inode,
7849 start, len);
7850 /* Can be negative only if we read from a hole */
7851 if (ret < 0) {
7852 ret = 0;
7853 free_extent_map(em);
7854 goto unlock_err;
7855 }
7856 /*
7857 * We need to unlock only the end area that we aren't using.
7858 * The rest is going to be unlocked by the endio routine.
7859 */
7860 lockstart = start + bh_result->b_size;
7861 if (lockstart < lockend) {
Omar Sandovale1821632019-08-15 14:04:04 -07007862 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
7863 lockstart, lockend, &cached_state);
Nikolay Borisov1c8d0172018-05-02 15:19:32 +03007864 } else {
7865 free_extent_state(cached_state);
7866 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007867 }
7868
Josef Bacik4b46fce2010-05-23 11:00:55 -04007869 free_extent_map(em);
7870
7871 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007872
7873unlock_err:
Omar Sandovale1821632019-08-15 14:04:04 -07007874 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7875 &cached_state);
Filipe Manana9c9464c2015-11-04 09:52:04 +00007876err:
chandan50745b02015-08-28 21:10:13 +05307877 if (dio_data)
7878 current->journal_info = dio_data;
Josef Bacikeb838e72012-07-31 16:28:48 -04007879 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007880}
7881
Omar Sandoval58efbc92017-08-22 23:45:59 -07007882static inline blk_status_t submit_dio_repair_bio(struct inode *inode,
7883 struct bio *bio,
7884 int mirror_num)
Miao Xie8b110e32014-09-12 18:44:03 +08007885{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007886 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Omar Sandoval58efbc92017-08-22 23:45:59 -07007887 blk_status_t ret;
Miao Xie8b110e32014-09-12 18:44:03 +08007888
Mike Christie37226b22016-06-05 14:31:52 -05007889 BUG_ON(bio_op(bio) == REQ_OP_WRITE);
Miao Xie8b110e32014-09-12 18:44:03 +08007890
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007891 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DIO_REPAIR);
Miao Xie8b110e32014-09-12 18:44:03 +08007892 if (ret)
Nikolay Borisovea057f62017-12-13 10:25:38 +02007893 return ret;
Miao Xie8b110e32014-09-12 18:44:03 +08007894
Chris Mason08635ba2019-07-10 12:28:14 -07007895 ret = btrfs_map_bio(fs_info, bio, mirror_num);
Nikolay Borisovea057f62017-12-13 10:25:38 +02007896
Miao Xie8b110e32014-09-12 18:44:03 +08007897 return ret;
7898}
7899
7900static int btrfs_check_dio_repairable(struct inode *inode,
7901 struct bio *failed_bio,
7902 struct io_failure_record *failrec,
7903 int failed_mirror)
7904{
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007905 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xie8b110e32014-09-12 18:44:03 +08007906 int num_copies;
7907
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007908 num_copies = btrfs_num_copies(fs_info, failrec->logical, failrec->len);
Miao Xie8b110e32014-09-12 18:44:03 +08007909 if (num_copies == 1) {
7910 /*
7911 * we only have a single copy of the data, so don't bother with
7912 * all the retry and error correction code that follows. no
7913 * matter what the error is, it is very likely to persist.
7914 */
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007915 btrfs_debug(fs_info,
7916 "Check DIO Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d",
7917 num_copies, failrec->this_mirror, failed_mirror);
Miao Xie8b110e32014-09-12 18:44:03 +08007918 return 0;
7919 }
7920
7921 failrec->failed_mirror = failed_mirror;
7922 failrec->this_mirror++;
7923 if (failrec->this_mirror == failed_mirror)
7924 failrec->this_mirror++;
7925
7926 if (failrec->this_mirror > num_copies) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007927 btrfs_debug(fs_info,
7928 "Check DIO Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d",
7929 num_copies, failrec->this_mirror, failed_mirror);
Miao Xie8b110e32014-09-12 18:44:03 +08007930 return 0;
7931 }
7932
7933 return 1;
7934}
7935
Omar Sandoval58efbc92017-08-22 23:45:59 -07007936static blk_status_t dio_read_error(struct inode *inode, struct bio *failed_bio,
7937 struct page *page, unsigned int pgoff,
7938 u64 start, u64 end, int failed_mirror,
7939 bio_end_io_t *repair_endio, void *repair_arg)
Miao Xie8b110e32014-09-12 18:44:03 +08007940{
7941 struct io_failure_record *failrec;
Josef Bacik7870d082017-05-05 11:57:15 -04007942 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
7943 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
Miao Xie8b110e32014-09-12 18:44:03 +08007944 struct bio *bio;
7945 int isector;
David Sterbaf1c77c52017-06-06 19:03:49 +02007946 unsigned int read_mode = 0;
Liu Bo17347ce2017-05-15 15:33:27 -07007947 int segs;
Miao Xie8b110e32014-09-12 18:44:03 +08007948 int ret;
Omar Sandoval58efbc92017-08-22 23:45:59 -07007949 blk_status_t status;
Ming Leic16a8ac2017-12-18 20:22:12 +08007950 struct bio_vec bvec;
Miao Xie8b110e32014-09-12 18:44:03 +08007951
Mike Christie37226b22016-06-05 14:31:52 -05007952 BUG_ON(bio_op(failed_bio) == REQ_OP_WRITE);
Miao Xie8b110e32014-09-12 18:44:03 +08007953
7954 ret = btrfs_get_io_failure_record(inode, start, end, &failrec);
7955 if (ret)
Omar Sandoval58efbc92017-08-22 23:45:59 -07007956 return errno_to_blk_status(ret);
Miao Xie8b110e32014-09-12 18:44:03 +08007957
7958 ret = btrfs_check_dio_repairable(inode, failed_bio, failrec,
7959 failed_mirror);
7960 if (!ret) {
Josef Bacik7870d082017-05-05 11:57:15 -04007961 free_io_failure(failure_tree, io_tree, failrec);
Omar Sandoval58efbc92017-08-22 23:45:59 -07007962 return BLK_STS_IOERR;
Miao Xie8b110e32014-09-12 18:44:03 +08007963 }
7964
Liu Bo17347ce2017-05-15 15:33:27 -07007965 segs = bio_segments(failed_bio);
Ming Leic16a8ac2017-12-18 20:22:12 +08007966 bio_get_first_bvec(failed_bio, &bvec);
Liu Bo17347ce2017-05-15 15:33:27 -07007967 if (segs > 1 ||
Ming Leic16a8ac2017-12-18 20:22:12 +08007968 (bvec.bv_len > btrfs_inode_sectorsize(inode)))
Christoph Hellwig70fd7612016-11-01 07:40:10 -06007969 read_mode |= REQ_FAILFAST_DEV;
Miao Xie8b110e32014-09-12 18:44:03 +08007970
7971 isector = start - btrfs_io_bio(failed_bio)->logical;
7972 isector >>= inode->i_sb->s_blocksize_bits;
7973 bio = btrfs_create_repair_bio(inode, failed_bio, failrec, page,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307974 pgoff, isector, repair_endio, repair_arg);
David Sterbaebcc3262018-06-29 10:56:53 +02007975 bio->bi_opf = REQ_OP_READ | read_mode;
Miao Xie8b110e32014-09-12 18:44:03 +08007976
7977 btrfs_debug(BTRFS_I(inode)->root->fs_info,
David Sterba913e1532017-07-13 15:32:18 +02007978 "repair DIO read error: submitting new dio read[%#x] to this_mirror=%d, in_validation=%d",
Miao Xie8b110e32014-09-12 18:44:03 +08007979 read_mode, failrec->this_mirror, failrec->in_validation);
7980
Omar Sandoval58efbc92017-08-22 23:45:59 -07007981 status = submit_dio_repair_bio(inode, bio, failrec->this_mirror);
7982 if (status) {
Josef Bacik7870d082017-05-05 11:57:15 -04007983 free_io_failure(failure_tree, io_tree, failrec);
Miao Xie8b110e32014-09-12 18:44:03 +08007984 bio_put(bio);
7985 }
7986
Omar Sandoval58efbc92017-08-22 23:45:59 -07007987 return status;
Miao Xie8b110e32014-09-12 18:44:03 +08007988}
7989
7990struct btrfs_retry_complete {
7991 struct completion done;
7992 struct inode *inode;
7993 u64 start;
7994 int uptodate;
7995};
7996
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007997static void btrfs_retry_endio_nocsum(struct bio *bio)
Miao Xie8b110e32014-09-12 18:44:03 +08007998{
7999 struct btrfs_retry_complete *done = bio->bi_private;
Josef Bacik7870d082017-05-05 11:57:15 -04008000 struct inode *inode = done->inode;
Miao Xie8b110e32014-09-12 18:44:03 +08008001 struct bio_vec *bvec;
Josef Bacik7870d082017-05-05 11:57:15 -04008002 struct extent_io_tree *io_tree, *failure_tree;
Ming Lei6dc4f102019-02-15 19:13:19 +08008003 struct bvec_iter_all iter_all;
Miao Xie8b110e32014-09-12 18:44:03 +08008004
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008005 if (bio->bi_status)
Miao Xie8b110e32014-09-12 18:44:03 +08008006 goto end;
8007
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308008 ASSERT(bio->bi_vcnt == 1);
Josef Bacik7870d082017-05-05 11:57:15 -04008009 io_tree = &BTRFS_I(inode)->io_tree;
8010 failure_tree = &BTRFS_I(inode)->io_failure_tree;
Ming Lei263663c2017-12-18 20:22:04 +08008011 ASSERT(bio_first_bvec_all(bio)->bv_len == btrfs_inode_sectorsize(inode));
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308012
Miao Xie8b110e32014-09-12 18:44:03 +08008013 done->uptodate = 1;
David Sterbac09abff2017-07-13 18:10:07 +02008014 ASSERT(!bio_flagged(bio, BIO_CLONED));
Christoph Hellwig2b070cf2019-04-25 09:03:00 +02008015 bio_for_each_segment_all(bvec, bio, iter_all)
Josef Bacik7870d082017-05-05 11:57:15 -04008016 clean_io_failure(BTRFS_I(inode)->root->fs_info, failure_tree,
8017 io_tree, done->start, bvec->bv_page,
8018 btrfs_ino(BTRFS_I(inode)), 0);
Miao Xie8b110e32014-09-12 18:44:03 +08008019end:
8020 complete(&done->done);
8021 bio_put(bio);
8022}
8023
Omar Sandoval58efbc92017-08-22 23:45:59 -07008024static blk_status_t __btrfs_correct_data_nocsum(struct inode *inode,
8025 struct btrfs_io_bio *io_bio)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008026{
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308027 struct btrfs_fs_info *fs_info;
Liu Bo17347ce2017-05-15 15:33:27 -07008028 struct bio_vec bvec;
8029 struct bvec_iter iter;
Miao Xie8b110e32014-09-12 18:44:03 +08008030 struct btrfs_retry_complete done;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008031 u64 start;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308032 unsigned int pgoff;
8033 u32 sectorsize;
8034 int nr_sectors;
Omar Sandoval58efbc92017-08-22 23:45:59 -07008035 blk_status_t ret;
8036 blk_status_t err = BLK_STS_OK;
Miao Xiedc380ae2014-09-12 18:43:55 +08008037
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308038 fs_info = BTRFS_I(inode)->root->fs_info;
Jeff Mahoneyda170662016-06-15 09:22:56 -04008039 sectorsize = fs_info->sectorsize;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308040
Miao Xiec1dc0892014-09-12 18:43:56 +08008041 start = io_bio->logical;
Miao Xie8b110e32014-09-12 18:44:03 +08008042 done.inode = inode;
Liu Bo17347ce2017-05-15 15:33:27 -07008043 io_bio->bio.bi_iter = io_bio->iter;
Miao Xie8b110e32014-09-12 18:44:03 +08008044
Liu Bo17347ce2017-05-15 15:33:27 -07008045 bio_for_each_segment(bvec, &io_bio->bio, iter) {
8046 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec.bv_len);
8047 pgoff = bvec.bv_offset;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308048
8049next_block_or_try_again:
Miao Xie8b110e32014-09-12 18:44:03 +08008050 done.uptodate = 0;
8051 done.start = start;
8052 init_completion(&done.done);
8053
Liu Bo17347ce2017-05-15 15:33:27 -07008054 ret = dio_read_error(inode, &io_bio->bio, bvec.bv_page,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308055 pgoff, start, start + sectorsize - 1,
8056 io_bio->mirror_num,
8057 btrfs_retry_endio_nocsum, &done);
Liu Bo629ebf42017-05-15 17:20:07 -07008058 if (ret) {
8059 err = ret;
8060 goto next;
8061 }
Miao Xie8b110e32014-09-12 18:44:03 +08008062
David Sterba9c17f6c2017-07-19 19:26:45 +02008063 wait_for_completion_io(&done.done);
Miao Xie8b110e32014-09-12 18:44:03 +08008064
8065 if (!done.uptodate) {
8066 /* We might have another mirror, so try again */
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308067 goto next_block_or_try_again;
Miao Xie8b110e32014-09-12 18:44:03 +08008068 }
8069
Liu Bo629ebf42017-05-15 17:20:07 -07008070next:
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308071 start += sectorsize;
8072
Liu Bo97bf5a52017-04-07 13:11:10 -07008073 nr_sectors--;
8074 if (nr_sectors) {
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308075 pgoff += sectorsize;
Liu Bo97bf5a52017-04-07 13:11:10 -07008076 ASSERT(pgoff < PAGE_SIZE);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308077 goto next_block_or_try_again;
8078 }
Miao Xie8b110e32014-09-12 18:44:03 +08008079 }
8080
Liu Bo629ebf42017-05-15 17:20:07 -07008081 return err;
Miao Xie8b110e32014-09-12 18:44:03 +08008082}
8083
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008084static void btrfs_retry_endio(struct bio *bio)
Miao Xie8b110e32014-09-12 18:44:03 +08008085{
8086 struct btrfs_retry_complete *done = bio->bi_private;
8087 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
Josef Bacik7870d082017-05-05 11:57:15 -04008088 struct extent_io_tree *io_tree, *failure_tree;
8089 struct inode *inode = done->inode;
Miao Xie8b110e32014-09-12 18:44:03 +08008090 struct bio_vec *bvec;
8091 int uptodate;
8092 int ret;
Christoph Hellwig2b070cf2019-04-25 09:03:00 +02008093 int i = 0;
Ming Lei6dc4f102019-02-15 19:13:19 +08008094 struct bvec_iter_all iter_all;
Miao Xie8b110e32014-09-12 18:44:03 +08008095
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008096 if (bio->bi_status)
Miao Xie8b110e32014-09-12 18:44:03 +08008097 goto end;
8098
8099 uptodate = 1;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308100
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308101 ASSERT(bio->bi_vcnt == 1);
Ming Lei263663c2017-12-18 20:22:04 +08008102 ASSERT(bio_first_bvec_all(bio)->bv_len == btrfs_inode_sectorsize(done->inode));
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308103
Josef Bacik7870d082017-05-05 11:57:15 -04008104 io_tree = &BTRFS_I(inode)->io_tree;
8105 failure_tree = &BTRFS_I(inode)->io_failure_tree;
8106
David Sterbac09abff2017-07-13 18:10:07 +02008107 ASSERT(!bio_flagged(bio, BIO_CLONED));
Christoph Hellwig2b070cf2019-04-25 09:03:00 +02008108 bio_for_each_segment_all(bvec, bio, iter_all) {
Josef Bacik7870d082017-05-05 11:57:15 -04008109 ret = __readpage_endio_check(inode, io_bio, i, bvec->bv_page,
8110 bvec->bv_offset, done->start,
8111 bvec->bv_len);
Miao Xie8b110e32014-09-12 18:44:03 +08008112 if (!ret)
Josef Bacik7870d082017-05-05 11:57:15 -04008113 clean_io_failure(BTRFS_I(inode)->root->fs_info,
8114 failure_tree, io_tree, done->start,
8115 bvec->bv_page,
8116 btrfs_ino(BTRFS_I(inode)),
8117 bvec->bv_offset);
Miao Xie8b110e32014-09-12 18:44:03 +08008118 else
8119 uptodate = 0;
Christoph Hellwig2b070cf2019-04-25 09:03:00 +02008120 i++;
Miao Xie8b110e32014-09-12 18:44:03 +08008121 }
8122
8123 done->uptodate = uptodate;
8124end:
8125 complete(&done->done);
8126 bio_put(bio);
8127}
8128
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008129static blk_status_t __btrfs_subio_endio_read(struct inode *inode,
8130 struct btrfs_io_bio *io_bio, blk_status_t err)
Miao Xie8b110e32014-09-12 18:44:03 +08008131{
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308132 struct btrfs_fs_info *fs_info;
Liu Bo17347ce2017-05-15 15:33:27 -07008133 struct bio_vec bvec;
8134 struct bvec_iter iter;
Miao Xie8b110e32014-09-12 18:44:03 +08008135 struct btrfs_retry_complete done;
8136 u64 start;
8137 u64 offset = 0;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308138 u32 sectorsize;
8139 int nr_sectors;
8140 unsigned int pgoff;
8141 int csum_pos;
Liu Boef7cdac12017-04-13 18:11:48 -07008142 bool uptodate = (err == 0);
Miao Xie8b110e32014-09-12 18:44:03 +08008143 int ret;
Omar Sandoval58efbc92017-08-22 23:45:59 -07008144 blk_status_t status;
Miao Xie8b110e32014-09-12 18:44:03 +08008145
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308146 fs_info = BTRFS_I(inode)->root->fs_info;
Jeff Mahoneyda170662016-06-15 09:22:56 -04008147 sectorsize = fs_info->sectorsize;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308148
Omar Sandoval58efbc92017-08-22 23:45:59 -07008149 err = BLK_STS_OK;
Miao Xie8b110e32014-09-12 18:44:03 +08008150 start = io_bio->logical;
8151 done.inode = inode;
Liu Bo17347ce2017-05-15 15:33:27 -07008152 io_bio->bio.bi_iter = io_bio->iter;
Miao Xie8b110e32014-09-12 18:44:03 +08008153
Liu Bo17347ce2017-05-15 15:33:27 -07008154 bio_for_each_segment(bvec, &io_bio->bio, iter) {
8155 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec.bv_len);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308156
Liu Bo17347ce2017-05-15 15:33:27 -07008157 pgoff = bvec.bv_offset;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308158next_block:
Liu Boef7cdac12017-04-13 18:11:48 -07008159 if (uptodate) {
8160 csum_pos = BTRFS_BYTES_TO_BLKS(fs_info, offset);
8161 ret = __readpage_endio_check(inode, io_bio, csum_pos,
8162 bvec.bv_page, pgoff, start, sectorsize);
8163 if (likely(!ret))
8164 goto next;
8165 }
Miao Xie8b110e32014-09-12 18:44:03 +08008166try_again:
8167 done.uptodate = 0;
8168 done.start = start;
8169 init_completion(&done.done);
8170
Omar Sandoval58efbc92017-08-22 23:45:59 -07008171 status = dio_read_error(inode, &io_bio->bio, bvec.bv_page,
8172 pgoff, start, start + sectorsize - 1,
8173 io_bio->mirror_num, btrfs_retry_endio,
8174 &done);
8175 if (status) {
8176 err = status;
Miao Xie8b110e32014-09-12 18:44:03 +08008177 goto next;
8178 }
8179
David Sterba9c17f6c2017-07-19 19:26:45 +02008180 wait_for_completion_io(&done.done);
Miao Xie8b110e32014-09-12 18:44:03 +08008181
8182 if (!done.uptodate) {
8183 /* We might have another mirror, so try again */
8184 goto try_again;
8185 }
8186next:
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308187 offset += sectorsize;
8188 start += sectorsize;
8189
8190 ASSERT(nr_sectors);
8191
Liu Bo97bf5a52017-04-07 13:11:10 -07008192 nr_sectors--;
8193 if (nr_sectors) {
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308194 pgoff += sectorsize;
Liu Bo97bf5a52017-04-07 13:11:10 -07008195 ASSERT(pgoff < PAGE_SIZE);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308196 goto next_block;
8197 }
Kent Overstreet2c30c712013-11-07 12:20:26 -08008198 }
Miao Xiec1dc0892014-09-12 18:43:56 +08008199
8200 return err;
8201}
8202
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008203static blk_status_t btrfs_subio_endio_read(struct inode *inode,
8204 struct btrfs_io_bio *io_bio, blk_status_t err)
Miao Xie8b110e32014-09-12 18:44:03 +08008205{
8206 bool skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
8207
8208 if (skip_csum) {
8209 if (unlikely(err))
8210 return __btrfs_correct_data_nocsum(inode, io_bio);
8211 else
Omar Sandoval58efbc92017-08-22 23:45:59 -07008212 return BLK_STS_OK;
Miao Xie8b110e32014-09-12 18:44:03 +08008213 } else {
8214 return __btrfs_subio_endio_read(inode, io_bio, err);
8215 }
8216}
8217
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008218static void btrfs_endio_direct_read(struct bio *bio)
Miao Xiec1dc0892014-09-12 18:43:56 +08008219{
8220 struct btrfs_dio_private *dip = bio->bi_private;
8221 struct inode *inode = dip->inode;
8222 struct bio *dio_bio;
8223 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008224 blk_status_t err = bio->bi_status;
Miao Xiec1dc0892014-09-12 18:43:56 +08008225
Liu Bo99c4e3b92017-09-15 15:06:51 -06008226 if (dip->flags & BTRFS_DIO_ORIG_BIO_SUBMITTED)
Miao Xie8b110e32014-09-12 18:44:03 +08008227 err = btrfs_subio_endio_read(inode, io_bio, err);
Miao Xiec1dc0892014-09-12 18:43:56 +08008228
Josef Bacik4b46fce2010-05-23 11:00:55 -04008229 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01008230 dip->logical_offset + dip->bytes - 1);
Chris Mason9be33952013-05-17 18:30:14 -04008231 dio_bio = dip->dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008232
Josef Bacik4b46fce2010-05-23 11:00:55 -04008233 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04008234
Liu Bo99c4e3b92017-09-15 15:06:51 -06008235 dio_bio->bi_status = err;
Christoph Hellwig40553512017-06-03 09:37:58 +02008236 dio_end_io(dio_bio);
David Sterbab3a0dd52018-11-22 17:16:49 +01008237 btrfs_io_bio_free_csum(io_bio);
Chris Mason9be33952013-05-17 18:30:14 -04008238 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008239}
8240
Qu Wenruo524272602017-03-08 10:25:52 +08008241static void __endio_write_update_ordered(struct inode *inode,
8242 const u64 offset, const u64 bytes,
8243 const bool uptodate)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008244{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008245 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008246 struct btrfs_ordered_extent *ordered = NULL;
Qu Wenruo524272602017-03-08 10:25:52 +08008247 struct btrfs_workqueue *wq;
Filipe Manana14543772015-11-24 16:23:54 +00008248 u64 ordered_offset = offset;
8249 u64 ordered_bytes = bytes;
Naohiro Aota67c003f2017-09-01 17:59:07 +09008250 u64 last_offset;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008251
Omar Sandovala0cac0e2019-09-16 11:30:57 -07008252 if (btrfs_is_free_space_inode(BTRFS_I(inode)))
Qu Wenruo524272602017-03-08 10:25:52 +08008253 wq = fs_info->endio_freespace_worker;
Omar Sandovala0cac0e2019-09-16 11:30:57 -07008254 else
Qu Wenruo524272602017-03-08 10:25:52 +08008255 wq = fs_info->endio_write_workers;
Qu Wenruo524272602017-03-08 10:25:52 +08008256
Nikolay Borisovb25f0d02018-04-11 11:21:17 +03008257 while (ordered_offset < offset + bytes) {
8258 last_offset = ordered_offset;
8259 if (btrfs_dec_test_first_ordered_pending(inode, &ordered,
8260 &ordered_offset,
8261 ordered_bytes,
8262 uptodate)) {
Omar Sandovala0cac0e2019-09-16 11:30:57 -07008263 btrfs_init_work(&ordered->work, finish_ordered_fn, NULL,
8264 NULL);
Nikolay Borisovb25f0d02018-04-11 11:21:17 +03008265 btrfs_queue_work(wq, &ordered->work);
8266 }
8267 /*
8268 * If btrfs_dec_test_ordered_pending does not find any ordered
8269 * extent in the range, we can exit.
8270 */
8271 if (ordered_offset == last_offset)
8272 return;
8273 /*
8274 * Our bio might span multiple ordered extents. In this case
Andrea Gelmini52042d82018-11-28 12:05:13 +01008275 * we keep going until we have accounted the whole dio.
Nikolay Borisovb25f0d02018-04-11 11:21:17 +03008276 */
8277 if (ordered_offset < offset + bytes) {
8278 ordered_bytes = offset + bytes - ordered_offset;
8279 ordered = NULL;
8280 }
Chris Mason163cf092010-11-28 19:56:33 -05008281 }
Filipe Manana14543772015-11-24 16:23:54 +00008282}
8283
8284static void btrfs_endio_direct_write(struct bio *bio)
8285{
8286 struct btrfs_dio_private *dip = bio->bi_private;
8287 struct bio *dio_bio = dip->dio_bio;
8288
Qu Wenruo524272602017-03-08 10:25:52 +08008289 __endio_write_update_ordered(dip->inode, dip->logical_offset,
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008290 dip->bytes, !bio->bi_status);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008291
Josef Bacik4b46fce2010-05-23 11:00:55 -04008292 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04008293
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008294 dio_bio->bi_status = bio->bi_status;
Christoph Hellwig40553512017-06-03 09:37:58 +02008295 dio_end_io(dio_bio);
Chris Mason9be33952013-05-17 18:30:14 -04008296 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008297}
8298
David Sterbad0ee3932018-03-08 14:35:48 +01008299static blk_status_t btrfs_submit_bio_start_direct_io(void *private_data,
David Sterbad0779292018-03-08 13:47:33 +01008300 struct bio *bio, u64 offset)
Chris Masoneaf25d92010-05-25 09:48:28 -04008301{
Josef Bacikc6100a42017-05-05 11:57:13 -04008302 struct inode *inode = private_data;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008303 blk_status_t ret;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008304 ret = btrfs_csum_one_bio(inode, bio, offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008305 BUG_ON(ret); /* -ENOMEM */
Chris Masoneaf25d92010-05-25 09:48:28 -04008306 return 0;
8307}
8308
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008309static void btrfs_end_dio_bio(struct bio *bio)
Miao Xiee65e1532010-11-22 03:04:43 +00008310{
8311 struct btrfs_dio_private *dip = bio->bi_private;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008312 blk_status_t err = bio->bi_status;
Miao Xiee65e1532010-11-22 03:04:43 +00008313
Miao Xie8b110e32014-09-12 18:44:03 +08008314 if (err)
8315 btrfs_warn(BTRFS_I(dip->inode)->root->fs_info,
Mike Christie6296b962016-06-05 14:32:21 -05008316 "direct IO failed ino %llu rw %d,%u sector %#Lx len %u err no %d",
David Sterbaf85b7372017-01-20 14:54:07 +01008317 btrfs_ino(BTRFS_I(dip->inode)), bio_op(bio),
8318 bio->bi_opf,
Miao Xie8b110e32014-09-12 18:44:03 +08008319 (unsigned long long)bio->bi_iter.bi_sector,
8320 bio->bi_iter.bi_size, err);
8321
8322 if (dip->subio_endio)
8323 err = dip->subio_endio(dip->inode, btrfs_io_bio(bio), err);
Miao Xiec1dc0892014-09-12 18:43:56 +08008324
8325 if (err) {
Miao Xiee65e1532010-11-22 03:04:43 +00008326 /*
Nikolay Borisovde224b72018-02-14 10:53:36 +02008327 * We want to perceive the errors flag being set before
8328 * decrementing the reference count. We don't need a barrier
8329 * since atomic operations with a return value are fully
8330 * ordered as per atomic_t.txt
Miao Xiee65e1532010-11-22 03:04:43 +00008331 */
Nikolay Borisovde224b72018-02-14 10:53:36 +02008332 dip->errors = 1;
Miao Xiee65e1532010-11-22 03:04:43 +00008333 }
8334
8335 /* if there are more bios still pending for this dio, just exit */
8336 if (!atomic_dec_and_test(&dip->pending_bios))
8337 goto out;
8338
Chris Mason9be33952013-05-17 18:30:14 -04008339 if (dip->errors) {
Miao Xiee65e1532010-11-22 03:04:43 +00008340 bio_io_error(dip->orig_bio);
Chris Mason9be33952013-05-17 18:30:14 -04008341 } else {
Anand Jain2dbe0c72017-10-14 08:35:56 +08008342 dip->dio_bio->bi_status = BLK_STS_OK;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008343 bio_endio(dip->orig_bio);
Miao Xiee65e1532010-11-22 03:04:43 +00008344 }
8345out:
8346 bio_put(bio);
8347}
8348
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008349static inline blk_status_t btrfs_lookup_and_bind_dio_csum(struct inode *inode,
Miao Xiec1dc0892014-09-12 18:43:56 +08008350 struct btrfs_dio_private *dip,
8351 struct bio *bio,
8352 u64 file_offset)
8353{
8354 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
8355 struct btrfs_io_bio *orig_io_bio = btrfs_io_bio(dip->orig_bio);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008356 blk_status_t ret;
Miao Xiec1dc0892014-09-12 18:43:56 +08008357
8358 /*
8359 * We load all the csum data we need when we submit
8360 * the first bio to reduce the csum tree search and
8361 * contention.
8362 */
8363 if (dip->logical_offset == file_offset) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008364 ret = btrfs_lookup_bio_sums_dio(inode, dip->orig_bio,
Miao Xiec1dc0892014-09-12 18:43:56 +08008365 file_offset);
8366 if (ret)
8367 return ret;
8368 }
8369
8370 if (bio == dip->orig_bio)
8371 return 0;
8372
8373 file_offset -= dip->logical_offset;
8374 file_offset >>= inode->i_sb->s_blocksize_bits;
8375 io_bio->csum = (u8 *)(((u32 *)orig_io_bio->csum) + file_offset);
8376
8377 return 0;
8378}
8379
David Sterbad0ee3932018-03-08 14:35:48 +01008380static inline blk_status_t btrfs_submit_dio_bio(struct bio *bio,
8381 struct inode *inode, u64 file_offset, int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00008382{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008383 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xiefacc8a222013-07-25 19:22:34 +08008384 struct btrfs_dio_private *dip = bio->bi_private;
Mike Christie37226b22016-06-05 14:31:52 -05008385 bool write = bio_op(bio) == REQ_OP_WRITE;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008386 blk_status_t ret;
Miao Xiee65e1532010-11-22 03:04:43 +00008387
Liu Bo4c274bc2017-11-01 17:19:27 -06008388 /* Check btrfs_submit_bio_hook() for rules about async submit. */
Josef Bacikb812ce22012-11-16 13:56:32 -05008389 if (async_submit)
8390 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
8391
Josef Bacik5fd02042012-05-02 14:00:54 -04008392 if (!write) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008393 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DATA);
Josef Bacik5fd02042012-05-02 14:00:54 -04008394 if (ret)
8395 goto err;
8396 }
Miao Xiee65e1532010-11-22 03:04:43 +00008397
Nikolay Borisove6961ca2017-08-03 15:44:58 +03008398 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Josef Bacik1ae39932011-04-06 14:41:34 -04008399 goto map;
8400
8401 if (write && async_submit) {
Josef Bacikc6100a42017-05-05 11:57:13 -04008402 ret = btrfs_wq_submit_bio(fs_info, bio, 0, 0,
8403 file_offset, inode,
David Sterbae288c082018-07-18 17:36:24 +02008404 btrfs_submit_bio_start_direct_io);
Miao Xiee65e1532010-11-22 03:04:43 +00008405 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04008406 } else if (write) {
8407 /*
8408 * If we aren't doing async submit, calculate the csum of the
8409 * bio now.
8410 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008411 ret = btrfs_csum_one_bio(inode, bio, file_offset, 1);
Josef Bacik1ae39932011-04-06 14:41:34 -04008412 if (ret)
8413 goto err;
Miao Xie23ea8e52014-09-12 18:43:54 +08008414 } else {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008415 ret = btrfs_lookup_and_bind_dio_csum(inode, dip, bio,
Miao Xiec1dc0892014-09-12 18:43:56 +08008416 file_offset);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00008417 if (ret)
8418 goto err;
8419 }
Josef Bacik1ae39932011-04-06 14:41:34 -04008420map:
Chris Mason08635ba2019-07-10 12:28:14 -07008421 ret = btrfs_map_bio(fs_info, bio, 0);
Miao Xiee65e1532010-11-22 03:04:43 +00008422err:
Miao Xiee65e1532010-11-22 03:04:43 +00008423 return ret;
8424}
8425
Nikolay Borisove6961ca2017-08-03 15:44:58 +03008426static int btrfs_submit_direct_hook(struct btrfs_dio_private *dip)
Miao Xiee65e1532010-11-22 03:04:43 +00008427{
8428 struct inode *inode = dip->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008429 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xiee65e1532010-11-22 03:04:43 +00008430 struct bio *bio;
8431 struct bio *orig_bio = dip->orig_bio;
Kent Overstreet4f024f32013-10-11 15:44:27 -07008432 u64 start_sector = orig_bio->bi_iter.bi_sector;
Miao Xiee65e1532010-11-22 03:04:43 +00008433 u64 file_offset = dip->logical_offset;
Josef Bacik1ae39932011-04-06 14:41:34 -04008434 int async_submit = 0;
Liu Bo725130b2017-05-16 09:51:39 -07008435 u64 submit_len;
8436 int clone_offset = 0;
8437 int clone_len;
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05308438 int ret;
Omar Sandoval58efbc92017-08-22 23:45:59 -07008439 blk_status_t status;
Nikolay Borisov89b798a2019-06-03 12:05:05 +03008440 struct btrfs_io_geometry geom;
Miao Xiee65e1532010-11-22 03:04:43 +00008441
Nikolay Borisov89b798a2019-06-03 12:05:05 +03008442 submit_len = orig_bio->bi_iter.bi_size;
8443 ret = btrfs_get_io_geometry(fs_info, btrfs_op(orig_bio),
8444 start_sector << 9, submit_len, &geom);
Miao Xie7a5c3c92014-06-17 18:58:59 +08008445 if (ret)
Miao Xiee65e1532010-11-22 03:04:43 +00008446 return -EIO;
Miao Xiefacc8a222013-07-25 19:22:34 +08008447
Nikolay Borisov89b798a2019-06-03 12:05:05 +03008448 if (geom.len >= submit_len) {
Josef Bacik02f57c72011-04-06 14:25:44 -04008449 bio = orig_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08008450 dip->flags |= BTRFS_DIO_ORIG_BIO_SUBMITTED;
Josef Bacik02f57c72011-04-06 14:25:44 -04008451 goto submit;
8452 }
8453
David Woodhouse53b381b2013-01-29 18:40:14 -05008454 /* async crcs make it difficult to collect full stripe writes. */
Jeff Mahoney1b868262017-05-17 11:38:35 -04008455 if (btrfs_data_alloc_profile(fs_info) & BTRFS_BLOCK_GROUP_RAID56_MASK)
David Woodhouse53b381b2013-01-29 18:40:14 -05008456 async_submit = 0;
8457 else
8458 async_submit = 1;
8459
Liu Bo725130b2017-05-16 09:51:39 -07008460 /* bio split */
Nikolay Borisov89b798a2019-06-03 12:05:05 +03008461 ASSERT(geom.len <= INT_MAX);
Josef Bacik02f57c72011-04-06 14:25:44 -04008462 atomic_inc(&dip->pending_bios);
Arnd Bergmann3c91ee62017-05-18 15:33:29 +02008463 do {
Nikolay Borisov89b798a2019-06-03 12:05:05 +03008464 clone_len = min_t(int, submit_len, geom.len);
Josef Bacik02f57c72011-04-06 14:25:44 -04008465
Liu Bo725130b2017-05-16 09:51:39 -07008466 /*
8467 * This will never fail as it's passing GPF_NOFS and
8468 * the allocation is backed by btrfs_bioset.
8469 */
Liu Boe4770942017-05-16 10:57:14 -07008470 bio = btrfs_bio_clone_partial(orig_bio, clone_offset,
Liu Bo725130b2017-05-16 09:51:39 -07008471 clone_len);
8472 bio->bi_private = dip;
8473 bio->bi_end_io = btrfs_end_dio_bio;
8474 btrfs_io_bio(bio)->logical = file_offset;
Miao Xiee65e1532010-11-22 03:04:43 +00008475
Liu Bo725130b2017-05-16 09:51:39 -07008476 ASSERT(submit_len >= clone_len);
8477 submit_len -= clone_len;
8478 if (submit_len == 0)
8479 break;
Miao Xiee65e1532010-11-22 03:04:43 +00008480
Liu Bo725130b2017-05-16 09:51:39 -07008481 /*
8482 * Increase the count before we submit the bio so we know
8483 * the end IO handler won't happen before we increase the
8484 * count. Otherwise, the dip might get freed before we're
8485 * done setting it up.
8486 */
8487 atomic_inc(&dip->pending_bios);
Miao Xiee65e1532010-11-22 03:04:43 +00008488
David Sterbad0ee3932018-03-08 14:35:48 +01008489 status = btrfs_submit_dio_bio(bio, inode, file_offset,
Omar Sandoval58efbc92017-08-22 23:45:59 -07008490 async_submit);
8491 if (status) {
Liu Bo725130b2017-05-16 09:51:39 -07008492 bio_put(bio);
8493 atomic_dec(&dip->pending_bios);
8494 goto out_err;
Miao Xiee65e1532010-11-22 03:04:43 +00008495 }
Liu Bo725130b2017-05-16 09:51:39 -07008496
8497 clone_offset += clone_len;
8498 start_sector += clone_len >> 9;
8499 file_offset += clone_len;
8500
Nikolay Borisov89b798a2019-06-03 12:05:05 +03008501 ret = btrfs_get_io_geometry(fs_info, btrfs_op(orig_bio),
8502 start_sector << 9, submit_len, &geom);
Liu Bo725130b2017-05-16 09:51:39 -07008503 if (ret)
8504 goto out_err;
Arnd Bergmann3c91ee62017-05-18 15:33:29 +02008505 } while (submit_len > 0);
Miao Xiee65e1532010-11-22 03:04:43 +00008506
Josef Bacik02f57c72011-04-06 14:25:44 -04008507submit:
David Sterbad0ee3932018-03-08 14:35:48 +01008508 status = btrfs_submit_dio_bio(bio, inode, file_offset, async_submit);
Omar Sandoval58efbc92017-08-22 23:45:59 -07008509 if (!status)
Miao Xiee65e1532010-11-22 03:04:43 +00008510 return 0;
8511
8512 bio_put(bio);
8513out_err:
8514 dip->errors = 1;
8515 /*
Nikolay Borisovde224b72018-02-14 10:53:36 +02008516 * Before atomic variable goto zero, we must make sure dip->errors is
8517 * perceived to be set. This ordering is ensured by the fact that an
8518 * atomic operations with a return value are fully ordered as per
8519 * atomic_t.txt
Miao Xiee65e1532010-11-22 03:04:43 +00008520 */
Miao Xiee65e1532010-11-22 03:04:43 +00008521 if (atomic_dec_and_test(&dip->pending_bios))
8522 bio_io_error(dip->orig_bio);
8523
8524 /* bio_end_io() will handle error, so we needn't return it */
8525 return 0;
8526}
8527
Mike Christie8a4c1e42016-06-05 14:31:50 -05008528static void btrfs_submit_direct(struct bio *dio_bio, struct inode *inode,
8529 loff_t file_offset)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008530{
Filipe Manana61de7182015-07-01 12:13:10 +01008531 struct btrfs_dio_private *dip = NULL;
Liu Bo3892ac92017-04-17 15:00:28 -07008532 struct bio *bio = NULL;
8533 struct btrfs_io_bio *io_bio;
Mike Christie8a4c1e42016-06-05 14:31:50 -05008534 bool write = (bio_op(dio_bio) == REQ_OP_WRITE);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008535 int ret = 0;
8536
David Sterba8b6c1d52017-06-02 17:48:13 +02008537 bio = btrfs_bio_clone(dio_bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008538
Miao Xiec1dc0892014-09-12 18:43:56 +08008539 dip = kzalloc(sizeof(*dip), GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04008540 if (!dip) {
8541 ret = -ENOMEM;
Filipe Manana61de7182015-07-01 12:13:10 +01008542 goto free_ordered;
Chris Mason9be33952013-05-17 18:30:14 -04008543 }
8544
8545 dip->private = dio_bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008546 dip->inode = inode;
8547 dip->logical_offset = file_offset;
Kent Overstreet4f024f32013-10-11 15:44:27 -07008548 dip->bytes = dio_bio->bi_iter.bi_size;
8549 dip->disk_bytenr = (u64)dio_bio->bi_iter.bi_sector << 9;
Liu Bo3892ac92017-04-17 15:00:28 -07008550 bio->bi_private = dip;
8551 dip->orig_bio = bio;
Chris Mason9be33952013-05-17 18:30:14 -04008552 dip->dio_bio = dio_bio;
Miao Xiee65e1532010-11-22 03:04:43 +00008553 atomic_set(&dip->pending_bios, 0);
Liu Bo3892ac92017-04-17 15:00:28 -07008554 io_bio = btrfs_io_bio(bio);
8555 io_bio->logical = file_offset;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008556
Miao Xiec1dc0892014-09-12 18:43:56 +08008557 if (write) {
Liu Bo3892ac92017-04-17 15:00:28 -07008558 bio->bi_end_io = btrfs_endio_direct_write;
Miao Xiec1dc0892014-09-12 18:43:56 +08008559 } else {
Liu Bo3892ac92017-04-17 15:00:28 -07008560 bio->bi_end_io = btrfs_endio_direct_read;
Miao Xiec1dc0892014-09-12 18:43:56 +08008561 dip->subio_endio = btrfs_subio_endio_read;
8562 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04008563
Filipe Mananaf28a4922015-12-08 19:23:20 +00008564 /*
8565 * Reset the range for unsubmitted ordered extents (to a 0 length range)
8566 * even if we fail to submit a bio, because in such case we do the
8567 * corresponding error handling below and it must not be done a second
8568 * time by btrfs_direct_IO().
8569 */
8570 if (write) {
8571 struct btrfs_dio_data *dio_data = current->journal_info;
8572
8573 dio_data->unsubmitted_oe_range_end = dip->logical_offset +
8574 dip->bytes;
8575 dio_data->unsubmitted_oe_range_start =
8576 dio_data->unsubmitted_oe_range_end;
8577 }
8578
Nikolay Borisove6961ca2017-08-03 15:44:58 +03008579 ret = btrfs_submit_direct_hook(dip);
Miao Xiee65e1532010-11-22 03:04:43 +00008580 if (!ret)
Chris Masoneaf25d92010-05-25 09:48:28 -04008581 return;
Chris Mason9be33952013-05-17 18:30:14 -04008582
David Sterbab3a0dd52018-11-22 17:16:49 +01008583 btrfs_io_bio_free_csum(io_bio);
Chris Mason9be33952013-05-17 18:30:14 -04008584
Josef Bacik4b46fce2010-05-23 11:00:55 -04008585free_ordered:
8586 /*
Filipe Manana61de7182015-07-01 12:13:10 +01008587 * If we arrived here it means either we failed to submit the dip
8588 * or we either failed to clone the dio_bio or failed to allocate the
8589 * dip. If we cloned the dio_bio and allocated the dip, we can just
8590 * call bio_endio against our io_bio so that we get proper resource
8591 * cleanup if we fail to submit the dip, otherwise, we must do the
8592 * same as btrfs_endio_direct_[write|read] because we can't call these
8593 * callbacks - they require an allocated dip and a clone of dio_bio.
Josef Bacik4b46fce2010-05-23 11:00:55 -04008594 */
Liu Bo3892ac92017-04-17 15:00:28 -07008595 if (bio && dip) {
Guoqing Jiang054ec2f2017-06-02 16:08:50 +08008596 bio_io_error(bio);
Filipe Manana61de7182015-07-01 12:13:10 +01008597 /*
Liu Bo3892ac92017-04-17 15:00:28 -07008598 * The end io callbacks free our dip, do the final put on bio
Filipe Manana61de7182015-07-01 12:13:10 +01008599 * and all the cleanup and final put for dio_bio (through
8600 * dio_end_io()).
8601 */
8602 dip = NULL;
Liu Bo3892ac92017-04-17 15:00:28 -07008603 bio = NULL;
Filipe Manana61de7182015-07-01 12:13:10 +01008604 } else {
Filipe Manana14543772015-11-24 16:23:54 +00008605 if (write)
Qu Wenruo524272602017-03-08 10:25:52 +08008606 __endio_write_update_ordered(inode,
Filipe Manana14543772015-11-24 16:23:54 +00008607 file_offset,
8608 dio_bio->bi_iter.bi_size,
Qu Wenruo524272602017-03-08 10:25:52 +08008609 false);
Filipe Manana14543772015-11-24 16:23:54 +00008610 else
Filipe Manana61de7182015-07-01 12:13:10 +01008611 unlock_extent(&BTRFS_I(inode)->io_tree, file_offset,
8612 file_offset + dio_bio->bi_iter.bi_size - 1);
Filipe Manana14543772015-11-24 16:23:54 +00008613
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008614 dio_bio->bi_status = BLK_STS_IOERR;
Filipe Manana61de7182015-07-01 12:13:10 +01008615 /*
8616 * Releases and cleans up our dio_bio, no need to bio_put()
8617 * nor bio_endio()/bio_io_error() against dio_bio.
8618 */
Christoph Hellwig40553512017-06-03 09:37:58 +02008619 dio_end_io(dio_bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008620 }
Liu Bo3892ac92017-04-17 15:00:28 -07008621 if (bio)
8622 bio_put(bio);
Filipe Manana61de7182015-07-01 12:13:10 +01008623 kfree(dip);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008624}
8625
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008626static ssize_t check_direct_IO(struct btrfs_fs_info *fs_info,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008627 const struct iov_iter *iter, loff_t offset)
Chris Mason5a5f79b2010-05-26 21:33:37 -04008628{
8629 int seg;
Josef Bacika1b75f72011-04-08 15:51:18 +00008630 int i;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008631 unsigned int blocksize_mask = fs_info->sectorsize - 1;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008632 ssize_t retval = -EINVAL;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008633
8634 if (offset & blocksize_mask)
8635 goto out;
8636
Al Viro28060d52014-03-22 05:15:17 -04008637 if (iov_iter_alignment(iter) & blocksize_mask)
8638 goto out;
Josef Bacika1b75f72011-04-08 15:51:18 +00008639
Al Viro28060d52014-03-22 05:15:17 -04008640 /* If this is a write we don't need to check anymore */
Al Virocd27e452016-10-10 13:39:05 -04008641 if (iov_iter_rw(iter) != READ || !iter_is_iovec(iter))
Al Viro28060d52014-03-22 05:15:17 -04008642 return 0;
8643 /*
8644 * Check to make sure we don't have duplicate iov_base's in this
8645 * iovec, if so return EINVAL, otherwise we'll get csum errors
8646 * when reading back.
8647 */
8648 for (seg = 0; seg < iter->nr_segs; seg++) {
8649 for (i = seg + 1; i < iter->nr_segs; i++) {
8650 if (iter->iov[seg].iov_base == iter->iov[i].iov_base)
Josef Bacika1b75f72011-04-08 15:51:18 +00008651 goto out;
8652 }
Chris Mason5a5f79b2010-05-26 21:33:37 -04008653 }
8654 retval = 0;
8655out:
8656 return retval;
8657}
Josef Bacikeb838e72012-07-31 16:28:48 -04008658
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008659static ssize_t btrfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
Chris Mason16432982008-04-10 10:23:21 -04008660{
Josef Bacik4b46fce2010-05-23 11:00:55 -04008661 struct file *file = iocb->ki_filp;
8662 struct inode *inode = file->f_mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008663 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
chandan50745b02015-08-28 21:10:13 +05308664 struct btrfs_dio_data dio_data = { 0 };
Qu Wenruo364ecf32017-02-27 15:10:38 +08008665 struct extent_changeset *data_reserved = NULL;
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008666 loff_t offset = iocb->ki_pos;
Miao Xie09348562013-02-07 10:12:07 +00008667 size_t count = 0;
Miao Xie2e60a512013-02-08 07:01:08 +00008668 int flags = 0;
Miao Xie38851cc2013-02-08 07:04:11 +00008669 bool wakeup = true;
8670 bool relock = false;
Miao Xie09348562013-02-07 10:12:07 +00008671 ssize_t ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008672
Nikolay Borisov8c70c9f2017-08-21 12:43:46 +03008673 if (check_direct_IO(fs_info, iter, offset))
Chris Mason5a5f79b2010-05-26 21:33:37 -04008674 return 0;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008675
Jens Axboefe0f07d2015-04-15 17:05:48 -06008676 inode_dio_begin(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008677
Josef Bacik0e267c42013-07-02 10:38:02 -04008678 /*
Miao Xie41bd9ca2014-03-06 13:54:57 +08008679 * The generic stuff only does filemap_write_and_wait_range, which
8680 * isn't enough if we've written compressed pages to this area, so
8681 * we need to flush the dirty pages again to make absolutely sure
8682 * that any outstanding dirty pages are on disk.
Josef Bacik0e267c42013-07-02 10:38:02 -04008683 */
Al Viroa6cbcd42014-03-04 22:38:00 -05008684 count = iov_iter_count(iter);
Miao Xie41bd9ca2014-03-06 13:54:57 +08008685 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
8686 &BTRFS_I(inode)->runtime_flags))
Wang Shilong9a025a02014-07-17 11:44:13 +08008687 filemap_fdatawrite_range(inode->i_mapping, offset,
8688 offset + count - 1);
Josef Bacik0e267c42013-07-02 10:38:02 -04008689
Omar Sandoval6f673762015-03-16 04:33:52 -07008690 if (iov_iter_rw(iter) == WRITE) {
Miao Xie38851cc2013-02-08 07:04:11 +00008691 /*
8692 * If the write DIO is beyond the EOF, we need update
8693 * the isize, but it is protected by i_mutex. So we can
8694 * not unlock the i_mutex at this case.
8695 */
8696 if (offset + count <= inode->i_size) {
Liu Bo4aaedfb2016-12-14 22:36:05 -08008697 dio_data.overwrite = 1;
Al Viro59551022016-01-22 15:40:57 -05008698 inode_unlock(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008699 relock = true;
Goldwyn Rodriguesedf064e2017-06-20 07:05:49 -05008700 } else if (iocb->ki_flags & IOCB_NOWAIT) {
8701 ret = -EAGAIN;
8702 goto out;
Miao Xie38851cc2013-02-08 07:04:11 +00008703 }
Qu Wenruo364ecf32017-02-27 15:10:38 +08008704 ret = btrfs_delalloc_reserve_space(inode, &data_reserved,
8705 offset, count);
Miao Xie09348562013-02-07 10:12:07 +00008706 if (ret)
Miao Xie38851cc2013-02-08 07:04:11 +00008707 goto out;
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008708
8709 /*
8710 * We need to know how many extents we reserved so that we can
8711 * do the accounting properly if we go over the number we
8712 * originally calculated. Abuse current->journal_info for this.
8713 */
Jeff Mahoneyda170662016-06-15 09:22:56 -04008714 dio_data.reserve = round_up(count,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008715 fs_info->sectorsize);
Filipe Mananaf28a4922015-12-08 19:23:20 +00008716 dio_data.unsubmitted_oe_range_start = (u64)offset;
8717 dio_data.unsubmitted_oe_range_end = (u64)offset;
chandan50745b02015-08-28 21:10:13 +05308718 current->journal_info = &dio_data;
Chandan Rajendra97dcdea2016-12-23 15:00:18 +05308719 down_read(&BTRFS_I(inode)->dio_sem);
David Sterbaee39b432014-09-30 01:33:33 +02008720 } else if (test_bit(BTRFS_INODE_READDIO_NEED_LOCK,
8721 &BTRFS_I(inode)->runtime_flags)) {
Jens Axboefe0f07d2015-04-15 17:05:48 -06008722 inode_dio_end(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008723 flags = DIO_LOCKING | DIO_SKIP_HOLES;
8724 wakeup = false;
Miao Xie09348562013-02-07 10:12:07 +00008725 }
8726
Omar Sandoval17f8c842015-03-16 04:33:50 -07008727 ret = __blockdev_direct_IO(iocb, inode,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008728 fs_info->fs_devices->latest_bdev,
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008729 iter, btrfs_get_blocks_direct, NULL,
Omar Sandoval17f8c842015-03-16 04:33:50 -07008730 btrfs_submit_direct, flags);
Omar Sandoval6f673762015-03-16 04:33:52 -07008731 if (iov_iter_rw(iter) == WRITE) {
Chandan Rajendra97dcdea2016-12-23 15:00:18 +05308732 up_read(&BTRFS_I(inode)->dio_sem);
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008733 current->journal_info = NULL;
Liu Boddba1bf2015-06-17 16:59:58 +08008734 if (ret < 0 && ret != -EIOCBQUEUED) {
chandan50745b02015-08-28 21:10:13 +05308735 if (dio_data.reserve)
Qu Wenruobc42bda2017-02-27 15:10:39 +08008736 btrfs_delalloc_release_space(inode, data_reserved,
Qu Wenruo43b18592017-12-12 15:34:32 +08008737 offset, dio_data.reserve, true);
Filipe Mananaf28a4922015-12-08 19:23:20 +00008738 /*
8739 * On error we might have left some ordered extents
8740 * without submitting corresponding bios for them, so
8741 * cleanup them up to avoid other tasks getting them
8742 * and waiting for them to complete forever.
8743 */
8744 if (dio_data.unsubmitted_oe_range_start <
8745 dio_data.unsubmitted_oe_range_end)
Qu Wenruo524272602017-03-08 10:25:52 +08008746 __endio_write_update_ordered(inode,
Filipe Mananaf28a4922015-12-08 19:23:20 +00008747 dio_data.unsubmitted_oe_range_start,
8748 dio_data.unsubmitted_oe_range_end -
8749 dio_data.unsubmitted_oe_range_start,
Qu Wenruo524272602017-03-08 10:25:52 +08008750 false);
Liu Boddba1bf2015-06-17 16:59:58 +08008751 } else if (ret >= 0 && (size_t)ret < count)
Qu Wenruobc42bda2017-02-27 15:10:39 +08008752 btrfs_delalloc_release_space(inode, data_reserved,
Qu Wenruo43b18592017-12-12 15:34:32 +08008753 offset, count - (size_t)ret, true);
Qu Wenruo8702ba92019-10-14 14:34:51 +08008754 btrfs_delalloc_release_extents(BTRFS_I(inode), count);
Miao Xie09348562013-02-07 10:12:07 +00008755 }
Miao Xie38851cc2013-02-08 07:04:11 +00008756out:
Miao Xie2e60a512013-02-08 07:01:08 +00008757 if (wakeup)
Jens Axboefe0f07d2015-04-15 17:05:48 -06008758 inode_dio_end(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008759 if (relock)
Al Viro59551022016-01-22 15:40:57 -05008760 inode_lock(inode);
Miao Xie09348562013-02-07 10:12:07 +00008761
Qu Wenruo364ecf32017-02-27 15:10:38 +08008762 extent_changeset_free(data_reserved);
Miao Xie09348562013-02-07 10:12:07 +00008763 return ret;
Chris Mason16432982008-04-10 10:23:21 -04008764}
8765
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008766#define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
8767
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008768static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
8769 __u64 start, __u64 len)
8770{
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008771 int ret;
8772
8773 ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS);
8774 if (ret)
8775 return ret;
8776
David Sterba2135fb92017-06-23 04:09:57 +02008777 return extent_fiemap(inode, fieinfo, start, len);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008778}
8779
Chris Mason9ebefb182007-06-15 13:50:00 -04008780int btrfs_readpage(struct file *file, struct page *page)
8781{
Chris Masond1310b22008-01-24 16:13:08 -05008782 struct extent_io_tree *tree;
8783 tree = &BTRFS_I(page->mapping->host)->io_tree;
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02008784 return extent_read_full_page(tree, page, btrfs_get_extent, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04008785}
Chris Mason1832a6d2007-12-21 16:27:21 -05008786
Chris Mason39279cc2007-06-12 06:35:45 -04008787static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
8788{
Josef Bacikbe7bd732015-10-22 15:05:09 -04008789 struct inode *inode = page->mapping->host;
8790 int ret;
Chris Masonb888db22007-08-27 16:49:44 -04008791
8792 if (current->flags & PF_MEMALLOC) {
8793 redirty_page_for_writepage(wbc, page);
8794 unlock_page(page);
8795 return 0;
8796 }
Josef Bacikbe7bd732015-10-22 15:05:09 -04008797
8798 /*
8799 * If we are under memory pressure we will call this directly from the
8800 * VM, we need to make sure we have the inode referenced for the ordered
8801 * extent. If not just return like we didn't do anything.
8802 */
8803 if (!igrab(inode)) {
8804 redirty_page_for_writepage(wbc, page);
8805 return AOP_WRITEPAGE_ACTIVATE;
8806 }
Nikolay Borisov0a9b0e52017-12-08 15:55:59 +02008807 ret = extent_write_full_page(page, wbc);
Josef Bacikbe7bd732015-10-22 15:05:09 -04008808 btrfs_add_delayed_iput(inode);
8809 return ret;
Chris Masona52d9a82007-08-27 16:49:44 -04008810}
Chris Mason39279cc2007-06-12 06:35:45 -04008811
Eric Sandeen48a3b632013-04-25 20:41:01 +00008812static int btrfs_writepages(struct address_space *mapping,
8813 struct writeback_control *wbc)
Chris Masonb293f02e2007-11-01 19:45:34 -04008814{
Nikolay Borisov8ae225a2018-04-19 10:46:38 +03008815 return extent_writepages(mapping, wbc);
Chris Masonb293f02e2007-11-01 19:45:34 -04008816}
8817
Chris Mason3ab2fb52007-11-08 10:59:22 -05008818static int
8819btrfs_readpages(struct file *file, struct address_space *mapping,
8820 struct list_head *pages, unsigned nr_pages)
8821{
Nikolay Borisov2a3ff0a2018-04-19 10:46:36 +03008822 return extent_readpages(mapping, pages, nr_pages);
Chris Mason3ab2fb52007-11-08 10:59:22 -05008823}
Nikolay Borisov2a3ff0a2018-04-19 10:46:36 +03008824
Chris Masone6dcd2d2008-07-17 12:53:50 -04008825static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04008826{
Nikolay Borisov477a30b2018-04-19 10:46:34 +03008827 int ret = try_release_extent_mapping(page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04008828 if (ret == 1) {
8829 ClearPagePrivate(page);
8830 set_page_private(page, 0);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008831 put_page(page);
Chris Masona52d9a82007-08-27 16:49:44 -04008832 }
8833 return ret;
8834}
Chris Mason39279cc2007-06-12 06:35:45 -04008835
Chris Masone6dcd2d2008-07-17 12:53:50 -04008836static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
8837{
Chris Mason98509cf2008-09-11 15:51:43 -04008838 if (PageWriteback(page) || PageDirty(page))
8839 return 0;
Michal Hocko3ba7ab22017-01-09 15:39:02 +01008840 return __btrfs_releasepage(page, gfp_flags);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008841}
8842
Lukas Czernerd47992f2013-05-21 23:17:23 -04008843static void btrfs_invalidatepage(struct page *page, unsigned int offset,
8844 unsigned int length)
Chris Masona52d9a82007-08-27 16:49:44 -04008845{
Josef Bacik5fd02042012-05-02 14:00:54 -04008846 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05008847 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008848 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008849 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008850 u64 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008851 u64 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308852 u64 start;
8853 u64 end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008854 int inode_evicting = inode->i_state & I_FREEING;
Chris Masona52d9a82007-08-27 16:49:44 -04008855
Chris Mason8b62b722009-09-02 16:53:46 -04008856 /*
8857 * we have the page locked, so new writeback can't start,
8858 * and the dirty bit won't be cleared while we are here.
8859 *
8860 * Wait for IO on this page so that we can safely clear
8861 * the PagePrivate2 bit and do ordered accounting
8862 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008863 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04008864
Josef Bacik5fd02042012-05-02 14:00:54 -04008865 tree = &BTRFS_I(inode)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008866 if (offset) {
8867 btrfs_releasepage(page, GFP_NOFS);
8868 return;
8869 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008870
8871 if (!inode_evicting)
David Sterbaff13db42015-12-03 14:30:40 +01008872 lock_extent_bits(tree, page_start, page_end, &cached_state);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308873again:
8874 start = page_start;
Nikolay Borisova776c6f2017-02-20 13:50:49 +02008875 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), start,
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308876 page_end - start + 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008877 if (ordered) {
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308878 end = min(page_end, ordered->file_offset + ordered->len - 1);
Chris Masoneb84ae02008-07-17 13:53:27 -04008879 /*
8880 * IO on this page will never be started, so we need
8881 * to account for any ordered extents now
8882 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008883 if (!inode_evicting)
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308884 clear_extent_bit(tree, start, end,
Omar Sandovale1821632019-08-15 14:04:04 -07008885 EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008886 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
David Sterbaae0f1622017-10-31 16:37:52 +01008887 EXTENT_DEFRAG, 1, 0, &cached_state);
Chris Mason8b62b722009-09-02 16:53:46 -04008888 /*
8889 * whoever cleared the private bit is responsible
8890 * for the finish_ordered_io
8891 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04008892 if (TestClearPagePrivate2(page)) {
8893 struct btrfs_ordered_inode_tree *tree;
8894 u64 new_len;
8895
8896 tree = &BTRFS_I(inode)->ordered_tree;
8897
8898 spin_lock_irq(&tree->lock);
8899 set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308900 new_len = start - ordered->file_offset;
Josef Bacik77cef2e2013-08-29 13:57:21 -04008901 if (new_len < ordered->truncated_len)
8902 ordered->truncated_len = new_len;
8903 spin_unlock_irq(&tree->lock);
8904
8905 if (btrfs_dec_test_ordered_pending(inode, &ordered,
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308906 start,
8907 end - start + 1, 1))
Josef Bacik77cef2e2013-08-29 13:57:21 -04008908 btrfs_finish_ordered_io(ordered);
Chris Mason8b62b722009-09-02 16:53:46 -04008909 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008910 btrfs_put_ordered_extent(ordered);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008911 if (!inode_evicting) {
8912 cached_state = NULL;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308913 lock_extent_bits(tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008914 &cached_state);
8915 }
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308916
8917 start = end + 1;
8918 if (start < page_end)
8919 goto again;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008920 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008921
Qu Wenruob9d0b382015-09-29 10:35:16 +08008922 /*
8923 * Qgroup reserved space handler
8924 * Page here will be either
8925 * 1) Already written to disk
8926 * In this case, its reserved space is released from data rsv map
8927 * and will be freed by delayed_ref handler finally.
8928 * So even we call qgroup_free_data(), it won't decrease reserved
8929 * space.
8930 * 2) Not written to disk
Goldwyn Rodrigues0b34c262016-09-30 10:40:52 -05008931 * This means the reserved space should be freed here. However,
8932 * if a truncate invalidates the page (by clearing PageDirty)
8933 * and the page is accounted for while allocating extent
8934 * in btrfs_check_data_free_space() we let delayed_ref to
8935 * free the entire extent.
Qu Wenruob9d0b382015-09-29 10:35:16 +08008936 */
Goldwyn Rodrigues0b34c262016-09-30 10:40:52 -05008937 if (PageDirty(page))
Qu Wenruobc42bda2017-02-27 15:10:39 +08008938 btrfs_qgroup_free_data(inode, NULL, page_start, PAGE_SIZE);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008939 if (!inode_evicting) {
Omar Sandovale1821632019-08-15 14:04:04 -07008940 clear_extent_bit(tree, page_start, page_end, EXTENT_LOCKED |
Filipe Mananaa7e3b972017-04-03 10:45:46 +01008941 EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
8942 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1,
David Sterbaae0f1622017-10-31 16:37:52 +01008943 &cached_state);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008944
8945 __btrfs_releasepage(page, GFP_NOFS);
8946 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008947
Chris Mason4a096752008-07-21 10:29:44 -04008948 ClearPageChecked(page);
Chris Mason9ad6b7bc2008-04-18 16:11:30 -04008949 if (PagePrivate(page)) {
Chris Mason9ad6b7bc2008-04-18 16:11:30 -04008950 ClearPagePrivate(page);
8951 set_page_private(page, 0);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008952 put_page(page);
Chris Mason9ad6b7bc2008-04-18 16:11:30 -04008953 }
Chris Mason39279cc2007-06-12 06:35:45 -04008954}
8955
Chris Mason9ebefb182007-06-15 13:50:00 -04008956/*
8957 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
8958 * called from a page fault handler when a page is first dirtied. Hence we must
8959 * be careful to check for EOF conditions here. We set the page up correctly
8960 * for a written page which means we get ENOSPC checking when writing into
8961 * holes and correct delalloc and unwritten extent mapping on filesystems that
8962 * support these features.
8963 *
8964 * We are not allowed to take the i_mutex here so we have to play games to
8965 * protect against truncate races as the page could now be beyond EOF. Because
Omar Sandovald1342aa2018-05-11 13:13:29 -07008966 * truncate_setsize() writes the inode size before removing pages, once we have
8967 * the page lock we can determine safely if the page is beyond EOF. If it is not
Chris Mason9ebefb182007-06-15 13:50:00 -04008968 * beyond EOF, then the page is guaranteed safe against truncation until we
8969 * unlock the page.
8970 */
Souptick Joardera528a242018-06-06 19:54:44 +05308971vm_fault_t btrfs_page_mkwrite(struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04008972{
Nick Pigginc2ec1752009-03-31 15:23:21 -07008973 struct page *page = vmf->page;
Dave Jiang11bac802017-02-24 14:56:41 -08008974 struct inode *inode = file_inode(vmf->vma->vm_file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008975 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008976 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
8977 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008978 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08008979 struct extent_changeset *data_reserved = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008980 char *kaddr;
8981 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04008982 loff_t size;
Souptick Joardera528a242018-06-06 19:54:44 +05308983 vm_fault_t ret;
8984 int ret2;
Chris Mason9998eb72012-01-25 13:47:40 -05008985 int reserved = 0;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308986 u64 reserved_space;
Chris Masona52d9a82007-08-27 16:49:44 -04008987 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008988 u64 page_end;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308989 u64 end;
8990
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008991 reserved_space = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04008992
Jan Karab2b5ef52012-06-12 16:20:45 +02008993 sb_start_pagefault(inode->i_sb);
Qu Wenruodf480632015-09-08 17:25:54 +08008994 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008995 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308996 end = page_end;
Qu Wenruodf480632015-09-08 17:25:54 +08008997
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308998 /*
8999 * Reserving delalloc space after obtaining the page lock can lead to
9000 * deadlock. For example, if a dirty page is locked by this function
9001 * and the call to btrfs_delalloc_reserve_space() ends up triggering
9002 * dirty page write out, then the btrfs_writepage() function could
9003 * end up waiting indefinitely to get a lock on the page currently
9004 * being processed by btrfs_page_mkwrite() function.
9005 */
Souptick Joardera528a242018-06-06 19:54:44 +05309006 ret2 = btrfs_delalloc_reserve_space(inode, &data_reserved, page_start,
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309007 reserved_space);
Souptick Joardera528a242018-06-06 19:54:44 +05309008 if (!ret2) {
9009 ret2 = file_update_time(vmf->vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05009010 reserved = 1;
9011 }
Souptick Joardera528a242018-06-06 19:54:44 +05309012 if (ret2) {
9013 ret = vmf_error(ret2);
Chris Mason9998eb72012-01-25 13:47:40 -05009014 if (reserved)
9015 goto out;
9016 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07009017 }
Chris Mason1832a6d2007-12-21 16:27:21 -05009018
Nick Piggin56a76f82009-03-31 15:23:23 -07009019 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04009020again:
Chris Mason9ebefb182007-06-15 13:50:00 -04009021 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04009022 size = i_size_read(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04009023
Chris Mason9ebefb182007-06-15 13:50:00 -04009024 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04009025 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04009026 /* page got truncated out from underneath us */
9027 goto out_unlock;
9028 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04009029 wait_on_page_writeback(page);
9030
David Sterbaff13db42015-12-03 14:30:40 +01009031 lock_extent_bits(io_tree, page_start, page_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009032 set_page_extent_mapped(page);
9033
Chris Masoneb84ae02008-07-17 13:53:27 -04009034 /*
9035 * we can't set the delalloc bits if there are pending ordered
9036 * extents. Drop our locks and wait for them to finish
9037 */
Nikolay Borisova776c6f2017-02-20 13:50:49 +02009038 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start,
9039 PAGE_SIZE);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009040 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00009041 unlock_extent_cached(io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01009042 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009043 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04009044 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009045 btrfs_put_ordered_extent(ordered);
9046 goto again;
9047 }
9048
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009049 if (page->index == ((size - 1) >> PAGE_SHIFT)) {
Jeff Mahoneyda170662016-06-15 09:22:56 -04009050 reserved_space = round_up(size - page_start,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009051 fs_info->sectorsize);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009052 if (reserved_space < PAGE_SIZE) {
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309053 end = page_start + reserved_space - 1;
Qu Wenruobc42bda2017-02-27 15:10:39 +08009054 btrfs_delalloc_release_space(inode, data_reserved,
Qu Wenruo43b18592017-12-12 15:34:32 +08009055 page_start, PAGE_SIZE - reserved_space,
9056 true);
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309057 }
9058 }
9059
Josef Bacikfbf19082009-10-01 17:10:23 -04009060 /*
Liu Bo54160342016-12-13 12:15:19 -08009061 * page_mkwrite gets called when the page is firstly dirtied after it's
9062 * faulted in, but write(2) could also dirty a page and set delalloc
9063 * bits, thus in this case for space account reason, we still need to
9064 * clear any delalloc bits within this page range since we have to
9065 * reserve data&meta space before lock_page() (see above comments).
Josef Bacikfbf19082009-10-01 17:10:23 -04009066 */
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309067 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, end,
Omar Sandovale1821632019-08-15 14:04:04 -07009068 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
9069 EXTENT_DEFRAG, 0, 0, &cached_state);
Josef Bacikfbf19082009-10-01 17:10:23 -04009070
Souptick Joardera528a242018-06-06 19:54:44 +05309071 ret2 = btrfs_set_extent_delalloc(inode, page_start, end, 0,
Nikolay Borisov330a5822019-07-17 16:18:17 +03009072 &cached_state);
Souptick Joardera528a242018-06-06 19:54:44 +05309073 if (ret2) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00009074 unlock_extent_cached(io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01009075 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009076 ret = VM_FAULT_SIGBUS;
9077 goto out_unlock;
9078 }
Souptick Joardera528a242018-06-06 19:54:44 +05309079 ret2 = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04009080
9081 /* page is wholly or partially inside EOF */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009082 if (page_start + PAGE_SIZE > size)
Johannes Thumshirn70730172018-12-05 15:23:03 +01009083 zero_start = offset_in_page(size);
Chris Mason9ebefb182007-06-15 13:50:00 -04009084 else
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009085 zero_start = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04009086
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009087 if (zero_start != PAGE_SIZE) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04009088 kaddr = kmap(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009089 memset(kaddr + zero_start, 0, PAGE_SIZE - zero_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009090 flush_dcache_page(page);
9091 kunmap(page);
9092 }
Chris Mason247e7432008-07-17 12:53:51 -04009093 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009094 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04009095 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04009096
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009097 BTRFS_I(inode)->last_trans = fs_info->generation;
Chris Mason257c62e2009-10-13 13:21:08 -04009098 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06009099 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
Chris Mason257c62e2009-10-13 13:21:08 -04009100
David Sterbae43bbe52017-12-12 21:43:52 +01009101 unlock_extent_cached(io_tree, page_start, page_end, &cached_state);
Chris Mason9ebefb182007-06-15 13:50:00 -04009102
Souptick Joardera528a242018-06-06 19:54:44 +05309103 if (!ret2) {
Qu Wenruo8702ba92019-10-14 14:34:51 +08009104 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE);
Jan Karab2b5ef52012-06-12 16:20:45 +02009105 sb_end_pagefault(inode->i_sb);
Qu Wenruo364ecf32017-02-27 15:10:38 +08009106 extent_changeset_free(data_reserved);
Chris Mason50a9b212009-09-11 12:33:12 -04009107 return VM_FAULT_LOCKED;
Jan Karab2b5ef52012-06-12 16:20:45 +02009108 }
Chris Mason717beb92018-06-25 10:03:41 -07009109
9110out_unlock:
Chris Mason9ebefb182007-06-15 13:50:00 -04009111 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05009112out:
Qu Wenruo8702ba92019-10-14 14:34:51 +08009113 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE);
Qu Wenruobc42bda2017-02-27 15:10:39 +08009114 btrfs_delalloc_release_space(inode, data_reserved, page_start,
Qu Wenruo43b18592017-12-12 15:34:32 +08009115 reserved_space, (ret != 0));
Chris Mason9998eb72012-01-25 13:47:40 -05009116out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02009117 sb_end_pagefault(inode->i_sb);
Qu Wenruo364ecf32017-02-27 15:10:38 +08009118 extent_changeset_free(data_reserved);
Chris Mason9ebefb182007-06-15 13:50:00 -04009119 return ret;
9120}
9121
Filipe Manana213e8c52018-02-06 20:40:31 +00009122static int btrfs_truncate(struct inode *inode, bool skip_writeback)
Chris Mason39279cc2007-06-12 06:35:45 -04009123{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009124 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009125 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04009126 struct btrfs_block_rsv *rsv;
Omar Sandovalad7e1a72018-05-22 09:59:50 -07009127 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04009128 struct btrfs_trans_handle *trans;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009129 u64 mask = fs_info->sectorsize - 1;
Josef Bacik2bd36e72019-08-22 15:14:33 -04009130 u64 min_size = btrfs_calc_metadata_size(fs_info, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04009131
Filipe Manana213e8c52018-02-06 20:40:31 +00009132 if (!skip_writeback) {
9133 ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask),
9134 (u64)-1);
9135 if (ret)
9136 return ret;
9137 }
Chris Mason39279cc2007-06-12 06:35:45 -04009138
Josef Bacikfcb80c22011-05-03 10:40:22 -04009139 /*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07009140 * Yes ladies and gentlemen, this is indeed ugly. We have a couple of
9141 * things going on here:
Josef Bacikfcb80c22011-05-03 10:40:22 -04009142 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07009143 * 1) We need to reserve space to update our inode.
Josef Bacikfcb80c22011-05-03 10:40:22 -04009144 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07009145 * 2) We need to have something to cache all the space that is going to
Josef Bacikfcb80c22011-05-03 10:40:22 -04009146 * be free'd up by the truncate operation, but also have some slack
9147 * space reserved in case it uses space during the truncate (thank you
9148 * very much snapshotting).
9149 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07009150 * And we need these to be separate. The fact is we can use a lot of
Josef Bacikfcb80c22011-05-03 10:40:22 -04009151 * space doing the truncate, and we have no earthly idea how much space
Nicholas D Steeves01327612016-05-19 21:18:45 -04009152 * we will use, so we need the truncate reservation to be separate so it
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07009153 * doesn't end up using space reserved for updating the inode. We also
9154 * need to be able to stop the transaction and start a new one, which
9155 * means we need to be able to update the inode several times, and we
9156 * have no idea of knowing how many times that will be, so we can't just
9157 * reserve 1 item for the entirety of the operation, so that has to be
9158 * done separately as well.
Josef Bacikfcb80c22011-05-03 10:40:22 -04009159 *
9160 * So that leaves us with
9161 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07009162 * 1) rsv - for the truncate reservation, which we will steal from the
Josef Bacikfcb80c22011-05-03 10:40:22 -04009163 * transaction reservation.
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07009164 * 2) fs_info->trans_block_rsv - this will have 1 items worth left for
Josef Bacikfcb80c22011-05-03 10:40:22 -04009165 * updating the inode.
9166 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009167 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009168 if (!rsv)
9169 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04009170 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04009171 rsv->failfast = 1;
Josef Bacikf0cd8462011-03-04 14:37:08 -05009172
Josef Bacik907cbce2011-08-08 13:46:15 -04009173 /*
Josef Bacik07127182011-08-19 10:29:59 -04009174 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04009175 * 1 for updating the inode.
9176 */
Josef Bacikf3fe8202013-01-07 17:03:21 -05009177 trans = btrfs_start_transaction(root, 2);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009178 if (IS_ERR(trans)) {
Omar Sandovalad7e1a72018-05-22 09:59:50 -07009179 ret = PTR_ERR(trans);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009180 goto out;
9181 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05009182
Josef Bacik907cbce2011-08-08 13:46:15 -04009183 /* Migrate the slack space for the truncate to our reserve */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009184 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, rsv,
Lu Fengqi3a584172018-08-04 21:10:55 +08009185 min_size, false);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009186 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05009187
Chris Mason5a3f23d2009-03-31 13:27:11 -04009188 /*
Josef Bacik5dc562c2012-08-17 13:14:17 -04009189 * So if we truncate and then write and fsync we normally would just
9190 * write the extents that changed, which is a problem if we need to
9191 * first truncate that entire inode. So set this flag so we write out
9192 * all of the extents in the inode to the sync log so we're completely
9193 * safe.
9194 */
9195 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009196 trans->block_rsv = rsv;
Josef Bacik5dc562c2012-08-17 13:14:17 -04009197
Yan, Zheng80825102009-11-12 09:35:36 +00009198 while (1) {
9199 ret = btrfs_truncate_inode_items(trans, root, inode,
9200 inode->i_size,
9201 BTRFS_EXTENT_DATA_KEY);
Josef Bacikddfae632017-10-19 14:16:02 -04009202 trans->block_rsv = &fs_info->trans_block_rsv;
Omar Sandovalad7e1a72018-05-22 09:59:50 -07009203 if (ret != -ENOSPC && ret != -EAGAIN)
Yan, Zheng80825102009-11-12 09:35:36 +00009204 break;
Chris Mason39279cc2007-06-12 06:35:45 -04009205
Yan, Zheng80825102009-11-12 09:35:36 +00009206 ret = btrfs_update_inode(trans, root, inode);
Omar Sandovalad7e1a72018-05-22 09:59:50 -07009207 if (ret)
Josef Bacik3893e332011-01-31 16:03:11 -05009208 break;
Josef Bacikca7e70f2012-08-27 17:48:15 -04009209
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009210 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009211 btrfs_btree_balance_dirty(fs_info);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009212
9213 trans = btrfs_start_transaction(root, 2);
9214 if (IS_ERR(trans)) {
Omar Sandovalad7e1a72018-05-22 09:59:50 -07009215 ret = PTR_ERR(trans);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009216 trans = NULL;
9217 break;
9218 }
9219
Wang Xiaoguang47b5d642016-09-07 20:17:38 +08009220 btrfs_block_rsv_release(fs_info, rsv, -1);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009221 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv,
Lu Fengqi3a584172018-08-04 21:10:55 +08009222 rsv, min_size, false);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009223 BUG_ON(ret); /* shouldn't happen */
9224 trans->block_rsv = rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00009225 }
9226
Josef Bacikddfae632017-10-19 14:16:02 -04009227 /*
9228 * We can't call btrfs_truncate_block inside a trans handle as we could
9229 * deadlock with freeze, if we got NEED_TRUNCATE_BLOCK then we know
9230 * we've truncated everything except the last little bit, and can do
9231 * btrfs_truncate_block and then update the disk_i_size.
9232 */
9233 if (ret == NEED_TRUNCATE_BLOCK) {
9234 btrfs_end_transaction(trans);
9235 btrfs_btree_balance_dirty(fs_info);
9236
9237 ret = btrfs_truncate_block(inode, inode->i_size, 0, 0);
9238 if (ret)
9239 goto out;
9240 trans = btrfs_start_transaction(root, 1);
9241 if (IS_ERR(trans)) {
9242 ret = PTR_ERR(trans);
9243 goto out;
9244 }
9245 btrfs_ordered_update_i_size(inode, inode->i_size, NULL);
9246 }
9247
Chris Mason917c16b2011-11-08 14:49:59 -05009248 if (trans) {
Omar Sandovalad7e1a72018-05-22 09:59:50 -07009249 int ret2;
Josef Bacik7b128762008-07-24 12:17:14 -04009250
Omar Sandovalad7e1a72018-05-22 09:59:50 -07009251 trans->block_rsv = &fs_info->trans_block_rsv;
9252 ret2 = btrfs_update_inode(trans, root, inode);
9253 if (ret2 && !ret)
9254 ret = ret2;
9255
9256 ret2 = btrfs_end_transaction(trans);
9257 if (ret2 && !ret)
9258 ret = ret2;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009259 btrfs_btree_balance_dirty(fs_info);
Chris Mason917c16b2011-11-08 14:49:59 -05009260 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04009261out:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009262 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009263
Omar Sandovalad7e1a72018-05-22 09:59:50 -07009264 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04009265}
9266
Sven Wegener3b963622008-06-09 21:57:42 -04009267/*
Chris Masond352ac62008-09-29 15:18:18 -04009268 * create a new subvolume directory/inode (helper for the ioctl).
9269 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05009270int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009271 struct btrfs_root *new_root,
9272 struct btrfs_root *parent_root,
9273 u64 new_dirid)
Chris Mason39279cc2007-06-12 06:35:45 -04009274{
Chris Mason39279cc2007-06-12 06:35:45 -04009275 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04009276 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04009277 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009278
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01009279 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
9280 new_dirid, new_dirid,
9281 S_IFDIR | (~current_umask() & S_IRWXUGO),
9282 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04009283 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04009284 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009285 inode->i_op = &btrfs_dir_inode_operations;
9286 inode->i_fop = &btrfs_dir_file_operations;
9287
Miklos Szeredibfe86842011-10-28 14:13:29 +02009288 set_nlink(inode, 1);
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02009289 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Masonb0d5d102014-09-08 13:08:51 -07009290 unlock_new_inode(inode);
Sven Wegener3b963622008-06-09 21:57:42 -04009291
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009292 err = btrfs_subvol_inherit_props(trans, new_root, parent_root);
9293 if (err)
9294 btrfs_err(new_root->fs_info,
David Sterba351fd352014-05-15 16:48:20 +02009295 "error inheriting subvolume %llu properties: %d",
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009296 new_root->root_key.objectid, err);
9297
Yan, Zheng76dda932009-09-21 16:00:26 -04009298 err = btrfs_update_inode(trans, new_root, inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04009299
Yan, Zheng76dda932009-09-21 16:00:26 -04009300 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07009301 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04009302}
9303
Chris Mason39279cc2007-06-12 06:35:45 -04009304struct inode *btrfs_alloc_inode(struct super_block *sb)
9305{
Josef Bacik69fe2d72017-10-19 14:15:57 -04009306 struct btrfs_fs_info *fs_info = btrfs_sb(sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009307 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009308 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009309
David Sterba712e36c2017-10-31 17:08:27 +01009310 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_KERNEL);
Chris Mason39279cc2007-06-12 06:35:45 -04009311 if (!ei)
9312 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009313
9314 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009315 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04009316 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04009317 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04009318 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009319 ei->delalloc_bytes = 0;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01009320 ei->new_delalloc_bytes = 0;
Wang Shilong47059d92014-07-03 18:22:07 +08009321 ei->defrag_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009322 ei->disk_i_size = 0;
9323 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04009324 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009325 ei->index_cnt = (u64)-1;
Miao Xie67de1172013-12-26 13:07:06 +08009326 ei->dir_index = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009327 ei->last_unlink_trans = 0;
Liu Bo46d8bc32012-08-29 01:07:55 -06009328 ei->last_log_commit = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009329
Josef Bacik9e0baf62011-07-15 15:16:44 +00009330 spin_lock_init(&ei->lock);
9331 ei->outstanding_extents = 0;
Josef Bacik69fe2d72017-10-19 14:15:57 -04009332 if (sb->s_magic != BTRFS_TEST_MAGIC)
9333 btrfs_init_metadata_block_rsv(fs_info, &ei->block_rsv,
9334 BTRFS_BLOCK_RSV_DELALLOC);
Josef Bacik72ac3c02012-05-23 14:13:11 -04009335 ei->runtime_flags = 0;
David Sterbab52aa8c2017-07-17 19:17:20 +02009336 ei->prop_compress = BTRFS_COMPRESS_NONE;
David Sterbaeec63c62017-07-17 19:41:31 +02009337 ei->defrag_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009338
Miao Xie16cdcec2011-04-22 18:12:22 +08009339 ei->delayed_node = NULL;
9340
chandan r9cc97d62012-07-04 12:48:07 +05309341 ei->i_otime.tv_sec = 0;
9342 ei->i_otime.tv_nsec = 0;
9343
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009344 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02009345 extent_map_tree_init(&ei->extent_tree);
Qu Wenruo43eb5f22019-03-01 10:47:59 +08009346 extent_io_tree_init(fs_info, &ei->io_tree, IO_TREE_INODE_IO, inode);
9347 extent_io_tree_init(fs_info, &ei->io_failure_tree,
9348 IO_TREE_INODE_IO_FAILURE, inode);
David Sterba7b439732019-03-11 15:58:30 +01009349 ei->io_tree.track_uptodate = true;
9350 ei->io_failure_tree.track_uptodate = true;
Josef Bacikb812ce22012-11-16 13:56:32 -05009351 atomic_set(&ei->sync_writers, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009352 mutex_init(&ei->log_mutex);
Josef Bacikf2486792012-01-13 12:09:22 -05009353 mutex_init(&ei->delalloc_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009354 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009355 INIT_LIST_HEAD(&ei->delalloc_inodes);
David Sterba8089fe62015-11-19 14:15:51 +01009356 INIT_LIST_HEAD(&ei->delayed_iput);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009357 RB_CLEAR_NODE(&ei->rb_node);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01009358 init_rwsem(&ei->dio_sem);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009359
9360 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009361}
9362
Josef Bacikaaedb552013-10-11 14:44:09 -04009363#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
9364void btrfs_test_destroy_inode(struct inode *inode)
9365{
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02009366 btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0);
Josef Bacikaaedb552013-10-11 14:44:09 -04009367 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
9368}
9369#endif
9370
Al Viro26602ca2019-04-10 15:14:41 -04009371void btrfs_free_inode(struct inode *inode)
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009372{
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009373 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
9374}
9375
Chris Mason39279cc2007-06-12 06:35:45 -04009376void btrfs_destroy_inode(struct inode *inode)
9377{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009378 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009379 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009380 struct btrfs_root *root = BTRFS_I(inode)->root;
9381
Al Virob3d9b7a2012-06-09 13:51:19 -04009382 WARN_ON(!hlist_empty(&inode->i_dentry));
Chris Mason39279cc2007-06-12 06:35:45 -04009383 WARN_ON(inode->i_data.nrpages);
Josef Bacik69fe2d72017-10-19 14:15:57 -04009384 WARN_ON(BTRFS_I(inode)->block_rsv.reserved);
9385 WARN_ON(BTRFS_I(inode)->block_rsv.size);
Josef Bacik9e0baf62011-07-15 15:16:44 +00009386 WARN_ON(BTRFS_I(inode)->outstanding_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04009387 WARN_ON(BTRFS_I(inode)->delalloc_bytes);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01009388 WARN_ON(BTRFS_I(inode)->new_delalloc_bytes);
Josef Bacik7709cde2011-08-04 10:25:02 -04009389 WARN_ON(BTRFS_I(inode)->csum_bytes);
Wang Shilong47059d92014-07-03 18:22:07 +08009390 WARN_ON(BTRFS_I(inode)->defrag_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04009391
Chris Mason5a3f23d2009-03-31 13:27:11 -04009392 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05009393 * This can happen where we create an inode, but somebody else also
9394 * created the same inode and we need to destroy the one we already
9395 * created.
9396 */
9397 if (!root)
Al Viro26602ca2019-04-10 15:14:41 -04009398 return;
Josef Bacika6dbd422009-11-11 15:53:34 -05009399
Chris Masond3977122009-01-05 21:25:51 -05009400 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04009401 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
9402 if (!ordered)
9403 break;
9404 else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009405 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04009406 "found ordered extent %llu %llu on inode cleanup",
9407 ordered->file_offset, ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009408 btrfs_remove_ordered_extent(inode, ordered);
9409 btrfs_put_ordered_extent(ordered);
9410 btrfs_put_ordered_extent(ordered);
9411 }
9412 }
Qu Wenruo56fa9d02015-10-13 09:53:10 +08009413 btrfs_qgroup_check_reserved_leak(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04009414 inode_tree_del(inode);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02009415 btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04009416}
9417
Al Viro45321ac2010-06-07 13:43:19 -04009418int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04009419{
9420 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04009421
Naohiro Aota6379ef92013-06-06 09:56:34 +00009422 if (root == NULL)
9423 return 1;
9424
Liu Bofa6ac872013-02-20 14:10:23 +00009425 /* the snap/subvol tree is on deleting */
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02009426 if (btrfs_root_refs(&root->root_item) == 0)
Al Viro45321ac2010-06-07 13:43:19 -04009427 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04009428 else
Al Viro45321ac2010-06-07 13:43:19 -04009429 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04009430}
9431
Sven Wegener0ee0fda2008-07-30 16:54:26 -04009432static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04009433{
9434 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
9435
9436 inode_init_once(&ei->vfs_inode);
9437}
9438
David Sterbae67c7182018-02-19 17:24:18 +01009439void __cold btrfs_destroy_cachep(void)
Chris Mason39279cc2007-06-12 06:35:45 -04009440{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10009441 /*
9442 * Make sure all delayed rcu free inodes are flushed before we
9443 * destroy cache.
9444 */
9445 rcu_barrier();
Kinglong Mee5598e902016-01-29 21:36:35 +08009446 kmem_cache_destroy(btrfs_inode_cachep);
9447 kmem_cache_destroy(btrfs_trans_handle_cachep);
Kinglong Mee5598e902016-01-29 21:36:35 +08009448 kmem_cache_destroy(btrfs_path_cachep);
9449 kmem_cache_destroy(btrfs_free_space_cachep);
Christophe Leroy3acd4852019-08-21 15:05:55 +00009450 kmem_cache_destroy(btrfs_free_space_bitmap_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04009451}
9452
Liu Bof5c29bd2017-11-02 17:21:50 -06009453int __init btrfs_init_cachep(void)
Chris Mason39279cc2007-06-12 06:35:45 -04009454{
David Sterba837e1972012-09-07 03:00:48 -06009455 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009456 sizeof(struct btrfs_inode), 0,
Vladimir Davydov5d097052016-01-14 15:18:21 -08009457 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD | SLAB_ACCOUNT,
9458 init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04009459 if (!btrfs_inode_cachep)
9460 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009461
David Sterba837e1972012-09-07 03:00:48 -06009462 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009463 sizeof(struct btrfs_trans_handle), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009464 SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009465 if (!btrfs_trans_handle_cachep)
9466 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009467
David Sterba837e1972012-09-07 03:00:48 -06009468 btrfs_path_cachep = kmem_cache_create("btrfs_path",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009469 sizeof(struct btrfs_path), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009470 SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009471 if (!btrfs_path_cachep)
9472 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009473
David Sterba837e1972012-09-07 03:00:48 -06009474 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
Josef Bacikdc89e982011-01-28 17:05:48 -05009475 sizeof(struct btrfs_free_space), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009476 SLAB_MEM_SPREAD, NULL);
Josef Bacikdc89e982011-01-28 17:05:48 -05009477 if (!btrfs_free_space_cachep)
9478 goto fail;
9479
Christophe Leroy3acd4852019-08-21 15:05:55 +00009480 btrfs_free_space_bitmap_cachep = kmem_cache_create("btrfs_free_space_bitmap",
9481 PAGE_SIZE, PAGE_SIZE,
9482 SLAB_RED_ZONE, NULL);
9483 if (!btrfs_free_space_bitmap_cachep)
9484 goto fail;
9485
Chris Mason39279cc2007-06-12 06:35:45 -04009486 return 0;
9487fail:
9488 btrfs_destroy_cachep();
9489 return -ENOMEM;
9490}
9491
David Howellsa528d352017-01-31 16:46:22 +00009492static int btrfs_getattr(const struct path *path, struct kstat *stat,
9493 u32 request_mask, unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04009494{
Miao Xiedf0af1a2013-01-29 10:11:59 +00009495 u64 delalloc_bytes;
David Howellsa528d352017-01-31 16:46:22 +00009496 struct inode *inode = d_inode(path->dentry);
David Sterbafadc0d82011-11-20 07:33:38 -05009497 u32 blocksize = inode->i_sb->s_blocksize;
Yonghong Song04a87e32017-05-12 15:07:43 -07009498 u32 bi_flags = BTRFS_I(inode)->flags;
9499
9500 stat->result_mask |= STATX_BTIME;
9501 stat->btime.tv_sec = BTRFS_I(inode)->i_otime.tv_sec;
9502 stat->btime.tv_nsec = BTRFS_I(inode)->i_otime.tv_nsec;
9503 if (bi_flags & BTRFS_INODE_APPEND)
9504 stat->attributes |= STATX_ATTR_APPEND;
9505 if (bi_flags & BTRFS_INODE_COMPRESS)
9506 stat->attributes |= STATX_ATTR_COMPRESSED;
9507 if (bi_flags & BTRFS_INODE_IMMUTABLE)
9508 stat->attributes |= STATX_ATTR_IMMUTABLE;
9509 if (bi_flags & BTRFS_INODE_NODUMP)
9510 stat->attributes |= STATX_ATTR_NODUMP;
9511
9512 stat->attributes_mask |= (STATX_ATTR_APPEND |
9513 STATX_ATTR_COMPRESSED |
9514 STATX_ATTR_IMMUTABLE |
9515 STATX_ATTR_NODUMP);
David Sterbafadc0d82011-11-20 07:33:38 -05009516
Chris Mason39279cc2007-06-12 06:35:45 -04009517 generic_fillattr(inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04009518 stat->dev = BTRFS_I(inode)->root->anon_dev;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009519
9520 spin_lock(&BTRFS_I(inode)->lock);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01009521 delalloc_bytes = BTRFS_I(inode)->new_delalloc_bytes;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009522 spin_unlock(&BTRFS_I(inode)->lock);
David Sterbafadc0d82011-11-20 07:33:38 -05009523 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
Miao Xiedf0af1a2013-01-29 10:11:59 +00009524 ALIGN(delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04009525 return 0;
9526}
9527
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009528static int btrfs_rename_exchange(struct inode *old_dir,
9529 struct dentry *old_dentry,
9530 struct inode *new_dir,
9531 struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04009532{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009533 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009534 struct btrfs_trans_handle *trans;
9535 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009536 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009537 struct inode *new_inode = new_dentry->d_inode;
9538 struct inode *old_inode = old_dentry->d_inode;
Deepa Dinamani95582b02018-05-08 19:36:02 -07009539 struct timespec64 ctime = current_time(old_inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009540 struct dentry *parent;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009541 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
9542 u64 new_ino = btrfs_ino(BTRFS_I(new_inode));
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009543 u64 old_idx = 0;
9544 u64 new_idx = 0;
9545 u64 root_objectid;
9546 int ret;
Filipe Manana86e8aa02016-05-05 02:02:27 +01009547 bool root_log_pinned = false;
9548 bool dest_log_pinned = false;
Filipe Mananad4682ba2018-06-11 19:24:28 +01009549 struct btrfs_log_ctx ctx_root;
9550 struct btrfs_log_ctx ctx_dest;
9551 bool sync_log_root = false;
9552 bool sync_log_dest = false;
9553 bool commit_transaction = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009554
9555 /* we only allow rename subvolume link between subvolumes */
9556 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
9557 return -EXDEV;
9558
Filipe Mananad4682ba2018-06-11 19:24:28 +01009559 btrfs_init_log_ctx(&ctx_root, old_inode);
9560 btrfs_init_log_ctx(&ctx_dest, new_inode);
9561
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009562 /* close the race window with snapshot create/destroy ioctl */
9563 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009564 down_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009565 if (new_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009566 down_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009567
9568 /*
9569 * We want to reserve the absolute worst case amount of items. So if
9570 * both inodes are subvols and we need to unlink them then that would
9571 * require 4 item modifications, but if they are both normal inodes it
9572 * would require 5 item modifications, so we'll assume their normal
9573 * inodes. So 5 * 2 is 10, plus 2 for the new links, so 12 total items
9574 * should cover the worst case number of items we'll modify.
9575 */
9576 trans = btrfs_start_transaction(root, 12);
9577 if (IS_ERR(trans)) {
9578 ret = PTR_ERR(trans);
9579 goto out_notrans;
9580 }
9581
9582 /*
9583 * We need to find a free sequence number both in the source and
9584 * in the destination directory for the exchange.
9585 */
Nikolay Borisov877574e2017-02-20 13:50:33 +02009586 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009587 if (ret)
9588 goto out_fail;
Nikolay Borisov877574e2017-02-20 13:50:33 +02009589 ret = btrfs_set_inode_index(BTRFS_I(old_dir), &new_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009590 if (ret)
9591 goto out_fail;
9592
9593 BTRFS_I(old_inode)->dir_index = 0ULL;
9594 BTRFS_I(new_inode)->dir_index = 0ULL;
9595
9596 /* Reference for the source. */
9597 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
9598 /* force full log commit if subvolume involved. */
David Sterba90787762019-03-20 13:28:05 +01009599 btrfs_set_log_full_commit(trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009600 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01009601 btrfs_pin_log_trans(root);
9602 root_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009603 ret = btrfs_insert_inode_ref(trans, dest,
9604 new_dentry->d_name.name,
9605 new_dentry->d_name.len,
9606 old_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01009607 btrfs_ino(BTRFS_I(new_dir)),
9608 old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009609 if (ret)
9610 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009611 }
9612
9613 /* And now for the dest. */
9614 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
9615 /* force full log commit if subvolume involved. */
David Sterba90787762019-03-20 13:28:05 +01009616 btrfs_set_log_full_commit(trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009617 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01009618 btrfs_pin_log_trans(dest);
9619 dest_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009620 ret = btrfs_insert_inode_ref(trans, root,
9621 old_dentry->d_name.name,
9622 old_dentry->d_name.len,
9623 new_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01009624 btrfs_ino(BTRFS_I(old_dir)),
9625 new_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009626 if (ret)
9627 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009628 }
9629
9630 /* Update inode version and ctime/mtime. */
9631 inode_inc_iversion(old_dir);
9632 inode_inc_iversion(new_dir);
9633 inode_inc_iversion(old_inode);
9634 inode_inc_iversion(new_inode);
9635 old_dir->i_ctime = old_dir->i_mtime = ctime;
9636 new_dir->i_ctime = new_dir->i_mtime = ctime;
9637 old_inode->i_ctime = ctime;
9638 new_inode->i_ctime = ctime;
9639
9640 if (old_dentry->d_parent != new_dentry->d_parent) {
David Sterbaf85b7372017-01-20 14:54:07 +01009641 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
9642 BTRFS_I(old_inode), 1);
9643 btrfs_record_unlink_dir(trans, BTRFS_I(new_dir),
9644 BTRFS_I(new_inode), 1);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009645 }
9646
9647 /* src is a subvolume */
9648 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
9649 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
Lu Fengqi401b3b12018-08-01 11:32:30 +08009650 ret = btrfs_unlink_subvol(trans, old_dir, root_objectid,
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009651 old_dentry->d_name.name,
9652 old_dentry->d_name.len);
9653 } else { /* src is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009654 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
9655 BTRFS_I(old_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009656 old_dentry->d_name.name,
9657 old_dentry->d_name.len);
9658 if (!ret)
9659 ret = btrfs_update_inode(trans, root, old_inode);
9660 }
9661 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009662 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009663 goto out_fail;
9664 }
9665
9666 /* dest is a subvolume */
9667 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
9668 root_objectid = BTRFS_I(new_inode)->root->root_key.objectid;
Lu Fengqi401b3b12018-08-01 11:32:30 +08009669 ret = btrfs_unlink_subvol(trans, new_dir, root_objectid,
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009670 new_dentry->d_name.name,
9671 new_dentry->d_name.len);
9672 } else { /* dest is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009673 ret = __btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
9674 BTRFS_I(new_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009675 new_dentry->d_name.name,
9676 new_dentry->d_name.len);
9677 if (!ret)
9678 ret = btrfs_update_inode(trans, dest, new_inode);
9679 }
9680 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009681 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009682 goto out_fail;
9683 }
9684
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009685 ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009686 new_dentry->d_name.name,
9687 new_dentry->d_name.len, 0, old_idx);
9688 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009689 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009690 goto out_fail;
9691 }
9692
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009693 ret = btrfs_add_link(trans, BTRFS_I(old_dir), BTRFS_I(new_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009694 old_dentry->d_name.name,
9695 old_dentry->d_name.len, 0, new_idx);
9696 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009697 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009698 goto out_fail;
9699 }
9700
9701 if (old_inode->i_nlink == 1)
9702 BTRFS_I(old_inode)->dir_index = old_idx;
9703 if (new_inode->i_nlink == 1)
9704 BTRFS_I(new_inode)->dir_index = new_idx;
9705
Filipe Manana86e8aa02016-05-05 02:02:27 +01009706 if (root_log_pinned) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009707 parent = new_dentry->d_parent;
Filipe Mananad4682ba2018-06-11 19:24:28 +01009708 ret = btrfs_log_new_name(trans, BTRFS_I(old_inode),
9709 BTRFS_I(old_dir), parent,
9710 false, &ctx_root);
9711 if (ret == BTRFS_NEED_LOG_SYNC)
9712 sync_log_root = true;
9713 else if (ret == BTRFS_NEED_TRANS_COMMIT)
9714 commit_transaction = true;
9715 ret = 0;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009716 btrfs_end_log_trans(root);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009717 root_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009718 }
Filipe Manana86e8aa02016-05-05 02:02:27 +01009719 if (dest_log_pinned) {
Filipe Mananad4682ba2018-06-11 19:24:28 +01009720 if (!commit_transaction) {
9721 parent = old_dentry->d_parent;
9722 ret = btrfs_log_new_name(trans, BTRFS_I(new_inode),
9723 BTRFS_I(new_dir), parent,
9724 false, &ctx_dest);
9725 if (ret == BTRFS_NEED_LOG_SYNC)
9726 sync_log_dest = true;
9727 else if (ret == BTRFS_NEED_TRANS_COMMIT)
9728 commit_transaction = true;
9729 ret = 0;
9730 }
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009731 btrfs_end_log_trans(dest);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009732 dest_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009733 }
9734out_fail:
Filipe Manana86e8aa02016-05-05 02:02:27 +01009735 /*
9736 * If we have pinned a log and an error happened, we unpin tasks
9737 * trying to sync the log and force them to fallback to a transaction
9738 * commit if the log currently contains any of the inodes involved in
9739 * this rename operation (to ensure we do not persist a log with an
9740 * inconsistent state for any of these inodes or leading to any
9741 * inconsistencies when replayed). If the transaction was aborted, the
9742 * abortion reason is propagated to userspace when attempting to commit
9743 * the transaction. If the log does not contain any of these inodes, we
9744 * allow the tasks to sync it.
9745 */
9746 if (ret && (root_log_pinned || dest_log_pinned)) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009747 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
9748 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
9749 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana86e8aa02016-05-05 02:02:27 +01009750 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009751 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
David Sterba90787762019-03-20 13:28:05 +01009752 btrfs_set_log_full_commit(trans);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009753
9754 if (root_log_pinned) {
9755 btrfs_end_log_trans(root);
9756 root_log_pinned = false;
9757 }
9758 if (dest_log_pinned) {
9759 btrfs_end_log_trans(dest);
9760 dest_log_pinned = false;
9761 }
9762 }
Filipe Mananad4682ba2018-06-11 19:24:28 +01009763 if (!ret && sync_log_root && !commit_transaction) {
9764 ret = btrfs_sync_log(trans, BTRFS_I(old_inode)->root,
9765 &ctx_root);
9766 if (ret)
9767 commit_transaction = true;
9768 }
9769 if (!ret && sync_log_dest && !commit_transaction) {
9770 ret = btrfs_sync_log(trans, BTRFS_I(new_inode)->root,
9771 &ctx_dest);
9772 if (ret)
9773 commit_transaction = true;
9774 }
9775 if (commit_transaction) {
Filipe Mananae6c61712019-11-08 16:11:56 +00009776 /*
9777 * We may have set commit_transaction when logging the new name
9778 * in the destination root, in which case we left the source
9779 * root context in the list of log contextes. So make sure we
9780 * remove it to avoid invalid memory accesses, since the context
9781 * was allocated in our stack frame.
9782 */
9783 if (sync_log_root) {
9784 mutex_lock(&root->log_mutex);
9785 list_del_init(&ctx_root.list);
9786 mutex_unlock(&root->log_mutex);
9787 }
Filipe Mananad4682ba2018-06-11 19:24:28 +01009788 ret = btrfs_commit_transaction(trans);
9789 } else {
9790 int ret2;
9791
9792 ret2 = btrfs_end_transaction(trans);
9793 ret = ret ? ret : ret2;
9794 }
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009795out_notrans:
9796 if (new_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009797 up_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009798 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009799 up_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009800
Filipe Mananae6c61712019-11-08 16:11:56 +00009801 ASSERT(list_empty(&ctx_root.list));
9802 ASSERT(list_empty(&ctx_dest.list));
9803
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009804 return ret;
9805}
9806
9807static int btrfs_whiteout_for_rename(struct btrfs_trans_handle *trans,
9808 struct btrfs_root *root,
9809 struct inode *dir,
9810 struct dentry *dentry)
9811{
9812 int ret;
9813 struct inode *inode;
9814 u64 objectid;
9815 u64 index;
9816
9817 ret = btrfs_find_free_ino(root, &objectid);
9818 if (ret)
9819 return ret;
9820
9821 inode = btrfs_new_inode(trans, root, dir,
9822 dentry->d_name.name,
9823 dentry->d_name.len,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009824 btrfs_ino(BTRFS_I(dir)),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009825 objectid,
9826 S_IFCHR | WHITEOUT_MODE,
9827 &index);
9828
9829 if (IS_ERR(inode)) {
9830 ret = PTR_ERR(inode);
9831 return ret;
9832 }
9833
9834 inode->i_op = &btrfs_special_inode_operations;
9835 init_special_inode(inode, inode->i_mode,
9836 WHITEOUT_DEV);
9837
9838 ret = btrfs_init_inode_security(trans, inode, dir,
9839 &dentry->d_name);
9840 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009841 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009842
Nikolay Borisovcef415a2017-02-20 13:51:09 +02009843 ret = btrfs_add_nondir(trans, BTRFS_I(dir), dentry,
9844 BTRFS_I(inode), 0, index);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009845 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009846 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009847
9848 ret = btrfs_update_inode(trans, root, inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009849out:
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009850 unlock_new_inode(inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009851 if (ret)
9852 inode_dec_link_count(inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009853 iput(inode);
9854
Filipe Mananac9901612016-05-05 01:41:57 +01009855 return ret;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009856}
9857
Chris Mason39279cc2007-06-12 06:35:45 -04009858static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009859 struct inode *new_dir, struct dentry *new_dentry,
9860 unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04009861{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009862 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009863 struct btrfs_trans_handle *trans;
Filipe Manana5062af32016-05-05 10:26:26 +01009864 unsigned int trans_num_items;
Chris Mason39279cc2007-06-12 06:35:45 -04009865 struct btrfs_root *root = BTRFS_I(old_dir)->root;
9866 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00009867 struct inode *new_inode = d_inode(new_dentry);
9868 struct inode *old_inode = d_inode(old_dentry);
Chris Mason00e4e6b2008-08-05 11:18:09 -04009869 u64 index = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009870 u64 root_objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04009871 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009872 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009873 bool log_pinned = false;
Filipe Mananad4682ba2018-06-11 19:24:28 +01009874 struct btrfs_log_ctx ctx;
9875 bool sync_log = false;
9876 bool commit_transaction = false;
Chris Mason39279cc2007-06-12 06:35:45 -04009877
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009878 if (btrfs_ino(BTRFS_I(new_dir)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04009879 return -EPERM;
9880
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009881 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08009882 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05009883 return -EXDEV;
9884
Li Zefan33345d012011-04-20 10:31:50 +08009885 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009886 (new_inode && btrfs_ino(BTRFS_I(new_inode)) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009887 return -ENOTEMPTY;
9888
Chris Mason39279cc2007-06-12 06:35:45 -04009889 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009890 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04009891 return -ENOTEMPTY;
Chris Mason9c520572012-12-17 14:26:57 -05009892
9893
9894 /* check for collisions, even if the name isn't there */
Josef Bacik4871c152013-10-09 12:24:04 -04009895 ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino,
Chris Mason9c520572012-12-17 14:26:57 -05009896 new_dentry->d_name.name,
9897 new_dentry->d_name.len);
9898
9899 if (ret) {
9900 if (ret == -EEXIST) {
9901 /* we shouldn't get
9902 * eexist without a new_inode */
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05309903 if (WARN_ON(!new_inode)) {
Chris Mason9c520572012-12-17 14:26:57 -05009904 return ret;
9905 }
9906 } else {
9907 /* maybe -EOVERFLOW */
9908 return ret;
9909 }
9910 }
9911 ret = 0;
9912
Chris Mason5a3f23d2009-03-31 13:27:11 -04009913 /*
Chris Mason8d875f92014-08-12 10:47:42 -07009914 * we're using rename to replace one file with another. Start IO on it
9915 * now so we don't add too much work to the end of the transaction
Chris Mason5a3f23d2009-03-31 13:27:11 -04009916 */
Chris Mason8d875f92014-08-12 10:47:42 -07009917 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size)
Chris Mason5a3f23d2009-03-31 13:27:11 -04009918 filemap_flush(old_inode->i_mapping);
9919
Yan, Zheng76dda932009-09-21 16:00:26 -04009920 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08009921 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009922 down_read(&fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04009923 /*
9924 * We want to reserve the absolute worst case amount of items. So if
9925 * both inodes are subvols and we need to unlink them then that would
9926 * require 4 item modifications, but if they are both normal inodes it
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009927 * would require 5 item modifications, so we'll assume they are normal
Yan, Zhenga22285a2010-05-16 10:48:46 -04009928 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
9929 * should cover the worst case number of items we'll modify.
Filipe Manana5062af32016-05-05 10:26:26 +01009930 * If our rename has the whiteout flag, we need more 5 units for the
9931 * new inode (1 inode item, 1 inode ref, 2 dir items and 1 xattr item
9932 * when selinux is enabled).
Yan, Zhenga22285a2010-05-16 10:48:46 -04009933 */
Filipe Manana5062af32016-05-05 10:26:26 +01009934 trans_num_items = 11;
9935 if (flags & RENAME_WHITEOUT)
9936 trans_num_items += 5;
9937 trans = btrfs_start_transaction(root, trans_num_items);
Johann Lombardib44c59a2011-03-31 13:23:47 +00009938 if (IS_ERR(trans)) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009939 ret = PTR_ERR(trans);
9940 goto out_notrans;
9941 }
Chris Mason5f39d392007-10-15 16:14:19 -04009942
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009943 if (dest != root)
9944 btrfs_record_root_in_trans(trans, dest);
9945
Nikolay Borisov877574e2017-02-20 13:50:33 +02009946 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009947 if (ret)
9948 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009949
Miao Xie67de1172013-12-26 13:07:06 +08009950 BTRFS_I(old_inode)->dir_index = 0ULL;
Li Zefan33345d012011-04-20 10:31:50 +08009951 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009952 /* force full log commit if subvolume involved. */
David Sterba90787762019-03-20 13:28:05 +01009953 btrfs_set_log_full_commit(trans);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009954 } else {
Filipe Mananac4aba952016-04-29 13:14:42 +01009955 btrfs_pin_log_trans(root);
9956 log_pinned = true;
Yan, Zhenga5719522009-09-24 09:17:31 -04009957 ret = btrfs_insert_inode_ref(trans, dest,
9958 new_dentry->d_name.name,
9959 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08009960 old_ino,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009961 btrfs_ino(BTRFS_I(new_dir)), index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009962 if (ret)
9963 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009964 }
Chris Mason5a3f23d2009-03-31 13:27:11 -04009965
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009966 inode_inc_iversion(old_dir);
9967 inode_inc_iversion(new_dir);
9968 inode_inc_iversion(old_inode);
Deepa Dinamani04b285f2016-02-06 23:57:21 -08009969 old_dir->i_ctime = old_dir->i_mtime =
9970 new_dir->i_ctime = new_dir->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009971 old_inode->i_ctime = current_time(old_dir);
Chris Mason5f39d392007-10-15 16:14:19 -04009972
Chris Mason12fcfd22009-03-24 10:24:20 -04009973 if (old_dentry->d_parent != new_dentry->d_parent)
David Sterbaf85b7372017-01-20 14:54:07 +01009974 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
9975 BTRFS_I(old_inode), 1);
Chris Mason12fcfd22009-03-24 10:24:20 -04009976
Li Zefan33345d012011-04-20 10:31:50 +08009977 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009978 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
Lu Fengqi401b3b12018-08-01 11:32:30 +08009979 ret = btrfs_unlink_subvol(trans, old_dir, root_objectid,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009980 old_dentry->d_name.name,
9981 old_dentry->d_name.len);
9982 } else {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009983 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
9984 BTRFS_I(d_inode(old_dentry)),
Al Viro92986792011-03-04 17:14:37 +00009985 old_dentry->d_name.name,
9986 old_dentry->d_name.len);
9987 if (!ret)
9988 ret = btrfs_update_inode(trans, root, old_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009989 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009990 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009991 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009992 goto out_fail;
9993 }
Chris Mason39279cc2007-06-12 06:35:45 -04009994
9995 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009996 inode_inc_iversion(new_inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009997 new_inode->i_ctime = current_time(new_inode);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009998 if (unlikely(btrfs_ino(BTRFS_I(new_inode)) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009999 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
10000 root_objectid = BTRFS_I(new_inode)->location.objectid;
Lu Fengqi401b3b12018-08-01 11:32:30 +080010001 ret = btrfs_unlink_subvol(trans, new_dir, root_objectid,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010002 new_dentry->d_name.name,
10003 new_dentry->d_name.len);
10004 BUG_ON(new_inode->i_nlink == 0);
10005 } else {
Nikolay Borisov4ec59342017-01-18 00:31:44 +020010006 ret = btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
10007 BTRFS_I(d_inode(new_dentry)),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010008 new_dentry->d_name.name,
10009 new_dentry->d_name.len);
10010 }
Josef Bacik4ef31a42013-08-13 14:10:08 -040010011 if (!ret && new_inode->i_nlink == 0)
Nikolay Borisov73f2e542017-02-20 13:50:59 +020010012 ret = btrfs_orphan_add(trans,
10013 BTRFS_I(d_inode(new_dentry)));
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010014 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -040010015 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010016 goto out_fail;
10017 }
Chris Mason39279cc2007-06-12 06:35:45 -040010018 }
Josef Bacikaec74772008-07-24 12:12:38 -040010019
Nikolay Borisovdb0a6692017-02-20 13:51:08 +020010020 ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010021 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -040010022 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010023 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -040010024 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010025 goto out_fail;
10026 }
Chris Mason39279cc2007-06-12 06:35:45 -040010027
Miao Xie67de1172013-12-26 13:07:06 +080010028 if (old_inode->i_nlink == 1)
10029 BTRFS_I(old_inode)->dir_index = index;
10030
Filipe Manana3dc9e8f72016-04-29 11:34:22 +010010031 if (log_pinned) {
Al Viro10d9f302011-07-16 23:09:10 -040010032 struct dentry *parent = new_dentry->d_parent;
Filipe Manana3dc9e8f72016-04-29 11:34:22 +010010033
Filipe Mananad4682ba2018-06-11 19:24:28 +010010034 btrfs_init_log_ctx(&ctx, old_inode);
10035 ret = btrfs_log_new_name(trans, BTRFS_I(old_inode),
10036 BTRFS_I(old_dir), parent,
10037 false, &ctx);
10038 if (ret == BTRFS_NEED_LOG_SYNC)
10039 sync_log = true;
10040 else if (ret == BTRFS_NEED_TRANS_COMMIT)
10041 commit_transaction = true;
10042 ret = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010043 btrfs_end_log_trans(root);
Filipe Manana3dc9e8f72016-04-29 11:34:22 +010010044 log_pinned = false;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010045 }
Dan Fuhrycdd1fed2016-03-17 15:23:38 +010010046
10047 if (flags & RENAME_WHITEOUT) {
10048 ret = btrfs_whiteout_for_rename(trans, root, old_dir,
10049 old_dentry);
10050
10051 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -040010052 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +010010053 goto out_fail;
10054 }
Chris Mason12fcfd22009-03-24 10:24:20 -040010055 }
Chris Mason39279cc2007-06-12 06:35:45 -040010056out_fail:
Filipe Manana3dc9e8f72016-04-29 11:34:22 +010010057 /*
10058 * If we have pinned the log and an error happened, we unpin tasks
10059 * trying to sync the log and force them to fallback to a transaction
10060 * commit if the log currently contains any of the inodes involved in
10061 * this rename operation (to ensure we do not persist a log with an
10062 * inconsistent state for any of these inodes or leading to any
10063 * inconsistencies when replayed). If the transaction was aborted, the
10064 * abortion reason is propagated to userspace when attempting to commit
10065 * the transaction. If the log does not contain any of these inodes, we
10066 * allow the tasks to sync it.
10067 */
10068 if (ret && log_pinned) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +020010069 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
10070 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
10071 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana3dc9e8f72016-04-29 11:34:22 +010010072 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +020010073 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
David Sterba90787762019-03-20 13:28:05 +010010074 btrfs_set_log_full_commit(trans);
Filipe Manana3dc9e8f72016-04-29 11:34:22 +010010075
10076 btrfs_end_log_trans(root);
10077 log_pinned = false;
10078 }
Filipe Mananad4682ba2018-06-11 19:24:28 +010010079 if (!ret && sync_log) {
10080 ret = btrfs_sync_log(trans, BTRFS_I(old_inode)->root, &ctx);
10081 if (ret)
10082 commit_transaction = true;
10083 }
10084 if (commit_transaction) {
10085 ret = btrfs_commit_transaction(trans);
10086 } else {
10087 int ret2;
10088
10089 ret2 = btrfs_end_transaction(trans);
10090 ret = ret ? ret : ret2;
10091 }
Johann Lombardib44c59a2011-03-31 13:23:47 +000010092out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +080010093 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010094 up_read(&fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -040010095
Chris Mason39279cc2007-06-12 06:35:45 -040010096 return ret;
10097}
10098
Miklos Szeredi80ace852014-07-23 15:15:32 +020010099static int btrfs_rename2(struct inode *old_dir, struct dentry *old_dentry,
10100 struct inode *new_dir, struct dentry *new_dentry,
10101 unsigned int flags)
10102{
Dan Fuhrycdd1fed2016-03-17 15:23:38 +010010103 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
Miklos Szeredi80ace852014-07-23 15:15:32 +020010104 return -EINVAL;
10105
Dan Fuhrycdd1fed2016-03-17 15:23:38 +010010106 if (flags & RENAME_EXCHANGE)
10107 return btrfs_rename_exchange(old_dir, old_dentry, new_dir,
10108 new_dentry);
10109
10110 return btrfs_rename(old_dir, old_dentry, new_dir, new_dentry, flags);
Miklos Szeredi80ace852014-07-23 15:15:32 +020010111}
10112
Nikolay Borisov3a2f8c02018-04-24 17:23:59 +030010113struct btrfs_delalloc_work {
10114 struct inode *inode;
10115 struct completion completion;
10116 struct list_head list;
10117 struct btrfs_work work;
10118};
10119
Miao Xie8ccf6f192012-10-25 09:28:04 +000010120static void btrfs_run_delalloc_work(struct btrfs_work *work)
10121{
10122 struct btrfs_delalloc_work *delalloc_work;
Josef Bacik9f23e282013-10-28 15:03:41 -040010123 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010124
10125 delalloc_work = container_of(work, struct btrfs_delalloc_work,
10126 work);
Josef Bacik9f23e282013-10-28 15:03:41 -040010127 inode = delalloc_work->inode;
David Sterba30424602015-11-27 19:27:11 +010010128 filemap_flush(inode->i_mapping);
10129 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
10130 &BTRFS_I(inode)->runtime_flags))
Josef Bacik9f23e282013-10-28 15:03:41 -040010131 filemap_flush(inode->i_mapping);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010132
Nikolay Borisov076da912018-04-23 10:54:16 +030010133 iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010134 complete(&delalloc_work->completion);
10135}
10136
Nikolay Borisov3a2f8c02018-04-24 17:23:59 +030010137static struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode)
Miao Xie8ccf6f192012-10-25 09:28:04 +000010138{
10139 struct btrfs_delalloc_work *work;
10140
David Sterba100d5702015-12-08 14:39:32 +010010141 work = kmalloc(sizeof(*work), GFP_NOFS);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010142 if (!work)
10143 return NULL;
10144
10145 init_completion(&work->completion);
10146 INIT_LIST_HEAD(&work->list);
10147 work->inode = inode;
Omar Sandovala0cac0e2019-09-16 11:30:57 -070010148 btrfs_init_work(&work->work, btrfs_run_delalloc_work, NULL, NULL);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010149
10150 return work;
10151}
10152
Chris Masond352ac62008-09-29 15:18:18 -040010153/*
10154 * some fairly slow code that needs optimization. This walks the list
10155 * of all the inodes with pending delalloc and forces them to disk.
10156 */
Ethan Lien3cd24c62018-11-01 14:49:03 +080010157static int start_delalloc_inodes(struct btrfs_root *root, int nr, bool snapshot)
Chris Masonea8c2812008-08-04 23:17:27 -040010158{
Chris Masonea8c2812008-08-04 23:17:27 -040010159 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010160 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010161 struct btrfs_delalloc_work *work, *next;
10162 struct list_head works;
Miao Xie1eafa6c2013-01-22 10:49:00 +000010163 struct list_head splice;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010164 int ret = 0;
Chris Masonea8c2812008-08-04 23:17:27 -040010165
Miao Xie8ccf6f192012-10-25 09:28:04 +000010166 INIT_LIST_HEAD(&works);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010167 INIT_LIST_HEAD(&splice);
Miao Xie63607cc2013-01-22 10:50:35 +000010168
Miao Xie573bfb72014-03-06 13:55:03 +080010169 mutex_lock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010170 spin_lock(&root->delalloc_lock);
10171 list_splice_init(&root->delalloc_inodes, &splice);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010172 while (!list_empty(&splice)) {
10173 binode = list_entry(splice.next, struct btrfs_inode,
Chris Masonea8c2812008-08-04 23:17:27 -040010174 delalloc_inodes);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010175
Miao Xieeb73c1b2013-05-15 07:48:22 +000010176 list_move_tail(&binode->delalloc_inodes,
10177 &root->delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010178 inode = igrab(&binode->vfs_inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +000010179 if (!inode) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010180 cond_resched_lock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010181 continue;
Miao Xiedf0af1a2013-01-29 10:11:59 +000010182 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010183 spin_unlock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010184
Ethan Lien3cd24c62018-11-01 14:49:03 +080010185 if (snapshot)
10186 set_bit(BTRFS_INODE_SNAPSHOT_FLUSH,
10187 &binode->runtime_flags);
Nikolay Borisov076da912018-04-23 10:54:16 +030010188 work = btrfs_alloc_delalloc_work(inode);
David Sterba5d99a9982014-09-29 19:20:37 +020010189 if (!work) {
Nikolay Borisov4fbb5142018-04-23 10:54:15 +030010190 iput(inode);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010191 ret = -ENOMEM;
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010192 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010193 }
Miao Xie1eafa6c2013-01-22 10:49:00 +000010194 list_add_tail(&work->list, &works);
Qu Wenruoa44903a2014-02-28 10:46:09 +080010195 btrfs_queue_work(root->fs_info->flush_workers,
10196 &work->work);
Miao Xie6c255e62014-03-06 13:55:01 +080010197 ret++;
10198 if (nr != -1 && ret >= nr)
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010199 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010200 cond_resched();
Miao Xieeb73c1b2013-05-15 07:48:22 +000010201 spin_lock(&root->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -040010202 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010203 spin_unlock(&root->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -040010204
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010205out:
Miao Xie1eafa6c2013-01-22 10:49:00 +000010206 list_for_each_entry_safe(work, next, &works, list) {
10207 list_del_init(&work->list);
Nikolay Borisov40012f92018-04-19 10:46:39 +030010208 wait_for_completion(&work->completion);
10209 kfree(work);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010210 }
10211
Nikolay Borisov81f1d392018-04-19 10:46:37 +030010212 if (!list_empty(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010213 spin_lock(&root->delalloc_lock);
10214 list_splice_tail(&splice, &root->delalloc_inodes);
10215 spin_unlock(&root->delalloc_lock);
10216 }
Miao Xie573bfb72014-03-06 13:55:03 +080010217 mutex_unlock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010218 return ret;
10219}
10220
Ethan Lien3cd24c62018-11-01 14:49:03 +080010221int btrfs_start_delalloc_snapshot(struct btrfs_root *root)
Miao Xieeb73c1b2013-05-15 07:48:22 +000010222{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010223 struct btrfs_fs_info *fs_info = root->fs_info;
Miao Xieeb73c1b2013-05-15 07:48:22 +000010224 int ret;
10225
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010226 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +000010227 return -EROFS;
10228
Ethan Lien3cd24c62018-11-01 14:49:03 +080010229 ret = start_delalloc_inodes(root, -1, true);
Miao Xie6c255e62014-03-06 13:55:01 +080010230 if (ret > 0)
10231 ret = 0;
Miao Xieeb73c1b2013-05-15 07:48:22 +000010232 return ret;
10233}
10234
Nikolay Borisov82b3e532018-04-23 10:54:13 +030010235int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, int nr)
Miao Xieeb73c1b2013-05-15 07:48:22 +000010236{
10237 struct btrfs_root *root;
10238 struct list_head splice;
10239 int ret;
10240
Wang Shilong2c21b4d2014-01-14 19:42:20 +080010241 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +000010242 return -EROFS;
10243
10244 INIT_LIST_HEAD(&splice);
10245
Miao Xie573bfb72014-03-06 13:55:03 +080010246 mutex_lock(&fs_info->delalloc_root_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010247 spin_lock(&fs_info->delalloc_root_lock);
10248 list_splice_init(&fs_info->delalloc_roots, &splice);
Miao Xie6c255e62014-03-06 13:55:01 +080010249 while (!list_empty(&splice) && nr) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010250 root = list_first_entry(&splice, struct btrfs_root,
10251 delalloc_root);
10252 root = btrfs_grab_fs_root(root);
10253 BUG_ON(!root);
10254 list_move_tail(&root->delalloc_root,
10255 &fs_info->delalloc_roots);
10256 spin_unlock(&fs_info->delalloc_root_lock);
10257
Ethan Lien3cd24c62018-11-01 14:49:03 +080010258 ret = start_delalloc_inodes(root, nr, false);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010259 btrfs_put_fs_root(root);
Miao Xie6c255e62014-03-06 13:55:01 +080010260 if (ret < 0)
Miao Xieeb73c1b2013-05-15 07:48:22 +000010261 goto out;
10262
Miao Xie6c255e62014-03-06 13:55:01 +080010263 if (nr != -1) {
10264 nr -= ret;
10265 WARN_ON(nr < 0);
10266 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010267 spin_lock(&fs_info->delalloc_root_lock);
10268 }
10269 spin_unlock(&fs_info->delalloc_root_lock);
10270
Miao Xie6c255e62014-03-06 13:55:01 +080010271 ret = 0;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010272out:
Nikolay Borisov81f1d392018-04-19 10:46:37 +030010273 if (!list_empty(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010274 spin_lock(&fs_info->delalloc_root_lock);
10275 list_splice_tail(&splice, &fs_info->delalloc_roots);
10276 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010277 }
Miao Xie573bfb72014-03-06 13:55:03 +080010278 mutex_unlock(&fs_info->delalloc_root_mutex);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010279 return ret;
Chris Masonea8c2812008-08-04 23:17:27 -040010280}
10281
Chris Mason39279cc2007-06-12 06:35:45 -040010282static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
10283 const char *symname)
10284{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010285 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -040010286 struct btrfs_trans_handle *trans;
10287 struct btrfs_root *root = BTRFS_I(dir)->root;
10288 struct btrfs_path *path;
10289 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -050010290 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -040010291 int err;
Chris Mason39279cc2007-06-12 06:35:45 -040010292 u64 objectid;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +053010293 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -040010294 int name_len;
10295 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -040010296 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -040010297 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -040010298 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -040010299
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +010010300 name_len = strlen(symname);
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010301 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info))
Chris Mason39279cc2007-06-12 06:35:45 -040010302 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -050010303
Josef Bacik9ed74f22009-09-11 16:12:44 -040010304 /*
10305 * 2 items for inode item and ref
10306 * 2 items for dir items
Filipe Manana9269d122015-12-31 18:16:29 +000010307 * 1 item for updating parent inode item
10308 * 1 item for the inline extent item
Josef Bacik9ed74f22009-09-11 16:12:44 -040010309 * 1 item for xattr if selinux is on
10310 */
Filipe Manana9269d122015-12-31 18:16:29 +000010311 trans = btrfs_start_transaction(root, 7);
Yan, Zhenga22285a2010-05-16 10:48:46 -040010312 if (IS_ERR(trans))
10313 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -050010314
Li Zefan581bb052011-04-20 10:06:11 +080010315 err = btrfs_find_free_ino(root, &objectid);
10316 if (err)
10317 goto out_unlock;
10318
Josef Bacikaec74772008-07-24 12:12:38 -040010319 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +010010320 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)),
10321 objectid, S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -040010322 if (IS_ERR(inode)) {
10323 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -040010324 inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -040010325 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -040010326 }
Chris Mason39279cc2007-06-12 06:35:45 -040010327
Casey Schauflerad19db72011-12-15 10:09:07 -050010328 /*
10329 * If the active LSM wants to access the inode during
10330 * d_instantiate it needs these. Smack checks to see
10331 * if the filesystem supports xattrs by looking at the
10332 * ops vector.
10333 */
10334 inode->i_fop = &btrfs_file_operations;
10335 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -070010336 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -070010337 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
10338
10339 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
10340 if (err)
Al Viro32955c52018-05-16 12:20:05 -040010341 goto out_unlock;
Casey Schauflerad19db72011-12-15 10:09:07 -050010342
Chris Mason39279cc2007-06-12 06:35:45 -040010343 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -070010344 if (!path) {
10345 err = -ENOMEM;
Al Viro32955c52018-05-16 12:20:05 -040010346 goto out_unlock;
Mark Fashehd8926bb2011-07-13 10:38:47 -070010347 }
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +020010348 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason39279cc2007-06-12 06:35:45 -040010349 key.offset = 0;
David Sterba962a2982014-06-04 18:41:45 +020010350 key.type = BTRFS_EXTENT_DATA_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -040010351 datasize = btrfs_file_extent_calc_inline_size(name_len);
10352 err = btrfs_insert_empty_item(trans, root, path, &key,
10353 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -040010354 if (err) {
Julia Lawallb0839162011-05-14 07:10:51 +000010355 btrfs_free_path(path);
Al Viro32955c52018-05-16 12:20:05 -040010356 goto out_unlock;
Chris Mason54aa1f42007-06-22 14:16:25 -040010357 }
Chris Mason5f39d392007-10-15 16:14:19 -040010358 leaf = path->nodes[0];
10359 ei = btrfs_item_ptr(leaf, path->slots[0],
10360 struct btrfs_file_extent_item);
10361 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
10362 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -040010363 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -040010364 btrfs_set_file_extent_encryption(leaf, ei, 0);
10365 btrfs_set_file_extent_compression(leaf, ei, 0);
10366 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
10367 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
10368
Chris Mason39279cc2007-06-12 06:35:45 -040010369 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -040010370 write_extent_buffer(leaf, symname, ptr, name_len);
10371 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -040010372 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -040010373
Chris Mason39279cc2007-06-12 06:35:45 -040010374 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -050010375 inode_nohighmem(inode);
Yan Zhengd899e052008-10-30 14:25:28 -040010376 inode_set_bytes(inode, name_len);
Nikolay Borisov6ef06d22017-02-20 13:50:34 +020010377 btrfs_i_size_write(BTRFS_I(inode), name_len);
Chris Mason54aa1f42007-06-22 14:16:25 -040010378 err = btrfs_update_inode(trans, root, inode);
Filipe Mananad50866d2015-12-31 18:08:24 +000010379 /*
10380 * Last step, add directory indexes for our symlink inode. This is the
10381 * last step to avoid extra cleanup of these indexes if an error happens
10382 * elsewhere above.
10383 */
10384 if (!err)
Nikolay Borisovcef415a2017-02-20 13:51:09 +020010385 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry,
10386 BTRFS_I(inode), 0, index);
Al Viro32955c52018-05-16 12:20:05 -040010387 if (err)
10388 goto out_unlock;
Chris Masonb0d5d102014-09-08 13:08:51 -070010389
Al Viro1e2e5472018-05-04 08:23:01 -040010390 d_instantiate_new(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010391
10392out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010393 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -040010394 if (err && inode) {
Chris Mason39279cc2007-06-12 06:35:45 -040010395 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -040010396 discard_new_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010397 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010398 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -040010399 return err;
10400}
Chris Mason16432982008-04-10 10:23:21 -040010401
Josef Bacik0af3d002010-06-21 14:48:16 -040010402static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
10403 u64 start, u64 num_bytes, u64 min_size,
10404 loff_t actual_len, u64 *alloc_hint,
10405 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -040010406{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010407 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik5dc562c2012-08-17 13:14:17 -040010408 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
10409 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -040010410 struct btrfs_root *root = BTRFS_I(inode)->root;
10411 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -040010412 u64 cur_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +000010413 u64 i_size;
Chris Mason154ea282013-03-05 11:11:26 -050010414 u64 cur_bytes;
Josef Bacik0b670dc2015-09-23 17:11:16 -040010415 u64 last_alloc = (u64)-1;
Yan Zhengd899e052008-10-30 14:25:28 -040010416 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -040010417 bool own_trans = true;
Wang Xiaoguang18513092016-07-25 15:51:40 +080010418 u64 end = start + num_bytes - 1;
Yan Zhengd899e052008-10-30 14:25:28 -040010419
Josef Bacik0af3d002010-06-21 14:48:16 -040010420 if (trans)
10421 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -040010422 while (num_bytes > 0) {
Josef Bacik0af3d002010-06-21 14:48:16 -040010423 if (own_trans) {
10424 trans = btrfs_start_transaction(root, 3);
10425 if (IS_ERR(trans)) {
10426 ret = PTR_ERR(trans);
10427 break;
10428 }
Yan Zhengd899e052008-10-30 14:25:28 -040010429 }
Yan, Zheng5a303d52009-11-12 09:34:52 +000010430
Byongho Leeee221842015-12-15 01:42:10 +090010431 cur_bytes = min_t(u64, num_bytes, SZ_256M);
Chris Mason154ea282013-03-05 11:11:26 -050010432 cur_bytes = max(cur_bytes, min_size);
Josef Bacik0b670dc2015-09-23 17:11:16 -040010433 /*
10434 * If we are severely fragmented we could end up with really
10435 * small allocations, so if the allocator is returning small
10436 * chunks lets make its job easier by only searching for those
10437 * sized chunks.
10438 */
10439 cur_bytes = min(cur_bytes, last_alloc);
Wang Xiaoguang18513092016-07-25 15:51:40 +080010440 ret = btrfs_reserve_extent(root, cur_bytes, cur_bytes,
10441 min_size, 0, *alloc_hint, &ins, 1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010442 if (ret) {
Josef Bacik0af3d002010-06-21 14:48:16 -040010443 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010444 btrfs_end_transaction(trans);
Yan, Zhenga22285a2010-05-16 10:48:46 -040010445 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +000010446 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010447 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010448
Josef Bacik0b670dc2015-09-23 17:11:16 -040010449 last_alloc = ins.offset;
Yan Zhengd899e052008-10-30 14:25:28 -040010450 ret = insert_reserved_file_extent(trans, inode,
10451 cur_offset, ins.objectid,
10452 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +000010453 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -040010454 BTRFS_FILE_EXTENT_PREALLOC);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010455 if (ret) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010456 btrfs_free_reserved_extent(fs_info, ins.objectid,
Miao Xiee570fd22014-06-19 10:42:50 +080010457 ins.offset, 0);
Jeff Mahoney66642832016-06-10 18:19:25 -040010458 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010459 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010460 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010461 break;
10462 }
Dongsheng Yang31193212014-12-12 16:44:35 +080010463
Nikolay Borisovdcdbc052017-02-20 13:50:45 +020010464 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Chris Masona1ed8352009-09-11 12:27:37 -040010465 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010466
Josef Bacik5dc562c2012-08-17 13:14:17 -040010467 em = alloc_extent_map();
10468 if (!em) {
10469 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
10470 &BTRFS_I(inode)->runtime_flags);
10471 goto next;
10472 }
10473
10474 em->start = cur_offset;
10475 em->orig_start = cur_offset;
10476 em->len = ins.offset;
10477 em->block_start = ins.objectid;
10478 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -050010479 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -040010480 em->ram_bytes = ins.offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010481 em->bdev = fs_info->fs_devices->latest_bdev;
Josef Bacik5dc562c2012-08-17 13:14:17 -040010482 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
10483 em->generation = trans->transid;
10484
10485 while (1) {
10486 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -040010487 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -040010488 write_unlock(&em_tree->lock);
10489 if (ret != -EEXIST)
10490 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +020010491 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -040010492 cur_offset + ins.offset - 1,
10493 0);
10494 }
10495 free_extent_map(em);
10496next:
Yan Zhengd899e052008-10-30 14:25:28 -040010497 num_bytes -= ins.offset;
10498 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -040010499 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +000010500
Josef Bacik0c4d2d92012-04-05 15:03:02 -040010501 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -070010502 inode->i_ctime = current_time(inode);
Christoph Hellwig6cbff002009-04-17 10:37:41 +020010503 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -040010504 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -040010505 (actual_len > inode->i_size) &&
10506 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +000010507 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +000010508 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +000010509 else
Josef Bacik55a61d12010-11-22 18:50:32 +000010510 i_size = cur_offset;
10511 i_size_write(inode, i_size);
10512 btrfs_ordered_update_i_size(inode, i_size, NULL);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010513 }
10514
Yan Zhengd899e052008-10-30 14:25:28 -040010515 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010516
10517 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -040010518 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010519 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010520 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010521 break;
10522 }
Yan Zhengd899e052008-10-30 14:25:28 -040010523
Josef Bacik0af3d002010-06-21 14:48:16 -040010524 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010525 btrfs_end_transaction(trans);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010526 }
Wang Xiaoguang18513092016-07-25 15:51:40 +080010527 if (cur_offset < end)
Qu Wenruobc42bda2017-02-27 15:10:39 +080010528 btrfs_free_reserved_data_space(inode, NULL, cur_offset,
Wang Xiaoguang18513092016-07-25 15:51:40 +080010529 end - cur_offset + 1);
Yan Zhengd899e052008-10-30 14:25:28 -040010530 return ret;
10531}
10532
Josef Bacik0af3d002010-06-21 14:48:16 -040010533int btrfs_prealloc_file_range(struct inode *inode, int mode,
10534 u64 start, u64 num_bytes, u64 min_size,
10535 loff_t actual_len, u64 *alloc_hint)
10536{
10537 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
10538 min_size, actual_len, alloc_hint,
10539 NULL);
10540}
10541
10542int btrfs_prealloc_file_range_trans(struct inode *inode,
10543 struct btrfs_trans_handle *trans, int mode,
10544 u64 start, u64 num_bytes, u64 min_size,
10545 loff_t actual_len, u64 *alloc_hint)
10546{
10547 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
10548 min_size, actual_len, alloc_hint, trans);
10549}
10550
Chris Masone6dcd2d2008-07-17 12:53:50 -040010551static int btrfs_set_page_dirty(struct page *page)
10552{
Chris Masone6dcd2d2008-07-17 12:53:50 -040010553 return __set_page_dirty_nobuffers(page);
10554}
10555
Al Viro10556cb22011-06-20 19:28:19 -040010556static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -050010557{
Li Zefanb83cc962010-12-20 16:04:08 +080010558 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +000010559 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +080010560
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +000010561 if (mask & MAY_WRITE &&
10562 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
10563 if (btrfs_root_readonly(root))
10564 return -EROFS;
10565 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
10566 return -EACCES;
10567 }
Al Viro2830ba72011-06-20 19:16:29 -040010568 return generic_permission(inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -050010569}
Chris Mason39279cc2007-06-12 06:35:45 -040010570
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010571static int btrfs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
10572{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010573 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010574 struct btrfs_trans_handle *trans;
10575 struct btrfs_root *root = BTRFS_I(dir)->root;
10576 struct inode *inode = NULL;
10577 u64 objectid;
10578 u64 index;
10579 int ret = 0;
10580
10581 /*
10582 * 5 units required for adding orphan entry
10583 */
10584 trans = btrfs_start_transaction(root, 5);
10585 if (IS_ERR(trans))
10586 return PTR_ERR(trans);
10587
10588 ret = btrfs_find_free_ino(root, &objectid);
10589 if (ret)
10590 goto out;
10591
10592 inode = btrfs_new_inode(trans, root, dir, NULL, 0,
David Sterbaf85b7372017-01-20 14:54:07 +010010593 btrfs_ino(BTRFS_I(dir)), objectid, mode, &index);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010594 if (IS_ERR(inode)) {
10595 ret = PTR_ERR(inode);
10596 inode = NULL;
10597 goto out;
10598 }
10599
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010600 inode->i_fop = &btrfs_file_operations;
10601 inode->i_op = &btrfs_file_inode_operations;
10602
10603 inode->i_mapping->a_ops = &btrfs_aops;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010604 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
10605
Chris Masonb0d5d102014-09-08 13:08:51 -070010606 ret = btrfs_init_inode_security(trans, inode, dir, NULL);
10607 if (ret)
Al Viro32955c52018-05-16 12:20:05 -040010608 goto out;
Chris Masonb0d5d102014-09-08 13:08:51 -070010609
10610 ret = btrfs_update_inode(trans, root, inode);
10611 if (ret)
Al Viro32955c52018-05-16 12:20:05 -040010612 goto out;
Nikolay Borisov73f2e542017-02-20 13:50:59 +020010613 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010614 if (ret)
Al Viro32955c52018-05-16 12:20:05 -040010615 goto out;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010616
Filipe Manana5762b5c2014-08-01 00:10:32 +010010617 /*
10618 * We set number of links to 0 in btrfs_new_inode(), and here we set
10619 * it to 1 because d_tmpfile() will issue a warning if the count is 0,
10620 * through:
10621 *
10622 * d_tmpfile() -> inode_dec_link_count() -> drop_nlink()
10623 */
10624 set_nlink(inode, 1);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010625 d_tmpfile(dentry, inode);
Al Viro32955c52018-05-16 12:20:05 -040010626 unlock_new_inode(inode);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010627 mark_inode_dirty(inode);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010628out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010629 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -040010630 if (ret && inode)
10631 discard_new_inode(inode);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010632 btrfs_btree_balance_dirty(fs_info);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010633 return ret;
10634}
10635
David Sterba5cdc84b2018-07-18 20:32:52 +020010636void btrfs_set_range_writeback(struct extent_io_tree *tree, u64 start, u64 end)
Josef Bacikc6100a42017-05-05 11:57:13 -040010637{
David Sterba5cdc84b2018-07-18 20:32:52 +020010638 struct inode *inode = tree->private_data;
Josef Bacikc6100a42017-05-05 11:57:13 -040010639 unsigned long index = start >> PAGE_SHIFT;
10640 unsigned long end_index = end >> PAGE_SHIFT;
10641 struct page *page;
10642
10643 while (index <= end_index) {
10644 page = find_get_page(inode->i_mapping, index);
10645 ASSERT(page); /* Pages should be in the extent_io_tree */
10646 set_page_writeback(page);
10647 put_page(page);
10648 index++;
10649 }
10650}
10651
Omar Sandovaled46ff32016-11-03 10:28:14 -070010652#ifdef CONFIG_SWAP
10653/*
10654 * Add an entry indicating a block group or device which is pinned by a
10655 * swapfile. Returns 0 on success, 1 if there is already an entry for it, or a
10656 * negative errno on failure.
10657 */
10658static int btrfs_add_swapfile_pin(struct inode *inode, void *ptr,
10659 bool is_block_group)
10660{
10661 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
10662 struct btrfs_swapfile_pin *sp, *entry;
10663 struct rb_node **p;
10664 struct rb_node *parent = NULL;
10665
10666 sp = kmalloc(sizeof(*sp), GFP_NOFS);
10667 if (!sp)
10668 return -ENOMEM;
10669 sp->ptr = ptr;
10670 sp->inode = inode;
10671 sp->is_block_group = is_block_group;
10672
10673 spin_lock(&fs_info->swapfile_pins_lock);
10674 p = &fs_info->swapfile_pins.rb_node;
10675 while (*p) {
10676 parent = *p;
10677 entry = rb_entry(parent, struct btrfs_swapfile_pin, node);
10678 if (sp->ptr < entry->ptr ||
10679 (sp->ptr == entry->ptr && sp->inode < entry->inode)) {
10680 p = &(*p)->rb_left;
10681 } else if (sp->ptr > entry->ptr ||
10682 (sp->ptr == entry->ptr && sp->inode > entry->inode)) {
10683 p = &(*p)->rb_right;
10684 } else {
10685 spin_unlock(&fs_info->swapfile_pins_lock);
10686 kfree(sp);
10687 return 1;
10688 }
10689 }
10690 rb_link_node(&sp->node, parent, p);
10691 rb_insert_color(&sp->node, &fs_info->swapfile_pins);
10692 spin_unlock(&fs_info->swapfile_pins_lock);
10693 return 0;
10694}
10695
10696/* Free all of the entries pinned by this swapfile. */
10697static void btrfs_free_swapfile_pins(struct inode *inode)
10698{
10699 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
10700 struct btrfs_swapfile_pin *sp;
10701 struct rb_node *node, *next;
10702
10703 spin_lock(&fs_info->swapfile_pins_lock);
10704 node = rb_first(&fs_info->swapfile_pins);
10705 while (node) {
10706 next = rb_next(node);
10707 sp = rb_entry(node, struct btrfs_swapfile_pin, node);
10708 if (sp->inode == inode) {
10709 rb_erase(&sp->node, &fs_info->swapfile_pins);
10710 if (sp->is_block_group)
10711 btrfs_put_block_group(sp->ptr);
10712 kfree(sp);
10713 }
10714 node = next;
10715 }
10716 spin_unlock(&fs_info->swapfile_pins_lock);
10717}
10718
10719struct btrfs_swap_info {
10720 u64 start;
10721 u64 block_start;
10722 u64 block_len;
10723 u64 lowest_ppage;
10724 u64 highest_ppage;
10725 unsigned long nr_pages;
10726 int nr_extents;
10727};
10728
10729static int btrfs_add_swap_extent(struct swap_info_struct *sis,
10730 struct btrfs_swap_info *bsi)
10731{
10732 unsigned long nr_pages;
10733 u64 first_ppage, first_ppage_reported, next_ppage;
10734 int ret;
10735
10736 first_ppage = ALIGN(bsi->block_start, PAGE_SIZE) >> PAGE_SHIFT;
10737 next_ppage = ALIGN_DOWN(bsi->block_start + bsi->block_len,
10738 PAGE_SIZE) >> PAGE_SHIFT;
10739
10740 if (first_ppage >= next_ppage)
10741 return 0;
10742 nr_pages = next_ppage - first_ppage;
10743
10744 first_ppage_reported = first_ppage;
10745 if (bsi->start == 0)
10746 first_ppage_reported++;
10747 if (bsi->lowest_ppage > first_ppage_reported)
10748 bsi->lowest_ppage = first_ppage_reported;
10749 if (bsi->highest_ppage < (next_ppage - 1))
10750 bsi->highest_ppage = next_ppage - 1;
10751
10752 ret = add_swap_extent(sis, bsi->nr_pages, nr_pages, first_ppage);
10753 if (ret < 0)
10754 return ret;
10755 bsi->nr_extents += ret;
10756 bsi->nr_pages += nr_pages;
10757 return 0;
10758}
10759
10760static void btrfs_swap_deactivate(struct file *file)
10761{
10762 struct inode *inode = file_inode(file);
10763
10764 btrfs_free_swapfile_pins(inode);
10765 atomic_dec(&BTRFS_I(inode)->root->nr_swapfiles);
10766}
10767
10768static int btrfs_swap_activate(struct swap_info_struct *sis, struct file *file,
10769 sector_t *span)
10770{
10771 struct inode *inode = file_inode(file);
10772 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
10773 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
10774 struct extent_state *cached_state = NULL;
10775 struct extent_map *em = NULL;
10776 struct btrfs_device *device = NULL;
10777 struct btrfs_swap_info bsi = {
10778 .lowest_ppage = (sector_t)-1ULL,
10779 };
10780 int ret = 0;
10781 u64 isize;
10782 u64 start;
10783
10784 /*
10785 * If the swap file was just created, make sure delalloc is done. If the
10786 * file changes again after this, the user is doing something stupid and
10787 * we don't really care.
10788 */
10789 ret = btrfs_wait_ordered_range(inode, 0, (u64)-1);
10790 if (ret)
10791 return ret;
10792
10793 /*
10794 * The inode is locked, so these flags won't change after we check them.
10795 */
10796 if (BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS) {
10797 btrfs_warn(fs_info, "swapfile must not be compressed");
10798 return -EINVAL;
10799 }
10800 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW)) {
10801 btrfs_warn(fs_info, "swapfile must not be copy-on-write");
10802 return -EINVAL;
10803 }
10804 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
10805 btrfs_warn(fs_info, "swapfile must not be checksummed");
10806 return -EINVAL;
10807 }
10808
10809 /*
10810 * Balance or device remove/replace/resize can move stuff around from
10811 * under us. The EXCL_OP flag makes sure they aren't running/won't run
10812 * concurrently while we are mapping the swap extents, and
10813 * fs_info->swapfile_pins prevents them from running while the swap file
10814 * is active and moving the extents. Note that this also prevents a
10815 * concurrent device add which isn't actually necessary, but it's not
10816 * really worth the trouble to allow it.
10817 */
10818 if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) {
10819 btrfs_warn(fs_info,
10820 "cannot activate swapfile while exclusive operation is running");
10821 return -EBUSY;
10822 }
10823 /*
10824 * Snapshots can create extents which require COW even if NODATACOW is
10825 * set. We use this counter to prevent snapshots. We must increment it
10826 * before walking the extents because we don't want a concurrent
10827 * snapshot to run after we've already checked the extents.
10828 */
10829 atomic_inc(&BTRFS_I(inode)->root->nr_swapfiles);
10830
10831 isize = ALIGN_DOWN(inode->i_size, fs_info->sectorsize);
10832
10833 lock_extent_bits(io_tree, 0, isize - 1, &cached_state);
10834 start = 0;
10835 while (start < isize) {
10836 u64 logical_block_start, physical_block_start;
10837 struct btrfs_block_group_cache *bg;
10838 u64 len = isize - start;
10839
10840 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len, 0);
10841 if (IS_ERR(em)) {
10842 ret = PTR_ERR(em);
10843 goto out;
10844 }
10845
10846 if (em->block_start == EXTENT_MAP_HOLE) {
10847 btrfs_warn(fs_info, "swapfile must not have holes");
10848 ret = -EINVAL;
10849 goto out;
10850 }
10851 if (em->block_start == EXTENT_MAP_INLINE) {
10852 /*
10853 * It's unlikely we'll ever actually find ourselves
10854 * here, as a file small enough to fit inline won't be
10855 * big enough to store more than the swap header, but in
10856 * case something changes in the future, let's catch it
10857 * here rather than later.
10858 */
10859 btrfs_warn(fs_info, "swapfile must not be inline");
10860 ret = -EINVAL;
10861 goto out;
10862 }
10863 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
10864 btrfs_warn(fs_info, "swapfile must not be compressed");
10865 ret = -EINVAL;
10866 goto out;
10867 }
10868
10869 logical_block_start = em->block_start + (start - em->start);
10870 len = min(len, em->len - (start - em->start));
10871 free_extent_map(em);
10872 em = NULL;
10873
10874 ret = can_nocow_extent(inode, start, &len, NULL, NULL, NULL);
10875 if (ret < 0) {
10876 goto out;
10877 } else if (ret) {
10878 ret = 0;
10879 } else {
10880 btrfs_warn(fs_info,
10881 "swapfile must not be copy-on-write");
10882 ret = -EINVAL;
10883 goto out;
10884 }
10885
10886 em = btrfs_get_chunk_map(fs_info, logical_block_start, len);
10887 if (IS_ERR(em)) {
10888 ret = PTR_ERR(em);
10889 goto out;
10890 }
10891
10892 if (em->map_lookup->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
10893 btrfs_warn(fs_info,
10894 "swapfile must have single data profile");
10895 ret = -EINVAL;
10896 goto out;
10897 }
10898
10899 if (device == NULL) {
10900 device = em->map_lookup->stripes[0].dev;
10901 ret = btrfs_add_swapfile_pin(inode, device, false);
10902 if (ret == 1)
10903 ret = 0;
10904 else if (ret)
10905 goto out;
10906 } else if (device != em->map_lookup->stripes[0].dev) {
10907 btrfs_warn(fs_info, "swapfile must be on one device");
10908 ret = -EINVAL;
10909 goto out;
10910 }
10911
10912 physical_block_start = (em->map_lookup->stripes[0].physical +
10913 (logical_block_start - em->start));
10914 len = min(len, em->len - (logical_block_start - em->start));
10915 free_extent_map(em);
10916 em = NULL;
10917
10918 bg = btrfs_lookup_block_group(fs_info, logical_block_start);
10919 if (!bg) {
10920 btrfs_warn(fs_info,
10921 "could not find block group containing swapfile");
10922 ret = -EINVAL;
10923 goto out;
10924 }
10925
10926 ret = btrfs_add_swapfile_pin(inode, bg, true);
10927 if (ret) {
10928 btrfs_put_block_group(bg);
10929 if (ret == 1)
10930 ret = 0;
10931 else
10932 goto out;
10933 }
10934
10935 if (bsi.block_len &&
10936 bsi.block_start + bsi.block_len == physical_block_start) {
10937 bsi.block_len += len;
10938 } else {
10939 if (bsi.block_len) {
10940 ret = btrfs_add_swap_extent(sis, &bsi);
10941 if (ret)
10942 goto out;
10943 }
10944 bsi.start = start;
10945 bsi.block_start = physical_block_start;
10946 bsi.block_len = len;
10947 }
10948
10949 start += len;
10950 }
10951
10952 if (bsi.block_len)
10953 ret = btrfs_add_swap_extent(sis, &bsi);
10954
10955out:
10956 if (!IS_ERR_OR_NULL(em))
10957 free_extent_map(em);
10958
10959 unlock_extent_cached(io_tree, 0, isize - 1, &cached_state);
10960
10961 if (ret)
10962 btrfs_swap_deactivate(file);
10963
10964 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
10965
10966 if (ret)
10967 return ret;
10968
10969 if (device)
10970 sis->bdev = device->bdev;
10971 *span = bsi.highest_ppage - bsi.lowest_ppage + 1;
10972 sis->max = bsi.nr_pages;
10973 sis->pages = bsi.nr_pages - 1;
10974 sis->highest_bit = bsi.nr_pages - 1;
10975 return bsi.nr_extents;
10976}
10977#else
10978static void btrfs_swap_deactivate(struct file *file)
10979{
10980}
10981
10982static int btrfs_swap_activate(struct swap_info_struct *sis, struct file *file,
10983 sector_t *span)
10984{
10985 return -EOPNOTSUPP;
10986}
10987#endif
10988
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010989static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -050010990 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -040010991 .lookup = btrfs_lookup,
10992 .create = btrfs_create,
10993 .unlink = btrfs_unlink,
10994 .link = btrfs_link,
10995 .mkdir = btrfs_mkdir,
10996 .rmdir = btrfs_rmdir,
Miklos Szeredi2773bf02016-09-27 11:03:58 +020010997 .rename = btrfs_rename2,
Chris Mason39279cc2007-06-12 06:35:45 -040010998 .symlink = btrfs_symlink,
10999 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -040011000 .mknod = btrfs_mknod,
Josef Bacik5103e942007-11-16 11:45:54 -050011001 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050011002 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020011003 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080011004 .set_acl = btrfs_set_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070011005 .update_time = btrfs_update_time,
Filipe Mananaef3b9af2014-04-27 20:40:45 +010011006 .tmpfile = btrfs_tmpfile,
Chris Mason39279cc2007-06-12 06:35:45 -040011007};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070011008static const struct inode_operations btrfs_dir_ro_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040011009 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -050011010 .permission = btrfs_permission,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070011011 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040011012};
Yan, Zheng76dda932009-09-21 16:00:26 -040011013
Alexey Dobriyan828c0952009-10-01 15:43:56 -070011014static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040011015 .llseek = generic_file_llseek,
11016 .read = generic_read_dir,
Omar Sandoval02dbfc92016-05-20 13:50:33 -070011017 .iterate_shared = btrfs_real_readdir,
Josef Bacik23b5ec72017-07-24 15:14:25 -040011018 .open = btrfs_opendir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -040011019 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040011020#ifdef CONFIG_COMPAT
Luke Dashjr4c63c242015-10-29 08:22:21 +000011021 .compat_ioctl = btrfs_compat_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040011022#endif
Sage Weil6bf13c02008-06-10 10:07:39 -040011023 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -040011024 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -040011025};
11026
David Sterba20e55062015-11-19 11:42:28 +010011027static const struct extent_io_ops btrfs_extent_io_ops = {
David Sterba4d53ddd2017-02-17 15:27:44 +010011028 /* mandatory callbacks */
Chris Mason065631f2008-02-20 12:07:25 -050011029 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -040011030 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
11031};
11032
Chris Mason35054392009-01-21 13:11:13 -050011033/*
11034 * btrfs doesn't support the bmap operation because swapfiles
11035 * use bmap to make a mapping of extents in the file. They assume
11036 * these extents won't change over the life of the file and they
11037 * use the bmap result to do IO directly to the drive.
11038 *
11039 * the btrfs bmap call would return logical addresses that aren't
11040 * suitable for IO and they also will change frequently as COW
11041 * operations happen. So, swapfile + btrfs == corruption.
11042 *
11043 * For now we're avoiding this by dropping bmap.
11044 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -070011045static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -040011046 .readpage = btrfs_readpage,
11047 .writepage = btrfs_writepage,
Chris Masonb293f02e2007-11-01 19:45:34 -040011048 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -050011049 .readpages = btrfs_readpages,
Chris Mason16432982008-04-10 10:23:21 -040011050 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -040011051 .invalidatepage = btrfs_invalidatepage,
11052 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -040011053 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +020011054 .error_remove_page = generic_error_remove_page,
Omar Sandovaled46ff32016-11-03 10:28:14 -070011055 .swap_activate = btrfs_swap_activate,
11056 .swap_deactivate = btrfs_swap_deactivate,
Chris Mason39279cc2007-06-12 06:35:45 -040011057};
11058
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070011059static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040011060 .getattr = btrfs_getattr,
11061 .setattr = btrfs_setattr,
Josef Bacik5103e942007-11-16 11:45:54 -050011062 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050011063 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -050011064 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020011065 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080011066 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040011067 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040011068};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070011069static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -040011070 .getattr = btrfs_getattr,
11071 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050011072 .permission = btrfs_permission,
Josef Bacik33268ea2008-07-24 12:16:36 -040011073 .listxattr = btrfs_listxattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020011074 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080011075 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040011076 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -040011077};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070011078static const struct inode_operations btrfs_symlink_inode_operations = {
Al Viro6b255392015-11-17 10:20:54 -050011079 .get_link = page_get_link,
Li Zefanf2095612010-11-19 02:05:24 +000011080 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -050011081 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050011082 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -050011083 .listxattr = btrfs_listxattr,
Josef Bacike41f9412012-03-26 09:46:47 -040011084 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040011085};
Yan, Zheng76dda932009-09-21 16:00:26 -040011086
Alexey Dobriyan82d339d2009-10-09 09:54:36 -040011087const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -040011088 .d_delete = btrfs_dentry_delete,
11089};