blob: c6dc4dd16cf77535b0745a4c0e133446b2e9c286 [file] [log] [blame]
David Sterbac1d7c512018-04-03 19:23:33 +02001// SPDX-License-Identifier: GPL-2.0
Chris Mason6cbd5572007-06-12 09:07:21 -04002/*
3 * Copyright (C) 2007 Oracle. All rights reserved.
Chris Mason6cbd5572007-06-12 09:07:21 -04004 */
5
Chris Mason8f18cf12008-04-25 16:53:30 -04006#include <linux/kernel.h>
Chris Mason065631f2008-02-20 12:07:25 -05007#include <linux/bio.h>
Chris Mason39279cc2007-06-12 06:35:45 -04008#include <linux/buffer_head.h>
Sage Weilf2eb0a22008-05-02 14:43:14 -04009#include <linux/file.h>
Chris Mason39279cc2007-06-12 06:35:45 -040010#include <linux/fs.h>
11#include <linux/pagemap.h>
12#include <linux/highmem.h>
13#include <linux/time.h>
14#include <linux/init.h>
15#include <linux/string.h>
Chris Mason39279cc2007-06-12 06:35:45 -040016#include <linux/backing-dev.h>
Chris Mason39279cc2007-06-12 06:35:45 -040017#include <linux/writeback.h>
Chris Mason39279cc2007-06-12 06:35:45 -040018#include <linux/compat.h>
Josef Bacik5103e942007-11-16 11:45:54 -050019#include <linux/xattr.h>
Josef Bacik33268ea2008-07-24 12:16:36 -040020#include <linux/posix_acl.h>
Yan Zhengd899e052008-10-30 14:25:28 -040021#include <linux/falloc.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090022#include <linux/slab.h>
David Sterba7a36dde2011-05-06 15:33:15 +020023#include <linux/ratelimit.h>
Filipe Brandenburger55e301f2013-01-29 06:04:50 +000024#include <linux/btrfs.h>
David Woodhouse53b381b2013-01-29 18:40:14 -050025#include <linux/blkdev.h>
Josef Bacikf23b5a52013-06-19 10:16:26 -040026#include <linux/posix_acl_xattr.h>
Christoph Hellwige2e40f22015-02-22 08:58:50 -080027#include <linux/uio.h>
Josef Bacik69fe2d72017-10-19 14:15:57 -040028#include <linux/magic.h>
Jeff Laytonae5e1652018-01-29 06:41:30 -050029#include <linux/iversion.h>
Omar Sandovaled46ff32016-11-03 10:28:14 -070030#include <linux/swap.h>
Nikolay Borisovb1c16ac2019-04-01 11:29:57 +030031#include <linux/sched/mm.h>
David Sterba92d32172018-04-16 21:10:14 +020032#include <asm/unaligned.h>
David Sterba602cbe92019-08-21 18:48:25 +020033#include "misc.h"
Chris Mason39279cc2007-06-12 06:35:45 -040034#include "ctree.h"
35#include "disk-io.h"
36#include "transaction.h"
37#include "btrfs_inode.h"
Chris Mason39279cc2007-06-12 06:35:45 -040038#include "print-tree.h"
Chris Masone6dcd2d2008-07-17 12:53:50 -040039#include "ordered-data.h"
Christoph Hellwig95819c02008-08-28 06:21:17 -040040#include "xattr.h"
Chris Masone02119d2008-09-05 16:13:11 -040041#include "tree-log.h"
Jan Schmidt4a54c8c2011-07-22 15:41:52 +020042#include "volumes.h"
Chris Masonc8b97812008-10-29 14:49:59 -040043#include "compression.h"
Chris Masonb4ce94d2009-02-04 09:25:08 -050044#include "locking.h"
Josef Bacikdc89e982011-01-28 17:05:48 -050045#include "free-space-cache.h"
Li Zefan581bb052011-04-20 10:06:11 +080046#include "inode-map.h"
Liu Bo38c227d2013-01-29 03:18:40 +000047#include "backref.h"
Filipe David Borba Manana63541922014-01-07 11:47:46 +000048#include "props.h"
Dongsheng Yang31193212014-12-12 16:44:35 +080049#include "qgroup.h"
Josef Bacik86736342019-06-19 15:12:00 -040050#include "delalloc-space.h"
Josef Bacikaac00232019-06-20 15:37:44 -040051#include "block-group.h"
Chris Mason39279cc2007-06-12 06:35:45 -040052
53struct btrfs_iget_args {
Chris Mason90d3e592014-01-09 17:28:00 -080054 struct btrfs_key *location;
Chris Mason39279cc2007-06-12 06:35:45 -040055 struct btrfs_root *root;
56};
57
Filipe Mananaf28a4922015-12-08 19:23:20 +000058struct btrfs_dio_data {
Filipe Mananaf28a4922015-12-08 19:23:20 +000059 u64 reserve;
60 u64 unsubmitted_oe_range_start;
61 u64 unsubmitted_oe_range_end;
Liu Bo4aaedfb2016-12-14 22:36:05 -080062 int overwrite;
Filipe Mananaf28a4922015-12-08 19:23:20 +000063};
64
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070065static const struct inode_operations btrfs_dir_inode_operations;
66static const struct inode_operations btrfs_symlink_inode_operations;
67static const struct inode_operations btrfs_dir_ro_inode_operations;
68static const struct inode_operations btrfs_special_inode_operations;
69static const struct inode_operations btrfs_file_inode_operations;
Alexey Dobriyan7f094102009-09-21 17:01:10 -070070static const struct address_space_operations btrfs_aops;
Alexey Dobriyan828c0952009-10-01 15:43:56 -070071static const struct file_operations btrfs_dir_file_operations;
David Sterba20e55062015-11-19 11:42:28 +010072static const struct extent_io_ops btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -040073
74static struct kmem_cache *btrfs_inode_cachep;
75struct kmem_cache *btrfs_trans_handle_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040076struct kmem_cache *btrfs_path_cachep;
Josef Bacikdc89e982011-01-28 17:05:48 -050077struct kmem_cache *btrfs_free_space_cachep;
Christophe Leroy3acd4852019-08-21 15:05:55 +000078struct kmem_cache *btrfs_free_space_bitmap_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040079
Eric Sandeen3972f262013-01-12 02:57:22 +000080static int btrfs_setsize(struct inode *inode, struct iattr *attr);
Filipe Manana213e8c52018-02-06 20:40:31 +000081static int btrfs_truncate(struct inode *inode, bool skip_writeback);
Josef Bacik5fd02042012-05-02 14:00:54 -040082static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent);
Chris Mason771ed682008-11-06 22:02:51 -050083static noinline int cow_file_range(struct inode *inode,
84 struct page *locked_page,
Nikolay Borisov74e91942019-07-17 16:18:16 +030085 u64 start, u64 end, int *page_started,
Nikolay Borisov330a5822019-07-17 16:18:17 +030086 unsigned long *nr_written, int unlock);
Liu Bo6f9994d2017-01-31 07:50:22 -080087static struct extent_map *create_io_em(struct inode *inode, u64 start, u64 len,
88 u64 orig_start, u64 block_start,
89 u64 block_len, u64 orig_block_len,
90 u64 ram_bytes, int compress_type,
91 int type);
Josef Bacik7b128762008-07-24 12:17:14 -040092
Qu Wenruo524272602017-03-08 10:25:52 +080093static void __endio_write_update_ordered(struct inode *inode,
94 const u64 offset, const u64 bytes,
95 const bool uptodate);
96
97/*
98 * Cleanup all submitted ordered extents in specified range to handle errors
Andrea Gelmini52042d82018-11-28 12:05:13 +010099 * from the btrfs_run_delalloc_range() callback.
Qu Wenruo524272602017-03-08 10:25:52 +0800100 *
101 * NOTE: caller must ensure that when an error happens, it can not call
102 * extent_clear_unlock_delalloc() to clear both the bits EXTENT_DO_ACCOUNTING
103 * and EXTENT_DELALLOC simultaneously, because that causes the reserved metadata
104 * to be released, which we want to happen only when finishing the ordered
Nikolay Borisovd1051d62018-11-21 17:10:52 +0200105 * extent (btrfs_finish_ordered_io()).
Qu Wenruo524272602017-03-08 10:25:52 +0800106 */
107static inline void btrfs_cleanup_ordered_extents(struct inode *inode,
Nikolay Borisovd1051d62018-11-21 17:10:52 +0200108 struct page *locked_page,
109 u64 offset, u64 bytes)
Qu Wenruo524272602017-03-08 10:25:52 +0800110{
Naohiro Aota63d71450c2017-09-01 17:58:47 +0900111 unsigned long index = offset >> PAGE_SHIFT;
112 unsigned long end_index = (offset + bytes - 1) >> PAGE_SHIFT;
Nikolay Borisovd1051d62018-11-21 17:10:52 +0200113 u64 page_start = page_offset(locked_page);
114 u64 page_end = page_start + PAGE_SIZE - 1;
115
Naohiro Aota63d71450c2017-09-01 17:58:47 +0900116 struct page *page;
117
118 while (index <= end_index) {
119 page = find_get_page(inode->i_mapping, index);
120 index++;
121 if (!page)
122 continue;
123 ClearPagePrivate2(page);
124 put_page(page);
125 }
Nikolay Borisovd1051d62018-11-21 17:10:52 +0200126
127 /*
128 * In case this page belongs to the delalloc range being instantiated
129 * then skip it, since the first page of a range is going to be
130 * properly cleaned up by the caller of run_delalloc_range
131 */
132 if (page_start >= offset && page_end <= (offset + bytes - 1)) {
133 offset += PAGE_SIZE;
134 bytes -= PAGE_SIZE;
135 }
136
137 return __endio_write_update_ordered(inode, offset, bytes, false);
Qu Wenruo524272602017-03-08 10:25:52 +0800138}
139
Eric Sandeen48a3b632013-04-25 20:41:01 +0000140static int btrfs_dirty_inode(struct inode *inode);
Chris Mason39279cc2007-06-12 06:35:45 -0400141
Josef Bacik6a3891c2015-03-16 17:38:52 -0400142#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
143void btrfs_test_inode_set_ops(struct inode *inode)
144{
145 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
146}
147#endif
148
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000149static int btrfs_init_inode_security(struct btrfs_trans_handle *trans,
Eric Paris2a7dba32011-02-01 11:05:39 -0500150 struct inode *inode, struct inode *dir,
151 const struct qstr *qstr)
Jim Owens0279b4c2009-02-04 09:29:13 -0500152{
153 int err;
154
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000155 err = btrfs_init_acl(trans, inode, dir);
Jim Owens0279b4c2009-02-04 09:29:13 -0500156 if (!err)
Eric Paris2a7dba32011-02-01 11:05:39 -0500157 err = btrfs_xattr_security_init(trans, inode, dir, qstr);
Jim Owens0279b4c2009-02-04 09:29:13 -0500158 return err;
159}
160
Chris Masond352ac62008-09-29 15:18:18 -0400161/*
Chris Masonc8b97812008-10-29 14:49:59 -0400162 * this does all the hard work for inserting an inline extent into
163 * the btree. The caller should have done a btrfs_drop_extents so that
164 * no overlapping inline items exist in the btree
165 */
Chris Mason40f76582014-05-21 13:35:51 -0700166static int insert_inline_extent(struct btrfs_trans_handle *trans,
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000167 struct btrfs_path *path, int extent_inserted,
Chris Masonc8b97812008-10-29 14:49:59 -0400168 struct btrfs_root *root, struct inode *inode,
169 u64 start, size_t size, size_t compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000170 int compress_type,
Chris Masonc8b97812008-10-29 14:49:59 -0400171 struct page **compressed_pages)
172{
Chris Masonc8b97812008-10-29 14:49:59 -0400173 struct extent_buffer *leaf;
174 struct page *page = NULL;
175 char *kaddr;
176 unsigned long ptr;
177 struct btrfs_file_extent_item *ei;
Chris Masonc8b97812008-10-29 14:49:59 -0400178 int ret;
179 size_t cur_size = size;
Chris Masonc8b97812008-10-29 14:49:59 -0400180 unsigned long offset;
Chris Masonc8b97812008-10-29 14:49:59 -0400181
Jia-Ju Bai982f1f5d2019-07-27 16:51:13 +0800182 ASSERT((compressed_size > 0 && compressed_pages) ||
183 (compressed_size == 0 && !compressed_pages));
184
Li Zefanfe3f5662011-03-28 08:30:38 +0000185 if (compressed_size && compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400186 cur_size = compressed_size;
Chris Masonc8b97812008-10-29 14:49:59 -0400187
Chris Masonc8b97812008-10-29 14:49:59 -0400188 inode_add_bytes(inode, size);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000189
190 if (!extent_inserted) {
191 struct btrfs_key key;
192 size_t datasize;
193
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +0200194 key.objectid = btrfs_ino(BTRFS_I(inode));
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000195 key.offset = start;
David Sterba962a2982014-06-04 18:41:45 +0200196 key.type = BTRFS_EXTENT_DATA_KEY;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000197
198 datasize = btrfs_file_extent_calc_inline_size(cur_size);
199 path->leave_spinning = 1;
200 ret = btrfs_insert_empty_item(trans, root, path, &key,
201 datasize);
David Sterba79b4f4c2017-06-15 19:09:51 +0200202 if (ret)
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000203 goto fail;
Chris Masonc8b97812008-10-29 14:49:59 -0400204 }
205 leaf = path->nodes[0];
206 ei = btrfs_item_ptr(leaf, path->slots[0],
207 struct btrfs_file_extent_item);
208 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
209 btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
210 btrfs_set_file_extent_encryption(leaf, ei, 0);
211 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
212 btrfs_set_file_extent_ram_bytes(leaf, ei, size);
213 ptr = btrfs_file_extent_inline_start(ei);
214
Li Zefan261507a02010-12-17 14:21:50 +0800215 if (compress_type != BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -0400216 struct page *cpage;
217 int i = 0;
Chris Masond3977122009-01-05 21:25:51 -0500218 while (compressed_size > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -0400219 cpage = compressed_pages[i];
Chris Mason5b050f02008-11-11 09:34:41 -0500220 cur_size = min_t(unsigned long, compressed_size,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300221 PAGE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400222
Cong Wang7ac687d2011-11-25 23:14:28 +0800223 kaddr = kmap_atomic(cpage);
Chris Masonc8b97812008-10-29 14:49:59 -0400224 write_extent_buffer(leaf, kaddr, ptr, cur_size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800225 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400226
227 i++;
228 ptr += cur_size;
229 compressed_size -= cur_size;
230 }
231 btrfs_set_file_extent_compression(leaf, ei,
Li Zefan261507a02010-12-17 14:21:50 +0800232 compress_type);
Chris Masonc8b97812008-10-29 14:49:59 -0400233 } else {
234 page = find_get_page(inode->i_mapping,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300235 start >> PAGE_SHIFT);
Chris Masonc8b97812008-10-29 14:49:59 -0400236 btrfs_set_file_extent_compression(leaf, ei, 0);
Cong Wang7ac687d2011-11-25 23:14:28 +0800237 kaddr = kmap_atomic(page);
Johannes Thumshirn70730172018-12-05 15:23:03 +0100238 offset = offset_in_page(start);
Chris Masonc8b97812008-10-29 14:49:59 -0400239 write_extent_buffer(leaf, kaddr + offset, ptr, size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800240 kunmap_atomic(kaddr);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300241 put_page(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400242 }
243 btrfs_mark_buffer_dirty(leaf);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000244 btrfs_release_path(path);
Chris Masonc8b97812008-10-29 14:49:59 -0400245
Yan, Zhengc2167752009-11-12 09:34:21 +0000246 /*
247 * we're an inline extent, so nobody can
248 * extend the file past i_size without locking
249 * a page we already have locked.
250 *
251 * We must do any isize and inode updates
252 * before we unlock the pages. Otherwise we
253 * could end up racing with unlink.
254 */
Chris Masonc8b97812008-10-29 14:49:59 -0400255 BTRFS_I(inode)->disk_i_size = inode->i_size;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100256 ret = btrfs_update_inode(trans, root, inode);
Yan, Zhengc2167752009-11-12 09:34:21 +0000257
Chris Masonc8b97812008-10-29 14:49:59 -0400258fail:
David Sterba79b4f4c2017-06-15 19:09:51 +0200259 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400260}
261
262
263/*
264 * conditionally insert an inline extent into the file. This
265 * does the checks required to make sure the data is small enough
266 * to fit as an inline extent.
267 */
Nikolay Borisovd02c0e22018-03-02 09:43:15 +0200268static noinline int cow_file_range_inline(struct inode *inode, u64 start,
Josef Bacik00361582013-08-14 14:02:47 -0400269 u64 end, size_t compressed_size,
270 int compress_type,
271 struct page **compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400272{
Nikolay Borisovd02c0e22018-03-02 09:43:15 +0200273 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400274 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik00361582013-08-14 14:02:47 -0400275 struct btrfs_trans_handle *trans;
Chris Masonc8b97812008-10-29 14:49:59 -0400276 u64 isize = i_size_read(inode);
277 u64 actual_end = min(end + 1, isize);
278 u64 inline_len = actual_end - start;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400279 u64 aligned_end = ALIGN(end, fs_info->sectorsize);
Chris Masonc8b97812008-10-29 14:49:59 -0400280 u64 data_len = inline_len;
281 int ret;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000282 struct btrfs_path *path;
283 int extent_inserted = 0;
284 u32 extent_item_size;
Chris Masonc8b97812008-10-29 14:49:59 -0400285
286 if (compressed_size)
287 data_len = compressed_size;
288
289 if (start > 0 ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400290 actual_end > fs_info->sectorsize ||
291 data_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info) ||
Chris Masonc8b97812008-10-29 14:49:59 -0400292 (!compressed_size &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400293 (actual_end & (fs_info->sectorsize - 1)) == 0) ||
Chris Masonc8b97812008-10-29 14:49:59 -0400294 end + 1 < isize ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400295 data_len > fs_info->max_inline) {
Chris Masonc8b97812008-10-29 14:49:59 -0400296 return 1;
297 }
298
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000299 path = btrfs_alloc_path();
300 if (!path)
301 return -ENOMEM;
302
Josef Bacik00361582013-08-14 14:02:47 -0400303 trans = btrfs_join_transaction(root);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000304 if (IS_ERR(trans)) {
305 btrfs_free_path(path);
Josef Bacik00361582013-08-14 14:02:47 -0400306 return PTR_ERR(trans);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000307 }
Josef Bacik69fe2d72017-10-19 14:15:57 -0400308 trans->block_rsv = &BTRFS_I(inode)->block_rsv;
Josef Bacik00361582013-08-14 14:02:47 -0400309
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000310 if (compressed_size && compressed_pages)
311 extent_item_size = btrfs_file_extent_calc_inline_size(
312 compressed_size);
313 else
314 extent_item_size = btrfs_file_extent_calc_inline_size(
315 inline_len);
316
317 ret = __btrfs_drop_extents(trans, root, inode, path,
318 start, aligned_end, NULL,
319 1, 1, extent_item_size, &extent_inserted);
Josef Bacik00361582013-08-14 14:02:47 -0400320 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -0400321 btrfs_abort_transaction(trans, ret);
Josef Bacik00361582013-08-14 14:02:47 -0400322 goto out;
323 }
Chris Masonc8b97812008-10-29 14:49:59 -0400324
325 if (isize > actual_end)
326 inline_len = min_t(u64, isize, actual_end);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000327 ret = insert_inline_extent(trans, path, extent_inserted,
328 root, inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -0400329 inline_len, compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000330 compress_type, compressed_pages);
Josef Bacik2adcac12012-05-23 16:10:14 -0400331 if (ret && ret != -ENOSPC) {
Jeff Mahoney66642832016-06-10 18:19:25 -0400332 btrfs_abort_transaction(trans, ret);
Josef Bacik00361582013-08-14 14:02:47 -0400333 goto out;
Josef Bacik2adcac12012-05-23 16:10:14 -0400334 } else if (ret == -ENOSPC) {
Josef Bacik00361582013-08-14 14:02:47 -0400335 ret = 1;
336 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100337 }
Josef Bacik2adcac12012-05-23 16:10:14 -0400338
Josef Bacikbdc20e62013-02-28 13:23:38 -0500339 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +0200340 btrfs_drop_extent_cache(BTRFS_I(inode), start, aligned_end - 1, 0);
Josef Bacik00361582013-08-14 14:02:47 -0400341out:
Qu Wenruo94ed9382015-09-08 17:25:56 +0800342 /*
343 * Don't forget to free the reserved space, as for inlined extent
344 * it won't count as data extent, free them directly here.
345 * And at reserve time, it's always aligned to page size, so
346 * just free one page here.
347 */
Qu Wenruobc42bda2017-02-27 15:10:39 +0800348 btrfs_qgroup_free_data(inode, NULL, 0, PAGE_SIZE);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000349 btrfs_free_path(path);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -0400350 btrfs_end_transaction(trans);
Josef Bacik00361582013-08-14 14:02:47 -0400351 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400352}
353
Chris Mason771ed682008-11-06 22:02:51 -0500354struct async_extent {
355 u64 start;
356 u64 ram_size;
357 u64 compressed_size;
358 struct page **pages;
359 unsigned long nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800360 int compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500361 struct list_head list;
362};
363
Nikolay Borisov97db1202019-03-12 17:20:24 +0200364struct async_chunk {
Chris Mason771ed682008-11-06 22:02:51 -0500365 struct inode *inode;
Chris Mason771ed682008-11-06 22:02:51 -0500366 struct page *locked_page;
367 u64 start;
368 u64 end;
Liu Bof82b7352017-10-23 23:18:16 -0600369 unsigned int write_flags;
Chris Mason771ed682008-11-06 22:02:51 -0500370 struct list_head extents;
371 struct btrfs_work work;
Nikolay Borisov97db1202019-03-12 17:20:24 +0200372 atomic_t *pending;
Chris Mason771ed682008-11-06 22:02:51 -0500373};
374
Nikolay Borisov97db1202019-03-12 17:20:24 +0200375struct async_cow {
376 /* Number of chunks in flight; must be first in the structure */
377 atomic_t num_chunks;
378 struct async_chunk chunks[];
379};
380
381static noinline int add_async_extent(struct async_chunk *cow,
Chris Mason771ed682008-11-06 22:02:51 -0500382 u64 start, u64 ram_size,
383 u64 compressed_size,
384 struct page **pages,
Li Zefan261507a02010-12-17 14:21:50 +0800385 unsigned long nr_pages,
386 int compress_type)
Chris Mason771ed682008-11-06 22:02:51 -0500387{
388 struct async_extent *async_extent;
389
390 async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100391 BUG_ON(!async_extent); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -0500392 async_extent->start = start;
393 async_extent->ram_size = ram_size;
394 async_extent->compressed_size = compressed_size;
395 async_extent->pages = pages;
396 async_extent->nr_pages = nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800397 async_extent->compress_type = compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500398 list_add_tail(&async_extent->list, &cow->extents);
399 return 0;
400}
401
Qu Wenruo42c16da2019-07-01 05:12:46 +0000402/*
403 * Check if the inode has flags compatible with compression
404 */
405static inline bool inode_can_compress(struct inode *inode)
406{
407 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW ||
408 BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
409 return false;
410 return true;
411}
412
413/*
414 * Check if the inode needs to be submitted to compression, based on mount
415 * options, defragmentation, properties or heuristics.
416 */
Timofey Titovetsc2fcdcd2017-07-17 16:52:58 +0300417static inline int inode_need_compress(struct inode *inode, u64 start, u64 end)
Wang Shilongf79707b2014-07-17 11:44:09 +0800418{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400419 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Wang Shilongf79707b2014-07-17 11:44:09 +0800420
Qu Wenruo42c16da2019-07-01 05:12:46 +0000421 if (!inode_can_compress(inode)) {
422 WARN(IS_ENABLED(CONFIG_BTRFS_DEBUG),
423 KERN_ERR "BTRFS: unexpected compression for ino %llu\n",
424 btrfs_ino(BTRFS_I(inode)));
425 return 0;
426 }
Wang Shilongf79707b2014-07-17 11:44:09 +0800427 /* force compress */
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400428 if (btrfs_test_opt(fs_info, FORCE_COMPRESS))
Wang Shilongf79707b2014-07-17 11:44:09 +0800429 return 1;
David Sterbaeec63c62017-07-17 19:41:31 +0200430 /* defrag ioctl */
431 if (BTRFS_I(inode)->defrag_compress)
432 return 1;
Wang Shilongf79707b2014-07-17 11:44:09 +0800433 /* bad compression ratios */
434 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS)
435 return 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400436 if (btrfs_test_opt(fs_info, COMPRESS) ||
Wang Shilongf79707b2014-07-17 11:44:09 +0800437 BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS ||
David Sterbab52aa8c2017-07-17 19:17:20 +0200438 BTRFS_I(inode)->prop_compress)
Timofey Titovetsc2fcdcd2017-07-17 16:52:58 +0300439 return btrfs_compress_heuristic(inode, start, end);
Wang Shilongf79707b2014-07-17 11:44:09 +0800440 return 0;
441}
442
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200443static inline void inode_should_defrag(struct btrfs_inode *inode,
Anand Jain26d30f82016-12-19 19:09:06 +0800444 u64 start, u64 end, u64 num_bytes, u64 small_write)
445{
446 /* If this is a small write inside eof, kick off a defrag */
447 if (num_bytes < small_write &&
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200448 (start > 0 || end + 1 < inode->disk_i_size))
Anand Jain26d30f82016-12-19 19:09:06 +0800449 btrfs_add_inode_defrag(NULL, inode);
450}
451
Chris Masonc8b97812008-10-29 14:49:59 -0400452/*
Chris Mason771ed682008-11-06 22:02:51 -0500453 * we create compressed extents in two phases. The first
454 * phase compresses a range of pages that have already been
455 * locked (both pages and state bits are locked).
Chris Masonc8b97812008-10-29 14:49:59 -0400456 *
Chris Mason771ed682008-11-06 22:02:51 -0500457 * This is done inside an ordered work queue, and the compression
458 * is spread across many cpus. The actual IO submission is step
459 * two, and the ordered work queue takes care of making sure that
460 * happens in the same order things were put onto the queue by
461 * writepages and friends.
Chris Masonc8b97812008-10-29 14:49:59 -0400462 *
Chris Mason771ed682008-11-06 22:02:51 -0500463 * If this code finds it can't get good compression, it puts an
464 * entry onto the work queue to write the uncompressed bytes. This
465 * makes sure that both compressed inodes and uncompressed inodes
Artem Bityutskiyb2570312012-07-25 18:12:06 +0300466 * are written in the same order that the flusher thread sent them
467 * down.
Chris Masond352ac62008-09-29 15:18:18 -0400468 */
Nikolay Borisovac3e9932019-07-17 14:41:44 +0300469static noinline int compress_file_range(struct async_chunk *async_chunk)
Chris Masonb888db22007-08-27 16:49:44 -0400470{
Nikolay Borisov1368c6d2019-03-12 17:20:27 +0200471 struct inode *inode = async_chunk->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400472 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400473 u64 blocksize = fs_info->sectorsize;
Nikolay Borisov1368c6d2019-03-12 17:20:27 +0200474 u64 start = async_chunk->start;
475 u64 end = async_chunk->end;
Chris Masonc8b97812008-10-29 14:49:59 -0400476 u64 actual_end;
Josef Bacikd98da492019-10-11 09:03:54 -0400477 u64 i_size;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400478 int ret = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400479 struct page **pages = NULL;
480 unsigned long nr_pages;
Chris Masonc8b97812008-10-29 14:49:59 -0400481 unsigned long total_compressed = 0;
482 unsigned long total_in = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400483 int i;
484 int will_compress;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400485 int compress_type = fs_info->compress_type;
Nikolay Borisovac3e9932019-07-17 14:41:44 +0300486 int compressed_extents = 0;
Chris Mason4adaa612013-03-26 13:07:00 -0400487 int redirty = 0;
Chris Masonb888db22007-08-27 16:49:44 -0400488
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200489 inode_should_defrag(BTRFS_I(inode), start, end, end - start + 1,
490 SZ_16K);
Chris Mason4cb53002011-05-24 15:35:30 -0400491
Josef Bacikd98da492019-10-11 09:03:54 -0400492 /*
493 * We need to save i_size before now because it could change in between
494 * us evaluating the size and assigning it. This is because we lock and
495 * unlock the page in truncate and fallocate, and then modify the i_size
496 * later on.
497 *
498 * The barriers are to emulate READ_ONCE, remove that once i_size_read
499 * does that for us.
500 */
501 barrier();
502 i_size = i_size_read(inode);
503 barrier();
504 actual_end = min_t(u64, i_size, end + 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400505again:
506 will_compress = 0;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300507 nr_pages = (end >> PAGE_SHIFT) - (start >> PAGE_SHIFT) + 1;
David Sterba069eac72017-02-14 19:36:54 +0100508 BUILD_BUG_ON((BTRFS_MAX_COMPRESSED % PAGE_SIZE) != 0);
509 nr_pages = min_t(unsigned long, nr_pages,
510 BTRFS_MAX_COMPRESSED / PAGE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400511
Chris Masonf03d9301f2009-02-04 09:31:06 -0500512 /*
513 * we don't want to send crud past the end of i_size through
514 * compression, that's just a waste of CPU time. So, if the
515 * end of the file is before the start of our current
516 * requested range of bytes, we bail out to the uncompressed
517 * cleanup code that can deal with all of this.
518 *
519 * It isn't really the fastest way to fix things, but this is a
520 * very uncommon corner.
521 */
522 if (actual_end <= start)
523 goto cleanup_and_bail_uncompressed;
524
Chris Masonc8b97812008-10-29 14:49:59 -0400525 total_compressed = actual_end - start;
526
Shilong Wang4bcbb332014-10-07 18:44:35 -0400527 /*
528 * skip compression for a small file range(<=blocksize) that
Nicholas D Steeves01327612016-05-19 21:18:45 -0400529 * isn't an inline extent, since it doesn't save disk space at all.
Shilong Wang4bcbb332014-10-07 18:44:35 -0400530 */
531 if (total_compressed <= blocksize &&
532 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
533 goto cleanup_and_bail_uncompressed;
534
David Sterba069eac72017-02-14 19:36:54 +0100535 total_compressed = min_t(unsigned long, total_compressed,
536 BTRFS_MAX_UNCOMPRESSED);
Chris Masonc8b97812008-10-29 14:49:59 -0400537 total_in = 0;
538 ret = 0;
Chris Masondb945352007-10-15 16:15:53 -0400539
Chris Mason771ed682008-11-06 22:02:51 -0500540 /*
541 * we do compression for mount -o compress and when the
542 * inode has not been flagged as nocompress. This flag can
543 * change at any time if we discover bad compression ratios.
Chris Masonc8b97812008-10-29 14:49:59 -0400544 */
Timofey Titovetsc2fcdcd2017-07-17 16:52:58 +0300545 if (inode_need_compress(inode, start, end)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400546 WARN_ON(pages);
David Sterba31e818f2015-02-20 18:00:26 +0100547 pages = kcalloc(nr_pages, sizeof(struct page *), GFP_NOFS);
Li Zefan560f7d72011-09-08 10:22:01 +0800548 if (!pages) {
549 /* just bail out to the uncompressed code */
Filipe Manana3527a012018-10-13 00:37:25 +0100550 nr_pages = 0;
Li Zefan560f7d72011-09-08 10:22:01 +0800551 goto cont;
552 }
Chris Mason179e29e2007-11-01 11:28:41 -0400553
David Sterbaeec63c62017-07-17 19:41:31 +0200554 if (BTRFS_I(inode)->defrag_compress)
555 compress_type = BTRFS_I(inode)->defrag_compress;
556 else if (BTRFS_I(inode)->prop_compress)
David Sterbab52aa8c2017-07-17 19:17:20 +0200557 compress_type = BTRFS_I(inode)->prop_compress;
Li Zefan261507a02010-12-17 14:21:50 +0800558
Chris Mason4adaa612013-03-26 13:07:00 -0400559 /*
560 * we need to call clear_page_dirty_for_io on each
561 * page in the range. Otherwise applications with the file
562 * mmap'd can wander in and change the page contents while
563 * we are compressing them.
564 *
565 * If the compression fails for any reason, we set the pages
566 * dirty again later on.
Timofey Titovetse9679de2017-10-24 01:29:48 +0300567 *
568 * Note that the remaining part is redirtied, the start pointer
569 * has moved, the end is the original one.
Chris Mason4adaa612013-03-26 13:07:00 -0400570 */
Timofey Titovetse9679de2017-10-24 01:29:48 +0300571 if (!redirty) {
572 extent_range_clear_dirty_for_io(inode, start, end);
573 redirty = 1;
574 }
David Sterbaf51d2b52017-09-15 17:36:57 +0200575
576 /* Compression level is applied here and only here */
577 ret = btrfs_compress_pages(
578 compress_type | (fs_info->compress_level << 4),
Li Zefan261507a02010-12-17 14:21:50 +0800579 inode->i_mapping, start,
David Sterba38c31462017-02-14 19:04:07 +0100580 pages,
David Sterba4d3a8002017-02-14 19:04:07 +0100581 &nr_pages,
Li Zefan261507a02010-12-17 14:21:50 +0800582 &total_in,
David Sterbae5d74902017-02-14 19:45:05 +0100583 &total_compressed);
Chris Masonc8b97812008-10-29 14:49:59 -0400584
585 if (!ret) {
Johannes Thumshirn70730172018-12-05 15:23:03 +0100586 unsigned long offset = offset_in_page(total_compressed);
David Sterba4d3a8002017-02-14 19:04:07 +0100587 struct page *page = pages[nr_pages - 1];
Chris Masonc8b97812008-10-29 14:49:59 -0400588 char *kaddr;
589
590 /* zero the tail end of the last page, we might be
591 * sending it down to disk
592 */
593 if (offset) {
Cong Wang7ac687d2011-11-25 23:14:28 +0800594 kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400595 memset(kaddr + offset, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300596 PAGE_SIZE - offset);
Cong Wang7ac687d2011-11-25 23:14:28 +0800597 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400598 }
599 will_compress = 1;
600 }
601 }
Li Zefan560f7d72011-09-08 10:22:01 +0800602cont:
Chris Masonc8b97812008-10-29 14:49:59 -0400603 if (start == 0) {
604 /* lets try to make an inline extent */
Timofey Titovets6018ba02017-09-15 01:57:26 +0300605 if (ret || total_in < actual_end) {
Chris Masonc8b97812008-10-29 14:49:59 -0400606 /* we didn't compress the entire range, try
Chris Mason771ed682008-11-06 22:02:51 -0500607 * to make an uncompressed inline extent.
Chris Masonc8b97812008-10-29 14:49:59 -0400608 */
Nikolay Borisovd02c0e22018-03-02 09:43:15 +0200609 ret = cow_file_range_inline(inode, start, end, 0,
610 BTRFS_COMPRESS_NONE, NULL);
Chris Masonc8b97812008-10-29 14:49:59 -0400611 } else {
Chris Mason771ed682008-11-06 22:02:51 -0500612 /* try making a compressed inline extent */
Nikolay Borisovd02c0e22018-03-02 09:43:15 +0200613 ret = cow_file_range_inline(inode, start, end,
Li Zefanfe3f5662011-03-28 08:30:38 +0000614 total_compressed,
615 compress_type, pages);
Chris Masonc8b97812008-10-29 14:49:59 -0400616 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100617 if (ret <= 0) {
Josef Bacik151a41b2013-07-29 13:22:24 -0400618 unsigned long clear_flags = EXTENT_DELALLOC |
Josef Bacik8b62f872017-10-19 14:15:55 -0400619 EXTENT_DELALLOC_NEW | EXTENT_DEFRAG |
620 EXTENT_DO_ACCOUNTING;
Filipe Mananae6eb4312014-10-10 10:45:12 +0100621 unsigned long page_error_op;
622
Filipe Mananae6eb4312014-10-10 10:45:12 +0100623 page_error_op = ret < 0 ? PAGE_SET_ERROR : 0;
Josef Bacik151a41b2013-07-29 13:22:24 -0400624
Chris Mason771ed682008-11-06 22:02:51 -0500625 /*
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100626 * inline extent creation worked or returned error,
627 * we don't need to create any more async work items.
628 * Unlock and free up our temp pages.
Josef Bacik8b62f872017-10-19 14:15:55 -0400629 *
630 * We use DO_ACCOUNTING here because we need the
631 * delalloc_release_metadata to be done _after_ we drop
632 * our outstanding extent for clearing delalloc for this
633 * range.
Chris Mason771ed682008-11-06 22:02:51 -0500634 */
Nikolay Borisov74e91942019-07-17 16:18:16 +0300635 extent_clear_unlock_delalloc(inode, start, end, NULL,
636 clear_flags,
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800637 PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -0400638 PAGE_CLEAR_DIRTY |
639 PAGE_SET_WRITEBACK |
Filipe Mananae6eb4312014-10-10 10:45:12 +0100640 page_error_op |
Josef Bacikc2790a22013-07-29 11:20:47 -0400641 PAGE_END_WRITEBACK);
Nikolay Borisovcecc8d92019-07-17 14:41:45 +0300642
643 for (i = 0; i < nr_pages; i++) {
644 WARN_ON(pages[i]->mapping);
645 put_page(pages[i]);
646 }
647 kfree(pages);
648
649 return 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400650 }
651 }
652
653 if (will_compress) {
654 /*
655 * we aren't doing an inline extent round the compressed size
656 * up to a block size boundary so the allocator does sane
657 * things
658 */
Qu Wenruofda28322013-02-26 08:10:22 +0000659 total_compressed = ALIGN(total_compressed, blocksize);
Chris Masonc8b97812008-10-29 14:49:59 -0400660
661 /*
662 * one last check to make sure the compression is really a
Timofey Titovets170607e2017-06-06 14:41:15 +0300663 * win, compare the page count read with the blocks on disk,
664 * compression must free at least one sector size
Chris Masonc8b97812008-10-29 14:49:59 -0400665 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300666 total_in = ALIGN(total_in, PAGE_SIZE);
Timofey Titovets170607e2017-06-06 14:41:15 +0300667 if (total_compressed + blocksize <= total_in) {
Nikolay Borisovac3e9932019-07-17 14:41:44 +0300668 compressed_extents++;
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700669
670 /*
671 * The async work queues will take care of doing actual
672 * allocation on disk for these compressed pages, and
673 * will submit them to the elevator.
674 */
Nikolay Borisovb5326272019-03-12 17:20:25 +0200675 add_async_extent(async_chunk, start, total_in,
David Sterba4d3a8002017-02-14 19:04:07 +0100676 total_compressed, pages, nr_pages,
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700677 compress_type);
678
Timofey Titovets11708622017-10-03 18:06:01 +0300679 if (start + total_in < end) {
680 start += total_in;
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700681 pages = NULL;
682 cond_resched();
683 goto again;
684 }
Nikolay Borisovac3e9932019-07-17 14:41:44 +0300685 return compressed_extents;
Chris Masonc8b97812008-10-29 14:49:59 -0400686 }
687 }
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700688 if (pages) {
Chris Masonc8b97812008-10-29 14:49:59 -0400689 /*
690 * the compression code ran but failed to make things smaller,
691 * free any pages it allocated and our page pointer array
692 */
David Sterba4d3a8002017-02-14 19:04:07 +0100693 for (i = 0; i < nr_pages; i++) {
Chris Mason70b99e62008-10-31 12:46:39 -0400694 WARN_ON(pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300695 put_page(pages[i]);
Chris Masonc8b97812008-10-29 14:49:59 -0400696 }
697 kfree(pages);
698 pages = NULL;
699 total_compressed = 0;
David Sterba4d3a8002017-02-14 19:04:07 +0100700 nr_pages = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400701
702 /* flag the file so we don't compress in the future */
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400703 if (!btrfs_test_opt(fs_info, FORCE_COMPRESS) &&
David Sterbab52aa8c2017-07-17 19:17:20 +0200704 !(BTRFS_I(inode)->prop_compress)) {
Chris Masona555f812010-01-28 16:18:15 -0500705 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
Chris Mason1e701a32010-03-11 09:42:04 -0500706 }
Chris Masonc8b97812008-10-29 14:49:59 -0400707 }
Chris Masonf03d9301f2009-02-04 09:31:06 -0500708cleanup_and_bail_uncompressed:
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700709 /*
710 * No compression, but we still need to write the pages in the file
711 * we've been given so far. redirty the locked page if it corresponds
712 * to our extent and set things up for the async work queue to run
713 * cow_file_range to do the normal delalloc dance.
714 */
Nikolay Borisov1368c6d2019-03-12 17:20:27 +0200715 if (page_offset(async_chunk->locked_page) >= start &&
716 page_offset(async_chunk->locked_page) <= end)
717 __set_page_dirty_nobuffers(async_chunk->locked_page);
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700718 /* unlocked later on in the async handlers */
719
720 if (redirty)
721 extent_range_redirty_for_io(inode, start, end);
Nikolay Borisovb5326272019-03-12 17:20:25 +0200722 add_async_extent(async_chunk, start, end - start + 1, 0, NULL, 0,
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700723 BTRFS_COMPRESS_NONE);
Nikolay Borisovac3e9932019-07-17 14:41:44 +0300724 compressed_extents++;
Chris Mason771ed682008-11-06 22:02:51 -0500725
Nikolay Borisovac3e9932019-07-17 14:41:44 +0300726 return compressed_extents;
Chris Mason771ed682008-11-06 22:02:51 -0500727}
Chris Mason771ed682008-11-06 22:02:51 -0500728
Filipe Manana40ae8372014-10-06 22:14:24 +0100729static void free_async_extent_pages(struct async_extent *async_extent)
730{
731 int i;
732
733 if (!async_extent->pages)
734 return;
735
736 for (i = 0; i < async_extent->nr_pages; i++) {
737 WARN_ON(async_extent->pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300738 put_page(async_extent->pages[i]);
Filipe Manana40ae8372014-10-06 22:14:24 +0100739 }
740 kfree(async_extent->pages);
741 async_extent->nr_pages = 0;
742 async_extent->pages = NULL;
Chris Mason771ed682008-11-06 22:02:51 -0500743}
744
745/*
746 * phase two of compressed writeback. This is the ordered portion
747 * of the code, which only gets called in the order the work was
748 * queued. We walk all the async extents created by compress_file_range
749 * and send them down to the disk.
750 */
Nikolay Borisovb5326272019-03-12 17:20:25 +0200751static noinline void submit_compressed_extents(struct async_chunk *async_chunk)
Chris Mason771ed682008-11-06 22:02:51 -0500752{
Nikolay Borisovb5326272019-03-12 17:20:25 +0200753 struct inode *inode = async_chunk->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400754 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason771ed682008-11-06 22:02:51 -0500755 struct async_extent *async_extent;
756 u64 alloc_hint = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500757 struct btrfs_key ins;
758 struct extent_map *em;
759 struct btrfs_root *root = BTRFS_I(inode)->root;
Nikolay Borisov43366502019-03-12 17:20:29 +0200760 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500761 int ret = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500762
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500763again:
Nikolay Borisovb5326272019-03-12 17:20:25 +0200764 while (!list_empty(&async_chunk->extents)) {
765 async_extent = list_entry(async_chunk->extents.next,
Chris Mason771ed682008-11-06 22:02:51 -0500766 struct async_extent, list);
767 list_del(&async_extent->list);
768
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500769retry:
Nikolay Borisov74475552019-03-12 17:20:30 +0200770 lock_extent(io_tree, async_extent->start,
771 async_extent->start + async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500772 /* did the compression code fall back to uncompressed IO? */
773 if (!async_extent->pages) {
774 int page_started = 0;
775 unsigned long nr_written = 0;
776
Chris Mason771ed682008-11-06 22:02:51 -0500777 /* allocate blocks */
Nikolay Borisovb5326272019-03-12 17:20:25 +0200778 ret = cow_file_range(inode, async_chunk->locked_page,
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500779 async_extent->start,
780 async_extent->start +
781 async_extent->ram_size - 1,
Nikolay Borisov330a5822019-07-17 16:18:17 +0300782 &page_started, &nr_written, 0);
Chris Mason771ed682008-11-06 22:02:51 -0500783
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100784 /* JDM XXX */
785
Chris Mason771ed682008-11-06 22:02:51 -0500786 /*
787 * if page_started, cow_file_range inserted an
788 * inline extent and took care of all the unlocking
789 * and IO for us. Otherwise, we need to submit
790 * all those pages down to the drive.
791 */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500792 if (!page_started && !ret)
Nikolay Borisov5e3ee232017-12-08 15:55:58 +0200793 extent_write_locked_range(inode,
794 async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500795 async_extent->start +
Chris Mason771ed682008-11-06 22:02:51 -0500796 async_extent->ram_size - 1,
Chris Mason771ed682008-11-06 22:02:51 -0500797 WB_SYNC_ALL);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500798 else if (ret)
Nikolay Borisovb5326272019-03-12 17:20:25 +0200799 unlock_page(async_chunk->locked_page);
Chris Mason771ed682008-11-06 22:02:51 -0500800 kfree(async_extent);
801 cond_resched();
802 continue;
803 }
804
Wang Xiaoguang18513092016-07-25 15:51:40 +0800805 ret = btrfs_reserve_extent(root, async_extent->ram_size,
Chris Mason771ed682008-11-06 22:02:51 -0500806 async_extent->compressed_size,
807 async_extent->compressed_size,
Miao Xiee570fd22014-06-19 10:42:50 +0800808 0, alloc_hint, &ins, 1, 1);
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500809 if (ret) {
Filipe Manana40ae8372014-10-06 22:14:24 +0100810 free_async_extent_pages(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500811
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400812 if (ret == -ENOSPC) {
813 unlock_extent(io_tree, async_extent->start,
814 async_extent->start +
815 async_extent->ram_size - 1);
Liu Boce620032014-07-24 22:48:05 +0800816
817 /*
818 * we need to redirty the pages if we decide to
819 * fallback to uncompressed IO, otherwise we
820 * will not submit these pages down to lower
821 * layers.
822 */
823 extent_range_redirty_for_io(inode,
824 async_extent->start,
825 async_extent->start +
826 async_extent->ram_size - 1);
827
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100828 goto retry;
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400829 }
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500830 goto out_free;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500831 }
Yan, Zhengc2167752009-11-12 09:34:21 +0000832 /*
833 * here we're doing allocation and writeback of the
834 * compressed pages
835 */
Liu Bo6f9994d2017-01-31 07:50:22 -0800836 em = create_io_em(inode, async_extent->start,
837 async_extent->ram_size, /* len */
838 async_extent->start, /* orig_start */
839 ins.objectid, /* block_start */
840 ins.offset, /* block_len */
841 ins.offset, /* orig_block_len */
842 async_extent->ram_size, /* ram_bytes */
843 async_extent->compress_type,
844 BTRFS_ORDERED_COMPRESSED);
845 if (IS_ERR(em))
846 /* ret value is not necessary due to void function */
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500847 goto out_free_reserve;
Liu Bo6f9994d2017-01-31 07:50:22 -0800848 free_extent_map(em);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500849
Li Zefan261507a02010-12-17 14:21:50 +0800850 ret = btrfs_add_ordered_extent_compress(inode,
851 async_extent->start,
852 ins.objectid,
853 async_extent->ram_size,
854 ins.offset,
855 BTRFS_ORDERED_COMPRESSED,
856 async_extent->compress_type);
Filipe Mananad9f85962014-08-25 10:43:00 +0100857 if (ret) {
Nikolay Borisovdcdbc052017-02-20 13:50:45 +0200858 btrfs_drop_extent_cache(BTRFS_I(inode),
859 async_extent->start,
Filipe Mananad9f85962014-08-25 10:43:00 +0100860 async_extent->start +
861 async_extent->ram_size - 1, 0);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500862 goto out_free_reserve;
Filipe Mananad9f85962014-08-25 10:43:00 +0100863 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400864 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Chris Mason771ed682008-11-06 22:02:51 -0500865
Chris Mason771ed682008-11-06 22:02:51 -0500866 /*
867 * clear dirty, set writeback and unlock the pages.
868 */
Josef Bacikc2790a22013-07-29 11:20:47 -0400869 extent_clear_unlock_delalloc(inode, async_extent->start,
Chris Masona791e352009-10-08 11:27:10 -0400870 async_extent->start +
871 async_extent->ram_size - 1,
Josef Bacik151a41b2013-07-29 13:22:24 -0400872 NULL, EXTENT_LOCKED | EXTENT_DELALLOC,
873 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -0400874 PAGE_SET_WRITEBACK);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200875 if (btrfs_submit_compressed_write(inode,
Chris Masond3977122009-01-05 21:25:51 -0500876 async_extent->start,
877 async_extent->ram_size,
878 ins.objectid,
879 ins.offset, async_extent->pages,
Liu Bof82b7352017-10-23 23:18:16 -0600880 async_extent->nr_pages,
Nikolay Borisovb5326272019-03-12 17:20:25 +0200881 async_chunk->write_flags)) {
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100882 struct page *p = async_extent->pages[0];
883 const u64 start = async_extent->start;
884 const u64 end = start + async_extent->ram_size - 1;
885
886 p->mapping = inode->i_mapping;
Nikolay Borisovc6297322018-11-08 10:18:08 +0200887 btrfs_writepage_endio_finish_ordered(p, start, end, 0);
Nikolay Borisov7087a9d2018-11-01 14:09:48 +0200888
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100889 p->mapping = NULL;
Nikolay Borisov74e91942019-07-17 16:18:16 +0300890 extent_clear_unlock_delalloc(inode, start, end,
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800891 NULL, 0,
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100892 PAGE_END_WRITEBACK |
893 PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +0100894 free_async_extent_pages(async_extent);
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100895 }
Chris Mason771ed682008-11-06 22:02:51 -0500896 alloc_hint = ins.objectid + ins.offset;
897 kfree(async_extent);
898 cond_resched();
899 }
Filipe Mananadec8f172014-10-06 22:14:26 +0100900 return;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500901out_free_reserve:
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400902 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400903 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100904out_free:
Josef Bacikc2790a22013-07-29 11:20:47 -0400905 extent_clear_unlock_delalloc(inode, async_extent->start,
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500906 async_extent->start +
907 async_extent->ram_size - 1,
Josef Bacikc2790a22013-07-29 11:20:47 -0400908 NULL, EXTENT_LOCKED | EXTENT_DELALLOC |
Filipe Mananaa7e3b972017-04-03 10:45:46 +0100909 EXTENT_DELALLOC_NEW |
Josef Bacik151a41b2013-07-29 13:22:24 -0400910 EXTENT_DEFRAG | EXTENT_DO_ACCOUNTING,
911 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Filipe Manana704de492014-10-06 22:14:22 +0100912 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK |
913 PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +0100914 free_async_extent_pages(async_extent);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100915 kfree(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500916 goto again;
Chris Mason771ed682008-11-06 22:02:51 -0500917}
918
Josef Bacik4b46fce2010-05-23 11:00:55 -0400919static u64 get_extent_allocation_hint(struct inode *inode, u64 start,
920 u64 num_bytes)
921{
922 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
923 struct extent_map *em;
924 u64 alloc_hint = 0;
925
926 read_lock(&em_tree->lock);
927 em = search_extent_mapping(em_tree, start, num_bytes);
928 if (em) {
929 /*
930 * if block start isn't an actual block number then find the
931 * first block in this inode and use that as a hint. If that
932 * block is also bogus then just don't worry about it.
933 */
934 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
935 free_extent_map(em);
936 em = search_extent_mapping(em_tree, 0, 0);
937 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
938 alloc_hint = em->block_start;
939 if (em)
940 free_extent_map(em);
941 } else {
942 alloc_hint = em->block_start;
943 free_extent_map(em);
944 }
945 }
946 read_unlock(&em_tree->lock);
947
948 return alloc_hint;
949}
950
Chris Mason771ed682008-11-06 22:02:51 -0500951/*
952 * when extent_io.c finds a delayed allocation range in the file,
953 * the call backs end up in this code. The basic idea is to
954 * allocate extents on disk for the range, and create ordered data structs
955 * in ram to track those extents.
956 *
957 * locked_page is the page that writepage had locked already. We use
958 * it to make sure we don't do extra locks or unlocks.
959 *
960 * *page_started is set to one if we unlock locked_page and do everything
961 * required to start IO on it. It may be clean and already done with
962 * IO when we return.
963 */
Josef Bacik00361582013-08-14 14:02:47 -0400964static noinline int cow_file_range(struct inode *inode,
965 struct page *locked_page,
Nikolay Borisov74e91942019-07-17 16:18:16 +0300966 u64 start, u64 end, int *page_started,
Nikolay Borisov330a5822019-07-17 16:18:17 +0300967 unsigned long *nr_written, int unlock)
Chris Mason771ed682008-11-06 22:02:51 -0500968{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400969 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik00361582013-08-14 14:02:47 -0400970 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason771ed682008-11-06 22:02:51 -0500971 u64 alloc_hint = 0;
972 u64 num_bytes;
973 unsigned long ram_size;
Filipe Mananaa315e682017-03-06 23:04:20 +0000974 u64 cur_alloc_size = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400975 u64 blocksize = fs_info->sectorsize;
Chris Mason771ed682008-11-06 22:02:51 -0500976 struct btrfs_key ins;
977 struct extent_map *em;
Filipe Mananaa315e682017-03-06 23:04:20 +0000978 unsigned clear_bits;
979 unsigned long page_ops;
980 bool extent_reserved = false;
Chris Mason771ed682008-11-06 22:02:51 -0500981 int ret = 0;
982
Nikolay Borisov70ddc552017-02-20 13:50:35 +0200983 if (btrfs_is_free_space_inode(BTRFS_I(inode))) {
Josef Bacik02ecd2c2013-10-25 16:19:08 -0400984 WARN_ON_ONCE(1);
Josef Bacik29bce2f2014-02-07 12:21:23 -0500985 ret = -EINVAL;
986 goto out_unlock;
Josef Bacik02ecd2c2013-10-25 16:19:08 -0400987 }
Chris Mason771ed682008-11-06 22:02:51 -0500988
Qu Wenruofda28322013-02-26 08:10:22 +0000989 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Mason771ed682008-11-06 22:02:51 -0500990 num_bytes = max(blocksize, num_bytes);
Anand Jain566b1762018-02-15 18:07:59 +0800991 ASSERT(num_bytes <= btrfs_super_total_bytes(fs_info->super_copy));
Chris Mason771ed682008-11-06 22:02:51 -0500992
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200993 inode_should_defrag(BTRFS_I(inode), start, end, num_bytes, SZ_64K);
Chris Mason4cb53002011-05-24 15:35:30 -0400994
Chris Mason771ed682008-11-06 22:02:51 -0500995 if (start == 0) {
996 /* lets try to make an inline extent */
Nikolay Borisovd02c0e22018-03-02 09:43:15 +0200997 ret = cow_file_range_inline(inode, start, end, 0,
998 BTRFS_COMPRESS_NONE, NULL);
Chris Mason771ed682008-11-06 22:02:51 -0500999 if (ret == 0) {
Josef Bacik8b62f872017-10-19 14:15:55 -04001000 /*
1001 * We use DO_ACCOUNTING here because we need the
1002 * delalloc_release_metadata to be run _after_ we drop
1003 * our outstanding extent for clearing delalloc for this
1004 * range.
1005 */
Nikolay Borisov74e91942019-07-17 16:18:16 +03001006 extent_clear_unlock_delalloc(inode, start, end, NULL,
Josef Bacikc2790a22013-07-29 11:20:47 -04001007 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik8b62f872017-10-19 14:15:55 -04001008 EXTENT_DELALLOC_NEW | EXTENT_DEFRAG |
1009 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001010 PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
1011 PAGE_END_WRITEBACK);
Chris Mason771ed682008-11-06 22:02:51 -05001012 *nr_written = *nr_written +
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001013 (end - start + PAGE_SIZE) / PAGE_SIZE;
Chris Mason771ed682008-11-06 22:02:51 -05001014 *page_started = 1;
Chris Mason771ed682008-11-06 22:02:51 -05001015 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001016 } else if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001017 goto out_unlock;
Chris Mason771ed682008-11-06 22:02:51 -05001018 }
1019 }
Chris Masonc8b97812008-10-29 14:49:59 -04001020
Josef Bacik4b46fce2010-05-23 11:00:55 -04001021 alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02001022 btrfs_drop_extent_cache(BTRFS_I(inode), start,
1023 start + num_bytes - 1, 0);
Chris Mason3b951512008-04-17 11:29:12 -04001024
Anand Jain3752d222018-02-15 12:29:38 +08001025 while (num_bytes > 0) {
1026 cur_alloc_size = num_bytes;
Wang Xiaoguang18513092016-07-25 15:51:40 +08001027 ret = btrfs_reserve_extent(root, cur_alloc_size, cur_alloc_size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001028 fs_info->sectorsize, 0, alloc_hint,
Miao Xiee570fd22014-06-19 10:42:50 +08001029 &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04001030 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001031 goto out_unlock;
Filipe Mananaa315e682017-03-06 23:04:20 +00001032 cur_alloc_size = ins.offset;
1033 extent_reserved = true;
Chris Masond3977122009-01-05 21:25:51 -05001034
Chris Mason771ed682008-11-06 22:02:51 -05001035 ram_size = ins.offset;
Liu Bo6f9994d2017-01-31 07:50:22 -08001036 em = create_io_em(inode, start, ins.offset, /* len */
1037 start, /* orig_start */
1038 ins.objectid, /* block_start */
1039 ins.offset, /* block_len */
1040 ins.offset, /* orig_block_len */
1041 ram_size, /* ram_bytes */
1042 BTRFS_COMPRESS_NONE, /* compress_type */
Liu Bo1af4a0a2017-02-13 15:35:09 -08001043 BTRFS_ORDERED_REGULAR /* type */);
Su Yue090a127a2018-05-30 16:48:56 +08001044 if (IS_ERR(em)) {
1045 ret = PTR_ERR(em);
Liu Boace68ba2013-04-22 10:53:47 +00001046 goto out_reserve;
Su Yue090a127a2018-05-30 16:48:56 +08001047 }
Liu Bo6f9994d2017-01-31 07:50:22 -08001048 free_extent_map(em);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001049
Chris Masone6dcd2d2008-07-17 12:53:50 -04001050 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
Chris Mason771ed682008-11-06 22:02:51 -05001051 ram_size, cur_alloc_size, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001052 if (ret)
Filipe Mananad9f85962014-08-25 10:43:00 +01001053 goto out_drop_extent_cache;
Chris Masonc8b97812008-10-29 14:49:59 -04001054
Yan Zheng17d217f2008-12-12 10:03:38 -05001055 if (root->root_key.objectid ==
1056 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1057 ret = btrfs_reloc_clone_csums(inode, start,
1058 cur_alloc_size);
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001059 /*
1060 * Only drop cache here, and process as normal.
1061 *
1062 * We must not allow extent_clear_unlock_delalloc()
1063 * at out_unlock label to free meta of this ordered
1064 * extent, as its meta should be freed by
1065 * btrfs_finish_ordered_io().
1066 *
1067 * So we must continue until @start is increased to
1068 * skip current ordered extent.
1069 */
Josef Bacik00361582013-08-14 14:02:47 -04001070 if (ret)
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001071 btrfs_drop_extent_cache(BTRFS_I(inode), start,
1072 start + ram_size - 1, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001073 }
1074
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001075 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana9cfa3e32016-04-26 15:39:32 +01001076
Chris Masonc8b97812008-10-29 14:49:59 -04001077 /* we're not doing compressed IO, don't unlock the first
1078 * page (which the caller expects to stay locked), don't
1079 * clear any dirty bits and don't set any writeback bits
Chris Mason8b62b722009-09-02 16:53:46 -04001080 *
1081 * Do set the Private2 bit so we know this page was properly
1082 * setup for writepage
Chris Masonc8b97812008-10-29 14:49:59 -04001083 */
Filipe Mananaa315e682017-03-06 23:04:20 +00001084 page_ops = unlock ? PAGE_UNLOCK : 0;
1085 page_ops |= PAGE_SET_PRIVATE2;
Chris Masona791e352009-10-08 11:27:10 -04001086
Josef Bacikc2790a22013-07-29 11:20:47 -04001087 extent_clear_unlock_delalloc(inode, start,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001088 start + ram_size - 1,
Nikolay Borisov74e91942019-07-17 16:18:16 +03001089 locked_page,
Josef Bacikc2790a22013-07-29 11:20:47 -04001090 EXTENT_LOCKED | EXTENT_DELALLOC,
Filipe Mananaa315e682017-03-06 23:04:20 +00001091 page_ops);
Anand Jain3752d222018-02-15 12:29:38 +08001092 if (num_bytes < cur_alloc_size)
1093 num_bytes = 0;
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001094 else
Anand Jain3752d222018-02-15 12:29:38 +08001095 num_bytes -= cur_alloc_size;
Chris Masonc59f8952007-12-17 20:14:04 -05001096 alloc_hint = ins.objectid + ins.offset;
1097 start += cur_alloc_size;
Filipe Mananaa315e682017-03-06 23:04:20 +00001098 extent_reserved = false;
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001099
1100 /*
1101 * btrfs_reloc_clone_csums() error, since start is increased
1102 * extent_clear_unlock_delalloc() at out_unlock label won't
1103 * free metadata of current ordered extent, we're OK to exit.
1104 */
1105 if (ret)
1106 goto out_unlock;
Chris Masonb888db22007-08-27 16:49:44 -04001107 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001108out:
Chris Masonbe20aa92007-12-17 20:14:01 -05001109 return ret;
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001110
Filipe Mananad9f85962014-08-25 10:43:00 +01001111out_drop_extent_cache:
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02001112 btrfs_drop_extent_cache(BTRFS_I(inode), start, start + ram_size - 1, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001113out_reserve:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001114 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001115 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001116out_unlock:
Filipe Mananaa7e3b972017-04-03 10:45:46 +01001117 clear_bits = EXTENT_LOCKED | EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
1118 EXTENT_DEFRAG | EXTENT_CLEAR_META_RESV;
Filipe Mananaa315e682017-03-06 23:04:20 +00001119 page_ops = PAGE_UNLOCK | PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
1120 PAGE_END_WRITEBACK;
1121 /*
1122 * If we reserved an extent for our delalloc range (or a subrange) and
1123 * failed to create the respective ordered extent, then it means that
1124 * when we reserved the extent we decremented the extent's size from
1125 * the data space_info's bytes_may_use counter and incremented the
1126 * space_info's bytes_reserved counter by the same amount. We must make
1127 * sure extent_clear_unlock_delalloc() does not try to decrement again
1128 * the data space_info's bytes_may_use counter, therefore we do not pass
1129 * it the flag EXTENT_CLEAR_DATA_RESV.
1130 */
1131 if (extent_reserved) {
1132 extent_clear_unlock_delalloc(inode, start,
1133 start + cur_alloc_size,
Filipe Mananaa315e682017-03-06 23:04:20 +00001134 locked_page,
1135 clear_bits,
1136 page_ops);
1137 start += cur_alloc_size;
1138 if (start >= end)
1139 goto out;
1140 }
Nikolay Borisov74e91942019-07-17 16:18:16 +03001141 extent_clear_unlock_delalloc(inode, start, end, locked_page,
Filipe Mananaa315e682017-03-06 23:04:20 +00001142 clear_bits | EXTENT_CLEAR_DATA_RESV,
1143 page_ops);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001144 goto out;
Chris Mason771ed682008-11-06 22:02:51 -05001145}
Chris Masonc8b97812008-10-29 14:49:59 -04001146
Chris Mason771ed682008-11-06 22:02:51 -05001147/*
1148 * work queue call back to started compression on a file and pages
1149 */
1150static noinline void async_cow_start(struct btrfs_work *work)
1151{
Nikolay Borisovb5326272019-03-12 17:20:25 +02001152 struct async_chunk *async_chunk;
Nikolay Borisovac3e9932019-07-17 14:41:44 +03001153 int compressed_extents;
Chris Mason771ed682008-11-06 22:02:51 -05001154
Nikolay Borisovb5326272019-03-12 17:20:25 +02001155 async_chunk = container_of(work, struct async_chunk, work);
Chris Mason771ed682008-11-06 22:02:51 -05001156
Nikolay Borisovac3e9932019-07-17 14:41:44 +03001157 compressed_extents = compress_file_range(async_chunk);
1158 if (compressed_extents == 0) {
Nikolay Borisovb5326272019-03-12 17:20:25 +02001159 btrfs_add_delayed_iput(async_chunk->inode);
1160 async_chunk->inode = NULL;
Josef Bacik8180ef82012-06-08 15:16:12 -04001161 }
Chris Mason771ed682008-11-06 22:02:51 -05001162}
1163
1164/*
1165 * work queue call back to submit previously compressed pages
1166 */
1167static noinline void async_cow_submit(struct btrfs_work *work)
1168{
Nikolay Borisovc5a68ae2019-03-12 17:20:26 +02001169 struct async_chunk *async_chunk = container_of(work, struct async_chunk,
1170 work);
1171 struct btrfs_fs_info *fs_info = btrfs_work_owner(work);
Chris Mason771ed682008-11-06 22:02:51 -05001172 unsigned long nr_pages;
1173
Nikolay Borisovb5326272019-03-12 17:20:25 +02001174 nr_pages = (async_chunk->end - async_chunk->start + PAGE_SIZE) >>
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001175 PAGE_SHIFT;
Chris Mason771ed682008-11-06 22:02:51 -05001176
David Sterba093258e2018-02-26 16:15:17 +01001177 /* atomic_sub_return implies a barrier */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001178 if (atomic_sub_return(nr_pages, &fs_info->async_delalloc_pages) <
David Sterba093258e2018-02-26 16:15:17 +01001179 5 * SZ_1M)
1180 cond_wake_up_nomb(&fs_info->async_submit_wait);
Chris Mason771ed682008-11-06 22:02:51 -05001181
Nikolay Borisov4546d172019-01-03 10:50:03 +02001182 /*
Nikolay Borisovb5326272019-03-12 17:20:25 +02001183 * ->inode could be NULL if async_chunk_start has failed to compress,
Nikolay Borisov4546d172019-01-03 10:50:03 +02001184 * in which case we don't have anything to submit, yet we need to
1185 * always adjust ->async_delalloc_pages as its paired with the init
1186 * happening in cow_file_range_async
1187 */
Nikolay Borisovb5326272019-03-12 17:20:25 +02001188 if (async_chunk->inode)
1189 submit_compressed_extents(async_chunk);
Chris Mason771ed682008-11-06 22:02:51 -05001190}
Chris Masonc8b97812008-10-29 14:49:59 -04001191
Chris Mason771ed682008-11-06 22:02:51 -05001192static noinline void async_cow_free(struct btrfs_work *work)
1193{
Nikolay Borisovb5326272019-03-12 17:20:25 +02001194 struct async_chunk *async_chunk;
Nikolay Borisov97db1202019-03-12 17:20:24 +02001195
Nikolay Borisovb5326272019-03-12 17:20:25 +02001196 async_chunk = container_of(work, struct async_chunk, work);
1197 if (async_chunk->inode)
1198 btrfs_add_delayed_iput(async_chunk->inode);
Nikolay Borisov97db1202019-03-12 17:20:24 +02001199 /*
1200 * Since the pointer to 'pending' is at the beginning of the array of
Nikolay Borisovb5326272019-03-12 17:20:25 +02001201 * async_chunk's, freeing it ensures the whole array has been freed.
Nikolay Borisov97db1202019-03-12 17:20:24 +02001202 */
Nikolay Borisovb5326272019-03-12 17:20:25 +02001203 if (atomic_dec_and_test(async_chunk->pending))
Nikolay Borisovb1c16ac2019-04-01 11:29:57 +03001204 kvfree(async_chunk->pending);
Chris Mason771ed682008-11-06 22:02:51 -05001205}
1206
1207static int cow_file_range_async(struct inode *inode, struct page *locked_page,
1208 u64 start, u64 end, int *page_started,
Liu Bof82b7352017-10-23 23:18:16 -06001209 unsigned long *nr_written,
1210 unsigned int write_flags)
Chris Mason771ed682008-11-06 22:02:51 -05001211{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001212 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Nikolay Borisov97db1202019-03-12 17:20:24 +02001213 struct async_cow *ctx;
1214 struct async_chunk *async_chunk;
Chris Mason771ed682008-11-06 22:02:51 -05001215 unsigned long nr_pages;
1216 u64 cur_end;
Nikolay Borisov97db1202019-03-12 17:20:24 +02001217 u64 num_chunks = DIV_ROUND_UP(end - start, SZ_512K);
1218 int i;
1219 bool should_compress;
Nikolay Borisovb1c16ac2019-04-01 11:29:57 +03001220 unsigned nofs_flag;
Chris Mason771ed682008-11-06 22:02:51 -05001221
Nikolay Borisov69684c52019-03-12 17:20:28 +02001222 unlock_extent(&BTRFS_I(inode)->io_tree, start, end);
Nikolay Borisov97db1202019-03-12 17:20:24 +02001223
1224 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS &&
1225 !btrfs_test_opt(fs_info, FORCE_COMPRESS)) {
1226 num_chunks = 1;
1227 should_compress = false;
1228 } else {
1229 should_compress = true;
1230 }
1231
Nikolay Borisovb1c16ac2019-04-01 11:29:57 +03001232 nofs_flag = memalloc_nofs_save();
1233 ctx = kvmalloc(struct_size(ctx, chunks, num_chunks), GFP_KERNEL);
1234 memalloc_nofs_restore(nofs_flag);
1235
Nikolay Borisov97db1202019-03-12 17:20:24 +02001236 if (!ctx) {
1237 unsigned clear_bits = EXTENT_LOCKED | EXTENT_DELALLOC |
1238 EXTENT_DELALLOC_NEW | EXTENT_DEFRAG |
1239 EXTENT_DO_ACCOUNTING;
1240 unsigned long page_ops = PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
1241 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK |
1242 PAGE_SET_ERROR;
1243
Nikolay Borisov74e91942019-07-17 16:18:16 +03001244 extent_clear_unlock_delalloc(inode, start, end, locked_page,
Nikolay Borisov97db1202019-03-12 17:20:24 +02001245 clear_bits, page_ops);
1246 return -ENOMEM;
1247 }
1248
1249 async_chunk = ctx->chunks;
1250 atomic_set(&ctx->num_chunks, num_chunks);
1251
1252 for (i = 0; i < num_chunks; i++) {
1253 if (should_compress)
1254 cur_end = min(end, start + SZ_512K - 1);
1255 else
1256 cur_end = end;
1257
Nikolay Borisovbd4691a2019-01-03 10:50:01 +02001258 /*
1259 * igrab is called higher up in the call chain, take only the
1260 * lightweight reference for the callback lifetime
1261 */
1262 ihold(inode);
Nikolay Borisov97db1202019-03-12 17:20:24 +02001263 async_chunk[i].pending = &ctx->num_chunks;
1264 async_chunk[i].inode = inode;
1265 async_chunk[i].start = start;
1266 async_chunk[i].end = cur_end;
Nikolay Borisov97db1202019-03-12 17:20:24 +02001267 async_chunk[i].locked_page = locked_page;
1268 async_chunk[i].write_flags = write_flags;
1269 INIT_LIST_HEAD(&async_chunk[i].extents);
Chris Mason771ed682008-11-06 22:02:51 -05001270
Nikolay Borisov97db1202019-03-12 17:20:24 +02001271 btrfs_init_work(&async_chunk[i].work,
Liu Bo9e0af232014-08-15 23:36:53 +08001272 btrfs_delalloc_helper,
1273 async_cow_start, async_cow_submit,
1274 async_cow_free);
Chris Mason771ed682008-11-06 22:02:51 -05001275
Nikolay Borisov97db1202019-03-12 17:20:24 +02001276 nr_pages = DIV_ROUND_UP(cur_end - start, PAGE_SIZE);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001277 atomic_add(nr_pages, &fs_info->async_delalloc_pages);
Chris Mason771ed682008-11-06 22:02:51 -05001278
Nikolay Borisov97db1202019-03-12 17:20:24 +02001279 btrfs_queue_work(fs_info->delalloc_workers, &async_chunk[i].work);
Chris Mason771ed682008-11-06 22:02:51 -05001280
Chris Mason771ed682008-11-06 22:02:51 -05001281 *nr_written += nr_pages;
1282 start = cur_end + 1;
1283 }
1284 *page_started = 1;
1285 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001286}
1287
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001288static noinline int csum_exist_in_range(struct btrfs_fs_info *fs_info,
Yan Zheng17d217f2008-12-12 10:03:38 -05001289 u64 bytenr, u64 num_bytes)
1290{
1291 int ret;
1292 struct btrfs_ordered_sum *sums;
1293 LIST_HEAD(list);
1294
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001295 ret = btrfs_lookup_csums_range(fs_info->csum_root, bytenr,
Arne Jansena2de7332011-03-08 14:14:00 +01001296 bytenr + num_bytes - 1, &list, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001297 if (ret == 0 && list_empty(&list))
1298 return 0;
1299
1300 while (!list_empty(&list)) {
1301 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1302 list_del(&sums->list);
1303 kfree(sums);
1304 }
Liu Bo58113752018-01-31 17:09:13 -07001305 if (ret < 0)
1306 return ret;
Yan Zheng17d217f2008-12-12 10:03:38 -05001307 return 1;
1308}
1309
Chris Masond352ac62008-09-29 15:18:18 -04001310/*
1311 * when nowcow writeback call back. This checks for snapshots or COW copies
1312 * of the extents that exist in the file, and COWs the file as required.
1313 *
1314 * If no cow copies or snapshots exist, we write directly to the existing
1315 * blocks on disk
1316 */
Chris Mason7f366cf2009-03-12 20:12:45 -04001317static noinline int run_delalloc_nocow(struct inode *inode,
1318 struct page *locked_page,
Nikolay Borisov3e024842019-08-21 10:42:03 +03001319 const u64 start, const u64 end,
1320 int *page_started, int force,
1321 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001322{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001323 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masonbe20aa92007-12-17 20:14:01 -05001324 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masonbe20aa92007-12-17 20:14:01 -05001325 struct btrfs_path *path;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001326 u64 cow_start = (u64)-1;
1327 u64 cur_offset = start;
Robbie Ko8ecebf4d2018-08-06 10:30:30 +08001328 int ret;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001329 bool check_prev = true;
1330 const bool freespace_inode = btrfs_is_free_space_inode(BTRFS_I(inode));
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02001331 u64 ino = btrfs_ino(BTRFS_I(inode));
Nikolay Borisov762bf092019-08-22 17:24:20 +03001332 bool nocow = false;
1333 u64 disk_bytenr = 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001334
1335 path = btrfs_alloc_path();
Josef Bacik17ca04a2012-05-31 15:58:55 -04001336 if (!path) {
Nikolay Borisov74e91942019-07-17 16:18:16 +03001337 extent_clear_unlock_delalloc(inode, start, end, locked_page,
Josef Bacikc2790a22013-07-29 11:20:47 -04001338 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -04001339 EXTENT_DO_ACCOUNTING |
1340 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001341 PAGE_CLEAR_DIRTY |
1342 PAGE_SET_WRITEBACK |
1343 PAGE_END_WRITEBACK);
Mark Fashehd8926bb2011-07-13 10:38:47 -07001344 return -ENOMEM;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001345 }
Li Zefan82d59022011-04-20 10:33:24 +08001346
Yan Zheng80ff3852008-10-30 14:20:02 -04001347 while (1) {
Nikolay Borisov3e024842019-08-21 10:42:03 +03001348 struct btrfs_key found_key;
1349 struct btrfs_file_extent_item *fi;
1350 struct extent_buffer *leaf;
1351 u64 extent_end;
1352 u64 extent_offset;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001353 u64 num_bytes = 0;
1354 u64 disk_num_bytes;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001355 u64 ram_bytes;
1356 int extent_type;
Nikolay Borisov762bf092019-08-22 17:24:20 +03001357
1358 nocow = false;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001359
Liu Boe4c3b2d2017-01-30 12:25:28 -08001360 ret = btrfs_lookup_file_extent(NULL, root, path, ino,
Yan Zheng80ff3852008-10-30 14:20:02 -04001361 cur_offset, 0);
Josef Bacikd788a342013-10-25 16:55:08 -04001362 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001363 goto error;
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001364
1365 /*
1366 * If there is no extent for our range when doing the initial
1367 * search, then go back to the previous slot as it will be the
1368 * one containing the search offset
1369 */
Yan Zheng80ff3852008-10-30 14:20:02 -04001370 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1371 leaf = path->nodes[0];
1372 btrfs_item_key_to_cpu(leaf, &found_key,
1373 path->slots[0] - 1);
Li Zefan33345d012011-04-20 10:31:50 +08001374 if (found_key.objectid == ino &&
Yan Zheng80ff3852008-10-30 14:20:02 -04001375 found_key.type == BTRFS_EXTENT_DATA_KEY)
1376 path->slots[0]--;
1377 }
Nikolay Borisov3e024842019-08-21 10:42:03 +03001378 check_prev = false;
Yan Zheng80ff3852008-10-30 14:20:02 -04001379next_slot:
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001380 /* Go to next leaf if we have exhausted the current one */
Yan Zheng80ff3852008-10-30 14:20:02 -04001381 leaf = path->nodes[0];
1382 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1383 ret = btrfs_next_leaf(root, path);
Liu Boe8916692018-01-25 11:02:50 -07001384 if (ret < 0) {
1385 if (cow_start != (u64)-1)
1386 cur_offset = cow_start;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001387 goto error;
Liu Boe8916692018-01-25 11:02:50 -07001388 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001389 if (ret > 0)
1390 break;
1391 leaf = path->nodes[0];
1392 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001393
Yan Zheng80ff3852008-10-30 14:20:02 -04001394 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001395
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001396 /* Didn't find anything for our INO */
Filipe Manana1d512cb2015-11-09 00:33:58 +00001397 if (found_key.objectid > ino)
1398 break;
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001399 /*
1400 * Keep searching until we find an EXTENT_ITEM or there are no
1401 * more extents for this inode
1402 */
Filipe Manana1d512cb2015-11-09 00:33:58 +00001403 if (WARN_ON_ONCE(found_key.objectid < ino) ||
1404 found_key.type < BTRFS_EXTENT_DATA_KEY) {
1405 path->slots[0]++;
1406 goto next_slot;
1407 }
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001408
1409 /* Found key is not EXTENT_DATA_KEY or starts after req range */
Filipe Manana1d512cb2015-11-09 00:33:58 +00001410 if (found_key.type > BTRFS_EXTENT_DATA_KEY ||
Yan Zheng80ff3852008-10-30 14:20:02 -04001411 found_key.offset > end)
1412 break;
Chris Masonbe20aa92007-12-17 20:14:01 -05001413
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001414 /*
1415 * If the found extent starts after requested offset, then
1416 * adjust extent_end to be right before this extent begins
1417 */
Yan Zheng80ff3852008-10-30 14:20:02 -04001418 if (found_key.offset > cur_offset) {
1419 extent_end = found_key.offset;
Chris Masone9061e22009-10-09 09:57:45 -04001420 extent_type = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001421 goto out_check;
1422 }
Chris Masonc31f8832008-01-08 15:46:31 -05001423
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001424 /*
1425 * Found extent which begins before our range and potentially
1426 * intersect it
1427 */
Yan Zheng80ff3852008-10-30 14:20:02 -04001428 fi = btrfs_item_ptr(leaf, path->slots[0],
1429 struct btrfs_file_extent_item);
1430 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001431
Josef Bacikcc95bef2013-04-04 14:31:27 -04001432 ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
Yan Zhengd899e052008-10-30 14:25:28 -04001433 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1434 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001435 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001436 extent_offset = btrfs_file_extent_offset(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001437 extent_end = found_key.offset +
1438 btrfs_file_extent_num_bytes(leaf, fi);
Josef Bacikb4939682012-12-03 10:31:19 -05001439 disk_num_bytes =
1440 btrfs_file_extent_disk_num_bytes(leaf, fi);
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001441 /*
1442 * If extent we got ends before our range starts, skip
1443 * to next extent
1444 */
Yan Zheng80ff3852008-10-30 14:20:02 -04001445 if (extent_end <= start) {
1446 path->slots[0]++;
1447 goto next_slot;
1448 }
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001449 /* Skip holes */
Yan Zheng17d217f2008-12-12 10:03:38 -05001450 if (disk_bytenr == 0)
1451 goto out_check;
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001452 /* Skip compressed/encrypted/encoded extents */
Yan Zheng80ff3852008-10-30 14:20:02 -04001453 if (btrfs_file_extent_compression(leaf, fi) ||
1454 btrfs_file_extent_encryption(leaf, fi) ||
1455 btrfs_file_extent_other_encoding(leaf, fi))
1456 goto out_check;
Ethan Lien78d42952018-05-17 14:58:29 +08001457 /*
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001458 * If extent is created before the last volume's snapshot
1459 * this implies the extent is shared, hence we can't do
1460 * nocow. This is the same check as in
1461 * btrfs_cross_ref_exist but without calling
1462 * btrfs_search_slot.
Ethan Lien78d42952018-05-17 14:58:29 +08001463 */
Nikolay Borisov3e024842019-08-21 10:42:03 +03001464 if (!freespace_inode &&
Lu Fengqi27a7ff52018-11-29 17:31:32 +08001465 btrfs_file_extent_generation(leaf, fi) <=
Ethan Lien78d42952018-05-17 14:58:29 +08001466 btrfs_root_last_snapshot(&root->root_item))
1467 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001468 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1469 goto out_check;
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001470 /* If extent is RO, we must COW it */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001471 if (btrfs_extent_readonly(fs_info, disk_bytenr))
Yan Zheng80ff3852008-10-30 14:20:02 -04001472 goto out_check;
Liu Bo58113752018-01-31 17:09:13 -07001473 ret = btrfs_cross_ref_exist(root, ino,
1474 found_key.offset -
1475 extent_offset, disk_bytenr);
1476 if (ret) {
1477 /*
1478 * ret could be -EIO if the above fails to read
1479 * metadata.
1480 */
1481 if (ret < 0) {
1482 if (cow_start != (u64)-1)
1483 cur_offset = cow_start;
1484 goto error;
1485 }
1486
Nikolay Borisov3e024842019-08-21 10:42:03 +03001487 WARN_ON_ONCE(freespace_inode);
Yan Zheng17d217f2008-12-12 10:03:38 -05001488 goto out_check;
Liu Bo58113752018-01-31 17:09:13 -07001489 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001490 disk_bytenr += extent_offset;
Yan Zheng17d217f2008-12-12 10:03:38 -05001491 disk_bytenr += cur_offset - found_key.offset;
1492 num_bytes = min(end + 1, extent_end) - cur_offset;
1493 /*
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001494 * If there are pending snapshots for this root, we
1495 * fall into common COW way
Wang Shilonge9894fd2014-03-27 11:12:25 +08001496 */
Nikolay Borisov3e024842019-08-21 10:42:03 +03001497 if (!freespace_inode && atomic_read(&root->snapshot_force_cow))
Robbie Ko8ecebf4d2018-08-06 10:30:30 +08001498 goto out_check;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001499 /*
Yan Zheng17d217f2008-12-12 10:03:38 -05001500 * force cow if csum exists in the range.
1501 * this ensure that csum for a given extent are
1502 * either valid or do not exist.
1503 */
Liu Bo58113752018-01-31 17:09:13 -07001504 ret = csum_exist_in_range(fs_info, disk_bytenr,
1505 num_bytes);
1506 if (ret) {
Liu Bo58113752018-01-31 17:09:13 -07001507 /*
1508 * ret could be -EIO if the above fails to read
1509 * metadata.
1510 */
1511 if (ret < 0) {
1512 if (cow_start != (u64)-1)
1513 cur_offset = cow_start;
1514 goto error;
1515 }
Nikolay Borisov3e024842019-08-21 10:42:03 +03001516 WARN_ON_ONCE(freespace_inode);
Yan Zheng17d217f2008-12-12 10:03:38 -05001517 goto out_check;
Robbie Ko91e1f562016-10-07 10:01:29 +08001518 }
Robbie Ko8ecebf4d2018-08-06 10:30:30 +08001519 if (!btrfs_inc_nocow_writers(fs_info, disk_bytenr))
Filipe Mananaf78c4362016-05-09 13:15:41 +01001520 goto out_check;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001521 nocow = true;
Yan Zheng80ff3852008-10-30 14:20:02 -04001522 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Nikolay Borisove8e21002019-08-22 17:25:23 +03001523 extent_end = found_key.offset + ram_bytes;
1524 extent_end = ALIGN(extent_end, fs_info->sectorsize);
Nikolay Borisov922f0512019-08-05 17:47:06 +03001525 /* Skip extents outside of our requested range */
1526 if (extent_end <= start) {
1527 path->slots[0]++;
1528 goto next_slot;
1529 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001530 } else {
Nikolay Borisove8e21002019-08-22 17:25:23 +03001531 /* If this triggers then we have a memory corruption */
Arnd Bergmann290342f2019-03-25 14:02:25 +01001532 BUG();
Yan Zheng80ff3852008-10-30 14:20:02 -04001533 }
1534out_check:
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001535 /*
1536 * If nocow is false then record the beginning of the range
1537 * that needs to be COWed
1538 */
Yan Zheng80ff3852008-10-30 14:20:02 -04001539 if (!nocow) {
1540 if (cow_start == (u64)-1)
1541 cow_start = cur_offset;
1542 cur_offset = extent_end;
1543 if (cur_offset > end)
1544 break;
1545 path->slots[0]++;
1546 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001547 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001548
David Sterbab3b4aa72011-04-21 01:20:15 +02001549 btrfs_release_path(path);
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001550
1551 /*
1552 * COW range from cow_start to found_key.offset - 1. As the key
1553 * will contain the beginning of the first extent that can be
1554 * NOCOW, following one which needs to be COW'ed
1555 */
Yan Zheng80ff3852008-10-30 14:20:02 -04001556 if (cow_start != (u64)-1) {
Josef Bacik00361582013-08-14 14:02:47 -04001557 ret = cow_file_range(inode, locked_page,
1558 cow_start, found_key.offset - 1,
Nikolay Borisov330a5822019-07-17 16:18:17 +03001559 page_started, nr_written, 1);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001560 if (ret) {
Filipe Mananaf78c4362016-05-09 13:15:41 +01001561 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001562 btrfs_dec_nocow_writers(fs_info,
Filipe Mananaf78c4362016-05-09 13:15:41 +01001563 disk_bytenr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001564 goto error;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001565 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001566 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001567 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001568
Yan Zhengd899e052008-10-30 14:25:28 -04001569 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Liu Bo6f9994d2017-01-31 07:50:22 -08001570 u64 orig_start = found_key.offset - extent_offset;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001571 struct extent_map *em;
Liu Bo6f9994d2017-01-31 07:50:22 -08001572
1573 em = create_io_em(inode, cur_offset, num_bytes,
1574 orig_start,
1575 disk_bytenr, /* block_start */
1576 num_bytes, /* block_len */
1577 disk_num_bytes, /* orig_block_len */
1578 ram_bytes, BTRFS_COMPRESS_NONE,
1579 BTRFS_ORDERED_PREALLOC);
1580 if (IS_ERR(em)) {
Liu Bo6f9994d2017-01-31 07:50:22 -08001581 if (nocow)
1582 btrfs_dec_nocow_writers(fs_info,
1583 disk_bytenr);
1584 ret = PTR_ERR(em);
1585 goto error;
Yan Zhengd899e052008-10-30 14:25:28 -04001586 }
Liu Bo6f9994d2017-01-31 07:50:22 -08001587 free_extent_map(em);
Nikolay Borisovbb55f622019-08-05 17:47:05 +03001588 ret = btrfs_add_ordered_extent(inode, cur_offset,
1589 disk_bytenr, num_bytes,
1590 num_bytes,
1591 BTRFS_ORDERED_PREALLOC);
Nikolay Borisov762bf092019-08-22 17:24:20 +03001592 if (ret) {
1593 btrfs_drop_extent_cache(BTRFS_I(inode),
1594 cur_offset,
1595 cur_offset + num_bytes - 1,
1596 0);
1597 goto error;
1598 }
Yan Zhengd899e052008-10-30 14:25:28 -04001599 } else {
Nikolay Borisovbb55f622019-08-05 17:47:05 +03001600 ret = btrfs_add_ordered_extent(inode, cur_offset,
1601 disk_bytenr, num_bytes,
1602 num_bytes,
1603 BTRFS_ORDERED_NOCOW);
Nikolay Borisov762bf092019-08-22 17:24:20 +03001604 if (ret)
1605 goto error;
Yan Zhengd899e052008-10-30 14:25:28 -04001606 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001607
Filipe Mananaf78c4362016-05-09 13:15:41 +01001608 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001609 btrfs_dec_nocow_writers(fs_info, disk_bytenr);
Nikolay Borisov762bf092019-08-22 17:24:20 +03001610 nocow = false;
Chris Mason771ed682008-11-06 22:02:51 -05001611
Yan, Zhengefa56462010-05-16 10:49:59 -04001612 if (root->root_key.objectid ==
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001613 BTRFS_DATA_RELOC_TREE_OBJECTID)
1614 /*
1615 * Error handled later, as we must prevent
1616 * extent_clear_unlock_delalloc() in error handler
1617 * from freeing metadata of created ordered extent.
1618 */
Yan, Zhengefa56462010-05-16 10:49:59 -04001619 ret = btrfs_reloc_clone_csums(inode, cur_offset,
1620 num_bytes);
Yan, Zhengefa56462010-05-16 10:49:59 -04001621
Josef Bacikc2790a22013-07-29 11:20:47 -04001622 extent_clear_unlock_delalloc(inode, cur_offset,
Nikolay Borisov74e91942019-07-17 16:18:16 +03001623 cur_offset + num_bytes - 1,
Josef Bacikc2790a22013-07-29 11:20:47 -04001624 locked_page, EXTENT_LOCKED |
Wang Xiaoguang18513092016-07-25 15:51:40 +08001625 EXTENT_DELALLOC |
1626 EXTENT_CLEAR_DATA_RESV,
1627 PAGE_UNLOCK | PAGE_SET_PRIVATE2);
1628
Yan Zheng80ff3852008-10-30 14:20:02 -04001629 cur_offset = extent_end;
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001630
1631 /*
1632 * btrfs_reloc_clone_csums() error, now we're OK to call error
1633 * handler, as metadata for created ordered extent will only
1634 * be freed by btrfs_finish_ordered_io().
1635 */
1636 if (ret)
1637 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001638 if (cur_offset > end)
1639 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001640 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001641 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001642
Robbie Ko506481b2018-10-30 18:04:04 +08001643 if (cur_offset <= end && cow_start == (u64)-1)
Yan Zheng80ff3852008-10-30 14:20:02 -04001644 cow_start = cur_offset;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001645
Yan Zheng80ff3852008-10-30 14:20:02 -04001646 if (cow_start != (u64)-1) {
Robbie Ko506481b2018-10-30 18:04:04 +08001647 cur_offset = end;
Nikolay Borisov74e91942019-07-17 16:18:16 +03001648 ret = cow_file_range(inode, locked_page, cow_start, end,
Nikolay Borisov330a5822019-07-17 16:18:17 +03001649 page_started, nr_written, 1);
Josef Bacikd788a342013-10-25 16:55:08 -04001650 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001651 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001652 }
1653
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001654error:
Nikolay Borisov762bf092019-08-22 17:24:20 +03001655 if (nocow)
1656 btrfs_dec_nocow_writers(fs_info, disk_bytenr);
1657
Josef Bacik17ca04a2012-05-31 15:58:55 -04001658 if (ret && cur_offset < end)
Nikolay Borisov74e91942019-07-17 16:18:16 +03001659 extent_clear_unlock_delalloc(inode, cur_offset, end,
Josef Bacikc2790a22013-07-29 11:20:47 -04001660 locked_page, EXTENT_LOCKED |
Josef Bacik151a41b2013-07-29 13:22:24 -04001661 EXTENT_DELALLOC | EXTENT_DEFRAG |
1662 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
1663 PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -04001664 PAGE_SET_WRITEBACK |
1665 PAGE_END_WRITEBACK);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001666 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001667 return ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001668}
1669
Wang Shilong47059d92014-07-03 18:22:07 +08001670static inline int need_force_cow(struct inode *inode, u64 start, u64 end)
1671{
1672
1673 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
1674 !(BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC))
1675 return 0;
1676
1677 /*
1678 * @defrag_bytes is a hint value, no spinlock held here,
1679 * if is not zero, it means the file is defragging.
1680 * Force cow if given extent needs to be defragged.
1681 */
1682 if (BTRFS_I(inode)->defrag_bytes &&
1683 test_range_bit(&BTRFS_I(inode)->io_tree, start, end,
1684 EXTENT_DEFRAG, 0, NULL))
1685 return 1;
1686
1687 return 0;
1688}
1689
Chris Masond352ac62008-09-29 15:18:18 -04001690/*
Nikolay Borisov5eaad972018-11-01 14:09:46 +02001691 * Function to process delayed allocation (create CoW) for ranges which are
1692 * being touched for the first time.
Chris Masond352ac62008-09-29 15:18:18 -04001693 */
Nikolay Borisovbc9a8bf2018-12-19 09:50:20 +02001694int btrfs_run_delalloc_range(struct inode *inode, struct page *locked_page,
Nikolay Borisov5eaad972018-11-01 14:09:46 +02001695 u64 start, u64 end, int *page_started, unsigned long *nr_written,
1696 struct writeback_control *wbc)
Chris Masonbe20aa92007-12-17 20:14:01 -05001697{
Chris Masonbe20aa92007-12-17 20:14:01 -05001698 int ret;
Wang Shilong47059d92014-07-03 18:22:07 +08001699 int force_cow = need_force_cow(inode, start, end);
Liu Bof82b7352017-10-23 23:18:16 -06001700 unsigned int write_flags = wbc_to_write_flags(wbc);
Chris Masona2135012008-06-25 16:01:30 -04001701
Wang Shilong47059d92014-07-03 18:22:07 +08001702 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW && !force_cow) {
Chris Masonc8b97812008-10-29 14:49:59 -04001703 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001704 page_started, 1, nr_written);
Wang Shilong47059d92014-07-03 18:22:07 +08001705 } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC && !force_cow) {
Yan Zhengd899e052008-10-30 14:25:28 -04001706 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001707 page_started, 0, nr_written);
Qu Wenruo42c16da2019-07-01 05:12:46 +00001708 } else if (!inode_can_compress(inode) ||
1709 !inode_need_compress(inode, start, end)) {
Nikolay Borisov74e91942019-07-17 16:18:16 +03001710 ret = cow_file_range(inode, locked_page, start, end,
Nikolay Borisov330a5822019-07-17 16:18:17 +03001711 page_started, nr_written, 1);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001712 } else {
1713 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1714 &BTRFS_I(inode)->runtime_flags);
Chris Mason771ed682008-11-06 22:02:51 -05001715 ret = cow_file_range_async(inode, locked_page, start, end,
Liu Bof82b7352017-10-23 23:18:16 -06001716 page_started, nr_written,
1717 write_flags);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001718 }
Qu Wenruo524272602017-03-08 10:25:52 +08001719 if (ret)
Nikolay Borisovd1051d62018-11-21 17:10:52 +02001720 btrfs_cleanup_ordered_extents(inode, locked_page, start,
1721 end - start + 1);
Chris Masonb888db22007-08-27 16:49:44 -04001722 return ret;
1723}
1724
Nikolay Borisovabbb55f2018-11-01 14:09:53 +02001725void btrfs_split_delalloc_extent(struct inode *inode,
1726 struct extent_state *orig, u64 split)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001727{
Josef Bacikdcab6a32015-02-11 15:08:59 -05001728 u64 size;
1729
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001730 /* not delalloc, ignore it */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001731 if (!(orig->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001732 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001733
Josef Bacikdcab6a32015-02-11 15:08:59 -05001734 size = orig->end - orig->start + 1;
1735 if (size > BTRFS_MAX_EXTENT_SIZE) {
David Sterba823bb202017-01-04 11:09:51 +01001736 u32 num_extents;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001737 u64 new_size;
1738
1739 /*
Nikolay Borisov5c848192018-11-01 14:09:52 +02001740 * See the explanation in btrfs_merge_delalloc_extent, the same
Josef Bacikba117212015-03-13 15:01:24 -04001741 * applies here, just in reverse.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001742 */
1743 new_size = orig->end - split + 1;
David Sterba823bb202017-01-04 11:09:51 +01001744 num_extents = count_max_extents(new_size);
Josef Bacikba117212015-03-13 15:01:24 -04001745 new_size = split - orig->start;
David Sterba823bb202017-01-04 11:09:51 +01001746 num_extents += count_max_extents(new_size);
1747 if (count_max_extents(size) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001748 return;
1749 }
1750
Josef Bacik9e0baf62011-07-15 15:16:44 +00001751 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04001752 btrfs_mod_outstanding_extents(BTRFS_I(inode), 1);
Josef Bacik9e0baf62011-07-15 15:16:44 +00001753 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001754}
1755
1756/*
Nikolay Borisov5c848192018-11-01 14:09:52 +02001757 * Handle merged delayed allocation extents so we can keep track of new extents
1758 * that are just merged onto old extents, such as when we are doing sequential
1759 * writes, so we can properly account for the metadata space we'll need.
Josef Bacik9ed74f22009-09-11 16:12:44 -04001760 */
Nikolay Borisov5c848192018-11-01 14:09:52 +02001761void btrfs_merge_delalloc_extent(struct inode *inode, struct extent_state *new,
1762 struct extent_state *other)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001763{
Josef Bacikdcab6a32015-02-11 15:08:59 -05001764 u64 new_size, old_size;
David Sterba823bb202017-01-04 11:09:51 +01001765 u32 num_extents;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001766
Josef Bacik9ed74f22009-09-11 16:12:44 -04001767 /* not delalloc, ignore it */
1768 if (!(other->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001769 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001770
Josef Bacik8461a3d2015-03-13 15:12:08 -04001771 if (new->start > other->start)
1772 new_size = new->end - other->start + 1;
1773 else
1774 new_size = other->end - new->start + 1;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001775
1776 /* we're not bigger than the max, unreserve the space and go */
1777 if (new_size <= BTRFS_MAX_EXTENT_SIZE) {
1778 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04001779 btrfs_mod_outstanding_extents(BTRFS_I(inode), -1);
Josef Bacikdcab6a32015-02-11 15:08:59 -05001780 spin_unlock(&BTRFS_I(inode)->lock);
1781 return;
1782 }
1783
1784 /*
Josef Bacikba117212015-03-13 15:01:24 -04001785 * We have to add up either side to figure out how many extents were
1786 * accounted for before we merged into one big extent. If the number of
1787 * extents we accounted for is <= the amount we need for the new range
1788 * then we can return, otherwise drop. Think of it like this
1789 *
1790 * [ 4k][MAX_SIZE]
1791 *
1792 * So we've grown the extent by a MAX_SIZE extent, this would mean we
1793 * need 2 outstanding extents, on one side we have 1 and the other side
1794 * we have 1 so they are == and we can return. But in this case
1795 *
1796 * [MAX_SIZE+4k][MAX_SIZE+4k]
1797 *
1798 * Each range on their own accounts for 2 extents, but merged together
1799 * they are only 3 extents worth of accounting, so we need to drop in
1800 * this case.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001801 */
Josef Bacikba117212015-03-13 15:01:24 -04001802 old_size = other->end - other->start + 1;
David Sterba823bb202017-01-04 11:09:51 +01001803 num_extents = count_max_extents(old_size);
Josef Bacikba117212015-03-13 15:01:24 -04001804 old_size = new->end - new->start + 1;
David Sterba823bb202017-01-04 11:09:51 +01001805 num_extents += count_max_extents(old_size);
1806 if (count_max_extents(new_size) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001807 return;
1808
Josef Bacik9e0baf62011-07-15 15:16:44 +00001809 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04001810 btrfs_mod_outstanding_extents(BTRFS_I(inode), -1);
Josef Bacik9e0baf62011-07-15 15:16:44 +00001811 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001812}
1813
Miao Xieeb73c1b2013-05-15 07:48:22 +00001814static void btrfs_add_delalloc_inodes(struct btrfs_root *root,
1815 struct inode *inode)
1816{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001817 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1818
Miao Xieeb73c1b2013-05-15 07:48:22 +00001819 spin_lock(&root->delalloc_lock);
1820 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1821 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1822 &root->delalloc_inodes);
1823 set_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1824 &BTRFS_I(inode)->runtime_flags);
1825 root->nr_delalloc_inodes++;
1826 if (root->nr_delalloc_inodes == 1) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001827 spin_lock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001828 BUG_ON(!list_empty(&root->delalloc_root));
1829 list_add_tail(&root->delalloc_root,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001830 &fs_info->delalloc_roots);
1831 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001832 }
1833 }
1834 spin_unlock(&root->delalloc_lock);
1835}
1836
Nikolay Borisov2b877332018-04-27 12:21:51 +03001837
1838void __btrfs_del_delalloc_inode(struct btrfs_root *root,
1839 struct btrfs_inode *inode)
Miao Xieeb73c1b2013-05-15 07:48:22 +00001840{
David Sterba3ffbd682018-06-29 10:56:42 +02001841 struct btrfs_fs_info *fs_info = root->fs_info;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001842
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001843 if (!list_empty(&inode->delalloc_inodes)) {
1844 list_del_init(&inode->delalloc_inodes);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001845 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001846 &inode->runtime_flags);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001847 root->nr_delalloc_inodes--;
1848 if (!root->nr_delalloc_inodes) {
Nikolay Borisov7c8a0d32018-04-27 12:21:52 +03001849 ASSERT(list_empty(&root->delalloc_inodes));
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001850 spin_lock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001851 BUG_ON(list_empty(&root->delalloc_root));
1852 list_del_init(&root->delalloc_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001853 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001854 }
1855 }
Nikolay Borisov2b877332018-04-27 12:21:51 +03001856}
1857
1858static void btrfs_del_delalloc_inode(struct btrfs_root *root,
1859 struct btrfs_inode *inode)
1860{
1861 spin_lock(&root->delalloc_lock);
1862 __btrfs_del_delalloc_inode(root, inode);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001863 spin_unlock(&root->delalloc_lock);
1864}
1865
Chris Masond352ac62008-09-29 15:18:18 -04001866/*
Nikolay Borisove06a1fc2018-11-01 14:09:50 +02001867 * Properly track delayed allocation bytes in the inode and to maintain the
1868 * list of inodes that have pending delalloc work to be done.
Chris Masond352ac62008-09-29 15:18:18 -04001869 */
Nikolay Borisove06a1fc2018-11-01 14:09:50 +02001870void btrfs_set_delalloc_extent(struct inode *inode, struct extent_state *state,
1871 unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001872{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001873 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1874
Wang Shilong47059d92014-07-03 18:22:07 +08001875 if ((*bits & EXTENT_DEFRAG) && !(*bits & EXTENT_DELALLOC))
1876 WARN_ON(1);
Chris Mason75eff682008-12-15 15:54:40 -05001877 /*
1878 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001879 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001880 * bit, which is only set or cleared with irqs on
1881 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001882 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001883 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001884 u64 len = state->end + 1 - state->start;
Josef Bacik8b62f872017-10-19 14:15:55 -04001885 u32 num_extents = count_max_extents(len);
Nikolay Borisov70ddc552017-02-20 13:50:35 +02001886 bool do_list = !btrfs_is_free_space_inode(BTRFS_I(inode));
Josef Bacik9ed74f22009-09-11 16:12:44 -04001887
Josef Bacik8b62f872017-10-19 14:15:55 -04001888 spin_lock(&BTRFS_I(inode)->lock);
1889 btrfs_mod_outstanding_extents(BTRFS_I(inode), num_extents);
1890 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik287a0ab2010-03-19 18:07:23 +00001891
Josef Bacik6a3891c2015-03-16 17:38:52 -04001892 /* For sanity tests */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001893 if (btrfs_is_testing(fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04001894 return;
1895
Nikolay Borisov104b4e52017-06-20 21:01:20 +03001896 percpu_counter_add_batch(&fs_info->delalloc_bytes, len,
1897 fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001898 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001899 BTRFS_I(inode)->delalloc_bytes += len;
Wang Shilong47059d92014-07-03 18:22:07 +08001900 if (*bits & EXTENT_DEFRAG)
1901 BTRFS_I(inode)->defrag_bytes += len;
Miao Xiedf0af1a2013-01-29 10:11:59 +00001902 if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00001903 &BTRFS_I(inode)->runtime_flags))
1904 btrfs_add_delalloc_inodes(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001905 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001906 }
Filipe Mananaa7e3b972017-04-03 10:45:46 +01001907
1908 if (!(state->state & EXTENT_DELALLOC_NEW) &&
1909 (*bits & EXTENT_DELALLOC_NEW)) {
1910 spin_lock(&BTRFS_I(inode)->lock);
1911 BTRFS_I(inode)->new_delalloc_bytes += state->end + 1 -
1912 state->start;
1913 spin_unlock(&BTRFS_I(inode)->lock);
1914 }
Chris Mason291d6732008-01-29 15:55:23 -05001915}
1916
Chris Masond352ac62008-09-29 15:18:18 -04001917/*
Nikolay Borisova36bb5f2018-11-01 14:09:51 +02001918 * Once a range is no longer delalloc this function ensures that proper
1919 * accounting happens.
Chris Masond352ac62008-09-29 15:18:18 -04001920 */
Nikolay Borisova36bb5f2018-11-01 14:09:51 +02001921void btrfs_clear_delalloc_extent(struct inode *vfs_inode,
1922 struct extent_state *state, unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001923{
Nikolay Borisova36bb5f2018-11-01 14:09:51 +02001924 struct btrfs_inode *inode = BTRFS_I(vfs_inode);
1925 struct btrfs_fs_info *fs_info = btrfs_sb(vfs_inode->i_sb);
Wang Shilong47059d92014-07-03 18:22:07 +08001926 u64 len = state->end + 1 - state->start;
David Sterba823bb202017-01-04 11:09:51 +01001927 u32 num_extents = count_max_extents(len);
Wang Shilong47059d92014-07-03 18:22:07 +08001928
Filipe Manana4a4b9642017-07-27 19:52:55 +01001929 if ((state->state & EXTENT_DEFRAG) && (*bits & EXTENT_DEFRAG)) {
1930 spin_lock(&inode->lock);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001931 inode->defrag_bytes -= len;
Filipe Manana4a4b9642017-07-27 19:52:55 +01001932 spin_unlock(&inode->lock);
1933 }
Wang Shilong47059d92014-07-03 18:22:07 +08001934
Chris Mason75eff682008-12-15 15:54:40 -05001935 /*
1936 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001937 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001938 * bit, which is only set or cleared with irqs on
1939 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001940 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001941 struct btrfs_root *root = inode->root;
Liu Bo83eea1f2012-07-10 05:28:39 -06001942 bool do_list = !btrfs_is_free_space_inode(inode);
Chris Masonbcbfce82008-04-22 13:26:47 -04001943
Josef Bacik8b62f872017-10-19 14:15:55 -04001944 spin_lock(&inode->lock);
1945 btrfs_mod_outstanding_extents(inode, -num_extents);
1946 spin_unlock(&inode->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001947
Josef Bacikb6d08f02013-09-27 14:57:43 -04001948 /*
1949 * We don't reserve metadata space for space cache inodes so we
Andrea Gelmini52042d82018-11-28 12:05:13 +01001950 * don't need to call delalloc_release_metadata if there is an
Josef Bacikb6d08f02013-09-27 14:57:43 -04001951 * error.
1952 */
Filipe Mananaa315e682017-03-06 23:04:20 +00001953 if (*bits & EXTENT_CLEAR_META_RESV &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001954 root != fs_info->tree_root)
Qu Wenruo43b18592017-12-12 15:34:32 +08001955 btrfs_delalloc_release_metadata(inode, len, false);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001956
Josef Bacik6a3891c2015-03-16 17:38:52 -04001957 /* For sanity tests. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001958 if (btrfs_is_testing(fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04001959 return;
1960
Filipe Mananaa315e682017-03-06 23:04:20 +00001961 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID &&
1962 do_list && !(state->state & EXTENT_NORESERVE) &&
1963 (*bits & EXTENT_CLEAR_DATA_RESV))
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001964 btrfs_free_reserved_data_space_noquota(
1965 &inode->vfs_inode,
Qu Wenruo51773be2015-10-08 18:19:37 +08001966 state->start, len);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001967
Nikolay Borisov104b4e52017-06-20 21:01:20 +03001968 percpu_counter_add_batch(&fs_info->delalloc_bytes, -len,
1969 fs_info->delalloc_batch);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001970 spin_lock(&inode->lock);
1971 inode->delalloc_bytes -= len;
1972 if (do_list && inode->delalloc_bytes == 0 &&
Miao Xiedf0af1a2013-01-29 10:11:59 +00001973 test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001974 &inode->runtime_flags))
Miao Xieeb73c1b2013-05-15 07:48:22 +00001975 btrfs_del_delalloc_inode(root, inode);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001976 spin_unlock(&inode->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001977 }
Filipe Mananaa7e3b972017-04-03 10:45:46 +01001978
1979 if ((state->state & EXTENT_DELALLOC_NEW) &&
1980 (*bits & EXTENT_DELALLOC_NEW)) {
1981 spin_lock(&inode->lock);
1982 ASSERT(inode->new_delalloc_bytes >= len);
1983 inode->new_delalloc_bytes -= len;
1984 spin_unlock(&inode->lock);
1985 }
Chris Mason291d6732008-01-29 15:55:23 -05001986}
1987
Chris Masond352ac62008-09-29 15:18:18 -04001988/*
Nikolay Borisovda12fe52018-11-27 20:57:58 +02001989 * btrfs_bio_fits_in_stripe - Checks whether the size of the given bio will fit
1990 * in a chunk's stripe. This function ensures that bios do not span a
1991 * stripe/chunk
Liu Bo6f034ec2016-06-22 18:31:49 -07001992 *
Nikolay Borisovda12fe52018-11-27 20:57:58 +02001993 * @page - The page we are about to add to the bio
1994 * @size - size we want to add to the bio
1995 * @bio - bio we want to ensure is smaller than a stripe
1996 * @bio_flags - flags of the bio
1997 *
1998 * return 1 if page cannot be added to the bio
1999 * return 0 if page can be added to the bio
Liu Bo6f034ec2016-06-22 18:31:49 -07002000 * return error otherwise
Chris Masond352ac62008-09-29 15:18:18 -04002001 */
Nikolay Borisovda12fe52018-11-27 20:57:58 +02002002int btrfs_bio_fits_in_stripe(struct page *page, size_t size, struct bio *bio,
2003 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04002004{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002005 struct inode *inode = page->mapping->host;
2006 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Kent Overstreet4f024f32013-10-11 15:44:27 -07002007 u64 logical = (u64)bio->bi_iter.bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04002008 u64 length = 0;
2009 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04002010 int ret;
Nikolay Borisov89b798a2019-06-03 12:05:05 +03002011 struct btrfs_io_geometry geom;
Chris Mason239b14b2008-03-24 15:02:07 -04002012
Chris Mason771ed682008-11-06 22:02:51 -05002013 if (bio_flags & EXTENT_BIO_COMPRESSED)
2014 return 0;
2015
Kent Overstreet4f024f32013-10-11 15:44:27 -07002016 length = bio->bi_iter.bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04002017 map_length = length;
Nikolay Borisov89b798a2019-06-03 12:05:05 +03002018 ret = btrfs_get_io_geometry(fs_info, btrfs_op(bio), logical, map_length,
2019 &geom);
Liu Bo6f034ec2016-06-22 18:31:49 -07002020 if (ret < 0)
2021 return ret;
Nikolay Borisov89b798a2019-06-03 12:05:05 +03002022
2023 if (geom.len < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04002024 return 1;
Jeff Mahoney3444a972011-10-03 23:23:13 -04002025 return 0;
Chris Mason239b14b2008-03-24 15:02:07 -04002026}
2027
Chris Masond352ac62008-09-29 15:18:18 -04002028/*
2029 * in order to insert checksums into the metadata in large chunks,
2030 * we wait until bio submission time. All the pages in the bio are
2031 * checksummed and sums are attached onto the ordered extent record.
2032 *
2033 * At IO completion time the cums attached on the ordered extent record
2034 * are inserted into the btree
2035 */
David Sterbad0ee3932018-03-08 14:35:48 +01002036static blk_status_t btrfs_submit_bio_start(void *private_data, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04002037 u64 bio_offset)
Chris Mason065631f2008-02-20 12:07:25 -05002038{
Josef Bacikc6100a42017-05-05 11:57:13 -04002039 struct inode *inode = private_data;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002040 blk_status_t ret = 0;
Chris Masone0156402008-04-16 11:15:20 -04002041
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002042 ret = btrfs_csum_one_bio(inode, bio, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002043 BUG_ON(ret); /* -ENOMEM */
Chris Mason4a69a412008-11-06 22:03:00 -05002044 return 0;
2045}
Chris Masone0156402008-04-16 11:15:20 -04002046
Chris Mason4a69a412008-11-06 22:03:00 -05002047/*
Chris Masoncad321a2008-12-17 14:51:42 -05002048 * extent_io.c submission hook. This does the right thing for csum calculation
Liu Bo4c274bc2017-11-01 17:19:27 -06002049 * on write, or reading the csums from the tree before a read.
2050 *
2051 * Rules about async/sync submit,
2052 * a) read: sync submit
2053 *
2054 * b) write without checksum: sync submit
2055 *
2056 * c) write with checksum:
2057 * c-1) if bio is issued by fsync: sync submit
2058 * (sync_writers != 0)
2059 *
2060 * c-2) if root is reloc root: sync submit
2061 * (only in case of buffered IO)
2062 *
2063 * c-3) otherwise: async submit
Chris Masond352ac62008-09-29 15:18:18 -04002064 */
Nikolay Borisova56b1c72019-04-10 17:24:39 +03002065static blk_status_t btrfs_submit_bio_hook(struct inode *inode, struct bio *bio,
Nikolay Borisov50489a52019-04-10 19:46:04 +03002066 int mirror_num,
2067 unsigned long bio_flags)
2068
Chris Mason44b8bd72008-04-16 11:14:51 -04002069{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002070 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason44b8bd72008-04-16 11:14:51 -04002071 struct btrfs_root *root = BTRFS_I(inode)->root;
Chandan Rajendra0d51e282015-07-27 15:26:43 +05302072 enum btrfs_wq_endio_type metadata = BTRFS_WQ_ENDIO_DATA;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002073 blk_status_t ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04002074 int skip_sum;
Josef Bacikb812ce22012-11-16 13:56:32 -05002075 int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
Chris Mason44b8bd72008-04-16 11:14:51 -04002076
Christoph Hellwig6cbff002009-04-17 10:37:41 +02002077 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masoncad321a2008-12-17 14:51:42 -05002078
Nikolay Borisov70ddc552017-02-20 13:50:35 +02002079 if (btrfs_is_free_space_inode(BTRFS_I(inode)))
Chandan Rajendra0d51e282015-07-27 15:26:43 +05302080 metadata = BTRFS_WQ_ENDIO_FREE_SPACE;
Jeff Mahoney04173412011-10-03 23:23:12 -04002081
Mike Christie37226b22016-06-05 14:31:52 -05002082 if (bio_op(bio) != REQ_OP_WRITE) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002083 ret = btrfs_bio_wq_end_io(fs_info, bio, metadata);
Josef Bacik5fd02042012-05-02 14:00:54 -04002084 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01002085 goto out;
Josef Bacik5fd02042012-05-02 14:00:54 -04002086
Chris Masond20f7042008-12-08 16:58:54 -05002087 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Stefan Behrens61891922012-11-05 18:51:52 +01002088 ret = btrfs_submit_compressed_read(inode, bio,
2089 mirror_num,
2090 bio_flags);
2091 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00002092 } else if (!skip_sum) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002093 ret = btrfs_lookup_bio_sums(inode, bio, NULL);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00002094 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01002095 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00002096 }
Chris Mason4d1b5fb2008-08-20 09:44:52 -04002097 goto mapit;
Josef Bacikb812ce22012-11-16 13:56:32 -05002098 } else if (async && !skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05002099 /* csum items have already been cloned */
2100 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
2101 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04002102 /* we're doing a write, do the async checksumming */
Josef Bacikc6100a42017-05-05 11:57:13 -04002103 ret = btrfs_wq_submit_bio(fs_info, bio, mirror_num, bio_flags,
Nikolay Borisove7681162019-04-10 17:24:41 +03002104 0, inode, btrfs_submit_bio_start);
Stefan Behrens61891922012-11-05 18:51:52 +01002105 goto out;
Josef Bacikb812ce22012-11-16 13:56:32 -05002106 } else if (!skip_sum) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002107 ret = btrfs_csum_one_bio(inode, bio, 0, 0);
Josef Bacikb812ce22012-11-16 13:56:32 -05002108 if (ret)
2109 goto out;
Chris Mason19b9bdb2008-10-30 14:23:13 -04002110 }
2111
Chris Mason0b86a832008-03-24 15:01:56 -04002112mapit:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002113 ret = btrfs_map_bio(fs_info, bio, mirror_num, 0);
Stefan Behrens61891922012-11-05 18:51:52 +01002114
2115out:
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002116 if (ret) {
2117 bio->bi_status = ret;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002118 bio_endio(bio);
2119 }
Stefan Behrens61891922012-11-05 18:51:52 +01002120 return ret;
Chris Mason065631f2008-02-20 12:07:25 -05002121}
Chris Mason6885f302008-02-20 16:11:05 -05002122
Chris Masond352ac62008-09-29 15:18:18 -04002123/*
2124 * given a list of ordered sums record them in the inode. This happens
2125 * at IO completion time based on sums calculated at bio submission time.
2126 */
Chris Masonba1da2f2008-07-17 12:54:15 -04002127static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
David Sterbadf9f6282017-02-10 19:35:37 +01002128 struct inode *inode, struct list_head *list)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002129{
Chris Masone6dcd2d2008-07-17 12:53:50 -04002130 struct btrfs_ordered_sum *sum;
Nikolay Borisovac01f262018-01-08 10:59:43 +02002131 int ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002132
Qinghuang Fengc6e30872009-01-21 10:59:08 -05002133 list_for_each_entry(sum, list, list) {
David Sterba7c2871a2017-11-08 01:07:43 +01002134 trans->adding_csums = true;
Nikolay Borisovac01f262018-01-08 10:59:43 +02002135 ret = btrfs_csum_file_blocks(trans,
Chris Masond20f7042008-12-08 16:58:54 -05002136 BTRFS_I(inode)->root->fs_info->csum_root, sum);
David Sterba7c2871a2017-11-08 01:07:43 +01002137 trans->adding_csums = false;
Nikolay Borisovac01f262018-01-08 10:59:43 +02002138 if (ret)
2139 return ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002140 }
2141 return 0;
2142}
2143
Josef Bacik2ac55d42010-02-03 19:33:23 +00002144int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
Filipe Mananae3b8a482017-11-04 00:16:59 +00002145 unsigned int extra_bits,
Nikolay Borisov330a5822019-07-17 16:18:17 +03002146 struct extent_state **cached_state)
Chris Masonea8c2812008-08-04 23:17:27 -04002147{
Johannes Thumshirnfdb1e122018-12-05 15:23:04 +01002148 WARN_ON(PAGE_ALIGNED(end));
Chris Masonea8c2812008-08-04 23:17:27 -04002149 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
Filipe Mananae3b8a482017-11-04 00:16:59 +00002150 extra_bits, cached_state);
Chris Masonea8c2812008-08-04 23:17:27 -04002151}
2152
Chris Masond352ac62008-09-29 15:18:18 -04002153/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04002154struct btrfs_writepage_fixup {
2155 struct page *page;
2156 struct btrfs_work work;
2157};
2158
Christoph Hellwigb2950862008-12-02 09:54:17 -05002159static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04002160{
2161 struct btrfs_writepage_fixup *fixup;
2162 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002163 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08002164 struct extent_changeset *data_reserved = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04002165 struct page *page;
2166 struct inode *inode;
2167 u64 page_start;
2168 u64 page_end;
Jeff Mahoney87826df2012-02-15 16:23:57 +01002169 int ret;
Chris Mason247e7432008-07-17 12:53:51 -04002170
2171 fixup = container_of(work, struct btrfs_writepage_fixup, work);
2172 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -04002173again:
Chris Mason247e7432008-07-17 12:53:51 -04002174 lock_page(page);
2175 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
2176 ClearPageChecked(page);
2177 goto out_page;
2178 }
2179
2180 inode = page->mapping->host;
2181 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002182 page_end = page_offset(page) + PAGE_SIZE - 1;
Chris Mason247e7432008-07-17 12:53:51 -04002183
David Sterbaff13db42015-12-03 14:30:40 +01002184 lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002185 &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04002186
2187 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04002188 if (PagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04002189 goto out;
Chris Mason4a096752008-07-21 10:29:44 -04002190
Nikolay Borisova776c6f2017-02-20 13:50:49 +02002191 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002192 PAGE_SIZE);
Chris Mason4a096752008-07-21 10:29:44 -04002193 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00002194 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
David Sterbae43bbe52017-12-12 21:43:52 +01002195 page_end, &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04002196 unlock_page(page);
2197 btrfs_start_ordered_extent(inode, ordered, 1);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002198 btrfs_put_ordered_extent(ordered);
Chris Mason4a096752008-07-21 10:29:44 -04002199 goto again;
2200 }
Chris Mason247e7432008-07-17 12:53:51 -04002201
Qu Wenruo364ecf32017-02-27 15:10:38 +08002202 ret = btrfs_delalloc_reserve_space(inode, &data_reserved, page_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002203 PAGE_SIZE);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002204 if (ret) {
2205 mapping_set_error(page->mapping, ret);
2206 end_extent_writepage(page, ret, page_start, page_end);
2207 ClearPageChecked(page);
2208 goto out;
2209 }
2210
Nikolay Borisovf3038ee2017-12-05 09:29:19 +02002211 ret = btrfs_set_extent_delalloc(inode, page_start, page_end, 0,
Nikolay Borisov330a5822019-07-17 16:18:17 +03002212 &cached_state);
Nikolay Borisovf3038ee2017-12-05 09:29:19 +02002213 if (ret) {
2214 mapping_set_error(page->mapping, ret);
2215 end_extent_writepage(page, ret, page_start, page_end);
2216 ClearPageChecked(page);
2217 goto out;
2218 }
2219
Chris Mason247e7432008-07-17 12:53:51 -04002220 ClearPageChecked(page);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002221 set_page_dirty(page);
Qu Wenruo8702ba92019-10-14 14:34:51 +08002222 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE);
Chris Mason247e7432008-07-17 12:53:51 -04002223out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00002224 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01002225 &cached_state);
Chris Mason247e7432008-07-17 12:53:51 -04002226out_page:
2227 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002228 put_page(page);
Miao Xieb897abe2011-01-26 16:19:22 +08002229 kfree(fixup);
Qu Wenruo364ecf32017-02-27 15:10:38 +08002230 extent_changeset_free(data_reserved);
Chris Mason247e7432008-07-17 12:53:51 -04002231}
2232
2233/*
2234 * There are a few paths in the higher layers of the kernel that directly
2235 * set the page dirty bit without asking the filesystem if it is a
2236 * good idea. This causes problems because we want to make sure COW
2237 * properly happens and the data=ordered rules are followed.
2238 *
Chris Masonc8b97812008-10-29 14:49:59 -04002239 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04002240 * hasn't been properly setup for IO. We kick off an async process
2241 * to fix it up. The async helper will wait for ordered extents, set
2242 * the delalloc bit and make it safe to write the page.
2243 */
Nikolay Borisovd75855b2018-11-01 14:09:47 +02002244int btrfs_writepage_cow_fixup(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04002245{
2246 struct inode *inode = page->mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002247 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason247e7432008-07-17 12:53:51 -04002248 struct btrfs_writepage_fixup *fixup;
Chris Mason247e7432008-07-17 12:53:51 -04002249
Chris Mason8b62b722009-09-02 16:53:46 -04002250 /* this page is properly in the ordered list */
2251 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04002252 return 0;
2253
2254 if (PageChecked(page))
2255 return -EAGAIN;
2256
2257 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
2258 if (!fixup)
2259 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04002260
Chris Mason247e7432008-07-17 12:53:51 -04002261 SetPageChecked(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002262 get_page(page);
Liu Bo9e0af232014-08-15 23:36:53 +08002263 btrfs_init_work(&fixup->work, btrfs_fixup_helper,
2264 btrfs_writepage_fixup_worker, NULL, NULL);
Chris Mason247e7432008-07-17 12:53:51 -04002265 fixup->page = page;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002266 btrfs_queue_work(fs_info->fixup_workers, &fixup->work);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002267 return -EBUSY;
Chris Mason247e7432008-07-17 12:53:51 -04002268}
2269
Yan Zhengd899e052008-10-30 14:25:28 -04002270static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
2271 struct inode *inode, u64 file_pos,
2272 u64 disk_bytenr, u64 disk_num_bytes,
2273 u64 num_bytes, u64 ram_bytes,
2274 u8 compression, u8 encryption,
2275 u16 other_encoding, int extent_type)
2276{
2277 struct btrfs_root *root = BTRFS_I(inode)->root;
2278 struct btrfs_file_extent_item *fi;
2279 struct btrfs_path *path;
2280 struct extent_buffer *leaf;
2281 struct btrfs_key ins;
Qu Wenruoa12b8772017-02-27 15:10:37 +08002282 u64 qg_released;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002283 int extent_inserted = 0;
Yan Zhengd899e052008-10-30 14:25:28 -04002284 int ret;
2285
2286 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07002287 if (!path)
2288 return -ENOMEM;
Yan Zhengd899e052008-10-30 14:25:28 -04002289
Chris Masona1ed8352009-09-11 12:27:37 -04002290 /*
2291 * we may be replacing one extent in the tree with another.
2292 * The new extent is pinned in the extent map, and we don't want
2293 * to drop it from the cache until it is completely in the btree.
2294 *
2295 * So, tell btrfs_drop_extents to leave this extent in the cache.
2296 * the caller is expected to unpin it and allow it to be merged
2297 * with the others.
2298 */
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002299 ret = __btrfs_drop_extents(trans, root, inode, path, file_pos,
2300 file_pos + num_bytes, NULL, 0,
2301 1, sizeof(*fi), &extent_inserted);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002302 if (ret)
2303 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04002304
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002305 if (!extent_inserted) {
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002306 ins.objectid = btrfs_ino(BTRFS_I(inode));
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002307 ins.offset = file_pos;
2308 ins.type = BTRFS_EXTENT_DATA_KEY;
2309
2310 path->leave_spinning = 1;
2311 ret = btrfs_insert_empty_item(trans, root, path, &ins,
2312 sizeof(*fi));
2313 if (ret)
2314 goto out;
2315 }
Yan Zhengd899e052008-10-30 14:25:28 -04002316 leaf = path->nodes[0];
2317 fi = btrfs_item_ptr(leaf, path->slots[0],
2318 struct btrfs_file_extent_item);
2319 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
2320 btrfs_set_file_extent_type(leaf, fi, extent_type);
2321 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
2322 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
2323 btrfs_set_file_extent_offset(leaf, fi, 0);
2324 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
2325 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
2326 btrfs_set_file_extent_compression(leaf, fi, compression);
2327 btrfs_set_file_extent_encryption(leaf, fi, encryption);
2328 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
Chris Masonb9473432009-03-13 11:00:37 -04002329
Yan Zhengd899e052008-10-30 14:25:28 -04002330 btrfs_mark_buffer_dirty(leaf);
Josef Bacikce195332012-09-25 15:26:16 -04002331 btrfs_release_path(path);
Yan Zhengd899e052008-10-30 14:25:28 -04002332
2333 inode_add_bytes(inode, num_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -04002334
2335 ins.objectid = disk_bytenr;
2336 ins.offset = disk_num_bytes;
2337 ins.type = BTRFS_EXTENT_ITEM_KEY;
Qu Wenruoa12b8772017-02-27 15:10:37 +08002338
Qu Wenruo297d7502015-09-08 17:08:37 +08002339 /*
Qu Wenruo5846a3c2015-10-26 14:11:18 +08002340 * Release the reserved range from inode dirty range map, as it is
2341 * already moved into delayed_ref_head
Qu Wenruo297d7502015-09-08 17:08:37 +08002342 */
Qu Wenruoa12b8772017-02-27 15:10:37 +08002343 ret = btrfs_qgroup_release_data(inode, file_pos, ram_bytes);
2344 if (ret < 0)
2345 goto out;
2346 qg_released = ret;
Josef Bacik84f7d8e2017-09-29 15:43:49 -04002347 ret = btrfs_alloc_reserved_file_extent(trans, root,
2348 btrfs_ino(BTRFS_I(inode)),
2349 file_pos, qg_released, &ins);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002350out:
Yan Zhengd899e052008-10-30 14:25:28 -04002351 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04002352
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002353 return ret;
Yan Zhengd899e052008-10-30 14:25:28 -04002354}
2355
Liu Bo38c227d2013-01-29 03:18:40 +00002356/* snapshot-aware defrag */
2357struct sa_defrag_extent_backref {
2358 struct rb_node node;
2359 struct old_sa_defrag_extent *old;
2360 u64 root_id;
2361 u64 inum;
2362 u64 file_pos;
2363 u64 extent_offset;
2364 u64 num_bytes;
2365 u64 generation;
2366};
2367
2368struct old_sa_defrag_extent {
2369 struct list_head list;
2370 struct new_sa_defrag_extent *new;
2371
2372 u64 extent_offset;
2373 u64 bytenr;
2374 u64 offset;
2375 u64 len;
2376 int count;
2377};
2378
2379struct new_sa_defrag_extent {
2380 struct rb_root root;
2381 struct list_head head;
2382 struct btrfs_path *path;
2383 struct inode *inode;
2384 u64 file_pos;
2385 u64 len;
2386 u64 bytenr;
2387 u64 disk_len;
2388 u8 compress_type;
2389};
2390
2391static int backref_comp(struct sa_defrag_extent_backref *b1,
2392 struct sa_defrag_extent_backref *b2)
2393{
2394 if (b1->root_id < b2->root_id)
2395 return -1;
2396 else if (b1->root_id > b2->root_id)
2397 return 1;
2398
2399 if (b1->inum < b2->inum)
2400 return -1;
2401 else if (b1->inum > b2->inum)
2402 return 1;
2403
2404 if (b1->file_pos < b2->file_pos)
2405 return -1;
2406 else if (b1->file_pos > b2->file_pos)
2407 return 1;
2408
2409 /*
2410 * [------------------------------] ===> (a range of space)
2411 * |<--->| |<---->| =============> (fs/file tree A)
2412 * |<---------------------------->| ===> (fs/file tree B)
2413 *
2414 * A range of space can refer to two file extents in one tree while
2415 * refer to only one file extent in another tree.
2416 *
2417 * So we may process a disk offset more than one time(two extents in A)
2418 * and locate at the same extent(one extent in B), then insert two same
2419 * backrefs(both refer to the extent in B).
2420 */
2421 return 0;
2422}
2423
2424static void backref_insert(struct rb_root *root,
2425 struct sa_defrag_extent_backref *backref)
2426{
2427 struct rb_node **p = &root->rb_node;
2428 struct rb_node *parent = NULL;
2429 struct sa_defrag_extent_backref *entry;
2430 int ret;
2431
2432 while (*p) {
2433 parent = *p;
2434 entry = rb_entry(parent, struct sa_defrag_extent_backref, node);
2435
2436 ret = backref_comp(backref, entry);
2437 if (ret < 0)
2438 p = &(*p)->rb_left;
2439 else
2440 p = &(*p)->rb_right;
2441 }
2442
2443 rb_link_node(&backref->node, parent, p);
2444 rb_insert_color(&backref->node, root);
2445}
2446
2447/*
2448 * Note the backref might has changed, and in this case we just return 0.
2449 */
2450static noinline int record_one_backref(u64 inum, u64 offset, u64 root_id,
2451 void *ctx)
2452{
2453 struct btrfs_file_extent_item *extent;
Liu Bo38c227d2013-01-29 03:18:40 +00002454 struct old_sa_defrag_extent *old = ctx;
2455 struct new_sa_defrag_extent *new = old->new;
2456 struct btrfs_path *path = new->path;
2457 struct btrfs_key key;
2458 struct btrfs_root *root;
2459 struct sa_defrag_extent_backref *backref;
2460 struct extent_buffer *leaf;
2461 struct inode *inode = new->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002462 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002463 int slot;
2464 int ret;
2465 u64 extent_offset;
2466 u64 num_bytes;
2467
2468 if (BTRFS_I(inode)->root->root_key.objectid == root_id &&
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002469 inum == btrfs_ino(BTRFS_I(inode)))
Liu Bo38c227d2013-01-29 03:18:40 +00002470 return 0;
2471
2472 key.objectid = root_id;
2473 key.type = BTRFS_ROOT_ITEM_KEY;
2474 key.offset = (u64)-1;
2475
Liu Bo38c227d2013-01-29 03:18:40 +00002476 root = btrfs_read_fs_root_no_name(fs_info, &key);
2477 if (IS_ERR(root)) {
2478 if (PTR_ERR(root) == -ENOENT)
2479 return 0;
2480 WARN_ON(1);
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002481 btrfs_debug(fs_info, "inum=%llu, offset=%llu, root_id=%llu",
Liu Bo38c227d2013-01-29 03:18:40 +00002482 inum, offset, root_id);
2483 return PTR_ERR(root);
2484 }
2485
2486 key.objectid = inum;
2487 key.type = BTRFS_EXTENT_DATA_KEY;
2488 if (offset > (u64)-1 << 32)
2489 key.offset = 0;
2490 else
2491 key.offset = offset;
2492
2493 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05302494 if (WARN_ON(ret < 0))
Liu Bo38c227d2013-01-29 03:18:40 +00002495 return ret;
Josef Bacik50f13192013-07-22 12:50:37 -04002496 ret = 0;
Liu Bo38c227d2013-01-29 03:18:40 +00002497
2498 while (1) {
2499 cond_resched();
2500
2501 leaf = path->nodes[0];
2502 slot = path->slots[0];
2503
2504 if (slot >= btrfs_header_nritems(leaf)) {
2505 ret = btrfs_next_leaf(root, path);
2506 if (ret < 0) {
2507 goto out;
2508 } else if (ret > 0) {
2509 ret = 0;
2510 goto out;
2511 }
2512 continue;
2513 }
2514
2515 path->slots[0]++;
2516
2517 btrfs_item_key_to_cpu(leaf, &key, slot);
2518
2519 if (key.objectid > inum)
2520 goto out;
2521
2522 if (key.objectid < inum || key.type != BTRFS_EXTENT_DATA_KEY)
2523 continue;
2524
2525 extent = btrfs_item_ptr(leaf, slot,
2526 struct btrfs_file_extent_item);
2527
2528 if (btrfs_file_extent_disk_bytenr(leaf, extent) != old->bytenr)
2529 continue;
2530
Liu Boe68afa42013-07-01 22:13:26 +08002531 /*
2532 * 'offset' refers to the exact key.offset,
2533 * NOT the 'offset' field in btrfs_extent_data_ref, ie.
2534 * (key.offset - extent_offset).
2535 */
2536 if (key.offset != offset)
Liu Bo38c227d2013-01-29 03:18:40 +00002537 continue;
2538
Liu Boe68afa42013-07-01 22:13:26 +08002539 extent_offset = btrfs_file_extent_offset(leaf, extent);
Liu Bo38c227d2013-01-29 03:18:40 +00002540 num_bytes = btrfs_file_extent_num_bytes(leaf, extent);
Liu Boe68afa42013-07-01 22:13:26 +08002541
Liu Bo38c227d2013-01-29 03:18:40 +00002542 if (extent_offset >= old->extent_offset + old->offset +
2543 old->len || extent_offset + num_bytes <=
2544 old->extent_offset + old->offset)
2545 continue;
Liu Bo38c227d2013-01-29 03:18:40 +00002546 break;
2547 }
2548
2549 backref = kmalloc(sizeof(*backref), GFP_NOFS);
2550 if (!backref) {
2551 ret = -ENOENT;
2552 goto out;
2553 }
2554
2555 backref->root_id = root_id;
2556 backref->inum = inum;
Liu Boe68afa42013-07-01 22:13:26 +08002557 backref->file_pos = offset;
Liu Bo38c227d2013-01-29 03:18:40 +00002558 backref->num_bytes = num_bytes;
2559 backref->extent_offset = extent_offset;
2560 backref->generation = btrfs_file_extent_generation(leaf, extent);
2561 backref->old = old;
2562 backref_insert(&new->root, backref);
2563 old->count++;
2564out:
2565 btrfs_release_path(path);
2566 WARN_ON(ret);
2567 return ret;
2568}
2569
2570static noinline bool record_extent_backrefs(struct btrfs_path *path,
2571 struct new_sa_defrag_extent *new)
2572{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002573 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002574 struct old_sa_defrag_extent *old, *tmp;
2575 int ret;
2576
2577 new->path = path;
2578
2579 list_for_each_entry_safe(old, tmp, &new->head, list) {
Liu Boe68afa42013-07-01 22:13:26 +08002580 ret = iterate_inodes_from_logical(old->bytenr +
2581 old->extent_offset, fs_info,
Liu Bo38c227d2013-01-29 03:18:40 +00002582 path, record_one_backref,
Zygo Blaxellc995ab32017-09-22 13:58:45 -04002583 old, false);
Josef Bacik4724b102013-11-05 11:11:40 -05002584 if (ret < 0 && ret != -ENOENT)
2585 return false;
Liu Bo38c227d2013-01-29 03:18:40 +00002586
2587 /* no backref to be processed for this extent */
2588 if (!old->count) {
2589 list_del(&old->list);
2590 kfree(old);
2591 }
2592 }
2593
2594 if (list_empty(&new->head))
2595 return false;
2596
2597 return true;
2598}
2599
2600static int relink_is_mergable(struct extent_buffer *leaf,
2601 struct btrfs_file_extent_item *fi,
Liu Bo116e0022013-08-02 16:30:40 +08002602 struct new_sa_defrag_extent *new)
Liu Bo38c227d2013-01-29 03:18:40 +00002603{
Liu Bo116e0022013-08-02 16:30:40 +08002604 if (btrfs_file_extent_disk_bytenr(leaf, fi) != new->bytenr)
Liu Bo38c227d2013-01-29 03:18:40 +00002605 return 0;
2606
2607 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG)
2608 return 0;
2609
Liu Bo116e0022013-08-02 16:30:40 +08002610 if (btrfs_file_extent_compression(leaf, fi) != new->compress_type)
2611 return 0;
2612
2613 if (btrfs_file_extent_encryption(leaf, fi) ||
Liu Bo38c227d2013-01-29 03:18:40 +00002614 btrfs_file_extent_other_encoding(leaf, fi))
2615 return 0;
2616
2617 return 1;
2618}
2619
2620/*
2621 * Note the backref might has changed, and in this case we just return 0.
2622 */
2623static noinline int relink_extent_backref(struct btrfs_path *path,
2624 struct sa_defrag_extent_backref *prev,
2625 struct sa_defrag_extent_backref *backref)
2626{
2627 struct btrfs_file_extent_item *extent;
2628 struct btrfs_file_extent_item *item;
2629 struct btrfs_ordered_extent *ordered;
2630 struct btrfs_trans_handle *trans;
Qu Wenruo82fa1132019-04-04 14:45:35 +08002631 struct btrfs_ref ref = { 0 };
Liu Bo38c227d2013-01-29 03:18:40 +00002632 struct btrfs_root *root;
2633 struct btrfs_key key;
2634 struct extent_buffer *leaf;
2635 struct old_sa_defrag_extent *old = backref->old;
2636 struct new_sa_defrag_extent *new = old->new;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002637 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002638 struct inode *inode;
2639 struct extent_state *cached = NULL;
2640 int ret = 0;
2641 u64 start;
2642 u64 len;
2643 u64 lock_start;
2644 u64 lock_end;
2645 bool merge = false;
2646 int index;
2647
2648 if (prev && prev->root_id == backref->root_id &&
2649 prev->inum == backref->inum &&
2650 prev->file_pos + prev->num_bytes == backref->file_pos)
2651 merge = true;
2652
2653 /* step 1: get root */
2654 key.objectid = backref->root_id;
2655 key.type = BTRFS_ROOT_ITEM_KEY;
2656 key.offset = (u64)-1;
2657
Liu Bo38c227d2013-01-29 03:18:40 +00002658 index = srcu_read_lock(&fs_info->subvol_srcu);
2659
2660 root = btrfs_read_fs_root_no_name(fs_info, &key);
2661 if (IS_ERR(root)) {
2662 srcu_read_unlock(&fs_info->subvol_srcu, index);
2663 if (PTR_ERR(root) == -ENOENT)
2664 return 0;
2665 return PTR_ERR(root);
2666 }
Liu Bo38c227d2013-01-29 03:18:40 +00002667
Wang Shilongbcbba5e2014-02-08 23:46:35 +08002668 if (btrfs_root_readonly(root)) {
2669 srcu_read_unlock(&fs_info->subvol_srcu, index);
2670 return 0;
2671 }
2672
Liu Bo38c227d2013-01-29 03:18:40 +00002673 /* step 2: get inode */
2674 key.objectid = backref->inum;
2675 key.type = BTRFS_INODE_ITEM_KEY;
2676 key.offset = 0;
2677
2678 inode = btrfs_iget(fs_info->sb, &key, root, NULL);
2679 if (IS_ERR(inode)) {
2680 srcu_read_unlock(&fs_info->subvol_srcu, index);
2681 return 0;
2682 }
2683
2684 srcu_read_unlock(&fs_info->subvol_srcu, index);
2685
2686 /* step 3: relink backref */
2687 lock_start = backref->file_pos;
2688 lock_end = backref->file_pos + backref->num_bytes - 1;
2689 lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
David Sterbaff13db42015-12-03 14:30:40 +01002690 &cached);
Liu Bo38c227d2013-01-29 03:18:40 +00002691
2692 ordered = btrfs_lookup_first_ordered_extent(inode, lock_end);
2693 if (ordered) {
2694 btrfs_put_ordered_extent(ordered);
2695 goto out_unlock;
2696 }
2697
2698 trans = btrfs_join_transaction(root);
2699 if (IS_ERR(trans)) {
2700 ret = PTR_ERR(trans);
2701 goto out_unlock;
2702 }
2703
2704 key.objectid = backref->inum;
2705 key.type = BTRFS_EXTENT_DATA_KEY;
2706 key.offset = backref->file_pos;
2707
2708 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2709 if (ret < 0) {
2710 goto out_free_path;
2711 } else if (ret > 0) {
2712 ret = 0;
2713 goto out_free_path;
2714 }
2715
2716 extent = btrfs_item_ptr(path->nodes[0], path->slots[0],
2717 struct btrfs_file_extent_item);
2718
2719 if (btrfs_file_extent_generation(path->nodes[0], extent) !=
2720 backref->generation)
2721 goto out_free_path;
2722
2723 btrfs_release_path(path);
2724
2725 start = backref->file_pos;
2726 if (backref->extent_offset < old->extent_offset + old->offset)
2727 start += old->extent_offset + old->offset -
2728 backref->extent_offset;
2729
2730 len = min(backref->extent_offset + backref->num_bytes,
2731 old->extent_offset + old->offset + old->len);
2732 len -= max(backref->extent_offset, old->extent_offset + old->offset);
2733
2734 ret = btrfs_drop_extents(trans, root, inode, start,
2735 start + len, 1);
2736 if (ret)
2737 goto out_free_path;
2738again:
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002739 key.objectid = btrfs_ino(BTRFS_I(inode));
Liu Bo38c227d2013-01-29 03:18:40 +00002740 key.type = BTRFS_EXTENT_DATA_KEY;
2741 key.offset = start;
2742
Liu Boa09a0a72013-03-11 09:20:58 +00002743 path->leave_spinning = 1;
Liu Bo38c227d2013-01-29 03:18:40 +00002744 if (merge) {
2745 struct btrfs_file_extent_item *fi;
2746 u64 extent_len;
2747 struct btrfs_key found_key;
2748
Gui Hecheng3c9665d2014-01-23 13:41:09 +08002749 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
Liu Bo38c227d2013-01-29 03:18:40 +00002750 if (ret < 0)
2751 goto out_free_path;
2752
2753 path->slots[0]--;
2754 leaf = path->nodes[0];
2755 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2756
2757 fi = btrfs_item_ptr(leaf, path->slots[0],
2758 struct btrfs_file_extent_item);
2759 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
2760
Liu Bo116e0022013-08-02 16:30:40 +08002761 if (extent_len + found_key.offset == start &&
2762 relink_is_mergable(leaf, fi, new)) {
Liu Bo38c227d2013-01-29 03:18:40 +00002763 btrfs_set_file_extent_num_bytes(leaf, fi,
2764 extent_len + len);
2765 btrfs_mark_buffer_dirty(leaf);
2766 inode_add_bytes(inode, len);
2767
2768 ret = 1;
2769 goto out_free_path;
2770 } else {
2771 merge = false;
2772 btrfs_release_path(path);
2773 goto again;
2774 }
2775 }
2776
2777 ret = btrfs_insert_empty_item(trans, root, path, &key,
2778 sizeof(*extent));
2779 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002780 btrfs_abort_transaction(trans, ret);
Liu Bo38c227d2013-01-29 03:18:40 +00002781 goto out_free_path;
2782 }
2783
2784 leaf = path->nodes[0];
2785 item = btrfs_item_ptr(leaf, path->slots[0],
2786 struct btrfs_file_extent_item);
2787 btrfs_set_file_extent_disk_bytenr(leaf, item, new->bytenr);
2788 btrfs_set_file_extent_disk_num_bytes(leaf, item, new->disk_len);
2789 btrfs_set_file_extent_offset(leaf, item, start - new->file_pos);
2790 btrfs_set_file_extent_num_bytes(leaf, item, len);
2791 btrfs_set_file_extent_ram_bytes(leaf, item, new->len);
2792 btrfs_set_file_extent_generation(leaf, item, trans->transid);
2793 btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG);
2794 btrfs_set_file_extent_compression(leaf, item, new->compress_type);
2795 btrfs_set_file_extent_encryption(leaf, item, 0);
2796 btrfs_set_file_extent_other_encoding(leaf, item, 0);
2797
2798 btrfs_mark_buffer_dirty(leaf);
2799 inode_add_bytes(inode, len);
Liu Boa09a0a72013-03-11 09:20:58 +00002800 btrfs_release_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002801
Qu Wenruo82fa1132019-04-04 14:45:35 +08002802 btrfs_init_generic_ref(&ref, BTRFS_ADD_DELAYED_REF, new->bytenr,
2803 new->disk_len, 0);
2804 btrfs_init_data_ref(&ref, backref->root_id, backref->inum,
2805 new->file_pos); /* start - extent_offset */
2806 ret = btrfs_inc_extent_ref(trans, &ref);
Liu Bo38c227d2013-01-29 03:18:40 +00002807 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002808 btrfs_abort_transaction(trans, ret);
Liu Bo38c227d2013-01-29 03:18:40 +00002809 goto out_free_path;
2810 }
2811
2812 ret = 1;
2813out_free_path:
2814 btrfs_release_path(path);
Liu Boa09a0a72013-03-11 09:20:58 +00002815 path->leave_spinning = 0;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002816 btrfs_end_transaction(trans);
Liu Bo38c227d2013-01-29 03:18:40 +00002817out_unlock:
2818 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
David Sterbae43bbe52017-12-12 21:43:52 +01002819 &cached);
Liu Bo38c227d2013-01-29 03:18:40 +00002820 iput(inode);
2821 return ret;
2822}
2823
Liu Bo6f519562013-10-29 10:45:05 +08002824static void free_sa_defrag_extent(struct new_sa_defrag_extent *new)
2825{
2826 struct old_sa_defrag_extent *old, *tmp;
2827
2828 if (!new)
2829 return;
2830
2831 list_for_each_entry_safe(old, tmp, &new->head, list) {
Liu Bo6f519562013-10-29 10:45:05 +08002832 kfree(old);
2833 }
2834 kfree(new);
2835}
2836
Liu Bo38c227d2013-01-29 03:18:40 +00002837static void relink_file_extents(struct new_sa_defrag_extent *new)
2838{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002839 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002840 struct btrfs_path *path;
Liu Bo38c227d2013-01-29 03:18:40 +00002841 struct sa_defrag_extent_backref *backref;
2842 struct sa_defrag_extent_backref *prev = NULL;
Liu Bo38c227d2013-01-29 03:18:40 +00002843 struct rb_node *node;
2844 int ret;
2845
Liu Bo38c227d2013-01-29 03:18:40 +00002846 path = btrfs_alloc_path();
2847 if (!path)
2848 return;
2849
2850 if (!record_extent_backrefs(path, new)) {
2851 btrfs_free_path(path);
2852 goto out;
2853 }
2854 btrfs_release_path(path);
2855
2856 while (1) {
2857 node = rb_first(&new->root);
2858 if (!node)
2859 break;
2860 rb_erase(node, &new->root);
2861
2862 backref = rb_entry(node, struct sa_defrag_extent_backref, node);
2863
2864 ret = relink_extent_backref(path, prev, backref);
2865 WARN_ON(ret < 0);
2866
2867 kfree(prev);
2868
2869 if (ret == 1)
2870 prev = backref;
2871 else
2872 prev = NULL;
2873 cond_resched();
2874 }
2875 kfree(prev);
2876
2877 btrfs_free_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002878out:
Liu Bo6f519562013-10-29 10:45:05 +08002879 free_sa_defrag_extent(new);
2880
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002881 atomic_dec(&fs_info->defrag_running);
2882 wake_up(&fs_info->transaction_wait);
Liu Bo38c227d2013-01-29 03:18:40 +00002883}
2884
2885static struct new_sa_defrag_extent *
2886record_old_file_extents(struct inode *inode,
2887 struct btrfs_ordered_extent *ordered)
2888{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002889 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002890 struct btrfs_root *root = BTRFS_I(inode)->root;
2891 struct btrfs_path *path;
2892 struct btrfs_key key;
Liu Bo6f519562013-10-29 10:45:05 +08002893 struct old_sa_defrag_extent *old;
Liu Bo38c227d2013-01-29 03:18:40 +00002894 struct new_sa_defrag_extent *new;
2895 int ret;
2896
2897 new = kmalloc(sizeof(*new), GFP_NOFS);
2898 if (!new)
2899 return NULL;
2900
2901 new->inode = inode;
2902 new->file_pos = ordered->file_offset;
2903 new->len = ordered->len;
2904 new->bytenr = ordered->start;
2905 new->disk_len = ordered->disk_len;
2906 new->compress_type = ordered->compress_type;
2907 new->root = RB_ROOT;
2908 INIT_LIST_HEAD(&new->head);
2909
2910 path = btrfs_alloc_path();
2911 if (!path)
2912 goto out_kfree;
2913
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002914 key.objectid = btrfs_ino(BTRFS_I(inode));
Liu Bo38c227d2013-01-29 03:18:40 +00002915 key.type = BTRFS_EXTENT_DATA_KEY;
2916 key.offset = new->file_pos;
2917
2918 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2919 if (ret < 0)
2920 goto out_free_path;
2921 if (ret > 0 && path->slots[0] > 0)
2922 path->slots[0]--;
2923
2924 /* find out all the old extents for the file range */
2925 while (1) {
2926 struct btrfs_file_extent_item *extent;
2927 struct extent_buffer *l;
2928 int slot;
2929 u64 num_bytes;
2930 u64 offset;
2931 u64 end;
2932 u64 disk_bytenr;
2933 u64 extent_offset;
2934
2935 l = path->nodes[0];
2936 slot = path->slots[0];
2937
2938 if (slot >= btrfs_header_nritems(l)) {
2939 ret = btrfs_next_leaf(root, path);
2940 if (ret < 0)
Liu Bo6f519562013-10-29 10:45:05 +08002941 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002942 else if (ret > 0)
2943 break;
2944 continue;
2945 }
2946
2947 btrfs_item_key_to_cpu(l, &key, slot);
2948
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002949 if (key.objectid != btrfs_ino(BTRFS_I(inode)))
Liu Bo38c227d2013-01-29 03:18:40 +00002950 break;
2951 if (key.type != BTRFS_EXTENT_DATA_KEY)
2952 break;
2953 if (key.offset >= new->file_pos + new->len)
2954 break;
2955
2956 extent = btrfs_item_ptr(l, slot, struct btrfs_file_extent_item);
2957
2958 num_bytes = btrfs_file_extent_num_bytes(l, extent);
2959 if (key.offset + num_bytes < new->file_pos)
2960 goto next;
2961
2962 disk_bytenr = btrfs_file_extent_disk_bytenr(l, extent);
2963 if (!disk_bytenr)
2964 goto next;
2965
2966 extent_offset = btrfs_file_extent_offset(l, extent);
2967
2968 old = kmalloc(sizeof(*old), GFP_NOFS);
2969 if (!old)
Liu Bo6f519562013-10-29 10:45:05 +08002970 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002971
2972 offset = max(new->file_pos, key.offset);
2973 end = min(new->file_pos + new->len, key.offset + num_bytes);
2974
2975 old->bytenr = disk_bytenr;
2976 old->extent_offset = extent_offset;
2977 old->offset = offset - key.offset;
2978 old->len = end - offset;
2979 old->new = new;
2980 old->count = 0;
2981 list_add_tail(&old->list, &new->head);
2982next:
2983 path->slots[0]++;
2984 cond_resched();
2985 }
2986
2987 btrfs_free_path(path);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002988 atomic_inc(&fs_info->defrag_running);
Liu Bo38c227d2013-01-29 03:18:40 +00002989
2990 return new;
2991
Liu Bo38c227d2013-01-29 03:18:40 +00002992out_free_path:
2993 btrfs_free_path(path);
2994out_kfree:
Liu Bo6f519562013-10-29 10:45:05 +08002995 free_sa_defrag_extent(new);
Liu Bo38c227d2013-01-29 03:18:40 +00002996 return NULL;
2997}
2998
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002999static void btrfs_release_delalloc_bytes(struct btrfs_fs_info *fs_info,
Miao Xiee570fd22014-06-19 10:42:50 +08003000 u64 start, u64 len)
3001{
3002 struct btrfs_block_group_cache *cache;
3003
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003004 cache = btrfs_lookup_block_group(fs_info, start);
Miao Xiee570fd22014-06-19 10:42:50 +08003005 ASSERT(cache);
3006
3007 spin_lock(&cache->lock);
3008 cache->delalloc_bytes -= len;
3009 spin_unlock(&cache->lock);
3010
3011 btrfs_put_block_group(cache);
3012}
3013
Chris Masond352ac62008-09-29 15:18:18 -04003014/* as ordered data IO finishes, this gets called so we can finish
3015 * an ordered extent if the range of bytes in the file it covers are
3016 * fully written.
3017 */
Josef Bacik5fd02042012-05-02 14:00:54 -04003018static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
Chris Masone6dcd2d2008-07-17 12:53:50 -04003019{
Josef Bacik5fd02042012-05-02 14:00:54 -04003020 struct inode *inode = ordered_extent->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003021 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003022 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003023 struct btrfs_trans_handle *trans = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003024 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00003025 struct extent_state *cached_state = NULL;
Liu Bo38c227d2013-01-29 03:18:40 +00003026 struct new_sa_defrag_extent *new = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08003027 int compress_type = 0;
Josef Bacik77cef2e2013-08-29 13:57:21 -04003028 int ret = 0;
3029 u64 logical_len = ordered_extent->len;
Li Zefan82d59022011-04-20 10:33:24 +08003030 bool nolock;
Josef Bacik77cef2e2013-08-29 13:57:21 -04003031 bool truncated = false;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003032 bool range_locked = false;
3033 bool clear_new_delalloc_bytes = false;
Josef Bacik49940bd2018-10-11 15:54:21 -04003034 bool clear_reserved_extent = true;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003035
3036 if (!test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
3037 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags) &&
3038 !test_bit(BTRFS_ORDERED_DIRECT, &ordered_extent->flags))
3039 clear_new_delalloc_bytes = true;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003040
Nikolay Borisov70ddc552017-02-20 13:50:35 +02003041 nolock = btrfs_is_free_space_inode(BTRFS_I(inode));
Josef Bacik0cb59c92010-07-02 12:14:14 -04003042
Josef Bacik5fd02042012-05-02 14:00:54 -04003043 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
3044 ret = -EIO;
3045 goto out;
3046 }
3047
Nikolay Borisov7ab79562017-02-20 13:50:57 +02003048 btrfs_free_io_failure_record(BTRFS_I(inode),
3049 ordered_extent->file_offset,
3050 ordered_extent->file_offset +
3051 ordered_extent->len - 1);
Miao Xief6124962014-09-12 18:44:04 +08003052
Josef Bacik77cef2e2013-08-29 13:57:21 -04003053 if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) {
3054 truncated = true;
3055 logical_len = ordered_extent->truncated_len;
3056 /* Truncated the entire extent, don't bother adding */
3057 if (!logical_len)
3058 goto out;
3059 }
3060
Yan, Zhengc2167752009-11-12 09:34:21 +00003061 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003062 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
Qu Wenruo94ed9382015-09-08 17:25:56 +08003063
3064 /*
3065 * For mwrite(mmap + memset to write) case, we still reserve
3066 * space for NOCOW range.
3067 * As NOCOW won't cause a new delayed ref, just free the space
3068 */
Qu Wenruobc42bda2017-02-27 15:10:39 +08003069 btrfs_qgroup_free_data(inode, NULL, ordered_extent->file_offset,
Qu Wenruo94ed9382015-09-08 17:25:56 +08003070 ordered_extent->len);
Josef Bacik6c760c02012-11-09 10:53:21 -05003071 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
3072 if (nolock)
3073 trans = btrfs_join_transaction_nolock(root);
3074 else
3075 trans = btrfs_join_transaction(root);
3076 if (IS_ERR(trans)) {
3077 ret = PTR_ERR(trans);
3078 trans = NULL;
3079 goto out;
Yan, Zhengc2167752009-11-12 09:34:21 +00003080 }
Josef Bacik69fe2d72017-10-19 14:15:57 -04003081 trans->block_rsv = &BTRFS_I(inode)->block_rsv;
Josef Bacik6c760c02012-11-09 10:53:21 -05003082 ret = btrfs_update_inode_fallback(trans, root, inode);
3083 if (ret) /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04003084 btrfs_abort_transaction(trans, ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00003085 goto out;
3086 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04003087
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003088 range_locked = true;
Josef Bacik2ac55d42010-02-03 19:33:23 +00003089 lock_extent_bits(io_tree, ordered_extent->file_offset,
3090 ordered_extent->file_offset + ordered_extent->len - 1,
David Sterbaff13db42015-12-03 14:30:40 +01003091 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003092
Liu Bo38c227d2013-01-29 03:18:40 +00003093 ret = test_range_bit(io_tree, ordered_extent->file_offset,
3094 ordered_extent->file_offset + ordered_extent->len - 1,
Liu Bo452e62b2017-05-26 17:44:23 -06003095 EXTENT_DEFRAG, 0, cached_state);
Liu Bo38c227d2013-01-29 03:18:40 +00003096 if (ret) {
3097 u64 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
Josef Bacik8101c8d2014-01-29 16:05:30 -05003098 if (0 && last_snapshot >= BTRFS_I(inode)->generation)
Liu Bo38c227d2013-01-29 03:18:40 +00003099 /* the inode is shared */
3100 new = record_old_file_extents(inode, ordered_extent);
3101
3102 clear_extent_bit(io_tree, ordered_extent->file_offset,
3103 ordered_extent->file_offset + ordered_extent->len - 1,
David Sterbaae0f1622017-10-31 16:37:52 +01003104 EXTENT_DEFRAG, 0, 0, &cached_state);
Liu Bo38c227d2013-01-29 03:18:40 +00003105 }
3106
Josef Bacik0cb59c92010-07-02 12:14:14 -04003107 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003108 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04003109 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003110 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003111 if (IS_ERR(trans)) {
3112 ret = PTR_ERR(trans);
3113 trans = NULL;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003114 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003115 }
Chris Masona79b7d42014-05-22 16:18:52 -07003116
Josef Bacik69fe2d72017-10-19 14:15:57 -04003117 trans->block_rsv = &BTRFS_I(inode)->block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00003118
Chris Masonc8b97812008-10-29 14:49:59 -04003119 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08003120 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04003121 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08003122 BUG_ON(compress_type);
Justin Maggardb430b772017-10-30 15:29:10 -07003123 btrfs_qgroup_free_data(inode, NULL, ordered_extent->file_offset,
3124 ordered_extent->len);
Nikolay Borisov7a6d7062017-02-20 13:50:48 +02003125 ret = btrfs_mark_extent_written(trans, BTRFS_I(inode),
Yan Zhengd899e052008-10-30 14:25:28 -04003126 ordered_extent->file_offset,
3127 ordered_extent->file_offset +
Josef Bacik77cef2e2013-08-29 13:57:21 -04003128 logical_len);
Yan Zhengd899e052008-10-30 14:25:28 -04003129 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003130 BUG_ON(root == fs_info->tree_root);
Yan Zhengd899e052008-10-30 14:25:28 -04003131 ret = insert_reserved_file_extent(trans, inode,
3132 ordered_extent->file_offset,
3133 ordered_extent->start,
3134 ordered_extent->disk_len,
Josef Bacik77cef2e2013-08-29 13:57:21 -04003135 logical_len, logical_len,
Li Zefan261507a02010-12-17 14:21:50 +08003136 compress_type, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04003137 BTRFS_FILE_EXTENT_REG);
Josef Bacik49940bd2018-10-11 15:54:21 -04003138 if (!ret) {
3139 clear_reserved_extent = false;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003140 btrfs_release_delalloc_bytes(fs_info,
Miao Xiee570fd22014-06-19 10:42:50 +08003141 ordered_extent->start,
3142 ordered_extent->disk_len);
Josef Bacik49940bd2018-10-11 15:54:21 -04003143 }
Yan Zhengd899e052008-10-30 14:25:28 -04003144 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04003145 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
3146 ordered_extent->file_offset, ordered_extent->len,
3147 trans->transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003148 if (ret < 0) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003149 btrfs_abort_transaction(trans, ret);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003150 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003151 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00003152
Nikolay Borisovac01f262018-01-08 10:59:43 +02003153 ret = add_pending_csums(trans, inode, &ordered_extent->list);
3154 if (ret) {
3155 btrfs_abort_transaction(trans, ret);
3156 goto out;
3157 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04003158
Josef Bacik6c760c02012-11-09 10:53:21 -05003159 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
3160 ret = btrfs_update_inode_fallback(trans, root, inode);
3161 if (ret) { /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04003162 btrfs_abort_transaction(trans, ret);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003163 goto out;
Josef Bacik1ef30be2011-04-05 19:25:36 -04003164 }
3165 ret = 0;
Yan, Zhengc2167752009-11-12 09:34:21 +00003166out:
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003167 if (range_locked || clear_new_delalloc_bytes) {
3168 unsigned int clear_bits = 0;
3169
3170 if (range_locked)
3171 clear_bits |= EXTENT_LOCKED;
3172 if (clear_new_delalloc_bytes)
3173 clear_bits |= EXTENT_DELALLOC_NEW;
3174 clear_extent_bit(&BTRFS_I(inode)->io_tree,
3175 ordered_extent->file_offset,
3176 ordered_extent->file_offset +
3177 ordered_extent->len - 1,
3178 clear_bits,
3179 (clear_bits & EXTENT_LOCKED) ? 1 : 0,
David Sterbaae0f1622017-10-31 16:37:52 +01003180 0, &cached_state);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003181 }
3182
Miao Xiea698d0752012-09-20 01:51:59 -06003183 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003184 btrfs_end_transaction(trans);
Josef Bacik0cb59c92010-07-02 12:14:14 -04003185
Josef Bacik77cef2e2013-08-29 13:57:21 -04003186 if (ret || truncated) {
3187 u64 start, end;
3188
3189 if (truncated)
3190 start = ordered_extent->file_offset + logical_len;
3191 else
3192 start = ordered_extent->file_offset;
3193 end = ordered_extent->file_offset + ordered_extent->len - 1;
David Sterbaf08dc362017-10-31 17:02:39 +01003194 clear_extent_uptodate(io_tree, start, end, NULL);
Josef Bacik77cef2e2013-08-29 13:57:21 -04003195
3196 /* Drop the cache for the part of the extent we didn't write. */
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02003197 btrfs_drop_extent_cache(BTRFS_I(inode), start, end, 0);
Josef Bacik5fd02042012-05-02 14:00:54 -04003198
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003199 /*
3200 * If the ordered extent had an IOERR or something else went
3201 * wrong we need to return the space for this ordered extent
Josef Bacik77cef2e2013-08-29 13:57:21 -04003202 * back to the allocator. We only free the extent in the
3203 * truncated case if we didn't write out the extent at all.
Josef Bacik49940bd2018-10-11 15:54:21 -04003204 *
3205 * If we made it past insert_reserved_file_extent before we
3206 * errored out then we don't need to do this as the accounting
3207 * has already been done.
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003208 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04003209 if ((ret || !logical_len) &&
Josef Bacik49940bd2018-10-11 15:54:21 -04003210 clear_reserved_extent &&
Josef Bacik77cef2e2013-08-29 13:57:21 -04003211 !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003212 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003213 btrfs_free_reserved_extent(fs_info,
3214 ordered_extent->start,
Miao Xiee570fd22014-06-19 10:42:50 +08003215 ordered_extent->disk_len, 1);
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003216 }
3217
3218
Josef Bacik5fd02042012-05-02 14:00:54 -04003219 /*
Liu Bo8bad3c02012-06-18 12:14:23 +08003220 * This needs to be done to make sure anybody waiting knows we are done
3221 * updating everything for this ordered extent.
Josef Bacik5fd02042012-05-02 14:00:54 -04003222 */
3223 btrfs_remove_ordered_extent(inode, ordered_extent);
3224
Liu Bo38c227d2013-01-29 03:18:40 +00003225 /* for snapshot-aware defrag */
Liu Bo6f519562013-10-29 10:45:05 +08003226 if (new) {
3227 if (ret) {
3228 free_sa_defrag_extent(new);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003229 atomic_dec(&fs_info->defrag_running);
Liu Bo6f519562013-10-29 10:45:05 +08003230 } else {
3231 relink_file_extents(new);
3232 }
3233 }
Liu Bo38c227d2013-01-29 03:18:40 +00003234
Chris Masone6dcd2d2008-07-17 12:53:50 -04003235 /* once for us */
3236 btrfs_put_ordered_extent(ordered_extent);
3237 /* once for the tree */
3238 btrfs_put_ordered_extent(ordered_extent);
3239
Josef Bacik5fd02042012-05-02 14:00:54 -04003240 return ret;
3241}
3242
3243static void finish_ordered_fn(struct btrfs_work *work)
3244{
3245 struct btrfs_ordered_extent *ordered_extent;
3246 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
3247 btrfs_finish_ordered_io(ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003248}
3249
Nikolay Borisovc6297322018-11-08 10:18:08 +02003250void btrfs_writepage_endio_finish_ordered(struct page *page, u64 start,
3251 u64 end, int uptodate)
Chris Mason211f90e2008-07-18 11:56:15 -04003252{
Josef Bacik5fd02042012-05-02 14:00:54 -04003253 struct inode *inode = page->mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003254 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik5fd02042012-05-02 14:00:54 -04003255 struct btrfs_ordered_extent *ordered_extent = NULL;
Liu Bo9e0af232014-08-15 23:36:53 +08003256 struct btrfs_workqueue *wq;
3257 btrfs_work_func_t func;
Josef Bacik5fd02042012-05-02 14:00:54 -04003258
liubo1abe9b82011-03-24 11:18:59 +00003259 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
3260
Chris Mason8b62b722009-09-02 16:53:46 -04003261 ClearPagePrivate2(page);
Josef Bacik5fd02042012-05-02 14:00:54 -04003262 if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
3263 end - start + 1, uptodate))
David Sterbac3988d62017-02-17 15:18:32 +01003264 return;
Josef Bacik5fd02042012-05-02 14:00:54 -04003265
Nikolay Borisov70ddc552017-02-20 13:50:35 +02003266 if (btrfs_is_free_space_inode(BTRFS_I(inode))) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003267 wq = fs_info->endio_freespace_worker;
Liu Bo9e0af232014-08-15 23:36:53 +08003268 func = btrfs_freespace_write_helper;
3269 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003270 wq = fs_info->endio_write_workers;
Liu Bo9e0af232014-08-15 23:36:53 +08003271 func = btrfs_endio_write_helper;
3272 }
Josef Bacik5fd02042012-05-02 14:00:54 -04003273
Liu Bo9e0af232014-08-15 23:36:53 +08003274 btrfs_init_work(&ordered_extent->work, func, finish_ordered_fn, NULL,
3275 NULL);
3276 btrfs_queue_work(wq, &ordered_extent->work);
Chris Mason211f90e2008-07-18 11:56:15 -04003277}
3278
Miao Xiedc380ae2014-09-12 18:43:55 +08003279static int __readpage_endio_check(struct inode *inode,
3280 struct btrfs_io_bio *io_bio,
3281 int icsum, struct page *page,
3282 int pgoff, u64 start, size_t len)
3283{
Johannes Thumshirnd5178572019-06-03 16:58:57 +02003284 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
3285 SHASH_DESC_ON_STACK(shash, fs_info->csum_shash);
Miao Xiedc380ae2014-09-12 18:43:55 +08003286 char *kaddr;
Johannes Thumshirnd5178572019-06-03 16:58:57 +02003287 u16 csum_size = btrfs_super_csum_size(fs_info->super_copy);
3288 u8 *csum_expected;
3289 u8 csum[BTRFS_CSUM_SIZE];
Miao Xiedc380ae2014-09-12 18:43:55 +08003290
Johannes Thumshirnd5178572019-06-03 16:58:57 +02003291 csum_expected = ((u8 *)io_bio->csum) + icsum * csum_size;
Miao Xiedc380ae2014-09-12 18:43:55 +08003292
3293 kaddr = kmap_atomic(page);
Johannes Thumshirnd5178572019-06-03 16:58:57 +02003294 shash->tfm = fs_info->csum_shash;
3295
3296 crypto_shash_init(shash);
3297 crypto_shash_update(shash, kaddr + pgoff, len);
3298 crypto_shash_final(shash, csum);
3299
3300 if (memcmp(csum, csum_expected, csum_size))
Miao Xiedc380ae2014-09-12 18:43:55 +08003301 goto zeroit;
3302
3303 kunmap_atomic(kaddr);
3304 return 0;
3305zeroit:
Johannes Thumshirnea41d6b2019-06-03 16:58:58 +02003306 btrfs_print_data_csum_error(BTRFS_I(inode), start, csum, csum_expected,
3307 io_bio->mirror_num);
Miao Xiedc380ae2014-09-12 18:43:55 +08003308 memset(kaddr + pgoff, 1, len);
3309 flush_dcache_page(page);
3310 kunmap_atomic(kaddr);
Miao Xiedc380ae2014-09-12 18:43:55 +08003311 return -EIO;
3312}
3313
Chris Masond352ac62008-09-29 15:18:18 -04003314/*
Chris Masond352ac62008-09-29 15:18:18 -04003315 * when reads are done, we need to check csums to verify the data is correct
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02003316 * if there's a match, we allow the bio to finish. If not, the code in
3317 * extent_io.c will try to find good copies for us.
Chris Masond352ac62008-09-29 15:18:18 -04003318 */
Miao Xiefacc8a222013-07-25 19:22:34 +08003319static int btrfs_readpage_end_io_hook(struct btrfs_io_bio *io_bio,
3320 u64 phy_offset, struct page *page,
3321 u64 start, u64 end, int mirror)
Chris Mason07157aa2007-08-30 08:50:51 -04003322{
Miao Xie4eee4fa2012-12-21 09:17:45 +00003323 size_t offset = start - page_offset(page);
Chris Mason07157aa2007-08-30 08:50:51 -04003324 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05003325 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masonff79f812007-10-15 16:22:25 -04003326 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -05003327
Chris Masond20f7042008-12-08 16:58:54 -05003328 if (PageChecked(page)) {
3329 ClearPageChecked(page);
Miao Xiedc380ae2014-09-12 18:43:55 +08003330 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003331 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003332
3333 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Miao Xiedc380ae2014-09-12 18:43:55 +08003334 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003335
Yan Zheng17d217f2008-12-12 10:03:38 -05003336 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04003337 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
David Sterba91166212016-04-26 23:54:39 +02003338 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM);
Yan Zheng17d217f2008-12-12 10:03:38 -05003339 return 0;
3340 }
3341
Miao Xiefacc8a222013-07-25 19:22:34 +08003342 phy_offset >>= inode->i_sb->s_blocksize_bits;
Miao Xiedc380ae2014-09-12 18:43:55 +08003343 return __readpage_endio_check(inode, io_bio, phy_offset, page, offset,
3344 start, (size_t)(end - start + 1));
Chris Mason07157aa2007-08-30 08:50:51 -04003345}
Chris Masonb888db22007-08-27 16:49:44 -04003346
Nikolay Borisovc1c3fac2018-01-16 09:31:58 +02003347/*
3348 * btrfs_add_delayed_iput - perform a delayed iput on @inode
3349 *
3350 * @inode: The inode we want to perform iput on
3351 *
3352 * This function uses the generic vfs_inode::i_count to track whether we should
3353 * just decrement it (in case it's > 1) or if this is the last iput then link
3354 * the inode to the delayed iput machinery. Delayed iputs are processed at
3355 * transaction commit time/superblock commit/cleaner kthread.
3356 */
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003357void btrfs_add_delayed_iput(struct inode *inode)
3358{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003359 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
David Sterba8089fe62015-11-19 14:15:51 +01003360 struct btrfs_inode *binode = BTRFS_I(inode);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003361
3362 if (atomic_add_unless(&inode->i_count, -1, 1))
3363 return;
3364
Josef Bacik034f7842018-12-03 11:06:52 -05003365 atomic_inc(&fs_info->nr_delayed_iputs);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003366 spin_lock(&fs_info->delayed_iput_lock);
Nikolay Borisovc1c3fac2018-01-16 09:31:58 +02003367 ASSERT(list_empty(&binode->delayed_iput));
3368 list_add_tail(&binode->delayed_iput, &fs_info->delayed_iputs);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003369 spin_unlock(&fs_info->delayed_iput_lock);
Josef Bacikfd340d02019-01-11 10:21:02 -05003370 if (!test_bit(BTRFS_FS_CLEANER_RUNNING, &fs_info->flags))
3371 wake_up_process(fs_info->cleaner_kthread);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003372}
3373
Josef Bacik63611e72019-06-18 10:59:18 -04003374static void run_delayed_iput_locked(struct btrfs_fs_info *fs_info,
3375 struct btrfs_inode *inode)
3376{
3377 list_del_init(&inode->delayed_iput);
3378 spin_unlock(&fs_info->delayed_iput_lock);
3379 iput(&inode->vfs_inode);
3380 if (atomic_dec_and_test(&fs_info->nr_delayed_iputs))
3381 wake_up(&fs_info->delayed_iputs_wait);
3382 spin_lock(&fs_info->delayed_iput_lock);
3383}
3384
3385static void btrfs_run_delayed_iput(struct btrfs_fs_info *fs_info,
3386 struct btrfs_inode *inode)
3387{
3388 if (!list_empty(&inode->delayed_iput)) {
3389 spin_lock(&fs_info->delayed_iput_lock);
3390 if (!list_empty(&inode->delayed_iput))
3391 run_delayed_iput_locked(fs_info, inode);
3392 spin_unlock(&fs_info->delayed_iput_lock);
3393 }
3394}
3395
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003396void btrfs_run_delayed_iputs(struct btrfs_fs_info *fs_info)
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003397{
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003398
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003399 spin_lock(&fs_info->delayed_iput_lock);
David Sterba8089fe62015-11-19 14:15:51 +01003400 while (!list_empty(&fs_info->delayed_iputs)) {
3401 struct btrfs_inode *inode;
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003402
David Sterba8089fe62015-11-19 14:15:51 +01003403 inode = list_first_entry(&fs_info->delayed_iputs,
3404 struct btrfs_inode, delayed_iput);
Josef Bacik63611e72019-06-18 10:59:18 -04003405 run_delayed_iput_locked(fs_info, inode);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003406 }
David Sterba8089fe62015-11-19 14:15:51 +01003407 spin_unlock(&fs_info->delayed_iput_lock);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003408}
3409
Josef Bacik034f7842018-12-03 11:06:52 -05003410/**
3411 * btrfs_wait_on_delayed_iputs - wait on the delayed iputs to be done running
3412 * @fs_info - the fs_info for this fs
3413 * @return - EINTR if we were killed, 0 if nothing's pending
3414 *
3415 * This will wait on any delayed iputs that are currently running with KILLABLE
3416 * set. Once they are all done running we will return, unless we are killed in
3417 * which case we return EINTR. This helps in user operations like fallocate etc
3418 * that might get blocked on the iputs.
3419 */
3420int btrfs_wait_on_delayed_iputs(struct btrfs_fs_info *fs_info)
3421{
3422 int ret = wait_event_killable(fs_info->delayed_iputs_wait,
3423 atomic_read(&fs_info->nr_delayed_iputs) == 0);
3424 if (ret)
3425 return -EINTR;
3426 return 0;
3427}
3428
Yan, Zhengd68fc572010-05-16 10:49:58 -04003429/*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003430 * This creates an orphan entry for the given inode in case something goes wrong
3431 * in the middle of an unlink.
Josef Bacik7b128762008-07-24 12:17:14 -04003432 */
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003433int btrfs_orphan_add(struct btrfs_trans_handle *trans,
Omar Sandoval27919062018-05-11 13:13:37 -07003434 struct btrfs_inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003435{
Yan, Zhengd68fc572010-05-16 10:49:58 -04003436 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003437
Omar Sandoval27919062018-05-11 13:13:37 -07003438 ret = btrfs_insert_orphan_item(trans, inode->root, btrfs_ino(inode));
3439 if (ret && ret != -EEXIST) {
3440 btrfs_abort_transaction(trans, ret);
3441 return ret;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003442 }
3443
Yan, Zhengd68fc572010-05-16 10:49:58 -04003444 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003445}
3446
3447/*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003448 * We have done the delete so we can go ahead and remove the orphan item for
3449 * this particular inode.
Josef Bacik7b128762008-07-24 12:17:14 -04003450 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00003451static int btrfs_orphan_del(struct btrfs_trans_handle *trans,
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003452 struct btrfs_inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003453{
Omar Sandoval27919062018-05-11 13:13:37 -07003454 return btrfs_del_orphan_item(trans, inode->root, btrfs_ino(inode));
Josef Bacik7b128762008-07-24 12:17:14 -04003455}
3456
3457/*
3458 * this cleans up any orphans that may be left on the list from the last use
3459 * of this root.
3460 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003461int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04003462{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003463 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik7b128762008-07-24 12:17:14 -04003464 struct btrfs_path *path;
3465 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04003466 struct btrfs_key key, found_key;
3467 struct btrfs_trans_handle *trans;
3468 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003469 u64 last_objectid = 0;
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003470 int ret = 0, nr_unlink = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003471
Yan, Zhengd68fc572010-05-16 10:49:58 -04003472 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003473 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00003474
3475 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003476 if (!path) {
3477 ret = -ENOMEM;
3478 goto out;
3479 }
David Sterbae4058b52015-11-27 16:31:35 +01003480 path->reada = READA_BACK;
Josef Bacik7b128762008-07-24 12:17:14 -04003481
3482 key.objectid = BTRFS_ORPHAN_OBJECTID;
David Sterba962a2982014-06-04 18:41:45 +02003483 key.type = BTRFS_ORPHAN_ITEM_KEY;
Josef Bacik7b128762008-07-24 12:17:14 -04003484 key.offset = (u64)-1;
3485
Josef Bacik7b128762008-07-24 12:17:14 -04003486 while (1) {
3487 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003488 if (ret < 0)
3489 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003490
3491 /*
3492 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003493 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04003494 * find the key and see if we have stuff that matches
3495 */
3496 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003497 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003498 if (path->slots[0] == 0)
3499 break;
3500 path->slots[0]--;
3501 }
3502
3503 /* pull out the item */
3504 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04003505 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3506
3507 /* make sure the item matches what we want */
3508 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
3509 break;
David Sterba962a2982014-06-04 18:41:45 +02003510 if (found_key.type != BTRFS_ORPHAN_ITEM_KEY)
Josef Bacik7b128762008-07-24 12:17:14 -04003511 break;
3512
3513 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02003514 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04003515
3516 /*
3517 * this is where we are basically btrfs_lookup, without the
3518 * crossing root thing. we store the inode number in the
3519 * offset of the orphan item.
3520 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003521
3522 if (found_key.offset == last_objectid) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003523 btrfs_err(fs_info,
3524 "Error removing orphan entry, stopping orphan cleanup");
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003525 ret = -EINVAL;
3526 goto out;
3527 }
3528
3529 last_objectid = found_key.offset;
3530
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003531 found_key.objectid = found_key.offset;
3532 found_key.type = BTRFS_INODE_ITEM_KEY;
3533 found_key.offset = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003534 inode = btrfs_iget(fs_info->sb, &found_key, root, NULL);
Rusty Russell8c6ffba2013-07-15 11:20:32 +09303535 ret = PTR_ERR_OR_ZERO(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003536 if (ret && ret != -ENOENT)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003537 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003538
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003539 if (ret == -ENOENT && root == fs_info->tree_root) {
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003540 struct btrfs_root *dead_root;
3541 struct btrfs_fs_info *fs_info = root->fs_info;
3542 int is_dead_root = 0;
3543
3544 /*
3545 * this is an orphan in the tree root. Currently these
3546 * could come from 2 sources:
3547 * a) a snapshot deletion in progress
3548 * b) a free space cache inode
3549 * We need to distinguish those two, as the snapshot
3550 * orphan must not get deleted.
3551 * find_dead_roots already ran before us, so if this
3552 * is a snapshot deletion, we should find the root
3553 * in the dead_roots list
3554 */
3555 spin_lock(&fs_info->trans_lock);
3556 list_for_each_entry(dead_root, &fs_info->dead_roots,
3557 root_list) {
3558 if (dead_root->root_key.objectid ==
3559 found_key.objectid) {
3560 is_dead_root = 1;
3561 break;
3562 }
3563 }
3564 spin_unlock(&fs_info->trans_lock);
3565 if (is_dead_root) {
3566 /* prevent this orphan from being found again */
3567 key.offset = found_key.objectid - 1;
3568 continue;
3569 }
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003570
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003571 }
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003572
Josef Bacika8c9e572011-09-21 16:55:59 -04003573 /*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003574 * If we have an inode with links, there are a couple of
3575 * possibilities. Old kernels (before v3.12) used to create an
3576 * orphan item for truncate indicating that there were possibly
3577 * extent items past i_size that needed to be deleted. In v3.12,
3578 * truncate was changed to update i_size in sync with the extent
3579 * items, but the (useless) orphan item was still created. Since
3580 * v4.18, we don't create the orphan item for truncate at all.
3581 *
3582 * So, this item could mean that we need to do a truncate, but
3583 * only if this filesystem was last used on a pre-v3.12 kernel
3584 * and was not cleanly unmounted. The odds of that are quite
3585 * slim, and it's a pain to do the truncate now, so just delete
3586 * the orphan item.
3587 *
3588 * It's also possible that this orphan item was supposed to be
3589 * deleted but wasn't. The inode number may have been reused,
3590 * but either way, we can delete the orphan item.
Josef Bacika8c9e572011-09-21 16:55:59 -04003591 */
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003592 if (ret == -ENOENT || inode->i_nlink) {
3593 if (!ret)
3594 iput(inode);
Josef Bacika8c9e572011-09-21 16:55:59 -04003595 trans = btrfs_start_transaction(root, 1);
3596 if (IS_ERR(trans)) {
3597 ret = PTR_ERR(trans);
3598 goto out;
3599 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003600 btrfs_debug(fs_info, "auto deleting %Lu",
3601 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003602 ret = btrfs_del_orphan_item(trans, root,
3603 found_key.objectid);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003604 btrfs_end_transaction(trans);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003605 if (ret)
3606 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003607 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003608 }
Josef Bacik7b128762008-07-24 12:17:14 -04003609
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003610 nr_unlink++;
Josef Bacik7b128762008-07-24 12:17:14 -04003611
3612 /* this will do delete_inode and everything for us */
3613 iput(inode);
3614 }
Miao Xie3254c872011-11-10 20:45:05 -05003615 /* release the path since we're done with it */
3616 btrfs_release_path(path);
3617
Yan, Zhengd68fc572010-05-16 10:49:58 -04003618 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
3619
Omar Sandovala575cee2018-05-11 13:13:38 -07003620 if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003621 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003622 if (!IS_ERR(trans))
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003623 btrfs_end_transaction(trans);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003624 }
Josef Bacik7b128762008-07-24 12:17:14 -04003625
3626 if (nr_unlink)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003627 btrfs_debug(fs_info, "unlinked %d orphans", nr_unlink);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003628
3629out:
3630 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003631 btrfs_err(fs_info, "could not do orphan cleanup %d", ret);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003632 btrfs_free_path(path);
3633 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003634}
3635
Chris Masond352ac62008-09-29 15:18:18 -04003636/*
Chris Mason46a53cc2009-04-27 11:47:50 -04003637 * very simple check to peek ahead in the leaf looking for xattrs. If we
3638 * don't find any xattrs, we know there can't be any acls.
3639 *
3640 * slot is the slot the inode is in, objectid is the objectid of the inode
3641 */
3642static noinline int acls_after_inode_item(struct extent_buffer *leaf,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003643 int slot, u64 objectid,
3644 int *first_xattr_slot)
Chris Mason46a53cc2009-04-27 11:47:50 -04003645{
3646 u32 nritems = btrfs_header_nritems(leaf);
3647 struct btrfs_key found_key;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003648 static u64 xattr_access = 0;
3649 static u64 xattr_default = 0;
Chris Mason46a53cc2009-04-27 11:47:50 -04003650 int scanned = 0;
3651
Josef Bacikf23b5a52013-06-19 10:16:26 -04003652 if (!xattr_access) {
Andreas Gruenbacher97d79292015-12-02 14:44:35 +01003653 xattr_access = btrfs_name_hash(XATTR_NAME_POSIX_ACL_ACCESS,
3654 strlen(XATTR_NAME_POSIX_ACL_ACCESS));
3655 xattr_default = btrfs_name_hash(XATTR_NAME_POSIX_ACL_DEFAULT,
3656 strlen(XATTR_NAME_POSIX_ACL_DEFAULT));
Josef Bacikf23b5a52013-06-19 10:16:26 -04003657 }
3658
Chris Mason46a53cc2009-04-27 11:47:50 -04003659 slot++;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003660 *first_xattr_slot = -1;
Chris Mason46a53cc2009-04-27 11:47:50 -04003661 while (slot < nritems) {
3662 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3663
3664 /* we found a different objectid, there must not be acls */
3665 if (found_key.objectid != objectid)
3666 return 0;
3667
3668 /* we found an xattr, assume we've got an acl */
Josef Bacikf23b5a52013-06-19 10:16:26 -04003669 if (found_key.type == BTRFS_XATTR_ITEM_KEY) {
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003670 if (*first_xattr_slot == -1)
3671 *first_xattr_slot = slot;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003672 if (found_key.offset == xattr_access ||
3673 found_key.offset == xattr_default)
3674 return 1;
3675 }
Chris Mason46a53cc2009-04-27 11:47:50 -04003676
3677 /*
3678 * we found a key greater than an xattr key, there can't
3679 * be any acls later on
3680 */
3681 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
3682 return 0;
3683
3684 slot++;
3685 scanned++;
3686
3687 /*
3688 * it goes inode, inode backrefs, xattrs, extents,
3689 * so if there are a ton of hard links to an inode there can
3690 * be a lot of backrefs. Don't waste time searching too hard,
3691 * this is just an optimization
3692 */
3693 if (scanned >= 8)
3694 break;
3695 }
3696 /* we hit the end of the leaf before we found an xattr or
3697 * something larger than an xattr. We have to assume the inode
3698 * has acls
3699 */
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003700 if (*first_xattr_slot == -1)
3701 *first_xattr_slot = slot;
Chris Mason46a53cc2009-04-27 11:47:50 -04003702 return 1;
3703}
3704
3705/*
Chris Masond352ac62008-09-29 15:18:18 -04003706 * read an inode from the btree into the in-memory inode
3707 */
Filipe Manana4222ea72018-10-24 10:13:03 +01003708static int btrfs_read_locked_inode(struct inode *inode,
3709 struct btrfs_path *in_path)
Chris Mason39279cc2007-06-12 06:35:45 -04003710{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003711 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Filipe Manana4222ea72018-10-24 10:13:03 +01003712 struct btrfs_path *path = in_path;
Chris Mason5f39d392007-10-15 16:14:19 -04003713 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003714 struct btrfs_inode_item *inode_item;
3715 struct btrfs_root *root = BTRFS_I(inode)->root;
3716 struct btrfs_key location;
Miao Xie67de1172013-12-26 13:07:06 +08003717 unsigned long ptr;
Chris Mason46a53cc2009-04-27 11:47:50 -04003718 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04003719 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04003720 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003721 bool filled = false;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003722 int first_xattr_slot;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003723
3724 ret = btrfs_fill_inode(inode, &rdev);
3725 if (!ret)
3726 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04003727
Filipe Manana4222ea72018-10-24 10:13:03 +01003728 if (!path) {
3729 path = btrfs_alloc_path();
3730 if (!path)
3731 return -ENOMEM;
3732 }
Mark Fasheh1748f842011-07-12 11:25:31 -07003733
Chris Mason39279cc2007-06-12 06:35:45 -04003734 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05003735
Chris Mason39279cc2007-06-12 06:35:45 -04003736 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Filipe Manana67710892016-06-06 11:51:25 +01003737 if (ret) {
Filipe Manana4222ea72018-10-24 10:13:03 +01003738 if (path != in_path)
3739 btrfs_free_path(path);
Al Virof5b3a412018-07-29 23:04:51 +01003740 return ret;
Filipe Manana67710892016-06-06 11:51:25 +01003741 }
Chris Mason39279cc2007-06-12 06:35:45 -04003742
Chris Mason5f39d392007-10-15 16:14:19 -04003743 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00003744
3745 if (filled)
Miao Xie67de1172013-12-26 13:07:06 +08003746 goto cache_index;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003747
Chris Mason5f39d392007-10-15 16:14:19 -04003748 inode_item = btrfs_item_ptr(leaf, path->slots[0],
3749 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003750 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02003751 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08003752 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
3753 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02003754 btrfs_i_size_write(BTRFS_I(inode), btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04003755
David Sterbaa937b972014-12-12 17:39:12 +01003756 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->atime);
3757 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->atime);
Chris Mason5f39d392007-10-15 16:14:19 -04003758
David Sterbaa937b972014-12-12 17:39:12 +01003759 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->mtime);
3760 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->mtime);
Chris Mason5f39d392007-10-15 16:14:19 -04003761
David Sterbaa937b972014-12-12 17:39:12 +01003762 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->ctime);
3763 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->ctime);
Chris Mason5f39d392007-10-15 16:14:19 -04003764
chandan r9cc97d62012-07-04 12:48:07 +05303765 BTRFS_I(inode)->i_otime.tv_sec =
3766 btrfs_timespec_sec(leaf, &inode_item->otime);
3767 BTRFS_I(inode)->i_otime.tv_nsec =
3768 btrfs_timespec_nsec(leaf, &inode_item->otime);
Chris Mason5f39d392007-10-15 16:14:19 -04003769
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003770 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003771 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003772 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
3773
Jeff Laytonc7f88c42017-12-11 06:35:12 -05003774 inode_set_iversion_queried(inode,
3775 btrfs_inode_sequence(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003776 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04003777 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003778 rdev = btrfs_inode_rdev(leaf, inode_item);
3779
Josef Bacikaec74772008-07-24 12:12:38 -04003780 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05003781 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Miao Xie67de1172013-12-26 13:07:06 +08003782
3783cache_index:
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003784 /*
3785 * If we were modified in the current generation and evicted from memory
3786 * and then re-read we need to do a full sync since we don't have any
3787 * idea about which extents were modified before we were evicted from
3788 * cache.
3789 *
3790 * This is required for both inode re-read from disk and delayed inode
3791 * in delayed_nodes_tree.
3792 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003793 if (BTRFS_I(inode)->last_trans == fs_info->generation)
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003794 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3795 &BTRFS_I(inode)->runtime_flags);
3796
Filipe Mananabde6c242015-07-24 00:00:19 +01003797 /*
3798 * We don't persist the id of the transaction where an unlink operation
3799 * against the inode was last made. So here we assume the inode might
3800 * have been evicted, and therefore the exact value of last_unlink_trans
3801 * lost, and set it to last_trans to avoid metadata inconsistencies
3802 * between the inode and its parent if the inode is fsync'ed and the log
3803 * replayed. For example, in the scenario:
3804 *
3805 * touch mydir/foo
3806 * ln mydir/foo mydir/bar
3807 * sync
3808 * unlink mydir/bar
3809 * echo 2 > /proc/sys/vm/drop_caches # evicts inode
3810 * xfs_io -c fsync mydir/foo
3811 * <power failure>
3812 * mount fs, triggers fsync log replay
3813 *
3814 * We must make sure that when we fsync our inode foo we also log its
3815 * parent inode, otherwise after log replay the parent still has the
3816 * dentry with the "bar" name but our inode foo has a link count of 1
3817 * and doesn't have an inode ref with the name "bar" anymore.
3818 *
3819 * Setting last_unlink_trans to last_trans is a pessimistic approach,
Nicholas D Steeves01327612016-05-19 21:18:45 -04003820 * but it guarantees correctness at the expense of occasional full
Filipe Mananabde6c242015-07-24 00:00:19 +01003821 * transaction commits on fsync if our inode is a directory, or if our
3822 * inode is not a directory, logging its parent unnecessarily.
3823 */
3824 BTRFS_I(inode)->last_unlink_trans = BTRFS_I(inode)->last_trans;
3825
Miao Xie67de1172013-12-26 13:07:06 +08003826 path->slots[0]++;
3827 if (inode->i_nlink != 1 ||
3828 path->slots[0] >= btrfs_header_nritems(leaf))
3829 goto cache_acl;
3830
3831 btrfs_item_key_to_cpu(leaf, &location, path->slots[0]);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003832 if (location.objectid != btrfs_ino(BTRFS_I(inode)))
Miao Xie67de1172013-12-26 13:07:06 +08003833 goto cache_acl;
3834
3835 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3836 if (location.type == BTRFS_INODE_REF_KEY) {
3837 struct btrfs_inode_ref *ref;
3838
3839 ref = (struct btrfs_inode_ref *)ptr;
3840 BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref);
3841 } else if (location.type == BTRFS_INODE_EXTREF_KEY) {
3842 struct btrfs_inode_extref *extref;
3843
3844 extref = (struct btrfs_inode_extref *)ptr;
3845 BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf,
3846 extref);
3847 }
Miao Xie2f7e33d2011-06-23 07:27:13 +00003848cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04003849 /*
3850 * try to precache a NULL acl entry for files that don't have
3851 * any xattrs or acls
3852 */
Li Zefan33345d012011-04-20 10:31:50 +08003853 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
David Sterbaf85b7372017-01-20 14:54:07 +01003854 btrfs_ino(BTRFS_I(inode)), &first_xattr_slot);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003855 if (first_xattr_slot != -1) {
3856 path->slots[0] = first_xattr_slot;
3857 ret = btrfs_load_inode_props(inode, path);
3858 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003859 btrfs_err(fs_info,
David Sterba351fd352014-05-15 16:48:20 +02003860 "error loading props for ino %llu (root %llu): %d",
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003861 btrfs_ino(BTRFS_I(inode)),
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003862 root->root_key.objectid, ret);
3863 }
Filipe Manana4222ea72018-10-24 10:13:03 +01003864 if (path != in_path)
3865 btrfs_free_path(path);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003866
Al Viro72c04902009-06-24 16:58:48 -04003867 if (!maybe_acls)
3868 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04003869
Chris Mason39279cc2007-06-12 06:35:45 -04003870 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04003871 case S_IFREG:
3872 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masond1310b22008-01-24 16:13:08 -05003873 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04003874 inode->i_fop = &btrfs_file_operations;
3875 inode->i_op = &btrfs_file_inode_operations;
3876 break;
3877 case S_IFDIR:
3878 inode->i_fop = &btrfs_dir_file_operations;
Omar Sandoval67ade052017-01-25 17:06:38 -08003879 inode->i_op = &btrfs_dir_inode_operations;
Chris Mason39279cc2007-06-12 06:35:45 -04003880 break;
3881 case S_IFLNK:
3882 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -05003883 inode_nohighmem(inode);
Omar Sandoval4779cc02018-09-24 15:16:55 -07003884 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason39279cc2007-06-12 06:35:45 -04003885 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04003886 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05003887 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04003888 init_special_inode(inode, inode->i_mode, rdev);
3889 break;
Chris Mason39279cc2007-06-12 06:35:45 -04003890 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003891
David Sterba7b6a2212018-03-26 18:40:21 +02003892 btrfs_sync_inode_flags_to_i_flags(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003893 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003894}
3895
Chris Masond352ac62008-09-29 15:18:18 -04003896/*
3897 * given a leaf and an inode, copy the inode fields into the leaf
3898 */
Chris Masone02119d2008-09-05 16:13:11 -04003899static void fill_inode_item(struct btrfs_trans_handle *trans,
3900 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04003901 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04003902 struct inode *inode)
3903{
Liu Bo51fab692012-12-27 09:01:21 +00003904 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003905
David Sterbac82f8232019-08-09 17:48:21 +02003906 btrfs_init_map_token(&token, leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04003907
Liu Bo51fab692012-12-27 09:01:21 +00003908 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
3909 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
3910 btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size,
3911 &token);
3912 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3913 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003914
David Sterbaa937b972014-12-12 17:39:12 +01003915 btrfs_set_token_timespec_sec(leaf, &item->atime,
Liu Bo51fab692012-12-27 09:01:21 +00003916 inode->i_atime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003917 btrfs_set_token_timespec_nsec(leaf, &item->atime,
Liu Bo51fab692012-12-27 09:01:21 +00003918 inode->i_atime.tv_nsec, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003919
David Sterbaa937b972014-12-12 17:39:12 +01003920 btrfs_set_token_timespec_sec(leaf, &item->mtime,
Liu Bo51fab692012-12-27 09:01:21 +00003921 inode->i_mtime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003922 btrfs_set_token_timespec_nsec(leaf, &item->mtime,
Liu Bo51fab692012-12-27 09:01:21 +00003923 inode->i_mtime.tv_nsec, &token);
3924
David Sterbaa937b972014-12-12 17:39:12 +01003925 btrfs_set_token_timespec_sec(leaf, &item->ctime,
Liu Bo51fab692012-12-27 09:01:21 +00003926 inode->i_ctime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003927 btrfs_set_token_timespec_nsec(leaf, &item->ctime,
Liu Bo51fab692012-12-27 09:01:21 +00003928 inode->i_ctime.tv_nsec, &token);
3929
chandan r9cc97d62012-07-04 12:48:07 +05303930 btrfs_set_token_timespec_sec(leaf, &item->otime,
3931 BTRFS_I(inode)->i_otime.tv_sec, &token);
3932 btrfs_set_token_timespec_nsec(leaf, &item->otime,
3933 BTRFS_I(inode)->i_otime.tv_nsec, &token);
3934
Liu Bo51fab692012-12-27 09:01:21 +00003935 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3936 &token);
3937 btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation,
3938 &token);
Jeff Laytonc7f88c42017-12-11 06:35:12 -05003939 btrfs_set_token_inode_sequence(leaf, item, inode_peek_iversion(inode),
3940 &token);
Liu Bo51fab692012-12-27 09:01:21 +00003941 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
3942 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3943 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3944 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
Chris Mason39279cc2007-06-12 06:35:45 -04003945}
3946
Chris Masond352ac62008-09-29 15:18:18 -04003947/*
3948 * copy everything in the in-memory inode into the btree.
3949 */
Chris Mason21151332011-11-10 20:39:08 -05003950static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Chris Masond3977122009-01-05 21:25:51 -05003951 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003952{
3953 struct btrfs_inode_item *inode_item;
3954 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003955 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003956 int ret;
3957
3958 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08003959 if (!path)
3960 return -ENOMEM;
3961
Chris Masonb9473432009-03-13 11:00:37 -04003962 path->leave_spinning = 1;
Miao Xie16cdcec2011-04-22 18:12:22 +08003963 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
3964 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003965 if (ret) {
3966 if (ret > 0)
3967 ret = -ENOENT;
3968 goto failed;
3969 }
3970
Chris Mason5f39d392007-10-15 16:14:19 -04003971 leaf = path->nodes[0];
3972 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08003973 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04003974
Chris Masone02119d2008-09-05 16:13:11 -04003975 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04003976 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04003977 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003978 ret = 0;
3979failed:
Chris Mason39279cc2007-06-12 06:35:45 -04003980 btrfs_free_path(path);
3981 return ret;
3982}
3983
Chris Masond352ac62008-09-29 15:18:18 -04003984/*
Chris Mason21151332011-11-10 20:39:08 -05003985 * copy everything in the in-memory inode into the btree.
3986 */
3987noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
3988 struct btrfs_root *root, struct inode *inode)
3989{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003990 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason21151332011-11-10 20:39:08 -05003991 int ret;
3992
3993 /*
3994 * If the inode is a free space inode, we can deadlock during commit
3995 * if we put it into the delayed code.
3996 *
3997 * The data relocation inode should also be directly updated
3998 * without delay
3999 */
Nikolay Borisov70ddc552017-02-20 13:50:35 +02004000 if (!btrfs_is_free_space_inode(BTRFS_I(inode))
Josef Bacik1d52c782014-09-18 11:30:44 -04004001 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004002 && !test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) {
Alexander Block8ea05e32012-07-25 17:35:53 +02004003 btrfs_update_root_times(trans, root);
4004
Chris Mason21151332011-11-10 20:39:08 -05004005 ret = btrfs_delayed_update_inode(trans, root, inode);
4006 if (!ret)
4007 btrfs_set_inode_last_trans(trans, inode);
4008 return ret;
4009 }
4010
4011 return btrfs_update_inode_item(trans, root, inode);
4012}
4013
Josef Bacikbe6aef62012-10-22 15:43:12 -04004014noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
4015 struct btrfs_root *root,
4016 struct inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05004017{
4018 int ret;
4019
4020 ret = btrfs_update_inode(trans, root, inode);
4021 if (ret == -ENOSPC)
4022 return btrfs_update_inode_item(trans, root, inode);
4023 return ret;
4024}
4025
4026/*
Chris Masond352ac62008-09-29 15:18:18 -04004027 * unlink helper that gets used here in inode.c and in the tree logging
4028 * recovery code. It remove a link in a directory with a given name, and
4029 * also drops the back refs in the inode to the directory
4030 */
Al Viro92986792011-03-04 17:14:37 +00004031static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
4032 struct btrfs_root *root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004033 struct btrfs_inode *dir,
4034 struct btrfs_inode *inode,
Al Viro92986792011-03-04 17:14:37 +00004035 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04004036{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004037 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04004038 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04004039 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004040 struct btrfs_dir_item *di;
Josef Bacikaec74772008-07-24 12:12:38 -04004041 u64 index;
Li Zefan33345d012011-04-20 10:31:50 +08004042 u64 ino = btrfs_ino(inode);
4043 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04004044
4045 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04004046 if (!path) {
4047 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00004048 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04004049 }
4050
Chris Masonb9473432009-03-13 11:00:37 -04004051 path->leave_spinning = 1;
Li Zefan33345d012011-04-20 10:31:50 +08004052 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04004053 name, name_len, -1);
Liu Bo3cf50682018-09-12 06:06:26 +08004054 if (IS_ERR_OR_NULL(di)) {
4055 ret = di ? PTR_ERR(di) : -ENOENT;
Chris Mason39279cc2007-06-12 06:35:45 -04004056 goto err;
4057 }
Chris Mason39279cc2007-06-12 06:35:45 -04004058 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04004059 if (ret)
4060 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02004061 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04004062
Miao Xie67de1172013-12-26 13:07:06 +08004063 /*
4064 * If we don't have dir index, we have to get it by looking up
4065 * the inode ref, since we get the inode ref, remove it directly,
4066 * it is unnecessary to do delayed deletion.
4067 *
4068 * But if we have dir index, needn't search inode ref to get it.
4069 * Since the inode ref is close to the inode item, it is better
4070 * that we delay to delete it, and just do this deletion when
4071 * we update the inode item.
4072 */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004073 if (inode->dir_index) {
Miao Xie67de1172013-12-26 13:07:06 +08004074 ret = btrfs_delayed_delete_inode_ref(inode);
4075 if (!ret) {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004076 index = inode->dir_index;
Miao Xie67de1172013-12-26 13:07:06 +08004077 goto skip_backref;
4078 }
4079 }
4080
Li Zefan33345d012011-04-20 10:31:50 +08004081 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
4082 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04004083 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004084 btrfs_info(fs_info,
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00004085 "failed to delete reference to %.*s, inode %llu parent %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02004086 name_len, name, ino, dir_ino);
Jeff Mahoney66642832016-06-10 18:19:25 -04004087 btrfs_abort_transaction(trans, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04004088 goto err;
4089 }
Miao Xie67de1172013-12-26 13:07:06 +08004090skip_backref:
Lu Fengqi9add2942018-08-01 11:32:26 +08004091 ret = btrfs_delete_delayed_dir_index(trans, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004092 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004093 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04004094 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004095 }
Chris Mason39279cc2007-06-12 06:35:45 -04004096
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004097 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len, inode,
4098 dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004099 if (ret != 0 && ret != -ENOENT) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004100 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004101 goto err;
4102 }
Chris Masone02119d2008-09-05 16:13:11 -04004103
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004104 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len, dir,
4105 index);
Chris Mason6418c962010-10-30 07:34:24 -04004106 if (ret == -ENOENT)
4107 ret = 0;
Zach Brownd4e39912013-04-02 21:02:16 +00004108 else if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004109 btrfs_abort_transaction(trans, ret);
Josef Bacik63611e72019-06-18 10:59:18 -04004110
4111 /*
4112 * If we have a pending delayed iput we could end up with the final iput
4113 * being run in btrfs-cleaner context. If we have enough of these built
4114 * up we can end up burning a lot of time in btrfs-cleaner without any
4115 * way to throttle the unlinks. Since we're currently holding a ref on
4116 * the inode we can run the delayed iput here without any issues as the
4117 * final iput won't be done until after we drop the ref we're currently
4118 * holding.
4119 */
4120 btrfs_run_delayed_iput(fs_info, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004121err:
4122 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04004123 if (ret)
4124 goto out;
4125
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004126 btrfs_i_size_write(dir, dir->vfs_inode.i_size - name_len * 2);
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004127 inode_inc_iversion(&inode->vfs_inode);
4128 inode_inc_iversion(&dir->vfs_inode);
4129 inode->vfs_inode.i_ctime = dir->vfs_inode.i_mtime =
4130 dir->vfs_inode.i_ctime = current_time(&inode->vfs_inode);
4131 ret = btrfs_update_inode(trans, root, &dir->vfs_inode);
Chris Masone02119d2008-09-05 16:13:11 -04004132out:
Chris Mason39279cc2007-06-12 06:35:45 -04004133 return ret;
4134}
4135
Al Viro92986792011-03-04 17:14:37 +00004136int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
4137 struct btrfs_root *root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004138 struct btrfs_inode *dir, struct btrfs_inode *inode,
Al Viro92986792011-03-04 17:14:37 +00004139 const char *name, int name_len)
4140{
4141 int ret;
4142 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
4143 if (!ret) {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004144 drop_nlink(&inode->vfs_inode);
4145 ret = btrfs_update_inode(trans, root, &inode->vfs_inode);
Al Viro92986792011-03-04 17:14:37 +00004146 }
4147 return ret;
4148}
Yan, Zhenga22285a2010-05-16 10:48:46 -04004149
4150/*
4151 * helper to start transaction for unlink and rmdir.
4152 *
Josef Bacikd52be812013-05-29 14:54:47 -04004153 * unlink and rmdir are special in btrfs, they do not always free space, so
4154 * if we cannot make our reservations the normal way try and see if there is
4155 * plenty of slack room in the global reserve to migrate, otherwise we cannot
4156 * allow the unlink to occur.
Yan, Zhenga22285a2010-05-16 10:48:46 -04004157 */
Josef Bacikd52be812013-05-29 14:54:47 -04004158static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir)
Yan, Zhenga22285a2010-05-16 10:48:46 -04004159{
Yan, Zhenga22285a2010-05-16 10:48:46 -04004160 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004161
Josef Bacike70bea52011-10-11 14:18:24 -04004162 /*
4163 * 1 for the possible orphan item
4164 * 1 for the dir item
4165 * 1 for the dir index
4166 * 1 for the inode ref
Josef Bacike70bea52011-10-11 14:18:24 -04004167 * 1 for the inode
4168 */
Filipe Manana8eab77f2015-11-13 23:57:16 +00004169 return btrfs_start_transaction_fallback_global_rsv(root, 5, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004170}
4171
Chris Mason39279cc2007-06-12 06:35:45 -04004172static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
4173{
Yan, Zhenga22285a2010-05-16 10:48:46 -04004174 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004175 struct btrfs_trans_handle *trans;
David Howells2b0143b2015-03-17 22:25:59 +00004176 struct inode *inode = d_inode(dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04004177 int ret;
4178
Josef Bacikd52be812013-05-29 14:54:47 -04004179 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004180 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004181 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04004182
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004183 btrfs_record_unlink_dir(trans, BTRFS_I(dir), BTRFS_I(d_inode(dentry)),
4184 0);
Chris Mason12fcfd22009-03-24 10:24:20 -04004185
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004186 ret = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
4187 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
4188 dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00004189 if (ret)
4190 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004191
Yan, Zhenga22285a2010-05-16 10:48:46 -04004192 if (inode->i_nlink == 0) {
Nikolay Borisov73f2e542017-02-20 13:50:59 +02004193 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Tsutomu Itohb5324022011-07-19 07:27:20 +00004194 if (ret)
4195 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004196 }
Josef Bacik7b128762008-07-24 12:17:14 -04004197
Tsutomu Itohb5324022011-07-19 07:27:20 +00004198out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004199 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004200 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04004201 return ret;
4202}
4203
Misono Tomohirof60a2362018-04-18 11:34:52 +09004204static int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
Lu Fengqi401b3b12018-08-01 11:32:30 +08004205 struct inode *dir, u64 objectid,
4206 const char *name, int name_len)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004207{
Lu Fengqi401b3b12018-08-01 11:32:30 +08004208 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004209 struct btrfs_path *path;
4210 struct extent_buffer *leaf;
4211 struct btrfs_dir_item *di;
4212 struct btrfs_key key;
4213 u64 index;
4214 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004215 u64 dir_ino = btrfs_ino(BTRFS_I(dir));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004216
4217 path = btrfs_alloc_path();
4218 if (!path)
4219 return -ENOMEM;
4220
Li Zefan33345d012011-04-20 10:31:50 +08004221 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004222 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004223 if (IS_ERR_OR_NULL(di)) {
Liu Bo3cf50682018-09-12 06:06:26 +08004224 ret = di ? PTR_ERR(di) : -ENOENT;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004225 goto out;
4226 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004227
4228 leaf = path->nodes[0];
4229 btrfs_dir_item_key_to_cpu(leaf, di, &key);
4230 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
4231 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004232 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004233 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004234 goto out;
4235 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004236 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004237
Lu Fengqi3ee1c552018-08-01 11:32:28 +08004238 ret = btrfs_del_root_ref(trans, objectid, root->root_key.objectid,
4239 dir_ino, &index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004240 if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004241 if (ret != -ENOENT) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004242 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004243 goto out;
4244 }
Li Zefan33345d012011-04-20 10:31:50 +08004245 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004246 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004247 if (IS_ERR_OR_NULL(di)) {
4248 if (!di)
4249 ret = -ENOENT;
4250 else
4251 ret = PTR_ERR(di);
Jeff Mahoney66642832016-06-10 18:19:25 -04004252 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004253 goto out;
4254 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004255
4256 leaf = path->nodes[0];
4257 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004258 index = key.offset;
4259 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004260 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004261
Lu Fengqi9add2942018-08-01 11:32:26 +08004262 ret = btrfs_delete_delayed_dir_index(trans, BTRFS_I(dir), index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004263 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004264 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004265 goto out;
4266 }
Miao Xie16cdcec2011-04-22 18:12:22 +08004267
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004268 btrfs_i_size_write(BTRFS_I(dir), dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004269 inode_inc_iversion(dir);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07004270 dir->i_mtime = dir->i_ctime = current_time(dir);
Josef Bacik5a24e842012-08-08 10:12:59 -06004271 ret = btrfs_update_inode_fallback(trans, root, dir);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004272 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004273 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004274out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04004275 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004276 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004277}
4278
Misono Tomohiroec42f162018-04-18 11:34:13 +09004279/*
4280 * Helper to check if the subvolume references other subvolumes or if it's
4281 * default.
4282 */
Misono Tomohirof60a2362018-04-18 11:34:52 +09004283static noinline int may_destroy_subvol(struct btrfs_root *root)
Misono Tomohiroec42f162018-04-18 11:34:13 +09004284{
4285 struct btrfs_fs_info *fs_info = root->fs_info;
4286 struct btrfs_path *path;
4287 struct btrfs_dir_item *di;
4288 struct btrfs_key key;
4289 u64 dir_id;
4290 int ret;
4291
4292 path = btrfs_alloc_path();
4293 if (!path)
4294 return -ENOMEM;
4295
4296 /* Make sure this root isn't set as the default subvol */
4297 dir_id = btrfs_super_root_dir(fs_info->super_copy);
4298 di = btrfs_lookup_dir_item(NULL, fs_info->tree_root, path,
4299 dir_id, "default", 7, 0);
4300 if (di && !IS_ERR(di)) {
4301 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key);
4302 if (key.objectid == root->root_key.objectid) {
4303 ret = -EPERM;
4304 btrfs_err(fs_info,
4305 "deleting default subvolume %llu is not allowed",
4306 key.objectid);
4307 goto out;
4308 }
4309 btrfs_release_path(path);
4310 }
4311
4312 key.objectid = root->root_key.objectid;
4313 key.type = BTRFS_ROOT_REF_KEY;
4314 key.offset = (u64)-1;
4315
4316 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
4317 if (ret < 0)
4318 goto out;
4319 BUG_ON(ret == 0);
4320
4321 ret = 0;
4322 if (path->slots[0] > 0) {
4323 path->slots[0]--;
4324 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
4325 if (key.objectid == root->root_key.objectid &&
4326 key.type == BTRFS_ROOT_REF_KEY)
4327 ret = -ENOTEMPTY;
4328 }
4329out:
4330 btrfs_free_path(path);
4331 return ret;
4332}
4333
Nikolay Borisov20a68002018-04-27 14:36:24 +03004334/* Delete all dentries for inodes belonging to the root */
4335static void btrfs_prune_dentries(struct btrfs_root *root)
4336{
4337 struct btrfs_fs_info *fs_info = root->fs_info;
4338 struct rb_node *node;
4339 struct rb_node *prev;
4340 struct btrfs_inode *entry;
4341 struct inode *inode;
4342 u64 objectid = 0;
4343
4344 if (!test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
4345 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
4346
4347 spin_lock(&root->inode_lock);
4348again:
4349 node = root->inode_tree.rb_node;
4350 prev = NULL;
4351 while (node) {
4352 prev = node;
4353 entry = rb_entry(node, struct btrfs_inode, rb_node);
4354
David Sterba37508512018-06-29 10:56:40 +02004355 if (objectid < btrfs_ino(entry))
Nikolay Borisov20a68002018-04-27 14:36:24 +03004356 node = node->rb_left;
David Sterba37508512018-06-29 10:56:40 +02004357 else if (objectid > btrfs_ino(entry))
Nikolay Borisov20a68002018-04-27 14:36:24 +03004358 node = node->rb_right;
4359 else
4360 break;
4361 }
4362 if (!node) {
4363 while (prev) {
4364 entry = rb_entry(prev, struct btrfs_inode, rb_node);
David Sterba37508512018-06-29 10:56:40 +02004365 if (objectid <= btrfs_ino(entry)) {
Nikolay Borisov20a68002018-04-27 14:36:24 +03004366 node = prev;
4367 break;
4368 }
4369 prev = rb_next(prev);
4370 }
4371 }
4372 while (node) {
4373 entry = rb_entry(node, struct btrfs_inode, rb_node);
David Sterba37508512018-06-29 10:56:40 +02004374 objectid = btrfs_ino(entry) + 1;
Nikolay Borisov20a68002018-04-27 14:36:24 +03004375 inode = igrab(&entry->vfs_inode);
4376 if (inode) {
4377 spin_unlock(&root->inode_lock);
4378 if (atomic_read(&inode->i_count) > 1)
4379 d_prune_aliases(inode);
4380 /*
4381 * btrfs_drop_inode will have it removed from the inode
4382 * cache when its usage count hits zero.
4383 */
4384 iput(inode);
4385 cond_resched();
4386 spin_lock(&root->inode_lock);
4387 goto again;
4388 }
4389
4390 if (cond_resched_lock(&root->inode_lock))
4391 goto again;
4392
4393 node = rb_next(node);
4394 }
4395 spin_unlock(&root->inode_lock);
4396}
4397
Misono Tomohirof60a2362018-04-18 11:34:52 +09004398int btrfs_delete_subvolume(struct inode *dir, struct dentry *dentry)
4399{
4400 struct btrfs_fs_info *fs_info = btrfs_sb(dentry->d_sb);
4401 struct btrfs_root *root = BTRFS_I(dir)->root;
4402 struct inode *inode = d_inode(dentry);
4403 struct btrfs_root *dest = BTRFS_I(inode)->root;
4404 struct btrfs_trans_handle *trans;
4405 struct btrfs_block_rsv block_rsv;
4406 u64 root_flags;
Misono Tomohirof60a2362018-04-18 11:34:52 +09004407 int ret;
4408 int err;
4409
4410 /*
4411 * Don't allow to delete a subvolume with send in progress. This is
4412 * inside the inode lock so the error handling that has to drop the bit
4413 * again is not run concurrently.
4414 */
4415 spin_lock(&dest->root_item_lock);
Lu Fengqia7176f72018-08-04 21:10:53 +08004416 if (dest->send_in_progress) {
Misono Tomohirof60a2362018-04-18 11:34:52 +09004417 spin_unlock(&dest->root_item_lock);
4418 btrfs_warn(fs_info,
4419 "attempt to delete subvolume %llu during send",
4420 dest->root_key.objectid);
4421 return -EPERM;
4422 }
Lu Fengqia7176f72018-08-04 21:10:53 +08004423 root_flags = btrfs_root_flags(&dest->root_item);
4424 btrfs_set_root_flags(&dest->root_item,
4425 root_flags | BTRFS_ROOT_SUBVOL_DEAD);
4426 spin_unlock(&dest->root_item_lock);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004427
4428 down_write(&fs_info->subvol_sem);
4429
4430 err = may_destroy_subvol(dest);
4431 if (err)
4432 goto out_up_write;
4433
4434 btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP);
4435 /*
4436 * One for dir inode,
4437 * two for dir entries,
4438 * two for root ref/backref.
4439 */
Gu JinXiangc4c129d2018-05-30 11:00:38 +08004440 err = btrfs_subvolume_reserve_metadata(root, &block_rsv, 5, true);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004441 if (err)
4442 goto out_up_write;
4443
4444 trans = btrfs_start_transaction(root, 0);
4445 if (IS_ERR(trans)) {
4446 err = PTR_ERR(trans);
4447 goto out_release;
4448 }
4449 trans->block_rsv = &block_rsv;
4450 trans->bytes_reserved = block_rsv.size;
4451
4452 btrfs_record_snapshot_destroy(trans, BTRFS_I(dir));
4453
Lu Fengqi401b3b12018-08-01 11:32:30 +08004454 ret = btrfs_unlink_subvol(trans, dir, dest->root_key.objectid,
4455 dentry->d_name.name, dentry->d_name.len);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004456 if (ret) {
4457 err = ret;
4458 btrfs_abort_transaction(trans, ret);
4459 goto out_end_trans;
4460 }
4461
4462 btrfs_record_root_in_trans(trans, dest);
4463
4464 memset(&dest->root_item.drop_progress, 0,
4465 sizeof(dest->root_item.drop_progress));
4466 dest->root_item.drop_level = 0;
4467 btrfs_set_root_refs(&dest->root_item, 0);
4468
4469 if (!test_and_set_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &dest->state)) {
4470 ret = btrfs_insert_orphan_item(trans,
4471 fs_info->tree_root,
4472 dest->root_key.objectid);
4473 if (ret) {
4474 btrfs_abort_transaction(trans, ret);
4475 err = ret;
4476 goto out_end_trans;
4477 }
4478 }
4479
Lu Fengqid1957792018-05-29 15:01:54 +08004480 ret = btrfs_uuid_tree_remove(trans, dest->root_item.uuid,
Misono Tomohirof60a2362018-04-18 11:34:52 +09004481 BTRFS_UUID_KEY_SUBVOL,
4482 dest->root_key.objectid);
4483 if (ret && ret != -ENOENT) {
4484 btrfs_abort_transaction(trans, ret);
4485 err = ret;
4486 goto out_end_trans;
4487 }
4488 if (!btrfs_is_empty_uuid(dest->root_item.received_uuid)) {
Lu Fengqid1957792018-05-29 15:01:54 +08004489 ret = btrfs_uuid_tree_remove(trans,
Misono Tomohirof60a2362018-04-18 11:34:52 +09004490 dest->root_item.received_uuid,
4491 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
4492 dest->root_key.objectid);
4493 if (ret && ret != -ENOENT) {
4494 btrfs_abort_transaction(trans, ret);
4495 err = ret;
4496 goto out_end_trans;
4497 }
4498 }
4499
4500out_end_trans:
4501 trans->block_rsv = NULL;
4502 trans->bytes_reserved = 0;
4503 ret = btrfs_end_transaction(trans);
4504 if (ret && !err)
4505 err = ret;
4506 inode->i_flags |= S_DEAD;
4507out_release:
4508 btrfs_subvolume_release_metadata(fs_info, &block_rsv);
4509out_up_write:
4510 up_write(&fs_info->subvol_sem);
4511 if (err) {
4512 spin_lock(&dest->root_item_lock);
4513 root_flags = btrfs_root_flags(&dest->root_item);
4514 btrfs_set_root_flags(&dest->root_item,
4515 root_flags & ~BTRFS_ROOT_SUBVOL_DEAD);
4516 spin_unlock(&dest->root_item_lock);
4517 } else {
4518 d_invalidate(dentry);
Nikolay Borisov20a68002018-04-27 14:36:24 +03004519 btrfs_prune_dentries(dest);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004520 ASSERT(dest->send_in_progress == 0);
4521
4522 /* the last ref */
4523 if (dest->ino_cache_inode) {
4524 iput(dest->ino_cache_inode);
4525 dest->ino_cache_inode = NULL;
4526 }
4527 }
4528
4529 return err;
4530}
4531
Chris Mason39279cc2007-06-12 06:35:45 -04004532static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
4533{
David Howells2b0143b2015-03-17 22:25:59 +00004534 struct inode *inode = d_inode(dentry);
Chris Mason1832a6d2007-12-21 16:27:21 -05004535 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004536 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004537 struct btrfs_trans_handle *trans;
Filipe Manana44f714d2016-06-06 16:11:13 +01004538 u64 last_unlink_trans;
Chris Mason39279cc2007-06-12 06:35:45 -04004539
David Sterbab3ae2442012-09-13 16:04:34 -06004540 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Yan134d4512007-10-25 15:49:25 -04004541 return -ENOTEMPTY;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004542 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_FIRST_FREE_OBJECTID)
Misono Tomohiroa79a4642018-04-18 11:35:31 +09004543 return btrfs_delete_subvolume(dir, dentry);
Yan134d4512007-10-25 15:49:25 -04004544
Josef Bacikd52be812013-05-29 14:54:47 -04004545 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004546 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004547 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004548
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004549 if (unlikely(btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Lu Fengqi401b3b12018-08-01 11:32:30 +08004550 err = btrfs_unlink_subvol(trans, dir,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004551 BTRFS_I(inode)->location.objectid,
4552 dentry->d_name.name,
4553 dentry->d_name.len);
4554 goto out;
4555 }
4556
Nikolay Borisov73f2e542017-02-20 13:50:59 +02004557 err = btrfs_orphan_add(trans, BTRFS_I(inode));
Josef Bacik7b128762008-07-24 12:17:14 -04004558 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004559 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004560
Filipe Manana44f714d2016-06-06 16:11:13 +01004561 last_unlink_trans = BTRFS_I(inode)->last_unlink_trans;
4562
Chris Mason39279cc2007-06-12 06:35:45 -04004563 /* now the directory is empty */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004564 err = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
4565 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
4566 dentry->d_name.len);
Filipe Manana44f714d2016-06-06 16:11:13 +01004567 if (!err) {
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004568 btrfs_i_size_write(BTRFS_I(inode), 0);
Filipe Manana44f714d2016-06-06 16:11:13 +01004569 /*
4570 * Propagate the last_unlink_trans value of the deleted dir to
4571 * its parent directory. This is to prevent an unrecoverable
4572 * log tree in the case we do something like this:
4573 * 1) create dir foo
4574 * 2) create snapshot under dir foo
4575 * 3) delete the snapshot
4576 * 4) rmdir foo
4577 * 5) mkdir foo
4578 * 6) fsync foo or some file inside foo
4579 */
4580 if (last_unlink_trans >= trans->transid)
4581 BTRFS_I(dir)->last_unlink_trans = last_unlink_trans;
4582 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004583out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004584 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004585 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39544012007-12-12 14:38:19 -05004586
Chris Mason39279cc2007-06-12 06:35:45 -04004587 return err;
4588}
4589
Josef Bacikddfae632017-10-19 14:16:02 -04004590/*
4591 * Return this if we need to call truncate_block for the last bit of the
4592 * truncate.
4593 */
4594#define NEED_TRUNCATE_BLOCK 1
Filipe Manana0305cd52015-10-16 12:34:25 +01004595
Chris Mason323ac952008-10-01 19:05:46 -04004596/*
Chris Mason39279cc2007-06-12 06:35:45 -04004597 * this can truncate away extent items, csum items and directory items.
4598 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04004599 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04004600 *
4601 * csum items that cross the new i_size are truncated to the new size
4602 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04004603 *
4604 * min_type is the minimum key type to truncate down to. If set to 0, this
4605 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04004606 */
Yan, Zheng80825102009-11-12 09:35:36 +00004607int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
4608 struct btrfs_root *root,
4609 struct inode *inode,
4610 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004611{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004612 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04004613 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004614 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004615 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00004616 struct btrfs_key key;
4617 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04004618 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04004619 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004620 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004621 u64 item_end = 0;
Filipe Mananac1aa4572015-06-20 18:20:09 +01004622 u64 last_size = new_size;
Yan, Zheng80825102009-11-12 09:35:36 +00004623 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04004624 int found_extent;
4625 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05004626 int pending_del_nr = 0;
4627 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04004628 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00004629 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004630 u64 ino = btrfs_ino(BTRFS_I(inode));
Chris Mason28ed1342014-12-17 09:41:04 -08004631 u64 bytes_deleted = 0;
Thomas Meyer897ca812017-10-07 16:02:21 +02004632 bool be_nice = false;
4633 bool should_throttle = false;
Yan, Zheng80825102009-11-12 09:35:36 +00004634
4635 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04004636
Chris Mason28ed1342014-12-17 09:41:04 -08004637 /*
4638 * for non-free space inodes and ref cows, we want to back off from
4639 * time to time
4640 */
Nikolay Borisov70ddc552017-02-20 13:50:35 +02004641 if (!btrfs_is_free_space_inode(BTRFS_I(inode)) &&
Chris Mason28ed1342014-12-17 09:41:04 -08004642 test_bit(BTRFS_ROOT_REF_COWS, &root->state))
Thomas Meyer897ca812017-10-07 16:02:21 +02004643 be_nice = true;
Chris Mason28ed1342014-12-17 09:41:04 -08004644
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004645 path = btrfs_alloc_path();
4646 if (!path)
4647 return -ENOMEM;
David Sterbae4058b52015-11-27 16:31:35 +01004648 path->reada = READA_BACK;
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004649
Josef Bacik5dc562c2012-08-17 13:14:17 -04004650 /*
4651 * We want to drop from the next block forward in case this new size is
4652 * not block aligned since we will be keeping the last block of the
4653 * extent just the way it is.
4654 */
Miao Xie27cdeb72014-04-02 19:51:05 +08004655 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004656 root == fs_info->tree_root)
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02004657 btrfs_drop_extent_cache(BTRFS_I(inode), ALIGN(new_size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004658 fs_info->sectorsize),
Jeff Mahoneyda170662016-06-15 09:22:56 -04004659 (u64)-1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00004660
Miao Xie16cdcec2011-04-22 18:12:22 +08004661 /*
4662 * This function is also used to drop the items in the log tree before
4663 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
Andrea Gelmini52042d82018-11-28 12:05:13 +01004664 * it is used to drop the logged items. So we shouldn't kill the delayed
Miao Xie16cdcec2011-04-22 18:12:22 +08004665 * items.
4666 */
4667 if (min_type == 0 && root == BTRFS_I(inode)->root)
Nikolay Borisov4ccb5c72017-01-10 20:35:38 +02004668 btrfs_kill_delayed_inode_items(BTRFS_I(inode));
Miao Xie16cdcec2011-04-22 18:12:22 +08004669
Li Zefan33345d012011-04-20 10:31:50 +08004670 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04004671 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04004672 key.type = (u8)-1;
4673
Chris Mason85e21ba2008-01-29 15:11:36 -05004674search_again:
Chris Mason28ed1342014-12-17 09:41:04 -08004675 /*
4676 * with a 16K leaf size and 128MB extents, you can actually queue
4677 * up a huge file in a single leaf. Most of the time that
4678 * bytes_deleted is > 0, it will be huge by the time we get here
4679 */
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004680 if (be_nice && bytes_deleted > SZ_32M &&
4681 btrfs_should_end_transaction(trans)) {
4682 ret = -EAGAIN;
Yan, Zheng80825102009-11-12 09:35:36 +00004683 goto out;
4684 }
Chris Masond3977122009-01-05 21:25:51 -05004685
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004686 path->leave_spinning = 1;
4687 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
4688 if (ret < 0)
4689 goto out;
4690
Chris Mason85e21ba2008-01-29 15:11:36 -05004691 if (ret > 0) {
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004692 ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -04004693 /* there are no items in the tree for us to truncate, we're
4694 * done
4695 */
Yan, Zheng80825102009-11-12 09:35:36 +00004696 if (path->slots[0] == 0)
4697 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05004698 path->slots[0]--;
4699 }
4700
Chris Masond3977122009-01-05 21:25:51 -05004701 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04004702 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04004703 leaf = path->nodes[0];
4704 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02004705 found_type = found_key.type;
Chris Mason39279cc2007-06-12 06:35:45 -04004706
Li Zefan33345d012011-04-20 10:31:50 +08004707 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04004708 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004709
Chris Mason85e21ba2008-01-29 15:11:36 -05004710 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004711 break;
4712
Chris Mason5f39d392007-10-15 16:14:19 -04004713 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04004714 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04004715 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04004716 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04004717 extent_type = btrfs_file_extent_type(leaf, fi);
4718 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04004719 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04004720 btrfs_file_extent_num_bytes(leaf, fi);
Liu Bo09ed2f12017-03-10 11:09:48 -08004721
4722 trace_btrfs_truncate_show_fi_regular(
4723 BTRFS_I(inode), leaf, fi,
4724 found_key.offset);
Chris Mason179e29e2007-11-01 11:28:41 -04004725 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Qu Wenruoe41ca582018-06-06 15:41:49 +08004726 item_end += btrfs_file_extent_ram_bytes(leaf,
4727 fi);
Liu Bo09ed2f12017-03-10 11:09:48 -08004728
4729 trace_btrfs_truncate_show_fi_inline(
4730 BTRFS_I(inode), leaf, fi, path->slots[0],
4731 found_key.offset);
Chris Mason39279cc2007-06-12 06:35:45 -04004732 }
Yan008630c2007-11-07 13:31:09 -05004733 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04004734 }
Yan, Zheng80825102009-11-12 09:35:36 +00004735 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04004736 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00004737 } else {
Filipe Manana76b42ab2017-02-14 16:56:01 +00004738 if (item_end < new_size)
Yan, Zheng80825102009-11-12 09:35:36 +00004739 break;
4740 if (found_key.offset >= new_size)
4741 del_item = 1;
4742 else
4743 del_item = 0;
4744 }
Chris Mason39279cc2007-06-12 06:35:45 -04004745 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004746 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04004747 if (found_type != BTRFS_EXTENT_DATA_KEY)
4748 goto delete;
4749
4750 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04004751 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04004752 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05004753 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04004754 u64 orig_num_bytes =
4755 btrfs_file_extent_num_bytes(leaf, fi);
Qu Wenruofda28322013-02-26 08:10:22 +00004756 extent_num_bytes = ALIGN(new_size -
4757 found_key.offset,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004758 fs_info->sectorsize);
Chris Masondb945352007-10-15 16:15:53 -04004759 btrfs_set_file_extent_num_bytes(leaf, fi,
4760 extent_num_bytes);
4761 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05004762 extent_num_bytes);
Miao Xie27cdeb72014-04-02 19:51:05 +08004763 if (test_bit(BTRFS_ROOT_REF_COWS,
4764 &root->state) &&
4765 extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004766 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04004767 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004768 } else {
Chris Masondb945352007-10-15 16:15:53 -04004769 extent_num_bytes =
4770 btrfs_file_extent_disk_num_bytes(leaf,
4771 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004772 extent_offset = found_key.offset -
4773 btrfs_file_extent_offset(leaf, fi);
4774
Chris Mason39279cc2007-06-12 06:35:45 -04004775 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05004776 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004777 if (extent_start != 0) {
4778 found_extent = 1;
Miao Xie27cdeb72014-04-02 19:51:05 +08004779 if (test_bit(BTRFS_ROOT_REF_COWS,
4780 &root->state))
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004781 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04004782 }
4783 }
Chris Mason90692182008-02-08 13:49:28 -05004784 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04004785 /*
4786 * we can't truncate inline items that have had
4787 * special encodings
4788 */
4789 if (!del_item &&
Chris Masonc8b97812008-10-29 14:49:59 -04004790 btrfs_file_extent_encryption(leaf, fi) == 0 &&
Josef Bacikddfae632017-10-19 14:16:02 -04004791 btrfs_file_extent_other_encoding(leaf, fi) == 0 &&
4792 btrfs_file_extent_compression(leaf, fi) == 0) {
4793 u32 size = (u32)(new_size - found_key.offset);
Chris Mason514ac8a2014-01-03 21:07:00 -08004794
Josef Bacikddfae632017-10-19 14:16:02 -04004795 btrfs_set_file_extent_ram_bytes(leaf, fi, size);
4796 size = btrfs_file_extent_calc_inline_size(size);
David Sterba78ac4f92019-03-20 14:49:12 +01004797 btrfs_truncate_item(path, size, 1);
Josef Bacikddfae632017-10-19 14:16:02 -04004798 } else if (!del_item) {
Chris Mason514ac8a2014-01-03 21:07:00 -08004799 /*
Josef Bacikddfae632017-10-19 14:16:02 -04004800 * We have to bail so the last_size is set to
4801 * just before this extent.
Chris Mason514ac8a2014-01-03 21:07:00 -08004802 */
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004803 ret = NEED_TRUNCATE_BLOCK;
Josef Bacikddfae632017-10-19 14:16:02 -04004804 break;
Chris Mason90692182008-02-08 13:49:28 -05004805 }
Josef Bacikddfae632017-10-19 14:16:02 -04004806
4807 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state))
4808 inode_sub_bytes(inode, item_end + 1 - new_size);
Chris Mason39279cc2007-06-12 06:35:45 -04004809 }
Chris Mason179e29e2007-11-01 11:28:41 -04004810delete:
Josef Bacikddfae632017-10-19 14:16:02 -04004811 if (del_item)
4812 last_size = found_key.offset;
4813 else
4814 last_size = new_size;
Chris Mason39279cc2007-06-12 06:35:45 -04004815 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05004816 if (!pending_del_nr) {
4817 /* no pending yet, add ourselves */
4818 pending_del_slot = path->slots[0];
4819 pending_del_nr = 1;
4820 } else if (pending_del_nr &&
4821 path->slots[0] + 1 == pending_del_slot) {
4822 /* hop on the pending chunk */
4823 pending_del_nr++;
4824 pending_del_slot = path->slots[0];
4825 } else {
Chris Masond3977122009-01-05 21:25:51 -05004826 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05004827 }
Chris Mason39279cc2007-06-12 06:35:45 -04004828 } else {
4829 break;
4830 }
Thomas Meyer897ca812017-10-07 16:02:21 +02004831 should_throttle = false;
Chris Mason28f75a02015-02-04 06:59:29 -08004832
Miao Xie27cdeb72014-04-02 19:51:05 +08004833 if (found_extent &&
4834 (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004835 root == fs_info->tree_root)) {
Qu Wenruoffd4bb22019-04-04 14:45:36 +08004836 struct btrfs_ref ref = { 0 };
4837
Chris Masonb9473432009-03-13 11:00:37 -04004838 btrfs_set_path_blocking(path);
Chris Mason28ed1342014-12-17 09:41:04 -08004839 bytes_deleted += extent_num_bytes;
Qu Wenruoffd4bb22019-04-04 14:45:36 +08004840
4841 btrfs_init_generic_ref(&ref, BTRFS_DROP_DELAYED_REF,
4842 extent_start, extent_num_bytes, 0);
4843 ref.real_root = root->root_key.objectid;
4844 btrfs_init_data_ref(&ref, btrfs_header_owner(leaf),
4845 ino, extent_offset);
4846 ret = btrfs_free_extent(trans, &ref);
Omar Sandoval05522102018-05-11 13:13:31 -07004847 if (ret) {
4848 btrfs_abort_transaction(trans, ret);
4849 break;
4850 }
Chris Mason28f75a02015-02-04 06:59:29 -08004851 if (be_nice) {
Lu Fengqi7c861622018-10-11 13:40:36 +08004852 if (btrfs_should_throttle_delayed_refs(trans))
Thomas Meyer897ca812017-10-07 16:02:21 +02004853 should_throttle = true;
Chris Mason28f75a02015-02-04 06:59:29 -08004854 }
Chris Mason39279cc2007-06-12 06:35:45 -04004855 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004856
Yan, Zheng80825102009-11-12 09:35:36 +00004857 if (found_type == BTRFS_INODE_ITEM_KEY)
4858 break;
4859
4860 if (path->slots[0] == 0 ||
Josef Bacik12621332015-02-03 07:50:16 -08004861 path->slots[0] != pending_del_slot ||
Josef Bacik28bad212018-12-03 10:20:38 -05004862 should_throttle) {
Yan, Zheng80825102009-11-12 09:35:36 +00004863 if (pending_del_nr) {
4864 ret = btrfs_del_items(trans, root, path,
4865 pending_del_slot,
4866 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004867 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004868 btrfs_abort_transaction(trans, ret);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004869 break;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004870 }
Yan, Zheng80825102009-11-12 09:35:36 +00004871 pending_del_nr = 0;
4872 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004873 btrfs_release_path(path);
Josef Bacik28bad212018-12-03 10:20:38 -05004874
Chris Mason28f75a02015-02-04 06:59:29 -08004875 /*
Josef Bacik28bad212018-12-03 10:20:38 -05004876 * We can generate a lot of delayed refs, so we need to
4877 * throttle every once and a while and make sure we're
4878 * adding enough space to keep up with the work we are
4879 * generating. Since we hold a transaction here we
4880 * can't flush, and we don't want to FLUSH_LIMIT because
4881 * we could have generated too many delayed refs to
4882 * actually allocate, so just bail if we're short and
4883 * let the normal reservation dance happen higher up.
Chris Mason28f75a02015-02-04 06:59:29 -08004884 */
Josef Bacik28bad212018-12-03 10:20:38 -05004885 if (should_throttle) {
4886 ret = btrfs_delayed_refs_rsv_refill(fs_info,
4887 BTRFS_RESERVE_NO_FLUSH);
4888 if (ret) {
4889 ret = -EAGAIN;
4890 break;
4891 }
Chris Mason28f75a02015-02-04 06:59:29 -08004892 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004893 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00004894 } else {
4895 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05004896 }
Chris Mason39279cc2007-06-12 06:35:45 -04004897 }
Yan, Zheng80825102009-11-12 09:35:36 +00004898out:
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004899 if (ret >= 0 && pending_del_nr) {
4900 int err;
4901
4902 err = btrfs_del_items(trans, root, path, pending_del_slot,
Chris Mason85e21ba2008-01-29 15:11:36 -05004903 pending_del_nr);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004904 if (err) {
4905 btrfs_abort_transaction(trans, err);
4906 ret = err;
4907 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004908 }
Filipe Manana76b42ab2017-02-14 16:56:01 +00004909 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
4910 ASSERT(last_size >= new_size);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004911 if (!ret && last_size > new_size)
Filipe Manana76b42ab2017-02-14 16:56:01 +00004912 last_size = new_size;
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004913 btrfs_ordered_update_i_size(inode, last_size, NULL);
Filipe Manana76b42ab2017-02-14 16:56:01 +00004914 }
Chris Mason28ed1342014-12-17 09:41:04 -08004915
Chris Mason39279cc2007-06-12 06:35:45 -04004916 btrfs_free_path(path);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004917 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04004918}
4919
Chris Masona52d9a82007-08-27 16:49:44 -04004920/*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304921 * btrfs_truncate_block - read, zero a chunk and write a block
Josef Bacik2aaa6652012-08-29 14:27:18 -04004922 * @inode - inode that we're zeroing
4923 * @from - the offset to start zeroing
4924 * @len - the length to zero, 0 to zero the entire range respective to the
4925 * offset
4926 * @front - zero up to the offset instead of from the offset on
4927 *
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304928 * This will find the block for the "from" offset and cow the block and zero the
Josef Bacik2aaa6652012-08-29 14:27:18 -04004929 * part we want to zero. This is used with truncate and hole punching.
Chris Masona52d9a82007-08-27 16:49:44 -04004930 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304931int btrfs_truncate_block(struct inode *inode, loff_t from, loff_t len,
Josef Bacik2aaa6652012-08-29 14:27:18 -04004932 int front)
Chris Masona52d9a82007-08-27 16:49:44 -04004933{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004934 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004935 struct address_space *mapping = inode->i_mapping;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004936 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4937 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004938 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08004939 struct extent_changeset *data_reserved = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004940 char *kaddr;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004941 u32 blocksize = fs_info->sectorsize;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004942 pgoff_t index = from >> PAGE_SHIFT;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304943 unsigned offset = from & (blocksize - 1);
Chris Masona52d9a82007-08-27 16:49:44 -04004944 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004945 gfp_t mask = btrfs_alloc_write_mask(mapping);
Chris Masona52d9a82007-08-27 16:49:44 -04004946 int ret = 0;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304947 u64 block_start;
4948 u64 block_end;
Chris Masona52d9a82007-08-27 16:49:44 -04004949
Nikolay Borisovb03ebd92018-01-18 14:47:06 +02004950 if (IS_ALIGNED(offset, blocksize) &&
4951 (!len || IS_ALIGNED(len, blocksize)))
Chris Masona52d9a82007-08-27 16:49:44 -04004952 goto out;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304953
Josef Bacik8b62f872017-10-19 14:15:55 -04004954 block_start = round_down(from, blocksize);
4955 block_end = block_start + blocksize - 1;
4956
Qu Wenruo364ecf32017-02-27 15:10:38 +08004957 ret = btrfs_delalloc_reserve_space(inode, &data_reserved,
Josef Bacik8b62f872017-10-19 14:15:55 -04004958 block_start, blocksize);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004959 if (ret)
4960 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004961
Chris Mason211c17f2008-05-15 09:13:45 -04004962again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004963 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004964 if (!page) {
Qu Wenruobc42bda2017-02-27 15:10:39 +08004965 btrfs_delalloc_release_space(inode, data_reserved,
Qu Wenruo43b18592017-12-12 15:34:32 +08004966 block_start, blocksize, true);
Qu Wenruo8702ba92019-10-14 14:34:51 +08004967 btrfs_delalloc_release_extents(BTRFS_I(inode), blocksize);
Miao Xieac6a2b32012-12-05 10:56:13 +00004968 ret = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -04004969 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04004970 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004971
Chris Masona52d9a82007-08-27 16:49:44 -04004972 if (!PageUptodate(page)) {
4973 ret = btrfs_readpage(NULL, page);
4974 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004975 if (page->mapping != mapping) {
4976 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004977 put_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004978 goto again;
4979 }
Chris Masona52d9a82007-08-27 16:49:44 -04004980 if (!PageUptodate(page)) {
4981 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04004982 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04004983 }
4984 }
Chris Mason211c17f2008-05-15 09:13:45 -04004985 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004986
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304987 lock_extent_bits(io_tree, block_start, block_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004988 set_page_extent_mapped(page);
4989
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304990 ordered = btrfs_lookup_ordered_extent(inode, block_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004991 if (ordered) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304992 unlock_extent_cached(io_tree, block_start, block_end,
David Sterbae43bbe52017-12-12 21:43:52 +01004993 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004994 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004995 put_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04004996 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004997 btrfs_put_ordered_extent(ordered);
4998 goto again;
4999 }
5000
Chandan Rajendra9703fef2016-01-21 15:55:56 +05305001 clear_extent_bit(&BTRFS_I(inode)->io_tree, block_start, block_end,
Omar Sandovale1821632019-08-15 14:04:04 -07005002 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
5003 0, 0, &cached_state);
Josef Bacik5d5e1032009-10-13 16:46:49 -04005004
Filipe Mananae3b8a482017-11-04 00:16:59 +00005005 ret = btrfs_set_extent_delalloc(inode, block_start, block_end, 0,
Nikolay Borisov330a5822019-07-17 16:18:17 +03005006 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04005007 if (ret) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05305008 unlock_extent_cached(io_tree, block_start, block_end,
David Sterbae43bbe52017-12-12 21:43:52 +01005009 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04005010 goto out_unlock;
5011 }
5012
Chandan Rajendra9703fef2016-01-21 15:55:56 +05305013 if (offset != blocksize) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04005014 if (!len)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05305015 len = blocksize - offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04005016 kaddr = kmap(page);
Josef Bacik2aaa6652012-08-29 14:27:18 -04005017 if (front)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05305018 memset(kaddr + (block_start - page_offset(page)),
5019 0, offset);
Josef Bacik2aaa6652012-08-29 14:27:18 -04005020 else
Chandan Rajendra9703fef2016-01-21 15:55:56 +05305021 memset(kaddr + (block_start - page_offset(page)) + offset,
5022 0, len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04005023 flush_dcache_page(page);
5024 kunmap(page);
5025 }
Chris Mason247e7432008-07-17 12:53:51 -04005026 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04005027 set_page_dirty(page);
David Sterbae43bbe52017-12-12 21:43:52 +01005028 unlock_extent_cached(io_tree, block_start, block_end, &cached_state);
Chris Mason39279cc2007-06-12 06:35:45 -04005029
Chris Mason89642222008-07-24 09:41:53 -04005030out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04005031 if (ret)
Qu Wenruobc42bda2017-02-27 15:10:39 +08005032 btrfs_delalloc_release_space(inode, data_reserved, block_start,
Qu Wenruo43b18592017-12-12 15:34:32 +08005033 blocksize, true);
Qu Wenruo8702ba92019-10-14 14:34:51 +08005034 btrfs_delalloc_release_extents(BTRFS_I(inode), blocksize);
Chris Mason39279cc2007-06-12 06:35:45 -04005035 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005036 put_page(page);
Chris Mason39279cc2007-06-12 06:35:45 -04005037out:
Qu Wenruo364ecf32017-02-27 15:10:38 +08005038 extent_changeset_free(data_reserved);
Chris Mason39279cc2007-06-12 06:35:45 -04005039 return ret;
5040}
5041
Josef Bacik16e75492013-10-22 12:18:51 -04005042static int maybe_insert_hole(struct btrfs_root *root, struct inode *inode,
5043 u64 offset, u64 len)
5044{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005045 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik16e75492013-10-22 12:18:51 -04005046 struct btrfs_trans_handle *trans;
5047 int ret;
5048
5049 /*
5050 * Still need to make sure the inode looks like it's been updated so
5051 * that any holes get logged if we fsync.
5052 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005053 if (btrfs_fs_incompat(fs_info, NO_HOLES)) {
5054 BTRFS_I(inode)->last_trans = fs_info->generation;
Josef Bacik16e75492013-10-22 12:18:51 -04005055 BTRFS_I(inode)->last_sub_trans = root->log_transid;
5056 BTRFS_I(inode)->last_log_commit = root->last_log_commit;
5057 return 0;
5058 }
5059
5060 /*
5061 * 1 - for the one we're dropping
5062 * 1 - for the one we're adding
5063 * 1 - for updating the inode.
5064 */
5065 trans = btrfs_start_transaction(root, 3);
5066 if (IS_ERR(trans))
5067 return PTR_ERR(trans);
5068
5069 ret = btrfs_drop_extents(trans, root, inode, offset, offset + len, 1);
5070 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04005071 btrfs_abort_transaction(trans, ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005072 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04005073 return ret;
5074 }
5075
David Sterbaf85b7372017-01-20 14:54:07 +01005076 ret = btrfs_insert_file_extent(trans, root, btrfs_ino(BTRFS_I(inode)),
5077 offset, 0, 0, len, 0, len, 0, 0, 0);
Josef Bacik16e75492013-10-22 12:18:51 -04005078 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04005079 btrfs_abort_transaction(trans, ret);
Josef Bacik16e75492013-10-22 12:18:51 -04005080 else
5081 btrfs_update_inode(trans, root, inode);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005082 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04005083 return ret;
5084}
5085
Josef Bacik695a0d02011-03-04 15:46:53 -05005086/*
5087 * This function puts in dummy file extents for the area we're creating a hole
5088 * for. So if we are truncating this file to a larger size we need to insert
5089 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
5090 * the range between oldsize and size
5091 */
Josef Bacika41ad392011-01-31 15:30:16 -05005092int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04005093{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005094 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Yan Zheng9036c102008-10-30 14:19:41 -04005095 struct btrfs_root *root = BTRFS_I(inode)->root;
5096 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04005097 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00005098 struct extent_state *cached_state = NULL;
Josef Bacik5dc562c2012-08-17 13:14:17 -04005099 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005100 u64 hole_start = ALIGN(oldsize, fs_info->sectorsize);
5101 u64 block_end = ALIGN(size, fs_info->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04005102 u64 last_byte;
5103 u64 cur_offset;
5104 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04005105 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04005106
Josef Bacika71754f2013-06-17 17:14:39 -04005107 /*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05305108 * If our size started in the middle of a block we need to zero out the
5109 * rest of the block before we expand the i_size, otherwise we could
Josef Bacika71754f2013-06-17 17:14:39 -04005110 * expose stale data.
5111 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05305112 err = btrfs_truncate_block(inode, oldsize, 0, 0);
Josef Bacika71754f2013-06-17 17:14:39 -04005113 if (err)
5114 return err;
5115
Yan Zheng9036c102008-10-30 14:19:41 -04005116 if (size <= hole_start)
5117 return 0;
5118
Nikolay Borisov23d31bd2019-05-07 10:19:23 +03005119 btrfs_lock_and_flush_ordered_range(io_tree, BTRFS_I(inode), hole_start,
5120 block_end - 1, &cached_state);
Yan Zheng9036c102008-10-30 14:19:41 -04005121 cur_offset = hole_start;
5122 while (1) {
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02005123 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, cur_offset,
Yan Zheng9036c102008-10-30 14:19:41 -04005124 block_end - cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005125 if (IS_ERR(em)) {
5126 err = PTR_ERR(em);
Zach Brownf2767952013-01-08 19:37:58 +00005127 em = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005128 break;
5129 }
Yan Zheng9036c102008-10-30 14:19:41 -04005130 last_byte = min(extent_map_end(em), block_end);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005131 last_byte = ALIGN(last_byte, fs_info->sectorsize);
Yan, Zheng80825102009-11-12 09:35:36 +00005132 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04005133 struct extent_map *hole_em;
Yan Zheng9036c102008-10-30 14:19:41 -04005134 hole_size = last_byte - cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00005135
Josef Bacik16e75492013-10-22 12:18:51 -04005136 err = maybe_insert_hole(root, inode, cur_offset,
5137 hole_size);
5138 if (err)
Chris Mason771ed682008-11-06 22:02:51 -05005139 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02005140 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04005141 cur_offset + hole_size - 1, 0);
5142 hole_em = alloc_extent_map();
5143 if (!hole_em) {
5144 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
5145 &BTRFS_I(inode)->runtime_flags);
5146 goto next;
5147 }
5148 hole_em->start = cur_offset;
5149 hole_em->len = hole_size;
5150 hole_em->orig_start = cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00005151
Josef Bacik5dc562c2012-08-17 13:14:17 -04005152 hole_em->block_start = EXTENT_MAP_HOLE;
5153 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05005154 hole_em->orig_block_len = 0;
Josef Bacikcc95bef2013-04-04 14:31:27 -04005155 hole_em->ram_bytes = hole_size;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005156 hole_em->bdev = fs_info->fs_devices->latest_bdev;
Josef Bacik5dc562c2012-08-17 13:14:17 -04005157 hole_em->compress_type = BTRFS_COMPRESS_NONE;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005158 hole_em->generation = fs_info->generation;
Josef Bacik5dc562c2012-08-17 13:14:17 -04005159
5160 while (1) {
5161 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04005162 err = add_extent_mapping(em_tree, hole_em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04005163 write_unlock(&em_tree->lock);
5164 if (err != -EEXIST)
5165 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02005166 btrfs_drop_extent_cache(BTRFS_I(inode),
5167 cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04005168 cur_offset +
5169 hole_size - 1, 0);
5170 }
5171 free_extent_map(hole_em);
Yan Zheng9036c102008-10-30 14:19:41 -04005172 }
Josef Bacik16e75492013-10-22 12:18:51 -04005173next:
Yan Zheng9036c102008-10-30 14:19:41 -04005174 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04005175 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04005176 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00005177 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04005178 break;
5179 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04005180 free_extent_map(em);
David Sterbae43bbe52017-12-12 21:43:52 +01005181 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state);
Yan Zheng9036c102008-10-30 14:19:41 -04005182 return err;
5183}
5184
Eric Sandeen3972f262013-01-12 02:57:22 +00005185static int btrfs_setsize(struct inode *inode, struct iattr *attr)
Yan, Zheng80825102009-11-12 09:35:36 +00005186{
Miao Xief4a2f4c2011-12-14 20:12:01 -05005187 struct btrfs_root *root = BTRFS_I(inode)->root;
5188 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05005189 loff_t oldsize = i_size_read(inode);
Eric Sandeen3972f262013-01-12 02:57:22 +00005190 loff_t newsize = attr->ia_size;
5191 int mask = attr->ia_valid;
Yan, Zheng80825102009-11-12 09:35:36 +00005192 int ret;
5193
Eric Sandeen3972f262013-01-12 02:57:22 +00005194 /*
5195 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
5196 * special case where we need to update the times despite not having
5197 * these flags set. For all other operations the VFS set these flags
5198 * explicitly if it wants a timestamp update.
5199 */
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08005200 if (newsize != oldsize) {
5201 inode_inc_iversion(inode);
5202 if (!(mask & (ATTR_CTIME | ATTR_MTIME)))
5203 inode->i_ctime = inode->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07005204 current_time(inode);
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08005205 }
Eric Sandeen3972f262013-01-12 02:57:22 +00005206
Josef Bacika41ad392011-01-31 15:30:16 -05005207 if (newsize > oldsize) {
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005208 /*
David Sterbaea14b57f2017-06-22 02:19:11 +02005209 * Don't do an expanding truncate while snapshotting is ongoing.
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005210 * This is to ensure the snapshot captures a fully consistent
5211 * state of this file - if the snapshot captures this expanding
5212 * truncation, it must capture all writes that happened before
5213 * this truncation.
5214 */
Zhao Lei0bc19f902016-01-06 18:56:36 +08005215 btrfs_wait_for_snapshot_creation(root);
Josef Bacika41ad392011-01-31 15:30:16 -05005216 ret = btrfs_cont_expand(inode, oldsize, newsize);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005217 if (ret) {
David Sterbaea14b57f2017-06-22 02:19:11 +02005218 btrfs_end_write_no_snapshotting(root);
Yan, Zheng80825102009-11-12 09:35:36 +00005219 return ret;
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005220 }
Yan, Zheng80825102009-11-12 09:35:36 +00005221
Miao Xief4a2f4c2011-12-14 20:12:01 -05005222 trans = btrfs_start_transaction(root, 1);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005223 if (IS_ERR(trans)) {
David Sterbaea14b57f2017-06-22 02:19:11 +02005224 btrfs_end_write_no_snapshotting(root);
Miao Xief4a2f4c2011-12-14 20:12:01 -05005225 return PTR_ERR(trans);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005226 }
Miao Xief4a2f4c2011-12-14 20:12:01 -05005227
5228 i_size_write(inode, newsize);
5229 btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
Chandan Rajendra27772b62016-01-21 15:56:03 +05305230 pagecache_isize_extended(inode, oldsize, newsize);
Miao Xief4a2f4c2011-12-14 20:12:01 -05005231 ret = btrfs_update_inode(trans, root, inode);
David Sterbaea14b57f2017-06-22 02:19:11 +02005232 btrfs_end_write_no_snapshotting(root);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005233 btrfs_end_transaction(trans);
Josef Bacika41ad392011-01-31 15:30:16 -05005234 } else {
Yan, Zheng80825102009-11-12 09:35:36 +00005235
Josef Bacika41ad392011-01-31 15:30:16 -05005236 /*
5237 * We're truncating a file that used to have good data down to
5238 * zero. Make sure it gets into the ordered flush list so that
5239 * any new writes get down to disk quickly.
5240 */
5241 if (newsize == 0)
Josef Bacik72ac3c02012-05-23 14:13:11 -04005242 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
5243 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00005244
Josef Bacika41ad392011-01-31 15:30:16 -05005245 truncate_setsize(inode, newsize);
Miao Xie2e60a512013-02-08 07:01:08 +00005246
Andrea Gelmini52042d82018-11-28 12:05:13 +01005247 /* Disable nonlocked read DIO to avoid the endless truncate */
Nikolay Borisovabcefb12017-02-20 13:51:10 +02005248 btrfs_inode_block_unlocked_dio(BTRFS_I(inode));
Miao Xie2e60a512013-02-08 07:01:08 +00005249 inode_dio_wait(inode);
Nikolay Borisov0b581702017-02-20 13:51:11 +02005250 btrfs_inode_resume_unlocked_dio(BTRFS_I(inode));
Miao Xie2e60a512013-02-08 07:01:08 +00005251
Filipe Manana213e8c52018-02-06 20:40:31 +00005252 ret = btrfs_truncate(inode, newsize == oldsize);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005253 if (ret && inode->i_nlink) {
5254 int err;
5255
5256 /*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07005257 * Truncate failed, so fix up the in-memory size. We
5258 * adjusted disk_i_size down as we removed extents, so
5259 * wait for disk_i_size to be stable and then update the
5260 * in-memory size to match.
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005261 */
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07005262 err = btrfs_wait_ordered_range(inode, 0, (u64)-1);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005263 if (err)
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07005264 return err;
5265 i_size_write(inode, BTRFS_I(inode)->disk_i_size);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005266 }
Yan, Zheng80825102009-11-12 09:35:36 +00005267 }
5268
Josef Bacika41ad392011-01-31 15:30:16 -05005269 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00005270}
5271
Chris Mason39279cc2007-06-12 06:35:45 -04005272static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
5273{
David Howells2b0143b2015-03-17 22:25:59 +00005274 struct inode *inode = d_inode(dentry);
Li Zefanb83cc962010-12-20 16:04:08 +08005275 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005276 int err;
5277
Li Zefanb83cc962010-12-20 16:04:08 +08005278 if (btrfs_root_readonly(root))
5279 return -EROFS;
5280
Jan Kara31051c82016-05-26 16:55:18 +02005281 err = setattr_prepare(dentry, attr);
Chris Mason39279cc2007-06-12 06:35:45 -04005282 if (err)
5283 return err;
5284
Chris Mason5a3f23d2009-03-31 13:27:11 -04005285 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Eric Sandeen3972f262013-01-12 02:57:22 +00005286 err = btrfs_setsize(inode, attr);
Yan, Zheng80825102009-11-12 09:35:36 +00005287 if (err)
5288 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005289 }
Yan Zheng9036c102008-10-30 14:19:41 -04005290
Christoph Hellwig10257742010-06-04 11:30:02 +02005291 if (attr->ia_valid) {
5292 setattr_copy(inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005293 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005294 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04005295
Josef Bacik22c44fe2011-11-30 10:45:38 -05005296 if (!err && attr->ia_valid & ATTR_MODE)
Christoph Hellwig996a7102013-12-20 05:16:43 -08005297 err = posix_acl_chmod(inode, inode->i_mode);
Christoph Hellwig10257742010-06-04 11:30:02 +02005298 }
5299
Chris Mason39279cc2007-06-12 06:35:45 -04005300 return err;
5301}
Chris Mason61295eb2008-01-14 16:24:38 -05005302
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005303/*
5304 * While truncating the inode pages during eviction, we get the VFS calling
5305 * btrfs_invalidatepage() against each page of the inode. This is slow because
5306 * the calls to btrfs_invalidatepage() result in a huge amount of calls to
5307 * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting
5308 * extent_state structures over and over, wasting lots of time.
5309 *
5310 * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all
5311 * those expensive operations on a per page basis and do only the ordered io
5312 * finishing, while we release here the extent_map and extent_state structures,
5313 * without the excessive merging and splitting.
5314 */
5315static void evict_inode_truncate_pages(struct inode *inode)
5316{
5317 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
5318 struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree;
5319 struct rb_node *node;
5320
5321 ASSERT(inode->i_state & I_FREEING);
Johannes Weiner91b0abe2014-04-03 14:47:49 -07005322 truncate_inode_pages_final(&inode->i_data);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005323
5324 write_lock(&map_tree->lock);
Liu Bo07e1ce02018-08-23 03:51:52 +08005325 while (!RB_EMPTY_ROOT(&map_tree->map.rb_root)) {
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005326 struct extent_map *em;
5327
Liu Bo07e1ce02018-08-23 03:51:52 +08005328 node = rb_first_cached(&map_tree->map);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005329 em = rb_entry(node, struct extent_map, rb_node);
Wang Shilong180589e2013-12-14 15:27:31 +08005330 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
5331 clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005332 remove_extent_mapping(map_tree, em);
5333 free_extent_map(em);
Filipe Manana7064dd52014-08-08 02:47:05 +01005334 if (need_resched()) {
5335 write_unlock(&map_tree->lock);
5336 cond_resched();
5337 write_lock(&map_tree->lock);
5338 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005339 }
5340 write_unlock(&map_tree->lock);
5341
Filipe Manana6ca07092015-05-26 00:55:42 +01005342 /*
5343 * Keep looping until we have no more ranges in the io tree.
5344 * We can have ongoing bios started by readpages (called from readahead)
Filipe Manana9c6429d2015-06-10 12:55:41 +01005345 * that have their endio callback (extent_io.c:end_bio_extent_readpage)
5346 * still in progress (unlocked the pages in the bio but did not yet
5347 * unlocked the ranges in the io tree). Therefore this means some
Filipe Manana6ca07092015-05-26 00:55:42 +01005348 * ranges can still be locked and eviction started because before
5349 * submitting those bios, which are executed by a separate task (work
5350 * queue kthread), inode references (inode->i_count) were not taken
5351 * (which would be dropped in the end io callback of each bio).
5352 * Therefore here we effectively end up waiting for those bios and
5353 * anyone else holding locked ranges without having bumped the inode's
5354 * reference count - if we don't do it, when they access the inode's
5355 * io_tree to unlock a range it may be too late, leading to an
5356 * use-after-free issue.
5357 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005358 spin_lock(&io_tree->lock);
5359 while (!RB_EMPTY_ROOT(&io_tree->state)) {
5360 struct extent_state *state;
5361 struct extent_state *cached_state = NULL;
Filipe Manana6ca07092015-05-26 00:55:42 +01005362 u64 start;
5363 u64 end;
Filipe Manana421f0922018-10-12 13:02:48 +01005364 unsigned state_flags;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005365
5366 node = rb_first(&io_tree->state);
5367 state = rb_entry(node, struct extent_state, rb_node);
Filipe Manana6ca07092015-05-26 00:55:42 +01005368 start = state->start;
5369 end = state->end;
Filipe Manana421f0922018-10-12 13:02:48 +01005370 state_flags = state->state;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005371 spin_unlock(&io_tree->lock);
5372
David Sterbaff13db42015-12-03 14:30:40 +01005373 lock_extent_bits(io_tree, start, end, &cached_state);
Qu Wenruob9d0b382015-09-29 10:35:16 +08005374
5375 /*
5376 * If still has DELALLOC flag, the extent didn't reach disk,
5377 * and its reserved space won't be freed by delayed_ref.
5378 * So we need to free its reserved space here.
5379 * (Refer to comment in btrfs_invalidatepage, case 2)
5380 *
5381 * Note, end is the bytenr of last byte, so we need + 1 here.
5382 */
Filipe Manana421f0922018-10-12 13:02:48 +01005383 if (state_flags & EXTENT_DELALLOC)
Qu Wenruobc42bda2017-02-27 15:10:39 +08005384 btrfs_qgroup_free_data(inode, NULL, start, end - start + 1);
Qu Wenruob9d0b382015-09-29 10:35:16 +08005385
Filipe Manana6ca07092015-05-26 00:55:42 +01005386 clear_extent_bit(io_tree, start, end,
Omar Sandovale1821632019-08-15 14:04:04 -07005387 EXTENT_LOCKED | EXTENT_DELALLOC |
5388 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1,
5389 &cached_state);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005390
Filipe Manana7064dd52014-08-08 02:47:05 +01005391 cond_resched();
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005392 spin_lock(&io_tree->lock);
5393 }
5394 spin_unlock(&io_tree->lock);
5395}
5396
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005397static struct btrfs_trans_handle *evict_refill_and_join(struct btrfs_root *root,
Josef Bacikad80cf52018-09-28 07:18:19 -04005398 struct btrfs_block_rsv *rsv)
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005399{
5400 struct btrfs_fs_info *fs_info = root->fs_info;
5401 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
Josef Bacikd3984c92019-08-01 18:19:37 -04005402 struct btrfs_trans_handle *trans;
Josef Bacik2bd36e72019-08-22 15:14:33 -04005403 u64 delayed_refs_extra = btrfs_calc_insert_metadata_size(fs_info, 1);
Josef Bacikd3984c92019-08-01 18:19:37 -04005404 int ret;
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005405
Josef Bacikd3984c92019-08-01 18:19:37 -04005406 /*
5407 * Eviction should be taking place at some place safe because of our
5408 * delayed iputs. However the normal flushing code will run delayed
5409 * iputs, so we cannot use FLUSH_ALL otherwise we'll deadlock.
5410 *
5411 * We reserve the delayed_refs_extra here again because we can't use
5412 * btrfs_start_transaction(root, 0) for the same deadlocky reason as
5413 * above. We reserve our extra bit here because we generate a ton of
5414 * delayed refs activity by truncating.
5415 *
5416 * If we cannot make our reservation we'll attempt to steal from the
5417 * global reserve, because we really want to be able to free up space.
5418 */
5419 ret = btrfs_block_rsv_refill(root, rsv, rsv->size + delayed_refs_extra,
5420 BTRFS_RESERVE_FLUSH_EVICT);
5421 if (ret) {
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005422 /*
5423 * Try to steal from the global reserve if there is space for
5424 * it.
5425 */
Josef Bacikd3984c92019-08-01 18:19:37 -04005426 if (btrfs_check_space_for_delayed_refs(fs_info) ||
5427 btrfs_block_rsv_migrate(global_rsv, rsv, rsv->size, 0)) {
5428 btrfs_warn(fs_info,
5429 "could not allocate space for delete; will truncate on mount");
5430 return ERR_PTR(-ENOSPC);
5431 }
5432 delayed_refs_extra = 0;
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005433 }
Josef Bacikd3984c92019-08-01 18:19:37 -04005434
5435 trans = btrfs_join_transaction(root);
5436 if (IS_ERR(trans))
5437 return trans;
5438
5439 if (delayed_refs_extra) {
5440 trans->block_rsv = &fs_info->trans_block_rsv;
5441 trans->bytes_reserved = delayed_refs_extra;
5442 btrfs_block_rsv_migrate(rsv, trans->block_rsv,
5443 delayed_refs_extra, 1);
5444 }
5445 return trans;
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005446}
5447
Al Virobd555972010-06-07 11:35:40 -04005448void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005449{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005450 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04005451 struct btrfs_trans_handle *trans;
5452 struct btrfs_root *root = BTRFS_I(inode)->root;
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005453 struct btrfs_block_rsv *rsv;
Chris Mason39279cc2007-06-12 06:35:45 -04005454 int ret;
5455
liubo1abe9b82011-03-24 11:18:59 +00005456 trace_btrfs_inode_evict(inode);
5457
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005458 if (!root) {
Liu Boe8f1bc12018-01-25 11:02:53 -07005459 clear_inode(inode);
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005460 return;
5461 }
5462
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005463 evict_inode_truncate_pages(inode);
5464
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005465 if (inode->i_nlink &&
5466 ((btrfs_root_refs(&root->root_item) != 0 &&
5467 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) ||
Nikolay Borisov70ddc552017-02-20 13:50:35 +02005468 btrfs_is_free_space_inode(BTRFS_I(inode))))
Al Virobd555972010-06-07 11:35:40 -04005469 goto no_delete;
5470
Omar Sandoval27919062018-05-11 13:13:37 -07005471 if (is_bad_inode(inode))
Chris Mason39279cc2007-06-12 06:35:45 -04005472 goto no_delete;
Chris Mason5f39d392007-10-15 16:14:19 -04005473
Nikolay Borisov7ab79562017-02-20 13:50:57 +02005474 btrfs_free_io_failure_record(BTRFS_I(inode), 0, (u64)-1);
Miao Xief6124962014-09-12 18:44:04 +08005475
Omar Sandoval7b40b692018-05-11 13:13:33 -07005476 if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags))
Yan, Zhengc71bf092009-11-12 09:34:40 +00005477 goto no_delete;
Yan, Zhengc71bf092009-11-12 09:34:40 +00005478
Yan, Zheng76dda932009-09-21 16:00:26 -04005479 if (inode->i_nlink > 0) {
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005480 BUG_ON(btrfs_root_refs(&root->root_item) != 0 &&
5481 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID);
Yan, Zheng76dda932009-09-21 16:00:26 -04005482 goto no_delete;
5483 }
5484
Nikolay Borisovaa790212017-01-10 20:35:40 +02005485 ret = btrfs_commit_inode_delayed_inode(BTRFS_I(inode));
Omar Sandoval27919062018-05-11 13:13:37 -07005486 if (ret)
Miao Xie0e8c36a2012-12-19 06:59:51 +00005487 goto no_delete;
Miao Xie0e8c36a2012-12-19 06:59:51 +00005488
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005489 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Omar Sandoval27919062018-05-11 13:13:37 -07005490 if (!rsv)
Josef Bacik4289a662011-08-05 13:22:24 -04005491 goto no_delete;
Josef Bacik2bd36e72019-08-22 15:14:33 -04005492 rsv->size = btrfs_calc_metadata_size(fs_info, 1);
Josef Bacikca7e70f2012-08-27 17:48:15 -04005493 rsv->failfast = 1;
Josef Bacik4289a662011-08-05 13:22:24 -04005494
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02005495 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Mason5f39d392007-10-15 16:14:19 -04005496
Yan, Zheng80825102009-11-12 09:35:36 +00005497 while (1) {
Josef Bacikad80cf52018-09-28 07:18:19 -04005498 trans = evict_refill_and_join(root, rsv);
Omar Sandoval27919062018-05-11 13:13:37 -07005499 if (IS_ERR(trans))
5500 goto free_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04005501
5502 trans->block_rsv = rsv;
5503
Yan, Zhengd68fc572010-05-16 10:49:58 -04005504 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Omar Sandoval27919062018-05-11 13:13:37 -07005505 trans->block_rsv = &fs_info->trans_block_rsv;
5506 btrfs_end_transaction(trans);
5507 btrfs_btree_balance_dirty(fs_info);
5508 if (ret && ret != -ENOSPC && ret != -EAGAIN)
5509 goto free_rsv;
5510 else if (!ret)
Yan, Zheng80825102009-11-12 09:35:36 +00005511 break;
Josef Bacik7b128762008-07-24 12:17:14 -04005512 }
5513
Josef Bacik4ef31a42013-08-13 14:10:08 -04005514 /*
Omar Sandoval27919062018-05-11 13:13:37 -07005515 * Errors here aren't a big deal, it just means we leave orphan items in
5516 * the tree. They will be cleaned up on the next mount. If the inode
5517 * number gets reused, cleanup deletes the orphan item without doing
5518 * anything, and unlink reuses the existing orphan item.
5519 *
5520 * If it turns out that we are dropping too many of these, we might want
5521 * to add a mechanism for retrying these after a commit.
Josef Bacik4ef31a42013-08-13 14:10:08 -04005522 */
Josef Bacikad80cf52018-09-28 07:18:19 -04005523 trans = evict_refill_and_join(root, rsv);
Omar Sandoval27919062018-05-11 13:13:37 -07005524 if (!IS_ERR(trans)) {
5525 trans->block_rsv = rsv;
5526 btrfs_orphan_del(trans, BTRFS_I(inode));
5527 trans->block_rsv = &fs_info->trans_block_rsv;
5528 btrfs_end_transaction(trans);
5529 }
Chris Mason85e21ba2008-01-29 15:11:36 -05005530
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005531 if (!(root == fs_info->tree_root ||
Li Zefan581bb052011-04-20 10:06:11 +08005532 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005533 btrfs_return_ino(root, btrfs_ino(BTRFS_I(inode)));
Li Zefan581bb052011-04-20 10:06:11 +08005534
Omar Sandoval27919062018-05-11 13:13:37 -07005535free_rsv:
5536 btrfs_free_block_rsv(fs_info, rsv);
Chris Mason39279cc2007-06-12 06:35:45 -04005537no_delete:
Omar Sandoval27919062018-05-11 13:13:37 -07005538 /*
5539 * If we didn't successfully delete, the orphan item will still be in
5540 * the tree and we'll retry on the next mount. Again, we might also want
5541 * to retry these periodically in the future.
5542 */
Nikolay Borisovf48d1cf2017-01-10 20:35:39 +02005543 btrfs_remove_delayed_node(BTRFS_I(inode));
Jan Karadbd57682012-05-03 14:48:02 +02005544 clear_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005545}
5546
5547/*
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005548 * Return the key found in the dir entry in the location pointer, fill @type
5549 * with BTRFS_FT_*, and return 0.
5550 *
Su Yue005d6712018-03-05 17:13:37 +08005551 * If no dir entries were found, returns -ENOENT.
5552 * If found a corrupted location in dir entry, returns -EUCLEAN.
Chris Mason39279cc2007-06-12 06:35:45 -04005553 */
5554static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005555 struct btrfs_key *location, u8 *type)
Chris Mason39279cc2007-06-12 06:35:45 -04005556{
5557 const char *name = dentry->d_name.name;
5558 int namelen = dentry->d_name.len;
5559 struct btrfs_dir_item *di;
5560 struct btrfs_path *path;
5561 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04005562 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005563
5564 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005565 if (!path)
5566 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05005567
David Sterbaf85b7372017-01-20 14:54:07 +01005568 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(BTRFS_I(dir)),
5569 name, namelen, 0);
Liu Bo3cf50682018-09-12 06:06:26 +08005570 if (IS_ERR_OR_NULL(di)) {
5571 ret = di ? PTR_ERR(di) : -ENOENT;
Su Yue005d6712018-03-05 17:13:37 +08005572 goto out;
5573 }
Chris Masond3977122009-01-05 21:25:51 -05005574
Chris Mason5f39d392007-10-15 16:14:19 -04005575 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Liu Bo56a0e702017-10-30 11:14:38 -06005576 if (location->type != BTRFS_INODE_ITEM_KEY &&
5577 location->type != BTRFS_ROOT_ITEM_KEY) {
Su Yue005d6712018-03-05 17:13:37 +08005578 ret = -EUCLEAN;
Liu Bo56a0e702017-10-30 11:14:38 -06005579 btrfs_warn(root->fs_info,
5580"%s gets something invalid in DIR_ITEM (name %s, directory ino %llu, location(%llu %u %llu))",
5581 __func__, name, btrfs_ino(BTRFS_I(dir)),
5582 location->objectid, location->type, location->offset);
Liu Bo56a0e702017-10-30 11:14:38 -06005583 }
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005584 if (!ret)
5585 *type = btrfs_dir_type(path->nodes[0], di);
Chris Mason39279cc2007-06-12 06:35:45 -04005586out:
Chris Mason39279cc2007-06-12 06:35:45 -04005587 btrfs_free_path(path);
5588 return ret;
5589}
5590
5591/*
5592 * when we hit a tree root in a directory, the btrfs part of the inode
5593 * needs to be changed to reflect the root directory of the tree root. This
5594 * is kind of like crossing a mount point.
5595 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005596static int fixup_tree_root_location(struct btrfs_fs_info *fs_info,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005597 struct inode *dir,
5598 struct dentry *dentry,
5599 struct btrfs_key *location,
5600 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04005601{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005602 struct btrfs_path *path;
5603 struct btrfs_root *new_root;
5604 struct btrfs_root_ref *ref;
5605 struct extent_buffer *leaf;
David Sterba1d4c08e2015-01-02 19:36:14 +01005606 struct btrfs_key key;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005607 int ret;
5608 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005609
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005610 path = btrfs_alloc_path();
5611 if (!path) {
5612 err = -ENOMEM;
5613 goto out;
5614 }
Chris Mason39279cc2007-06-12 06:35:45 -04005615
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005616 err = -ENOENT;
David Sterba1d4c08e2015-01-02 19:36:14 +01005617 key.objectid = BTRFS_I(dir)->root->root_key.objectid;
5618 key.type = BTRFS_ROOT_REF_KEY;
5619 key.offset = location->objectid;
5620
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005621 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005622 if (ret) {
5623 if (ret < 0)
5624 err = ret;
5625 goto out;
5626 }
Chris Mason39279cc2007-06-12 06:35:45 -04005627
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005628 leaf = path->nodes[0];
5629 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005630 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(BTRFS_I(dir)) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005631 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
5632 goto out;
5633
5634 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
5635 (unsigned long)(ref + 1),
5636 dentry->d_name.len);
5637 if (ret)
5638 goto out;
5639
David Sterbab3b4aa72011-04-21 01:20:15 +02005640 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005641
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005642 new_root = btrfs_read_fs_root_no_name(fs_info, location);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005643 if (IS_ERR(new_root)) {
5644 err = PTR_ERR(new_root);
5645 goto out;
5646 }
5647
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005648 *sub_root = new_root;
5649 location->objectid = btrfs_root_dirid(&new_root->root_item);
5650 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04005651 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005652 err = 0;
5653out:
5654 btrfs_free_path(path);
5655 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005656}
5657
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005658static void inode_tree_add(struct inode *inode)
5659{
5660 struct btrfs_root *root = BTRFS_I(inode)->root;
5661 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005662 struct rb_node **p;
5663 struct rb_node *parent;
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005664 struct rb_node *new = &BTRFS_I(inode)->rb_node;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005665 u64 ino = btrfs_ino(BTRFS_I(inode));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005666
Al Viro1d3382cb2010-10-23 15:19:20 -04005667 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005668 return;
Miao Xiee1409ce2013-05-15 07:48:16 +00005669 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005670 spin_lock(&root->inode_lock);
Miao Xiee1409ce2013-05-15 07:48:16 +00005671 p = &root->inode_tree.rb_node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005672 while (*p) {
5673 parent = *p;
5674 entry = rb_entry(parent, struct btrfs_inode, rb_node);
5675
David Sterba37508512018-06-29 10:56:40 +02005676 if (ino < btrfs_ino(entry))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005677 p = &parent->rb_left;
David Sterba37508512018-06-29 10:56:40 +02005678 else if (ino > btrfs_ino(entry))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005679 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005680 else {
5681 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04005682 (I_WILL_FREE | I_FREEING)));
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005683 rb_replace_node(parent, new, &root->inode_tree);
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005684 RB_CLEAR_NODE(parent);
5685 spin_unlock(&root->inode_lock);
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005686 return;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005687 }
5688 }
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005689 rb_link_node(new, parent, p);
5690 rb_insert_color(new, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005691 spin_unlock(&root->inode_lock);
5692}
5693
5694static void inode_tree_del(struct inode *inode)
5695{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005696 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005697 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04005698 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005699
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005700 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005701 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005702 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005703 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04005704 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005705 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005706 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04005707
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005708 if (empty && btrfs_root_refs(&root->root_item) == 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005709 synchronize_srcu(&fs_info->subvol_srcu);
Yan, Zheng76dda932009-09-21 16:00:26 -04005710 spin_lock(&root->inode_lock);
5711 empty = RB_EMPTY_ROOT(&root->inode_tree);
5712 spin_unlock(&root->inode_lock);
5713 if (empty)
5714 btrfs_add_dead_root(root);
5715 }
5716}
5717
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005718
Chris Masone02119d2008-09-05 16:13:11 -04005719static int btrfs_init_locked_inode(struct inode *inode, void *p)
5720{
5721 struct btrfs_iget_args *args = p;
Chris Mason90d3e592014-01-09 17:28:00 -08005722 inode->i_ino = args->location->objectid;
5723 memcpy(&BTRFS_I(inode)->location, args->location,
5724 sizeof(*args->location));
Chris Masone02119d2008-09-05 16:13:11 -04005725 BTRFS_I(inode)->root = args->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005726 return 0;
5727}
5728
5729static int btrfs_find_actor(struct inode *inode, void *opaque)
5730{
5731 struct btrfs_iget_args *args = opaque;
Chris Mason90d3e592014-01-09 17:28:00 -08005732 return args->location->objectid == BTRFS_I(inode)->location.objectid &&
Chris Masond3977122009-01-05 21:25:51 -05005733 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005734}
5735
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005736static struct inode *btrfs_iget_locked(struct super_block *s,
Chris Mason90d3e592014-01-09 17:28:00 -08005737 struct btrfs_key *location,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005738 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04005739{
5740 struct inode *inode;
5741 struct btrfs_iget_args args;
Chris Mason90d3e592014-01-09 17:28:00 -08005742 unsigned long hashval = btrfs_inode_hash(location->objectid, root);
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005743
Chris Mason90d3e592014-01-09 17:28:00 -08005744 args.location = location;
Chris Mason39279cc2007-06-12 06:35:45 -04005745 args.root = root;
5746
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005747 inode = iget5_locked(s, hashval, btrfs_find_actor,
Chris Mason39279cc2007-06-12 06:35:45 -04005748 btrfs_init_locked_inode,
5749 (void *)&args);
5750 return inode;
5751}
5752
Balaji Rao1a54ef82008-07-21 02:01:04 +05305753/* Get an inode object given its location and corresponding root.
5754 * Returns in *is_new if the inode was read from disk
5755 */
Filipe Manana4222ea72018-10-24 10:13:03 +01005756struct inode *btrfs_iget_path(struct super_block *s, struct btrfs_key *location,
5757 struct btrfs_root *root, int *new,
5758 struct btrfs_path *path)
Balaji Rao1a54ef82008-07-21 02:01:04 +05305759{
5760 struct inode *inode;
5761
Chris Mason90d3e592014-01-09 17:28:00 -08005762 inode = btrfs_iget_locked(s, location, root);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305763 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005764 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305765
5766 if (inode->i_state & I_NEW) {
Filipe Manana67710892016-06-06 11:51:25 +01005767 int ret;
5768
Filipe Manana4222ea72018-10-24 10:13:03 +01005769 ret = btrfs_read_locked_inode(inode, path);
Al Viro9bc2cef2018-07-29 23:04:50 +01005770 if (!ret) {
Mark Fasheh1748f842011-07-12 11:25:31 -07005771 inode_tree_add(inode);
5772 unlock_new_inode(inode);
5773 if (new)
5774 *new = 1;
5775 } else {
Al Virof5b3a412018-07-29 23:04:51 +01005776 iget_failed(inode);
5777 /*
5778 * ret > 0 can come from btrfs_search_slot called by
5779 * btrfs_read_locked_inode, this means the inode item
5780 * was not found.
5781 */
5782 if (ret > 0)
5783 ret = -ENOENT;
5784 inode = ERR_PTR(ret);
Mark Fasheh1748f842011-07-12 11:25:31 -07005785 }
5786 }
5787
Balaji Rao1a54ef82008-07-21 02:01:04 +05305788 return inode;
5789}
5790
Filipe Manana4222ea72018-10-24 10:13:03 +01005791struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
5792 struct btrfs_root *root, int *new)
5793{
5794 return btrfs_iget_path(s, location, root, new, NULL);
5795}
5796
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005797static struct inode *new_simple_dir(struct super_block *s,
5798 struct btrfs_key *key,
5799 struct btrfs_root *root)
5800{
5801 struct inode *inode = new_inode(s);
5802
5803 if (!inode)
5804 return ERR_PTR(-ENOMEM);
5805
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005806 BTRFS_I(inode)->root = root;
5807 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04005808 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005809
5810 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Li Zefan848cce02012-02-21 17:04:28 +08005811 inode->i_op = &btrfs_dir_ro_inode_operations;
Omar Sandoval1fdf4192017-01-25 17:06:39 -08005812 inode->i_opflags &= ~IOP_XATTR;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005813 inode->i_fop = &simple_dir_operations;
5814 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
Deepa Dinamanic2050a42016-09-14 07:48:06 -07005815 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05305816 inode->i_atime = inode->i_mtime;
5817 inode->i_ctime = inode->i_mtime;
Arnd Bergmannd3c6be6f2018-06-21 18:04:06 +02005818 BTRFS_I(inode)->i_otime = inode->i_mtime;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005819
5820 return inode;
5821}
5822
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005823static inline u8 btrfs_inode_type(struct inode *inode)
5824{
5825 /*
5826 * Compile-time asserts that generic FT_* types still match
5827 * BTRFS_FT_* types
5828 */
5829 BUILD_BUG_ON(BTRFS_FT_UNKNOWN != FT_UNKNOWN);
5830 BUILD_BUG_ON(BTRFS_FT_REG_FILE != FT_REG_FILE);
5831 BUILD_BUG_ON(BTRFS_FT_DIR != FT_DIR);
5832 BUILD_BUG_ON(BTRFS_FT_CHRDEV != FT_CHRDEV);
5833 BUILD_BUG_ON(BTRFS_FT_BLKDEV != FT_BLKDEV);
5834 BUILD_BUG_ON(BTRFS_FT_FIFO != FT_FIFO);
5835 BUILD_BUG_ON(BTRFS_FT_SOCK != FT_SOCK);
5836 BUILD_BUG_ON(BTRFS_FT_SYMLINK != FT_SYMLINK);
5837
5838 return fs_umode_to_ftype(inode->i_mode);
5839}
5840
Chris Mason3de45862008-11-17 21:02:50 -05005841struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04005842{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005843 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masond3977122009-01-05 21:25:51 -05005844 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005845 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005846 struct btrfs_root *sub_root = root;
5847 struct btrfs_key location;
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005848 u8 di_type = 0;
Yan, Zheng76dda932009-09-21 16:00:26 -04005849 int index;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005850 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005851
5852 if (dentry->d_name.len > BTRFS_NAME_LEN)
5853 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04005854
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005855 ret = btrfs_inode_by_name(dir, dentry, &location, &di_type);
Chris Mason39279cc2007-06-12 06:35:45 -04005856 if (ret < 0)
5857 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04005858
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005859 if (location.type == BTRFS_INODE_ITEM_KEY) {
Josef Bacik73f73412009-12-04 17:38:27 +00005860 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005861 if (IS_ERR(inode))
5862 return inode;
5863
5864 /* Do extra check against inode mode with di_type */
5865 if (btrfs_inode_type(inode) != di_type) {
5866 btrfs_crit(fs_info,
5867"inode mode mismatch with dir: inode mode=0%o btrfs type=%u dir type=%u",
5868 inode->i_mode, btrfs_inode_type(inode),
5869 di_type);
5870 iput(inode);
5871 return ERR_PTR(-EUCLEAN);
5872 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005873 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04005874 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005875
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005876 index = srcu_read_lock(&fs_info->subvol_srcu);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005877 ret = fixup_tree_root_location(fs_info, dir, dentry,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005878 &location, &sub_root);
5879 if (ret < 0) {
5880 if (ret != -ENOENT)
5881 inode = ERR_PTR(ret);
5882 else
5883 inode = new_simple_dir(dir->i_sb, &location, sub_root);
5884 } else {
Josef Bacik73f73412009-12-04 17:38:27 +00005885 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04005886 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005887 srcu_read_unlock(&fs_info->subvol_srcu, index);
Yan, Zheng76dda932009-09-21 16:00:26 -04005888
Julia Lawall34d19ba2011-01-24 19:55:19 +00005889 if (!IS_ERR(inode) && root != sub_root) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005890 down_read(&fs_info->cleanup_work_sem);
David Howellsbc98a422017-07-17 08:45:34 +01005891 if (!sb_rdonly(inode->i_sb))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005892 ret = btrfs_orphan_cleanup(sub_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005893 up_read(&fs_info->cleanup_work_sem);
Josef Bacik01cd3362013-06-03 21:39:49 -04005894 if (ret) {
5895 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005896 inode = ERR_PTR(ret);
Josef Bacik01cd3362013-06-03 21:39:49 -04005897 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00005898 }
5899
Chris Mason3de45862008-11-17 21:02:50 -05005900 return inode;
5901}
5902
Nick Pigginfe15ce42011-01-07 17:49:23 +11005903static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04005904{
5905 struct btrfs_root *root;
David Howells2b0143b2015-03-17 22:25:59 +00005906 struct inode *inode = d_inode(dentry);
Yan, Zheng76dda932009-09-21 16:00:26 -04005907
Li Zefan848cce02012-02-21 17:04:28 +08005908 if (!inode && !IS_ROOT(dentry))
David Howells2b0143b2015-03-17 22:25:59 +00005909 inode = d_inode(dentry->d_parent);
Yan, Zheng76dda932009-09-21 16:00:26 -04005910
Li Zefan848cce02012-02-21 17:04:28 +08005911 if (inode) {
5912 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04005913 if (btrfs_root_refs(&root->root_item) == 0)
5914 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08005915
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005916 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Li Zefan848cce02012-02-21 17:04:28 +08005917 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04005918 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005919 return 0;
5920}
5921
Chris Mason3de45862008-11-17 21:02:50 -05005922static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04005923 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05005924{
Al Viro3837d202018-10-10 16:38:27 -04005925 struct inode *inode = btrfs_lookup_dentry(dir, dentry);
Josef Bacika66e7cc2011-09-18 10:34:03 -04005926
Al Viro3837d202018-10-10 16:38:27 -04005927 if (inode == ERR_PTR(-ENOENT))
5928 inode = NULL;
Al Viro41d28bc2014-10-12 22:24:21 -04005929 return d_splice_alias(inode, dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04005930}
5931
Josef Bacik23b5ec72017-07-24 15:14:25 -04005932/*
5933 * All this infrastructure exists because dir_emit can fault, and we are holding
5934 * the tree lock when doing readdir. For now just allocate a buffer and copy
5935 * our information into that, and then dir_emit from the buffer. This is
5936 * similar to what NFS does, only we don't keep the buffer around in pagecache
5937 * because I'm afraid I'll mess that up. Long term we need to make filldir do
5938 * copy_to_user_inatomic so we don't have to worry about page faulting under the
5939 * tree lock.
5940 */
5941static int btrfs_opendir(struct inode *inode, struct file *file)
5942{
5943 struct btrfs_file_private *private;
5944
5945 private = kzalloc(sizeof(struct btrfs_file_private), GFP_KERNEL);
5946 if (!private)
5947 return -ENOMEM;
5948 private->filldir_buf = kzalloc(PAGE_SIZE, GFP_KERNEL);
5949 if (!private->filldir_buf) {
5950 kfree(private);
5951 return -ENOMEM;
5952 }
5953 file->private_data = private;
5954 return 0;
5955}
5956
5957struct dir_entry {
5958 u64 ino;
5959 u64 offset;
5960 unsigned type;
5961 int name_len;
5962};
5963
5964static int btrfs_filldir(void *addr, int entries, struct dir_context *ctx)
5965{
5966 while (entries--) {
5967 struct dir_entry *entry = addr;
5968 char *name = (char *)(entry + 1);
5969
David Sterba92d32172018-04-16 21:10:14 +02005970 ctx->pos = get_unaligned(&entry->offset);
5971 if (!dir_emit(ctx, name, get_unaligned(&entry->name_len),
5972 get_unaligned(&entry->ino),
5973 get_unaligned(&entry->type)))
Josef Bacik23b5ec72017-07-24 15:14:25 -04005974 return 1;
David Sterba92d32172018-04-16 21:10:14 +02005975 addr += sizeof(struct dir_entry) +
5976 get_unaligned(&entry->name_len);
Josef Bacik23b5ec72017-07-24 15:14:25 -04005977 ctx->pos++;
5978 }
5979 return 0;
5980}
5981
Al Viro9cdda8d2013-05-22 16:48:09 -04005982static int btrfs_real_readdir(struct file *file, struct dir_context *ctx)
Chris Mason39279cc2007-06-12 06:35:45 -04005983{
Al Viro9cdda8d2013-05-22 16:48:09 -04005984 struct inode *inode = file_inode(file);
Chris Mason39279cc2007-06-12 06:35:45 -04005985 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005986 struct btrfs_file_private *private = file->private_data;
Chris Mason39279cc2007-06-12 06:35:45 -04005987 struct btrfs_dir_item *di;
5988 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04005989 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04005990 struct btrfs_path *path;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005991 void *addr;
Miao Xie16cdcec2011-04-22 18:12:22 +08005992 struct list_head ins_list;
5993 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04005994 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04005995 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04005996 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04005997 char *name_ptr;
5998 int name_len;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005999 int entries = 0;
6000 int total_len = 0;
Omar Sandoval02dbfc92016-05-20 13:50:33 -07006001 bool put = false;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006002 struct btrfs_key location;
Chris Mason5f39d392007-10-15 16:14:19 -04006003
Al Viro9cdda8d2013-05-22 16:48:09 -04006004 if (!dir_emit_dots(file, ctx))
6005 return 0;
6006
David Woodhouse49593bf2008-08-17 17:08:36 +01006007 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08006008 if (!path)
6009 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04006010
Josef Bacik23b5ec72017-07-24 15:14:25 -04006011 addr = private->filldir_buf;
David Sterbae4058b52015-11-27 16:31:35 +01006012 path->reada = READA_FORWARD;
David Woodhouse49593bf2008-08-17 17:08:36 +01006013
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006014 INIT_LIST_HEAD(&ins_list);
6015 INIT_LIST_HEAD(&del_list);
6016 put = btrfs_readdir_get_delayed_items(inode, &ins_list, &del_list);
Miao Xie16cdcec2011-04-22 18:12:22 +08006017
Josef Bacik23b5ec72017-07-24 15:14:25 -04006018again:
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006019 key.type = BTRFS_DIR_INDEX_KEY;
Al Viro9cdda8d2013-05-22 16:48:09 -04006020 key.offset = ctx->pos;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02006021 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason5f39d392007-10-15 16:14:19 -04006022
Chris Mason39279cc2007-06-12 06:35:45 -04006023 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
6024 if (ret < 0)
6025 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01006026
6027 while (1) {
Josef Bacik23b5ec72017-07-24 15:14:25 -04006028 struct dir_entry *entry;
6029
Chris Mason5f39d392007-10-15 16:14:19 -04006030 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04006031 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08006032 if (slot >= btrfs_header_nritems(leaf)) {
6033 ret = btrfs_next_leaf(root, path);
6034 if (ret < 0)
6035 goto err;
6036 else if (ret > 0)
6037 break;
6038 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04006039 }
Chris Mason3de45862008-11-17 21:02:50 -05006040
Chris Mason5f39d392007-10-15 16:14:19 -04006041 btrfs_item_key_to_cpu(leaf, &found_key, slot);
6042
6043 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04006044 break;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006045 if (found_key.type != BTRFS_DIR_INDEX_KEY)
Chris Mason39279cc2007-06-12 06:35:45 -04006046 break;
Al Viro9cdda8d2013-05-22 16:48:09 -04006047 if (found_key.offset < ctx->pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08006048 goto next;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006049 if (btrfs_should_delete_dir_index(&del_list, found_key.offset))
Miao Xie16cdcec2011-04-22 18:12:22 +08006050 goto next;
Chris Mason39279cc2007-06-12 06:35:45 -04006051 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006052 name_len = btrfs_dir_name_len(leaf, di);
Josef Bacik23b5ec72017-07-24 15:14:25 -04006053 if ((total_len + sizeof(struct dir_entry) + name_len) >=
6054 PAGE_SIZE) {
6055 btrfs_release_path(path);
6056 ret = btrfs_filldir(private->filldir_buf, entries, ctx);
6057 if (ret)
6058 goto nopos;
6059 addr = private->filldir_buf;
6060 entries = 0;
6061 total_len = 0;
6062 goto again;
Chris Mason39279cc2007-06-12 06:35:45 -04006063 }
Josef Bacik23b5ec72017-07-24 15:14:25 -04006064
6065 entry = addr;
David Sterba92d32172018-04-16 21:10:14 +02006066 put_unaligned(name_len, &entry->name_len);
Josef Bacik23b5ec72017-07-24 15:14:25 -04006067 name_ptr = (char *)(entry + 1);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006068 read_extent_buffer(leaf, name_ptr, (unsigned long)(di + 1),
6069 name_len);
Phillip Potter7d157c32019-03-26 21:39:34 +00006070 put_unaligned(fs_ftype_to_dtype(btrfs_dir_type(leaf, di)),
David Sterba92d32172018-04-16 21:10:14 +02006071 &entry->type);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006072 btrfs_dir_item_key_to_cpu(leaf, di, &location);
David Sterba92d32172018-04-16 21:10:14 +02006073 put_unaligned(location.objectid, &entry->ino);
6074 put_unaligned(found_key.offset, &entry->offset);
Josef Bacik23b5ec72017-07-24 15:14:25 -04006075 entries++;
6076 addr += sizeof(struct dir_entry) + name_len;
6077 total_len += sizeof(struct dir_entry) + name_len;
Li Zefanb9e03af2011-03-23 10:43:58 +08006078next:
6079 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04006080 }
Josef Bacik23b5ec72017-07-24 15:14:25 -04006081 btrfs_release_path(path);
6082
6083 ret = btrfs_filldir(private->filldir_buf, entries, ctx);
6084 if (ret)
6085 goto nopos;
David Woodhouse49593bf2008-08-17 17:08:36 +01006086
Jeff Mahoneyd2fbb2b2016-11-05 13:26:35 -04006087 ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006088 if (ret)
David Sterbabc4ef752015-11-13 13:44:28 +01006089 goto nopos;
6090
Zach Browndb62efb2013-07-11 16:19:42 -07006091 /*
6092 * Stop new entries from being returned after we return the last
6093 * entry.
6094 *
6095 * New directory entries are assigned a strictly increasing
6096 * offset. This means that new entries created during readdir
6097 * are *guaranteed* to be seen in the future by that readdir.
6098 * This has broken buggy programs which operate on names as
6099 * they're returned by readdir. Until we re-use freed offsets
6100 * we have this hack to stop new entries from being returned
6101 * under the assumption that they'll never reach this huge
6102 * offset.
6103 *
6104 * This is being careful not to overflow 32bit loff_t unless the
6105 * last entry requires it because doing so has broken 32bit apps
6106 * in the past.
6107 */
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006108 if (ctx->pos >= INT_MAX)
6109 ctx->pos = LLONG_MAX;
6110 else
6111 ctx->pos = INT_MAX;
Chris Mason39279cc2007-06-12 06:35:45 -04006112nopos:
6113 ret = 0;
6114err:
Omar Sandoval02dbfc92016-05-20 13:50:33 -07006115 if (put)
6116 btrfs_readdir_put_delayed_items(inode, &ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04006117 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04006118 return ret;
6119}
6120
Chris Mason39279cc2007-06-12 06:35:45 -04006121/*
Chris Mason54aa1f42007-06-22 14:16:25 -04006122 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04006123 * inode changes. But, it is most likely to find the inode in cache.
6124 * FIXME, needs more benchmarking...there are no reasons other than performance
6125 * to keep or drop this code.
6126 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00006127static int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04006128{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006129 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04006130 struct btrfs_root *root = BTRFS_I(inode)->root;
6131 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006132 int ret;
6133
Josef Bacik72ac3c02012-05-23 14:13:11 -04006134 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05006135 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006136
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006137 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006138 if (IS_ERR(trans))
6139 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006140
6141 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04006142 if (ret && ret == -ENOSPC) {
6143 /* whoops, lets try again with the full transaction */
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006144 btrfs_end_transaction(trans);
Chris Mason94b60442010-05-26 11:02:00 -04006145 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006146 if (IS_ERR(trans))
6147 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006148
Chris Mason94b60442010-05-26 11:02:00 -04006149 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04006150 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006151 btrfs_end_transaction(trans);
Miao Xie16cdcec2011-04-22 18:12:22 +08006152 if (BTRFS_I(inode)->delayed_node)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006153 btrfs_balance_delayed_items(fs_info);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006154
6155 return ret;
6156}
6157
6158/*
6159 * This is a copy of file_update_time. We need this so we can return error on
6160 * ENOSPC for updating the inode in the case of file write and mmap writes.
6161 */
Deepa Dinamani95582b02018-05-08 19:36:02 -07006162static int btrfs_update_time(struct inode *inode, struct timespec64 *now,
Josef Bacike41f9412012-03-26 09:46:47 -04006163 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05006164{
Alexander Block2bc5565282012-06-15 09:49:33 +02006165 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Layton3a8c7232017-12-11 06:35:24 -05006166 bool dirty = flags & ~S_VERSION;
Alexander Block2bc5565282012-06-15 09:49:33 +02006167
6168 if (btrfs_root_readonly(root))
6169 return -EROFS;
6170
Josef Bacike41f9412012-03-26 09:46:47 -04006171 if (flags & S_VERSION)
Jeff Layton3a8c7232017-12-11 06:35:24 -05006172 dirty |= inode_maybe_inc_iversion(inode, dirty);
Josef Bacike41f9412012-03-26 09:46:47 -04006173 if (flags & S_CTIME)
6174 inode->i_ctime = *now;
6175 if (flags & S_MTIME)
6176 inode->i_mtime = *now;
6177 if (flags & S_ATIME)
6178 inode->i_atime = *now;
Jeff Layton3a8c7232017-12-11 06:35:24 -05006179 return dirty ? btrfs_dirty_inode(inode) : 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006180}
6181
Chris Masond352ac62008-09-29 15:18:18 -04006182/*
6183 * find the highest existing sequence number in a directory
6184 * and then set the in-memory index_cnt variable to reflect
6185 * free sequence numbers
6186 */
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006187static int btrfs_set_inode_index_count(struct btrfs_inode *inode)
Josef Bacikaec74772008-07-24 12:12:38 -04006188{
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006189 struct btrfs_root *root = inode->root;
Josef Bacikaec74772008-07-24 12:12:38 -04006190 struct btrfs_key key, found_key;
6191 struct btrfs_path *path;
6192 struct extent_buffer *leaf;
6193 int ret;
6194
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006195 key.objectid = btrfs_ino(inode);
David Sterba962a2982014-06-04 18:41:45 +02006196 key.type = BTRFS_DIR_INDEX_KEY;
Josef Bacikaec74772008-07-24 12:12:38 -04006197 key.offset = (u64)-1;
6198
6199 path = btrfs_alloc_path();
6200 if (!path)
6201 return -ENOMEM;
6202
6203 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
6204 if (ret < 0)
6205 goto out;
6206 /* FIXME: we should be able to handle this */
6207 if (ret == 0)
6208 goto out;
6209 ret = 0;
6210
6211 /*
6212 * MAGIC NUMBER EXPLANATION:
6213 * since we search a directory based on f_pos we have to start at 2
6214 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
6215 * else has to start at 2
6216 */
6217 if (path->slots[0] == 0) {
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006218 inode->index_cnt = 2;
Josef Bacikaec74772008-07-24 12:12:38 -04006219 goto out;
6220 }
6221
6222 path->slots[0]--;
6223
6224 leaf = path->nodes[0];
6225 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6226
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006227 if (found_key.objectid != btrfs_ino(inode) ||
David Sterba962a2982014-06-04 18:41:45 +02006228 found_key.type != BTRFS_DIR_INDEX_KEY) {
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006229 inode->index_cnt = 2;
Josef Bacikaec74772008-07-24 12:12:38 -04006230 goto out;
6231 }
6232
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006233 inode->index_cnt = found_key.offset + 1;
Josef Bacikaec74772008-07-24 12:12:38 -04006234out:
6235 btrfs_free_path(path);
6236 return ret;
6237}
6238
Chris Masond352ac62008-09-29 15:18:18 -04006239/*
6240 * helper to find a free sequence number in a given directory. This current
6241 * code is very simple, later versions will do smarter things in the btree
6242 */
Nikolay Borisov877574e2017-02-20 13:50:33 +02006243int btrfs_set_inode_index(struct btrfs_inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04006244{
6245 int ret = 0;
6246
Nikolay Borisov877574e2017-02-20 13:50:33 +02006247 if (dir->index_cnt == (u64)-1) {
6248 ret = btrfs_inode_delayed_dir_index_count(dir);
Miao Xie16cdcec2011-04-22 18:12:22 +08006249 if (ret) {
6250 ret = btrfs_set_inode_index_count(dir);
6251 if (ret)
6252 return ret;
6253 }
Josef Bacikaec74772008-07-24 12:12:38 -04006254 }
6255
Nikolay Borisov877574e2017-02-20 13:50:33 +02006256 *index = dir->index_cnt;
6257 dir->index_cnt++;
Josef Bacikaec74772008-07-24 12:12:38 -04006258
6259 return ret;
6260}
6261
Chris Masonb0d5d102014-09-08 13:08:51 -07006262static int btrfs_insert_inode_locked(struct inode *inode)
6263{
6264 struct btrfs_iget_args args;
6265 args.location = &BTRFS_I(inode)->location;
6266 args.root = BTRFS_I(inode)->root;
6267
6268 return insert_inode_locked4(inode,
6269 btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root),
6270 btrfs_find_actor, &args);
6271}
6272
Anand Jain19aee8d2017-07-18 17:37:05 +08006273/*
6274 * Inherit flags from the parent inode.
6275 *
6276 * Currently only the compression flags and the cow flags are inherited.
6277 */
6278static void btrfs_inherit_iflags(struct inode *inode, struct inode *dir)
6279{
6280 unsigned int flags;
6281
6282 if (!dir)
6283 return;
6284
6285 flags = BTRFS_I(dir)->flags;
6286
6287 if (flags & BTRFS_INODE_NOCOMPRESS) {
6288 BTRFS_I(inode)->flags &= ~BTRFS_INODE_COMPRESS;
6289 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
6290 } else if (flags & BTRFS_INODE_COMPRESS) {
6291 BTRFS_I(inode)->flags &= ~BTRFS_INODE_NOCOMPRESS;
6292 BTRFS_I(inode)->flags |= BTRFS_INODE_COMPRESS;
6293 }
6294
6295 if (flags & BTRFS_INODE_NODATACOW) {
6296 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
6297 if (S_ISREG(inode->i_mode))
6298 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
6299 }
6300
David Sterba7b6a2212018-03-26 18:40:21 +02006301 btrfs_sync_inode_flags_to_i_flags(inode);
Anand Jain19aee8d2017-07-18 17:37:05 +08006302}
6303
Chris Mason39279cc2007-06-12 06:35:45 -04006304static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
6305 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04006306 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05006307 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04006308 u64 ref_objectid, u64 objectid,
6309 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04006310{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006311 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04006312 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006313 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04006314 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04006315 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05006316 struct btrfs_inode_ref *ref;
6317 struct btrfs_key key[2];
6318 u32 sizes[2];
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006319 int nitems = name ? 2 : 1;
Chris Mason9c583092008-01-29 15:15:18 -05006320 unsigned long ptr;
Josef Bacik11a19a92019-09-09 10:12:04 -04006321 unsigned int nofs_flag;
Chris Mason39279cc2007-06-12 06:35:45 -04006322 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006323
Chris Mason5f39d392007-10-15 16:14:19 -04006324 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07006325 if (!path)
6326 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04006327
Josef Bacik11a19a92019-09-09 10:12:04 -04006328 nofs_flag = memalloc_nofs_save();
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006329 inode = new_inode(fs_info->sb);
Josef Bacik11a19a92019-09-09 10:12:04 -04006330 memalloc_nofs_restore(nofs_flag);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006331 if (!inode) {
6332 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04006333 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006334 }
Chris Mason39279cc2007-06-12 06:35:45 -04006335
Li Zefan581bb052011-04-20 10:06:11 +08006336 /*
Filipe Manana5762b5c2014-08-01 00:10:32 +01006337 * O_TMPFILE, set link count to 0, so that after this point,
6338 * we fill in an inode item with the correct link count.
6339 */
6340 if (!name)
6341 set_nlink(inode, 0);
6342
6343 /*
Li Zefan581bb052011-04-20 10:06:11 +08006344 * we have to initialize this early, so we can reclaim the inode
6345 * number if we fail afterwards in this function.
6346 */
6347 inode->i_ino = objectid;
6348
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006349 if (dir && name) {
liubo1abe9b82011-03-24 11:18:59 +00006350 trace_btrfs_inode_request(dir);
6351
Nikolay Borisov877574e2017-02-20 13:50:33 +02006352 ret = btrfs_set_inode_index(BTRFS_I(dir), index);
Shen Feng09771432009-04-02 16:46:06 -04006353 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006354 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04006355 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04006356 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04006357 }
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006358 } else if (dir) {
6359 *index = 0;
Josef Bacikaec74772008-07-24 12:12:38 -04006360 }
6361 /*
6362 * index_cnt is ignored for everything but a dir,
Su Yuedf6703e2018-01-12 11:08:02 +08006363 * btrfs_set_inode_index_count has an explanation for the magic
Josef Bacikaec74772008-07-24 12:12:38 -04006364 * number
6365 */
6366 BTRFS_I(inode)->index_cnt = 2;
Miao Xie67de1172013-12-26 13:07:06 +08006367 BTRFS_I(inode)->dir_index = *index;
Chris Mason39279cc2007-06-12 06:35:45 -04006368 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04006369 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00006370 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db22007-08-27 16:49:44 -04006371
Josef Bacik5dc562c2012-08-17 13:14:17 -04006372 /*
6373 * We could have gotten an inode number from somebody who was fsynced
6374 * and then removed in this same transaction, so let's just set full
6375 * sync since it will be a full sync anyway and this will blow away the
6376 * old info in the log.
6377 */
6378 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
6379
Chris Mason9c583092008-01-29 15:15:18 -05006380 key[0].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006381 key[0].type = BTRFS_INODE_ITEM_KEY;
Chris Mason9c583092008-01-29 15:15:18 -05006382 key[0].offset = 0;
6383
Chris Mason9c583092008-01-29 15:15:18 -05006384 sizes[0] = sizeof(struct btrfs_inode_item);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006385
6386 if (name) {
6387 /*
6388 * Start new inodes with an inode_ref. This is slightly more
6389 * efficient for small numbers of hard links since they will
6390 * be packed into one item. Extended refs will kick in if we
6391 * add more hard links than can fit in the ref item.
6392 */
6393 key[1].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006394 key[1].type = BTRFS_INODE_REF_KEY;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006395 key[1].offset = ref_objectid;
6396
6397 sizes[1] = name_len + sizeof(*ref);
6398 }
Chris Mason9c583092008-01-29 15:15:18 -05006399
Chris Masonb0d5d102014-09-08 13:08:51 -07006400 location = &BTRFS_I(inode)->location;
6401 location->objectid = objectid;
6402 location->offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02006403 location->type = BTRFS_INODE_ITEM_KEY;
Chris Masonb0d5d102014-09-08 13:08:51 -07006404
6405 ret = btrfs_insert_inode_locked(inode);
Al Viro32955c52018-05-16 12:20:05 -04006406 if (ret < 0) {
6407 iput(inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006408 goto fail;
Al Viro32955c52018-05-16 12:20:05 -04006409 }
Chris Masonb0d5d102014-09-08 13:08:51 -07006410
Chris Masonb9473432009-03-13 11:00:37 -04006411 path->leave_spinning = 1;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006412 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems);
Chris Mason9c583092008-01-29 15:15:18 -05006413 if (ret != 0)
Chris Masonb0d5d102014-09-08 13:08:51 -07006414 goto fail_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04006415
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03006416 inode_init_owner(inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04006417 inode_set_bytes(inode, 0);
chandan r9cc97d62012-07-04 12:48:07 +05306418
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006419 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05306420 inode->i_atime = inode->i_mtime;
6421 inode->i_ctime = inode->i_mtime;
Arnd Bergmannd3c6be6f2018-06-21 18:04:06 +02006422 BTRFS_I(inode)->i_otime = inode->i_mtime;
chandan r9cc97d62012-07-04 12:48:07 +05306423
Chris Mason5f39d392007-10-15 16:14:19 -04006424 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
6425 struct btrfs_inode_item);
David Sterbab159fa22016-11-08 18:09:03 +01006426 memzero_extent_buffer(path->nodes[0], (unsigned long)inode_item,
Li Zefan293f7e02012-07-10 00:58:58 -06006427 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04006428 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05006429
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006430 if (name) {
6431 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
6432 struct btrfs_inode_ref);
6433 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
6434 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
6435 ptr = (unsigned long)(ref + 1);
6436 write_extent_buffer(path->nodes[0], name, ptr, name_len);
6437 }
Chris Mason9c583092008-01-29 15:15:18 -05006438
Chris Mason5f39d392007-10-15 16:14:19 -04006439 btrfs_mark_buffer_dirty(path->nodes[0]);
6440 btrfs_free_path(path);
6441
Christoph Hellwig6cbff002009-04-17 10:37:41 +02006442 btrfs_inherit_iflags(inode, dir);
6443
Al Viro569254b2011-07-24 17:08:40 -04006444 if (S_ISREG(mode)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006445 if (btrfs_test_opt(fs_info, NODATASUM))
Chris Mason94272162009-07-02 12:26:06 -04006446 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006447 if (btrfs_test_opt(fs_info, NODATACOW))
Josef Bacikf2bdf9a2013-02-21 15:28:28 -05006448 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
6449 BTRFS_INODE_NODATASUM;
Chris Mason94272162009-07-02 12:26:06 -04006450 }
6451
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006452 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00006453
6454 trace_btrfs_inode_new(inode);
Chris Mason1973f0f2011-06-24 13:13:29 -04006455 btrfs_set_inode_last_trans(trans, inode);
liubo1abe9b82011-03-24 11:18:59 +00006456
Alexander Block8ea05e32012-07-25 17:35:53 +02006457 btrfs_update_root_times(trans, root);
6458
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006459 ret = btrfs_inode_inherit_props(trans, inode, dir);
6460 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006461 btrfs_err(fs_info,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006462 "error inheriting props for ino %llu (root %llu): %d",
David Sterbaf85b7372017-01-20 14:54:07 +01006463 btrfs_ino(BTRFS_I(inode)), root->root_key.objectid, ret);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006464
Chris Mason39279cc2007-06-12 06:35:45 -04006465 return inode;
Chris Masonb0d5d102014-09-08 13:08:51 -07006466
6467fail_unlock:
Al Viro32955c52018-05-16 12:20:05 -04006468 discard_new_inode(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006469fail:
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006470 if (dir && name)
Josef Bacikaec74772008-07-24 12:12:38 -04006471 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04006472 btrfs_free_path(path);
6473 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006474}
6475
Chris Masond352ac62008-09-29 15:18:18 -04006476/*
6477 * utility function to add 'inode' into 'parent_inode' with
6478 * a give name and a given sequence number.
6479 * if 'add_backref' is true, also insert a backref from the
6480 * inode to the parent directory.
6481 */
Chris Masone02119d2008-09-05 16:13:11 -04006482int btrfs_add_link(struct btrfs_trans_handle *trans,
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006483 struct btrfs_inode *parent_inode, struct btrfs_inode *inode,
Chris Masone02119d2008-09-05 16:13:11 -04006484 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006485{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006486 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006487 struct btrfs_key key;
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006488 struct btrfs_root *root = parent_inode->root;
6489 u64 ino = btrfs_ino(inode);
6490 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006491
Li Zefan33345d012011-04-20 10:31:50 +08006492 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006493 memcpy(&key, &inode->root->root_key, sizeof(key));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006494 } else {
Li Zefan33345d012011-04-20 10:31:50 +08006495 key.objectid = ino;
David Sterba962a2982014-06-04 18:41:45 +02006496 key.type = BTRFS_INODE_ITEM_KEY;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006497 key.offset = 0;
6498 }
Chris Mason39279cc2007-06-12 06:35:45 -04006499
Li Zefan33345d012011-04-20 10:31:50 +08006500 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Lu Fengqi6025c192018-08-01 11:32:29 +08006501 ret = btrfs_add_root_ref(trans, key.objectid,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006502 root->root_key.objectid, parent_ino,
6503 index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006504 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08006505 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
6506 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006507 }
6508
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006509 /* Nothing to clean up yet */
6510 if (ret)
6511 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006512
Lu Fengqi684572d2018-08-04 21:10:57 +08006513 ret = btrfs_insert_dir_item(trans, name, name_len, parent_inode, &key,
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006514 btrfs_inode_type(&inode->vfs_inode), index);
Chris Mason9c520572012-12-17 14:26:57 -05006515 if (ret == -EEXIST || ret == -EOVERFLOW)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006516 goto fail_dir_item;
6517 else if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04006518 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006519 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006520 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006521
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006522 btrfs_i_size_write(parent_inode, parent_inode->vfs_inode.i_size +
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006523 name_len * 2);
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006524 inode_inc_iversion(&parent_inode->vfs_inode);
Filipe Manana5338e432019-05-15 16:02:47 +01006525 /*
6526 * If we are replaying a log tree, we do not want to update the mtime
6527 * and ctime of the parent directory with the current time, since the
6528 * log replay procedure is responsible for setting them to their correct
6529 * values (the ones it had when the fsync was done).
6530 */
6531 if (!test_bit(BTRFS_FS_LOG_RECOVERING, &root->fs_info->flags)) {
6532 struct timespec64 now = current_time(&parent_inode->vfs_inode);
6533
6534 parent_inode->vfs_inode.i_mtime = now;
6535 parent_inode->vfs_inode.i_ctime = now;
6536 }
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006537 ret = btrfs_update_inode(trans, root, &parent_inode->vfs_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006538 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04006539 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006540 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05006541
6542fail_dir_item:
6543 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
6544 u64 local_index;
6545 int err;
Lu Fengqi3ee1c552018-08-01 11:32:28 +08006546 err = btrfs_del_root_ref(trans, key.objectid,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006547 root->root_key.objectid, parent_ino,
6548 &local_index, name, name_len);
Johannes Thumshirn1690dd412018-12-12 15:14:17 +01006549 if (err)
6550 btrfs_abort_transaction(trans, err);
Chris Masonfe66a052012-02-20 08:40:56 -05006551 } else if (add_backref) {
6552 u64 local_index;
6553 int err;
6554
6555 err = btrfs_del_inode_ref(trans, root, name, name_len,
6556 ino, parent_ino, &local_index);
Johannes Thumshirn1690dd412018-12-12 15:14:17 +01006557 if (err)
6558 btrfs_abort_transaction(trans, err);
Chris Masonfe66a052012-02-20 08:40:56 -05006559 }
Johannes Thumshirn1690dd412018-12-12 15:14:17 +01006560
6561 /* Return the original error code */
Chris Masonfe66a052012-02-20 08:40:56 -05006562 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006563}
6564
6565static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006566 struct btrfs_inode *dir, struct dentry *dentry,
6567 struct btrfs_inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006568{
Josef Bacika1b075d2010-11-19 20:36:11 +00006569 int err = btrfs_add_link(trans, dir, inode,
6570 dentry->d_name.name, dentry->d_name.len,
6571 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006572 if (err > 0)
6573 err = -EEXIST;
6574 return err;
6575}
6576
Josef Bacik618e21d2007-07-11 10:18:17 -04006577static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04006578 umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04006579{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006580 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Josef Bacik618e21d2007-07-11 10:18:17 -04006581 struct btrfs_trans_handle *trans;
6582 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006583 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04006584 int err;
Josef Bacik618e21d2007-07-11 10:18:17 -04006585 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006586 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04006587
Josef Bacik9ed74f22009-09-11 16:12:44 -04006588 /*
6589 * 2 for inode item and ref
6590 * 2 for dir items
6591 * 1 for xattr if selinux is on
6592 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006593 trans = btrfs_start_transaction(root, 5);
6594 if (IS_ERR(trans))
6595 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05006596
Li Zefan581bb052011-04-20 10:06:11 +08006597 err = btrfs_find_free_ino(root, &objectid);
6598 if (err)
6599 goto out_unlock;
6600
Josef Bacikaec74772008-07-24 12:12:38 -04006601 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006602 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6603 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006604 if (IS_ERR(inode)) {
6605 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -04006606 inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04006607 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006608 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006609
Casey Schauflerad19db72011-12-15 10:09:07 -05006610 /*
6611 * If the active LSM wants to access the inode during
6612 * d_instantiate it needs these. Smack checks to see
6613 * if the filesystem supports xattrs by looking at the
6614 * ops vector.
6615 */
Casey Schauflerad19db72011-12-15 10:09:07 -05006616 inode->i_op = &btrfs_special_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006617 init_special_inode(inode, inode->i_mode, rdev);
6618
6619 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik618e21d2007-07-11 10:18:17 -04006620 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006621 goto out_unlock;
Chris Masonb0d5d102014-09-08 13:08:51 -07006622
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006623 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6624 0, index);
Al Viro32955c52018-05-16 12:20:05 -04006625 if (err)
6626 goto out_unlock;
6627
6628 btrfs_update_inode(trans, root, inode);
6629 d_instantiate_new(dentry, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006630
Josef Bacik618e21d2007-07-11 10:18:17 -04006631out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006632 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006633 btrfs_btree_balance_dirty(fs_info);
Al Viro32955c52018-05-16 12:20:05 -04006634 if (err && inode) {
Josef Bacik618e21d2007-07-11 10:18:17 -04006635 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -04006636 discard_new_inode(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006637 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006638 return err;
6639}
6640
Chris Mason39279cc2007-06-12 06:35:45 -04006641static int btrfs_create(struct inode *dir, struct dentry *dentry,
Al Viroebfc3b42012-06-10 18:05:36 -04006642 umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -04006643{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006644 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04006645 struct btrfs_trans_handle *trans;
6646 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006647 struct inode *inode = NULL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006648 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04006649 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006650 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006651
Josef Bacik9ed74f22009-09-11 16:12:44 -04006652 /*
6653 * 2 for inode item and ref
6654 * 2 for dir items
6655 * 1 for xattr if selinux is on
6656 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006657 trans = btrfs_start_transaction(root, 5);
6658 if (IS_ERR(trans))
6659 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006660
Li Zefan581bb052011-04-20 10:06:11 +08006661 err = btrfs_find_free_ino(root, &objectid);
6662 if (err)
6663 goto out_unlock;
6664
Josef Bacikaec74772008-07-24 12:12:38 -04006665 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006666 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6667 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006668 if (IS_ERR(inode)) {
6669 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -04006670 inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006671 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006672 }
Casey Schauflerad19db72011-12-15 10:09:07 -05006673 /*
6674 * If the active LSM wants to access the inode during
6675 * d_instantiate it needs these. Smack checks to see
6676 * if the filesystem supports xattrs by looking at the
6677 * ops vector.
6678 */
6679 inode->i_fop = &btrfs_file_operations;
6680 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006681 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006682
6683 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
6684 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006685 goto out_unlock;
Chris Masonb0d5d102014-09-08 13:08:51 -07006686
6687 err = btrfs_update_inode(trans, root, inode);
6688 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006689 goto out_unlock;
Casey Schauflerad19db72011-12-15 10:09:07 -05006690
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006691 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6692 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006693 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006694 goto out_unlock;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006695
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006696 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Al Viro1e2e5472018-05-04 08:23:01 -04006697 d_instantiate_new(dentry, inode);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006698
Chris Mason39279cc2007-06-12 06:35:45 -04006699out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006700 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -04006701 if (err && inode) {
Chris Mason39279cc2007-06-12 06:35:45 -04006702 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -04006703 discard_new_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006704 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006705 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006706 return err;
6707}
6708
6709static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
6710 struct dentry *dentry)
6711{
Filipe Manana271dba452016-01-05 16:24:05 +00006712 struct btrfs_trans_handle *trans = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006713 struct btrfs_root *root = BTRFS_I(dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00006714 struct inode *inode = d_inode(old_dentry);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006715 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason00e4e6b2008-08-05 11:18:09 -04006716 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04006717 int err;
6718 int drop_inode = 0;
6719
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006720 /* do not allow sys_link's with other subvols of the same device */
Misono Tomohiro4fd786e2018-08-06 14:25:24 +09006721 if (root->root_key.objectid != BTRFS_I(inode)->root->root_key.objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00006722 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006723
Mark Fashehf1863732012-08-08 11:32:27 -07006724 if (inode->i_nlink >= BTRFS_LINK_MAX)
Al Viroc055e992011-03-04 17:15:18 +00006725 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04006726
Nikolay Borisov877574e2017-02-20 13:50:33 +02006727 err = btrfs_set_inode_index(BTRFS_I(dir), &index);
Josef Bacikaec74772008-07-24 12:12:38 -04006728 if (err)
6729 goto fail;
6730
Yan, Zhenga22285a2010-05-16 10:48:46 -04006731 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00006732 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04006733 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00006734 * 1 item for parent inode
Omar Sandoval399b0bb2018-05-11 13:13:40 -07006735 * 1 item for orphan item deletion if O_TMPFILE
Yan, Zhenga22285a2010-05-16 10:48:46 -04006736 */
Omar Sandoval399b0bb2018-05-11 13:13:40 -07006737 trans = btrfs_start_transaction(root, inode->i_nlink ? 5 : 6);
Yan, Zhenga22285a2010-05-16 10:48:46 -04006738 if (IS_ERR(trans)) {
6739 err = PTR_ERR(trans);
Filipe Manana271dba452016-01-05 16:24:05 +00006740 trans = NULL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006741 goto fail;
6742 }
Chris Mason5f39d392007-10-15 16:14:19 -04006743
Miao Xie67de1172013-12-26 13:07:06 +08006744 /* There are several dir indexes for this inode, clear the cache. */
6745 BTRFS_I(inode)->dir_index = 0ULL;
Zach Brown8b558c52013-10-16 12:10:34 -07006746 inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04006747 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006748 inode->i_ctime = current_time(inode);
Al Viro7de9c6ee2010-10-23 11:11:40 -04006749 ihold(inode);
Josef Bacike9976152012-10-11 15:53:56 -04006750 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
Josef Bacikaec74772008-07-24 12:12:38 -04006751
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006752 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6753 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04006754
Yan, Zhenga5719522009-09-24 09:17:31 -04006755 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006756 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04006757 } else {
Al Viro10d9f302011-07-16 23:09:10 -04006758 struct dentry *parent = dentry->d_parent;
Filipe Mananad4682ba2018-06-11 19:24:28 +01006759 int ret;
6760
Yan, Zhenga5719522009-09-24 09:17:31 -04006761 err = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006762 if (err)
6763 goto fail;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006764 if (inode->i_nlink == 1) {
6765 /*
6766 * If new hard link count is 1, it's a file created
6767 * with open(2) O_TMPFILE flag.
6768 */
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02006769 err = btrfs_orphan_del(trans, BTRFS_I(inode));
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006770 if (err)
6771 goto fail;
6772 }
Al Viro08c422c2011-12-23 07:58:13 -05006773 d_instantiate(dentry, inode);
Filipe Mananad4682ba2018-06-11 19:24:28 +01006774 ret = btrfs_log_new_name(trans, BTRFS_I(inode), NULL, parent,
6775 true, NULL);
6776 if (ret == BTRFS_NEED_TRANS_COMMIT) {
6777 err = btrfs_commit_transaction(trans);
6778 trans = NULL;
6779 }
Yan, Zhenga5719522009-09-24 09:17:31 -04006780 }
Chris Mason39279cc2007-06-12 06:35:45 -04006781
Chris Mason1832a6d2007-12-21 16:27:21 -05006782fail:
Filipe Manana271dba452016-01-05 16:24:05 +00006783 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006784 btrfs_end_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006785 if (drop_inode) {
6786 inode_dec_link_count(inode);
6787 iput(inode);
6788 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006789 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006790 return err;
6791}
6792
Al Viro18bb1db2011-07-26 01:41:39 -04006793static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04006794{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006795 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masonb9d86662008-05-02 16:13:49 -04006796 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006797 struct btrfs_trans_handle *trans;
6798 struct btrfs_root *root = BTRFS_I(dir)->root;
6799 int err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04006800 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006801 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006802
Josef Bacik9ed74f22009-09-11 16:12:44 -04006803 /*
6804 * 2 items for inode and ref
6805 * 2 items for dir items
6806 * 1 for xattr if selinux is on
6807 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006808 trans = btrfs_start_transaction(root, 5);
6809 if (IS_ERR(trans))
6810 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006811
Li Zefan581bb052011-04-20 10:06:11 +08006812 err = btrfs_find_free_ino(root, &objectid);
6813 if (err)
6814 goto out_fail;
6815
Josef Bacikaec74772008-07-24 12:12:38 -04006816 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006817 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6818 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04006819 if (IS_ERR(inode)) {
6820 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -04006821 inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006822 goto out_fail;
6823 }
Chris Mason5f39d392007-10-15 16:14:19 -04006824
Chris Masonb0d5d102014-09-08 13:08:51 -07006825 /* these must be set before we unlock the inode */
6826 inode->i_op = &btrfs_dir_inode_operations;
6827 inode->i_fop = &btrfs_dir_file_operations;
Josef Bacik33268ea2008-07-24 12:16:36 -04006828
Eric Paris2a7dba32011-02-01 11:05:39 -05006829 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04006830 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006831 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04006832
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02006833 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Mason39279cc2007-06-12 06:35:45 -04006834 err = btrfs_update_inode(trans, root, inode);
6835 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006836 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04006837
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006838 err = btrfs_add_link(trans, BTRFS_I(dir), BTRFS_I(inode),
6839 dentry->d_name.name,
6840 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006841 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006842 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04006843
Al Viro1e2e5472018-05-04 08:23:01 -04006844 d_instantiate_new(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006845
6846out_fail:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006847 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -04006848 if (err && inode) {
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006849 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -04006850 discard_new_inode(inode);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006851 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006852 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006853 return err;
6854}
6855
Chris Masonc8b97812008-10-29 14:49:59 -04006856static noinline int uncompress_inline(struct btrfs_path *path,
Byongho Leee40da0e2015-05-19 23:46:45 +09006857 struct page *page,
Chris Masonc8b97812008-10-29 14:49:59 -04006858 size_t pg_offset, u64 extent_offset,
6859 struct btrfs_file_extent_item *item)
6860{
6861 int ret;
6862 struct extent_buffer *leaf = path->nodes[0];
6863 char *tmp;
6864 size_t max_size;
6865 unsigned long inline_size;
6866 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08006867 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04006868
6869 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08006870 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04006871 max_size = btrfs_file_extent_ram_bytes(leaf, item);
6872 inline_size = btrfs_file_extent_inline_item_len(leaf,
Ross Kirkdd3cc162013-09-16 15:58:09 +01006873 btrfs_item_nr(path->slots[0]));
Chris Masonc8b97812008-10-29 14:49:59 -04006874 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04006875 if (!tmp)
6876 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04006877 ptr = btrfs_file_extent_inline_start(item);
6878
6879 read_extent_buffer(leaf, tmp, ptr, inline_size);
6880
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006881 max_size = min_t(unsigned long, PAGE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08006882 ret = btrfs_decompress(compress_type, tmp, page,
6883 extent_offset, inline_size, max_size);
Zygo Blaxelle1699d22017-03-10 16:45:44 -05006884
6885 /*
6886 * decompression code contains a memset to fill in any space between the end
6887 * of the uncompressed data and the end of max_size in case the decompressed
6888 * data ends up shorter than ram_bytes. That doesn't cover the hole between
6889 * the end of an inline extent and the beginning of the next block, so we
6890 * cover that region here.
6891 */
6892
6893 if (max_size + pg_offset < PAGE_SIZE) {
6894 char *map = kmap(page);
6895 memset(map + pg_offset + max_size, 0, PAGE_SIZE - max_size - pg_offset);
6896 kunmap(page);
6897 }
Chris Masonc8b97812008-10-29 14:49:59 -04006898 kfree(tmp);
Zach Brown166ae5a2014-05-09 17:15:10 -04006899 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -04006900}
6901
Chris Masond352ac62008-09-29 15:18:18 -04006902/*
6903 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05006904 * the ugly parts come from merging extents from the disk with the in-ram
6905 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04006906 * where the in-ram extents might be locked pending data=ordered completion.
6907 *
6908 * This also copies inline extents directly into the page.
6909 */
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006910struct extent_map *btrfs_get_extent(struct btrfs_inode *inode,
Liu Bode2c6612018-08-25 13:47:59 +08006911 struct page *page,
6912 size_t pg_offset, u64 start, u64 len,
6913 int create)
Chris Masona52d9a82007-08-27 16:49:44 -04006914{
David Sterba3ffbd682018-06-29 10:56:42 +02006915 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Chris Masona52d9a82007-08-27 16:49:44 -04006916 int ret;
6917 int err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006918 u64 extent_start = 0;
6919 u64 extent_end = 0;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006920 u64 objectid = btrfs_ino(inode);
Linus Torvalds7e74e232019-05-01 12:19:20 -07006921 int extent_type = -1;
Chris Masonf4219502008-07-22 11:18:09 -04006922 struct btrfs_path *path = NULL;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006923 struct btrfs_root *root = inode->root;
Chris Masona52d9a82007-08-27 16:49:44 -04006924 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04006925 struct extent_buffer *leaf;
6926 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04006927 struct extent_map *em = NULL;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006928 struct extent_map_tree *em_tree = &inode->extent_tree;
6929 struct extent_io_tree *io_tree = &inode->io_tree;
Filipe Manana7ffbb592014-06-09 03:48:05 +01006930 const bool new_inline = !page || create;
Chris Masona52d9a82007-08-27 16:49:44 -04006931
Chris Mason890871b2009-09-02 16:24:52 -04006932 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006933 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04006934 if (em)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006935 em->bdev = fs_info->fs_devices->latest_bdev;
Chris Mason890871b2009-09-02 16:24:52 -04006936 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006937
Chris Masona52d9a82007-08-27 16:49:44 -04006938 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04006939 if (em->start > start || em->start + em->len <= start)
6940 free_extent_map(em);
6941 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05006942 free_extent_map(em);
6943 else
6944 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006945 }
David Sterba172ddd62011-04-21 00:48:27 +02006946 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04006947 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05006948 err = -ENOMEM;
6949 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006950 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006951 em->bdev = fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05006952 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05006953 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05006954 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04006955 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04006956
Liu Bobee6ec82018-08-17 05:05:28 +08006957 path = btrfs_alloc_path();
Chris Masonf4219502008-07-22 11:18:09 -04006958 if (!path) {
Liu Bobee6ec82018-08-17 05:05:28 +08006959 err = -ENOMEM;
6960 goto out;
Chris Masonf4219502008-07-22 11:18:09 -04006961 }
6962
Liu Bobee6ec82018-08-17 05:05:28 +08006963 /* Chances are we'll be called again, so go ahead and do readahead */
6964 path->reada = READA_FORWARD;
6965
Liu Boe49aabd2018-08-25 13:47:09 +08006966 /*
6967 * Unless we're going to uncompress the inline extent, no sleep would
6968 * happen.
6969 */
6970 path->leave_spinning = 1;
6971
Nikolay Borisov5c9a7022017-12-01 11:19:40 +02006972 ret = btrfs_lookup_file_extent(NULL, root, path, objectid, start, 0);
Chris Masona52d9a82007-08-27 16:49:44 -04006973 if (ret < 0) {
6974 err = ret;
6975 goto out;
Nikolay Borisovb8eeab72018-12-17 11:49:00 +02006976 } else if (ret > 0) {
Chris Masona52d9a82007-08-27 16:49:44 -04006977 if (path->slots[0] == 0)
6978 goto not_found;
6979 path->slots[0]--;
6980 }
6981
Chris Mason5f39d392007-10-15 16:14:19 -04006982 leaf = path->nodes[0];
6983 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04006984 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04006985 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04006986 if (found_key.objectid != objectid ||
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006987 found_key.type != BTRFS_EXTENT_DATA_KEY) {
Josef Bacik25a50342013-10-14 12:08:38 -04006988 /*
6989 * If we backup past the first extent we want to move forward
6990 * and see if there is an extent in front of us, otherwise we'll
6991 * say there is a hole for our whole search range which can
6992 * cause problems.
6993 */
6994 extent_end = start;
6995 goto next;
Chris Masona52d9a82007-08-27 16:49:44 -04006996 }
6997
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006998 extent_type = btrfs_file_extent_type(leaf, item);
Chris Mason5f39d392007-10-15 16:14:19 -04006999 extent_start = found_key.offset;
Nikolay Borisov694c12e2018-12-17 10:35:59 +02007000 if (extent_type == BTRFS_FILE_EXTENT_REG ||
7001 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08007002 /* Only regular file could have regular/prealloc extent */
7003 if (!S_ISREG(inode->vfs_inode.i_mode)) {
7004 ret = -EUCLEAN;
7005 btrfs_crit(fs_info,
7006 "regular/prealloc extent found for non-regular inode %llu",
7007 btrfs_ino(inode));
7008 goto out;
7009 }
Chris Masona52d9a82007-08-27 16:49:44 -04007010 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04007011 btrfs_file_extent_num_bytes(leaf, item);
Liu Bo09ed2f12017-03-10 11:09:48 -08007012
7013 trace_btrfs_get_extent_show_fi_regular(inode, leaf, item,
7014 extent_start);
Nikolay Borisov694c12e2018-12-17 10:35:59 +02007015 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Yan Zheng9036c102008-10-30 14:19:41 -04007016 size_t size;
Qu Wenruoe41ca582018-06-06 15:41:49 +08007017
7018 size = btrfs_file_extent_ram_bytes(leaf, item);
Jeff Mahoneyda170662016-06-15 09:22:56 -04007019 extent_end = ALIGN(extent_start + size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007020 fs_info->sectorsize);
Liu Bo09ed2f12017-03-10 11:09:48 -08007021
7022 trace_btrfs_get_extent_show_fi_inline(inode, leaf, item,
7023 path->slots[0],
7024 extent_start);
Yan Zheng9036c102008-10-30 14:19:41 -04007025 }
Josef Bacik25a50342013-10-14 12:08:38 -04007026next:
Yan Zheng9036c102008-10-30 14:19:41 -04007027 if (start >= extent_end) {
7028 path->slots[0]++;
7029 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
7030 ret = btrfs_next_leaf(root, path);
7031 if (ret < 0) {
7032 err = ret;
7033 goto out;
Nikolay Borisovb8eeab72018-12-17 11:49:00 +02007034 } else if (ret > 0) {
Yan Zheng9036c102008-10-30 14:19:41 -04007035 goto not_found;
Nikolay Borisovb8eeab72018-12-17 11:49:00 +02007036 }
Yan Zheng9036c102008-10-30 14:19:41 -04007037 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04007038 }
Yan Zheng9036c102008-10-30 14:19:41 -04007039 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
7040 if (found_key.objectid != objectid ||
7041 found_key.type != BTRFS_EXTENT_DATA_KEY)
7042 goto not_found;
7043 if (start + len <= found_key.offset)
7044 goto not_found;
Wang Shilonge2eca692014-07-17 11:44:14 +08007045 if (start > found_key.offset)
7046 goto next;
Nikolay Borisov02a033d2018-12-17 10:36:02 +02007047
7048 /* New extent overlaps with existing one */
Yan Zheng9036c102008-10-30 14:19:41 -04007049 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04007050 em->orig_start = start;
Yan Zheng9036c102008-10-30 14:19:41 -04007051 em->len = found_key.offset - start;
Nikolay Borisov02a033d2018-12-17 10:36:02 +02007052 em->block_start = EXTENT_MAP_HOLE;
7053 goto insert;
Yan Zheng9036c102008-10-30 14:19:41 -04007054 }
7055
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007056 btrfs_extent_item_to_extent_map(inode, path, item,
Nikolay Borisov9cdc5122017-02-20 13:51:02 +02007057 new_inline, em);
Filipe Manana7ffbb592014-06-09 03:48:05 +01007058
Nikolay Borisov694c12e2018-12-17 10:35:59 +02007059 if (extent_type == BTRFS_FILE_EXTENT_REG ||
7060 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04007061 goto insert;
Nikolay Borisov694c12e2018-12-17 10:35:59 +02007062 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04007063 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04007064 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04007065 size_t size;
7066 size_t extent_offset;
7067 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04007068
Filipe Manana7ffbb592014-06-09 03:48:05 +01007069 if (new_inline)
Yan689f9342007-10-29 11:41:07 -04007070 goto out;
Yan689f9342007-10-29 11:41:07 -04007071
Qu Wenruoe41ca582018-06-06 15:41:49 +08007072 size = btrfs_file_extent_ram_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04007073 extent_offset = page_offset(page) + pg_offset - extent_start;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007074 copy_size = min_t(u64, PAGE_SIZE - pg_offset,
7075 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04007076 em->start = extent_start + extent_offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007077 em->len = ALIGN(copy_size, fs_info->sectorsize);
Josef Bacikb4939682012-12-03 10:31:19 -05007078 em->orig_block_len = em->len;
Josef Bacik70c8a912012-10-11 16:54:30 -04007079 em->orig_start = em->start;
Yan689f9342007-10-29 11:41:07 -04007080 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Liu Boe49aabd2018-08-25 13:47:09 +08007081
7082 btrfs_set_path_blocking(path);
Edmund Nadolskibf46f522017-11-20 13:24:49 -07007083 if (!PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08007084 if (btrfs_file_extent_compression(leaf, item) !=
7085 BTRFS_COMPRESS_NONE) {
Byongho Leee40da0e2015-05-19 23:46:45 +09007086 ret = uncompress_inline(path, page, pg_offset,
Chris Masonc8b97812008-10-29 14:49:59 -04007087 extent_offset, item);
Zach Brown166ae5a2014-05-09 17:15:10 -04007088 if (ret) {
7089 err = ret;
7090 goto out;
7091 }
Chris Masonc8b97812008-10-29 14:49:59 -04007092 } else {
7093 map = kmap(page);
7094 read_extent_buffer(leaf, map + pg_offset, ptr,
7095 copy_size);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007096 if (pg_offset + copy_size < PAGE_SIZE) {
Chris Mason93c82d52009-09-11 12:36:29 -04007097 memset(map + pg_offset + copy_size, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007098 PAGE_SIZE - pg_offset -
Chris Mason93c82d52009-09-11 12:36:29 -04007099 copy_size);
7100 }
Chris Masonc8b97812008-10-29 14:49:59 -04007101 kunmap(page);
7102 }
Chris Mason179e29e2007-11-01 11:28:41 -04007103 flush_dcache_page(page);
Chris Masona52d9a82007-08-27 16:49:44 -04007104 }
Chris Masond1310b22008-01-24 16:13:08 -05007105 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00007106 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04007107 goto insert;
Chris Masona52d9a82007-08-27 16:49:44 -04007108 }
7109not_found:
7110 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04007111 em->orig_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05007112 em->len = len;
Chris Mason5f39d392007-10-15 16:14:19 -04007113 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04007114insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02007115 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05007116 if (em->start > start || extent_map_end(em) <= start) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007117 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04007118 "bad extent! em: [%llu %llu] passed [%llu %llu]",
7119 em->start, em->len, start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04007120 err = -EIO;
7121 goto out;
7122 }
Chris Masond1310b22008-01-24 16:13:08 -05007123
7124 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04007125 write_lock(&em_tree->lock);
David Sterbaf46b24c2018-04-03 21:45:57 +02007126 err = btrfs_add_extent_mapping(fs_info, em_tree, &em, start, len);
Chris Mason890871b2009-09-02 16:24:52 -04007127 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04007128out:
Liu Boc6414282018-08-23 07:36:17 +08007129 btrfs_free_path(path);
liubo1abe9b82011-03-24 11:18:59 +00007130
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007131 trace_btrfs_get_extent(root, inode, em);
liubo1abe9b82011-03-24 11:18:59 +00007132
Chris Masona52d9a82007-08-27 16:49:44 -04007133 if (err) {
7134 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04007135 return ERR_PTR(err);
7136 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007137 BUG_ON(!em); /* Error is always set */
Chris Masona52d9a82007-08-27 16:49:44 -04007138 return em;
7139}
7140
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007141struct extent_map *btrfs_get_extent_fiemap(struct btrfs_inode *inode,
Nikolay Borisov4ab47a82018-12-12 09:42:32 +02007142 u64 start, u64 len)
Chris Masonec29ed52011-02-23 16:23:20 -05007143{
7144 struct extent_map *em;
7145 struct extent_map *hole_em = NULL;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007146 u64 delalloc_start = start;
Chris Masonec29ed52011-02-23 16:23:20 -05007147 u64 end;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007148 u64 delalloc_len;
7149 u64 delalloc_end;
Chris Masonec29ed52011-02-23 16:23:20 -05007150 int err = 0;
7151
Nikolay Borisov4ab47a82018-12-12 09:42:32 +02007152 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
Chris Masonec29ed52011-02-23 16:23:20 -05007153 if (IS_ERR(em))
7154 return em;
Dan Carpenter99862772017-04-11 11:57:15 +03007155 /*
7156 * If our em maps to:
7157 * - a hole or
7158 * - a pre-alloc extent,
7159 * there might actually be delalloc bytes behind it.
7160 */
7161 if (em->block_start != EXTENT_MAP_HOLE &&
7162 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7163 return em;
7164 else
7165 hole_em = em;
Chris Masonec29ed52011-02-23 16:23:20 -05007166
7167 /* check to see if we've wrapped (len == -1 or similar) */
7168 end = start + len;
7169 if (end < start)
7170 end = (u64)-1;
7171 else
7172 end -= 1;
7173
7174 em = NULL;
7175
7176 /* ok, we didn't find anything, lets look for delalloc */
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007177 delalloc_len = count_range_bits(&inode->io_tree, &delalloc_start,
Chris Masonec29ed52011-02-23 16:23:20 -05007178 end, len, EXTENT_DELALLOC, 1);
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007179 delalloc_end = delalloc_start + delalloc_len;
7180 if (delalloc_end < delalloc_start)
7181 delalloc_end = (u64)-1;
Chris Masonec29ed52011-02-23 16:23:20 -05007182
7183 /*
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007184 * We didn't find anything useful, return the original results from
7185 * get_extent()
Chris Masonec29ed52011-02-23 16:23:20 -05007186 */
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007187 if (delalloc_start > end || delalloc_end <= start) {
Chris Masonec29ed52011-02-23 16:23:20 -05007188 em = hole_em;
7189 hole_em = NULL;
7190 goto out;
7191 }
7192
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007193 /*
7194 * Adjust the delalloc_start to make sure it doesn't go backwards from
7195 * the start they passed in
Chris Masonec29ed52011-02-23 16:23:20 -05007196 */
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007197 delalloc_start = max(start, delalloc_start);
7198 delalloc_len = delalloc_end - delalloc_start;
Chris Masonec29ed52011-02-23 16:23:20 -05007199
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007200 if (delalloc_len > 0) {
7201 u64 hole_start;
Nikolay Borisov02950af2018-12-12 09:42:34 +02007202 u64 hole_len;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007203 const u64 hole_end = extent_map_end(hole_em);
Chris Masonec29ed52011-02-23 16:23:20 -05007204
David Sterba172ddd62011-04-21 00:48:27 +02007205 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05007206 if (!em) {
7207 err = -ENOMEM;
7208 goto out;
7209 }
Chris Masonec29ed52011-02-23 16:23:20 -05007210 em->bdev = NULL;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007211
7212 ASSERT(hole_em);
7213 /*
7214 * When btrfs_get_extent can't find anything it returns one
7215 * huge hole
7216 *
7217 * Make sure what it found really fits our range, and adjust to
7218 * make sure it is based on the start from the caller
7219 */
7220 if (hole_end <= start || hole_em->start > end) {
7221 free_extent_map(hole_em);
7222 hole_em = NULL;
7223 } else {
7224 hole_start = max(hole_em->start, start);
7225 hole_len = hole_end - hole_start;
7226 }
7227
7228 if (hole_em && delalloc_start > hole_start) {
7229 /*
7230 * Our hole starts before our delalloc, so we have to
7231 * return just the parts of the hole that go until the
7232 * delalloc starts
Chris Masonec29ed52011-02-23 16:23:20 -05007233 */
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007234 em->len = min(hole_len, delalloc_start - hole_start);
Chris Masonec29ed52011-02-23 16:23:20 -05007235 em->start = hole_start;
7236 em->orig_start = hole_start;
7237 /*
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007238 * Don't adjust block start at all, it is fixed at
7239 * EXTENT_MAP_HOLE
Chris Masonec29ed52011-02-23 16:23:20 -05007240 */
7241 em->block_start = hole_em->block_start;
7242 em->block_len = hole_len;
Liu Bof9e4fb52013-01-07 10:10:12 +00007243 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
7244 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonec29ed52011-02-23 16:23:20 -05007245 } else {
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007246 /*
7247 * Hole is out of passed range or it starts after
7248 * delalloc range
7249 */
7250 em->start = delalloc_start;
7251 em->len = delalloc_len;
7252 em->orig_start = delalloc_start;
Chris Masonec29ed52011-02-23 16:23:20 -05007253 em->block_start = EXTENT_MAP_DELALLOC;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007254 em->block_len = delalloc_len;
Chris Masonec29ed52011-02-23 16:23:20 -05007255 }
Nikolay Borisovbf8d32b2017-12-01 11:19:43 +02007256 } else {
Chris Masonec29ed52011-02-23 16:23:20 -05007257 return hole_em;
7258 }
7259out:
7260
7261 free_extent_map(hole_em);
7262 if (err) {
7263 free_extent_map(em);
7264 return ERR_PTR(err);
7265 }
7266 return em;
7267}
7268
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007269static struct extent_map *btrfs_create_dio_extent(struct inode *inode,
7270 const u64 start,
7271 const u64 len,
7272 const u64 orig_start,
7273 const u64 block_start,
7274 const u64 block_len,
7275 const u64 orig_block_len,
7276 const u64 ram_bytes,
7277 const int type)
7278{
7279 struct extent_map *em = NULL;
7280 int ret;
7281
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007282 if (type != BTRFS_ORDERED_NOCOW) {
Liu Bo6f9994d2017-01-31 07:50:22 -08007283 em = create_io_em(inode, start, len, orig_start,
7284 block_start, block_len, orig_block_len,
7285 ram_bytes,
7286 BTRFS_COMPRESS_NONE, /* compress_type */
7287 type);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007288 if (IS_ERR(em))
7289 goto out;
7290 }
7291 ret = btrfs_add_ordered_extent_dio(inode, start, block_start,
7292 len, block_len, type);
7293 if (ret) {
7294 if (em) {
7295 free_extent_map(em);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02007296 btrfs_drop_extent_cache(BTRFS_I(inode), start,
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007297 start + len - 1, 0);
7298 }
7299 em = ERR_PTR(ret);
7300 }
7301 out:
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007302
7303 return em;
7304}
7305
Josef Bacik4b46fce2010-05-23 11:00:55 -04007306static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
7307 u64 start, u64 len)
7308{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007309 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007310 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik70c8a912012-10-11 16:54:30 -04007311 struct extent_map *em;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007312 struct btrfs_key ins;
7313 u64 alloc_hint;
7314 int ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007315
Josef Bacik4b46fce2010-05-23 11:00:55 -04007316 alloc_hint = get_extent_allocation_hint(inode, start, len);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007317 ret = btrfs_reserve_extent(root, len, len, fs_info->sectorsize,
Jeff Mahoneyda170662016-06-15 09:22:56 -04007318 0, alloc_hint, &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007319 if (ret)
7320 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007321
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007322 em = btrfs_create_dio_extent(inode, start, ins.offset, start,
7323 ins.objectid, ins.offset, ins.offset,
Liu Bo6288d6e2017-02-21 12:12:58 -08007324 ins.offset, BTRFS_ORDERED_REGULAR);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007325 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007326 if (IS_ERR(em))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007327 btrfs_free_reserved_extent(fs_info, ins.objectid,
7328 ins.offset, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007329
Josef Bacik4b46fce2010-05-23 11:00:55 -04007330 return em;
7331}
7332
Chris Mason46bfbb52010-05-26 11:04:10 -04007333/*
7334 * returns 1 when the nocow is safe, < 1 on error, 0 if the
7335 * block must be cow'd
7336 */
Josef Bacik00361582013-08-14 14:02:47 -04007337noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
Josef Bacik7ee9e442013-06-21 16:37:03 -04007338 u64 *orig_start, u64 *orig_block_len,
7339 u64 *ram_bytes)
Chris Mason46bfbb52010-05-26 11:04:10 -04007340{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007341 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason46bfbb52010-05-26 11:04:10 -04007342 struct btrfs_path *path;
7343 int ret;
7344 struct extent_buffer *leaf;
7345 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xie7b2b7082014-02-27 13:58:05 +08007346 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason46bfbb52010-05-26 11:04:10 -04007347 struct btrfs_file_extent_item *fi;
7348 struct btrfs_key key;
7349 u64 disk_bytenr;
7350 u64 backref_offset;
7351 u64 extent_end;
7352 u64 num_bytes;
7353 int slot;
7354 int found_type;
Josef Bacik7ee9e442013-06-21 16:37:03 -04007355 bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW);
Miao Xiee77751a2013-12-27 21:11:50 +08007356
Chris Mason46bfbb52010-05-26 11:04:10 -04007357 path = btrfs_alloc_path();
7358 if (!path)
7359 return -ENOMEM;
7360
David Sterbaf85b7372017-01-20 14:54:07 +01007361 ret = btrfs_lookup_file_extent(NULL, root, path,
7362 btrfs_ino(BTRFS_I(inode)), offset, 0);
Chris Mason46bfbb52010-05-26 11:04:10 -04007363 if (ret < 0)
7364 goto out;
7365
7366 slot = path->slots[0];
7367 if (ret == 1) {
7368 if (slot == 0) {
7369 /* can't find the item, must cow */
7370 ret = 0;
7371 goto out;
7372 }
7373 slot--;
7374 }
7375 ret = 0;
7376 leaf = path->nodes[0];
7377 btrfs_item_key_to_cpu(leaf, &key, slot);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02007378 if (key.objectid != btrfs_ino(BTRFS_I(inode)) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04007379 key.type != BTRFS_EXTENT_DATA_KEY) {
7380 /* not our file or wrong item type, must cow */
7381 goto out;
7382 }
7383
7384 if (key.offset > offset) {
7385 /* Wrong offset, must cow */
7386 goto out;
7387 }
7388
7389 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
7390 found_type = btrfs_file_extent_type(leaf, fi);
7391 if (found_type != BTRFS_FILE_EXTENT_REG &&
7392 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
7393 /* not a regular extent, must cow */
7394 goto out;
7395 }
Josef Bacik7ee9e442013-06-21 16:37:03 -04007396
7397 if (!nocow && found_type == BTRFS_FILE_EXTENT_REG)
7398 goto out;
7399
Miao Xiee77751a2013-12-27 21:11:50 +08007400 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
7401 if (extent_end <= offset)
7402 goto out;
7403
Chris Mason46bfbb52010-05-26 11:04:10 -04007404 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Josef Bacik7ee9e442013-06-21 16:37:03 -04007405 if (disk_bytenr == 0)
7406 goto out;
7407
7408 if (btrfs_file_extent_compression(leaf, fi) ||
7409 btrfs_file_extent_encryption(leaf, fi) ||
7410 btrfs_file_extent_other_encoding(leaf, fi))
7411 goto out;
7412
Ethan Lien78d42952018-05-17 14:58:29 +08007413 /*
7414 * Do the same check as in btrfs_cross_ref_exist but without the
7415 * unnecessary search.
7416 */
7417 if (btrfs_file_extent_generation(leaf, fi) <=
7418 btrfs_root_last_snapshot(&root->root_item))
7419 goto out;
7420
Chris Mason46bfbb52010-05-26 11:04:10 -04007421 backref_offset = btrfs_file_extent_offset(leaf, fi);
7422
Josef Bacik7ee9e442013-06-21 16:37:03 -04007423 if (orig_start) {
7424 *orig_start = key.offset - backref_offset;
7425 *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
7426 *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
7427 }
Josef Bacikeb384b52013-04-24 16:32:55 -04007428
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007429 if (btrfs_extent_readonly(fs_info, disk_bytenr))
Chris Mason46bfbb52010-05-26 11:04:10 -04007430 goto out;
Miao Xie7b2b7082014-02-27 13:58:05 +08007431
7432 num_bytes = min(offset + *len, extent_end) - offset;
7433 if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) {
7434 u64 range_end;
7435
Jeff Mahoneyda170662016-06-15 09:22:56 -04007436 range_end = round_up(offset + num_bytes,
7437 root->fs_info->sectorsize) - 1;
Miao Xie7b2b7082014-02-27 13:58:05 +08007438 ret = test_range_bit(io_tree, offset, range_end,
7439 EXTENT_DELALLOC, 0, NULL);
7440 if (ret) {
7441 ret = -EAGAIN;
7442 goto out;
7443 }
7444 }
7445
Josef Bacik1bda19e2013-10-18 12:10:36 -04007446 btrfs_release_path(path);
Chris Mason46bfbb52010-05-26 11:04:10 -04007447
7448 /*
7449 * look for other files referencing this extent, if we
7450 * find any we must cow
7451 */
Josef Bacik00361582013-08-14 14:02:47 -04007452
Liu Boe4c3b2d2017-01-30 12:25:28 -08007453 ret = btrfs_cross_ref_exist(root, btrfs_ino(BTRFS_I(inode)),
Josef Bacik00361582013-08-14 14:02:47 -04007454 key.offset - backref_offset, disk_bytenr);
Josef Bacik00361582013-08-14 14:02:47 -04007455 if (ret) {
7456 ret = 0;
7457 goto out;
7458 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007459
7460 /*
7461 * adjust disk_bytenr and num_bytes to cover just the bytes
7462 * in this extent we are about to write. If there
7463 * are any csums in that range we have to cow in order
7464 * to keep the csums correct
7465 */
7466 disk_bytenr += backref_offset;
7467 disk_bytenr += offset - key.offset;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007468 if (csum_exist_in_range(fs_info, disk_bytenr, num_bytes))
7469 goto out;
Chris Mason46bfbb52010-05-26 11:04:10 -04007470 /*
7471 * all of the above have passed, it is safe to overwrite this extent
7472 * without cow
7473 */
Josef Bacikeb384b52013-04-24 16:32:55 -04007474 *len = num_bytes;
Chris Mason46bfbb52010-05-26 11:04:10 -04007475 ret = 1;
7476out:
7477 btrfs_free_path(path);
7478 return ret;
7479}
7480
Josef Bacikeb838e72012-07-31 16:28:48 -04007481static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
7482 struct extent_state **cached_state, int writing)
7483{
7484 struct btrfs_ordered_extent *ordered;
7485 int ret = 0;
7486
7487 while (1) {
7488 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbaff13db42015-12-03 14:30:40 +01007489 cached_state);
Josef Bacikeb838e72012-07-31 16:28:48 -04007490 /*
7491 * We're concerned with the entire range that we're going to be
Nicholas D Steeves01327612016-05-19 21:18:45 -04007492 * doing DIO to, so we need to make sure there's no ordered
Josef Bacikeb838e72012-07-31 16:28:48 -04007493 * extents in this range.
7494 */
Nikolay Borisova776c6f2017-02-20 13:50:49 +02007495 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), lockstart,
Josef Bacikeb838e72012-07-31 16:28:48 -04007496 lockend - lockstart + 1);
7497
7498 /*
7499 * We need to make sure there are no buffered pages in this
7500 * range either, we could have raced between the invalidate in
7501 * generic_file_direct_write and locking the extent. The
7502 * invalidate needs to happen so that reads after a write do not
7503 * get stale data.
7504 */
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007505 if (!ordered &&
David Sterba051c98e2018-03-07 15:33:22 +01007506 (!writing || !filemap_range_has_page(inode->i_mapping,
7507 lockstart, lockend)))
Josef Bacikeb838e72012-07-31 16:28:48 -04007508 break;
7509
7510 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbae43bbe52017-12-12 21:43:52 +01007511 cached_state);
Josef Bacikeb838e72012-07-31 16:28:48 -04007512
7513 if (ordered) {
Filipe Mananaade77022016-02-18 14:28:55 +00007514 /*
7515 * If we are doing a DIO read and the ordered extent we
7516 * found is for a buffered write, we can not wait for it
7517 * to complete and retry, because if we do so we can
7518 * deadlock with concurrent buffered writes on page
7519 * locks. This happens only if our DIO read covers more
7520 * than one extent map, if at this point has already
7521 * created an ordered extent for a previous extent map
7522 * and locked its range in the inode's io tree, and a
7523 * concurrent write against that previous extent map's
7524 * range and this range started (we unlock the ranges
7525 * in the io tree only when the bios complete and
7526 * buffered writes always lock pages before attempting
7527 * to lock range in the io tree).
7528 */
7529 if (writing ||
7530 test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags))
7531 btrfs_start_ordered_extent(inode, ordered, 1);
7532 else
7533 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007534 btrfs_put_ordered_extent(ordered);
7535 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007536 /*
Filipe Mananab850ae12015-12-08 16:23:16 +00007537 * We could trigger writeback for this range (and wait
7538 * for it to complete) and then invalidate the pages for
7539 * this range (through invalidate_inode_pages2_range()),
7540 * but that can lead us to a deadlock with a concurrent
7541 * call to readpages() (a buffered read or a defrag call
7542 * triggered a readahead) on a page lock due to an
7543 * ordered dio extent we created before but did not have
7544 * yet a corresponding bio submitted (whence it can not
7545 * complete), which makes readpages() wait for that
7546 * ordered extent to complete while holding a lock on
7547 * that page.
Josef Bacikeb838e72012-07-31 16:28:48 -04007548 */
Filipe Mananab850ae12015-12-08 16:23:16 +00007549 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007550 }
7551
Filipe Mananaade77022016-02-18 14:28:55 +00007552 if (ret)
7553 break;
7554
Josef Bacikeb838e72012-07-31 16:28:48 -04007555 cond_resched();
7556 }
7557
7558 return ret;
7559}
7560
Liu Bo6f9994d2017-01-31 07:50:22 -08007561/* The callers of this must take lock_extent() */
7562static struct extent_map *create_io_em(struct inode *inode, u64 start, u64 len,
7563 u64 orig_start, u64 block_start,
7564 u64 block_len, u64 orig_block_len,
7565 u64 ram_bytes, int compress_type,
7566 int type)
Josef Bacik69ffb542012-09-11 15:40:07 -04007567{
7568 struct extent_map_tree *em_tree;
7569 struct extent_map *em;
7570 struct btrfs_root *root = BTRFS_I(inode)->root;
7571 int ret;
7572
Liu Bo6f9994d2017-01-31 07:50:22 -08007573 ASSERT(type == BTRFS_ORDERED_PREALLOC ||
7574 type == BTRFS_ORDERED_COMPRESSED ||
7575 type == BTRFS_ORDERED_NOCOW ||
Liu Bo1af4a0a2017-02-13 15:35:09 -08007576 type == BTRFS_ORDERED_REGULAR);
Liu Bo6f9994d2017-01-31 07:50:22 -08007577
Josef Bacik69ffb542012-09-11 15:40:07 -04007578 em_tree = &BTRFS_I(inode)->extent_tree;
7579 em = alloc_extent_map();
7580 if (!em)
7581 return ERR_PTR(-ENOMEM);
7582
7583 em->start = start;
7584 em->orig_start = orig_start;
7585 em->len = len;
7586 em->block_len = block_len;
7587 em->block_start = block_start;
7588 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacikb4939682012-12-03 10:31:19 -05007589 em->orig_block_len = orig_block_len;
Josef Bacikcc95bef2013-04-04 14:31:27 -04007590 em->ram_bytes = ram_bytes;
Josef Bacik70c8a912012-10-11 16:54:30 -04007591 em->generation = -1;
Josef Bacik69ffb542012-09-11 15:40:07 -04007592 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Liu Bo1af4a0a2017-02-13 15:35:09 -08007593 if (type == BTRFS_ORDERED_PREALLOC) {
Josef Bacikb11e2342012-12-03 10:58:15 -05007594 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Liu Bo1af4a0a2017-02-13 15:35:09 -08007595 } else if (type == BTRFS_ORDERED_COMPRESSED) {
Liu Bo6f9994d2017-01-31 07:50:22 -08007596 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
7597 em->compress_type = compress_type;
7598 }
Josef Bacik69ffb542012-09-11 15:40:07 -04007599
7600 do {
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02007601 btrfs_drop_extent_cache(BTRFS_I(inode), em->start,
Josef Bacik69ffb542012-09-11 15:40:07 -04007602 em->start + em->len - 1, 0);
7603 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04007604 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik69ffb542012-09-11 15:40:07 -04007605 write_unlock(&em_tree->lock);
Liu Bo6f9994d2017-01-31 07:50:22 -08007606 /*
7607 * The caller has taken lock_extent(), who could race with us
7608 * to add em?
7609 */
Josef Bacik69ffb542012-09-11 15:40:07 -04007610 } while (ret == -EEXIST);
7611
7612 if (ret) {
7613 free_extent_map(em);
7614 return ERR_PTR(ret);
7615 }
7616
Liu Bo6f9994d2017-01-31 07:50:22 -08007617 /* em got 2 refs now, callers needs to do free_extent_map once. */
Josef Bacik69ffb542012-09-11 15:40:07 -04007618 return em;
7619}
7620
Nikolay Borisov1c8d0172018-05-02 15:19:32 +03007621
7622static int btrfs_get_blocks_direct_read(struct extent_map *em,
7623 struct buffer_head *bh_result,
7624 struct inode *inode,
7625 u64 start, u64 len)
7626{
7627 if (em->block_start == EXTENT_MAP_HOLE ||
7628 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7629 return -ENOENT;
7630
7631 len = min(len, em->len - (start - em->start));
7632
7633 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
7634 inode->i_blkbits;
7635 bh_result->b_size = len;
7636 bh_result->b_bdev = em->bdev;
7637 set_buffer_mapped(bh_result);
7638
7639 return 0;
7640}
7641
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007642static int btrfs_get_blocks_direct_write(struct extent_map **map,
7643 struct buffer_head *bh_result,
7644 struct inode *inode,
7645 struct btrfs_dio_data *dio_data,
7646 u64 start, u64 len)
7647{
7648 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
7649 struct extent_map *em = *map;
7650 int ret = 0;
7651
7652 /*
7653 * We don't allocate a new extent in the following cases
7654 *
7655 * 1) The inode is marked as NODATACOW. In this case we'll just use the
7656 * existing extent.
7657 * 2) The extent is marked as PREALLOC. We're good to go here and can
7658 * just use the extent.
7659 *
7660 */
7661 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
7662 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
7663 em->block_start != EXTENT_MAP_HOLE)) {
7664 int type;
7665 u64 block_start, orig_start, orig_block_len, ram_bytes;
7666
7667 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7668 type = BTRFS_ORDERED_PREALLOC;
7669 else
7670 type = BTRFS_ORDERED_NOCOW;
7671 len = min(len, em->len - (start - em->start));
7672 block_start = em->block_start + (start - em->start);
7673
7674 if (can_nocow_extent(inode, start, &len, &orig_start,
7675 &orig_block_len, &ram_bytes) == 1 &&
7676 btrfs_inc_nocow_writers(fs_info, block_start)) {
7677 struct extent_map *em2;
7678
7679 em2 = btrfs_create_dio_extent(inode, start, len,
7680 orig_start, block_start,
7681 len, orig_block_len,
7682 ram_bytes, type);
7683 btrfs_dec_nocow_writers(fs_info, block_start);
7684 if (type == BTRFS_ORDERED_PREALLOC) {
7685 free_extent_map(em);
7686 *map = em = em2;
7687 }
7688
7689 if (em2 && IS_ERR(em2)) {
7690 ret = PTR_ERR(em2);
7691 goto out;
7692 }
7693 /*
7694 * For inode marked NODATACOW or extent marked PREALLOC,
7695 * use the existing or preallocated extent, so does not
7696 * need to adjust btrfs_space_info's bytes_may_use.
7697 */
7698 btrfs_free_reserved_data_space_noquota(inode, start,
7699 len);
7700 goto skip_cow;
7701 }
7702 }
7703
7704 /* this will cow the extent */
7705 len = bh_result->b_size;
7706 free_extent_map(em);
7707 *map = em = btrfs_new_extent_direct(inode, start, len);
7708 if (IS_ERR(em)) {
7709 ret = PTR_ERR(em);
7710 goto out;
7711 }
7712
7713 len = min(len, em->len - (start - em->start));
7714
7715skip_cow:
7716 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
7717 inode->i_blkbits;
7718 bh_result->b_size = len;
7719 bh_result->b_bdev = em->bdev;
7720 set_buffer_mapped(bh_result);
7721
7722 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7723 set_buffer_new(bh_result);
7724
7725 /*
7726 * Need to update the i_size under the extent lock so buffered
7727 * readers will get the updated i_size when we unlock.
7728 */
7729 if (!dio_data->overwrite && start + len > i_size_read(inode))
7730 i_size_write(inode, start + len);
7731
7732 WARN_ON(dio_data->reserve < len);
7733 dio_data->reserve -= len;
7734 dio_data->unsubmitted_oe_range_end = start + len;
7735 current->journal_info = dio_data;
7736out:
7737 return ret;
7738}
7739
Josef Bacik4b46fce2010-05-23 11:00:55 -04007740static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
7741 struct buffer_head *bh_result, int create)
7742{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007743 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007744 struct extent_map *em;
Josef Bacikeb838e72012-07-31 16:28:48 -04007745 struct extent_state *cached_state = NULL;
chandan50745b02015-08-28 21:10:13 +05307746 struct btrfs_dio_data *dio_data = NULL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007747 u64 start = iblock << inode->i_blkbits;
Josef Bacikeb838e72012-07-31 16:28:48 -04007748 u64 lockstart, lockend;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007749 u64 len = bh_result->b_size;
Miao Xie09348562013-02-07 10:12:07 +00007750 int ret = 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007751
Omar Sandovale1821632019-08-15 14:04:04 -07007752 if (!create)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007753 len = min_t(u64, len, fs_info->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04007754
Josef Bacikc3298612012-08-03 16:49:19 -04007755 lockstart = start;
7756 lockend = start + len - 1;
7757
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007758 if (current->journal_info) {
7759 /*
7760 * Need to pull our outstanding extents and set journal_info to NULL so
Nicholas D Steeves01327612016-05-19 21:18:45 -04007761 * that anything that needs to check if there's a transaction doesn't get
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007762 * confused.
7763 */
chandan50745b02015-08-28 21:10:13 +05307764 dio_data = current->journal_info;
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007765 current->journal_info = NULL;
7766 }
7767
Josef Bacikeb838e72012-07-31 16:28:48 -04007768 /*
7769 * If this errors out it's because we couldn't invalidate pagecache for
7770 * this range and we need to fallback to buffered.
7771 */
Filipe Manana9c9464c2015-11-04 09:52:04 +00007772 if (lock_extent_direct(inode, lockstart, lockend, &cached_state,
7773 create)) {
7774 ret = -ENOTBLK;
7775 goto err;
7776 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007777
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007778 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len, 0);
Josef Bacikeb838e72012-07-31 16:28:48 -04007779 if (IS_ERR(em)) {
7780 ret = PTR_ERR(em);
7781 goto unlock_err;
7782 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007783
7784 /*
7785 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
7786 * io. INLINE is special, and we could probably kludge it in here, but
7787 * it's still buffered so for safety lets just fall back to the generic
7788 * buffered path.
7789 *
7790 * For COMPRESSED we _have_ to read the entire extent in so we can
7791 * decompress it, so there will be buffering required no matter what we
7792 * do, so go ahead and fallback to buffered.
7793 *
Nicholas D Steeves01327612016-05-19 21:18:45 -04007794 * We return -ENOTBLK because that's what makes DIO go ahead and go back
Josef Bacik4b46fce2010-05-23 11:00:55 -04007795 * to buffered IO. Don't blame me, this is the price we pay for using
7796 * the generic code.
7797 */
7798 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
7799 em->block_start == EXTENT_MAP_INLINE) {
7800 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007801 ret = -ENOTBLK;
7802 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007803 }
7804
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007805 if (create) {
7806 ret = btrfs_get_blocks_direct_write(&em, bh_result, inode,
7807 dio_data, start, len);
7808 if (ret < 0)
7809 goto unlock_err;
7810
Omar Sandovale1821632019-08-15 14:04:04 -07007811 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart,
7812 lockend, &cached_state);
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007813 } else {
Nikolay Borisov1c8d0172018-05-02 15:19:32 +03007814 ret = btrfs_get_blocks_direct_read(em, bh_result, inode,
7815 start, len);
7816 /* Can be negative only if we read from a hole */
7817 if (ret < 0) {
7818 ret = 0;
7819 free_extent_map(em);
7820 goto unlock_err;
7821 }
7822 /*
7823 * We need to unlock only the end area that we aren't using.
7824 * The rest is going to be unlocked by the endio routine.
7825 */
7826 lockstart = start + bh_result->b_size;
7827 if (lockstart < lockend) {
Omar Sandovale1821632019-08-15 14:04:04 -07007828 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
7829 lockstart, lockend, &cached_state);
Nikolay Borisov1c8d0172018-05-02 15:19:32 +03007830 } else {
7831 free_extent_state(cached_state);
7832 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007833 }
7834
Josef Bacik4b46fce2010-05-23 11:00:55 -04007835 free_extent_map(em);
7836
7837 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007838
7839unlock_err:
Omar Sandovale1821632019-08-15 14:04:04 -07007840 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7841 &cached_state);
Filipe Manana9c9464c2015-11-04 09:52:04 +00007842err:
chandan50745b02015-08-28 21:10:13 +05307843 if (dio_data)
7844 current->journal_info = dio_data;
Josef Bacikeb838e72012-07-31 16:28:48 -04007845 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007846}
7847
Omar Sandoval58efbc92017-08-22 23:45:59 -07007848static inline blk_status_t submit_dio_repair_bio(struct inode *inode,
7849 struct bio *bio,
7850 int mirror_num)
Miao Xie8b110e32014-09-12 18:44:03 +08007851{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007852 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Omar Sandoval58efbc92017-08-22 23:45:59 -07007853 blk_status_t ret;
Miao Xie8b110e32014-09-12 18:44:03 +08007854
Mike Christie37226b22016-06-05 14:31:52 -05007855 BUG_ON(bio_op(bio) == REQ_OP_WRITE);
Miao Xie8b110e32014-09-12 18:44:03 +08007856
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007857 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DIO_REPAIR);
Miao Xie8b110e32014-09-12 18:44:03 +08007858 if (ret)
Nikolay Borisovea057f62017-12-13 10:25:38 +02007859 return ret;
Miao Xie8b110e32014-09-12 18:44:03 +08007860
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007861 ret = btrfs_map_bio(fs_info, bio, mirror_num, 0);
Nikolay Borisovea057f62017-12-13 10:25:38 +02007862
Miao Xie8b110e32014-09-12 18:44:03 +08007863 return ret;
7864}
7865
7866static int btrfs_check_dio_repairable(struct inode *inode,
7867 struct bio *failed_bio,
7868 struct io_failure_record *failrec,
7869 int failed_mirror)
7870{
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007871 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xie8b110e32014-09-12 18:44:03 +08007872 int num_copies;
7873
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007874 num_copies = btrfs_num_copies(fs_info, failrec->logical, failrec->len);
Miao Xie8b110e32014-09-12 18:44:03 +08007875 if (num_copies == 1) {
7876 /*
7877 * we only have a single copy of the data, so don't bother with
7878 * all the retry and error correction code that follows. no
7879 * matter what the error is, it is very likely to persist.
7880 */
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007881 btrfs_debug(fs_info,
7882 "Check DIO Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d",
7883 num_copies, failrec->this_mirror, failed_mirror);
Miao Xie8b110e32014-09-12 18:44:03 +08007884 return 0;
7885 }
7886
7887 failrec->failed_mirror = failed_mirror;
7888 failrec->this_mirror++;
7889 if (failrec->this_mirror == failed_mirror)
7890 failrec->this_mirror++;
7891
7892 if (failrec->this_mirror > num_copies) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007893 btrfs_debug(fs_info,
7894 "Check DIO Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d",
7895 num_copies, failrec->this_mirror, failed_mirror);
Miao Xie8b110e32014-09-12 18:44:03 +08007896 return 0;
7897 }
7898
7899 return 1;
7900}
7901
Omar Sandoval58efbc92017-08-22 23:45:59 -07007902static blk_status_t dio_read_error(struct inode *inode, struct bio *failed_bio,
7903 struct page *page, unsigned int pgoff,
7904 u64 start, u64 end, int failed_mirror,
7905 bio_end_io_t *repair_endio, void *repair_arg)
Miao Xie8b110e32014-09-12 18:44:03 +08007906{
7907 struct io_failure_record *failrec;
Josef Bacik7870d082017-05-05 11:57:15 -04007908 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
7909 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
Miao Xie8b110e32014-09-12 18:44:03 +08007910 struct bio *bio;
7911 int isector;
David Sterbaf1c77c52017-06-06 19:03:49 +02007912 unsigned int read_mode = 0;
Liu Bo17347ce2017-05-15 15:33:27 -07007913 int segs;
Miao Xie8b110e32014-09-12 18:44:03 +08007914 int ret;
Omar Sandoval58efbc92017-08-22 23:45:59 -07007915 blk_status_t status;
Ming Leic16a8ac2017-12-18 20:22:12 +08007916 struct bio_vec bvec;
Miao Xie8b110e32014-09-12 18:44:03 +08007917
Mike Christie37226b22016-06-05 14:31:52 -05007918 BUG_ON(bio_op(failed_bio) == REQ_OP_WRITE);
Miao Xie8b110e32014-09-12 18:44:03 +08007919
7920 ret = btrfs_get_io_failure_record(inode, start, end, &failrec);
7921 if (ret)
Omar Sandoval58efbc92017-08-22 23:45:59 -07007922 return errno_to_blk_status(ret);
Miao Xie8b110e32014-09-12 18:44:03 +08007923
7924 ret = btrfs_check_dio_repairable(inode, failed_bio, failrec,
7925 failed_mirror);
7926 if (!ret) {
Josef Bacik7870d082017-05-05 11:57:15 -04007927 free_io_failure(failure_tree, io_tree, failrec);
Omar Sandoval58efbc92017-08-22 23:45:59 -07007928 return BLK_STS_IOERR;
Miao Xie8b110e32014-09-12 18:44:03 +08007929 }
7930
Liu Bo17347ce2017-05-15 15:33:27 -07007931 segs = bio_segments(failed_bio);
Ming Leic16a8ac2017-12-18 20:22:12 +08007932 bio_get_first_bvec(failed_bio, &bvec);
Liu Bo17347ce2017-05-15 15:33:27 -07007933 if (segs > 1 ||
Ming Leic16a8ac2017-12-18 20:22:12 +08007934 (bvec.bv_len > btrfs_inode_sectorsize(inode)))
Christoph Hellwig70fd7612016-11-01 07:40:10 -06007935 read_mode |= REQ_FAILFAST_DEV;
Miao Xie8b110e32014-09-12 18:44:03 +08007936
7937 isector = start - btrfs_io_bio(failed_bio)->logical;
7938 isector >>= inode->i_sb->s_blocksize_bits;
7939 bio = btrfs_create_repair_bio(inode, failed_bio, failrec, page,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307940 pgoff, isector, repair_endio, repair_arg);
David Sterbaebcc3262018-06-29 10:56:53 +02007941 bio->bi_opf = REQ_OP_READ | read_mode;
Miao Xie8b110e32014-09-12 18:44:03 +08007942
7943 btrfs_debug(BTRFS_I(inode)->root->fs_info,
David Sterba913e1532017-07-13 15:32:18 +02007944 "repair DIO read error: submitting new dio read[%#x] to this_mirror=%d, in_validation=%d",
Miao Xie8b110e32014-09-12 18:44:03 +08007945 read_mode, failrec->this_mirror, failrec->in_validation);
7946
Omar Sandoval58efbc92017-08-22 23:45:59 -07007947 status = submit_dio_repair_bio(inode, bio, failrec->this_mirror);
7948 if (status) {
Josef Bacik7870d082017-05-05 11:57:15 -04007949 free_io_failure(failure_tree, io_tree, failrec);
Miao Xie8b110e32014-09-12 18:44:03 +08007950 bio_put(bio);
7951 }
7952
Omar Sandoval58efbc92017-08-22 23:45:59 -07007953 return status;
Miao Xie8b110e32014-09-12 18:44:03 +08007954}
7955
7956struct btrfs_retry_complete {
7957 struct completion done;
7958 struct inode *inode;
7959 u64 start;
7960 int uptodate;
7961};
7962
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007963static void btrfs_retry_endio_nocsum(struct bio *bio)
Miao Xie8b110e32014-09-12 18:44:03 +08007964{
7965 struct btrfs_retry_complete *done = bio->bi_private;
Josef Bacik7870d082017-05-05 11:57:15 -04007966 struct inode *inode = done->inode;
Miao Xie8b110e32014-09-12 18:44:03 +08007967 struct bio_vec *bvec;
Josef Bacik7870d082017-05-05 11:57:15 -04007968 struct extent_io_tree *io_tree, *failure_tree;
Ming Lei6dc4f102019-02-15 19:13:19 +08007969 struct bvec_iter_all iter_all;
Miao Xie8b110e32014-09-12 18:44:03 +08007970
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02007971 if (bio->bi_status)
Miao Xie8b110e32014-09-12 18:44:03 +08007972 goto end;
7973
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307974 ASSERT(bio->bi_vcnt == 1);
Josef Bacik7870d082017-05-05 11:57:15 -04007975 io_tree = &BTRFS_I(inode)->io_tree;
7976 failure_tree = &BTRFS_I(inode)->io_failure_tree;
Ming Lei263663c2017-12-18 20:22:04 +08007977 ASSERT(bio_first_bvec_all(bio)->bv_len == btrfs_inode_sectorsize(inode));
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307978
Miao Xie8b110e32014-09-12 18:44:03 +08007979 done->uptodate = 1;
David Sterbac09abff2017-07-13 18:10:07 +02007980 ASSERT(!bio_flagged(bio, BIO_CLONED));
Christoph Hellwig2b070cf2019-04-25 09:03:00 +02007981 bio_for_each_segment_all(bvec, bio, iter_all)
Josef Bacik7870d082017-05-05 11:57:15 -04007982 clean_io_failure(BTRFS_I(inode)->root->fs_info, failure_tree,
7983 io_tree, done->start, bvec->bv_page,
7984 btrfs_ino(BTRFS_I(inode)), 0);
Miao Xie8b110e32014-09-12 18:44:03 +08007985end:
7986 complete(&done->done);
7987 bio_put(bio);
7988}
7989
Omar Sandoval58efbc92017-08-22 23:45:59 -07007990static blk_status_t __btrfs_correct_data_nocsum(struct inode *inode,
7991 struct btrfs_io_bio *io_bio)
Josef Bacik4b46fce2010-05-23 11:00:55 -04007992{
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307993 struct btrfs_fs_info *fs_info;
Liu Bo17347ce2017-05-15 15:33:27 -07007994 struct bio_vec bvec;
7995 struct bvec_iter iter;
Miao Xie8b110e32014-09-12 18:44:03 +08007996 struct btrfs_retry_complete done;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007997 u64 start;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307998 unsigned int pgoff;
7999 u32 sectorsize;
8000 int nr_sectors;
Omar Sandoval58efbc92017-08-22 23:45:59 -07008001 blk_status_t ret;
8002 blk_status_t err = BLK_STS_OK;
Miao Xiedc380ae2014-09-12 18:43:55 +08008003
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308004 fs_info = BTRFS_I(inode)->root->fs_info;
Jeff Mahoneyda170662016-06-15 09:22:56 -04008005 sectorsize = fs_info->sectorsize;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308006
Miao Xiec1dc0892014-09-12 18:43:56 +08008007 start = io_bio->logical;
Miao Xie8b110e32014-09-12 18:44:03 +08008008 done.inode = inode;
Liu Bo17347ce2017-05-15 15:33:27 -07008009 io_bio->bio.bi_iter = io_bio->iter;
Miao Xie8b110e32014-09-12 18:44:03 +08008010
Liu Bo17347ce2017-05-15 15:33:27 -07008011 bio_for_each_segment(bvec, &io_bio->bio, iter) {
8012 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec.bv_len);
8013 pgoff = bvec.bv_offset;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308014
8015next_block_or_try_again:
Miao Xie8b110e32014-09-12 18:44:03 +08008016 done.uptodate = 0;
8017 done.start = start;
8018 init_completion(&done.done);
8019
Liu Bo17347ce2017-05-15 15:33:27 -07008020 ret = dio_read_error(inode, &io_bio->bio, bvec.bv_page,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308021 pgoff, start, start + sectorsize - 1,
8022 io_bio->mirror_num,
8023 btrfs_retry_endio_nocsum, &done);
Liu Bo629ebf42017-05-15 17:20:07 -07008024 if (ret) {
8025 err = ret;
8026 goto next;
8027 }
Miao Xie8b110e32014-09-12 18:44:03 +08008028
David Sterba9c17f6c2017-07-19 19:26:45 +02008029 wait_for_completion_io(&done.done);
Miao Xie8b110e32014-09-12 18:44:03 +08008030
8031 if (!done.uptodate) {
8032 /* We might have another mirror, so try again */
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308033 goto next_block_or_try_again;
Miao Xie8b110e32014-09-12 18:44:03 +08008034 }
8035
Liu Bo629ebf42017-05-15 17:20:07 -07008036next:
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308037 start += sectorsize;
8038
Liu Bo97bf5a52017-04-07 13:11:10 -07008039 nr_sectors--;
8040 if (nr_sectors) {
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308041 pgoff += sectorsize;
Liu Bo97bf5a52017-04-07 13:11:10 -07008042 ASSERT(pgoff < PAGE_SIZE);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308043 goto next_block_or_try_again;
8044 }
Miao Xie8b110e32014-09-12 18:44:03 +08008045 }
8046
Liu Bo629ebf42017-05-15 17:20:07 -07008047 return err;
Miao Xie8b110e32014-09-12 18:44:03 +08008048}
8049
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008050static void btrfs_retry_endio(struct bio *bio)
Miao Xie8b110e32014-09-12 18:44:03 +08008051{
8052 struct btrfs_retry_complete *done = bio->bi_private;
8053 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
Josef Bacik7870d082017-05-05 11:57:15 -04008054 struct extent_io_tree *io_tree, *failure_tree;
8055 struct inode *inode = done->inode;
Miao Xie8b110e32014-09-12 18:44:03 +08008056 struct bio_vec *bvec;
8057 int uptodate;
8058 int ret;
Christoph Hellwig2b070cf2019-04-25 09:03:00 +02008059 int i = 0;
Ming Lei6dc4f102019-02-15 19:13:19 +08008060 struct bvec_iter_all iter_all;
Miao Xie8b110e32014-09-12 18:44:03 +08008061
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008062 if (bio->bi_status)
Miao Xie8b110e32014-09-12 18:44:03 +08008063 goto end;
8064
8065 uptodate = 1;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308066
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308067 ASSERT(bio->bi_vcnt == 1);
Ming Lei263663c2017-12-18 20:22:04 +08008068 ASSERT(bio_first_bvec_all(bio)->bv_len == btrfs_inode_sectorsize(done->inode));
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308069
Josef Bacik7870d082017-05-05 11:57:15 -04008070 io_tree = &BTRFS_I(inode)->io_tree;
8071 failure_tree = &BTRFS_I(inode)->io_failure_tree;
8072
David Sterbac09abff2017-07-13 18:10:07 +02008073 ASSERT(!bio_flagged(bio, BIO_CLONED));
Christoph Hellwig2b070cf2019-04-25 09:03:00 +02008074 bio_for_each_segment_all(bvec, bio, iter_all) {
Josef Bacik7870d082017-05-05 11:57:15 -04008075 ret = __readpage_endio_check(inode, io_bio, i, bvec->bv_page,
8076 bvec->bv_offset, done->start,
8077 bvec->bv_len);
Miao Xie8b110e32014-09-12 18:44:03 +08008078 if (!ret)
Josef Bacik7870d082017-05-05 11:57:15 -04008079 clean_io_failure(BTRFS_I(inode)->root->fs_info,
8080 failure_tree, io_tree, done->start,
8081 bvec->bv_page,
8082 btrfs_ino(BTRFS_I(inode)),
8083 bvec->bv_offset);
Miao Xie8b110e32014-09-12 18:44:03 +08008084 else
8085 uptodate = 0;
Christoph Hellwig2b070cf2019-04-25 09:03:00 +02008086 i++;
Miao Xie8b110e32014-09-12 18:44:03 +08008087 }
8088
8089 done->uptodate = uptodate;
8090end:
8091 complete(&done->done);
8092 bio_put(bio);
8093}
8094
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008095static blk_status_t __btrfs_subio_endio_read(struct inode *inode,
8096 struct btrfs_io_bio *io_bio, blk_status_t err)
Miao Xie8b110e32014-09-12 18:44:03 +08008097{
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308098 struct btrfs_fs_info *fs_info;
Liu Bo17347ce2017-05-15 15:33:27 -07008099 struct bio_vec bvec;
8100 struct bvec_iter iter;
Miao Xie8b110e32014-09-12 18:44:03 +08008101 struct btrfs_retry_complete done;
8102 u64 start;
8103 u64 offset = 0;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308104 u32 sectorsize;
8105 int nr_sectors;
8106 unsigned int pgoff;
8107 int csum_pos;
Liu Boef7cdac12017-04-13 18:11:48 -07008108 bool uptodate = (err == 0);
Miao Xie8b110e32014-09-12 18:44:03 +08008109 int ret;
Omar Sandoval58efbc92017-08-22 23:45:59 -07008110 blk_status_t status;
Miao Xie8b110e32014-09-12 18:44:03 +08008111
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308112 fs_info = BTRFS_I(inode)->root->fs_info;
Jeff Mahoneyda170662016-06-15 09:22:56 -04008113 sectorsize = fs_info->sectorsize;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308114
Omar Sandoval58efbc92017-08-22 23:45:59 -07008115 err = BLK_STS_OK;
Miao Xie8b110e32014-09-12 18:44:03 +08008116 start = io_bio->logical;
8117 done.inode = inode;
Liu Bo17347ce2017-05-15 15:33:27 -07008118 io_bio->bio.bi_iter = io_bio->iter;
Miao Xie8b110e32014-09-12 18:44:03 +08008119
Liu Bo17347ce2017-05-15 15:33:27 -07008120 bio_for_each_segment(bvec, &io_bio->bio, iter) {
8121 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec.bv_len);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308122
Liu Bo17347ce2017-05-15 15:33:27 -07008123 pgoff = bvec.bv_offset;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308124next_block:
Liu Boef7cdac12017-04-13 18:11:48 -07008125 if (uptodate) {
8126 csum_pos = BTRFS_BYTES_TO_BLKS(fs_info, offset);
8127 ret = __readpage_endio_check(inode, io_bio, csum_pos,
8128 bvec.bv_page, pgoff, start, sectorsize);
8129 if (likely(!ret))
8130 goto next;
8131 }
Miao Xie8b110e32014-09-12 18:44:03 +08008132try_again:
8133 done.uptodate = 0;
8134 done.start = start;
8135 init_completion(&done.done);
8136
Omar Sandoval58efbc92017-08-22 23:45:59 -07008137 status = dio_read_error(inode, &io_bio->bio, bvec.bv_page,
8138 pgoff, start, start + sectorsize - 1,
8139 io_bio->mirror_num, btrfs_retry_endio,
8140 &done);
8141 if (status) {
8142 err = status;
Miao Xie8b110e32014-09-12 18:44:03 +08008143 goto next;
8144 }
8145
David Sterba9c17f6c2017-07-19 19:26:45 +02008146 wait_for_completion_io(&done.done);
Miao Xie8b110e32014-09-12 18:44:03 +08008147
8148 if (!done.uptodate) {
8149 /* We might have another mirror, so try again */
8150 goto try_again;
8151 }
8152next:
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308153 offset += sectorsize;
8154 start += sectorsize;
8155
8156 ASSERT(nr_sectors);
8157
Liu Bo97bf5a52017-04-07 13:11:10 -07008158 nr_sectors--;
8159 if (nr_sectors) {
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308160 pgoff += sectorsize;
Liu Bo97bf5a52017-04-07 13:11:10 -07008161 ASSERT(pgoff < PAGE_SIZE);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308162 goto next_block;
8163 }
Kent Overstreet2c30c712013-11-07 12:20:26 -08008164 }
Miao Xiec1dc0892014-09-12 18:43:56 +08008165
8166 return err;
8167}
8168
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008169static blk_status_t btrfs_subio_endio_read(struct inode *inode,
8170 struct btrfs_io_bio *io_bio, blk_status_t err)
Miao Xie8b110e32014-09-12 18:44:03 +08008171{
8172 bool skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
8173
8174 if (skip_csum) {
8175 if (unlikely(err))
8176 return __btrfs_correct_data_nocsum(inode, io_bio);
8177 else
Omar Sandoval58efbc92017-08-22 23:45:59 -07008178 return BLK_STS_OK;
Miao Xie8b110e32014-09-12 18:44:03 +08008179 } else {
8180 return __btrfs_subio_endio_read(inode, io_bio, err);
8181 }
8182}
8183
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008184static void btrfs_endio_direct_read(struct bio *bio)
Miao Xiec1dc0892014-09-12 18:43:56 +08008185{
8186 struct btrfs_dio_private *dip = bio->bi_private;
8187 struct inode *inode = dip->inode;
8188 struct bio *dio_bio;
8189 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008190 blk_status_t err = bio->bi_status;
Miao Xiec1dc0892014-09-12 18:43:56 +08008191
Liu Bo99c4e3b92017-09-15 15:06:51 -06008192 if (dip->flags & BTRFS_DIO_ORIG_BIO_SUBMITTED)
Miao Xie8b110e32014-09-12 18:44:03 +08008193 err = btrfs_subio_endio_read(inode, io_bio, err);
Miao Xiec1dc0892014-09-12 18:43:56 +08008194
Josef Bacik4b46fce2010-05-23 11:00:55 -04008195 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01008196 dip->logical_offset + dip->bytes - 1);
Chris Mason9be33952013-05-17 18:30:14 -04008197 dio_bio = dip->dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008198
Josef Bacik4b46fce2010-05-23 11:00:55 -04008199 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04008200
Liu Bo99c4e3b92017-09-15 15:06:51 -06008201 dio_bio->bi_status = err;
Christoph Hellwig40553512017-06-03 09:37:58 +02008202 dio_end_io(dio_bio);
David Sterbab3a0dd52018-11-22 17:16:49 +01008203 btrfs_io_bio_free_csum(io_bio);
Chris Mason9be33952013-05-17 18:30:14 -04008204 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008205}
8206
Qu Wenruo524272602017-03-08 10:25:52 +08008207static void __endio_write_update_ordered(struct inode *inode,
8208 const u64 offset, const u64 bytes,
8209 const bool uptodate)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008210{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008211 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008212 struct btrfs_ordered_extent *ordered = NULL;
Qu Wenruo524272602017-03-08 10:25:52 +08008213 struct btrfs_workqueue *wq;
8214 btrfs_work_func_t func;
Filipe Manana14543772015-11-24 16:23:54 +00008215 u64 ordered_offset = offset;
8216 u64 ordered_bytes = bytes;
Naohiro Aota67c003f2017-09-01 17:59:07 +09008217 u64 last_offset;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008218
Qu Wenruo524272602017-03-08 10:25:52 +08008219 if (btrfs_is_free_space_inode(BTRFS_I(inode))) {
8220 wq = fs_info->endio_freespace_worker;
8221 func = btrfs_freespace_write_helper;
8222 } else {
8223 wq = fs_info->endio_write_workers;
8224 func = btrfs_endio_write_helper;
8225 }
8226
Nikolay Borisovb25f0d02018-04-11 11:21:17 +03008227 while (ordered_offset < offset + bytes) {
8228 last_offset = ordered_offset;
8229 if (btrfs_dec_test_first_ordered_pending(inode, &ordered,
8230 &ordered_offset,
8231 ordered_bytes,
8232 uptodate)) {
8233 btrfs_init_work(&ordered->work, func,
8234 finish_ordered_fn,
8235 NULL, NULL);
8236 btrfs_queue_work(wq, &ordered->work);
8237 }
8238 /*
8239 * If btrfs_dec_test_ordered_pending does not find any ordered
8240 * extent in the range, we can exit.
8241 */
8242 if (ordered_offset == last_offset)
8243 return;
8244 /*
8245 * Our bio might span multiple ordered extents. In this case
Andrea Gelmini52042d82018-11-28 12:05:13 +01008246 * we keep going until we have accounted the whole dio.
Nikolay Borisovb25f0d02018-04-11 11:21:17 +03008247 */
8248 if (ordered_offset < offset + bytes) {
8249 ordered_bytes = offset + bytes - ordered_offset;
8250 ordered = NULL;
8251 }
Chris Mason163cf092010-11-28 19:56:33 -05008252 }
Filipe Manana14543772015-11-24 16:23:54 +00008253}
8254
8255static void btrfs_endio_direct_write(struct bio *bio)
8256{
8257 struct btrfs_dio_private *dip = bio->bi_private;
8258 struct bio *dio_bio = dip->dio_bio;
8259
Qu Wenruo524272602017-03-08 10:25:52 +08008260 __endio_write_update_ordered(dip->inode, dip->logical_offset,
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008261 dip->bytes, !bio->bi_status);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008262
Josef Bacik4b46fce2010-05-23 11:00:55 -04008263 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04008264
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008265 dio_bio->bi_status = bio->bi_status;
Christoph Hellwig40553512017-06-03 09:37:58 +02008266 dio_end_io(dio_bio);
Chris Mason9be33952013-05-17 18:30:14 -04008267 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008268}
8269
David Sterbad0ee3932018-03-08 14:35:48 +01008270static blk_status_t btrfs_submit_bio_start_direct_io(void *private_data,
David Sterbad0779292018-03-08 13:47:33 +01008271 struct bio *bio, u64 offset)
Chris Masoneaf25d92010-05-25 09:48:28 -04008272{
Josef Bacikc6100a42017-05-05 11:57:13 -04008273 struct inode *inode = private_data;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008274 blk_status_t ret;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008275 ret = btrfs_csum_one_bio(inode, bio, offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008276 BUG_ON(ret); /* -ENOMEM */
Chris Masoneaf25d92010-05-25 09:48:28 -04008277 return 0;
8278}
8279
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008280static void btrfs_end_dio_bio(struct bio *bio)
Miao Xiee65e1532010-11-22 03:04:43 +00008281{
8282 struct btrfs_dio_private *dip = bio->bi_private;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008283 blk_status_t err = bio->bi_status;
Miao Xiee65e1532010-11-22 03:04:43 +00008284
Miao Xie8b110e32014-09-12 18:44:03 +08008285 if (err)
8286 btrfs_warn(BTRFS_I(dip->inode)->root->fs_info,
Mike Christie6296b962016-06-05 14:32:21 -05008287 "direct IO failed ino %llu rw %d,%u sector %#Lx len %u err no %d",
David Sterbaf85b7372017-01-20 14:54:07 +01008288 btrfs_ino(BTRFS_I(dip->inode)), bio_op(bio),
8289 bio->bi_opf,
Miao Xie8b110e32014-09-12 18:44:03 +08008290 (unsigned long long)bio->bi_iter.bi_sector,
8291 bio->bi_iter.bi_size, err);
8292
8293 if (dip->subio_endio)
8294 err = dip->subio_endio(dip->inode, btrfs_io_bio(bio), err);
Miao Xiec1dc0892014-09-12 18:43:56 +08008295
8296 if (err) {
Miao Xiee65e1532010-11-22 03:04:43 +00008297 /*
Nikolay Borisovde224b72018-02-14 10:53:36 +02008298 * We want to perceive the errors flag being set before
8299 * decrementing the reference count. We don't need a barrier
8300 * since atomic operations with a return value are fully
8301 * ordered as per atomic_t.txt
Miao Xiee65e1532010-11-22 03:04:43 +00008302 */
Nikolay Borisovde224b72018-02-14 10:53:36 +02008303 dip->errors = 1;
Miao Xiee65e1532010-11-22 03:04:43 +00008304 }
8305
8306 /* if there are more bios still pending for this dio, just exit */
8307 if (!atomic_dec_and_test(&dip->pending_bios))
8308 goto out;
8309
Chris Mason9be33952013-05-17 18:30:14 -04008310 if (dip->errors) {
Miao Xiee65e1532010-11-22 03:04:43 +00008311 bio_io_error(dip->orig_bio);
Chris Mason9be33952013-05-17 18:30:14 -04008312 } else {
Anand Jain2dbe0c72017-10-14 08:35:56 +08008313 dip->dio_bio->bi_status = BLK_STS_OK;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008314 bio_endio(dip->orig_bio);
Miao Xiee65e1532010-11-22 03:04:43 +00008315 }
8316out:
8317 bio_put(bio);
8318}
8319
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008320static inline blk_status_t btrfs_lookup_and_bind_dio_csum(struct inode *inode,
Miao Xiec1dc0892014-09-12 18:43:56 +08008321 struct btrfs_dio_private *dip,
8322 struct bio *bio,
8323 u64 file_offset)
8324{
8325 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
8326 struct btrfs_io_bio *orig_io_bio = btrfs_io_bio(dip->orig_bio);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008327 blk_status_t ret;
Miao Xiec1dc0892014-09-12 18:43:56 +08008328
8329 /*
8330 * We load all the csum data we need when we submit
8331 * the first bio to reduce the csum tree search and
8332 * contention.
8333 */
8334 if (dip->logical_offset == file_offset) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008335 ret = btrfs_lookup_bio_sums_dio(inode, dip->orig_bio,
Miao Xiec1dc0892014-09-12 18:43:56 +08008336 file_offset);
8337 if (ret)
8338 return ret;
8339 }
8340
8341 if (bio == dip->orig_bio)
8342 return 0;
8343
8344 file_offset -= dip->logical_offset;
8345 file_offset >>= inode->i_sb->s_blocksize_bits;
8346 io_bio->csum = (u8 *)(((u32 *)orig_io_bio->csum) + file_offset);
8347
8348 return 0;
8349}
8350
David Sterbad0ee3932018-03-08 14:35:48 +01008351static inline blk_status_t btrfs_submit_dio_bio(struct bio *bio,
8352 struct inode *inode, u64 file_offset, int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00008353{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008354 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xiefacc8a222013-07-25 19:22:34 +08008355 struct btrfs_dio_private *dip = bio->bi_private;
Mike Christie37226b22016-06-05 14:31:52 -05008356 bool write = bio_op(bio) == REQ_OP_WRITE;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008357 blk_status_t ret;
Miao Xiee65e1532010-11-22 03:04:43 +00008358
Liu Bo4c274bc2017-11-01 17:19:27 -06008359 /* Check btrfs_submit_bio_hook() for rules about async submit. */
Josef Bacikb812ce22012-11-16 13:56:32 -05008360 if (async_submit)
8361 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
8362
Josef Bacik5fd02042012-05-02 14:00:54 -04008363 if (!write) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008364 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DATA);
Josef Bacik5fd02042012-05-02 14:00:54 -04008365 if (ret)
8366 goto err;
8367 }
Miao Xiee65e1532010-11-22 03:04:43 +00008368
Nikolay Borisove6961ca2017-08-03 15:44:58 +03008369 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Josef Bacik1ae39932011-04-06 14:41:34 -04008370 goto map;
8371
8372 if (write && async_submit) {
Josef Bacikc6100a42017-05-05 11:57:13 -04008373 ret = btrfs_wq_submit_bio(fs_info, bio, 0, 0,
8374 file_offset, inode,
David Sterbae288c082018-07-18 17:36:24 +02008375 btrfs_submit_bio_start_direct_io);
Miao Xiee65e1532010-11-22 03:04:43 +00008376 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04008377 } else if (write) {
8378 /*
8379 * If we aren't doing async submit, calculate the csum of the
8380 * bio now.
8381 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008382 ret = btrfs_csum_one_bio(inode, bio, file_offset, 1);
Josef Bacik1ae39932011-04-06 14:41:34 -04008383 if (ret)
8384 goto err;
Miao Xie23ea8e52014-09-12 18:43:54 +08008385 } else {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008386 ret = btrfs_lookup_and_bind_dio_csum(inode, dip, bio,
Miao Xiec1dc0892014-09-12 18:43:56 +08008387 file_offset);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00008388 if (ret)
8389 goto err;
8390 }
Josef Bacik1ae39932011-04-06 14:41:34 -04008391map:
Liu Bo9b4a9b22017-08-18 11:54:02 -06008392 ret = btrfs_map_bio(fs_info, bio, 0, 0);
Miao Xiee65e1532010-11-22 03:04:43 +00008393err:
Miao Xiee65e1532010-11-22 03:04:43 +00008394 return ret;
8395}
8396
Nikolay Borisove6961ca2017-08-03 15:44:58 +03008397static int btrfs_submit_direct_hook(struct btrfs_dio_private *dip)
Miao Xiee65e1532010-11-22 03:04:43 +00008398{
8399 struct inode *inode = dip->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008400 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xiee65e1532010-11-22 03:04:43 +00008401 struct bio *bio;
8402 struct bio *orig_bio = dip->orig_bio;
Kent Overstreet4f024f32013-10-11 15:44:27 -07008403 u64 start_sector = orig_bio->bi_iter.bi_sector;
Miao Xiee65e1532010-11-22 03:04:43 +00008404 u64 file_offset = dip->logical_offset;
Josef Bacik1ae39932011-04-06 14:41:34 -04008405 int async_submit = 0;
Liu Bo725130b2017-05-16 09:51:39 -07008406 u64 submit_len;
8407 int clone_offset = 0;
8408 int clone_len;
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05308409 int ret;
Omar Sandoval58efbc92017-08-22 23:45:59 -07008410 blk_status_t status;
Nikolay Borisov89b798a2019-06-03 12:05:05 +03008411 struct btrfs_io_geometry geom;
Miao Xiee65e1532010-11-22 03:04:43 +00008412
Nikolay Borisov89b798a2019-06-03 12:05:05 +03008413 submit_len = orig_bio->bi_iter.bi_size;
8414 ret = btrfs_get_io_geometry(fs_info, btrfs_op(orig_bio),
8415 start_sector << 9, submit_len, &geom);
Miao Xie7a5c3c92014-06-17 18:58:59 +08008416 if (ret)
Miao Xiee65e1532010-11-22 03:04:43 +00008417 return -EIO;
Miao Xiefacc8a222013-07-25 19:22:34 +08008418
Nikolay Borisov89b798a2019-06-03 12:05:05 +03008419 if (geom.len >= submit_len) {
Josef Bacik02f57c72011-04-06 14:25:44 -04008420 bio = orig_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08008421 dip->flags |= BTRFS_DIO_ORIG_BIO_SUBMITTED;
Josef Bacik02f57c72011-04-06 14:25:44 -04008422 goto submit;
8423 }
8424
David Woodhouse53b381b2013-01-29 18:40:14 -05008425 /* async crcs make it difficult to collect full stripe writes. */
Jeff Mahoney1b868262017-05-17 11:38:35 -04008426 if (btrfs_data_alloc_profile(fs_info) & BTRFS_BLOCK_GROUP_RAID56_MASK)
David Woodhouse53b381b2013-01-29 18:40:14 -05008427 async_submit = 0;
8428 else
8429 async_submit = 1;
8430
Liu Bo725130b2017-05-16 09:51:39 -07008431 /* bio split */
Nikolay Borisov89b798a2019-06-03 12:05:05 +03008432 ASSERT(geom.len <= INT_MAX);
Josef Bacik02f57c72011-04-06 14:25:44 -04008433 atomic_inc(&dip->pending_bios);
Arnd Bergmann3c91ee62017-05-18 15:33:29 +02008434 do {
Nikolay Borisov89b798a2019-06-03 12:05:05 +03008435 clone_len = min_t(int, submit_len, geom.len);
Josef Bacik02f57c72011-04-06 14:25:44 -04008436
Liu Bo725130b2017-05-16 09:51:39 -07008437 /*
8438 * This will never fail as it's passing GPF_NOFS and
8439 * the allocation is backed by btrfs_bioset.
8440 */
Liu Boe4770942017-05-16 10:57:14 -07008441 bio = btrfs_bio_clone_partial(orig_bio, clone_offset,
Liu Bo725130b2017-05-16 09:51:39 -07008442 clone_len);
8443 bio->bi_private = dip;
8444 bio->bi_end_io = btrfs_end_dio_bio;
8445 btrfs_io_bio(bio)->logical = file_offset;
Miao Xiee65e1532010-11-22 03:04:43 +00008446
Liu Bo725130b2017-05-16 09:51:39 -07008447 ASSERT(submit_len >= clone_len);
8448 submit_len -= clone_len;
8449 if (submit_len == 0)
8450 break;
Miao Xiee65e1532010-11-22 03:04:43 +00008451
Liu Bo725130b2017-05-16 09:51:39 -07008452 /*
8453 * Increase the count before we submit the bio so we know
8454 * the end IO handler won't happen before we increase the
8455 * count. Otherwise, the dip might get freed before we're
8456 * done setting it up.
8457 */
8458 atomic_inc(&dip->pending_bios);
Miao Xiee65e1532010-11-22 03:04:43 +00008459
David Sterbad0ee3932018-03-08 14:35:48 +01008460 status = btrfs_submit_dio_bio(bio, inode, file_offset,
Omar Sandoval58efbc92017-08-22 23:45:59 -07008461 async_submit);
8462 if (status) {
Liu Bo725130b2017-05-16 09:51:39 -07008463 bio_put(bio);
8464 atomic_dec(&dip->pending_bios);
8465 goto out_err;
Miao Xiee65e1532010-11-22 03:04:43 +00008466 }
Liu Bo725130b2017-05-16 09:51:39 -07008467
8468 clone_offset += clone_len;
8469 start_sector += clone_len >> 9;
8470 file_offset += clone_len;
8471
Nikolay Borisov89b798a2019-06-03 12:05:05 +03008472 ret = btrfs_get_io_geometry(fs_info, btrfs_op(orig_bio),
8473 start_sector << 9, submit_len, &geom);
Liu Bo725130b2017-05-16 09:51:39 -07008474 if (ret)
8475 goto out_err;
Arnd Bergmann3c91ee62017-05-18 15:33:29 +02008476 } while (submit_len > 0);
Miao Xiee65e1532010-11-22 03:04:43 +00008477
Josef Bacik02f57c72011-04-06 14:25:44 -04008478submit:
David Sterbad0ee3932018-03-08 14:35:48 +01008479 status = btrfs_submit_dio_bio(bio, inode, file_offset, async_submit);
Omar Sandoval58efbc92017-08-22 23:45:59 -07008480 if (!status)
Miao Xiee65e1532010-11-22 03:04:43 +00008481 return 0;
8482
8483 bio_put(bio);
8484out_err:
8485 dip->errors = 1;
8486 /*
Nikolay Borisovde224b72018-02-14 10:53:36 +02008487 * Before atomic variable goto zero, we must make sure dip->errors is
8488 * perceived to be set. This ordering is ensured by the fact that an
8489 * atomic operations with a return value are fully ordered as per
8490 * atomic_t.txt
Miao Xiee65e1532010-11-22 03:04:43 +00008491 */
Miao Xiee65e1532010-11-22 03:04:43 +00008492 if (atomic_dec_and_test(&dip->pending_bios))
8493 bio_io_error(dip->orig_bio);
8494
8495 /* bio_end_io() will handle error, so we needn't return it */
8496 return 0;
8497}
8498
Mike Christie8a4c1e42016-06-05 14:31:50 -05008499static void btrfs_submit_direct(struct bio *dio_bio, struct inode *inode,
8500 loff_t file_offset)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008501{
Filipe Manana61de7182015-07-01 12:13:10 +01008502 struct btrfs_dio_private *dip = NULL;
Liu Bo3892ac92017-04-17 15:00:28 -07008503 struct bio *bio = NULL;
8504 struct btrfs_io_bio *io_bio;
Mike Christie8a4c1e42016-06-05 14:31:50 -05008505 bool write = (bio_op(dio_bio) == REQ_OP_WRITE);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008506 int ret = 0;
8507
David Sterba8b6c1d52017-06-02 17:48:13 +02008508 bio = btrfs_bio_clone(dio_bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008509
Miao Xiec1dc0892014-09-12 18:43:56 +08008510 dip = kzalloc(sizeof(*dip), GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04008511 if (!dip) {
8512 ret = -ENOMEM;
Filipe Manana61de7182015-07-01 12:13:10 +01008513 goto free_ordered;
Chris Mason9be33952013-05-17 18:30:14 -04008514 }
8515
8516 dip->private = dio_bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008517 dip->inode = inode;
8518 dip->logical_offset = file_offset;
Kent Overstreet4f024f32013-10-11 15:44:27 -07008519 dip->bytes = dio_bio->bi_iter.bi_size;
8520 dip->disk_bytenr = (u64)dio_bio->bi_iter.bi_sector << 9;
Liu Bo3892ac92017-04-17 15:00:28 -07008521 bio->bi_private = dip;
8522 dip->orig_bio = bio;
Chris Mason9be33952013-05-17 18:30:14 -04008523 dip->dio_bio = dio_bio;
Miao Xiee65e1532010-11-22 03:04:43 +00008524 atomic_set(&dip->pending_bios, 0);
Liu Bo3892ac92017-04-17 15:00:28 -07008525 io_bio = btrfs_io_bio(bio);
8526 io_bio->logical = file_offset;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008527
Miao Xiec1dc0892014-09-12 18:43:56 +08008528 if (write) {
Liu Bo3892ac92017-04-17 15:00:28 -07008529 bio->bi_end_io = btrfs_endio_direct_write;
Miao Xiec1dc0892014-09-12 18:43:56 +08008530 } else {
Liu Bo3892ac92017-04-17 15:00:28 -07008531 bio->bi_end_io = btrfs_endio_direct_read;
Miao Xiec1dc0892014-09-12 18:43:56 +08008532 dip->subio_endio = btrfs_subio_endio_read;
8533 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04008534
Filipe Mananaf28a4922015-12-08 19:23:20 +00008535 /*
8536 * Reset the range for unsubmitted ordered extents (to a 0 length range)
8537 * even if we fail to submit a bio, because in such case we do the
8538 * corresponding error handling below and it must not be done a second
8539 * time by btrfs_direct_IO().
8540 */
8541 if (write) {
8542 struct btrfs_dio_data *dio_data = current->journal_info;
8543
8544 dio_data->unsubmitted_oe_range_end = dip->logical_offset +
8545 dip->bytes;
8546 dio_data->unsubmitted_oe_range_start =
8547 dio_data->unsubmitted_oe_range_end;
8548 }
8549
Nikolay Borisove6961ca2017-08-03 15:44:58 +03008550 ret = btrfs_submit_direct_hook(dip);
Miao Xiee65e1532010-11-22 03:04:43 +00008551 if (!ret)
Chris Masoneaf25d92010-05-25 09:48:28 -04008552 return;
Chris Mason9be33952013-05-17 18:30:14 -04008553
David Sterbab3a0dd52018-11-22 17:16:49 +01008554 btrfs_io_bio_free_csum(io_bio);
Chris Mason9be33952013-05-17 18:30:14 -04008555
Josef Bacik4b46fce2010-05-23 11:00:55 -04008556free_ordered:
8557 /*
Filipe Manana61de7182015-07-01 12:13:10 +01008558 * If we arrived here it means either we failed to submit the dip
8559 * or we either failed to clone the dio_bio or failed to allocate the
8560 * dip. If we cloned the dio_bio and allocated the dip, we can just
8561 * call bio_endio against our io_bio so that we get proper resource
8562 * cleanup if we fail to submit the dip, otherwise, we must do the
8563 * same as btrfs_endio_direct_[write|read] because we can't call these
8564 * callbacks - they require an allocated dip and a clone of dio_bio.
Josef Bacik4b46fce2010-05-23 11:00:55 -04008565 */
Liu Bo3892ac92017-04-17 15:00:28 -07008566 if (bio && dip) {
Guoqing Jiang054ec2f2017-06-02 16:08:50 +08008567 bio_io_error(bio);
Filipe Manana61de7182015-07-01 12:13:10 +01008568 /*
Liu Bo3892ac92017-04-17 15:00:28 -07008569 * The end io callbacks free our dip, do the final put on bio
Filipe Manana61de7182015-07-01 12:13:10 +01008570 * and all the cleanup and final put for dio_bio (through
8571 * dio_end_io()).
8572 */
8573 dip = NULL;
Liu Bo3892ac92017-04-17 15:00:28 -07008574 bio = NULL;
Filipe Manana61de7182015-07-01 12:13:10 +01008575 } else {
Filipe Manana14543772015-11-24 16:23:54 +00008576 if (write)
Qu Wenruo524272602017-03-08 10:25:52 +08008577 __endio_write_update_ordered(inode,
Filipe Manana14543772015-11-24 16:23:54 +00008578 file_offset,
8579 dio_bio->bi_iter.bi_size,
Qu Wenruo524272602017-03-08 10:25:52 +08008580 false);
Filipe Manana14543772015-11-24 16:23:54 +00008581 else
Filipe Manana61de7182015-07-01 12:13:10 +01008582 unlock_extent(&BTRFS_I(inode)->io_tree, file_offset,
8583 file_offset + dio_bio->bi_iter.bi_size - 1);
Filipe Manana14543772015-11-24 16:23:54 +00008584
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008585 dio_bio->bi_status = BLK_STS_IOERR;
Filipe Manana61de7182015-07-01 12:13:10 +01008586 /*
8587 * Releases and cleans up our dio_bio, no need to bio_put()
8588 * nor bio_endio()/bio_io_error() against dio_bio.
8589 */
Christoph Hellwig40553512017-06-03 09:37:58 +02008590 dio_end_io(dio_bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008591 }
Liu Bo3892ac92017-04-17 15:00:28 -07008592 if (bio)
8593 bio_put(bio);
Filipe Manana61de7182015-07-01 12:13:10 +01008594 kfree(dip);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008595}
8596
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008597static ssize_t check_direct_IO(struct btrfs_fs_info *fs_info,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008598 const struct iov_iter *iter, loff_t offset)
Chris Mason5a5f79b2010-05-26 21:33:37 -04008599{
8600 int seg;
Josef Bacika1b75f72011-04-08 15:51:18 +00008601 int i;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008602 unsigned int blocksize_mask = fs_info->sectorsize - 1;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008603 ssize_t retval = -EINVAL;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008604
8605 if (offset & blocksize_mask)
8606 goto out;
8607
Al Viro28060d52014-03-22 05:15:17 -04008608 if (iov_iter_alignment(iter) & blocksize_mask)
8609 goto out;
Josef Bacika1b75f72011-04-08 15:51:18 +00008610
Al Viro28060d52014-03-22 05:15:17 -04008611 /* If this is a write we don't need to check anymore */
Al Virocd27e452016-10-10 13:39:05 -04008612 if (iov_iter_rw(iter) != READ || !iter_is_iovec(iter))
Al Viro28060d52014-03-22 05:15:17 -04008613 return 0;
8614 /*
8615 * Check to make sure we don't have duplicate iov_base's in this
8616 * iovec, if so return EINVAL, otherwise we'll get csum errors
8617 * when reading back.
8618 */
8619 for (seg = 0; seg < iter->nr_segs; seg++) {
8620 for (i = seg + 1; i < iter->nr_segs; i++) {
8621 if (iter->iov[seg].iov_base == iter->iov[i].iov_base)
Josef Bacika1b75f72011-04-08 15:51:18 +00008622 goto out;
8623 }
Chris Mason5a5f79b2010-05-26 21:33:37 -04008624 }
8625 retval = 0;
8626out:
8627 return retval;
8628}
Josef Bacikeb838e72012-07-31 16:28:48 -04008629
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008630static ssize_t btrfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
Chris Mason16432982008-04-10 10:23:21 -04008631{
Josef Bacik4b46fce2010-05-23 11:00:55 -04008632 struct file *file = iocb->ki_filp;
8633 struct inode *inode = file->f_mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008634 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
chandan50745b02015-08-28 21:10:13 +05308635 struct btrfs_dio_data dio_data = { 0 };
Qu Wenruo364ecf32017-02-27 15:10:38 +08008636 struct extent_changeset *data_reserved = NULL;
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008637 loff_t offset = iocb->ki_pos;
Miao Xie09348562013-02-07 10:12:07 +00008638 size_t count = 0;
Miao Xie2e60a512013-02-08 07:01:08 +00008639 int flags = 0;
Miao Xie38851cc2013-02-08 07:04:11 +00008640 bool wakeup = true;
8641 bool relock = false;
Miao Xie09348562013-02-07 10:12:07 +00008642 ssize_t ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008643
Nikolay Borisov8c70c9f2017-08-21 12:43:46 +03008644 if (check_direct_IO(fs_info, iter, offset))
Chris Mason5a5f79b2010-05-26 21:33:37 -04008645 return 0;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008646
Jens Axboefe0f07d2015-04-15 17:05:48 -06008647 inode_dio_begin(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008648
Josef Bacik0e267c42013-07-02 10:38:02 -04008649 /*
Miao Xie41bd9ca2014-03-06 13:54:57 +08008650 * The generic stuff only does filemap_write_and_wait_range, which
8651 * isn't enough if we've written compressed pages to this area, so
8652 * we need to flush the dirty pages again to make absolutely sure
8653 * that any outstanding dirty pages are on disk.
Josef Bacik0e267c42013-07-02 10:38:02 -04008654 */
Al Viroa6cbcd42014-03-04 22:38:00 -05008655 count = iov_iter_count(iter);
Miao Xie41bd9ca2014-03-06 13:54:57 +08008656 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
8657 &BTRFS_I(inode)->runtime_flags))
Wang Shilong9a025a02014-07-17 11:44:13 +08008658 filemap_fdatawrite_range(inode->i_mapping, offset,
8659 offset + count - 1);
Josef Bacik0e267c42013-07-02 10:38:02 -04008660
Omar Sandoval6f673762015-03-16 04:33:52 -07008661 if (iov_iter_rw(iter) == WRITE) {
Miao Xie38851cc2013-02-08 07:04:11 +00008662 /*
8663 * If the write DIO is beyond the EOF, we need update
8664 * the isize, but it is protected by i_mutex. So we can
8665 * not unlock the i_mutex at this case.
8666 */
8667 if (offset + count <= inode->i_size) {
Liu Bo4aaedfb2016-12-14 22:36:05 -08008668 dio_data.overwrite = 1;
Al Viro59551022016-01-22 15:40:57 -05008669 inode_unlock(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008670 relock = true;
Goldwyn Rodriguesedf064e2017-06-20 07:05:49 -05008671 } else if (iocb->ki_flags & IOCB_NOWAIT) {
8672 ret = -EAGAIN;
8673 goto out;
Miao Xie38851cc2013-02-08 07:04:11 +00008674 }
Qu Wenruo364ecf32017-02-27 15:10:38 +08008675 ret = btrfs_delalloc_reserve_space(inode, &data_reserved,
8676 offset, count);
Miao Xie09348562013-02-07 10:12:07 +00008677 if (ret)
Miao Xie38851cc2013-02-08 07:04:11 +00008678 goto out;
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008679
8680 /*
8681 * We need to know how many extents we reserved so that we can
8682 * do the accounting properly if we go over the number we
8683 * originally calculated. Abuse current->journal_info for this.
8684 */
Jeff Mahoneyda170662016-06-15 09:22:56 -04008685 dio_data.reserve = round_up(count,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008686 fs_info->sectorsize);
Filipe Mananaf28a4922015-12-08 19:23:20 +00008687 dio_data.unsubmitted_oe_range_start = (u64)offset;
8688 dio_data.unsubmitted_oe_range_end = (u64)offset;
chandan50745b02015-08-28 21:10:13 +05308689 current->journal_info = &dio_data;
Chandan Rajendra97dcdea2016-12-23 15:00:18 +05308690 down_read(&BTRFS_I(inode)->dio_sem);
David Sterbaee39b432014-09-30 01:33:33 +02008691 } else if (test_bit(BTRFS_INODE_READDIO_NEED_LOCK,
8692 &BTRFS_I(inode)->runtime_flags)) {
Jens Axboefe0f07d2015-04-15 17:05:48 -06008693 inode_dio_end(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008694 flags = DIO_LOCKING | DIO_SKIP_HOLES;
8695 wakeup = false;
Miao Xie09348562013-02-07 10:12:07 +00008696 }
8697
Omar Sandoval17f8c842015-03-16 04:33:50 -07008698 ret = __blockdev_direct_IO(iocb, inode,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008699 fs_info->fs_devices->latest_bdev,
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008700 iter, btrfs_get_blocks_direct, NULL,
Omar Sandoval17f8c842015-03-16 04:33:50 -07008701 btrfs_submit_direct, flags);
Omar Sandoval6f673762015-03-16 04:33:52 -07008702 if (iov_iter_rw(iter) == WRITE) {
Chandan Rajendra97dcdea2016-12-23 15:00:18 +05308703 up_read(&BTRFS_I(inode)->dio_sem);
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008704 current->journal_info = NULL;
Liu Boddba1bf2015-06-17 16:59:58 +08008705 if (ret < 0 && ret != -EIOCBQUEUED) {
chandan50745b02015-08-28 21:10:13 +05308706 if (dio_data.reserve)
Qu Wenruobc42bda2017-02-27 15:10:39 +08008707 btrfs_delalloc_release_space(inode, data_reserved,
Qu Wenruo43b18592017-12-12 15:34:32 +08008708 offset, dio_data.reserve, true);
Filipe Mananaf28a4922015-12-08 19:23:20 +00008709 /*
8710 * On error we might have left some ordered extents
8711 * without submitting corresponding bios for them, so
8712 * cleanup them up to avoid other tasks getting them
8713 * and waiting for them to complete forever.
8714 */
8715 if (dio_data.unsubmitted_oe_range_start <
8716 dio_data.unsubmitted_oe_range_end)
Qu Wenruo524272602017-03-08 10:25:52 +08008717 __endio_write_update_ordered(inode,
Filipe Mananaf28a4922015-12-08 19:23:20 +00008718 dio_data.unsubmitted_oe_range_start,
8719 dio_data.unsubmitted_oe_range_end -
8720 dio_data.unsubmitted_oe_range_start,
Qu Wenruo524272602017-03-08 10:25:52 +08008721 false);
Liu Boddba1bf2015-06-17 16:59:58 +08008722 } else if (ret >= 0 && (size_t)ret < count)
Qu Wenruobc42bda2017-02-27 15:10:39 +08008723 btrfs_delalloc_release_space(inode, data_reserved,
Qu Wenruo43b18592017-12-12 15:34:32 +08008724 offset, count - (size_t)ret, true);
Qu Wenruo8702ba92019-10-14 14:34:51 +08008725 btrfs_delalloc_release_extents(BTRFS_I(inode), count);
Miao Xie09348562013-02-07 10:12:07 +00008726 }
Miao Xie38851cc2013-02-08 07:04:11 +00008727out:
Miao Xie2e60a512013-02-08 07:01:08 +00008728 if (wakeup)
Jens Axboefe0f07d2015-04-15 17:05:48 -06008729 inode_dio_end(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008730 if (relock)
Al Viro59551022016-01-22 15:40:57 -05008731 inode_lock(inode);
Miao Xie09348562013-02-07 10:12:07 +00008732
Qu Wenruo364ecf32017-02-27 15:10:38 +08008733 extent_changeset_free(data_reserved);
Miao Xie09348562013-02-07 10:12:07 +00008734 return ret;
Chris Mason16432982008-04-10 10:23:21 -04008735}
8736
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008737#define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
8738
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008739static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
8740 __u64 start, __u64 len)
8741{
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008742 int ret;
8743
8744 ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS);
8745 if (ret)
8746 return ret;
8747
David Sterba2135fb92017-06-23 04:09:57 +02008748 return extent_fiemap(inode, fieinfo, start, len);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008749}
8750
Chris Mason9ebefb182007-06-15 13:50:00 -04008751int btrfs_readpage(struct file *file, struct page *page)
8752{
Chris Masond1310b22008-01-24 16:13:08 -05008753 struct extent_io_tree *tree;
8754 tree = &BTRFS_I(page->mapping->host)->io_tree;
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02008755 return extent_read_full_page(tree, page, btrfs_get_extent, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04008756}
Chris Mason1832a6d2007-12-21 16:27:21 -05008757
Chris Mason39279cc2007-06-12 06:35:45 -04008758static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
8759{
Josef Bacikbe7bd732015-10-22 15:05:09 -04008760 struct inode *inode = page->mapping->host;
8761 int ret;
Chris Masonb888db22007-08-27 16:49:44 -04008762
8763 if (current->flags & PF_MEMALLOC) {
8764 redirty_page_for_writepage(wbc, page);
8765 unlock_page(page);
8766 return 0;
8767 }
Josef Bacikbe7bd732015-10-22 15:05:09 -04008768
8769 /*
8770 * If we are under memory pressure we will call this directly from the
8771 * VM, we need to make sure we have the inode referenced for the ordered
8772 * extent. If not just return like we didn't do anything.
8773 */
8774 if (!igrab(inode)) {
8775 redirty_page_for_writepage(wbc, page);
8776 return AOP_WRITEPAGE_ACTIVATE;
8777 }
Nikolay Borisov0a9b0e52017-12-08 15:55:59 +02008778 ret = extent_write_full_page(page, wbc);
Josef Bacikbe7bd732015-10-22 15:05:09 -04008779 btrfs_add_delayed_iput(inode);
8780 return ret;
Chris Masona52d9a82007-08-27 16:49:44 -04008781}
Chris Mason39279cc2007-06-12 06:35:45 -04008782
Eric Sandeen48a3b632013-04-25 20:41:01 +00008783static int btrfs_writepages(struct address_space *mapping,
8784 struct writeback_control *wbc)
Chris Masonb293f02e2007-11-01 19:45:34 -04008785{
Nikolay Borisov8ae225a2018-04-19 10:46:38 +03008786 return extent_writepages(mapping, wbc);
Chris Masonb293f02e2007-11-01 19:45:34 -04008787}
8788
Chris Mason3ab2fb52007-11-08 10:59:22 -05008789static int
8790btrfs_readpages(struct file *file, struct address_space *mapping,
8791 struct list_head *pages, unsigned nr_pages)
8792{
Nikolay Borisov2a3ff0a2018-04-19 10:46:36 +03008793 return extent_readpages(mapping, pages, nr_pages);
Chris Mason3ab2fb52007-11-08 10:59:22 -05008794}
Nikolay Borisov2a3ff0a2018-04-19 10:46:36 +03008795
Chris Masone6dcd2d2008-07-17 12:53:50 -04008796static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04008797{
Nikolay Borisov477a30b2018-04-19 10:46:34 +03008798 int ret = try_release_extent_mapping(page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04008799 if (ret == 1) {
8800 ClearPagePrivate(page);
8801 set_page_private(page, 0);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008802 put_page(page);
Chris Masona52d9a82007-08-27 16:49:44 -04008803 }
8804 return ret;
8805}
Chris Mason39279cc2007-06-12 06:35:45 -04008806
Chris Masone6dcd2d2008-07-17 12:53:50 -04008807static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
8808{
Chris Mason98509cf2008-09-11 15:51:43 -04008809 if (PageWriteback(page) || PageDirty(page))
8810 return 0;
Michal Hocko3ba7ab22017-01-09 15:39:02 +01008811 return __btrfs_releasepage(page, gfp_flags);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008812}
8813
Lukas Czernerd47992f2013-05-21 23:17:23 -04008814static void btrfs_invalidatepage(struct page *page, unsigned int offset,
8815 unsigned int length)
Chris Masona52d9a82007-08-27 16:49:44 -04008816{
Josef Bacik5fd02042012-05-02 14:00:54 -04008817 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05008818 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008819 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008820 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008821 u64 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008822 u64 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308823 u64 start;
8824 u64 end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008825 int inode_evicting = inode->i_state & I_FREEING;
Chris Masona52d9a82007-08-27 16:49:44 -04008826
Chris Mason8b62b722009-09-02 16:53:46 -04008827 /*
8828 * we have the page locked, so new writeback can't start,
8829 * and the dirty bit won't be cleared while we are here.
8830 *
8831 * Wait for IO on this page so that we can safely clear
8832 * the PagePrivate2 bit and do ordered accounting
8833 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008834 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04008835
Josef Bacik5fd02042012-05-02 14:00:54 -04008836 tree = &BTRFS_I(inode)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008837 if (offset) {
8838 btrfs_releasepage(page, GFP_NOFS);
8839 return;
8840 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008841
8842 if (!inode_evicting)
David Sterbaff13db42015-12-03 14:30:40 +01008843 lock_extent_bits(tree, page_start, page_end, &cached_state);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308844again:
8845 start = page_start;
Nikolay Borisova776c6f2017-02-20 13:50:49 +02008846 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), start,
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308847 page_end - start + 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008848 if (ordered) {
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308849 end = min(page_end, ordered->file_offset + ordered->len - 1);
Chris Masoneb84ae02008-07-17 13:53:27 -04008850 /*
8851 * IO on this page will never be started, so we need
8852 * to account for any ordered extents now
8853 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008854 if (!inode_evicting)
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308855 clear_extent_bit(tree, start, end,
Omar Sandovale1821632019-08-15 14:04:04 -07008856 EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008857 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
David Sterbaae0f1622017-10-31 16:37:52 +01008858 EXTENT_DEFRAG, 1, 0, &cached_state);
Chris Mason8b62b722009-09-02 16:53:46 -04008859 /*
8860 * whoever cleared the private bit is responsible
8861 * for the finish_ordered_io
8862 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04008863 if (TestClearPagePrivate2(page)) {
8864 struct btrfs_ordered_inode_tree *tree;
8865 u64 new_len;
8866
8867 tree = &BTRFS_I(inode)->ordered_tree;
8868
8869 spin_lock_irq(&tree->lock);
8870 set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308871 new_len = start - ordered->file_offset;
Josef Bacik77cef2e2013-08-29 13:57:21 -04008872 if (new_len < ordered->truncated_len)
8873 ordered->truncated_len = new_len;
8874 spin_unlock_irq(&tree->lock);
8875
8876 if (btrfs_dec_test_ordered_pending(inode, &ordered,
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308877 start,
8878 end - start + 1, 1))
Josef Bacik77cef2e2013-08-29 13:57:21 -04008879 btrfs_finish_ordered_io(ordered);
Chris Mason8b62b722009-09-02 16:53:46 -04008880 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008881 btrfs_put_ordered_extent(ordered);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008882 if (!inode_evicting) {
8883 cached_state = NULL;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308884 lock_extent_bits(tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008885 &cached_state);
8886 }
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308887
8888 start = end + 1;
8889 if (start < page_end)
8890 goto again;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008891 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008892
Qu Wenruob9d0b382015-09-29 10:35:16 +08008893 /*
8894 * Qgroup reserved space handler
8895 * Page here will be either
8896 * 1) Already written to disk
8897 * In this case, its reserved space is released from data rsv map
8898 * and will be freed by delayed_ref handler finally.
8899 * So even we call qgroup_free_data(), it won't decrease reserved
8900 * space.
8901 * 2) Not written to disk
Goldwyn Rodrigues0b34c262016-09-30 10:40:52 -05008902 * This means the reserved space should be freed here. However,
8903 * if a truncate invalidates the page (by clearing PageDirty)
8904 * and the page is accounted for while allocating extent
8905 * in btrfs_check_data_free_space() we let delayed_ref to
8906 * free the entire extent.
Qu Wenruob9d0b382015-09-29 10:35:16 +08008907 */
Goldwyn Rodrigues0b34c262016-09-30 10:40:52 -05008908 if (PageDirty(page))
Qu Wenruobc42bda2017-02-27 15:10:39 +08008909 btrfs_qgroup_free_data(inode, NULL, page_start, PAGE_SIZE);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008910 if (!inode_evicting) {
Omar Sandovale1821632019-08-15 14:04:04 -07008911 clear_extent_bit(tree, page_start, page_end, EXTENT_LOCKED |
Filipe Mananaa7e3b972017-04-03 10:45:46 +01008912 EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
8913 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1,
David Sterbaae0f1622017-10-31 16:37:52 +01008914 &cached_state);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008915
8916 __btrfs_releasepage(page, GFP_NOFS);
8917 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008918
Chris Mason4a096752008-07-21 10:29:44 -04008919 ClearPageChecked(page);
Chris Mason9ad6b7bc2008-04-18 16:11:30 -04008920 if (PagePrivate(page)) {
Chris Mason9ad6b7bc2008-04-18 16:11:30 -04008921 ClearPagePrivate(page);
8922 set_page_private(page, 0);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008923 put_page(page);
Chris Mason9ad6b7bc2008-04-18 16:11:30 -04008924 }
Chris Mason39279cc2007-06-12 06:35:45 -04008925}
8926
Chris Mason9ebefb182007-06-15 13:50:00 -04008927/*
8928 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
8929 * called from a page fault handler when a page is first dirtied. Hence we must
8930 * be careful to check for EOF conditions here. We set the page up correctly
8931 * for a written page which means we get ENOSPC checking when writing into
8932 * holes and correct delalloc and unwritten extent mapping on filesystems that
8933 * support these features.
8934 *
8935 * We are not allowed to take the i_mutex here so we have to play games to
8936 * protect against truncate races as the page could now be beyond EOF. Because
Omar Sandovald1342aa2018-05-11 13:13:29 -07008937 * truncate_setsize() writes the inode size before removing pages, once we have
8938 * the page lock we can determine safely if the page is beyond EOF. If it is not
Chris Mason9ebefb182007-06-15 13:50:00 -04008939 * beyond EOF, then the page is guaranteed safe against truncation until we
8940 * unlock the page.
8941 */
Souptick Joardera528a242018-06-06 19:54:44 +05308942vm_fault_t btrfs_page_mkwrite(struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04008943{
Nick Pigginc2ec1752009-03-31 15:23:21 -07008944 struct page *page = vmf->page;
Dave Jiang11bac802017-02-24 14:56:41 -08008945 struct inode *inode = file_inode(vmf->vma->vm_file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008946 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008947 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
8948 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008949 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08008950 struct extent_changeset *data_reserved = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008951 char *kaddr;
8952 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04008953 loff_t size;
Souptick Joardera528a242018-06-06 19:54:44 +05308954 vm_fault_t ret;
8955 int ret2;
Chris Mason9998eb72012-01-25 13:47:40 -05008956 int reserved = 0;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308957 u64 reserved_space;
Chris Masona52d9a82007-08-27 16:49:44 -04008958 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008959 u64 page_end;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308960 u64 end;
8961
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008962 reserved_space = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04008963
Jan Karab2b5ef52012-06-12 16:20:45 +02008964 sb_start_pagefault(inode->i_sb);
Qu Wenruodf480632015-09-08 17:25:54 +08008965 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008966 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308967 end = page_end;
Qu Wenruodf480632015-09-08 17:25:54 +08008968
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308969 /*
8970 * Reserving delalloc space after obtaining the page lock can lead to
8971 * deadlock. For example, if a dirty page is locked by this function
8972 * and the call to btrfs_delalloc_reserve_space() ends up triggering
8973 * dirty page write out, then the btrfs_writepage() function could
8974 * end up waiting indefinitely to get a lock on the page currently
8975 * being processed by btrfs_page_mkwrite() function.
8976 */
Souptick Joardera528a242018-06-06 19:54:44 +05308977 ret2 = btrfs_delalloc_reserve_space(inode, &data_reserved, page_start,
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308978 reserved_space);
Souptick Joardera528a242018-06-06 19:54:44 +05308979 if (!ret2) {
8980 ret2 = file_update_time(vmf->vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05008981 reserved = 1;
8982 }
Souptick Joardera528a242018-06-06 19:54:44 +05308983 if (ret2) {
8984 ret = vmf_error(ret2);
Chris Mason9998eb72012-01-25 13:47:40 -05008985 if (reserved)
8986 goto out;
8987 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07008988 }
Chris Mason1832a6d2007-12-21 16:27:21 -05008989
Nick Piggin56a76f82009-03-31 15:23:23 -07008990 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008991again:
Chris Mason9ebefb182007-06-15 13:50:00 -04008992 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04008993 size = i_size_read(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04008994
Chris Mason9ebefb182007-06-15 13:50:00 -04008995 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04008996 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04008997 /* page got truncated out from underneath us */
8998 goto out_unlock;
8999 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04009000 wait_on_page_writeback(page);
9001
David Sterbaff13db42015-12-03 14:30:40 +01009002 lock_extent_bits(io_tree, page_start, page_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009003 set_page_extent_mapped(page);
9004
Chris Masoneb84ae02008-07-17 13:53:27 -04009005 /*
9006 * we can't set the delalloc bits if there are pending ordered
9007 * extents. Drop our locks and wait for them to finish
9008 */
Nikolay Borisova776c6f2017-02-20 13:50:49 +02009009 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start,
9010 PAGE_SIZE);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009011 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00009012 unlock_extent_cached(io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01009013 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009014 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04009015 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009016 btrfs_put_ordered_extent(ordered);
9017 goto again;
9018 }
9019
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009020 if (page->index == ((size - 1) >> PAGE_SHIFT)) {
Jeff Mahoneyda170662016-06-15 09:22:56 -04009021 reserved_space = round_up(size - page_start,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009022 fs_info->sectorsize);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009023 if (reserved_space < PAGE_SIZE) {
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309024 end = page_start + reserved_space - 1;
Qu Wenruobc42bda2017-02-27 15:10:39 +08009025 btrfs_delalloc_release_space(inode, data_reserved,
Qu Wenruo43b18592017-12-12 15:34:32 +08009026 page_start, PAGE_SIZE - reserved_space,
9027 true);
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309028 }
9029 }
9030
Josef Bacikfbf19082009-10-01 17:10:23 -04009031 /*
Liu Bo54160342016-12-13 12:15:19 -08009032 * page_mkwrite gets called when the page is firstly dirtied after it's
9033 * faulted in, but write(2) could also dirty a page and set delalloc
9034 * bits, thus in this case for space account reason, we still need to
9035 * clear any delalloc bits within this page range since we have to
9036 * reserve data&meta space before lock_page() (see above comments).
Josef Bacikfbf19082009-10-01 17:10:23 -04009037 */
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309038 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, end,
Omar Sandovale1821632019-08-15 14:04:04 -07009039 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
9040 EXTENT_DEFRAG, 0, 0, &cached_state);
Josef Bacikfbf19082009-10-01 17:10:23 -04009041
Souptick Joardera528a242018-06-06 19:54:44 +05309042 ret2 = btrfs_set_extent_delalloc(inode, page_start, end, 0,
Nikolay Borisov330a5822019-07-17 16:18:17 +03009043 &cached_state);
Souptick Joardera528a242018-06-06 19:54:44 +05309044 if (ret2) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00009045 unlock_extent_cached(io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01009046 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009047 ret = VM_FAULT_SIGBUS;
9048 goto out_unlock;
9049 }
Souptick Joardera528a242018-06-06 19:54:44 +05309050 ret2 = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04009051
9052 /* page is wholly or partially inside EOF */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009053 if (page_start + PAGE_SIZE > size)
Johannes Thumshirn70730172018-12-05 15:23:03 +01009054 zero_start = offset_in_page(size);
Chris Mason9ebefb182007-06-15 13:50:00 -04009055 else
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009056 zero_start = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04009057
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009058 if (zero_start != PAGE_SIZE) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04009059 kaddr = kmap(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009060 memset(kaddr + zero_start, 0, PAGE_SIZE - zero_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009061 flush_dcache_page(page);
9062 kunmap(page);
9063 }
Chris Mason247e7432008-07-17 12:53:51 -04009064 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009065 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04009066 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04009067
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009068 BTRFS_I(inode)->last_trans = fs_info->generation;
Chris Mason257c62e2009-10-13 13:21:08 -04009069 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06009070 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
Chris Mason257c62e2009-10-13 13:21:08 -04009071
David Sterbae43bbe52017-12-12 21:43:52 +01009072 unlock_extent_cached(io_tree, page_start, page_end, &cached_state);
Chris Mason9ebefb182007-06-15 13:50:00 -04009073
Souptick Joardera528a242018-06-06 19:54:44 +05309074 if (!ret2) {
Qu Wenruo8702ba92019-10-14 14:34:51 +08009075 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE);
Jan Karab2b5ef52012-06-12 16:20:45 +02009076 sb_end_pagefault(inode->i_sb);
Qu Wenruo364ecf32017-02-27 15:10:38 +08009077 extent_changeset_free(data_reserved);
Chris Mason50a9b212009-09-11 12:33:12 -04009078 return VM_FAULT_LOCKED;
Jan Karab2b5ef52012-06-12 16:20:45 +02009079 }
Chris Mason717beb92018-06-25 10:03:41 -07009080
9081out_unlock:
Chris Mason9ebefb182007-06-15 13:50:00 -04009082 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05009083out:
Qu Wenruo8702ba92019-10-14 14:34:51 +08009084 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE);
Qu Wenruobc42bda2017-02-27 15:10:39 +08009085 btrfs_delalloc_release_space(inode, data_reserved, page_start,
Qu Wenruo43b18592017-12-12 15:34:32 +08009086 reserved_space, (ret != 0));
Chris Mason9998eb72012-01-25 13:47:40 -05009087out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02009088 sb_end_pagefault(inode->i_sb);
Qu Wenruo364ecf32017-02-27 15:10:38 +08009089 extent_changeset_free(data_reserved);
Chris Mason9ebefb182007-06-15 13:50:00 -04009090 return ret;
9091}
9092
Filipe Manana213e8c52018-02-06 20:40:31 +00009093static int btrfs_truncate(struct inode *inode, bool skip_writeback)
Chris Mason39279cc2007-06-12 06:35:45 -04009094{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009095 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009096 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04009097 struct btrfs_block_rsv *rsv;
Omar Sandovalad7e1a72018-05-22 09:59:50 -07009098 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04009099 struct btrfs_trans_handle *trans;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009100 u64 mask = fs_info->sectorsize - 1;
Josef Bacik2bd36e72019-08-22 15:14:33 -04009101 u64 min_size = btrfs_calc_metadata_size(fs_info, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04009102
Filipe Manana213e8c52018-02-06 20:40:31 +00009103 if (!skip_writeback) {
9104 ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask),
9105 (u64)-1);
9106 if (ret)
9107 return ret;
9108 }
Chris Mason39279cc2007-06-12 06:35:45 -04009109
Josef Bacikfcb80c22011-05-03 10:40:22 -04009110 /*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07009111 * Yes ladies and gentlemen, this is indeed ugly. We have a couple of
9112 * things going on here:
Josef Bacikfcb80c22011-05-03 10:40:22 -04009113 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07009114 * 1) We need to reserve space to update our inode.
Josef Bacikfcb80c22011-05-03 10:40:22 -04009115 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07009116 * 2) We need to have something to cache all the space that is going to
Josef Bacikfcb80c22011-05-03 10:40:22 -04009117 * be free'd up by the truncate operation, but also have some slack
9118 * space reserved in case it uses space during the truncate (thank you
9119 * very much snapshotting).
9120 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07009121 * And we need these to be separate. The fact is we can use a lot of
Josef Bacikfcb80c22011-05-03 10:40:22 -04009122 * space doing the truncate, and we have no earthly idea how much space
Nicholas D Steeves01327612016-05-19 21:18:45 -04009123 * we will use, so we need the truncate reservation to be separate so it
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07009124 * doesn't end up using space reserved for updating the inode. We also
9125 * need to be able to stop the transaction and start a new one, which
9126 * means we need to be able to update the inode several times, and we
9127 * have no idea of knowing how many times that will be, so we can't just
9128 * reserve 1 item for the entirety of the operation, so that has to be
9129 * done separately as well.
Josef Bacikfcb80c22011-05-03 10:40:22 -04009130 *
9131 * So that leaves us with
9132 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07009133 * 1) rsv - for the truncate reservation, which we will steal from the
Josef Bacikfcb80c22011-05-03 10:40:22 -04009134 * transaction reservation.
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07009135 * 2) fs_info->trans_block_rsv - this will have 1 items worth left for
Josef Bacikfcb80c22011-05-03 10:40:22 -04009136 * updating the inode.
9137 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009138 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009139 if (!rsv)
9140 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04009141 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04009142 rsv->failfast = 1;
Josef Bacikf0cd8462011-03-04 14:37:08 -05009143
Josef Bacik907cbce2011-08-08 13:46:15 -04009144 /*
Josef Bacik07127182011-08-19 10:29:59 -04009145 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04009146 * 1 for updating the inode.
9147 */
Josef Bacikf3fe8202013-01-07 17:03:21 -05009148 trans = btrfs_start_transaction(root, 2);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009149 if (IS_ERR(trans)) {
Omar Sandovalad7e1a72018-05-22 09:59:50 -07009150 ret = PTR_ERR(trans);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009151 goto out;
9152 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05009153
Josef Bacik907cbce2011-08-08 13:46:15 -04009154 /* Migrate the slack space for the truncate to our reserve */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009155 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, rsv,
Lu Fengqi3a584172018-08-04 21:10:55 +08009156 min_size, false);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009157 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05009158
Chris Mason5a3f23d2009-03-31 13:27:11 -04009159 /*
Josef Bacik5dc562c2012-08-17 13:14:17 -04009160 * So if we truncate and then write and fsync we normally would just
9161 * write the extents that changed, which is a problem if we need to
9162 * first truncate that entire inode. So set this flag so we write out
9163 * all of the extents in the inode to the sync log so we're completely
9164 * safe.
9165 */
9166 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009167 trans->block_rsv = rsv;
Josef Bacik5dc562c2012-08-17 13:14:17 -04009168
Yan, Zheng80825102009-11-12 09:35:36 +00009169 while (1) {
9170 ret = btrfs_truncate_inode_items(trans, root, inode,
9171 inode->i_size,
9172 BTRFS_EXTENT_DATA_KEY);
Josef Bacikddfae632017-10-19 14:16:02 -04009173 trans->block_rsv = &fs_info->trans_block_rsv;
Omar Sandovalad7e1a72018-05-22 09:59:50 -07009174 if (ret != -ENOSPC && ret != -EAGAIN)
Yan, Zheng80825102009-11-12 09:35:36 +00009175 break;
Chris Mason39279cc2007-06-12 06:35:45 -04009176
Yan, Zheng80825102009-11-12 09:35:36 +00009177 ret = btrfs_update_inode(trans, root, inode);
Omar Sandovalad7e1a72018-05-22 09:59:50 -07009178 if (ret)
Josef Bacik3893e332011-01-31 16:03:11 -05009179 break;
Josef Bacikca7e70f2012-08-27 17:48:15 -04009180
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009181 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009182 btrfs_btree_balance_dirty(fs_info);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009183
9184 trans = btrfs_start_transaction(root, 2);
9185 if (IS_ERR(trans)) {
Omar Sandovalad7e1a72018-05-22 09:59:50 -07009186 ret = PTR_ERR(trans);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009187 trans = NULL;
9188 break;
9189 }
9190
Wang Xiaoguang47b5d642016-09-07 20:17:38 +08009191 btrfs_block_rsv_release(fs_info, rsv, -1);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009192 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv,
Lu Fengqi3a584172018-08-04 21:10:55 +08009193 rsv, min_size, false);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009194 BUG_ON(ret); /* shouldn't happen */
9195 trans->block_rsv = rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00009196 }
9197
Josef Bacikddfae632017-10-19 14:16:02 -04009198 /*
9199 * We can't call btrfs_truncate_block inside a trans handle as we could
9200 * deadlock with freeze, if we got NEED_TRUNCATE_BLOCK then we know
9201 * we've truncated everything except the last little bit, and can do
9202 * btrfs_truncate_block and then update the disk_i_size.
9203 */
9204 if (ret == NEED_TRUNCATE_BLOCK) {
9205 btrfs_end_transaction(trans);
9206 btrfs_btree_balance_dirty(fs_info);
9207
9208 ret = btrfs_truncate_block(inode, inode->i_size, 0, 0);
9209 if (ret)
9210 goto out;
9211 trans = btrfs_start_transaction(root, 1);
9212 if (IS_ERR(trans)) {
9213 ret = PTR_ERR(trans);
9214 goto out;
9215 }
9216 btrfs_ordered_update_i_size(inode, inode->i_size, NULL);
9217 }
9218
Chris Mason917c16b2011-11-08 14:49:59 -05009219 if (trans) {
Omar Sandovalad7e1a72018-05-22 09:59:50 -07009220 int ret2;
Josef Bacik7b128762008-07-24 12:17:14 -04009221
Omar Sandovalad7e1a72018-05-22 09:59:50 -07009222 trans->block_rsv = &fs_info->trans_block_rsv;
9223 ret2 = btrfs_update_inode(trans, root, inode);
9224 if (ret2 && !ret)
9225 ret = ret2;
9226
9227 ret2 = btrfs_end_transaction(trans);
9228 if (ret2 && !ret)
9229 ret = ret2;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009230 btrfs_btree_balance_dirty(fs_info);
Chris Mason917c16b2011-11-08 14:49:59 -05009231 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04009232out:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009233 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009234
Omar Sandovalad7e1a72018-05-22 09:59:50 -07009235 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04009236}
9237
Sven Wegener3b963622008-06-09 21:57:42 -04009238/*
Chris Masond352ac62008-09-29 15:18:18 -04009239 * create a new subvolume directory/inode (helper for the ioctl).
9240 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05009241int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009242 struct btrfs_root *new_root,
9243 struct btrfs_root *parent_root,
9244 u64 new_dirid)
Chris Mason39279cc2007-06-12 06:35:45 -04009245{
Chris Mason39279cc2007-06-12 06:35:45 -04009246 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04009247 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04009248 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009249
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01009250 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
9251 new_dirid, new_dirid,
9252 S_IFDIR | (~current_umask() & S_IRWXUGO),
9253 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04009254 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04009255 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009256 inode->i_op = &btrfs_dir_inode_operations;
9257 inode->i_fop = &btrfs_dir_file_operations;
9258
Miklos Szeredibfe86842011-10-28 14:13:29 +02009259 set_nlink(inode, 1);
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02009260 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Masonb0d5d102014-09-08 13:08:51 -07009261 unlock_new_inode(inode);
Sven Wegener3b963622008-06-09 21:57:42 -04009262
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009263 err = btrfs_subvol_inherit_props(trans, new_root, parent_root);
9264 if (err)
9265 btrfs_err(new_root->fs_info,
David Sterba351fd352014-05-15 16:48:20 +02009266 "error inheriting subvolume %llu properties: %d",
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009267 new_root->root_key.objectid, err);
9268
Yan, Zheng76dda932009-09-21 16:00:26 -04009269 err = btrfs_update_inode(trans, new_root, inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04009270
Yan, Zheng76dda932009-09-21 16:00:26 -04009271 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07009272 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04009273}
9274
Chris Mason39279cc2007-06-12 06:35:45 -04009275struct inode *btrfs_alloc_inode(struct super_block *sb)
9276{
Josef Bacik69fe2d72017-10-19 14:15:57 -04009277 struct btrfs_fs_info *fs_info = btrfs_sb(sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009278 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009279 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009280
David Sterba712e36c2017-10-31 17:08:27 +01009281 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_KERNEL);
Chris Mason39279cc2007-06-12 06:35:45 -04009282 if (!ei)
9283 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009284
9285 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009286 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04009287 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04009288 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04009289 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009290 ei->delalloc_bytes = 0;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01009291 ei->new_delalloc_bytes = 0;
Wang Shilong47059d92014-07-03 18:22:07 +08009292 ei->defrag_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009293 ei->disk_i_size = 0;
9294 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04009295 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009296 ei->index_cnt = (u64)-1;
Miao Xie67de1172013-12-26 13:07:06 +08009297 ei->dir_index = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009298 ei->last_unlink_trans = 0;
Liu Bo46d8bc32012-08-29 01:07:55 -06009299 ei->last_log_commit = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009300
Josef Bacik9e0baf62011-07-15 15:16:44 +00009301 spin_lock_init(&ei->lock);
9302 ei->outstanding_extents = 0;
Josef Bacik69fe2d72017-10-19 14:15:57 -04009303 if (sb->s_magic != BTRFS_TEST_MAGIC)
9304 btrfs_init_metadata_block_rsv(fs_info, &ei->block_rsv,
9305 BTRFS_BLOCK_RSV_DELALLOC);
Josef Bacik72ac3c02012-05-23 14:13:11 -04009306 ei->runtime_flags = 0;
David Sterbab52aa8c2017-07-17 19:17:20 +02009307 ei->prop_compress = BTRFS_COMPRESS_NONE;
David Sterbaeec63c62017-07-17 19:41:31 +02009308 ei->defrag_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009309
Miao Xie16cdcec2011-04-22 18:12:22 +08009310 ei->delayed_node = NULL;
9311
chandan r9cc97d62012-07-04 12:48:07 +05309312 ei->i_otime.tv_sec = 0;
9313 ei->i_otime.tv_nsec = 0;
9314
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009315 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02009316 extent_map_tree_init(&ei->extent_tree);
Qu Wenruo43eb5f22019-03-01 10:47:59 +08009317 extent_io_tree_init(fs_info, &ei->io_tree, IO_TREE_INODE_IO, inode);
9318 extent_io_tree_init(fs_info, &ei->io_failure_tree,
9319 IO_TREE_INODE_IO_FAILURE, inode);
David Sterba7b439732019-03-11 15:58:30 +01009320 ei->io_tree.track_uptodate = true;
9321 ei->io_failure_tree.track_uptodate = true;
Josef Bacikb812ce22012-11-16 13:56:32 -05009322 atomic_set(&ei->sync_writers, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009323 mutex_init(&ei->log_mutex);
Josef Bacikf2486792012-01-13 12:09:22 -05009324 mutex_init(&ei->delalloc_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009325 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009326 INIT_LIST_HEAD(&ei->delalloc_inodes);
David Sterba8089fe62015-11-19 14:15:51 +01009327 INIT_LIST_HEAD(&ei->delayed_iput);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009328 RB_CLEAR_NODE(&ei->rb_node);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01009329 init_rwsem(&ei->dio_sem);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009330
9331 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009332}
9333
Josef Bacikaaedb552013-10-11 14:44:09 -04009334#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
9335void btrfs_test_destroy_inode(struct inode *inode)
9336{
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02009337 btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0);
Josef Bacikaaedb552013-10-11 14:44:09 -04009338 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
9339}
9340#endif
9341
Al Viro26602ca2019-04-10 15:14:41 -04009342void btrfs_free_inode(struct inode *inode)
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009343{
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009344 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
9345}
9346
Chris Mason39279cc2007-06-12 06:35:45 -04009347void btrfs_destroy_inode(struct inode *inode)
9348{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009349 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009350 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009351 struct btrfs_root *root = BTRFS_I(inode)->root;
9352
Al Virob3d9b7a2012-06-09 13:51:19 -04009353 WARN_ON(!hlist_empty(&inode->i_dentry));
Chris Mason39279cc2007-06-12 06:35:45 -04009354 WARN_ON(inode->i_data.nrpages);
Josef Bacik69fe2d72017-10-19 14:15:57 -04009355 WARN_ON(BTRFS_I(inode)->block_rsv.reserved);
9356 WARN_ON(BTRFS_I(inode)->block_rsv.size);
Josef Bacik9e0baf62011-07-15 15:16:44 +00009357 WARN_ON(BTRFS_I(inode)->outstanding_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04009358 WARN_ON(BTRFS_I(inode)->delalloc_bytes);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01009359 WARN_ON(BTRFS_I(inode)->new_delalloc_bytes);
Josef Bacik7709cde2011-08-04 10:25:02 -04009360 WARN_ON(BTRFS_I(inode)->csum_bytes);
Wang Shilong47059d92014-07-03 18:22:07 +08009361 WARN_ON(BTRFS_I(inode)->defrag_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04009362
Chris Mason5a3f23d2009-03-31 13:27:11 -04009363 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05009364 * This can happen where we create an inode, but somebody else also
9365 * created the same inode and we need to destroy the one we already
9366 * created.
9367 */
9368 if (!root)
Al Viro26602ca2019-04-10 15:14:41 -04009369 return;
Josef Bacika6dbd422009-11-11 15:53:34 -05009370
Chris Masond3977122009-01-05 21:25:51 -05009371 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04009372 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
9373 if (!ordered)
9374 break;
9375 else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009376 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04009377 "found ordered extent %llu %llu on inode cleanup",
9378 ordered->file_offset, ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009379 btrfs_remove_ordered_extent(inode, ordered);
9380 btrfs_put_ordered_extent(ordered);
9381 btrfs_put_ordered_extent(ordered);
9382 }
9383 }
Qu Wenruo56fa9d02015-10-13 09:53:10 +08009384 btrfs_qgroup_check_reserved_leak(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04009385 inode_tree_del(inode);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02009386 btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04009387}
9388
Al Viro45321ac2010-06-07 13:43:19 -04009389int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04009390{
9391 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04009392
Naohiro Aota6379ef92013-06-06 09:56:34 +00009393 if (root == NULL)
9394 return 1;
9395
Liu Bofa6ac872013-02-20 14:10:23 +00009396 /* the snap/subvol tree is on deleting */
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02009397 if (btrfs_root_refs(&root->root_item) == 0)
Al Viro45321ac2010-06-07 13:43:19 -04009398 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04009399 else
Al Viro45321ac2010-06-07 13:43:19 -04009400 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04009401}
9402
Sven Wegener0ee0fda2008-07-30 16:54:26 -04009403static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04009404{
9405 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
9406
9407 inode_init_once(&ei->vfs_inode);
9408}
9409
David Sterbae67c7182018-02-19 17:24:18 +01009410void __cold btrfs_destroy_cachep(void)
Chris Mason39279cc2007-06-12 06:35:45 -04009411{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10009412 /*
9413 * Make sure all delayed rcu free inodes are flushed before we
9414 * destroy cache.
9415 */
9416 rcu_barrier();
Kinglong Mee5598e902016-01-29 21:36:35 +08009417 kmem_cache_destroy(btrfs_inode_cachep);
9418 kmem_cache_destroy(btrfs_trans_handle_cachep);
Kinglong Mee5598e902016-01-29 21:36:35 +08009419 kmem_cache_destroy(btrfs_path_cachep);
9420 kmem_cache_destroy(btrfs_free_space_cachep);
Christophe Leroy3acd4852019-08-21 15:05:55 +00009421 kmem_cache_destroy(btrfs_free_space_bitmap_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04009422}
9423
Liu Bof5c29bd2017-11-02 17:21:50 -06009424int __init btrfs_init_cachep(void)
Chris Mason39279cc2007-06-12 06:35:45 -04009425{
David Sterba837e1972012-09-07 03:00:48 -06009426 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009427 sizeof(struct btrfs_inode), 0,
Vladimir Davydov5d097052016-01-14 15:18:21 -08009428 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD | SLAB_ACCOUNT,
9429 init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04009430 if (!btrfs_inode_cachep)
9431 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009432
David Sterba837e1972012-09-07 03:00:48 -06009433 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009434 sizeof(struct btrfs_trans_handle), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009435 SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009436 if (!btrfs_trans_handle_cachep)
9437 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009438
David Sterba837e1972012-09-07 03:00:48 -06009439 btrfs_path_cachep = kmem_cache_create("btrfs_path",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009440 sizeof(struct btrfs_path), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009441 SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009442 if (!btrfs_path_cachep)
9443 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009444
David Sterba837e1972012-09-07 03:00:48 -06009445 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
Josef Bacikdc89e982011-01-28 17:05:48 -05009446 sizeof(struct btrfs_free_space), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009447 SLAB_MEM_SPREAD, NULL);
Josef Bacikdc89e982011-01-28 17:05:48 -05009448 if (!btrfs_free_space_cachep)
9449 goto fail;
9450
Christophe Leroy3acd4852019-08-21 15:05:55 +00009451 btrfs_free_space_bitmap_cachep = kmem_cache_create("btrfs_free_space_bitmap",
9452 PAGE_SIZE, PAGE_SIZE,
9453 SLAB_RED_ZONE, NULL);
9454 if (!btrfs_free_space_bitmap_cachep)
9455 goto fail;
9456
Chris Mason39279cc2007-06-12 06:35:45 -04009457 return 0;
9458fail:
9459 btrfs_destroy_cachep();
9460 return -ENOMEM;
9461}
9462
David Howellsa528d352017-01-31 16:46:22 +00009463static int btrfs_getattr(const struct path *path, struct kstat *stat,
9464 u32 request_mask, unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04009465{
Miao Xiedf0af1a2013-01-29 10:11:59 +00009466 u64 delalloc_bytes;
David Howellsa528d352017-01-31 16:46:22 +00009467 struct inode *inode = d_inode(path->dentry);
David Sterbafadc0d82011-11-20 07:33:38 -05009468 u32 blocksize = inode->i_sb->s_blocksize;
Yonghong Song04a87e32017-05-12 15:07:43 -07009469 u32 bi_flags = BTRFS_I(inode)->flags;
9470
9471 stat->result_mask |= STATX_BTIME;
9472 stat->btime.tv_sec = BTRFS_I(inode)->i_otime.tv_sec;
9473 stat->btime.tv_nsec = BTRFS_I(inode)->i_otime.tv_nsec;
9474 if (bi_flags & BTRFS_INODE_APPEND)
9475 stat->attributes |= STATX_ATTR_APPEND;
9476 if (bi_flags & BTRFS_INODE_COMPRESS)
9477 stat->attributes |= STATX_ATTR_COMPRESSED;
9478 if (bi_flags & BTRFS_INODE_IMMUTABLE)
9479 stat->attributes |= STATX_ATTR_IMMUTABLE;
9480 if (bi_flags & BTRFS_INODE_NODUMP)
9481 stat->attributes |= STATX_ATTR_NODUMP;
9482
9483 stat->attributes_mask |= (STATX_ATTR_APPEND |
9484 STATX_ATTR_COMPRESSED |
9485 STATX_ATTR_IMMUTABLE |
9486 STATX_ATTR_NODUMP);
David Sterbafadc0d82011-11-20 07:33:38 -05009487
Chris Mason39279cc2007-06-12 06:35:45 -04009488 generic_fillattr(inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04009489 stat->dev = BTRFS_I(inode)->root->anon_dev;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009490
9491 spin_lock(&BTRFS_I(inode)->lock);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01009492 delalloc_bytes = BTRFS_I(inode)->new_delalloc_bytes;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009493 spin_unlock(&BTRFS_I(inode)->lock);
David Sterbafadc0d82011-11-20 07:33:38 -05009494 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
Miao Xiedf0af1a2013-01-29 10:11:59 +00009495 ALIGN(delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04009496 return 0;
9497}
9498
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009499static int btrfs_rename_exchange(struct inode *old_dir,
9500 struct dentry *old_dentry,
9501 struct inode *new_dir,
9502 struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04009503{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009504 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009505 struct btrfs_trans_handle *trans;
9506 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009507 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009508 struct inode *new_inode = new_dentry->d_inode;
9509 struct inode *old_inode = old_dentry->d_inode;
Deepa Dinamani95582b02018-05-08 19:36:02 -07009510 struct timespec64 ctime = current_time(old_inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009511 struct dentry *parent;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009512 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
9513 u64 new_ino = btrfs_ino(BTRFS_I(new_inode));
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009514 u64 old_idx = 0;
9515 u64 new_idx = 0;
9516 u64 root_objectid;
9517 int ret;
Filipe Manana86e8aa02016-05-05 02:02:27 +01009518 bool root_log_pinned = false;
9519 bool dest_log_pinned = false;
Filipe Mananad4682ba2018-06-11 19:24:28 +01009520 struct btrfs_log_ctx ctx_root;
9521 struct btrfs_log_ctx ctx_dest;
9522 bool sync_log_root = false;
9523 bool sync_log_dest = false;
9524 bool commit_transaction = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009525
9526 /* we only allow rename subvolume link between subvolumes */
9527 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
9528 return -EXDEV;
9529
Filipe Mananad4682ba2018-06-11 19:24:28 +01009530 btrfs_init_log_ctx(&ctx_root, old_inode);
9531 btrfs_init_log_ctx(&ctx_dest, new_inode);
9532
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009533 /* close the race window with snapshot create/destroy ioctl */
9534 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009535 down_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009536 if (new_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009537 down_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009538
9539 /*
9540 * We want to reserve the absolute worst case amount of items. So if
9541 * both inodes are subvols and we need to unlink them then that would
9542 * require 4 item modifications, but if they are both normal inodes it
9543 * would require 5 item modifications, so we'll assume their normal
9544 * inodes. So 5 * 2 is 10, plus 2 for the new links, so 12 total items
9545 * should cover the worst case number of items we'll modify.
9546 */
9547 trans = btrfs_start_transaction(root, 12);
9548 if (IS_ERR(trans)) {
9549 ret = PTR_ERR(trans);
9550 goto out_notrans;
9551 }
9552
9553 /*
9554 * We need to find a free sequence number both in the source and
9555 * in the destination directory for the exchange.
9556 */
Nikolay Borisov877574e2017-02-20 13:50:33 +02009557 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009558 if (ret)
9559 goto out_fail;
Nikolay Borisov877574e2017-02-20 13:50:33 +02009560 ret = btrfs_set_inode_index(BTRFS_I(old_dir), &new_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009561 if (ret)
9562 goto out_fail;
9563
9564 BTRFS_I(old_inode)->dir_index = 0ULL;
9565 BTRFS_I(new_inode)->dir_index = 0ULL;
9566
9567 /* Reference for the source. */
9568 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
9569 /* force full log commit if subvolume involved. */
David Sterba90787762019-03-20 13:28:05 +01009570 btrfs_set_log_full_commit(trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009571 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01009572 btrfs_pin_log_trans(root);
9573 root_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009574 ret = btrfs_insert_inode_ref(trans, dest,
9575 new_dentry->d_name.name,
9576 new_dentry->d_name.len,
9577 old_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01009578 btrfs_ino(BTRFS_I(new_dir)),
9579 old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009580 if (ret)
9581 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009582 }
9583
9584 /* And now for the dest. */
9585 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
9586 /* force full log commit if subvolume involved. */
David Sterba90787762019-03-20 13:28:05 +01009587 btrfs_set_log_full_commit(trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009588 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01009589 btrfs_pin_log_trans(dest);
9590 dest_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009591 ret = btrfs_insert_inode_ref(trans, root,
9592 old_dentry->d_name.name,
9593 old_dentry->d_name.len,
9594 new_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01009595 btrfs_ino(BTRFS_I(old_dir)),
9596 new_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009597 if (ret)
9598 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009599 }
9600
9601 /* Update inode version and ctime/mtime. */
9602 inode_inc_iversion(old_dir);
9603 inode_inc_iversion(new_dir);
9604 inode_inc_iversion(old_inode);
9605 inode_inc_iversion(new_inode);
9606 old_dir->i_ctime = old_dir->i_mtime = ctime;
9607 new_dir->i_ctime = new_dir->i_mtime = ctime;
9608 old_inode->i_ctime = ctime;
9609 new_inode->i_ctime = ctime;
9610
9611 if (old_dentry->d_parent != new_dentry->d_parent) {
David Sterbaf85b7372017-01-20 14:54:07 +01009612 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
9613 BTRFS_I(old_inode), 1);
9614 btrfs_record_unlink_dir(trans, BTRFS_I(new_dir),
9615 BTRFS_I(new_inode), 1);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009616 }
9617
9618 /* src is a subvolume */
9619 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
9620 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
Lu Fengqi401b3b12018-08-01 11:32:30 +08009621 ret = btrfs_unlink_subvol(trans, old_dir, root_objectid,
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009622 old_dentry->d_name.name,
9623 old_dentry->d_name.len);
9624 } else { /* src is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009625 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
9626 BTRFS_I(old_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009627 old_dentry->d_name.name,
9628 old_dentry->d_name.len);
9629 if (!ret)
9630 ret = btrfs_update_inode(trans, root, old_inode);
9631 }
9632 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009633 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009634 goto out_fail;
9635 }
9636
9637 /* dest is a subvolume */
9638 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
9639 root_objectid = BTRFS_I(new_inode)->root->root_key.objectid;
Lu Fengqi401b3b12018-08-01 11:32:30 +08009640 ret = btrfs_unlink_subvol(trans, new_dir, root_objectid,
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009641 new_dentry->d_name.name,
9642 new_dentry->d_name.len);
9643 } else { /* dest is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009644 ret = __btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
9645 BTRFS_I(new_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009646 new_dentry->d_name.name,
9647 new_dentry->d_name.len);
9648 if (!ret)
9649 ret = btrfs_update_inode(trans, dest, new_inode);
9650 }
9651 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009652 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009653 goto out_fail;
9654 }
9655
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009656 ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009657 new_dentry->d_name.name,
9658 new_dentry->d_name.len, 0, old_idx);
9659 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009660 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009661 goto out_fail;
9662 }
9663
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009664 ret = btrfs_add_link(trans, BTRFS_I(old_dir), BTRFS_I(new_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009665 old_dentry->d_name.name,
9666 old_dentry->d_name.len, 0, new_idx);
9667 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009668 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009669 goto out_fail;
9670 }
9671
9672 if (old_inode->i_nlink == 1)
9673 BTRFS_I(old_inode)->dir_index = old_idx;
9674 if (new_inode->i_nlink == 1)
9675 BTRFS_I(new_inode)->dir_index = new_idx;
9676
Filipe Manana86e8aa02016-05-05 02:02:27 +01009677 if (root_log_pinned) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009678 parent = new_dentry->d_parent;
Filipe Mananad4682ba2018-06-11 19:24:28 +01009679 ret = btrfs_log_new_name(trans, BTRFS_I(old_inode),
9680 BTRFS_I(old_dir), parent,
9681 false, &ctx_root);
9682 if (ret == BTRFS_NEED_LOG_SYNC)
9683 sync_log_root = true;
9684 else if (ret == BTRFS_NEED_TRANS_COMMIT)
9685 commit_transaction = true;
9686 ret = 0;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009687 btrfs_end_log_trans(root);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009688 root_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009689 }
Filipe Manana86e8aa02016-05-05 02:02:27 +01009690 if (dest_log_pinned) {
Filipe Mananad4682ba2018-06-11 19:24:28 +01009691 if (!commit_transaction) {
9692 parent = old_dentry->d_parent;
9693 ret = btrfs_log_new_name(trans, BTRFS_I(new_inode),
9694 BTRFS_I(new_dir), parent,
9695 false, &ctx_dest);
9696 if (ret == BTRFS_NEED_LOG_SYNC)
9697 sync_log_dest = true;
9698 else if (ret == BTRFS_NEED_TRANS_COMMIT)
9699 commit_transaction = true;
9700 ret = 0;
9701 }
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009702 btrfs_end_log_trans(dest);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009703 dest_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009704 }
9705out_fail:
Filipe Manana86e8aa02016-05-05 02:02:27 +01009706 /*
9707 * If we have pinned a log and an error happened, we unpin tasks
9708 * trying to sync the log and force them to fallback to a transaction
9709 * commit if the log currently contains any of the inodes involved in
9710 * this rename operation (to ensure we do not persist a log with an
9711 * inconsistent state for any of these inodes or leading to any
9712 * inconsistencies when replayed). If the transaction was aborted, the
9713 * abortion reason is propagated to userspace when attempting to commit
9714 * the transaction. If the log does not contain any of these inodes, we
9715 * allow the tasks to sync it.
9716 */
9717 if (ret && (root_log_pinned || dest_log_pinned)) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009718 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
9719 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
9720 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana86e8aa02016-05-05 02:02:27 +01009721 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009722 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
David Sterba90787762019-03-20 13:28:05 +01009723 btrfs_set_log_full_commit(trans);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009724
9725 if (root_log_pinned) {
9726 btrfs_end_log_trans(root);
9727 root_log_pinned = false;
9728 }
9729 if (dest_log_pinned) {
9730 btrfs_end_log_trans(dest);
9731 dest_log_pinned = false;
9732 }
9733 }
Filipe Mananad4682ba2018-06-11 19:24:28 +01009734 if (!ret && sync_log_root && !commit_transaction) {
9735 ret = btrfs_sync_log(trans, BTRFS_I(old_inode)->root,
9736 &ctx_root);
9737 if (ret)
9738 commit_transaction = true;
9739 }
9740 if (!ret && sync_log_dest && !commit_transaction) {
9741 ret = btrfs_sync_log(trans, BTRFS_I(new_inode)->root,
9742 &ctx_dest);
9743 if (ret)
9744 commit_transaction = true;
9745 }
9746 if (commit_transaction) {
9747 ret = btrfs_commit_transaction(trans);
9748 } else {
9749 int ret2;
9750
9751 ret2 = btrfs_end_transaction(trans);
9752 ret = ret ? ret : ret2;
9753 }
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009754out_notrans:
9755 if (new_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009756 up_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009757 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009758 up_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009759
9760 return ret;
9761}
9762
9763static int btrfs_whiteout_for_rename(struct btrfs_trans_handle *trans,
9764 struct btrfs_root *root,
9765 struct inode *dir,
9766 struct dentry *dentry)
9767{
9768 int ret;
9769 struct inode *inode;
9770 u64 objectid;
9771 u64 index;
9772
9773 ret = btrfs_find_free_ino(root, &objectid);
9774 if (ret)
9775 return ret;
9776
9777 inode = btrfs_new_inode(trans, root, dir,
9778 dentry->d_name.name,
9779 dentry->d_name.len,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009780 btrfs_ino(BTRFS_I(dir)),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009781 objectid,
9782 S_IFCHR | WHITEOUT_MODE,
9783 &index);
9784
9785 if (IS_ERR(inode)) {
9786 ret = PTR_ERR(inode);
9787 return ret;
9788 }
9789
9790 inode->i_op = &btrfs_special_inode_operations;
9791 init_special_inode(inode, inode->i_mode,
9792 WHITEOUT_DEV);
9793
9794 ret = btrfs_init_inode_security(trans, inode, dir,
9795 &dentry->d_name);
9796 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009797 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009798
Nikolay Borisovcef415a2017-02-20 13:51:09 +02009799 ret = btrfs_add_nondir(trans, BTRFS_I(dir), dentry,
9800 BTRFS_I(inode), 0, index);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009801 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009802 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009803
9804 ret = btrfs_update_inode(trans, root, inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009805out:
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009806 unlock_new_inode(inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009807 if (ret)
9808 inode_dec_link_count(inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009809 iput(inode);
9810
Filipe Mananac9901612016-05-05 01:41:57 +01009811 return ret;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009812}
9813
Chris Mason39279cc2007-06-12 06:35:45 -04009814static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009815 struct inode *new_dir, struct dentry *new_dentry,
9816 unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04009817{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009818 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009819 struct btrfs_trans_handle *trans;
Filipe Manana5062af32016-05-05 10:26:26 +01009820 unsigned int trans_num_items;
Chris Mason39279cc2007-06-12 06:35:45 -04009821 struct btrfs_root *root = BTRFS_I(old_dir)->root;
9822 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00009823 struct inode *new_inode = d_inode(new_dentry);
9824 struct inode *old_inode = d_inode(old_dentry);
Chris Mason00e4e6b2008-08-05 11:18:09 -04009825 u64 index = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009826 u64 root_objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04009827 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009828 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009829 bool log_pinned = false;
Filipe Mananad4682ba2018-06-11 19:24:28 +01009830 struct btrfs_log_ctx ctx;
9831 bool sync_log = false;
9832 bool commit_transaction = false;
Chris Mason39279cc2007-06-12 06:35:45 -04009833
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009834 if (btrfs_ino(BTRFS_I(new_dir)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04009835 return -EPERM;
9836
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009837 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08009838 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05009839 return -EXDEV;
9840
Li Zefan33345d012011-04-20 10:31:50 +08009841 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009842 (new_inode && btrfs_ino(BTRFS_I(new_inode)) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009843 return -ENOTEMPTY;
9844
Chris Mason39279cc2007-06-12 06:35:45 -04009845 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009846 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04009847 return -ENOTEMPTY;
Chris Mason9c520572012-12-17 14:26:57 -05009848
9849
9850 /* check for collisions, even if the name isn't there */
Josef Bacik4871c152013-10-09 12:24:04 -04009851 ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino,
Chris Mason9c520572012-12-17 14:26:57 -05009852 new_dentry->d_name.name,
9853 new_dentry->d_name.len);
9854
9855 if (ret) {
9856 if (ret == -EEXIST) {
9857 /* we shouldn't get
9858 * eexist without a new_inode */
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05309859 if (WARN_ON(!new_inode)) {
Chris Mason9c520572012-12-17 14:26:57 -05009860 return ret;
9861 }
9862 } else {
9863 /* maybe -EOVERFLOW */
9864 return ret;
9865 }
9866 }
9867 ret = 0;
9868
Chris Mason5a3f23d2009-03-31 13:27:11 -04009869 /*
Chris Mason8d875f92014-08-12 10:47:42 -07009870 * we're using rename to replace one file with another. Start IO on it
9871 * now so we don't add too much work to the end of the transaction
Chris Mason5a3f23d2009-03-31 13:27:11 -04009872 */
Chris Mason8d875f92014-08-12 10:47:42 -07009873 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size)
Chris Mason5a3f23d2009-03-31 13:27:11 -04009874 filemap_flush(old_inode->i_mapping);
9875
Yan, Zheng76dda932009-09-21 16:00:26 -04009876 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08009877 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009878 down_read(&fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04009879 /*
9880 * We want to reserve the absolute worst case amount of items. So if
9881 * both inodes are subvols and we need to unlink them then that would
9882 * require 4 item modifications, but if they are both normal inodes it
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009883 * would require 5 item modifications, so we'll assume they are normal
Yan, Zhenga22285a2010-05-16 10:48:46 -04009884 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
9885 * should cover the worst case number of items we'll modify.
Filipe Manana5062af32016-05-05 10:26:26 +01009886 * If our rename has the whiteout flag, we need more 5 units for the
9887 * new inode (1 inode item, 1 inode ref, 2 dir items and 1 xattr item
9888 * when selinux is enabled).
Yan, Zhenga22285a2010-05-16 10:48:46 -04009889 */
Filipe Manana5062af32016-05-05 10:26:26 +01009890 trans_num_items = 11;
9891 if (flags & RENAME_WHITEOUT)
9892 trans_num_items += 5;
9893 trans = btrfs_start_transaction(root, trans_num_items);
Johann Lombardib44c59a2011-03-31 13:23:47 +00009894 if (IS_ERR(trans)) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009895 ret = PTR_ERR(trans);
9896 goto out_notrans;
9897 }
Chris Mason5f39d392007-10-15 16:14:19 -04009898
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009899 if (dest != root)
9900 btrfs_record_root_in_trans(trans, dest);
9901
Nikolay Borisov877574e2017-02-20 13:50:33 +02009902 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009903 if (ret)
9904 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009905
Miao Xie67de1172013-12-26 13:07:06 +08009906 BTRFS_I(old_inode)->dir_index = 0ULL;
Li Zefan33345d012011-04-20 10:31:50 +08009907 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009908 /* force full log commit if subvolume involved. */
David Sterba90787762019-03-20 13:28:05 +01009909 btrfs_set_log_full_commit(trans);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009910 } else {
Filipe Mananac4aba952016-04-29 13:14:42 +01009911 btrfs_pin_log_trans(root);
9912 log_pinned = true;
Yan, Zhenga5719522009-09-24 09:17:31 -04009913 ret = btrfs_insert_inode_ref(trans, dest,
9914 new_dentry->d_name.name,
9915 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08009916 old_ino,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009917 btrfs_ino(BTRFS_I(new_dir)), index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009918 if (ret)
9919 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009920 }
Chris Mason5a3f23d2009-03-31 13:27:11 -04009921
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009922 inode_inc_iversion(old_dir);
9923 inode_inc_iversion(new_dir);
9924 inode_inc_iversion(old_inode);
Deepa Dinamani04b285f2016-02-06 23:57:21 -08009925 old_dir->i_ctime = old_dir->i_mtime =
9926 new_dir->i_ctime = new_dir->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009927 old_inode->i_ctime = current_time(old_dir);
Chris Mason5f39d392007-10-15 16:14:19 -04009928
Chris Mason12fcfd22009-03-24 10:24:20 -04009929 if (old_dentry->d_parent != new_dentry->d_parent)
David Sterbaf85b7372017-01-20 14:54:07 +01009930 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
9931 BTRFS_I(old_inode), 1);
Chris Mason12fcfd22009-03-24 10:24:20 -04009932
Li Zefan33345d012011-04-20 10:31:50 +08009933 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009934 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
Lu Fengqi401b3b12018-08-01 11:32:30 +08009935 ret = btrfs_unlink_subvol(trans, old_dir, root_objectid,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009936 old_dentry->d_name.name,
9937 old_dentry->d_name.len);
9938 } else {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009939 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
9940 BTRFS_I(d_inode(old_dentry)),
Al Viro92986792011-03-04 17:14:37 +00009941 old_dentry->d_name.name,
9942 old_dentry->d_name.len);
9943 if (!ret)
9944 ret = btrfs_update_inode(trans, root, old_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009945 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009946 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009947 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009948 goto out_fail;
9949 }
Chris Mason39279cc2007-06-12 06:35:45 -04009950
9951 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009952 inode_inc_iversion(new_inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009953 new_inode->i_ctime = current_time(new_inode);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009954 if (unlikely(btrfs_ino(BTRFS_I(new_inode)) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009955 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
9956 root_objectid = BTRFS_I(new_inode)->location.objectid;
Lu Fengqi401b3b12018-08-01 11:32:30 +08009957 ret = btrfs_unlink_subvol(trans, new_dir, root_objectid,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009958 new_dentry->d_name.name,
9959 new_dentry->d_name.len);
9960 BUG_ON(new_inode->i_nlink == 0);
9961 } else {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009962 ret = btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
9963 BTRFS_I(d_inode(new_dentry)),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009964 new_dentry->d_name.name,
9965 new_dentry->d_name.len);
9966 }
Josef Bacik4ef31a42013-08-13 14:10:08 -04009967 if (!ret && new_inode->i_nlink == 0)
Nikolay Borisov73f2e542017-02-20 13:50:59 +02009968 ret = btrfs_orphan_add(trans,
9969 BTRFS_I(d_inode(new_dentry)));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009970 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009971 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009972 goto out_fail;
9973 }
Chris Mason39279cc2007-06-12 06:35:45 -04009974 }
Josef Bacikaec74772008-07-24 12:12:38 -04009975
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009976 ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009977 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04009978 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009979 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009980 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009981 goto out_fail;
9982 }
Chris Mason39279cc2007-06-12 06:35:45 -04009983
Miao Xie67de1172013-12-26 13:07:06 +08009984 if (old_inode->i_nlink == 1)
9985 BTRFS_I(old_inode)->dir_index = index;
9986
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009987 if (log_pinned) {
Al Viro10d9f302011-07-16 23:09:10 -04009988 struct dentry *parent = new_dentry->d_parent;
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009989
Filipe Mananad4682ba2018-06-11 19:24:28 +01009990 btrfs_init_log_ctx(&ctx, old_inode);
9991 ret = btrfs_log_new_name(trans, BTRFS_I(old_inode),
9992 BTRFS_I(old_dir), parent,
9993 false, &ctx);
9994 if (ret == BTRFS_NEED_LOG_SYNC)
9995 sync_log = true;
9996 else if (ret == BTRFS_NEED_TRANS_COMMIT)
9997 commit_transaction = true;
9998 ret = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009999 btrfs_end_log_trans(root);
Filipe Manana3dc9e8f72016-04-29 11:34:22 +010010000 log_pinned = false;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010001 }
Dan Fuhrycdd1fed2016-03-17 15:23:38 +010010002
10003 if (flags & RENAME_WHITEOUT) {
10004 ret = btrfs_whiteout_for_rename(trans, root, old_dir,
10005 old_dentry);
10006
10007 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -040010008 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +010010009 goto out_fail;
10010 }
Chris Mason12fcfd22009-03-24 10:24:20 -040010011 }
Chris Mason39279cc2007-06-12 06:35:45 -040010012out_fail:
Filipe Manana3dc9e8f72016-04-29 11:34:22 +010010013 /*
10014 * If we have pinned the log and an error happened, we unpin tasks
10015 * trying to sync the log and force them to fallback to a transaction
10016 * commit if the log currently contains any of the inodes involved in
10017 * this rename operation (to ensure we do not persist a log with an
10018 * inconsistent state for any of these inodes or leading to any
10019 * inconsistencies when replayed). If the transaction was aborted, the
10020 * abortion reason is propagated to userspace when attempting to commit
10021 * the transaction. If the log does not contain any of these inodes, we
10022 * allow the tasks to sync it.
10023 */
10024 if (ret && log_pinned) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +020010025 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
10026 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
10027 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana3dc9e8f72016-04-29 11:34:22 +010010028 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +020010029 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
David Sterba90787762019-03-20 13:28:05 +010010030 btrfs_set_log_full_commit(trans);
Filipe Manana3dc9e8f72016-04-29 11:34:22 +010010031
10032 btrfs_end_log_trans(root);
10033 log_pinned = false;
10034 }
Filipe Mananad4682ba2018-06-11 19:24:28 +010010035 if (!ret && sync_log) {
10036 ret = btrfs_sync_log(trans, BTRFS_I(old_inode)->root, &ctx);
10037 if (ret)
10038 commit_transaction = true;
10039 }
10040 if (commit_transaction) {
10041 ret = btrfs_commit_transaction(trans);
10042 } else {
10043 int ret2;
10044
10045 ret2 = btrfs_end_transaction(trans);
10046 ret = ret ? ret : ret2;
10047 }
Johann Lombardib44c59a2011-03-31 13:23:47 +000010048out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +080010049 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010050 up_read(&fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -040010051
Chris Mason39279cc2007-06-12 06:35:45 -040010052 return ret;
10053}
10054
Miklos Szeredi80ace852014-07-23 15:15:32 +020010055static int btrfs_rename2(struct inode *old_dir, struct dentry *old_dentry,
10056 struct inode *new_dir, struct dentry *new_dentry,
10057 unsigned int flags)
10058{
Dan Fuhrycdd1fed2016-03-17 15:23:38 +010010059 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
Miklos Szeredi80ace852014-07-23 15:15:32 +020010060 return -EINVAL;
10061
Dan Fuhrycdd1fed2016-03-17 15:23:38 +010010062 if (flags & RENAME_EXCHANGE)
10063 return btrfs_rename_exchange(old_dir, old_dentry, new_dir,
10064 new_dentry);
10065
10066 return btrfs_rename(old_dir, old_dentry, new_dir, new_dentry, flags);
Miklos Szeredi80ace852014-07-23 15:15:32 +020010067}
10068
Nikolay Borisov3a2f8c02018-04-24 17:23:59 +030010069struct btrfs_delalloc_work {
10070 struct inode *inode;
10071 struct completion completion;
10072 struct list_head list;
10073 struct btrfs_work work;
10074};
10075
Miao Xie8ccf6f192012-10-25 09:28:04 +000010076static void btrfs_run_delalloc_work(struct btrfs_work *work)
10077{
10078 struct btrfs_delalloc_work *delalloc_work;
Josef Bacik9f23e282013-10-28 15:03:41 -040010079 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010080
10081 delalloc_work = container_of(work, struct btrfs_delalloc_work,
10082 work);
Josef Bacik9f23e282013-10-28 15:03:41 -040010083 inode = delalloc_work->inode;
David Sterba30424602015-11-27 19:27:11 +010010084 filemap_flush(inode->i_mapping);
10085 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
10086 &BTRFS_I(inode)->runtime_flags))
Josef Bacik9f23e282013-10-28 15:03:41 -040010087 filemap_flush(inode->i_mapping);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010088
Nikolay Borisov076da912018-04-23 10:54:16 +030010089 iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010090 complete(&delalloc_work->completion);
10091}
10092
Nikolay Borisov3a2f8c02018-04-24 17:23:59 +030010093static struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode)
Miao Xie8ccf6f192012-10-25 09:28:04 +000010094{
10095 struct btrfs_delalloc_work *work;
10096
David Sterba100d5702015-12-08 14:39:32 +010010097 work = kmalloc(sizeof(*work), GFP_NOFS);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010098 if (!work)
10099 return NULL;
10100
10101 init_completion(&work->completion);
10102 INIT_LIST_HEAD(&work->list);
10103 work->inode = inode;
Liu Bo9e0af232014-08-15 23:36:53 +080010104 btrfs_init_work(&work->work, btrfs_flush_delalloc_helper,
10105 btrfs_run_delalloc_work, NULL, NULL);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010106
10107 return work;
10108}
10109
Chris Masond352ac62008-09-29 15:18:18 -040010110/*
10111 * some fairly slow code that needs optimization. This walks the list
10112 * of all the inodes with pending delalloc and forces them to disk.
10113 */
Ethan Lien3cd24c62018-11-01 14:49:03 +080010114static int start_delalloc_inodes(struct btrfs_root *root, int nr, bool snapshot)
Chris Masonea8c2812008-08-04 23:17:27 -040010115{
Chris Masonea8c2812008-08-04 23:17:27 -040010116 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010117 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010118 struct btrfs_delalloc_work *work, *next;
10119 struct list_head works;
Miao Xie1eafa6c2013-01-22 10:49:00 +000010120 struct list_head splice;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010121 int ret = 0;
Chris Masonea8c2812008-08-04 23:17:27 -040010122
Miao Xie8ccf6f192012-10-25 09:28:04 +000010123 INIT_LIST_HEAD(&works);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010124 INIT_LIST_HEAD(&splice);
Miao Xie63607cc2013-01-22 10:50:35 +000010125
Miao Xie573bfb72014-03-06 13:55:03 +080010126 mutex_lock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010127 spin_lock(&root->delalloc_lock);
10128 list_splice_init(&root->delalloc_inodes, &splice);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010129 while (!list_empty(&splice)) {
10130 binode = list_entry(splice.next, struct btrfs_inode,
Chris Masonea8c2812008-08-04 23:17:27 -040010131 delalloc_inodes);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010132
Miao Xieeb73c1b2013-05-15 07:48:22 +000010133 list_move_tail(&binode->delalloc_inodes,
10134 &root->delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010135 inode = igrab(&binode->vfs_inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +000010136 if (!inode) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010137 cond_resched_lock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010138 continue;
Miao Xiedf0af1a2013-01-29 10:11:59 +000010139 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010140 spin_unlock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010141
Ethan Lien3cd24c62018-11-01 14:49:03 +080010142 if (snapshot)
10143 set_bit(BTRFS_INODE_SNAPSHOT_FLUSH,
10144 &binode->runtime_flags);
Nikolay Borisov076da912018-04-23 10:54:16 +030010145 work = btrfs_alloc_delalloc_work(inode);
David Sterba5d99a9982014-09-29 19:20:37 +020010146 if (!work) {
Nikolay Borisov4fbb5142018-04-23 10:54:15 +030010147 iput(inode);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010148 ret = -ENOMEM;
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010149 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010150 }
Miao Xie1eafa6c2013-01-22 10:49:00 +000010151 list_add_tail(&work->list, &works);
Qu Wenruoa44903a2014-02-28 10:46:09 +080010152 btrfs_queue_work(root->fs_info->flush_workers,
10153 &work->work);
Miao Xie6c255e62014-03-06 13:55:01 +080010154 ret++;
10155 if (nr != -1 && ret >= nr)
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010156 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010157 cond_resched();
Miao Xieeb73c1b2013-05-15 07:48:22 +000010158 spin_lock(&root->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -040010159 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010160 spin_unlock(&root->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -040010161
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010162out:
Miao Xie1eafa6c2013-01-22 10:49:00 +000010163 list_for_each_entry_safe(work, next, &works, list) {
10164 list_del_init(&work->list);
Nikolay Borisov40012f92018-04-19 10:46:39 +030010165 wait_for_completion(&work->completion);
10166 kfree(work);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010167 }
10168
Nikolay Borisov81f1d392018-04-19 10:46:37 +030010169 if (!list_empty(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010170 spin_lock(&root->delalloc_lock);
10171 list_splice_tail(&splice, &root->delalloc_inodes);
10172 spin_unlock(&root->delalloc_lock);
10173 }
Miao Xie573bfb72014-03-06 13:55:03 +080010174 mutex_unlock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010175 return ret;
10176}
10177
Ethan Lien3cd24c62018-11-01 14:49:03 +080010178int btrfs_start_delalloc_snapshot(struct btrfs_root *root)
Miao Xieeb73c1b2013-05-15 07:48:22 +000010179{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010180 struct btrfs_fs_info *fs_info = root->fs_info;
Miao Xieeb73c1b2013-05-15 07:48:22 +000010181 int ret;
10182
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010183 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +000010184 return -EROFS;
10185
Ethan Lien3cd24c62018-11-01 14:49:03 +080010186 ret = start_delalloc_inodes(root, -1, true);
Miao Xie6c255e62014-03-06 13:55:01 +080010187 if (ret > 0)
10188 ret = 0;
Miao Xieeb73c1b2013-05-15 07:48:22 +000010189 return ret;
10190}
10191
Nikolay Borisov82b3e532018-04-23 10:54:13 +030010192int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, int nr)
Miao Xieeb73c1b2013-05-15 07:48:22 +000010193{
10194 struct btrfs_root *root;
10195 struct list_head splice;
10196 int ret;
10197
Wang Shilong2c21b4d2014-01-14 19:42:20 +080010198 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +000010199 return -EROFS;
10200
10201 INIT_LIST_HEAD(&splice);
10202
Miao Xie573bfb72014-03-06 13:55:03 +080010203 mutex_lock(&fs_info->delalloc_root_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010204 spin_lock(&fs_info->delalloc_root_lock);
10205 list_splice_init(&fs_info->delalloc_roots, &splice);
Miao Xie6c255e62014-03-06 13:55:01 +080010206 while (!list_empty(&splice) && nr) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010207 root = list_first_entry(&splice, struct btrfs_root,
10208 delalloc_root);
10209 root = btrfs_grab_fs_root(root);
10210 BUG_ON(!root);
10211 list_move_tail(&root->delalloc_root,
10212 &fs_info->delalloc_roots);
10213 spin_unlock(&fs_info->delalloc_root_lock);
10214
Ethan Lien3cd24c62018-11-01 14:49:03 +080010215 ret = start_delalloc_inodes(root, nr, false);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010216 btrfs_put_fs_root(root);
Miao Xie6c255e62014-03-06 13:55:01 +080010217 if (ret < 0)
Miao Xieeb73c1b2013-05-15 07:48:22 +000010218 goto out;
10219
Miao Xie6c255e62014-03-06 13:55:01 +080010220 if (nr != -1) {
10221 nr -= ret;
10222 WARN_ON(nr < 0);
10223 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010224 spin_lock(&fs_info->delalloc_root_lock);
10225 }
10226 spin_unlock(&fs_info->delalloc_root_lock);
10227
Miao Xie6c255e62014-03-06 13:55:01 +080010228 ret = 0;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010229out:
Nikolay Borisov81f1d392018-04-19 10:46:37 +030010230 if (!list_empty(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010231 spin_lock(&fs_info->delalloc_root_lock);
10232 list_splice_tail(&splice, &fs_info->delalloc_roots);
10233 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010234 }
Miao Xie573bfb72014-03-06 13:55:03 +080010235 mutex_unlock(&fs_info->delalloc_root_mutex);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010236 return ret;
Chris Masonea8c2812008-08-04 23:17:27 -040010237}
10238
Chris Mason39279cc2007-06-12 06:35:45 -040010239static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
10240 const char *symname)
10241{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010242 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -040010243 struct btrfs_trans_handle *trans;
10244 struct btrfs_root *root = BTRFS_I(dir)->root;
10245 struct btrfs_path *path;
10246 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -050010247 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -040010248 int err;
Chris Mason39279cc2007-06-12 06:35:45 -040010249 u64 objectid;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +053010250 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -040010251 int name_len;
10252 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -040010253 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -040010254 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -040010255 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -040010256
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +010010257 name_len = strlen(symname);
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010258 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info))
Chris Mason39279cc2007-06-12 06:35:45 -040010259 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -050010260
Josef Bacik9ed74f22009-09-11 16:12:44 -040010261 /*
10262 * 2 items for inode item and ref
10263 * 2 items for dir items
Filipe Manana9269d122015-12-31 18:16:29 +000010264 * 1 item for updating parent inode item
10265 * 1 item for the inline extent item
Josef Bacik9ed74f22009-09-11 16:12:44 -040010266 * 1 item for xattr if selinux is on
10267 */
Filipe Manana9269d122015-12-31 18:16:29 +000010268 trans = btrfs_start_transaction(root, 7);
Yan, Zhenga22285a2010-05-16 10:48:46 -040010269 if (IS_ERR(trans))
10270 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -050010271
Li Zefan581bb052011-04-20 10:06:11 +080010272 err = btrfs_find_free_ino(root, &objectid);
10273 if (err)
10274 goto out_unlock;
10275
Josef Bacikaec74772008-07-24 12:12:38 -040010276 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +010010277 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)),
10278 objectid, S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -040010279 if (IS_ERR(inode)) {
10280 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -040010281 inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -040010282 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -040010283 }
Chris Mason39279cc2007-06-12 06:35:45 -040010284
Casey Schauflerad19db72011-12-15 10:09:07 -050010285 /*
10286 * If the active LSM wants to access the inode during
10287 * d_instantiate it needs these. Smack checks to see
10288 * if the filesystem supports xattrs by looking at the
10289 * ops vector.
10290 */
10291 inode->i_fop = &btrfs_file_operations;
10292 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -070010293 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -070010294 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
10295
10296 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
10297 if (err)
Al Viro32955c52018-05-16 12:20:05 -040010298 goto out_unlock;
Casey Schauflerad19db72011-12-15 10:09:07 -050010299
Chris Mason39279cc2007-06-12 06:35:45 -040010300 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -070010301 if (!path) {
10302 err = -ENOMEM;
Al Viro32955c52018-05-16 12:20:05 -040010303 goto out_unlock;
Mark Fashehd8926bb2011-07-13 10:38:47 -070010304 }
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +020010305 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason39279cc2007-06-12 06:35:45 -040010306 key.offset = 0;
David Sterba962a2982014-06-04 18:41:45 +020010307 key.type = BTRFS_EXTENT_DATA_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -040010308 datasize = btrfs_file_extent_calc_inline_size(name_len);
10309 err = btrfs_insert_empty_item(trans, root, path, &key,
10310 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -040010311 if (err) {
Julia Lawallb0839162011-05-14 07:10:51 +000010312 btrfs_free_path(path);
Al Viro32955c52018-05-16 12:20:05 -040010313 goto out_unlock;
Chris Mason54aa1f42007-06-22 14:16:25 -040010314 }
Chris Mason5f39d392007-10-15 16:14:19 -040010315 leaf = path->nodes[0];
10316 ei = btrfs_item_ptr(leaf, path->slots[0],
10317 struct btrfs_file_extent_item);
10318 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
10319 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -040010320 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -040010321 btrfs_set_file_extent_encryption(leaf, ei, 0);
10322 btrfs_set_file_extent_compression(leaf, ei, 0);
10323 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
10324 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
10325
Chris Mason39279cc2007-06-12 06:35:45 -040010326 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -040010327 write_extent_buffer(leaf, symname, ptr, name_len);
10328 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -040010329 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -040010330
Chris Mason39279cc2007-06-12 06:35:45 -040010331 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -050010332 inode_nohighmem(inode);
Yan Zhengd899e052008-10-30 14:25:28 -040010333 inode_set_bytes(inode, name_len);
Nikolay Borisov6ef06d22017-02-20 13:50:34 +020010334 btrfs_i_size_write(BTRFS_I(inode), name_len);
Chris Mason54aa1f42007-06-22 14:16:25 -040010335 err = btrfs_update_inode(trans, root, inode);
Filipe Mananad50866d2015-12-31 18:08:24 +000010336 /*
10337 * Last step, add directory indexes for our symlink inode. This is the
10338 * last step to avoid extra cleanup of these indexes if an error happens
10339 * elsewhere above.
10340 */
10341 if (!err)
Nikolay Borisovcef415a2017-02-20 13:51:09 +020010342 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry,
10343 BTRFS_I(inode), 0, index);
Al Viro32955c52018-05-16 12:20:05 -040010344 if (err)
10345 goto out_unlock;
Chris Masonb0d5d102014-09-08 13:08:51 -070010346
Al Viro1e2e5472018-05-04 08:23:01 -040010347 d_instantiate_new(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010348
10349out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010350 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -040010351 if (err && inode) {
Chris Mason39279cc2007-06-12 06:35:45 -040010352 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -040010353 discard_new_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010354 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010355 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -040010356 return err;
10357}
Chris Mason16432982008-04-10 10:23:21 -040010358
Josef Bacik0af3d002010-06-21 14:48:16 -040010359static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
10360 u64 start, u64 num_bytes, u64 min_size,
10361 loff_t actual_len, u64 *alloc_hint,
10362 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -040010363{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010364 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik5dc562c2012-08-17 13:14:17 -040010365 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
10366 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -040010367 struct btrfs_root *root = BTRFS_I(inode)->root;
10368 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -040010369 u64 cur_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +000010370 u64 i_size;
Chris Mason154ea282013-03-05 11:11:26 -050010371 u64 cur_bytes;
Josef Bacik0b670dc2015-09-23 17:11:16 -040010372 u64 last_alloc = (u64)-1;
Yan Zhengd899e052008-10-30 14:25:28 -040010373 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -040010374 bool own_trans = true;
Wang Xiaoguang18513092016-07-25 15:51:40 +080010375 u64 end = start + num_bytes - 1;
Yan Zhengd899e052008-10-30 14:25:28 -040010376
Josef Bacik0af3d002010-06-21 14:48:16 -040010377 if (trans)
10378 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -040010379 while (num_bytes > 0) {
Josef Bacik0af3d002010-06-21 14:48:16 -040010380 if (own_trans) {
10381 trans = btrfs_start_transaction(root, 3);
10382 if (IS_ERR(trans)) {
10383 ret = PTR_ERR(trans);
10384 break;
10385 }
Yan Zhengd899e052008-10-30 14:25:28 -040010386 }
Yan, Zheng5a303d52009-11-12 09:34:52 +000010387
Byongho Leeee221842015-12-15 01:42:10 +090010388 cur_bytes = min_t(u64, num_bytes, SZ_256M);
Chris Mason154ea282013-03-05 11:11:26 -050010389 cur_bytes = max(cur_bytes, min_size);
Josef Bacik0b670dc2015-09-23 17:11:16 -040010390 /*
10391 * If we are severely fragmented we could end up with really
10392 * small allocations, so if the allocator is returning small
10393 * chunks lets make its job easier by only searching for those
10394 * sized chunks.
10395 */
10396 cur_bytes = min(cur_bytes, last_alloc);
Wang Xiaoguang18513092016-07-25 15:51:40 +080010397 ret = btrfs_reserve_extent(root, cur_bytes, cur_bytes,
10398 min_size, 0, *alloc_hint, &ins, 1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010399 if (ret) {
Josef Bacik0af3d002010-06-21 14:48:16 -040010400 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010401 btrfs_end_transaction(trans);
Yan, Zhenga22285a2010-05-16 10:48:46 -040010402 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +000010403 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010404 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010405
Josef Bacik0b670dc2015-09-23 17:11:16 -040010406 last_alloc = ins.offset;
Yan Zhengd899e052008-10-30 14:25:28 -040010407 ret = insert_reserved_file_extent(trans, inode,
10408 cur_offset, ins.objectid,
10409 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +000010410 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -040010411 BTRFS_FILE_EXTENT_PREALLOC);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010412 if (ret) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010413 btrfs_free_reserved_extent(fs_info, ins.objectid,
Miao Xiee570fd22014-06-19 10:42:50 +080010414 ins.offset, 0);
Jeff Mahoney66642832016-06-10 18:19:25 -040010415 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010416 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010417 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010418 break;
10419 }
Dongsheng Yang31193212014-12-12 16:44:35 +080010420
Nikolay Borisovdcdbc052017-02-20 13:50:45 +020010421 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Chris Masona1ed8352009-09-11 12:27:37 -040010422 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010423
Josef Bacik5dc562c2012-08-17 13:14:17 -040010424 em = alloc_extent_map();
10425 if (!em) {
10426 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
10427 &BTRFS_I(inode)->runtime_flags);
10428 goto next;
10429 }
10430
10431 em->start = cur_offset;
10432 em->orig_start = cur_offset;
10433 em->len = ins.offset;
10434 em->block_start = ins.objectid;
10435 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -050010436 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -040010437 em->ram_bytes = ins.offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010438 em->bdev = fs_info->fs_devices->latest_bdev;
Josef Bacik5dc562c2012-08-17 13:14:17 -040010439 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
10440 em->generation = trans->transid;
10441
10442 while (1) {
10443 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -040010444 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -040010445 write_unlock(&em_tree->lock);
10446 if (ret != -EEXIST)
10447 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +020010448 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -040010449 cur_offset + ins.offset - 1,
10450 0);
10451 }
10452 free_extent_map(em);
10453next:
Yan Zhengd899e052008-10-30 14:25:28 -040010454 num_bytes -= ins.offset;
10455 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -040010456 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +000010457
Josef Bacik0c4d2d92012-04-05 15:03:02 -040010458 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -070010459 inode->i_ctime = current_time(inode);
Christoph Hellwig6cbff002009-04-17 10:37:41 +020010460 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -040010461 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -040010462 (actual_len > inode->i_size) &&
10463 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +000010464 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +000010465 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +000010466 else
Josef Bacik55a61d12010-11-22 18:50:32 +000010467 i_size = cur_offset;
10468 i_size_write(inode, i_size);
10469 btrfs_ordered_update_i_size(inode, i_size, NULL);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010470 }
10471
Yan Zhengd899e052008-10-30 14:25:28 -040010472 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010473
10474 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -040010475 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010476 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010477 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010478 break;
10479 }
Yan Zhengd899e052008-10-30 14:25:28 -040010480
Josef Bacik0af3d002010-06-21 14:48:16 -040010481 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010482 btrfs_end_transaction(trans);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010483 }
Wang Xiaoguang18513092016-07-25 15:51:40 +080010484 if (cur_offset < end)
Qu Wenruobc42bda2017-02-27 15:10:39 +080010485 btrfs_free_reserved_data_space(inode, NULL, cur_offset,
Wang Xiaoguang18513092016-07-25 15:51:40 +080010486 end - cur_offset + 1);
Yan Zhengd899e052008-10-30 14:25:28 -040010487 return ret;
10488}
10489
Josef Bacik0af3d002010-06-21 14:48:16 -040010490int btrfs_prealloc_file_range(struct inode *inode, int mode,
10491 u64 start, u64 num_bytes, u64 min_size,
10492 loff_t actual_len, u64 *alloc_hint)
10493{
10494 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
10495 min_size, actual_len, alloc_hint,
10496 NULL);
10497}
10498
10499int btrfs_prealloc_file_range_trans(struct inode *inode,
10500 struct btrfs_trans_handle *trans, int mode,
10501 u64 start, u64 num_bytes, u64 min_size,
10502 loff_t actual_len, u64 *alloc_hint)
10503{
10504 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
10505 min_size, actual_len, alloc_hint, trans);
10506}
10507
Chris Masone6dcd2d2008-07-17 12:53:50 -040010508static int btrfs_set_page_dirty(struct page *page)
10509{
Chris Masone6dcd2d2008-07-17 12:53:50 -040010510 return __set_page_dirty_nobuffers(page);
10511}
10512
Al Viro10556cb22011-06-20 19:28:19 -040010513static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -050010514{
Li Zefanb83cc962010-12-20 16:04:08 +080010515 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +000010516 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +080010517
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +000010518 if (mask & MAY_WRITE &&
10519 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
10520 if (btrfs_root_readonly(root))
10521 return -EROFS;
10522 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
10523 return -EACCES;
10524 }
Al Viro2830ba72011-06-20 19:16:29 -040010525 return generic_permission(inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -050010526}
Chris Mason39279cc2007-06-12 06:35:45 -040010527
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010528static int btrfs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
10529{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010530 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010531 struct btrfs_trans_handle *trans;
10532 struct btrfs_root *root = BTRFS_I(dir)->root;
10533 struct inode *inode = NULL;
10534 u64 objectid;
10535 u64 index;
10536 int ret = 0;
10537
10538 /*
10539 * 5 units required for adding orphan entry
10540 */
10541 trans = btrfs_start_transaction(root, 5);
10542 if (IS_ERR(trans))
10543 return PTR_ERR(trans);
10544
10545 ret = btrfs_find_free_ino(root, &objectid);
10546 if (ret)
10547 goto out;
10548
10549 inode = btrfs_new_inode(trans, root, dir, NULL, 0,
David Sterbaf85b7372017-01-20 14:54:07 +010010550 btrfs_ino(BTRFS_I(dir)), objectid, mode, &index);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010551 if (IS_ERR(inode)) {
10552 ret = PTR_ERR(inode);
10553 inode = NULL;
10554 goto out;
10555 }
10556
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010557 inode->i_fop = &btrfs_file_operations;
10558 inode->i_op = &btrfs_file_inode_operations;
10559
10560 inode->i_mapping->a_ops = &btrfs_aops;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010561 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
10562
Chris Masonb0d5d102014-09-08 13:08:51 -070010563 ret = btrfs_init_inode_security(trans, inode, dir, NULL);
10564 if (ret)
Al Viro32955c52018-05-16 12:20:05 -040010565 goto out;
Chris Masonb0d5d102014-09-08 13:08:51 -070010566
10567 ret = btrfs_update_inode(trans, root, inode);
10568 if (ret)
Al Viro32955c52018-05-16 12:20:05 -040010569 goto out;
Nikolay Borisov73f2e542017-02-20 13:50:59 +020010570 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010571 if (ret)
Al Viro32955c52018-05-16 12:20:05 -040010572 goto out;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010573
Filipe Manana5762b5c2014-08-01 00:10:32 +010010574 /*
10575 * We set number of links to 0 in btrfs_new_inode(), and here we set
10576 * it to 1 because d_tmpfile() will issue a warning if the count is 0,
10577 * through:
10578 *
10579 * d_tmpfile() -> inode_dec_link_count() -> drop_nlink()
10580 */
10581 set_nlink(inode, 1);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010582 d_tmpfile(dentry, inode);
Al Viro32955c52018-05-16 12:20:05 -040010583 unlock_new_inode(inode);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010584 mark_inode_dirty(inode);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010585out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010586 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -040010587 if (ret && inode)
10588 discard_new_inode(inode);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010589 btrfs_btree_balance_dirty(fs_info);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010590 return ret;
10591}
10592
David Sterba5cdc84b2018-07-18 20:32:52 +020010593void btrfs_set_range_writeback(struct extent_io_tree *tree, u64 start, u64 end)
Josef Bacikc6100a42017-05-05 11:57:13 -040010594{
David Sterba5cdc84b2018-07-18 20:32:52 +020010595 struct inode *inode = tree->private_data;
Josef Bacikc6100a42017-05-05 11:57:13 -040010596 unsigned long index = start >> PAGE_SHIFT;
10597 unsigned long end_index = end >> PAGE_SHIFT;
10598 struct page *page;
10599
10600 while (index <= end_index) {
10601 page = find_get_page(inode->i_mapping, index);
10602 ASSERT(page); /* Pages should be in the extent_io_tree */
10603 set_page_writeback(page);
10604 put_page(page);
10605 index++;
10606 }
10607}
10608
Omar Sandovaled46ff32016-11-03 10:28:14 -070010609#ifdef CONFIG_SWAP
10610/*
10611 * Add an entry indicating a block group or device which is pinned by a
10612 * swapfile. Returns 0 on success, 1 if there is already an entry for it, or a
10613 * negative errno on failure.
10614 */
10615static int btrfs_add_swapfile_pin(struct inode *inode, void *ptr,
10616 bool is_block_group)
10617{
10618 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
10619 struct btrfs_swapfile_pin *sp, *entry;
10620 struct rb_node **p;
10621 struct rb_node *parent = NULL;
10622
10623 sp = kmalloc(sizeof(*sp), GFP_NOFS);
10624 if (!sp)
10625 return -ENOMEM;
10626 sp->ptr = ptr;
10627 sp->inode = inode;
10628 sp->is_block_group = is_block_group;
10629
10630 spin_lock(&fs_info->swapfile_pins_lock);
10631 p = &fs_info->swapfile_pins.rb_node;
10632 while (*p) {
10633 parent = *p;
10634 entry = rb_entry(parent, struct btrfs_swapfile_pin, node);
10635 if (sp->ptr < entry->ptr ||
10636 (sp->ptr == entry->ptr && sp->inode < entry->inode)) {
10637 p = &(*p)->rb_left;
10638 } else if (sp->ptr > entry->ptr ||
10639 (sp->ptr == entry->ptr && sp->inode > entry->inode)) {
10640 p = &(*p)->rb_right;
10641 } else {
10642 spin_unlock(&fs_info->swapfile_pins_lock);
10643 kfree(sp);
10644 return 1;
10645 }
10646 }
10647 rb_link_node(&sp->node, parent, p);
10648 rb_insert_color(&sp->node, &fs_info->swapfile_pins);
10649 spin_unlock(&fs_info->swapfile_pins_lock);
10650 return 0;
10651}
10652
10653/* Free all of the entries pinned by this swapfile. */
10654static void btrfs_free_swapfile_pins(struct inode *inode)
10655{
10656 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
10657 struct btrfs_swapfile_pin *sp;
10658 struct rb_node *node, *next;
10659
10660 spin_lock(&fs_info->swapfile_pins_lock);
10661 node = rb_first(&fs_info->swapfile_pins);
10662 while (node) {
10663 next = rb_next(node);
10664 sp = rb_entry(node, struct btrfs_swapfile_pin, node);
10665 if (sp->inode == inode) {
10666 rb_erase(&sp->node, &fs_info->swapfile_pins);
10667 if (sp->is_block_group)
10668 btrfs_put_block_group(sp->ptr);
10669 kfree(sp);
10670 }
10671 node = next;
10672 }
10673 spin_unlock(&fs_info->swapfile_pins_lock);
10674}
10675
10676struct btrfs_swap_info {
10677 u64 start;
10678 u64 block_start;
10679 u64 block_len;
10680 u64 lowest_ppage;
10681 u64 highest_ppage;
10682 unsigned long nr_pages;
10683 int nr_extents;
10684};
10685
10686static int btrfs_add_swap_extent(struct swap_info_struct *sis,
10687 struct btrfs_swap_info *bsi)
10688{
10689 unsigned long nr_pages;
10690 u64 first_ppage, first_ppage_reported, next_ppage;
10691 int ret;
10692
10693 first_ppage = ALIGN(bsi->block_start, PAGE_SIZE) >> PAGE_SHIFT;
10694 next_ppage = ALIGN_DOWN(bsi->block_start + bsi->block_len,
10695 PAGE_SIZE) >> PAGE_SHIFT;
10696
10697 if (first_ppage >= next_ppage)
10698 return 0;
10699 nr_pages = next_ppage - first_ppage;
10700
10701 first_ppage_reported = first_ppage;
10702 if (bsi->start == 0)
10703 first_ppage_reported++;
10704 if (bsi->lowest_ppage > first_ppage_reported)
10705 bsi->lowest_ppage = first_ppage_reported;
10706 if (bsi->highest_ppage < (next_ppage - 1))
10707 bsi->highest_ppage = next_ppage - 1;
10708
10709 ret = add_swap_extent(sis, bsi->nr_pages, nr_pages, first_ppage);
10710 if (ret < 0)
10711 return ret;
10712 bsi->nr_extents += ret;
10713 bsi->nr_pages += nr_pages;
10714 return 0;
10715}
10716
10717static void btrfs_swap_deactivate(struct file *file)
10718{
10719 struct inode *inode = file_inode(file);
10720
10721 btrfs_free_swapfile_pins(inode);
10722 atomic_dec(&BTRFS_I(inode)->root->nr_swapfiles);
10723}
10724
10725static int btrfs_swap_activate(struct swap_info_struct *sis, struct file *file,
10726 sector_t *span)
10727{
10728 struct inode *inode = file_inode(file);
10729 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
10730 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
10731 struct extent_state *cached_state = NULL;
10732 struct extent_map *em = NULL;
10733 struct btrfs_device *device = NULL;
10734 struct btrfs_swap_info bsi = {
10735 .lowest_ppage = (sector_t)-1ULL,
10736 };
10737 int ret = 0;
10738 u64 isize;
10739 u64 start;
10740
10741 /*
10742 * If the swap file was just created, make sure delalloc is done. If the
10743 * file changes again after this, the user is doing something stupid and
10744 * we don't really care.
10745 */
10746 ret = btrfs_wait_ordered_range(inode, 0, (u64)-1);
10747 if (ret)
10748 return ret;
10749
10750 /*
10751 * The inode is locked, so these flags won't change after we check them.
10752 */
10753 if (BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS) {
10754 btrfs_warn(fs_info, "swapfile must not be compressed");
10755 return -EINVAL;
10756 }
10757 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW)) {
10758 btrfs_warn(fs_info, "swapfile must not be copy-on-write");
10759 return -EINVAL;
10760 }
10761 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
10762 btrfs_warn(fs_info, "swapfile must not be checksummed");
10763 return -EINVAL;
10764 }
10765
10766 /*
10767 * Balance or device remove/replace/resize can move stuff around from
10768 * under us. The EXCL_OP flag makes sure they aren't running/won't run
10769 * concurrently while we are mapping the swap extents, and
10770 * fs_info->swapfile_pins prevents them from running while the swap file
10771 * is active and moving the extents. Note that this also prevents a
10772 * concurrent device add which isn't actually necessary, but it's not
10773 * really worth the trouble to allow it.
10774 */
10775 if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) {
10776 btrfs_warn(fs_info,
10777 "cannot activate swapfile while exclusive operation is running");
10778 return -EBUSY;
10779 }
10780 /*
10781 * Snapshots can create extents which require COW even if NODATACOW is
10782 * set. We use this counter to prevent snapshots. We must increment it
10783 * before walking the extents because we don't want a concurrent
10784 * snapshot to run after we've already checked the extents.
10785 */
10786 atomic_inc(&BTRFS_I(inode)->root->nr_swapfiles);
10787
10788 isize = ALIGN_DOWN(inode->i_size, fs_info->sectorsize);
10789
10790 lock_extent_bits(io_tree, 0, isize - 1, &cached_state);
10791 start = 0;
10792 while (start < isize) {
10793 u64 logical_block_start, physical_block_start;
10794 struct btrfs_block_group_cache *bg;
10795 u64 len = isize - start;
10796
10797 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len, 0);
10798 if (IS_ERR(em)) {
10799 ret = PTR_ERR(em);
10800 goto out;
10801 }
10802
10803 if (em->block_start == EXTENT_MAP_HOLE) {
10804 btrfs_warn(fs_info, "swapfile must not have holes");
10805 ret = -EINVAL;
10806 goto out;
10807 }
10808 if (em->block_start == EXTENT_MAP_INLINE) {
10809 /*
10810 * It's unlikely we'll ever actually find ourselves
10811 * here, as a file small enough to fit inline won't be
10812 * big enough to store more than the swap header, but in
10813 * case something changes in the future, let's catch it
10814 * here rather than later.
10815 */
10816 btrfs_warn(fs_info, "swapfile must not be inline");
10817 ret = -EINVAL;
10818 goto out;
10819 }
10820 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
10821 btrfs_warn(fs_info, "swapfile must not be compressed");
10822 ret = -EINVAL;
10823 goto out;
10824 }
10825
10826 logical_block_start = em->block_start + (start - em->start);
10827 len = min(len, em->len - (start - em->start));
10828 free_extent_map(em);
10829 em = NULL;
10830
10831 ret = can_nocow_extent(inode, start, &len, NULL, NULL, NULL);
10832 if (ret < 0) {
10833 goto out;
10834 } else if (ret) {
10835 ret = 0;
10836 } else {
10837 btrfs_warn(fs_info,
10838 "swapfile must not be copy-on-write");
10839 ret = -EINVAL;
10840 goto out;
10841 }
10842
10843 em = btrfs_get_chunk_map(fs_info, logical_block_start, len);
10844 if (IS_ERR(em)) {
10845 ret = PTR_ERR(em);
10846 goto out;
10847 }
10848
10849 if (em->map_lookup->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
10850 btrfs_warn(fs_info,
10851 "swapfile must have single data profile");
10852 ret = -EINVAL;
10853 goto out;
10854 }
10855
10856 if (device == NULL) {
10857 device = em->map_lookup->stripes[0].dev;
10858 ret = btrfs_add_swapfile_pin(inode, device, false);
10859 if (ret == 1)
10860 ret = 0;
10861 else if (ret)
10862 goto out;
10863 } else if (device != em->map_lookup->stripes[0].dev) {
10864 btrfs_warn(fs_info, "swapfile must be on one device");
10865 ret = -EINVAL;
10866 goto out;
10867 }
10868
10869 physical_block_start = (em->map_lookup->stripes[0].physical +
10870 (logical_block_start - em->start));
10871 len = min(len, em->len - (logical_block_start - em->start));
10872 free_extent_map(em);
10873 em = NULL;
10874
10875 bg = btrfs_lookup_block_group(fs_info, logical_block_start);
10876 if (!bg) {
10877 btrfs_warn(fs_info,
10878 "could not find block group containing swapfile");
10879 ret = -EINVAL;
10880 goto out;
10881 }
10882
10883 ret = btrfs_add_swapfile_pin(inode, bg, true);
10884 if (ret) {
10885 btrfs_put_block_group(bg);
10886 if (ret == 1)
10887 ret = 0;
10888 else
10889 goto out;
10890 }
10891
10892 if (bsi.block_len &&
10893 bsi.block_start + bsi.block_len == physical_block_start) {
10894 bsi.block_len += len;
10895 } else {
10896 if (bsi.block_len) {
10897 ret = btrfs_add_swap_extent(sis, &bsi);
10898 if (ret)
10899 goto out;
10900 }
10901 bsi.start = start;
10902 bsi.block_start = physical_block_start;
10903 bsi.block_len = len;
10904 }
10905
10906 start += len;
10907 }
10908
10909 if (bsi.block_len)
10910 ret = btrfs_add_swap_extent(sis, &bsi);
10911
10912out:
10913 if (!IS_ERR_OR_NULL(em))
10914 free_extent_map(em);
10915
10916 unlock_extent_cached(io_tree, 0, isize - 1, &cached_state);
10917
10918 if (ret)
10919 btrfs_swap_deactivate(file);
10920
10921 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
10922
10923 if (ret)
10924 return ret;
10925
10926 if (device)
10927 sis->bdev = device->bdev;
10928 *span = bsi.highest_ppage - bsi.lowest_ppage + 1;
10929 sis->max = bsi.nr_pages;
10930 sis->pages = bsi.nr_pages - 1;
10931 sis->highest_bit = bsi.nr_pages - 1;
10932 return bsi.nr_extents;
10933}
10934#else
10935static void btrfs_swap_deactivate(struct file *file)
10936{
10937}
10938
10939static int btrfs_swap_activate(struct swap_info_struct *sis, struct file *file,
10940 sector_t *span)
10941{
10942 return -EOPNOTSUPP;
10943}
10944#endif
10945
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010946static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -050010947 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -040010948 .lookup = btrfs_lookup,
10949 .create = btrfs_create,
10950 .unlink = btrfs_unlink,
10951 .link = btrfs_link,
10952 .mkdir = btrfs_mkdir,
10953 .rmdir = btrfs_rmdir,
Miklos Szeredi2773bf02016-09-27 11:03:58 +020010954 .rename = btrfs_rename2,
Chris Mason39279cc2007-06-12 06:35:45 -040010955 .symlink = btrfs_symlink,
10956 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -040010957 .mknod = btrfs_mknod,
Josef Bacik5103e942007-11-16 11:45:54 -050010958 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010959 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010960 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010961 .set_acl = btrfs_set_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070010962 .update_time = btrfs_update_time,
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010963 .tmpfile = btrfs_tmpfile,
Chris Mason39279cc2007-06-12 06:35:45 -040010964};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010965static const struct inode_operations btrfs_dir_ro_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010966 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -050010967 .permission = btrfs_permission,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070010968 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010969};
Yan, Zheng76dda932009-09-21 16:00:26 -040010970
Alexey Dobriyan828c0952009-10-01 15:43:56 -070010971static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010972 .llseek = generic_file_llseek,
10973 .read = generic_read_dir,
Omar Sandoval02dbfc92016-05-20 13:50:33 -070010974 .iterate_shared = btrfs_real_readdir,
Josef Bacik23b5ec72017-07-24 15:14:25 -040010975 .open = btrfs_opendir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -040010976 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010977#ifdef CONFIG_COMPAT
Luke Dashjr4c63c242015-10-29 08:22:21 +000010978 .compat_ioctl = btrfs_compat_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010979#endif
Sage Weil6bf13c02008-06-10 10:07:39 -040010980 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -040010981 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -040010982};
10983
David Sterba20e55062015-11-19 11:42:28 +010010984static const struct extent_io_ops btrfs_extent_io_ops = {
David Sterba4d53ddd2017-02-17 15:27:44 +010010985 /* mandatory callbacks */
Chris Mason065631f2008-02-20 12:07:25 -050010986 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -040010987 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
10988};
10989
Chris Mason35054392009-01-21 13:11:13 -050010990/*
10991 * btrfs doesn't support the bmap operation because swapfiles
10992 * use bmap to make a mapping of extents in the file. They assume
10993 * these extents won't change over the life of the file and they
10994 * use the bmap result to do IO directly to the drive.
10995 *
10996 * the btrfs bmap call would return logical addresses that aren't
10997 * suitable for IO and they also will change frequently as COW
10998 * operations happen. So, swapfile + btrfs == corruption.
10999 *
11000 * For now we're avoiding this by dropping bmap.
11001 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -070011002static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -040011003 .readpage = btrfs_readpage,
11004 .writepage = btrfs_writepage,
Chris Masonb293f02e2007-11-01 19:45:34 -040011005 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -050011006 .readpages = btrfs_readpages,
Chris Mason16432982008-04-10 10:23:21 -040011007 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -040011008 .invalidatepage = btrfs_invalidatepage,
11009 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -040011010 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +020011011 .error_remove_page = generic_error_remove_page,
Omar Sandovaled46ff32016-11-03 10:28:14 -070011012 .swap_activate = btrfs_swap_activate,
11013 .swap_deactivate = btrfs_swap_deactivate,
Chris Mason39279cc2007-06-12 06:35:45 -040011014};
11015
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070011016static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040011017 .getattr = btrfs_getattr,
11018 .setattr = btrfs_setattr,
Josef Bacik5103e942007-11-16 11:45:54 -050011019 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050011020 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -050011021 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020011022 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080011023 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040011024 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040011025};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070011026static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -040011027 .getattr = btrfs_getattr,
11028 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050011029 .permission = btrfs_permission,
Josef Bacik33268ea2008-07-24 12:16:36 -040011030 .listxattr = btrfs_listxattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020011031 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080011032 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040011033 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -040011034};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070011035static const struct inode_operations btrfs_symlink_inode_operations = {
Al Viro6b255392015-11-17 10:20:54 -050011036 .get_link = page_get_link,
Li Zefanf2095612010-11-19 02:05:24 +000011037 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -050011038 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050011039 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -050011040 .listxattr = btrfs_listxattr,
Josef Bacike41f9412012-03-26 09:46:47 -040011041 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040011042};
Yan, Zheng76dda932009-09-21 16:00:26 -040011043
Alexey Dobriyan82d339d2009-10-09 09:54:36 -040011044const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -040011045 .d_delete = btrfs_dentry_delete,
11046};