blob: 5509c41a4f437bde12de7f6f232b38294760a806 [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,
David Sterbafac07d22019-10-29 18:28:57 +01001217 unsigned long *nr_written)
Chris Mason771ed682008-11-06 22:02:51 -05001218{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001219 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masonec39f762019-07-10 12:28:17 -07001220 struct cgroup_subsys_state *blkcg_css = wbc_blkcg_css(wbc);
Nikolay Borisov97db1202019-03-12 17:20:24 +02001221 struct async_cow *ctx;
1222 struct async_chunk *async_chunk;
Chris Mason771ed682008-11-06 22:02:51 -05001223 unsigned long nr_pages;
1224 u64 cur_end;
Nikolay Borisov97db1202019-03-12 17:20:24 +02001225 u64 num_chunks = DIV_ROUND_UP(end - start, SZ_512K);
1226 int i;
1227 bool should_compress;
Nikolay Borisovb1c16ac2019-04-01 11:29:57 +03001228 unsigned nofs_flag;
David Sterbafac07d22019-10-29 18:28:57 +01001229 const unsigned int write_flags = wbc_to_write_flags(wbc);
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 /*
Filipe Mananade7999a2019-12-11 09:01:40 +00001482 * If the extent we got ends before our current offset,
1483 * skip to the next extent.
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001484 */
Filipe Mananade7999a2019-12-11 09:01:40 +00001485 if (extent_end <= cur_offset) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001486 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);
Chris Masona2135012008-06-25 16:01:30 -04001740
Wang Shilong47059d92014-07-03 18:22:07 +08001741 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW && !force_cow) {
Chris Masonc8b97812008-10-29 14:49:59 -04001742 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001743 page_started, 1, nr_written);
Wang Shilong47059d92014-07-03 18:22:07 +08001744 } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC && !force_cow) {
Yan Zhengd899e052008-10-30 14:25:28 -04001745 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001746 page_started, 0, nr_written);
Qu Wenruo42c16da2019-07-01 05:12:46 +00001747 } else if (!inode_can_compress(inode) ||
1748 !inode_need_compress(inode, start, end)) {
Nikolay Borisov74e91942019-07-17 16:18:16 +03001749 ret = cow_file_range(inode, locked_page, start, end,
Nikolay Borisov330a5822019-07-17 16:18:17 +03001750 page_started, nr_written, 1);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001751 } else {
1752 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1753 &BTRFS_I(inode)->runtime_flags);
Chris Masonec39f762019-07-10 12:28:17 -07001754 ret = cow_file_range_async(inode, wbc, locked_page, start, end,
David Sterbafac07d22019-10-29 18:28:57 +01001755 page_started, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001756 }
Qu Wenruo524272602017-03-08 10:25:52 +08001757 if (ret)
Nikolay Borisovd1051d62018-11-21 17:10:52 +02001758 btrfs_cleanup_ordered_extents(inode, locked_page, start,
1759 end - start + 1);
Chris Masonb888db22007-08-27 16:49:44 -04001760 return ret;
1761}
1762
Nikolay Borisovabbb55f2018-11-01 14:09:53 +02001763void btrfs_split_delalloc_extent(struct inode *inode,
1764 struct extent_state *orig, u64 split)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001765{
Josef Bacikdcab6a32015-02-11 15:08:59 -05001766 u64 size;
1767
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001768 /* not delalloc, ignore it */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001769 if (!(orig->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001770 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001771
Josef Bacikdcab6a32015-02-11 15:08:59 -05001772 size = orig->end - orig->start + 1;
1773 if (size > BTRFS_MAX_EXTENT_SIZE) {
David Sterba823bb202017-01-04 11:09:51 +01001774 u32 num_extents;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001775 u64 new_size;
1776
1777 /*
Nikolay Borisov5c848192018-11-01 14:09:52 +02001778 * See the explanation in btrfs_merge_delalloc_extent, the same
Josef Bacikba117212015-03-13 15:01:24 -04001779 * applies here, just in reverse.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001780 */
1781 new_size = orig->end - split + 1;
David Sterba823bb202017-01-04 11:09:51 +01001782 num_extents = count_max_extents(new_size);
Josef Bacikba117212015-03-13 15:01:24 -04001783 new_size = split - orig->start;
David Sterba823bb202017-01-04 11:09:51 +01001784 num_extents += count_max_extents(new_size);
1785 if (count_max_extents(size) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001786 return;
1787 }
1788
Josef Bacik9e0baf62011-07-15 15:16:44 +00001789 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04001790 btrfs_mod_outstanding_extents(BTRFS_I(inode), 1);
Josef Bacik9e0baf62011-07-15 15:16:44 +00001791 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001792}
1793
1794/*
Nikolay Borisov5c848192018-11-01 14:09:52 +02001795 * Handle merged delayed allocation extents so we can keep track of new extents
1796 * that are just merged onto old extents, such as when we are doing sequential
1797 * writes, so we can properly account for the metadata space we'll need.
Josef Bacik9ed74f22009-09-11 16:12:44 -04001798 */
Nikolay Borisov5c848192018-11-01 14:09:52 +02001799void btrfs_merge_delalloc_extent(struct inode *inode, struct extent_state *new,
1800 struct extent_state *other)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001801{
Josef Bacikdcab6a32015-02-11 15:08:59 -05001802 u64 new_size, old_size;
David Sterba823bb202017-01-04 11:09:51 +01001803 u32 num_extents;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001804
Josef Bacik9ed74f22009-09-11 16:12:44 -04001805 /* not delalloc, ignore it */
1806 if (!(other->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001807 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001808
Josef Bacik8461a3d2015-03-13 15:12:08 -04001809 if (new->start > other->start)
1810 new_size = new->end - other->start + 1;
1811 else
1812 new_size = other->end - new->start + 1;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001813
1814 /* we're not bigger than the max, unreserve the space and go */
1815 if (new_size <= BTRFS_MAX_EXTENT_SIZE) {
1816 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04001817 btrfs_mod_outstanding_extents(BTRFS_I(inode), -1);
Josef Bacikdcab6a32015-02-11 15:08:59 -05001818 spin_unlock(&BTRFS_I(inode)->lock);
1819 return;
1820 }
1821
1822 /*
Josef Bacikba117212015-03-13 15:01:24 -04001823 * We have to add up either side to figure out how many extents were
1824 * accounted for before we merged into one big extent. If the number of
1825 * extents we accounted for is <= the amount we need for the new range
1826 * then we can return, otherwise drop. Think of it like this
1827 *
1828 * [ 4k][MAX_SIZE]
1829 *
1830 * So we've grown the extent by a MAX_SIZE extent, this would mean we
1831 * need 2 outstanding extents, on one side we have 1 and the other side
1832 * we have 1 so they are == and we can return. But in this case
1833 *
1834 * [MAX_SIZE+4k][MAX_SIZE+4k]
1835 *
1836 * Each range on their own accounts for 2 extents, but merged together
1837 * they are only 3 extents worth of accounting, so we need to drop in
1838 * this case.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001839 */
Josef Bacikba117212015-03-13 15:01:24 -04001840 old_size = other->end - other->start + 1;
David Sterba823bb202017-01-04 11:09:51 +01001841 num_extents = count_max_extents(old_size);
Josef Bacikba117212015-03-13 15:01:24 -04001842 old_size = new->end - new->start + 1;
David Sterba823bb202017-01-04 11:09:51 +01001843 num_extents += count_max_extents(old_size);
1844 if (count_max_extents(new_size) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001845 return;
1846
Josef Bacik9e0baf62011-07-15 15:16:44 +00001847 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04001848 btrfs_mod_outstanding_extents(BTRFS_I(inode), -1);
Josef Bacik9e0baf62011-07-15 15:16:44 +00001849 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001850}
1851
Miao Xieeb73c1b2013-05-15 07:48:22 +00001852static void btrfs_add_delalloc_inodes(struct btrfs_root *root,
1853 struct inode *inode)
1854{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001855 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1856
Miao Xieeb73c1b2013-05-15 07:48:22 +00001857 spin_lock(&root->delalloc_lock);
1858 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1859 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1860 &root->delalloc_inodes);
1861 set_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1862 &BTRFS_I(inode)->runtime_flags);
1863 root->nr_delalloc_inodes++;
1864 if (root->nr_delalloc_inodes == 1) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001865 spin_lock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001866 BUG_ON(!list_empty(&root->delalloc_root));
1867 list_add_tail(&root->delalloc_root,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001868 &fs_info->delalloc_roots);
1869 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001870 }
1871 }
1872 spin_unlock(&root->delalloc_lock);
1873}
1874
Nikolay Borisov2b877332018-04-27 12:21:51 +03001875
1876void __btrfs_del_delalloc_inode(struct btrfs_root *root,
1877 struct btrfs_inode *inode)
Miao Xieeb73c1b2013-05-15 07:48:22 +00001878{
David Sterba3ffbd682018-06-29 10:56:42 +02001879 struct btrfs_fs_info *fs_info = root->fs_info;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001880
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001881 if (!list_empty(&inode->delalloc_inodes)) {
1882 list_del_init(&inode->delalloc_inodes);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001883 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001884 &inode->runtime_flags);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001885 root->nr_delalloc_inodes--;
1886 if (!root->nr_delalloc_inodes) {
Nikolay Borisov7c8a0d32018-04-27 12:21:52 +03001887 ASSERT(list_empty(&root->delalloc_inodes));
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001888 spin_lock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001889 BUG_ON(list_empty(&root->delalloc_root));
1890 list_del_init(&root->delalloc_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001891 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001892 }
1893 }
Nikolay Borisov2b877332018-04-27 12:21:51 +03001894}
1895
1896static void btrfs_del_delalloc_inode(struct btrfs_root *root,
1897 struct btrfs_inode *inode)
1898{
1899 spin_lock(&root->delalloc_lock);
1900 __btrfs_del_delalloc_inode(root, inode);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001901 spin_unlock(&root->delalloc_lock);
1902}
1903
Chris Masond352ac62008-09-29 15:18:18 -04001904/*
Nikolay Borisove06a1fc2018-11-01 14:09:50 +02001905 * Properly track delayed allocation bytes in the inode and to maintain the
1906 * list of inodes that have pending delalloc work to be done.
Chris Masond352ac62008-09-29 15:18:18 -04001907 */
Nikolay Borisove06a1fc2018-11-01 14:09:50 +02001908void btrfs_set_delalloc_extent(struct inode *inode, struct extent_state *state,
1909 unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001910{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001911 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1912
Wang Shilong47059d92014-07-03 18:22:07 +08001913 if ((*bits & EXTENT_DEFRAG) && !(*bits & EXTENT_DELALLOC))
1914 WARN_ON(1);
Chris Mason75eff682008-12-15 15:54:40 -05001915 /*
1916 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001917 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001918 * bit, which is only set or cleared with irqs on
1919 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001920 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001921 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001922 u64 len = state->end + 1 - state->start;
Josef Bacik8b62f872017-10-19 14:15:55 -04001923 u32 num_extents = count_max_extents(len);
Nikolay Borisov70ddc552017-02-20 13:50:35 +02001924 bool do_list = !btrfs_is_free_space_inode(BTRFS_I(inode));
Josef Bacik9ed74f22009-09-11 16:12:44 -04001925
Josef Bacik8b62f872017-10-19 14:15:55 -04001926 spin_lock(&BTRFS_I(inode)->lock);
1927 btrfs_mod_outstanding_extents(BTRFS_I(inode), num_extents);
1928 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik287a0ab2010-03-19 18:07:23 +00001929
Josef Bacik6a3891c2015-03-16 17:38:52 -04001930 /* For sanity tests */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001931 if (btrfs_is_testing(fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04001932 return;
1933
Nikolay Borisov104b4e52017-06-20 21:01:20 +03001934 percpu_counter_add_batch(&fs_info->delalloc_bytes, len,
1935 fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001936 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001937 BTRFS_I(inode)->delalloc_bytes += len;
Wang Shilong47059d92014-07-03 18:22:07 +08001938 if (*bits & EXTENT_DEFRAG)
1939 BTRFS_I(inode)->defrag_bytes += len;
Miao Xiedf0af1a2013-01-29 10:11:59 +00001940 if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00001941 &BTRFS_I(inode)->runtime_flags))
1942 btrfs_add_delalloc_inodes(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001943 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001944 }
Filipe Mananaa7e3b972017-04-03 10:45:46 +01001945
1946 if (!(state->state & EXTENT_DELALLOC_NEW) &&
1947 (*bits & EXTENT_DELALLOC_NEW)) {
1948 spin_lock(&BTRFS_I(inode)->lock);
1949 BTRFS_I(inode)->new_delalloc_bytes += state->end + 1 -
1950 state->start;
1951 spin_unlock(&BTRFS_I(inode)->lock);
1952 }
Chris Mason291d6732008-01-29 15:55:23 -05001953}
1954
Chris Masond352ac62008-09-29 15:18:18 -04001955/*
Nikolay Borisova36bb5f2018-11-01 14:09:51 +02001956 * Once a range is no longer delalloc this function ensures that proper
1957 * accounting happens.
Chris Masond352ac62008-09-29 15:18:18 -04001958 */
Nikolay Borisova36bb5f2018-11-01 14:09:51 +02001959void btrfs_clear_delalloc_extent(struct inode *vfs_inode,
1960 struct extent_state *state, unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001961{
Nikolay Borisova36bb5f2018-11-01 14:09:51 +02001962 struct btrfs_inode *inode = BTRFS_I(vfs_inode);
1963 struct btrfs_fs_info *fs_info = btrfs_sb(vfs_inode->i_sb);
Wang Shilong47059d92014-07-03 18:22:07 +08001964 u64 len = state->end + 1 - state->start;
David Sterba823bb202017-01-04 11:09:51 +01001965 u32 num_extents = count_max_extents(len);
Wang Shilong47059d92014-07-03 18:22:07 +08001966
Filipe Manana4a4b9642017-07-27 19:52:55 +01001967 if ((state->state & EXTENT_DEFRAG) && (*bits & EXTENT_DEFRAG)) {
1968 spin_lock(&inode->lock);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001969 inode->defrag_bytes -= len;
Filipe Manana4a4b9642017-07-27 19:52:55 +01001970 spin_unlock(&inode->lock);
1971 }
Wang Shilong47059d92014-07-03 18:22:07 +08001972
Chris Mason75eff682008-12-15 15:54:40 -05001973 /*
1974 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001975 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001976 * bit, which is only set or cleared with irqs on
1977 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001978 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001979 struct btrfs_root *root = inode->root;
Liu Bo83eea1f2012-07-10 05:28:39 -06001980 bool do_list = !btrfs_is_free_space_inode(inode);
Chris Masonbcbfce82008-04-22 13:26:47 -04001981
Josef Bacik8b62f872017-10-19 14:15:55 -04001982 spin_lock(&inode->lock);
1983 btrfs_mod_outstanding_extents(inode, -num_extents);
1984 spin_unlock(&inode->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001985
Josef Bacikb6d08f02013-09-27 14:57:43 -04001986 /*
1987 * We don't reserve metadata space for space cache inodes so we
Andrea Gelmini52042d82018-11-28 12:05:13 +01001988 * don't need to call delalloc_release_metadata if there is an
Josef Bacikb6d08f02013-09-27 14:57:43 -04001989 * error.
1990 */
Filipe Mananaa315e682017-03-06 23:04:20 +00001991 if (*bits & EXTENT_CLEAR_META_RESV &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001992 root != fs_info->tree_root)
Qu Wenruo43b18592017-12-12 15:34:32 +08001993 btrfs_delalloc_release_metadata(inode, len, false);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001994
Josef Bacik6a3891c2015-03-16 17:38:52 -04001995 /* For sanity tests. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001996 if (btrfs_is_testing(fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04001997 return;
1998
Filipe Mananaa315e682017-03-06 23:04:20 +00001999 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID &&
2000 do_list && !(state->state & EXTENT_NORESERVE) &&
2001 (*bits & EXTENT_CLEAR_DATA_RESV))
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02002002 btrfs_free_reserved_data_space_noquota(
2003 &inode->vfs_inode,
Qu Wenruo51773be2015-10-08 18:19:37 +08002004 state->start, len);
Josef Bacik9ed74f22009-09-11 16:12:44 -04002005
Nikolay Borisov104b4e52017-06-20 21:01:20 +03002006 percpu_counter_add_batch(&fs_info->delalloc_bytes, -len,
2007 fs_info->delalloc_batch);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02002008 spin_lock(&inode->lock);
2009 inode->delalloc_bytes -= len;
2010 if (do_list && inode->delalloc_bytes == 0 &&
Miao Xiedf0af1a2013-01-29 10:11:59 +00002011 test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02002012 &inode->runtime_flags))
Miao Xieeb73c1b2013-05-15 07:48:22 +00002013 btrfs_del_delalloc_inode(root, inode);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02002014 spin_unlock(&inode->lock);
Chris Mason291d6732008-01-29 15:55:23 -05002015 }
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002016
2017 if ((state->state & EXTENT_DELALLOC_NEW) &&
2018 (*bits & EXTENT_DELALLOC_NEW)) {
2019 spin_lock(&inode->lock);
2020 ASSERT(inode->new_delalloc_bytes >= len);
2021 inode->new_delalloc_bytes -= len;
2022 spin_unlock(&inode->lock);
2023 }
Chris Mason291d6732008-01-29 15:55:23 -05002024}
2025
Chris Masond352ac62008-09-29 15:18:18 -04002026/*
Nikolay Borisovda12fe52018-11-27 20:57:58 +02002027 * btrfs_bio_fits_in_stripe - Checks whether the size of the given bio will fit
2028 * in a chunk's stripe. This function ensures that bios do not span a
2029 * stripe/chunk
Liu Bo6f034ec2016-06-22 18:31:49 -07002030 *
Nikolay Borisovda12fe52018-11-27 20:57:58 +02002031 * @page - The page we are about to add to the bio
2032 * @size - size we want to add to the bio
2033 * @bio - bio we want to ensure is smaller than a stripe
2034 * @bio_flags - flags of the bio
2035 *
2036 * return 1 if page cannot be added to the bio
2037 * return 0 if page can be added to the bio
Liu Bo6f034ec2016-06-22 18:31:49 -07002038 * return error otherwise
Chris Masond352ac62008-09-29 15:18:18 -04002039 */
Nikolay Borisovda12fe52018-11-27 20:57:58 +02002040int btrfs_bio_fits_in_stripe(struct page *page, size_t size, struct bio *bio,
2041 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04002042{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002043 struct inode *inode = page->mapping->host;
2044 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Kent Overstreet4f024f32013-10-11 15:44:27 -07002045 u64 logical = (u64)bio->bi_iter.bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04002046 u64 length = 0;
2047 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04002048 int ret;
Nikolay Borisov89b798a2019-06-03 12:05:05 +03002049 struct btrfs_io_geometry geom;
Chris Mason239b14b2008-03-24 15:02:07 -04002050
Chris Mason771ed682008-11-06 22:02:51 -05002051 if (bio_flags & EXTENT_BIO_COMPRESSED)
2052 return 0;
2053
Kent Overstreet4f024f32013-10-11 15:44:27 -07002054 length = bio->bi_iter.bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04002055 map_length = length;
Nikolay Borisov89b798a2019-06-03 12:05:05 +03002056 ret = btrfs_get_io_geometry(fs_info, btrfs_op(bio), logical, map_length,
2057 &geom);
Liu Bo6f034ec2016-06-22 18:31:49 -07002058 if (ret < 0)
2059 return ret;
Nikolay Borisov89b798a2019-06-03 12:05:05 +03002060
2061 if (geom.len < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04002062 return 1;
Jeff Mahoney3444a972011-10-03 23:23:13 -04002063 return 0;
Chris Mason239b14b2008-03-24 15:02:07 -04002064}
2065
Chris Masond352ac62008-09-29 15:18:18 -04002066/*
2067 * in order to insert checksums into the metadata in large chunks,
2068 * we wait until bio submission time. All the pages in the bio are
2069 * checksummed and sums are attached onto the ordered extent record.
2070 *
2071 * At IO completion time the cums attached on the ordered extent record
2072 * are inserted into the btree
2073 */
David Sterbad0ee3932018-03-08 14:35:48 +01002074static blk_status_t btrfs_submit_bio_start(void *private_data, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04002075 u64 bio_offset)
Chris Mason065631f2008-02-20 12:07:25 -05002076{
Josef Bacikc6100a42017-05-05 11:57:13 -04002077 struct inode *inode = private_data;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002078 blk_status_t ret = 0;
Chris Masone0156402008-04-16 11:15:20 -04002079
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002080 ret = btrfs_csum_one_bio(inode, bio, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002081 BUG_ON(ret); /* -ENOMEM */
Chris Mason4a69a412008-11-06 22:03:00 -05002082 return 0;
2083}
Chris Masone0156402008-04-16 11:15:20 -04002084
Chris Mason4a69a412008-11-06 22:03:00 -05002085/*
Chris Masoncad321a2008-12-17 14:51:42 -05002086 * extent_io.c submission hook. This does the right thing for csum calculation
Liu Bo4c274bc2017-11-01 17:19:27 -06002087 * on write, or reading the csums from the tree before a read.
2088 *
2089 * Rules about async/sync submit,
2090 * a) read: sync submit
2091 *
2092 * b) write without checksum: sync submit
2093 *
2094 * c) write with checksum:
2095 * c-1) if bio is issued by fsync: sync submit
2096 * (sync_writers != 0)
2097 *
2098 * c-2) if root is reloc root: sync submit
2099 * (only in case of buffered IO)
2100 *
2101 * c-3) otherwise: async submit
Chris Masond352ac62008-09-29 15:18:18 -04002102 */
Nikolay Borisova56b1c72019-04-10 17:24:39 +03002103static blk_status_t btrfs_submit_bio_hook(struct inode *inode, struct bio *bio,
Nikolay Borisov50489a52019-04-10 19:46:04 +03002104 int mirror_num,
2105 unsigned long bio_flags)
2106
Chris Mason44b8bd72008-04-16 11:14:51 -04002107{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002108 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason44b8bd72008-04-16 11:14:51 -04002109 struct btrfs_root *root = BTRFS_I(inode)->root;
Chandan Rajendra0d51e282015-07-27 15:26:43 +05302110 enum btrfs_wq_endio_type metadata = BTRFS_WQ_ENDIO_DATA;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002111 blk_status_t ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04002112 int skip_sum;
Josef Bacikb812ce22012-11-16 13:56:32 -05002113 int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
Chris Mason44b8bd72008-04-16 11:14:51 -04002114
Christoph Hellwig6cbff002009-04-17 10:37:41 +02002115 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masoncad321a2008-12-17 14:51:42 -05002116
Nikolay Borisov70ddc552017-02-20 13:50:35 +02002117 if (btrfs_is_free_space_inode(BTRFS_I(inode)))
Chandan Rajendra0d51e282015-07-27 15:26:43 +05302118 metadata = BTRFS_WQ_ENDIO_FREE_SPACE;
Jeff Mahoney04173412011-10-03 23:23:12 -04002119
Mike Christie37226b22016-06-05 14:31:52 -05002120 if (bio_op(bio) != REQ_OP_WRITE) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002121 ret = btrfs_bio_wq_end_io(fs_info, bio, metadata);
Josef Bacik5fd02042012-05-02 14:00:54 -04002122 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01002123 goto out;
Josef Bacik5fd02042012-05-02 14:00:54 -04002124
Chris Masond20f7042008-12-08 16:58:54 -05002125 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Stefan Behrens61891922012-11-05 18:51:52 +01002126 ret = btrfs_submit_compressed_read(inode, bio,
2127 mirror_num,
2128 bio_flags);
2129 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00002130 } else if (!skip_sum) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002131 ret = btrfs_lookup_bio_sums(inode, bio, NULL);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00002132 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01002133 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00002134 }
Chris Mason4d1b5fb2008-08-20 09:44:52 -04002135 goto mapit;
Josef Bacikb812ce22012-11-16 13:56:32 -05002136 } else if (async && !skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05002137 /* csum items have already been cloned */
2138 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
2139 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04002140 /* we're doing a write, do the async checksumming */
Josef Bacikc6100a42017-05-05 11:57:13 -04002141 ret = btrfs_wq_submit_bio(fs_info, bio, mirror_num, bio_flags,
Nikolay Borisove7681162019-04-10 17:24:41 +03002142 0, inode, btrfs_submit_bio_start);
Stefan Behrens61891922012-11-05 18:51:52 +01002143 goto out;
Josef Bacikb812ce22012-11-16 13:56:32 -05002144 } else if (!skip_sum) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002145 ret = btrfs_csum_one_bio(inode, bio, 0, 0);
Josef Bacikb812ce22012-11-16 13:56:32 -05002146 if (ret)
2147 goto out;
Chris Mason19b9bdb2008-10-30 14:23:13 -04002148 }
2149
Chris Mason0b86a832008-03-24 15:01:56 -04002150mapit:
Chris Mason08635ba2019-07-10 12:28:14 -07002151 ret = btrfs_map_bio(fs_info, bio, mirror_num);
Stefan Behrens61891922012-11-05 18:51:52 +01002152
2153out:
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002154 if (ret) {
2155 bio->bi_status = ret;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002156 bio_endio(bio);
2157 }
Stefan Behrens61891922012-11-05 18:51:52 +01002158 return ret;
Chris Mason065631f2008-02-20 12:07:25 -05002159}
Chris Mason6885f302008-02-20 16:11:05 -05002160
Chris Masond352ac62008-09-29 15:18:18 -04002161/*
2162 * given a list of ordered sums record them in the inode. This happens
2163 * at IO completion time based on sums calculated at bio submission time.
2164 */
Chris Masonba1da2f2008-07-17 12:54:15 -04002165static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
David Sterbadf9f6282017-02-10 19:35:37 +01002166 struct inode *inode, struct list_head *list)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002167{
Chris Masone6dcd2d2008-07-17 12:53:50 -04002168 struct btrfs_ordered_sum *sum;
Nikolay Borisovac01f262018-01-08 10:59:43 +02002169 int ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002170
Qinghuang Fengc6e30872009-01-21 10:59:08 -05002171 list_for_each_entry(sum, list, list) {
David Sterba7c2871a2017-11-08 01:07:43 +01002172 trans->adding_csums = true;
Nikolay Borisovac01f262018-01-08 10:59:43 +02002173 ret = btrfs_csum_file_blocks(trans,
Chris Masond20f7042008-12-08 16:58:54 -05002174 BTRFS_I(inode)->root->fs_info->csum_root, sum);
David Sterba7c2871a2017-11-08 01:07:43 +01002175 trans->adding_csums = false;
Nikolay Borisovac01f262018-01-08 10:59:43 +02002176 if (ret)
2177 return ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002178 }
2179 return 0;
2180}
2181
Josef Bacik2ac55d42010-02-03 19:33:23 +00002182int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
Filipe Mananae3b8a482017-11-04 00:16:59 +00002183 unsigned int extra_bits,
Nikolay Borisov330a5822019-07-17 16:18:17 +03002184 struct extent_state **cached_state)
Chris Masonea8c2812008-08-04 23:17:27 -04002185{
Johannes Thumshirnfdb1e122018-12-05 15:23:04 +01002186 WARN_ON(PAGE_ALIGNED(end));
Chris Masonea8c2812008-08-04 23:17:27 -04002187 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
Filipe Mananae3b8a482017-11-04 00:16:59 +00002188 extra_bits, cached_state);
Chris Masonea8c2812008-08-04 23:17:27 -04002189}
2190
Chris Masond352ac62008-09-29 15:18:18 -04002191/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04002192struct btrfs_writepage_fixup {
2193 struct page *page;
2194 struct btrfs_work work;
2195};
2196
Christoph Hellwigb2950862008-12-02 09:54:17 -05002197static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04002198{
2199 struct btrfs_writepage_fixup *fixup;
2200 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002201 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08002202 struct extent_changeset *data_reserved = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04002203 struct page *page;
2204 struct inode *inode;
2205 u64 page_start;
2206 u64 page_end;
Jeff Mahoney87826df2012-02-15 16:23:57 +01002207 int ret;
Chris Mason247e7432008-07-17 12:53:51 -04002208
2209 fixup = container_of(work, struct btrfs_writepage_fixup, work);
2210 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -04002211again:
Chris Mason247e7432008-07-17 12:53:51 -04002212 lock_page(page);
2213 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
2214 ClearPageChecked(page);
2215 goto out_page;
2216 }
2217
2218 inode = page->mapping->host;
2219 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002220 page_end = page_offset(page) + PAGE_SIZE - 1;
Chris Mason247e7432008-07-17 12:53:51 -04002221
David Sterbaff13db42015-12-03 14:30:40 +01002222 lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002223 &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04002224
2225 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04002226 if (PagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04002227 goto out;
Chris Mason4a096752008-07-21 10:29:44 -04002228
Nikolay Borisova776c6f2017-02-20 13:50:49 +02002229 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002230 PAGE_SIZE);
Chris Mason4a096752008-07-21 10:29:44 -04002231 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00002232 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
David Sterbae43bbe52017-12-12 21:43:52 +01002233 page_end, &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04002234 unlock_page(page);
2235 btrfs_start_ordered_extent(inode, ordered, 1);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002236 btrfs_put_ordered_extent(ordered);
Chris Mason4a096752008-07-21 10:29:44 -04002237 goto again;
2238 }
Chris Mason247e7432008-07-17 12:53:51 -04002239
Qu Wenruo364ecf32017-02-27 15:10:38 +08002240 ret = btrfs_delalloc_reserve_space(inode, &data_reserved, page_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002241 PAGE_SIZE);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002242 if (ret) {
2243 mapping_set_error(page->mapping, ret);
2244 end_extent_writepage(page, ret, page_start, page_end);
2245 ClearPageChecked(page);
2246 goto out;
2247 }
2248
Nikolay Borisovf3038ee2017-12-05 09:29:19 +02002249 ret = btrfs_set_extent_delalloc(inode, page_start, page_end, 0,
Nikolay Borisov330a5822019-07-17 16:18:17 +03002250 &cached_state);
Nikolay Borisovf3038ee2017-12-05 09:29:19 +02002251 if (ret) {
2252 mapping_set_error(page->mapping, ret);
2253 end_extent_writepage(page, ret, page_start, page_end);
2254 ClearPageChecked(page);
Filipe Manana53687002019-10-09 17:43:59 +01002255 goto out_reserved;
Nikolay Borisovf3038ee2017-12-05 09:29:19 +02002256 }
2257
Chris Mason247e7432008-07-17 12:53:51 -04002258 ClearPageChecked(page);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002259 set_page_dirty(page);
Filipe Manana53687002019-10-09 17:43:59 +01002260out_reserved:
Qu Wenruo8702ba92019-10-14 14:34:51 +08002261 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE);
Filipe Manana53687002019-10-09 17:43:59 +01002262 if (ret)
2263 btrfs_delalloc_release_space(inode, data_reserved, page_start,
2264 PAGE_SIZE, true);
Chris Mason247e7432008-07-17 12:53:51 -04002265out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00002266 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01002267 &cached_state);
Chris Mason247e7432008-07-17 12:53:51 -04002268out_page:
2269 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002270 put_page(page);
Miao Xieb897abe2011-01-26 16:19:22 +08002271 kfree(fixup);
Qu Wenruo364ecf32017-02-27 15:10:38 +08002272 extent_changeset_free(data_reserved);
Chris Mason247e7432008-07-17 12:53:51 -04002273}
2274
2275/*
2276 * There are a few paths in the higher layers of the kernel that directly
2277 * set the page dirty bit without asking the filesystem if it is a
2278 * good idea. This causes problems because we want to make sure COW
2279 * properly happens and the data=ordered rules are followed.
2280 *
Chris Masonc8b97812008-10-29 14:49:59 -04002281 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04002282 * hasn't been properly setup for IO. We kick off an async process
2283 * to fix it up. The async helper will wait for ordered extents, set
2284 * the delalloc bit and make it safe to write the page.
2285 */
Nikolay Borisovd75855b2018-11-01 14:09:47 +02002286int btrfs_writepage_cow_fixup(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04002287{
2288 struct inode *inode = page->mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002289 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason247e7432008-07-17 12:53:51 -04002290 struct btrfs_writepage_fixup *fixup;
Chris Mason247e7432008-07-17 12:53:51 -04002291
Chris Mason8b62b722009-09-02 16:53:46 -04002292 /* this page is properly in the ordered list */
2293 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04002294 return 0;
2295
2296 if (PageChecked(page))
2297 return -EAGAIN;
2298
2299 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
2300 if (!fixup)
2301 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04002302
Chris Mason247e7432008-07-17 12:53:51 -04002303 SetPageChecked(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002304 get_page(page);
Omar Sandovala0cac0e2019-09-16 11:30:57 -07002305 btrfs_init_work(&fixup->work, btrfs_writepage_fixup_worker, NULL, NULL);
Chris Mason247e7432008-07-17 12:53:51 -04002306 fixup->page = page;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002307 btrfs_queue_work(fs_info->fixup_workers, &fixup->work);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002308 return -EBUSY;
Chris Mason247e7432008-07-17 12:53:51 -04002309}
2310
Yan Zhengd899e052008-10-30 14:25:28 -04002311static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
2312 struct inode *inode, u64 file_pos,
2313 u64 disk_bytenr, u64 disk_num_bytes,
2314 u64 num_bytes, u64 ram_bytes,
2315 u8 compression, u8 encryption,
2316 u16 other_encoding, int extent_type)
2317{
2318 struct btrfs_root *root = BTRFS_I(inode)->root;
2319 struct btrfs_file_extent_item *fi;
2320 struct btrfs_path *path;
2321 struct extent_buffer *leaf;
2322 struct btrfs_key ins;
Qu Wenruoa12b8772017-02-27 15:10:37 +08002323 u64 qg_released;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002324 int extent_inserted = 0;
Yan Zhengd899e052008-10-30 14:25:28 -04002325 int ret;
2326
2327 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07002328 if (!path)
2329 return -ENOMEM;
Yan Zhengd899e052008-10-30 14:25:28 -04002330
Chris Masona1ed8352009-09-11 12:27:37 -04002331 /*
2332 * we may be replacing one extent in the tree with another.
2333 * The new extent is pinned in the extent map, and we don't want
2334 * to drop it from the cache until it is completely in the btree.
2335 *
2336 * So, tell btrfs_drop_extents to leave this extent in the cache.
2337 * the caller is expected to unpin it and allow it to be merged
2338 * with the others.
2339 */
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002340 ret = __btrfs_drop_extents(trans, root, inode, path, file_pos,
2341 file_pos + num_bytes, NULL, 0,
2342 1, sizeof(*fi), &extent_inserted);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002343 if (ret)
2344 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04002345
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002346 if (!extent_inserted) {
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002347 ins.objectid = btrfs_ino(BTRFS_I(inode));
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002348 ins.offset = file_pos;
2349 ins.type = BTRFS_EXTENT_DATA_KEY;
2350
2351 path->leave_spinning = 1;
2352 ret = btrfs_insert_empty_item(trans, root, path, &ins,
2353 sizeof(*fi));
2354 if (ret)
2355 goto out;
2356 }
Yan Zhengd899e052008-10-30 14:25:28 -04002357 leaf = path->nodes[0];
2358 fi = btrfs_item_ptr(leaf, path->slots[0],
2359 struct btrfs_file_extent_item);
2360 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
2361 btrfs_set_file_extent_type(leaf, fi, extent_type);
2362 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
2363 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
2364 btrfs_set_file_extent_offset(leaf, fi, 0);
2365 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
2366 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
2367 btrfs_set_file_extent_compression(leaf, fi, compression);
2368 btrfs_set_file_extent_encryption(leaf, fi, encryption);
2369 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
Chris Masonb9473432009-03-13 11:00:37 -04002370
Yan Zhengd899e052008-10-30 14:25:28 -04002371 btrfs_mark_buffer_dirty(leaf);
Josef Bacikce195332012-09-25 15:26:16 -04002372 btrfs_release_path(path);
Yan Zhengd899e052008-10-30 14:25:28 -04002373
2374 inode_add_bytes(inode, num_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -04002375
2376 ins.objectid = disk_bytenr;
2377 ins.offset = disk_num_bytes;
2378 ins.type = BTRFS_EXTENT_ITEM_KEY;
Qu Wenruoa12b8772017-02-27 15:10:37 +08002379
Qu Wenruo297d7502015-09-08 17:08:37 +08002380 /*
Qu Wenruo5846a3c2015-10-26 14:11:18 +08002381 * Release the reserved range from inode dirty range map, as it is
2382 * already moved into delayed_ref_head
Qu Wenruo297d7502015-09-08 17:08:37 +08002383 */
Qu Wenruoa12b8772017-02-27 15:10:37 +08002384 ret = btrfs_qgroup_release_data(inode, file_pos, ram_bytes);
2385 if (ret < 0)
2386 goto out;
2387 qg_released = ret;
Josef Bacik84f7d8e2017-09-29 15:43:49 -04002388 ret = btrfs_alloc_reserved_file_extent(trans, root,
2389 btrfs_ino(BTRFS_I(inode)),
2390 file_pos, qg_released, &ins);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002391out:
Yan Zhengd899e052008-10-30 14:25:28 -04002392 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04002393
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002394 return ret;
Yan Zhengd899e052008-10-30 14:25:28 -04002395}
2396
Liu Bo38c227d2013-01-29 03:18:40 +00002397/* snapshot-aware defrag */
2398struct sa_defrag_extent_backref {
2399 struct rb_node node;
2400 struct old_sa_defrag_extent *old;
2401 u64 root_id;
2402 u64 inum;
2403 u64 file_pos;
2404 u64 extent_offset;
2405 u64 num_bytes;
2406 u64 generation;
2407};
2408
2409struct old_sa_defrag_extent {
2410 struct list_head list;
2411 struct new_sa_defrag_extent *new;
2412
2413 u64 extent_offset;
2414 u64 bytenr;
2415 u64 offset;
2416 u64 len;
2417 int count;
2418};
2419
2420struct new_sa_defrag_extent {
2421 struct rb_root root;
2422 struct list_head head;
2423 struct btrfs_path *path;
2424 struct inode *inode;
2425 u64 file_pos;
2426 u64 len;
2427 u64 bytenr;
2428 u64 disk_len;
2429 u8 compress_type;
2430};
2431
2432static int backref_comp(struct sa_defrag_extent_backref *b1,
2433 struct sa_defrag_extent_backref *b2)
2434{
2435 if (b1->root_id < b2->root_id)
2436 return -1;
2437 else if (b1->root_id > b2->root_id)
2438 return 1;
2439
2440 if (b1->inum < b2->inum)
2441 return -1;
2442 else if (b1->inum > b2->inum)
2443 return 1;
2444
2445 if (b1->file_pos < b2->file_pos)
2446 return -1;
2447 else if (b1->file_pos > b2->file_pos)
2448 return 1;
2449
2450 /*
2451 * [------------------------------] ===> (a range of space)
2452 * |<--->| |<---->| =============> (fs/file tree A)
2453 * |<---------------------------->| ===> (fs/file tree B)
2454 *
2455 * A range of space can refer to two file extents in one tree while
2456 * refer to only one file extent in another tree.
2457 *
2458 * So we may process a disk offset more than one time(two extents in A)
2459 * and locate at the same extent(one extent in B), then insert two same
2460 * backrefs(both refer to the extent in B).
2461 */
2462 return 0;
2463}
2464
2465static void backref_insert(struct rb_root *root,
2466 struct sa_defrag_extent_backref *backref)
2467{
2468 struct rb_node **p = &root->rb_node;
2469 struct rb_node *parent = NULL;
2470 struct sa_defrag_extent_backref *entry;
2471 int ret;
2472
2473 while (*p) {
2474 parent = *p;
2475 entry = rb_entry(parent, struct sa_defrag_extent_backref, node);
2476
2477 ret = backref_comp(backref, entry);
2478 if (ret < 0)
2479 p = &(*p)->rb_left;
2480 else
2481 p = &(*p)->rb_right;
2482 }
2483
2484 rb_link_node(&backref->node, parent, p);
2485 rb_insert_color(&backref->node, root);
2486}
2487
2488/*
2489 * Note the backref might has changed, and in this case we just return 0.
2490 */
2491static noinline int record_one_backref(u64 inum, u64 offset, u64 root_id,
2492 void *ctx)
2493{
2494 struct btrfs_file_extent_item *extent;
Liu Bo38c227d2013-01-29 03:18:40 +00002495 struct old_sa_defrag_extent *old = ctx;
2496 struct new_sa_defrag_extent *new = old->new;
2497 struct btrfs_path *path = new->path;
2498 struct btrfs_key key;
2499 struct btrfs_root *root;
2500 struct sa_defrag_extent_backref *backref;
2501 struct extent_buffer *leaf;
2502 struct inode *inode = new->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002503 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002504 int slot;
2505 int ret;
2506 u64 extent_offset;
2507 u64 num_bytes;
2508
2509 if (BTRFS_I(inode)->root->root_key.objectid == root_id &&
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002510 inum == btrfs_ino(BTRFS_I(inode)))
Liu Bo38c227d2013-01-29 03:18:40 +00002511 return 0;
2512
2513 key.objectid = root_id;
2514 key.type = BTRFS_ROOT_ITEM_KEY;
2515 key.offset = (u64)-1;
2516
Liu Bo38c227d2013-01-29 03:18:40 +00002517 root = btrfs_read_fs_root_no_name(fs_info, &key);
2518 if (IS_ERR(root)) {
2519 if (PTR_ERR(root) == -ENOENT)
2520 return 0;
2521 WARN_ON(1);
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002522 btrfs_debug(fs_info, "inum=%llu, offset=%llu, root_id=%llu",
Liu Bo38c227d2013-01-29 03:18:40 +00002523 inum, offset, root_id);
2524 return PTR_ERR(root);
2525 }
2526
2527 key.objectid = inum;
2528 key.type = BTRFS_EXTENT_DATA_KEY;
2529 if (offset > (u64)-1 << 32)
2530 key.offset = 0;
2531 else
2532 key.offset = offset;
2533
2534 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05302535 if (WARN_ON(ret < 0))
Liu Bo38c227d2013-01-29 03:18:40 +00002536 return ret;
Josef Bacik50f13192013-07-22 12:50:37 -04002537 ret = 0;
Liu Bo38c227d2013-01-29 03:18:40 +00002538
2539 while (1) {
2540 cond_resched();
2541
2542 leaf = path->nodes[0];
2543 slot = path->slots[0];
2544
2545 if (slot >= btrfs_header_nritems(leaf)) {
2546 ret = btrfs_next_leaf(root, path);
2547 if (ret < 0) {
2548 goto out;
2549 } else if (ret > 0) {
2550 ret = 0;
2551 goto out;
2552 }
2553 continue;
2554 }
2555
2556 path->slots[0]++;
2557
2558 btrfs_item_key_to_cpu(leaf, &key, slot);
2559
2560 if (key.objectid > inum)
2561 goto out;
2562
2563 if (key.objectid < inum || key.type != BTRFS_EXTENT_DATA_KEY)
2564 continue;
2565
2566 extent = btrfs_item_ptr(leaf, slot,
2567 struct btrfs_file_extent_item);
2568
2569 if (btrfs_file_extent_disk_bytenr(leaf, extent) != old->bytenr)
2570 continue;
2571
Liu Boe68afa42013-07-01 22:13:26 +08002572 /*
2573 * 'offset' refers to the exact key.offset,
2574 * NOT the 'offset' field in btrfs_extent_data_ref, ie.
2575 * (key.offset - extent_offset).
2576 */
2577 if (key.offset != offset)
Liu Bo38c227d2013-01-29 03:18:40 +00002578 continue;
2579
Liu Boe68afa42013-07-01 22:13:26 +08002580 extent_offset = btrfs_file_extent_offset(leaf, extent);
Liu Bo38c227d2013-01-29 03:18:40 +00002581 num_bytes = btrfs_file_extent_num_bytes(leaf, extent);
Liu Boe68afa42013-07-01 22:13:26 +08002582
Liu Bo38c227d2013-01-29 03:18:40 +00002583 if (extent_offset >= old->extent_offset + old->offset +
2584 old->len || extent_offset + num_bytes <=
2585 old->extent_offset + old->offset)
2586 continue;
Liu Bo38c227d2013-01-29 03:18:40 +00002587 break;
2588 }
2589
2590 backref = kmalloc(sizeof(*backref), GFP_NOFS);
2591 if (!backref) {
2592 ret = -ENOENT;
2593 goto out;
2594 }
2595
2596 backref->root_id = root_id;
2597 backref->inum = inum;
Liu Boe68afa42013-07-01 22:13:26 +08002598 backref->file_pos = offset;
Liu Bo38c227d2013-01-29 03:18:40 +00002599 backref->num_bytes = num_bytes;
2600 backref->extent_offset = extent_offset;
2601 backref->generation = btrfs_file_extent_generation(leaf, extent);
2602 backref->old = old;
2603 backref_insert(&new->root, backref);
2604 old->count++;
2605out:
2606 btrfs_release_path(path);
2607 WARN_ON(ret);
2608 return ret;
2609}
2610
2611static noinline bool record_extent_backrefs(struct btrfs_path *path,
2612 struct new_sa_defrag_extent *new)
2613{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002614 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002615 struct old_sa_defrag_extent *old, *tmp;
2616 int ret;
2617
2618 new->path = path;
2619
2620 list_for_each_entry_safe(old, tmp, &new->head, list) {
Liu Boe68afa42013-07-01 22:13:26 +08002621 ret = iterate_inodes_from_logical(old->bytenr +
2622 old->extent_offset, fs_info,
Liu Bo38c227d2013-01-29 03:18:40 +00002623 path, record_one_backref,
Zygo Blaxellc995ab32017-09-22 13:58:45 -04002624 old, false);
Josef Bacik4724b102013-11-05 11:11:40 -05002625 if (ret < 0 && ret != -ENOENT)
2626 return false;
Liu Bo38c227d2013-01-29 03:18:40 +00002627
2628 /* no backref to be processed for this extent */
2629 if (!old->count) {
2630 list_del(&old->list);
2631 kfree(old);
2632 }
2633 }
2634
2635 if (list_empty(&new->head))
2636 return false;
2637
2638 return true;
2639}
2640
2641static int relink_is_mergable(struct extent_buffer *leaf,
2642 struct btrfs_file_extent_item *fi,
Liu Bo116e0022013-08-02 16:30:40 +08002643 struct new_sa_defrag_extent *new)
Liu Bo38c227d2013-01-29 03:18:40 +00002644{
Liu Bo116e0022013-08-02 16:30:40 +08002645 if (btrfs_file_extent_disk_bytenr(leaf, fi) != new->bytenr)
Liu Bo38c227d2013-01-29 03:18:40 +00002646 return 0;
2647
2648 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG)
2649 return 0;
2650
Liu Bo116e0022013-08-02 16:30:40 +08002651 if (btrfs_file_extent_compression(leaf, fi) != new->compress_type)
2652 return 0;
2653
2654 if (btrfs_file_extent_encryption(leaf, fi) ||
Liu Bo38c227d2013-01-29 03:18:40 +00002655 btrfs_file_extent_other_encoding(leaf, fi))
2656 return 0;
2657
2658 return 1;
2659}
2660
2661/*
2662 * Note the backref might has changed, and in this case we just return 0.
2663 */
2664static noinline int relink_extent_backref(struct btrfs_path *path,
2665 struct sa_defrag_extent_backref *prev,
2666 struct sa_defrag_extent_backref *backref)
2667{
2668 struct btrfs_file_extent_item *extent;
2669 struct btrfs_file_extent_item *item;
2670 struct btrfs_ordered_extent *ordered;
2671 struct btrfs_trans_handle *trans;
Qu Wenruo82fa1132019-04-04 14:45:35 +08002672 struct btrfs_ref ref = { 0 };
Liu Bo38c227d2013-01-29 03:18:40 +00002673 struct btrfs_root *root;
2674 struct btrfs_key key;
2675 struct extent_buffer *leaf;
2676 struct old_sa_defrag_extent *old = backref->old;
2677 struct new_sa_defrag_extent *new = old->new;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002678 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002679 struct inode *inode;
2680 struct extent_state *cached = NULL;
2681 int ret = 0;
2682 u64 start;
2683 u64 len;
2684 u64 lock_start;
2685 u64 lock_end;
2686 bool merge = false;
2687 int index;
2688
2689 if (prev && prev->root_id == backref->root_id &&
2690 prev->inum == backref->inum &&
2691 prev->file_pos + prev->num_bytes == backref->file_pos)
2692 merge = true;
2693
2694 /* step 1: get root */
2695 key.objectid = backref->root_id;
2696 key.type = BTRFS_ROOT_ITEM_KEY;
2697 key.offset = (u64)-1;
2698
Liu Bo38c227d2013-01-29 03:18:40 +00002699 index = srcu_read_lock(&fs_info->subvol_srcu);
2700
2701 root = btrfs_read_fs_root_no_name(fs_info, &key);
2702 if (IS_ERR(root)) {
2703 srcu_read_unlock(&fs_info->subvol_srcu, index);
2704 if (PTR_ERR(root) == -ENOENT)
2705 return 0;
2706 return PTR_ERR(root);
2707 }
Liu Bo38c227d2013-01-29 03:18:40 +00002708
Wang Shilongbcbba5e2014-02-08 23:46:35 +08002709 if (btrfs_root_readonly(root)) {
2710 srcu_read_unlock(&fs_info->subvol_srcu, index);
2711 return 0;
2712 }
2713
Liu Bo38c227d2013-01-29 03:18:40 +00002714 /* step 2: get inode */
2715 key.objectid = backref->inum;
2716 key.type = BTRFS_INODE_ITEM_KEY;
2717 key.offset = 0;
2718
David Sterba4c66e0d2019-10-03 19:09:35 +02002719 inode = btrfs_iget(fs_info->sb, &key, root);
Liu Bo38c227d2013-01-29 03:18:40 +00002720 if (IS_ERR(inode)) {
2721 srcu_read_unlock(&fs_info->subvol_srcu, index);
2722 return 0;
2723 }
2724
2725 srcu_read_unlock(&fs_info->subvol_srcu, index);
2726
2727 /* step 3: relink backref */
2728 lock_start = backref->file_pos;
2729 lock_end = backref->file_pos + backref->num_bytes - 1;
2730 lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
David Sterbaff13db42015-12-03 14:30:40 +01002731 &cached);
Liu Bo38c227d2013-01-29 03:18:40 +00002732
2733 ordered = btrfs_lookup_first_ordered_extent(inode, lock_end);
2734 if (ordered) {
2735 btrfs_put_ordered_extent(ordered);
2736 goto out_unlock;
2737 }
2738
2739 trans = btrfs_join_transaction(root);
2740 if (IS_ERR(trans)) {
2741 ret = PTR_ERR(trans);
2742 goto out_unlock;
2743 }
2744
2745 key.objectid = backref->inum;
2746 key.type = BTRFS_EXTENT_DATA_KEY;
2747 key.offset = backref->file_pos;
2748
2749 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2750 if (ret < 0) {
2751 goto out_free_path;
2752 } else if (ret > 0) {
2753 ret = 0;
2754 goto out_free_path;
2755 }
2756
2757 extent = btrfs_item_ptr(path->nodes[0], path->slots[0],
2758 struct btrfs_file_extent_item);
2759
2760 if (btrfs_file_extent_generation(path->nodes[0], extent) !=
2761 backref->generation)
2762 goto out_free_path;
2763
2764 btrfs_release_path(path);
2765
2766 start = backref->file_pos;
2767 if (backref->extent_offset < old->extent_offset + old->offset)
2768 start += old->extent_offset + old->offset -
2769 backref->extent_offset;
2770
2771 len = min(backref->extent_offset + backref->num_bytes,
2772 old->extent_offset + old->offset + old->len);
2773 len -= max(backref->extent_offset, old->extent_offset + old->offset);
2774
2775 ret = btrfs_drop_extents(trans, root, inode, start,
2776 start + len, 1);
2777 if (ret)
2778 goto out_free_path;
2779again:
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002780 key.objectid = btrfs_ino(BTRFS_I(inode));
Liu Bo38c227d2013-01-29 03:18:40 +00002781 key.type = BTRFS_EXTENT_DATA_KEY;
2782 key.offset = start;
2783
Liu Boa09a0a72013-03-11 09:20:58 +00002784 path->leave_spinning = 1;
Liu Bo38c227d2013-01-29 03:18:40 +00002785 if (merge) {
2786 struct btrfs_file_extent_item *fi;
2787 u64 extent_len;
2788 struct btrfs_key found_key;
2789
Gui Hecheng3c9665d2014-01-23 13:41:09 +08002790 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
Liu Bo38c227d2013-01-29 03:18:40 +00002791 if (ret < 0)
2792 goto out_free_path;
2793
2794 path->slots[0]--;
2795 leaf = path->nodes[0];
2796 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2797
2798 fi = btrfs_item_ptr(leaf, path->slots[0],
2799 struct btrfs_file_extent_item);
2800 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
2801
Liu Bo116e0022013-08-02 16:30:40 +08002802 if (extent_len + found_key.offset == start &&
2803 relink_is_mergable(leaf, fi, new)) {
Liu Bo38c227d2013-01-29 03:18:40 +00002804 btrfs_set_file_extent_num_bytes(leaf, fi,
2805 extent_len + len);
2806 btrfs_mark_buffer_dirty(leaf);
2807 inode_add_bytes(inode, len);
2808
2809 ret = 1;
2810 goto out_free_path;
2811 } else {
2812 merge = false;
2813 btrfs_release_path(path);
2814 goto again;
2815 }
2816 }
2817
2818 ret = btrfs_insert_empty_item(trans, root, path, &key,
2819 sizeof(*extent));
2820 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002821 btrfs_abort_transaction(trans, ret);
Liu Bo38c227d2013-01-29 03:18:40 +00002822 goto out_free_path;
2823 }
2824
2825 leaf = path->nodes[0];
2826 item = btrfs_item_ptr(leaf, path->slots[0],
2827 struct btrfs_file_extent_item);
2828 btrfs_set_file_extent_disk_bytenr(leaf, item, new->bytenr);
2829 btrfs_set_file_extent_disk_num_bytes(leaf, item, new->disk_len);
2830 btrfs_set_file_extent_offset(leaf, item, start - new->file_pos);
2831 btrfs_set_file_extent_num_bytes(leaf, item, len);
2832 btrfs_set_file_extent_ram_bytes(leaf, item, new->len);
2833 btrfs_set_file_extent_generation(leaf, item, trans->transid);
2834 btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG);
2835 btrfs_set_file_extent_compression(leaf, item, new->compress_type);
2836 btrfs_set_file_extent_encryption(leaf, item, 0);
2837 btrfs_set_file_extent_other_encoding(leaf, item, 0);
2838
2839 btrfs_mark_buffer_dirty(leaf);
2840 inode_add_bytes(inode, len);
Liu Boa09a0a72013-03-11 09:20:58 +00002841 btrfs_release_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002842
Qu Wenruo82fa1132019-04-04 14:45:35 +08002843 btrfs_init_generic_ref(&ref, BTRFS_ADD_DELAYED_REF, new->bytenr,
2844 new->disk_len, 0);
2845 btrfs_init_data_ref(&ref, backref->root_id, backref->inum,
2846 new->file_pos); /* start - extent_offset */
2847 ret = btrfs_inc_extent_ref(trans, &ref);
Liu Bo38c227d2013-01-29 03:18:40 +00002848 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002849 btrfs_abort_transaction(trans, ret);
Liu Bo38c227d2013-01-29 03:18:40 +00002850 goto out_free_path;
2851 }
2852
2853 ret = 1;
2854out_free_path:
2855 btrfs_release_path(path);
Liu Boa09a0a72013-03-11 09:20:58 +00002856 path->leave_spinning = 0;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002857 btrfs_end_transaction(trans);
Liu Bo38c227d2013-01-29 03:18:40 +00002858out_unlock:
2859 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
David Sterbae43bbe52017-12-12 21:43:52 +01002860 &cached);
Liu Bo38c227d2013-01-29 03:18:40 +00002861 iput(inode);
2862 return ret;
2863}
2864
Liu Bo6f519562013-10-29 10:45:05 +08002865static void free_sa_defrag_extent(struct new_sa_defrag_extent *new)
2866{
2867 struct old_sa_defrag_extent *old, *tmp;
2868
2869 if (!new)
2870 return;
2871
2872 list_for_each_entry_safe(old, tmp, &new->head, list) {
Liu Bo6f519562013-10-29 10:45:05 +08002873 kfree(old);
2874 }
2875 kfree(new);
2876}
2877
Liu Bo38c227d2013-01-29 03:18:40 +00002878static void relink_file_extents(struct new_sa_defrag_extent *new)
2879{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002880 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002881 struct btrfs_path *path;
Liu Bo38c227d2013-01-29 03:18:40 +00002882 struct sa_defrag_extent_backref *backref;
2883 struct sa_defrag_extent_backref *prev = NULL;
Liu Bo38c227d2013-01-29 03:18:40 +00002884 struct rb_node *node;
2885 int ret;
2886
Liu Bo38c227d2013-01-29 03:18:40 +00002887 path = btrfs_alloc_path();
2888 if (!path)
2889 return;
2890
2891 if (!record_extent_backrefs(path, new)) {
2892 btrfs_free_path(path);
2893 goto out;
2894 }
2895 btrfs_release_path(path);
2896
2897 while (1) {
2898 node = rb_first(&new->root);
2899 if (!node)
2900 break;
2901 rb_erase(node, &new->root);
2902
2903 backref = rb_entry(node, struct sa_defrag_extent_backref, node);
2904
2905 ret = relink_extent_backref(path, prev, backref);
2906 WARN_ON(ret < 0);
2907
2908 kfree(prev);
2909
2910 if (ret == 1)
2911 prev = backref;
2912 else
2913 prev = NULL;
2914 cond_resched();
2915 }
2916 kfree(prev);
2917
2918 btrfs_free_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002919out:
Liu Bo6f519562013-10-29 10:45:05 +08002920 free_sa_defrag_extent(new);
2921
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002922 atomic_dec(&fs_info->defrag_running);
2923 wake_up(&fs_info->transaction_wait);
Liu Bo38c227d2013-01-29 03:18:40 +00002924}
2925
2926static struct new_sa_defrag_extent *
2927record_old_file_extents(struct inode *inode,
2928 struct btrfs_ordered_extent *ordered)
2929{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002930 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002931 struct btrfs_root *root = BTRFS_I(inode)->root;
2932 struct btrfs_path *path;
2933 struct btrfs_key key;
Liu Bo6f519562013-10-29 10:45:05 +08002934 struct old_sa_defrag_extent *old;
Liu Bo38c227d2013-01-29 03:18:40 +00002935 struct new_sa_defrag_extent *new;
2936 int ret;
2937
2938 new = kmalloc(sizeof(*new), GFP_NOFS);
2939 if (!new)
2940 return NULL;
2941
2942 new->inode = inode;
2943 new->file_pos = ordered->file_offset;
2944 new->len = ordered->len;
2945 new->bytenr = ordered->start;
2946 new->disk_len = ordered->disk_len;
2947 new->compress_type = ordered->compress_type;
2948 new->root = RB_ROOT;
2949 INIT_LIST_HEAD(&new->head);
2950
2951 path = btrfs_alloc_path();
2952 if (!path)
2953 goto out_kfree;
2954
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002955 key.objectid = btrfs_ino(BTRFS_I(inode));
Liu Bo38c227d2013-01-29 03:18:40 +00002956 key.type = BTRFS_EXTENT_DATA_KEY;
2957 key.offset = new->file_pos;
2958
2959 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2960 if (ret < 0)
2961 goto out_free_path;
2962 if (ret > 0 && path->slots[0] > 0)
2963 path->slots[0]--;
2964
2965 /* find out all the old extents for the file range */
2966 while (1) {
2967 struct btrfs_file_extent_item *extent;
2968 struct extent_buffer *l;
2969 int slot;
2970 u64 num_bytes;
2971 u64 offset;
2972 u64 end;
2973 u64 disk_bytenr;
2974 u64 extent_offset;
2975
2976 l = path->nodes[0];
2977 slot = path->slots[0];
2978
2979 if (slot >= btrfs_header_nritems(l)) {
2980 ret = btrfs_next_leaf(root, path);
2981 if (ret < 0)
Liu Bo6f519562013-10-29 10:45:05 +08002982 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002983 else if (ret > 0)
2984 break;
2985 continue;
2986 }
2987
2988 btrfs_item_key_to_cpu(l, &key, slot);
2989
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002990 if (key.objectid != btrfs_ino(BTRFS_I(inode)))
Liu Bo38c227d2013-01-29 03:18:40 +00002991 break;
2992 if (key.type != BTRFS_EXTENT_DATA_KEY)
2993 break;
2994 if (key.offset >= new->file_pos + new->len)
2995 break;
2996
2997 extent = btrfs_item_ptr(l, slot, struct btrfs_file_extent_item);
2998
2999 num_bytes = btrfs_file_extent_num_bytes(l, extent);
3000 if (key.offset + num_bytes < new->file_pos)
3001 goto next;
3002
3003 disk_bytenr = btrfs_file_extent_disk_bytenr(l, extent);
3004 if (!disk_bytenr)
3005 goto next;
3006
3007 extent_offset = btrfs_file_extent_offset(l, extent);
3008
3009 old = kmalloc(sizeof(*old), GFP_NOFS);
3010 if (!old)
Liu Bo6f519562013-10-29 10:45:05 +08003011 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00003012
3013 offset = max(new->file_pos, key.offset);
3014 end = min(new->file_pos + new->len, key.offset + num_bytes);
3015
3016 old->bytenr = disk_bytenr;
3017 old->extent_offset = extent_offset;
3018 old->offset = offset - key.offset;
3019 old->len = end - offset;
3020 old->new = new;
3021 old->count = 0;
3022 list_add_tail(&old->list, &new->head);
3023next:
3024 path->slots[0]++;
3025 cond_resched();
3026 }
3027
3028 btrfs_free_path(path);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003029 atomic_inc(&fs_info->defrag_running);
Liu Bo38c227d2013-01-29 03:18:40 +00003030
3031 return new;
3032
Liu Bo38c227d2013-01-29 03:18:40 +00003033out_free_path:
3034 btrfs_free_path(path);
3035out_kfree:
Liu Bo6f519562013-10-29 10:45:05 +08003036 free_sa_defrag_extent(new);
Liu Bo38c227d2013-01-29 03:18:40 +00003037 return NULL;
3038}
3039
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003040static void btrfs_release_delalloc_bytes(struct btrfs_fs_info *fs_info,
Miao Xiee570fd22014-06-19 10:42:50 +08003041 u64 start, u64 len)
3042{
David Sterba32da53862019-10-29 19:20:18 +01003043 struct btrfs_block_group *cache;
Miao Xiee570fd22014-06-19 10:42:50 +08003044
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003045 cache = btrfs_lookup_block_group(fs_info, start);
Miao Xiee570fd22014-06-19 10:42:50 +08003046 ASSERT(cache);
3047
3048 spin_lock(&cache->lock);
3049 cache->delalloc_bytes -= len;
3050 spin_unlock(&cache->lock);
3051
3052 btrfs_put_block_group(cache);
3053}
3054
Chris Masond352ac62008-09-29 15:18:18 -04003055/* as ordered data IO finishes, this gets called so we can finish
3056 * an ordered extent if the range of bytes in the file it covers are
3057 * fully written.
3058 */
Josef Bacik5fd02042012-05-02 14:00:54 -04003059static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
Chris Masone6dcd2d2008-07-17 12:53:50 -04003060{
Josef Bacik5fd02042012-05-02 14:00:54 -04003061 struct inode *inode = ordered_extent->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003062 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003063 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003064 struct btrfs_trans_handle *trans = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003065 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00003066 struct extent_state *cached_state = NULL;
Liu Bo38c227d2013-01-29 03:18:40 +00003067 struct new_sa_defrag_extent *new = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08003068 int compress_type = 0;
Josef Bacik77cef2e2013-08-29 13:57:21 -04003069 int ret = 0;
3070 u64 logical_len = ordered_extent->len;
Nikolay Borisov8d510122019-10-08 20:43:06 +03003071 bool freespace_inode;
Josef Bacik77cef2e2013-08-29 13:57:21 -04003072 bool truncated = false;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003073 bool range_locked = false;
3074 bool clear_new_delalloc_bytes = false;
Josef Bacik49940bd2018-10-11 15:54:21 -04003075 bool clear_reserved_extent = true;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003076
3077 if (!test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
3078 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags) &&
3079 !test_bit(BTRFS_ORDERED_DIRECT, &ordered_extent->flags))
3080 clear_new_delalloc_bytes = true;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003081
Nikolay Borisov8d510122019-10-08 20:43:06 +03003082 freespace_inode = btrfs_is_free_space_inode(BTRFS_I(inode));
Josef Bacik0cb59c92010-07-02 12:14:14 -04003083
Josef Bacik5fd02042012-05-02 14:00:54 -04003084 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
3085 ret = -EIO;
3086 goto out;
3087 }
3088
Nikolay Borisov7ab79562017-02-20 13:50:57 +02003089 btrfs_free_io_failure_record(BTRFS_I(inode),
3090 ordered_extent->file_offset,
3091 ordered_extent->file_offset +
3092 ordered_extent->len - 1);
Miao Xief6124962014-09-12 18:44:04 +08003093
Josef Bacik77cef2e2013-08-29 13:57:21 -04003094 if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) {
3095 truncated = true;
3096 logical_len = ordered_extent->truncated_len;
3097 /* Truncated the entire extent, don't bother adding */
3098 if (!logical_len)
3099 goto out;
3100 }
3101
Yan, Zhengc2167752009-11-12 09:34:21 +00003102 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003103 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
Qu Wenruo94ed9382015-09-08 17:25:56 +08003104
3105 /*
3106 * For mwrite(mmap + memset to write) case, we still reserve
3107 * space for NOCOW range.
3108 * As NOCOW won't cause a new delayed ref, just free the space
3109 */
Qu Wenruobc42bda2017-02-27 15:10:39 +08003110 btrfs_qgroup_free_data(inode, NULL, ordered_extent->file_offset,
Qu Wenruo94ed9382015-09-08 17:25:56 +08003111 ordered_extent->len);
Josef Bacik6c760c02012-11-09 10:53:21 -05003112 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
Nikolay Borisov8d510122019-10-08 20:43:06 +03003113 if (freespace_inode)
3114 trans = btrfs_join_transaction_spacecache(root);
Josef Bacik6c760c02012-11-09 10:53:21 -05003115 else
3116 trans = btrfs_join_transaction(root);
3117 if (IS_ERR(trans)) {
3118 ret = PTR_ERR(trans);
3119 trans = NULL;
3120 goto out;
Yan, Zhengc2167752009-11-12 09:34:21 +00003121 }
Josef Bacik69fe2d72017-10-19 14:15:57 -04003122 trans->block_rsv = &BTRFS_I(inode)->block_rsv;
Josef Bacik6c760c02012-11-09 10:53:21 -05003123 ret = btrfs_update_inode_fallback(trans, root, inode);
3124 if (ret) /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04003125 btrfs_abort_transaction(trans, ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00003126 goto out;
3127 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04003128
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003129 range_locked = true;
Josef Bacik2ac55d42010-02-03 19:33:23 +00003130 lock_extent_bits(io_tree, ordered_extent->file_offset,
3131 ordered_extent->file_offset + ordered_extent->len - 1,
David Sterbaff13db42015-12-03 14:30:40 +01003132 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003133
Liu Bo38c227d2013-01-29 03:18:40 +00003134 ret = test_range_bit(io_tree, ordered_extent->file_offset,
3135 ordered_extent->file_offset + ordered_extent->len - 1,
Liu Bo452e62b2017-05-26 17:44:23 -06003136 EXTENT_DEFRAG, 0, cached_state);
Liu Bo38c227d2013-01-29 03:18:40 +00003137 if (ret) {
3138 u64 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
Josef Bacik8101c8d2014-01-29 16:05:30 -05003139 if (0 && last_snapshot >= BTRFS_I(inode)->generation)
Liu Bo38c227d2013-01-29 03:18:40 +00003140 /* the inode is shared */
3141 new = record_old_file_extents(inode, ordered_extent);
3142
3143 clear_extent_bit(io_tree, ordered_extent->file_offset,
3144 ordered_extent->file_offset + ordered_extent->len - 1,
David Sterbaae0f1622017-10-31 16:37:52 +01003145 EXTENT_DEFRAG, 0, 0, &cached_state);
Liu Bo38c227d2013-01-29 03:18:40 +00003146 }
3147
Nikolay Borisov8d510122019-10-08 20:43:06 +03003148 if (freespace_inode)
3149 trans = btrfs_join_transaction_spacecache(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04003150 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003151 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003152 if (IS_ERR(trans)) {
3153 ret = PTR_ERR(trans);
3154 trans = NULL;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003155 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003156 }
Chris Masona79b7d42014-05-22 16:18:52 -07003157
Josef Bacik69fe2d72017-10-19 14:15:57 -04003158 trans->block_rsv = &BTRFS_I(inode)->block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00003159
Chris Masonc8b97812008-10-29 14:49:59 -04003160 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08003161 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04003162 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08003163 BUG_ON(compress_type);
Justin Maggardb430b772017-10-30 15:29:10 -07003164 btrfs_qgroup_free_data(inode, NULL, ordered_extent->file_offset,
3165 ordered_extent->len);
Nikolay Borisov7a6d7062017-02-20 13:50:48 +02003166 ret = btrfs_mark_extent_written(trans, BTRFS_I(inode),
Yan Zhengd899e052008-10-30 14:25:28 -04003167 ordered_extent->file_offset,
3168 ordered_extent->file_offset +
Josef Bacik77cef2e2013-08-29 13:57:21 -04003169 logical_len);
Yan Zhengd899e052008-10-30 14:25:28 -04003170 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003171 BUG_ON(root == fs_info->tree_root);
Yan Zhengd899e052008-10-30 14:25:28 -04003172 ret = insert_reserved_file_extent(trans, inode,
3173 ordered_extent->file_offset,
3174 ordered_extent->start,
3175 ordered_extent->disk_len,
Josef Bacik77cef2e2013-08-29 13:57:21 -04003176 logical_len, logical_len,
Li Zefan261507a02010-12-17 14:21:50 +08003177 compress_type, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04003178 BTRFS_FILE_EXTENT_REG);
Josef Bacik49940bd2018-10-11 15:54:21 -04003179 if (!ret) {
3180 clear_reserved_extent = false;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003181 btrfs_release_delalloc_bytes(fs_info,
Miao Xiee570fd22014-06-19 10:42:50 +08003182 ordered_extent->start,
3183 ordered_extent->disk_len);
Josef Bacik49940bd2018-10-11 15:54:21 -04003184 }
Yan Zhengd899e052008-10-30 14:25:28 -04003185 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04003186 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
3187 ordered_extent->file_offset, ordered_extent->len,
3188 trans->transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003189 if (ret < 0) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003190 btrfs_abort_transaction(trans, ret);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003191 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003192 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00003193
Nikolay Borisovac01f262018-01-08 10:59:43 +02003194 ret = add_pending_csums(trans, inode, &ordered_extent->list);
3195 if (ret) {
3196 btrfs_abort_transaction(trans, ret);
3197 goto out;
3198 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04003199
Josef Bacik6c760c02012-11-09 10:53:21 -05003200 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
3201 ret = btrfs_update_inode_fallback(trans, root, inode);
3202 if (ret) { /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04003203 btrfs_abort_transaction(trans, ret);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003204 goto out;
Josef Bacik1ef30be2011-04-05 19:25:36 -04003205 }
3206 ret = 0;
Yan, Zhengc2167752009-11-12 09:34:21 +00003207out:
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003208 if (range_locked || clear_new_delalloc_bytes) {
3209 unsigned int clear_bits = 0;
3210
3211 if (range_locked)
3212 clear_bits |= EXTENT_LOCKED;
3213 if (clear_new_delalloc_bytes)
3214 clear_bits |= EXTENT_DELALLOC_NEW;
3215 clear_extent_bit(&BTRFS_I(inode)->io_tree,
3216 ordered_extent->file_offset,
3217 ordered_extent->file_offset +
3218 ordered_extent->len - 1,
3219 clear_bits,
3220 (clear_bits & EXTENT_LOCKED) ? 1 : 0,
David Sterbaae0f1622017-10-31 16:37:52 +01003221 0, &cached_state);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003222 }
3223
Miao Xiea698d0752012-09-20 01:51:59 -06003224 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003225 btrfs_end_transaction(trans);
Josef Bacik0cb59c92010-07-02 12:14:14 -04003226
Josef Bacik77cef2e2013-08-29 13:57:21 -04003227 if (ret || truncated) {
3228 u64 start, end;
3229
3230 if (truncated)
3231 start = ordered_extent->file_offset + logical_len;
3232 else
3233 start = ordered_extent->file_offset;
3234 end = ordered_extent->file_offset + ordered_extent->len - 1;
David Sterbaf08dc362017-10-31 17:02:39 +01003235 clear_extent_uptodate(io_tree, start, end, NULL);
Josef Bacik77cef2e2013-08-29 13:57:21 -04003236
3237 /* Drop the cache for the part of the extent we didn't write. */
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02003238 btrfs_drop_extent_cache(BTRFS_I(inode), start, end, 0);
Josef Bacik5fd02042012-05-02 14:00:54 -04003239
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003240 /*
3241 * If the ordered extent had an IOERR or something else went
3242 * wrong we need to return the space for this ordered extent
Josef Bacik77cef2e2013-08-29 13:57:21 -04003243 * back to the allocator. We only free the extent in the
3244 * truncated case if we didn't write out the extent at all.
Josef Bacik49940bd2018-10-11 15:54:21 -04003245 *
3246 * If we made it past insert_reserved_file_extent before we
3247 * errored out then we don't need to do this as the accounting
3248 * has already been done.
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003249 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04003250 if ((ret || !logical_len) &&
Josef Bacik49940bd2018-10-11 15:54:21 -04003251 clear_reserved_extent &&
Josef Bacik77cef2e2013-08-29 13:57:21 -04003252 !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003253 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003254 btrfs_free_reserved_extent(fs_info,
3255 ordered_extent->start,
Miao Xiee570fd22014-06-19 10:42:50 +08003256 ordered_extent->disk_len, 1);
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003257 }
3258
3259
Josef Bacik5fd02042012-05-02 14:00:54 -04003260 /*
Liu Bo8bad3c02012-06-18 12:14:23 +08003261 * This needs to be done to make sure anybody waiting knows we are done
3262 * updating everything for this ordered extent.
Josef Bacik5fd02042012-05-02 14:00:54 -04003263 */
3264 btrfs_remove_ordered_extent(inode, ordered_extent);
3265
Liu Bo38c227d2013-01-29 03:18:40 +00003266 /* for snapshot-aware defrag */
Liu Bo6f519562013-10-29 10:45:05 +08003267 if (new) {
3268 if (ret) {
3269 free_sa_defrag_extent(new);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003270 atomic_dec(&fs_info->defrag_running);
Liu Bo6f519562013-10-29 10:45:05 +08003271 } else {
3272 relink_file_extents(new);
3273 }
3274 }
Liu Bo38c227d2013-01-29 03:18:40 +00003275
Chris Masone6dcd2d2008-07-17 12:53:50 -04003276 /* once for us */
3277 btrfs_put_ordered_extent(ordered_extent);
3278 /* once for the tree */
3279 btrfs_put_ordered_extent(ordered_extent);
3280
Josef Bacik5fd02042012-05-02 14:00:54 -04003281 return ret;
3282}
3283
3284static void finish_ordered_fn(struct btrfs_work *work)
3285{
3286 struct btrfs_ordered_extent *ordered_extent;
3287 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
3288 btrfs_finish_ordered_io(ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003289}
3290
Nikolay Borisovc6297322018-11-08 10:18:08 +02003291void btrfs_writepage_endio_finish_ordered(struct page *page, u64 start,
3292 u64 end, int uptodate)
Chris Mason211f90e2008-07-18 11:56:15 -04003293{
Josef Bacik5fd02042012-05-02 14:00:54 -04003294 struct inode *inode = page->mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003295 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik5fd02042012-05-02 14:00:54 -04003296 struct btrfs_ordered_extent *ordered_extent = NULL;
Liu Bo9e0af232014-08-15 23:36:53 +08003297 struct btrfs_workqueue *wq;
Josef Bacik5fd02042012-05-02 14:00:54 -04003298
liubo1abe9b82011-03-24 11:18:59 +00003299 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
3300
Chris Mason8b62b722009-09-02 16:53:46 -04003301 ClearPagePrivate2(page);
Josef Bacik5fd02042012-05-02 14:00:54 -04003302 if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
3303 end - start + 1, uptodate))
David Sterbac3988d62017-02-17 15:18:32 +01003304 return;
Josef Bacik5fd02042012-05-02 14:00:54 -04003305
Omar Sandovala0cac0e2019-09-16 11:30:57 -07003306 if (btrfs_is_free_space_inode(BTRFS_I(inode)))
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003307 wq = fs_info->endio_freespace_worker;
Omar Sandovala0cac0e2019-09-16 11:30:57 -07003308 else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003309 wq = fs_info->endio_write_workers;
Josef Bacik5fd02042012-05-02 14:00:54 -04003310
Omar Sandovala0cac0e2019-09-16 11:30:57 -07003311 btrfs_init_work(&ordered_extent->work, finish_ordered_fn, NULL, NULL);
Liu Bo9e0af232014-08-15 23:36:53 +08003312 btrfs_queue_work(wq, &ordered_extent->work);
Chris Mason211f90e2008-07-18 11:56:15 -04003313}
3314
Miao Xiedc380ae2014-09-12 18:43:55 +08003315static int __readpage_endio_check(struct inode *inode,
3316 struct btrfs_io_bio *io_bio,
3317 int icsum, struct page *page,
3318 int pgoff, u64 start, size_t len)
3319{
Johannes Thumshirnd5178572019-06-03 16:58:57 +02003320 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
3321 SHASH_DESC_ON_STACK(shash, fs_info->csum_shash);
Miao Xiedc380ae2014-09-12 18:43:55 +08003322 char *kaddr;
Johannes Thumshirnd5178572019-06-03 16:58:57 +02003323 u16 csum_size = btrfs_super_csum_size(fs_info->super_copy);
3324 u8 *csum_expected;
3325 u8 csum[BTRFS_CSUM_SIZE];
Miao Xiedc380ae2014-09-12 18:43:55 +08003326
Johannes Thumshirnd5178572019-06-03 16:58:57 +02003327 csum_expected = ((u8 *)io_bio->csum) + icsum * csum_size;
Miao Xiedc380ae2014-09-12 18:43:55 +08003328
3329 kaddr = kmap_atomic(page);
Johannes Thumshirnd5178572019-06-03 16:58:57 +02003330 shash->tfm = fs_info->csum_shash;
3331
3332 crypto_shash_init(shash);
3333 crypto_shash_update(shash, kaddr + pgoff, len);
3334 crypto_shash_final(shash, csum);
3335
3336 if (memcmp(csum, csum_expected, csum_size))
Miao Xiedc380ae2014-09-12 18:43:55 +08003337 goto zeroit;
3338
3339 kunmap_atomic(kaddr);
3340 return 0;
3341zeroit:
Johannes Thumshirnea41d6b2019-06-03 16:58:58 +02003342 btrfs_print_data_csum_error(BTRFS_I(inode), start, csum, csum_expected,
3343 io_bio->mirror_num);
Miao Xiedc380ae2014-09-12 18:43:55 +08003344 memset(kaddr + pgoff, 1, len);
3345 flush_dcache_page(page);
3346 kunmap_atomic(kaddr);
Miao Xiedc380ae2014-09-12 18:43:55 +08003347 return -EIO;
3348}
3349
Chris Masond352ac62008-09-29 15:18:18 -04003350/*
Chris Masond352ac62008-09-29 15:18:18 -04003351 * when reads are done, we need to check csums to verify the data is correct
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02003352 * if there's a match, we allow the bio to finish. If not, the code in
3353 * extent_io.c will try to find good copies for us.
Chris Masond352ac62008-09-29 15:18:18 -04003354 */
Miao Xiefacc8a222013-07-25 19:22:34 +08003355static int btrfs_readpage_end_io_hook(struct btrfs_io_bio *io_bio,
3356 u64 phy_offset, struct page *page,
3357 u64 start, u64 end, int mirror)
Chris Mason07157aa2007-08-30 08:50:51 -04003358{
Miao Xie4eee4fa2012-12-21 09:17:45 +00003359 size_t offset = start - page_offset(page);
Chris Mason07157aa2007-08-30 08:50:51 -04003360 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05003361 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masonff79f812007-10-15 16:22:25 -04003362 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -05003363
Chris Masond20f7042008-12-08 16:58:54 -05003364 if (PageChecked(page)) {
3365 ClearPageChecked(page);
Miao Xiedc380ae2014-09-12 18:43:55 +08003366 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003367 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003368
3369 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Miao Xiedc380ae2014-09-12 18:43:55 +08003370 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003371
Yan Zheng17d217f2008-12-12 10:03:38 -05003372 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04003373 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
David Sterba91166212016-04-26 23:54:39 +02003374 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM);
Yan Zheng17d217f2008-12-12 10:03:38 -05003375 return 0;
3376 }
3377
Miao Xiefacc8a222013-07-25 19:22:34 +08003378 phy_offset >>= inode->i_sb->s_blocksize_bits;
Miao Xiedc380ae2014-09-12 18:43:55 +08003379 return __readpage_endio_check(inode, io_bio, phy_offset, page, offset,
3380 start, (size_t)(end - start + 1));
Chris Mason07157aa2007-08-30 08:50:51 -04003381}
Chris Masonb888db22007-08-27 16:49:44 -04003382
Nikolay Borisovc1c3fac2018-01-16 09:31:58 +02003383/*
3384 * btrfs_add_delayed_iput - perform a delayed iput on @inode
3385 *
3386 * @inode: The inode we want to perform iput on
3387 *
3388 * This function uses the generic vfs_inode::i_count to track whether we should
3389 * just decrement it (in case it's > 1) or if this is the last iput then link
3390 * the inode to the delayed iput machinery. Delayed iputs are processed at
3391 * transaction commit time/superblock commit/cleaner kthread.
3392 */
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003393void btrfs_add_delayed_iput(struct inode *inode)
3394{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003395 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
David Sterba8089fe62015-11-19 14:15:51 +01003396 struct btrfs_inode *binode = BTRFS_I(inode);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003397
3398 if (atomic_add_unless(&inode->i_count, -1, 1))
3399 return;
3400
Josef Bacik034f7842018-12-03 11:06:52 -05003401 atomic_inc(&fs_info->nr_delayed_iputs);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003402 spin_lock(&fs_info->delayed_iput_lock);
Nikolay Borisovc1c3fac2018-01-16 09:31:58 +02003403 ASSERT(list_empty(&binode->delayed_iput));
3404 list_add_tail(&binode->delayed_iput, &fs_info->delayed_iputs);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003405 spin_unlock(&fs_info->delayed_iput_lock);
Josef Bacikfd340d02019-01-11 10:21:02 -05003406 if (!test_bit(BTRFS_FS_CLEANER_RUNNING, &fs_info->flags))
3407 wake_up_process(fs_info->cleaner_kthread);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003408}
3409
Josef Bacik63611e72019-06-18 10:59:18 -04003410static void run_delayed_iput_locked(struct btrfs_fs_info *fs_info,
3411 struct btrfs_inode *inode)
3412{
3413 list_del_init(&inode->delayed_iput);
3414 spin_unlock(&fs_info->delayed_iput_lock);
3415 iput(&inode->vfs_inode);
3416 if (atomic_dec_and_test(&fs_info->nr_delayed_iputs))
3417 wake_up(&fs_info->delayed_iputs_wait);
3418 spin_lock(&fs_info->delayed_iput_lock);
3419}
3420
3421static void btrfs_run_delayed_iput(struct btrfs_fs_info *fs_info,
3422 struct btrfs_inode *inode)
3423{
3424 if (!list_empty(&inode->delayed_iput)) {
3425 spin_lock(&fs_info->delayed_iput_lock);
3426 if (!list_empty(&inode->delayed_iput))
3427 run_delayed_iput_locked(fs_info, inode);
3428 spin_unlock(&fs_info->delayed_iput_lock);
3429 }
3430}
3431
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003432void btrfs_run_delayed_iputs(struct btrfs_fs_info *fs_info)
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003433{
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003434
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003435 spin_lock(&fs_info->delayed_iput_lock);
David Sterba8089fe62015-11-19 14:15:51 +01003436 while (!list_empty(&fs_info->delayed_iputs)) {
3437 struct btrfs_inode *inode;
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003438
David Sterba8089fe62015-11-19 14:15:51 +01003439 inode = list_first_entry(&fs_info->delayed_iputs,
3440 struct btrfs_inode, delayed_iput);
Josef Bacik63611e72019-06-18 10:59:18 -04003441 run_delayed_iput_locked(fs_info, inode);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003442 }
David Sterba8089fe62015-11-19 14:15:51 +01003443 spin_unlock(&fs_info->delayed_iput_lock);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003444}
3445
Josef Bacik034f7842018-12-03 11:06:52 -05003446/**
3447 * btrfs_wait_on_delayed_iputs - wait on the delayed iputs to be done running
3448 * @fs_info - the fs_info for this fs
3449 * @return - EINTR if we were killed, 0 if nothing's pending
3450 *
3451 * This will wait on any delayed iputs that are currently running with KILLABLE
3452 * set. Once they are all done running we will return, unless we are killed in
3453 * which case we return EINTR. This helps in user operations like fallocate etc
3454 * that might get blocked on the iputs.
3455 */
3456int btrfs_wait_on_delayed_iputs(struct btrfs_fs_info *fs_info)
3457{
3458 int ret = wait_event_killable(fs_info->delayed_iputs_wait,
3459 atomic_read(&fs_info->nr_delayed_iputs) == 0);
3460 if (ret)
3461 return -EINTR;
3462 return 0;
3463}
3464
Yan, Zhengd68fc572010-05-16 10:49:58 -04003465/*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003466 * This creates an orphan entry for the given inode in case something goes wrong
3467 * in the middle of an unlink.
Josef Bacik7b128762008-07-24 12:17:14 -04003468 */
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003469int btrfs_orphan_add(struct btrfs_trans_handle *trans,
Omar Sandoval27919062018-05-11 13:13:37 -07003470 struct btrfs_inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003471{
Yan, Zhengd68fc572010-05-16 10:49:58 -04003472 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003473
Omar Sandoval27919062018-05-11 13:13:37 -07003474 ret = btrfs_insert_orphan_item(trans, inode->root, btrfs_ino(inode));
3475 if (ret && ret != -EEXIST) {
3476 btrfs_abort_transaction(trans, ret);
3477 return ret;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003478 }
3479
Yan, Zhengd68fc572010-05-16 10:49:58 -04003480 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003481}
3482
3483/*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003484 * We have done the delete so we can go ahead and remove the orphan item for
3485 * this particular inode.
Josef Bacik7b128762008-07-24 12:17:14 -04003486 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00003487static int btrfs_orphan_del(struct btrfs_trans_handle *trans,
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003488 struct btrfs_inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003489{
Omar Sandoval27919062018-05-11 13:13:37 -07003490 return btrfs_del_orphan_item(trans, inode->root, btrfs_ino(inode));
Josef Bacik7b128762008-07-24 12:17:14 -04003491}
3492
3493/*
3494 * this cleans up any orphans that may be left on the list from the last use
3495 * of this root.
3496 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003497int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04003498{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003499 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik7b128762008-07-24 12:17:14 -04003500 struct btrfs_path *path;
3501 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04003502 struct btrfs_key key, found_key;
3503 struct btrfs_trans_handle *trans;
3504 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003505 u64 last_objectid = 0;
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003506 int ret = 0, nr_unlink = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003507
Yan, Zhengd68fc572010-05-16 10:49:58 -04003508 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003509 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00003510
3511 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003512 if (!path) {
3513 ret = -ENOMEM;
3514 goto out;
3515 }
David Sterbae4058b52015-11-27 16:31:35 +01003516 path->reada = READA_BACK;
Josef Bacik7b128762008-07-24 12:17:14 -04003517
3518 key.objectid = BTRFS_ORPHAN_OBJECTID;
David Sterba962a2982014-06-04 18:41:45 +02003519 key.type = BTRFS_ORPHAN_ITEM_KEY;
Josef Bacik7b128762008-07-24 12:17:14 -04003520 key.offset = (u64)-1;
3521
Josef Bacik7b128762008-07-24 12:17:14 -04003522 while (1) {
3523 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003524 if (ret < 0)
3525 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003526
3527 /*
3528 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003529 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04003530 * find the key and see if we have stuff that matches
3531 */
3532 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003533 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003534 if (path->slots[0] == 0)
3535 break;
3536 path->slots[0]--;
3537 }
3538
3539 /* pull out the item */
3540 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04003541 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3542
3543 /* make sure the item matches what we want */
3544 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
3545 break;
David Sterba962a2982014-06-04 18:41:45 +02003546 if (found_key.type != BTRFS_ORPHAN_ITEM_KEY)
Josef Bacik7b128762008-07-24 12:17:14 -04003547 break;
3548
3549 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02003550 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04003551
3552 /*
3553 * this is where we are basically btrfs_lookup, without the
3554 * crossing root thing. we store the inode number in the
3555 * offset of the orphan item.
3556 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003557
3558 if (found_key.offset == last_objectid) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003559 btrfs_err(fs_info,
3560 "Error removing orphan entry, stopping orphan cleanup");
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003561 ret = -EINVAL;
3562 goto out;
3563 }
3564
3565 last_objectid = found_key.offset;
3566
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003567 found_key.objectid = found_key.offset;
3568 found_key.type = BTRFS_INODE_ITEM_KEY;
3569 found_key.offset = 0;
David Sterba4c66e0d2019-10-03 19:09:35 +02003570 inode = btrfs_iget(fs_info->sb, &found_key, root);
Rusty Russell8c6ffba2013-07-15 11:20:32 +09303571 ret = PTR_ERR_OR_ZERO(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003572 if (ret && ret != -ENOENT)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003573 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003574
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003575 if (ret == -ENOENT && root == fs_info->tree_root) {
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003576 struct btrfs_root *dead_root;
3577 struct btrfs_fs_info *fs_info = root->fs_info;
3578 int is_dead_root = 0;
3579
3580 /*
3581 * this is an orphan in the tree root. Currently these
3582 * could come from 2 sources:
3583 * a) a snapshot deletion in progress
3584 * b) a free space cache inode
3585 * We need to distinguish those two, as the snapshot
3586 * orphan must not get deleted.
3587 * find_dead_roots already ran before us, so if this
3588 * is a snapshot deletion, we should find the root
3589 * in the dead_roots list
3590 */
3591 spin_lock(&fs_info->trans_lock);
3592 list_for_each_entry(dead_root, &fs_info->dead_roots,
3593 root_list) {
3594 if (dead_root->root_key.objectid ==
3595 found_key.objectid) {
3596 is_dead_root = 1;
3597 break;
3598 }
3599 }
3600 spin_unlock(&fs_info->trans_lock);
3601 if (is_dead_root) {
3602 /* prevent this orphan from being found again */
3603 key.offset = found_key.objectid - 1;
3604 continue;
3605 }
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003606
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003607 }
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003608
Josef Bacika8c9e572011-09-21 16:55:59 -04003609 /*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003610 * If we have an inode with links, there are a couple of
3611 * possibilities. Old kernels (before v3.12) used to create an
3612 * orphan item for truncate indicating that there were possibly
3613 * extent items past i_size that needed to be deleted. In v3.12,
3614 * truncate was changed to update i_size in sync with the extent
3615 * items, but the (useless) orphan item was still created. Since
3616 * v4.18, we don't create the orphan item for truncate at all.
3617 *
3618 * So, this item could mean that we need to do a truncate, but
3619 * only if this filesystem was last used on a pre-v3.12 kernel
3620 * and was not cleanly unmounted. The odds of that are quite
3621 * slim, and it's a pain to do the truncate now, so just delete
3622 * the orphan item.
3623 *
3624 * It's also possible that this orphan item was supposed to be
3625 * deleted but wasn't. The inode number may have been reused,
3626 * but either way, we can delete the orphan item.
Josef Bacika8c9e572011-09-21 16:55:59 -04003627 */
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003628 if (ret == -ENOENT || inode->i_nlink) {
3629 if (!ret)
3630 iput(inode);
Josef Bacika8c9e572011-09-21 16:55:59 -04003631 trans = btrfs_start_transaction(root, 1);
3632 if (IS_ERR(trans)) {
3633 ret = PTR_ERR(trans);
3634 goto out;
3635 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003636 btrfs_debug(fs_info, "auto deleting %Lu",
3637 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003638 ret = btrfs_del_orphan_item(trans, root,
3639 found_key.objectid);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003640 btrfs_end_transaction(trans);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003641 if (ret)
3642 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003643 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003644 }
Josef Bacik7b128762008-07-24 12:17:14 -04003645
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003646 nr_unlink++;
Josef Bacik7b128762008-07-24 12:17:14 -04003647
3648 /* this will do delete_inode and everything for us */
3649 iput(inode);
3650 }
Miao Xie3254c872011-11-10 20:45:05 -05003651 /* release the path since we're done with it */
3652 btrfs_release_path(path);
3653
Yan, Zhengd68fc572010-05-16 10:49:58 -04003654 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
3655
Omar Sandovala575cee2018-05-11 13:13:38 -07003656 if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003657 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003658 if (!IS_ERR(trans))
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003659 btrfs_end_transaction(trans);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003660 }
Josef Bacik7b128762008-07-24 12:17:14 -04003661
3662 if (nr_unlink)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003663 btrfs_debug(fs_info, "unlinked %d orphans", nr_unlink);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003664
3665out:
3666 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003667 btrfs_err(fs_info, "could not do orphan cleanup %d", ret);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003668 btrfs_free_path(path);
3669 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003670}
3671
Chris Masond352ac62008-09-29 15:18:18 -04003672/*
Chris Mason46a53cc2009-04-27 11:47:50 -04003673 * very simple check to peek ahead in the leaf looking for xattrs. If we
3674 * don't find any xattrs, we know there can't be any acls.
3675 *
3676 * slot is the slot the inode is in, objectid is the objectid of the inode
3677 */
3678static noinline int acls_after_inode_item(struct extent_buffer *leaf,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003679 int slot, u64 objectid,
3680 int *first_xattr_slot)
Chris Mason46a53cc2009-04-27 11:47:50 -04003681{
3682 u32 nritems = btrfs_header_nritems(leaf);
3683 struct btrfs_key found_key;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003684 static u64 xattr_access = 0;
3685 static u64 xattr_default = 0;
Chris Mason46a53cc2009-04-27 11:47:50 -04003686 int scanned = 0;
3687
Josef Bacikf23b5a52013-06-19 10:16:26 -04003688 if (!xattr_access) {
Andreas Gruenbacher97d79292015-12-02 14:44:35 +01003689 xattr_access = btrfs_name_hash(XATTR_NAME_POSIX_ACL_ACCESS,
3690 strlen(XATTR_NAME_POSIX_ACL_ACCESS));
3691 xattr_default = btrfs_name_hash(XATTR_NAME_POSIX_ACL_DEFAULT,
3692 strlen(XATTR_NAME_POSIX_ACL_DEFAULT));
Josef Bacikf23b5a52013-06-19 10:16:26 -04003693 }
3694
Chris Mason46a53cc2009-04-27 11:47:50 -04003695 slot++;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003696 *first_xattr_slot = -1;
Chris Mason46a53cc2009-04-27 11:47:50 -04003697 while (slot < nritems) {
3698 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3699
3700 /* we found a different objectid, there must not be acls */
3701 if (found_key.objectid != objectid)
3702 return 0;
3703
3704 /* we found an xattr, assume we've got an acl */
Josef Bacikf23b5a52013-06-19 10:16:26 -04003705 if (found_key.type == BTRFS_XATTR_ITEM_KEY) {
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003706 if (*first_xattr_slot == -1)
3707 *first_xattr_slot = slot;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003708 if (found_key.offset == xattr_access ||
3709 found_key.offset == xattr_default)
3710 return 1;
3711 }
Chris Mason46a53cc2009-04-27 11:47:50 -04003712
3713 /*
3714 * we found a key greater than an xattr key, there can't
3715 * be any acls later on
3716 */
3717 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
3718 return 0;
3719
3720 slot++;
3721 scanned++;
3722
3723 /*
3724 * it goes inode, inode backrefs, xattrs, extents,
3725 * so if there are a ton of hard links to an inode there can
3726 * be a lot of backrefs. Don't waste time searching too hard,
3727 * this is just an optimization
3728 */
3729 if (scanned >= 8)
3730 break;
3731 }
3732 /* we hit the end of the leaf before we found an xattr or
3733 * something larger than an xattr. We have to assume the inode
3734 * has acls
3735 */
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003736 if (*first_xattr_slot == -1)
3737 *first_xattr_slot = slot;
Chris Mason46a53cc2009-04-27 11:47:50 -04003738 return 1;
3739}
3740
3741/*
Chris Masond352ac62008-09-29 15:18:18 -04003742 * read an inode from the btree into the in-memory inode
3743 */
Filipe Manana4222ea72018-10-24 10:13:03 +01003744static int btrfs_read_locked_inode(struct inode *inode,
3745 struct btrfs_path *in_path)
Chris Mason39279cc2007-06-12 06:35:45 -04003746{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003747 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Filipe Manana4222ea72018-10-24 10:13:03 +01003748 struct btrfs_path *path = in_path;
Chris Mason5f39d392007-10-15 16:14:19 -04003749 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003750 struct btrfs_inode_item *inode_item;
3751 struct btrfs_root *root = BTRFS_I(inode)->root;
3752 struct btrfs_key location;
Miao Xie67de1172013-12-26 13:07:06 +08003753 unsigned long ptr;
Chris Mason46a53cc2009-04-27 11:47:50 -04003754 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04003755 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04003756 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003757 bool filled = false;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003758 int first_xattr_slot;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003759
3760 ret = btrfs_fill_inode(inode, &rdev);
3761 if (!ret)
3762 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04003763
Filipe Manana4222ea72018-10-24 10:13:03 +01003764 if (!path) {
3765 path = btrfs_alloc_path();
3766 if (!path)
3767 return -ENOMEM;
3768 }
Mark Fasheh1748f842011-07-12 11:25:31 -07003769
Chris Mason39279cc2007-06-12 06:35:45 -04003770 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05003771
Chris Mason39279cc2007-06-12 06:35:45 -04003772 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Filipe Manana67710892016-06-06 11:51:25 +01003773 if (ret) {
Filipe Manana4222ea72018-10-24 10:13:03 +01003774 if (path != in_path)
3775 btrfs_free_path(path);
Al Virof5b3a412018-07-29 23:04:51 +01003776 return ret;
Filipe Manana67710892016-06-06 11:51:25 +01003777 }
Chris Mason39279cc2007-06-12 06:35:45 -04003778
Chris Mason5f39d392007-10-15 16:14:19 -04003779 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00003780
3781 if (filled)
Miao Xie67de1172013-12-26 13:07:06 +08003782 goto cache_index;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003783
Chris Mason5f39d392007-10-15 16:14:19 -04003784 inode_item = btrfs_item_ptr(leaf, path->slots[0],
3785 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003786 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02003787 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08003788 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
3789 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02003790 btrfs_i_size_write(BTRFS_I(inode), btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04003791
David Sterbaa937b972014-12-12 17:39:12 +01003792 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->atime);
3793 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->atime);
Chris Mason5f39d392007-10-15 16:14:19 -04003794
David Sterbaa937b972014-12-12 17:39:12 +01003795 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->mtime);
3796 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->mtime);
Chris Mason5f39d392007-10-15 16:14:19 -04003797
David Sterbaa937b972014-12-12 17:39:12 +01003798 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->ctime);
3799 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->ctime);
Chris Mason5f39d392007-10-15 16:14:19 -04003800
chandan r9cc97d62012-07-04 12:48:07 +05303801 BTRFS_I(inode)->i_otime.tv_sec =
3802 btrfs_timespec_sec(leaf, &inode_item->otime);
3803 BTRFS_I(inode)->i_otime.tv_nsec =
3804 btrfs_timespec_nsec(leaf, &inode_item->otime);
Chris Mason5f39d392007-10-15 16:14:19 -04003805
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003806 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003807 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003808 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
3809
Jeff Laytonc7f88c42017-12-11 06:35:12 -05003810 inode_set_iversion_queried(inode,
3811 btrfs_inode_sequence(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003812 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04003813 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003814 rdev = btrfs_inode_rdev(leaf, inode_item);
3815
Josef Bacikaec74772008-07-24 12:12:38 -04003816 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05003817 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Miao Xie67de1172013-12-26 13:07:06 +08003818
3819cache_index:
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003820 /*
3821 * If we were modified in the current generation and evicted from memory
3822 * and then re-read we need to do a full sync since we don't have any
3823 * idea about which extents were modified before we were evicted from
3824 * cache.
3825 *
3826 * This is required for both inode re-read from disk and delayed inode
3827 * in delayed_nodes_tree.
3828 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003829 if (BTRFS_I(inode)->last_trans == fs_info->generation)
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003830 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3831 &BTRFS_I(inode)->runtime_flags);
3832
Filipe Mananabde6c242015-07-24 00:00:19 +01003833 /*
3834 * We don't persist the id of the transaction where an unlink operation
3835 * against the inode was last made. So here we assume the inode might
3836 * have been evicted, and therefore the exact value of last_unlink_trans
3837 * lost, and set it to last_trans to avoid metadata inconsistencies
3838 * between the inode and its parent if the inode is fsync'ed and the log
3839 * replayed. For example, in the scenario:
3840 *
3841 * touch mydir/foo
3842 * ln mydir/foo mydir/bar
3843 * sync
3844 * unlink mydir/bar
3845 * echo 2 > /proc/sys/vm/drop_caches # evicts inode
3846 * xfs_io -c fsync mydir/foo
3847 * <power failure>
3848 * mount fs, triggers fsync log replay
3849 *
3850 * We must make sure that when we fsync our inode foo we also log its
3851 * parent inode, otherwise after log replay the parent still has the
3852 * dentry with the "bar" name but our inode foo has a link count of 1
3853 * and doesn't have an inode ref with the name "bar" anymore.
3854 *
3855 * Setting last_unlink_trans to last_trans is a pessimistic approach,
Nicholas D Steeves01327612016-05-19 21:18:45 -04003856 * but it guarantees correctness at the expense of occasional full
Filipe Mananabde6c242015-07-24 00:00:19 +01003857 * transaction commits on fsync if our inode is a directory, or if our
3858 * inode is not a directory, logging its parent unnecessarily.
3859 */
3860 BTRFS_I(inode)->last_unlink_trans = BTRFS_I(inode)->last_trans;
3861
Miao Xie67de1172013-12-26 13:07:06 +08003862 path->slots[0]++;
3863 if (inode->i_nlink != 1 ||
3864 path->slots[0] >= btrfs_header_nritems(leaf))
3865 goto cache_acl;
3866
3867 btrfs_item_key_to_cpu(leaf, &location, path->slots[0]);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003868 if (location.objectid != btrfs_ino(BTRFS_I(inode)))
Miao Xie67de1172013-12-26 13:07:06 +08003869 goto cache_acl;
3870
3871 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3872 if (location.type == BTRFS_INODE_REF_KEY) {
3873 struct btrfs_inode_ref *ref;
3874
3875 ref = (struct btrfs_inode_ref *)ptr;
3876 BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref);
3877 } else if (location.type == BTRFS_INODE_EXTREF_KEY) {
3878 struct btrfs_inode_extref *extref;
3879
3880 extref = (struct btrfs_inode_extref *)ptr;
3881 BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf,
3882 extref);
3883 }
Miao Xie2f7e33d2011-06-23 07:27:13 +00003884cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04003885 /*
3886 * try to precache a NULL acl entry for files that don't have
3887 * any xattrs or acls
3888 */
Li Zefan33345d012011-04-20 10:31:50 +08003889 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
David Sterbaf85b7372017-01-20 14:54:07 +01003890 btrfs_ino(BTRFS_I(inode)), &first_xattr_slot);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003891 if (first_xattr_slot != -1) {
3892 path->slots[0] = first_xattr_slot;
3893 ret = btrfs_load_inode_props(inode, path);
3894 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003895 btrfs_err(fs_info,
David Sterba351fd352014-05-15 16:48:20 +02003896 "error loading props for ino %llu (root %llu): %d",
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003897 btrfs_ino(BTRFS_I(inode)),
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003898 root->root_key.objectid, ret);
3899 }
Filipe Manana4222ea72018-10-24 10:13:03 +01003900 if (path != in_path)
3901 btrfs_free_path(path);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003902
Al Viro72c04902009-06-24 16:58:48 -04003903 if (!maybe_acls)
3904 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04003905
Chris Mason39279cc2007-06-12 06:35:45 -04003906 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04003907 case S_IFREG:
3908 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masond1310b22008-01-24 16:13:08 -05003909 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04003910 inode->i_fop = &btrfs_file_operations;
3911 inode->i_op = &btrfs_file_inode_operations;
3912 break;
3913 case S_IFDIR:
3914 inode->i_fop = &btrfs_dir_file_operations;
Omar Sandoval67ade052017-01-25 17:06:38 -08003915 inode->i_op = &btrfs_dir_inode_operations;
Chris Mason39279cc2007-06-12 06:35:45 -04003916 break;
3917 case S_IFLNK:
3918 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -05003919 inode_nohighmem(inode);
Omar Sandoval4779cc02018-09-24 15:16:55 -07003920 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason39279cc2007-06-12 06:35:45 -04003921 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04003922 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05003923 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04003924 init_special_inode(inode, inode->i_mode, rdev);
3925 break;
Chris Mason39279cc2007-06-12 06:35:45 -04003926 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003927
David Sterba7b6a2212018-03-26 18:40:21 +02003928 btrfs_sync_inode_flags_to_i_flags(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003929 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003930}
3931
Chris Masond352ac62008-09-29 15:18:18 -04003932/*
3933 * given a leaf and an inode, copy the inode fields into the leaf
3934 */
Chris Masone02119d2008-09-05 16:13:11 -04003935static void fill_inode_item(struct btrfs_trans_handle *trans,
3936 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04003937 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04003938 struct inode *inode)
3939{
Liu Bo51fab692012-12-27 09:01:21 +00003940 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003941
David Sterbac82f8232019-08-09 17:48:21 +02003942 btrfs_init_map_token(&token, leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04003943
Liu Bo51fab692012-12-27 09:01:21 +00003944 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
3945 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
3946 btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size,
3947 &token);
3948 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3949 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003950
David Sterbaa937b972014-12-12 17:39:12 +01003951 btrfs_set_token_timespec_sec(leaf, &item->atime,
Liu Bo51fab692012-12-27 09:01:21 +00003952 inode->i_atime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003953 btrfs_set_token_timespec_nsec(leaf, &item->atime,
Liu Bo51fab692012-12-27 09:01:21 +00003954 inode->i_atime.tv_nsec, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003955
David Sterbaa937b972014-12-12 17:39:12 +01003956 btrfs_set_token_timespec_sec(leaf, &item->mtime,
Liu Bo51fab692012-12-27 09:01:21 +00003957 inode->i_mtime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003958 btrfs_set_token_timespec_nsec(leaf, &item->mtime,
Liu Bo51fab692012-12-27 09:01:21 +00003959 inode->i_mtime.tv_nsec, &token);
3960
David Sterbaa937b972014-12-12 17:39:12 +01003961 btrfs_set_token_timespec_sec(leaf, &item->ctime,
Liu Bo51fab692012-12-27 09:01:21 +00003962 inode->i_ctime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003963 btrfs_set_token_timespec_nsec(leaf, &item->ctime,
Liu Bo51fab692012-12-27 09:01:21 +00003964 inode->i_ctime.tv_nsec, &token);
3965
chandan r9cc97d62012-07-04 12:48:07 +05303966 btrfs_set_token_timespec_sec(leaf, &item->otime,
3967 BTRFS_I(inode)->i_otime.tv_sec, &token);
3968 btrfs_set_token_timespec_nsec(leaf, &item->otime,
3969 BTRFS_I(inode)->i_otime.tv_nsec, &token);
3970
Liu Bo51fab692012-12-27 09:01:21 +00003971 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3972 &token);
3973 btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation,
3974 &token);
Jeff Laytonc7f88c42017-12-11 06:35:12 -05003975 btrfs_set_token_inode_sequence(leaf, item, inode_peek_iversion(inode),
3976 &token);
Liu Bo51fab692012-12-27 09:01:21 +00003977 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
3978 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3979 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3980 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
Chris Mason39279cc2007-06-12 06:35:45 -04003981}
3982
Chris Masond352ac62008-09-29 15:18:18 -04003983/*
3984 * copy everything in the in-memory inode into the btree.
3985 */
Chris Mason21151332011-11-10 20:39:08 -05003986static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Chris Masond3977122009-01-05 21:25:51 -05003987 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003988{
3989 struct btrfs_inode_item *inode_item;
3990 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003991 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003992 int ret;
3993
3994 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08003995 if (!path)
3996 return -ENOMEM;
3997
Chris Masonb9473432009-03-13 11:00:37 -04003998 path->leave_spinning = 1;
Miao Xie16cdcec2011-04-22 18:12:22 +08003999 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
4000 1);
Chris Mason39279cc2007-06-12 06:35:45 -04004001 if (ret) {
4002 if (ret > 0)
4003 ret = -ENOENT;
4004 goto failed;
4005 }
4006
Chris Mason5f39d392007-10-15 16:14:19 -04004007 leaf = path->nodes[0];
4008 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08004009 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04004010
Chris Masone02119d2008-09-05 16:13:11 -04004011 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04004012 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04004013 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004014 ret = 0;
4015failed:
Chris Mason39279cc2007-06-12 06:35:45 -04004016 btrfs_free_path(path);
4017 return ret;
4018}
4019
Chris Masond352ac62008-09-29 15:18:18 -04004020/*
Chris Mason21151332011-11-10 20:39:08 -05004021 * copy everything in the in-memory inode into the btree.
4022 */
4023noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
4024 struct btrfs_root *root, struct inode *inode)
4025{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004026 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason21151332011-11-10 20:39:08 -05004027 int ret;
4028
4029 /*
4030 * If the inode is a free space inode, we can deadlock during commit
4031 * if we put it into the delayed code.
4032 *
4033 * The data relocation inode should also be directly updated
4034 * without delay
4035 */
Nikolay Borisov70ddc552017-02-20 13:50:35 +02004036 if (!btrfs_is_free_space_inode(BTRFS_I(inode))
Josef Bacik1d52c782014-09-18 11:30:44 -04004037 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004038 && !test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) {
Alexander Block8ea05e32012-07-25 17:35:53 +02004039 btrfs_update_root_times(trans, root);
4040
Chris Mason21151332011-11-10 20:39:08 -05004041 ret = btrfs_delayed_update_inode(trans, root, inode);
4042 if (!ret)
4043 btrfs_set_inode_last_trans(trans, inode);
4044 return ret;
4045 }
4046
4047 return btrfs_update_inode_item(trans, root, inode);
4048}
4049
Josef Bacikbe6aef62012-10-22 15:43:12 -04004050noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
4051 struct btrfs_root *root,
4052 struct inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05004053{
4054 int ret;
4055
4056 ret = btrfs_update_inode(trans, root, inode);
4057 if (ret == -ENOSPC)
4058 return btrfs_update_inode_item(trans, root, inode);
4059 return ret;
4060}
4061
4062/*
Chris Masond352ac62008-09-29 15:18:18 -04004063 * unlink helper that gets used here in inode.c and in the tree logging
4064 * recovery code. It remove a link in a directory with a given name, and
4065 * also drops the back refs in the inode to the directory
4066 */
Al Viro92986792011-03-04 17:14:37 +00004067static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
4068 struct btrfs_root *root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004069 struct btrfs_inode *dir,
4070 struct btrfs_inode *inode,
Al Viro92986792011-03-04 17:14:37 +00004071 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04004072{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004073 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04004074 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04004075 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004076 struct btrfs_dir_item *di;
Josef Bacikaec74772008-07-24 12:12:38 -04004077 u64 index;
Li Zefan33345d012011-04-20 10:31:50 +08004078 u64 ino = btrfs_ino(inode);
4079 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04004080
4081 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04004082 if (!path) {
4083 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00004084 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04004085 }
4086
Chris Masonb9473432009-03-13 11:00:37 -04004087 path->leave_spinning = 1;
Li Zefan33345d012011-04-20 10:31:50 +08004088 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04004089 name, name_len, -1);
Liu Bo3cf50682018-09-12 06:06:26 +08004090 if (IS_ERR_OR_NULL(di)) {
4091 ret = di ? PTR_ERR(di) : -ENOENT;
Chris Mason39279cc2007-06-12 06:35:45 -04004092 goto err;
4093 }
Chris Mason39279cc2007-06-12 06:35:45 -04004094 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04004095 if (ret)
4096 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02004097 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04004098
Miao Xie67de1172013-12-26 13:07:06 +08004099 /*
4100 * If we don't have dir index, we have to get it by looking up
4101 * the inode ref, since we get the inode ref, remove it directly,
4102 * it is unnecessary to do delayed deletion.
4103 *
4104 * But if we have dir index, needn't search inode ref to get it.
4105 * Since the inode ref is close to the inode item, it is better
4106 * that we delay to delete it, and just do this deletion when
4107 * we update the inode item.
4108 */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004109 if (inode->dir_index) {
Miao Xie67de1172013-12-26 13:07:06 +08004110 ret = btrfs_delayed_delete_inode_ref(inode);
4111 if (!ret) {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004112 index = inode->dir_index;
Miao Xie67de1172013-12-26 13:07:06 +08004113 goto skip_backref;
4114 }
4115 }
4116
Li Zefan33345d012011-04-20 10:31:50 +08004117 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
4118 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04004119 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004120 btrfs_info(fs_info,
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00004121 "failed to delete reference to %.*s, inode %llu parent %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02004122 name_len, name, ino, dir_ino);
Jeff Mahoney66642832016-06-10 18:19:25 -04004123 btrfs_abort_transaction(trans, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04004124 goto err;
4125 }
Miao Xie67de1172013-12-26 13:07:06 +08004126skip_backref:
Lu Fengqi9add2942018-08-01 11:32:26 +08004127 ret = btrfs_delete_delayed_dir_index(trans, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004128 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004129 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04004130 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004131 }
Chris Mason39279cc2007-06-12 06:35:45 -04004132
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004133 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len, inode,
4134 dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004135 if (ret != 0 && ret != -ENOENT) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004136 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004137 goto err;
4138 }
Chris Masone02119d2008-09-05 16:13:11 -04004139
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004140 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len, dir,
4141 index);
Chris Mason6418c962010-10-30 07:34:24 -04004142 if (ret == -ENOENT)
4143 ret = 0;
Zach Brownd4e39912013-04-02 21:02:16 +00004144 else if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004145 btrfs_abort_transaction(trans, ret);
Josef Bacik63611e72019-06-18 10:59:18 -04004146
4147 /*
4148 * If we have a pending delayed iput we could end up with the final iput
4149 * being run in btrfs-cleaner context. If we have enough of these built
4150 * up we can end up burning a lot of time in btrfs-cleaner without any
4151 * way to throttle the unlinks. Since we're currently holding a ref on
4152 * the inode we can run the delayed iput here without any issues as the
4153 * final iput won't be done until after we drop the ref we're currently
4154 * holding.
4155 */
4156 btrfs_run_delayed_iput(fs_info, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004157err:
4158 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04004159 if (ret)
4160 goto out;
4161
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004162 btrfs_i_size_write(dir, dir->vfs_inode.i_size - name_len * 2);
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004163 inode_inc_iversion(&inode->vfs_inode);
4164 inode_inc_iversion(&dir->vfs_inode);
4165 inode->vfs_inode.i_ctime = dir->vfs_inode.i_mtime =
4166 dir->vfs_inode.i_ctime = current_time(&inode->vfs_inode);
4167 ret = btrfs_update_inode(trans, root, &dir->vfs_inode);
Chris Masone02119d2008-09-05 16:13:11 -04004168out:
Chris Mason39279cc2007-06-12 06:35:45 -04004169 return ret;
4170}
4171
Al Viro92986792011-03-04 17:14:37 +00004172int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
4173 struct btrfs_root *root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004174 struct btrfs_inode *dir, struct btrfs_inode *inode,
Al Viro92986792011-03-04 17:14:37 +00004175 const char *name, int name_len)
4176{
4177 int ret;
4178 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
4179 if (!ret) {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004180 drop_nlink(&inode->vfs_inode);
4181 ret = btrfs_update_inode(trans, root, &inode->vfs_inode);
Al Viro92986792011-03-04 17:14:37 +00004182 }
4183 return ret;
4184}
Yan, Zhenga22285a2010-05-16 10:48:46 -04004185
4186/*
4187 * helper to start transaction for unlink and rmdir.
4188 *
Josef Bacikd52be812013-05-29 14:54:47 -04004189 * unlink and rmdir are special in btrfs, they do not always free space, so
4190 * if we cannot make our reservations the normal way try and see if there is
4191 * plenty of slack room in the global reserve to migrate, otherwise we cannot
4192 * allow the unlink to occur.
Yan, Zhenga22285a2010-05-16 10:48:46 -04004193 */
Josef Bacikd52be812013-05-29 14:54:47 -04004194static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir)
Yan, Zhenga22285a2010-05-16 10:48:46 -04004195{
Yan, Zhenga22285a2010-05-16 10:48:46 -04004196 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004197
Josef Bacike70bea52011-10-11 14:18:24 -04004198 /*
4199 * 1 for the possible orphan item
4200 * 1 for the dir item
4201 * 1 for the dir index
4202 * 1 for the inode ref
Josef Bacike70bea52011-10-11 14:18:24 -04004203 * 1 for the inode
4204 */
Filipe Manana8eab77f2015-11-13 23:57:16 +00004205 return btrfs_start_transaction_fallback_global_rsv(root, 5, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004206}
4207
Chris Mason39279cc2007-06-12 06:35:45 -04004208static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
4209{
Yan, Zhenga22285a2010-05-16 10:48:46 -04004210 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004211 struct btrfs_trans_handle *trans;
David Howells2b0143b2015-03-17 22:25:59 +00004212 struct inode *inode = d_inode(dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04004213 int ret;
4214
Josef Bacikd52be812013-05-29 14:54:47 -04004215 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004216 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004217 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04004218
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004219 btrfs_record_unlink_dir(trans, BTRFS_I(dir), BTRFS_I(d_inode(dentry)),
4220 0);
Chris Mason12fcfd22009-03-24 10:24:20 -04004221
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004222 ret = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
4223 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
4224 dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00004225 if (ret)
4226 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004227
Yan, Zhenga22285a2010-05-16 10:48:46 -04004228 if (inode->i_nlink == 0) {
Nikolay Borisov73f2e542017-02-20 13:50:59 +02004229 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Tsutomu Itohb5324022011-07-19 07:27:20 +00004230 if (ret)
4231 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004232 }
Josef Bacik7b128762008-07-24 12:17:14 -04004233
Tsutomu Itohb5324022011-07-19 07:27:20 +00004234out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004235 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004236 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04004237 return ret;
4238}
4239
Misono Tomohirof60a2362018-04-18 11:34:52 +09004240static int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
Lu Fengqi401b3b12018-08-01 11:32:30 +08004241 struct inode *dir, u64 objectid,
4242 const char *name, int name_len)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004243{
Lu Fengqi401b3b12018-08-01 11:32:30 +08004244 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004245 struct btrfs_path *path;
4246 struct extent_buffer *leaf;
4247 struct btrfs_dir_item *di;
4248 struct btrfs_key key;
4249 u64 index;
4250 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004251 u64 dir_ino = btrfs_ino(BTRFS_I(dir));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004252
4253 path = btrfs_alloc_path();
4254 if (!path)
4255 return -ENOMEM;
4256
Li Zefan33345d012011-04-20 10:31:50 +08004257 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004258 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004259 if (IS_ERR_OR_NULL(di)) {
Liu Bo3cf50682018-09-12 06:06:26 +08004260 ret = di ? PTR_ERR(di) : -ENOENT;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004261 goto out;
4262 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004263
4264 leaf = path->nodes[0];
4265 btrfs_dir_item_key_to_cpu(leaf, di, &key);
4266 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
4267 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004268 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004269 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004270 goto out;
4271 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004272 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004273
Lu Fengqi3ee1c552018-08-01 11:32:28 +08004274 ret = btrfs_del_root_ref(trans, objectid, root->root_key.objectid,
4275 dir_ino, &index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004276 if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004277 if (ret != -ENOENT) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004278 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004279 goto out;
4280 }
Li Zefan33345d012011-04-20 10:31:50 +08004281 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004282 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004283 if (IS_ERR_OR_NULL(di)) {
4284 if (!di)
4285 ret = -ENOENT;
4286 else
4287 ret = PTR_ERR(di);
Jeff Mahoney66642832016-06-10 18:19:25 -04004288 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004289 goto out;
4290 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004291
4292 leaf = path->nodes[0];
4293 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004294 index = key.offset;
4295 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004296 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004297
Lu Fengqi9add2942018-08-01 11:32:26 +08004298 ret = btrfs_delete_delayed_dir_index(trans, BTRFS_I(dir), index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004299 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004300 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004301 goto out;
4302 }
Miao Xie16cdcec2011-04-22 18:12:22 +08004303
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004304 btrfs_i_size_write(BTRFS_I(dir), dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004305 inode_inc_iversion(dir);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07004306 dir->i_mtime = dir->i_ctime = current_time(dir);
Josef Bacik5a24e842012-08-08 10:12:59 -06004307 ret = btrfs_update_inode_fallback(trans, root, dir);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004308 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004309 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004310out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04004311 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004312 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004313}
4314
Misono Tomohiroec42f162018-04-18 11:34:13 +09004315/*
4316 * Helper to check if the subvolume references other subvolumes or if it's
4317 * default.
4318 */
Misono Tomohirof60a2362018-04-18 11:34:52 +09004319static noinline int may_destroy_subvol(struct btrfs_root *root)
Misono Tomohiroec42f162018-04-18 11:34:13 +09004320{
4321 struct btrfs_fs_info *fs_info = root->fs_info;
4322 struct btrfs_path *path;
4323 struct btrfs_dir_item *di;
4324 struct btrfs_key key;
4325 u64 dir_id;
4326 int ret;
4327
4328 path = btrfs_alloc_path();
4329 if (!path)
4330 return -ENOMEM;
4331
4332 /* Make sure this root isn't set as the default subvol */
4333 dir_id = btrfs_super_root_dir(fs_info->super_copy);
4334 di = btrfs_lookup_dir_item(NULL, fs_info->tree_root, path,
4335 dir_id, "default", 7, 0);
4336 if (di && !IS_ERR(di)) {
4337 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key);
4338 if (key.objectid == root->root_key.objectid) {
4339 ret = -EPERM;
4340 btrfs_err(fs_info,
4341 "deleting default subvolume %llu is not allowed",
4342 key.objectid);
4343 goto out;
4344 }
4345 btrfs_release_path(path);
4346 }
4347
4348 key.objectid = root->root_key.objectid;
4349 key.type = BTRFS_ROOT_REF_KEY;
4350 key.offset = (u64)-1;
4351
4352 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
4353 if (ret < 0)
4354 goto out;
4355 BUG_ON(ret == 0);
4356
4357 ret = 0;
4358 if (path->slots[0] > 0) {
4359 path->slots[0]--;
4360 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
4361 if (key.objectid == root->root_key.objectid &&
4362 key.type == BTRFS_ROOT_REF_KEY)
4363 ret = -ENOTEMPTY;
4364 }
4365out:
4366 btrfs_free_path(path);
4367 return ret;
4368}
4369
Nikolay Borisov20a68002018-04-27 14:36:24 +03004370/* Delete all dentries for inodes belonging to the root */
4371static void btrfs_prune_dentries(struct btrfs_root *root)
4372{
4373 struct btrfs_fs_info *fs_info = root->fs_info;
4374 struct rb_node *node;
4375 struct rb_node *prev;
4376 struct btrfs_inode *entry;
4377 struct inode *inode;
4378 u64 objectid = 0;
4379
4380 if (!test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
4381 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
4382
4383 spin_lock(&root->inode_lock);
4384again:
4385 node = root->inode_tree.rb_node;
4386 prev = NULL;
4387 while (node) {
4388 prev = node;
4389 entry = rb_entry(node, struct btrfs_inode, rb_node);
4390
David Sterba37508512018-06-29 10:56:40 +02004391 if (objectid < btrfs_ino(entry))
Nikolay Borisov20a68002018-04-27 14:36:24 +03004392 node = node->rb_left;
David Sterba37508512018-06-29 10:56:40 +02004393 else if (objectid > btrfs_ino(entry))
Nikolay Borisov20a68002018-04-27 14:36:24 +03004394 node = node->rb_right;
4395 else
4396 break;
4397 }
4398 if (!node) {
4399 while (prev) {
4400 entry = rb_entry(prev, struct btrfs_inode, rb_node);
David Sterba37508512018-06-29 10:56:40 +02004401 if (objectid <= btrfs_ino(entry)) {
Nikolay Borisov20a68002018-04-27 14:36:24 +03004402 node = prev;
4403 break;
4404 }
4405 prev = rb_next(prev);
4406 }
4407 }
4408 while (node) {
4409 entry = rb_entry(node, struct btrfs_inode, rb_node);
David Sterba37508512018-06-29 10:56:40 +02004410 objectid = btrfs_ino(entry) + 1;
Nikolay Borisov20a68002018-04-27 14:36:24 +03004411 inode = igrab(&entry->vfs_inode);
4412 if (inode) {
4413 spin_unlock(&root->inode_lock);
4414 if (atomic_read(&inode->i_count) > 1)
4415 d_prune_aliases(inode);
4416 /*
4417 * btrfs_drop_inode will have it removed from the inode
4418 * cache when its usage count hits zero.
4419 */
4420 iput(inode);
4421 cond_resched();
4422 spin_lock(&root->inode_lock);
4423 goto again;
4424 }
4425
4426 if (cond_resched_lock(&root->inode_lock))
4427 goto again;
4428
4429 node = rb_next(node);
4430 }
4431 spin_unlock(&root->inode_lock);
4432}
4433
Misono Tomohirof60a2362018-04-18 11:34:52 +09004434int btrfs_delete_subvolume(struct inode *dir, struct dentry *dentry)
4435{
4436 struct btrfs_fs_info *fs_info = btrfs_sb(dentry->d_sb);
4437 struct btrfs_root *root = BTRFS_I(dir)->root;
4438 struct inode *inode = d_inode(dentry);
4439 struct btrfs_root *dest = BTRFS_I(inode)->root;
4440 struct btrfs_trans_handle *trans;
4441 struct btrfs_block_rsv block_rsv;
4442 u64 root_flags;
Misono Tomohirof60a2362018-04-18 11:34:52 +09004443 int ret;
4444 int err;
4445
4446 /*
4447 * Don't allow to delete a subvolume with send in progress. This is
4448 * inside the inode lock so the error handling that has to drop the bit
4449 * again is not run concurrently.
4450 */
4451 spin_lock(&dest->root_item_lock);
Lu Fengqia7176f72018-08-04 21:10:53 +08004452 if (dest->send_in_progress) {
Misono Tomohirof60a2362018-04-18 11:34:52 +09004453 spin_unlock(&dest->root_item_lock);
4454 btrfs_warn(fs_info,
4455 "attempt to delete subvolume %llu during send",
4456 dest->root_key.objectid);
4457 return -EPERM;
4458 }
Lu Fengqia7176f72018-08-04 21:10:53 +08004459 root_flags = btrfs_root_flags(&dest->root_item);
4460 btrfs_set_root_flags(&dest->root_item,
4461 root_flags | BTRFS_ROOT_SUBVOL_DEAD);
4462 spin_unlock(&dest->root_item_lock);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004463
4464 down_write(&fs_info->subvol_sem);
4465
4466 err = may_destroy_subvol(dest);
4467 if (err)
4468 goto out_up_write;
4469
4470 btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP);
4471 /*
4472 * One for dir inode,
4473 * two for dir entries,
4474 * two for root ref/backref.
4475 */
Gu JinXiangc4c129d2018-05-30 11:00:38 +08004476 err = btrfs_subvolume_reserve_metadata(root, &block_rsv, 5, true);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004477 if (err)
4478 goto out_up_write;
4479
4480 trans = btrfs_start_transaction(root, 0);
4481 if (IS_ERR(trans)) {
4482 err = PTR_ERR(trans);
4483 goto out_release;
4484 }
4485 trans->block_rsv = &block_rsv;
4486 trans->bytes_reserved = block_rsv.size;
4487
4488 btrfs_record_snapshot_destroy(trans, BTRFS_I(dir));
4489
Lu Fengqi401b3b12018-08-01 11:32:30 +08004490 ret = btrfs_unlink_subvol(trans, dir, dest->root_key.objectid,
4491 dentry->d_name.name, dentry->d_name.len);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004492 if (ret) {
4493 err = ret;
4494 btrfs_abort_transaction(trans, ret);
4495 goto out_end_trans;
4496 }
4497
4498 btrfs_record_root_in_trans(trans, dest);
4499
4500 memset(&dest->root_item.drop_progress, 0,
4501 sizeof(dest->root_item.drop_progress));
4502 dest->root_item.drop_level = 0;
4503 btrfs_set_root_refs(&dest->root_item, 0);
4504
4505 if (!test_and_set_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &dest->state)) {
4506 ret = btrfs_insert_orphan_item(trans,
4507 fs_info->tree_root,
4508 dest->root_key.objectid);
4509 if (ret) {
4510 btrfs_abort_transaction(trans, ret);
4511 err = ret;
4512 goto out_end_trans;
4513 }
4514 }
4515
Lu Fengqid1957792018-05-29 15:01:54 +08004516 ret = btrfs_uuid_tree_remove(trans, dest->root_item.uuid,
Misono Tomohirof60a2362018-04-18 11:34:52 +09004517 BTRFS_UUID_KEY_SUBVOL,
4518 dest->root_key.objectid);
4519 if (ret && ret != -ENOENT) {
4520 btrfs_abort_transaction(trans, ret);
4521 err = ret;
4522 goto out_end_trans;
4523 }
4524 if (!btrfs_is_empty_uuid(dest->root_item.received_uuid)) {
Lu Fengqid1957792018-05-29 15:01:54 +08004525 ret = btrfs_uuid_tree_remove(trans,
Misono Tomohirof60a2362018-04-18 11:34:52 +09004526 dest->root_item.received_uuid,
4527 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
4528 dest->root_key.objectid);
4529 if (ret && ret != -ENOENT) {
4530 btrfs_abort_transaction(trans, ret);
4531 err = ret;
4532 goto out_end_trans;
4533 }
4534 }
4535
4536out_end_trans:
4537 trans->block_rsv = NULL;
4538 trans->bytes_reserved = 0;
4539 ret = btrfs_end_transaction(trans);
4540 if (ret && !err)
4541 err = ret;
4542 inode->i_flags |= S_DEAD;
4543out_release:
4544 btrfs_subvolume_release_metadata(fs_info, &block_rsv);
4545out_up_write:
4546 up_write(&fs_info->subvol_sem);
4547 if (err) {
4548 spin_lock(&dest->root_item_lock);
4549 root_flags = btrfs_root_flags(&dest->root_item);
4550 btrfs_set_root_flags(&dest->root_item,
4551 root_flags & ~BTRFS_ROOT_SUBVOL_DEAD);
4552 spin_unlock(&dest->root_item_lock);
4553 } else {
4554 d_invalidate(dentry);
Nikolay Borisov20a68002018-04-27 14:36:24 +03004555 btrfs_prune_dentries(dest);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004556 ASSERT(dest->send_in_progress == 0);
4557
4558 /* the last ref */
4559 if (dest->ino_cache_inode) {
4560 iput(dest->ino_cache_inode);
4561 dest->ino_cache_inode = NULL;
4562 }
4563 }
4564
4565 return err;
4566}
4567
Chris Mason39279cc2007-06-12 06:35:45 -04004568static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
4569{
David Howells2b0143b2015-03-17 22:25:59 +00004570 struct inode *inode = d_inode(dentry);
Chris Mason1832a6d2007-12-21 16:27:21 -05004571 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004572 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004573 struct btrfs_trans_handle *trans;
Filipe Manana44f714d2016-06-06 16:11:13 +01004574 u64 last_unlink_trans;
Chris Mason39279cc2007-06-12 06:35:45 -04004575
David Sterbab3ae2442012-09-13 16:04:34 -06004576 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Yan134d4512007-10-25 15:49:25 -04004577 return -ENOTEMPTY;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004578 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_FIRST_FREE_OBJECTID)
Misono Tomohiroa79a4642018-04-18 11:35:31 +09004579 return btrfs_delete_subvolume(dir, dentry);
Yan134d4512007-10-25 15:49:25 -04004580
Josef Bacikd52be812013-05-29 14:54:47 -04004581 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004582 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004583 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004584
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004585 if (unlikely(btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Lu Fengqi401b3b12018-08-01 11:32:30 +08004586 err = btrfs_unlink_subvol(trans, dir,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004587 BTRFS_I(inode)->location.objectid,
4588 dentry->d_name.name,
4589 dentry->d_name.len);
4590 goto out;
4591 }
4592
Nikolay Borisov73f2e542017-02-20 13:50:59 +02004593 err = btrfs_orphan_add(trans, BTRFS_I(inode));
Josef Bacik7b128762008-07-24 12:17:14 -04004594 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004595 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004596
Filipe Manana44f714d2016-06-06 16:11:13 +01004597 last_unlink_trans = BTRFS_I(inode)->last_unlink_trans;
4598
Chris Mason39279cc2007-06-12 06:35:45 -04004599 /* now the directory is empty */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004600 err = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
4601 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
4602 dentry->d_name.len);
Filipe Manana44f714d2016-06-06 16:11:13 +01004603 if (!err) {
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004604 btrfs_i_size_write(BTRFS_I(inode), 0);
Filipe Manana44f714d2016-06-06 16:11:13 +01004605 /*
4606 * Propagate the last_unlink_trans value of the deleted dir to
4607 * its parent directory. This is to prevent an unrecoverable
4608 * log tree in the case we do something like this:
4609 * 1) create dir foo
4610 * 2) create snapshot under dir foo
4611 * 3) delete the snapshot
4612 * 4) rmdir foo
4613 * 5) mkdir foo
4614 * 6) fsync foo or some file inside foo
4615 */
4616 if (last_unlink_trans >= trans->transid)
4617 BTRFS_I(dir)->last_unlink_trans = last_unlink_trans;
4618 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004619out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004620 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004621 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39544012007-12-12 14:38:19 -05004622
Chris Mason39279cc2007-06-12 06:35:45 -04004623 return err;
4624}
4625
Josef Bacikddfae632017-10-19 14:16:02 -04004626/*
4627 * Return this if we need to call truncate_block for the last bit of the
4628 * truncate.
4629 */
4630#define NEED_TRUNCATE_BLOCK 1
Filipe Manana0305cd52015-10-16 12:34:25 +01004631
Chris Mason323ac952008-10-01 19:05:46 -04004632/*
Chris Mason39279cc2007-06-12 06:35:45 -04004633 * this can truncate away extent items, csum items and directory items.
4634 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04004635 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04004636 *
4637 * csum items that cross the new i_size are truncated to the new size
4638 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04004639 *
4640 * min_type is the minimum key type to truncate down to. If set to 0, this
4641 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04004642 */
Yan, Zheng80825102009-11-12 09:35:36 +00004643int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
4644 struct btrfs_root *root,
4645 struct inode *inode,
4646 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004647{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004648 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04004649 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004650 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004651 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00004652 struct btrfs_key key;
4653 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04004654 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04004655 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004656 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004657 u64 item_end = 0;
Filipe Mananac1aa4572015-06-20 18:20:09 +01004658 u64 last_size = new_size;
Yan, Zheng80825102009-11-12 09:35:36 +00004659 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04004660 int found_extent;
4661 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05004662 int pending_del_nr = 0;
4663 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04004664 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00004665 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004666 u64 ino = btrfs_ino(BTRFS_I(inode));
Chris Mason28ed1342014-12-17 09:41:04 -08004667 u64 bytes_deleted = 0;
Thomas Meyer897ca812017-10-07 16:02:21 +02004668 bool be_nice = false;
4669 bool should_throttle = false;
Yan, Zheng80825102009-11-12 09:35:36 +00004670
4671 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04004672
Chris Mason28ed1342014-12-17 09:41:04 -08004673 /*
4674 * for non-free space inodes and ref cows, we want to back off from
4675 * time to time
4676 */
Nikolay Borisov70ddc552017-02-20 13:50:35 +02004677 if (!btrfs_is_free_space_inode(BTRFS_I(inode)) &&
Chris Mason28ed1342014-12-17 09:41:04 -08004678 test_bit(BTRFS_ROOT_REF_COWS, &root->state))
Thomas Meyer897ca812017-10-07 16:02:21 +02004679 be_nice = true;
Chris Mason28ed1342014-12-17 09:41:04 -08004680
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004681 path = btrfs_alloc_path();
4682 if (!path)
4683 return -ENOMEM;
David Sterbae4058b52015-11-27 16:31:35 +01004684 path->reada = READA_BACK;
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004685
Josef Bacik5dc562c2012-08-17 13:14:17 -04004686 /*
4687 * We want to drop from the next block forward in case this new size is
4688 * not block aligned since we will be keeping the last block of the
4689 * extent just the way it is.
4690 */
Miao Xie27cdeb72014-04-02 19:51:05 +08004691 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004692 root == fs_info->tree_root)
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02004693 btrfs_drop_extent_cache(BTRFS_I(inode), ALIGN(new_size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004694 fs_info->sectorsize),
Jeff Mahoneyda170662016-06-15 09:22:56 -04004695 (u64)-1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00004696
Miao Xie16cdcec2011-04-22 18:12:22 +08004697 /*
4698 * This function is also used to drop the items in the log tree before
4699 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
Andrea Gelmini52042d82018-11-28 12:05:13 +01004700 * it is used to drop the logged items. So we shouldn't kill the delayed
Miao Xie16cdcec2011-04-22 18:12:22 +08004701 * items.
4702 */
4703 if (min_type == 0 && root == BTRFS_I(inode)->root)
Nikolay Borisov4ccb5c72017-01-10 20:35:38 +02004704 btrfs_kill_delayed_inode_items(BTRFS_I(inode));
Miao Xie16cdcec2011-04-22 18:12:22 +08004705
Li Zefan33345d012011-04-20 10:31:50 +08004706 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04004707 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04004708 key.type = (u8)-1;
4709
Chris Mason85e21ba2008-01-29 15:11:36 -05004710search_again:
Chris Mason28ed1342014-12-17 09:41:04 -08004711 /*
4712 * with a 16K leaf size and 128MB extents, you can actually queue
4713 * up a huge file in a single leaf. Most of the time that
4714 * bytes_deleted is > 0, it will be huge by the time we get here
4715 */
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004716 if (be_nice && bytes_deleted > SZ_32M &&
4717 btrfs_should_end_transaction(trans)) {
4718 ret = -EAGAIN;
Yan, Zheng80825102009-11-12 09:35:36 +00004719 goto out;
4720 }
Chris Masond3977122009-01-05 21:25:51 -05004721
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004722 path->leave_spinning = 1;
4723 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
4724 if (ret < 0)
4725 goto out;
4726
Chris Mason85e21ba2008-01-29 15:11:36 -05004727 if (ret > 0) {
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004728 ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -04004729 /* there are no items in the tree for us to truncate, we're
4730 * done
4731 */
Yan, Zheng80825102009-11-12 09:35:36 +00004732 if (path->slots[0] == 0)
4733 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05004734 path->slots[0]--;
4735 }
4736
Chris Masond3977122009-01-05 21:25:51 -05004737 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04004738 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04004739 leaf = path->nodes[0];
4740 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02004741 found_type = found_key.type;
Chris Mason39279cc2007-06-12 06:35:45 -04004742
Li Zefan33345d012011-04-20 10:31:50 +08004743 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04004744 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004745
Chris Mason85e21ba2008-01-29 15:11:36 -05004746 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004747 break;
4748
Chris Mason5f39d392007-10-15 16:14:19 -04004749 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04004750 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04004751 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04004752 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04004753 extent_type = btrfs_file_extent_type(leaf, fi);
4754 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04004755 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04004756 btrfs_file_extent_num_bytes(leaf, fi);
Liu Bo09ed2f12017-03-10 11:09:48 -08004757
4758 trace_btrfs_truncate_show_fi_regular(
4759 BTRFS_I(inode), leaf, fi,
4760 found_key.offset);
Chris Mason179e29e2007-11-01 11:28:41 -04004761 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Qu Wenruoe41ca582018-06-06 15:41:49 +08004762 item_end += btrfs_file_extent_ram_bytes(leaf,
4763 fi);
Liu Bo09ed2f12017-03-10 11:09:48 -08004764
4765 trace_btrfs_truncate_show_fi_inline(
4766 BTRFS_I(inode), leaf, fi, path->slots[0],
4767 found_key.offset);
Chris Mason39279cc2007-06-12 06:35:45 -04004768 }
Yan008630c2007-11-07 13:31:09 -05004769 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04004770 }
Yan, Zheng80825102009-11-12 09:35:36 +00004771 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04004772 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00004773 } else {
Filipe Manana76b42ab2017-02-14 16:56:01 +00004774 if (item_end < new_size)
Yan, Zheng80825102009-11-12 09:35:36 +00004775 break;
4776 if (found_key.offset >= new_size)
4777 del_item = 1;
4778 else
4779 del_item = 0;
4780 }
Chris Mason39279cc2007-06-12 06:35:45 -04004781 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004782 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04004783 if (found_type != BTRFS_EXTENT_DATA_KEY)
4784 goto delete;
4785
4786 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04004787 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04004788 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05004789 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04004790 u64 orig_num_bytes =
4791 btrfs_file_extent_num_bytes(leaf, fi);
Qu Wenruofda28322013-02-26 08:10:22 +00004792 extent_num_bytes = ALIGN(new_size -
4793 found_key.offset,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004794 fs_info->sectorsize);
Chris Masondb945352007-10-15 16:15:53 -04004795 btrfs_set_file_extent_num_bytes(leaf, fi,
4796 extent_num_bytes);
4797 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05004798 extent_num_bytes);
Miao Xie27cdeb72014-04-02 19:51:05 +08004799 if (test_bit(BTRFS_ROOT_REF_COWS,
4800 &root->state) &&
4801 extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004802 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04004803 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004804 } else {
Chris Masondb945352007-10-15 16:15:53 -04004805 extent_num_bytes =
4806 btrfs_file_extent_disk_num_bytes(leaf,
4807 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004808 extent_offset = found_key.offset -
4809 btrfs_file_extent_offset(leaf, fi);
4810
Chris Mason39279cc2007-06-12 06:35:45 -04004811 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05004812 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004813 if (extent_start != 0) {
4814 found_extent = 1;
Miao Xie27cdeb72014-04-02 19:51:05 +08004815 if (test_bit(BTRFS_ROOT_REF_COWS,
4816 &root->state))
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004817 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04004818 }
4819 }
Chris Mason90692182008-02-08 13:49:28 -05004820 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04004821 /*
4822 * we can't truncate inline items that have had
4823 * special encodings
4824 */
4825 if (!del_item &&
Chris Masonc8b97812008-10-29 14:49:59 -04004826 btrfs_file_extent_encryption(leaf, fi) == 0 &&
Josef Bacikddfae632017-10-19 14:16:02 -04004827 btrfs_file_extent_other_encoding(leaf, fi) == 0 &&
4828 btrfs_file_extent_compression(leaf, fi) == 0) {
4829 u32 size = (u32)(new_size - found_key.offset);
Chris Mason514ac8a2014-01-03 21:07:00 -08004830
Josef Bacikddfae632017-10-19 14:16:02 -04004831 btrfs_set_file_extent_ram_bytes(leaf, fi, size);
4832 size = btrfs_file_extent_calc_inline_size(size);
David Sterba78ac4f92019-03-20 14:49:12 +01004833 btrfs_truncate_item(path, size, 1);
Josef Bacikddfae632017-10-19 14:16:02 -04004834 } else if (!del_item) {
Chris Mason514ac8a2014-01-03 21:07:00 -08004835 /*
Josef Bacikddfae632017-10-19 14:16:02 -04004836 * We have to bail so the last_size is set to
4837 * just before this extent.
Chris Mason514ac8a2014-01-03 21:07:00 -08004838 */
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004839 ret = NEED_TRUNCATE_BLOCK;
Josef Bacikddfae632017-10-19 14:16:02 -04004840 break;
Chris Mason90692182008-02-08 13:49:28 -05004841 }
Josef Bacikddfae632017-10-19 14:16:02 -04004842
4843 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state))
4844 inode_sub_bytes(inode, item_end + 1 - new_size);
Chris Mason39279cc2007-06-12 06:35:45 -04004845 }
Chris Mason179e29e2007-11-01 11:28:41 -04004846delete:
Josef Bacikddfae632017-10-19 14:16:02 -04004847 if (del_item)
4848 last_size = found_key.offset;
4849 else
4850 last_size = new_size;
Chris Mason39279cc2007-06-12 06:35:45 -04004851 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05004852 if (!pending_del_nr) {
4853 /* no pending yet, add ourselves */
4854 pending_del_slot = path->slots[0];
4855 pending_del_nr = 1;
4856 } else if (pending_del_nr &&
4857 path->slots[0] + 1 == pending_del_slot) {
4858 /* hop on the pending chunk */
4859 pending_del_nr++;
4860 pending_del_slot = path->slots[0];
4861 } else {
Chris Masond3977122009-01-05 21:25:51 -05004862 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05004863 }
Chris Mason39279cc2007-06-12 06:35:45 -04004864 } else {
4865 break;
4866 }
Thomas Meyer897ca812017-10-07 16:02:21 +02004867 should_throttle = false;
Chris Mason28f75a02015-02-04 06:59:29 -08004868
Miao Xie27cdeb72014-04-02 19:51:05 +08004869 if (found_extent &&
4870 (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004871 root == fs_info->tree_root)) {
Qu Wenruoffd4bb22019-04-04 14:45:36 +08004872 struct btrfs_ref ref = { 0 };
4873
Chris Masonb9473432009-03-13 11:00:37 -04004874 btrfs_set_path_blocking(path);
Chris Mason28ed1342014-12-17 09:41:04 -08004875 bytes_deleted += extent_num_bytes;
Qu Wenruoffd4bb22019-04-04 14:45:36 +08004876
4877 btrfs_init_generic_ref(&ref, BTRFS_DROP_DELAYED_REF,
4878 extent_start, extent_num_bytes, 0);
4879 ref.real_root = root->root_key.objectid;
4880 btrfs_init_data_ref(&ref, btrfs_header_owner(leaf),
4881 ino, extent_offset);
4882 ret = btrfs_free_extent(trans, &ref);
Omar Sandoval05522102018-05-11 13:13:31 -07004883 if (ret) {
4884 btrfs_abort_transaction(trans, ret);
4885 break;
4886 }
Chris Mason28f75a02015-02-04 06:59:29 -08004887 if (be_nice) {
Lu Fengqi7c861622018-10-11 13:40:36 +08004888 if (btrfs_should_throttle_delayed_refs(trans))
Thomas Meyer897ca812017-10-07 16:02:21 +02004889 should_throttle = true;
Chris Mason28f75a02015-02-04 06:59:29 -08004890 }
Chris Mason39279cc2007-06-12 06:35:45 -04004891 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004892
Yan, Zheng80825102009-11-12 09:35:36 +00004893 if (found_type == BTRFS_INODE_ITEM_KEY)
4894 break;
4895
4896 if (path->slots[0] == 0 ||
Josef Bacik12621332015-02-03 07:50:16 -08004897 path->slots[0] != pending_del_slot ||
Josef Bacik28bad212018-12-03 10:20:38 -05004898 should_throttle) {
Yan, Zheng80825102009-11-12 09:35:36 +00004899 if (pending_del_nr) {
4900 ret = btrfs_del_items(trans, root, path,
4901 pending_del_slot,
4902 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004903 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004904 btrfs_abort_transaction(trans, ret);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004905 break;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004906 }
Yan, Zheng80825102009-11-12 09:35:36 +00004907 pending_del_nr = 0;
4908 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004909 btrfs_release_path(path);
Josef Bacik28bad212018-12-03 10:20:38 -05004910
Chris Mason28f75a02015-02-04 06:59:29 -08004911 /*
Josef Bacik28bad212018-12-03 10:20:38 -05004912 * We can generate a lot of delayed refs, so we need to
4913 * throttle every once and a while and make sure we're
4914 * adding enough space to keep up with the work we are
4915 * generating. Since we hold a transaction here we
4916 * can't flush, and we don't want to FLUSH_LIMIT because
4917 * we could have generated too many delayed refs to
4918 * actually allocate, so just bail if we're short and
4919 * let the normal reservation dance happen higher up.
Chris Mason28f75a02015-02-04 06:59:29 -08004920 */
Josef Bacik28bad212018-12-03 10:20:38 -05004921 if (should_throttle) {
4922 ret = btrfs_delayed_refs_rsv_refill(fs_info,
4923 BTRFS_RESERVE_NO_FLUSH);
4924 if (ret) {
4925 ret = -EAGAIN;
4926 break;
4927 }
Chris Mason28f75a02015-02-04 06:59:29 -08004928 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004929 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00004930 } else {
4931 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05004932 }
Chris Mason39279cc2007-06-12 06:35:45 -04004933 }
Yan, Zheng80825102009-11-12 09:35:36 +00004934out:
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004935 if (ret >= 0 && pending_del_nr) {
4936 int err;
4937
4938 err = btrfs_del_items(trans, root, path, pending_del_slot,
Chris Mason85e21ba2008-01-29 15:11:36 -05004939 pending_del_nr);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004940 if (err) {
4941 btrfs_abort_transaction(trans, err);
4942 ret = err;
4943 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004944 }
Filipe Manana76b42ab2017-02-14 16:56:01 +00004945 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
4946 ASSERT(last_size >= new_size);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004947 if (!ret && last_size > new_size)
Filipe Manana76b42ab2017-02-14 16:56:01 +00004948 last_size = new_size;
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004949 btrfs_ordered_update_i_size(inode, last_size, NULL);
Filipe Manana76b42ab2017-02-14 16:56:01 +00004950 }
Chris Mason28ed1342014-12-17 09:41:04 -08004951
Chris Mason39279cc2007-06-12 06:35:45 -04004952 btrfs_free_path(path);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004953 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04004954}
4955
Chris Masona52d9a82007-08-27 16:49:44 -04004956/*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304957 * btrfs_truncate_block - read, zero a chunk and write a block
Josef Bacik2aaa6652012-08-29 14:27:18 -04004958 * @inode - inode that we're zeroing
4959 * @from - the offset to start zeroing
4960 * @len - the length to zero, 0 to zero the entire range respective to the
4961 * offset
4962 * @front - zero up to the offset instead of from the offset on
4963 *
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304964 * This will find the block for the "from" offset and cow the block and zero the
Josef Bacik2aaa6652012-08-29 14:27:18 -04004965 * part we want to zero. This is used with truncate and hole punching.
Chris Masona52d9a82007-08-27 16:49:44 -04004966 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304967int btrfs_truncate_block(struct inode *inode, loff_t from, loff_t len,
Josef Bacik2aaa6652012-08-29 14:27:18 -04004968 int front)
Chris Masona52d9a82007-08-27 16:49:44 -04004969{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004970 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004971 struct address_space *mapping = inode->i_mapping;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004972 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4973 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004974 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08004975 struct extent_changeset *data_reserved = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004976 char *kaddr;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004977 u32 blocksize = fs_info->sectorsize;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004978 pgoff_t index = from >> PAGE_SHIFT;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304979 unsigned offset = from & (blocksize - 1);
Chris Masona52d9a82007-08-27 16:49:44 -04004980 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004981 gfp_t mask = btrfs_alloc_write_mask(mapping);
Chris Masona52d9a82007-08-27 16:49:44 -04004982 int ret = 0;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304983 u64 block_start;
4984 u64 block_end;
Chris Masona52d9a82007-08-27 16:49:44 -04004985
Nikolay Borisovb03ebd92018-01-18 14:47:06 +02004986 if (IS_ALIGNED(offset, blocksize) &&
4987 (!len || IS_ALIGNED(len, blocksize)))
Chris Masona52d9a82007-08-27 16:49:44 -04004988 goto out;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304989
Josef Bacik8b62f872017-10-19 14:15:55 -04004990 block_start = round_down(from, blocksize);
4991 block_end = block_start + blocksize - 1;
4992
Qu Wenruo364ecf32017-02-27 15:10:38 +08004993 ret = btrfs_delalloc_reserve_space(inode, &data_reserved,
Josef Bacik8b62f872017-10-19 14:15:55 -04004994 block_start, blocksize);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004995 if (ret)
4996 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004997
Chris Mason211c17f2008-05-15 09:13:45 -04004998again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004999 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04005000 if (!page) {
Qu Wenruobc42bda2017-02-27 15:10:39 +08005001 btrfs_delalloc_release_space(inode, data_reserved,
Qu Wenruo43b18592017-12-12 15:34:32 +08005002 block_start, blocksize, true);
Qu Wenruo8702ba92019-10-14 14:34:51 +08005003 btrfs_delalloc_release_extents(BTRFS_I(inode), blocksize);
Miao Xieac6a2b32012-12-05 10:56:13 +00005004 ret = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -04005005 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04005006 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04005007
Chris Masona52d9a82007-08-27 16:49:44 -04005008 if (!PageUptodate(page)) {
5009 ret = btrfs_readpage(NULL, page);
5010 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04005011 if (page->mapping != mapping) {
5012 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005013 put_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04005014 goto again;
5015 }
Chris Masona52d9a82007-08-27 16:49:44 -04005016 if (!PageUptodate(page)) {
5017 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04005018 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04005019 }
5020 }
Chris Mason211c17f2008-05-15 09:13:45 -04005021 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04005022
Chandan Rajendra9703fef2016-01-21 15:55:56 +05305023 lock_extent_bits(io_tree, block_start, block_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04005024 set_page_extent_mapped(page);
5025
Chandan Rajendra9703fef2016-01-21 15:55:56 +05305026 ordered = btrfs_lookup_ordered_extent(inode, block_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04005027 if (ordered) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05305028 unlock_extent_cached(io_tree, block_start, block_end,
David Sterbae43bbe52017-12-12 21:43:52 +01005029 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04005030 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005031 put_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04005032 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04005033 btrfs_put_ordered_extent(ordered);
5034 goto again;
5035 }
5036
Chandan Rajendra9703fef2016-01-21 15:55:56 +05305037 clear_extent_bit(&BTRFS_I(inode)->io_tree, block_start, block_end,
Omar Sandovale1821632019-08-15 14:04:04 -07005038 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
5039 0, 0, &cached_state);
Josef Bacik5d5e1032009-10-13 16:46:49 -04005040
Filipe Mananae3b8a482017-11-04 00:16:59 +00005041 ret = btrfs_set_extent_delalloc(inode, block_start, block_end, 0,
Nikolay Borisov330a5822019-07-17 16:18:17 +03005042 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04005043 if (ret) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05305044 unlock_extent_cached(io_tree, block_start, block_end,
David Sterbae43bbe52017-12-12 21:43:52 +01005045 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04005046 goto out_unlock;
5047 }
5048
Chandan Rajendra9703fef2016-01-21 15:55:56 +05305049 if (offset != blocksize) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04005050 if (!len)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05305051 len = blocksize - offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04005052 kaddr = kmap(page);
Josef Bacik2aaa6652012-08-29 14:27:18 -04005053 if (front)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05305054 memset(kaddr + (block_start - page_offset(page)),
5055 0, offset);
Josef Bacik2aaa6652012-08-29 14:27:18 -04005056 else
Chandan Rajendra9703fef2016-01-21 15:55:56 +05305057 memset(kaddr + (block_start - page_offset(page)) + offset,
5058 0, len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04005059 flush_dcache_page(page);
5060 kunmap(page);
5061 }
Chris Mason247e7432008-07-17 12:53:51 -04005062 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04005063 set_page_dirty(page);
David Sterbae43bbe52017-12-12 21:43:52 +01005064 unlock_extent_cached(io_tree, block_start, block_end, &cached_state);
Chris Mason39279cc2007-06-12 06:35:45 -04005065
Chris Mason89642222008-07-24 09:41:53 -04005066out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04005067 if (ret)
Qu Wenruobc42bda2017-02-27 15:10:39 +08005068 btrfs_delalloc_release_space(inode, data_reserved, block_start,
Qu Wenruo43b18592017-12-12 15:34:32 +08005069 blocksize, true);
Qu Wenruo8702ba92019-10-14 14:34:51 +08005070 btrfs_delalloc_release_extents(BTRFS_I(inode), blocksize);
Chris Mason39279cc2007-06-12 06:35:45 -04005071 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005072 put_page(page);
Chris Mason39279cc2007-06-12 06:35:45 -04005073out:
Qu Wenruo364ecf32017-02-27 15:10:38 +08005074 extent_changeset_free(data_reserved);
Chris Mason39279cc2007-06-12 06:35:45 -04005075 return ret;
5076}
5077
Josef Bacik16e75492013-10-22 12:18:51 -04005078static int maybe_insert_hole(struct btrfs_root *root, struct inode *inode,
5079 u64 offset, u64 len)
5080{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005081 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik16e75492013-10-22 12:18:51 -04005082 struct btrfs_trans_handle *trans;
5083 int ret;
5084
5085 /*
5086 * Still need to make sure the inode looks like it's been updated so
5087 * that any holes get logged if we fsync.
5088 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005089 if (btrfs_fs_incompat(fs_info, NO_HOLES)) {
5090 BTRFS_I(inode)->last_trans = fs_info->generation;
Josef Bacik16e75492013-10-22 12:18:51 -04005091 BTRFS_I(inode)->last_sub_trans = root->log_transid;
5092 BTRFS_I(inode)->last_log_commit = root->last_log_commit;
5093 return 0;
5094 }
5095
5096 /*
5097 * 1 - for the one we're dropping
5098 * 1 - for the one we're adding
5099 * 1 - for updating the inode.
5100 */
5101 trans = btrfs_start_transaction(root, 3);
5102 if (IS_ERR(trans))
5103 return PTR_ERR(trans);
5104
5105 ret = btrfs_drop_extents(trans, root, inode, offset, offset + len, 1);
5106 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04005107 btrfs_abort_transaction(trans, ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005108 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04005109 return ret;
5110 }
5111
David Sterbaf85b7372017-01-20 14:54:07 +01005112 ret = btrfs_insert_file_extent(trans, root, btrfs_ino(BTRFS_I(inode)),
5113 offset, 0, 0, len, 0, len, 0, 0, 0);
Josef Bacik16e75492013-10-22 12:18:51 -04005114 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04005115 btrfs_abort_transaction(trans, ret);
Josef Bacik16e75492013-10-22 12:18:51 -04005116 else
5117 btrfs_update_inode(trans, root, inode);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005118 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04005119 return ret;
5120}
5121
Josef Bacik695a0d02011-03-04 15:46:53 -05005122/*
5123 * This function puts in dummy file extents for the area we're creating a hole
5124 * for. So if we are truncating this file to a larger size we need to insert
5125 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
5126 * the range between oldsize and size
5127 */
Josef Bacika41ad392011-01-31 15:30:16 -05005128int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04005129{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005130 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Yan Zheng9036c102008-10-30 14:19:41 -04005131 struct btrfs_root *root = BTRFS_I(inode)->root;
5132 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04005133 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00005134 struct extent_state *cached_state = NULL;
Josef Bacik5dc562c2012-08-17 13:14:17 -04005135 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005136 u64 hole_start = ALIGN(oldsize, fs_info->sectorsize);
5137 u64 block_end = ALIGN(size, fs_info->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04005138 u64 last_byte;
5139 u64 cur_offset;
5140 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04005141 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04005142
Josef Bacika71754f2013-06-17 17:14:39 -04005143 /*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05305144 * If our size started in the middle of a block we need to zero out the
5145 * rest of the block before we expand the i_size, otherwise we could
Josef Bacika71754f2013-06-17 17:14:39 -04005146 * expose stale data.
5147 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05305148 err = btrfs_truncate_block(inode, oldsize, 0, 0);
Josef Bacika71754f2013-06-17 17:14:39 -04005149 if (err)
5150 return err;
5151
Yan Zheng9036c102008-10-30 14:19:41 -04005152 if (size <= hole_start)
5153 return 0;
5154
Nikolay Borisov23d31bd2019-05-07 10:19:23 +03005155 btrfs_lock_and_flush_ordered_range(io_tree, BTRFS_I(inode), hole_start,
5156 block_end - 1, &cached_state);
Yan Zheng9036c102008-10-30 14:19:41 -04005157 cur_offset = hole_start;
5158 while (1) {
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02005159 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, cur_offset,
Yan Zheng9036c102008-10-30 14:19:41 -04005160 block_end - cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005161 if (IS_ERR(em)) {
5162 err = PTR_ERR(em);
Zach Brownf2767952013-01-08 19:37:58 +00005163 em = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005164 break;
5165 }
Yan Zheng9036c102008-10-30 14:19:41 -04005166 last_byte = min(extent_map_end(em), block_end);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005167 last_byte = ALIGN(last_byte, fs_info->sectorsize);
Yan, Zheng80825102009-11-12 09:35:36 +00005168 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04005169 struct extent_map *hole_em;
Yan Zheng9036c102008-10-30 14:19:41 -04005170 hole_size = last_byte - cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00005171
Josef Bacik16e75492013-10-22 12:18:51 -04005172 err = maybe_insert_hole(root, inode, cur_offset,
5173 hole_size);
5174 if (err)
Chris Mason771ed682008-11-06 22:02:51 -05005175 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02005176 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04005177 cur_offset + hole_size - 1, 0);
5178 hole_em = alloc_extent_map();
5179 if (!hole_em) {
5180 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
5181 &BTRFS_I(inode)->runtime_flags);
5182 goto next;
5183 }
5184 hole_em->start = cur_offset;
5185 hole_em->len = hole_size;
5186 hole_em->orig_start = cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00005187
Josef Bacik5dc562c2012-08-17 13:14:17 -04005188 hole_em->block_start = EXTENT_MAP_HOLE;
5189 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05005190 hole_em->orig_block_len = 0;
Josef Bacikcc95bef2013-04-04 14:31:27 -04005191 hole_em->ram_bytes = hole_size;
Josef Bacik5dc562c2012-08-17 13:14:17 -04005192 hole_em->compress_type = BTRFS_COMPRESS_NONE;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005193 hole_em->generation = fs_info->generation;
Josef Bacik5dc562c2012-08-17 13:14:17 -04005194
5195 while (1) {
5196 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04005197 err = add_extent_mapping(em_tree, hole_em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04005198 write_unlock(&em_tree->lock);
5199 if (err != -EEXIST)
5200 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02005201 btrfs_drop_extent_cache(BTRFS_I(inode),
5202 cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04005203 cur_offset +
5204 hole_size - 1, 0);
5205 }
5206 free_extent_map(hole_em);
Yan Zheng9036c102008-10-30 14:19:41 -04005207 }
Josef Bacik16e75492013-10-22 12:18:51 -04005208next:
Yan Zheng9036c102008-10-30 14:19:41 -04005209 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04005210 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04005211 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00005212 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04005213 break;
5214 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04005215 free_extent_map(em);
David Sterbae43bbe52017-12-12 21:43:52 +01005216 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state);
Yan Zheng9036c102008-10-30 14:19:41 -04005217 return err;
5218}
5219
Eric Sandeen3972f262013-01-12 02:57:22 +00005220static int btrfs_setsize(struct inode *inode, struct iattr *attr)
Yan, Zheng80825102009-11-12 09:35:36 +00005221{
Miao Xief4a2f4c2011-12-14 20:12:01 -05005222 struct btrfs_root *root = BTRFS_I(inode)->root;
5223 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05005224 loff_t oldsize = i_size_read(inode);
Eric Sandeen3972f262013-01-12 02:57:22 +00005225 loff_t newsize = attr->ia_size;
5226 int mask = attr->ia_valid;
Yan, Zheng80825102009-11-12 09:35:36 +00005227 int ret;
5228
Eric Sandeen3972f262013-01-12 02:57:22 +00005229 /*
5230 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
5231 * special case where we need to update the times despite not having
5232 * these flags set. For all other operations the VFS set these flags
5233 * explicitly if it wants a timestamp update.
5234 */
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08005235 if (newsize != oldsize) {
5236 inode_inc_iversion(inode);
5237 if (!(mask & (ATTR_CTIME | ATTR_MTIME)))
5238 inode->i_ctime = inode->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07005239 current_time(inode);
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08005240 }
Eric Sandeen3972f262013-01-12 02:57:22 +00005241
Josef Bacika41ad392011-01-31 15:30:16 -05005242 if (newsize > oldsize) {
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005243 /*
David Sterbaea14b57f2017-06-22 02:19:11 +02005244 * Don't do an expanding truncate while snapshotting is ongoing.
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005245 * This is to ensure the snapshot captures a fully consistent
5246 * state of this file - if the snapshot captures this expanding
5247 * truncation, it must capture all writes that happened before
5248 * this truncation.
5249 */
Zhao Lei0bc19f902016-01-06 18:56:36 +08005250 btrfs_wait_for_snapshot_creation(root);
Josef Bacika41ad392011-01-31 15:30:16 -05005251 ret = btrfs_cont_expand(inode, oldsize, newsize);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005252 if (ret) {
David Sterbaea14b57f2017-06-22 02:19:11 +02005253 btrfs_end_write_no_snapshotting(root);
Yan, Zheng80825102009-11-12 09:35:36 +00005254 return ret;
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005255 }
Yan, Zheng80825102009-11-12 09:35:36 +00005256
Miao Xief4a2f4c2011-12-14 20:12:01 -05005257 trans = btrfs_start_transaction(root, 1);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005258 if (IS_ERR(trans)) {
David Sterbaea14b57f2017-06-22 02:19:11 +02005259 btrfs_end_write_no_snapshotting(root);
Miao Xief4a2f4c2011-12-14 20:12:01 -05005260 return PTR_ERR(trans);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005261 }
Miao Xief4a2f4c2011-12-14 20:12:01 -05005262
5263 i_size_write(inode, newsize);
5264 btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
Chandan Rajendra27772b62016-01-21 15:56:03 +05305265 pagecache_isize_extended(inode, oldsize, newsize);
Miao Xief4a2f4c2011-12-14 20:12:01 -05005266 ret = btrfs_update_inode(trans, root, inode);
David Sterbaea14b57f2017-06-22 02:19:11 +02005267 btrfs_end_write_no_snapshotting(root);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005268 btrfs_end_transaction(trans);
Josef Bacika41ad392011-01-31 15:30:16 -05005269 } else {
Yan, Zheng80825102009-11-12 09:35:36 +00005270
Josef Bacika41ad392011-01-31 15:30:16 -05005271 /*
5272 * We're truncating a file that used to have good data down to
5273 * zero. Make sure it gets into the ordered flush list so that
5274 * any new writes get down to disk quickly.
5275 */
5276 if (newsize == 0)
Josef Bacik72ac3c02012-05-23 14:13:11 -04005277 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
5278 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00005279
Josef Bacika41ad392011-01-31 15:30:16 -05005280 truncate_setsize(inode, newsize);
Miao Xie2e60a512013-02-08 07:01:08 +00005281
Andrea Gelmini52042d82018-11-28 12:05:13 +01005282 /* Disable nonlocked read DIO to avoid the endless truncate */
Nikolay Borisovabcefb12017-02-20 13:51:10 +02005283 btrfs_inode_block_unlocked_dio(BTRFS_I(inode));
Miao Xie2e60a512013-02-08 07:01:08 +00005284 inode_dio_wait(inode);
Nikolay Borisov0b581702017-02-20 13:51:11 +02005285 btrfs_inode_resume_unlocked_dio(BTRFS_I(inode));
Miao Xie2e60a512013-02-08 07:01:08 +00005286
Filipe Manana213e8c52018-02-06 20:40:31 +00005287 ret = btrfs_truncate(inode, newsize == oldsize);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005288 if (ret && inode->i_nlink) {
5289 int err;
5290
5291 /*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07005292 * Truncate failed, so fix up the in-memory size. We
5293 * adjusted disk_i_size down as we removed extents, so
5294 * wait for disk_i_size to be stable and then update the
5295 * in-memory size to match.
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005296 */
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07005297 err = btrfs_wait_ordered_range(inode, 0, (u64)-1);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005298 if (err)
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07005299 return err;
5300 i_size_write(inode, BTRFS_I(inode)->disk_i_size);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005301 }
Yan, Zheng80825102009-11-12 09:35:36 +00005302 }
5303
Josef Bacika41ad392011-01-31 15:30:16 -05005304 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00005305}
5306
Chris Mason39279cc2007-06-12 06:35:45 -04005307static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
5308{
David Howells2b0143b2015-03-17 22:25:59 +00005309 struct inode *inode = d_inode(dentry);
Li Zefanb83cc962010-12-20 16:04:08 +08005310 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005311 int err;
5312
Li Zefanb83cc962010-12-20 16:04:08 +08005313 if (btrfs_root_readonly(root))
5314 return -EROFS;
5315
Jan Kara31051c82016-05-26 16:55:18 +02005316 err = setattr_prepare(dentry, attr);
Chris Mason39279cc2007-06-12 06:35:45 -04005317 if (err)
5318 return err;
5319
Chris Mason5a3f23d2009-03-31 13:27:11 -04005320 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Eric Sandeen3972f262013-01-12 02:57:22 +00005321 err = btrfs_setsize(inode, attr);
Yan, Zheng80825102009-11-12 09:35:36 +00005322 if (err)
5323 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005324 }
Yan Zheng9036c102008-10-30 14:19:41 -04005325
Christoph Hellwig10257742010-06-04 11:30:02 +02005326 if (attr->ia_valid) {
5327 setattr_copy(inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005328 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005329 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04005330
Josef Bacik22c44fe2011-11-30 10:45:38 -05005331 if (!err && attr->ia_valid & ATTR_MODE)
Christoph Hellwig996a7102013-12-20 05:16:43 -08005332 err = posix_acl_chmod(inode, inode->i_mode);
Christoph Hellwig10257742010-06-04 11:30:02 +02005333 }
5334
Chris Mason39279cc2007-06-12 06:35:45 -04005335 return err;
5336}
Chris Mason61295eb2008-01-14 16:24:38 -05005337
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005338/*
5339 * While truncating the inode pages during eviction, we get the VFS calling
5340 * btrfs_invalidatepage() against each page of the inode. This is slow because
5341 * the calls to btrfs_invalidatepage() result in a huge amount of calls to
5342 * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting
5343 * extent_state structures over and over, wasting lots of time.
5344 *
5345 * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all
5346 * those expensive operations on a per page basis and do only the ordered io
5347 * finishing, while we release here the extent_map and extent_state structures,
5348 * without the excessive merging and splitting.
5349 */
5350static void evict_inode_truncate_pages(struct inode *inode)
5351{
5352 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
5353 struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree;
5354 struct rb_node *node;
5355
5356 ASSERT(inode->i_state & I_FREEING);
Johannes Weiner91b0abe2014-04-03 14:47:49 -07005357 truncate_inode_pages_final(&inode->i_data);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005358
5359 write_lock(&map_tree->lock);
Liu Bo07e1ce02018-08-23 03:51:52 +08005360 while (!RB_EMPTY_ROOT(&map_tree->map.rb_root)) {
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005361 struct extent_map *em;
5362
Liu Bo07e1ce02018-08-23 03:51:52 +08005363 node = rb_first_cached(&map_tree->map);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005364 em = rb_entry(node, struct extent_map, rb_node);
Wang Shilong180589e2013-12-14 15:27:31 +08005365 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
5366 clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005367 remove_extent_mapping(map_tree, em);
5368 free_extent_map(em);
Filipe Manana7064dd52014-08-08 02:47:05 +01005369 if (need_resched()) {
5370 write_unlock(&map_tree->lock);
5371 cond_resched();
5372 write_lock(&map_tree->lock);
5373 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005374 }
5375 write_unlock(&map_tree->lock);
5376
Filipe Manana6ca07092015-05-26 00:55:42 +01005377 /*
5378 * Keep looping until we have no more ranges in the io tree.
5379 * We can have ongoing bios started by readpages (called from readahead)
Filipe Manana9c6429d2015-06-10 12:55:41 +01005380 * that have their endio callback (extent_io.c:end_bio_extent_readpage)
5381 * still in progress (unlocked the pages in the bio but did not yet
5382 * unlocked the ranges in the io tree). Therefore this means some
Filipe Manana6ca07092015-05-26 00:55:42 +01005383 * ranges can still be locked and eviction started because before
5384 * submitting those bios, which are executed by a separate task (work
5385 * queue kthread), inode references (inode->i_count) were not taken
5386 * (which would be dropped in the end io callback of each bio).
5387 * Therefore here we effectively end up waiting for those bios and
5388 * anyone else holding locked ranges without having bumped the inode's
5389 * reference count - if we don't do it, when they access the inode's
5390 * io_tree to unlock a range it may be too late, leading to an
5391 * use-after-free issue.
5392 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005393 spin_lock(&io_tree->lock);
5394 while (!RB_EMPTY_ROOT(&io_tree->state)) {
5395 struct extent_state *state;
5396 struct extent_state *cached_state = NULL;
Filipe Manana6ca07092015-05-26 00:55:42 +01005397 u64 start;
5398 u64 end;
Filipe Manana421f0922018-10-12 13:02:48 +01005399 unsigned state_flags;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005400
5401 node = rb_first(&io_tree->state);
5402 state = rb_entry(node, struct extent_state, rb_node);
Filipe Manana6ca07092015-05-26 00:55:42 +01005403 start = state->start;
5404 end = state->end;
Filipe Manana421f0922018-10-12 13:02:48 +01005405 state_flags = state->state;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005406 spin_unlock(&io_tree->lock);
5407
David Sterbaff13db42015-12-03 14:30:40 +01005408 lock_extent_bits(io_tree, start, end, &cached_state);
Qu Wenruob9d0b382015-09-29 10:35:16 +08005409
5410 /*
5411 * If still has DELALLOC flag, the extent didn't reach disk,
5412 * and its reserved space won't be freed by delayed_ref.
5413 * So we need to free its reserved space here.
5414 * (Refer to comment in btrfs_invalidatepage, case 2)
5415 *
5416 * Note, end is the bytenr of last byte, so we need + 1 here.
5417 */
Filipe Manana421f0922018-10-12 13:02:48 +01005418 if (state_flags & EXTENT_DELALLOC)
Qu Wenruobc42bda2017-02-27 15:10:39 +08005419 btrfs_qgroup_free_data(inode, NULL, start, end - start + 1);
Qu Wenruob9d0b382015-09-29 10:35:16 +08005420
Filipe Manana6ca07092015-05-26 00:55:42 +01005421 clear_extent_bit(io_tree, start, end,
Omar Sandovale1821632019-08-15 14:04:04 -07005422 EXTENT_LOCKED | EXTENT_DELALLOC |
5423 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1,
5424 &cached_state);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005425
Filipe Manana7064dd52014-08-08 02:47:05 +01005426 cond_resched();
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005427 spin_lock(&io_tree->lock);
5428 }
5429 spin_unlock(&io_tree->lock);
5430}
5431
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005432static struct btrfs_trans_handle *evict_refill_and_join(struct btrfs_root *root,
Josef Bacikad80cf52018-09-28 07:18:19 -04005433 struct btrfs_block_rsv *rsv)
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005434{
5435 struct btrfs_fs_info *fs_info = root->fs_info;
5436 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
Josef Bacikd3984c92019-08-01 18:19:37 -04005437 struct btrfs_trans_handle *trans;
Josef Bacik2bd36e72019-08-22 15:14:33 -04005438 u64 delayed_refs_extra = btrfs_calc_insert_metadata_size(fs_info, 1);
Josef Bacikd3984c92019-08-01 18:19:37 -04005439 int ret;
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005440
Josef Bacikd3984c92019-08-01 18:19:37 -04005441 /*
5442 * Eviction should be taking place at some place safe because of our
5443 * delayed iputs. However the normal flushing code will run delayed
5444 * iputs, so we cannot use FLUSH_ALL otherwise we'll deadlock.
5445 *
5446 * We reserve the delayed_refs_extra here again because we can't use
5447 * btrfs_start_transaction(root, 0) for the same deadlocky reason as
5448 * above. We reserve our extra bit here because we generate a ton of
5449 * delayed refs activity by truncating.
5450 *
5451 * If we cannot make our reservation we'll attempt to steal from the
5452 * global reserve, because we really want to be able to free up space.
5453 */
5454 ret = btrfs_block_rsv_refill(root, rsv, rsv->size + delayed_refs_extra,
5455 BTRFS_RESERVE_FLUSH_EVICT);
5456 if (ret) {
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005457 /*
5458 * Try to steal from the global reserve if there is space for
5459 * it.
5460 */
Josef Bacikd3984c92019-08-01 18:19:37 -04005461 if (btrfs_check_space_for_delayed_refs(fs_info) ||
5462 btrfs_block_rsv_migrate(global_rsv, rsv, rsv->size, 0)) {
5463 btrfs_warn(fs_info,
5464 "could not allocate space for delete; will truncate on mount");
5465 return ERR_PTR(-ENOSPC);
5466 }
5467 delayed_refs_extra = 0;
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005468 }
Josef Bacikd3984c92019-08-01 18:19:37 -04005469
5470 trans = btrfs_join_transaction(root);
5471 if (IS_ERR(trans))
5472 return trans;
5473
5474 if (delayed_refs_extra) {
5475 trans->block_rsv = &fs_info->trans_block_rsv;
5476 trans->bytes_reserved = delayed_refs_extra;
5477 btrfs_block_rsv_migrate(rsv, trans->block_rsv,
5478 delayed_refs_extra, 1);
5479 }
5480 return trans;
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005481}
5482
Al Virobd555972010-06-07 11:35:40 -04005483void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005484{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005485 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04005486 struct btrfs_trans_handle *trans;
5487 struct btrfs_root *root = BTRFS_I(inode)->root;
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005488 struct btrfs_block_rsv *rsv;
Chris Mason39279cc2007-06-12 06:35:45 -04005489 int ret;
5490
liubo1abe9b82011-03-24 11:18:59 +00005491 trace_btrfs_inode_evict(inode);
5492
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005493 if (!root) {
Liu Boe8f1bc12018-01-25 11:02:53 -07005494 clear_inode(inode);
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005495 return;
5496 }
5497
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005498 evict_inode_truncate_pages(inode);
5499
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005500 if (inode->i_nlink &&
5501 ((btrfs_root_refs(&root->root_item) != 0 &&
5502 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) ||
Nikolay Borisov70ddc552017-02-20 13:50:35 +02005503 btrfs_is_free_space_inode(BTRFS_I(inode))))
Al Virobd555972010-06-07 11:35:40 -04005504 goto no_delete;
5505
Omar Sandoval27919062018-05-11 13:13:37 -07005506 if (is_bad_inode(inode))
Chris Mason39279cc2007-06-12 06:35:45 -04005507 goto no_delete;
Chris Mason5f39d392007-10-15 16:14:19 -04005508
Nikolay Borisov7ab79562017-02-20 13:50:57 +02005509 btrfs_free_io_failure_record(BTRFS_I(inode), 0, (u64)-1);
Miao Xief6124962014-09-12 18:44:04 +08005510
Omar Sandoval7b40b692018-05-11 13:13:33 -07005511 if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags))
Yan, Zhengc71bf092009-11-12 09:34:40 +00005512 goto no_delete;
Yan, Zhengc71bf092009-11-12 09:34:40 +00005513
Yan, Zheng76dda932009-09-21 16:00:26 -04005514 if (inode->i_nlink > 0) {
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005515 BUG_ON(btrfs_root_refs(&root->root_item) != 0 &&
5516 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID);
Yan, Zheng76dda932009-09-21 16:00:26 -04005517 goto no_delete;
5518 }
5519
Nikolay Borisovaa790212017-01-10 20:35:40 +02005520 ret = btrfs_commit_inode_delayed_inode(BTRFS_I(inode));
Omar Sandoval27919062018-05-11 13:13:37 -07005521 if (ret)
Miao Xie0e8c36a2012-12-19 06:59:51 +00005522 goto no_delete;
Miao Xie0e8c36a2012-12-19 06:59:51 +00005523
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005524 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Omar Sandoval27919062018-05-11 13:13:37 -07005525 if (!rsv)
Josef Bacik4289a662011-08-05 13:22:24 -04005526 goto no_delete;
Josef Bacik2bd36e72019-08-22 15:14:33 -04005527 rsv->size = btrfs_calc_metadata_size(fs_info, 1);
Josef Bacikca7e70f2012-08-27 17:48:15 -04005528 rsv->failfast = 1;
Josef Bacik4289a662011-08-05 13:22:24 -04005529
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02005530 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Mason5f39d392007-10-15 16:14:19 -04005531
Yan, Zheng80825102009-11-12 09:35:36 +00005532 while (1) {
Josef Bacikad80cf52018-09-28 07:18:19 -04005533 trans = evict_refill_and_join(root, rsv);
Omar Sandoval27919062018-05-11 13:13:37 -07005534 if (IS_ERR(trans))
5535 goto free_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04005536
5537 trans->block_rsv = rsv;
5538
Yan, Zhengd68fc572010-05-16 10:49:58 -04005539 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Omar Sandoval27919062018-05-11 13:13:37 -07005540 trans->block_rsv = &fs_info->trans_block_rsv;
5541 btrfs_end_transaction(trans);
5542 btrfs_btree_balance_dirty(fs_info);
5543 if (ret && ret != -ENOSPC && ret != -EAGAIN)
5544 goto free_rsv;
5545 else if (!ret)
Yan, Zheng80825102009-11-12 09:35:36 +00005546 break;
Josef Bacik7b128762008-07-24 12:17:14 -04005547 }
5548
Josef Bacik4ef31a42013-08-13 14:10:08 -04005549 /*
Omar Sandoval27919062018-05-11 13:13:37 -07005550 * Errors here aren't a big deal, it just means we leave orphan items in
5551 * the tree. They will be cleaned up on the next mount. If the inode
5552 * number gets reused, cleanup deletes the orphan item without doing
5553 * anything, and unlink reuses the existing orphan item.
5554 *
5555 * If it turns out that we are dropping too many of these, we might want
5556 * to add a mechanism for retrying these after a commit.
Josef Bacik4ef31a42013-08-13 14:10:08 -04005557 */
Josef Bacikad80cf52018-09-28 07:18:19 -04005558 trans = evict_refill_and_join(root, rsv);
Omar Sandoval27919062018-05-11 13:13:37 -07005559 if (!IS_ERR(trans)) {
5560 trans->block_rsv = rsv;
5561 btrfs_orphan_del(trans, BTRFS_I(inode));
5562 trans->block_rsv = &fs_info->trans_block_rsv;
5563 btrfs_end_transaction(trans);
5564 }
Chris Mason85e21ba2008-01-29 15:11:36 -05005565
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005566 if (!(root == fs_info->tree_root ||
Li Zefan581bb052011-04-20 10:06:11 +08005567 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005568 btrfs_return_ino(root, btrfs_ino(BTRFS_I(inode)));
Li Zefan581bb052011-04-20 10:06:11 +08005569
Omar Sandoval27919062018-05-11 13:13:37 -07005570free_rsv:
5571 btrfs_free_block_rsv(fs_info, rsv);
Chris Mason39279cc2007-06-12 06:35:45 -04005572no_delete:
Omar Sandoval27919062018-05-11 13:13:37 -07005573 /*
5574 * If we didn't successfully delete, the orphan item will still be in
5575 * the tree and we'll retry on the next mount. Again, we might also want
5576 * to retry these periodically in the future.
5577 */
Nikolay Borisovf48d1cf2017-01-10 20:35:39 +02005578 btrfs_remove_delayed_node(BTRFS_I(inode));
Jan Karadbd57682012-05-03 14:48:02 +02005579 clear_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005580}
5581
5582/*
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005583 * Return the key found in the dir entry in the location pointer, fill @type
5584 * with BTRFS_FT_*, and return 0.
5585 *
Su Yue005d6712018-03-05 17:13:37 +08005586 * If no dir entries were found, returns -ENOENT.
5587 * If found a corrupted location in dir entry, returns -EUCLEAN.
Chris Mason39279cc2007-06-12 06:35:45 -04005588 */
5589static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005590 struct btrfs_key *location, u8 *type)
Chris Mason39279cc2007-06-12 06:35:45 -04005591{
5592 const char *name = dentry->d_name.name;
5593 int namelen = dentry->d_name.len;
5594 struct btrfs_dir_item *di;
5595 struct btrfs_path *path;
5596 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04005597 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005598
5599 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005600 if (!path)
5601 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05005602
David Sterbaf85b7372017-01-20 14:54:07 +01005603 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(BTRFS_I(dir)),
5604 name, namelen, 0);
Liu Bo3cf50682018-09-12 06:06:26 +08005605 if (IS_ERR_OR_NULL(di)) {
5606 ret = di ? PTR_ERR(di) : -ENOENT;
Su Yue005d6712018-03-05 17:13:37 +08005607 goto out;
5608 }
Chris Masond3977122009-01-05 21:25:51 -05005609
Chris Mason5f39d392007-10-15 16:14:19 -04005610 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Liu Bo56a0e702017-10-30 11:14:38 -06005611 if (location->type != BTRFS_INODE_ITEM_KEY &&
5612 location->type != BTRFS_ROOT_ITEM_KEY) {
Su Yue005d6712018-03-05 17:13:37 +08005613 ret = -EUCLEAN;
Liu Bo56a0e702017-10-30 11:14:38 -06005614 btrfs_warn(root->fs_info,
5615"%s gets something invalid in DIR_ITEM (name %s, directory ino %llu, location(%llu %u %llu))",
5616 __func__, name, btrfs_ino(BTRFS_I(dir)),
5617 location->objectid, location->type, location->offset);
Liu Bo56a0e702017-10-30 11:14:38 -06005618 }
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005619 if (!ret)
5620 *type = btrfs_dir_type(path->nodes[0], di);
Chris Mason39279cc2007-06-12 06:35:45 -04005621out:
Chris Mason39279cc2007-06-12 06:35:45 -04005622 btrfs_free_path(path);
5623 return ret;
5624}
5625
5626/*
5627 * when we hit a tree root in a directory, the btrfs part of the inode
5628 * needs to be changed to reflect the root directory of the tree root. This
5629 * is kind of like crossing a mount point.
5630 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005631static int fixup_tree_root_location(struct btrfs_fs_info *fs_info,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005632 struct inode *dir,
5633 struct dentry *dentry,
5634 struct btrfs_key *location,
5635 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04005636{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005637 struct btrfs_path *path;
5638 struct btrfs_root *new_root;
5639 struct btrfs_root_ref *ref;
5640 struct extent_buffer *leaf;
David Sterba1d4c08e2015-01-02 19:36:14 +01005641 struct btrfs_key key;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005642 int ret;
5643 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005644
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005645 path = btrfs_alloc_path();
5646 if (!path) {
5647 err = -ENOMEM;
5648 goto out;
5649 }
Chris Mason39279cc2007-06-12 06:35:45 -04005650
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005651 err = -ENOENT;
David Sterba1d4c08e2015-01-02 19:36:14 +01005652 key.objectid = BTRFS_I(dir)->root->root_key.objectid;
5653 key.type = BTRFS_ROOT_REF_KEY;
5654 key.offset = location->objectid;
5655
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005656 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005657 if (ret) {
5658 if (ret < 0)
5659 err = ret;
5660 goto out;
5661 }
Chris Mason39279cc2007-06-12 06:35:45 -04005662
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005663 leaf = path->nodes[0];
5664 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005665 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(BTRFS_I(dir)) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005666 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
5667 goto out;
5668
5669 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
5670 (unsigned long)(ref + 1),
5671 dentry->d_name.len);
5672 if (ret)
5673 goto out;
5674
David Sterbab3b4aa72011-04-21 01:20:15 +02005675 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005676
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005677 new_root = btrfs_read_fs_root_no_name(fs_info, location);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005678 if (IS_ERR(new_root)) {
5679 err = PTR_ERR(new_root);
5680 goto out;
5681 }
5682
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005683 *sub_root = new_root;
5684 location->objectid = btrfs_root_dirid(&new_root->root_item);
5685 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04005686 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005687 err = 0;
5688out:
5689 btrfs_free_path(path);
5690 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005691}
5692
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005693static void inode_tree_add(struct inode *inode)
5694{
5695 struct btrfs_root *root = BTRFS_I(inode)->root;
5696 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005697 struct rb_node **p;
5698 struct rb_node *parent;
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005699 struct rb_node *new = &BTRFS_I(inode)->rb_node;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005700 u64 ino = btrfs_ino(BTRFS_I(inode));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005701
Al Viro1d3382cb2010-10-23 15:19:20 -04005702 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005703 return;
Miao Xiee1409ce2013-05-15 07:48:16 +00005704 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005705 spin_lock(&root->inode_lock);
Miao Xiee1409ce2013-05-15 07:48:16 +00005706 p = &root->inode_tree.rb_node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005707 while (*p) {
5708 parent = *p;
5709 entry = rb_entry(parent, struct btrfs_inode, rb_node);
5710
David Sterba37508512018-06-29 10:56:40 +02005711 if (ino < btrfs_ino(entry))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005712 p = &parent->rb_left;
David Sterba37508512018-06-29 10:56:40 +02005713 else if (ino > btrfs_ino(entry))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005714 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005715 else {
5716 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04005717 (I_WILL_FREE | I_FREEING)));
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005718 rb_replace_node(parent, new, &root->inode_tree);
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005719 RB_CLEAR_NODE(parent);
5720 spin_unlock(&root->inode_lock);
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005721 return;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005722 }
5723 }
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005724 rb_link_node(new, parent, p);
5725 rb_insert_color(new, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005726 spin_unlock(&root->inode_lock);
5727}
5728
5729static void inode_tree_del(struct inode *inode)
5730{
5731 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) {
Yan, Zheng76dda932009-09-21 16:00:26 -04005743 spin_lock(&root->inode_lock);
5744 empty = RB_EMPTY_ROOT(&root->inode_tree);
5745 spin_unlock(&root->inode_lock);
5746 if (empty)
5747 btrfs_add_dead_root(root);
5748 }
5749}
5750
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005751
Chris Masone02119d2008-09-05 16:13:11 -04005752static int btrfs_init_locked_inode(struct inode *inode, void *p)
5753{
5754 struct btrfs_iget_args *args = p;
Chris Mason90d3e592014-01-09 17:28:00 -08005755 inode->i_ino = args->location->objectid;
5756 memcpy(&BTRFS_I(inode)->location, args->location,
5757 sizeof(*args->location));
Chris Masone02119d2008-09-05 16:13:11 -04005758 BTRFS_I(inode)->root = args->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005759 return 0;
5760}
5761
5762static int btrfs_find_actor(struct inode *inode, void *opaque)
5763{
5764 struct btrfs_iget_args *args = opaque;
Chris Mason90d3e592014-01-09 17:28:00 -08005765 return args->location->objectid == BTRFS_I(inode)->location.objectid &&
Chris Masond3977122009-01-05 21:25:51 -05005766 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005767}
5768
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005769static struct inode *btrfs_iget_locked(struct super_block *s,
Chris Mason90d3e592014-01-09 17:28:00 -08005770 struct btrfs_key *location,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005771 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04005772{
5773 struct inode *inode;
5774 struct btrfs_iget_args args;
Chris Mason90d3e592014-01-09 17:28:00 -08005775 unsigned long hashval = btrfs_inode_hash(location->objectid, root);
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005776
Chris Mason90d3e592014-01-09 17:28:00 -08005777 args.location = location;
Chris Mason39279cc2007-06-12 06:35:45 -04005778 args.root = root;
5779
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005780 inode = iget5_locked(s, hashval, btrfs_find_actor,
Chris Mason39279cc2007-06-12 06:35:45 -04005781 btrfs_init_locked_inode,
5782 (void *)&args);
5783 return inode;
5784}
5785
David Sterba4c66e0d2019-10-03 19:09:35 +02005786/*
5787 * Get an inode object given its location and corresponding root.
5788 * Path can be preallocated to prevent recursing back to iget through
5789 * allocator. NULL is also valid but may require an additional allocation
5790 * later.
Balaji Rao1a54ef82008-07-21 02:01:04 +05305791 */
Filipe Manana4222ea72018-10-24 10:13:03 +01005792struct inode *btrfs_iget_path(struct super_block *s, struct btrfs_key *location,
David Sterba4c66e0d2019-10-03 19:09:35 +02005793 struct btrfs_root *root, struct btrfs_path *path)
Balaji Rao1a54ef82008-07-21 02:01:04 +05305794{
5795 struct inode *inode;
5796
Chris Mason90d3e592014-01-09 17:28:00 -08005797 inode = btrfs_iget_locked(s, location, root);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305798 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005799 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305800
5801 if (inode->i_state & I_NEW) {
Filipe Manana67710892016-06-06 11:51:25 +01005802 int ret;
5803
Filipe Manana4222ea72018-10-24 10:13:03 +01005804 ret = btrfs_read_locked_inode(inode, path);
Al Viro9bc2cef2018-07-29 23:04:50 +01005805 if (!ret) {
Mark Fasheh1748f842011-07-12 11:25:31 -07005806 inode_tree_add(inode);
5807 unlock_new_inode(inode);
Mark Fasheh1748f842011-07-12 11:25:31 -07005808 } else {
Al Virof5b3a412018-07-29 23:04:51 +01005809 iget_failed(inode);
5810 /*
5811 * ret > 0 can come from btrfs_search_slot called by
5812 * btrfs_read_locked_inode, this means the inode item
5813 * was not found.
5814 */
5815 if (ret > 0)
5816 ret = -ENOENT;
5817 inode = ERR_PTR(ret);
Mark Fasheh1748f842011-07-12 11:25:31 -07005818 }
5819 }
5820
Balaji Rao1a54ef82008-07-21 02:01:04 +05305821 return inode;
5822}
5823
Filipe Manana4222ea72018-10-24 10:13:03 +01005824struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
David Sterba4c66e0d2019-10-03 19:09:35 +02005825 struct btrfs_root *root)
Filipe Manana4222ea72018-10-24 10:13:03 +01005826{
David Sterba4c66e0d2019-10-03 19:09:35 +02005827 return btrfs_iget_path(s, location, root, NULL);
Filipe Manana4222ea72018-10-24 10:13:03 +01005828}
5829
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005830static struct inode *new_simple_dir(struct super_block *s,
5831 struct btrfs_key *key,
5832 struct btrfs_root *root)
5833{
5834 struct inode *inode = new_inode(s);
5835
5836 if (!inode)
5837 return ERR_PTR(-ENOMEM);
5838
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005839 BTRFS_I(inode)->root = root;
5840 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04005841 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005842
5843 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Li Zefan848cce02012-02-21 17:04:28 +08005844 inode->i_op = &btrfs_dir_ro_inode_operations;
Omar Sandoval1fdf4192017-01-25 17:06:39 -08005845 inode->i_opflags &= ~IOP_XATTR;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005846 inode->i_fop = &simple_dir_operations;
5847 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
Deepa Dinamanic2050a42016-09-14 07:48:06 -07005848 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05305849 inode->i_atime = inode->i_mtime;
5850 inode->i_ctime = inode->i_mtime;
Arnd Bergmannd3c6be6f2018-06-21 18:04:06 +02005851 BTRFS_I(inode)->i_otime = inode->i_mtime;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005852
5853 return inode;
5854}
5855
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005856static inline u8 btrfs_inode_type(struct inode *inode)
5857{
5858 /*
5859 * Compile-time asserts that generic FT_* types still match
5860 * BTRFS_FT_* types
5861 */
5862 BUILD_BUG_ON(BTRFS_FT_UNKNOWN != FT_UNKNOWN);
5863 BUILD_BUG_ON(BTRFS_FT_REG_FILE != FT_REG_FILE);
5864 BUILD_BUG_ON(BTRFS_FT_DIR != FT_DIR);
5865 BUILD_BUG_ON(BTRFS_FT_CHRDEV != FT_CHRDEV);
5866 BUILD_BUG_ON(BTRFS_FT_BLKDEV != FT_BLKDEV);
5867 BUILD_BUG_ON(BTRFS_FT_FIFO != FT_FIFO);
5868 BUILD_BUG_ON(BTRFS_FT_SOCK != FT_SOCK);
5869 BUILD_BUG_ON(BTRFS_FT_SYMLINK != FT_SYMLINK);
5870
5871 return fs_umode_to_ftype(inode->i_mode);
5872}
5873
Chris Mason3de45862008-11-17 21:02:50 -05005874struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04005875{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005876 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masond3977122009-01-05 21:25:51 -05005877 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005878 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005879 struct btrfs_root *sub_root = root;
5880 struct btrfs_key location;
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005881 u8 di_type = 0;
Yan, Zheng76dda932009-09-21 16:00:26 -04005882 int index;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005883 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005884
5885 if (dentry->d_name.len > BTRFS_NAME_LEN)
5886 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04005887
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005888 ret = btrfs_inode_by_name(dir, dentry, &location, &di_type);
Chris Mason39279cc2007-06-12 06:35:45 -04005889 if (ret < 0)
5890 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04005891
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005892 if (location.type == BTRFS_INODE_ITEM_KEY) {
David Sterba4c66e0d2019-10-03 19:09:35 +02005893 inode = btrfs_iget(dir->i_sb, &location, root);
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005894 if (IS_ERR(inode))
5895 return inode;
5896
5897 /* Do extra check against inode mode with di_type */
5898 if (btrfs_inode_type(inode) != di_type) {
5899 btrfs_crit(fs_info,
5900"inode mode mismatch with dir: inode mode=0%o btrfs type=%u dir type=%u",
5901 inode->i_mode, btrfs_inode_type(inode),
5902 di_type);
5903 iput(inode);
5904 return ERR_PTR(-EUCLEAN);
5905 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005906 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04005907 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005908
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005909 index = srcu_read_lock(&fs_info->subvol_srcu);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005910 ret = fixup_tree_root_location(fs_info, dir, dentry,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005911 &location, &sub_root);
5912 if (ret < 0) {
5913 if (ret != -ENOENT)
5914 inode = ERR_PTR(ret);
5915 else
5916 inode = new_simple_dir(dir->i_sb, &location, sub_root);
5917 } else {
David Sterba4c66e0d2019-10-03 19:09:35 +02005918 inode = btrfs_iget(dir->i_sb, &location, sub_root);
Chris Mason39279cc2007-06-12 06:35:45 -04005919 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005920 srcu_read_unlock(&fs_info->subvol_srcu, index);
Yan, Zheng76dda932009-09-21 16:00:26 -04005921
Julia Lawall34d19ba2011-01-24 19:55:19 +00005922 if (!IS_ERR(inode) && root != sub_root) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005923 down_read(&fs_info->cleanup_work_sem);
David Howellsbc98a422017-07-17 08:45:34 +01005924 if (!sb_rdonly(inode->i_sb))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005925 ret = btrfs_orphan_cleanup(sub_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005926 up_read(&fs_info->cleanup_work_sem);
Josef Bacik01cd3362013-06-03 21:39:49 -04005927 if (ret) {
5928 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005929 inode = ERR_PTR(ret);
Josef Bacik01cd3362013-06-03 21:39:49 -04005930 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00005931 }
5932
Chris Mason3de45862008-11-17 21:02:50 -05005933 return inode;
5934}
5935
Nick Pigginfe15ce42011-01-07 17:49:23 +11005936static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04005937{
5938 struct btrfs_root *root;
David Howells2b0143b2015-03-17 22:25:59 +00005939 struct inode *inode = d_inode(dentry);
Yan, Zheng76dda932009-09-21 16:00:26 -04005940
Li Zefan848cce02012-02-21 17:04:28 +08005941 if (!inode && !IS_ROOT(dentry))
David Howells2b0143b2015-03-17 22:25:59 +00005942 inode = d_inode(dentry->d_parent);
Yan, Zheng76dda932009-09-21 16:00:26 -04005943
Li Zefan848cce02012-02-21 17:04:28 +08005944 if (inode) {
5945 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04005946 if (btrfs_root_refs(&root->root_item) == 0)
5947 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08005948
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005949 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Li Zefan848cce02012-02-21 17:04:28 +08005950 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04005951 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005952 return 0;
5953}
5954
Chris Mason3de45862008-11-17 21:02:50 -05005955static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04005956 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05005957{
Al Viro3837d202018-10-10 16:38:27 -04005958 struct inode *inode = btrfs_lookup_dentry(dir, dentry);
Josef Bacika66e7cc2011-09-18 10:34:03 -04005959
Al Viro3837d202018-10-10 16:38:27 -04005960 if (inode == ERR_PTR(-ENOENT))
5961 inode = NULL;
Al Viro41d28bc2014-10-12 22:24:21 -04005962 return d_splice_alias(inode, dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04005963}
5964
Josef Bacik23b5ec72017-07-24 15:14:25 -04005965/*
5966 * All this infrastructure exists because dir_emit can fault, and we are holding
5967 * the tree lock when doing readdir. For now just allocate a buffer and copy
5968 * our information into that, and then dir_emit from the buffer. This is
5969 * similar to what NFS does, only we don't keep the buffer around in pagecache
5970 * because I'm afraid I'll mess that up. Long term we need to make filldir do
5971 * copy_to_user_inatomic so we don't have to worry about page faulting under the
5972 * tree lock.
5973 */
5974static int btrfs_opendir(struct inode *inode, struct file *file)
5975{
5976 struct btrfs_file_private *private;
5977
5978 private = kzalloc(sizeof(struct btrfs_file_private), GFP_KERNEL);
5979 if (!private)
5980 return -ENOMEM;
5981 private->filldir_buf = kzalloc(PAGE_SIZE, GFP_KERNEL);
5982 if (!private->filldir_buf) {
5983 kfree(private);
5984 return -ENOMEM;
5985 }
5986 file->private_data = private;
5987 return 0;
5988}
5989
5990struct dir_entry {
5991 u64 ino;
5992 u64 offset;
5993 unsigned type;
5994 int name_len;
5995};
5996
5997static int btrfs_filldir(void *addr, int entries, struct dir_context *ctx)
5998{
5999 while (entries--) {
6000 struct dir_entry *entry = addr;
6001 char *name = (char *)(entry + 1);
6002
David Sterba92d32172018-04-16 21:10:14 +02006003 ctx->pos = get_unaligned(&entry->offset);
6004 if (!dir_emit(ctx, name, get_unaligned(&entry->name_len),
6005 get_unaligned(&entry->ino),
6006 get_unaligned(&entry->type)))
Josef Bacik23b5ec72017-07-24 15:14:25 -04006007 return 1;
David Sterba92d32172018-04-16 21:10:14 +02006008 addr += sizeof(struct dir_entry) +
6009 get_unaligned(&entry->name_len);
Josef Bacik23b5ec72017-07-24 15:14:25 -04006010 ctx->pos++;
6011 }
6012 return 0;
6013}
6014
Al Viro9cdda8d2013-05-22 16:48:09 -04006015static int btrfs_real_readdir(struct file *file, struct dir_context *ctx)
Chris Mason39279cc2007-06-12 06:35:45 -04006016{
Al Viro9cdda8d2013-05-22 16:48:09 -04006017 struct inode *inode = file_inode(file);
Chris Mason39279cc2007-06-12 06:35:45 -04006018 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik23b5ec72017-07-24 15:14:25 -04006019 struct btrfs_file_private *private = file->private_data;
Chris Mason39279cc2007-06-12 06:35:45 -04006020 struct btrfs_dir_item *di;
6021 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04006022 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04006023 struct btrfs_path *path;
Josef Bacik23b5ec72017-07-24 15:14:25 -04006024 void *addr;
Miao Xie16cdcec2011-04-22 18:12:22 +08006025 struct list_head ins_list;
6026 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04006027 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04006028 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04006029 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04006030 char *name_ptr;
6031 int name_len;
Josef Bacik23b5ec72017-07-24 15:14:25 -04006032 int entries = 0;
6033 int total_len = 0;
Omar Sandoval02dbfc92016-05-20 13:50:33 -07006034 bool put = false;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006035 struct btrfs_key location;
Chris Mason5f39d392007-10-15 16:14:19 -04006036
Al Viro9cdda8d2013-05-22 16:48:09 -04006037 if (!dir_emit_dots(file, ctx))
6038 return 0;
6039
David Woodhouse49593bf2008-08-17 17:08:36 +01006040 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08006041 if (!path)
6042 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04006043
Josef Bacik23b5ec72017-07-24 15:14:25 -04006044 addr = private->filldir_buf;
David Sterbae4058b52015-11-27 16:31:35 +01006045 path->reada = READA_FORWARD;
David Woodhouse49593bf2008-08-17 17:08:36 +01006046
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006047 INIT_LIST_HEAD(&ins_list);
6048 INIT_LIST_HEAD(&del_list);
6049 put = btrfs_readdir_get_delayed_items(inode, &ins_list, &del_list);
Miao Xie16cdcec2011-04-22 18:12:22 +08006050
Josef Bacik23b5ec72017-07-24 15:14:25 -04006051again:
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006052 key.type = BTRFS_DIR_INDEX_KEY;
Al Viro9cdda8d2013-05-22 16:48:09 -04006053 key.offset = ctx->pos;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02006054 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason5f39d392007-10-15 16:14:19 -04006055
Chris Mason39279cc2007-06-12 06:35:45 -04006056 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
6057 if (ret < 0)
6058 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01006059
6060 while (1) {
Josef Bacik23b5ec72017-07-24 15:14:25 -04006061 struct dir_entry *entry;
6062
Chris Mason5f39d392007-10-15 16:14:19 -04006063 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04006064 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08006065 if (slot >= btrfs_header_nritems(leaf)) {
6066 ret = btrfs_next_leaf(root, path);
6067 if (ret < 0)
6068 goto err;
6069 else if (ret > 0)
6070 break;
6071 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04006072 }
Chris Mason3de45862008-11-17 21:02:50 -05006073
Chris Mason5f39d392007-10-15 16:14:19 -04006074 btrfs_item_key_to_cpu(leaf, &found_key, slot);
6075
6076 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04006077 break;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006078 if (found_key.type != BTRFS_DIR_INDEX_KEY)
Chris Mason39279cc2007-06-12 06:35:45 -04006079 break;
Al Viro9cdda8d2013-05-22 16:48:09 -04006080 if (found_key.offset < ctx->pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08006081 goto next;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006082 if (btrfs_should_delete_dir_index(&del_list, found_key.offset))
Miao Xie16cdcec2011-04-22 18:12:22 +08006083 goto next;
Chris Mason39279cc2007-06-12 06:35:45 -04006084 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006085 name_len = btrfs_dir_name_len(leaf, di);
Josef Bacik23b5ec72017-07-24 15:14:25 -04006086 if ((total_len + sizeof(struct dir_entry) + name_len) >=
6087 PAGE_SIZE) {
6088 btrfs_release_path(path);
6089 ret = btrfs_filldir(private->filldir_buf, entries, ctx);
6090 if (ret)
6091 goto nopos;
6092 addr = private->filldir_buf;
6093 entries = 0;
6094 total_len = 0;
6095 goto again;
Chris Mason39279cc2007-06-12 06:35:45 -04006096 }
Josef Bacik23b5ec72017-07-24 15:14:25 -04006097
6098 entry = addr;
David Sterba92d32172018-04-16 21:10:14 +02006099 put_unaligned(name_len, &entry->name_len);
Josef Bacik23b5ec72017-07-24 15:14:25 -04006100 name_ptr = (char *)(entry + 1);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006101 read_extent_buffer(leaf, name_ptr, (unsigned long)(di + 1),
6102 name_len);
Phillip Potter7d157c32019-03-26 21:39:34 +00006103 put_unaligned(fs_ftype_to_dtype(btrfs_dir_type(leaf, di)),
David Sterba92d32172018-04-16 21:10:14 +02006104 &entry->type);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006105 btrfs_dir_item_key_to_cpu(leaf, di, &location);
David Sterba92d32172018-04-16 21:10:14 +02006106 put_unaligned(location.objectid, &entry->ino);
6107 put_unaligned(found_key.offset, &entry->offset);
Josef Bacik23b5ec72017-07-24 15:14:25 -04006108 entries++;
6109 addr += sizeof(struct dir_entry) + name_len;
6110 total_len += sizeof(struct dir_entry) + name_len;
Li Zefanb9e03af2011-03-23 10:43:58 +08006111next:
6112 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04006113 }
Josef Bacik23b5ec72017-07-24 15:14:25 -04006114 btrfs_release_path(path);
6115
6116 ret = btrfs_filldir(private->filldir_buf, entries, ctx);
6117 if (ret)
6118 goto nopos;
David Woodhouse49593bf2008-08-17 17:08:36 +01006119
Jeff Mahoneyd2fbb2b2016-11-05 13:26:35 -04006120 ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006121 if (ret)
David Sterbabc4ef752015-11-13 13:44:28 +01006122 goto nopos;
6123
Zach Browndb62efb2013-07-11 16:19:42 -07006124 /*
6125 * Stop new entries from being returned after we return the last
6126 * entry.
6127 *
6128 * New directory entries are assigned a strictly increasing
6129 * offset. This means that new entries created during readdir
6130 * are *guaranteed* to be seen in the future by that readdir.
6131 * This has broken buggy programs which operate on names as
6132 * they're returned by readdir. Until we re-use freed offsets
6133 * we have this hack to stop new entries from being returned
6134 * under the assumption that they'll never reach this huge
6135 * offset.
6136 *
6137 * This is being careful not to overflow 32bit loff_t unless the
6138 * last entry requires it because doing so has broken 32bit apps
6139 * in the past.
6140 */
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006141 if (ctx->pos >= INT_MAX)
6142 ctx->pos = LLONG_MAX;
6143 else
6144 ctx->pos = INT_MAX;
Chris Mason39279cc2007-06-12 06:35:45 -04006145nopos:
6146 ret = 0;
6147err:
Omar Sandoval02dbfc92016-05-20 13:50:33 -07006148 if (put)
6149 btrfs_readdir_put_delayed_items(inode, &ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04006150 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04006151 return ret;
6152}
6153
Chris Mason39279cc2007-06-12 06:35:45 -04006154/*
Chris Mason54aa1f42007-06-22 14:16:25 -04006155 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04006156 * inode changes. But, it is most likely to find the inode in cache.
6157 * FIXME, needs more benchmarking...there are no reasons other than performance
6158 * to keep or drop this code.
6159 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00006160static int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04006161{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006162 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04006163 struct btrfs_root *root = BTRFS_I(inode)->root;
6164 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006165 int ret;
6166
Josef Bacik72ac3c02012-05-23 14:13:11 -04006167 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05006168 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006169
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006170 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006171 if (IS_ERR(trans))
6172 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006173
6174 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04006175 if (ret && ret == -ENOSPC) {
6176 /* whoops, lets try again with the full transaction */
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006177 btrfs_end_transaction(trans);
Chris Mason94b60442010-05-26 11:02:00 -04006178 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006179 if (IS_ERR(trans))
6180 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006181
Chris Mason94b60442010-05-26 11:02:00 -04006182 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04006183 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006184 btrfs_end_transaction(trans);
Miao Xie16cdcec2011-04-22 18:12:22 +08006185 if (BTRFS_I(inode)->delayed_node)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006186 btrfs_balance_delayed_items(fs_info);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006187
6188 return ret;
6189}
6190
6191/*
6192 * This is a copy of file_update_time. We need this so we can return error on
6193 * ENOSPC for updating the inode in the case of file write and mmap writes.
6194 */
Deepa Dinamani95582b02018-05-08 19:36:02 -07006195static int btrfs_update_time(struct inode *inode, struct timespec64 *now,
Josef Bacike41f9412012-03-26 09:46:47 -04006196 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05006197{
Alexander Block2bc5565282012-06-15 09:49:33 +02006198 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Layton3a8c7232017-12-11 06:35:24 -05006199 bool dirty = flags & ~S_VERSION;
Alexander Block2bc5565282012-06-15 09:49:33 +02006200
6201 if (btrfs_root_readonly(root))
6202 return -EROFS;
6203
Josef Bacike41f9412012-03-26 09:46:47 -04006204 if (flags & S_VERSION)
Jeff Layton3a8c7232017-12-11 06:35:24 -05006205 dirty |= inode_maybe_inc_iversion(inode, dirty);
Josef Bacike41f9412012-03-26 09:46:47 -04006206 if (flags & S_CTIME)
6207 inode->i_ctime = *now;
6208 if (flags & S_MTIME)
6209 inode->i_mtime = *now;
6210 if (flags & S_ATIME)
6211 inode->i_atime = *now;
Jeff Layton3a8c7232017-12-11 06:35:24 -05006212 return dirty ? btrfs_dirty_inode(inode) : 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006213}
6214
Chris Masond352ac62008-09-29 15:18:18 -04006215/*
6216 * find the highest existing sequence number in a directory
6217 * and then set the in-memory index_cnt variable to reflect
6218 * free sequence numbers
6219 */
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006220static int btrfs_set_inode_index_count(struct btrfs_inode *inode)
Josef Bacikaec74772008-07-24 12:12:38 -04006221{
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006222 struct btrfs_root *root = inode->root;
Josef Bacikaec74772008-07-24 12:12:38 -04006223 struct btrfs_key key, found_key;
6224 struct btrfs_path *path;
6225 struct extent_buffer *leaf;
6226 int ret;
6227
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006228 key.objectid = btrfs_ino(inode);
David Sterba962a2982014-06-04 18:41:45 +02006229 key.type = BTRFS_DIR_INDEX_KEY;
Josef Bacikaec74772008-07-24 12:12:38 -04006230 key.offset = (u64)-1;
6231
6232 path = btrfs_alloc_path();
6233 if (!path)
6234 return -ENOMEM;
6235
6236 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
6237 if (ret < 0)
6238 goto out;
6239 /* FIXME: we should be able to handle this */
6240 if (ret == 0)
6241 goto out;
6242 ret = 0;
6243
6244 /*
6245 * MAGIC NUMBER EXPLANATION:
6246 * since we search a directory based on f_pos we have to start at 2
6247 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
6248 * else has to start at 2
6249 */
6250 if (path->slots[0] == 0) {
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006251 inode->index_cnt = 2;
Josef Bacikaec74772008-07-24 12:12:38 -04006252 goto out;
6253 }
6254
6255 path->slots[0]--;
6256
6257 leaf = path->nodes[0];
6258 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6259
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006260 if (found_key.objectid != btrfs_ino(inode) ||
David Sterba962a2982014-06-04 18:41:45 +02006261 found_key.type != BTRFS_DIR_INDEX_KEY) {
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006262 inode->index_cnt = 2;
Josef Bacikaec74772008-07-24 12:12:38 -04006263 goto out;
6264 }
6265
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006266 inode->index_cnt = found_key.offset + 1;
Josef Bacikaec74772008-07-24 12:12:38 -04006267out:
6268 btrfs_free_path(path);
6269 return ret;
6270}
6271
Chris Masond352ac62008-09-29 15:18:18 -04006272/*
6273 * helper to find a free sequence number in a given directory. This current
6274 * code is very simple, later versions will do smarter things in the btree
6275 */
Nikolay Borisov877574e2017-02-20 13:50:33 +02006276int btrfs_set_inode_index(struct btrfs_inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04006277{
6278 int ret = 0;
6279
Nikolay Borisov877574e2017-02-20 13:50:33 +02006280 if (dir->index_cnt == (u64)-1) {
6281 ret = btrfs_inode_delayed_dir_index_count(dir);
Miao Xie16cdcec2011-04-22 18:12:22 +08006282 if (ret) {
6283 ret = btrfs_set_inode_index_count(dir);
6284 if (ret)
6285 return ret;
6286 }
Josef Bacikaec74772008-07-24 12:12:38 -04006287 }
6288
Nikolay Borisov877574e2017-02-20 13:50:33 +02006289 *index = dir->index_cnt;
6290 dir->index_cnt++;
Josef Bacikaec74772008-07-24 12:12:38 -04006291
6292 return ret;
6293}
6294
Chris Masonb0d5d102014-09-08 13:08:51 -07006295static int btrfs_insert_inode_locked(struct inode *inode)
6296{
6297 struct btrfs_iget_args args;
6298 args.location = &BTRFS_I(inode)->location;
6299 args.root = BTRFS_I(inode)->root;
6300
6301 return insert_inode_locked4(inode,
6302 btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root),
6303 btrfs_find_actor, &args);
6304}
6305
Anand Jain19aee8d2017-07-18 17:37:05 +08006306/*
6307 * Inherit flags from the parent inode.
6308 *
6309 * Currently only the compression flags and the cow flags are inherited.
6310 */
6311static void btrfs_inherit_iflags(struct inode *inode, struct inode *dir)
6312{
6313 unsigned int flags;
6314
6315 if (!dir)
6316 return;
6317
6318 flags = BTRFS_I(dir)->flags;
6319
6320 if (flags & BTRFS_INODE_NOCOMPRESS) {
6321 BTRFS_I(inode)->flags &= ~BTRFS_INODE_COMPRESS;
6322 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
6323 } else if (flags & BTRFS_INODE_COMPRESS) {
6324 BTRFS_I(inode)->flags &= ~BTRFS_INODE_NOCOMPRESS;
6325 BTRFS_I(inode)->flags |= BTRFS_INODE_COMPRESS;
6326 }
6327
6328 if (flags & BTRFS_INODE_NODATACOW) {
6329 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
6330 if (S_ISREG(inode->i_mode))
6331 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
6332 }
6333
David Sterba7b6a2212018-03-26 18:40:21 +02006334 btrfs_sync_inode_flags_to_i_flags(inode);
Anand Jain19aee8d2017-07-18 17:37:05 +08006335}
6336
Chris Mason39279cc2007-06-12 06:35:45 -04006337static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
6338 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04006339 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05006340 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04006341 u64 ref_objectid, u64 objectid,
6342 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04006343{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006344 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04006345 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006346 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04006347 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04006348 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05006349 struct btrfs_inode_ref *ref;
6350 struct btrfs_key key[2];
6351 u32 sizes[2];
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006352 int nitems = name ? 2 : 1;
Chris Mason9c583092008-01-29 15:15:18 -05006353 unsigned long ptr;
Josef Bacik11a19a92019-09-09 10:12:04 -04006354 unsigned int nofs_flag;
Chris Mason39279cc2007-06-12 06:35:45 -04006355 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006356
Chris Mason5f39d392007-10-15 16:14:19 -04006357 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07006358 if (!path)
6359 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04006360
Josef Bacik11a19a92019-09-09 10:12:04 -04006361 nofs_flag = memalloc_nofs_save();
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006362 inode = new_inode(fs_info->sb);
Josef Bacik11a19a92019-09-09 10:12:04 -04006363 memalloc_nofs_restore(nofs_flag);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006364 if (!inode) {
6365 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04006366 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006367 }
Chris Mason39279cc2007-06-12 06:35:45 -04006368
Li Zefan581bb052011-04-20 10:06:11 +08006369 /*
Filipe Manana5762b5c2014-08-01 00:10:32 +01006370 * O_TMPFILE, set link count to 0, so that after this point,
6371 * we fill in an inode item with the correct link count.
6372 */
6373 if (!name)
6374 set_nlink(inode, 0);
6375
6376 /*
Li Zefan581bb052011-04-20 10:06:11 +08006377 * we have to initialize this early, so we can reclaim the inode
6378 * number if we fail afterwards in this function.
6379 */
6380 inode->i_ino = objectid;
6381
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006382 if (dir && name) {
liubo1abe9b82011-03-24 11:18:59 +00006383 trace_btrfs_inode_request(dir);
6384
Nikolay Borisov877574e2017-02-20 13:50:33 +02006385 ret = btrfs_set_inode_index(BTRFS_I(dir), index);
Shen Feng09771432009-04-02 16:46:06 -04006386 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006387 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04006388 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04006389 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04006390 }
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006391 } else if (dir) {
6392 *index = 0;
Josef Bacikaec74772008-07-24 12:12:38 -04006393 }
6394 /*
6395 * index_cnt is ignored for everything but a dir,
Su Yuedf6703e2018-01-12 11:08:02 +08006396 * btrfs_set_inode_index_count has an explanation for the magic
Josef Bacikaec74772008-07-24 12:12:38 -04006397 * number
6398 */
6399 BTRFS_I(inode)->index_cnt = 2;
Miao Xie67de1172013-12-26 13:07:06 +08006400 BTRFS_I(inode)->dir_index = *index;
Chris Mason39279cc2007-06-12 06:35:45 -04006401 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04006402 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00006403 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db22007-08-27 16:49:44 -04006404
Josef Bacik5dc562c2012-08-17 13:14:17 -04006405 /*
6406 * We could have gotten an inode number from somebody who was fsynced
6407 * and then removed in this same transaction, so let's just set full
6408 * sync since it will be a full sync anyway and this will blow away the
6409 * old info in the log.
6410 */
6411 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
6412
Chris Mason9c583092008-01-29 15:15:18 -05006413 key[0].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006414 key[0].type = BTRFS_INODE_ITEM_KEY;
Chris Mason9c583092008-01-29 15:15:18 -05006415 key[0].offset = 0;
6416
Chris Mason9c583092008-01-29 15:15:18 -05006417 sizes[0] = sizeof(struct btrfs_inode_item);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006418
6419 if (name) {
6420 /*
6421 * Start new inodes with an inode_ref. This is slightly more
6422 * efficient for small numbers of hard links since they will
6423 * be packed into one item. Extended refs will kick in if we
6424 * add more hard links than can fit in the ref item.
6425 */
6426 key[1].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006427 key[1].type = BTRFS_INODE_REF_KEY;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006428 key[1].offset = ref_objectid;
6429
6430 sizes[1] = name_len + sizeof(*ref);
6431 }
Chris Mason9c583092008-01-29 15:15:18 -05006432
Chris Masonb0d5d102014-09-08 13:08:51 -07006433 location = &BTRFS_I(inode)->location;
6434 location->objectid = objectid;
6435 location->offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02006436 location->type = BTRFS_INODE_ITEM_KEY;
Chris Masonb0d5d102014-09-08 13:08:51 -07006437
6438 ret = btrfs_insert_inode_locked(inode);
Al Viro32955c52018-05-16 12:20:05 -04006439 if (ret < 0) {
6440 iput(inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006441 goto fail;
Al Viro32955c52018-05-16 12:20:05 -04006442 }
Chris Masonb0d5d102014-09-08 13:08:51 -07006443
Chris Masonb9473432009-03-13 11:00:37 -04006444 path->leave_spinning = 1;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006445 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems);
Chris Mason9c583092008-01-29 15:15:18 -05006446 if (ret != 0)
Chris Masonb0d5d102014-09-08 13:08:51 -07006447 goto fail_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04006448
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03006449 inode_init_owner(inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04006450 inode_set_bytes(inode, 0);
chandan r9cc97d62012-07-04 12:48:07 +05306451
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006452 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05306453 inode->i_atime = inode->i_mtime;
6454 inode->i_ctime = inode->i_mtime;
Arnd Bergmannd3c6be6f2018-06-21 18:04:06 +02006455 BTRFS_I(inode)->i_otime = inode->i_mtime;
chandan r9cc97d62012-07-04 12:48:07 +05306456
Chris Mason5f39d392007-10-15 16:14:19 -04006457 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
6458 struct btrfs_inode_item);
David Sterbab159fa22016-11-08 18:09:03 +01006459 memzero_extent_buffer(path->nodes[0], (unsigned long)inode_item,
Li Zefan293f7e02012-07-10 00:58:58 -06006460 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04006461 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05006462
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006463 if (name) {
6464 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
6465 struct btrfs_inode_ref);
6466 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
6467 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
6468 ptr = (unsigned long)(ref + 1);
6469 write_extent_buffer(path->nodes[0], name, ptr, name_len);
6470 }
Chris Mason9c583092008-01-29 15:15:18 -05006471
Chris Mason5f39d392007-10-15 16:14:19 -04006472 btrfs_mark_buffer_dirty(path->nodes[0]);
6473 btrfs_free_path(path);
6474
Christoph Hellwig6cbff002009-04-17 10:37:41 +02006475 btrfs_inherit_iflags(inode, dir);
6476
Al Viro569254b2011-07-24 17:08:40 -04006477 if (S_ISREG(mode)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006478 if (btrfs_test_opt(fs_info, NODATASUM))
Chris Mason94272162009-07-02 12:26:06 -04006479 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006480 if (btrfs_test_opt(fs_info, NODATACOW))
Josef Bacikf2bdf9a2013-02-21 15:28:28 -05006481 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
6482 BTRFS_INODE_NODATASUM;
Chris Mason94272162009-07-02 12:26:06 -04006483 }
6484
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006485 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00006486
6487 trace_btrfs_inode_new(inode);
Chris Mason1973f0f2011-06-24 13:13:29 -04006488 btrfs_set_inode_last_trans(trans, inode);
liubo1abe9b82011-03-24 11:18:59 +00006489
Alexander Block8ea05e32012-07-25 17:35:53 +02006490 btrfs_update_root_times(trans, root);
6491
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006492 ret = btrfs_inode_inherit_props(trans, inode, dir);
6493 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006494 btrfs_err(fs_info,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006495 "error inheriting props for ino %llu (root %llu): %d",
David Sterbaf85b7372017-01-20 14:54:07 +01006496 btrfs_ino(BTRFS_I(inode)), root->root_key.objectid, ret);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006497
Chris Mason39279cc2007-06-12 06:35:45 -04006498 return inode;
Chris Masonb0d5d102014-09-08 13:08:51 -07006499
6500fail_unlock:
Al Viro32955c52018-05-16 12:20:05 -04006501 discard_new_inode(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006502fail:
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006503 if (dir && name)
Josef Bacikaec74772008-07-24 12:12:38 -04006504 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04006505 btrfs_free_path(path);
6506 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006507}
6508
Chris Masond352ac62008-09-29 15:18:18 -04006509/*
6510 * utility function to add 'inode' into 'parent_inode' with
6511 * a give name and a given sequence number.
6512 * if 'add_backref' is true, also insert a backref from the
6513 * inode to the parent directory.
6514 */
Chris Masone02119d2008-09-05 16:13:11 -04006515int btrfs_add_link(struct btrfs_trans_handle *trans,
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006516 struct btrfs_inode *parent_inode, struct btrfs_inode *inode,
Chris Masone02119d2008-09-05 16:13:11 -04006517 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006518{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006519 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006520 struct btrfs_key key;
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006521 struct btrfs_root *root = parent_inode->root;
6522 u64 ino = btrfs_ino(inode);
6523 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006524
Li Zefan33345d012011-04-20 10:31:50 +08006525 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006526 memcpy(&key, &inode->root->root_key, sizeof(key));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006527 } else {
Li Zefan33345d012011-04-20 10:31:50 +08006528 key.objectid = ino;
David Sterba962a2982014-06-04 18:41:45 +02006529 key.type = BTRFS_INODE_ITEM_KEY;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006530 key.offset = 0;
6531 }
Chris Mason39279cc2007-06-12 06:35:45 -04006532
Li Zefan33345d012011-04-20 10:31:50 +08006533 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Lu Fengqi6025c192018-08-01 11:32:29 +08006534 ret = btrfs_add_root_ref(trans, key.objectid,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006535 root->root_key.objectid, parent_ino,
6536 index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006537 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08006538 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
6539 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006540 }
6541
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006542 /* Nothing to clean up yet */
6543 if (ret)
6544 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006545
Lu Fengqi684572d2018-08-04 21:10:57 +08006546 ret = btrfs_insert_dir_item(trans, name, name_len, parent_inode, &key,
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006547 btrfs_inode_type(&inode->vfs_inode), index);
Chris Mason9c520572012-12-17 14:26:57 -05006548 if (ret == -EEXIST || ret == -EOVERFLOW)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006549 goto fail_dir_item;
6550 else if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04006551 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006552 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006553 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006554
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006555 btrfs_i_size_write(parent_inode, parent_inode->vfs_inode.i_size +
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006556 name_len * 2);
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006557 inode_inc_iversion(&parent_inode->vfs_inode);
Filipe Manana5338e432019-05-15 16:02:47 +01006558 /*
6559 * If we are replaying a log tree, we do not want to update the mtime
6560 * and ctime of the parent directory with the current time, since the
6561 * log replay procedure is responsible for setting them to their correct
6562 * values (the ones it had when the fsync was done).
6563 */
6564 if (!test_bit(BTRFS_FS_LOG_RECOVERING, &root->fs_info->flags)) {
6565 struct timespec64 now = current_time(&parent_inode->vfs_inode);
6566
6567 parent_inode->vfs_inode.i_mtime = now;
6568 parent_inode->vfs_inode.i_ctime = now;
6569 }
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006570 ret = btrfs_update_inode(trans, root, &parent_inode->vfs_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006571 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04006572 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006573 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05006574
6575fail_dir_item:
6576 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
6577 u64 local_index;
6578 int err;
Lu Fengqi3ee1c552018-08-01 11:32:28 +08006579 err = btrfs_del_root_ref(trans, key.objectid,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006580 root->root_key.objectid, parent_ino,
6581 &local_index, name, name_len);
Johannes Thumshirn1690dd412018-12-12 15:14:17 +01006582 if (err)
6583 btrfs_abort_transaction(trans, err);
Chris Masonfe66a052012-02-20 08:40:56 -05006584 } else if (add_backref) {
6585 u64 local_index;
6586 int err;
6587
6588 err = btrfs_del_inode_ref(trans, root, name, name_len,
6589 ino, parent_ino, &local_index);
Johannes Thumshirn1690dd412018-12-12 15:14:17 +01006590 if (err)
6591 btrfs_abort_transaction(trans, err);
Chris Masonfe66a052012-02-20 08:40:56 -05006592 }
Johannes Thumshirn1690dd412018-12-12 15:14:17 +01006593
6594 /* Return the original error code */
Chris Masonfe66a052012-02-20 08:40:56 -05006595 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006596}
6597
6598static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006599 struct btrfs_inode *dir, struct dentry *dentry,
6600 struct btrfs_inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006601{
Josef Bacika1b075d2010-11-19 20:36:11 +00006602 int err = btrfs_add_link(trans, dir, inode,
6603 dentry->d_name.name, dentry->d_name.len,
6604 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006605 if (err > 0)
6606 err = -EEXIST;
6607 return err;
6608}
6609
Josef Bacik618e21d2007-07-11 10:18:17 -04006610static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04006611 umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04006612{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006613 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Josef Bacik618e21d2007-07-11 10:18:17 -04006614 struct btrfs_trans_handle *trans;
6615 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006616 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04006617 int err;
Josef Bacik618e21d2007-07-11 10:18:17 -04006618 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006619 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04006620
Josef Bacik9ed74f22009-09-11 16:12:44 -04006621 /*
6622 * 2 for inode item and ref
6623 * 2 for dir items
6624 * 1 for xattr if selinux is on
6625 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006626 trans = btrfs_start_transaction(root, 5);
6627 if (IS_ERR(trans))
6628 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05006629
Li Zefan581bb052011-04-20 10:06:11 +08006630 err = btrfs_find_free_ino(root, &objectid);
6631 if (err)
6632 goto out_unlock;
6633
Josef Bacikaec74772008-07-24 12:12:38 -04006634 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006635 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6636 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006637 if (IS_ERR(inode)) {
6638 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -04006639 inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04006640 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006641 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006642
Casey Schauflerad19db72011-12-15 10:09:07 -05006643 /*
6644 * If the active LSM wants to access the inode during
6645 * d_instantiate it needs these. Smack checks to see
6646 * if the filesystem supports xattrs by looking at the
6647 * ops vector.
6648 */
Casey Schauflerad19db72011-12-15 10:09:07 -05006649 inode->i_op = &btrfs_special_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006650 init_special_inode(inode, inode->i_mode, rdev);
6651
6652 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik618e21d2007-07-11 10:18:17 -04006653 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006654 goto out_unlock;
Chris Masonb0d5d102014-09-08 13:08:51 -07006655
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006656 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6657 0, index);
Al Viro32955c52018-05-16 12:20:05 -04006658 if (err)
6659 goto out_unlock;
6660
6661 btrfs_update_inode(trans, root, inode);
6662 d_instantiate_new(dentry, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006663
Josef Bacik618e21d2007-07-11 10:18:17 -04006664out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006665 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006666 btrfs_btree_balance_dirty(fs_info);
Al Viro32955c52018-05-16 12:20:05 -04006667 if (err && inode) {
Josef Bacik618e21d2007-07-11 10:18:17 -04006668 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -04006669 discard_new_inode(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006670 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006671 return err;
6672}
6673
Chris Mason39279cc2007-06-12 06:35:45 -04006674static int btrfs_create(struct inode *dir, struct dentry *dentry,
Al Viroebfc3b42012-06-10 18:05:36 -04006675 umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -04006676{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006677 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04006678 struct btrfs_trans_handle *trans;
6679 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006680 struct inode *inode = NULL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006681 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04006682 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006683 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006684
Josef Bacik9ed74f22009-09-11 16:12:44 -04006685 /*
6686 * 2 for inode item and ref
6687 * 2 for dir items
6688 * 1 for xattr if selinux is on
6689 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006690 trans = btrfs_start_transaction(root, 5);
6691 if (IS_ERR(trans))
6692 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006693
Li Zefan581bb052011-04-20 10:06:11 +08006694 err = btrfs_find_free_ino(root, &objectid);
6695 if (err)
6696 goto out_unlock;
6697
Josef Bacikaec74772008-07-24 12:12:38 -04006698 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006699 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6700 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006701 if (IS_ERR(inode)) {
6702 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -04006703 inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006704 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006705 }
Casey Schauflerad19db72011-12-15 10:09:07 -05006706 /*
6707 * If the active LSM wants to access the inode during
6708 * d_instantiate it needs these. Smack checks to see
6709 * if the filesystem supports xattrs by looking at the
6710 * ops vector.
6711 */
6712 inode->i_fop = &btrfs_file_operations;
6713 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006714 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006715
6716 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
6717 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006718 goto out_unlock;
Chris Masonb0d5d102014-09-08 13:08:51 -07006719
6720 err = btrfs_update_inode(trans, root, inode);
6721 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006722 goto out_unlock;
Casey Schauflerad19db72011-12-15 10:09:07 -05006723
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006724 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6725 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006726 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006727 goto out_unlock;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006728
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006729 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Al Viro1e2e5472018-05-04 08:23:01 -04006730 d_instantiate_new(dentry, inode);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006731
Chris Mason39279cc2007-06-12 06:35:45 -04006732out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006733 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -04006734 if (err && inode) {
Chris Mason39279cc2007-06-12 06:35:45 -04006735 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -04006736 discard_new_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006737 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006738 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006739 return err;
6740}
6741
6742static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
6743 struct dentry *dentry)
6744{
Filipe Manana271dba452016-01-05 16:24:05 +00006745 struct btrfs_trans_handle *trans = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006746 struct btrfs_root *root = BTRFS_I(dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00006747 struct inode *inode = d_inode(old_dentry);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006748 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason00e4e6b2008-08-05 11:18:09 -04006749 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04006750 int err;
6751 int drop_inode = 0;
6752
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006753 /* do not allow sys_link's with other subvols of the same device */
Misono Tomohiro4fd786e2018-08-06 14:25:24 +09006754 if (root->root_key.objectid != BTRFS_I(inode)->root->root_key.objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00006755 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006756
Mark Fashehf1863732012-08-08 11:32:27 -07006757 if (inode->i_nlink >= BTRFS_LINK_MAX)
Al Viroc055e992011-03-04 17:15:18 +00006758 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04006759
Nikolay Borisov877574e2017-02-20 13:50:33 +02006760 err = btrfs_set_inode_index(BTRFS_I(dir), &index);
Josef Bacikaec74772008-07-24 12:12:38 -04006761 if (err)
6762 goto fail;
6763
Yan, Zhenga22285a2010-05-16 10:48:46 -04006764 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00006765 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04006766 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00006767 * 1 item for parent inode
Omar Sandoval399b0bb2018-05-11 13:13:40 -07006768 * 1 item for orphan item deletion if O_TMPFILE
Yan, Zhenga22285a2010-05-16 10:48:46 -04006769 */
Omar Sandoval399b0bb2018-05-11 13:13:40 -07006770 trans = btrfs_start_transaction(root, inode->i_nlink ? 5 : 6);
Yan, Zhenga22285a2010-05-16 10:48:46 -04006771 if (IS_ERR(trans)) {
6772 err = PTR_ERR(trans);
Filipe Manana271dba452016-01-05 16:24:05 +00006773 trans = NULL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006774 goto fail;
6775 }
Chris Mason5f39d392007-10-15 16:14:19 -04006776
Miao Xie67de1172013-12-26 13:07:06 +08006777 /* There are several dir indexes for this inode, clear the cache. */
6778 BTRFS_I(inode)->dir_index = 0ULL;
Zach Brown8b558c52013-10-16 12:10:34 -07006779 inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04006780 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006781 inode->i_ctime = current_time(inode);
Al Viro7de9c6ee2010-10-23 11:11:40 -04006782 ihold(inode);
Josef Bacike9976152012-10-11 15:53:56 -04006783 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
Josef Bacikaec74772008-07-24 12:12:38 -04006784
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006785 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6786 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04006787
Yan, Zhenga5719522009-09-24 09:17:31 -04006788 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006789 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04006790 } else {
Al Viro10d9f302011-07-16 23:09:10 -04006791 struct dentry *parent = dentry->d_parent;
Filipe Mananad4682ba2018-06-11 19:24:28 +01006792 int ret;
6793
Yan, Zhenga5719522009-09-24 09:17:31 -04006794 err = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006795 if (err)
6796 goto fail;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006797 if (inode->i_nlink == 1) {
6798 /*
6799 * If new hard link count is 1, it's a file created
6800 * with open(2) O_TMPFILE flag.
6801 */
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02006802 err = btrfs_orphan_del(trans, BTRFS_I(inode));
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006803 if (err)
6804 goto fail;
6805 }
Al Viro08c422c2011-12-23 07:58:13 -05006806 d_instantiate(dentry, inode);
Filipe Mananad4682ba2018-06-11 19:24:28 +01006807 ret = btrfs_log_new_name(trans, BTRFS_I(inode), NULL, parent,
6808 true, NULL);
6809 if (ret == BTRFS_NEED_TRANS_COMMIT) {
6810 err = btrfs_commit_transaction(trans);
6811 trans = NULL;
6812 }
Yan, Zhenga5719522009-09-24 09:17:31 -04006813 }
Chris Mason39279cc2007-06-12 06:35:45 -04006814
Chris Mason1832a6d2007-12-21 16:27:21 -05006815fail:
Filipe Manana271dba452016-01-05 16:24:05 +00006816 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006817 btrfs_end_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006818 if (drop_inode) {
6819 inode_dec_link_count(inode);
6820 iput(inode);
6821 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006822 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006823 return err;
6824}
6825
Al Viro18bb1db2011-07-26 01:41:39 -04006826static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04006827{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006828 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masonb9d86662008-05-02 16:13:49 -04006829 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006830 struct btrfs_trans_handle *trans;
6831 struct btrfs_root *root = BTRFS_I(dir)->root;
6832 int err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04006833 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006834 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006835
Josef Bacik9ed74f22009-09-11 16:12:44 -04006836 /*
6837 * 2 items for inode and ref
6838 * 2 items for dir items
6839 * 1 for xattr if selinux is on
6840 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006841 trans = btrfs_start_transaction(root, 5);
6842 if (IS_ERR(trans))
6843 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006844
Li Zefan581bb052011-04-20 10:06:11 +08006845 err = btrfs_find_free_ino(root, &objectid);
6846 if (err)
6847 goto out_fail;
6848
Josef Bacikaec74772008-07-24 12:12:38 -04006849 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006850 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6851 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04006852 if (IS_ERR(inode)) {
6853 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -04006854 inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006855 goto out_fail;
6856 }
Chris Mason5f39d392007-10-15 16:14:19 -04006857
Chris Masonb0d5d102014-09-08 13:08:51 -07006858 /* these must be set before we unlock the inode */
6859 inode->i_op = &btrfs_dir_inode_operations;
6860 inode->i_fop = &btrfs_dir_file_operations;
Josef Bacik33268ea2008-07-24 12:16:36 -04006861
Eric Paris2a7dba32011-02-01 11:05:39 -05006862 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04006863 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006864 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04006865
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02006866 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Mason39279cc2007-06-12 06:35:45 -04006867 err = btrfs_update_inode(trans, root, inode);
6868 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006869 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04006870
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006871 err = btrfs_add_link(trans, BTRFS_I(dir), BTRFS_I(inode),
6872 dentry->d_name.name,
6873 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006874 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006875 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04006876
Al Viro1e2e5472018-05-04 08:23:01 -04006877 d_instantiate_new(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006878
6879out_fail:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006880 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -04006881 if (err && inode) {
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006882 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -04006883 discard_new_inode(inode);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006884 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006885 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006886 return err;
6887}
6888
Chris Masonc8b97812008-10-29 14:49:59 -04006889static noinline int uncompress_inline(struct btrfs_path *path,
Byongho Leee40da0e2015-05-19 23:46:45 +09006890 struct page *page,
Chris Masonc8b97812008-10-29 14:49:59 -04006891 size_t pg_offset, u64 extent_offset,
6892 struct btrfs_file_extent_item *item)
6893{
6894 int ret;
6895 struct extent_buffer *leaf = path->nodes[0];
6896 char *tmp;
6897 size_t max_size;
6898 unsigned long inline_size;
6899 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08006900 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04006901
6902 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08006903 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04006904 max_size = btrfs_file_extent_ram_bytes(leaf, item);
6905 inline_size = btrfs_file_extent_inline_item_len(leaf,
Ross Kirkdd3cc162013-09-16 15:58:09 +01006906 btrfs_item_nr(path->slots[0]));
Chris Masonc8b97812008-10-29 14:49:59 -04006907 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04006908 if (!tmp)
6909 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04006910 ptr = btrfs_file_extent_inline_start(item);
6911
6912 read_extent_buffer(leaf, tmp, ptr, inline_size);
6913
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006914 max_size = min_t(unsigned long, PAGE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08006915 ret = btrfs_decompress(compress_type, tmp, page,
6916 extent_offset, inline_size, max_size);
Zygo Blaxelle1699d22017-03-10 16:45:44 -05006917
6918 /*
6919 * decompression code contains a memset to fill in any space between the end
6920 * of the uncompressed data and the end of max_size in case the decompressed
6921 * data ends up shorter than ram_bytes. That doesn't cover the hole between
6922 * the end of an inline extent and the beginning of the next block, so we
6923 * cover that region here.
6924 */
6925
6926 if (max_size + pg_offset < PAGE_SIZE) {
6927 char *map = kmap(page);
6928 memset(map + pg_offset + max_size, 0, PAGE_SIZE - max_size - pg_offset);
6929 kunmap(page);
6930 }
Chris Masonc8b97812008-10-29 14:49:59 -04006931 kfree(tmp);
Zach Brown166ae5a2014-05-09 17:15:10 -04006932 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -04006933}
6934
Chris Masond352ac62008-09-29 15:18:18 -04006935/*
6936 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05006937 * the ugly parts come from merging extents from the disk with the in-ram
6938 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04006939 * where the in-ram extents might be locked pending data=ordered completion.
6940 *
6941 * This also copies inline extents directly into the page.
6942 */
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006943struct extent_map *btrfs_get_extent(struct btrfs_inode *inode,
Liu Bode2c6612018-08-25 13:47:59 +08006944 struct page *page,
6945 size_t pg_offset, u64 start, u64 len,
6946 int create)
Chris Masona52d9a82007-08-27 16:49:44 -04006947{
David Sterba3ffbd682018-06-29 10:56:42 +02006948 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Chris Masona52d9a82007-08-27 16:49:44 -04006949 int ret;
6950 int err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006951 u64 extent_start = 0;
6952 u64 extent_end = 0;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006953 u64 objectid = btrfs_ino(inode);
Linus Torvalds7e74e232019-05-01 12:19:20 -07006954 int extent_type = -1;
Chris Masonf4219502008-07-22 11:18:09 -04006955 struct btrfs_path *path = NULL;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006956 struct btrfs_root *root = inode->root;
Chris Masona52d9a82007-08-27 16:49:44 -04006957 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04006958 struct extent_buffer *leaf;
6959 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04006960 struct extent_map *em = NULL;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006961 struct extent_map_tree *em_tree = &inode->extent_tree;
6962 struct extent_io_tree *io_tree = &inode->io_tree;
Filipe Manana7ffbb592014-06-09 03:48:05 +01006963 const bool new_inline = !page || create;
Chris Masona52d9a82007-08-27 16:49:44 -04006964
Chris Mason890871b2009-09-02 16:24:52 -04006965 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006966 em = lookup_extent_mapping(em_tree, start, len);
Chris Mason890871b2009-09-02 16:24:52 -04006967 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006968
Chris Masona52d9a82007-08-27 16:49:44 -04006969 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04006970 if (em->start > start || em->start + em->len <= start)
6971 free_extent_map(em);
6972 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05006973 free_extent_map(em);
6974 else
6975 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006976 }
David Sterba172ddd62011-04-21 00:48:27 +02006977 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04006978 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05006979 err = -ENOMEM;
6980 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006981 }
Chris Masond1310b22008-01-24 16:13:08 -05006982 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05006983 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05006984 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04006985 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04006986
Liu Bobee6ec82018-08-17 05:05:28 +08006987 path = btrfs_alloc_path();
Chris Masonf4219502008-07-22 11:18:09 -04006988 if (!path) {
Liu Bobee6ec82018-08-17 05:05:28 +08006989 err = -ENOMEM;
6990 goto out;
Chris Masonf4219502008-07-22 11:18:09 -04006991 }
6992
Liu Bobee6ec82018-08-17 05:05:28 +08006993 /* Chances are we'll be called again, so go ahead and do readahead */
6994 path->reada = READA_FORWARD;
6995
Liu Boe49aabd2018-08-25 13:47:09 +08006996 /*
6997 * Unless we're going to uncompress the inline extent, no sleep would
6998 * happen.
6999 */
7000 path->leave_spinning = 1;
7001
Nikolay Borisov5c9a7022017-12-01 11:19:40 +02007002 ret = btrfs_lookup_file_extent(NULL, root, path, objectid, start, 0);
Chris Masona52d9a82007-08-27 16:49:44 -04007003 if (ret < 0) {
7004 err = ret;
7005 goto out;
Nikolay Borisovb8eeab72018-12-17 11:49:00 +02007006 } else if (ret > 0) {
Chris Masona52d9a82007-08-27 16:49:44 -04007007 if (path->slots[0] == 0)
7008 goto not_found;
7009 path->slots[0]--;
7010 }
7011
Chris Mason5f39d392007-10-15 16:14:19 -04007012 leaf = path->nodes[0];
7013 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04007014 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04007015 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04007016 if (found_key.objectid != objectid ||
Nikolay Borisov694c12e2018-12-17 10:35:59 +02007017 found_key.type != BTRFS_EXTENT_DATA_KEY) {
Josef Bacik25a50342013-10-14 12:08:38 -04007018 /*
7019 * If we backup past the first extent we want to move forward
7020 * and see if there is an extent in front of us, otherwise we'll
7021 * say there is a hole for our whole search range which can
7022 * cause problems.
7023 */
7024 extent_end = start;
7025 goto next;
Chris Masona52d9a82007-08-27 16:49:44 -04007026 }
7027
Nikolay Borisov694c12e2018-12-17 10:35:59 +02007028 extent_type = btrfs_file_extent_type(leaf, item);
Chris Mason5f39d392007-10-15 16:14:19 -04007029 extent_start = found_key.offset;
Nikolay Borisov694c12e2018-12-17 10:35:59 +02007030 if (extent_type == BTRFS_FILE_EXTENT_REG ||
7031 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08007032 /* Only regular file could have regular/prealloc extent */
7033 if (!S_ISREG(inode->vfs_inode.i_mode)) {
7034 ret = -EUCLEAN;
7035 btrfs_crit(fs_info,
7036 "regular/prealloc extent found for non-regular inode %llu",
7037 btrfs_ino(inode));
7038 goto out;
7039 }
Chris Masona52d9a82007-08-27 16:49:44 -04007040 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04007041 btrfs_file_extent_num_bytes(leaf, item);
Liu Bo09ed2f12017-03-10 11:09:48 -08007042
7043 trace_btrfs_get_extent_show_fi_regular(inode, leaf, item,
7044 extent_start);
Nikolay Borisov694c12e2018-12-17 10:35:59 +02007045 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Yan Zheng9036c102008-10-30 14:19:41 -04007046 size_t size;
Qu Wenruoe41ca582018-06-06 15:41:49 +08007047
7048 size = btrfs_file_extent_ram_bytes(leaf, item);
Jeff Mahoneyda170662016-06-15 09:22:56 -04007049 extent_end = ALIGN(extent_start + size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007050 fs_info->sectorsize);
Liu Bo09ed2f12017-03-10 11:09:48 -08007051
7052 trace_btrfs_get_extent_show_fi_inline(inode, leaf, item,
7053 path->slots[0],
7054 extent_start);
Yan Zheng9036c102008-10-30 14:19:41 -04007055 }
Josef Bacik25a50342013-10-14 12:08:38 -04007056next:
Yan Zheng9036c102008-10-30 14:19:41 -04007057 if (start >= extent_end) {
7058 path->slots[0]++;
7059 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
7060 ret = btrfs_next_leaf(root, path);
7061 if (ret < 0) {
7062 err = ret;
7063 goto out;
Nikolay Borisovb8eeab72018-12-17 11:49:00 +02007064 } else if (ret > 0) {
Yan Zheng9036c102008-10-30 14:19:41 -04007065 goto not_found;
Nikolay Borisovb8eeab72018-12-17 11:49:00 +02007066 }
Yan Zheng9036c102008-10-30 14:19:41 -04007067 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04007068 }
Yan Zheng9036c102008-10-30 14:19:41 -04007069 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
7070 if (found_key.objectid != objectid ||
7071 found_key.type != BTRFS_EXTENT_DATA_KEY)
7072 goto not_found;
7073 if (start + len <= found_key.offset)
7074 goto not_found;
Wang Shilonge2eca692014-07-17 11:44:14 +08007075 if (start > found_key.offset)
7076 goto next;
Nikolay Borisov02a033d2018-12-17 10:36:02 +02007077
7078 /* New extent overlaps with existing one */
Yan Zheng9036c102008-10-30 14:19:41 -04007079 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04007080 em->orig_start = start;
Yan Zheng9036c102008-10-30 14:19:41 -04007081 em->len = found_key.offset - start;
Nikolay Borisov02a033d2018-12-17 10:36:02 +02007082 em->block_start = EXTENT_MAP_HOLE;
7083 goto insert;
Yan Zheng9036c102008-10-30 14:19:41 -04007084 }
7085
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007086 btrfs_extent_item_to_extent_map(inode, path, item,
Nikolay Borisov9cdc5122017-02-20 13:51:02 +02007087 new_inline, em);
Filipe Manana7ffbb592014-06-09 03:48:05 +01007088
Nikolay Borisov694c12e2018-12-17 10:35:59 +02007089 if (extent_type == BTRFS_FILE_EXTENT_REG ||
7090 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04007091 goto insert;
Nikolay Borisov694c12e2018-12-17 10:35:59 +02007092 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04007093 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04007094 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04007095 size_t size;
7096 size_t extent_offset;
7097 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04007098
Filipe Manana7ffbb592014-06-09 03:48:05 +01007099 if (new_inline)
Yan689f9342007-10-29 11:41:07 -04007100 goto out;
Yan689f9342007-10-29 11:41:07 -04007101
Qu Wenruoe41ca582018-06-06 15:41:49 +08007102 size = btrfs_file_extent_ram_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04007103 extent_offset = page_offset(page) + pg_offset - extent_start;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007104 copy_size = min_t(u64, PAGE_SIZE - pg_offset,
7105 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04007106 em->start = extent_start + extent_offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007107 em->len = ALIGN(copy_size, fs_info->sectorsize);
Josef Bacikb4939682012-12-03 10:31:19 -05007108 em->orig_block_len = em->len;
Josef Bacik70c8a912012-10-11 16:54:30 -04007109 em->orig_start = em->start;
Yan689f9342007-10-29 11:41:07 -04007110 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Liu Boe49aabd2018-08-25 13:47:09 +08007111
7112 btrfs_set_path_blocking(path);
Edmund Nadolskibf46f522017-11-20 13:24:49 -07007113 if (!PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08007114 if (btrfs_file_extent_compression(leaf, item) !=
7115 BTRFS_COMPRESS_NONE) {
Byongho Leee40da0e2015-05-19 23:46:45 +09007116 ret = uncompress_inline(path, page, pg_offset,
Chris Masonc8b97812008-10-29 14:49:59 -04007117 extent_offset, item);
Zach Brown166ae5a2014-05-09 17:15:10 -04007118 if (ret) {
7119 err = ret;
7120 goto out;
7121 }
Chris Masonc8b97812008-10-29 14:49:59 -04007122 } else {
7123 map = kmap(page);
7124 read_extent_buffer(leaf, map + pg_offset, ptr,
7125 copy_size);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007126 if (pg_offset + copy_size < PAGE_SIZE) {
Chris Mason93c82d52009-09-11 12:36:29 -04007127 memset(map + pg_offset + copy_size, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007128 PAGE_SIZE - pg_offset -
Chris Mason93c82d52009-09-11 12:36:29 -04007129 copy_size);
7130 }
Chris Masonc8b97812008-10-29 14:49:59 -04007131 kunmap(page);
7132 }
Chris Mason179e29e2007-11-01 11:28:41 -04007133 flush_dcache_page(page);
Chris Masona52d9a82007-08-27 16:49:44 -04007134 }
Chris Masond1310b22008-01-24 16:13:08 -05007135 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00007136 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04007137 goto insert;
Chris Masona52d9a82007-08-27 16:49:44 -04007138 }
7139not_found:
7140 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04007141 em->orig_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05007142 em->len = len;
Chris Mason5f39d392007-10-15 16:14:19 -04007143 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04007144insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02007145 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05007146 if (em->start > start || extent_map_end(em) <= start) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007147 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04007148 "bad extent! em: [%llu %llu] passed [%llu %llu]",
7149 em->start, em->len, start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04007150 err = -EIO;
7151 goto out;
7152 }
Chris Masond1310b22008-01-24 16:13:08 -05007153
7154 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04007155 write_lock(&em_tree->lock);
David Sterbaf46b24c2018-04-03 21:45:57 +02007156 err = btrfs_add_extent_mapping(fs_info, em_tree, &em, start, len);
Chris Mason890871b2009-09-02 16:24:52 -04007157 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04007158out:
Liu Boc6414282018-08-23 07:36:17 +08007159 btrfs_free_path(path);
liubo1abe9b82011-03-24 11:18:59 +00007160
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007161 trace_btrfs_get_extent(root, inode, em);
liubo1abe9b82011-03-24 11:18:59 +00007162
Chris Masona52d9a82007-08-27 16:49:44 -04007163 if (err) {
7164 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04007165 return ERR_PTR(err);
7166 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007167 BUG_ON(!em); /* Error is always set */
Chris Masona52d9a82007-08-27 16:49:44 -04007168 return em;
7169}
7170
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007171struct extent_map *btrfs_get_extent_fiemap(struct btrfs_inode *inode,
Nikolay Borisov4ab47a82018-12-12 09:42:32 +02007172 u64 start, u64 len)
Chris Masonec29ed52011-02-23 16:23:20 -05007173{
7174 struct extent_map *em;
7175 struct extent_map *hole_em = NULL;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007176 u64 delalloc_start = start;
Chris Masonec29ed52011-02-23 16:23:20 -05007177 u64 end;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007178 u64 delalloc_len;
7179 u64 delalloc_end;
Chris Masonec29ed52011-02-23 16:23:20 -05007180 int err = 0;
7181
Nikolay Borisov4ab47a82018-12-12 09:42:32 +02007182 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
Chris Masonec29ed52011-02-23 16:23:20 -05007183 if (IS_ERR(em))
7184 return em;
Dan Carpenter99862772017-04-11 11:57:15 +03007185 /*
7186 * If our em maps to:
7187 * - a hole or
7188 * - a pre-alloc extent,
7189 * there might actually be delalloc bytes behind it.
7190 */
7191 if (em->block_start != EXTENT_MAP_HOLE &&
7192 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7193 return em;
7194 else
7195 hole_em = em;
Chris Masonec29ed52011-02-23 16:23:20 -05007196
7197 /* check to see if we've wrapped (len == -1 or similar) */
7198 end = start + len;
7199 if (end < start)
7200 end = (u64)-1;
7201 else
7202 end -= 1;
7203
7204 em = NULL;
7205
7206 /* ok, we didn't find anything, lets look for delalloc */
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007207 delalloc_len = count_range_bits(&inode->io_tree, &delalloc_start,
Chris Masonec29ed52011-02-23 16:23:20 -05007208 end, len, EXTENT_DELALLOC, 1);
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007209 delalloc_end = delalloc_start + delalloc_len;
7210 if (delalloc_end < delalloc_start)
7211 delalloc_end = (u64)-1;
Chris Masonec29ed52011-02-23 16:23:20 -05007212
7213 /*
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007214 * We didn't find anything useful, return the original results from
7215 * get_extent()
Chris Masonec29ed52011-02-23 16:23:20 -05007216 */
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007217 if (delalloc_start > end || delalloc_end <= start) {
Chris Masonec29ed52011-02-23 16:23:20 -05007218 em = hole_em;
7219 hole_em = NULL;
7220 goto out;
7221 }
7222
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007223 /*
7224 * Adjust the delalloc_start to make sure it doesn't go backwards from
7225 * the start they passed in
Chris Masonec29ed52011-02-23 16:23:20 -05007226 */
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007227 delalloc_start = max(start, delalloc_start);
7228 delalloc_len = delalloc_end - delalloc_start;
Chris Masonec29ed52011-02-23 16:23:20 -05007229
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007230 if (delalloc_len > 0) {
7231 u64 hole_start;
Nikolay Borisov02950af2018-12-12 09:42:34 +02007232 u64 hole_len;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007233 const u64 hole_end = extent_map_end(hole_em);
Chris Masonec29ed52011-02-23 16:23:20 -05007234
David Sterba172ddd62011-04-21 00:48:27 +02007235 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05007236 if (!em) {
7237 err = -ENOMEM;
7238 goto out;
7239 }
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007240
7241 ASSERT(hole_em);
7242 /*
7243 * When btrfs_get_extent can't find anything it returns one
7244 * huge hole
7245 *
7246 * Make sure what it found really fits our range, and adjust to
7247 * make sure it is based on the start from the caller
7248 */
7249 if (hole_end <= start || hole_em->start > end) {
7250 free_extent_map(hole_em);
7251 hole_em = NULL;
7252 } else {
7253 hole_start = max(hole_em->start, start);
7254 hole_len = hole_end - hole_start;
7255 }
7256
7257 if (hole_em && delalloc_start > hole_start) {
7258 /*
7259 * Our hole starts before our delalloc, so we have to
7260 * return just the parts of the hole that go until the
7261 * delalloc starts
Chris Masonec29ed52011-02-23 16:23:20 -05007262 */
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007263 em->len = min(hole_len, delalloc_start - hole_start);
Chris Masonec29ed52011-02-23 16:23:20 -05007264 em->start = hole_start;
7265 em->orig_start = hole_start;
7266 /*
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007267 * Don't adjust block start at all, it is fixed at
7268 * EXTENT_MAP_HOLE
Chris Masonec29ed52011-02-23 16:23:20 -05007269 */
7270 em->block_start = hole_em->block_start;
7271 em->block_len = hole_len;
Liu Bof9e4fb52013-01-07 10:10:12 +00007272 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
7273 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonec29ed52011-02-23 16:23:20 -05007274 } else {
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007275 /*
7276 * Hole is out of passed range or it starts after
7277 * delalloc range
7278 */
7279 em->start = delalloc_start;
7280 em->len = delalloc_len;
7281 em->orig_start = delalloc_start;
Chris Masonec29ed52011-02-23 16:23:20 -05007282 em->block_start = EXTENT_MAP_DELALLOC;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007283 em->block_len = delalloc_len;
Chris Masonec29ed52011-02-23 16:23:20 -05007284 }
Nikolay Borisovbf8d32b2017-12-01 11:19:43 +02007285 } else {
Chris Masonec29ed52011-02-23 16:23:20 -05007286 return hole_em;
7287 }
7288out:
7289
7290 free_extent_map(hole_em);
7291 if (err) {
7292 free_extent_map(em);
7293 return ERR_PTR(err);
7294 }
7295 return em;
7296}
7297
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007298static struct extent_map *btrfs_create_dio_extent(struct inode *inode,
7299 const u64 start,
7300 const u64 len,
7301 const u64 orig_start,
7302 const u64 block_start,
7303 const u64 block_len,
7304 const u64 orig_block_len,
7305 const u64 ram_bytes,
7306 const int type)
7307{
7308 struct extent_map *em = NULL;
7309 int ret;
7310
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007311 if (type != BTRFS_ORDERED_NOCOW) {
Liu Bo6f9994d2017-01-31 07:50:22 -08007312 em = create_io_em(inode, start, len, orig_start,
7313 block_start, block_len, orig_block_len,
7314 ram_bytes,
7315 BTRFS_COMPRESS_NONE, /* compress_type */
7316 type);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007317 if (IS_ERR(em))
7318 goto out;
7319 }
7320 ret = btrfs_add_ordered_extent_dio(inode, start, block_start,
7321 len, block_len, type);
7322 if (ret) {
7323 if (em) {
7324 free_extent_map(em);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02007325 btrfs_drop_extent_cache(BTRFS_I(inode), start,
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007326 start + len - 1, 0);
7327 }
7328 em = ERR_PTR(ret);
7329 }
7330 out:
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007331
7332 return em;
7333}
7334
Josef Bacik4b46fce2010-05-23 11:00:55 -04007335static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
7336 u64 start, u64 len)
7337{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007338 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007339 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik70c8a912012-10-11 16:54:30 -04007340 struct extent_map *em;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007341 struct btrfs_key ins;
7342 u64 alloc_hint;
7343 int ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007344
Josef Bacik4b46fce2010-05-23 11:00:55 -04007345 alloc_hint = get_extent_allocation_hint(inode, start, len);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007346 ret = btrfs_reserve_extent(root, len, len, fs_info->sectorsize,
Jeff Mahoneyda170662016-06-15 09:22:56 -04007347 0, alloc_hint, &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007348 if (ret)
7349 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007350
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007351 em = btrfs_create_dio_extent(inode, start, ins.offset, start,
7352 ins.objectid, ins.offset, ins.offset,
Liu Bo6288d6e2017-02-21 12:12:58 -08007353 ins.offset, BTRFS_ORDERED_REGULAR);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007354 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007355 if (IS_ERR(em))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007356 btrfs_free_reserved_extent(fs_info, ins.objectid,
7357 ins.offset, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007358
Josef Bacik4b46fce2010-05-23 11:00:55 -04007359 return em;
7360}
7361
Chris Mason46bfbb52010-05-26 11:04:10 -04007362/*
7363 * returns 1 when the nocow is safe, < 1 on error, 0 if the
7364 * block must be cow'd
7365 */
Josef Bacik00361582013-08-14 14:02:47 -04007366noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
Josef Bacik7ee9e442013-06-21 16:37:03 -04007367 u64 *orig_start, u64 *orig_block_len,
7368 u64 *ram_bytes)
Chris Mason46bfbb52010-05-26 11:04:10 -04007369{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007370 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason46bfbb52010-05-26 11:04:10 -04007371 struct btrfs_path *path;
7372 int ret;
7373 struct extent_buffer *leaf;
7374 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xie7b2b7082014-02-27 13:58:05 +08007375 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason46bfbb52010-05-26 11:04:10 -04007376 struct btrfs_file_extent_item *fi;
7377 struct btrfs_key key;
7378 u64 disk_bytenr;
7379 u64 backref_offset;
7380 u64 extent_end;
7381 u64 num_bytes;
7382 int slot;
7383 int found_type;
Josef Bacik7ee9e442013-06-21 16:37:03 -04007384 bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW);
Miao Xiee77751a2013-12-27 21:11:50 +08007385
Chris Mason46bfbb52010-05-26 11:04:10 -04007386 path = btrfs_alloc_path();
7387 if (!path)
7388 return -ENOMEM;
7389
David Sterbaf85b7372017-01-20 14:54:07 +01007390 ret = btrfs_lookup_file_extent(NULL, root, path,
7391 btrfs_ino(BTRFS_I(inode)), offset, 0);
Chris Mason46bfbb52010-05-26 11:04:10 -04007392 if (ret < 0)
7393 goto out;
7394
7395 slot = path->slots[0];
7396 if (ret == 1) {
7397 if (slot == 0) {
7398 /* can't find the item, must cow */
7399 ret = 0;
7400 goto out;
7401 }
7402 slot--;
7403 }
7404 ret = 0;
7405 leaf = path->nodes[0];
7406 btrfs_item_key_to_cpu(leaf, &key, slot);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02007407 if (key.objectid != btrfs_ino(BTRFS_I(inode)) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04007408 key.type != BTRFS_EXTENT_DATA_KEY) {
7409 /* not our file or wrong item type, must cow */
7410 goto out;
7411 }
7412
7413 if (key.offset > offset) {
7414 /* Wrong offset, must cow */
7415 goto out;
7416 }
7417
7418 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
7419 found_type = btrfs_file_extent_type(leaf, fi);
7420 if (found_type != BTRFS_FILE_EXTENT_REG &&
7421 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
7422 /* not a regular extent, must cow */
7423 goto out;
7424 }
Josef Bacik7ee9e442013-06-21 16:37:03 -04007425
7426 if (!nocow && found_type == BTRFS_FILE_EXTENT_REG)
7427 goto out;
7428
Miao Xiee77751a2013-12-27 21:11:50 +08007429 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
7430 if (extent_end <= offset)
7431 goto out;
7432
Chris Mason46bfbb52010-05-26 11:04:10 -04007433 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Josef Bacik7ee9e442013-06-21 16:37:03 -04007434 if (disk_bytenr == 0)
7435 goto out;
7436
7437 if (btrfs_file_extent_compression(leaf, fi) ||
7438 btrfs_file_extent_encryption(leaf, fi) ||
7439 btrfs_file_extent_other_encoding(leaf, fi))
7440 goto out;
7441
Ethan Lien78d42952018-05-17 14:58:29 +08007442 /*
7443 * Do the same check as in btrfs_cross_ref_exist but without the
7444 * unnecessary search.
7445 */
7446 if (btrfs_file_extent_generation(leaf, fi) <=
7447 btrfs_root_last_snapshot(&root->root_item))
7448 goto out;
7449
Chris Mason46bfbb52010-05-26 11:04:10 -04007450 backref_offset = btrfs_file_extent_offset(leaf, fi);
7451
Josef Bacik7ee9e442013-06-21 16:37:03 -04007452 if (orig_start) {
7453 *orig_start = key.offset - backref_offset;
7454 *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
7455 *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
7456 }
Josef Bacikeb384b52013-04-24 16:32:55 -04007457
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007458 if (btrfs_extent_readonly(fs_info, disk_bytenr))
Chris Mason46bfbb52010-05-26 11:04:10 -04007459 goto out;
Miao Xie7b2b7082014-02-27 13:58:05 +08007460
7461 num_bytes = min(offset + *len, extent_end) - offset;
7462 if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) {
7463 u64 range_end;
7464
Jeff Mahoneyda170662016-06-15 09:22:56 -04007465 range_end = round_up(offset + num_bytes,
7466 root->fs_info->sectorsize) - 1;
Miao Xie7b2b7082014-02-27 13:58:05 +08007467 ret = test_range_bit(io_tree, offset, range_end,
7468 EXTENT_DELALLOC, 0, NULL);
7469 if (ret) {
7470 ret = -EAGAIN;
7471 goto out;
7472 }
7473 }
7474
Josef Bacik1bda19e2013-10-18 12:10:36 -04007475 btrfs_release_path(path);
Chris Mason46bfbb52010-05-26 11:04:10 -04007476
7477 /*
7478 * look for other files referencing this extent, if we
7479 * find any we must cow
7480 */
Josef Bacik00361582013-08-14 14:02:47 -04007481
Liu Boe4c3b2d2017-01-30 12:25:28 -08007482 ret = btrfs_cross_ref_exist(root, btrfs_ino(BTRFS_I(inode)),
Josef Bacik00361582013-08-14 14:02:47 -04007483 key.offset - backref_offset, disk_bytenr);
Josef Bacik00361582013-08-14 14:02:47 -04007484 if (ret) {
7485 ret = 0;
7486 goto out;
7487 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007488
7489 /*
7490 * adjust disk_bytenr and num_bytes to cover just the bytes
7491 * in this extent we are about to write. If there
7492 * are any csums in that range we have to cow in order
7493 * to keep the csums correct
7494 */
7495 disk_bytenr += backref_offset;
7496 disk_bytenr += offset - key.offset;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007497 if (csum_exist_in_range(fs_info, disk_bytenr, num_bytes))
7498 goto out;
Chris Mason46bfbb52010-05-26 11:04:10 -04007499 /*
7500 * all of the above have passed, it is safe to overwrite this extent
7501 * without cow
7502 */
Josef Bacikeb384b52013-04-24 16:32:55 -04007503 *len = num_bytes;
Chris Mason46bfbb52010-05-26 11:04:10 -04007504 ret = 1;
7505out:
7506 btrfs_free_path(path);
7507 return ret;
7508}
7509
Josef Bacikeb838e72012-07-31 16:28:48 -04007510static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
7511 struct extent_state **cached_state, int writing)
7512{
7513 struct btrfs_ordered_extent *ordered;
7514 int ret = 0;
7515
7516 while (1) {
7517 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbaff13db42015-12-03 14:30:40 +01007518 cached_state);
Josef Bacikeb838e72012-07-31 16:28:48 -04007519 /*
7520 * We're concerned with the entire range that we're going to be
Nicholas D Steeves01327612016-05-19 21:18:45 -04007521 * doing DIO to, so we need to make sure there's no ordered
Josef Bacikeb838e72012-07-31 16:28:48 -04007522 * extents in this range.
7523 */
Nikolay Borisova776c6f2017-02-20 13:50:49 +02007524 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), lockstart,
Josef Bacikeb838e72012-07-31 16:28:48 -04007525 lockend - lockstart + 1);
7526
7527 /*
7528 * We need to make sure there are no buffered pages in this
7529 * range either, we could have raced between the invalidate in
7530 * generic_file_direct_write and locking the extent. The
7531 * invalidate needs to happen so that reads after a write do not
7532 * get stale data.
7533 */
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007534 if (!ordered &&
David Sterba051c98e2018-03-07 15:33:22 +01007535 (!writing || !filemap_range_has_page(inode->i_mapping,
7536 lockstart, lockend)))
Josef Bacikeb838e72012-07-31 16:28:48 -04007537 break;
7538
7539 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbae43bbe52017-12-12 21:43:52 +01007540 cached_state);
Josef Bacikeb838e72012-07-31 16:28:48 -04007541
7542 if (ordered) {
Filipe Mananaade77022016-02-18 14:28:55 +00007543 /*
7544 * If we are doing a DIO read and the ordered extent we
7545 * found is for a buffered write, we can not wait for it
7546 * to complete and retry, because if we do so we can
7547 * deadlock with concurrent buffered writes on page
7548 * locks. This happens only if our DIO read covers more
7549 * than one extent map, if at this point has already
7550 * created an ordered extent for a previous extent map
7551 * and locked its range in the inode's io tree, and a
7552 * concurrent write against that previous extent map's
7553 * range and this range started (we unlock the ranges
7554 * in the io tree only when the bios complete and
7555 * buffered writes always lock pages before attempting
7556 * to lock range in the io tree).
7557 */
7558 if (writing ||
7559 test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags))
7560 btrfs_start_ordered_extent(inode, ordered, 1);
7561 else
7562 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007563 btrfs_put_ordered_extent(ordered);
7564 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007565 /*
Filipe Mananab850ae12015-12-08 16:23:16 +00007566 * We could trigger writeback for this range (and wait
7567 * for it to complete) and then invalidate the pages for
7568 * this range (through invalidate_inode_pages2_range()),
7569 * but that can lead us to a deadlock with a concurrent
7570 * call to readpages() (a buffered read or a defrag call
7571 * triggered a readahead) on a page lock due to an
7572 * ordered dio extent we created before but did not have
7573 * yet a corresponding bio submitted (whence it can not
7574 * complete), which makes readpages() wait for that
7575 * ordered extent to complete while holding a lock on
7576 * that page.
Josef Bacikeb838e72012-07-31 16:28:48 -04007577 */
Filipe Mananab850ae12015-12-08 16:23:16 +00007578 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007579 }
7580
Filipe Mananaade77022016-02-18 14:28:55 +00007581 if (ret)
7582 break;
7583
Josef Bacikeb838e72012-07-31 16:28:48 -04007584 cond_resched();
7585 }
7586
7587 return ret;
7588}
7589
Liu Bo6f9994d2017-01-31 07:50:22 -08007590/* The callers of this must take lock_extent() */
7591static struct extent_map *create_io_em(struct inode *inode, u64 start, u64 len,
7592 u64 orig_start, u64 block_start,
7593 u64 block_len, u64 orig_block_len,
7594 u64 ram_bytes, int compress_type,
7595 int type)
Josef Bacik69ffb542012-09-11 15:40:07 -04007596{
7597 struct extent_map_tree *em_tree;
7598 struct extent_map *em;
Josef Bacik69ffb542012-09-11 15:40:07 -04007599 int ret;
7600
Liu Bo6f9994d2017-01-31 07:50:22 -08007601 ASSERT(type == BTRFS_ORDERED_PREALLOC ||
7602 type == BTRFS_ORDERED_COMPRESSED ||
7603 type == BTRFS_ORDERED_NOCOW ||
Liu Bo1af4a0a2017-02-13 15:35:09 -08007604 type == BTRFS_ORDERED_REGULAR);
Liu Bo6f9994d2017-01-31 07:50:22 -08007605
Josef Bacik69ffb542012-09-11 15:40:07 -04007606 em_tree = &BTRFS_I(inode)->extent_tree;
7607 em = alloc_extent_map();
7608 if (!em)
7609 return ERR_PTR(-ENOMEM);
7610
7611 em->start = start;
7612 em->orig_start = orig_start;
7613 em->len = len;
7614 em->block_len = block_len;
7615 em->block_start = block_start;
Josef Bacikb4939682012-12-03 10:31:19 -05007616 em->orig_block_len = orig_block_len;
Josef Bacikcc95bef2013-04-04 14:31:27 -04007617 em->ram_bytes = ram_bytes;
Josef Bacik70c8a912012-10-11 16:54:30 -04007618 em->generation = -1;
Josef Bacik69ffb542012-09-11 15:40:07 -04007619 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Liu Bo1af4a0a2017-02-13 15:35:09 -08007620 if (type == BTRFS_ORDERED_PREALLOC) {
Josef Bacikb11e2342012-12-03 10:58:15 -05007621 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Liu Bo1af4a0a2017-02-13 15:35:09 -08007622 } else if (type == BTRFS_ORDERED_COMPRESSED) {
Liu Bo6f9994d2017-01-31 07:50:22 -08007623 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
7624 em->compress_type = compress_type;
7625 }
Josef Bacik69ffb542012-09-11 15:40:07 -04007626
7627 do {
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02007628 btrfs_drop_extent_cache(BTRFS_I(inode), em->start,
Josef Bacik69ffb542012-09-11 15:40:07 -04007629 em->start + em->len - 1, 0);
7630 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04007631 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik69ffb542012-09-11 15:40:07 -04007632 write_unlock(&em_tree->lock);
Liu Bo6f9994d2017-01-31 07:50:22 -08007633 /*
7634 * The caller has taken lock_extent(), who could race with us
7635 * to add em?
7636 */
Josef Bacik69ffb542012-09-11 15:40:07 -04007637 } while (ret == -EEXIST);
7638
7639 if (ret) {
7640 free_extent_map(em);
7641 return ERR_PTR(ret);
7642 }
7643
Liu Bo6f9994d2017-01-31 07:50:22 -08007644 /* em got 2 refs now, callers needs to do free_extent_map once. */
Josef Bacik69ffb542012-09-11 15:40:07 -04007645 return em;
7646}
7647
Nikolay Borisov1c8d0172018-05-02 15:19:32 +03007648
7649static int btrfs_get_blocks_direct_read(struct extent_map *em,
7650 struct buffer_head *bh_result,
7651 struct inode *inode,
7652 u64 start, u64 len)
7653{
David Sterba8530c372019-08-30 15:42:07 +02007654 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
7655
Nikolay Borisov1c8d0172018-05-02 15:19:32 +03007656 if (em->block_start == EXTENT_MAP_HOLE ||
7657 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7658 return -ENOENT;
7659
7660 len = min(len, em->len - (start - em->start));
7661
7662 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
7663 inode->i_blkbits;
7664 bh_result->b_size = len;
David Sterba8530c372019-08-30 15:42:07 +02007665 bh_result->b_bdev = fs_info->fs_devices->latest_bdev;
Nikolay Borisov1c8d0172018-05-02 15:19:32 +03007666 set_buffer_mapped(bh_result);
7667
7668 return 0;
7669}
7670
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007671static int btrfs_get_blocks_direct_write(struct extent_map **map,
7672 struct buffer_head *bh_result,
7673 struct inode *inode,
7674 struct btrfs_dio_data *dio_data,
7675 u64 start, u64 len)
7676{
7677 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
7678 struct extent_map *em = *map;
7679 int ret = 0;
7680
7681 /*
7682 * We don't allocate a new extent in the following cases
7683 *
7684 * 1) The inode is marked as NODATACOW. In this case we'll just use the
7685 * existing extent.
7686 * 2) The extent is marked as PREALLOC. We're good to go here and can
7687 * just use the extent.
7688 *
7689 */
7690 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
7691 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
7692 em->block_start != EXTENT_MAP_HOLE)) {
7693 int type;
7694 u64 block_start, orig_start, orig_block_len, ram_bytes;
7695
7696 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7697 type = BTRFS_ORDERED_PREALLOC;
7698 else
7699 type = BTRFS_ORDERED_NOCOW;
7700 len = min(len, em->len - (start - em->start));
7701 block_start = em->block_start + (start - em->start);
7702
7703 if (can_nocow_extent(inode, start, &len, &orig_start,
7704 &orig_block_len, &ram_bytes) == 1 &&
7705 btrfs_inc_nocow_writers(fs_info, block_start)) {
7706 struct extent_map *em2;
7707
7708 em2 = btrfs_create_dio_extent(inode, start, len,
7709 orig_start, block_start,
7710 len, orig_block_len,
7711 ram_bytes, type);
7712 btrfs_dec_nocow_writers(fs_info, block_start);
7713 if (type == BTRFS_ORDERED_PREALLOC) {
7714 free_extent_map(em);
7715 *map = em = em2;
7716 }
7717
7718 if (em2 && IS_ERR(em2)) {
7719 ret = PTR_ERR(em2);
7720 goto out;
7721 }
7722 /*
7723 * For inode marked NODATACOW or extent marked PREALLOC,
7724 * use the existing or preallocated extent, so does not
7725 * need to adjust btrfs_space_info's bytes_may_use.
7726 */
7727 btrfs_free_reserved_data_space_noquota(inode, start,
7728 len);
7729 goto skip_cow;
7730 }
7731 }
7732
7733 /* this will cow the extent */
7734 len = bh_result->b_size;
7735 free_extent_map(em);
7736 *map = em = btrfs_new_extent_direct(inode, start, len);
7737 if (IS_ERR(em)) {
7738 ret = PTR_ERR(em);
7739 goto out;
7740 }
7741
7742 len = min(len, em->len - (start - em->start));
7743
7744skip_cow:
7745 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
7746 inode->i_blkbits;
7747 bh_result->b_size = len;
David Sterba8530c372019-08-30 15:42:07 +02007748 bh_result->b_bdev = fs_info->fs_devices->latest_bdev;
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007749 set_buffer_mapped(bh_result);
7750
7751 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7752 set_buffer_new(bh_result);
7753
7754 /*
7755 * Need to update the i_size under the extent lock so buffered
7756 * readers will get the updated i_size when we unlock.
7757 */
7758 if (!dio_data->overwrite && start + len > i_size_read(inode))
7759 i_size_write(inode, start + len);
7760
7761 WARN_ON(dio_data->reserve < len);
7762 dio_data->reserve -= len;
7763 dio_data->unsubmitted_oe_range_end = start + len;
7764 current->journal_info = dio_data;
7765out:
7766 return ret;
7767}
7768
Josef Bacik4b46fce2010-05-23 11:00:55 -04007769static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
7770 struct buffer_head *bh_result, int create)
7771{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007772 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007773 struct extent_map *em;
Josef Bacikeb838e72012-07-31 16:28:48 -04007774 struct extent_state *cached_state = NULL;
chandan50745b02015-08-28 21:10:13 +05307775 struct btrfs_dio_data *dio_data = NULL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007776 u64 start = iblock << inode->i_blkbits;
Josef Bacikeb838e72012-07-31 16:28:48 -04007777 u64 lockstart, lockend;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007778 u64 len = bh_result->b_size;
Miao Xie09348562013-02-07 10:12:07 +00007779 int ret = 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007780
Omar Sandovale1821632019-08-15 14:04:04 -07007781 if (!create)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007782 len = min_t(u64, len, fs_info->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04007783
Josef Bacikc3298612012-08-03 16:49:19 -04007784 lockstart = start;
7785 lockend = start + len - 1;
7786
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007787 if (current->journal_info) {
7788 /*
7789 * Need to pull our outstanding extents and set journal_info to NULL so
Nicholas D Steeves01327612016-05-19 21:18:45 -04007790 * that anything that needs to check if there's a transaction doesn't get
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007791 * confused.
7792 */
chandan50745b02015-08-28 21:10:13 +05307793 dio_data = current->journal_info;
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007794 current->journal_info = NULL;
7795 }
7796
Josef Bacikeb838e72012-07-31 16:28:48 -04007797 /*
7798 * If this errors out it's because we couldn't invalidate pagecache for
7799 * this range and we need to fallback to buffered.
7800 */
Filipe Manana9c9464c2015-11-04 09:52:04 +00007801 if (lock_extent_direct(inode, lockstart, lockend, &cached_state,
7802 create)) {
7803 ret = -ENOTBLK;
7804 goto err;
7805 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007806
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007807 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len, 0);
Josef Bacikeb838e72012-07-31 16:28:48 -04007808 if (IS_ERR(em)) {
7809 ret = PTR_ERR(em);
7810 goto unlock_err;
7811 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007812
7813 /*
7814 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
7815 * io. INLINE is special, and we could probably kludge it in here, but
7816 * it's still buffered so for safety lets just fall back to the generic
7817 * buffered path.
7818 *
7819 * For COMPRESSED we _have_ to read the entire extent in so we can
7820 * decompress it, so there will be buffering required no matter what we
7821 * do, so go ahead and fallback to buffered.
7822 *
Nicholas D Steeves01327612016-05-19 21:18:45 -04007823 * We return -ENOTBLK because that's what makes DIO go ahead and go back
Josef Bacik4b46fce2010-05-23 11:00:55 -04007824 * to buffered IO. Don't blame me, this is the price we pay for using
7825 * the generic code.
7826 */
7827 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
7828 em->block_start == EXTENT_MAP_INLINE) {
7829 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007830 ret = -ENOTBLK;
7831 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007832 }
7833
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007834 if (create) {
7835 ret = btrfs_get_blocks_direct_write(&em, bh_result, inode,
7836 dio_data, start, len);
7837 if (ret < 0)
7838 goto unlock_err;
7839
Omar Sandovale1821632019-08-15 14:04:04 -07007840 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart,
7841 lockend, &cached_state);
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007842 } else {
Nikolay Borisov1c8d0172018-05-02 15:19:32 +03007843 ret = btrfs_get_blocks_direct_read(em, bh_result, inode,
7844 start, len);
7845 /* Can be negative only if we read from a hole */
7846 if (ret < 0) {
7847 ret = 0;
7848 free_extent_map(em);
7849 goto unlock_err;
7850 }
7851 /*
7852 * We need to unlock only the end area that we aren't using.
7853 * The rest is going to be unlocked by the endio routine.
7854 */
7855 lockstart = start + bh_result->b_size;
7856 if (lockstart < lockend) {
Omar Sandovale1821632019-08-15 14:04:04 -07007857 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
7858 lockstart, lockend, &cached_state);
Nikolay Borisov1c8d0172018-05-02 15:19:32 +03007859 } else {
7860 free_extent_state(cached_state);
7861 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007862 }
7863
Josef Bacik4b46fce2010-05-23 11:00:55 -04007864 free_extent_map(em);
7865
7866 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007867
7868unlock_err:
Omar Sandovale1821632019-08-15 14:04:04 -07007869 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7870 &cached_state);
Filipe Manana9c9464c2015-11-04 09:52:04 +00007871err:
chandan50745b02015-08-28 21:10:13 +05307872 if (dio_data)
7873 current->journal_info = dio_data;
Josef Bacikeb838e72012-07-31 16:28:48 -04007874 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007875}
7876
Omar Sandoval58efbc92017-08-22 23:45:59 -07007877static inline blk_status_t submit_dio_repair_bio(struct inode *inode,
7878 struct bio *bio,
7879 int mirror_num)
Miao Xie8b110e32014-09-12 18:44:03 +08007880{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007881 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Omar Sandoval58efbc92017-08-22 23:45:59 -07007882 blk_status_t ret;
Miao Xie8b110e32014-09-12 18:44:03 +08007883
Mike Christie37226b22016-06-05 14:31:52 -05007884 BUG_ON(bio_op(bio) == REQ_OP_WRITE);
Miao Xie8b110e32014-09-12 18:44:03 +08007885
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007886 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DIO_REPAIR);
Miao Xie8b110e32014-09-12 18:44:03 +08007887 if (ret)
Nikolay Borisovea057f62017-12-13 10:25:38 +02007888 return ret;
Miao Xie8b110e32014-09-12 18:44:03 +08007889
Chris Mason08635ba2019-07-10 12:28:14 -07007890 ret = btrfs_map_bio(fs_info, bio, mirror_num);
Nikolay Borisovea057f62017-12-13 10:25:38 +02007891
Miao Xie8b110e32014-09-12 18:44:03 +08007892 return ret;
7893}
7894
7895static int btrfs_check_dio_repairable(struct inode *inode,
7896 struct bio *failed_bio,
7897 struct io_failure_record *failrec,
7898 int failed_mirror)
7899{
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007900 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xie8b110e32014-09-12 18:44:03 +08007901 int num_copies;
7902
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007903 num_copies = btrfs_num_copies(fs_info, failrec->logical, failrec->len);
Miao Xie8b110e32014-09-12 18:44:03 +08007904 if (num_copies == 1) {
7905 /*
7906 * we only have a single copy of the data, so don't bother with
7907 * all the retry and error correction code that follows. no
7908 * matter what the error is, it is very likely to persist.
7909 */
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007910 btrfs_debug(fs_info,
7911 "Check DIO Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d",
7912 num_copies, failrec->this_mirror, failed_mirror);
Miao Xie8b110e32014-09-12 18:44:03 +08007913 return 0;
7914 }
7915
7916 failrec->failed_mirror = failed_mirror;
7917 failrec->this_mirror++;
7918 if (failrec->this_mirror == failed_mirror)
7919 failrec->this_mirror++;
7920
7921 if (failrec->this_mirror > num_copies) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007922 btrfs_debug(fs_info,
7923 "Check DIO Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d",
7924 num_copies, failrec->this_mirror, failed_mirror);
Miao Xie8b110e32014-09-12 18:44:03 +08007925 return 0;
7926 }
7927
7928 return 1;
7929}
7930
Omar Sandoval58efbc92017-08-22 23:45:59 -07007931static blk_status_t dio_read_error(struct inode *inode, struct bio *failed_bio,
7932 struct page *page, unsigned int pgoff,
7933 u64 start, u64 end, int failed_mirror,
7934 bio_end_io_t *repair_endio, void *repair_arg)
Miao Xie8b110e32014-09-12 18:44:03 +08007935{
7936 struct io_failure_record *failrec;
Josef Bacik7870d082017-05-05 11:57:15 -04007937 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
7938 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
Miao Xie8b110e32014-09-12 18:44:03 +08007939 struct bio *bio;
7940 int isector;
David Sterbaf1c77c52017-06-06 19:03:49 +02007941 unsigned int read_mode = 0;
Liu Bo17347ce2017-05-15 15:33:27 -07007942 int segs;
Miao Xie8b110e32014-09-12 18:44:03 +08007943 int ret;
Omar Sandoval58efbc92017-08-22 23:45:59 -07007944 blk_status_t status;
Ming Leic16a8ac2017-12-18 20:22:12 +08007945 struct bio_vec bvec;
Miao Xie8b110e32014-09-12 18:44:03 +08007946
Mike Christie37226b22016-06-05 14:31:52 -05007947 BUG_ON(bio_op(failed_bio) == REQ_OP_WRITE);
Miao Xie8b110e32014-09-12 18:44:03 +08007948
7949 ret = btrfs_get_io_failure_record(inode, start, end, &failrec);
7950 if (ret)
Omar Sandoval58efbc92017-08-22 23:45:59 -07007951 return errno_to_blk_status(ret);
Miao Xie8b110e32014-09-12 18:44:03 +08007952
7953 ret = btrfs_check_dio_repairable(inode, failed_bio, failrec,
7954 failed_mirror);
7955 if (!ret) {
Josef Bacik7870d082017-05-05 11:57:15 -04007956 free_io_failure(failure_tree, io_tree, failrec);
Omar Sandoval58efbc92017-08-22 23:45:59 -07007957 return BLK_STS_IOERR;
Miao Xie8b110e32014-09-12 18:44:03 +08007958 }
7959
Liu Bo17347ce2017-05-15 15:33:27 -07007960 segs = bio_segments(failed_bio);
Ming Leic16a8ac2017-12-18 20:22:12 +08007961 bio_get_first_bvec(failed_bio, &bvec);
Liu Bo17347ce2017-05-15 15:33:27 -07007962 if (segs > 1 ||
Ming Leic16a8ac2017-12-18 20:22:12 +08007963 (bvec.bv_len > btrfs_inode_sectorsize(inode)))
Christoph Hellwig70fd7612016-11-01 07:40:10 -06007964 read_mode |= REQ_FAILFAST_DEV;
Miao Xie8b110e32014-09-12 18:44:03 +08007965
7966 isector = start - btrfs_io_bio(failed_bio)->logical;
7967 isector >>= inode->i_sb->s_blocksize_bits;
7968 bio = btrfs_create_repair_bio(inode, failed_bio, failrec, page,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307969 pgoff, isector, repair_endio, repair_arg);
David Sterbaebcc3262018-06-29 10:56:53 +02007970 bio->bi_opf = REQ_OP_READ | read_mode;
Miao Xie8b110e32014-09-12 18:44:03 +08007971
7972 btrfs_debug(BTRFS_I(inode)->root->fs_info,
David Sterba913e1532017-07-13 15:32:18 +02007973 "repair DIO read error: submitting new dio read[%#x] to this_mirror=%d, in_validation=%d",
Miao Xie8b110e32014-09-12 18:44:03 +08007974 read_mode, failrec->this_mirror, failrec->in_validation);
7975
Omar Sandoval58efbc92017-08-22 23:45:59 -07007976 status = submit_dio_repair_bio(inode, bio, failrec->this_mirror);
7977 if (status) {
Josef Bacik7870d082017-05-05 11:57:15 -04007978 free_io_failure(failure_tree, io_tree, failrec);
Miao Xie8b110e32014-09-12 18:44:03 +08007979 bio_put(bio);
7980 }
7981
Omar Sandoval58efbc92017-08-22 23:45:59 -07007982 return status;
Miao Xie8b110e32014-09-12 18:44:03 +08007983}
7984
7985struct btrfs_retry_complete {
7986 struct completion done;
7987 struct inode *inode;
7988 u64 start;
7989 int uptodate;
7990};
7991
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007992static void btrfs_retry_endio_nocsum(struct bio *bio)
Miao Xie8b110e32014-09-12 18:44:03 +08007993{
7994 struct btrfs_retry_complete *done = bio->bi_private;
Josef Bacik7870d082017-05-05 11:57:15 -04007995 struct inode *inode = done->inode;
Miao Xie8b110e32014-09-12 18:44:03 +08007996 struct bio_vec *bvec;
Josef Bacik7870d082017-05-05 11:57:15 -04007997 struct extent_io_tree *io_tree, *failure_tree;
Ming Lei6dc4f102019-02-15 19:13:19 +08007998 struct bvec_iter_all iter_all;
Miao Xie8b110e32014-09-12 18:44:03 +08007999
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008000 if (bio->bi_status)
Miao Xie8b110e32014-09-12 18:44:03 +08008001 goto end;
8002
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308003 ASSERT(bio->bi_vcnt == 1);
Josef Bacik7870d082017-05-05 11:57:15 -04008004 io_tree = &BTRFS_I(inode)->io_tree;
8005 failure_tree = &BTRFS_I(inode)->io_failure_tree;
Ming Lei263663c2017-12-18 20:22:04 +08008006 ASSERT(bio_first_bvec_all(bio)->bv_len == btrfs_inode_sectorsize(inode));
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308007
Miao Xie8b110e32014-09-12 18:44:03 +08008008 done->uptodate = 1;
David Sterbac09abff2017-07-13 18:10:07 +02008009 ASSERT(!bio_flagged(bio, BIO_CLONED));
Christoph Hellwig2b070cf2019-04-25 09:03:00 +02008010 bio_for_each_segment_all(bvec, bio, iter_all)
Josef Bacik7870d082017-05-05 11:57:15 -04008011 clean_io_failure(BTRFS_I(inode)->root->fs_info, failure_tree,
8012 io_tree, done->start, bvec->bv_page,
8013 btrfs_ino(BTRFS_I(inode)), 0);
Miao Xie8b110e32014-09-12 18:44:03 +08008014end:
8015 complete(&done->done);
8016 bio_put(bio);
8017}
8018
Omar Sandoval58efbc92017-08-22 23:45:59 -07008019static blk_status_t __btrfs_correct_data_nocsum(struct inode *inode,
8020 struct btrfs_io_bio *io_bio)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008021{
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308022 struct btrfs_fs_info *fs_info;
Liu Bo17347ce2017-05-15 15:33:27 -07008023 struct bio_vec bvec;
8024 struct bvec_iter iter;
Miao Xie8b110e32014-09-12 18:44:03 +08008025 struct btrfs_retry_complete done;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008026 u64 start;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308027 unsigned int pgoff;
8028 u32 sectorsize;
8029 int nr_sectors;
Omar Sandoval58efbc92017-08-22 23:45:59 -07008030 blk_status_t ret;
8031 blk_status_t err = BLK_STS_OK;
Miao Xiedc380ae2014-09-12 18:43:55 +08008032
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308033 fs_info = BTRFS_I(inode)->root->fs_info;
Jeff Mahoneyda170662016-06-15 09:22:56 -04008034 sectorsize = fs_info->sectorsize;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308035
Miao Xiec1dc0892014-09-12 18:43:56 +08008036 start = io_bio->logical;
Miao Xie8b110e32014-09-12 18:44:03 +08008037 done.inode = inode;
Liu Bo17347ce2017-05-15 15:33:27 -07008038 io_bio->bio.bi_iter = io_bio->iter;
Miao Xie8b110e32014-09-12 18:44:03 +08008039
Liu Bo17347ce2017-05-15 15:33:27 -07008040 bio_for_each_segment(bvec, &io_bio->bio, iter) {
8041 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec.bv_len);
8042 pgoff = bvec.bv_offset;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308043
8044next_block_or_try_again:
Miao Xie8b110e32014-09-12 18:44:03 +08008045 done.uptodate = 0;
8046 done.start = start;
8047 init_completion(&done.done);
8048
Liu Bo17347ce2017-05-15 15:33:27 -07008049 ret = dio_read_error(inode, &io_bio->bio, bvec.bv_page,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308050 pgoff, start, start + sectorsize - 1,
8051 io_bio->mirror_num,
8052 btrfs_retry_endio_nocsum, &done);
Liu Bo629ebf42017-05-15 17:20:07 -07008053 if (ret) {
8054 err = ret;
8055 goto next;
8056 }
Miao Xie8b110e32014-09-12 18:44:03 +08008057
David Sterba9c17f6c2017-07-19 19:26:45 +02008058 wait_for_completion_io(&done.done);
Miao Xie8b110e32014-09-12 18:44:03 +08008059
8060 if (!done.uptodate) {
8061 /* We might have another mirror, so try again */
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308062 goto next_block_or_try_again;
Miao Xie8b110e32014-09-12 18:44:03 +08008063 }
8064
Liu Bo629ebf42017-05-15 17:20:07 -07008065next:
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308066 start += sectorsize;
8067
Liu Bo97bf5a52017-04-07 13:11:10 -07008068 nr_sectors--;
8069 if (nr_sectors) {
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308070 pgoff += sectorsize;
Liu Bo97bf5a52017-04-07 13:11:10 -07008071 ASSERT(pgoff < PAGE_SIZE);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308072 goto next_block_or_try_again;
8073 }
Miao Xie8b110e32014-09-12 18:44:03 +08008074 }
8075
Liu Bo629ebf42017-05-15 17:20:07 -07008076 return err;
Miao Xie8b110e32014-09-12 18:44:03 +08008077}
8078
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008079static void btrfs_retry_endio(struct bio *bio)
Miao Xie8b110e32014-09-12 18:44:03 +08008080{
8081 struct btrfs_retry_complete *done = bio->bi_private;
8082 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
Josef Bacik7870d082017-05-05 11:57:15 -04008083 struct extent_io_tree *io_tree, *failure_tree;
8084 struct inode *inode = done->inode;
Miao Xie8b110e32014-09-12 18:44:03 +08008085 struct bio_vec *bvec;
8086 int uptodate;
8087 int ret;
Christoph Hellwig2b070cf2019-04-25 09:03:00 +02008088 int i = 0;
Ming Lei6dc4f102019-02-15 19:13:19 +08008089 struct bvec_iter_all iter_all;
Miao Xie8b110e32014-09-12 18:44:03 +08008090
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008091 if (bio->bi_status)
Miao Xie8b110e32014-09-12 18:44:03 +08008092 goto end;
8093
8094 uptodate = 1;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308095
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308096 ASSERT(bio->bi_vcnt == 1);
Ming Lei263663c2017-12-18 20:22:04 +08008097 ASSERT(bio_first_bvec_all(bio)->bv_len == btrfs_inode_sectorsize(done->inode));
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308098
Josef Bacik7870d082017-05-05 11:57:15 -04008099 io_tree = &BTRFS_I(inode)->io_tree;
8100 failure_tree = &BTRFS_I(inode)->io_failure_tree;
8101
David Sterbac09abff2017-07-13 18:10:07 +02008102 ASSERT(!bio_flagged(bio, BIO_CLONED));
Christoph Hellwig2b070cf2019-04-25 09:03:00 +02008103 bio_for_each_segment_all(bvec, bio, iter_all) {
Josef Bacik7870d082017-05-05 11:57:15 -04008104 ret = __readpage_endio_check(inode, io_bio, i, bvec->bv_page,
8105 bvec->bv_offset, done->start,
8106 bvec->bv_len);
Miao Xie8b110e32014-09-12 18:44:03 +08008107 if (!ret)
Josef Bacik7870d082017-05-05 11:57:15 -04008108 clean_io_failure(BTRFS_I(inode)->root->fs_info,
8109 failure_tree, io_tree, done->start,
8110 bvec->bv_page,
8111 btrfs_ino(BTRFS_I(inode)),
8112 bvec->bv_offset);
Miao Xie8b110e32014-09-12 18:44:03 +08008113 else
8114 uptodate = 0;
Christoph Hellwig2b070cf2019-04-25 09:03:00 +02008115 i++;
Miao Xie8b110e32014-09-12 18:44:03 +08008116 }
8117
8118 done->uptodate = uptodate;
8119end:
8120 complete(&done->done);
8121 bio_put(bio);
8122}
8123
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008124static blk_status_t __btrfs_subio_endio_read(struct inode *inode,
8125 struct btrfs_io_bio *io_bio, blk_status_t err)
Miao Xie8b110e32014-09-12 18:44:03 +08008126{
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308127 struct btrfs_fs_info *fs_info;
Liu Bo17347ce2017-05-15 15:33:27 -07008128 struct bio_vec bvec;
8129 struct bvec_iter iter;
Miao Xie8b110e32014-09-12 18:44:03 +08008130 struct btrfs_retry_complete done;
8131 u64 start;
8132 u64 offset = 0;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308133 u32 sectorsize;
8134 int nr_sectors;
8135 unsigned int pgoff;
8136 int csum_pos;
Liu Boef7cdac12017-04-13 18:11:48 -07008137 bool uptodate = (err == 0);
Miao Xie8b110e32014-09-12 18:44:03 +08008138 int ret;
Omar Sandoval58efbc92017-08-22 23:45:59 -07008139 blk_status_t status;
Miao Xie8b110e32014-09-12 18:44:03 +08008140
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308141 fs_info = BTRFS_I(inode)->root->fs_info;
Jeff Mahoneyda170662016-06-15 09:22:56 -04008142 sectorsize = fs_info->sectorsize;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308143
Omar Sandoval58efbc92017-08-22 23:45:59 -07008144 err = BLK_STS_OK;
Miao Xie8b110e32014-09-12 18:44:03 +08008145 start = io_bio->logical;
8146 done.inode = inode;
Liu Bo17347ce2017-05-15 15:33:27 -07008147 io_bio->bio.bi_iter = io_bio->iter;
Miao Xie8b110e32014-09-12 18:44:03 +08008148
Liu Bo17347ce2017-05-15 15:33:27 -07008149 bio_for_each_segment(bvec, &io_bio->bio, iter) {
8150 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec.bv_len);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308151
Liu Bo17347ce2017-05-15 15:33:27 -07008152 pgoff = bvec.bv_offset;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308153next_block:
Liu Boef7cdac12017-04-13 18:11:48 -07008154 if (uptodate) {
8155 csum_pos = BTRFS_BYTES_TO_BLKS(fs_info, offset);
8156 ret = __readpage_endio_check(inode, io_bio, csum_pos,
8157 bvec.bv_page, pgoff, start, sectorsize);
8158 if (likely(!ret))
8159 goto next;
8160 }
Miao Xie8b110e32014-09-12 18:44:03 +08008161try_again:
8162 done.uptodate = 0;
8163 done.start = start;
8164 init_completion(&done.done);
8165
Omar Sandoval58efbc92017-08-22 23:45:59 -07008166 status = dio_read_error(inode, &io_bio->bio, bvec.bv_page,
8167 pgoff, start, start + sectorsize - 1,
8168 io_bio->mirror_num, btrfs_retry_endio,
8169 &done);
8170 if (status) {
8171 err = status;
Miao Xie8b110e32014-09-12 18:44:03 +08008172 goto next;
8173 }
8174
David Sterba9c17f6c2017-07-19 19:26:45 +02008175 wait_for_completion_io(&done.done);
Miao Xie8b110e32014-09-12 18:44:03 +08008176
8177 if (!done.uptodate) {
8178 /* We might have another mirror, so try again */
8179 goto try_again;
8180 }
8181next:
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308182 offset += sectorsize;
8183 start += sectorsize;
8184
8185 ASSERT(nr_sectors);
8186
Liu Bo97bf5a52017-04-07 13:11:10 -07008187 nr_sectors--;
8188 if (nr_sectors) {
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308189 pgoff += sectorsize;
Liu Bo97bf5a52017-04-07 13:11:10 -07008190 ASSERT(pgoff < PAGE_SIZE);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308191 goto next_block;
8192 }
Kent Overstreet2c30c712013-11-07 12:20:26 -08008193 }
Miao Xiec1dc0892014-09-12 18:43:56 +08008194
8195 return err;
8196}
8197
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008198static blk_status_t btrfs_subio_endio_read(struct inode *inode,
8199 struct btrfs_io_bio *io_bio, blk_status_t err)
Miao Xie8b110e32014-09-12 18:44:03 +08008200{
8201 bool skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
8202
8203 if (skip_csum) {
8204 if (unlikely(err))
8205 return __btrfs_correct_data_nocsum(inode, io_bio);
8206 else
Omar Sandoval58efbc92017-08-22 23:45:59 -07008207 return BLK_STS_OK;
Miao Xie8b110e32014-09-12 18:44:03 +08008208 } else {
8209 return __btrfs_subio_endio_read(inode, io_bio, err);
8210 }
8211}
8212
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008213static void btrfs_endio_direct_read(struct bio *bio)
Miao Xiec1dc0892014-09-12 18:43:56 +08008214{
8215 struct btrfs_dio_private *dip = bio->bi_private;
8216 struct inode *inode = dip->inode;
8217 struct bio *dio_bio;
8218 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008219 blk_status_t err = bio->bi_status;
Miao Xiec1dc0892014-09-12 18:43:56 +08008220
Liu Bo99c4e3b92017-09-15 15:06:51 -06008221 if (dip->flags & BTRFS_DIO_ORIG_BIO_SUBMITTED)
Miao Xie8b110e32014-09-12 18:44:03 +08008222 err = btrfs_subio_endio_read(inode, io_bio, err);
Miao Xiec1dc0892014-09-12 18:43:56 +08008223
Josef Bacik4b46fce2010-05-23 11:00:55 -04008224 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01008225 dip->logical_offset + dip->bytes - 1);
Chris Mason9be33952013-05-17 18:30:14 -04008226 dio_bio = dip->dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008227
Josef Bacik4b46fce2010-05-23 11:00:55 -04008228 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04008229
Liu Bo99c4e3b92017-09-15 15:06:51 -06008230 dio_bio->bi_status = err;
Christoph Hellwig40553512017-06-03 09:37:58 +02008231 dio_end_io(dio_bio);
David Sterbab3a0dd52018-11-22 17:16:49 +01008232 btrfs_io_bio_free_csum(io_bio);
Chris Mason9be33952013-05-17 18:30:14 -04008233 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008234}
8235
Qu Wenruo524272602017-03-08 10:25:52 +08008236static void __endio_write_update_ordered(struct inode *inode,
8237 const u64 offset, const u64 bytes,
8238 const bool uptodate)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008239{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008240 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008241 struct btrfs_ordered_extent *ordered = NULL;
Qu Wenruo524272602017-03-08 10:25:52 +08008242 struct btrfs_workqueue *wq;
Filipe Manana14543772015-11-24 16:23:54 +00008243 u64 ordered_offset = offset;
8244 u64 ordered_bytes = bytes;
Naohiro Aota67c003f2017-09-01 17:59:07 +09008245 u64 last_offset;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008246
Omar Sandovala0cac0e2019-09-16 11:30:57 -07008247 if (btrfs_is_free_space_inode(BTRFS_I(inode)))
Qu Wenruo524272602017-03-08 10:25:52 +08008248 wq = fs_info->endio_freespace_worker;
Omar Sandovala0cac0e2019-09-16 11:30:57 -07008249 else
Qu Wenruo524272602017-03-08 10:25:52 +08008250 wq = fs_info->endio_write_workers;
Qu Wenruo524272602017-03-08 10:25:52 +08008251
Nikolay Borisovb25f0d02018-04-11 11:21:17 +03008252 while (ordered_offset < offset + bytes) {
8253 last_offset = ordered_offset;
8254 if (btrfs_dec_test_first_ordered_pending(inode, &ordered,
8255 &ordered_offset,
8256 ordered_bytes,
8257 uptodate)) {
Omar Sandovala0cac0e2019-09-16 11:30:57 -07008258 btrfs_init_work(&ordered->work, finish_ordered_fn, NULL,
8259 NULL);
Nikolay Borisovb25f0d02018-04-11 11:21:17 +03008260 btrfs_queue_work(wq, &ordered->work);
8261 }
8262 /*
8263 * If btrfs_dec_test_ordered_pending does not find any ordered
8264 * extent in the range, we can exit.
8265 */
8266 if (ordered_offset == last_offset)
8267 return;
8268 /*
8269 * Our bio might span multiple ordered extents. In this case
Andrea Gelmini52042d82018-11-28 12:05:13 +01008270 * we keep going until we have accounted the whole dio.
Nikolay Borisovb25f0d02018-04-11 11:21:17 +03008271 */
8272 if (ordered_offset < offset + bytes) {
8273 ordered_bytes = offset + bytes - ordered_offset;
8274 ordered = NULL;
8275 }
Chris Mason163cf092010-11-28 19:56:33 -05008276 }
Filipe Manana14543772015-11-24 16:23:54 +00008277}
8278
8279static void btrfs_endio_direct_write(struct bio *bio)
8280{
8281 struct btrfs_dio_private *dip = bio->bi_private;
8282 struct bio *dio_bio = dip->dio_bio;
8283
Qu Wenruo524272602017-03-08 10:25:52 +08008284 __endio_write_update_ordered(dip->inode, dip->logical_offset,
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008285 dip->bytes, !bio->bi_status);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008286
Josef Bacik4b46fce2010-05-23 11:00:55 -04008287 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04008288
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008289 dio_bio->bi_status = bio->bi_status;
Christoph Hellwig40553512017-06-03 09:37:58 +02008290 dio_end_io(dio_bio);
Chris Mason9be33952013-05-17 18:30:14 -04008291 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008292}
8293
David Sterbad0ee3932018-03-08 14:35:48 +01008294static blk_status_t btrfs_submit_bio_start_direct_io(void *private_data,
David Sterbad0779292018-03-08 13:47:33 +01008295 struct bio *bio, u64 offset)
Chris Masoneaf25d92010-05-25 09:48:28 -04008296{
Josef Bacikc6100a42017-05-05 11:57:13 -04008297 struct inode *inode = private_data;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008298 blk_status_t ret;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008299 ret = btrfs_csum_one_bio(inode, bio, offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008300 BUG_ON(ret); /* -ENOMEM */
Chris Masoneaf25d92010-05-25 09:48:28 -04008301 return 0;
8302}
8303
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008304static void btrfs_end_dio_bio(struct bio *bio)
Miao Xiee65e1532010-11-22 03:04:43 +00008305{
8306 struct btrfs_dio_private *dip = bio->bi_private;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008307 blk_status_t err = bio->bi_status;
Miao Xiee65e1532010-11-22 03:04:43 +00008308
Miao Xie8b110e32014-09-12 18:44:03 +08008309 if (err)
8310 btrfs_warn(BTRFS_I(dip->inode)->root->fs_info,
Mike Christie6296b962016-06-05 14:32:21 -05008311 "direct IO failed ino %llu rw %d,%u sector %#Lx len %u err no %d",
David Sterbaf85b7372017-01-20 14:54:07 +01008312 btrfs_ino(BTRFS_I(dip->inode)), bio_op(bio),
8313 bio->bi_opf,
Miao Xie8b110e32014-09-12 18:44:03 +08008314 (unsigned long long)bio->bi_iter.bi_sector,
8315 bio->bi_iter.bi_size, err);
8316
8317 if (dip->subio_endio)
8318 err = dip->subio_endio(dip->inode, btrfs_io_bio(bio), err);
Miao Xiec1dc0892014-09-12 18:43:56 +08008319
8320 if (err) {
Miao Xiee65e1532010-11-22 03:04:43 +00008321 /*
Nikolay Borisovde224b72018-02-14 10:53:36 +02008322 * We want to perceive the errors flag being set before
8323 * decrementing the reference count. We don't need a barrier
8324 * since atomic operations with a return value are fully
8325 * ordered as per atomic_t.txt
Miao Xiee65e1532010-11-22 03:04:43 +00008326 */
Nikolay Borisovde224b72018-02-14 10:53:36 +02008327 dip->errors = 1;
Miao Xiee65e1532010-11-22 03:04:43 +00008328 }
8329
8330 /* if there are more bios still pending for this dio, just exit */
8331 if (!atomic_dec_and_test(&dip->pending_bios))
8332 goto out;
8333
Chris Mason9be33952013-05-17 18:30:14 -04008334 if (dip->errors) {
Miao Xiee65e1532010-11-22 03:04:43 +00008335 bio_io_error(dip->orig_bio);
Chris Mason9be33952013-05-17 18:30:14 -04008336 } else {
Anand Jain2dbe0c72017-10-14 08:35:56 +08008337 dip->dio_bio->bi_status = BLK_STS_OK;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008338 bio_endio(dip->orig_bio);
Miao Xiee65e1532010-11-22 03:04:43 +00008339 }
8340out:
8341 bio_put(bio);
8342}
8343
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008344static inline blk_status_t btrfs_lookup_and_bind_dio_csum(struct inode *inode,
Miao Xiec1dc0892014-09-12 18:43:56 +08008345 struct btrfs_dio_private *dip,
8346 struct bio *bio,
8347 u64 file_offset)
8348{
8349 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
8350 struct btrfs_io_bio *orig_io_bio = btrfs_io_bio(dip->orig_bio);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008351 blk_status_t ret;
Miao Xiec1dc0892014-09-12 18:43:56 +08008352
8353 /*
8354 * We load all the csum data we need when we submit
8355 * the first bio to reduce the csum tree search and
8356 * contention.
8357 */
8358 if (dip->logical_offset == file_offset) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008359 ret = btrfs_lookup_bio_sums_dio(inode, dip->orig_bio,
Miao Xiec1dc0892014-09-12 18:43:56 +08008360 file_offset);
8361 if (ret)
8362 return ret;
8363 }
8364
8365 if (bio == dip->orig_bio)
8366 return 0;
8367
8368 file_offset -= dip->logical_offset;
8369 file_offset >>= inode->i_sb->s_blocksize_bits;
8370 io_bio->csum = (u8 *)(((u32 *)orig_io_bio->csum) + file_offset);
8371
8372 return 0;
8373}
8374
David Sterbad0ee3932018-03-08 14:35:48 +01008375static inline blk_status_t btrfs_submit_dio_bio(struct bio *bio,
8376 struct inode *inode, u64 file_offset, int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00008377{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008378 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xiefacc8a222013-07-25 19:22:34 +08008379 struct btrfs_dio_private *dip = bio->bi_private;
Mike Christie37226b22016-06-05 14:31:52 -05008380 bool write = bio_op(bio) == REQ_OP_WRITE;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008381 blk_status_t ret;
Miao Xiee65e1532010-11-22 03:04:43 +00008382
Liu Bo4c274bc2017-11-01 17:19:27 -06008383 /* Check btrfs_submit_bio_hook() for rules about async submit. */
Josef Bacikb812ce22012-11-16 13:56:32 -05008384 if (async_submit)
8385 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
8386
Josef Bacik5fd02042012-05-02 14:00:54 -04008387 if (!write) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008388 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DATA);
Josef Bacik5fd02042012-05-02 14:00:54 -04008389 if (ret)
8390 goto err;
8391 }
Miao Xiee65e1532010-11-22 03:04:43 +00008392
Nikolay Borisove6961ca2017-08-03 15:44:58 +03008393 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Josef Bacik1ae39932011-04-06 14:41:34 -04008394 goto map;
8395
8396 if (write && async_submit) {
Josef Bacikc6100a42017-05-05 11:57:13 -04008397 ret = btrfs_wq_submit_bio(fs_info, bio, 0, 0,
8398 file_offset, inode,
David Sterbae288c082018-07-18 17:36:24 +02008399 btrfs_submit_bio_start_direct_io);
Miao Xiee65e1532010-11-22 03:04:43 +00008400 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04008401 } else if (write) {
8402 /*
8403 * If we aren't doing async submit, calculate the csum of the
8404 * bio now.
8405 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008406 ret = btrfs_csum_one_bio(inode, bio, file_offset, 1);
Josef Bacik1ae39932011-04-06 14:41:34 -04008407 if (ret)
8408 goto err;
Miao Xie23ea8e52014-09-12 18:43:54 +08008409 } else {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008410 ret = btrfs_lookup_and_bind_dio_csum(inode, dip, bio,
Miao Xiec1dc0892014-09-12 18:43:56 +08008411 file_offset);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00008412 if (ret)
8413 goto err;
8414 }
Josef Bacik1ae39932011-04-06 14:41:34 -04008415map:
Chris Mason08635ba2019-07-10 12:28:14 -07008416 ret = btrfs_map_bio(fs_info, bio, 0);
Miao Xiee65e1532010-11-22 03:04:43 +00008417err:
Miao Xiee65e1532010-11-22 03:04:43 +00008418 return ret;
8419}
8420
Nikolay Borisove6961ca2017-08-03 15:44:58 +03008421static int btrfs_submit_direct_hook(struct btrfs_dio_private *dip)
Miao Xiee65e1532010-11-22 03:04:43 +00008422{
8423 struct inode *inode = dip->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008424 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xiee65e1532010-11-22 03:04:43 +00008425 struct bio *bio;
8426 struct bio *orig_bio = dip->orig_bio;
Kent Overstreet4f024f32013-10-11 15:44:27 -07008427 u64 start_sector = orig_bio->bi_iter.bi_sector;
Miao Xiee65e1532010-11-22 03:04:43 +00008428 u64 file_offset = dip->logical_offset;
Josef Bacik1ae39932011-04-06 14:41:34 -04008429 int async_submit = 0;
Liu Bo725130b2017-05-16 09:51:39 -07008430 u64 submit_len;
8431 int clone_offset = 0;
8432 int clone_len;
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05308433 int ret;
Omar Sandoval58efbc92017-08-22 23:45:59 -07008434 blk_status_t status;
Nikolay Borisov89b798a2019-06-03 12:05:05 +03008435 struct btrfs_io_geometry geom;
Miao Xiee65e1532010-11-22 03:04:43 +00008436
Nikolay Borisov89b798a2019-06-03 12:05:05 +03008437 submit_len = orig_bio->bi_iter.bi_size;
8438 ret = btrfs_get_io_geometry(fs_info, btrfs_op(orig_bio),
8439 start_sector << 9, submit_len, &geom);
Miao Xie7a5c3c92014-06-17 18:58:59 +08008440 if (ret)
Miao Xiee65e1532010-11-22 03:04:43 +00008441 return -EIO;
Miao Xiefacc8a222013-07-25 19:22:34 +08008442
Nikolay Borisov89b798a2019-06-03 12:05:05 +03008443 if (geom.len >= submit_len) {
Josef Bacik02f57c72011-04-06 14:25:44 -04008444 bio = orig_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08008445 dip->flags |= BTRFS_DIO_ORIG_BIO_SUBMITTED;
Josef Bacik02f57c72011-04-06 14:25:44 -04008446 goto submit;
8447 }
8448
David Woodhouse53b381b2013-01-29 18:40:14 -05008449 /* async crcs make it difficult to collect full stripe writes. */
Jeff Mahoney1b868262017-05-17 11:38:35 -04008450 if (btrfs_data_alloc_profile(fs_info) & BTRFS_BLOCK_GROUP_RAID56_MASK)
David Woodhouse53b381b2013-01-29 18:40:14 -05008451 async_submit = 0;
8452 else
8453 async_submit = 1;
8454
Liu Bo725130b2017-05-16 09:51:39 -07008455 /* bio split */
Nikolay Borisov89b798a2019-06-03 12:05:05 +03008456 ASSERT(geom.len <= INT_MAX);
Josef Bacik02f57c72011-04-06 14:25:44 -04008457 atomic_inc(&dip->pending_bios);
Arnd Bergmann3c91ee62017-05-18 15:33:29 +02008458 do {
Nikolay Borisov89b798a2019-06-03 12:05:05 +03008459 clone_len = min_t(int, submit_len, geom.len);
Josef Bacik02f57c72011-04-06 14:25:44 -04008460
Liu Bo725130b2017-05-16 09:51:39 -07008461 /*
8462 * This will never fail as it's passing GPF_NOFS and
8463 * the allocation is backed by btrfs_bioset.
8464 */
Liu Boe4770942017-05-16 10:57:14 -07008465 bio = btrfs_bio_clone_partial(orig_bio, clone_offset,
Liu Bo725130b2017-05-16 09:51:39 -07008466 clone_len);
8467 bio->bi_private = dip;
8468 bio->bi_end_io = btrfs_end_dio_bio;
8469 btrfs_io_bio(bio)->logical = file_offset;
Miao Xiee65e1532010-11-22 03:04:43 +00008470
Liu Bo725130b2017-05-16 09:51:39 -07008471 ASSERT(submit_len >= clone_len);
8472 submit_len -= clone_len;
8473 if (submit_len == 0)
8474 break;
Miao Xiee65e1532010-11-22 03:04:43 +00008475
Liu Bo725130b2017-05-16 09:51:39 -07008476 /*
8477 * Increase the count before we submit the bio so we know
8478 * the end IO handler won't happen before we increase the
8479 * count. Otherwise, the dip might get freed before we're
8480 * done setting it up.
8481 */
8482 atomic_inc(&dip->pending_bios);
Miao Xiee65e1532010-11-22 03:04:43 +00008483
David Sterbad0ee3932018-03-08 14:35:48 +01008484 status = btrfs_submit_dio_bio(bio, inode, file_offset,
Omar Sandoval58efbc92017-08-22 23:45:59 -07008485 async_submit);
8486 if (status) {
Liu Bo725130b2017-05-16 09:51:39 -07008487 bio_put(bio);
8488 atomic_dec(&dip->pending_bios);
8489 goto out_err;
Miao Xiee65e1532010-11-22 03:04:43 +00008490 }
Liu Bo725130b2017-05-16 09:51:39 -07008491
8492 clone_offset += clone_len;
8493 start_sector += clone_len >> 9;
8494 file_offset += clone_len;
8495
Nikolay Borisov89b798a2019-06-03 12:05:05 +03008496 ret = btrfs_get_io_geometry(fs_info, btrfs_op(orig_bio),
8497 start_sector << 9, submit_len, &geom);
Liu Bo725130b2017-05-16 09:51:39 -07008498 if (ret)
8499 goto out_err;
Arnd Bergmann3c91ee62017-05-18 15:33:29 +02008500 } while (submit_len > 0);
Miao Xiee65e1532010-11-22 03:04:43 +00008501
Josef Bacik02f57c72011-04-06 14:25:44 -04008502submit:
David Sterbad0ee3932018-03-08 14:35:48 +01008503 status = btrfs_submit_dio_bio(bio, inode, file_offset, async_submit);
Omar Sandoval58efbc92017-08-22 23:45:59 -07008504 if (!status)
Miao Xiee65e1532010-11-22 03:04:43 +00008505 return 0;
8506
8507 bio_put(bio);
8508out_err:
8509 dip->errors = 1;
8510 /*
Nikolay Borisovde224b72018-02-14 10:53:36 +02008511 * Before atomic variable goto zero, we must make sure dip->errors is
8512 * perceived to be set. This ordering is ensured by the fact that an
8513 * atomic operations with a return value are fully ordered as per
8514 * atomic_t.txt
Miao Xiee65e1532010-11-22 03:04:43 +00008515 */
Miao Xiee65e1532010-11-22 03:04:43 +00008516 if (atomic_dec_and_test(&dip->pending_bios))
8517 bio_io_error(dip->orig_bio);
8518
8519 /* bio_end_io() will handle error, so we needn't return it */
8520 return 0;
8521}
8522
Mike Christie8a4c1e42016-06-05 14:31:50 -05008523static void btrfs_submit_direct(struct bio *dio_bio, struct inode *inode,
8524 loff_t file_offset)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008525{
Filipe Manana61de7182015-07-01 12:13:10 +01008526 struct btrfs_dio_private *dip = NULL;
Liu Bo3892ac92017-04-17 15:00:28 -07008527 struct bio *bio = NULL;
8528 struct btrfs_io_bio *io_bio;
Mike Christie8a4c1e42016-06-05 14:31:50 -05008529 bool write = (bio_op(dio_bio) == REQ_OP_WRITE);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008530 int ret = 0;
8531
David Sterba8b6c1d52017-06-02 17:48:13 +02008532 bio = btrfs_bio_clone(dio_bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008533
Miao Xiec1dc0892014-09-12 18:43:56 +08008534 dip = kzalloc(sizeof(*dip), GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04008535 if (!dip) {
8536 ret = -ENOMEM;
Filipe Manana61de7182015-07-01 12:13:10 +01008537 goto free_ordered;
Chris Mason9be33952013-05-17 18:30:14 -04008538 }
8539
8540 dip->private = dio_bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008541 dip->inode = inode;
8542 dip->logical_offset = file_offset;
Kent Overstreet4f024f32013-10-11 15:44:27 -07008543 dip->bytes = dio_bio->bi_iter.bi_size;
8544 dip->disk_bytenr = (u64)dio_bio->bi_iter.bi_sector << 9;
Liu Bo3892ac92017-04-17 15:00:28 -07008545 bio->bi_private = dip;
8546 dip->orig_bio = bio;
Chris Mason9be33952013-05-17 18:30:14 -04008547 dip->dio_bio = dio_bio;
Miao Xiee65e1532010-11-22 03:04:43 +00008548 atomic_set(&dip->pending_bios, 0);
Liu Bo3892ac92017-04-17 15:00:28 -07008549 io_bio = btrfs_io_bio(bio);
8550 io_bio->logical = file_offset;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008551
Miao Xiec1dc0892014-09-12 18:43:56 +08008552 if (write) {
Liu Bo3892ac92017-04-17 15:00:28 -07008553 bio->bi_end_io = btrfs_endio_direct_write;
Miao Xiec1dc0892014-09-12 18:43:56 +08008554 } else {
Liu Bo3892ac92017-04-17 15:00:28 -07008555 bio->bi_end_io = btrfs_endio_direct_read;
Miao Xiec1dc0892014-09-12 18:43:56 +08008556 dip->subio_endio = btrfs_subio_endio_read;
8557 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04008558
Filipe Mananaf28a4922015-12-08 19:23:20 +00008559 /*
8560 * Reset the range for unsubmitted ordered extents (to a 0 length range)
8561 * even if we fail to submit a bio, because in such case we do the
8562 * corresponding error handling below and it must not be done a second
8563 * time by btrfs_direct_IO().
8564 */
8565 if (write) {
8566 struct btrfs_dio_data *dio_data = current->journal_info;
8567
8568 dio_data->unsubmitted_oe_range_end = dip->logical_offset +
8569 dip->bytes;
8570 dio_data->unsubmitted_oe_range_start =
8571 dio_data->unsubmitted_oe_range_end;
8572 }
8573
Nikolay Borisove6961ca2017-08-03 15:44:58 +03008574 ret = btrfs_submit_direct_hook(dip);
Miao Xiee65e1532010-11-22 03:04:43 +00008575 if (!ret)
Chris Masoneaf25d92010-05-25 09:48:28 -04008576 return;
Chris Mason9be33952013-05-17 18:30:14 -04008577
David Sterbab3a0dd52018-11-22 17:16:49 +01008578 btrfs_io_bio_free_csum(io_bio);
Chris Mason9be33952013-05-17 18:30:14 -04008579
Josef Bacik4b46fce2010-05-23 11:00:55 -04008580free_ordered:
8581 /*
Filipe Manana61de7182015-07-01 12:13:10 +01008582 * If we arrived here it means either we failed to submit the dip
8583 * or we either failed to clone the dio_bio or failed to allocate the
8584 * dip. If we cloned the dio_bio and allocated the dip, we can just
8585 * call bio_endio against our io_bio so that we get proper resource
8586 * cleanup if we fail to submit the dip, otherwise, we must do the
8587 * same as btrfs_endio_direct_[write|read] because we can't call these
8588 * callbacks - they require an allocated dip and a clone of dio_bio.
Josef Bacik4b46fce2010-05-23 11:00:55 -04008589 */
Liu Bo3892ac92017-04-17 15:00:28 -07008590 if (bio && dip) {
Guoqing Jiang054ec2f2017-06-02 16:08:50 +08008591 bio_io_error(bio);
Filipe Manana61de7182015-07-01 12:13:10 +01008592 /*
Liu Bo3892ac92017-04-17 15:00:28 -07008593 * The end io callbacks free our dip, do the final put on bio
Filipe Manana61de7182015-07-01 12:13:10 +01008594 * and all the cleanup and final put for dio_bio (through
8595 * dio_end_io()).
8596 */
8597 dip = NULL;
Liu Bo3892ac92017-04-17 15:00:28 -07008598 bio = NULL;
Filipe Manana61de7182015-07-01 12:13:10 +01008599 } else {
Filipe Manana14543772015-11-24 16:23:54 +00008600 if (write)
Qu Wenruo524272602017-03-08 10:25:52 +08008601 __endio_write_update_ordered(inode,
Filipe Manana14543772015-11-24 16:23:54 +00008602 file_offset,
8603 dio_bio->bi_iter.bi_size,
Qu Wenruo524272602017-03-08 10:25:52 +08008604 false);
Filipe Manana14543772015-11-24 16:23:54 +00008605 else
Filipe Manana61de7182015-07-01 12:13:10 +01008606 unlock_extent(&BTRFS_I(inode)->io_tree, file_offset,
8607 file_offset + dio_bio->bi_iter.bi_size - 1);
Filipe Manana14543772015-11-24 16:23:54 +00008608
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008609 dio_bio->bi_status = BLK_STS_IOERR;
Filipe Manana61de7182015-07-01 12:13:10 +01008610 /*
8611 * Releases and cleans up our dio_bio, no need to bio_put()
8612 * nor bio_endio()/bio_io_error() against dio_bio.
8613 */
Christoph Hellwig40553512017-06-03 09:37:58 +02008614 dio_end_io(dio_bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008615 }
Liu Bo3892ac92017-04-17 15:00:28 -07008616 if (bio)
8617 bio_put(bio);
Filipe Manana61de7182015-07-01 12:13:10 +01008618 kfree(dip);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008619}
8620
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008621static ssize_t check_direct_IO(struct btrfs_fs_info *fs_info,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008622 const struct iov_iter *iter, loff_t offset)
Chris Mason5a5f79b2010-05-26 21:33:37 -04008623{
8624 int seg;
Josef Bacika1b75f72011-04-08 15:51:18 +00008625 int i;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008626 unsigned int blocksize_mask = fs_info->sectorsize - 1;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008627 ssize_t retval = -EINVAL;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008628
8629 if (offset & blocksize_mask)
8630 goto out;
8631
Al Viro28060d52014-03-22 05:15:17 -04008632 if (iov_iter_alignment(iter) & blocksize_mask)
8633 goto out;
Josef Bacika1b75f72011-04-08 15:51:18 +00008634
Al Viro28060d52014-03-22 05:15:17 -04008635 /* If this is a write we don't need to check anymore */
Al Virocd27e452016-10-10 13:39:05 -04008636 if (iov_iter_rw(iter) != READ || !iter_is_iovec(iter))
Al Viro28060d52014-03-22 05:15:17 -04008637 return 0;
8638 /*
8639 * Check to make sure we don't have duplicate iov_base's in this
8640 * iovec, if so return EINVAL, otherwise we'll get csum errors
8641 * when reading back.
8642 */
8643 for (seg = 0; seg < iter->nr_segs; seg++) {
8644 for (i = seg + 1; i < iter->nr_segs; i++) {
8645 if (iter->iov[seg].iov_base == iter->iov[i].iov_base)
Josef Bacika1b75f72011-04-08 15:51:18 +00008646 goto out;
8647 }
Chris Mason5a5f79b2010-05-26 21:33:37 -04008648 }
8649 retval = 0;
8650out:
8651 return retval;
8652}
Josef Bacikeb838e72012-07-31 16:28:48 -04008653
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008654static ssize_t btrfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
Chris Mason16432982008-04-10 10:23:21 -04008655{
Josef Bacik4b46fce2010-05-23 11:00:55 -04008656 struct file *file = iocb->ki_filp;
8657 struct inode *inode = file->f_mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008658 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
chandan50745b02015-08-28 21:10:13 +05308659 struct btrfs_dio_data dio_data = { 0 };
Qu Wenruo364ecf32017-02-27 15:10:38 +08008660 struct extent_changeset *data_reserved = NULL;
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008661 loff_t offset = iocb->ki_pos;
Miao Xie09348562013-02-07 10:12:07 +00008662 size_t count = 0;
Miao Xie2e60a512013-02-08 07:01:08 +00008663 int flags = 0;
Miao Xie38851cc2013-02-08 07:04:11 +00008664 bool wakeup = true;
8665 bool relock = false;
Miao Xie09348562013-02-07 10:12:07 +00008666 ssize_t ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008667
Nikolay Borisov8c70c9f2017-08-21 12:43:46 +03008668 if (check_direct_IO(fs_info, iter, offset))
Chris Mason5a5f79b2010-05-26 21:33:37 -04008669 return 0;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008670
Jens Axboefe0f07d2015-04-15 17:05:48 -06008671 inode_dio_begin(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008672
Josef Bacik0e267c42013-07-02 10:38:02 -04008673 /*
Miao Xie41bd9ca2014-03-06 13:54:57 +08008674 * The generic stuff only does filemap_write_and_wait_range, which
8675 * isn't enough if we've written compressed pages to this area, so
8676 * we need to flush the dirty pages again to make absolutely sure
8677 * that any outstanding dirty pages are on disk.
Josef Bacik0e267c42013-07-02 10:38:02 -04008678 */
Al Viroa6cbcd42014-03-04 22:38:00 -05008679 count = iov_iter_count(iter);
Miao Xie41bd9ca2014-03-06 13:54:57 +08008680 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
8681 &BTRFS_I(inode)->runtime_flags))
Wang Shilong9a025a02014-07-17 11:44:13 +08008682 filemap_fdatawrite_range(inode->i_mapping, offset,
8683 offset + count - 1);
Josef Bacik0e267c42013-07-02 10:38:02 -04008684
Omar Sandoval6f673762015-03-16 04:33:52 -07008685 if (iov_iter_rw(iter) == WRITE) {
Miao Xie38851cc2013-02-08 07:04:11 +00008686 /*
8687 * If the write DIO is beyond the EOF, we need update
8688 * the isize, but it is protected by i_mutex. So we can
8689 * not unlock the i_mutex at this case.
8690 */
8691 if (offset + count <= inode->i_size) {
Liu Bo4aaedfb2016-12-14 22:36:05 -08008692 dio_data.overwrite = 1;
Al Viro59551022016-01-22 15:40:57 -05008693 inode_unlock(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008694 relock = true;
Goldwyn Rodriguesedf064e2017-06-20 07:05:49 -05008695 } else if (iocb->ki_flags & IOCB_NOWAIT) {
8696 ret = -EAGAIN;
8697 goto out;
Miao Xie38851cc2013-02-08 07:04:11 +00008698 }
Qu Wenruo364ecf32017-02-27 15:10:38 +08008699 ret = btrfs_delalloc_reserve_space(inode, &data_reserved,
8700 offset, count);
Miao Xie09348562013-02-07 10:12:07 +00008701 if (ret)
Miao Xie38851cc2013-02-08 07:04:11 +00008702 goto out;
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008703
8704 /*
8705 * We need to know how many extents we reserved so that we can
8706 * do the accounting properly if we go over the number we
8707 * originally calculated. Abuse current->journal_info for this.
8708 */
Jeff Mahoneyda170662016-06-15 09:22:56 -04008709 dio_data.reserve = round_up(count,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008710 fs_info->sectorsize);
Filipe Mananaf28a4922015-12-08 19:23:20 +00008711 dio_data.unsubmitted_oe_range_start = (u64)offset;
8712 dio_data.unsubmitted_oe_range_end = (u64)offset;
chandan50745b02015-08-28 21:10:13 +05308713 current->journal_info = &dio_data;
Chandan Rajendra97dcdea2016-12-23 15:00:18 +05308714 down_read(&BTRFS_I(inode)->dio_sem);
David Sterbaee39b432014-09-30 01:33:33 +02008715 } else if (test_bit(BTRFS_INODE_READDIO_NEED_LOCK,
8716 &BTRFS_I(inode)->runtime_flags)) {
Jens Axboefe0f07d2015-04-15 17:05:48 -06008717 inode_dio_end(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008718 flags = DIO_LOCKING | DIO_SKIP_HOLES;
8719 wakeup = false;
Miao Xie09348562013-02-07 10:12:07 +00008720 }
8721
Omar Sandoval17f8c842015-03-16 04:33:50 -07008722 ret = __blockdev_direct_IO(iocb, inode,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008723 fs_info->fs_devices->latest_bdev,
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008724 iter, btrfs_get_blocks_direct, NULL,
Omar Sandoval17f8c842015-03-16 04:33:50 -07008725 btrfs_submit_direct, flags);
Omar Sandoval6f673762015-03-16 04:33:52 -07008726 if (iov_iter_rw(iter) == WRITE) {
Chandan Rajendra97dcdea2016-12-23 15:00:18 +05308727 up_read(&BTRFS_I(inode)->dio_sem);
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008728 current->journal_info = NULL;
Liu Boddba1bf2015-06-17 16:59:58 +08008729 if (ret < 0 && ret != -EIOCBQUEUED) {
chandan50745b02015-08-28 21:10:13 +05308730 if (dio_data.reserve)
Qu Wenruobc42bda2017-02-27 15:10:39 +08008731 btrfs_delalloc_release_space(inode, data_reserved,
Qu Wenruo43b18592017-12-12 15:34:32 +08008732 offset, dio_data.reserve, true);
Filipe Mananaf28a4922015-12-08 19:23:20 +00008733 /*
8734 * On error we might have left some ordered extents
8735 * without submitting corresponding bios for them, so
8736 * cleanup them up to avoid other tasks getting them
8737 * and waiting for them to complete forever.
8738 */
8739 if (dio_data.unsubmitted_oe_range_start <
8740 dio_data.unsubmitted_oe_range_end)
Qu Wenruo524272602017-03-08 10:25:52 +08008741 __endio_write_update_ordered(inode,
Filipe Mananaf28a4922015-12-08 19:23:20 +00008742 dio_data.unsubmitted_oe_range_start,
8743 dio_data.unsubmitted_oe_range_end -
8744 dio_data.unsubmitted_oe_range_start,
Qu Wenruo524272602017-03-08 10:25:52 +08008745 false);
Liu Boddba1bf2015-06-17 16:59:58 +08008746 } else if (ret >= 0 && (size_t)ret < count)
Qu Wenruobc42bda2017-02-27 15:10:39 +08008747 btrfs_delalloc_release_space(inode, data_reserved,
Qu Wenruo43b18592017-12-12 15:34:32 +08008748 offset, count - (size_t)ret, true);
Qu Wenruo8702ba92019-10-14 14:34:51 +08008749 btrfs_delalloc_release_extents(BTRFS_I(inode), count);
Miao Xie09348562013-02-07 10:12:07 +00008750 }
Miao Xie38851cc2013-02-08 07:04:11 +00008751out:
Miao Xie2e60a512013-02-08 07:01:08 +00008752 if (wakeup)
Jens Axboefe0f07d2015-04-15 17:05:48 -06008753 inode_dio_end(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008754 if (relock)
Al Viro59551022016-01-22 15:40:57 -05008755 inode_lock(inode);
Miao Xie09348562013-02-07 10:12:07 +00008756
Qu Wenruo364ecf32017-02-27 15:10:38 +08008757 extent_changeset_free(data_reserved);
Miao Xie09348562013-02-07 10:12:07 +00008758 return ret;
Chris Mason16432982008-04-10 10:23:21 -04008759}
8760
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008761#define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
8762
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008763static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
8764 __u64 start, __u64 len)
8765{
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008766 int ret;
8767
8768 ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS);
8769 if (ret)
8770 return ret;
8771
David Sterba2135fb92017-06-23 04:09:57 +02008772 return extent_fiemap(inode, fieinfo, start, len);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008773}
8774
Chris Mason9ebefb182007-06-15 13:50:00 -04008775int btrfs_readpage(struct file *file, struct page *page)
8776{
Chris Masond1310b22008-01-24 16:13:08 -05008777 struct extent_io_tree *tree;
8778 tree = &BTRFS_I(page->mapping->host)->io_tree;
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02008779 return extent_read_full_page(tree, page, btrfs_get_extent, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04008780}
Chris Mason1832a6d2007-12-21 16:27:21 -05008781
Chris Mason39279cc2007-06-12 06:35:45 -04008782static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
8783{
Josef Bacikbe7bd732015-10-22 15:05:09 -04008784 struct inode *inode = page->mapping->host;
8785 int ret;
Chris Masonb888db22007-08-27 16:49:44 -04008786
8787 if (current->flags & PF_MEMALLOC) {
8788 redirty_page_for_writepage(wbc, page);
8789 unlock_page(page);
8790 return 0;
8791 }
Josef Bacikbe7bd732015-10-22 15:05:09 -04008792
8793 /*
8794 * If we are under memory pressure we will call this directly from the
8795 * VM, we need to make sure we have the inode referenced for the ordered
8796 * extent. If not just return like we didn't do anything.
8797 */
8798 if (!igrab(inode)) {
8799 redirty_page_for_writepage(wbc, page);
8800 return AOP_WRITEPAGE_ACTIVATE;
8801 }
Nikolay Borisov0a9b0e52017-12-08 15:55:59 +02008802 ret = extent_write_full_page(page, wbc);
Josef Bacikbe7bd732015-10-22 15:05:09 -04008803 btrfs_add_delayed_iput(inode);
8804 return ret;
Chris Masona52d9a82007-08-27 16:49:44 -04008805}
Chris Mason39279cc2007-06-12 06:35:45 -04008806
Eric Sandeen48a3b632013-04-25 20:41:01 +00008807static int btrfs_writepages(struct address_space *mapping,
8808 struct writeback_control *wbc)
Chris Masonb293f02e2007-11-01 19:45:34 -04008809{
Nikolay Borisov8ae225a2018-04-19 10:46:38 +03008810 return extent_writepages(mapping, wbc);
Chris Masonb293f02e2007-11-01 19:45:34 -04008811}
8812
Chris Mason3ab2fb52007-11-08 10:59:22 -05008813static int
8814btrfs_readpages(struct file *file, struct address_space *mapping,
8815 struct list_head *pages, unsigned nr_pages)
8816{
Nikolay Borisov2a3ff0a2018-04-19 10:46:36 +03008817 return extent_readpages(mapping, pages, nr_pages);
Chris Mason3ab2fb52007-11-08 10:59:22 -05008818}
Nikolay Borisov2a3ff0a2018-04-19 10:46:36 +03008819
Chris Masone6dcd2d2008-07-17 12:53:50 -04008820static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04008821{
Nikolay Borisov477a30b2018-04-19 10:46:34 +03008822 int ret = try_release_extent_mapping(page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04008823 if (ret == 1) {
8824 ClearPagePrivate(page);
8825 set_page_private(page, 0);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008826 put_page(page);
Chris Masona52d9a82007-08-27 16:49:44 -04008827 }
8828 return ret;
8829}
Chris Mason39279cc2007-06-12 06:35:45 -04008830
Chris Masone6dcd2d2008-07-17 12:53:50 -04008831static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
8832{
Chris Mason98509cf2008-09-11 15:51:43 -04008833 if (PageWriteback(page) || PageDirty(page))
8834 return 0;
Michal Hocko3ba7ab22017-01-09 15:39:02 +01008835 return __btrfs_releasepage(page, gfp_flags);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008836}
8837
Lukas Czernerd47992f2013-05-21 23:17:23 -04008838static void btrfs_invalidatepage(struct page *page, unsigned int offset,
8839 unsigned int length)
Chris Masona52d9a82007-08-27 16:49:44 -04008840{
Josef Bacik5fd02042012-05-02 14:00:54 -04008841 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05008842 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008843 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008844 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008845 u64 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008846 u64 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308847 u64 start;
8848 u64 end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008849 int inode_evicting = inode->i_state & I_FREEING;
Chris Masona52d9a82007-08-27 16:49:44 -04008850
Chris Mason8b62b722009-09-02 16:53:46 -04008851 /*
8852 * we have the page locked, so new writeback can't start,
8853 * and the dirty bit won't be cleared while we are here.
8854 *
8855 * Wait for IO on this page so that we can safely clear
8856 * the PagePrivate2 bit and do ordered accounting
8857 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008858 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04008859
Josef Bacik5fd02042012-05-02 14:00:54 -04008860 tree = &BTRFS_I(inode)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008861 if (offset) {
8862 btrfs_releasepage(page, GFP_NOFS);
8863 return;
8864 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008865
8866 if (!inode_evicting)
David Sterbaff13db42015-12-03 14:30:40 +01008867 lock_extent_bits(tree, page_start, page_end, &cached_state);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308868again:
8869 start = page_start;
Nikolay Borisova776c6f2017-02-20 13:50:49 +02008870 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), start,
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308871 page_end - start + 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008872 if (ordered) {
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308873 end = min(page_end, ordered->file_offset + ordered->len - 1);
Chris Masoneb84ae02008-07-17 13:53:27 -04008874 /*
8875 * IO on this page will never be started, so we need
8876 * to account for any ordered extents now
8877 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008878 if (!inode_evicting)
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308879 clear_extent_bit(tree, start, end,
Omar Sandovale1821632019-08-15 14:04:04 -07008880 EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008881 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
David Sterbaae0f1622017-10-31 16:37:52 +01008882 EXTENT_DEFRAG, 1, 0, &cached_state);
Chris Mason8b62b722009-09-02 16:53:46 -04008883 /*
8884 * whoever cleared the private bit is responsible
8885 * for the finish_ordered_io
8886 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04008887 if (TestClearPagePrivate2(page)) {
8888 struct btrfs_ordered_inode_tree *tree;
8889 u64 new_len;
8890
8891 tree = &BTRFS_I(inode)->ordered_tree;
8892
8893 spin_lock_irq(&tree->lock);
8894 set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308895 new_len = start - ordered->file_offset;
Josef Bacik77cef2e2013-08-29 13:57:21 -04008896 if (new_len < ordered->truncated_len)
8897 ordered->truncated_len = new_len;
8898 spin_unlock_irq(&tree->lock);
8899
8900 if (btrfs_dec_test_ordered_pending(inode, &ordered,
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308901 start,
8902 end - start + 1, 1))
Josef Bacik77cef2e2013-08-29 13:57:21 -04008903 btrfs_finish_ordered_io(ordered);
Chris Mason8b62b722009-09-02 16:53:46 -04008904 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008905 btrfs_put_ordered_extent(ordered);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008906 if (!inode_evicting) {
8907 cached_state = NULL;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308908 lock_extent_bits(tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008909 &cached_state);
8910 }
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308911
8912 start = end + 1;
8913 if (start < page_end)
8914 goto again;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008915 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008916
Qu Wenruob9d0b382015-09-29 10:35:16 +08008917 /*
8918 * Qgroup reserved space handler
8919 * Page here will be either
8920 * 1) Already written to disk
8921 * In this case, its reserved space is released from data rsv map
8922 * and will be freed by delayed_ref handler finally.
8923 * So even we call qgroup_free_data(), it won't decrease reserved
8924 * space.
8925 * 2) Not written to disk
Goldwyn Rodrigues0b34c262016-09-30 10:40:52 -05008926 * This means the reserved space should be freed here. However,
8927 * if a truncate invalidates the page (by clearing PageDirty)
8928 * and the page is accounted for while allocating extent
8929 * in btrfs_check_data_free_space() we let delayed_ref to
8930 * free the entire extent.
Qu Wenruob9d0b382015-09-29 10:35:16 +08008931 */
Goldwyn Rodrigues0b34c262016-09-30 10:40:52 -05008932 if (PageDirty(page))
Qu Wenruobc42bda2017-02-27 15:10:39 +08008933 btrfs_qgroup_free_data(inode, NULL, page_start, PAGE_SIZE);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008934 if (!inode_evicting) {
Omar Sandovale1821632019-08-15 14:04:04 -07008935 clear_extent_bit(tree, page_start, page_end, EXTENT_LOCKED |
Filipe Mananaa7e3b972017-04-03 10:45:46 +01008936 EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
8937 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1,
David Sterbaae0f1622017-10-31 16:37:52 +01008938 &cached_state);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008939
8940 __btrfs_releasepage(page, GFP_NOFS);
8941 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008942
Chris Mason4a096752008-07-21 10:29:44 -04008943 ClearPageChecked(page);
Chris Mason9ad6b7bc2008-04-18 16:11:30 -04008944 if (PagePrivate(page)) {
Chris Mason9ad6b7bc2008-04-18 16:11:30 -04008945 ClearPagePrivate(page);
8946 set_page_private(page, 0);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008947 put_page(page);
Chris Mason9ad6b7bc2008-04-18 16:11:30 -04008948 }
Chris Mason39279cc2007-06-12 06:35:45 -04008949}
8950
Chris Mason9ebefb182007-06-15 13:50:00 -04008951/*
8952 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
8953 * called from a page fault handler when a page is first dirtied. Hence we must
8954 * be careful to check for EOF conditions here. We set the page up correctly
8955 * for a written page which means we get ENOSPC checking when writing into
8956 * holes and correct delalloc and unwritten extent mapping on filesystems that
8957 * support these features.
8958 *
8959 * We are not allowed to take the i_mutex here so we have to play games to
8960 * protect against truncate races as the page could now be beyond EOF. Because
Omar Sandovald1342aa2018-05-11 13:13:29 -07008961 * truncate_setsize() writes the inode size before removing pages, once we have
8962 * the page lock we can determine safely if the page is beyond EOF. If it is not
Chris Mason9ebefb182007-06-15 13:50:00 -04008963 * beyond EOF, then the page is guaranteed safe against truncation until we
8964 * unlock the page.
8965 */
Souptick Joardera528a242018-06-06 19:54:44 +05308966vm_fault_t btrfs_page_mkwrite(struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04008967{
Nick Pigginc2ec1752009-03-31 15:23:21 -07008968 struct page *page = vmf->page;
Dave Jiang11bac802017-02-24 14:56:41 -08008969 struct inode *inode = file_inode(vmf->vma->vm_file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008970 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008971 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
8972 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008973 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08008974 struct extent_changeset *data_reserved = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008975 char *kaddr;
8976 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04008977 loff_t size;
Souptick Joardera528a242018-06-06 19:54:44 +05308978 vm_fault_t ret;
8979 int ret2;
Chris Mason9998eb72012-01-25 13:47:40 -05008980 int reserved = 0;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308981 u64 reserved_space;
Chris Masona52d9a82007-08-27 16:49:44 -04008982 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008983 u64 page_end;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308984 u64 end;
8985
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008986 reserved_space = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04008987
Jan Karab2b5ef52012-06-12 16:20:45 +02008988 sb_start_pagefault(inode->i_sb);
Qu Wenruodf480632015-09-08 17:25:54 +08008989 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008990 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308991 end = page_end;
Qu Wenruodf480632015-09-08 17:25:54 +08008992
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308993 /*
8994 * Reserving delalloc space after obtaining the page lock can lead to
8995 * deadlock. For example, if a dirty page is locked by this function
8996 * and the call to btrfs_delalloc_reserve_space() ends up triggering
8997 * dirty page write out, then the btrfs_writepage() function could
8998 * end up waiting indefinitely to get a lock on the page currently
8999 * being processed by btrfs_page_mkwrite() function.
9000 */
Souptick Joardera528a242018-06-06 19:54:44 +05309001 ret2 = btrfs_delalloc_reserve_space(inode, &data_reserved, page_start,
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309002 reserved_space);
Souptick Joardera528a242018-06-06 19:54:44 +05309003 if (!ret2) {
9004 ret2 = file_update_time(vmf->vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05009005 reserved = 1;
9006 }
Souptick Joardera528a242018-06-06 19:54:44 +05309007 if (ret2) {
9008 ret = vmf_error(ret2);
Chris Mason9998eb72012-01-25 13:47:40 -05009009 if (reserved)
9010 goto out;
9011 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07009012 }
Chris Mason1832a6d2007-12-21 16:27:21 -05009013
Nick Piggin56a76f82009-03-31 15:23:23 -07009014 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04009015again:
Chris Mason9ebefb182007-06-15 13:50:00 -04009016 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04009017 size = i_size_read(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04009018
Chris Mason9ebefb182007-06-15 13:50:00 -04009019 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04009020 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04009021 /* page got truncated out from underneath us */
9022 goto out_unlock;
9023 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04009024 wait_on_page_writeback(page);
9025
David Sterbaff13db42015-12-03 14:30:40 +01009026 lock_extent_bits(io_tree, page_start, page_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009027 set_page_extent_mapped(page);
9028
Chris Masoneb84ae02008-07-17 13:53:27 -04009029 /*
9030 * we can't set the delalloc bits if there are pending ordered
9031 * extents. Drop our locks and wait for them to finish
9032 */
Nikolay Borisova776c6f2017-02-20 13:50:49 +02009033 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start,
9034 PAGE_SIZE);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009035 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00009036 unlock_extent_cached(io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01009037 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009038 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04009039 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009040 btrfs_put_ordered_extent(ordered);
9041 goto again;
9042 }
9043
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009044 if (page->index == ((size - 1) >> PAGE_SHIFT)) {
Jeff Mahoneyda170662016-06-15 09:22:56 -04009045 reserved_space = round_up(size - page_start,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009046 fs_info->sectorsize);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009047 if (reserved_space < PAGE_SIZE) {
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309048 end = page_start + reserved_space - 1;
Qu Wenruobc42bda2017-02-27 15:10:39 +08009049 btrfs_delalloc_release_space(inode, data_reserved,
Qu Wenruo43b18592017-12-12 15:34:32 +08009050 page_start, PAGE_SIZE - reserved_space,
9051 true);
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309052 }
9053 }
9054
Josef Bacikfbf19082009-10-01 17:10:23 -04009055 /*
Liu Bo54160342016-12-13 12:15:19 -08009056 * page_mkwrite gets called when the page is firstly dirtied after it's
9057 * faulted in, but write(2) could also dirty a page and set delalloc
9058 * bits, thus in this case for space account reason, we still need to
9059 * clear any delalloc bits within this page range since we have to
9060 * reserve data&meta space before lock_page() (see above comments).
Josef Bacikfbf19082009-10-01 17:10:23 -04009061 */
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309062 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, end,
Omar Sandovale1821632019-08-15 14:04:04 -07009063 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
9064 EXTENT_DEFRAG, 0, 0, &cached_state);
Josef Bacikfbf19082009-10-01 17:10:23 -04009065
Souptick Joardera528a242018-06-06 19:54:44 +05309066 ret2 = btrfs_set_extent_delalloc(inode, page_start, end, 0,
Nikolay Borisov330a5822019-07-17 16:18:17 +03009067 &cached_state);
Souptick Joardera528a242018-06-06 19:54:44 +05309068 if (ret2) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00009069 unlock_extent_cached(io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01009070 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009071 ret = VM_FAULT_SIGBUS;
9072 goto out_unlock;
9073 }
Souptick Joardera528a242018-06-06 19:54:44 +05309074 ret2 = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04009075
9076 /* page is wholly or partially inside EOF */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009077 if (page_start + PAGE_SIZE > size)
Johannes Thumshirn70730172018-12-05 15:23:03 +01009078 zero_start = offset_in_page(size);
Chris Mason9ebefb182007-06-15 13:50:00 -04009079 else
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009080 zero_start = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04009081
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009082 if (zero_start != PAGE_SIZE) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04009083 kaddr = kmap(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009084 memset(kaddr + zero_start, 0, PAGE_SIZE - zero_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009085 flush_dcache_page(page);
9086 kunmap(page);
9087 }
Chris Mason247e7432008-07-17 12:53:51 -04009088 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009089 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04009090 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04009091
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009092 BTRFS_I(inode)->last_trans = fs_info->generation;
Chris Mason257c62e2009-10-13 13:21:08 -04009093 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06009094 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
Chris Mason257c62e2009-10-13 13:21:08 -04009095
David Sterbae43bbe52017-12-12 21:43:52 +01009096 unlock_extent_cached(io_tree, page_start, page_end, &cached_state);
Chris Mason9ebefb182007-06-15 13:50:00 -04009097
Souptick Joardera528a242018-06-06 19:54:44 +05309098 if (!ret2) {
Qu Wenruo8702ba92019-10-14 14:34:51 +08009099 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE);
Jan Karab2b5ef52012-06-12 16:20:45 +02009100 sb_end_pagefault(inode->i_sb);
Qu Wenruo364ecf32017-02-27 15:10:38 +08009101 extent_changeset_free(data_reserved);
Chris Mason50a9b212009-09-11 12:33:12 -04009102 return VM_FAULT_LOCKED;
Jan Karab2b5ef52012-06-12 16:20:45 +02009103 }
Chris Mason717beb92018-06-25 10:03:41 -07009104
9105out_unlock:
Chris Mason9ebefb182007-06-15 13:50:00 -04009106 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05009107out:
Qu Wenruo8702ba92019-10-14 14:34:51 +08009108 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE);
Qu Wenruobc42bda2017-02-27 15:10:39 +08009109 btrfs_delalloc_release_space(inode, data_reserved, page_start,
Qu Wenruo43b18592017-12-12 15:34:32 +08009110 reserved_space, (ret != 0));
Chris Mason9998eb72012-01-25 13:47:40 -05009111out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02009112 sb_end_pagefault(inode->i_sb);
Qu Wenruo364ecf32017-02-27 15:10:38 +08009113 extent_changeset_free(data_reserved);
Chris Mason9ebefb182007-06-15 13:50:00 -04009114 return ret;
9115}
9116
Filipe Manana213e8c52018-02-06 20:40:31 +00009117static int btrfs_truncate(struct inode *inode, bool skip_writeback)
Chris Mason39279cc2007-06-12 06:35:45 -04009118{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009119 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009120 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04009121 struct btrfs_block_rsv *rsv;
Omar Sandovalad7e1a72018-05-22 09:59:50 -07009122 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04009123 struct btrfs_trans_handle *trans;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009124 u64 mask = fs_info->sectorsize - 1;
Josef Bacik2bd36e72019-08-22 15:14:33 -04009125 u64 min_size = btrfs_calc_metadata_size(fs_info, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04009126
Filipe Manana213e8c52018-02-06 20:40:31 +00009127 if (!skip_writeback) {
9128 ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask),
9129 (u64)-1);
9130 if (ret)
9131 return ret;
9132 }
Chris Mason39279cc2007-06-12 06:35:45 -04009133
Josef Bacikfcb80c22011-05-03 10:40:22 -04009134 /*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07009135 * Yes ladies and gentlemen, this is indeed ugly. We have a couple of
9136 * things going on here:
Josef Bacikfcb80c22011-05-03 10:40:22 -04009137 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07009138 * 1) We need to reserve space to update our inode.
Josef Bacikfcb80c22011-05-03 10:40:22 -04009139 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07009140 * 2) We need to have something to cache all the space that is going to
Josef Bacikfcb80c22011-05-03 10:40:22 -04009141 * be free'd up by the truncate operation, but also have some slack
9142 * space reserved in case it uses space during the truncate (thank you
9143 * very much snapshotting).
9144 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07009145 * And we need these to be separate. The fact is we can use a lot of
Josef Bacikfcb80c22011-05-03 10:40:22 -04009146 * space doing the truncate, and we have no earthly idea how much space
Nicholas D Steeves01327612016-05-19 21:18:45 -04009147 * we will use, so we need the truncate reservation to be separate so it
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07009148 * doesn't end up using space reserved for updating the inode. We also
9149 * need to be able to stop the transaction and start a new one, which
9150 * means we need to be able to update the inode several times, and we
9151 * have no idea of knowing how many times that will be, so we can't just
9152 * reserve 1 item for the entirety of the operation, so that has to be
9153 * done separately as well.
Josef Bacikfcb80c22011-05-03 10:40:22 -04009154 *
9155 * So that leaves us with
9156 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07009157 * 1) rsv - for the truncate reservation, which we will steal from the
Josef Bacikfcb80c22011-05-03 10:40:22 -04009158 * transaction reservation.
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07009159 * 2) fs_info->trans_block_rsv - this will have 1 items worth left for
Josef Bacikfcb80c22011-05-03 10:40:22 -04009160 * updating the inode.
9161 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009162 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009163 if (!rsv)
9164 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04009165 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04009166 rsv->failfast = 1;
Josef Bacikf0cd8462011-03-04 14:37:08 -05009167
Josef Bacik907cbce2011-08-08 13:46:15 -04009168 /*
Josef Bacik07127182011-08-19 10:29:59 -04009169 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04009170 * 1 for updating the inode.
9171 */
Josef Bacikf3fe8202013-01-07 17:03:21 -05009172 trans = btrfs_start_transaction(root, 2);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009173 if (IS_ERR(trans)) {
Omar Sandovalad7e1a72018-05-22 09:59:50 -07009174 ret = PTR_ERR(trans);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009175 goto out;
9176 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05009177
Josef Bacik907cbce2011-08-08 13:46:15 -04009178 /* Migrate the slack space for the truncate to our reserve */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009179 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, rsv,
Lu Fengqi3a584172018-08-04 21:10:55 +08009180 min_size, false);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009181 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05009182
Chris Mason5a3f23d2009-03-31 13:27:11 -04009183 /*
Josef Bacik5dc562c2012-08-17 13:14:17 -04009184 * So if we truncate and then write and fsync we normally would just
9185 * write the extents that changed, which is a problem if we need to
9186 * first truncate that entire inode. So set this flag so we write out
9187 * all of the extents in the inode to the sync log so we're completely
9188 * safe.
9189 */
9190 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009191 trans->block_rsv = rsv;
Josef Bacik5dc562c2012-08-17 13:14:17 -04009192
Yan, Zheng80825102009-11-12 09:35:36 +00009193 while (1) {
9194 ret = btrfs_truncate_inode_items(trans, root, inode,
9195 inode->i_size,
9196 BTRFS_EXTENT_DATA_KEY);
Josef Bacikddfae632017-10-19 14:16:02 -04009197 trans->block_rsv = &fs_info->trans_block_rsv;
Omar Sandovalad7e1a72018-05-22 09:59:50 -07009198 if (ret != -ENOSPC && ret != -EAGAIN)
Yan, Zheng80825102009-11-12 09:35:36 +00009199 break;
Chris Mason39279cc2007-06-12 06:35:45 -04009200
Yan, Zheng80825102009-11-12 09:35:36 +00009201 ret = btrfs_update_inode(trans, root, inode);
Omar Sandovalad7e1a72018-05-22 09:59:50 -07009202 if (ret)
Josef Bacik3893e332011-01-31 16:03:11 -05009203 break;
Josef Bacikca7e70f2012-08-27 17:48:15 -04009204
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009205 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009206 btrfs_btree_balance_dirty(fs_info);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009207
9208 trans = btrfs_start_transaction(root, 2);
9209 if (IS_ERR(trans)) {
Omar Sandovalad7e1a72018-05-22 09:59:50 -07009210 ret = PTR_ERR(trans);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009211 trans = NULL;
9212 break;
9213 }
9214
Wang Xiaoguang47b5d642016-09-07 20:17:38 +08009215 btrfs_block_rsv_release(fs_info, rsv, -1);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009216 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv,
Lu Fengqi3a584172018-08-04 21:10:55 +08009217 rsv, min_size, false);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009218 BUG_ON(ret); /* shouldn't happen */
9219 trans->block_rsv = rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00009220 }
9221
Josef Bacikddfae632017-10-19 14:16:02 -04009222 /*
9223 * We can't call btrfs_truncate_block inside a trans handle as we could
9224 * deadlock with freeze, if we got NEED_TRUNCATE_BLOCK then we know
9225 * we've truncated everything except the last little bit, and can do
9226 * btrfs_truncate_block and then update the disk_i_size.
9227 */
9228 if (ret == NEED_TRUNCATE_BLOCK) {
9229 btrfs_end_transaction(trans);
9230 btrfs_btree_balance_dirty(fs_info);
9231
9232 ret = btrfs_truncate_block(inode, inode->i_size, 0, 0);
9233 if (ret)
9234 goto out;
9235 trans = btrfs_start_transaction(root, 1);
9236 if (IS_ERR(trans)) {
9237 ret = PTR_ERR(trans);
9238 goto out;
9239 }
9240 btrfs_ordered_update_i_size(inode, inode->i_size, NULL);
9241 }
9242
Chris Mason917c16b2011-11-08 14:49:59 -05009243 if (trans) {
Omar Sandovalad7e1a72018-05-22 09:59:50 -07009244 int ret2;
Josef Bacik7b128762008-07-24 12:17:14 -04009245
Omar Sandovalad7e1a72018-05-22 09:59:50 -07009246 trans->block_rsv = &fs_info->trans_block_rsv;
9247 ret2 = btrfs_update_inode(trans, root, inode);
9248 if (ret2 && !ret)
9249 ret = ret2;
9250
9251 ret2 = btrfs_end_transaction(trans);
9252 if (ret2 && !ret)
9253 ret = ret2;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009254 btrfs_btree_balance_dirty(fs_info);
Chris Mason917c16b2011-11-08 14:49:59 -05009255 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04009256out:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009257 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009258
Omar Sandovalad7e1a72018-05-22 09:59:50 -07009259 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04009260}
9261
Sven Wegener3b963622008-06-09 21:57:42 -04009262/*
Chris Masond352ac62008-09-29 15:18:18 -04009263 * create a new subvolume directory/inode (helper for the ioctl).
9264 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05009265int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009266 struct btrfs_root *new_root,
9267 struct btrfs_root *parent_root,
9268 u64 new_dirid)
Chris Mason39279cc2007-06-12 06:35:45 -04009269{
Chris Mason39279cc2007-06-12 06:35:45 -04009270 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04009271 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04009272 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009273
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01009274 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
9275 new_dirid, new_dirid,
9276 S_IFDIR | (~current_umask() & S_IRWXUGO),
9277 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04009278 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04009279 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009280 inode->i_op = &btrfs_dir_inode_operations;
9281 inode->i_fop = &btrfs_dir_file_operations;
9282
Miklos Szeredibfe86842011-10-28 14:13:29 +02009283 set_nlink(inode, 1);
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02009284 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Masonb0d5d102014-09-08 13:08:51 -07009285 unlock_new_inode(inode);
Sven Wegener3b963622008-06-09 21:57:42 -04009286
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009287 err = btrfs_subvol_inherit_props(trans, new_root, parent_root);
9288 if (err)
9289 btrfs_err(new_root->fs_info,
David Sterba351fd352014-05-15 16:48:20 +02009290 "error inheriting subvolume %llu properties: %d",
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009291 new_root->root_key.objectid, err);
9292
Yan, Zheng76dda932009-09-21 16:00:26 -04009293 err = btrfs_update_inode(trans, new_root, inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04009294
Yan, Zheng76dda932009-09-21 16:00:26 -04009295 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07009296 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04009297}
9298
Chris Mason39279cc2007-06-12 06:35:45 -04009299struct inode *btrfs_alloc_inode(struct super_block *sb)
9300{
Josef Bacik69fe2d72017-10-19 14:15:57 -04009301 struct btrfs_fs_info *fs_info = btrfs_sb(sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009302 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009303 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009304
David Sterba712e36c2017-10-31 17:08:27 +01009305 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_KERNEL);
Chris Mason39279cc2007-06-12 06:35:45 -04009306 if (!ei)
9307 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009308
9309 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009310 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04009311 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04009312 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04009313 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009314 ei->delalloc_bytes = 0;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01009315 ei->new_delalloc_bytes = 0;
Wang Shilong47059d92014-07-03 18:22:07 +08009316 ei->defrag_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009317 ei->disk_i_size = 0;
9318 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04009319 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009320 ei->index_cnt = (u64)-1;
Miao Xie67de1172013-12-26 13:07:06 +08009321 ei->dir_index = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009322 ei->last_unlink_trans = 0;
Liu Bo46d8bc32012-08-29 01:07:55 -06009323 ei->last_log_commit = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009324
Josef Bacik9e0baf62011-07-15 15:16:44 +00009325 spin_lock_init(&ei->lock);
9326 ei->outstanding_extents = 0;
Josef Bacik69fe2d72017-10-19 14:15:57 -04009327 if (sb->s_magic != BTRFS_TEST_MAGIC)
9328 btrfs_init_metadata_block_rsv(fs_info, &ei->block_rsv,
9329 BTRFS_BLOCK_RSV_DELALLOC);
Josef Bacik72ac3c02012-05-23 14:13:11 -04009330 ei->runtime_flags = 0;
David Sterbab52aa8c2017-07-17 19:17:20 +02009331 ei->prop_compress = BTRFS_COMPRESS_NONE;
David Sterbaeec63c62017-07-17 19:41:31 +02009332 ei->defrag_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009333
Miao Xie16cdcec2011-04-22 18:12:22 +08009334 ei->delayed_node = NULL;
9335
chandan r9cc97d62012-07-04 12:48:07 +05309336 ei->i_otime.tv_sec = 0;
9337 ei->i_otime.tv_nsec = 0;
9338
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009339 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02009340 extent_map_tree_init(&ei->extent_tree);
Qu Wenruo43eb5f22019-03-01 10:47:59 +08009341 extent_io_tree_init(fs_info, &ei->io_tree, IO_TREE_INODE_IO, inode);
9342 extent_io_tree_init(fs_info, &ei->io_failure_tree,
9343 IO_TREE_INODE_IO_FAILURE, inode);
David Sterba7b439732019-03-11 15:58:30 +01009344 ei->io_tree.track_uptodate = true;
9345 ei->io_failure_tree.track_uptodate = true;
Josef Bacikb812ce22012-11-16 13:56:32 -05009346 atomic_set(&ei->sync_writers, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009347 mutex_init(&ei->log_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009348 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009349 INIT_LIST_HEAD(&ei->delalloc_inodes);
David Sterba8089fe62015-11-19 14:15:51 +01009350 INIT_LIST_HEAD(&ei->delayed_iput);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009351 RB_CLEAR_NODE(&ei->rb_node);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01009352 init_rwsem(&ei->dio_sem);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009353
9354 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009355}
9356
Josef Bacikaaedb552013-10-11 14:44:09 -04009357#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
9358void btrfs_test_destroy_inode(struct inode *inode)
9359{
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02009360 btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0);
Josef Bacikaaedb552013-10-11 14:44:09 -04009361 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
9362}
9363#endif
9364
Al Viro26602ca2019-04-10 15:14:41 -04009365void btrfs_free_inode(struct inode *inode)
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009366{
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009367 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
9368}
9369
Chris Mason39279cc2007-06-12 06:35:45 -04009370void btrfs_destroy_inode(struct inode *inode)
9371{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009372 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009373 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009374 struct btrfs_root *root = BTRFS_I(inode)->root;
9375
Al Virob3d9b7a2012-06-09 13:51:19 -04009376 WARN_ON(!hlist_empty(&inode->i_dentry));
Chris Mason39279cc2007-06-12 06:35:45 -04009377 WARN_ON(inode->i_data.nrpages);
Josef Bacik69fe2d72017-10-19 14:15:57 -04009378 WARN_ON(BTRFS_I(inode)->block_rsv.reserved);
9379 WARN_ON(BTRFS_I(inode)->block_rsv.size);
Josef Bacik9e0baf62011-07-15 15:16:44 +00009380 WARN_ON(BTRFS_I(inode)->outstanding_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04009381 WARN_ON(BTRFS_I(inode)->delalloc_bytes);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01009382 WARN_ON(BTRFS_I(inode)->new_delalloc_bytes);
Josef Bacik7709cde2011-08-04 10:25:02 -04009383 WARN_ON(BTRFS_I(inode)->csum_bytes);
Wang Shilong47059d92014-07-03 18:22:07 +08009384 WARN_ON(BTRFS_I(inode)->defrag_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04009385
Chris Mason5a3f23d2009-03-31 13:27:11 -04009386 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05009387 * This can happen where we create an inode, but somebody else also
9388 * created the same inode and we need to destroy the one we already
9389 * created.
9390 */
9391 if (!root)
Al Viro26602ca2019-04-10 15:14:41 -04009392 return;
Josef Bacika6dbd422009-11-11 15:53:34 -05009393
Chris Masond3977122009-01-05 21:25:51 -05009394 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04009395 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
9396 if (!ordered)
9397 break;
9398 else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009399 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04009400 "found ordered extent %llu %llu on inode cleanup",
9401 ordered->file_offset, ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009402 btrfs_remove_ordered_extent(inode, ordered);
9403 btrfs_put_ordered_extent(ordered);
9404 btrfs_put_ordered_extent(ordered);
9405 }
9406 }
Qu Wenruo56fa9d02015-10-13 09:53:10 +08009407 btrfs_qgroup_check_reserved_leak(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04009408 inode_tree_del(inode);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02009409 btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04009410}
9411
Al Viro45321ac2010-06-07 13:43:19 -04009412int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04009413{
9414 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04009415
Naohiro Aota6379ef92013-06-06 09:56:34 +00009416 if (root == NULL)
9417 return 1;
9418
Liu Bofa6ac872013-02-20 14:10:23 +00009419 /* the snap/subvol tree is on deleting */
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02009420 if (btrfs_root_refs(&root->root_item) == 0)
Al Viro45321ac2010-06-07 13:43:19 -04009421 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04009422 else
Al Viro45321ac2010-06-07 13:43:19 -04009423 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04009424}
9425
Sven Wegener0ee0fda2008-07-30 16:54:26 -04009426static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04009427{
9428 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
9429
9430 inode_init_once(&ei->vfs_inode);
9431}
9432
David Sterbae67c7182018-02-19 17:24:18 +01009433void __cold btrfs_destroy_cachep(void)
Chris Mason39279cc2007-06-12 06:35:45 -04009434{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10009435 /*
9436 * Make sure all delayed rcu free inodes are flushed before we
9437 * destroy cache.
9438 */
9439 rcu_barrier();
Kinglong Mee5598e902016-01-29 21:36:35 +08009440 kmem_cache_destroy(btrfs_inode_cachep);
9441 kmem_cache_destroy(btrfs_trans_handle_cachep);
Kinglong Mee5598e902016-01-29 21:36:35 +08009442 kmem_cache_destroy(btrfs_path_cachep);
9443 kmem_cache_destroy(btrfs_free_space_cachep);
Christophe Leroy3acd4852019-08-21 15:05:55 +00009444 kmem_cache_destroy(btrfs_free_space_bitmap_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04009445}
9446
Liu Bof5c29bd2017-11-02 17:21:50 -06009447int __init btrfs_init_cachep(void)
Chris Mason39279cc2007-06-12 06:35:45 -04009448{
David Sterba837e1972012-09-07 03:00:48 -06009449 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009450 sizeof(struct btrfs_inode), 0,
Vladimir Davydov5d097052016-01-14 15:18:21 -08009451 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD | SLAB_ACCOUNT,
9452 init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04009453 if (!btrfs_inode_cachep)
9454 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009455
David Sterba837e1972012-09-07 03:00:48 -06009456 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009457 sizeof(struct btrfs_trans_handle), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009458 SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009459 if (!btrfs_trans_handle_cachep)
9460 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009461
David Sterba837e1972012-09-07 03:00:48 -06009462 btrfs_path_cachep = kmem_cache_create("btrfs_path",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009463 sizeof(struct btrfs_path), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009464 SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009465 if (!btrfs_path_cachep)
9466 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009467
David Sterba837e1972012-09-07 03:00:48 -06009468 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
Josef Bacikdc89e982011-01-28 17:05:48 -05009469 sizeof(struct btrfs_free_space), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009470 SLAB_MEM_SPREAD, NULL);
Josef Bacikdc89e982011-01-28 17:05:48 -05009471 if (!btrfs_free_space_cachep)
9472 goto fail;
9473
Christophe Leroy3acd4852019-08-21 15:05:55 +00009474 btrfs_free_space_bitmap_cachep = kmem_cache_create("btrfs_free_space_bitmap",
9475 PAGE_SIZE, PAGE_SIZE,
9476 SLAB_RED_ZONE, NULL);
9477 if (!btrfs_free_space_bitmap_cachep)
9478 goto fail;
9479
Chris Mason39279cc2007-06-12 06:35:45 -04009480 return 0;
9481fail:
9482 btrfs_destroy_cachep();
9483 return -ENOMEM;
9484}
9485
David Howellsa528d352017-01-31 16:46:22 +00009486static int btrfs_getattr(const struct path *path, struct kstat *stat,
9487 u32 request_mask, unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04009488{
Miao Xiedf0af1a2013-01-29 10:11:59 +00009489 u64 delalloc_bytes;
David Howellsa528d352017-01-31 16:46:22 +00009490 struct inode *inode = d_inode(path->dentry);
David Sterbafadc0d82011-11-20 07:33:38 -05009491 u32 blocksize = inode->i_sb->s_blocksize;
Yonghong Song04a87e32017-05-12 15:07:43 -07009492 u32 bi_flags = BTRFS_I(inode)->flags;
9493
9494 stat->result_mask |= STATX_BTIME;
9495 stat->btime.tv_sec = BTRFS_I(inode)->i_otime.tv_sec;
9496 stat->btime.tv_nsec = BTRFS_I(inode)->i_otime.tv_nsec;
9497 if (bi_flags & BTRFS_INODE_APPEND)
9498 stat->attributes |= STATX_ATTR_APPEND;
9499 if (bi_flags & BTRFS_INODE_COMPRESS)
9500 stat->attributes |= STATX_ATTR_COMPRESSED;
9501 if (bi_flags & BTRFS_INODE_IMMUTABLE)
9502 stat->attributes |= STATX_ATTR_IMMUTABLE;
9503 if (bi_flags & BTRFS_INODE_NODUMP)
9504 stat->attributes |= STATX_ATTR_NODUMP;
9505
9506 stat->attributes_mask |= (STATX_ATTR_APPEND |
9507 STATX_ATTR_COMPRESSED |
9508 STATX_ATTR_IMMUTABLE |
9509 STATX_ATTR_NODUMP);
David Sterbafadc0d82011-11-20 07:33:38 -05009510
Chris Mason39279cc2007-06-12 06:35:45 -04009511 generic_fillattr(inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04009512 stat->dev = BTRFS_I(inode)->root->anon_dev;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009513
9514 spin_lock(&BTRFS_I(inode)->lock);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01009515 delalloc_bytes = BTRFS_I(inode)->new_delalloc_bytes;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009516 spin_unlock(&BTRFS_I(inode)->lock);
David Sterbafadc0d82011-11-20 07:33:38 -05009517 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
Miao Xiedf0af1a2013-01-29 10:11:59 +00009518 ALIGN(delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04009519 return 0;
9520}
9521
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009522static int btrfs_rename_exchange(struct inode *old_dir,
9523 struct dentry *old_dentry,
9524 struct inode *new_dir,
9525 struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04009526{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009527 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009528 struct btrfs_trans_handle *trans;
9529 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009530 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009531 struct inode *new_inode = new_dentry->d_inode;
9532 struct inode *old_inode = old_dentry->d_inode;
Deepa Dinamani95582b02018-05-08 19:36:02 -07009533 struct timespec64 ctime = current_time(old_inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009534 struct dentry *parent;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009535 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
9536 u64 new_ino = btrfs_ino(BTRFS_I(new_inode));
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009537 u64 old_idx = 0;
9538 u64 new_idx = 0;
9539 u64 root_objectid;
9540 int ret;
Filipe Manana86e8aa02016-05-05 02:02:27 +01009541 bool root_log_pinned = false;
9542 bool dest_log_pinned = false;
Filipe Mananad4682ba2018-06-11 19:24:28 +01009543 struct btrfs_log_ctx ctx_root;
9544 struct btrfs_log_ctx ctx_dest;
9545 bool sync_log_root = false;
9546 bool sync_log_dest = false;
9547 bool commit_transaction = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009548
9549 /* we only allow rename subvolume link between subvolumes */
9550 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
9551 return -EXDEV;
9552
Filipe Mananad4682ba2018-06-11 19:24:28 +01009553 btrfs_init_log_ctx(&ctx_root, old_inode);
9554 btrfs_init_log_ctx(&ctx_dest, new_inode);
9555
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009556 /* close the race window with snapshot create/destroy ioctl */
Josef Bacik943eb3b2019-11-19 13:59:20 -05009557 if (old_ino == BTRFS_FIRST_FREE_OBJECTID ||
9558 new_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009559 down_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009560
9561 /*
9562 * We want to reserve the absolute worst case amount of items. So if
9563 * both inodes are subvols and we need to unlink them then that would
9564 * require 4 item modifications, but if they are both normal inodes it
9565 * would require 5 item modifications, so we'll assume their normal
9566 * inodes. So 5 * 2 is 10, plus 2 for the new links, so 12 total items
9567 * should cover the worst case number of items we'll modify.
9568 */
9569 trans = btrfs_start_transaction(root, 12);
9570 if (IS_ERR(trans)) {
9571 ret = PTR_ERR(trans);
9572 goto out_notrans;
9573 }
9574
Josef Bacik3e174092019-11-15 15:43:06 -05009575 if (dest != root)
9576 btrfs_record_root_in_trans(trans, dest);
9577
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009578 /*
9579 * We need to find a free sequence number both in the source and
9580 * in the destination directory for the exchange.
9581 */
Nikolay Borisov877574e2017-02-20 13:50:33 +02009582 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009583 if (ret)
9584 goto out_fail;
Nikolay Borisov877574e2017-02-20 13:50:33 +02009585 ret = btrfs_set_inode_index(BTRFS_I(old_dir), &new_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009586 if (ret)
9587 goto out_fail;
9588
9589 BTRFS_I(old_inode)->dir_index = 0ULL;
9590 BTRFS_I(new_inode)->dir_index = 0ULL;
9591
9592 /* Reference for the source. */
9593 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
9594 /* force full log commit if subvolume involved. */
David Sterba90787762019-03-20 13:28:05 +01009595 btrfs_set_log_full_commit(trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009596 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01009597 btrfs_pin_log_trans(root);
9598 root_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009599 ret = btrfs_insert_inode_ref(trans, dest,
9600 new_dentry->d_name.name,
9601 new_dentry->d_name.len,
9602 old_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01009603 btrfs_ino(BTRFS_I(new_dir)),
9604 old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009605 if (ret)
9606 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009607 }
9608
9609 /* And now for the dest. */
9610 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
9611 /* force full log commit if subvolume involved. */
David Sterba90787762019-03-20 13:28:05 +01009612 btrfs_set_log_full_commit(trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009613 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01009614 btrfs_pin_log_trans(dest);
9615 dest_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009616 ret = btrfs_insert_inode_ref(trans, root,
9617 old_dentry->d_name.name,
9618 old_dentry->d_name.len,
9619 new_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01009620 btrfs_ino(BTRFS_I(old_dir)),
9621 new_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009622 if (ret)
9623 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009624 }
9625
9626 /* Update inode version and ctime/mtime. */
9627 inode_inc_iversion(old_dir);
9628 inode_inc_iversion(new_dir);
9629 inode_inc_iversion(old_inode);
9630 inode_inc_iversion(new_inode);
9631 old_dir->i_ctime = old_dir->i_mtime = ctime;
9632 new_dir->i_ctime = new_dir->i_mtime = ctime;
9633 old_inode->i_ctime = ctime;
9634 new_inode->i_ctime = ctime;
9635
9636 if (old_dentry->d_parent != new_dentry->d_parent) {
David Sterbaf85b7372017-01-20 14:54:07 +01009637 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
9638 BTRFS_I(old_inode), 1);
9639 btrfs_record_unlink_dir(trans, BTRFS_I(new_dir),
9640 BTRFS_I(new_inode), 1);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009641 }
9642
9643 /* src is a subvolume */
9644 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
9645 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
Lu Fengqi401b3b12018-08-01 11:32:30 +08009646 ret = btrfs_unlink_subvol(trans, old_dir, root_objectid,
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009647 old_dentry->d_name.name,
9648 old_dentry->d_name.len);
9649 } else { /* src is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009650 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
9651 BTRFS_I(old_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009652 old_dentry->d_name.name,
9653 old_dentry->d_name.len);
9654 if (!ret)
9655 ret = btrfs_update_inode(trans, root, old_inode);
9656 }
9657 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009658 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009659 goto out_fail;
9660 }
9661
9662 /* dest is a subvolume */
9663 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
9664 root_objectid = BTRFS_I(new_inode)->root->root_key.objectid;
Lu Fengqi401b3b12018-08-01 11:32:30 +08009665 ret = btrfs_unlink_subvol(trans, new_dir, root_objectid,
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009666 new_dentry->d_name.name,
9667 new_dentry->d_name.len);
9668 } else { /* dest is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009669 ret = __btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
9670 BTRFS_I(new_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009671 new_dentry->d_name.name,
9672 new_dentry->d_name.len);
9673 if (!ret)
9674 ret = btrfs_update_inode(trans, dest, new_inode);
9675 }
9676 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009677 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009678 goto out_fail;
9679 }
9680
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009681 ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009682 new_dentry->d_name.name,
9683 new_dentry->d_name.len, 0, old_idx);
9684 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009685 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009686 goto out_fail;
9687 }
9688
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009689 ret = btrfs_add_link(trans, BTRFS_I(old_dir), BTRFS_I(new_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009690 old_dentry->d_name.name,
9691 old_dentry->d_name.len, 0, new_idx);
9692 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009693 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009694 goto out_fail;
9695 }
9696
9697 if (old_inode->i_nlink == 1)
9698 BTRFS_I(old_inode)->dir_index = old_idx;
9699 if (new_inode->i_nlink == 1)
9700 BTRFS_I(new_inode)->dir_index = new_idx;
9701
Filipe Manana86e8aa02016-05-05 02:02:27 +01009702 if (root_log_pinned) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009703 parent = new_dentry->d_parent;
Filipe Mananad4682ba2018-06-11 19:24:28 +01009704 ret = btrfs_log_new_name(trans, BTRFS_I(old_inode),
9705 BTRFS_I(old_dir), parent,
9706 false, &ctx_root);
9707 if (ret == BTRFS_NEED_LOG_SYNC)
9708 sync_log_root = true;
9709 else if (ret == BTRFS_NEED_TRANS_COMMIT)
9710 commit_transaction = true;
9711 ret = 0;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009712 btrfs_end_log_trans(root);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009713 root_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009714 }
Filipe Manana86e8aa02016-05-05 02:02:27 +01009715 if (dest_log_pinned) {
Filipe Mananad4682ba2018-06-11 19:24:28 +01009716 if (!commit_transaction) {
9717 parent = old_dentry->d_parent;
9718 ret = btrfs_log_new_name(trans, BTRFS_I(new_inode),
9719 BTRFS_I(new_dir), parent,
9720 false, &ctx_dest);
9721 if (ret == BTRFS_NEED_LOG_SYNC)
9722 sync_log_dest = true;
9723 else if (ret == BTRFS_NEED_TRANS_COMMIT)
9724 commit_transaction = true;
9725 ret = 0;
9726 }
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009727 btrfs_end_log_trans(dest);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009728 dest_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009729 }
9730out_fail:
Filipe Manana86e8aa02016-05-05 02:02:27 +01009731 /*
9732 * If we have pinned a log and an error happened, we unpin tasks
9733 * trying to sync the log and force them to fallback to a transaction
9734 * commit if the log currently contains any of the inodes involved in
9735 * this rename operation (to ensure we do not persist a log with an
9736 * inconsistent state for any of these inodes or leading to any
9737 * inconsistencies when replayed). If the transaction was aborted, the
9738 * abortion reason is propagated to userspace when attempting to commit
9739 * the transaction. If the log does not contain any of these inodes, we
9740 * allow the tasks to sync it.
9741 */
9742 if (ret && (root_log_pinned || dest_log_pinned)) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009743 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
9744 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
9745 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana86e8aa02016-05-05 02:02:27 +01009746 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009747 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
David Sterba90787762019-03-20 13:28:05 +01009748 btrfs_set_log_full_commit(trans);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009749
9750 if (root_log_pinned) {
9751 btrfs_end_log_trans(root);
9752 root_log_pinned = false;
9753 }
9754 if (dest_log_pinned) {
9755 btrfs_end_log_trans(dest);
9756 dest_log_pinned = false;
9757 }
9758 }
Filipe Mananad4682ba2018-06-11 19:24:28 +01009759 if (!ret && sync_log_root && !commit_transaction) {
9760 ret = btrfs_sync_log(trans, BTRFS_I(old_inode)->root,
9761 &ctx_root);
9762 if (ret)
9763 commit_transaction = true;
9764 }
9765 if (!ret && sync_log_dest && !commit_transaction) {
9766 ret = btrfs_sync_log(trans, BTRFS_I(new_inode)->root,
9767 &ctx_dest);
9768 if (ret)
9769 commit_transaction = true;
9770 }
9771 if (commit_transaction) {
Filipe Mananae6c61712019-11-08 16:11:56 +00009772 /*
9773 * We may have set commit_transaction when logging the new name
9774 * in the destination root, in which case we left the source
9775 * root context in the list of log contextes. So make sure we
9776 * remove it to avoid invalid memory accesses, since the context
9777 * was allocated in our stack frame.
9778 */
9779 if (sync_log_root) {
9780 mutex_lock(&root->log_mutex);
9781 list_del_init(&ctx_root.list);
9782 mutex_unlock(&root->log_mutex);
9783 }
Filipe Mananad4682ba2018-06-11 19:24:28 +01009784 ret = btrfs_commit_transaction(trans);
9785 } else {
9786 int ret2;
9787
9788 ret2 = btrfs_end_transaction(trans);
9789 ret = ret ? ret : ret2;
9790 }
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009791out_notrans:
Josef Bacik943eb3b2019-11-19 13:59:20 -05009792 if (new_ino == BTRFS_FIRST_FREE_OBJECTID ||
9793 old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009794 up_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009795
Filipe Mananae6c61712019-11-08 16:11:56 +00009796 ASSERT(list_empty(&ctx_root.list));
9797 ASSERT(list_empty(&ctx_dest.list));
9798
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009799 return ret;
9800}
9801
9802static int btrfs_whiteout_for_rename(struct btrfs_trans_handle *trans,
9803 struct btrfs_root *root,
9804 struct inode *dir,
9805 struct dentry *dentry)
9806{
9807 int ret;
9808 struct inode *inode;
9809 u64 objectid;
9810 u64 index;
9811
9812 ret = btrfs_find_free_ino(root, &objectid);
9813 if (ret)
9814 return ret;
9815
9816 inode = btrfs_new_inode(trans, root, dir,
9817 dentry->d_name.name,
9818 dentry->d_name.len,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009819 btrfs_ino(BTRFS_I(dir)),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009820 objectid,
9821 S_IFCHR | WHITEOUT_MODE,
9822 &index);
9823
9824 if (IS_ERR(inode)) {
9825 ret = PTR_ERR(inode);
9826 return ret;
9827 }
9828
9829 inode->i_op = &btrfs_special_inode_operations;
9830 init_special_inode(inode, inode->i_mode,
9831 WHITEOUT_DEV);
9832
9833 ret = btrfs_init_inode_security(trans, inode, dir,
9834 &dentry->d_name);
9835 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009836 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009837
Nikolay Borisovcef415a2017-02-20 13:51:09 +02009838 ret = btrfs_add_nondir(trans, BTRFS_I(dir), dentry,
9839 BTRFS_I(inode), 0, index);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009840 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009841 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009842
9843 ret = btrfs_update_inode(trans, root, inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009844out:
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009845 unlock_new_inode(inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009846 if (ret)
9847 inode_dec_link_count(inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009848 iput(inode);
9849
Filipe Mananac9901612016-05-05 01:41:57 +01009850 return ret;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009851}
9852
Chris Mason39279cc2007-06-12 06:35:45 -04009853static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009854 struct inode *new_dir, struct dentry *new_dentry,
9855 unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04009856{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009857 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009858 struct btrfs_trans_handle *trans;
Filipe Manana5062af32016-05-05 10:26:26 +01009859 unsigned int trans_num_items;
Chris Mason39279cc2007-06-12 06:35:45 -04009860 struct btrfs_root *root = BTRFS_I(old_dir)->root;
9861 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00009862 struct inode *new_inode = d_inode(new_dentry);
9863 struct inode *old_inode = d_inode(old_dentry);
Chris Mason00e4e6b2008-08-05 11:18:09 -04009864 u64 index = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009865 u64 root_objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04009866 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009867 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009868 bool log_pinned = false;
Filipe Mananad4682ba2018-06-11 19:24:28 +01009869 struct btrfs_log_ctx ctx;
9870 bool sync_log = false;
9871 bool commit_transaction = false;
Chris Mason39279cc2007-06-12 06:35:45 -04009872
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009873 if (btrfs_ino(BTRFS_I(new_dir)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04009874 return -EPERM;
9875
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009876 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08009877 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05009878 return -EXDEV;
9879
Li Zefan33345d012011-04-20 10:31:50 +08009880 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009881 (new_inode && btrfs_ino(BTRFS_I(new_inode)) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009882 return -ENOTEMPTY;
9883
Chris Mason39279cc2007-06-12 06:35:45 -04009884 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009885 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04009886 return -ENOTEMPTY;
Chris Mason9c520572012-12-17 14:26:57 -05009887
9888
9889 /* check for collisions, even if the name isn't there */
Josef Bacik4871c152013-10-09 12:24:04 -04009890 ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino,
Chris Mason9c520572012-12-17 14:26:57 -05009891 new_dentry->d_name.name,
9892 new_dentry->d_name.len);
9893
9894 if (ret) {
9895 if (ret == -EEXIST) {
9896 /* we shouldn't get
9897 * eexist without a new_inode */
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05309898 if (WARN_ON(!new_inode)) {
Chris Mason9c520572012-12-17 14:26:57 -05009899 return ret;
9900 }
9901 } else {
9902 /* maybe -EOVERFLOW */
9903 return ret;
9904 }
9905 }
9906 ret = 0;
9907
Chris Mason5a3f23d2009-03-31 13:27:11 -04009908 /*
Chris Mason8d875f92014-08-12 10:47:42 -07009909 * we're using rename to replace one file with another. Start IO on it
9910 * now so we don't add too much work to the end of the transaction
Chris Mason5a3f23d2009-03-31 13:27:11 -04009911 */
Chris Mason8d875f92014-08-12 10:47:42 -07009912 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size)
Chris Mason5a3f23d2009-03-31 13:27:11 -04009913 filemap_flush(old_inode->i_mapping);
9914
Yan, Zheng76dda932009-09-21 16:00:26 -04009915 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08009916 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009917 down_read(&fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04009918 /*
9919 * We want to reserve the absolute worst case amount of items. So if
9920 * both inodes are subvols and we need to unlink them then that would
9921 * require 4 item modifications, but if they are both normal inodes it
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009922 * would require 5 item modifications, so we'll assume they are normal
Yan, Zhenga22285a2010-05-16 10:48:46 -04009923 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
9924 * should cover the worst case number of items we'll modify.
Filipe Manana5062af32016-05-05 10:26:26 +01009925 * If our rename has the whiteout flag, we need more 5 units for the
9926 * new inode (1 inode item, 1 inode ref, 2 dir items and 1 xattr item
9927 * when selinux is enabled).
Yan, Zhenga22285a2010-05-16 10:48:46 -04009928 */
Filipe Manana5062af32016-05-05 10:26:26 +01009929 trans_num_items = 11;
9930 if (flags & RENAME_WHITEOUT)
9931 trans_num_items += 5;
9932 trans = btrfs_start_transaction(root, trans_num_items);
Johann Lombardib44c59a2011-03-31 13:23:47 +00009933 if (IS_ERR(trans)) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009934 ret = PTR_ERR(trans);
9935 goto out_notrans;
9936 }
Chris Mason5f39d392007-10-15 16:14:19 -04009937
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009938 if (dest != root)
9939 btrfs_record_root_in_trans(trans, dest);
9940
Nikolay Borisov877574e2017-02-20 13:50:33 +02009941 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009942 if (ret)
9943 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009944
Miao Xie67de1172013-12-26 13:07:06 +08009945 BTRFS_I(old_inode)->dir_index = 0ULL;
Li Zefan33345d012011-04-20 10:31:50 +08009946 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009947 /* force full log commit if subvolume involved. */
David Sterba90787762019-03-20 13:28:05 +01009948 btrfs_set_log_full_commit(trans);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009949 } else {
Filipe Mananac4aba952016-04-29 13:14:42 +01009950 btrfs_pin_log_trans(root);
9951 log_pinned = true;
Yan, Zhenga5719522009-09-24 09:17:31 -04009952 ret = btrfs_insert_inode_ref(trans, dest,
9953 new_dentry->d_name.name,
9954 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08009955 old_ino,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009956 btrfs_ino(BTRFS_I(new_dir)), index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009957 if (ret)
9958 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009959 }
Chris Mason5a3f23d2009-03-31 13:27:11 -04009960
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009961 inode_inc_iversion(old_dir);
9962 inode_inc_iversion(new_dir);
9963 inode_inc_iversion(old_inode);
Deepa Dinamani04b285f2016-02-06 23:57:21 -08009964 old_dir->i_ctime = old_dir->i_mtime =
9965 new_dir->i_ctime = new_dir->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009966 old_inode->i_ctime = current_time(old_dir);
Chris Mason5f39d392007-10-15 16:14:19 -04009967
Chris Mason12fcfd22009-03-24 10:24:20 -04009968 if (old_dentry->d_parent != new_dentry->d_parent)
David Sterbaf85b7372017-01-20 14:54:07 +01009969 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
9970 BTRFS_I(old_inode), 1);
Chris Mason12fcfd22009-03-24 10:24:20 -04009971
Li Zefan33345d012011-04-20 10:31:50 +08009972 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009973 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
Lu Fengqi401b3b12018-08-01 11:32:30 +08009974 ret = btrfs_unlink_subvol(trans, old_dir, root_objectid,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009975 old_dentry->d_name.name,
9976 old_dentry->d_name.len);
9977 } else {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009978 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
9979 BTRFS_I(d_inode(old_dentry)),
Al Viro92986792011-03-04 17:14:37 +00009980 old_dentry->d_name.name,
9981 old_dentry->d_name.len);
9982 if (!ret)
9983 ret = btrfs_update_inode(trans, root, old_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009984 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009985 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009986 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009987 goto out_fail;
9988 }
Chris Mason39279cc2007-06-12 06:35:45 -04009989
9990 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009991 inode_inc_iversion(new_inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009992 new_inode->i_ctime = current_time(new_inode);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009993 if (unlikely(btrfs_ino(BTRFS_I(new_inode)) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009994 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
9995 root_objectid = BTRFS_I(new_inode)->location.objectid;
Lu Fengqi401b3b12018-08-01 11:32:30 +08009996 ret = btrfs_unlink_subvol(trans, new_dir, root_objectid,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009997 new_dentry->d_name.name,
9998 new_dentry->d_name.len);
9999 BUG_ON(new_inode->i_nlink == 0);
10000 } else {
Nikolay Borisov4ec59342017-01-18 00:31:44 +020010001 ret = btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
10002 BTRFS_I(d_inode(new_dentry)),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010003 new_dentry->d_name.name,
10004 new_dentry->d_name.len);
10005 }
Josef Bacik4ef31a42013-08-13 14:10:08 -040010006 if (!ret && new_inode->i_nlink == 0)
Nikolay Borisov73f2e542017-02-20 13:50:59 +020010007 ret = btrfs_orphan_add(trans,
10008 BTRFS_I(d_inode(new_dentry)));
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010009 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -040010010 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010011 goto out_fail;
10012 }
Chris Mason39279cc2007-06-12 06:35:45 -040010013 }
Josef Bacikaec74772008-07-24 12:12:38 -040010014
Nikolay Borisovdb0a6692017-02-20 13:51:08 +020010015 ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010016 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -040010017 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010018 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -040010019 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010020 goto out_fail;
10021 }
Chris Mason39279cc2007-06-12 06:35:45 -040010022
Miao Xie67de1172013-12-26 13:07:06 +080010023 if (old_inode->i_nlink == 1)
10024 BTRFS_I(old_inode)->dir_index = index;
10025
Filipe Manana3dc9e8f72016-04-29 11:34:22 +010010026 if (log_pinned) {
Al Viro10d9f302011-07-16 23:09:10 -040010027 struct dentry *parent = new_dentry->d_parent;
Filipe Manana3dc9e8f72016-04-29 11:34:22 +010010028
Filipe Mananad4682ba2018-06-11 19:24:28 +010010029 btrfs_init_log_ctx(&ctx, old_inode);
10030 ret = btrfs_log_new_name(trans, BTRFS_I(old_inode),
10031 BTRFS_I(old_dir), parent,
10032 false, &ctx);
10033 if (ret == BTRFS_NEED_LOG_SYNC)
10034 sync_log = true;
10035 else if (ret == BTRFS_NEED_TRANS_COMMIT)
10036 commit_transaction = true;
10037 ret = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010038 btrfs_end_log_trans(root);
Filipe Manana3dc9e8f72016-04-29 11:34:22 +010010039 log_pinned = false;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010040 }
Dan Fuhrycdd1fed2016-03-17 15:23:38 +010010041
10042 if (flags & RENAME_WHITEOUT) {
10043 ret = btrfs_whiteout_for_rename(trans, root, old_dir,
10044 old_dentry);
10045
10046 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -040010047 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +010010048 goto out_fail;
10049 }
Chris Mason12fcfd22009-03-24 10:24:20 -040010050 }
Chris Mason39279cc2007-06-12 06:35:45 -040010051out_fail:
Filipe Manana3dc9e8f72016-04-29 11:34:22 +010010052 /*
10053 * If we have pinned the log and an error happened, we unpin tasks
10054 * trying to sync the log and force them to fallback to a transaction
10055 * commit if the log currently contains any of the inodes involved in
10056 * this rename operation (to ensure we do not persist a log with an
10057 * inconsistent state for any of these inodes or leading to any
10058 * inconsistencies when replayed). If the transaction was aborted, the
10059 * abortion reason is propagated to userspace when attempting to commit
10060 * the transaction. If the log does not contain any of these inodes, we
10061 * allow the tasks to sync it.
10062 */
10063 if (ret && log_pinned) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +020010064 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
10065 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
10066 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana3dc9e8f72016-04-29 11:34:22 +010010067 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +020010068 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
David Sterba90787762019-03-20 13:28:05 +010010069 btrfs_set_log_full_commit(trans);
Filipe Manana3dc9e8f72016-04-29 11:34:22 +010010070
10071 btrfs_end_log_trans(root);
10072 log_pinned = false;
10073 }
Filipe Mananad4682ba2018-06-11 19:24:28 +010010074 if (!ret && sync_log) {
10075 ret = btrfs_sync_log(trans, BTRFS_I(old_inode)->root, &ctx);
10076 if (ret)
10077 commit_transaction = true;
10078 }
10079 if (commit_transaction) {
10080 ret = btrfs_commit_transaction(trans);
10081 } else {
10082 int ret2;
10083
10084 ret2 = btrfs_end_transaction(trans);
10085 ret = ret ? ret : ret2;
10086 }
Johann Lombardib44c59a2011-03-31 13:23:47 +000010087out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +080010088 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010089 up_read(&fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -040010090
Chris Mason39279cc2007-06-12 06:35:45 -040010091 return ret;
10092}
10093
Miklos Szeredi80ace852014-07-23 15:15:32 +020010094static int btrfs_rename2(struct inode *old_dir, struct dentry *old_dentry,
10095 struct inode *new_dir, struct dentry *new_dentry,
10096 unsigned int flags)
10097{
Dan Fuhrycdd1fed2016-03-17 15:23:38 +010010098 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
Miklos Szeredi80ace852014-07-23 15:15:32 +020010099 return -EINVAL;
10100
Dan Fuhrycdd1fed2016-03-17 15:23:38 +010010101 if (flags & RENAME_EXCHANGE)
10102 return btrfs_rename_exchange(old_dir, old_dentry, new_dir,
10103 new_dentry);
10104
10105 return btrfs_rename(old_dir, old_dentry, new_dir, new_dentry, flags);
Miklos Szeredi80ace852014-07-23 15:15:32 +020010106}
10107
Nikolay Borisov3a2f8c02018-04-24 17:23:59 +030010108struct btrfs_delalloc_work {
10109 struct inode *inode;
10110 struct completion completion;
10111 struct list_head list;
10112 struct btrfs_work work;
10113};
10114
Miao Xie8ccf6f192012-10-25 09:28:04 +000010115static void btrfs_run_delalloc_work(struct btrfs_work *work)
10116{
10117 struct btrfs_delalloc_work *delalloc_work;
Josef Bacik9f23e282013-10-28 15:03:41 -040010118 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010119
10120 delalloc_work = container_of(work, struct btrfs_delalloc_work,
10121 work);
Josef Bacik9f23e282013-10-28 15:03:41 -040010122 inode = delalloc_work->inode;
David Sterba30424602015-11-27 19:27:11 +010010123 filemap_flush(inode->i_mapping);
10124 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
10125 &BTRFS_I(inode)->runtime_flags))
Josef Bacik9f23e282013-10-28 15:03:41 -040010126 filemap_flush(inode->i_mapping);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010127
Nikolay Borisov076da912018-04-23 10:54:16 +030010128 iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010129 complete(&delalloc_work->completion);
10130}
10131
Nikolay Borisov3a2f8c02018-04-24 17:23:59 +030010132static struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode)
Miao Xie8ccf6f192012-10-25 09:28:04 +000010133{
10134 struct btrfs_delalloc_work *work;
10135
David Sterba100d5702015-12-08 14:39:32 +010010136 work = kmalloc(sizeof(*work), GFP_NOFS);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010137 if (!work)
10138 return NULL;
10139
10140 init_completion(&work->completion);
10141 INIT_LIST_HEAD(&work->list);
10142 work->inode = inode;
Omar Sandovala0cac0e2019-09-16 11:30:57 -070010143 btrfs_init_work(&work->work, btrfs_run_delalloc_work, NULL, NULL);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010144
10145 return work;
10146}
10147
Chris Masond352ac62008-09-29 15:18:18 -040010148/*
10149 * some fairly slow code that needs optimization. This walks the list
10150 * of all the inodes with pending delalloc and forces them to disk.
10151 */
Ethan Lien3cd24c62018-11-01 14:49:03 +080010152static int start_delalloc_inodes(struct btrfs_root *root, int nr, bool snapshot)
Chris Masonea8c2812008-08-04 23:17:27 -040010153{
Chris Masonea8c2812008-08-04 23:17:27 -040010154 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010155 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010156 struct btrfs_delalloc_work *work, *next;
10157 struct list_head works;
Miao Xie1eafa6c2013-01-22 10:49:00 +000010158 struct list_head splice;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010159 int ret = 0;
Chris Masonea8c2812008-08-04 23:17:27 -040010160
Miao Xie8ccf6f192012-10-25 09:28:04 +000010161 INIT_LIST_HEAD(&works);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010162 INIT_LIST_HEAD(&splice);
Miao Xie63607cc2013-01-22 10:50:35 +000010163
Miao Xie573bfb72014-03-06 13:55:03 +080010164 mutex_lock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010165 spin_lock(&root->delalloc_lock);
10166 list_splice_init(&root->delalloc_inodes, &splice);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010167 while (!list_empty(&splice)) {
10168 binode = list_entry(splice.next, struct btrfs_inode,
Chris Masonea8c2812008-08-04 23:17:27 -040010169 delalloc_inodes);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010170
Miao Xieeb73c1b2013-05-15 07:48:22 +000010171 list_move_tail(&binode->delalloc_inodes,
10172 &root->delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010173 inode = igrab(&binode->vfs_inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +000010174 if (!inode) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010175 cond_resched_lock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010176 continue;
Miao Xiedf0af1a2013-01-29 10:11:59 +000010177 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010178 spin_unlock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010179
Ethan Lien3cd24c62018-11-01 14:49:03 +080010180 if (snapshot)
10181 set_bit(BTRFS_INODE_SNAPSHOT_FLUSH,
10182 &binode->runtime_flags);
Nikolay Borisov076da912018-04-23 10:54:16 +030010183 work = btrfs_alloc_delalloc_work(inode);
David Sterba5d99a9982014-09-29 19:20:37 +020010184 if (!work) {
Nikolay Borisov4fbb5142018-04-23 10:54:15 +030010185 iput(inode);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010186 ret = -ENOMEM;
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010187 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010188 }
Miao Xie1eafa6c2013-01-22 10:49:00 +000010189 list_add_tail(&work->list, &works);
Qu Wenruoa44903a2014-02-28 10:46:09 +080010190 btrfs_queue_work(root->fs_info->flush_workers,
10191 &work->work);
Miao Xie6c255e62014-03-06 13:55:01 +080010192 ret++;
10193 if (nr != -1 && ret >= nr)
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010194 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010195 cond_resched();
Miao Xieeb73c1b2013-05-15 07:48:22 +000010196 spin_lock(&root->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -040010197 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010198 spin_unlock(&root->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -040010199
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010200out:
Miao Xie1eafa6c2013-01-22 10:49:00 +000010201 list_for_each_entry_safe(work, next, &works, list) {
10202 list_del_init(&work->list);
Nikolay Borisov40012f92018-04-19 10:46:39 +030010203 wait_for_completion(&work->completion);
10204 kfree(work);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010205 }
10206
Nikolay Borisov81f1d392018-04-19 10:46:37 +030010207 if (!list_empty(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010208 spin_lock(&root->delalloc_lock);
10209 list_splice_tail(&splice, &root->delalloc_inodes);
10210 spin_unlock(&root->delalloc_lock);
10211 }
Miao Xie573bfb72014-03-06 13:55:03 +080010212 mutex_unlock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010213 return ret;
10214}
10215
Ethan Lien3cd24c62018-11-01 14:49:03 +080010216int btrfs_start_delalloc_snapshot(struct btrfs_root *root)
Miao Xieeb73c1b2013-05-15 07:48:22 +000010217{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010218 struct btrfs_fs_info *fs_info = root->fs_info;
Miao Xieeb73c1b2013-05-15 07:48:22 +000010219 int ret;
10220
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010221 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +000010222 return -EROFS;
10223
Ethan Lien3cd24c62018-11-01 14:49:03 +080010224 ret = start_delalloc_inodes(root, -1, true);
Miao Xie6c255e62014-03-06 13:55:01 +080010225 if (ret > 0)
10226 ret = 0;
Miao Xieeb73c1b2013-05-15 07:48:22 +000010227 return ret;
10228}
10229
Nikolay Borisov82b3e532018-04-23 10:54:13 +030010230int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, int nr)
Miao Xieeb73c1b2013-05-15 07:48:22 +000010231{
10232 struct btrfs_root *root;
10233 struct list_head splice;
10234 int ret;
10235
Wang Shilong2c21b4d2014-01-14 19:42:20 +080010236 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +000010237 return -EROFS;
10238
10239 INIT_LIST_HEAD(&splice);
10240
Miao Xie573bfb72014-03-06 13:55:03 +080010241 mutex_lock(&fs_info->delalloc_root_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010242 spin_lock(&fs_info->delalloc_root_lock);
10243 list_splice_init(&fs_info->delalloc_roots, &splice);
Miao Xie6c255e62014-03-06 13:55:01 +080010244 while (!list_empty(&splice) && nr) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010245 root = list_first_entry(&splice, struct btrfs_root,
10246 delalloc_root);
10247 root = btrfs_grab_fs_root(root);
10248 BUG_ON(!root);
10249 list_move_tail(&root->delalloc_root,
10250 &fs_info->delalloc_roots);
10251 spin_unlock(&fs_info->delalloc_root_lock);
10252
Ethan Lien3cd24c62018-11-01 14:49:03 +080010253 ret = start_delalloc_inodes(root, nr, false);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010254 btrfs_put_fs_root(root);
Miao Xie6c255e62014-03-06 13:55:01 +080010255 if (ret < 0)
Miao Xieeb73c1b2013-05-15 07:48:22 +000010256 goto out;
10257
Miao Xie6c255e62014-03-06 13:55:01 +080010258 if (nr != -1) {
10259 nr -= ret;
10260 WARN_ON(nr < 0);
10261 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010262 spin_lock(&fs_info->delalloc_root_lock);
10263 }
10264 spin_unlock(&fs_info->delalloc_root_lock);
10265
Miao Xie6c255e62014-03-06 13:55:01 +080010266 ret = 0;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010267out:
Nikolay Borisov81f1d392018-04-19 10:46:37 +030010268 if (!list_empty(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010269 spin_lock(&fs_info->delalloc_root_lock);
10270 list_splice_tail(&splice, &fs_info->delalloc_roots);
10271 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010272 }
Miao Xie573bfb72014-03-06 13:55:03 +080010273 mutex_unlock(&fs_info->delalloc_root_mutex);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010274 return ret;
Chris Masonea8c2812008-08-04 23:17:27 -040010275}
10276
Chris Mason39279cc2007-06-12 06:35:45 -040010277static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
10278 const char *symname)
10279{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010280 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -040010281 struct btrfs_trans_handle *trans;
10282 struct btrfs_root *root = BTRFS_I(dir)->root;
10283 struct btrfs_path *path;
10284 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -050010285 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -040010286 int err;
Chris Mason39279cc2007-06-12 06:35:45 -040010287 u64 objectid;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +053010288 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -040010289 int name_len;
10290 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -040010291 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -040010292 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -040010293 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -040010294
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +010010295 name_len = strlen(symname);
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010296 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info))
Chris Mason39279cc2007-06-12 06:35:45 -040010297 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -050010298
Josef Bacik9ed74f22009-09-11 16:12:44 -040010299 /*
10300 * 2 items for inode item and ref
10301 * 2 items for dir items
Filipe Manana9269d122015-12-31 18:16:29 +000010302 * 1 item for updating parent inode item
10303 * 1 item for the inline extent item
Josef Bacik9ed74f22009-09-11 16:12:44 -040010304 * 1 item for xattr if selinux is on
10305 */
Filipe Manana9269d122015-12-31 18:16:29 +000010306 trans = btrfs_start_transaction(root, 7);
Yan, Zhenga22285a2010-05-16 10:48:46 -040010307 if (IS_ERR(trans))
10308 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -050010309
Li Zefan581bb052011-04-20 10:06:11 +080010310 err = btrfs_find_free_ino(root, &objectid);
10311 if (err)
10312 goto out_unlock;
10313
Josef Bacikaec74772008-07-24 12:12:38 -040010314 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +010010315 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)),
10316 objectid, S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -040010317 if (IS_ERR(inode)) {
10318 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -040010319 inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -040010320 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -040010321 }
Chris Mason39279cc2007-06-12 06:35:45 -040010322
Casey Schauflerad19db72011-12-15 10:09:07 -050010323 /*
10324 * If the active LSM wants to access the inode during
10325 * d_instantiate it needs these. Smack checks to see
10326 * if the filesystem supports xattrs by looking at the
10327 * ops vector.
10328 */
10329 inode->i_fop = &btrfs_file_operations;
10330 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -070010331 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -070010332 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
10333
10334 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
10335 if (err)
Al Viro32955c52018-05-16 12:20:05 -040010336 goto out_unlock;
Casey Schauflerad19db72011-12-15 10:09:07 -050010337
Chris Mason39279cc2007-06-12 06:35:45 -040010338 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -070010339 if (!path) {
10340 err = -ENOMEM;
Al Viro32955c52018-05-16 12:20:05 -040010341 goto out_unlock;
Mark Fashehd8926bb2011-07-13 10:38:47 -070010342 }
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +020010343 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason39279cc2007-06-12 06:35:45 -040010344 key.offset = 0;
David Sterba962a2982014-06-04 18:41:45 +020010345 key.type = BTRFS_EXTENT_DATA_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -040010346 datasize = btrfs_file_extent_calc_inline_size(name_len);
10347 err = btrfs_insert_empty_item(trans, root, path, &key,
10348 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -040010349 if (err) {
Julia Lawallb0839162011-05-14 07:10:51 +000010350 btrfs_free_path(path);
Al Viro32955c52018-05-16 12:20:05 -040010351 goto out_unlock;
Chris Mason54aa1f42007-06-22 14:16:25 -040010352 }
Chris Mason5f39d392007-10-15 16:14:19 -040010353 leaf = path->nodes[0];
10354 ei = btrfs_item_ptr(leaf, path->slots[0],
10355 struct btrfs_file_extent_item);
10356 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
10357 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -040010358 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -040010359 btrfs_set_file_extent_encryption(leaf, ei, 0);
10360 btrfs_set_file_extent_compression(leaf, ei, 0);
10361 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
10362 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
10363
Chris Mason39279cc2007-06-12 06:35:45 -040010364 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -040010365 write_extent_buffer(leaf, symname, ptr, name_len);
10366 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -040010367 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -040010368
Chris Mason39279cc2007-06-12 06:35:45 -040010369 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -050010370 inode_nohighmem(inode);
Yan Zhengd899e052008-10-30 14:25:28 -040010371 inode_set_bytes(inode, name_len);
Nikolay Borisov6ef06d22017-02-20 13:50:34 +020010372 btrfs_i_size_write(BTRFS_I(inode), name_len);
Chris Mason54aa1f42007-06-22 14:16:25 -040010373 err = btrfs_update_inode(trans, root, inode);
Filipe Mananad50866d2015-12-31 18:08:24 +000010374 /*
10375 * Last step, add directory indexes for our symlink inode. This is the
10376 * last step to avoid extra cleanup of these indexes if an error happens
10377 * elsewhere above.
10378 */
10379 if (!err)
Nikolay Borisovcef415a2017-02-20 13:51:09 +020010380 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry,
10381 BTRFS_I(inode), 0, index);
Al Viro32955c52018-05-16 12:20:05 -040010382 if (err)
10383 goto out_unlock;
Chris Masonb0d5d102014-09-08 13:08:51 -070010384
Al Viro1e2e5472018-05-04 08:23:01 -040010385 d_instantiate_new(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010386
10387out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010388 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -040010389 if (err && inode) {
Chris Mason39279cc2007-06-12 06:35:45 -040010390 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -040010391 discard_new_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010392 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010393 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -040010394 return err;
10395}
Chris Mason16432982008-04-10 10:23:21 -040010396
Josef Bacik0af3d002010-06-21 14:48:16 -040010397static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
10398 u64 start, u64 num_bytes, u64 min_size,
10399 loff_t actual_len, u64 *alloc_hint,
10400 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -040010401{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010402 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik5dc562c2012-08-17 13:14:17 -040010403 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
10404 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -040010405 struct btrfs_root *root = BTRFS_I(inode)->root;
10406 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -040010407 u64 cur_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +000010408 u64 i_size;
Chris Mason154ea282013-03-05 11:11:26 -050010409 u64 cur_bytes;
Josef Bacik0b670dc2015-09-23 17:11:16 -040010410 u64 last_alloc = (u64)-1;
Yan Zhengd899e052008-10-30 14:25:28 -040010411 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -040010412 bool own_trans = true;
Wang Xiaoguang18513092016-07-25 15:51:40 +080010413 u64 end = start + num_bytes - 1;
Yan Zhengd899e052008-10-30 14:25:28 -040010414
Josef Bacik0af3d002010-06-21 14:48:16 -040010415 if (trans)
10416 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -040010417 while (num_bytes > 0) {
Josef Bacik0af3d002010-06-21 14:48:16 -040010418 if (own_trans) {
10419 trans = btrfs_start_transaction(root, 3);
10420 if (IS_ERR(trans)) {
10421 ret = PTR_ERR(trans);
10422 break;
10423 }
Yan Zhengd899e052008-10-30 14:25:28 -040010424 }
Yan, Zheng5a303d52009-11-12 09:34:52 +000010425
Byongho Leeee221842015-12-15 01:42:10 +090010426 cur_bytes = min_t(u64, num_bytes, SZ_256M);
Chris Mason154ea282013-03-05 11:11:26 -050010427 cur_bytes = max(cur_bytes, min_size);
Josef Bacik0b670dc2015-09-23 17:11:16 -040010428 /*
10429 * If we are severely fragmented we could end up with really
10430 * small allocations, so if the allocator is returning small
10431 * chunks lets make its job easier by only searching for those
10432 * sized chunks.
10433 */
10434 cur_bytes = min(cur_bytes, last_alloc);
Wang Xiaoguang18513092016-07-25 15:51:40 +080010435 ret = btrfs_reserve_extent(root, cur_bytes, cur_bytes,
10436 min_size, 0, *alloc_hint, &ins, 1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010437 if (ret) {
Josef Bacik0af3d002010-06-21 14:48:16 -040010438 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010439 btrfs_end_transaction(trans);
Yan, Zhenga22285a2010-05-16 10:48:46 -040010440 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +000010441 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010442 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010443
Josef Bacik0b670dc2015-09-23 17:11:16 -040010444 last_alloc = ins.offset;
Yan Zhengd899e052008-10-30 14:25:28 -040010445 ret = insert_reserved_file_extent(trans, inode,
10446 cur_offset, ins.objectid,
10447 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +000010448 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -040010449 BTRFS_FILE_EXTENT_PREALLOC);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010450 if (ret) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010451 btrfs_free_reserved_extent(fs_info, ins.objectid,
Miao Xiee570fd22014-06-19 10:42:50 +080010452 ins.offset, 0);
Jeff Mahoney66642832016-06-10 18:19:25 -040010453 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010454 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010455 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010456 break;
10457 }
Dongsheng Yang31193212014-12-12 16:44:35 +080010458
Nikolay Borisovdcdbc052017-02-20 13:50:45 +020010459 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Chris Masona1ed8352009-09-11 12:27:37 -040010460 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010461
Josef Bacik5dc562c2012-08-17 13:14:17 -040010462 em = alloc_extent_map();
10463 if (!em) {
10464 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
10465 &BTRFS_I(inode)->runtime_flags);
10466 goto next;
10467 }
10468
10469 em->start = cur_offset;
10470 em->orig_start = cur_offset;
10471 em->len = ins.offset;
10472 em->block_start = ins.objectid;
10473 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -050010474 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -040010475 em->ram_bytes = ins.offset;
Josef Bacik5dc562c2012-08-17 13:14:17 -040010476 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
10477 em->generation = trans->transid;
10478
10479 while (1) {
10480 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -040010481 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -040010482 write_unlock(&em_tree->lock);
10483 if (ret != -EEXIST)
10484 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +020010485 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -040010486 cur_offset + ins.offset - 1,
10487 0);
10488 }
10489 free_extent_map(em);
10490next:
Yan Zhengd899e052008-10-30 14:25:28 -040010491 num_bytes -= ins.offset;
10492 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -040010493 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +000010494
Josef Bacik0c4d2d92012-04-05 15:03:02 -040010495 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -070010496 inode->i_ctime = current_time(inode);
Christoph Hellwig6cbff002009-04-17 10:37:41 +020010497 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -040010498 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -040010499 (actual_len > inode->i_size) &&
10500 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +000010501 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +000010502 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +000010503 else
Josef Bacik55a61d12010-11-22 18:50:32 +000010504 i_size = cur_offset;
10505 i_size_write(inode, i_size);
10506 btrfs_ordered_update_i_size(inode, i_size, NULL);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010507 }
10508
Yan Zhengd899e052008-10-30 14:25:28 -040010509 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010510
10511 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -040010512 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010513 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010514 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010515 break;
10516 }
Yan Zhengd899e052008-10-30 14:25:28 -040010517
Josef Bacik0af3d002010-06-21 14:48:16 -040010518 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010519 btrfs_end_transaction(trans);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010520 }
Wang Xiaoguang18513092016-07-25 15:51:40 +080010521 if (cur_offset < end)
Qu Wenruobc42bda2017-02-27 15:10:39 +080010522 btrfs_free_reserved_data_space(inode, NULL, cur_offset,
Wang Xiaoguang18513092016-07-25 15:51:40 +080010523 end - cur_offset + 1);
Yan Zhengd899e052008-10-30 14:25:28 -040010524 return ret;
10525}
10526
Josef Bacik0af3d002010-06-21 14:48:16 -040010527int btrfs_prealloc_file_range(struct inode *inode, int mode,
10528 u64 start, u64 num_bytes, u64 min_size,
10529 loff_t actual_len, u64 *alloc_hint)
10530{
10531 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
10532 min_size, actual_len, alloc_hint,
10533 NULL);
10534}
10535
10536int btrfs_prealloc_file_range_trans(struct inode *inode,
10537 struct btrfs_trans_handle *trans, int mode,
10538 u64 start, u64 num_bytes, u64 min_size,
10539 loff_t actual_len, u64 *alloc_hint)
10540{
10541 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
10542 min_size, actual_len, alloc_hint, trans);
10543}
10544
Chris Masone6dcd2d2008-07-17 12:53:50 -040010545static int btrfs_set_page_dirty(struct page *page)
10546{
Chris Masone6dcd2d2008-07-17 12:53:50 -040010547 return __set_page_dirty_nobuffers(page);
10548}
10549
Al Viro10556cb22011-06-20 19:28:19 -040010550static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -050010551{
Li Zefanb83cc962010-12-20 16:04:08 +080010552 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +000010553 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +080010554
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +000010555 if (mask & MAY_WRITE &&
10556 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
10557 if (btrfs_root_readonly(root))
10558 return -EROFS;
10559 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
10560 return -EACCES;
10561 }
Al Viro2830ba72011-06-20 19:16:29 -040010562 return generic_permission(inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -050010563}
Chris Mason39279cc2007-06-12 06:35:45 -040010564
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010565static int btrfs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
10566{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010567 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010568 struct btrfs_trans_handle *trans;
10569 struct btrfs_root *root = BTRFS_I(dir)->root;
10570 struct inode *inode = NULL;
10571 u64 objectid;
10572 u64 index;
10573 int ret = 0;
10574
10575 /*
10576 * 5 units required for adding orphan entry
10577 */
10578 trans = btrfs_start_transaction(root, 5);
10579 if (IS_ERR(trans))
10580 return PTR_ERR(trans);
10581
10582 ret = btrfs_find_free_ino(root, &objectid);
10583 if (ret)
10584 goto out;
10585
10586 inode = btrfs_new_inode(trans, root, dir, NULL, 0,
David Sterbaf85b7372017-01-20 14:54:07 +010010587 btrfs_ino(BTRFS_I(dir)), objectid, mode, &index);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010588 if (IS_ERR(inode)) {
10589 ret = PTR_ERR(inode);
10590 inode = NULL;
10591 goto out;
10592 }
10593
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010594 inode->i_fop = &btrfs_file_operations;
10595 inode->i_op = &btrfs_file_inode_operations;
10596
10597 inode->i_mapping->a_ops = &btrfs_aops;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010598 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
10599
Chris Masonb0d5d102014-09-08 13:08:51 -070010600 ret = btrfs_init_inode_security(trans, inode, dir, NULL);
10601 if (ret)
Al Viro32955c52018-05-16 12:20:05 -040010602 goto out;
Chris Masonb0d5d102014-09-08 13:08:51 -070010603
10604 ret = btrfs_update_inode(trans, root, inode);
10605 if (ret)
Al Viro32955c52018-05-16 12:20:05 -040010606 goto out;
Nikolay Borisov73f2e542017-02-20 13:50:59 +020010607 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010608 if (ret)
Al Viro32955c52018-05-16 12:20:05 -040010609 goto out;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010610
Filipe Manana5762b5c2014-08-01 00:10:32 +010010611 /*
10612 * We set number of links to 0 in btrfs_new_inode(), and here we set
10613 * it to 1 because d_tmpfile() will issue a warning if the count is 0,
10614 * through:
10615 *
10616 * d_tmpfile() -> inode_dec_link_count() -> drop_nlink()
10617 */
10618 set_nlink(inode, 1);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010619 d_tmpfile(dentry, inode);
Al Viro32955c52018-05-16 12:20:05 -040010620 unlock_new_inode(inode);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010621 mark_inode_dirty(inode);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010622out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010623 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -040010624 if (ret && inode)
10625 discard_new_inode(inode);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010626 btrfs_btree_balance_dirty(fs_info);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010627 return ret;
10628}
10629
David Sterba5cdc84b2018-07-18 20:32:52 +020010630void btrfs_set_range_writeback(struct extent_io_tree *tree, u64 start, u64 end)
Josef Bacikc6100a42017-05-05 11:57:13 -040010631{
David Sterba5cdc84b2018-07-18 20:32:52 +020010632 struct inode *inode = tree->private_data;
Josef Bacikc6100a42017-05-05 11:57:13 -040010633 unsigned long index = start >> PAGE_SHIFT;
10634 unsigned long end_index = end >> PAGE_SHIFT;
10635 struct page *page;
10636
10637 while (index <= end_index) {
10638 page = find_get_page(inode->i_mapping, index);
10639 ASSERT(page); /* Pages should be in the extent_io_tree */
10640 set_page_writeback(page);
10641 put_page(page);
10642 index++;
10643 }
10644}
10645
Omar Sandovaled46ff32016-11-03 10:28:14 -070010646#ifdef CONFIG_SWAP
10647/*
10648 * Add an entry indicating a block group or device which is pinned by a
10649 * swapfile. Returns 0 on success, 1 if there is already an entry for it, or a
10650 * negative errno on failure.
10651 */
10652static int btrfs_add_swapfile_pin(struct inode *inode, void *ptr,
10653 bool is_block_group)
10654{
10655 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
10656 struct btrfs_swapfile_pin *sp, *entry;
10657 struct rb_node **p;
10658 struct rb_node *parent = NULL;
10659
10660 sp = kmalloc(sizeof(*sp), GFP_NOFS);
10661 if (!sp)
10662 return -ENOMEM;
10663 sp->ptr = ptr;
10664 sp->inode = inode;
10665 sp->is_block_group = is_block_group;
10666
10667 spin_lock(&fs_info->swapfile_pins_lock);
10668 p = &fs_info->swapfile_pins.rb_node;
10669 while (*p) {
10670 parent = *p;
10671 entry = rb_entry(parent, struct btrfs_swapfile_pin, node);
10672 if (sp->ptr < entry->ptr ||
10673 (sp->ptr == entry->ptr && sp->inode < entry->inode)) {
10674 p = &(*p)->rb_left;
10675 } else if (sp->ptr > entry->ptr ||
10676 (sp->ptr == entry->ptr && sp->inode > entry->inode)) {
10677 p = &(*p)->rb_right;
10678 } else {
10679 spin_unlock(&fs_info->swapfile_pins_lock);
10680 kfree(sp);
10681 return 1;
10682 }
10683 }
10684 rb_link_node(&sp->node, parent, p);
10685 rb_insert_color(&sp->node, &fs_info->swapfile_pins);
10686 spin_unlock(&fs_info->swapfile_pins_lock);
10687 return 0;
10688}
10689
10690/* Free all of the entries pinned by this swapfile. */
10691static void btrfs_free_swapfile_pins(struct inode *inode)
10692{
10693 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
10694 struct btrfs_swapfile_pin *sp;
10695 struct rb_node *node, *next;
10696
10697 spin_lock(&fs_info->swapfile_pins_lock);
10698 node = rb_first(&fs_info->swapfile_pins);
10699 while (node) {
10700 next = rb_next(node);
10701 sp = rb_entry(node, struct btrfs_swapfile_pin, node);
10702 if (sp->inode == inode) {
10703 rb_erase(&sp->node, &fs_info->swapfile_pins);
10704 if (sp->is_block_group)
10705 btrfs_put_block_group(sp->ptr);
10706 kfree(sp);
10707 }
10708 node = next;
10709 }
10710 spin_unlock(&fs_info->swapfile_pins_lock);
10711}
10712
10713struct btrfs_swap_info {
10714 u64 start;
10715 u64 block_start;
10716 u64 block_len;
10717 u64 lowest_ppage;
10718 u64 highest_ppage;
10719 unsigned long nr_pages;
10720 int nr_extents;
10721};
10722
10723static int btrfs_add_swap_extent(struct swap_info_struct *sis,
10724 struct btrfs_swap_info *bsi)
10725{
10726 unsigned long nr_pages;
10727 u64 first_ppage, first_ppage_reported, next_ppage;
10728 int ret;
10729
10730 first_ppage = ALIGN(bsi->block_start, PAGE_SIZE) >> PAGE_SHIFT;
10731 next_ppage = ALIGN_DOWN(bsi->block_start + bsi->block_len,
10732 PAGE_SIZE) >> PAGE_SHIFT;
10733
10734 if (first_ppage >= next_ppage)
10735 return 0;
10736 nr_pages = next_ppage - first_ppage;
10737
10738 first_ppage_reported = first_ppage;
10739 if (bsi->start == 0)
10740 first_ppage_reported++;
10741 if (bsi->lowest_ppage > first_ppage_reported)
10742 bsi->lowest_ppage = first_ppage_reported;
10743 if (bsi->highest_ppage < (next_ppage - 1))
10744 bsi->highest_ppage = next_ppage - 1;
10745
10746 ret = add_swap_extent(sis, bsi->nr_pages, nr_pages, first_ppage);
10747 if (ret < 0)
10748 return ret;
10749 bsi->nr_extents += ret;
10750 bsi->nr_pages += nr_pages;
10751 return 0;
10752}
10753
10754static void btrfs_swap_deactivate(struct file *file)
10755{
10756 struct inode *inode = file_inode(file);
10757
10758 btrfs_free_swapfile_pins(inode);
10759 atomic_dec(&BTRFS_I(inode)->root->nr_swapfiles);
10760}
10761
10762static int btrfs_swap_activate(struct swap_info_struct *sis, struct file *file,
10763 sector_t *span)
10764{
10765 struct inode *inode = file_inode(file);
10766 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
10767 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
10768 struct extent_state *cached_state = NULL;
10769 struct extent_map *em = NULL;
10770 struct btrfs_device *device = NULL;
10771 struct btrfs_swap_info bsi = {
10772 .lowest_ppage = (sector_t)-1ULL,
10773 };
10774 int ret = 0;
10775 u64 isize;
10776 u64 start;
10777
10778 /*
10779 * If the swap file was just created, make sure delalloc is done. If the
10780 * file changes again after this, the user is doing something stupid and
10781 * we don't really care.
10782 */
10783 ret = btrfs_wait_ordered_range(inode, 0, (u64)-1);
10784 if (ret)
10785 return ret;
10786
10787 /*
10788 * The inode is locked, so these flags won't change after we check them.
10789 */
10790 if (BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS) {
10791 btrfs_warn(fs_info, "swapfile must not be compressed");
10792 return -EINVAL;
10793 }
10794 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW)) {
10795 btrfs_warn(fs_info, "swapfile must not be copy-on-write");
10796 return -EINVAL;
10797 }
10798 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
10799 btrfs_warn(fs_info, "swapfile must not be checksummed");
10800 return -EINVAL;
10801 }
10802
10803 /*
10804 * Balance or device remove/replace/resize can move stuff around from
10805 * under us. The EXCL_OP flag makes sure they aren't running/won't run
10806 * concurrently while we are mapping the swap extents, and
10807 * fs_info->swapfile_pins prevents them from running while the swap file
10808 * is active and moving the extents. Note that this also prevents a
10809 * concurrent device add which isn't actually necessary, but it's not
10810 * really worth the trouble to allow it.
10811 */
10812 if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) {
10813 btrfs_warn(fs_info,
10814 "cannot activate swapfile while exclusive operation is running");
10815 return -EBUSY;
10816 }
10817 /*
10818 * Snapshots can create extents which require COW even if NODATACOW is
10819 * set. We use this counter to prevent snapshots. We must increment it
10820 * before walking the extents because we don't want a concurrent
10821 * snapshot to run after we've already checked the extents.
10822 */
10823 atomic_inc(&BTRFS_I(inode)->root->nr_swapfiles);
10824
10825 isize = ALIGN_DOWN(inode->i_size, fs_info->sectorsize);
10826
10827 lock_extent_bits(io_tree, 0, isize - 1, &cached_state);
10828 start = 0;
10829 while (start < isize) {
10830 u64 logical_block_start, physical_block_start;
David Sterba32da53862019-10-29 19:20:18 +010010831 struct btrfs_block_group *bg;
Omar Sandovaled46ff32016-11-03 10:28:14 -070010832 u64 len = isize - start;
10833
10834 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len, 0);
10835 if (IS_ERR(em)) {
10836 ret = PTR_ERR(em);
10837 goto out;
10838 }
10839
10840 if (em->block_start == EXTENT_MAP_HOLE) {
10841 btrfs_warn(fs_info, "swapfile must not have holes");
10842 ret = -EINVAL;
10843 goto out;
10844 }
10845 if (em->block_start == EXTENT_MAP_INLINE) {
10846 /*
10847 * It's unlikely we'll ever actually find ourselves
10848 * here, as a file small enough to fit inline won't be
10849 * big enough to store more than the swap header, but in
10850 * case something changes in the future, let's catch it
10851 * here rather than later.
10852 */
10853 btrfs_warn(fs_info, "swapfile must not be inline");
10854 ret = -EINVAL;
10855 goto out;
10856 }
10857 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
10858 btrfs_warn(fs_info, "swapfile must not be compressed");
10859 ret = -EINVAL;
10860 goto out;
10861 }
10862
10863 logical_block_start = em->block_start + (start - em->start);
10864 len = min(len, em->len - (start - em->start));
10865 free_extent_map(em);
10866 em = NULL;
10867
10868 ret = can_nocow_extent(inode, start, &len, NULL, NULL, NULL);
10869 if (ret < 0) {
10870 goto out;
10871 } else if (ret) {
10872 ret = 0;
10873 } else {
10874 btrfs_warn(fs_info,
10875 "swapfile must not be copy-on-write");
10876 ret = -EINVAL;
10877 goto out;
10878 }
10879
10880 em = btrfs_get_chunk_map(fs_info, logical_block_start, len);
10881 if (IS_ERR(em)) {
10882 ret = PTR_ERR(em);
10883 goto out;
10884 }
10885
10886 if (em->map_lookup->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
10887 btrfs_warn(fs_info,
10888 "swapfile must have single data profile");
10889 ret = -EINVAL;
10890 goto out;
10891 }
10892
10893 if (device == NULL) {
10894 device = em->map_lookup->stripes[0].dev;
10895 ret = btrfs_add_swapfile_pin(inode, device, false);
10896 if (ret == 1)
10897 ret = 0;
10898 else if (ret)
10899 goto out;
10900 } else if (device != em->map_lookup->stripes[0].dev) {
10901 btrfs_warn(fs_info, "swapfile must be on one device");
10902 ret = -EINVAL;
10903 goto out;
10904 }
10905
10906 physical_block_start = (em->map_lookup->stripes[0].physical +
10907 (logical_block_start - em->start));
10908 len = min(len, em->len - (logical_block_start - em->start));
10909 free_extent_map(em);
10910 em = NULL;
10911
10912 bg = btrfs_lookup_block_group(fs_info, logical_block_start);
10913 if (!bg) {
10914 btrfs_warn(fs_info,
10915 "could not find block group containing swapfile");
10916 ret = -EINVAL;
10917 goto out;
10918 }
10919
10920 ret = btrfs_add_swapfile_pin(inode, bg, true);
10921 if (ret) {
10922 btrfs_put_block_group(bg);
10923 if (ret == 1)
10924 ret = 0;
10925 else
10926 goto out;
10927 }
10928
10929 if (bsi.block_len &&
10930 bsi.block_start + bsi.block_len == physical_block_start) {
10931 bsi.block_len += len;
10932 } else {
10933 if (bsi.block_len) {
10934 ret = btrfs_add_swap_extent(sis, &bsi);
10935 if (ret)
10936 goto out;
10937 }
10938 bsi.start = start;
10939 bsi.block_start = physical_block_start;
10940 bsi.block_len = len;
10941 }
10942
10943 start += len;
10944 }
10945
10946 if (bsi.block_len)
10947 ret = btrfs_add_swap_extent(sis, &bsi);
10948
10949out:
10950 if (!IS_ERR_OR_NULL(em))
10951 free_extent_map(em);
10952
10953 unlock_extent_cached(io_tree, 0, isize - 1, &cached_state);
10954
10955 if (ret)
10956 btrfs_swap_deactivate(file);
10957
10958 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
10959
10960 if (ret)
10961 return ret;
10962
10963 if (device)
10964 sis->bdev = device->bdev;
10965 *span = bsi.highest_ppage - bsi.lowest_ppage + 1;
10966 sis->max = bsi.nr_pages;
10967 sis->pages = bsi.nr_pages - 1;
10968 sis->highest_bit = bsi.nr_pages - 1;
10969 return bsi.nr_extents;
10970}
10971#else
10972static void btrfs_swap_deactivate(struct file *file)
10973{
10974}
10975
10976static int btrfs_swap_activate(struct swap_info_struct *sis, struct file *file,
10977 sector_t *span)
10978{
10979 return -EOPNOTSUPP;
10980}
10981#endif
10982
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010983static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -050010984 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -040010985 .lookup = btrfs_lookup,
10986 .create = btrfs_create,
10987 .unlink = btrfs_unlink,
10988 .link = btrfs_link,
10989 .mkdir = btrfs_mkdir,
10990 .rmdir = btrfs_rmdir,
Miklos Szeredi2773bf02016-09-27 11:03:58 +020010991 .rename = btrfs_rename2,
Chris Mason39279cc2007-06-12 06:35:45 -040010992 .symlink = btrfs_symlink,
10993 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -040010994 .mknod = btrfs_mknod,
Josef Bacik5103e942007-11-16 11:45:54 -050010995 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010996 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010997 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010998 .set_acl = btrfs_set_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070010999 .update_time = btrfs_update_time,
Filipe Mananaef3b9af2014-04-27 20:40:45 +010011000 .tmpfile = btrfs_tmpfile,
Chris Mason39279cc2007-06-12 06:35:45 -040011001};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070011002static const struct inode_operations btrfs_dir_ro_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040011003 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -050011004 .permission = btrfs_permission,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070011005 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040011006};
Yan, Zheng76dda932009-09-21 16:00:26 -040011007
Alexey Dobriyan828c0952009-10-01 15:43:56 -070011008static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040011009 .llseek = generic_file_llseek,
11010 .read = generic_read_dir,
Omar Sandoval02dbfc92016-05-20 13:50:33 -070011011 .iterate_shared = btrfs_real_readdir,
Josef Bacik23b5ec72017-07-24 15:14:25 -040011012 .open = btrfs_opendir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -040011013 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040011014#ifdef CONFIG_COMPAT
Luke Dashjr4c63c242015-10-29 08:22:21 +000011015 .compat_ioctl = btrfs_compat_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040011016#endif
Sage Weil6bf13c02008-06-10 10:07:39 -040011017 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -040011018 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -040011019};
11020
David Sterba20e55062015-11-19 11:42:28 +010011021static const struct extent_io_ops btrfs_extent_io_ops = {
David Sterba4d53ddd2017-02-17 15:27:44 +010011022 /* mandatory callbacks */
Chris Mason065631f2008-02-20 12:07:25 -050011023 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -040011024 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
11025};
11026
Chris Mason35054392009-01-21 13:11:13 -050011027/*
11028 * btrfs doesn't support the bmap operation because swapfiles
11029 * use bmap to make a mapping of extents in the file. They assume
11030 * these extents won't change over the life of the file and they
11031 * use the bmap result to do IO directly to the drive.
11032 *
11033 * the btrfs bmap call would return logical addresses that aren't
11034 * suitable for IO and they also will change frequently as COW
11035 * operations happen. So, swapfile + btrfs == corruption.
11036 *
11037 * For now we're avoiding this by dropping bmap.
11038 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -070011039static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -040011040 .readpage = btrfs_readpage,
11041 .writepage = btrfs_writepage,
Chris Masonb293f02e2007-11-01 19:45:34 -040011042 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -050011043 .readpages = btrfs_readpages,
Chris Mason16432982008-04-10 10:23:21 -040011044 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -040011045 .invalidatepage = btrfs_invalidatepage,
11046 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -040011047 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +020011048 .error_remove_page = generic_error_remove_page,
Omar Sandovaled46ff32016-11-03 10:28:14 -070011049 .swap_activate = btrfs_swap_activate,
11050 .swap_deactivate = btrfs_swap_deactivate,
Chris Mason39279cc2007-06-12 06:35:45 -040011051};
11052
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070011053static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040011054 .getattr = btrfs_getattr,
11055 .setattr = btrfs_setattr,
Josef Bacik5103e942007-11-16 11:45:54 -050011056 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050011057 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -050011058 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020011059 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080011060 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040011061 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040011062};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070011063static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -040011064 .getattr = btrfs_getattr,
11065 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050011066 .permission = btrfs_permission,
Josef Bacik33268ea2008-07-24 12:16:36 -040011067 .listxattr = btrfs_listxattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020011068 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080011069 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040011070 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -040011071};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070011072static const struct inode_operations btrfs_symlink_inode_operations = {
Al Viro6b255392015-11-17 10:20:54 -050011073 .get_link = page_get_link,
Li Zefanf2095612010-11-19 02:05:24 +000011074 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -050011075 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050011076 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -050011077 .listxattr = btrfs_listxattr,
Josef Bacike41f9412012-03-26 09:46:47 -040011078 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040011079};
Yan, Zheng76dda932009-09-21 16:00:26 -040011080
Alexey Dobriyan82d339d2009-10-09 09:54:36 -040011081const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -040011082 .d_delete = btrfs_dentry_delete,
11083};